sendmail -> paths.h
[unix-history] / usr / src / usr.bin / rdist / docmd.c
index a9ad132..448c707 100644 (file)
+/*
+ * Copyright (c) 1983 Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that the above copyright notice and this paragraph are
+ * duplicated in all such forms and that any documentation,
+ * advertising materials, and other materials related to such
+ * distribution and use acknowledge that the software was developed
+ * by the University of California, Berkeley.  The name of the
+ * University may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ */
+
 #ifndef lint
 #ifndef lint
-static char *sccsid = "@(#)docmd.c     4.9 (Berkeley) 83/11/29";
-#endif
+static char sccsid[] = "@(#)docmd.c    5.4 (Berkeley) %G%";
+#endif /* not lint */
 
 #include "defs.h"
 
 #include "defs.h"
+#include <setjmp.h>
+#include <netdb.h>
 
 
-FILE   *lfp;           /* log file for recording files updated */
-struct block *special; /* list of special commands */
+FILE   *lfp;                   /* log file for recording files updated */
+struct subcmd *subcmds;        /* list of sub-commands for current cmd */
+jmp_buf        env;
+
+int    cleanup();
+int    lostconn();
+
+/*
+ * Do the commands in cmds (initialized by yyparse).
+ */
+docmds(dhosts, argc, argv)
+       char **dhosts;
+       int argc;
+       char **argv;
+{
+       register struct cmd *c;
+       register struct namelist *f;
+       register char **cpp;
+       extern struct cmd *cmds;
+
+       signal(SIGHUP, cleanup);
+       signal(SIGINT, cleanup);
+       signal(SIGQUIT, cleanup);
+       signal(SIGTERM, cleanup);
+
+       for (c = cmds; c != NULL; c = c->c_next) {
+               if (dhosts != NULL && *dhosts != NULL) {
+                       for (cpp = dhosts; *cpp; cpp++)
+                               if (strcmp(c->c_name, *cpp) == 0)
+                                       goto fndhost;
+                       continue;
+               }
+       fndhost:
+               if (argc) {
+                       for (cpp = argv; *cpp; cpp++) {
+                               if (c->c_label != NULL &&
+                                   strcmp(c->c_label, *cpp) == 0) {
+                                       cpp = NULL;
+                                       goto found;
+                               }
+                               for (f = c->c_files; f != NULL; f = f->n_next)
+                                       if (strcmp(f->n_name, *cpp) == 0)
+                                               goto found;
+                       }
+                       continue;
+               } else
+                       cpp = NULL;
+       found:
+               switch (c->c_type) {
+               case ARROW:
+                       doarrow(cpp, c->c_files, c->c_name, c->c_cmds);
+                       break;
+               case DCOLON:
+                       dodcolon(cpp, c->c_files, c->c_name, c->c_cmds);
+                       break;
+               default:
+                       fatal("illegal command type %d\n", c->c_type);
+               }
+       }
+       closeconn();
+}
 
 /*
  * Process commands for sending files to other machines.
  */
 
 /*
  * Process commands for sending files to other machines.
  */
-dohcmds(files, hosts, cmds)
-       struct block *files, *hosts, *cmds;
+doarrow(filev, files, rhost, cmds)
+       char **filev;
+       struct namelist *files;
+       char *rhost;
+       struct subcmd *cmds;
 {
 {
-       register struct block *h, *f, *c;
+       register struct namelist *f;
+       register struct subcmd *sc;
        register char **cpp;
        register char **cpp;
-       int n, ddir;
+       int n, ddir, opts = options;
 
        if (debug)
 
        if (debug)
-               printf("dohcmds(%x, %x, %x)\n", files, hosts, cmds);
+               printf("doarrow(%x, %s, %x)\n", files, rhost, cmds);
 
 
-       files = expand(files, E_VARS|E_SHELL);
        if (files == NULL) {
                error("no files to be updated\n");
                return;
        }
        if (files == NULL) {
                error("no files to be updated\n");
                return;
        }
-       hosts = expand(hosts, E_VARS|E_SHELL);
-       if (hosts == NULL) {
-               error("empty list of hosts to be updated\n");
-               return;
-       }
-       if (!mkexceptlist(cmds))
-               return;
-       special = cmds;
-
-       ddir = files->b_next != NULL;
 
 
-       for (h = hosts; h != NULL; h = h->b_next) {
-               if (!qflag)
-                       printf("updating host %s\n", h->b_name);
-               if (!nflag) {
-                       if (!makeconn(h->b_name))
-                               continue;
-                       if ((lfp = fopen(tmpfile, "w")) == NULL) {
-                               fatal("cannot open %s\n", tmpfile);
-                               exit(1);
-                       }
+       subcmds = cmds;
+       ddir = files->n_next != NULL;   /* destination is a directory */
+       if (nflag)
+               printf("updating host %s\n", rhost);
+       else {
+               if (setjmp(env))
+                       goto done;
+               signal(SIGPIPE, lostconn);
+               if (!makeconn(rhost))
+                       return;
+               if ((lfp = fopen(tmpfile, "w")) == NULL) {
+                       fatal("cannot open %s\n", tmpfile);
+                       exit(1);
                }
                }
-               for (f = files; f != NULL; f = f->b_next) {
-                       if (filec) {
-                               for (cpp = filev; *cpp; cpp++)
-                                       if (!strcmp(f->b_name, *cpp))
-                                               goto found;
-                               if (!nflag)
-                                       (void) fclose(lfp);
-                               continue;
-                       }
-               found:
-                       n = 0;
-                       for (c = cmds; c != NULL; c = c->b_next) {
-                               if (c->b_type != INSTALL)
-                                       continue;
-                               n++;
-                               install(f->b_name, c->b_name,
-                                       c->b_name == NULL ? 0 : ddir,
-                                       c->b_options);
-                       }
-                       if (n == 0)
-                               install(f->b_name, NULL, 0, options);
+       }
+       for (f = files; f != NULL; f = f->n_next) {
+               if (filev) {
+                       for (cpp = filev; *cpp; cpp++)
+                               if (strcmp(f->n_name, *cpp) == 0)
+                                       goto found;
+                       if (!nflag)
+                               (void) fclose(lfp);
+                       continue;
                }
                }
-               if (!nflag) {
-                       /* signal end of connection */
-                       (void) write(rem, "\2\n", 2);
-                       (void) close(rem);
-                       (void) fclose(lfp);
+       found:
+               n = 0;
+               for (sc = cmds; sc != NULL; sc = sc->sc_next) {
+                       if (sc->sc_type != INSTALL)
+                               continue;
+                       n++;
+                       install(f->n_name, sc->sc_name,
+                               sc->sc_name == NULL ? 0 : ddir, sc->sc_options);
+                       opts = sc->sc_options;
                }
                }
-               for (c = cmds; c != NULL; c = c->b_next)
-                       if (c->b_type == NOTIFY)
-                               notify(tmpfile, h->b_name, c->b_args, 0);
+               if (n == 0)
+                       install(f->n_name, NULL, 0, options);
        }
        }
-       if (!nflag)
+done:
+       if (!nflag) {
+               (void) signal(SIGPIPE, cleanup);
+               (void) fclose(lfp);
+               lfp = NULL;
+       }
+       for (sc = cmds; sc != NULL; sc = sc->sc_next)
+               if (sc->sc_type == NOTIFY)
+                       notify(tmpfile, rhost, sc->sc_args, 0);
+       if (!nflag) {
                (void) unlink(tmpfile);
                (void) unlink(tmpfile);
+               for (; ihead != NULL; ihead = ihead->nextp) {
+                       free(ihead);
+                       if ((opts & IGNLNKS) || ihead->count == 0)
+                               continue;
+                       log(lfp, "%s: Warning: missing links\n",
+                               ihead->pathname);
+               }
+       }
 }
 
 /*
 }
 
 /*
@@ -89,33 +171,103 @@ dohcmds(files, hosts, cmds)
 makeconn(rhost)
        char *rhost;
 {
 makeconn(rhost)
        char *rhost;
 {
-       register struct block *c;
-       register char *ruser;
+       register char *ruser, *cp;
+       static char *cur_host = NULL;
+       static int port = -1;
+       char tuser[20];
+       int n;
        extern char user[];
        extern char user[];
+       extern int userid;
 
 
-       (void) sprintf(buf, "/usr/local/rdist -Server%s", qflag ? " -q" : "");
+       if (debug)
+               printf("makeconn(%s)\n", rhost);
 
 
-       ruser = rindex(rhost, '.');
-       if (ruser != NULL) {
-               *ruser++ = '\0';
-               if (!okname(ruser))
+       if (cur_host != NULL && rem >= 0) {
+               if (strcmp(cur_host, rhost) == 0)
+                       return(1);
+               closeconn();
+       }
+       cur_host = rhost;
+       cp = index(rhost, '@');
+       if (cp != NULL) {
+               char c = *cp;
+
+               *cp = '\0';
+               strncpy(tuser, rhost, sizeof(tuser)-1);
+               *cp = c;
+               rhost = cp + 1;
+               ruser = tuser;
+               if (*ruser == '\0')
+                       ruser = user;
+               else if (!okname(ruser))
                        return(0);
        } else
                ruser = user;
                        return(0);
        } else
                ruser = user;
+       if (!qflag)
+               printf("updating host %s\n", rhost);
+       (void) sprintf(buf, "%s -Server%s", _PATH_RDIST, qflag ? " -q" : "");
+       if (port < 0) {
+               struct servent *sp;
+
+               if ((sp = getservbyname("shell", "tcp")) == NULL)
+                       fatal("shell/tcp: unknown service");
+               port = sp->s_port;
+       }
 
        if (debug) {
 
        if (debug) {
-               printf("makeconn(%s)\n", rhost);
-               printf("luser = %s, ruser = %s\n", user, ruser);
+               printf("port = %d, luser = %s, ruser = %s\n", ntohs(port), user, ruser);
                printf("buf = %s\n", buf);
        }
 
        fflush(stdout);
                printf("buf = %s\n", buf);
        }
 
        fflush(stdout);
-       rem = rcmd(&rhost, IPPORT_CMDSERVER, user, ruser, buf, 0);
+       setreuid(userid, 0);
+       rem = rcmd(&rhost, port, user, ruser, buf, 0);
+       setreuid(0, userid);
        if (rem < 0)
                return(0);
        if (rem < 0)
                return(0);
-       if (response() < 0)
-               return(0);
-       return(1);
+       cp = buf;
+       if (read(rem, cp, 1) != 1)
+               lostconn();
+       if (*cp == 'V') {
+               do {
+                       if (read(rem, cp, 1) != 1)
+                               lostconn();
+               } while (*cp++ != '\n' && cp < &buf[BUFSIZ]);
+               *--cp = '\0';
+               cp = buf;
+               n = 0;
+               while (*cp >= '0' && *cp <= '9')
+                       n = (n * 10) + (*cp++ - '0');
+               if (*cp == '\0' && n == VERSION)
+                       return(1);
+               error("connection failed: version numbers don't match (local %d, remote %d)\n", VERSION, n);
+       } else
+               error("connection failed: version numbers don't match\n");
+       closeconn();
+       return(0);
+}
+
+/*
+ * Signal end of previous connection.
+ */
+closeconn()
+{
+       if (debug)
+               printf("closeconn()\n");
+
+       if (rem >= 0) {
+               (void) write(rem, "\2\n", 2);
+               (void) close(rem);
+               rem = -1;
+       }
+}
+
+lostconn()
+{
+       if (iamremote)
+               cleanup();
+       log(lfp, "rdist: lost connection\n");
+       longjmp(env, 1);
 }
 
 okname(name)
 }
 
 okname(name)
@@ -145,68 +297,64 @@ extern    char target[], *tp;
 /*
  * Process commands for comparing files to time stamp files.
  */
 /*
  * Process commands for comparing files to time stamp files.
  */
-dofcmds(files, stamp, cmds)
-       struct block *files, *stamp, *cmds;
+dodcolon(filev, files, stamp, cmds)
+       char **filev;
+       struct namelist *files;
+       char *stamp;
+       struct subcmd *cmds;
 {
 {
-       register struct block *b;
+       register struct subcmd *sc;
+       register struct namelist *f;
        register char **cpp;
        struct timeval tv[2];
        struct timezone tz;
        struct stat stb;
 
        if (debug)
        register char **cpp;
        struct timeval tv[2];
        struct timezone tz;
        struct stat stb;
 
        if (debug)
-               printf("dofcmds()\n");
+               printf("dodcolon()\n");
 
 
-       files = expand(files, E_ALL);
        if (files == NULL) {
                error("no files to be updated\n");
                return;
        }
        if (files == NULL) {
                error("no files to be updated\n");
                return;
        }
-       stamp = expand(stamp, E_ALL);
-       if (stamp == NULL || stamp->b_next != NULL) {
-               error("Only one time stamp file allowed\n");
-               return;
-       }
-       if (!mkexceptlist(cmds))
-               return;
-
-       if (stat(stamp->b_name, &stb) < 0) {
-               error("%s: %s\n", stamp->b_name, sys_errlist[errno]);
+       if (stat(stamp, &stb) < 0) {
+               error("%s: %s\n", stamp, sys_errlist[errno]);
                return;
        }
        if (debug)
                return;
        }
        if (debug)
-               printf("%s: %d\n", stamp->b_name, stb.st_mtime);
-       if (!nflag) {
-               lastmod = stb.st_mtime;
-               (void) gettimeofday(&tv[0], &tz);
-               tv[1] = tv[0];
-               (void) utimes(stamp->b_name, tv);
-               if (options & VERIFY)
-                       tfp = NULL;
-               else if ((tfp = fopen(tmpfile, "w")) == NULL) {
-                       error("%s: %s\n", stamp->b_name, sys_errlist[errno]);
+               printf("%s: %d\n", stamp, stb.st_mtime);
+
+       subcmds = cmds;
+       lastmod = stb.st_mtime;
+       if (nflag || (options & VERIFY))
+               tfp = NULL;
+       else {
+               if ((tfp = fopen(tmpfile, "w")) == NULL) {
+                       error("%s: %s\n", stamp, sys_errlist[errno]);
                        return;
                }
                        return;
                }
-       } else
-               tfp = NULL;
+               (void) gettimeofday(&tv[0], &tz);
+               tv[1] = tv[0];
+               (void) utimes(stamp, tv);
+       }
 
 
-       for (b = files; b != NULL; b = b->b_next) {
-               if (filec) {
+       for (f = files; f != NULL; f = f->n_next) {
+               if (filev) {
                        for (cpp = filev; *cpp; cpp++)
                        for (cpp = filev; *cpp; cpp++)
-                               if (!strcmp(b->b_name, *cpp))
+                               if (strcmp(f->n_name, *cpp) == 0)
                                        goto found;
                        continue;
                }
        found:
                tp = NULL;
                                        goto found;
                        continue;
                }
        found:
                tp = NULL;
-               cmptime(b->b_name);
+               cmptime(f->n_name);
        }
 
        if (tfp != NULL)
                (void) fclose(tfp);
        }
 
        if (tfp != NULL)
                (void) fclose(tfp);
-       for (b = cmds; b != NULL; b = b->b_next)
-               if (b->b_type == NOTIFY)
-                       notify(tmpfile, NULL, b->b_args, lastmod);
+       for (sc = cmds; sc != NULL; sc = sc->sc_next)
+               if (sc->sc_type == NOTIFY)
+                       notify(tmpfile, NULL, sc->sc_args, lastmod);
        if (!nflag && !(options & VERIFY))
                (void) unlink(tmpfile);
 }
        if (!nflag && !(options & VERIFY))
                (void) unlink(tmpfile);
 }
@@ -222,7 +370,7 @@ cmptime(name)
        if (debug)
                printf("cmptime(%s)\n", name);
 
        if (debug)
                printf("cmptime(%s)\n", name);
 
-       if (inlist(except, name))
+       if (except(name))
                return;
 
        if (nflag) {
                return;
 
        if (nflag) {
@@ -307,7 +455,7 @@ rcmptime(st)
  */
 notify(file, rhost, to, lmod)
        char *file, *rhost;
  */
 notify(file, rhost, to, lmod)
        char *file, *rhost;
-       register struct block *to;
+       register struct namelist *to;
        time_t lmod;
 {
        register int fd, len;
        time_t lmod;
 {
        register int fd, len;
@@ -341,9 +489,10 @@ notify(file, rhost, to, lmod)
        /*
         * Create a pipe to mailling program.
         */
        /*
         * Create a pipe to mailling program.
         */
-       pf = popen(MAILCMD, "w");
+       (void)sprintf(buf, "%s -oi -t", _PATH_SENDMAIL);
+       pf = popen(buf, "w");
        if (pf == NULL) {
        if (pf == NULL) {
-               error("notify: \"%s\" failed\n", MAILCMD);
+               error("notify: \"%s\" failed\n", _PATH_SENDMAIL);
                (void) close(fd);
                return;
        }
                (void) close(fd);
                return;
        }
@@ -352,17 +501,17 @@ notify(file, rhost, to, lmod)
         */
        fprintf(pf, "From: rdist (Remote distribution program)\n");
        fprintf(pf, "To:");
         */
        fprintf(pf, "From: rdist (Remote distribution program)\n");
        fprintf(pf, "To:");
-       if (!any('@', to->b_name) && host != NULL)
-               fprintf(pf, " %s@%s", to->b_name, rhost);
+       if (!any('@', to->n_name) && rhost != NULL)
+               fprintf(pf, " %s@%s", to->n_name, rhost);
        else
        else
-               fprintf(pf, " %s", to->b_name);
-       to = to->b_next;
+               fprintf(pf, " %s", to->n_name);
+       to = to->n_next;
        while (to != NULL) {
        while (to != NULL) {
-               if (!any('@', to->b_name) && host != NULL)
-                       fprintf(pf, ", %s@%s", to->b_name, rhost);
+               if (!any('@', to->n_name) && rhost != NULL)
+                       fprintf(pf, ", %s@%s", to->n_name, rhost);
                else
                else
-                       fprintf(pf, ", %s", to->b_name);
-               to = to->b_next;
+                       fprintf(pf, ", %s", to->n_name);
+               to = to->n_next;
        }
        putc('\n', pf);
        if (rhost != NULL)
        }
        putc('\n', pf);
        if (rhost != NULL)
@@ -378,51 +527,48 @@ notify(file, rhost, to, lmod)
        (void) pclose(pf);
 }
 
        (void) pclose(pf);
 }
 
-struct block *except;          /* list of files to exclude */
-
 /*
  * Return true if name is in the list.
  */
 inlist(list, file)
 /*
  * Return true if name is in the list.
  */
 inlist(list, file)
-       struct block *list;
+       struct namelist *list;
        char *file;
 {
        char *file;
 {
-       register struct block *c;
+       register struct namelist *nl;
 
 
-       for (c = list; c != NULL; c = c->b_next)
-               if (!strcmp(file, c->b_name))
+       for (nl = list; nl != NULL; nl = nl->n_next)
+               if (!strcmp(file, nl->n_name))
                        return(1);
        return(0);
 }
 
 /*
                        return(1);
        return(0);
 }
 
 /*
- * Build the exception list from an unexpanded list of commands.
+ * Return TRUE if file is in the exception list.
  */
  */
-mkexceptlist(cmds)
-       struct block *cmds;
+except(file)
+       char *file;
 {
 {
-       register struct block *f, *a, *c;
-       register char *cp;
+       register struct subcmd *sc;
+       register struct namelist *nl;
 
        if (debug)
 
        if (debug)
-               printf("mkexceptlist()\n");
+               printf("except(%s)\n", file);
 
 
-       except = f = NULL;
-       for (c = cmds; c != NULL; c = c->b_next) {
-               if (c->b_type != EXCEPT)
+       for (sc = subcmds; sc != NULL; sc = sc->sc_next) {
+               if (sc->sc_type != EXCEPT && sc->sc_type != PATTERN)
                        continue;
                        continue;
-               for (a = c->b_args; a != NULL; a = a->b_next) {
-                       if (f == NULL)
-                               except = f = expand(makeblock(NAME, a->b_name), E_ALL);
-                       else
-                               f->b_next = expand(makeblock(NAME, a->b_name), E_ALL);
-                       while (f->b_next != NULL)
-                               f = f->b_next;
+               for (nl = sc->sc_args; nl != NULL; nl = nl->n_next) {
+                       if (sc->sc_type == EXCEPT) {
+                               if (!strcmp(file, nl->n_name))
+                                       return(1);
+                               continue;
+                       }
+                       re_comp(nl->n_name);
+                       if (re_exec(file) > 0)
+                               return(1);
                }
        }
                }
        }
-       if (debug)
-               prnames(except);
-       return(1);
+       return(0);
 }
 
 char *
 }
 
 char *