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