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