xref: /csrg-svn/usr.bin/rdist/main.c (revision 17481)
114921Sralph #ifndef lint
2*17481Sralph static	char *sccsid = "@(#)main.c	4.13 (Berkeley) 84/12/06";
314921Sralph #endif
414921Sralph 
514921Sralph #include "defs.h"
614921Sralph 
714921Sralph /*
814921Sralph  * Remote distribution program.
914921Sralph  */
1014921Sralph 
1114921Sralph char	*distfile = "distfile";
1215611Sralph char	tmpfile[] = "/tmp/rdistXXXXXX";
1315113Sralph char	*tmpname = &tmpfile[5];
1414921Sralph 
1514921Sralph int	debug;		/* debugging flag */
1614921Sralph int	nflag;		/* NOP flag, just print commands without executing */
1714921Sralph int	qflag;		/* Quiet. Don't print messages */
1815218Sralph int	options;	/* global options */
1914921Sralph int	iamremote;	/* act as remote server for transfering files */
2014921Sralph 
2114921Sralph FILE	*fin = NULL;	/* input file pointer */
2216028Sralph int	rem = -1;	/* file descriptor to remote source/sink process */
2314921Sralph char	host[32];	/* host name */
2416028Sralph int	nerrs;		/* number of errors while sending/receiving */
2514921Sralph char	user[10];	/* user's name */
2614921Sralph char	homedir[128];	/* user's home directory */
2714921Sralph int	userid;		/* user's user ID */
2815113Sralph int	groupid;	/* user's group ID */
2914921Sralph 
3015349Sralph struct	passwd *pw;	/* pointer to static area used by getpwent */
3115349Sralph struct	group *gr;	/* pointer to static area used by getgrent */
3215349Sralph 
3314921Sralph main(argc, argv)
3414921Sralph 	int argc;
3514921Sralph 	char *argv[];
3614921Sralph {
3714921Sralph 	register char *arg;
3815218Sralph 	int cmdargs = 0;
3914921Sralph 
4014921Sralph 	pw = getpwuid(userid = getuid());
4114921Sralph 	if (pw == NULL) {
4215113Sralph 		fprintf(stderr, "%s: Who are you?\n", argv[0]);
4314921Sralph 		exit(1);
4414921Sralph 	}
4514921Sralph 	strcpy(user, pw->pw_name);
4614921Sralph 	strcpy(homedir, pw->pw_dir);
4715113Sralph 	groupid = pw->pw_gid;
4814921Sralph 	gethostname(host, sizeof(host));
4914921Sralph 
5014921Sralph 	while (--argc > 0) {
5114921Sralph 		if ((arg = *++argv)[0] != '-')
5214921Sralph 			break;
5314921Sralph 		if (!strcmp(arg, "-Server"))
5414921Sralph 			iamremote++;
5514921Sralph 		else while (*++arg)
5614921Sralph 			switch (*arg) {
5714921Sralph 			case 'f':
5814921Sralph 				if (--argc <= 0)
5914921Sralph 					usage();
6014921Sralph 				distfile = *++argv;
6114921Sralph 				if (distfile[0] == '-' && distfile[1] == '\0')
6214921Sralph 					fin = stdin;
6314921Sralph 				break;
6414921Sralph 
6514921Sralph 			case 'd':
6615113Sralph 				if (--argc <= 0)
6715113Sralph 					usage();
6815113Sralph 				define(*++argv);
6915113Sralph 				break;
7015113Sralph 
7115113Sralph 			case 'D':
7214921Sralph 				debug++;
7314921Sralph 				break;
7414921Sralph 
7515218Sralph 			case 'c':
7615218Sralph 				cmdargs++;
7715218Sralph 				break;
7815218Sralph 
7914921Sralph 			case 'n':
8015611Sralph 				if (options & VERIFY) {
8115611Sralph 					printf("rdist: -n overrides -v\n");
8215611Sralph 					options &= ~VERIFY;
8315611Sralph 				}
8414921Sralph 				nflag++;
8514921Sralph 				break;
8614921Sralph 
8714921Sralph 			case 'q':
8814921Sralph 				qflag++;
8914921Sralph 				break;
9014921Sralph 
9115293Sralph 			case 'b':
9215293Sralph 				options |= COMPARE;
9315293Sralph 				break;
9415293Sralph 
9515611Sralph 			case 'R':
9615271Sralph 				options |= REMOVE;
9715271Sralph 				break;
9815271Sralph 
9914921Sralph 			case 'v':
10015611Sralph 				if (nflag) {
10115611Sralph 					printf("rdist: -n overrides -v\n");
10215611Sralph 					break;
10315611Sralph 				}
10415218Sralph 				options |= VERIFY;
10514921Sralph 				break;
10614921Sralph 
10715218Sralph 			case 'w':
10815218Sralph 				options |= WHOLE;
10915218Sralph 				break;
11015218Sralph 
11114921Sralph 			case 'y':
11215218Sralph 				options |= YOUNGER;
11314921Sralph 				break;
11414921Sralph 
11516650Sralph 			case 'h':
11616650Sralph 				options |= FOLLOW;
11716650Sralph 				break;
11816650Sralph 
11916650Sralph 			case 'i':
12016650Sralph 				options |= IGNLNKS;
12116650Sralph 				break;
12216650Sralph 
12314921Sralph 			default:
12414921Sralph 				usage();
12514921Sralph 			}
12614921Sralph 	}
12715113Sralph 
128*17481Sralph 	setreuid(0, userid);
12915113Sralph 	mktemp(tmpfile);
13016028Sralph 
13114921Sralph 	if (iamremote) {
13214921Sralph 		server();
133*17481Sralph 		exit(nerrs != 0);
13414921Sralph 	}
13514921Sralph 
13615218Sralph 	if (cmdargs)
13715218Sralph 		docmdargs(argc, argv);
13815218Sralph 	else {
13915218Sralph 		if (fin == NULL && (fin = fopen(distfile, "r")) == NULL) {
14015218Sralph 			perror(distfile);
14115218Sralph 			exit(1);
14215218Sralph 		}
14315218Sralph 		yyparse();
14416028Sralph 		if (nerrs == 0)
14516028Sralph 			docmds(argc, argv);
14615113Sralph 	}
14715113Sralph 
148*17481Sralph 	exit(nerrs != 0);
14914921Sralph }
15014921Sralph 
15114921Sralph usage()
15214921Sralph {
15316650Sralph 	printf("Usage: rdist [-nqbhirvwyD] [-f distfile] [-d var=value] [file ...]\n");
15416650Sralph 	printf("or: rdist [-nqbhirvwyD] -c source [...] machine[:dest]\n");
15514921Sralph 	exit(1);
15614921Sralph }
15714921Sralph 
15814921Sralph /*
15915218Sralph  * rcp like interface for distributing files.
16015218Sralph  */
16115218Sralph docmdargs(nargs, args)
16215218Sralph 	int nargs;
16315218Sralph 	char *args[];
16415218Sralph {
16516028Sralph 	register struct namelist *nl, *prev;
16616028Sralph 	register char *cp;
16716028Sralph 	struct namelist *files, *hosts;
16816028Sralph 	struct subcmd *cmds;
16916028Sralph 	char *dest;
17016028Sralph 	static struct namelist tnl = { NULL, NULL };
17115218Sralph 	int i;
17215218Sralph 
17315218Sralph 	if (nargs < 2)
17415218Sralph 		usage();
17515218Sralph 
17615218Sralph 	prev = NULL;
17716028Sralph 	for (i = 0; i < nargs - 1; i++) {
17816028Sralph 		nl = makenl(args[i]);
17916028Sralph 		if (prev == NULL)
18016028Sralph 			files = prev = nl;
18116028Sralph 		else {
18216028Sralph 			prev->n_next = nl;
18316028Sralph 			prev = nl;
18416028Sralph 		}
18515218Sralph 	}
18615218Sralph 
18716028Sralph 	cp = args[i];
18816028Sralph 	if ((dest = index(cp, ':')) != NULL)
18916028Sralph 		*dest++ = '\0';
19016028Sralph 	tnl.n_name = cp;
19116028Sralph 	hosts = expand(&tnl, E_ALL);
19215218Sralph 
19316028Sralph 	if (dest == NULL || *dest == '\0')
19415218Sralph 		cmds = NULL;
19515218Sralph 	else {
19616028Sralph 		cmds = makesubcmd(INSTALL);
19716028Sralph 		cmds->sc_options = options;
19816028Sralph 		cmds->sc_name = dest;
19915218Sralph 	}
20015218Sralph 
20115218Sralph 	if (debug) {
20215218Sralph 		printf("docmdargs()\nfiles = ");
20315218Sralph 		prnames(files);
20415218Sralph 		printf("hosts = ");
20515218Sralph 		prnames(hosts);
20615218Sralph 	}
20716317Sralph 	insert(NULL, files, hosts, cmds);
20816028Sralph 	docmds(0, NULL);
20915218Sralph }
21015218Sralph 
21115218Sralph /*
21214921Sralph  * Print a list of NAME blocks (mostly for debugging).
21314921Sralph  */
21416028Sralph prnames(nl)
21516028Sralph 	register struct namelist *nl;
21614921Sralph {
21714921Sralph 	printf("( ");
21816028Sralph 	while (nl != NULL) {
21916028Sralph 		printf("%s ", nl->n_name);
22016028Sralph 		nl = nl->n_next;
22114921Sralph 	}
22214921Sralph 	printf(")\n");
22314921Sralph }
22414921Sralph 
22514921Sralph /*VARARGS*/
22614921Sralph warn(fmt, a1, a2,a3)
22714921Sralph 	char *fmt;
22814921Sralph {
22914921Sralph 	extern int yylineno;
23014921Sralph 
23114921Sralph 	fprintf(stderr, "rdist: line %d: Warning: ", yylineno);
23214921Sralph 	fprintf(stderr, fmt, a1, a2, a3);
23314921Sralph 	fputc('\n', stderr);
23414921Sralph }
235