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