2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
36 static char sccsid[] = "From: @(#)docmd.c 8.1 (Berkeley) 6/9/93";
38 static const char rcsid[] =
47 FILE *lfp; /* log file for recording files updated */
48 struct subcmd *subcmds; /* list of sub-commands for current cmd */
51 static int makeconn __P((char *));
52 static int okname __P((char *));
53 static void closeconn __P((void));
54 static void cmptime __P((char *));
55 static void doarrow __P((char **,
56 struct namelist *, char *, struct subcmd *));
57 static void dodcolon __P((char **,
58 struct namelist *, char *, struct subcmd *));
59 static void notify __P((char *, char *, struct namelist *, time_t));
60 static void rcmptime __P((struct stat *));
61 static int remotecmd __P((char *, char *, char *, char *));
64 * Do the commands in cmds (initialized by yyparse).
67 docmds(dhosts, argc, argv)
72 register struct cmd *c;
73 register struct namelist *f;
75 extern struct cmd *cmds;
77 signal(SIGHUP, cleanup);
78 signal(SIGINT, cleanup);
79 signal(SIGQUIT, cleanup);
80 signal(SIGTERM, cleanup);
82 for (c = cmds; c != NULL; c = c->c_next) {
83 if (dhosts != NULL && *dhosts != NULL) {
84 for (cpp = dhosts; *cpp; cpp++)
85 if (strcmp(c->c_name, *cpp) == 0)
91 for (cpp = argv; *cpp; cpp++) {
92 if (c->c_label != NULL &&
93 strcmp(c->c_label, *cpp) == 0) {
97 for (f = c->c_files; f != NULL; f = f->n_next)
98 if (strcmp(f->n_name, *cpp) == 0)
107 doarrow(cpp, c->c_files, c->c_name, c->c_cmds);
110 dodcolon(cpp, c->c_files, c->c_name, c->c_cmds);
113 fatal("illegal command type %d\n", c->c_type);
120 * Process commands for sending files to other machines.
123 doarrow(filev, files, rhost, cmds)
125 struct namelist *files;
129 register struct namelist *f;
130 register struct subcmd *sc;
132 int n, ddir, opts = options;
135 printf("doarrow(%p, %s, %p)\n", files, rhost, cmds);
138 error("no files to be updated\n");
143 ddir = files->n_next != NULL; /* destination is a directory */
145 printf("updating host %s\n", rhost);
149 signal(SIGPIPE, lostconn);
150 if (!makeconn(rhost))
152 if ((lfp = fopen(tempfile, "w")) == NULL) {
153 fatal("cannot open %s\n", tempfile);
157 for (f = files; f != NULL; f = f->n_next) {
159 for (cpp = filev; *cpp; cpp++)
160 if (strcmp(f->n_name, *cpp) == 0)
168 for (sc = cmds; sc != NULL; sc = sc->sc_next) {
169 if (sc->sc_type != INSTALL)
172 install(f->n_name, sc->sc_name,
173 sc->sc_name == NULL ? 0 : ddir, sc->sc_options);
174 opts = sc->sc_options;
177 install(f->n_name, NULL, 0, options);
181 (void) signal(SIGPIPE, cleanup);
186 for (sc = cmds; sc != NULL; sc = sc->sc_next)
187 if (sc->sc_type == NOTIFY)
188 notify(tempfile, rhost, sc->sc_args, 0);
190 struct linkbuf *nextihead;
192 (void) unlink(tempfile);
193 for (; ihead != NULL; ihead = nextihead) {
194 nextihead = ihead->nextp;
195 if ((opts & IGNLNKS) || ihead->count == 0)
197 log(lfp, "%s: Warning: missing links\n",
204 static int remotecmd(rhost, luser, ruser, cmd)
210 static int port = -1;
213 printf("local user = %s remote user = %s\n", luser, ruser);
214 printf("Remote command = '%s'\n", cmd);
217 (void) fflush(stdout);
218 (void) fflush(stderr);
219 (void) signal(SIGALRM, cleanup);
220 (void) alarm(RTIMEOUT);
222 if (geteuid() == 0 && strcmp(path_rsh, _PATH_RSH) == 0) {
224 printf("I am root, therefore direct rcmd\n");
226 (void) signal(SIGPIPE, cleanup);
231 if ((sp = getservbyname("shell", "tcp")) == NULL)
232 fatal("shell/tcp: unknown service");
236 desc = rcmd(&rhost, port, luser, ruser, cmd, 0);
239 printf("Remote shell command = '%s'\n", path_rsh);
240 (void) signal(SIGPIPE, SIG_IGN);
241 desc = rshrcmd(&rhost, -1, luser, ruser, cmd, 0);
243 (void) signal(SIGPIPE, cleanup);
253 * Create a connection to the rdist server on the machine rhost.
259 register char *ruser, *cp;
260 static char *cur_host = NULL;
261 static int port = -1;
265 #if defined(DIRECT_RCMD)
270 printf("makeconn(%s)\n", rhost);
272 if (cur_host != NULL && rem >= 0) {
273 if (strcmp(cur_host, rhost) == 0)
278 cp = index(rhost, '@');
283 strncpy(tuser, rhost, sizeof(tuser)-1);
289 else if (!okname(ruser))
294 printf("updating host %s\n", rhost);
295 (void) snprintf(buf, sizeof(buf), "%s -Server%s",
296 _PATH_RDIST, qflag ? " -q" : "");
300 if ((sp = getservbyname("shell", "tcp")) == NULL)
301 fatal("shell/tcp: unknown service");
306 printf("port = %d, luser = %s, ruser = %s\n", ntohs(port), user, ruser);
307 printf("buf = %s\n", buf);
311 #if defined(DIRECT_RCMD)
313 rem = rcmd(&rhost, port, user, ruser, buf, 0);
316 rem = remotecmd(rhost, user, ruser, buf);
321 if (read(rem, cp, 1) != 1)
325 if (read(rem, cp, 1) != 1)
327 } while (*cp++ != '\n' && cp < &buf[BUFSIZ]);
331 while (*cp >= '0' && *cp <= '9')
332 n = (n * 10) + (*cp++ - '0');
333 if (*cp == '\0' && n == VERSION)
335 error("connection failed: version numbers don't match (local %d, remote %d)\n", VERSION, n);
337 error("connection failed: version numbers don't match\n");
338 error("got unexpected input:");
341 } while (*cp != '\n' && read(rem, cp, 1) == 1);
348 * Signal end of previous connection.
354 printf("closeconn()\n");
357 (void) write(rem, "\2\n", 2);
369 log(lfp, "rdist: lost connection\n");
377 register char *cp = name;
384 if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-')
390 error("invalid user name %s\n", name);
396 extern char target[], *tp;
399 * Process commands for comparing files to time stamp files.
402 dodcolon(filev, files, stamp, cmds)
404 struct namelist *files;
408 register struct subcmd *sc;
409 register struct namelist *f;
411 struct timeval tv[2];
416 printf("dodcolon()\n");
419 error("no files to be updated\n");
422 if (stat(stamp, &stb) < 0) {
423 error("%s: %s\n", stamp, strerror(errno));
427 printf("%s: %ld\n", stamp, stb.st_mtime);
430 lastmod = stb.st_mtime;
431 if (nflag || (options & VERIFY))
434 if ((tfp = fopen(tempfile, "w")) == NULL) {
435 error("%s: %s\n", stamp, strerror(errno));
438 (void) gettimeofday(&tv[0], &tz);
440 (void) utimes(stamp, tv);
443 for (f = files; f != NULL; f = f->n_next) {
445 for (cpp = filev; *cpp; cpp++)
446 if (strcmp(f->n_name, *cpp) == 0)
457 for (sc = cmds; sc != NULL; sc = sc->sc_next)
458 if (sc->sc_type == NOTIFY)
459 notify(tempfile, NULL, sc->sc_args, lastmod);
460 if (!nflag && !(options & VERIFY))
461 (void) unlink(tempfile);
465 * Compare the mtime of file to the list of time stamps.
474 printf("cmptime(%s)\n", name);
480 printf("comparing dates: %s\n", name);
485 * first time cmptime() is called?
488 if (exptilde(target, name, sizeof(target)) == NULL)
494 if (access(name, 4) < 0 || stat(name, &stb) < 0) {
495 error("%s: %s\n", name, strerror(errno));
499 switch (stb.st_mode & S_IFMT) {
508 error("%s: not a plain file\n", name);
512 if (stb.st_mtime > lastmod)
513 log(tfp, "new: %s\n", name);
521 register struct dirent *dp;
527 printf("rcmptime(%p)\n", st);
529 if ((d = opendir(target)) == NULL) {
530 error("%s: %s\n", target, strerror(errno));
535 while ((dp = readdir(d))) {
536 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
538 if (len + 1 + strlen(dp->d_name) >= BUFSIZ - 1) {
539 error("%s/%s: Name too long\n", target, dp->d_name);
545 while ((*tp++ = *cp++))
556 * Notify the list of people the changes that were made.
557 * rhost == NULL if we are mailing a list of changes compared to at time
561 notify(file, rhost, to, lmod)
563 register struct namelist *to;
566 register int fd, len;
570 if ((options & VERIFY) || to == NULL)
575 printf("@%s ", rhost);
581 if ((fd = open(file, 0)) < 0) {
582 error("%s: %s\n", file, strerror(errno));
585 if (fstat(fd, &stb) < 0) {
586 error("%s: %s\n", file, strerror(errno));
590 if (stb.st_size == 0) {
595 * Create a pipe to mailling program.
597 (void) snprintf(buf, sizeof(buf), "%s -oi -t", _PATH_SENDMAIL);
598 pf = popen(buf, "w");
600 error("notify: \"%s\" failed\n", _PATH_SENDMAIL);
605 * Output the proper header information.
607 fprintf(pf, "From: rdist (Remote distribution program)\n");
609 if (!any('@', to->n_name) && rhost != NULL)
610 fprintf(pf, " %s@%s", to->n_name, rhost);
612 fprintf(pf, " %s", to->n_name);
615 if (!any('@', to->n_name) && rhost != NULL)
616 fprintf(pf, ", %s@%s", to->n_name, rhost);
618 fprintf(pf, ", %s", to->n_name);
623 fprintf(pf, "Subject: files updated by rdist from %s to %s\n",
626 fprintf(pf, "Subject: files updated after %s\n", ctime(&lmod));
629 while ((len = read(fd, buf, BUFSIZ)) > 0)
630 (void) fwrite(buf, 1, len, pf);
636 * Return true if name is in the list.
640 struct namelist *list;
643 register struct namelist *nl;
645 for (nl = list; nl != NULL; nl = nl->n_next)
646 if (!strcmp(file, nl->n_name))
652 * Return TRUE if file is in the exception list.
658 register struct subcmd *sc;
659 register struct namelist *nl;
664 printf("except(%s)\n", file);
666 for (sc = subcmds; sc != NULL; sc = sc->sc_next) {
667 if (sc->sc_type != EXCEPT && sc->sc_type != PATTERN)
669 for (nl = sc->sc_args; nl != NULL; nl = nl->n_next) {
670 if (sc->sc_type == EXCEPT) {
671 if (!strcmp(file, nl->n_name))
675 val = regcomp(&rx, nl->n_name,
676 REG_EXTENDED | REG_NOSUB);
677 if (!regexec(&rx, file, 0, 0, 0)) {