xref: /csrg-svn/usr.bin/rdist/main.c (revision 15293)
114921Sralph #ifndef lint
2*15293Sralph static	char *sccsid = "@(#)main.c	4.7 (Berkeley) 83/10/26";
314921Sralph #endif
414921Sralph 
514921Sralph #include "defs.h"
614921Sralph 
714921Sralph /*
814921Sralph  * Remote distribution program.
914921Sralph  */
1014921Sralph 
1114921Sralph char	*distfile = "distfile";
1215113Sralph char	tmpfile[] = "/tmp/rdistAXXXXXX";
1315113Sralph char	*tmpname = &tmpfile[5];
1415113Sralph char	*tmpinc = &tmpfile[10];
1514921Sralph 
1614921Sralph int	debug;		/* debugging flag */
1714921Sralph int	nflag;		/* NOP flag, just print commands without executing */
1814921Sralph int	qflag;		/* Quiet. Don't print messages */
1915218Sralph int	options;	/* global options */
2014921Sralph int	iamremote;	/* act as remote server for transfering files */
2114921Sralph 
2214921Sralph int	filec;		/* number of files to update */
2314921Sralph char	**filev;	/* list of files/directories to update */
2414921Sralph FILE	*fin = NULL;	/* input file pointer */
2514921Sralph int	rem = 0;	/* file descriptor to remote source/sink process */
2614921Sralph char	host[32];	/* host name */
2714921Sralph int	errs;		/* number of errors while sending/receiving */
2814921Sralph char	user[10];	/* user's name */
2914921Sralph char	homedir[128];	/* user's home directory */
3014921Sralph int	userid;		/* user's user ID */
3115113Sralph int	groupid;	/* user's group ID */
3214921Sralph 
3314921Sralph int	cleanup();
3414921Sralph int	lostconn();
3514921Sralph 
3614921Sralph main(argc, argv)
3714921Sralph 	int argc;
3814921Sralph 	char *argv[];
3914921Sralph {
4014921Sralph 	register char *arg;
4114921Sralph 	register struct	passwd *pw;
4215218Sralph 	int cmdargs = 0;
4314921Sralph 
4414921Sralph 	pw = getpwuid(userid = getuid());
4514921Sralph 	if (pw == NULL) {
4615113Sralph 		fprintf(stderr, "%s: Who are you?\n", argv[0]);
4714921Sralph 		exit(1);
4814921Sralph 	}
4914921Sralph 	strcpy(user, pw->pw_name);
5014921Sralph 	strcpy(homedir, pw->pw_dir);
5115113Sralph 	groupid = pw->pw_gid;
5214921Sralph 	gethostname(host, sizeof(host));
5314921Sralph 
5414921Sralph 	while (--argc > 0) {
5514921Sralph 		if ((arg = *++argv)[0] != '-')
5614921Sralph 			break;
5714921Sralph 		if (!strcmp(arg, "-Server"))
5814921Sralph 			iamremote++;
5914921Sralph 		else while (*++arg)
6014921Sralph 			switch (*arg) {
6114921Sralph 			case 'f':
6214921Sralph 				if (--argc <= 0)
6314921Sralph 					usage();
6414921Sralph 				distfile = *++argv;
6514921Sralph 				if (distfile[0] == '-' && distfile[1] == '\0')
6614921Sralph 					fin = stdin;
6714921Sralph 				break;
6814921Sralph 
6914921Sralph 			case 'd':
7015113Sralph 				if (--argc <= 0)
7115113Sralph 					usage();
7215113Sralph 				define(*++argv);
7315113Sralph 				break;
7415113Sralph 
7515113Sralph 			case 'D':
7614921Sralph 				debug++;
7714921Sralph 				break;
7814921Sralph 
7915218Sralph 			case 'c':
8015218Sralph 				cmdargs++;
8115218Sralph 				break;
8215218Sralph 
8314921Sralph 			case 'n':
8414921Sralph 				nflag++;
8514921Sralph 				break;
8614921Sralph 
8714921Sralph 			case 'q':
8814921Sralph 				qflag++;
8914921Sralph 				break;
9014921Sralph 
91*15293Sralph 			case 'b':
92*15293Sralph 				options |= COMPARE;
93*15293Sralph 				break;
94*15293Sralph 
9515271Sralph 			case 'r':
9615271Sralph 				options |= REMOVE;
9715271Sralph 				break;
9815271Sralph 
9914921Sralph 			case 'v':
10015218Sralph 				options |= VERIFY;
10114921Sralph 				break;
10214921Sralph 
10315218Sralph 			case 'w':
10415218Sralph 				options |= WHOLE;
10515218Sralph 				break;
10615218Sralph 
10714921Sralph 			case 'y':
10815218Sralph 				options |= YOUNGER;
10914921Sralph 				break;
11014921Sralph 
11114921Sralph 			default:
11214921Sralph 				usage();
11314921Sralph 			}
11414921Sralph 	}
11515113Sralph 
11615113Sralph 	mktemp(tmpfile);
11714921Sralph 	signal(SIGPIPE, lostconn);
11814921Sralph 	if (iamremote) {
11914921Sralph 		server();
12014921Sralph 		exit(errs);
12114921Sralph 	}
12214921Sralph 
12314921Sralph 	signal(SIGHUP, cleanup);
12414921Sralph 	signal(SIGINT, cleanup);
12514921Sralph 	signal(SIGQUIT, cleanup);
12614921Sralph 	signal(SIGTERM, cleanup);
12714921Sralph 
12815218Sralph 	if (cmdargs)
12915218Sralph 		docmdargs(argc, argv);
13015218Sralph 	else {
13115218Sralph 		filec = argc;
13215218Sralph 		filev = argv;
13315218Sralph 		if (fin == NULL && (fin = fopen(distfile, "r")) == NULL) {
13415218Sralph 			perror(distfile);
13515218Sralph 			exit(1);
13615218Sralph 		}
13715218Sralph 		yyparse();
13815113Sralph 	}
13915113Sralph 
14014921Sralph 	exit(errs);
14114921Sralph }
14214921Sralph 
14314921Sralph usage()
14414921Sralph {
145*15293Sralph 	printf("Usage: rdist [-nqbrvwyD] [-f distfile] [-d var=value] [file ...]\n");
146*15293Sralph 	printf("or: rdist [-nqbrvwyD] -c source [...] machine[:dest]\n");
14714921Sralph 	exit(1);
14814921Sralph }
14914921Sralph 
15014921Sralph /*
15115218Sralph  * rcp like interface for distributing files.
15215218Sralph  */
15315218Sralph docmdargs(nargs, args)
15415218Sralph 	int nargs;
15515218Sralph 	char *args[];
15615218Sralph {
15715218Sralph 	struct block *bp, *files, *hosts, *cmds, *prev;
15815218Sralph 	int i;
15915218Sralph 	char *pos, dest[BUFSIZ];
16015218Sralph 
16115218Sralph 	if (nargs < 2)
16215218Sralph 		usage();
16315218Sralph 
16415218Sralph 	prev = NULL;
16515218Sralph 	bp = files = ALLOC(block);
16615218Sralph 	for (i = 0; i < nargs - 1; bp = ALLOC(block), i++) {
16715218Sralph 		bp->b_type = NAME;
16815218Sralph 		bp->b_name = args[i];
16915218Sralph 		if (prev != NULL)
17015218Sralph 			prev->b_next = bp;
17115218Sralph 		bp->b_next = bp->b_args = NULL;
17215218Sralph 		prev = bp;
17315218Sralph 	}
17415218Sralph 
17515218Sralph 	hosts = ALLOC(block);
17615218Sralph 	hosts->b_type = NAME;
17715218Sralph 	hosts->b_name = args[i];
17815218Sralph 	hosts->b_name = args[i];
17915218Sralph 	hosts->b_next = hosts->b_args = NULL;
18015218Sralph 	if ((pos = index(hosts->b_name, ':')) != NULL) {
18115218Sralph 		*pos++ = '\0';
18215218Sralph 		strcpy(dest, pos);
18315218Sralph 	} else
18415218Sralph 		dest[0] = '\0';
18515218Sralph 
18615218Sralph 	hosts = expand(hosts, 0);
18715218Sralph 
18815218Sralph 	if (dest[0] == '\0')
18915218Sralph 		cmds = NULL;
19015218Sralph 	else {
19115218Sralph 		cmds = ALLOC(block);
19215218Sralph 		cmds->b_type = INSTALL;
19315218Sralph 		cmds->b_options = options;
19415218Sralph 		cmds->b_name = dest;
19515218Sralph 		cmds->b_next = cmds->b_args = NULL;
19615218Sralph 	}
19715218Sralph 
19815218Sralph 	if (debug) {
19915218Sralph 		printf("docmdargs()\nfiles = ");
20015218Sralph 		prnames(files);
20115218Sralph 		printf("hosts = ");
20215218Sralph 		prnames(hosts);
20315218Sralph 	}
20415218Sralph 	dohcmds(files, hosts, cmds);
20515218Sralph }
20615218Sralph 
20715218Sralph /*
20814921Sralph  * Remove temporary files and do any cleanup operations before exiting.
20914921Sralph  */
21014921Sralph cleanup()
21114921Sralph {
21215218Sralph 	do {
21315218Sralph 		(void) unlink(tmpfile);
21415218Sralph 		(*tmpinc)--;
21515218Sralph 	} while (*tmpinc >= 'A');
21614921Sralph 	exit(1);
21714921Sralph }
21814921Sralph 
21914921Sralph /*
22014921Sralph  * Print a list of NAME blocks (mostly for debugging).
22114921Sralph  */
22214921Sralph prnames(bp)
22314921Sralph 	register struct block *bp;
22414921Sralph {
22514921Sralph 	printf("( ");
22614921Sralph 	while (bp != NULL) {
22714921Sralph 		printf("%s ", bp->b_name);
22814921Sralph 		bp = bp->b_next;
22914921Sralph 	}
23014921Sralph 	printf(")\n");
23114921Sralph }
23214921Sralph 
23314921Sralph /*VARARGS*/
23414921Sralph warn(fmt, a1, a2,a3)
23514921Sralph 	char *fmt;
23614921Sralph {
23714921Sralph 	extern int yylineno;
23814921Sralph 
23914921Sralph 	fprintf(stderr, "rdist: line %d: Warning: ", yylineno);
24014921Sralph 	fprintf(stderr, fmt, a1, a2, a3);
24114921Sralph 	fputc('\n', stderr);
24214921Sralph }
243