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
35 static char sccsid[] = "@(#)docmd.c 8.1 (Berkeley) 6/9/93";
42 FILE *lfp; /* log file for recording files updated */
43 struct subcmd *subcmds; /* list of sub-commands for current cmd */
46 static int makeconn __P((char *));
47 static int okname __P((char *));
48 static void closeconn __P((void));
49 static void cmptime __P((char *));
50 static void doarrow __P((char **,
51 struct namelist *, char *, struct subcmd *));
52 static void dodcolon __P((char **,
53 struct namelist *, char *, struct subcmd *));
54 static void notify __P((char *, char *, struct namelist *, time_t));
55 static void rcmptime __P((struct stat *));
58 * Do the commands in cmds (initialized by yyparse).
61 docmds(dhosts, argc, argv)
66 register struct cmd *c;
67 register struct namelist *f;
69 extern struct cmd *cmds;
71 signal(SIGHUP, cleanup);
72 signal(SIGINT, cleanup);
73 signal(SIGQUIT, cleanup);
74 signal(SIGTERM, cleanup);
76 for (c = cmds; c != NULL; c = c->c_next) {
77 if (dhosts != NULL && *dhosts != NULL) {
78 for (cpp = dhosts; *cpp; cpp++)
79 if (strcmp(c->c_name, *cpp) == 0)
85 for (cpp = argv; *cpp; cpp++) {
86 if (c->c_label != NULL &&
87 strcmp(c->c_label, *cpp) == 0) {
91 for (f = c->c_files; f != NULL; f = f->n_next)
92 if (strcmp(f->n_name, *cpp) == 0)
101 doarrow(cpp, c->c_files, c->c_name, c->c_cmds);
104 dodcolon(cpp, c->c_files, c->c_name, c->c_cmds);
107 fatal("illegal command type %d\n", c->c_type);
114 * Process commands for sending files to other machines.
117 doarrow(filev, files, rhost, cmds)
119 struct namelist *files;
123 register struct namelist *f;
124 register struct subcmd *sc;
126 int n, ddir, opts = options;
129 printf("doarrow(%x, %s, %x)\n", files, rhost, cmds);
132 error("no files to be updated\n");
137 ddir = files->n_next != NULL; /* destination is a directory */
139 printf("updating host %s\n", rhost);
143 signal(SIGPIPE, lostconn);
144 if (!makeconn(rhost))
146 if ((lfp = fopen(tempfile, "w")) == NULL) {
147 fatal("cannot open %s\n", tempfile);
151 for (f = files; f != NULL; f = f->n_next) {
153 for (cpp = filev; *cpp; cpp++)
154 if (strcmp(f->n_name, *cpp) == 0)
162 for (sc = cmds; sc != NULL; sc = sc->sc_next) {
163 if (sc->sc_type != INSTALL)
166 install(f->n_name, sc->sc_name,
167 sc->sc_name == NULL ? 0 : ddir, sc->sc_options);
168 opts = sc->sc_options;
171 install(f->n_name, NULL, 0, options);
175 (void) signal(SIGPIPE, cleanup);
179 for (sc = cmds; sc != NULL; sc = sc->sc_next)
180 if (sc->sc_type == NOTIFY)
181 notify(tempfile, rhost, sc->sc_args, 0);
183 (void) unlink(tempfile);
184 for (; ihead != NULL; ihead = ihead->nextp) {
186 if ((opts & IGNLNKS) || ihead->count == 0)
188 log(lfp, "%s: Warning: missing links\n",
195 * Create a connection to the rdist server on the machine rhost.
201 register char *ruser, *cp;
202 static char *cur_host = NULL;
203 static int port = -1;
210 printf("makeconn(%s)\n", rhost);
212 if (cur_host != NULL && rem >= 0) {
213 if (strcmp(cur_host, rhost) == 0)
218 cp = index(rhost, '@');
223 strncpy(tuser, rhost, sizeof(tuser)-1);
229 else if (!okname(ruser))
234 printf("updating host %s\n", rhost);
235 (void) sprintf(buf, "%s -Server%s", _PATH_RDIST, qflag ? " -q" : "");
239 if ((sp = getservbyname("shell", "tcp")) == NULL)
240 fatal("shell/tcp: unknown service");
245 printf("port = %d, luser = %s, ruser = %s\n", ntohs(port), user, ruser);
246 printf("buf = %s\n", buf);
251 rem = rcmd(&rhost, port, user, ruser, buf, 0);
256 if (read(rem, cp, 1) != 1)
260 if (read(rem, cp, 1) != 1)
262 } while (*cp++ != '\n' && cp < &buf[BUFSIZ]);
266 while (*cp >= '0' && *cp <= '9')
267 n = (n * 10) + (*cp++ - '0');
268 if (*cp == '\0' && n == VERSION)
270 error("connection failed: version numbers don't match (local %d, remote %d)\n", VERSION, n);
272 error("connection failed: version numbers don't match\n");
273 error("got unexpected input:");
276 } while (*cp != '\n' && read(rem, cp, 1) == 1);
283 * Signal end of previous connection.
289 printf("closeconn()\n");
292 (void) write(rem, "\2\n", 2);
304 log(lfp, "rdist: lost connection\n");
312 register char *cp = name;
319 if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-')
325 error("invalid user name %s\n", name);
331 extern char target[], *tp;
334 * Process commands for comparing files to time stamp files.
337 dodcolon(filev, files, stamp, cmds)
339 struct namelist *files;
343 register struct subcmd *sc;
344 register struct namelist *f;
346 struct timeval tv[2];
351 printf("dodcolon()\n");
354 error("no files to be updated\n");
357 if (stat(stamp, &stb) < 0) {
358 error("%s: %s\n", stamp, strerror(errno));
362 printf("%s: %ld\n", stamp, stb.st_mtime);
365 lastmod = stb.st_mtime;
366 if (nflag || (options & VERIFY))
369 if ((tfp = fopen(tempfile, "w")) == NULL) {
370 error("%s: %s\n", stamp, strerror(errno));
373 (void) gettimeofday(&tv[0], &tz);
375 (void) utimes(stamp, tv);
378 for (f = files; f != NULL; f = f->n_next) {
380 for (cpp = filev; *cpp; cpp++)
381 if (strcmp(f->n_name, *cpp) == 0)
392 for (sc = cmds; sc != NULL; sc = sc->sc_next)
393 if (sc->sc_type == NOTIFY)
394 notify(tempfile, NULL, sc->sc_args, lastmod);
395 if (!nflag && !(options & VERIFY))
396 (void) unlink(tempfile);
400 * Compare the mtime of file to the list of time stamps.
409 printf("cmptime(%s)\n", name);
415 printf("comparing dates: %s\n", name);
420 * first time cmptime() is called?
423 if (exptilde(target, name) == NULL)
429 if (access(name, 4) < 0 || stat(name, &stb) < 0) {
430 error("%s: %s\n", name, strerror(errno));
434 switch (stb.st_mode & S_IFMT) {
443 error("%s: not a plain file\n", name);
447 if (stb.st_mtime > lastmod)
448 log(tfp, "new: %s\n", name);
456 register struct direct *dp;
462 printf("rcmptime(%x)\n", st);
464 if ((d = opendir(target)) == NULL) {
465 error("%s: %s\n", target, strerror(errno));
470 while (dp = readdir(d)) {
471 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
473 if (len + 1 + strlen(dp->d_name) >= BUFSIZ - 1) {
474 error("%s/%s: Name too long\n", target, dp->d_name);
480 while (*tp++ = *cp++)
491 * Notify the list of people the changes that were made.
492 * rhost == NULL if we are mailing a list of changes compared to at time
496 notify(file, rhost, to, lmod)
498 register struct namelist *to;
501 register int fd, len;
505 if ((options & VERIFY) || to == NULL)
510 printf("@%s ", rhost);
516 if ((fd = open(file, 0)) < 0) {
517 error("%s: %s\n", file, strerror(errno));
520 if (fstat(fd, &stb) < 0) {
521 error("%s: %s\n", file, strerror(errno));
525 if (stb.st_size == 0) {
530 * Create a pipe to mailling program.
532 (void)sprintf(buf, "%s -oi -t", _PATH_SENDMAIL);
533 pf = popen(buf, "w");
535 error("notify: \"%s\" failed\n", _PATH_SENDMAIL);
540 * Output the proper header information.
542 fprintf(pf, "From: rdist (Remote distribution program)\n");
544 if (!any('@', to->n_name) && rhost != NULL)
545 fprintf(pf, " %s@%s", to->n_name, rhost);
547 fprintf(pf, " %s", to->n_name);
550 if (!any('@', to->n_name) && rhost != NULL)
551 fprintf(pf, ", %s@%s", to->n_name, rhost);
553 fprintf(pf, ", %s", to->n_name);
558 fprintf(pf, "Subject: files updated by rdist from %s to %s\n",
561 fprintf(pf, "Subject: files updated after %s\n", ctime(&lmod));
564 while ((len = read(fd, buf, BUFSIZ)) > 0)
565 (void) fwrite(buf, 1, len, pf);
571 * Return true if name is in the list.
575 struct namelist *list;
578 register struct namelist *nl;
580 for (nl = list; nl != NULL; nl = nl->n_next)
581 if (!strcmp(file, nl->n_name))
587 * Return TRUE if file is in the exception list.
593 register struct subcmd *sc;
594 register struct namelist *nl;
597 printf("except(%s)\n", file);
599 for (sc = subcmds; sc != NULL; sc = sc->sc_next) {
600 if (sc->sc_type != EXCEPT && sc->sc_type != PATTERN)
602 for (nl = sc->sc_args; nl != NULL; nl = nl->n_next) {
603 if (sc->sc_type == EXCEPT) {
604 if (!strcmp(file, nl->n_name))
609 if (re_exec(file) > 0)