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
32 static char sccsid[] = "@(#)update.c 1.2 91/03/11 Copyr 1986 Sun Micro";
34 static const char rcsid[] =
35 "$FreeBSD: src/usr.bin/newkey/update.c,v 1.5 1999/08/28 01:04:35 peter Exp $";
39 * Copyright (C) 1986, 1989, Sun Microsystems, Inc.
43 * Administrative tool to add a new user to the publickey database
48 #include <rpc/key_prot.h>
50 #include <rpcsvc/yp_prot.h>
51 #include <rpcsvc/ypclnt.h>
58 #include <sys/resource.h>
61 #define MAXMAPNAMELEN 256
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 */
70 static char *basename();
71 static char SHELL[] = "/bin/sh";
72 static char YPDBPATH[]="/var/yp"; /* This is defined but not used! */
73 static char PKMAP[] = "publickey.byname";
74 static char UPDATEFILE[] = "updaters";
76 static char PKFILE[] = "/etc/publickey";
80 static int _openchild __P(( char *, FILE **, FILE ** ));
83 * Determine if requester is allowed to update the given map,
84 * and update it if so. Returns the yp status, which is zero
85 * if there is no access violation.
87 mapupdate(requester, mapname, op, keylen, key, datalen, data)
96 char updater[MAXMAPNAMELEN + 40];
109 printf("%s %s\n", key, data);
111 (void)sprintf(updater, "make -s -f %s/%s %s", YPDBPATH, /* !!! */
112 UPDATEFILE, mapname);
113 pid = _openchild(updater, &childargs, &childrslt);
115 return (YPERR_YPERR);
121 (void)fprintf(childargs, "%s\n", requester);
122 (void)fprintf(childargs, "%u\n", op);
123 (void)fprintf(childargs, "%u\n", keylen);
124 (void)fwrite(key, (int)keylen, 1, childargs);
125 (void)fprintf(childargs, "\n");
126 (void)fprintf(childargs, "%u\n", datalen);
127 (void)fwrite(data, (int)datalen, 1, childargs);
128 (void)fprintf(childargs, "\n");
129 (void)fclose(childargs);
134 (void)fscanf(childrslt, "%d", &yperrno);
135 (void)fclose(childrslt);
139 if (WEXITSTATUS(status) != 0) {
141 if (status.w_retcode != 0) {
143 return (YPERR_YPERR);
149 * returns pid, or -1 for failure
152 _openchild(command, fto, ffrom)
164 if (pipe(pdto) < 0) {
167 if (pipe(pdfrom) < 0) {
170 switch (pid = fork()) {
176 * child: read from pdto[0], write into pdfrom[1]
181 (void)dup(pdfrom[1]);
182 getrlimit(RLIMIT_NOFILE, &rl);
183 for (i = rl.rlim_max - 1; i >= 3; i--) {
186 com = malloc((unsigned) strlen(command) + 6);
190 (void)sprintf(com, "exec %s", command);
191 execl(SHELL, basename(SHELL), "-c", com, NULL);
196 * parent: write into pdto[1], read from pdfrom[0]
198 *fto = fdopen(pdto[1], "w");
199 (void)close(pdto[0]);
200 *ffrom = fdopen(pdfrom[0], "r");
201 (void)close(pdfrom[1]);
207 * error cleanup and return
210 (void)close(pdfrom[0]);
211 (void)close(pdfrom[1]);
213 (void)close(pdto[0]);
214 (void)close(pdto[1]);
225 p = strrchr(path, '/');
242 static int match __P(( char * , char * ));
245 * Determine if requester is allowed to update the given map,
246 * and update it if so. Returns the status, which is zero
247 * if there is no access violation. This function updates
248 * the local file and then shuts up.
250 localupdate(name, filename, op, keylen, key, datalen, data)
251 char *name; /* Name of the requestor */
254 u_int keylen; /* Not used */
256 u_int datalen; /* Not used */
268 if (strcmp(name, key) != 0) {
271 if (strcmp(name, "nobody") == 0) {
273 * Can't change "nobody"s key.
281 tmpname = malloc(strlen(filename) + 4);
282 if (tmpname == NULL) {
285 sprintf(tmpname, "%s.tmp", filename);
286 rf = fopen(filename, "r");
290 wf = fopen(tmpname, "w");
295 while (fgets(line, sizeof (line), rf)) {
296 if (err < 0 && match(line, name)) {
303 fprintf(wf, "%s %s\n", key, data);
324 fprintf(wf, "%s %s\n", key, data);
331 if (rename(tmpname, filename) < 0) {
335 if (unlink(tmpname) < 0) {
350 return (strncmp(line, name, len) == 0 &&
351 (line[len] == ' ' || line[len] == '\t'));