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
33 * @(#)docmd.c 8.1 (Berkeley) 6/9/93
34 * $FreeBSD: src/usr.bin/rdist/docmd.c,v 1.12 1999/08/28 01:05:06 peter Exp $
35 * $DragonFly: src/usr.bin/rdist/docmd.c,v 1.2 2003/06/17 04:29:30 dillon Exp $
43 FILE *lfp; /* log file for recording files updated */
44 struct subcmd *subcmds; /* list of sub-commands for current cmd */
47 static int makeconn __P((char *));
48 static int okname __P((char *));
49 static void closeconn __P((void));
50 static void cmptime __P((char *));
51 static void doarrow __P((char **,
52 struct namelist *, char *, struct subcmd *));
53 static void dodcolon __P((char **,
54 struct namelist *, char *, struct subcmd *));
55 static void notify __P((char *, char *, struct namelist *, time_t));
56 static void rcmptime __P((struct stat *));
57 static int remotecmd __P((char *, char *, char *, char *));
60 * Do the commands in cmds (initialized by yyparse).
63 docmds(dhosts, argc, argv)
68 register struct cmd *c;
69 register struct namelist *f;
71 extern struct cmd *cmds;
73 signal(SIGHUP, cleanup);
74 signal(SIGINT, cleanup);
75 signal(SIGQUIT, cleanup);
76 signal(SIGTERM, cleanup);
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)
87 for (cpp = argv; *cpp; cpp++) {
88 if (c->c_label != NULL &&
89 strcmp(c->c_label, *cpp) == 0) {
93 for (f = c->c_files; f != NULL; f = f->n_next)
94 if (strcmp(f->n_name, *cpp) == 0)
103 doarrow(cpp, c->c_files, c->c_name, c->c_cmds);
106 dodcolon(cpp, c->c_files, c->c_name, c->c_cmds);
109 fatal("illegal command type %d\n", c->c_type);
116 * Process commands for sending files to other machines.
119 doarrow(filev, files, rhost, cmds)
121 struct namelist *files;
125 register struct namelist *f;
126 register struct subcmd *sc;
128 int n, ddir, opts = options;
131 printf("doarrow(%p, %s, %p)\n", files, rhost, cmds);
134 error("no files to be updated\n");
139 ddir = files->n_next != NULL; /* destination is a directory */
141 printf("updating host %s\n", rhost);
145 signal(SIGPIPE, lostconn);
146 if (!makeconn(rhost))
148 if ((lfp = fopen(tempfile, "w")) == NULL) {
149 fatal("cannot open %s\n", tempfile);
153 for (f = files; f != NULL; f = f->n_next) {
155 for (cpp = filev; *cpp; cpp++)
156 if (strcmp(f->n_name, *cpp) == 0)
164 for (sc = cmds; sc != NULL; sc = sc->sc_next) {
165 if (sc->sc_type != INSTALL)
168 install(f->n_name, sc->sc_name,
169 sc->sc_name == NULL ? 0 : ddir, sc->sc_options);
170 opts = sc->sc_options;
173 install(f->n_name, NULL, 0, options);
177 (void) signal(SIGPIPE, cleanup);
182 for (sc = cmds; sc != NULL; sc = sc->sc_next)
183 if (sc->sc_type == NOTIFY)
184 notify(tempfile, rhost, sc->sc_args, 0);
186 struct linkbuf *nextihead;
188 (void) unlink(tempfile);
189 for (; ihead != NULL; ihead = nextihead) {
190 nextihead = ihead->nextp;
191 if ((opts & IGNLNKS) || ihead->count == 0)
193 log(lfp, "%s: Warning: missing links\n",
200 static int remotecmd(rhost, luser, ruser, cmd)
206 static int port = -1;
209 printf("local user = %s remote user = %s\n", luser, ruser);
210 printf("Remote command = '%s'\n", cmd);
213 (void) fflush(stdout);
214 (void) fflush(stderr);
215 (void) signal(SIGALRM, cleanup);
216 (void) alarm(RTIMEOUT);
218 if (geteuid() == 0 && strcmp(path_rsh, _PATH_RSH) == 0) {
220 printf("I am root, therefore direct rcmd\n");
222 (void) signal(SIGPIPE, cleanup);
227 if ((sp = getservbyname("shell", "tcp")) == NULL)
228 fatal("shell/tcp: unknown service");
232 desc = rcmd(&rhost, port, luser, ruser, cmd, 0);
235 printf("Remote shell command = '%s'\n", path_rsh);
236 (void) signal(SIGPIPE, SIG_IGN);
237 desc = rshrcmd(&rhost, -1, luser, ruser, cmd, 0);
239 (void) signal(SIGPIPE, cleanup);
249 * Create a connection to the rdist server on the machine rhost.
255 register char *ruser, *cp;
256 static char *cur_host = NULL;
257 static int port = -1;
261 #if defined(DIRECT_RCMD)
266 printf("makeconn(%s)\n", rhost);
268 if (cur_host != NULL && rem >= 0) {
269 if (strcmp(cur_host, rhost) == 0)
274 cp = index(rhost, '@');
279 strncpy(tuser, rhost, sizeof(tuser)-1);
285 else if (!okname(ruser))
290 printf("updating host %s\n", rhost);
291 (void) snprintf(buf, sizeof(buf), "%s -Server%s",
292 _PATH_RDIST, qflag ? " -q" : "");
296 if ((sp = getservbyname("shell", "tcp")) == NULL)
297 fatal("shell/tcp: unknown service");
302 printf("port = %d, luser = %s, ruser = %s\n", ntohs(port), user, ruser);
303 printf("buf = %s\n", buf);
307 #if defined(DIRECT_RCMD)
309 rem = rcmd(&rhost, port, user, ruser, buf, 0);
312 rem = remotecmd(rhost, user, ruser, buf);
317 if (read(rem, cp, 1) != 1)
321 if (read(rem, cp, 1) != 1)
323 } while (*cp++ != '\n' && cp < &buf[BUFSIZ]);
327 while (*cp >= '0' && *cp <= '9')
328 n = (n * 10) + (*cp++ - '0');
329 if (*cp == '\0' && n == VERSION)
331 error("connection failed: version numbers don't match (local %d, remote %d)\n", VERSION, n);
333 error("connection failed: version numbers don't match\n");
334 error("got unexpected input:");
337 } while (*cp != '\n' && read(rem, cp, 1) == 1);
344 * Signal end of previous connection.
350 printf("closeconn()\n");
353 (void) write(rem, "\2\n", 2);
365 log(lfp, "rdist: lost connection\n");
373 register char *cp = name;
380 if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-')
386 error("invalid user name %s\n", name);
392 extern char target[], *tp;
395 * Process commands for comparing files to time stamp files.
398 dodcolon(filev, files, stamp, cmds)
400 struct namelist *files;
404 register struct subcmd *sc;
405 register struct namelist *f;
407 struct timeval tv[2];
412 printf("dodcolon()\n");
415 error("no files to be updated\n");
418 if (stat(stamp, &stb) < 0) {
419 error("%s: %s\n", stamp, strerror(errno));
423 printf("%s: %ld\n", stamp, stb.st_mtime);
426 lastmod = stb.st_mtime;
427 if (nflag || (options & VERIFY))
430 if ((tfp = fopen(tempfile, "w")) == NULL) {
431 error("%s: %s\n", stamp, strerror(errno));
434 (void) gettimeofday(&tv[0], &tz);
436 (void) utimes(stamp, tv);
439 for (f = files; f != NULL; f = f->n_next) {
441 for (cpp = filev; *cpp; cpp++)
442 if (strcmp(f->n_name, *cpp) == 0)
453 for (sc = cmds; sc != NULL; sc = sc->sc_next)
454 if (sc->sc_type == NOTIFY)
455 notify(tempfile, NULL, sc->sc_args, lastmod);
456 if (!nflag && !(options & VERIFY))
457 (void) unlink(tempfile);
461 * Compare the mtime of file to the list of time stamps.
470 printf("cmptime(%s)\n", name);
476 printf("comparing dates: %s\n", name);
481 * first time cmptime() is called?
484 if (exptilde(target, name, sizeof(target)) == NULL)
490 if (access(name, 4) < 0 || stat(name, &stb) < 0) {
491 error("%s: %s\n", name, strerror(errno));
495 switch (stb.st_mode & S_IFMT) {
504 error("%s: not a plain file\n", name);
508 if (stb.st_mtime > lastmod)
509 log(tfp, "new: %s\n", name);
517 register struct dirent *dp;
523 printf("rcmptime(%p)\n", st);
525 if ((d = opendir(target)) == NULL) {
526 error("%s: %s\n", target, strerror(errno));
531 while ((dp = readdir(d))) {
532 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
534 if (len + 1 + strlen(dp->d_name) >= BUFSIZ - 1) {
535 error("%s/%s: Name too long\n", target, dp->d_name);
541 while ((*tp++ = *cp++))
552 * Notify the list of people the changes that were made.
553 * rhost == NULL if we are mailing a list of changes compared to at time
557 notify(file, rhost, to, lmod)
559 register struct namelist *to;
562 register int fd, len;
566 if ((options & VERIFY) || to == NULL)
571 printf("@%s ", rhost);
577 if ((fd = open(file, 0)) < 0) {
578 error("%s: %s\n", file, strerror(errno));
581 if (fstat(fd, &stb) < 0) {
582 error("%s: %s\n", file, strerror(errno));
586 if (stb.st_size == 0) {
591 * Create a pipe to mailling program.
593 (void) snprintf(buf, sizeof(buf), "%s -oi -t", _PATH_SENDMAIL);
594 pf = popen(buf, "w");
596 error("notify: \"%s\" failed\n", _PATH_SENDMAIL);
601 * Output the proper header information.
603 fprintf(pf, "From: rdist (Remote distribution program)\n");
605 if (!any('@', to->n_name) && rhost != NULL)
606 fprintf(pf, " %s@%s", to->n_name, rhost);
608 fprintf(pf, " %s", to->n_name);
611 if (!any('@', to->n_name) && rhost != NULL)
612 fprintf(pf, ", %s@%s", to->n_name, rhost);
614 fprintf(pf, ", %s", to->n_name);
619 fprintf(pf, "Subject: files updated by rdist from %s to %s\n",
622 fprintf(pf, "Subject: files updated after %s\n", ctime(&lmod));
625 while ((len = read(fd, buf, BUFSIZ)) > 0)
626 (void) fwrite(buf, 1, len, pf);
632 * Return true if name is in the list.
636 struct namelist *list;
639 register struct namelist *nl;
641 for (nl = list; nl != NULL; nl = nl->n_next)
642 if (!strcmp(file, nl->n_name))
648 * Return TRUE if file is in the exception list.
654 register struct subcmd *sc;
655 register struct namelist *nl;
660 printf("except(%s)\n", file);
662 for (sc = subcmds; sc != NULL; sc = sc->sc_next) {
663 if (sc->sc_type != EXCEPT && sc->sc_type != PATTERN)
665 for (nl = sc->sc_args; nl != NULL; nl = nl->n_next) {
666 if (sc->sc_type == EXCEPT) {
667 if (!strcmp(file, nl->n_name))
671 val = regcomp(&rx, nl->n_name,
672 REG_EXTENDED | REG_NOSUB);
673 if (!regexec(&rx, file, 0, 0, 0)) {