1 /* 2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 3 * unrestricted use provided that this legend is included on all tape 4 * media and as a part of the software program in whole or part. Users 5 * may copy or modify Sun RPC without charge, but are not authorized 6 * to license or distribute it to anyone else except as part of a product or 7 * program developed by the user or with the express written consent of 8 * Sun Microsystems, Inc. 9 * 10 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 11 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 12 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 13 * 14 * Sun RPC is provided with no support and without any obligation on the 15 * part of Sun Microsystems, Inc. to assist in its use, correction, 16 * modification or enhancement. 17 * 18 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 19 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 20 * OR ANY PART THEREOF. 21 * 22 * In no event will Sun Microsystems, Inc. be liable for any lost revenue 23 * or profits or other special, indirect and consequential damages, even if 24 * Sun has been advised of the possibility of such damages. 25 * 26 * Sun Microsystems, Inc. 27 * 2550 Garcia Avenue 28 * Mountain View, California 94043 29 */ 30 31 #ifndef lint 32 #endif 33 34 /* 35 * Copyright (C) 1986, 1989, Sun Microsystems, Inc. 36 */ 37 38 /* 39 * Administrative tool to add a new user to the publickey database 40 */ 41 #include <sys/cdefs.h> 42 #include <sys/types.h> 43 #include <sys/time.h> 44 #include <sys/resource.h> 45 46 #include <rpc/rpc.h> 47 #include <rpc/key_prot.h> 48 49 #ifdef YP 50 #include <sys/wait.h> 51 #include <rpcsvc/yp_prot.h> 52 #include <rpcsvc/ypclnt.h> 53 #include <netdb.h> 54 #endif /* YP */ 55 56 #include <pwd.h> 57 #include <stdio.h> 58 #include <stdlib.h> 59 #include <string.h> 60 #include <unistd.h> 61 62 #include "extern.h" 63 64 #ifdef YP 65 static char SHELL[] = "/bin/sh"; 66 static char YPDBPATH[]="/var/yp"; /* This is defined but not used! */ 67 static char UPDATEFILE[] = "updaters"; 68 69 static int _openchild(char *, FILE **, FILE **); 70 static char *basename(char *path); 71 72 /* 73 * Determine if requester is allowed to update the given map, 74 * and update it if so. Returns the yp status, which is zero 75 * if there is no access violation. 76 */ 77 int 78 mapupdate(char *requester, char *mapname, u_int op, u_int keylen, 79 char *key, u_int datalen, char *data) 80 { 81 char updater[MAXMAPNAMELEN + 40]; 82 FILE *childargs; 83 FILE *childrslt; 84 #ifdef WEXITSTATUS 85 int status; 86 #else 87 union wait status; 88 #endif 89 pid_t pid; 90 u_int yperrno; 91 92 93 #ifdef DEBUG 94 printf("%s %s\n", key, data); 95 #endif 96 (void)sprintf(updater, "make -s -f %s/%s %s", YPDBPATH, /* !!! */ 97 UPDATEFILE, mapname); 98 pid = _openchild(updater, &childargs, &childrslt); 99 if (pid < 0) { 100 return (YPERR_YPERR); 101 } 102 103 /* 104 * Write to child 105 */ 106 (void)fprintf(childargs, "%s\n", requester); 107 (void)fprintf(childargs, "%u\n", op); 108 (void)fprintf(childargs, "%u\n", keylen); 109 (void)fwrite(key, (int)keylen, 1, childargs); 110 (void)fprintf(childargs, "\n"); 111 (void)fprintf(childargs, "%u\n", datalen); 112 (void)fwrite(data, (int)datalen, 1, childargs); 113 (void)fprintf(childargs, "\n"); 114 (void)fclose(childargs); 115 116 /* 117 * Read from child 118 */ 119 (void)fscanf(childrslt, "%d", &yperrno); 120 (void)fclose(childrslt); 121 122 (void)wait(&status); 123 #ifdef WEXITSTATUS 124 if (WEXITSTATUS(status) != 0) { 125 #else 126 if (status.w_retcode != 0) { 127 #endif 128 return (YPERR_YPERR); 129 } 130 return (yperrno); 131 } 132 133 /* 134 * returns pid, or -1 for failure 135 */ 136 static pid_t 137 _openchild(char *command, FILE **fto, FILE **ffrom) 138 { 139 int i; 140 pid_t pid; 141 int pdto[2]; 142 int pdfrom[2]; 143 char *com; 144 struct rlimit rl; 145 146 if (pipe(pdto) < 0) { 147 goto error1; 148 } 149 if (pipe(pdfrom) < 0) { 150 goto error2; 151 } 152 switch (pid = fork()) { 153 case -1: 154 goto error3; 155 156 case 0: 157 /* 158 * child: read from pdto[0], write into pdfrom[1] 159 */ 160 (void)close(0); 161 (void)dup(pdto[0]); 162 (void)close(1); 163 (void)dup(pdfrom[1]); 164 getrlimit(RLIMIT_NOFILE, &rl); 165 for (i = rl.rlim_max - 1; i >= 3; i--) { 166 (void) close(i); 167 } 168 com = malloc((unsigned) strlen(command) + 6); 169 if (com == NULL) { 170 _exit(~0); 171 } 172 (void)sprintf(com, "exec %s", command); 173 execl(SHELL, basename(SHELL), "-c", com, (char *)NULL); 174 _exit(~0); 175 176 default: 177 /* 178 * parent: write into pdto[1], read from pdfrom[0] 179 */ 180 *fto = fdopen(pdto[1], "w"); 181 (void)close(pdto[0]); 182 *ffrom = fdopen(pdfrom[0], "r"); 183 (void)close(pdfrom[1]); 184 break; 185 } 186 return (pid); 187 188 /* 189 * error cleanup and return 190 */ 191 error3: 192 (void)close(pdfrom[0]); 193 (void)close(pdfrom[1]); 194 error2: 195 (void)close(pdto[0]); 196 (void)close(pdto[1]); 197 error1: 198 return (-1); 199 } 200 201 static char * 202 basename(char *path) 203 { 204 char *p; 205 206 p = strrchr(path, '/'); 207 if (p == NULL) { 208 return (path); 209 } else { 210 return (p + 1); 211 } 212 } 213 214 #else /* YP */ 215 216 #define ERR_ACCESS 1 217 #define ERR_MALLOC 2 218 #define ERR_READ 3 219 #define ERR_WRITE 4 220 #define ERR_DBASE 5 221 #define ERR_KEY 6 222 223 static int match(char *, char *); 224 225 /* 226 * Determine if requester is allowed to update the given map, 227 * and update it if so. Returns the status, which is zero 228 * if there is no access violation. This function updates 229 * the local file and then shuts up. 230 */ 231 int 232 localupdate(char *name, char *filename, u_int op, u_int keylen __unused, 233 char *key, u_int datalen __unused, char *data) 234 { 235 char line[256]; 236 FILE *rf; 237 FILE *wf; 238 char *tmpname; 239 int err; 240 241 /* 242 * Check permission 243 */ 244 if (strcmp(name, key) != 0) { 245 return (ERR_ACCESS); 246 } 247 if (strcmp(name, "nobody") == 0) { 248 /* 249 * Can't change "nobody"s key. 250 */ 251 return (ERR_ACCESS); 252 } 253 254 /* 255 * Open files 256 */ 257 tmpname = malloc(strlen(filename) + 4); 258 if (tmpname == NULL) { 259 return (ERR_MALLOC); 260 } 261 sprintf(tmpname, "%s.tmp", filename); 262 rf = fopen(filename, "r"); 263 if (rf == NULL) { 264 err = ERR_READ; 265 goto cleanup; 266 } 267 wf = fopen(tmpname, "w"); 268 if (wf == NULL) { 269 fclose(rf); 270 err = ERR_WRITE; 271 goto cleanup; 272 } 273 err = -1; 274 while (fgets(line, sizeof (line), rf)) { 275 if (err < 0 && match(line, name)) { 276 switch (op) { 277 case YPOP_INSERT: 278 err = ERR_KEY; 279 break; 280 case YPOP_STORE: 281 case YPOP_CHANGE: 282 fprintf(wf, "%s %s\n", key, data); 283 err = 0; 284 break; 285 case YPOP_DELETE: 286 /* do nothing */ 287 err = 0; 288 break; 289 } 290 } else { 291 fputs(line, wf); 292 } 293 } 294 if (err < 0) { 295 switch (op) { 296 case YPOP_CHANGE: 297 case YPOP_DELETE: 298 err = ERR_KEY; 299 break; 300 case YPOP_INSERT: 301 case YPOP_STORE: 302 err = 0; 303 fprintf(wf, "%s %s\n", key, data); 304 break; 305 } 306 } 307 fclose(wf); 308 fclose(rf); 309 if (err == 0) { 310 if (rename(tmpname, filename) < 0) { 311 err = ERR_DBASE; 312 goto cleanup; 313 } 314 } else { 315 if (unlink(tmpname) < 0) { 316 err = ERR_DBASE; 317 goto cleanup; 318 } 319 } 320 321 cleanup: 322 free(tmpname); 323 return (err); 324 } 325 326 static int 327 match(char *line, char *name) 328 { 329 int len; 330 331 len = strlen(name); 332 return (strncmp(line, name, len) == 0 && 333 (line[len] == ' ' || line[len] == '\t')); 334 } 335 #endif /* !YP */ 336