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