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";
38 * Copyright (C) 1986, 1989, Sun Microsystems, Inc.
42 * Administrative tool to add a new user to the publickey database
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
47 #include <sys/types.h>
49 #include <sys/resource.h>
52 #include <rpc/key_prot.h>
56 #include <rpcsvc/yp_prot.h>
57 #include <rpcsvc/ypclnt.h>
70 static char SHELL[] = "/bin/sh";
71 static char YPDBPATH[]="/var/yp"; /* This is defined but not used! */
72 static char UPDATEFILE[] = "updaters";
74 static int _openchild(char *, FILE **, FILE **);
75 static char *basename(char *path);
78 * Determine if requester is allowed to update the given map,
79 * and update it if so. Returns the yp status, which is zero
80 * if there is no access violation.
83 mapupdate(char *requester, char *mapname, u_int op, u_int keylen,
84 char *key, u_int datalen, char *data)
86 char updater[MAXMAPNAMELEN + 40];
99 printf("%s %s\n", key, data);
101 (void)sprintf(updater, "make -s -f %s/%s %s", YPDBPATH, /* !!! */
102 UPDATEFILE, mapname);
103 pid = _openchild(updater, &childargs, &childrslt);
105 return (YPERR_YPERR);
111 (void)fprintf(childargs, "%s\n", requester);
112 (void)fprintf(childargs, "%u\n", op);
113 (void)fprintf(childargs, "%u\n", keylen);
114 (void)fwrite(key, (int)keylen, 1, childargs);
115 (void)fprintf(childargs, "\n");
116 (void)fprintf(childargs, "%u\n", datalen);
117 (void)fwrite(data, (int)datalen, 1, childargs);
118 (void)fprintf(childargs, "\n");
119 (void)fclose(childargs);
124 (void)fscanf(childrslt, "%d", &yperrno);
125 (void)fclose(childrslt);
129 if (WEXITSTATUS(status) != 0) {
131 if (status.w_retcode != 0) {
133 return (YPERR_YPERR);
139 * returns pid, or -1 for failure
142 _openchild(char *command, FILE **fto, FILE **ffrom)
151 if (pipe(pdto) < 0) {
154 if (pipe(pdfrom) < 0) {
157 switch (pid = fork()) {
163 * child: read from pdto[0], write into pdfrom[1]
168 (void)dup(pdfrom[1]);
169 getrlimit(RLIMIT_NOFILE, &rl);
170 for (i = rl.rlim_max - 1; i >= 3; i--) {
173 com = malloc((unsigned) strlen(command) + 6);
177 (void)sprintf(com, "exec %s", command);
178 execl(SHELL, basename(SHELL), "-c", com, (char *)NULL);
183 * parent: write into pdto[1], read from pdfrom[0]
185 *fto = fdopen(pdto[1], "w");
186 (void)close(pdto[0]);
187 *ffrom = fdopen(pdfrom[0], "r");
188 (void)close(pdfrom[1]);
194 * error cleanup and return
197 (void)close(pdfrom[0]);
198 (void)close(pdfrom[1]);
200 (void)close(pdto[0]);
201 (void)close(pdto[1]);
211 p = strrchr(path, '/');
228 static int match(char *, char *);
231 * Determine if requester is allowed to update the given map,
232 * and update it if so. Returns the status, which is zero
233 * if there is no access violation. This function updates
234 * the local file and then shuts up.
237 localupdate(char *name, char *filename, u_int op, u_int keylen __unused,
238 char *key, u_int datalen __unused, char *data)
249 if (strcmp(name, key) != 0) {
252 if (strcmp(name, "nobody") == 0) {
254 * Can't change "nobody"s key.
262 tmpname = malloc(strlen(filename) + 4);
263 if (tmpname == NULL) {
266 sprintf(tmpname, "%s.tmp", filename);
267 rf = fopen(filename, "r");
272 wf = fopen(tmpname, "w");
279 while (fgets(line, sizeof (line), rf)) {
280 if (err < 0 && match(line, name)) {
287 fprintf(wf, "%s %s\n", key, data);
308 fprintf(wf, "%s %s\n", key, data);
315 if (rename(tmpname, filename) < 0) {
320 if (unlink(tmpname) < 0) {
332 match(char *line, char *name)
337 return (strncmp(line, name, len) == 0 &&
338 (line[len] == ' ' || line[len] == '\t'));