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