Annotation of src/usr.bin/oldrdist/docmd.c, Revision 1.16
1.16 ! millert 1: /* $OpenBSD: docmd.c,v 1.15 2002/02/16 21:27:50 millert Exp $ */
1.3 deraadt 2:
1.1 dm 3: /*
4: * Copyright (c) 1983, 1993
5: * The Regents of the University of California. 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
37: /* from: static char sccsid[] = "@(#)docmd.c 8.1 (Berkeley) 6/9/93"; */
1.16 ! millert 38: static char *rcsid = "$OpenBSD: docmd.c,v 1.15 2002/02/16 21:27:50 millert Exp $";
1.1 dm 39: #endif /* not lint */
40:
41: #include "defs.h"
42: #include <setjmp.h>
43: #include <netdb.h>
1.7 millert 44: #include <regex.h>
1.1 dm 45:
46: FILE *lfp; /* log file for recording files updated */
47: struct subcmd *subcmds; /* list of sub-commands for current cmd */
48: jmp_buf env;
49:
1.15 millert 50: static int makeconn(char *);
51: static int okname(char *);
52: static void closeconn(void);
53: static void cmptime(char *);
1.16 ! millert 54: static void doarrow(char **, struct namelist *, char *, struct subcmd *);
! 55: static void dodcolon(char **, struct namelist *, char *, struct subcmd *);
1.15 millert 56: static void notify(char *, char *, struct namelist *, time_t);
57: static void rcmptime(struct stat *);
1.1 dm 58:
59: /*
60: * Do the commands in cmds (initialized by yyparse).
61: */
62: void
63: docmds(dhosts, argc, argv)
64: char **dhosts;
65: int argc;
66: char **argv;
67: {
1.14 mpech 68: struct cmd *c;
69: struct namelist *f;
70: char **cpp;
1.1 dm 71: extern struct cmd *cmds;
72:
73: signal(SIGHUP, cleanup);
74: signal(SIGINT, cleanup);
75: signal(SIGQUIT, cleanup);
76: signal(SIGTERM, cleanup);
77:
78: for (c = cmds; c != NULL; c = c->c_next) {
79: if (dhosts != NULL && *dhosts != NULL) {
80: for (cpp = dhosts; *cpp; cpp++)
81: if (strcmp(c->c_name, *cpp) == 0)
82: goto fndhost;
83: continue;
84: }
85: fndhost:
86: if (argc) {
87: for (cpp = argv; *cpp; cpp++) {
88: if (c->c_label != NULL &&
89: strcmp(c->c_label, *cpp) == 0) {
90: cpp = NULL;
91: goto found;
92: }
93: for (f = c->c_files; f != NULL; f = f->n_next)
94: if (strcmp(f->n_name, *cpp) == 0)
95: goto found;
96: }
97: continue;
98: } else
99: cpp = NULL;
100: found:
101: switch (c->c_type) {
102: case ARROW:
103: doarrow(cpp, c->c_files, c->c_name, c->c_cmds);
104: break;
105: case DCOLON:
106: dodcolon(cpp, c->c_files, c->c_name, c->c_cmds);
107: break;
108: default:
109: fatal("illegal command type %d\n", c->c_type);
110: }
111: }
112: closeconn();
113: }
114:
115: /*
116: * Process commands for sending files to other machines.
117: */
118: static void
119: doarrow(filev, files, rhost, cmds)
120: char **filev;
121: struct namelist *files;
122: char *rhost;
123: struct subcmd *cmds;
124: {
1.14 mpech 125: struct namelist *f;
126: struct subcmd *sc;
127: char **cpp;
1.1 dm 128: int n, ddir, opts = options;
129:
130: if (debug)
1.13 pvalchev 131: printf("doarrow(%lx, %s, %lx)\n", (long)files, rhost, (long)cmds);
1.1 dm 132:
133: if (files == NULL) {
134: error("no files to be updated\n");
135: return;
136: }
137:
138: subcmds = cmds;
139: ddir = files->n_next != NULL; /* destination is a directory */
140: if (nflag)
141: printf("updating host %s\n", rhost);
142: else {
1.2 deraadt 143: int fd;
144:
1.1 dm 145: if (setjmp(env))
146: goto done;
147: signal(SIGPIPE, lostconn);
148: if (!makeconn(rhost))
149: return;
1.7 millert 150: if ((fd = open(tempfile, O_CREAT|O_EXCL|O_WRONLY, 0600)) < 0 ||
1.2 deraadt 151: (lfp = fdopen(fd, "w")) == NULL) {
1.7 millert 152: if (fd >= 0)
153: (void) close(fd);
1.1 dm 154: fatal("cannot open %s\n", tempfile);
155: exit(1);
156: }
157: }
158: for (f = files; f != NULL; f = f->n_next) {
159: if (filev) {
160: for (cpp = filev; *cpp; cpp++)
161: if (strcmp(f->n_name, *cpp) == 0)
162: goto found;
1.4 millert 163: if (!nflag && lfp) {
1.1 dm 164: (void) fclose(lfp);
1.4 millert 165: lfp = NULL;
166: }
1.1 dm 167: continue;
168: }
169: found:
170: n = 0;
171: for (sc = cmds; sc != NULL; sc = sc->sc_next) {
172: if (sc->sc_type != INSTALL)
173: continue;
174: n++;
175: install(f->n_name, sc->sc_name,
176: sc->sc_name == NULL ? 0 : ddir, sc->sc_options);
177: opts = sc->sc_options;
178: }
179: if (n == 0)
180: install(f->n_name, NULL, 0, options);
181: }
182: done:
183: if (!nflag) {
184: (void) signal(SIGPIPE, cleanup);
1.4 millert 185: if (lfp)
186: (void) fclose(lfp);
1.1 dm 187: lfp = NULL;
188: }
189: for (sc = cmds; sc != NULL; sc = sc->sc_next)
190: if (sc->sc_type == NOTIFY)
191: notify(tempfile, rhost, sc->sc_args, 0);
192: if (!nflag) {
1.11 deraadt 193: struct linkbuf *nextihead;
194:
1.1 dm 195: (void) unlink(tempfile);
1.11 deraadt 196: for (; ihead != NULL; ihead = nextihead) {
197: nextihead = ihead->nextp;
1.1 dm 198: if ((opts & IGNLNKS) || ihead->count == 0)
199: continue;
200: log(lfp, "%s: Warning: missing links\n",
201: ihead->pathname);
1.11 deraadt 202: free(ihead);
1.1 dm 203: }
204: }
205: }
206:
207: /*
208: * Create a connection to the rdist server on the machine rhost.
209: */
210: static int
211: makeconn(rhost)
212: char *rhost;
213: {
1.14 mpech 214: char *ruser, *cp;
1.1 dm 215: static char *cur_host = NULL;
1.4 millert 216: #if defined(DIRECT_RCMD)
1.1 dm 217: static int port = -1;
1.4 millert 218: #endif /* DIRECT_RCMD */
1.1 dm 219: char tuser[20];
220: int n;
221: extern char user[];
1.13 pvalchev 222: #if defined(DIRECT_RCMD)
1.1 dm 223: extern int userid;
1.13 pvalchev 224: #endif
1.1 dm 225:
226: if (debug)
227: printf("makeconn(%s)\n", rhost);
228:
229: if (cur_host != NULL && rem >= 0) {
230: if (strcmp(cur_host, rhost) == 0)
231: return(1);
232: closeconn();
233: }
234: cur_host = rhost;
1.4 millert 235: cp = strchr(rhost, '@');
1.1 dm 236: if (cp != NULL) {
237: char c = *cp;
238:
239: *cp = '\0';
240: strncpy(tuser, rhost, sizeof(tuser)-1);
241: *cp = c;
242: rhost = cp + 1;
243: ruser = tuser;
244: if (*ruser == '\0')
245: ruser = user;
246: else if (!okname(ruser))
247: return(0);
248: } else
249: ruser = user;
250: if (!qflag)
251: printf("updating host %s\n", rhost);
1.7 millert 252: (void) snprintf(buf, sizeof(buf), "%s -Server%s", _PATH_RDIST,
1.5 millert 253: qflag ? " -q" : "");
1.4 millert 254: #if defined(DIRECT_RCMD)
1.1 dm 255: if (port < 0) {
256: struct servent *sp;
257:
258: if ((sp = getservbyname("shell", "tcp")) == NULL)
259: fatal("shell/tcp: unknown service");
260: port = sp->s_port;
261: }
1.4 millert 262: #endif /* !DIRECT_RCMD */
1.1 dm 263:
264: if (debug) {
1.4 millert 265: #if defined(DIRECT_RCMD)
1.1 dm 266: printf("port = %d, luser = %s, ruser = %s\n", ntohs(port), user, ruser);
1.4 millert 267: #else /* !DIRECT_RCMD */
268: printf("luser = %s, ruser = %s\n", user, ruser);
269: #endif /* !DIRECT_RCMD */
1.1 dm 270: printf("buf = %s\n", buf);
271: }
272:
273: fflush(stdout);
1.4 millert 274: #if defined(DIRECT_RCMD)
1.1 dm 275: seteuid(0);
276: rem = rcmd(&rhost, port, user, ruser, buf, 0);
277: seteuid(userid);
1.4 millert 278: #else /* !DIRECT_RCMD */
1.7 millert 279: rem = rcmdsh(&rhost, -1, user, ruser, buf, NULL);
1.4 millert 280: #endif /* !DIRECT_RCMD */
1.1 dm 281: if (rem < 0)
282: return(0);
283: cp = buf;
284: if (read(rem, cp, 1) != 1)
285: lostconn(0);
286: if (*cp == 'V') {
287: do {
288: if (read(rem, cp, 1) != 1)
289: lostconn(0);
290: } while (*cp++ != '\n' && cp < &buf[BUFSIZ]);
291: *--cp = '\0';
292: cp = buf;
293: n = 0;
294: while (*cp >= '0' && *cp <= '9')
295: n = (n * 10) + (*cp++ - '0');
296: if (*cp == '\0' && n == VERSION)
297: return(1);
298: error("connection failed: version numbers don't match (local %d, remote %d)\n", VERSION, n);
299: } else {
300: error("connection failed: version numbers don't match\n");
301: error("got unexpected input:");
302: do {
303: error("%c", *cp);
304: } while (*cp != '\n' && read(rem, cp, 1) == 1);
305: }
306: closeconn();
307: return(0);
308: }
309:
310: /*
311: * Signal end of previous connection.
312: */
313: static void
314: closeconn()
315: {
316: if (debug)
317: printf("closeconn()\n");
318:
319: if (rem >= 0) {
1.4 millert 320: void (*osig)();
321: osig = signal(SIGPIPE, SIG_IGN);
1.1 dm 322: (void) write(rem, "\2\n", 2);
1.4 millert 323: (void) signal(SIGPIPE, osig);
1.1 dm 324: (void) close(rem);
325: rem = -1;
326: }
327: }
328:
329: void
330: lostconn(signo)
331: int signo;
332: {
333: if (iamremote)
334: cleanup(0);
335: log(lfp, "rdist: lost connection\n");
1.4 millert 336: if (rem >= 0) {
337: (void) close(rem);
338: rem = -1;
339: }
1.1 dm 340: longjmp(env, 1);
341: }
342:
343: static int
344: okname(name)
1.14 mpech 345: char *name;
1.1 dm 346: {
1.14 mpech 347: char *cp = name;
348: int c;
1.1 dm 349:
350: do {
351: c = *cp;
352: if (c & 0200)
353: goto bad;
354: if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-')
355: goto bad;
356: cp++;
357: } while (*cp);
358: return(1);
359: bad:
360: error("invalid user name %s\n", name);
361: return(0);
362: }
363:
364: time_t lastmod;
365: FILE *tfp;
1.8 deraadt 366: extern char *tp;
1.1 dm 367:
368: /*
369: * Process commands for comparing files to time stamp files.
370: */
371: static void
372: dodcolon(filev, files, stamp, cmds)
373: char **filev;
374: struct namelist *files;
375: char *stamp;
376: struct subcmd *cmds;
377: {
1.14 mpech 378: struct subcmd *sc;
379: struct namelist *f;
380: char **cpp;
1.1 dm 381: struct timeval tv[2];
382: struct stat stb;
383:
384: if (debug)
385: printf("dodcolon()\n");
386:
387: if (files == NULL) {
388: error("no files to be updated\n");
389: return;
390: }
391: if (stat(stamp, &stb) < 0) {
392: error("%s: %s\n", stamp, strerror(errno));
393: return;
394: }
395: if (debug)
1.13 pvalchev 396: printf("%s: %lld\n", stamp, (long long)stb.st_mtime);
1.1 dm 397:
398: subcmds = cmds;
399: lastmod = stb.st_mtime;
400: if (nflag || (options & VERIFY))
401: tfp = NULL;
402: else {
1.2 deraadt 403: int fd;
404:
1.7 millert 405: if ((fd = open(tempfile, O_CREAT|O_EXCL|O_WRONLY, 0600)) < 0 ||
1.2 deraadt 406: (tfp = fdopen(fd, "w")) == NULL) {
1.10 deraadt 407: error("%s: %s\n", tempfile, strerror(errno));
1.7 millert 408: if (fd >= 0)
409: (void) close(fd);
1.1 dm 410: return;
411: }
1.9 deraadt 412: (void) gettimeofday(&tv[0], NULL);
1.1 dm 413: tv[1] = tv[0];
414: (void) utimes(stamp, tv);
415: }
416:
417: for (f = files; f != NULL; f = f->n_next) {
418: if (filev) {
419: for (cpp = filev; *cpp; cpp++)
420: if (strcmp(f->n_name, *cpp) == 0)
421: goto found;
422: continue;
423: }
424: found:
425: tp = NULL;
426: cmptime(f->n_name);
427: }
428:
429: if (tfp != NULL)
430: (void) fclose(tfp);
431: for (sc = cmds; sc != NULL; sc = sc->sc_next)
432: if (sc->sc_type == NOTIFY)
433: notify(tempfile, NULL, sc->sc_args, lastmod);
434: if (!nflag && !(options & VERIFY))
435: (void) unlink(tempfile);
436: }
437:
438: /*
439: * Compare the mtime of file to the list of time stamps.
440: */
441: static void
442: cmptime(name)
443: char *name;
444: {
445: struct stat stb;
446:
447: if (debug)
448: printf("cmptime(%s)\n", name);
449:
450: if (except(name))
451: return;
452:
453: if (nflag) {
454: printf("comparing dates: %s\n", name);
455: return;
456: }
457:
458: /*
459: * first time cmptime() is called?
460: */
461: if (tp == NULL) {
1.8 deraadt 462: if (exptilde(target, name, sizeof (target)) == NULL)
1.1 dm 463: return;
464: tp = name = target;
465: while (*tp)
466: tp++;
467: }
468: if (access(name, 4) < 0 || stat(name, &stb) < 0) {
469: error("%s: %s\n", name, strerror(errno));
470: return;
471: }
472:
473: switch (stb.st_mode & S_IFMT) {
474: case S_IFREG:
475: break;
476:
477: case S_IFDIR:
478: rcmptime(&stb);
479: return;
480:
481: default:
482: error("%s: not a plain file\n", name);
483: return;
484: }
485:
486: if (stb.st_mtime > lastmod)
487: log(tfp, "new: %s\n", name);
488: }
489:
490: static void
491: rcmptime(st)
492: struct stat *st;
493: {
1.14 mpech 494: DIR *d;
495: struct direct *dp;
496: char *cp;
1.1 dm 497: char *otp;
498: int len;
499:
500: if (debug)
1.13 pvalchev 501: printf("rcmptime(%lx)\n", (long)st);
1.1 dm 502:
503: if ((d = opendir(target)) == NULL) {
504: error("%s: %s\n", target, strerror(errno));
505: return;
506: }
507: otp = tp;
508: len = tp - target;
509: while (dp = readdir(d)) {
510: if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
511: continue;
512: if (len + 1 + strlen(dp->d_name) >= BUFSIZ - 1) {
513: error("%s/%s: Name too long\n", target, dp->d_name);
514: continue;
515: }
516: tp = otp;
517: *tp++ = '/';
518: cp = dp->d_name;
519: while (*tp++ = *cp++)
520: ;
521: tp--;
522: cmptime(target);
523: }
524: closedir(d);
525: tp = otp;
526: *tp = '\0';
527: }
528:
529: /*
530: * Notify the list of people the changes that were made.
531: * rhost == NULL if we are mailing a list of changes compared to at time
532: * stamp file.
533: */
534: static void
535: notify(file, rhost, to, lmod)
536: char *file, *rhost;
1.14 mpech 537: struct namelist *to;
1.1 dm 538: time_t lmod;
539: {
1.14 mpech 540: int fd, len;
1.1 dm 541: struct stat stb;
542: FILE *pf;
543:
544: if ((options & VERIFY) || to == NULL)
545: return;
546: if (!qflag) {
547: printf("notify ");
548: if (rhost)
549: printf("@%s ", rhost);
550: prnames(to);
551: }
552: if (nflag)
553: return;
554:
1.12 millert 555: if ((fd = open(file, O_RDONLY)) < 0) {
1.1 dm 556: error("%s: %s\n", file, strerror(errno));
557: return;
558: }
559: if (fstat(fd, &stb) < 0) {
560: error("%s: %s\n", file, strerror(errno));
561: (void) close(fd);
562: return;
563: }
564: if (stb.st_size == 0) {
565: (void) close(fd);
566: return;
567: }
568: /*
569: * Create a pipe to mailling program.
570: */
1.7 millert 571: (void) snprintf(buf, sizeof(buf), "%s -oi -t", _PATH_SENDMAIL);
1.1 dm 572: pf = popen(buf, "w");
573: if (pf == NULL) {
574: error("notify: \"%s\" failed\n", _PATH_SENDMAIL);
575: (void) close(fd);
576: return;
577: }
578: /*
579: * Output the proper header information.
580: */
581: fprintf(pf, "From: rdist (Remote distribution program)\n");
582: fprintf(pf, "To:");
583: if (!any('@', to->n_name) && rhost != NULL)
584: fprintf(pf, " %s@%s", to->n_name, rhost);
585: else
586: fprintf(pf, " %s", to->n_name);
587: to = to->n_next;
588: while (to != NULL) {
589: if (!any('@', to->n_name) && rhost != NULL)
590: fprintf(pf, ", %s@%s", to->n_name, rhost);
591: else
592: fprintf(pf, ", %s", to->n_name);
593: to = to->n_next;
594: }
595: putc('\n', pf);
596: if (rhost != NULL)
597: fprintf(pf, "Subject: files updated by rdist from %s to %s\n",
598: host, rhost);
599: else
600: fprintf(pf, "Subject: files updated after %s\n", ctime(&lmod));
601: putc('\n', pf);
602:
603: while ((len = read(fd, buf, BUFSIZ)) > 0)
604: (void) fwrite(buf, 1, len, pf);
605: (void) close(fd);
606: (void) pclose(pf);
607: }
608:
609: /*
610: * Return true if name is in the list.
611: */
612: int
613: inlist(list, file)
614: struct namelist *list;
615: char *file;
616: {
1.14 mpech 617: struct namelist *nl;
1.1 dm 618:
619: for (nl = list; nl != NULL; nl = nl->n_next)
620: if (!strcmp(file, nl->n_name))
621: return(1);
622: return(0);
623: }
624:
625: /*
626: * Return TRUE if file is in the exception list.
627: */
628: int
629: except(file)
630: char *file;
631: {
1.14 mpech 632: struct subcmd *sc;
633: struct namelist *nl;
1.7 millert 634: regex_t s;
635: int err;
1.1 dm 636:
637: if (debug)
638: printf("except(%s)\n", file);
639:
640: for (sc = subcmds; sc != NULL; sc = sc->sc_next) {
641: if (sc->sc_type != EXCEPT && sc->sc_type != PATTERN)
642: continue;
643: for (nl = sc->sc_args; nl != NULL; nl = nl->n_next) {
644: if (sc->sc_type == EXCEPT) {
645: if (!strcmp(file, nl->n_name))
646: return(1);
647: continue;
648: }
1.7 millert 649: if ((err = regcomp(&s, nl->n_name, 0)) != 0) {
650: (void) regerror(err, &s, buf, sizeof(buf));
651: error("%s: %s\n", nl->n_name, buf);
652: }
653: if (regexec(&s, file, 0, NULL, 0) == 0) {
654: regfree(&s);
1.1 dm 655: return(1);
1.7 millert 656: }
657: regfree(&s);
1.1 dm 658: }
659: }
660: return(0);
661: }
662:
663: char *
664: colon(cp)
1.14 mpech 665: char *cp;
1.1 dm 666: {
667:
668: while (*cp) {
669: if (*cp == ':')
670: return(cp);
671: if (*cp == '/')
672: return(0);
673: cp++;
674: }
675: return(0);
676: }