xref: /dragonfly/usr.sbin/rpc.ypupdated/update.c (revision 7d3e9a5b)
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.sbin/rpc.ypupdated/update.c,v 1.8 2007/02/15 02:45:14 trhodes 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 <stdio.h>
42 #include <stdlib.h>
43 #include <unistd.h>
44 #include <rpc/rpc.h>
45 #include <rpc/key_prot.h>
46 #ifdef YP
47 #include <rpcsvc/yp_prot.h>
48 #include <rpcsvc/ypclnt.h>
49 #include <sys/wait.h>
50 #include <netdb.h>
51 #endif	/* YP */
52 #include <pwd.h>
53 #include <string.h>
54 #include <sys/resource.h>
55 #include "ypupdated_extern.h"
56 
57 #ifdef YP
58 #define	MAXMAPNAMELEN 256
59 #else
60 #define	YPOP_CHANGE 1			/* change, do not add */
61 #define	YPOP_INSERT 2			/* add, do not change */
62 #define	YPOP_DELETE 3			/* delete this entry */
63 #define	YPOP_STORE  4			/* add, or change */
64 #endif
65 
66 #ifdef YP
67 static char SHELL[] = "/bin/sh";
68 static char YPDBPATH[]="/var/yp";	/* This is defined but not used! */
69 static char PKMAP[] = "publickey.byname";
70 static char UPDATEFILE[] = "updaters";
71 static char PKFILE[] = "/etc/publickey";
72 #endif	/* YP */
73 
74 #ifdef YP
75 static int _openchild(char *, FILE **, FILE **);
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, char *key,
84 	  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 	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 	fprintf(childargs, "%s\n", requester);
112 	fprintf(childargs, "%u\n", op);
113 	fprintf(childargs, "%u\n", keylen);
114 	fwrite(key, (int)keylen, 1, childargs);
115 	fprintf(childargs, "\n");
116 	fprintf(childargs, "%u\n", datalen);
117 	fwrite(data, (int)datalen, 1, childargs);
118 	fprintf(childargs, "\n");
119 	fclose(childargs);
120 
121 	/*
122 	 * Read from child
123 	 */
124 	fscanf(childrslt, "%d", &yperrno);
125 	fclose(childrslt);
126 
127 	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 	return (yperrno);
135 }
136 
137 /*
138  * returns pid, or -1 for failure
139  */
140 static int
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 		close(0);
165 		dup(pdto[0]);
166 		close(1);
167 		dup(pdfrom[1]);
168 		getrlimit(RLIMIT_NOFILE, &rl);
169 		for (i = rl.rlim_max - 1; i >= 3; i--) {
170 			close(i);
171 		}
172 		com = malloc((unsigned) strlen(command) + 6);
173 		if (com == NULL) {
174 			_exit(~0);
175 		}
176 		sprintf(com, "exec %s", command);
177 		execl(SHELL, basename(SHELL), "-c", com, 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 		close(pdto[0]);
186 		*ffrom = fdopen(pdfrom[0], "r");
187 		close(pdfrom[1]);
188 		break;
189 	}
190 	return (pid);
191 
192 	/*
193 	 * error cleanup and return
194 	 */
195 error3:
196 	close(pdfrom[0]);
197 	close(pdfrom[1]);
198 error2:
199 	close(pdto[0]);
200 	close(pdto[1]);
201 error1:
202 	return (-1);
203 }
204 
205 static char *
206 basename(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 static int match(char *, char *);
221 
222 /*
223  * Determine if requester is allowed to update the given map,
224  * and update it if so. Returns the status, which is zero
225  * if there is no access violation. This function updates
226  * the local file and then shuts up.
227  */
228 int
229 localupdate(char *name, char *filename, u_int op, u_int keylen __unused,
230 	    char *key, u_int datalen __unused, char *data)
231 {
232 	char line[256];
233 	FILE *rf;
234 	FILE *wf;
235 	char *tmpname;
236 	int err;
237 
238 	/*
239 	 * Check permission
240 	 */
241 	if (strcmp(name, key) != 0) {
242 		return (ERR_ACCESS);
243 	}
244 	if (strcmp(name, "nobody") == 0) {
245 		/*
246 		 * Can't change "nobody"s key.
247 		 */
248 		return (ERR_ACCESS);
249 	}
250 
251 	/*
252 	 * Open files
253 	 */
254 	tmpname = malloc(strlen(filename) + 4);
255 	if (tmpname == NULL) {
256 		return (ERR_MALLOC);
257 	}
258 	sprintf(tmpname, "%s.tmp", filename);
259 	rf = fopen(filename, "r");
260 	if (rf == NULL) {
261 		return (ERR_READ);
262 	}
263 	wf = fopen(tmpname, "w");
264 	if (wf == NULL) {
265 		return (ERR_WRITE);
266 	}
267 	err = -1;
268 	while (fgets(line, sizeof (line), rf)) {
269 		if (err < 0 && match(line, name)) {
270 			switch (op) {
271 			case YPOP_INSERT:
272 				err = ERR_KEY;
273 				break;
274 			case YPOP_STORE:
275 			case YPOP_CHANGE:
276 				fprintf(wf, "%s %s\n", key, data);
277 				err = 0;
278 				break;
279 			case YPOP_DELETE:
280 				/* do nothing */
281 				err = 0;
282 				break;
283 			}
284 		} else {
285 			fputs(line, wf);
286 		}
287 	}
288 	if (err < 0) {
289 		switch (op) {
290 		case YPOP_CHANGE:
291 		case YPOP_DELETE:
292 			err = ERR_KEY;
293 			break;
294 		case YPOP_INSERT:
295 		case YPOP_STORE:
296 			err = 0;
297 			fprintf(wf, "%s %s\n", key, data);
298 			break;
299 		}
300 	}
301 	fclose(wf);
302 	fclose(rf);
303 	if (err == 0) {
304 		if (rename(tmpname, filename) < 0) {
305 			return (ERR_DBASE);
306 		}
307 	} else {
308 		if (unlink(tmpname) < 0) {
309 			return (ERR_DBASE);
310 		}
311 	}
312 	return (err);
313 }
314 
315 static int
316 match(char *line, char *name)
317 {
318 	int len;
319 
320 	len = strlen(name);
321 	return (strncmp(line, name, len) == 0 &&
322 		(line[len] == ' ' || line[len] == '\t'));
323 }
324 #endif /* !YP */
325