]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - usr.bin/newkey/update.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.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
31 #ifndef lint
32 #if 0
33 static  char sccsid[] = "@(#)update.c 1.2 91/03/11 Copyr 1986 Sun Micro";
34 #endif
35 #endif
36
37 /*
38  * Copyright (C) 1986, 1989, Sun Microsystems, Inc.
39  */
40
41 /*
42  * Administrative tool to add a new user to the publickey database
43  */
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
46
47 #include <sys/types.h>
48 #include <sys/time.h>
49 #include <sys/resource.h>
50
51 #include <rpc/rpc.h>
52 #include <rpc/key_prot.h>
53
54 #ifdef YP
55 #include <sys/wait.h>
56 #include <rpcsvc/yp_prot.h>
57 #include <rpcsvc/ypclnt.h>
58 #include <netdb.h>
59 #endif  /* YP */
60
61 #include <pwd.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <unistd.h>
66
67 #include "extern.h"
68
69 #ifdef YP
70 static char SHELL[] = "/bin/sh";
71 static char YPDBPATH[]="/var/yp";       /* This is defined but not used! */
72 static char UPDATEFILE[] = "updaters";
73 #else
74 static char PKFILE[] = "/etc/publickey";
75 #endif  /* YP */
76
77 #ifdef YP
78 static int _openchild(char *, FILE **, FILE **);
79 static char *basename(char *path);
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,
88     char *key, 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 pid_t
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, (char *)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, u_int keylen,
242     char *key, u_int datalen, char *data)
243 {
244         char line[256];
245         FILE *rf;
246         FILE *wf;
247         char *tmpname;
248         int err;
249
250         /*
251          * Check permission
252          */
253         if (strcmp(name, key) != 0) {
254                 return (ERR_ACCESS);
255         }
256         if (strcmp(name, "nobody") == 0) {
257                 /*
258                  * Can't change "nobody"s key.
259                  */
260                 return (ERR_ACCESS);
261         }
262
263         /*
264          * Open files
265          */
266         tmpname = malloc(strlen(filename) + 4);
267         if (tmpname == NULL) {
268                 return (ERR_MALLOC);
269         }
270         sprintf(tmpname, "%s.tmp", filename);
271         rf = fopen(filename, "r");
272         if (rf == NULL) {
273                 return (ERR_READ);
274         }
275         wf = fopen(tmpname, "w");
276         if (wf == NULL) {
277                 return (ERR_WRITE);
278         }
279         err = -1;
280         while (fgets(line, sizeof (line), rf)) {
281                 if (err < 0 && match(line, name)) {
282                         switch (op) {
283                         case YPOP_INSERT:
284                                 err = ERR_KEY;
285                                 break;
286                         case YPOP_STORE:
287                         case YPOP_CHANGE:
288                                 fprintf(wf, "%s %s\n", key, data);
289                                 err = 0;
290                                 break;
291                         case YPOP_DELETE:
292                                 /* do nothing */
293                                 err = 0;
294                                 break;
295                         }
296                 } else {
297                         fputs(line, wf);
298                 }
299         }
300         if (err < 0) {
301                 switch (op) {
302                 case YPOP_CHANGE:
303                 case YPOP_DELETE:
304                         err = ERR_KEY;
305                         break;
306                 case YPOP_INSERT:
307                 case YPOP_STORE:
308                         err = 0;
309                         fprintf(wf, "%s %s\n", key, data);
310                         break;
311                 }
312         }
313         fclose(wf);
314         fclose(rf);
315         if (err == 0) {
316                 if (rename(tmpname, filename) < 0) {
317                         return (ERR_DBASE);
318                 }
319         } else {
320                 if (unlink(tmpname) < 0) {
321                         return (ERR_DBASE);
322                 }
323         }
324         return (err);
325 }
326
327 static int
328 match(char *line, char *name)
329 {
330         int len;
331
332         len = strlen(name);
333         return (strncmp(line, name, len) == 0 &&
334                 (line[len] == ' ' || line[len] == '\t'));
335 }
336 #endif /* !YP */