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