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