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
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.4 2005/11/25 00:32:49 swildner Exp $
36 * Copyright (C) 1986, 1989, Sun Microsystems, Inc.
40 * Administrative tool to add a new user to the publickey database
46 #include <rpc/key_prot.h>
48 #include <rpcsvc/yp_prot.h>
49 #include <rpcsvc/ypclnt.h>
55 #include <sys/resource.h>
57 #include "ypupdated_extern.h"
60 #define MAXMAPNAMELEN 256
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 */
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";
77 static int _openchild(char *, FILE **, FILE **);
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.
84 mapupdate(char *requester, char *mapname, u_int op, u_int keylen, char *key,
85 u_int datalen, char *data)
87 char updater[MAXMAPNAMELEN + 40];
100 printf("%s %s\n", key, data);
102 sprintf(updater, "make -s -f %s/%s %s", YPDBPATH, /* !!! */
103 UPDATEFILE, mapname);
104 pid = _openchild(updater, &childargs, &childrslt);
106 return (YPERR_YPERR);
112 fprintf(childargs, "%s\n", requester);
113 fprintf(childargs, "%u\n", op);
114 fprintf(childargs, "%u\n", keylen);
115 fwrite(key, (int)keylen, 1, childargs);
116 fprintf(childargs, "\n");
117 fprintf(childargs, "%u\n", datalen);
118 fwrite(data, (int)datalen, 1, childargs);
119 fprintf(childargs, "\n");
125 fscanf(childrslt, "%d", &yperrno);
130 if (WEXITSTATUS(status) != 0) {
132 if (status.w_retcode != 0) {
134 return (YPERR_YPERR);
140 * returns pid, or -1 for failure
143 _openchild(char *command, FILE **fto, FILE **ffrom)
152 if (pipe(pdto) < 0) {
155 if (pipe(pdfrom) < 0) {
159 switch (pid = vfork()) {
161 switch (pid = fork()) {
168 * child: read from pdto[0], write into pdfrom[1]
174 getrlimit(RLIMIT_NOFILE, &rl);
175 for (i = rl.rlim_max - 1; i >= 3; i--) {
178 com = malloc((unsigned) strlen(command) + 6);
182 sprintf(com, "exec %s", command);
183 execl(SHELL, basename(SHELL), "-c", com, NULL);
188 * parent: write into pdto[1], read from pdfrom[0]
190 *fto = fdopen(pdto[1], "w");
192 *ffrom = fdopen(pdfrom[0], "r");
199 * error cleanup and return
216 p = strrchr(path, '/');
233 extern char *malloc();
236 static int match(char *, char *);
239 * Determine if requester is allowed to update the given map,
240 * and update it if so. Returns the status, which is zero
241 * if there is no access violation. This function updates
242 * the local file and then shuts up.
245 * name: Name of the requestor
250 localupdate(char *name, char *filename, u_int op, u_int keylen, char *key,
251 u_int datalen, char *data)
262 if (strcmp(name, key) != 0) {
265 if (strcmp(name, "nobody") == 0) {
267 * Can't change "nobody"s key.
275 tmpname = malloc(strlen(filename) + 4);
276 if (tmpname == NULL) {
279 sprintf(tmpname, "%s.tmp", filename);
280 rf = fopen(filename, "r");
284 wf = fopen(tmpname, "w");
289 while (fgets(line, sizeof (line), rf)) {
290 if (err < 0 && match(line, name)) {
297 fprintf(wf, "%s %s\n", key, data);
318 fprintf(wf, "%s %s\n", key, data);
325 if (rename(tmpname, filename) < 0) {
329 if (unlink(tmpname) < 0) {
337 match(char *line, char *name)
342 return (strncmp(line, name, len) == 0 &&
343 (line[len] == ' ' || line[len] == '\t'));