]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - usr.sbin/rpc.ypupdated/update.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.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$";
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 "ypupdated_extern.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 SHELL[] = "/bin/sh";
73 static char YPDBPATH[]="/var/yp";       /* This is defined but not used! */
74 static char PKMAP[] = "publickey.byname";
75 static char UPDATEFILE[] = "updaters";
76 static char PKFILE[] = "/etc/publickey";
77 #endif  /* YP */
78
79 #ifdef YP
80 static int _openchild(char *, FILE **, FILE **);
81
82 /*
83  * Determine if requester is allowed to update the given map,
84  * and update it if so. Returns the yp status, which is zero
85  * if there is no access violation.
86  */
87 int
88 mapupdate(char *requester, char *mapname, u_int op, u_int keylen, char *key,
89     u_int datalen, char *data)
90 {
91         char updater[MAXMAPNAMELEN + 40];
92         FILE *childargs;
93         FILE *childrslt;
94 #ifdef WEXITSTATUS
95         int status;
96 #else
97         union wait status;
98 #endif
99         pid_t pid;
100         u_int yperrno;
101
102
103 #ifdef DEBUG
104         printf("%s %s\n", key, data);
105 #endif
106         (void)sprintf(updater, "make -s -f %s/%s %s", YPDBPATH, /* !!! */
107                                         UPDATEFILE, mapname);
108         pid = _openchild(updater, &childargs, &childrslt);
109         if (pid < 0) {
110                 return (YPERR_YPERR);
111         }
112
113         /*
114          * Write to child
115          */
116         (void)fprintf(childargs, "%s\n", requester);
117         (void)fprintf(childargs, "%u\n", op);
118         (void)fprintf(childargs, "%u\n", keylen);
119         (void)fwrite(key, (int)keylen, 1, childargs);
120         (void)fprintf(childargs, "\n");
121         (void)fprintf(childargs, "%u\n", datalen);
122         (void)fwrite(data, (int)datalen, 1, childargs);
123         (void)fprintf(childargs, "\n");
124         (void)fclose(childargs);
125
126         /*
127          * Read from child
128          */
129         (void)fscanf(childrslt, "%d", &yperrno);
130         (void)fclose(childrslt);
131
132         (void)wait(&status);
133 #ifdef WEXITSTATUS
134         if (WEXITSTATUS(status) != 0)
135 #else
136         if (status.w_retcode != 0)
137 #endif
138                 return (YPERR_YPERR);
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, (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 static int match(char *, char *);
226
227 /*
228  * Determine if requester is allowed to update the given map,
229  * and update it if so. Returns the status, which is zero
230  * if there is no access violation. This function updates
231  * the local file and then shuts up.
232  */
233 int
234 localupdate(char *name, char *filename, u_int op, u_int keylen __unused,
235     char *key, u_int datalen __unused, char *data)
236 {
237         char line[256];
238         FILE *rf;
239         FILE *wf;
240         char *tmpname;
241         int err;
242
243         /*
244          * Check permission
245          */
246         if (strcmp(name, key) != 0) {
247                 return (ERR_ACCESS);
248         }
249         if (strcmp(name, "nobody") == 0) {
250                 /*
251                  * Can't change "nobody"s key.
252                  */
253                 return (ERR_ACCESS);
254         }
255
256         /*
257          * Open files
258          */
259         tmpname = malloc(strlen(filename) + 4);
260         if (tmpname == NULL) {
261                 return (ERR_MALLOC);
262         }
263         sprintf(tmpname, "%s.tmp", filename);
264         rf = fopen(filename, "r");
265         if (rf == NULL) {
266                 return (ERR_READ);
267         }
268         wf = fopen(tmpname, "w");
269         if (wf == NULL) {
270                 return (ERR_WRITE);
271         }
272         err = -1;
273         while (fgets(line, sizeof (line), rf)) {
274                 if (err < 0 && match(line, name)) {
275                         switch (op) {
276                         case YPOP_INSERT:
277                                 err = ERR_KEY;
278                                 break;
279                         case YPOP_STORE:
280                         case YPOP_CHANGE:
281                                 fprintf(wf, "%s %s\n", key, data);
282                                 err = 0;
283                                 break;
284                         case YPOP_DELETE:
285                                 /* do nothing */
286                                 err = 0;
287                                 break;
288                         }
289                 } else {
290                         fputs(line, wf);
291                 }
292         }
293         if (err < 0) {
294                 switch (op) {
295                 case YPOP_CHANGE:
296                 case YPOP_DELETE:
297                         err = ERR_KEY;
298                         break;
299                 case YPOP_INSERT:
300                 case YPOP_STORE:
301                         err = 0;
302                         fprintf(wf, "%s %s\n", key, data);
303                         break;
304                 }
305         }
306         fclose(wf);
307         fclose(rf);
308         if (err == 0) {
309                 if (rename(tmpname, filename) < 0) {
310                         return (ERR_DBASE);
311                 }
312         } else {
313                 if (unlink(tmpname) < 0) {
314                         return (ERR_DBASE);
315                 }
316         }
317         return (err);
318 }
319
320 static int
321 match(char *line, char *name)
322 {
323         int len;
324
325         len = strlen(name);
326         return (strncmp(line, name, len) == 0 &&
327                 (line[len] == ' ' || line[len] == '\t'));
328 }
329 #endif /* !YP */