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