xref: /original-bsd/usr.bin/rdist/main.c (revision e22fe102)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 char copyright[] =
10 "@(#) Copyright (c) 1983 Regents of the University of California.\n\
11  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)main.c	5.5 (Berkeley) 06/01/90";
16 #endif /* not lint */
17 
18 #include "defs.h"
19 
20 #define NHOSTS 100
21 
22 /*
23  * Remote distribution program.
24  */
25 
26 char	*distfile = NULL;
27 char	tmpfile[] = _PATH_TMP;
28 char	*tmpname = &tmpfile[5];
29 
30 int	debug;		/* debugging flag */
31 int	nflag;		/* NOP flag, just print commands without executing */
32 int	qflag;		/* Quiet. Don't print messages */
33 int	options;	/* global options */
34 int	iamremote;	/* act as remote server for transfering files */
35 
36 FILE	*fin = NULL;	/* input file pointer */
37 int	rem = -1;	/* file descriptor to remote source/sink process */
38 char	host[32];	/* host name */
39 int	nerrs;		/* number of errors while sending/receiving */
40 char	user[10];	/* user's name */
41 char	homedir[128];	/* user's home directory */
42 int	userid;		/* user's user ID */
43 int	groupid;	/* user's group ID */
44 
45 struct	passwd *pw;	/* pointer to static area used by getpwent */
46 struct	group *gr;	/* pointer to static area used by getgrent */
47 
48 main(argc, argv)
49 	int argc;
50 	char *argv[];
51 {
52 	register char *arg;
53 	int cmdargs = 0;
54 	char *dhosts[NHOSTS], **hp = dhosts;
55 
56 	pw = getpwuid(userid = getuid());
57 	if (pw == NULL) {
58 		fprintf(stderr, "%s: Who are you?\n", argv[0]);
59 		exit(1);
60 	}
61 	strcpy(user, pw->pw_name);
62 	strcpy(homedir, pw->pw_dir);
63 	groupid = pw->pw_gid;
64 	gethostname(host, sizeof(host));
65 
66 	while (--argc > 0) {
67 		if ((arg = *++argv)[0] != '-')
68 			break;
69 		if (!strcmp(arg, "-Server"))
70 			iamremote++;
71 		else while (*++arg)
72 			switch (*arg) {
73 			case 'f':
74 				if (--argc <= 0)
75 					usage();
76 				distfile = *++argv;
77 				if (distfile[0] == '-' && distfile[1] == '\0')
78 					fin = stdin;
79 				break;
80 
81 			case 'm':
82 				if (--argc <= 0)
83 					usage();
84 				if (hp >= &dhosts[NHOSTS-2]) {
85 					fprintf(stderr, "rdist: too many destination hosts\n");
86 					exit(1);
87 				}
88 				*hp++ = *++argv;
89 				break;
90 
91 			case 'd':
92 				if (--argc <= 0)
93 					usage();
94 				define(*++argv);
95 				break;
96 
97 			case 'D':
98 				debug++;
99 				break;
100 
101 			case 'c':
102 				cmdargs++;
103 				break;
104 
105 			case 'n':
106 				if (options & VERIFY) {
107 					printf("rdist: -n overrides -v\n");
108 					options &= ~VERIFY;
109 				}
110 				nflag++;
111 				break;
112 
113 			case 'q':
114 				qflag++;
115 				break;
116 
117 			case 'b':
118 				options |= COMPARE;
119 				break;
120 
121 			case 'R':
122 				options |= REMOVE;
123 				break;
124 
125 			case 'v':
126 				if (nflag) {
127 					printf("rdist: -n overrides -v\n");
128 					break;
129 				}
130 				options |= VERIFY;
131 				break;
132 
133 			case 'w':
134 				options |= WHOLE;
135 				break;
136 
137 			case 'y':
138 				options |= YOUNGER;
139 				break;
140 
141 			case 'h':
142 				options |= FOLLOW;
143 				break;
144 
145 			case 'i':
146 				options |= IGNLNKS;
147 				break;
148 
149 			default:
150 				usage();
151 			}
152 	}
153 	*hp = NULL;
154 
155 	setreuid(0, userid);
156 	mktemp(tmpfile);
157 
158 	if (iamremote) {
159 		server();
160 		exit(nerrs != 0);
161 	}
162 
163 	if (cmdargs)
164 		docmdargs(argc, argv);
165 	else {
166 		if (fin == NULL) {
167 			if(distfile == NULL) {
168 				if((fin = fopen("distfile","r")) == NULL)
169 					fin = fopen("Distfile", "r");
170 			} else
171 				fin = fopen(distfile, "r");
172 			if(fin == NULL) {
173 				perror(distfile ? distfile : "distfile");
174 				exit(1);
175 			}
176 		}
177 		yyparse();
178 		if (nerrs == 0)
179 			docmds(dhosts, argc, argv);
180 	}
181 
182 	exit(nerrs != 0);
183 }
184 
185 usage()
186 {
187 	printf("Usage: rdist [-nqbhirvwyD] [-f distfile] [-d var=value] [-m host] [file ...]\n");
188 	printf("or: rdist [-nqbhirvwyD] -c source [...] machine[:dest]\n");
189 	exit(1);
190 }
191 
192 /*
193  * rcp like interface for distributing files.
194  */
195 docmdargs(nargs, args)
196 	int nargs;
197 	char *args[];
198 {
199 	register struct namelist *nl, *prev;
200 	register char *cp;
201 	struct namelist *files, *hosts;
202 	struct subcmd *cmds;
203 	char *dest;
204 	static struct namelist tnl = { NULL, NULL };
205 	int i;
206 
207 	if (nargs < 2)
208 		usage();
209 
210 	prev = NULL;
211 	for (i = 0; i < nargs - 1; i++) {
212 		nl = makenl(args[i]);
213 		if (prev == NULL)
214 			files = prev = nl;
215 		else {
216 			prev->n_next = nl;
217 			prev = nl;
218 		}
219 	}
220 
221 	cp = args[i];
222 	if ((dest = index(cp, ':')) != NULL)
223 		*dest++ = '\0';
224 	tnl.n_name = cp;
225 	hosts = expand(&tnl, E_ALL);
226 	if (nerrs)
227 		exit(1);
228 
229 	if (dest == NULL || *dest == '\0')
230 		cmds = NULL;
231 	else {
232 		cmds = makesubcmd(INSTALL);
233 		cmds->sc_options = options;
234 		cmds->sc_name = dest;
235 	}
236 
237 	if (debug) {
238 		printf("docmdargs()\nfiles = ");
239 		prnames(files);
240 		printf("hosts = ");
241 		prnames(hosts);
242 	}
243 	insert(NULL, files, hosts, cmds);
244 	docmds(NULL, 0, NULL);
245 }
246 
247 /*
248  * Print a list of NAME blocks (mostly for debugging).
249  */
250 prnames(nl)
251 	register struct namelist *nl;
252 {
253 	printf("( ");
254 	while (nl != NULL) {
255 		printf("%s ", nl->n_name);
256 		nl = nl->n_next;
257 	}
258 	printf(")\n");
259 }
260 
261 /*VARARGS*/
262 warn(fmt, a1, a2,a3)
263 	char *fmt;
264 {
265 	extern int yylineno;
266 
267 	fprintf(stderr, "rdist: line %d: Warning: ", yylineno);
268 	fprintf(stderr, fmt, a1, a2, a3);
269 	fputc('\n', stderr);
270 }
271