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  * Copyright 1992 Sun Microsystems, Inc.  All rights reserved.
23*0Sstevel@tonic-gate  * Use is subject to license terms.
24*0Sstevel@tonic-gate  */
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27*0Sstevel@tonic-gate /*	  All Rights Reserved   */
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley
31*0Sstevel@tonic-gate  * under license from the Regents of the University of
32*0Sstevel@tonic-gate  * California.
33*0Sstevel@tonic-gate  */
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate /*
38*0Sstevel@tonic-gate  * YP updater for public key map
39*0Sstevel@tonic-gate  */
40*0Sstevel@tonic-gate #include <stdio.h>
41*0Sstevel@tonic-gate #include <rpc/rpc.h>
42*0Sstevel@tonic-gate #include <rpcsvc/ypclnt.h>
43*0Sstevel@tonic-gate #include <sys/file.h>
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate extern char *malloc();
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate main(argc, argv)
48*0Sstevel@tonic-gate 	int argc;
49*0Sstevel@tonic-gate 	char *argv[];
50*0Sstevel@tonic-gate {
51*0Sstevel@tonic-gate 	unsigned op;
52*0Sstevel@tonic-gate 	char name[MAXNETNAMELEN + 1];
53*0Sstevel@tonic-gate 	char key[256];
54*0Sstevel@tonic-gate 	char data[256];
55*0Sstevel@tonic-gate 	char line[256];
56*0Sstevel@tonic-gate 	unsigned keylen;
57*0Sstevel@tonic-gate 	unsigned datalen;
58*0Sstevel@tonic-gate 	FILE *rf;
59*0Sstevel@tonic-gate 	FILE *wf;
60*0Sstevel@tonic-gate 	char *fname;
61*0Sstevel@tonic-gate 	char *tmpname;
62*0Sstevel@tonic-gate 	int err;
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate 	if (argc !=  3) {
66*0Sstevel@tonic-gate 		exit(YPERR_YPERR);
67*0Sstevel@tonic-gate 	}
68*0Sstevel@tonic-gate 	fname = argv[1];
69*0Sstevel@tonic-gate 	tmpname = malloc(strlen(fname) + 4);
70*0Sstevel@tonic-gate 	if (tmpname == NULL) {
71*0Sstevel@tonic-gate 		exit(YPERR_YPERR);
72*0Sstevel@tonic-gate 	}
73*0Sstevel@tonic-gate 	sprintf(tmpname, "%s.tmp", fname);
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate 	/*
76*0Sstevel@tonic-gate 	 * Get input
77*0Sstevel@tonic-gate 	 */
78*0Sstevel@tonic-gate 	if (! scanf("%s\n", name)) {
79*0Sstevel@tonic-gate 		exit(YPERR_YPERR);
80*0Sstevel@tonic-gate 	}
81*0Sstevel@tonic-gate 	if (! scanf("%u\n", &op)) {
82*0Sstevel@tonic-gate 		exit(YPERR_YPERR);
83*0Sstevel@tonic-gate 	}
84*0Sstevel@tonic-gate 	if (! scanf("%u\n", &keylen)) {
85*0Sstevel@tonic-gate 		exit(YPERR_YPERR);
86*0Sstevel@tonic-gate 	}
87*0Sstevel@tonic-gate 	if (! fread(key, keylen, 1, stdin)) {
88*0Sstevel@tonic-gate 		exit(YPERR_YPERR);
89*0Sstevel@tonic-gate 	}
90*0Sstevel@tonic-gate 	key[keylen] = 0;
91*0Sstevel@tonic-gate 	if (! scanf("%u\n", &datalen)) {
92*0Sstevel@tonic-gate 		exit(YPERR_YPERR);
93*0Sstevel@tonic-gate 	}
94*0Sstevel@tonic-gate 	if (! fread(data, datalen, 1, stdin)) {
95*0Sstevel@tonic-gate 		exit(YPERR_YPERR);
96*0Sstevel@tonic-gate 	}
97*0Sstevel@tonic-gate 	data[datalen] = 0;
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate 	/*
100*0Sstevel@tonic-gate 	 * Check permission
101*0Sstevel@tonic-gate 	 */
102*0Sstevel@tonic-gate 	if (strcmp(name, key) != 0) {
103*0Sstevel@tonic-gate 		exit(YPERR_ACCESS);
104*0Sstevel@tonic-gate 	}
105*0Sstevel@tonic-gate 	if (strcmp(name, "nobody") == 0) {
106*0Sstevel@tonic-gate 		/*
107*0Sstevel@tonic-gate 		 * Can't change "nobody"s key.
108*0Sstevel@tonic-gate 		 */
109*0Sstevel@tonic-gate 		exit(YPERR_ACCESS);
110*0Sstevel@tonic-gate 	}
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate 	/*
113*0Sstevel@tonic-gate 	 * Open files
114*0Sstevel@tonic-gate 	 */
115*0Sstevel@tonic-gate 	rf = fopen(fname, "r");
116*0Sstevel@tonic-gate 	if (rf == NULL) {
117*0Sstevel@tonic-gate 		exit(YPERR_YPERR);
118*0Sstevel@tonic-gate 	}
119*0Sstevel@tonic-gate 	wf = fopen(tmpname, "w");
120*0Sstevel@tonic-gate 	if (wf == NULL) {
121*0Sstevel@tonic-gate 		exit(YPERR_YPERR);
122*0Sstevel@tonic-gate 	}
123*0Sstevel@tonic-gate 	err = -1;
124*0Sstevel@tonic-gate 	while (fgets(line, sizeof (line), rf)) {
125*0Sstevel@tonic-gate 		if (err < 0 && match(line, name)) {
126*0Sstevel@tonic-gate 			switch (op) {
127*0Sstevel@tonic-gate 			case YPOP_INSERT:
128*0Sstevel@tonic-gate 				err = YPERR_KEY;
129*0Sstevel@tonic-gate 				break;
130*0Sstevel@tonic-gate 			case YPOP_STORE:
131*0Sstevel@tonic-gate 			case YPOP_CHANGE:
132*0Sstevel@tonic-gate 				fprintf(wf, "%s %s\n", key, data);
133*0Sstevel@tonic-gate 				err = 0;
134*0Sstevel@tonic-gate 				break;
135*0Sstevel@tonic-gate 			case YPOP_DELETE:
136*0Sstevel@tonic-gate 				/* do nothing */
137*0Sstevel@tonic-gate 				err = 0;
138*0Sstevel@tonic-gate 				break;
139*0Sstevel@tonic-gate 			}
140*0Sstevel@tonic-gate 		} else {
141*0Sstevel@tonic-gate 			fputs(line, wf);
142*0Sstevel@tonic-gate 		}
143*0Sstevel@tonic-gate 	}
144*0Sstevel@tonic-gate 	if (err < 0) {
145*0Sstevel@tonic-gate 		switch (op) {
146*0Sstevel@tonic-gate 		case YPOP_CHANGE:
147*0Sstevel@tonic-gate 		case YPOP_DELETE:
148*0Sstevel@tonic-gate 			err = YPERR_KEY;
149*0Sstevel@tonic-gate 			break;
150*0Sstevel@tonic-gate 		case YPOP_INSERT:
151*0Sstevel@tonic-gate 		case YPOP_STORE:
152*0Sstevel@tonic-gate 			err = 0;
153*0Sstevel@tonic-gate 			fprintf(wf, "%s %s\n", key, data);
154*0Sstevel@tonic-gate 			break;
155*0Sstevel@tonic-gate 		}
156*0Sstevel@tonic-gate 	}
157*0Sstevel@tonic-gate 	fclose(wf);
158*0Sstevel@tonic-gate 	fclose(rf);
159*0Sstevel@tonic-gate 	if (err == 0) {
160*0Sstevel@tonic-gate 		if (rename(tmpname, fname) < 0) {
161*0Sstevel@tonic-gate 			exit(YPERR_YPERR);
162*0Sstevel@tonic-gate 		}
163*0Sstevel@tonic-gate 	} else {
164*0Sstevel@tonic-gate 		if (unlink(tmpname) < 0) {
165*0Sstevel@tonic-gate 			exit(YPERR_YPERR);
166*0Sstevel@tonic-gate 		}
167*0Sstevel@tonic-gate 	}
168*0Sstevel@tonic-gate 	if (fork() == 0) {
169*0Sstevel@tonic-gate 		close(0); close(1); close(2);
170*0Sstevel@tonic-gate 		open("/dev/null", O_RDWR, 0);
171*0Sstevel@tonic-gate 		dup(0); dup(0);
172*0Sstevel@tonic-gate 		execl("/bin/sh", "sh", "-c", argv[2], NULL);
173*0Sstevel@tonic-gate 	}
174*0Sstevel@tonic-gate 	exit(err);
175*0Sstevel@tonic-gate 	/* NOTREACHED */
176*0Sstevel@tonic-gate }
177*0Sstevel@tonic-gate 
178*0Sstevel@tonic-gate 
179*0Sstevel@tonic-gate match(line, name)
180*0Sstevel@tonic-gate 	char *line;
181*0Sstevel@tonic-gate 	char *name;
182*0Sstevel@tonic-gate {
183*0Sstevel@tonic-gate 	int len;
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate 	len = strlen(name);
186*0Sstevel@tonic-gate 	return (strncmp(line, name, len) == 0 &&
187*0Sstevel@tonic-gate 		(line[len] == ' ' || line[len] == '\t'));
188*0Sstevel@tonic-gate }
189