Annotation of src/usr.bin/rdist/rdist.c, Revision 1.15
1.15 ! millert 1: /* $OpenBSD: rdist.c,v 1.14 2003/04/05 20:31:58 deraadt Exp $ */
1.3 deraadt 2:
1.1 dm 3: /*
4: * Copyright (c) 1983 Regents of the University of California.
5: * All rights reserved.
6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
15: * 3. All advertising materials mentioning features or use of this software
16: * must display the following acknowledgement:
17: * This product includes software developed by the University of
18: * California, Berkeley and its contributors.
19: * 4. Neither the name of the University nor the names of its contributors
20: * may be used to endorse or promote products derived from this software
21: * without specific prior written permission.
22: *
23: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33: * SUCH DAMAGE.
34: */
35:
36: #ifndef lint
1.5 millert 37: #if 0
1.1 dm 38: static char RCSid[] =
1.5 millert 39: "$From: rdist.c,v 6.65 1995/12/12 00:20:39 mcooper Exp $";
40: #else
41: static char RCSid[] =
1.15 ! millert 42: "$OpenBSD: rdist.c,v 1.14 2003/04/05 20:31:58 deraadt Exp $";
1.5 millert 43: #endif
1.1 dm 44:
45: static char sccsid[] = "@(#)main.c 5.1 (Berkeley) 6/6/85";
46:
47: static char copyright[] =
48: "@(#) Copyright (c) 1983 Regents of the University of California.\n\
49: All rights reserved.\n";
50: #endif /* not lint */
51:
52:
53: #include "defs.h"
54: #include "y.tab.h"
55: #include <netdb.h>
56: #include <sys/ioctl.h>
57:
58: /*
59: * Remote distribution program.
60: */
1.6 millert 61:
62: void docmdargs(int, char **);
63: void usage(void);
1.1 dm 64:
65: char *distfile = NULL; /* Name of distfile to use */
66: int maxchildren = MAXCHILDREN; /* Max no of concurrent PIDs */
67: int nflag = 0; /* Say without doing */
68: long min_freespace = 0; /* Min filesys free space */
69: long min_freefiles = 0; /* Min filesys free # files */
70: FILE *fin = NULL; /* Input file pointer */
71: struct group *gr = NULL; /* Static area for getgrent */
72: char localmsglist[] = "stdout=all:notify=all:syslog=nerror,ferror";
73: char *remotemsglist = NULL;
74: char optchars[] = "A:a:bcd:DFf:hil:L:M:m:NnOo:p:P:qRrst:Vvwxy";
75: FILE *opendist();
76: char *path_rdistd = _PATH_RDISTD;
1.4 millert 77: char *path_remsh = NULL;
1.1 dm 78:
79: /*
80: * Add a hostname to the host list
81: */
82: static void addhostlist(name, hostlist)
83: char *name;
84: struct namelist **hostlist;
85: {
1.9 mpech 86: struct namelist *ptr, *new;
1.1 dm 87:
88: if (!name || !hostlist)
89: return;
90:
91: new = (struct namelist *) xmalloc(sizeof(struct namelist));
1.7 millert 92: new->n_name = xstrdup(name);
1.15 ! millert 93: new->n_regex = NULL;
1.1 dm 94: new->n_next = NULL;
95:
96: if (*hostlist) {
97: for (ptr = *hostlist; ptr && ptr->n_next; ptr = ptr->n_next)
98: ;
99: ptr->n_next = new;
100: } else
101: *hostlist = new;
102: }
103:
1.5 millert 104: int
1.1 dm 105: main(argc, argv, envp)
106: int argc;
107: char *argv[];
108: char **envp;
109: {
110: struct namelist *hostlist = NULL;
1.9 mpech 111: int x;
112: char *cp;
1.1 dm 113: int cmdargs = 0;
114: int c;
115:
116: /*
117: * We initialize progname here instead of init() because
118: * things in msgparseopts() need progname set.
119: */
120: setprogname(argv);
121:
1.5 millert 122: if ((cp = msgparseopts(localmsglist, TRUE))) {
1.1 dm 123: error("Bad builtin log option (%s): %s.",
124: localmsglist, cp);
125: usage();
126: }
127:
128: if (init(argc, argv, envp) < 0)
129: exit(1);
130:
131: /*
132: * Be backwards compatible.
133: */
134: for (x = 1; x <= argc && argv[x]; x++) {
135: if (strcmp(argv[x], "-Server") != 0)
136: continue;
137: #if defined(_PATH_OLDRDIST)
138: message(MT_SYSLOG,
139: "Old rdist (-Server) requested; running %s",
140: _PATH_OLDRDIST);
141: (void) execl(_PATH_OLDRDIST, xbasename(_PATH_OLDRDIST),
1.8 deraadt 142: "-Server", (char *)NULL);
1.1 dm 143: fatalerr("Exec old rdist failed: %s: %s.",
144: _PATH_OLDRDIST, SYSERR);
145: #else /* !_PATH_OLDRDIST */
146: fatalerr("Old rdist not available.");
147: #endif /* _PATH_OLDRDIST */
148: exit(1);
149: }
150:
151: #if defined(DIRECT_RCMD)
152: if (becomeuser() != 0)
153: exit(1);
154: #else /* !DIRECT_RCMD */
155: /*
156: * Perform check to make sure we are not incorrectly installed
157: * setuid to root or anybody else.
158: */
159: if (getuid() != geteuid())
160: fatalerr("This version of rdist should not be installed setuid.");
161: #endif /* DIRECT_RCMD */
162:
163: while ((c = getopt(argc, argv, optchars)) != -1)
164: switch (c) {
165: case 'l':
1.5 millert 166: if ((cp = msgparseopts(optarg, TRUE))) {
1.1 dm 167: error("Bad log option \"%s\": %s.", optarg,cp);
168: usage();
169: }
170: break;
171:
172: case 'L':
1.7 millert 173: remotemsglist = xstrdup(optarg);
1.1 dm 174: break;
175:
176: case 'A':
177: case 'a':
178: case 'M':
179: case 't':
180: if (!isdigit(*optarg)) {
181: error("\"%s\" is not a number.", optarg);
182: usage();
183: }
184: if (c == 'a')
185: min_freespace = atoi(optarg);
186: else if (c == 'A')
187: min_freefiles = atoi(optarg);
188: else if (c == 'M')
189: maxchildren = atoi(optarg);
190: else if (c == 't')
191: rtimeout = atoi(optarg);
192: break;
193:
194: case 'F':
195: do_fork = FALSE;
196: break;
197:
198: case 'f':
1.7 millert 199: distfile = xstrdup(optarg);
1.1 dm 200: if (distfile[0] == '-' && distfile[1] == CNULL)
201: fin = stdin;
202: break;
203:
204: case 'm':
205: addhostlist(optarg, &hostlist);
206: break;
207:
208: case 'd':
209: define(optarg);
210: break;
211:
212: case 'D':
213: debug = DM_ALL;
1.5 millert 214: if ((cp = msgparseopts("stdout=all,debug", TRUE))) {
1.1 dm 215: error("Enable debug messages failed: %s.", cp);
216: usage();
217: }
218: break;
219:
220: case 'c':
221: cmdargs++;
222: break;
223:
224: case 'n':
225: nflag++;
226: break;
227:
228: case 'V':
229: printf("%s\n", getversion());
230: exit(0);
231:
232: case 'o':
233: if (parsedistopts(optarg, &options, TRUE)) {
234: error("Bad dist option string \"%s\".",
235: optarg);
236: usage();
237: }
238: break;
239:
240: case 'p':
241: if (!optarg) {
242: error("No path specified to \"-p\".");
243: usage();
244: }
1.7 millert 245: path_rdistd = xstrdup(optarg);
1.1 dm 246: break;
247:
248: case 'P':
249: if (!optarg) {
250: error("No path specified to \"-P\".");
251: usage();
252: }
1.5 millert 253: if ((cp = searchpath(optarg)))
1.7 millert 254: path_remsh = xstrdup(cp);
1.1 dm 255: else {
256: error("No component of path \"%s\" exists.",
257: optarg);
258: usage();
259: }
260: break;
261:
262: /*
263: * These options are obsoleted by -o. They are
264: * provided only for backwards compatibility
265: */
266: case 'v': FLAG_ON(options, DO_VERIFY); break;
267: case 'N': FLAG_ON(options, DO_CHKNFS); break;
268: case 'O': FLAG_ON(options, DO_CHKREADONLY); break;
269: case 'q': FLAG_ON(options, DO_QUIET); break;
270: case 'b': FLAG_ON(options, DO_COMPARE); break;
271: case 'r': FLAG_ON(options, DO_NODESCEND); break;
272: case 'R': FLAG_ON(options, DO_REMOVE); break;
273: case 's': FLAG_ON(options, DO_SAVETARGETS); break;
274: case 'w': FLAG_ON(options, DO_WHOLE); break;
275: case 'y': FLAG_ON(options, DO_YOUNGER); break;
276: case 'h': FLAG_ON(options, DO_FOLLOW); break;
277: case 'i': FLAG_ON(options, DO_IGNLNKS); break;
278: case 'x': FLAG_ON(options, DO_NOEXEC); break;
279:
280: case '?':
281: default:
282: usage();
283: }
284:
285: if (debug) {
286: printf("%s\n", getversion());
287: msgprconfig();
288: }
289:
290: if (nflag && IS_ON(options, DO_VERIFY))
291: fatalerr(
292: "The -n flag and \"verify\" mode may not both be used.");
1.4 millert 293:
1.13 millert 294: if (path_remsh == NULL) {
295: if ((cp = getenv("RSH")) != NULL && *cp != '\0')
296: path_remsh = cp;
297: else
298: path_remsh = _PATH_SSH;
299: }
1.1 dm 300:
301: /*
302: * Don't fork children for nflag
303: */
304: if (nflag)
305: do_fork = 0;
306:
307: if (cmdargs)
308: docmdargs(realargc - optind, &realargv[optind]);
309: else {
310: if (fin == NULL)
311: fin = opendist(distfile);
312: (void) yyparse();
313: /*
314: * Need to keep stdin open for child processing later
315: */
316: if (fin != stdin)
317: (void) fclose(fin);
318: if (nerrs == 0)
319: docmds(hostlist, realargc-optind, &realargv[optind]);
320: }
321:
322: exit(nerrs != 0);
323: }
324:
325: /*
326: * Open a distfile
327: */
328: FILE *opendist(distfile)
329: char *distfile;
330: {
331: char *file = NULL;
332: FILE *fp;
333:
334: if (distfile == NULL) {
335: if (access("distfile", R_OK) == 0)
336: file = "distfile";
337: else if (access("Distfile", R_OK) == 0)
338: file = "Distfile";
339: } else {
340: /*
341: * Try to test to see if file is readable before running m4.
342: */
343: if (access(distfile, R_OK) != 0)
344: fatalerr("%s: Cannot access file: %s.",
345: distfile, SYSERR);
346: file = distfile;
347: }
348:
349: if (file == NULL)
350: fatalerr("No distfile found.");
351:
352: fp = fopen(file, "r");
353:
354: if (fp == NULL)
355: fatalerr("%s: open failed: %s.", file, SYSERR);
356:
357: return(fp);
358: }
359:
360: /*
361: * Print usage message and exit.
362: */
1.5 millert 363: void
1.1 dm 364: usage()
365: {
366: char *sopts = "cDFnv";
367:
368: (void) fprintf(stderr,
369: "Usage: %s [-%s] [-A <num>] [-a <num>] [-d var=value]\n",
370: progname, sopts);
371: (void) fprintf(stderr,
372: "\t[-f distfile] [-l <msgopt>] [-L <msgopt>] [-M <maxproc>]\n");
373: (void) fprintf(stderr,
374: "\t[-m host] [-o <distopts>] [-p <rdistd-cmd>] [-P <rsh-path>]\n");
375: (void) fprintf(stderr,
376: "\t[-t <timeout>] [target ...]\n");
377:
378: (void) fprintf(stderr,
379: "OR: %s [-%s] -c source [...] machine[:dest]\n",
380: progname, sopts);
381:
382: (void) fprintf(stderr, "OR: %s -V\n", progname);
383:
384: (void) fprintf(stderr, "\nThe values for <distopts> are:\n\t%s\n",
385: getdistoptlist());
386:
387: msgprusage();
388:
389: exit(1);
390: }
391:
392: /*
393: * rcp like interface for distributing files.
394: */
1.5 millert 395: void
1.1 dm 396: docmdargs(nargs, args)
397: int nargs;
398: char *args[];
399: {
1.9 mpech 400: struct namelist *nl, *prev;
401: char *cp;
1.1 dm 402: struct namelist *files, *hosts;
403: struct subcmd *cmds;
404: char *dest;
1.15 ! millert 405: static struct namelist tnl;
1.1 dm 406: int i;
407:
408: if (nargs < 2)
409: usage();
410:
411: prev = NULL;
412: files = NULL;
413: for (i = 0; i < nargs - 1; i++) {
414: nl = makenl(args[i]);
415: if (prev == NULL)
416: files = prev = nl;
417: else {
418: prev->n_next = nl;
419: prev = nl;
420: }
421: }
422:
423: cp = args[i];
424: if ((dest = strchr(cp, ':')) != NULL)
425: *dest++ = '\0';
426: tnl.n_name = cp;
1.15 ! millert 427: tnl.n_regex = NULL;
! 428: tnl.n_next = NULL;
1.1 dm 429: hosts = expand(&tnl, E_ALL);
430: if (nerrs)
431: exit(1);
432:
433: if (dest == NULL || *dest == '\0')
434: cmds = NULL;
435: else {
436: cmds = makesubcmd(INSTALL);
437: cmds->sc_options = options;
438: cmds->sc_name = dest;
439: }
440:
441: debugmsg(DM_MISC, "docmdargs()\nfiles = %s", getnlstr(files));
442: debugmsg(DM_MISC, "host = %s", getnlstr(hosts));
443:
1.5 millert 444: insert(NULL, files, hosts, cmds);
445: docmds(0, NULL, 0, (char **)NULL);
1.1 dm 446: }
447:
448: /*
449: * Get a list of NAME blocks (mostly for debugging).
450: */
451: extern char *getnlstr(nl)
1.9 mpech 452: struct namelist *nl;
1.1 dm 453: {
454: static char buf[16384];
1.9 mpech 455: int count = 0, len = 0;
1.1 dm 456:
1.14 deraadt 457: (void) snprintf(buf, sizeof buf, "(");
1.1 dm 458:
459: while (nl != NULL) {
460: if (nl->n_name == NULL)
461: continue;
462: len += strlen(nl->n_name) + 2;
463: if (len >= sizeof(buf)) {
1.14 deraadt 464: (void) strlcpy(buf,
465: "getnlstr() Buffer not large enough",
466: sizeof buf);
1.1 dm 467: return(buf);
468: }
469: ++count;
1.14 deraadt 470: (void) strlcat(buf, " ", sizeof buf);
471: (void) strlcat(buf, nl->n_name, sizeof buf);
1.1 dm 472: nl = nl->n_next;
473: }
474:
1.14 deraadt 475: (void) strlcat(buf, " )", sizeof buf);
1.1 dm 476:
477: return(buf);
478: }