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 #if 0 33 static char sccsid[] = "@(#)update.c 1.2 91/03/11 Copyr 1986 Sun Micro"; 34 #endif 35 #endif /* not lint */ 36 37 /* 38 * Copyright (C) 1986, 1989, Sun Microsystems, Inc. 39 */ 40 41 /* 42 * Administrative tool to add a new user to the publickey database 43 */ 44 #include <stdio.h> 45 #include <stdlib.h> 46 #include <unistd.h> 47 #include <rpc/rpc.h> 48 #include <rpc/key_prot.h> 49 #ifdef YP 50 #include <rpcsvc/yp_prot.h> 51 #include <rpcsvc/ypclnt.h> 52 #include <sys/wait.h> 53 #include <netdb.h> 54 #endif /* YP */ 55 #include <pwd.h> 56 #include <string.h> 57 #include <sys/resource.h> 58 #include "ypupdated_extern.h" 59 60 #ifdef YP 61 #define MAXMAPNAMELEN 256 62 #else 63 #define YPOP_CHANGE 1 /* change, do not add */ 64 #define YPOP_INSERT 2 /* add, do not change */ 65 #define YPOP_DELETE 3 /* delete this entry */ 66 #define YPOP_STORE 4 /* add, or change */ 67 #endif 68 69 #ifdef YP 70 static char SHELL[] = "/bin/sh"; 71 static char YPDBPATH[]="/var/yp"; /* This is defined but not used! */ 72 static char PKMAP[] = "publickey.byname"; 73 static char UPDATEFILE[] = "updaters"; 74 static char PKFILE[] = "/etc/publickey"; 75 #endif /* YP */ 76 77 #ifdef YP 78 static int _openchild(char *, FILE **, FILE **); 79 80 /* 81 * Determine if requester is allowed to update the given map, 82 * and update it if so. Returns the yp status, which is zero 83 * if there is no access violation. 84 */ 85 int 86 mapupdate(char *requester, char *mapname, u_int op, u_int keylen, char *key, 87 u_int datalen, char *data) 88 { 89 char updater[MAXMAPNAMELEN + 40]; 90 FILE *childargs; 91 FILE *childrslt; 92 #ifdef WEXITSTATUS 93 int status; 94 #else 95 union wait status; 96 #endif 97 pid_t pid; 98 u_int yperrno; 99 100 101 #ifdef DEBUG 102 printf("%s %s\n", key, data); 103 #endif 104 (void)sprintf(updater, "make -s -f %s/%s %s", YPDBPATH, /* !!! */ 105 UPDATEFILE, mapname); 106 pid = _openchild(updater, &childargs, &childrslt); 107 if (pid < 0) { 108 return (YPERR_YPERR); 109 } 110 111 /* 112 * Write to child 113 */ 114 (void)fprintf(childargs, "%s\n", requester); 115 (void)fprintf(childargs, "%u\n", op); 116 (void)fprintf(childargs, "%u\n", keylen); 117 (void)fwrite(key, (int)keylen, 1, childargs); 118 (void)fprintf(childargs, "\n"); 119 (void)fprintf(childargs, "%u\n", datalen); 120 (void)fwrite(data, (int)datalen, 1, childargs); 121 (void)fprintf(childargs, "\n"); 122 (void)fclose(childargs); 123 124 /* 125 * Read from child 126 */ 127 (void)fscanf(childrslt, "%d", &yperrno); 128 (void)fclose(childrslt); 129 130 (void)wait(&status); 131 #ifdef WEXITSTATUS 132 if (WEXITSTATUS(status) != 0) 133 #else 134 if (status.w_retcode != 0) 135 #endif 136 return (YPERR_YPERR); 137 return (yperrno); 138 } 139 140 /* 141 * returns pid, or -1 for failure 142 */ 143 static int 144 _openchild(char *command, FILE **fto, FILE **ffrom) 145 { 146 int i; 147 pid_t pid; 148 int pdto[2]; 149 int pdfrom[2]; 150 char *com; 151 struct rlimit rl; 152 153 if (pipe(pdto) < 0) { 154 goto error1; 155 } 156 if (pipe(pdfrom) < 0) { 157 goto error2; 158 } 159 switch (pid = fork()) { 160 case -1: 161 goto error3; 162 163 case 0: 164 /* 165 * child: read from pdto[0], write into pdfrom[1] 166 */ 167 (void)close(0); 168 (void)dup(pdto[0]); 169 (void)close(1); 170 (void)dup(pdfrom[1]); 171 getrlimit(RLIMIT_NOFILE, &rl); 172 for (i = rl.rlim_max - 1; i >= 3; i--) { 173 (void) close(i); 174 } 175 com = malloc((unsigned) strlen(command) + 6); 176 if (com == NULL) { 177 _exit(~0); 178 } 179 (void)sprintf(com, "exec %s", command); 180 execl(SHELL, basename(SHELL), "-c", com, (char *)NULL); 181 _exit(~0); 182 183 default: 184 /* 185 * parent: write into pdto[1], read from pdfrom[0] 186 */ 187 *fto = fdopen(pdto[1], "w"); 188 (void)close(pdto[0]); 189 *ffrom = fdopen(pdfrom[0], "r"); 190 (void)close(pdfrom[1]); 191 break; 192 } 193 return (pid); 194 195 /* 196 * error cleanup and return 197 */ 198 error3: 199 (void)close(pdfrom[0]); 200 (void)close(pdfrom[1]); 201 error2: 202 (void)close(pdto[0]); 203 (void)close(pdto[1]); 204 error1: 205 return (-1); 206 } 207 208 static char * 209 basename(char *path) 210 { 211 char *p; 212 213 p = strrchr(path, '/'); 214 if (p == NULL) { 215 return (path); 216 } else { 217 return (p + 1); 218 } 219 } 220 221 #else /* YP */ 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 cleanup: 321 free(tmpname); 322 return (err); 323 } 324 325 static int 326 match(char *line, char *name) 327 { 328 int len; 329 330 len = strlen(name); 331 return (strncmp(line, name, len) == 0 && 332 (line[len] == ' ' || line[len] == '\t')); 333 } 334 #endif /* !YP */ 335