xref: /illumos-gate/usr/src/cmd/cmd-inet/usr.bin/rdist/main.c (revision 159d09a20817016f09b3ea28d1bdada4a336bb91)
17c478bd9Sstevel@tonic-gate /*
2*159d09a2SMark Phalan  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate /*
77c478bd9Sstevel@tonic-gate  * Copyright (c) 1983 Regents of the University of California.
87c478bd9Sstevel@tonic-gate  * All rights reserved.
97c478bd9Sstevel@tonic-gate  *
107c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms are permitted
117c478bd9Sstevel@tonic-gate  * provided that the above copyright notice and this paragraph are
127c478bd9Sstevel@tonic-gate  * duplicated in all such forms and that any documentation,
137c478bd9Sstevel@tonic-gate  * advertising materials, and other materials related to such
147c478bd9Sstevel@tonic-gate  * distribution and use acknowledge that the software was developed
157c478bd9Sstevel@tonic-gate  * by the University of California, Berkeley.  The name of the
167c478bd9Sstevel@tonic-gate  * University may not be used to endorse or promote products derived
177c478bd9Sstevel@tonic-gate  * from this software without specific prior written permission.
187c478bd9Sstevel@tonic-gate  */
197c478bd9Sstevel@tonic-gate 
207c478bd9Sstevel@tonic-gate #include "defs.h"
217c478bd9Sstevel@tonic-gate #include <string.h>
227c478bd9Sstevel@tonic-gate #include <syslog.h>
237c478bd9Sstevel@tonic-gate #include <k5-int.h>
24*159d09a2SMark Phalan #include <krb5defs.h>
257c478bd9Sstevel@tonic-gate #include <priv_utils.h>
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #define	NHOSTS 100
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * Remote distribution program.
317c478bd9Sstevel@tonic-gate  */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate char	*distfile = NULL;
347c478bd9Sstevel@tonic-gate char	Tmpfile[] = "/tmp/rdistXXXXXX";
357c478bd9Sstevel@tonic-gate char	*tmpname = &Tmpfile[5];
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate int	debug;		/* debugging flag */
387c478bd9Sstevel@tonic-gate int	nflag;		/* NOP flag, just print commands without executing */
397c478bd9Sstevel@tonic-gate int	qflag;		/* Quiet. Don't print messages */
407c478bd9Sstevel@tonic-gate int	options;	/* global options */
417c478bd9Sstevel@tonic-gate int	iamremote;	/* act as remote server for transfering files */
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate FILE	*fin = NULL;	/* input file pointer */
447c478bd9Sstevel@tonic-gate int	rem = -1;	/* file descriptor to remote source/sink process */
457c478bd9Sstevel@tonic-gate char	host[32];	/* host name */
467c478bd9Sstevel@tonic-gate int	nerrs;		/* number of errors while sending/receiving */
477c478bd9Sstevel@tonic-gate char	user[10];	/* user's name */
487c478bd9Sstevel@tonic-gate char	homedir[128];	/* user's home directory */
497c478bd9Sstevel@tonic-gate char	buf[RDIST_BUFSIZ];	/* general purpose buffer */
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate struct	passwd *pw;	/* pointer to static area used by getpwent */
527c478bd9Sstevel@tonic-gate struct	group *gr;	/* pointer to static area used by getgrent */
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate char des_inbuf[2 * RDIST_BUFSIZ];	/* needs to be > largest read size */
557c478bd9Sstevel@tonic-gate char des_outbuf[2 * RDIST_BUFSIZ];	/* needs to be > largest write size */
567c478bd9Sstevel@tonic-gate krb5_data desinbuf, desoutbuf;
577c478bd9Sstevel@tonic-gate krb5_encrypt_block eblock;		/* eblock for encrypt/decrypt */
587c478bd9Sstevel@tonic-gate krb5_context bsd_context;
597c478bd9Sstevel@tonic-gate krb5_auth_context auth_context;
607c478bd9Sstevel@tonic-gate krb5_creds *cred;
617c478bd9Sstevel@tonic-gate char *krb_cache = NULL;
627c478bd9Sstevel@tonic-gate krb5_flags authopts;
637c478bd9Sstevel@tonic-gate krb5_error_code status;
647c478bd9Sstevel@tonic-gate enum kcmd_proto kcmd_proto = KCMD_NEW_PROTOCOL;
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate int encrypt_flag = 0;	/* Flag set when encryption is used */
677c478bd9Sstevel@tonic-gate int krb5auth_flag = 0;	/* Flag set, when KERBEROS is enabled */
687c478bd9Sstevel@tonic-gate int debug_port = 0;
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate int retval = 0;
717c478bd9Sstevel@tonic-gate char *krb_realm = NULL;
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate /* Flag set, if -PN / -PO is specified */
747c478bd9Sstevel@tonic-gate static boolean_t rcmdoption_done = B_FALSE;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate static int encrypt_done = 0;	/* Flag set, if -x is specified */
777c478bd9Sstevel@tonic-gate profile_options_boolean option[] = {
787c478bd9Sstevel@tonic-gate 	{ "encrypt", &encrypt_flag, 0 },
797c478bd9Sstevel@tonic-gate 	{ NULL, NULL, 0 }
807c478bd9Sstevel@tonic-gate };
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate static char *rcmdproto = NULL;
837c478bd9Sstevel@tonic-gate profile_option_strings rcmdversion[] = {
847c478bd9Sstevel@tonic-gate 	{ "rcmd_protocol", &rcmdproto, 0 },
857c478bd9Sstevel@tonic-gate 	{ NULL, NULL, 0 }
867c478bd9Sstevel@tonic-gate };
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate char *realmdef[] = { "realms", NULL, "rdist", NULL };
897c478bd9Sstevel@tonic-gate char *appdef[] = { "appdefaults", "rdist", NULL };
907c478bd9Sstevel@tonic-gate 
91740638c8Sbw static void usage(void);
92740638c8Sbw static char *prtype(int t);
93740638c8Sbw static void prsubcmd(struct subcmd *s);
94740638c8Sbw static void docmdargs(int nargs, char *args[]);
95740638c8Sbw void prnames();
96740638c8Sbw void prcmd();
97740638c8Sbw 
987c478bd9Sstevel@tonic-gate int
997c478bd9Sstevel@tonic-gate main(argc, argv)
1007c478bd9Sstevel@tonic-gate 	int argc;
1017c478bd9Sstevel@tonic-gate 	char *argv[];
1027c478bd9Sstevel@tonic-gate {
1037c478bd9Sstevel@tonic-gate 	register char *arg;
1047c478bd9Sstevel@tonic-gate 	int cmdargs = 0;
1057c478bd9Sstevel@tonic-gate 	char *dhosts[NHOSTS], **hp = dhosts;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	pw = getpwuid(getuid());
1107c478bd9Sstevel@tonic-gate 	if (pw == NULL) {
1117c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: Who are you?\n"), argv[0]);
1127c478bd9Sstevel@tonic-gate 		exit(1);
1137c478bd9Sstevel@tonic-gate 	}
1147c478bd9Sstevel@tonic-gate 	strncpy(user, pw->pw_name, sizeof (user));
1157c478bd9Sstevel@tonic-gate 	user[sizeof (user) - 1] = '\0';
1167c478bd9Sstevel@tonic-gate 	strncpy(homedir, pw->pw_dir, sizeof (homedir));
1177c478bd9Sstevel@tonic-gate 	homedir[sizeof (homedir) - 1] = '\0';
1187c478bd9Sstevel@tonic-gate 	gethostname(host, sizeof (host));
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	while (--argc > 0) {
1217c478bd9Sstevel@tonic-gate 		if ((arg = *++argv)[0] != '-')
1227c478bd9Sstevel@tonic-gate 			break;
1237c478bd9Sstevel@tonic-gate 		if ((strcmp(arg, "-Server") == 0))
1247c478bd9Sstevel@tonic-gate 			iamremote++;
1257c478bd9Sstevel@tonic-gate 		else while (*++arg) {
1267c478bd9Sstevel@tonic-gate 			if (strncmp(*argv, "-PO", 3) == 0) {
1277c478bd9Sstevel@tonic-gate 				if (rcmdoption_done == B_TRUE) {
1287c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext("rdist: "
1297c478bd9Sstevel@tonic-gate 						"Only one of -PN "
1307c478bd9Sstevel@tonic-gate 						"and -PO allowed.\n"));
1317c478bd9Sstevel@tonic-gate 					usage();
1327c478bd9Sstevel@tonic-gate 				}
1337c478bd9Sstevel@tonic-gate 				kcmd_proto = KCMD_OLD_PROTOCOL;
1347c478bd9Sstevel@tonic-gate 				krb5auth_flag++;
1357c478bd9Sstevel@tonic-gate 				rcmdoption_done = B_TRUE;
1367c478bd9Sstevel@tonic-gate 				break;
1377c478bd9Sstevel@tonic-gate 			}
1387c478bd9Sstevel@tonic-gate 			if (strncmp(*argv, "-PN", 3) == 0) {
1397c478bd9Sstevel@tonic-gate 				if (rcmdoption_done == B_TRUE) {
1407c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext("rdist: "
1417c478bd9Sstevel@tonic-gate 						"Only one of -PN "
1427c478bd9Sstevel@tonic-gate 						"and -PO allowed.\n"));
1437c478bd9Sstevel@tonic-gate 					usage();
1447c478bd9Sstevel@tonic-gate 				}
1457c478bd9Sstevel@tonic-gate 				kcmd_proto = KCMD_NEW_PROTOCOL;
1467c478bd9Sstevel@tonic-gate 				krb5auth_flag++;
1477c478bd9Sstevel@tonic-gate 				rcmdoption_done = B_TRUE;
1487c478bd9Sstevel@tonic-gate 				break;
1497c478bd9Sstevel@tonic-gate 			}
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 			switch (*arg) {
1527c478bd9Sstevel@tonic-gate #ifdef DEBUG
1537c478bd9Sstevel@tonic-gate 			case 'p':
1547c478bd9Sstevel@tonic-gate 				if (--argc <= 0)
1557c478bd9Sstevel@tonic-gate 					usage();
1567c478bd9Sstevel@tonic-gate 				debug_port = htons(atoi(*++argv));
1577c478bd9Sstevel@tonic-gate 				break;
1587c478bd9Sstevel@tonic-gate #endif /* DEBUG */
1597c478bd9Sstevel@tonic-gate 			case 'k':
1607c478bd9Sstevel@tonic-gate 				if (--argc <= 0) {
1617c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext("rdist: "
1627c478bd9Sstevel@tonic-gate 						"-k flag must be followed with "
1637c478bd9Sstevel@tonic-gate 						" a realm name.\n"));
1647c478bd9Sstevel@tonic-gate 					exit(1);
1657c478bd9Sstevel@tonic-gate 				}
1667c478bd9Sstevel@tonic-gate 				if ((krb_realm = strdup(*++argv)) == NULL) {
1677c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext("rdist: "
1687c478bd9Sstevel@tonic-gate 						"Cannot malloc.\n"));
1697c478bd9Sstevel@tonic-gate 					exit(1);
1707c478bd9Sstevel@tonic-gate 				}
1717c478bd9Sstevel@tonic-gate 				krb5auth_flag++;
1727c478bd9Sstevel@tonic-gate 				break;
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 			case 'a':
1757c478bd9Sstevel@tonic-gate 				krb5auth_flag++;
1767c478bd9Sstevel@tonic-gate 				break;
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 			case 'x':
1797c478bd9Sstevel@tonic-gate 				encrypt_flag++;
1807c478bd9Sstevel@tonic-gate 				encrypt_done++;
1817c478bd9Sstevel@tonic-gate 				krb5auth_flag++;
1827c478bd9Sstevel@tonic-gate 				break;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 			case 'f':
1857c478bd9Sstevel@tonic-gate 				if (--argc <= 0)
1867c478bd9Sstevel@tonic-gate 					usage();
1877c478bd9Sstevel@tonic-gate 				distfile = *++argv;
1887c478bd9Sstevel@tonic-gate 				if (distfile[0] == '-' && distfile[1] == '\0')
1897c478bd9Sstevel@tonic-gate 					fin = stdin;
1907c478bd9Sstevel@tonic-gate 				break;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 			case 'm':
1937c478bd9Sstevel@tonic-gate 				if (--argc <= 0)
1947c478bd9Sstevel@tonic-gate 					usage();
1957c478bd9Sstevel@tonic-gate 				if (hp >= &dhosts[NHOSTS-2]) {
1967c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext("rdist:"
1977c478bd9Sstevel@tonic-gate 						" too many destination"
1987c478bd9Sstevel@tonic-gate 						" hosts\n"));
1997c478bd9Sstevel@tonic-gate 					exit(1);
2007c478bd9Sstevel@tonic-gate 				}
2017c478bd9Sstevel@tonic-gate 				*hp++ = *++argv;
2027c478bd9Sstevel@tonic-gate 				break;
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 			case 'd':
2057c478bd9Sstevel@tonic-gate 				if (--argc <= 0)
2067c478bd9Sstevel@tonic-gate 					usage();
2077c478bd9Sstevel@tonic-gate 				define(*++argv);
2087c478bd9Sstevel@tonic-gate 				break;
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 			case 'D':
2117c478bd9Sstevel@tonic-gate 				debug++;
2127c478bd9Sstevel@tonic-gate 				break;
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 			case 'c':
2157c478bd9Sstevel@tonic-gate 				cmdargs++;
2167c478bd9Sstevel@tonic-gate 				break;
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 			case 'n':
2197c478bd9Sstevel@tonic-gate 				if (options & VERIFY) {
2207c478bd9Sstevel@tonic-gate 					printf("rdist: -n overrides -v\n");
2217c478bd9Sstevel@tonic-gate 					options &= ~VERIFY;
2227c478bd9Sstevel@tonic-gate 				}
2237c478bd9Sstevel@tonic-gate 				nflag++;
2247c478bd9Sstevel@tonic-gate 				break;
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 			case 'q':
2277c478bd9Sstevel@tonic-gate 				qflag++;
2287c478bd9Sstevel@tonic-gate 				break;
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 			case 'b':
2317c478bd9Sstevel@tonic-gate 				options |= COMPARE;
2327c478bd9Sstevel@tonic-gate 				break;
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 			case 'R':
2357c478bd9Sstevel@tonic-gate 				options |= REMOVE;
2367c478bd9Sstevel@tonic-gate 				break;
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 			case 'v':
2397c478bd9Sstevel@tonic-gate 				if (nflag) {
2407c478bd9Sstevel@tonic-gate 					printf("rdist: -n overrides -v\n");
2417c478bd9Sstevel@tonic-gate 					break;
2427c478bd9Sstevel@tonic-gate 				}
2437c478bd9Sstevel@tonic-gate 				options |= VERIFY;
2447c478bd9Sstevel@tonic-gate 				break;
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 			case 'w':
2477c478bd9Sstevel@tonic-gate 				options |= WHOLE;
2487c478bd9Sstevel@tonic-gate 				break;
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 			case 'y':
2517c478bd9Sstevel@tonic-gate 				options |= YOUNGER;
2527c478bd9Sstevel@tonic-gate 				break;
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 			case 'h':
2557c478bd9Sstevel@tonic-gate 				options |= FOLLOW;
2567c478bd9Sstevel@tonic-gate 				break;
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 			case 'i':
2597c478bd9Sstevel@tonic-gate 				options |= IGNLNKS;
2607c478bd9Sstevel@tonic-gate 				break;
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 			default:
2637c478bd9Sstevel@tonic-gate 				usage();
2647c478bd9Sstevel@tonic-gate 			}
2657c478bd9Sstevel@tonic-gate 		}
2667c478bd9Sstevel@tonic-gate 	}
2677c478bd9Sstevel@tonic-gate 	*hp = NULL;
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	mktemp(Tmpfile);
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	if (krb5auth_flag > 0) {
2727c478bd9Sstevel@tonic-gate 		status = krb5_init_context(&bsd_context);
2737c478bd9Sstevel@tonic-gate 		if (status) {
2747c478bd9Sstevel@tonic-gate 			com_err("rdist", status,
2757c478bd9Sstevel@tonic-gate 				gettext("while initializing krb5"));
2767c478bd9Sstevel@tonic-gate 			exit(1);
2777c478bd9Sstevel@tonic-gate 		}
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 		/* Set up des buffers */
2807c478bd9Sstevel@tonic-gate 		desinbuf.data = des_inbuf;
2817c478bd9Sstevel@tonic-gate 		desoutbuf.data = des_outbuf;
2827c478bd9Sstevel@tonic-gate 		desinbuf.length = sizeof (des_inbuf);
2837c478bd9Sstevel@tonic-gate 		desoutbuf.length = sizeof (des_outbuf);
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 		/*
2867c478bd9Sstevel@tonic-gate 		 * Get our local realm to look up local realm options.
2877c478bd9Sstevel@tonic-gate 		 */
2887c478bd9Sstevel@tonic-gate 		status = krb5_get_default_realm(bsd_context, &realmdef[1]);
2897c478bd9Sstevel@tonic-gate 		if (status) {
2907c478bd9Sstevel@tonic-gate 			com_err("rdist", status,
2917c478bd9Sstevel@tonic-gate 				gettext("while getting default realm"));
2927c478bd9Sstevel@tonic-gate 			exit(1);
2937c478bd9Sstevel@tonic-gate 		}
2947c478bd9Sstevel@tonic-gate 		/*
2957c478bd9Sstevel@tonic-gate 		 * See if encryption should be done for this realm
2967c478bd9Sstevel@tonic-gate 		 */
2977c478bd9Sstevel@tonic-gate 		profile_get_options_boolean(bsd_context->profile, realmdef,
2987c478bd9Sstevel@tonic-gate 						option);
2997c478bd9Sstevel@tonic-gate 		/*
3007c478bd9Sstevel@tonic-gate 		 * Check the appdefaults section
3017c478bd9Sstevel@tonic-gate 		 */
3027c478bd9Sstevel@tonic-gate 		profile_get_options_boolean(bsd_context->profile, appdef,
3037c478bd9Sstevel@tonic-gate 						option);
3047c478bd9Sstevel@tonic-gate 		profile_get_options_string(bsd_context->profile, appdef,
3057c478bd9Sstevel@tonic-gate 						rcmdversion);
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 		if ((encrypt_done > 0) || (encrypt_flag > 0)) {
3087c478bd9Sstevel@tonic-gate 			if (krb5_privacy_allowed() == TRUE) {
3097c478bd9Sstevel@tonic-gate 				encrypt_flag++;
3107c478bd9Sstevel@tonic-gate 			} else {
3117c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("rdist: "
3127c478bd9Sstevel@tonic-gate 						"Encryption not supported.\n"));
3137c478bd9Sstevel@tonic-gate 				exit(1);
3147c478bd9Sstevel@tonic-gate 			}
3157c478bd9Sstevel@tonic-gate 		}
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 		if ((rcmdoption_done == B_FALSE) && (rcmdproto != NULL)) {
3187c478bd9Sstevel@tonic-gate 			if (strncmp(rcmdproto, "rcmdv2", 6) == 0) {
3197c478bd9Sstevel@tonic-gate 				kcmd_proto = KCMD_NEW_PROTOCOL;
3207c478bd9Sstevel@tonic-gate 			} else if (strncmp(rcmdproto, "rcmdv1", 6) == 0) {
3217c478bd9Sstevel@tonic-gate 				kcmd_proto = KCMD_OLD_PROTOCOL;
3227c478bd9Sstevel@tonic-gate 			} else {
3237c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("Unrecognized "
3247c478bd9Sstevel@tonic-gate 					"KCMD protocol (%s)"), rcmdproto);
3257c478bd9Sstevel@tonic-gate 				exit(1);
3267c478bd9Sstevel@tonic-gate 			}
3277c478bd9Sstevel@tonic-gate 		}
3287c478bd9Sstevel@tonic-gate 	}
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	if (iamremote) {
3317c478bd9Sstevel@tonic-gate 		setreuid(getuid(), getuid());
3327c478bd9Sstevel@tonic-gate 		server();
3337c478bd9Sstevel@tonic-gate 		exit(nerrs != 0);
3347c478bd9Sstevel@tonic-gate 	}
3357c478bd9Sstevel@tonic-gate 	if (__init_suid_priv(0, PRIV_NET_PRIVADDR, NULL) == -1) {
3367c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
3377c478bd9Sstevel@tonic-gate 			"rdist needs to run with sufficient privilege\n");
3387c478bd9Sstevel@tonic-gate 		exit(1);
3397c478bd9Sstevel@tonic-gate 	}
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	if (cmdargs)
3427c478bd9Sstevel@tonic-gate 		docmdargs(argc, argv);
3437c478bd9Sstevel@tonic-gate 	else {
3447c478bd9Sstevel@tonic-gate 		if (fin == NULL) {
3457c478bd9Sstevel@tonic-gate 			if (distfile == NULL) {
3467c478bd9Sstevel@tonic-gate 				if ((fin = fopen("distfile", "r")) == NULL)
3477c478bd9Sstevel@tonic-gate 					fin = fopen("Distfile", "r");
3487c478bd9Sstevel@tonic-gate 			} else
3497c478bd9Sstevel@tonic-gate 				fin = fopen(distfile, "r");
3507c478bd9Sstevel@tonic-gate 			if (fin == NULL) {
3517c478bd9Sstevel@tonic-gate 				perror(distfile ? distfile : "distfile");
3527c478bd9Sstevel@tonic-gate 				exit(1);
3537c478bd9Sstevel@tonic-gate 			}
3547c478bd9Sstevel@tonic-gate 		}
3557c478bd9Sstevel@tonic-gate 		yyparse();
3567c478bd9Sstevel@tonic-gate 		if (nerrs == 0)
3577c478bd9Sstevel@tonic-gate 			docmds(dhosts, argc, argv);
3587c478bd9Sstevel@tonic-gate 	}
3597c478bd9Sstevel@tonic-gate 
360740638c8Sbw 	return (nerrs != 0);
3617c478bd9Sstevel@tonic-gate }
3627c478bd9Sstevel@tonic-gate 
363740638c8Sbw static void
3647c478bd9Sstevel@tonic-gate usage()
3657c478bd9Sstevel@tonic-gate {
3667c478bd9Sstevel@tonic-gate 	printf(gettext("Usage: rdist [-nqbhirvwyDax] [-PN / -PO] "
3677c478bd9Sstevel@tonic-gate #ifdef DEBUG
3687c478bd9Sstevel@tonic-gate 	"[-p port] "
3697c478bd9Sstevel@tonic-gate #endif /* DEBUG */
3707c478bd9Sstevel@tonic-gate 	"[-k realm] [-f distfile] [-d var=value] [-m host] [file ...]\n"));
3717c478bd9Sstevel@tonic-gate 	printf(gettext("or: rdist [-nqbhirvwyDax] [-PN / -PO] [-p port] "
3727c478bd9Sstevel@tonic-gate 	"[-k realm] -c source [...] machine[:dest]\n"));
3737c478bd9Sstevel@tonic-gate 	exit(1);
3747c478bd9Sstevel@tonic-gate }
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate /*
3777c478bd9Sstevel@tonic-gate  * rcp like interface for distributing files.
3787c478bd9Sstevel@tonic-gate  */
379740638c8Sbw static void
3807c478bd9Sstevel@tonic-gate docmdargs(nargs, args)
3817c478bd9Sstevel@tonic-gate 	int nargs;
3827c478bd9Sstevel@tonic-gate 	char *args[];
3837c478bd9Sstevel@tonic-gate {
3847c478bd9Sstevel@tonic-gate 	register struct namelist *nl, *prev;
3857c478bd9Sstevel@tonic-gate 	register char *cp;
3867c478bd9Sstevel@tonic-gate 	struct namelist *files, *hosts;
3877c478bd9Sstevel@tonic-gate 	struct subcmd *cmds;
3887c478bd9Sstevel@tonic-gate 	char *dest;
3897c478bd9Sstevel@tonic-gate 	static struct namelist tnl = { NULL, NULL };
3907c478bd9Sstevel@tonic-gate 	int i;
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	if (nargs < 2)
3937c478bd9Sstevel@tonic-gate 		usage();
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 	prev = NULL;
3967c478bd9Sstevel@tonic-gate 	for (i = 0; i < nargs - 1; i++) {
3977c478bd9Sstevel@tonic-gate 		nl = makenl(args[i]);
3987c478bd9Sstevel@tonic-gate 		if (prev == NULL)
3997c478bd9Sstevel@tonic-gate 			files = prev = nl;
4007c478bd9Sstevel@tonic-gate 		else {
4017c478bd9Sstevel@tonic-gate 			prev->n_next = nl;
4027c478bd9Sstevel@tonic-gate 			prev = nl;
4037c478bd9Sstevel@tonic-gate 		}
4047c478bd9Sstevel@tonic-gate 	}
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	cp = args[i];
4077c478bd9Sstevel@tonic-gate 	if ((dest = index(cp, ':')) != NULL)
4087c478bd9Sstevel@tonic-gate 		*dest++ = '\0';
4097c478bd9Sstevel@tonic-gate 	tnl.n_name = cp;
4107c478bd9Sstevel@tonic-gate 	hosts = expand(&tnl, E_ALL);
4117c478bd9Sstevel@tonic-gate 	if (nerrs)
4127c478bd9Sstevel@tonic-gate 		exit(1);
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	if (dest == NULL || *dest == '\0')
4157c478bd9Sstevel@tonic-gate 		cmds = NULL;
4167c478bd9Sstevel@tonic-gate 	else {
4177c478bd9Sstevel@tonic-gate 		cmds = makesubcmd(INSTALL);
4187c478bd9Sstevel@tonic-gate 		cmds->sc_options = options;
4197c478bd9Sstevel@tonic-gate 		cmds->sc_name = dest;
4207c478bd9Sstevel@tonic-gate 	}
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	if (debug) {
4237c478bd9Sstevel@tonic-gate 		printf("docmdargs()\nfiles = ");
4247c478bd9Sstevel@tonic-gate 		prnames(files);
4257c478bd9Sstevel@tonic-gate 		printf("hosts = ");
4267c478bd9Sstevel@tonic-gate 		prnames(hosts);
4277c478bd9Sstevel@tonic-gate 	}
4287c478bd9Sstevel@tonic-gate 	insert(NULL, files, hosts, cmds);
4297c478bd9Sstevel@tonic-gate 	docmds(NULL, 0, NULL);
4307c478bd9Sstevel@tonic-gate }
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate /*
4337c478bd9Sstevel@tonic-gate  * Print a list of NAME blocks (mostly for debugging).
4347c478bd9Sstevel@tonic-gate  */
435740638c8Sbw void
4367c478bd9Sstevel@tonic-gate prnames(nl)
4377c478bd9Sstevel@tonic-gate 	register struct namelist *nl;
4387c478bd9Sstevel@tonic-gate {
4397c478bd9Sstevel@tonic-gate 	printf("( ");
4407c478bd9Sstevel@tonic-gate 	while (nl != NULL) {
4417c478bd9Sstevel@tonic-gate 		printf("%s ", nl->n_name);
4427c478bd9Sstevel@tonic-gate 		nl = nl->n_next;
4437c478bd9Sstevel@tonic-gate 	}
4447c478bd9Sstevel@tonic-gate 	printf(")\n");
4457c478bd9Sstevel@tonic-gate }
4467c478bd9Sstevel@tonic-gate 
447740638c8Sbw void
4487c478bd9Sstevel@tonic-gate prcmd(c)
4497c478bd9Sstevel@tonic-gate 	struct cmd *c;
4507c478bd9Sstevel@tonic-gate {
4517c478bd9Sstevel@tonic-gate 	extern char *prtype();
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	while (c) {
4547c478bd9Sstevel@tonic-gate 		printf("c_type %s, c_name %s, c_label %s, c_files ",
4557c478bd9Sstevel@tonic-gate 			prtype(c->c_type), c->c_name,
4567c478bd9Sstevel@tonic-gate 			c->c_label?  c->c_label : "NULL");
4577c478bd9Sstevel@tonic-gate 		prnames(c->c_files);
4587c478bd9Sstevel@tonic-gate 		prsubcmd(c->c_cmds);
4597c478bd9Sstevel@tonic-gate 		c = c->c_next;
4607c478bd9Sstevel@tonic-gate 	}
4617c478bd9Sstevel@tonic-gate }
4627c478bd9Sstevel@tonic-gate 
463740638c8Sbw static void
4647c478bd9Sstevel@tonic-gate prsubcmd(s)
4657c478bd9Sstevel@tonic-gate 	struct subcmd *s;
4667c478bd9Sstevel@tonic-gate {
4677c478bd9Sstevel@tonic-gate 	extern char *prtype();
4687c478bd9Sstevel@tonic-gate 	extern char *proptions();
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	while (s) {
4717c478bd9Sstevel@tonic-gate 		printf("sc_type %s, sc_options %d%s, sc_name %s, sc_args ",
4727c478bd9Sstevel@tonic-gate 			prtype(s->sc_type),
4737c478bd9Sstevel@tonic-gate 			s->sc_options, proptions(s->sc_options),
4747c478bd9Sstevel@tonic-gate 			s->sc_name ? s->sc_name : "NULL");
4757c478bd9Sstevel@tonic-gate 		prnames(s->sc_args);
4767c478bd9Sstevel@tonic-gate 		s = s->sc_next;
4777c478bd9Sstevel@tonic-gate 	}
4787c478bd9Sstevel@tonic-gate }
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate char *
4817c478bd9Sstevel@tonic-gate prtype(t)
4827c478bd9Sstevel@tonic-gate 	int t;
4837c478bd9Sstevel@tonic-gate {
4847c478bd9Sstevel@tonic-gate 	switch (t) {
4857c478bd9Sstevel@tonic-gate 		case EQUAL:
4867c478bd9Sstevel@tonic-gate 			return ("EQUAL");
4877c478bd9Sstevel@tonic-gate 		case LP:
4887c478bd9Sstevel@tonic-gate 			return ("LP");
4897c478bd9Sstevel@tonic-gate 		case RP:
4907c478bd9Sstevel@tonic-gate 			return ("RP");
4917c478bd9Sstevel@tonic-gate 		case SM:
4927c478bd9Sstevel@tonic-gate 			return ("SM");
4937c478bd9Sstevel@tonic-gate 		case ARROW:
4947c478bd9Sstevel@tonic-gate 			return ("ARROW");
4957c478bd9Sstevel@tonic-gate 		case COLON:
4967c478bd9Sstevel@tonic-gate 			return ("COLON");
4977c478bd9Sstevel@tonic-gate 		case DCOLON:
4987c478bd9Sstevel@tonic-gate 			return ("DCOLON");
4997c478bd9Sstevel@tonic-gate 		case NAME:
5007c478bd9Sstevel@tonic-gate 			return ("NAME");
5017c478bd9Sstevel@tonic-gate 		case STRING:
5027c478bd9Sstevel@tonic-gate 			return ("STRING");
5037c478bd9Sstevel@tonic-gate 		case INSTALL:
5047c478bd9Sstevel@tonic-gate 			return ("INSTALL");
5057c478bd9Sstevel@tonic-gate 		case NOTIFY:
5067c478bd9Sstevel@tonic-gate 			return ("NOTIFY");
5077c478bd9Sstevel@tonic-gate 		case EXCEPT:
5087c478bd9Sstevel@tonic-gate 			return ("EXCEPT");
5097c478bd9Sstevel@tonic-gate 		case PATTERN:
5107c478bd9Sstevel@tonic-gate 			return ("PATTERN");
5117c478bd9Sstevel@tonic-gate 		case SPECIAL:
5127c478bd9Sstevel@tonic-gate 			return ("SPECIAL");
5137c478bd9Sstevel@tonic-gate 		case OPTION:
5147c478bd9Sstevel@tonic-gate 			return ("OPTION");
5157c478bd9Sstevel@tonic-gate 	}
516740638c8Sbw 	return (NULL);
5177c478bd9Sstevel@tonic-gate }
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate char *
5207c478bd9Sstevel@tonic-gate proptions(o)
5217c478bd9Sstevel@tonic-gate 	int o;
5227c478bd9Sstevel@tonic-gate {
5237c478bd9Sstevel@tonic-gate 	return (printb((unsigned short) o, OBITS));
5247c478bd9Sstevel@tonic-gate }
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate char *
5277c478bd9Sstevel@tonic-gate printb(v, bits)
5287c478bd9Sstevel@tonic-gate 	register char *bits;
5297c478bd9Sstevel@tonic-gate 	register unsigned short v;
5307c478bd9Sstevel@tonic-gate {
5317c478bd9Sstevel@tonic-gate 	register int i, any = 0;
5327c478bd9Sstevel@tonic-gate 	register char c;
5337c478bd9Sstevel@tonic-gate 	char *p = buf;
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	bits++;
5367c478bd9Sstevel@tonic-gate 	if (bits) {
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 		*p++ = '<';
5397c478bd9Sstevel@tonic-gate 		while ((i = *bits++) != 0) {
5407c478bd9Sstevel@tonic-gate 			if (v & (1 << (i-1))) {
5417c478bd9Sstevel@tonic-gate 				if (any)
5427c478bd9Sstevel@tonic-gate 					*p++ = ',';
5437c478bd9Sstevel@tonic-gate 				any = 1;
5447c478bd9Sstevel@tonic-gate 				for (; (c = *bits) > 32; bits++)
5457c478bd9Sstevel@tonic-gate 					*p++ = c;
5467c478bd9Sstevel@tonic-gate 			} else
5477c478bd9Sstevel@tonic-gate 				for (; *bits > 32; bits++)
5487c478bd9Sstevel@tonic-gate 					;
5497c478bd9Sstevel@tonic-gate 		}
5507c478bd9Sstevel@tonic-gate 		*p++ = '>';
5517c478bd9Sstevel@tonic-gate 	}
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	*p = '\0';
5547c478bd9Sstevel@tonic-gate 	return (buf);
5557c478bd9Sstevel@tonic-gate }
556