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