xref: /netbsd-src/usr.bin/rdist/main.c (revision 0b9f50897e9a9c6709320fafb4c3787fddcc0a45)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 char copyright[] =
36 "@(#) Copyright (c) 1983 Regents of the University of California.\n\
37  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 /*static char sccsid[] = "from: @(#)main.c	5.6 (Berkeley) 8/27/90";*/
42 static char rcsid[] = "$Id: main.c,v 1.2 1993/08/01 18:09:42 mycroft Exp $";
43 #endif /* not lint */
44 
45 #include "defs.h"
46 
47 #define NHOSTS 100
48 
49 /*
50  * Remote distribution program.
51  */
52 
53 char	*distfile = NULL;
54 #define _RDIST_TMP	"/rdistXXXXXX"
55 char	tempfile[sizeof _PATH_TMP + sizeof _RDIST_TMP + 1];
56 char	*tempname;
57 
58 int	debug;		/* debugging flag */
59 int	nflag;		/* NOP flag, just print commands without executing */
60 int	qflag;		/* Quiet. Don't print messages */
61 int	options;	/* global options */
62 int	iamremote;	/* act as remote server for transfering files */
63 
64 FILE	*fin = NULL;	/* input file pointer */
65 int	rem = -1;	/* file descriptor to remote source/sink process */
66 char	host[32];	/* host name */
67 int	nerrs;		/* number of errors while sending/receiving */
68 char	user[10];	/* user's name */
69 char	homedir[128];	/* user's home directory */
70 int	userid;		/* user's user ID */
71 int	groupid;	/* user's group ID */
72 
73 struct	passwd *pw;	/* pointer to static area used by getpwent */
74 struct	group *gr;	/* pointer to static area used by getgrent */
75 
76 main(argc, argv)
77 	int argc;
78 	char *argv[];
79 {
80 	register char *arg;
81 	int cmdargs = 0;
82 	char *dhosts[NHOSTS], **hp = dhosts;
83 
84 	pw = getpwuid(userid = getuid());
85 	if (pw == NULL) {
86 		fprintf(stderr, "%s: Who are you?\n", argv[0]);
87 		exit(1);
88 	}
89 	strcpy(user, pw->pw_name);
90 	strcpy(homedir, pw->pw_dir);
91 	groupid = pw->pw_gid;
92 	gethostname(host, sizeof(host));
93 	strcpy(tempfile, _PATH_TMP);
94 	strcat(tempfile, _RDIST_TMP);
95 	if ((tempname = rindex(tempfile, '/')) != 0)
96 		tempname++;
97 	else
98 		tempname = tempfile;
99 
100 	while (--argc > 0) {
101 		if ((arg = *++argv)[0] != '-')
102 			break;
103 		if (!strcmp(arg, "-Server"))
104 			iamremote++;
105 		else while (*++arg)
106 			switch (*arg) {
107 			case 'f':
108 				if (--argc <= 0)
109 					usage();
110 				distfile = *++argv;
111 				if (distfile[0] == '-' && distfile[1] == '\0')
112 					fin = stdin;
113 				break;
114 
115 			case 'm':
116 				if (--argc <= 0)
117 					usage();
118 				if (hp >= &dhosts[NHOSTS-2]) {
119 					fprintf(stderr, "rdist: too many destination hosts\n");
120 					exit(1);
121 				}
122 				*hp++ = *++argv;
123 				break;
124 
125 			case 'd':
126 				if (--argc <= 0)
127 					usage();
128 				define(*++argv);
129 				break;
130 
131 			case 'D':
132 				debug++;
133 				break;
134 
135 			case 'c':
136 				cmdargs++;
137 				break;
138 
139 			case 'n':
140 				if (options & VERIFY) {
141 					printf("rdist: -n overrides -v\n");
142 					options &= ~VERIFY;
143 				}
144 				nflag++;
145 				break;
146 
147 			case 'q':
148 				qflag++;
149 				break;
150 
151 			case 'b':
152 				options |= COMPARE;
153 				break;
154 
155 			case 'R':
156 				options |= REMOVE;
157 				break;
158 
159 			case 'v':
160 				if (nflag) {
161 					printf("rdist: -n overrides -v\n");
162 					break;
163 				}
164 				options |= VERIFY;
165 				break;
166 
167 			case 'w':
168 				options |= WHOLE;
169 				break;
170 
171 			case 'y':
172 				options |= YOUNGER;
173 				break;
174 
175 			case 'h':
176 				options |= FOLLOW;
177 				break;
178 
179 			case 'i':
180 				options |= IGNLNKS;
181 				break;
182 
183 			default:
184 				usage();
185 			}
186 	}
187 	*hp = NULL;
188 
189 	setreuid(0, userid);
190 	mktemp(tempfile);
191 
192 	if (iamremote) {
193 		server();
194 		exit(nerrs != 0);
195 	}
196 
197 	if (cmdargs)
198 		docmdargs(argc, argv);
199 	else {
200 		if (fin == NULL) {
201 			if(distfile == NULL) {
202 				if((fin = fopen("distfile","r")) == NULL)
203 					fin = fopen("Distfile", "r");
204 			} else
205 				fin = fopen(distfile, "r");
206 			if(fin == NULL) {
207 				perror(distfile ? distfile : "distfile");
208 				exit(1);
209 			}
210 		}
211 		yyparse();
212 		if (nerrs == 0)
213 			docmds(dhosts, argc, argv);
214 	}
215 
216 	exit(nerrs != 0);
217 }
218 
219 usage()
220 {
221 	printf("Usage: rdist [-nqbhirvwyD] [-f distfile] [-d var=value] [-m host] [file ...]\n");
222 	printf("or: rdist [-nqbhirvwyD] -c source [...] machine[:dest]\n");
223 	exit(1);
224 }
225 
226 /*
227  * rcp like interface for distributing files.
228  */
229 docmdargs(nargs, args)
230 	int nargs;
231 	char *args[];
232 {
233 	register struct namelist *nl, *prev;
234 	register char *cp;
235 	struct namelist *files, *hosts;
236 	struct subcmd *cmds;
237 	char *dest;
238 	static struct namelist tnl = { NULL, NULL };
239 	int i;
240 
241 	if (nargs < 2)
242 		usage();
243 
244 	prev = NULL;
245 	for (i = 0; i < nargs - 1; i++) {
246 		nl = makenl(args[i]);
247 		if (prev == NULL)
248 			files = prev = nl;
249 		else {
250 			prev->n_next = nl;
251 			prev = nl;
252 		}
253 	}
254 
255 	cp = args[i];
256 	if ((dest = index(cp, ':')) != NULL)
257 		*dest++ = '\0';
258 	tnl.n_name = cp;
259 	hosts = expand(&tnl, E_ALL);
260 	if (nerrs)
261 		exit(1);
262 
263 	if (dest == NULL || *dest == '\0')
264 		cmds = NULL;
265 	else {
266 		cmds = makesubcmd(INSTALL);
267 		cmds->sc_options = options;
268 		cmds->sc_name = dest;
269 	}
270 
271 	if (debug) {
272 		printf("docmdargs()\nfiles = ");
273 		prnames(files);
274 		printf("hosts = ");
275 		prnames(hosts);
276 	}
277 	insert(NULL, files, hosts, cmds);
278 	docmds(NULL, 0, NULL);
279 }
280 
281 /*
282  * Print a list of NAME blocks (mostly for debugging).
283  */
284 prnames(nl)
285 	register struct namelist *nl;
286 {
287 	printf("( ");
288 	while (nl != NULL) {
289 		printf("%s ", nl->n_name);
290 		nl = nl->n_next;
291 	}
292 	printf(")\n");
293 }
294 
295 /*VARARGS*/
296 warn(fmt, a1, a2,a3)
297 	char *fmt;
298 {
299 	extern int yylineno;
300 
301 	fprintf(stderr, "rdist: line %d: Warning: ", yylineno);
302 	fprintf(stderr, fmt, a1, a2, a3);
303 	fputc('\n', stderr);
304 }
305