xref: /freebsd/usr.bin/newkey/update.c (revision 2f513db7)
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 
74 static int _openchild(char *, FILE **, FILE **);
75 static char *basename(char *path);
76 
77 /*
78  * Determine if requester is allowed to update the given map,
79  * and update it if so. Returns the yp status, which is zero
80  * if there is no access violation.
81  */
82 int
83 mapupdate(char *requester, char *mapname, u_int op, u_int keylen,
84     char *key, u_int datalen, char *data)
85 {
86 	char updater[MAXMAPNAMELEN + 40];
87 	FILE *childargs;
88 	FILE *childrslt;
89 #ifdef WEXITSTATUS
90 	int status;
91 #else
92 	union wait status;
93 #endif
94 	pid_t pid;
95 	u_int yperrno;
96 
97 
98 #ifdef DEBUG
99 	printf("%s %s\n", key, data);
100 #endif
101 	(void)sprintf(updater, "make -s -f %s/%s %s", YPDBPATH, /* !!! */
102 					UPDATEFILE, mapname);
103 	pid = _openchild(updater, &childargs, &childrslt);
104 	if (pid < 0) {
105 		return (YPERR_YPERR);
106 	}
107 
108 	/*
109 	 * Write to child
110 	 */
111 	(void)fprintf(childargs, "%s\n", requester);
112 	(void)fprintf(childargs, "%u\n", op);
113 	(void)fprintf(childargs, "%u\n", keylen);
114 	(void)fwrite(key, (int)keylen, 1, childargs);
115 	(void)fprintf(childargs, "\n");
116 	(void)fprintf(childargs, "%u\n", datalen);
117 	(void)fwrite(data, (int)datalen, 1, childargs);
118 	(void)fprintf(childargs, "\n");
119 	(void)fclose(childargs);
120 
121 	/*
122 	 * Read from child
123 	 */
124 	(void)fscanf(childrslt, "%d", &yperrno);
125 	(void)fclose(childrslt);
126 
127 	(void)wait(&status);
128 #ifdef WEXITSTATUS
129 	if (WEXITSTATUS(status) != 0) {
130 #else
131 	if (status.w_retcode != 0) {
132 #endif
133 		return (YPERR_YPERR);
134 	}
135 	return (yperrno);
136 }
137 
138 /*
139  * returns pid, or -1 for failure
140  */
141 static pid_t
142 _openchild(char *command, FILE **fto, FILE **ffrom)
143 {
144 	int i;
145 	pid_t pid;
146 	int pdto[2];
147 	int pdfrom[2];
148 	char *com;
149 	struct rlimit rl;
150 
151 	if (pipe(pdto) < 0) {
152 		goto error1;
153 	}
154 	if (pipe(pdfrom) < 0) {
155 		goto error2;
156 	}
157 	switch (pid = fork()) {
158 	case -1:
159 		goto error3;
160 
161 	case 0:
162 		/*
163 		 * child: read from pdto[0], write into pdfrom[1]
164 		 */
165 		(void)close(0);
166 		(void)dup(pdto[0]);
167 		(void)close(1);
168 		(void)dup(pdfrom[1]);
169 		getrlimit(RLIMIT_NOFILE, &rl);
170 		for (i = rl.rlim_max - 1; i >= 3; i--) {
171 			(void) close(i);
172 		}
173 		com = malloc((unsigned) strlen(command) + 6);
174 		if (com == NULL) {
175 			_exit(~0);
176 		}
177 		(void)sprintf(com, "exec %s", command);
178 		execl(SHELL, basename(SHELL), "-c", com, (char *)NULL);
179 		_exit(~0);
180 
181 	default:
182 		/*
183 		 * parent: write into pdto[1], read from pdfrom[0]
184 		 */
185 		*fto = fdopen(pdto[1], "w");
186 		(void)close(pdto[0]);
187 		*ffrom = fdopen(pdfrom[0], "r");
188 		(void)close(pdfrom[1]);
189 		break;
190 	}
191 	return (pid);
192 
193 	/*
194 	 * error cleanup and return
195 	 */
196 error3:
197 	(void)close(pdfrom[0]);
198 	(void)close(pdfrom[1]);
199 error2:
200 	(void)close(pdto[0]);
201 	(void)close(pdto[1]);
202 error1:
203 	return (-1);
204 }
205 
206 static char *
207 basename(char *path)
208 {
209 	char *p;
210 
211 	p = strrchr(path, '/');
212 	if (p == NULL) {
213 		return (path);
214 	} else {
215 		return (p + 1);
216 	}
217 }
218 
219 #else /* YP */
220 
221 #define	ERR_ACCESS	1
222 #define	ERR_MALLOC	2
223 #define	ERR_READ	3
224 #define	ERR_WRITE	4
225 #define	ERR_DBASE	5
226 #define	ERR_KEY		6
227 
228 static int match(char *, char *);
229 
230 /*
231  * Determine if requester is allowed to update the given map,
232  * and update it if so. Returns the status, which is zero
233  * if there is no access violation. This function updates
234  * the local file and then shuts up.
235  */
236 int
237 localupdate(char *name, char *filename, u_int op, u_int keylen __unused,
238     char *key, u_int datalen __unused, char *data)
239 {
240 	char line[256];
241 	FILE *rf;
242 	FILE *wf;
243 	char *tmpname;
244 	int err;
245 
246 	/*
247 	 * Check permission
248 	 */
249 	if (strcmp(name, key) != 0) {
250 		return (ERR_ACCESS);
251 	}
252 	if (strcmp(name, "nobody") == 0) {
253 		/*
254 		 * Can't change "nobody"s key.
255 		 */
256 		return (ERR_ACCESS);
257 	}
258 
259 	/*
260 	 * Open files
261 	 */
262 	tmpname = malloc(strlen(filename) + 4);
263 	if (tmpname == NULL) {
264 		return (ERR_MALLOC);
265 	}
266 	sprintf(tmpname, "%s.tmp", filename);
267 	rf = fopen(filename, "r");
268 	if (rf == NULL) {
269 		err = ERR_READ;
270 		goto cleanup;
271 	}
272 	wf = fopen(tmpname, "w");
273 	if (wf == NULL) {
274 		fclose(rf);
275 		err = ERR_WRITE;
276 		goto cleanup;
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 			err = ERR_DBASE;
317 			goto cleanup;
318 		}
319 	} else {
320 		if (unlink(tmpname) < 0) {
321 			err = ERR_DBASE;
322 			goto cleanup;
323 		}
324 	}
325 
326 cleanup:
327 	free(tmpname);
328 	return (err);
329 }
330 
331 static int
332 match(char *line, char *name)
333 {
334 	int len;
335 
336 	len = strlen(name);
337 	return (strncmp(line, name, len) == 0 &&
338 		(line[len] == ' ' || line[len] == '\t'));
339 }
340 #endif /* !YP */
341