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 /* 32 * Copyright (C) 1986, 1989, Sun Microsystems, Inc. 33 */ 34 35 /* 36 * Administrative tool to add a new user to the publickey database 37 */ 38 39 #include <sys/types.h> 40 #include <sys/time.h> 41 #include <sys/resource.h> 42 43 #include <rpc/rpc.h> 44 #include <rpc/key_prot.h> 45 46 #ifdef YP 47 #include <sys/wait.h> 48 #include <rpcsvc/yp_prot.h> 49 #include <rpcsvc/ypclnt.h> 50 #include <netdb.h> 51 #endif /* YP */ 52 53 #include <pwd.h> 54 #include <stdio.h> 55 #include <stdlib.h> 56 #include <string.h> 57 #include <unistd.h> 58 59 #include "extern.h" 60 61 #ifdef YP 62 static char SHELL[] = "/bin/sh"; 63 static char YPDBPATH[]="/var/yp"; /* This is defined but not used! */ 64 static char UPDATEFILE[] = "updaters"; 65 66 static int _openchild(char *, FILE **, FILE **); 67 static char *basename(char *path); 68 69 /* 70 * Determine if requester is allowed to update the given map, 71 * and update it if so. Returns the yp status, which is zero 72 * if there is no access violation. 73 */ 74 int 75 mapupdate(char *requester, char *mapname, u_int op, u_int keylen, 76 char *key, u_int datalen, char *data) 77 { 78 char updater[MAXMAPNAMELEN + 40]; 79 FILE *childargs; 80 FILE *childrslt; 81 #ifdef WEXITSTATUS 82 int status; 83 #else 84 union wait status; 85 #endif 86 pid_t pid; 87 u_int yperrno; 88 89 90 #ifdef DEBUG 91 printf("%s %s\n", key, data); 92 #endif 93 (void)sprintf(updater, "make -s -f %s/%s %s", YPDBPATH, /* !!! */ 94 UPDATEFILE, mapname); 95 pid = _openchild(updater, &childargs, &childrslt); 96 if (pid < 0) { 97 return (YPERR_YPERR); 98 } 99 100 /* 101 * Write to child 102 */ 103 (void)fprintf(childargs, "%s\n", requester); 104 (void)fprintf(childargs, "%u\n", op); 105 (void)fprintf(childargs, "%u\n", keylen); 106 (void)fwrite(key, (int)keylen, 1, childargs); 107 (void)fprintf(childargs, "\n"); 108 (void)fprintf(childargs, "%u\n", datalen); 109 (void)fwrite(data, (int)datalen, 1, childargs); 110 (void)fprintf(childargs, "\n"); 111 (void)fclose(childargs); 112 113 /* 114 * Read from child 115 */ 116 (void)fscanf(childrslt, "%d", &yperrno); 117 (void)fclose(childrslt); 118 119 (void)wait(&status); 120 #ifdef WEXITSTATUS 121 if (WEXITSTATUS(status) != 0) { 122 #else 123 if (status.w_retcode != 0) { 124 #endif 125 return (YPERR_YPERR); 126 } 127 return (yperrno); 128 } 129 130 /* 131 * returns pid, or -1 for failure 132 */ 133 static pid_t 134 _openchild(char *command, FILE **fto, FILE **ffrom) 135 { 136 int i; 137 pid_t pid; 138 int pdto[2]; 139 int pdfrom[2]; 140 char *com; 141 struct rlimit rl; 142 143 if (pipe(pdto) < 0) { 144 goto error1; 145 } 146 if (pipe(pdfrom) < 0) { 147 goto error2; 148 } 149 switch (pid = fork()) { 150 case -1: 151 goto error3; 152 153 case 0: 154 /* 155 * child: read from pdto[0], write into pdfrom[1] 156 */ 157 (void)close(0); 158 (void)dup(pdto[0]); 159 (void)close(1); 160 (void)dup(pdfrom[1]); 161 getrlimit(RLIMIT_NOFILE, &rl); 162 for (i = rl.rlim_max - 1; i >= 3; i--) { 163 (void) close(i); 164 } 165 com = malloc((unsigned) strlen(command) + 6); 166 if (com == NULL) { 167 _exit(~0); 168 } 169 (void)sprintf(com, "exec %s", command); 170 execl(SHELL, basename(SHELL), "-c", com, (char *)NULL); 171 _exit(~0); 172 173 default: 174 /* 175 * parent: write into pdto[1], read from pdfrom[0] 176 */ 177 *fto = fdopen(pdto[1], "w"); 178 (void)close(pdto[0]); 179 *ffrom = fdopen(pdfrom[0], "r"); 180 (void)close(pdfrom[1]); 181 break; 182 } 183 return (pid); 184 185 /* 186 * error cleanup and return 187 */ 188 error3: 189 (void)close(pdfrom[0]); 190 (void)close(pdfrom[1]); 191 error2: 192 (void)close(pdto[0]); 193 (void)close(pdto[1]); 194 error1: 195 return (-1); 196 } 197 198 static char * 199 basename(char *path) 200 { 201 char *p; 202 203 p = strrchr(path, '/'); 204 if (p == NULL) { 205 return (path); 206 } else { 207 return (p + 1); 208 } 209 } 210 211 #else /* YP */ 212 213 #define ERR_ACCESS 1 214 #define ERR_MALLOC 2 215 #define ERR_READ 3 216 #define ERR_WRITE 4 217 #define ERR_DBASE 5 218 #define ERR_KEY 6 219 220 static int match(char *, char *); 221 222 /* 223 * Determine if requester is allowed to update the given map, 224 * and update it if so. Returns the status, which is zero 225 * if there is no access violation. This function updates 226 * the local file and then shuts up. 227 */ 228 int 229 localupdate(char *name, char *filename, u_int op, u_int keylen __unused, 230 char *key, u_int datalen __unused, char *data) 231 { 232 char line[256]; 233 FILE *rf; 234 FILE *wf; 235 char *tmpname; 236 int err; 237 238 /* 239 * Check permission 240 */ 241 if (strcmp(name, key) != 0) { 242 return (ERR_ACCESS); 243 } 244 if (strcmp(name, "nobody") == 0) { 245 /* 246 * Can't change "nobody"s key. 247 */ 248 return (ERR_ACCESS); 249 } 250 251 /* 252 * Open files 253 */ 254 tmpname = malloc(strlen(filename) + 4); 255 if (tmpname == NULL) { 256 return (ERR_MALLOC); 257 } 258 sprintf(tmpname, "%s.tmp", filename); 259 rf = fopen(filename, "r"); 260 if (rf == NULL) { 261 err = ERR_READ; 262 goto cleanup; 263 } 264 wf = fopen(tmpname, "w"); 265 if (wf == NULL) { 266 fclose(rf); 267 err = ERR_WRITE; 268 goto cleanup; 269 } 270 err = -1; 271 while (fgets(line, sizeof (line), rf)) { 272 if (err < 0 && match(line, name)) { 273 switch (op) { 274 case YPOP_INSERT: 275 err = ERR_KEY; 276 break; 277 case YPOP_STORE: 278 case YPOP_CHANGE: 279 fprintf(wf, "%s %s\n", key, data); 280 err = 0; 281 break; 282 case YPOP_DELETE: 283 /* do nothing */ 284 err = 0; 285 break; 286 } 287 } else { 288 fputs(line, wf); 289 } 290 } 291 if (err < 0) { 292 switch (op) { 293 case YPOP_CHANGE: 294 case YPOP_DELETE: 295 err = ERR_KEY; 296 break; 297 case YPOP_INSERT: 298 case YPOP_STORE: 299 err = 0; 300 fprintf(wf, "%s %s\n", key, data); 301 break; 302 } 303 } 304 fclose(wf); 305 fclose(rf); 306 if (err == 0) { 307 if (rename(tmpname, filename) < 0) { 308 err = ERR_DBASE; 309 goto cleanup; 310 } 311 } else { 312 if (unlink(tmpname) < 0) { 313 err = ERR_DBASE; 314 goto cleanup; 315 } 316 } 317 318 cleanup: 319 free(tmpname); 320 return (err); 321 } 322 323 static int 324 match(char *line, char *name) 325 { 326 int len; 327 328 len = strlen(name); 329 return (strncmp(line, name, len) == 0 && 330 (line[len] == ' ' || line[len] == '\t')); 331 } 332 #endif /* !YP */ 333