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