Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.sbin / rpc.ypupdated / update.c
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.
9  *
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.
13  *
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.
17  *
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.
21  *
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.
25  *
26  * Sun Microsystems, Inc.
27  * 2550 Garcia Avenue
28  * Mountain View, California  94043
29  */
30
31 #ifndef lint
32 #if 0
33 static  char sccsid[] = "@(#)update.c 1.2 91/03/11 Copyr 1986 Sun Micro";
34 #endif
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 $";
37 #endif /* not lint */
38
39 /*
40  * Copyright (C) 1986, 1989, Sun Microsystems, Inc.
41  */
42
43 /*
44  * Administrative tool to add a new user to the publickey database
45  */
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <unistd.h>
49 #include <rpc/rpc.h>
50 #include <rpc/key_prot.h>
51 #ifdef YP
52 #include <rpcsvc/yp_prot.h>
53 #include <rpcsvc/ypclnt.h>
54 #include <sys/wait.h>
55 #include <netdb.h>
56 #endif  /* YP */
57 #include <pwd.h>
58 #include <string.h>
59 #include <sys/resource.h>
60 #include <stdlib.h>
61 #include "ypupdated_extern.h"
62
63 #ifdef YP
64 #define MAXMAPNAMELEN 256
65 #else
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 */
70 #endif
71
72 #ifdef YP
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";
78 #endif  /* YP */
79
80 #ifdef YP
81 static int _openchild(char *, FILE **, FILE **);
82
83 /*
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.
87  */
88 mapupdate(requester, mapname, op, keylen, key, datalen, data)
89         char *requester;
90         char *mapname;
91         u_int op;
92         u_int keylen;
93         char *key;
94         u_int datalen;
95         char *data;
96 {
97         char updater[MAXMAPNAMELEN + 40];
98         FILE *childargs;
99         FILE *childrslt;
100 #ifdef WEXITSTATUS
101         int status;
102 #else
103         union wait status;
104 #endif
105         pid_t pid;
106         u_int yperrno;
107
108
109 #ifdef DEBUG
110         printf("%s %s\n", key, data);
111 #endif
112         (void)sprintf(updater, "make -s -f %s/%s %s", YPDBPATH, /* !!! */
113                                         UPDATEFILE, mapname);
114         pid = _openchild(updater, &childargs, &childrslt);
115         if (pid < 0) {
116                 return (YPERR_YPERR);
117         }
118
119         /*
120          * Write to child
121          */
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);
131
132         /*
133          * Read from child
134          */
135         (void)fscanf(childrslt, "%d", &yperrno);
136         (void)fclose(childrslt);
137
138         (void)wait(&status);
139 #ifdef WEXITSTATUS
140         if (WEXITSTATUS(status) != 0) {
141 #else
142         if (status.w_retcode != 0) {
143 #endif
144                 return (YPERR_YPERR);
145         }
146         return (yperrno);
147 }
148
149 /*
150  * returns pid, or -1 for failure
151  */
152 static
153 _openchild(command, fto, ffrom)
154         char *command;
155         FILE **fto;
156         FILE **ffrom;
157 {
158         int i;
159         pid_t pid;
160         int pdto[2];
161         int pdfrom[2];
162         char *com;
163         struct rlimit rl;
164
165         if (pipe(pdto) < 0) {
166                 goto error1;
167         }
168         if (pipe(pdfrom) < 0) {
169                 goto error2;
170         }
171 #ifdef VFORK
172         switch (pid = vfork()) {
173 #else
174         switch (pid = fork()) {
175 #endif
176         case -1:
177                 goto error3;
178
179         case 0:
180                 /*
181                  * child: read from pdto[0], write into pdfrom[1]
182                  */
183                 (void)close(0);
184                 (void)dup(pdto[0]);
185                 (void)close(1);
186                 (void)dup(pdfrom[1]);
187                 getrlimit(RLIMIT_NOFILE, &rl);
188                 for (i = rl.rlim_max - 1; i >= 3; i--) {
189                         (void) close(i);
190                 }
191                 com = malloc((unsigned) strlen(command) + 6);
192                 if (com == NULL) {
193                         _exit(~0);
194                 }
195                 (void)sprintf(com, "exec %s", command);
196                 execl(SHELL, basename(SHELL), "-c", com, NULL);
197                 _exit(~0);
198
199         default:
200                 /*
201                  * parent: write into pdto[1], read from pdfrom[0]
202                  */
203                 *fto = fdopen(pdto[1], "w");
204                 (void)close(pdto[0]);
205                 *ffrom = fdopen(pdfrom[0], "r");
206                 (void)close(pdfrom[1]);
207                 break;
208         }
209         return (pid);
210
211         /*
212          * error cleanup and return
213          */
214 error3:
215         (void)close(pdfrom[0]);
216         (void)close(pdfrom[1]);
217 error2:
218         (void)close(pdto[0]);
219         (void)close(pdto[1]);
220 error1:
221         return (-1);
222 }
223
224 static char *
225 basename(path)
226         char *path;
227 {
228         char *p;
229
230         p = strrchr(path, '/');
231         if (p == NULL) {
232                 return (path);
233         } else {
234                 return (p + 1);
235         }
236 }
237
238 #else /* YP */
239
240 #ifdef foo
241 #define ERR_ACCESS      1
242 #define ERR_MALLOC      2
243 #define ERR_READ        3
244 #define ERR_WRITE       4
245 #define ERR_DBASE       5
246 #define ERR_KEY         6
247 extern char *malloc();
248 #endif
249
250 static int match(char *, char *);
251
252 /*
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.
257  */
258 int
259 localupdate(name, filename, op, keylen, key, datalen, data)
260         char *name;     /* Name of the requestor */
261         char *filename;
262         u_int op;
263         u_int keylen;   /* Not used */
264         char *key;
265         u_int datalen;  /* Not used */
266         char *data;
267 {
268         char line[256];
269         FILE *rf;
270         FILE *wf;
271         char *tmpname;
272         int err;
273
274         /*
275          * Check permission
276          */
277         if (strcmp(name, key) != 0) {
278                 return (ERR_ACCESS);
279         }
280         if (strcmp(name, "nobody") == 0) {
281                 /*
282                  * Can't change "nobody"s key.
283                  */
284                 return (ERR_ACCESS);
285         }
286
287         /*
288          * Open files
289          */
290         tmpname = malloc(strlen(filename) + 4);
291         if (tmpname == NULL) {
292                 return (ERR_MALLOC);
293         }
294         sprintf(tmpname, "%s.tmp", filename);
295         rf = fopen(filename, "r");
296         if (rf == NULL) {
297                 return (ERR_READ);
298         }
299         wf = fopen(tmpname, "w");
300         if (wf == NULL) {
301                 return (ERR_WRITE);
302         }
303         err = -1;
304         while (fgets(line, sizeof (line), rf)) {
305                 if (err < 0 && match(line, name)) {
306                         switch (op) {
307                         case YPOP_INSERT:
308                                 err = ERR_KEY;
309                                 break;
310                         case YPOP_STORE:
311                         case YPOP_CHANGE:
312                                 fprintf(wf, "%s %s\n", key, data);
313                                 err = 0;
314                                 break;
315                         case YPOP_DELETE:
316                                 /* do nothing */
317                                 err = 0;
318                                 break;
319                         }
320                 } else {
321                         fputs(line, wf);
322                 }
323         }
324         if (err < 0) {
325                 switch (op) {
326                 case YPOP_CHANGE:
327                 case YPOP_DELETE:
328                         err = ERR_KEY;
329                         break;
330                 case YPOP_INSERT:
331                 case YPOP_STORE:
332                         err = 0;
333                         fprintf(wf, "%s %s\n", key, data);
334                         break;
335                 }
336         }
337         fclose(wf);
338         fclose(rf);
339         if (err == 0) {
340                 if (rename(tmpname, filename) < 0) {
341                         return (ERR_DBASE);
342                 }
343         } else {
344                 if (unlink(tmpname) < 0) {
345                         return (ERR_DBASE);
346                 }
347         }
348         return (err);
349 }
350
351 static int
352 match(line, name)
353         char *line;
354         char *name;
355 {
356         int len;
357
358         len = strlen(name);
359         return (strncmp(line, name, len) == 0 &&
360                 (line[len] == ' ' || line[len] == '\t'));
361 }
362 #endif /* !YP */
363