xref: /csrg-svn/usr.bin/rdist/main.c (revision 37856)
122374Sdist /*
222374Sdist  * Copyright (c) 1983 Regents of the University of California.
333415Sbostic  * All rights reserved.
433415Sbostic  *
533415Sbostic  * Redistribution and use in source and binary forms are permitted
634899Sbostic  * provided that the above copyright notice and this paragraph are
734899Sbostic  * duplicated in all such forms and that any documentation,
834899Sbostic  * advertising materials, and other materials related to such
934899Sbostic  * distribution and use acknowledge that the software was developed
1034899Sbostic  * by the University of California, Berkeley.  The name of the
1134899Sbostic  * University may not be used to endorse or promote products derived
1234899Sbostic  * from this software without specific prior written permission.
1334899Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1434899Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1534899Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1622374Sdist  */
1722374Sdist 
1814921Sralph #ifndef lint
1922374Sdist char copyright[] =
2022374Sdist "@(#) Copyright (c) 1983 Regents of the University of California.\n\
2122374Sdist  All rights reserved.\n";
2233415Sbostic #endif /* not lint */
2314921Sralph 
2422374Sdist #ifndef lint
25*37856Sbostic static char sccsid[] = "@(#)main.c	5.4 (Berkeley) 05/11/89";
2633415Sbostic #endif /* not lint */
2722374Sdist 
2814921Sralph #include "defs.h"
2914921Sralph 
3017914Sralph #define NHOSTS 100
3117914Sralph 
3214921Sralph /*
3314921Sralph  * Remote distribution program.
3414921Sralph  */
3514921Sralph 
3620846Sbloom char	*distfile = NULL;
37*37856Sbostic char	tmpfile[] = _PATH_TMP;
3815113Sralph char	*tmpname = &tmpfile[5];
3914921Sralph 
4014921Sralph int	debug;		/* debugging flag */
4114921Sralph int	nflag;		/* NOP flag, just print commands without executing */
4214921Sralph int	qflag;		/* Quiet. Don't print messages */
4315218Sralph int	options;	/* global options */
4414921Sralph int	iamremote;	/* act as remote server for transfering files */
4514921Sralph 
4614921Sralph FILE	*fin = NULL;	/* input file pointer */
4716028Sralph int	rem = -1;	/* file descriptor to remote source/sink process */
4814921Sralph char	host[32];	/* host name */
4916028Sralph int	nerrs;		/* number of errors while sending/receiving */
5014921Sralph char	user[10];	/* user's name */
5114921Sralph char	homedir[128];	/* user's home directory */
5214921Sralph int	userid;		/* user's user ID */
5315113Sralph int	groupid;	/* user's group ID */
5414921Sralph 
5515349Sralph struct	passwd *pw;	/* pointer to static area used by getpwent */
5615349Sralph struct	group *gr;	/* pointer to static area used by getgrent */
5715349Sralph 
5814921Sralph main(argc, argv)
5914921Sralph 	int argc;
6014921Sralph 	char *argv[];
6114921Sralph {
6214921Sralph 	register char *arg;
6315218Sralph 	int cmdargs = 0;
6417914Sralph 	char *dhosts[NHOSTS], **hp = dhosts;
6514921Sralph 
6614921Sralph 	pw = getpwuid(userid = getuid());
6714921Sralph 	if (pw == NULL) {
6815113Sralph 		fprintf(stderr, "%s: Who are you?\n", argv[0]);
6914921Sralph 		exit(1);
7014921Sralph 	}
7114921Sralph 	strcpy(user, pw->pw_name);
7214921Sralph 	strcpy(homedir, pw->pw_dir);
7315113Sralph 	groupid = pw->pw_gid;
7414921Sralph 	gethostname(host, sizeof(host));
7514921Sralph 
7614921Sralph 	while (--argc > 0) {
7714921Sralph 		if ((arg = *++argv)[0] != '-')
7814921Sralph 			break;
7914921Sralph 		if (!strcmp(arg, "-Server"))
8014921Sralph 			iamremote++;
8114921Sralph 		else while (*++arg)
8214921Sralph 			switch (*arg) {
8314921Sralph 			case 'f':
8414921Sralph 				if (--argc <= 0)
8514921Sralph 					usage();
8614921Sralph 				distfile = *++argv;
8714921Sralph 				if (distfile[0] == '-' && distfile[1] == '\0')
8814921Sralph 					fin = stdin;
8914921Sralph 				break;
9014921Sralph 
9117914Sralph 			case 'm':
9217914Sralph 				if (--argc <= 0)
9317914Sralph 					usage();
9417914Sralph 				if (hp >= &dhosts[NHOSTS-2]) {
9517914Sralph 					fprintf(stderr, "rdist: too many destination hosts\n");
9617914Sralph 					exit(1);
9717914Sralph 				}
9817914Sralph 				*hp++ = *++argv;
9917914Sralph 				break;
10017914Sralph 
10114921Sralph 			case 'd':
10215113Sralph 				if (--argc <= 0)
10315113Sralph 					usage();
10415113Sralph 				define(*++argv);
10515113Sralph 				break;
10615113Sralph 
10715113Sralph 			case 'D':
10814921Sralph 				debug++;
10914921Sralph 				break;
11014921Sralph 
11115218Sralph 			case 'c':
11215218Sralph 				cmdargs++;
11315218Sralph 				break;
11415218Sralph 
11514921Sralph 			case 'n':
11615611Sralph 				if (options & VERIFY) {
11715611Sralph 					printf("rdist: -n overrides -v\n");
11815611Sralph 					options &= ~VERIFY;
11915611Sralph 				}
12014921Sralph 				nflag++;
12114921Sralph 				break;
12214921Sralph 
12314921Sralph 			case 'q':
12414921Sralph 				qflag++;
12514921Sralph 				break;
12614921Sralph 
12715293Sralph 			case 'b':
12815293Sralph 				options |= COMPARE;
12915293Sralph 				break;
13015293Sralph 
13115611Sralph 			case 'R':
13215271Sralph 				options |= REMOVE;
13315271Sralph 				break;
13415271Sralph 
13514921Sralph 			case 'v':
13615611Sralph 				if (nflag) {
13715611Sralph 					printf("rdist: -n overrides -v\n");
13815611Sralph 					break;
13915611Sralph 				}
14015218Sralph 				options |= VERIFY;
14114921Sralph 				break;
14214921Sralph 
14315218Sralph 			case 'w':
14415218Sralph 				options |= WHOLE;
14515218Sralph 				break;
14615218Sralph 
14714921Sralph 			case 'y':
14815218Sralph 				options |= YOUNGER;
14914921Sralph 				break;
15014921Sralph 
15116650Sralph 			case 'h':
15216650Sralph 				options |= FOLLOW;
15316650Sralph 				break;
15416650Sralph 
15516650Sralph 			case 'i':
15616650Sralph 				options |= IGNLNKS;
15716650Sralph 				break;
15816650Sralph 
15914921Sralph 			default:
16014921Sralph 				usage();
16114921Sralph 			}
16214921Sralph 	}
16317914Sralph 	*hp = NULL;
16415113Sralph 
16517481Sralph 	setreuid(0, userid);
16615113Sralph 	mktemp(tmpfile);
16716028Sralph 
16814921Sralph 	if (iamremote) {
16914921Sralph 		server();
17017481Sralph 		exit(nerrs != 0);
17114921Sralph 	}
17214921Sralph 
17315218Sralph 	if (cmdargs)
17415218Sralph 		docmdargs(argc, argv);
17515218Sralph 	else {
17620846Sbloom 		if (fin == NULL) {
17720846Sbloom 			if(distfile == NULL) {
17820846Sbloom 				if((fin = fopen("distfile","r")) == NULL)
17920846Sbloom 					fin = fopen("Distfile", "r");
18020846Sbloom 			} else
18120846Sbloom 				fin = fopen(distfile, "r");
18220846Sbloom 			if(fin == NULL) {
18320846Sbloom 				perror(distfile ? distfile : "distfile");
18420846Sbloom 				exit(1);
18520846Sbloom 			}
18615218Sralph 		}
18715218Sralph 		yyparse();
18816028Sralph 		if (nerrs == 0)
18917914Sralph 			docmds(dhosts, argc, argv);
19015113Sralph 	}
19115113Sralph 
19217481Sralph 	exit(nerrs != 0);
19314921Sralph }
19414921Sralph 
19514921Sralph usage()
19614921Sralph {
19717914Sralph 	printf("Usage: rdist [-nqbhirvwyD] [-f distfile] [-d var=value] [-m host] [file ...]\n");
19816650Sralph 	printf("or: rdist [-nqbhirvwyD] -c source [...] machine[:dest]\n");
19914921Sralph 	exit(1);
20014921Sralph }
20114921Sralph 
20214921Sralph /*
20315218Sralph  * rcp like interface for distributing files.
20415218Sralph  */
20515218Sralph docmdargs(nargs, args)
20615218Sralph 	int nargs;
20715218Sralph 	char *args[];
20815218Sralph {
20916028Sralph 	register struct namelist *nl, *prev;
21016028Sralph 	register char *cp;
21116028Sralph 	struct namelist *files, *hosts;
21216028Sralph 	struct subcmd *cmds;
21316028Sralph 	char *dest;
21416028Sralph 	static struct namelist tnl = { NULL, NULL };
21515218Sralph 	int i;
21615218Sralph 
21715218Sralph 	if (nargs < 2)
21815218Sralph 		usage();
21915218Sralph 
22015218Sralph 	prev = NULL;
22116028Sralph 	for (i = 0; i < nargs - 1; i++) {
22216028Sralph 		nl = makenl(args[i]);
22316028Sralph 		if (prev == NULL)
22416028Sralph 			files = prev = nl;
22516028Sralph 		else {
22616028Sralph 			prev->n_next = nl;
22716028Sralph 			prev = nl;
22816028Sralph 		}
22915218Sralph 	}
23015218Sralph 
23116028Sralph 	cp = args[i];
23216028Sralph 	if ((dest = index(cp, ':')) != NULL)
23316028Sralph 		*dest++ = '\0';
23416028Sralph 	tnl.n_name = cp;
23516028Sralph 	hosts = expand(&tnl, E_ALL);
23617914Sralph 	if (nerrs)
23717914Sralph 		exit(1);
23815218Sralph 
23916028Sralph 	if (dest == NULL || *dest == '\0')
24015218Sralph 		cmds = NULL;
24115218Sralph 	else {
24216028Sralph 		cmds = makesubcmd(INSTALL);
24316028Sralph 		cmds->sc_options = options;
24416028Sralph 		cmds->sc_name = dest;
24515218Sralph 	}
24615218Sralph 
24715218Sralph 	if (debug) {
24815218Sralph 		printf("docmdargs()\nfiles = ");
24915218Sralph 		prnames(files);
25015218Sralph 		printf("hosts = ");
25115218Sralph 		prnames(hosts);
25215218Sralph 	}
25316317Sralph 	insert(NULL, files, hosts, cmds);
25417914Sralph 	docmds(NULL, 0, NULL);
25515218Sralph }
25615218Sralph 
25715218Sralph /*
25814921Sralph  * Print a list of NAME blocks (mostly for debugging).
25914921Sralph  */
26016028Sralph prnames(nl)
26116028Sralph 	register struct namelist *nl;
26214921Sralph {
26314921Sralph 	printf("( ");
26416028Sralph 	while (nl != NULL) {
26516028Sralph 		printf("%s ", nl->n_name);
26616028Sralph 		nl = nl->n_next;
26714921Sralph 	}
26814921Sralph 	printf(")\n");
26914921Sralph }
27014921Sralph 
27114921Sralph /*VARARGS*/
27214921Sralph warn(fmt, a1, a2,a3)
27314921Sralph 	char *fmt;
27414921Sralph {
27514921Sralph 	extern int yylineno;
27614921Sralph 
27714921Sralph 	fprintf(stderr, "rdist: line %d: Warning: ", yylineno);
27814921Sralph 	fprintf(stderr, fmt, a1, a2, a3);
27914921Sralph 	fputc('\n', stderr);
28014921Sralph }
281