xref: /csrg-svn/usr.bin/rdist/main.c (revision 15349)
114921Sralph #ifndef lint
2*15349Sralph static	char *sccsid = "@(#)main.c	4.8 (Berkeley) 83/11/01";
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 
33*15349Sralph struct	passwd *pw;	/* pointer to static area used by getpwent */
34*15349Sralph struct	group *gr;	/* pointer to static area used by getgrent */
35*15349Sralph 
3614921Sralph int	cleanup();
3714921Sralph int	lostconn();
3814921Sralph 
3914921Sralph main(argc, argv)
4014921Sralph 	int argc;
4114921Sralph 	char *argv[];
4214921Sralph {
4314921Sralph 	register char *arg;
4415218Sralph 	int cmdargs = 0;
4514921Sralph 
4614921Sralph 	pw = getpwuid(userid = getuid());
4714921Sralph 	if (pw == NULL) {
4815113Sralph 		fprintf(stderr, "%s: Who are you?\n", argv[0]);
4914921Sralph 		exit(1);
5014921Sralph 	}
5114921Sralph 	strcpy(user, pw->pw_name);
5214921Sralph 	strcpy(homedir, pw->pw_dir);
5315113Sralph 	groupid = pw->pw_gid;
5414921Sralph 	gethostname(host, sizeof(host));
5514921Sralph 
5614921Sralph 	while (--argc > 0) {
5714921Sralph 		if ((arg = *++argv)[0] != '-')
5814921Sralph 			break;
5914921Sralph 		if (!strcmp(arg, "-Server"))
6014921Sralph 			iamremote++;
6114921Sralph 		else while (*++arg)
6214921Sralph 			switch (*arg) {
6314921Sralph 			case 'f':
6414921Sralph 				if (--argc <= 0)
6514921Sralph 					usage();
6614921Sralph 				distfile = *++argv;
6714921Sralph 				if (distfile[0] == '-' && distfile[1] == '\0')
6814921Sralph 					fin = stdin;
6914921Sralph 				break;
7014921Sralph 
7114921Sralph 			case 'd':
7215113Sralph 				if (--argc <= 0)
7315113Sralph 					usage();
7415113Sralph 				define(*++argv);
7515113Sralph 				break;
7615113Sralph 
7715113Sralph 			case 'D':
7814921Sralph 				debug++;
7914921Sralph 				break;
8014921Sralph 
8115218Sralph 			case 'c':
8215218Sralph 				cmdargs++;
8315218Sralph 				break;
8415218Sralph 
8514921Sralph 			case 'n':
8614921Sralph 				nflag++;
8714921Sralph 				break;
8814921Sralph 
8914921Sralph 			case 'q':
9014921Sralph 				qflag++;
9114921Sralph 				break;
9214921Sralph 
9315293Sralph 			case 'b':
9415293Sralph 				options |= COMPARE;
9515293Sralph 				break;
9615293Sralph 
9715271Sralph 			case 'r':
9815271Sralph 				options |= REMOVE;
9915271Sralph 				break;
10015271Sralph 
10114921Sralph 			case 'v':
10215218Sralph 				options |= VERIFY;
10314921Sralph 				break;
10414921Sralph 
10515218Sralph 			case 'w':
10615218Sralph 				options |= WHOLE;
10715218Sralph 				break;
10815218Sralph 
10914921Sralph 			case 'y':
11015218Sralph 				options |= YOUNGER;
11114921Sralph 				break;
11214921Sralph 
11314921Sralph 			default:
11414921Sralph 				usage();
11514921Sralph 			}
11614921Sralph 	}
11715113Sralph 
11815113Sralph 	mktemp(tmpfile);
11914921Sralph 	signal(SIGPIPE, lostconn);
12014921Sralph 	if (iamremote) {
12114921Sralph 		server();
12214921Sralph 		exit(errs);
12314921Sralph 	}
12414921Sralph 
12514921Sralph 	signal(SIGHUP, cleanup);
12614921Sralph 	signal(SIGINT, cleanup);
12714921Sralph 	signal(SIGQUIT, cleanup);
12814921Sralph 	signal(SIGTERM, cleanup);
12914921Sralph 
13015218Sralph 	if (cmdargs)
13115218Sralph 		docmdargs(argc, argv);
13215218Sralph 	else {
13315218Sralph 		filec = argc;
13415218Sralph 		filev = argv;
13515218Sralph 		if (fin == NULL && (fin = fopen(distfile, "r")) == NULL) {
13615218Sralph 			perror(distfile);
13715218Sralph 			exit(1);
13815218Sralph 		}
13915218Sralph 		yyparse();
14015113Sralph 	}
14115113Sralph 
14214921Sralph 	exit(errs);
14314921Sralph }
14414921Sralph 
14514921Sralph usage()
14614921Sralph {
14715293Sralph 	printf("Usage: rdist [-nqbrvwyD] [-f distfile] [-d var=value] [file ...]\n");
14815293Sralph 	printf("or: rdist [-nqbrvwyD] -c source [...] machine[:dest]\n");
14914921Sralph 	exit(1);
15014921Sralph }
15114921Sralph 
15214921Sralph /*
15315218Sralph  * rcp like interface for distributing files.
15415218Sralph  */
15515218Sralph docmdargs(nargs, args)
15615218Sralph 	int nargs;
15715218Sralph 	char *args[];
15815218Sralph {
15915218Sralph 	struct block *bp, *files, *hosts, *cmds, *prev;
16015218Sralph 	int i;
16115218Sralph 	char *pos, dest[BUFSIZ];
16215218Sralph 
16315218Sralph 	if (nargs < 2)
16415218Sralph 		usage();
16515218Sralph 
16615218Sralph 	prev = NULL;
16715218Sralph 	bp = files = ALLOC(block);
16815218Sralph 	for (i = 0; i < nargs - 1; bp = ALLOC(block), i++) {
16915218Sralph 		bp->b_type = NAME;
17015218Sralph 		bp->b_name = args[i];
17115218Sralph 		if (prev != NULL)
17215218Sralph 			prev->b_next = bp;
17315218Sralph 		bp->b_next = bp->b_args = NULL;
17415218Sralph 		prev = bp;
17515218Sralph 	}
17615218Sralph 
17715218Sralph 	hosts = ALLOC(block);
17815218Sralph 	hosts->b_type = NAME;
17915218Sralph 	hosts->b_name = args[i];
18015218Sralph 	hosts->b_name = args[i];
18115218Sralph 	hosts->b_next = hosts->b_args = NULL;
18215218Sralph 	if ((pos = index(hosts->b_name, ':')) != NULL) {
18315218Sralph 		*pos++ = '\0';
18415218Sralph 		strcpy(dest, pos);
18515218Sralph 	} else
18615218Sralph 		dest[0] = '\0';
18715218Sralph 
18815218Sralph 	hosts = expand(hosts, 0);
18915218Sralph 
19015218Sralph 	if (dest[0] == '\0')
19115218Sralph 		cmds = NULL;
19215218Sralph 	else {
19315218Sralph 		cmds = ALLOC(block);
19415218Sralph 		cmds->b_type = INSTALL;
19515218Sralph 		cmds->b_options = options;
19615218Sralph 		cmds->b_name = dest;
19715218Sralph 		cmds->b_next = cmds->b_args = NULL;
19815218Sralph 	}
19915218Sralph 
20015218Sralph 	if (debug) {
20115218Sralph 		printf("docmdargs()\nfiles = ");
20215218Sralph 		prnames(files);
20315218Sralph 		printf("hosts = ");
20415218Sralph 		prnames(hosts);
20515218Sralph 	}
20615218Sralph 	dohcmds(files, hosts, cmds);
20715218Sralph }
20815218Sralph 
20915218Sralph /*
21014921Sralph  * Remove temporary files and do any cleanup operations before exiting.
21114921Sralph  */
21214921Sralph cleanup()
21314921Sralph {
21415218Sralph 	do {
21515218Sralph 		(void) unlink(tmpfile);
21615218Sralph 		(*tmpinc)--;
21715218Sralph 	} while (*tmpinc >= 'A');
21814921Sralph 	exit(1);
21914921Sralph }
22014921Sralph 
22114921Sralph /*
22214921Sralph  * Print a list of NAME blocks (mostly for debugging).
22314921Sralph  */
22414921Sralph prnames(bp)
22514921Sralph 	register struct block *bp;
22614921Sralph {
22714921Sralph 	printf("( ");
22814921Sralph 	while (bp != NULL) {
22914921Sralph 		printf("%s ", bp->b_name);
23014921Sralph 		bp = bp->b_next;
23114921Sralph 	}
23214921Sralph 	printf(")\n");
23314921Sralph }
23414921Sralph 
23514921Sralph /*VARARGS*/
23614921Sralph warn(fmt, a1, a2,a3)
23714921Sralph 	char *fmt;
23814921Sralph {
23914921Sralph 	extern int yylineno;
24014921Sralph 
24114921Sralph 	fprintf(stderr, "rdist: line %d: Warning: ", yylineno);
24214921Sralph 	fprintf(stderr, fmt, a1, a2, a3);
24314921Sralph 	fputc('\n', stderr);
24414921Sralph }
245