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.
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.
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.
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.
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.
26 * Sun Microsystems, Inc.
28 * Mountain View, California 94043
33 static char sccsid[] = "@(#)update.c 1.2 91/03/11 Copyr 1986 Sun Micro";
35 static const char rcsid[] =
36 "$FreeBSD: src/usr.sbin/rpc.ypupdated/update.c,v 1.4.2.1 2002/02/15 00:46:57 des Exp $";
40 * Copyright (C) 1986, 1989, Sun Microsystems, Inc.
44 * Administrative tool to add a new user to the publickey database
50 #include <rpc/key_prot.h>
52 #include <rpcsvc/yp_prot.h>
53 #include <rpcsvc/ypclnt.h>
59 #include <sys/resource.h>
61 #include "ypupdated_extern.h"
64 #define MAXMAPNAMELEN 256
66 #define YPOP_CHANGE 1 /* change, do not add */
67 #define YPOP_INSERT 2 /* add, do not change */
68 #define YPOP_DELETE 3 /* delete this entry */
69 #define YPOP_STORE 4 /* add, or change */
73 static char SHELL[] = "/bin/sh";
74 static char YPDBPATH[]="/var/yp"; /* This is defined but not used! */
75 static char PKMAP[] = "publickey.byname";
76 static char UPDATEFILE[] = "updaters";
77 static char PKFILE[] = "/etc/publickey";
81 static int _openchild(char *, FILE **, FILE **);
84 * Determine if requester is allowed to update the given map,
85 * and update it if so. Returns the yp status, which is zero
86 * if there is no access violation.
88 mapupdate(requester, mapname, op, keylen, key, datalen, data)
97 char updater[MAXMAPNAMELEN + 40];
110 printf("%s %s\n", key, data);
112 (void)sprintf(updater, "make -s -f %s/%s %s", YPDBPATH, /* !!! */
113 UPDATEFILE, mapname);
114 pid = _openchild(updater, &childargs, &childrslt);
116 return (YPERR_YPERR);
122 (void)fprintf(childargs, "%s\n", requester);
123 (void)fprintf(childargs, "%u\n", op);
124 (void)fprintf(childargs, "%u\n", keylen);
125 (void)fwrite(key, (int)keylen, 1, childargs);
126 (void)fprintf(childargs, "\n");
127 (void)fprintf(childargs, "%u\n", datalen);
128 (void)fwrite(data, (int)datalen, 1, childargs);
129 (void)fprintf(childargs, "\n");
130 (void)fclose(childargs);
135 (void)fscanf(childrslt, "%d", &yperrno);
136 (void)fclose(childrslt);
140 if (WEXITSTATUS(status) != 0) {
142 if (status.w_retcode != 0) {
144 return (YPERR_YPERR);
150 * returns pid, or -1 for failure
153 _openchild(command, fto, ffrom)
165 if (pipe(pdto) < 0) {
168 if (pipe(pdfrom) < 0) {
172 switch (pid = vfork()) {
174 switch (pid = fork()) {
181 * child: read from pdto[0], write into pdfrom[1]
186 (void)dup(pdfrom[1]);
187 getrlimit(RLIMIT_NOFILE, &rl);
188 for (i = rl.rlim_max - 1; i >= 3; i--) {
191 com = malloc((unsigned) strlen(command) + 6);
195 (void)sprintf(com, "exec %s", command);
196 execl(SHELL, basename(SHELL), "-c", com, NULL);
201 * parent: write into pdto[1], read from pdfrom[0]
203 *fto = fdopen(pdto[1], "w");
204 (void)close(pdto[0]);
205 *ffrom = fdopen(pdfrom[0], "r");
206 (void)close(pdfrom[1]);
212 * error cleanup and return
215 (void)close(pdfrom[0]);
216 (void)close(pdfrom[1]);
218 (void)close(pdto[0]);
219 (void)close(pdto[1]);
230 p = strrchr(path, '/');
247 extern char *malloc();
250 static int match(char *, char *);
253 * Determine if requester is allowed to update the given map,
254 * and update it if so. Returns the status, which is zero
255 * if there is no access violation. This function updates
256 * the local file and then shuts up.
259 localupdate(name, filename, op, keylen, key, datalen, data)
260 char *name; /* Name of the requestor */
263 u_int keylen; /* Not used */
265 u_int datalen; /* Not used */
277 if (strcmp(name, key) != 0) {
280 if (strcmp(name, "nobody") == 0) {
282 * Can't change "nobody"s key.
290 tmpname = malloc(strlen(filename) + 4);
291 if (tmpname == NULL) {
294 sprintf(tmpname, "%s.tmp", filename);
295 rf = fopen(filename, "r");
299 wf = fopen(tmpname, "w");
304 while (fgets(line, sizeof (line), rf)) {
305 if (err < 0 && match(line, name)) {
312 fprintf(wf, "%s %s\n", key, data);
333 fprintf(wf, "%s %s\n", key, data);
340 if (rename(tmpname, filename) < 0) {
344 if (unlink(tmpname) < 0) {
359 return (strncmp(line, name, len) == 0 &&
360 (line[len] == ' ' || line[len] == '\t'));