xref: /onnv-gate/usr/src/cmd/keyserv/update.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2000 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
32*0Sstevel@tonic-gate  * The Regents of the University of California
33*0Sstevel@tonic-gate  * All Rights Reserved
34*0Sstevel@tonic-gate  *
35*0Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
36*0Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
37*0Sstevel@tonic-gate  * contributors.
38*0Sstevel@tonic-gate  */
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate /*
43*0Sstevel@tonic-gate  * Administrative tool to add a new user to the publickey database
44*0Sstevel@tonic-gate  */
45*0Sstevel@tonic-gate #include <stdio.h>
46*0Sstevel@tonic-gate #include <stdlib.h>
47*0Sstevel@tonic-gate #include <unistd.h>
48*0Sstevel@tonic-gate #include <rpc/rpc.h>
49*0Sstevel@tonic-gate #include <rpc/key_prot.h>
50*0Sstevel@tonic-gate #include <rpcsvc/ypclnt.h>
51*0Sstevel@tonic-gate #include <sys/wait.h>
52*0Sstevel@tonic-gate #include <netdb.h>
53*0Sstevel@tonic-gate #include <string.h>
54*0Sstevel@tonic-gate #include <sys/stat.h>
55*0Sstevel@tonic-gate #include <errno.h>
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate #define	MAXMAPNAMELEN 256
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate extern	char	*program_name;
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate static	char	*basename(char *path);
62*0Sstevel@tonic-gate static	int	match(char *line, char *name);
63*0Sstevel@tonic-gate static	int	_openchild(char *command, FILE **fto, FILE **ffrom);
64*0Sstevel@tonic-gate static	char	SHELL[] = "/bin/sh";
65*0Sstevel@tonic-gate static	char	UPDATEFILE[] = "updaters";
66*0Sstevel@tonic-gate static	char	MAKE[] = "/usr/ccs/bin/make";
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate /*
69*0Sstevel@tonic-gate  * Determine if requester is allowed to update the given map,
70*0Sstevel@tonic-gate  * and update it if so. Returns the yp status, which is zero
71*0Sstevel@tonic-gate  * if there is no access violation.
72*0Sstevel@tonic-gate  */
73*0Sstevel@tonic-gate int
mapupdate(char * name,char * mapname,uint_t op,char * data)74*0Sstevel@tonic-gate mapupdate(char *name, char *mapname, uint_t op, char *data)
75*0Sstevel@tonic-gate {
76*0Sstevel@tonic-gate 	char	updater[MAXMAPNAMELEN + 40];
77*0Sstevel@tonic-gate 	FILE	*childargs;
78*0Sstevel@tonic-gate 	FILE	*childrslt;
79*0Sstevel@tonic-gate #ifdef WEXITSTATUS
80*0Sstevel@tonic-gate 	int	status;
81*0Sstevel@tonic-gate #else
82*0Sstevel@tonic-gate 	union wait status;
83*0Sstevel@tonic-gate #endif
84*0Sstevel@tonic-gate 	pid_t	pid;
85*0Sstevel@tonic-gate 	uint_t	yperrno;
86*0Sstevel@tonic-gate 	int	namelen, datalen;
87*0Sstevel@tonic-gate 	struct	stat	stbuf;
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate #ifdef DEBUG
90*0Sstevel@tonic-gate 	(void) fprintf(stderr, "%s %s\n", name, data);
91*0Sstevel@tonic-gate #endif
92*0Sstevel@tonic-gate 	namelen = strlen(name);
93*0Sstevel@tonic-gate 	datalen = strlen(data);
94*0Sstevel@tonic-gate 	errno = 0;
95*0Sstevel@tonic-gate 	if (stat(MAKE, &stbuf) < 0)
96*0Sstevel@tonic-gate 		switch (errno) {
97*0Sstevel@tonic-gate 		case ENOENT:
98*0Sstevel@tonic-gate 			(void) fprintf(stderr,
99*0Sstevel@tonic-gate 			"%s: %s not found, please install on the system\n",
100*0Sstevel@tonic-gate 			program_name, MAKE);
101*0Sstevel@tonic-gate 			return (1);
102*0Sstevel@tonic-gate 		default:
103*0Sstevel@tonic-gate 			(void) fprintf(stderr,
104*0Sstevel@tonic-gate 				"%s: cannot access %s, errno=%d.\n",
105*0Sstevel@tonic-gate 				program_name, MAKE, errno);
106*0Sstevel@tonic-gate 			return (1);
107*0Sstevel@tonic-gate 		}
108*0Sstevel@tonic-gate 	(void) sprintf(updater, "%s -s -f %s %s",
109*0Sstevel@tonic-gate 			MAKE, UPDATEFILE, mapname);
110*0Sstevel@tonic-gate 	pid = _openchild(updater, &childargs, &childrslt);
111*0Sstevel@tonic-gate 	if (pid < 0)
112*0Sstevel@tonic-gate 		return (YPERR_YPERR);
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate 	/*
115*0Sstevel@tonic-gate 	 * Write to child
116*0Sstevel@tonic-gate 	 */
117*0Sstevel@tonic-gate 	(void) fprintf(childargs, "%s\n", name);
118*0Sstevel@tonic-gate 	(void) fprintf(childargs, "%u\n", op);
119*0Sstevel@tonic-gate 	(void) fprintf(childargs, "%u\n", namelen);
120*0Sstevel@tonic-gate 	(void) fwrite(name, namelen, 1, childargs);
121*0Sstevel@tonic-gate 	(void) fprintf(childargs, "\n");
122*0Sstevel@tonic-gate 	(void) fprintf(childargs, "%u\n", datalen);
123*0Sstevel@tonic-gate 	(void) fwrite(data, datalen, 1, childargs);
124*0Sstevel@tonic-gate 	(void) fprintf(childargs, "\n");
125*0Sstevel@tonic-gate 	(void) fclose(childargs);
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate 	/*
128*0Sstevel@tonic-gate 	 * Read from child
129*0Sstevel@tonic-gate 	 */
130*0Sstevel@tonic-gate 	(void) fscanf(childrslt, "%d", &yperrno);
131*0Sstevel@tonic-gate 	(void) fclose(childrslt);
132*0Sstevel@tonic-gate 
133*0Sstevel@tonic-gate 	(void) wait(&status);
134*0Sstevel@tonic-gate #ifdef WEXITSTATUS
135*0Sstevel@tonic-gate 	if (WEXITSTATUS(status) != 0) {
136*0Sstevel@tonic-gate #else
137*0Sstevel@tonic-gate 	if (status.w_retcode != 0) {
138*0Sstevel@tonic-gate #endif
139*0Sstevel@tonic-gate 		return (YPERR_YPERR);
140*0Sstevel@tonic-gate 	}
141*0Sstevel@tonic-gate 	return (yperrno);
142*0Sstevel@tonic-gate }
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate /*
145*0Sstevel@tonic-gate  * returns pid, or -1 for failure
146*0Sstevel@tonic-gate  */
147*0Sstevel@tonic-gate static int
148*0Sstevel@tonic-gate _openchild(char *command, FILE **fto, FILE **ffrom)
149*0Sstevel@tonic-gate {
150*0Sstevel@tonic-gate 	int i;
151*0Sstevel@tonic-gate 	pid_t pid;
152*0Sstevel@tonic-gate 	int pdto[2];
153*0Sstevel@tonic-gate 	int pdfrom[2];
154*0Sstevel@tonic-gate 	char *com;
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate 	if (pipe(pdto) < 0) {
157*0Sstevel@tonic-gate 		goto error1;
158*0Sstevel@tonic-gate 	}
159*0Sstevel@tonic-gate 	if (pipe(pdfrom) < 0) {
160*0Sstevel@tonic-gate 		goto error2;
161*0Sstevel@tonic-gate 	}
162*0Sstevel@tonic-gate #ifdef VFORK
163*0Sstevel@tonic-gate 	switch (pid = vfork()) {
164*0Sstevel@tonic-gate #else
165*0Sstevel@tonic-gate 	switch (pid = fork()) {
166*0Sstevel@tonic-gate #endif
167*0Sstevel@tonic-gate 	case -1:
168*0Sstevel@tonic-gate 		goto error3;
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate 	case 0:
171*0Sstevel@tonic-gate 		/*
172*0Sstevel@tonic-gate 		 * child: read from pdto[0], write into pdfrom[1]
173*0Sstevel@tonic-gate 		 */
174*0Sstevel@tonic-gate 		(void) close(0);
175*0Sstevel@tonic-gate 		(void) dup(pdto[0]);
176*0Sstevel@tonic-gate 		(void) close(1);
177*0Sstevel@tonic-gate 		(void) dup(pdfrom[1]);
178*0Sstevel@tonic-gate 		closefrom(3);
179*0Sstevel@tonic-gate 		com = malloc((unsigned)strlen(command) + 6);
180*0Sstevel@tonic-gate 		if (com == NULL) {
181*0Sstevel@tonic-gate 			_exit(~0);
182*0Sstevel@tonic-gate 		}
183*0Sstevel@tonic-gate 		(void) sprintf(com, "exec %s", command);
184*0Sstevel@tonic-gate 		execl(SHELL, basename(SHELL), "-c", com, NULL);
185*0Sstevel@tonic-gate 		_exit(~0);
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate 	default:
188*0Sstevel@tonic-gate 		/*
189*0Sstevel@tonic-gate 		 * parent: write into pdto[1], read from pdfrom[0]
190*0Sstevel@tonic-gate 		 */
191*0Sstevel@tonic-gate 		*fto = fdopen(pdto[1], "w");
192*0Sstevel@tonic-gate 		(void) close(pdto[0]);
193*0Sstevel@tonic-gate 		*ffrom = fdopen(pdfrom[0], "r");
194*0Sstevel@tonic-gate 		(void) close(pdfrom[1]);
195*0Sstevel@tonic-gate 		break;
196*0Sstevel@tonic-gate 	}
197*0Sstevel@tonic-gate 	return (pid);
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate 	/*
200*0Sstevel@tonic-gate 	 * error cleanup and return
201*0Sstevel@tonic-gate 	 */
202*0Sstevel@tonic-gate error3:
203*0Sstevel@tonic-gate 	(void) close(pdfrom[0]);
204*0Sstevel@tonic-gate 	(void) close(pdfrom[1]);
205*0Sstevel@tonic-gate error2:
206*0Sstevel@tonic-gate 	(void) close(pdto[0]);
207*0Sstevel@tonic-gate 	(void) close(pdto[1]);
208*0Sstevel@tonic-gate error1:
209*0Sstevel@tonic-gate 	return (-1);
210*0Sstevel@tonic-gate }
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate static char *
213*0Sstevel@tonic-gate basename(char *path)
214*0Sstevel@tonic-gate {
215*0Sstevel@tonic-gate 	char	*p;
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate 	p = strrchr(path, '/');
218*0Sstevel@tonic-gate 	if (p == NULL)
219*0Sstevel@tonic-gate 		return (path);
220*0Sstevel@tonic-gate 	return (p + 1);
221*0Sstevel@tonic-gate }
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate /*
224*0Sstevel@tonic-gate  * Determine if requester is allowed to update the given map,
225*0Sstevel@tonic-gate  * and update it if so. Returns the status, which is zero
226*0Sstevel@tonic-gate  * if there is no access violation, 1 otherwise.
227*0Sstevel@tonic-gate  * This function updates the local file.
228*0Sstevel@tonic-gate  */
229*0Sstevel@tonic-gate int
230*0Sstevel@tonic-gate localupdate(char *name, char *filename, uint_t op, char *data)
231*0Sstevel@tonic-gate {
232*0Sstevel@tonic-gate 	char	line[256];
233*0Sstevel@tonic-gate 	FILE	*rf;
234*0Sstevel@tonic-gate 	FILE	*wf;
235*0Sstevel@tonic-gate 	int	wfd;
236*0Sstevel@tonic-gate 	char	tmpname[80];
237*0Sstevel@tonic-gate 	int	err;
238*0Sstevel@tonic-gate 
239*0Sstevel@tonic-gate 	/*
240*0Sstevel@tonic-gate 	 * Check permission
241*0Sstevel@tonic-gate 	 */
242*0Sstevel@tonic-gate 	if (strcmp(name, "nobody") == 0) {
243*0Sstevel@tonic-gate 		/* cannot change keys for nobody */
244*0Sstevel@tonic-gate 		(void) fprintf(stderr,
245*0Sstevel@tonic-gate 			"%s: cannot change key-pair for %s\n",
246*0Sstevel@tonic-gate 			program_name, name);
247*0Sstevel@tonic-gate 		return (1);
248*0Sstevel@tonic-gate 	}
249*0Sstevel@tonic-gate 
250*0Sstevel@tonic-gate 	/*
251*0Sstevel@tonic-gate 	 * Open files
252*0Sstevel@tonic-gate 	 */
253*0Sstevel@tonic-gate 	(void) memset(tmpname, 0, 80);
254*0Sstevel@tonic-gate 	(void) sprintf(tmpname, "%s.tmp", filename);
255*0Sstevel@tonic-gate 	rf = fopen(filename, "r");
256*0Sstevel@tonic-gate 	if (rf == NULL) {
257*0Sstevel@tonic-gate 		(void) fprintf(stderr,
258*0Sstevel@tonic-gate 		"%s: cannot read %s\n", program_name, filename);
259*0Sstevel@tonic-gate 		return (1);
260*0Sstevel@tonic-gate 	}
261*0Sstevel@tonic-gate 
262*0Sstevel@tonic-gate 	(void) umask(0);
263*0Sstevel@tonic-gate 
264*0Sstevel@tonic-gate 	/*
265*0Sstevel@tonic-gate 	 * Create the new file with the correct permissions
266*0Sstevel@tonic-gate 	 */
267*0Sstevel@tonic-gate 	wfd = open(tmpname, O_CREAT|O_RDWR|O_TRUNC,
268*0Sstevel@tonic-gate 					S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
269*0Sstevel@tonic-gate 	if (wfd == -1) {
270*0Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: cannot create '%s' to write to.\n",
271*0Sstevel@tonic-gate 			program_name, tmpname);
272*0Sstevel@tonic-gate 		(void) fclose(rf);
273*0Sstevel@tonic-gate 		return (1);
274*0Sstevel@tonic-gate 	}
275*0Sstevel@tonic-gate 	wf = fdopen(wfd, "w");
276*0Sstevel@tonic-gate 	if (wf == NULL) {
277*0Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: cannot fdopen '%s'.\n",
278*0Sstevel@tonic-gate 			program_name, tmpname);
279*0Sstevel@tonic-gate 		(void) close(wfd);
280*0Sstevel@tonic-gate 		(void) fclose(rf);
281*0Sstevel@tonic-gate 		return (1);
282*0Sstevel@tonic-gate 	}
283*0Sstevel@tonic-gate 
284*0Sstevel@tonic-gate 	err = -1;
285*0Sstevel@tonic-gate 	while (fgets(line, sizeof (line), rf)) {
286*0Sstevel@tonic-gate 		if (err < 0 && match(line, name)) {
287*0Sstevel@tonic-gate 			switch (op) {
288*0Sstevel@tonic-gate 			case YPOP_INSERT:
289*0Sstevel@tonic-gate 				err = 1;
290*0Sstevel@tonic-gate 				break;
291*0Sstevel@tonic-gate 			case YPOP_STORE:
292*0Sstevel@tonic-gate 			case YPOP_CHANGE:
293*0Sstevel@tonic-gate 				(void) fprintf(wf, "%s\t%s\n", name, data);
294*0Sstevel@tonic-gate 				err = 0;
295*0Sstevel@tonic-gate 				break;
296*0Sstevel@tonic-gate 			case YPOP_DELETE:
297*0Sstevel@tonic-gate 				/* do nothing */
298*0Sstevel@tonic-gate 				err = 0;
299*0Sstevel@tonic-gate 				break;
300*0Sstevel@tonic-gate 			}
301*0Sstevel@tonic-gate 		} else {
302*0Sstevel@tonic-gate 			fputs(line, wf);
303*0Sstevel@tonic-gate 		}
304*0Sstevel@tonic-gate 	}
305*0Sstevel@tonic-gate 	if (err < 0) {
306*0Sstevel@tonic-gate 		switch (op) {
307*0Sstevel@tonic-gate 		case YPOP_CHANGE:
308*0Sstevel@tonic-gate 		case YPOP_DELETE:
309*0Sstevel@tonic-gate 			err = 1;
310*0Sstevel@tonic-gate 			break;
311*0Sstevel@tonic-gate 		case YPOP_INSERT:
312*0Sstevel@tonic-gate 		case YPOP_STORE:
313*0Sstevel@tonic-gate 			err = 0;
314*0Sstevel@tonic-gate 			(void) fprintf(wf, "%s\t%s\n", name, data);
315*0Sstevel@tonic-gate 			break;
316*0Sstevel@tonic-gate 		}
317*0Sstevel@tonic-gate 	}
318*0Sstevel@tonic-gate 	(void) fclose(wf);
319*0Sstevel@tonic-gate 	(void) fclose(rf);
320*0Sstevel@tonic-gate 	if (err == 0) {
321*0Sstevel@tonic-gate 		if (rename(tmpname, filename) < 0) {
322*0Sstevel@tonic-gate 			(void) fprintf(stderr,
323*0Sstevel@tonic-gate 				"%s: cannot rename %s to %s\n",
324*0Sstevel@tonic-gate 				program_name, tmpname, filename);
325*0Sstevel@tonic-gate 			return (1);
326*0Sstevel@tonic-gate 		}
327*0Sstevel@tonic-gate 	} else {
328*0Sstevel@tonic-gate 		if (unlink(tmpname) < 0) {
329*0Sstevel@tonic-gate 			(void) fprintf(stderr,
330*0Sstevel@tonic-gate 				"%s: cannot delete %s\n",
331*0Sstevel@tonic-gate 				program_name, tmpname);
332*0Sstevel@tonic-gate 			return (1);
333*0Sstevel@tonic-gate 		}
334*0Sstevel@tonic-gate 	}
335*0Sstevel@tonic-gate 	return (err);
336*0Sstevel@tonic-gate }
337*0Sstevel@tonic-gate 
338*0Sstevel@tonic-gate static int
339*0Sstevel@tonic-gate match(char *line, char *name)
340*0Sstevel@tonic-gate {
341*0Sstevel@tonic-gate 	int	len;
342*0Sstevel@tonic-gate 
343*0Sstevel@tonic-gate 	len = strlen(name);
344*0Sstevel@tonic-gate 	return (strncmp(line, name, len) == 0 &&
345*0Sstevel@tonic-gate 		(line[len] == ' ' || line[len] == '\t'));
346*0Sstevel@tonic-gate }
347