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