xref: /illumos-gate/usr/src/cmd/cmd-inet/usr.bin/rdist/main.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright 1998-2003 Sun Microsystems, Inc.  All rights reserved.
3*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
4*7c478bd9Sstevel@tonic-gate  */
5*7c478bd9Sstevel@tonic-gate 
6*7c478bd9Sstevel@tonic-gate /*
7*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1983 Regents of the University of California.
8*7c478bd9Sstevel@tonic-gate  * All rights reserved.
9*7c478bd9Sstevel@tonic-gate  *
10*7c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms are permitted
11*7c478bd9Sstevel@tonic-gate  * provided that the above copyright notice and this paragraph are
12*7c478bd9Sstevel@tonic-gate  * duplicated in all such forms and that any documentation,
13*7c478bd9Sstevel@tonic-gate  * advertising materials, and other materials related to such
14*7c478bd9Sstevel@tonic-gate  * distribution and use acknowledge that the software was developed
15*7c478bd9Sstevel@tonic-gate  * by the University of California, Berkeley.  The name of the
16*7c478bd9Sstevel@tonic-gate  * University may not be used to endorse or promote products derived
17*7c478bd9Sstevel@tonic-gate  * from this software without specific prior written permission.
18*7c478bd9Sstevel@tonic-gate  */
19*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
20*7c478bd9Sstevel@tonic-gate 
21*7c478bd9Sstevel@tonic-gate #include "defs.h"
22*7c478bd9Sstevel@tonic-gate #include <string.h>
23*7c478bd9Sstevel@tonic-gate #include <syslog.h>
24*7c478bd9Sstevel@tonic-gate #include <krb5defs.h>
25*7c478bd9Sstevel@tonic-gate #include <k5-int.h>
26*7c478bd9Sstevel@tonic-gate #include <priv_utils.h>
27*7c478bd9Sstevel@tonic-gate 
28*7c478bd9Sstevel@tonic-gate #define	NHOSTS 100
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate  * Remote distribution program.
32*7c478bd9Sstevel@tonic-gate  */
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate char	*distfile = NULL;
35*7c478bd9Sstevel@tonic-gate char	Tmpfile[] = "/tmp/rdistXXXXXX";
36*7c478bd9Sstevel@tonic-gate char	*tmpname = &Tmpfile[5];
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate int	debug;		/* debugging flag */
39*7c478bd9Sstevel@tonic-gate int	nflag;		/* NOP flag, just print commands without executing */
40*7c478bd9Sstevel@tonic-gate int	qflag;		/* Quiet. Don't print messages */
41*7c478bd9Sstevel@tonic-gate int	options;	/* global options */
42*7c478bd9Sstevel@tonic-gate int	iamremote;	/* act as remote server for transfering files */
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate FILE	*fin = NULL;	/* input file pointer */
45*7c478bd9Sstevel@tonic-gate int	rem = -1;	/* file descriptor to remote source/sink process */
46*7c478bd9Sstevel@tonic-gate char	host[32];	/* host name */
47*7c478bd9Sstevel@tonic-gate int	nerrs;		/* number of errors while sending/receiving */
48*7c478bd9Sstevel@tonic-gate char	user[10];	/* user's name */
49*7c478bd9Sstevel@tonic-gate char	homedir[128];	/* user's home directory */
50*7c478bd9Sstevel@tonic-gate char	buf[RDIST_BUFSIZ];	/* general purpose buffer */
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate struct	passwd *pw;	/* pointer to static area used by getpwent */
53*7c478bd9Sstevel@tonic-gate struct	group *gr;	/* pointer to static area used by getgrent */
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate char des_inbuf[2 * RDIST_BUFSIZ];	/* needs to be > largest read size */
56*7c478bd9Sstevel@tonic-gate char des_outbuf[2 * RDIST_BUFSIZ];	/* needs to be > largest write size */
57*7c478bd9Sstevel@tonic-gate krb5_data desinbuf, desoutbuf;
58*7c478bd9Sstevel@tonic-gate krb5_encrypt_block eblock;		/* eblock for encrypt/decrypt */
59*7c478bd9Sstevel@tonic-gate krb5_context bsd_context;
60*7c478bd9Sstevel@tonic-gate krb5_auth_context auth_context;
61*7c478bd9Sstevel@tonic-gate krb5_creds *cred;
62*7c478bd9Sstevel@tonic-gate char *krb_cache = NULL;
63*7c478bd9Sstevel@tonic-gate krb5_flags authopts;
64*7c478bd9Sstevel@tonic-gate krb5_error_code status;
65*7c478bd9Sstevel@tonic-gate enum kcmd_proto kcmd_proto = KCMD_NEW_PROTOCOL;
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate int encrypt_flag = 0;	/* Flag set when encryption is used */
68*7c478bd9Sstevel@tonic-gate int krb5auth_flag = 0;	/* Flag set, when KERBEROS is enabled */
69*7c478bd9Sstevel@tonic-gate int debug_port = 0;
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate int retval = 0;
72*7c478bd9Sstevel@tonic-gate char *krb_realm = NULL;
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate /* Flag set, if -PN / -PO is specified */
75*7c478bd9Sstevel@tonic-gate static boolean_t rcmdoption_done = B_FALSE;
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate static int encrypt_done = 0;	/* Flag set, if -x is specified */
78*7c478bd9Sstevel@tonic-gate profile_options_boolean option[] = {
79*7c478bd9Sstevel@tonic-gate 	{ "encrypt", &encrypt_flag, 0 },
80*7c478bd9Sstevel@tonic-gate 	{ NULL, NULL, 0 }
81*7c478bd9Sstevel@tonic-gate };
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate static char *rcmdproto = NULL;
84*7c478bd9Sstevel@tonic-gate profile_option_strings rcmdversion[] = {
85*7c478bd9Sstevel@tonic-gate 	{ "rcmd_protocol", &rcmdproto, 0 },
86*7c478bd9Sstevel@tonic-gate 	{ NULL, NULL, 0 }
87*7c478bd9Sstevel@tonic-gate };
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate char *realmdef[] = { "realms", NULL, "rdist", NULL };
90*7c478bd9Sstevel@tonic-gate char *appdef[] = { "appdefaults", "rdist", NULL };
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate int
93*7c478bd9Sstevel@tonic-gate main(argc, argv)
94*7c478bd9Sstevel@tonic-gate 	int argc;
95*7c478bd9Sstevel@tonic-gate 	char *argv[];
96*7c478bd9Sstevel@tonic-gate {
97*7c478bd9Sstevel@tonic-gate 	register char *arg;
98*7c478bd9Sstevel@tonic-gate 	int cmdargs = 0;
99*7c478bd9Sstevel@tonic-gate 	char *dhosts[NHOSTS], **hp = dhosts;
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate 	pw = getpwuid(getuid());
104*7c478bd9Sstevel@tonic-gate 	if (pw == NULL) {
105*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: Who are you?\n"), argv[0]);
106*7c478bd9Sstevel@tonic-gate 		exit(1);
107*7c478bd9Sstevel@tonic-gate 	}
108*7c478bd9Sstevel@tonic-gate 	strncpy(user, pw->pw_name, sizeof (user));
109*7c478bd9Sstevel@tonic-gate 	user[sizeof (user) - 1] = '\0';
110*7c478bd9Sstevel@tonic-gate 	strncpy(homedir, pw->pw_dir, sizeof (homedir));
111*7c478bd9Sstevel@tonic-gate 	homedir[sizeof (homedir) - 1] = '\0';
112*7c478bd9Sstevel@tonic-gate 	gethostname(host, sizeof (host));
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 	while (--argc > 0) {
115*7c478bd9Sstevel@tonic-gate 		if ((arg = *++argv)[0] != '-')
116*7c478bd9Sstevel@tonic-gate 			break;
117*7c478bd9Sstevel@tonic-gate 		if ((strcmp(arg, "-Server") == 0))
118*7c478bd9Sstevel@tonic-gate 			iamremote++;
119*7c478bd9Sstevel@tonic-gate 		else while (*++arg) {
120*7c478bd9Sstevel@tonic-gate 			if (strncmp(*argv, "-PO", 3) == 0) {
121*7c478bd9Sstevel@tonic-gate 				if (rcmdoption_done == B_TRUE) {
122*7c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext("rdist: "
123*7c478bd9Sstevel@tonic-gate 						"Only one of -PN "
124*7c478bd9Sstevel@tonic-gate 						"and -PO allowed.\n"));
125*7c478bd9Sstevel@tonic-gate 					usage();
126*7c478bd9Sstevel@tonic-gate 				}
127*7c478bd9Sstevel@tonic-gate 				kcmd_proto = KCMD_OLD_PROTOCOL;
128*7c478bd9Sstevel@tonic-gate 				krb5auth_flag++;
129*7c478bd9Sstevel@tonic-gate 				rcmdoption_done = B_TRUE;
130*7c478bd9Sstevel@tonic-gate 				break;
131*7c478bd9Sstevel@tonic-gate 			}
132*7c478bd9Sstevel@tonic-gate 			if (strncmp(*argv, "-PN", 3) == 0) {
133*7c478bd9Sstevel@tonic-gate 				if (rcmdoption_done == B_TRUE) {
134*7c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext("rdist: "
135*7c478bd9Sstevel@tonic-gate 						"Only one of -PN "
136*7c478bd9Sstevel@tonic-gate 						"and -PO allowed.\n"));
137*7c478bd9Sstevel@tonic-gate 					usage();
138*7c478bd9Sstevel@tonic-gate 				}
139*7c478bd9Sstevel@tonic-gate 				kcmd_proto = KCMD_NEW_PROTOCOL;
140*7c478bd9Sstevel@tonic-gate 				krb5auth_flag++;
141*7c478bd9Sstevel@tonic-gate 				rcmdoption_done = B_TRUE;
142*7c478bd9Sstevel@tonic-gate 				break;
143*7c478bd9Sstevel@tonic-gate 			}
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate 			switch (*arg) {
146*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
147*7c478bd9Sstevel@tonic-gate 			case 'p':
148*7c478bd9Sstevel@tonic-gate 				if (--argc <= 0)
149*7c478bd9Sstevel@tonic-gate 					usage();
150*7c478bd9Sstevel@tonic-gate 				debug_port = htons(atoi(*++argv));
151*7c478bd9Sstevel@tonic-gate 				break;
152*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */
153*7c478bd9Sstevel@tonic-gate 			case 'k':
154*7c478bd9Sstevel@tonic-gate 				if (--argc <= 0) {
155*7c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext("rdist: "
156*7c478bd9Sstevel@tonic-gate 						"-k flag must be followed with "
157*7c478bd9Sstevel@tonic-gate 						" a realm name.\n"));
158*7c478bd9Sstevel@tonic-gate 					exit(1);
159*7c478bd9Sstevel@tonic-gate 				}
160*7c478bd9Sstevel@tonic-gate 				if ((krb_realm = strdup(*++argv)) == NULL) {
161*7c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext("rdist: "
162*7c478bd9Sstevel@tonic-gate 						"Cannot malloc.\n"));
163*7c478bd9Sstevel@tonic-gate 					exit(1);
164*7c478bd9Sstevel@tonic-gate 				}
165*7c478bd9Sstevel@tonic-gate 				krb5auth_flag++;
166*7c478bd9Sstevel@tonic-gate 				break;
167*7c478bd9Sstevel@tonic-gate 
168*7c478bd9Sstevel@tonic-gate 			case 'a':
169*7c478bd9Sstevel@tonic-gate 				krb5auth_flag++;
170*7c478bd9Sstevel@tonic-gate 				break;
171*7c478bd9Sstevel@tonic-gate 
172*7c478bd9Sstevel@tonic-gate 			case 'x':
173*7c478bd9Sstevel@tonic-gate 				encrypt_flag++;
174*7c478bd9Sstevel@tonic-gate 				encrypt_done++;
175*7c478bd9Sstevel@tonic-gate 				krb5auth_flag++;
176*7c478bd9Sstevel@tonic-gate 				break;
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate 			case 'f':
179*7c478bd9Sstevel@tonic-gate 				if (--argc <= 0)
180*7c478bd9Sstevel@tonic-gate 					usage();
181*7c478bd9Sstevel@tonic-gate 				distfile = *++argv;
182*7c478bd9Sstevel@tonic-gate 				if (distfile[0] == '-' && distfile[1] == '\0')
183*7c478bd9Sstevel@tonic-gate 					fin = stdin;
184*7c478bd9Sstevel@tonic-gate 				break;
185*7c478bd9Sstevel@tonic-gate 
186*7c478bd9Sstevel@tonic-gate 			case 'm':
187*7c478bd9Sstevel@tonic-gate 				if (--argc <= 0)
188*7c478bd9Sstevel@tonic-gate 					usage();
189*7c478bd9Sstevel@tonic-gate 				if (hp >= &dhosts[NHOSTS-2]) {
190*7c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext("rdist:"
191*7c478bd9Sstevel@tonic-gate 						" too many destination"
192*7c478bd9Sstevel@tonic-gate 						" hosts\n"));
193*7c478bd9Sstevel@tonic-gate 					exit(1);
194*7c478bd9Sstevel@tonic-gate 				}
195*7c478bd9Sstevel@tonic-gate 				*hp++ = *++argv;
196*7c478bd9Sstevel@tonic-gate 				break;
197*7c478bd9Sstevel@tonic-gate 
198*7c478bd9Sstevel@tonic-gate 			case 'd':
199*7c478bd9Sstevel@tonic-gate 				if (--argc <= 0)
200*7c478bd9Sstevel@tonic-gate 					usage();
201*7c478bd9Sstevel@tonic-gate 				define(*++argv);
202*7c478bd9Sstevel@tonic-gate 				break;
203*7c478bd9Sstevel@tonic-gate 
204*7c478bd9Sstevel@tonic-gate 			case 'D':
205*7c478bd9Sstevel@tonic-gate 				debug++;
206*7c478bd9Sstevel@tonic-gate 				break;
207*7c478bd9Sstevel@tonic-gate 
208*7c478bd9Sstevel@tonic-gate 			case 'c':
209*7c478bd9Sstevel@tonic-gate 				cmdargs++;
210*7c478bd9Sstevel@tonic-gate 				break;
211*7c478bd9Sstevel@tonic-gate 
212*7c478bd9Sstevel@tonic-gate 			case 'n':
213*7c478bd9Sstevel@tonic-gate 				if (options & VERIFY) {
214*7c478bd9Sstevel@tonic-gate 					printf("rdist: -n overrides -v\n");
215*7c478bd9Sstevel@tonic-gate 					options &= ~VERIFY;
216*7c478bd9Sstevel@tonic-gate 				}
217*7c478bd9Sstevel@tonic-gate 				nflag++;
218*7c478bd9Sstevel@tonic-gate 				break;
219*7c478bd9Sstevel@tonic-gate 
220*7c478bd9Sstevel@tonic-gate 			case 'q':
221*7c478bd9Sstevel@tonic-gate 				qflag++;
222*7c478bd9Sstevel@tonic-gate 				break;
223*7c478bd9Sstevel@tonic-gate 
224*7c478bd9Sstevel@tonic-gate 			case 'b':
225*7c478bd9Sstevel@tonic-gate 				options |= COMPARE;
226*7c478bd9Sstevel@tonic-gate 				break;
227*7c478bd9Sstevel@tonic-gate 
228*7c478bd9Sstevel@tonic-gate 			case 'R':
229*7c478bd9Sstevel@tonic-gate 				options |= REMOVE;
230*7c478bd9Sstevel@tonic-gate 				break;
231*7c478bd9Sstevel@tonic-gate 
232*7c478bd9Sstevel@tonic-gate 			case 'v':
233*7c478bd9Sstevel@tonic-gate 				if (nflag) {
234*7c478bd9Sstevel@tonic-gate 					printf("rdist: -n overrides -v\n");
235*7c478bd9Sstevel@tonic-gate 					break;
236*7c478bd9Sstevel@tonic-gate 				}
237*7c478bd9Sstevel@tonic-gate 				options |= VERIFY;
238*7c478bd9Sstevel@tonic-gate 				break;
239*7c478bd9Sstevel@tonic-gate 
240*7c478bd9Sstevel@tonic-gate 			case 'w':
241*7c478bd9Sstevel@tonic-gate 				options |= WHOLE;
242*7c478bd9Sstevel@tonic-gate 				break;
243*7c478bd9Sstevel@tonic-gate 
244*7c478bd9Sstevel@tonic-gate 			case 'y':
245*7c478bd9Sstevel@tonic-gate 				options |= YOUNGER;
246*7c478bd9Sstevel@tonic-gate 				break;
247*7c478bd9Sstevel@tonic-gate 
248*7c478bd9Sstevel@tonic-gate 			case 'h':
249*7c478bd9Sstevel@tonic-gate 				options |= FOLLOW;
250*7c478bd9Sstevel@tonic-gate 				break;
251*7c478bd9Sstevel@tonic-gate 
252*7c478bd9Sstevel@tonic-gate 			case 'i':
253*7c478bd9Sstevel@tonic-gate 				options |= IGNLNKS;
254*7c478bd9Sstevel@tonic-gate 				break;
255*7c478bd9Sstevel@tonic-gate 
256*7c478bd9Sstevel@tonic-gate 			default:
257*7c478bd9Sstevel@tonic-gate 				usage();
258*7c478bd9Sstevel@tonic-gate 			}
259*7c478bd9Sstevel@tonic-gate 		}
260*7c478bd9Sstevel@tonic-gate 	}
261*7c478bd9Sstevel@tonic-gate 	*hp = NULL;
262*7c478bd9Sstevel@tonic-gate 
263*7c478bd9Sstevel@tonic-gate 	mktemp(Tmpfile);
264*7c478bd9Sstevel@tonic-gate 
265*7c478bd9Sstevel@tonic-gate 	if (krb5auth_flag > 0) {
266*7c478bd9Sstevel@tonic-gate 		status = krb5_init_context(&bsd_context);
267*7c478bd9Sstevel@tonic-gate 		if (status) {
268*7c478bd9Sstevel@tonic-gate 			com_err("rdist", status,
269*7c478bd9Sstevel@tonic-gate 				gettext("while initializing krb5"));
270*7c478bd9Sstevel@tonic-gate 			exit(1);
271*7c478bd9Sstevel@tonic-gate 		}
272*7c478bd9Sstevel@tonic-gate 
273*7c478bd9Sstevel@tonic-gate 		/* Set up des buffers */
274*7c478bd9Sstevel@tonic-gate 		desinbuf.data = des_inbuf;
275*7c478bd9Sstevel@tonic-gate 		desoutbuf.data = des_outbuf;
276*7c478bd9Sstevel@tonic-gate 		desinbuf.length = sizeof (des_inbuf);
277*7c478bd9Sstevel@tonic-gate 		desoutbuf.length = sizeof (des_outbuf);
278*7c478bd9Sstevel@tonic-gate 
279*7c478bd9Sstevel@tonic-gate 		/*
280*7c478bd9Sstevel@tonic-gate 		 * Get our local realm to look up local realm options.
281*7c478bd9Sstevel@tonic-gate 		 */
282*7c478bd9Sstevel@tonic-gate 		status = krb5_get_default_realm(bsd_context, &realmdef[1]);
283*7c478bd9Sstevel@tonic-gate 		if (status) {
284*7c478bd9Sstevel@tonic-gate 			com_err("rdist", status,
285*7c478bd9Sstevel@tonic-gate 				gettext("while getting default realm"));
286*7c478bd9Sstevel@tonic-gate 			exit(1);
287*7c478bd9Sstevel@tonic-gate 		}
288*7c478bd9Sstevel@tonic-gate 		/*
289*7c478bd9Sstevel@tonic-gate 		 * See if encryption should be done for this realm
290*7c478bd9Sstevel@tonic-gate 		 */
291*7c478bd9Sstevel@tonic-gate 		profile_get_options_boolean(bsd_context->profile, realmdef,
292*7c478bd9Sstevel@tonic-gate 						option);
293*7c478bd9Sstevel@tonic-gate 		/*
294*7c478bd9Sstevel@tonic-gate 		 * Check the appdefaults section
295*7c478bd9Sstevel@tonic-gate 		 */
296*7c478bd9Sstevel@tonic-gate 		profile_get_options_boolean(bsd_context->profile, appdef,
297*7c478bd9Sstevel@tonic-gate 						option);
298*7c478bd9Sstevel@tonic-gate 		profile_get_options_string(bsd_context->profile, appdef,
299*7c478bd9Sstevel@tonic-gate 						rcmdversion);
300*7c478bd9Sstevel@tonic-gate 
301*7c478bd9Sstevel@tonic-gate 		if ((encrypt_done > 0) || (encrypt_flag > 0)) {
302*7c478bd9Sstevel@tonic-gate 			if (krb5_privacy_allowed() == TRUE) {
303*7c478bd9Sstevel@tonic-gate 				encrypt_flag++;
304*7c478bd9Sstevel@tonic-gate 			} else {
305*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("rdist: "
306*7c478bd9Sstevel@tonic-gate 						"Encryption not supported.\n"));
307*7c478bd9Sstevel@tonic-gate 				exit(1);
308*7c478bd9Sstevel@tonic-gate 			}
309*7c478bd9Sstevel@tonic-gate 		}
310*7c478bd9Sstevel@tonic-gate 
311*7c478bd9Sstevel@tonic-gate 		if ((rcmdoption_done == B_FALSE) && (rcmdproto != NULL)) {
312*7c478bd9Sstevel@tonic-gate 			if (strncmp(rcmdproto, "rcmdv2", 6) == 0) {
313*7c478bd9Sstevel@tonic-gate 				kcmd_proto = KCMD_NEW_PROTOCOL;
314*7c478bd9Sstevel@tonic-gate 			} else if (strncmp(rcmdproto, "rcmdv1", 6) == 0) {
315*7c478bd9Sstevel@tonic-gate 				kcmd_proto = KCMD_OLD_PROTOCOL;
316*7c478bd9Sstevel@tonic-gate 			} else {
317*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("Unrecognized "
318*7c478bd9Sstevel@tonic-gate 					"KCMD protocol (%s)"), rcmdproto);
319*7c478bd9Sstevel@tonic-gate 				exit(1);
320*7c478bd9Sstevel@tonic-gate 			}
321*7c478bd9Sstevel@tonic-gate 		}
322*7c478bd9Sstevel@tonic-gate 	}
323*7c478bd9Sstevel@tonic-gate 
324*7c478bd9Sstevel@tonic-gate 	if (iamremote) {
325*7c478bd9Sstevel@tonic-gate 		setreuid(getuid(), getuid());
326*7c478bd9Sstevel@tonic-gate 		server();
327*7c478bd9Sstevel@tonic-gate 		exit(nerrs != 0);
328*7c478bd9Sstevel@tonic-gate 	}
329*7c478bd9Sstevel@tonic-gate 	if (__init_suid_priv(0, PRIV_NET_PRIVADDR, NULL) == -1) {
330*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
331*7c478bd9Sstevel@tonic-gate 			"rdist needs to run with sufficient privilege\n");
332*7c478bd9Sstevel@tonic-gate 		exit(1);
333*7c478bd9Sstevel@tonic-gate 	}
334*7c478bd9Sstevel@tonic-gate 
335*7c478bd9Sstevel@tonic-gate 	if (cmdargs)
336*7c478bd9Sstevel@tonic-gate 		docmdargs(argc, argv);
337*7c478bd9Sstevel@tonic-gate 	else {
338*7c478bd9Sstevel@tonic-gate 		if (fin == NULL) {
339*7c478bd9Sstevel@tonic-gate 			if (distfile == NULL) {
340*7c478bd9Sstevel@tonic-gate 				if ((fin = fopen("distfile", "r")) == NULL)
341*7c478bd9Sstevel@tonic-gate 					fin = fopen("Distfile", "r");
342*7c478bd9Sstevel@tonic-gate 			} else
343*7c478bd9Sstevel@tonic-gate 				fin = fopen(distfile, "r");
344*7c478bd9Sstevel@tonic-gate 			if (fin == NULL) {
345*7c478bd9Sstevel@tonic-gate 				perror(distfile ? distfile : "distfile");
346*7c478bd9Sstevel@tonic-gate 				exit(1);
347*7c478bd9Sstevel@tonic-gate 			}
348*7c478bd9Sstevel@tonic-gate 		}
349*7c478bd9Sstevel@tonic-gate 		yyparse();
350*7c478bd9Sstevel@tonic-gate 		if (nerrs == 0)
351*7c478bd9Sstevel@tonic-gate 			docmds(dhosts, argc, argv);
352*7c478bd9Sstevel@tonic-gate 	}
353*7c478bd9Sstevel@tonic-gate 
354*7c478bd9Sstevel@tonic-gate 	exit(nerrs != 0);
355*7c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
356*7c478bd9Sstevel@tonic-gate }
357*7c478bd9Sstevel@tonic-gate 
358*7c478bd9Sstevel@tonic-gate usage()
359*7c478bd9Sstevel@tonic-gate {
360*7c478bd9Sstevel@tonic-gate 	printf(gettext("Usage: rdist [-nqbhirvwyDax] [-PN / -PO] "
361*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
362*7c478bd9Sstevel@tonic-gate 	"[-p port] "
363*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */
364*7c478bd9Sstevel@tonic-gate 	"[-k realm] [-f distfile] [-d var=value] [-m host] [file ...]\n"));
365*7c478bd9Sstevel@tonic-gate 	printf(gettext("or: rdist [-nqbhirvwyDax] [-PN / -PO] [-p port] "
366*7c478bd9Sstevel@tonic-gate 	"[-k realm] -c source [...] machine[:dest]\n"));
367*7c478bd9Sstevel@tonic-gate 	exit(1);
368*7c478bd9Sstevel@tonic-gate }
369*7c478bd9Sstevel@tonic-gate 
370*7c478bd9Sstevel@tonic-gate /*
371*7c478bd9Sstevel@tonic-gate  * rcp like interface for distributing files.
372*7c478bd9Sstevel@tonic-gate  */
373*7c478bd9Sstevel@tonic-gate docmdargs(nargs, args)
374*7c478bd9Sstevel@tonic-gate 	int nargs;
375*7c478bd9Sstevel@tonic-gate 	char *args[];
376*7c478bd9Sstevel@tonic-gate {
377*7c478bd9Sstevel@tonic-gate 	register struct namelist *nl, *prev;
378*7c478bd9Sstevel@tonic-gate 	register char *cp;
379*7c478bd9Sstevel@tonic-gate 	struct namelist *files, *hosts;
380*7c478bd9Sstevel@tonic-gate 	struct subcmd *cmds;
381*7c478bd9Sstevel@tonic-gate 	char *dest;
382*7c478bd9Sstevel@tonic-gate 	static struct namelist tnl = { NULL, NULL };
383*7c478bd9Sstevel@tonic-gate 	int i;
384*7c478bd9Sstevel@tonic-gate 
385*7c478bd9Sstevel@tonic-gate 	if (nargs < 2)
386*7c478bd9Sstevel@tonic-gate 		usage();
387*7c478bd9Sstevel@tonic-gate 
388*7c478bd9Sstevel@tonic-gate 	prev = NULL;
389*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < nargs - 1; i++) {
390*7c478bd9Sstevel@tonic-gate 		nl = makenl(args[i]);
391*7c478bd9Sstevel@tonic-gate 		if (prev == NULL)
392*7c478bd9Sstevel@tonic-gate 			files = prev = nl;
393*7c478bd9Sstevel@tonic-gate 		else {
394*7c478bd9Sstevel@tonic-gate 			prev->n_next = nl;
395*7c478bd9Sstevel@tonic-gate 			prev = nl;
396*7c478bd9Sstevel@tonic-gate 		}
397*7c478bd9Sstevel@tonic-gate 	}
398*7c478bd9Sstevel@tonic-gate 
399*7c478bd9Sstevel@tonic-gate 	cp = args[i];
400*7c478bd9Sstevel@tonic-gate 	if ((dest = index(cp, ':')) != NULL)
401*7c478bd9Sstevel@tonic-gate 		*dest++ = '\0';
402*7c478bd9Sstevel@tonic-gate 	tnl.n_name = cp;
403*7c478bd9Sstevel@tonic-gate 	hosts = expand(&tnl, E_ALL);
404*7c478bd9Sstevel@tonic-gate 	if (nerrs)
405*7c478bd9Sstevel@tonic-gate 		exit(1);
406*7c478bd9Sstevel@tonic-gate 
407*7c478bd9Sstevel@tonic-gate 	if (dest == NULL || *dest == '\0')
408*7c478bd9Sstevel@tonic-gate 		cmds = NULL;
409*7c478bd9Sstevel@tonic-gate 	else {
410*7c478bd9Sstevel@tonic-gate 		cmds = makesubcmd(INSTALL);
411*7c478bd9Sstevel@tonic-gate 		cmds->sc_options = options;
412*7c478bd9Sstevel@tonic-gate 		cmds->sc_name = dest;
413*7c478bd9Sstevel@tonic-gate 	}
414*7c478bd9Sstevel@tonic-gate 
415*7c478bd9Sstevel@tonic-gate 	if (debug) {
416*7c478bd9Sstevel@tonic-gate 		printf("docmdargs()\nfiles = ");
417*7c478bd9Sstevel@tonic-gate 		prnames(files);
418*7c478bd9Sstevel@tonic-gate 		printf("hosts = ");
419*7c478bd9Sstevel@tonic-gate 		prnames(hosts);
420*7c478bd9Sstevel@tonic-gate 	}
421*7c478bd9Sstevel@tonic-gate 	insert(NULL, files, hosts, cmds);
422*7c478bd9Sstevel@tonic-gate 	docmds(NULL, 0, NULL);
423*7c478bd9Sstevel@tonic-gate }
424*7c478bd9Sstevel@tonic-gate 
425*7c478bd9Sstevel@tonic-gate /*
426*7c478bd9Sstevel@tonic-gate  * Print a list of NAME blocks (mostly for debugging).
427*7c478bd9Sstevel@tonic-gate  */
428*7c478bd9Sstevel@tonic-gate prnames(nl)
429*7c478bd9Sstevel@tonic-gate 	register struct namelist *nl;
430*7c478bd9Sstevel@tonic-gate {
431*7c478bd9Sstevel@tonic-gate 	printf("( ");
432*7c478bd9Sstevel@tonic-gate 	while (nl != NULL) {
433*7c478bd9Sstevel@tonic-gate 		printf("%s ", nl->n_name);
434*7c478bd9Sstevel@tonic-gate 		nl = nl->n_next;
435*7c478bd9Sstevel@tonic-gate 	}
436*7c478bd9Sstevel@tonic-gate 	printf(")\n");
437*7c478bd9Sstevel@tonic-gate }
438*7c478bd9Sstevel@tonic-gate 
439*7c478bd9Sstevel@tonic-gate prcmd(c)
440*7c478bd9Sstevel@tonic-gate 	struct cmd *c;
441*7c478bd9Sstevel@tonic-gate {
442*7c478bd9Sstevel@tonic-gate 	extern char *prtype();
443*7c478bd9Sstevel@tonic-gate 
444*7c478bd9Sstevel@tonic-gate 	while (c) {
445*7c478bd9Sstevel@tonic-gate 		printf("c_type %s, c_name %s, c_label %s, c_files ",
446*7c478bd9Sstevel@tonic-gate 			prtype(c->c_type), c->c_name,
447*7c478bd9Sstevel@tonic-gate 			c->c_label?  c->c_label : "NULL");
448*7c478bd9Sstevel@tonic-gate 		prnames(c->c_files);
449*7c478bd9Sstevel@tonic-gate 		prsubcmd(c->c_cmds);
450*7c478bd9Sstevel@tonic-gate 		c = c->c_next;
451*7c478bd9Sstevel@tonic-gate 	}
452*7c478bd9Sstevel@tonic-gate }
453*7c478bd9Sstevel@tonic-gate 
454*7c478bd9Sstevel@tonic-gate prsubcmd(s)
455*7c478bd9Sstevel@tonic-gate 	struct subcmd *s;
456*7c478bd9Sstevel@tonic-gate {
457*7c478bd9Sstevel@tonic-gate 	extern char *prtype();
458*7c478bd9Sstevel@tonic-gate 	extern char *proptions();
459*7c478bd9Sstevel@tonic-gate 
460*7c478bd9Sstevel@tonic-gate 	while (s) {
461*7c478bd9Sstevel@tonic-gate 		printf("sc_type %s, sc_options %d%s, sc_name %s, sc_args ",
462*7c478bd9Sstevel@tonic-gate 			prtype(s->sc_type),
463*7c478bd9Sstevel@tonic-gate 			s->sc_options, proptions(s->sc_options),
464*7c478bd9Sstevel@tonic-gate 			s->sc_name ? s->sc_name : "NULL");
465*7c478bd9Sstevel@tonic-gate 		prnames(s->sc_args);
466*7c478bd9Sstevel@tonic-gate 		s = s->sc_next;
467*7c478bd9Sstevel@tonic-gate 	}
468*7c478bd9Sstevel@tonic-gate }
469*7c478bd9Sstevel@tonic-gate 
470*7c478bd9Sstevel@tonic-gate char *
471*7c478bd9Sstevel@tonic-gate prtype(t)
472*7c478bd9Sstevel@tonic-gate 	int t;
473*7c478bd9Sstevel@tonic-gate {
474*7c478bd9Sstevel@tonic-gate 	switch (t) {
475*7c478bd9Sstevel@tonic-gate 		case EQUAL:
476*7c478bd9Sstevel@tonic-gate 			return ("EQUAL");
477*7c478bd9Sstevel@tonic-gate 		case LP:
478*7c478bd9Sstevel@tonic-gate 			return ("LP");
479*7c478bd9Sstevel@tonic-gate 		case RP:
480*7c478bd9Sstevel@tonic-gate 			return ("RP");
481*7c478bd9Sstevel@tonic-gate 		case SM:
482*7c478bd9Sstevel@tonic-gate 			return ("SM");
483*7c478bd9Sstevel@tonic-gate 		case ARROW:
484*7c478bd9Sstevel@tonic-gate 			return ("ARROW");
485*7c478bd9Sstevel@tonic-gate 		case COLON:
486*7c478bd9Sstevel@tonic-gate 			return ("COLON");
487*7c478bd9Sstevel@tonic-gate 		case DCOLON:
488*7c478bd9Sstevel@tonic-gate 			return ("DCOLON");
489*7c478bd9Sstevel@tonic-gate 		case NAME:
490*7c478bd9Sstevel@tonic-gate 			return ("NAME");
491*7c478bd9Sstevel@tonic-gate 		case STRING:
492*7c478bd9Sstevel@tonic-gate 			return ("STRING");
493*7c478bd9Sstevel@tonic-gate 		case INSTALL:
494*7c478bd9Sstevel@tonic-gate 			return ("INSTALL");
495*7c478bd9Sstevel@tonic-gate 		case NOTIFY:
496*7c478bd9Sstevel@tonic-gate 			return ("NOTIFY");
497*7c478bd9Sstevel@tonic-gate 		case EXCEPT:
498*7c478bd9Sstevel@tonic-gate 			return ("EXCEPT");
499*7c478bd9Sstevel@tonic-gate 		case PATTERN:
500*7c478bd9Sstevel@tonic-gate 			return ("PATTERN");
501*7c478bd9Sstevel@tonic-gate 		case SPECIAL:
502*7c478bd9Sstevel@tonic-gate 			return ("SPECIAL");
503*7c478bd9Sstevel@tonic-gate 		case OPTION:
504*7c478bd9Sstevel@tonic-gate 			return ("OPTION");
505*7c478bd9Sstevel@tonic-gate 	}
506*7c478bd9Sstevel@tonic-gate }
507*7c478bd9Sstevel@tonic-gate 
508*7c478bd9Sstevel@tonic-gate char *
509*7c478bd9Sstevel@tonic-gate proptions(o)
510*7c478bd9Sstevel@tonic-gate 	int o;
511*7c478bd9Sstevel@tonic-gate {
512*7c478bd9Sstevel@tonic-gate 	return (printb((unsigned short) o, OBITS));
513*7c478bd9Sstevel@tonic-gate }
514*7c478bd9Sstevel@tonic-gate 
515*7c478bd9Sstevel@tonic-gate char *
516*7c478bd9Sstevel@tonic-gate printb(v, bits)
517*7c478bd9Sstevel@tonic-gate 	register char *bits;
518*7c478bd9Sstevel@tonic-gate 	register unsigned short v;
519*7c478bd9Sstevel@tonic-gate {
520*7c478bd9Sstevel@tonic-gate 	register int i, any = 0;
521*7c478bd9Sstevel@tonic-gate 	register char c;
522*7c478bd9Sstevel@tonic-gate 	char *p = buf;
523*7c478bd9Sstevel@tonic-gate 
524*7c478bd9Sstevel@tonic-gate 	bits++;
525*7c478bd9Sstevel@tonic-gate 	if (bits) {
526*7c478bd9Sstevel@tonic-gate 
527*7c478bd9Sstevel@tonic-gate 		*p++ = '<';
528*7c478bd9Sstevel@tonic-gate 		while ((i = *bits++) != 0) {
529*7c478bd9Sstevel@tonic-gate 			if (v & (1 << (i-1))) {
530*7c478bd9Sstevel@tonic-gate 				if (any)
531*7c478bd9Sstevel@tonic-gate 					*p++ = ',';
532*7c478bd9Sstevel@tonic-gate 				any = 1;
533*7c478bd9Sstevel@tonic-gate 				for (; (c = *bits) > 32; bits++)
534*7c478bd9Sstevel@tonic-gate 					*p++ = c;
535*7c478bd9Sstevel@tonic-gate 			} else
536*7c478bd9Sstevel@tonic-gate 				for (; *bits > 32; bits++)
537*7c478bd9Sstevel@tonic-gate 					;
538*7c478bd9Sstevel@tonic-gate 		}
539*7c478bd9Sstevel@tonic-gate 		*p++ = '>';
540*7c478bd9Sstevel@tonic-gate 	}
541*7c478bd9Sstevel@tonic-gate 
542*7c478bd9Sstevel@tonic-gate 	*p = '\0';
543*7c478bd9Sstevel@tonic-gate 	return (buf);
544*7c478bd9Sstevel@tonic-gate }
545*7c478bd9Sstevel@tonic-gate 
546*7c478bd9Sstevel@tonic-gate /*VARARGS*/
547*7c478bd9Sstevel@tonic-gate warn(fmt, a1, a2, a3)
548*7c478bd9Sstevel@tonic-gate 	char *fmt;
549*7c478bd9Sstevel@tonic-gate {
550*7c478bd9Sstevel@tonic-gate 	extern int yylineno;
551*7c478bd9Sstevel@tonic-gate 
552*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "rdist: line %d: Warning: ", yylineno);
553*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, fmt, a1, a2, a3);
554*7c478bd9Sstevel@tonic-gate 	fputc('\n', stderr);
555*7c478bd9Sstevel@tonic-gate }
556