Clarify that the argument to -t is in seconds.
[dragonfly/netmp.git] / usr.sbin / rpc.ypupdated / update.c
blob19d71ae467f91fee0ef77c840abefdca552543eb
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.
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.
27 * 2550 Garcia Avenue
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
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <unistd.h>
45 #include <rpc/rpc.h>
46 #include <rpc/key_prot.h>
47 #ifdef YP
48 #include <rpcsvc/yp_prot.h>
49 #include <rpcsvc/ypclnt.h>
50 #include <sys/wait.h>
51 #include <netdb.h>
52 #endif /* YP */
53 #include <pwd.h>
54 #include <string.h>
55 #include <sys/resource.h>
56 #include <stdlib.h>
57 #include "ypupdated_extern.h"
59 #ifdef YP
60 #define MAXMAPNAMELEN 256
61 #else
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 */
66 #endif
68 #ifdef YP
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";
74 #endif /* YP */
76 #ifdef YP
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];
88 FILE *childargs;
89 FILE *childrslt;
90 #ifdef WEXITSTATUS
91 int status;
92 #else
93 union wait status;
94 #endif
95 pid_t pid;
96 u_int yperrno;
99 #ifdef DEBUG
100 printf("%s %s\n", key, data);
101 #endif
102 sprintf(updater, "make -s -f %s/%s %s", YPDBPATH, /* !!! */
103 UPDATEFILE, mapname);
104 pid = _openchild(updater, &childargs, &childrslt);
105 if (pid < 0) {
106 return (YPERR_YPERR);
110 * Write to child
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");
120 fclose(childargs);
123 * Read from child
125 fscanf(childrslt, "%d", &yperrno);
126 fclose(childrslt);
128 wait(&status);
129 #ifdef WEXITSTATUS
130 if (WEXITSTATUS(status) != 0) {
131 #else
132 if (status.w_retcode != 0) {
133 #endif
134 return (YPERR_YPERR);
136 return (yperrno);
140 * returns pid, or -1 for failure
142 static
143 _openchild(char *command, FILE **fto, FILE **ffrom)
145 int i;
146 pid_t pid;
147 int pdto[2];
148 int pdfrom[2];
149 char *com;
150 struct rlimit rl;
152 if (pipe(pdto) < 0) {
153 goto error1;
155 if (pipe(pdfrom) < 0) {
156 goto error2;
158 #ifdef VFORK
159 switch (pid = vfork()) {
160 #else
161 switch (pid = fork()) {
162 #endif
163 case -1:
164 goto error3;
166 case 0:
168 * child: read from pdto[0], write into pdfrom[1]
170 close(0);
171 dup(pdto[0]);
172 close(1);
173 dup(pdfrom[1]);
174 getrlimit(RLIMIT_NOFILE, &rl);
175 for (i = rl.rlim_max - 1; i >= 3; i--) {
176 close(i);
178 com = malloc((unsigned) strlen(command) + 6);
179 if (com == NULL) {
180 _exit(~0);
182 sprintf(com, "exec %s", command);
183 execl(SHELL, basename(SHELL), "-c", com, NULL);
184 _exit(~0);
186 default:
188 * parent: write into pdto[1], read from pdfrom[0]
190 *fto = fdopen(pdto[1], "w");
191 close(pdto[0]);
192 *ffrom = fdopen(pdfrom[0], "r");
193 close(pdfrom[1]);
194 break;
196 return (pid);
199 * error cleanup and return
201 error3:
202 close(pdfrom[0]);
203 close(pdfrom[1]);
204 error2:
205 close(pdto[0]);
206 close(pdto[1]);
207 error1:
208 return (-1);
211 static char *
212 basename(char *path)
214 char *p;
216 p = strrchr(path, '/');
217 if (p == NULL) {
218 return (path);
219 } else {
220 return (p + 1);
224 #else /* YP */
226 #ifdef foo
227 #define ERR_ACCESS 1
228 #define ERR_MALLOC 2
229 #define ERR_READ 3
230 #define ERR_WRITE 4
231 #define ERR_DBASE 5
232 #define ERR_KEY 6
233 extern char *malloc();
234 #endif
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.
244 * Parameters:
245 * name: Name of the requestor
246 * keylen: Not used
247 * datalen: Not used
250 localupdate(char *name, char *filename, u_int op, u_int keylen, char *key,
251 u_int datalen, char *data)
253 char line[256];
254 FILE *rf;
255 FILE *wf;
256 char *tmpname;
257 int err;
260 * Check permission
262 if (strcmp(name, key) != 0) {
263 return (ERR_ACCESS);
265 if (strcmp(name, "nobody") == 0) {
267 * Can't change "nobody"s key.
269 return (ERR_ACCESS);
273 * Open files
275 tmpname = malloc(strlen(filename) + 4);
276 if (tmpname == NULL) {
277 return (ERR_MALLOC);
279 sprintf(tmpname, "%s.tmp", filename);
280 rf = fopen(filename, "r");
281 if (rf == NULL) {
282 return (ERR_READ);
284 wf = fopen(tmpname, "w");
285 if (wf == NULL) {
286 return (ERR_WRITE);
288 err = -1;
289 while (fgets(line, sizeof (line), rf)) {
290 if (err < 0 && match(line, name)) {
291 switch (op) {
292 case YPOP_INSERT:
293 err = ERR_KEY;
294 break;
295 case YPOP_STORE:
296 case YPOP_CHANGE:
297 fprintf(wf, "%s %s\n", key, data);
298 err = 0;
299 break;
300 case YPOP_DELETE:
301 /* do nothing */
302 err = 0;
303 break;
305 } else {
306 fputs(line, wf);
309 if (err < 0) {
310 switch (op) {
311 case YPOP_CHANGE:
312 case YPOP_DELETE:
313 err = ERR_KEY;
314 break;
315 case YPOP_INSERT:
316 case YPOP_STORE:
317 err = 0;
318 fprintf(wf, "%s %s\n", key, data);
319 break;
322 fclose(wf);
323 fclose(rf);
324 if (err == 0) {
325 if (rename(tmpname, filename) < 0) {
326 return (ERR_DBASE);
328 } else {
329 if (unlink(tmpname) < 0) {
330 return (ERR_DBASE);
333 return (err);
336 static int
337 match(char *line, char *name)
339 int len;
341 len = strlen(name);
342 return (strncmp(line, name, len) == 0 &&
343 (line[len] == ' ' || line[len] == '\t'));
345 #endif /* !YP */