xref: /csrg-svn/usr.bin/rdist/main.c (revision 42760)
122374Sdist /*
222374Sdist  * Copyright (c) 1983 Regents of the University of California.
333415Sbostic  * All rights reserved.
433415Sbostic  *
5*42760Sbostic  * %sccs.include.redist.c%
622374Sdist  */
722374Sdist 
814921Sralph #ifndef lint
922374Sdist char copyright[] =
1022374Sdist "@(#) Copyright (c) 1983 Regents of the University of California.\n\
1122374Sdist  All rights reserved.\n";
1233415Sbostic #endif /* not lint */
1314921Sralph 
1422374Sdist #ifndef lint
15*42760Sbostic static char sccsid[] = "@(#)main.c	5.5 (Berkeley) 06/01/90";
1633415Sbostic #endif /* not lint */
1722374Sdist 
1814921Sralph #include "defs.h"
1914921Sralph 
2017914Sralph #define NHOSTS 100
2117914Sralph 
2214921Sralph /*
2314921Sralph  * Remote distribution program.
2414921Sralph  */
2514921Sralph 
2620846Sbloom char	*distfile = NULL;
2737856Sbostic char	tmpfile[] = _PATH_TMP;
2815113Sralph char	*tmpname = &tmpfile[5];
2914921Sralph 
3014921Sralph int	debug;		/* debugging flag */
3114921Sralph int	nflag;		/* NOP flag, just print commands without executing */
3214921Sralph int	qflag;		/* Quiet. Don't print messages */
3315218Sralph int	options;	/* global options */
3414921Sralph int	iamremote;	/* act as remote server for transfering files */
3514921Sralph 
3614921Sralph FILE	*fin = NULL;	/* input file pointer */
3716028Sralph int	rem = -1;	/* file descriptor to remote source/sink process */
3814921Sralph char	host[32];	/* host name */
3916028Sralph int	nerrs;		/* number of errors while sending/receiving */
4014921Sralph char	user[10];	/* user's name */
4114921Sralph char	homedir[128];	/* user's home directory */
4214921Sralph int	userid;		/* user's user ID */
4315113Sralph int	groupid;	/* user's group ID */
4414921Sralph 
4515349Sralph struct	passwd *pw;	/* pointer to static area used by getpwent */
4615349Sralph struct	group *gr;	/* pointer to static area used by getgrent */
4715349Sralph 
4814921Sralph main(argc, argv)
4914921Sralph 	int argc;
5014921Sralph 	char *argv[];
5114921Sralph {
5214921Sralph 	register char *arg;
5315218Sralph 	int cmdargs = 0;
5417914Sralph 	char *dhosts[NHOSTS], **hp = dhosts;
5514921Sralph 
5614921Sralph 	pw = getpwuid(userid = getuid());
5714921Sralph 	if (pw == NULL) {
5815113Sralph 		fprintf(stderr, "%s: Who are you?\n", argv[0]);
5914921Sralph 		exit(1);
6014921Sralph 	}
6114921Sralph 	strcpy(user, pw->pw_name);
6214921Sralph 	strcpy(homedir, pw->pw_dir);
6315113Sralph 	groupid = pw->pw_gid;
6414921Sralph 	gethostname(host, sizeof(host));
6514921Sralph 
6614921Sralph 	while (--argc > 0) {
6714921Sralph 		if ((arg = *++argv)[0] != '-')
6814921Sralph 			break;
6914921Sralph 		if (!strcmp(arg, "-Server"))
7014921Sralph 			iamremote++;
7114921Sralph 		else while (*++arg)
7214921Sralph 			switch (*arg) {
7314921Sralph 			case 'f':
7414921Sralph 				if (--argc <= 0)
7514921Sralph 					usage();
7614921Sralph 				distfile = *++argv;
7714921Sralph 				if (distfile[0] == '-' && distfile[1] == '\0')
7814921Sralph 					fin = stdin;
7914921Sralph 				break;
8014921Sralph 
8117914Sralph 			case 'm':
8217914Sralph 				if (--argc <= 0)
8317914Sralph 					usage();
8417914Sralph 				if (hp >= &dhosts[NHOSTS-2]) {
8517914Sralph 					fprintf(stderr, "rdist: too many destination hosts\n");
8617914Sralph 					exit(1);
8717914Sralph 				}
8817914Sralph 				*hp++ = *++argv;
8917914Sralph 				break;
9017914Sralph 
9114921Sralph 			case 'd':
9215113Sralph 				if (--argc <= 0)
9315113Sralph 					usage();
9415113Sralph 				define(*++argv);
9515113Sralph 				break;
9615113Sralph 
9715113Sralph 			case 'D':
9814921Sralph 				debug++;
9914921Sralph 				break;
10014921Sralph 
10115218Sralph 			case 'c':
10215218Sralph 				cmdargs++;
10315218Sralph 				break;
10415218Sralph 
10514921Sralph 			case 'n':
10615611Sralph 				if (options & VERIFY) {
10715611Sralph 					printf("rdist: -n overrides -v\n");
10815611Sralph 					options &= ~VERIFY;
10915611Sralph 				}
11014921Sralph 				nflag++;
11114921Sralph 				break;
11214921Sralph 
11314921Sralph 			case 'q':
11414921Sralph 				qflag++;
11514921Sralph 				break;
11614921Sralph 
11715293Sralph 			case 'b':
11815293Sralph 				options |= COMPARE;
11915293Sralph 				break;
12015293Sralph 
12115611Sralph 			case 'R':
12215271Sralph 				options |= REMOVE;
12315271Sralph 				break;
12415271Sralph 
12514921Sralph 			case 'v':
12615611Sralph 				if (nflag) {
12715611Sralph 					printf("rdist: -n overrides -v\n");
12815611Sralph 					break;
12915611Sralph 				}
13015218Sralph 				options |= VERIFY;
13114921Sralph 				break;
13214921Sralph 
13315218Sralph 			case 'w':
13415218Sralph 				options |= WHOLE;
13515218Sralph 				break;
13615218Sralph 
13714921Sralph 			case 'y':
13815218Sralph 				options |= YOUNGER;
13914921Sralph 				break;
14014921Sralph 
14116650Sralph 			case 'h':
14216650Sralph 				options |= FOLLOW;
14316650Sralph 				break;
14416650Sralph 
14516650Sralph 			case 'i':
14616650Sralph 				options |= IGNLNKS;
14716650Sralph 				break;
14816650Sralph 
14914921Sralph 			default:
15014921Sralph 				usage();
15114921Sralph 			}
15214921Sralph 	}
15317914Sralph 	*hp = NULL;
15415113Sralph 
15517481Sralph 	setreuid(0, userid);
15615113Sralph 	mktemp(tmpfile);
15716028Sralph 
15814921Sralph 	if (iamremote) {
15914921Sralph 		server();
16017481Sralph 		exit(nerrs != 0);
16114921Sralph 	}
16214921Sralph 
16315218Sralph 	if (cmdargs)
16415218Sralph 		docmdargs(argc, argv);
16515218Sralph 	else {
16620846Sbloom 		if (fin == NULL) {
16720846Sbloom 			if(distfile == NULL) {
16820846Sbloom 				if((fin = fopen("distfile","r")) == NULL)
16920846Sbloom 					fin = fopen("Distfile", "r");
17020846Sbloom 			} else
17120846Sbloom 				fin = fopen(distfile, "r");
17220846Sbloom 			if(fin == NULL) {
17320846Sbloom 				perror(distfile ? distfile : "distfile");
17420846Sbloom 				exit(1);
17520846Sbloom 			}
17615218Sralph 		}
17715218Sralph 		yyparse();
17816028Sralph 		if (nerrs == 0)
17917914Sralph 			docmds(dhosts, argc, argv);
18015113Sralph 	}
18115113Sralph 
18217481Sralph 	exit(nerrs != 0);
18314921Sralph }
18414921Sralph 
18514921Sralph usage()
18614921Sralph {
18717914Sralph 	printf("Usage: rdist [-nqbhirvwyD] [-f distfile] [-d var=value] [-m host] [file ...]\n");
18816650Sralph 	printf("or: rdist [-nqbhirvwyD] -c source [...] machine[:dest]\n");
18914921Sralph 	exit(1);
19014921Sralph }
19114921Sralph 
19214921Sralph /*
19315218Sralph  * rcp like interface for distributing files.
19415218Sralph  */
19515218Sralph docmdargs(nargs, args)
19615218Sralph 	int nargs;
19715218Sralph 	char *args[];
19815218Sralph {
19916028Sralph 	register struct namelist *nl, *prev;
20016028Sralph 	register char *cp;
20116028Sralph 	struct namelist *files, *hosts;
20216028Sralph 	struct subcmd *cmds;
20316028Sralph 	char *dest;
20416028Sralph 	static struct namelist tnl = { NULL, NULL };
20515218Sralph 	int i;
20615218Sralph 
20715218Sralph 	if (nargs < 2)
20815218Sralph 		usage();
20915218Sralph 
21015218Sralph 	prev = NULL;
21116028Sralph 	for (i = 0; i < nargs - 1; i++) {
21216028Sralph 		nl = makenl(args[i]);
21316028Sralph 		if (prev == NULL)
21416028Sralph 			files = prev = nl;
21516028Sralph 		else {
21616028Sralph 			prev->n_next = nl;
21716028Sralph 			prev = nl;
21816028Sralph 		}
21915218Sralph 	}
22015218Sralph 
22116028Sralph 	cp = args[i];
22216028Sralph 	if ((dest = index(cp, ':')) != NULL)
22316028Sralph 		*dest++ = '\0';
22416028Sralph 	tnl.n_name = cp;
22516028Sralph 	hosts = expand(&tnl, E_ALL);
22617914Sralph 	if (nerrs)
22717914Sralph 		exit(1);
22815218Sralph 
22916028Sralph 	if (dest == NULL || *dest == '\0')
23015218Sralph 		cmds = NULL;
23115218Sralph 	else {
23216028Sralph 		cmds = makesubcmd(INSTALL);
23316028Sralph 		cmds->sc_options = options;
23416028Sralph 		cmds->sc_name = dest;
23515218Sralph 	}
23615218Sralph 
23715218Sralph 	if (debug) {
23815218Sralph 		printf("docmdargs()\nfiles = ");
23915218Sralph 		prnames(files);
24015218Sralph 		printf("hosts = ");
24115218Sralph 		prnames(hosts);
24215218Sralph 	}
24316317Sralph 	insert(NULL, files, hosts, cmds);
24417914Sralph 	docmds(NULL, 0, NULL);
24515218Sralph }
24615218Sralph 
24715218Sralph /*
24814921Sralph  * Print a list of NAME blocks (mostly for debugging).
24914921Sralph  */
25016028Sralph prnames(nl)
25116028Sralph 	register struct namelist *nl;
25214921Sralph {
25314921Sralph 	printf("( ");
25416028Sralph 	while (nl != NULL) {
25516028Sralph 		printf("%s ", nl->n_name);
25616028Sralph 		nl = nl->n_next;
25714921Sralph 	}
25814921Sralph 	printf(")\n");
25914921Sralph }
26014921Sralph 
26114921Sralph /*VARARGS*/
26214921Sralph warn(fmt, a1, a2,a3)
26314921Sralph 	char *fmt;
26414921Sralph {
26514921Sralph 	extern int yylineno;
26614921Sralph 
26714921Sralph 	fprintf(stderr, "rdist: line %d: Warning: ", yylineno);
26814921Sralph 	fprintf(stderr, fmt, a1, a2, a3);
26914921Sralph 	fputc('\n', stderr);
27014921Sralph }
271