xref: /csrg-svn/usr.bin/rdist/main.c (revision 20846)
114921Sralph #ifndef lint
2*20846Sbloom static	char *sccsid = "@(#)main.c	4.15 (Berkeley) 85/05/16";
314921Sralph #endif
414921Sralph 
514921Sralph #include "defs.h"
614921Sralph 
717914Sralph #define NHOSTS 100
817914Sralph 
914921Sralph /*
1014921Sralph  * Remote distribution program.
1114921Sralph  */
1214921Sralph 
13*20846Sbloom char	*distfile = NULL;
1415611Sralph char	tmpfile[] = "/tmp/rdistXXXXXX";
1515113Sralph char	*tmpname = &tmpfile[5];
1614921Sralph 
1714921Sralph int	debug;		/* debugging flag */
1814921Sralph int	nflag;		/* NOP flag, just print commands without executing */
1914921Sralph int	qflag;		/* Quiet. Don't print messages */
2015218Sralph int	options;	/* global options */
2114921Sralph int	iamremote;	/* act as remote server for transfering files */
2214921Sralph 
2314921Sralph FILE	*fin = NULL;	/* input file pointer */
2416028Sralph int	rem = -1;	/* file descriptor to remote source/sink process */
2514921Sralph char	host[32];	/* host name */
2616028Sralph int	nerrs;		/* number of errors while sending/receiving */
2714921Sralph char	user[10];	/* user's name */
2814921Sralph char	homedir[128];	/* user's home directory */
2914921Sralph int	userid;		/* user's user ID */
3015113Sralph int	groupid;	/* user's group ID */
3114921Sralph 
3215349Sralph struct	passwd *pw;	/* pointer to static area used by getpwent */
3315349Sralph struct	group *gr;	/* pointer to static area used by getgrent */
3415349Sralph 
3514921Sralph main(argc, argv)
3614921Sralph 	int argc;
3714921Sralph 	char *argv[];
3814921Sralph {
3914921Sralph 	register char *arg;
4015218Sralph 	int cmdargs = 0;
4117914Sralph 	char *dhosts[NHOSTS], **hp = dhosts;
4214921Sralph 
4314921Sralph 	pw = getpwuid(userid = getuid());
4414921Sralph 	if (pw == NULL) {
4515113Sralph 		fprintf(stderr, "%s: Who are you?\n", argv[0]);
4614921Sralph 		exit(1);
4714921Sralph 	}
4814921Sralph 	strcpy(user, pw->pw_name);
4914921Sralph 	strcpy(homedir, pw->pw_dir);
5015113Sralph 	groupid = pw->pw_gid;
5114921Sralph 	gethostname(host, sizeof(host));
5214921Sralph 
5314921Sralph 	while (--argc > 0) {
5414921Sralph 		if ((arg = *++argv)[0] != '-')
5514921Sralph 			break;
5614921Sralph 		if (!strcmp(arg, "-Server"))
5714921Sralph 			iamremote++;
5814921Sralph 		else while (*++arg)
5914921Sralph 			switch (*arg) {
6014921Sralph 			case 'f':
6114921Sralph 				if (--argc <= 0)
6214921Sralph 					usage();
6314921Sralph 				distfile = *++argv;
6414921Sralph 				if (distfile[0] == '-' && distfile[1] == '\0')
6514921Sralph 					fin = stdin;
6614921Sralph 				break;
6714921Sralph 
6817914Sralph 			case 'm':
6917914Sralph 				if (--argc <= 0)
7017914Sralph 					usage();
7117914Sralph 				if (hp >= &dhosts[NHOSTS-2]) {
7217914Sralph 					fprintf(stderr, "rdist: too many destination hosts\n");
7317914Sralph 					exit(1);
7417914Sralph 				}
7517914Sralph 				*hp++ = *++argv;
7617914Sralph 				break;
7717914Sralph 
7814921Sralph 			case 'd':
7915113Sralph 				if (--argc <= 0)
8015113Sralph 					usage();
8115113Sralph 				define(*++argv);
8215113Sralph 				break;
8315113Sralph 
8415113Sralph 			case 'D':
8514921Sralph 				debug++;
8614921Sralph 				break;
8714921Sralph 
8815218Sralph 			case 'c':
8915218Sralph 				cmdargs++;
9015218Sralph 				break;
9115218Sralph 
9214921Sralph 			case 'n':
9315611Sralph 				if (options & VERIFY) {
9415611Sralph 					printf("rdist: -n overrides -v\n");
9515611Sralph 					options &= ~VERIFY;
9615611Sralph 				}
9714921Sralph 				nflag++;
9814921Sralph 				break;
9914921Sralph 
10014921Sralph 			case 'q':
10114921Sralph 				qflag++;
10214921Sralph 				break;
10314921Sralph 
10415293Sralph 			case 'b':
10515293Sralph 				options |= COMPARE;
10615293Sralph 				break;
10715293Sralph 
10815611Sralph 			case 'R':
10915271Sralph 				options |= REMOVE;
11015271Sralph 				break;
11115271Sralph 
11214921Sralph 			case 'v':
11315611Sralph 				if (nflag) {
11415611Sralph 					printf("rdist: -n overrides -v\n");
11515611Sralph 					break;
11615611Sralph 				}
11715218Sralph 				options |= VERIFY;
11814921Sralph 				break;
11914921Sralph 
12015218Sralph 			case 'w':
12115218Sralph 				options |= WHOLE;
12215218Sralph 				break;
12315218Sralph 
12414921Sralph 			case 'y':
12515218Sralph 				options |= YOUNGER;
12614921Sralph 				break;
12714921Sralph 
12816650Sralph 			case 'h':
12916650Sralph 				options |= FOLLOW;
13016650Sralph 				break;
13116650Sralph 
13216650Sralph 			case 'i':
13316650Sralph 				options |= IGNLNKS;
13416650Sralph 				break;
13516650Sralph 
13614921Sralph 			default:
13714921Sralph 				usage();
13814921Sralph 			}
13914921Sralph 	}
14017914Sralph 	*hp = NULL;
14115113Sralph 
14217481Sralph 	setreuid(0, userid);
14315113Sralph 	mktemp(tmpfile);
14416028Sralph 
14514921Sralph 	if (iamremote) {
14614921Sralph 		server();
14717481Sralph 		exit(nerrs != 0);
14814921Sralph 	}
14914921Sralph 
15015218Sralph 	if (cmdargs)
15115218Sralph 		docmdargs(argc, argv);
15215218Sralph 	else {
153*20846Sbloom 		if (fin == NULL) {
154*20846Sbloom 			if(distfile == NULL) {
155*20846Sbloom 				if((fin = fopen("distfile","r")) == NULL)
156*20846Sbloom 					fin = fopen("Distfile", "r");
157*20846Sbloom 			} else
158*20846Sbloom 				fin = fopen(distfile, "r");
159*20846Sbloom 			if(fin == NULL) {
160*20846Sbloom 				perror(distfile ? distfile : "distfile");
161*20846Sbloom 				exit(1);
162*20846Sbloom 			}
16315218Sralph 		}
16415218Sralph 		yyparse();
16516028Sralph 		if (nerrs == 0)
16617914Sralph 			docmds(dhosts, argc, argv);
16715113Sralph 	}
16815113Sralph 
16917481Sralph 	exit(nerrs != 0);
17014921Sralph }
17114921Sralph 
17214921Sralph usage()
17314921Sralph {
17417914Sralph 	printf("Usage: rdist [-nqbhirvwyD] [-f distfile] [-d var=value] [-m host] [file ...]\n");
17516650Sralph 	printf("or: rdist [-nqbhirvwyD] -c source [...] machine[:dest]\n");
17614921Sralph 	exit(1);
17714921Sralph }
17814921Sralph 
17914921Sralph /*
18015218Sralph  * rcp like interface for distributing files.
18115218Sralph  */
18215218Sralph docmdargs(nargs, args)
18315218Sralph 	int nargs;
18415218Sralph 	char *args[];
18515218Sralph {
18616028Sralph 	register struct namelist *nl, *prev;
18716028Sralph 	register char *cp;
18816028Sralph 	struct namelist *files, *hosts;
18916028Sralph 	struct subcmd *cmds;
19016028Sralph 	char *dest;
19116028Sralph 	static struct namelist tnl = { NULL, NULL };
19215218Sralph 	int i;
19315218Sralph 
19415218Sralph 	if (nargs < 2)
19515218Sralph 		usage();
19615218Sralph 
19715218Sralph 	prev = NULL;
19816028Sralph 	for (i = 0; i < nargs - 1; i++) {
19916028Sralph 		nl = makenl(args[i]);
20016028Sralph 		if (prev == NULL)
20116028Sralph 			files = prev = nl;
20216028Sralph 		else {
20316028Sralph 			prev->n_next = nl;
20416028Sralph 			prev = nl;
20516028Sralph 		}
20615218Sralph 	}
20715218Sralph 
20816028Sralph 	cp = args[i];
20916028Sralph 	if ((dest = index(cp, ':')) != NULL)
21016028Sralph 		*dest++ = '\0';
21116028Sralph 	tnl.n_name = cp;
21216028Sralph 	hosts = expand(&tnl, E_ALL);
21317914Sralph 	if (nerrs)
21417914Sralph 		exit(1);
21515218Sralph 
21616028Sralph 	if (dest == NULL || *dest == '\0')
21715218Sralph 		cmds = NULL;
21815218Sralph 	else {
21916028Sralph 		cmds = makesubcmd(INSTALL);
22016028Sralph 		cmds->sc_options = options;
22116028Sralph 		cmds->sc_name = dest;
22215218Sralph 	}
22315218Sralph 
22415218Sralph 	if (debug) {
22515218Sralph 		printf("docmdargs()\nfiles = ");
22615218Sralph 		prnames(files);
22715218Sralph 		printf("hosts = ");
22815218Sralph 		prnames(hosts);
22915218Sralph 	}
23016317Sralph 	insert(NULL, files, hosts, cmds);
23117914Sralph 	docmds(NULL, 0, NULL);
23215218Sralph }
23315218Sralph 
23415218Sralph /*
23514921Sralph  * Print a list of NAME blocks (mostly for debugging).
23614921Sralph  */
23716028Sralph prnames(nl)
23816028Sralph 	register struct namelist *nl;
23914921Sralph {
24014921Sralph 	printf("( ");
24116028Sralph 	while (nl != NULL) {
24216028Sralph 		printf("%s ", nl->n_name);
24316028Sralph 		nl = nl->n_next;
24414921Sralph 	}
24514921Sralph 	printf(")\n");
24614921Sralph }
24714921Sralph 
24814921Sralph /*VARARGS*/
24914921Sralph warn(fmt, a1, a2,a3)
25014921Sralph 	char *fmt;
25114921Sralph {
25214921Sralph 	extern int yylineno;
25314921Sralph 
25414921Sralph 	fprintf(stderr, "rdist: line %d: Warning: ", yylineno);
25514921Sralph 	fprintf(stderr, fmt, a1, a2, a3);
25614921Sralph 	fputc('\n', stderr);
25714921Sralph }
258