123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552 |
- #ifndef lint
- static char *sccsid = "@(#)docmd.c 4.21 (Berkeley) 84/06/28";
- #endif
- #include "defs.h"
- #include <setjmp.h>
- 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(argc, argv)
- 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 (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.
- */
- doarrow(filev, files, rhost, cmds)
- char **filev;
- struct namelist *files;
- char *rhost;
- struct subcmd *cmds;
- {
- register struct namelist *f;
- register struct subcmd *sc;
- register char **cpp;
- int n, ddir, opts = options;
- if (debug)
- printf("doarrow(%x, %s, %x)\n", files, rhost, cmds);
- if (files == NULL) {
- error("no files to be updated\n");
- return;
- }
- 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->n_next) {
- if (filev) {
- for (cpp = filev; *cpp; cpp++)
- if (strcmp(f->n_name, *cpp) == 0)
- goto found;
- if (!nflag)
- (void) fclose(lfp);
- continue;
- }
- 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;
- }
- if (n == 0)
- install(f->n_name, NULL, 0, options);
- }
- done:
- if (!nflag) {
- (void) signal(SIGPIPE, cleanup);
- (void) fclose(lfp);
- lfp = NULL;
- }
- /*++23-Nov-84++*/
- /*
- * scan for make commands which have to be executed.
- */
- for (sc = cmds; sc != NULL; sc = sc->sc_next)
- if ( sc->sc_type == DEPEND )
- dependcmd(sc->sc_name, sc->sc_options, sc->sc_args);
- /*--23-Nov-84--*/
- 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);
- for (; ihead != NULL; ihead = ihead->nextp) {
- free(ihead);
- if ((opts & IGNLNKS) || ihead->count == 0)
- continue;
- log(lfp, "%s: Warning: missing links\n",
- ihead->pathname);
- }
- }
- }
- /*
- * Create a connection to the rdist server on the machine rhost.
- */
- makeconn(rhost)
- char *rhost;
- {
- register char *ruser, *cp;
- static char *cur_host = NULL;
- int n;
- extern char user[];
- if (debug)
- printf("makeconn(%s)\n", rhost);
- if (cur_host != NULL && rem >= 0) {
- if (strcmp(cur_host, rhost) == 0)
- return(1);
- closeconn();
- }
- ruser = rindex(rhost, '.');
- if (ruser != NULL) {
- *ruser++ = '\0';
- if (!okname(ruser))
- return(0);
- } else
- ruser = user;
- if (!qflag)
- printf("updating host %s\n", rhost);
- (void) sprintf(buf, "/usr/local/rdist -Server%s", qflag ? " -q" : "");
- if (debug) {
- printf("luser = %s, ruser = %s\n", user, ruser);
- printf("buf = %s\n", buf);
- }
- fflush(stdout);
- cur_host = rhost;
- rem = rcmd(&rhost, IPPORT_CMDSERVER, user, ruser, buf, 0);
- if (rem < 0)
- return(0);
- 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)
- register char *name;
- {
- register char *cp = name;
- register int c;
- do {
- c = *cp;
- if (c & 0200)
- goto bad;
- if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-')
- goto bad;
- cp++;
- } while (*cp);
- return(1);
- bad:
- error("invalid user name %s\n", name);
- return(0);
- }
- time_t lastmod;
- FILE *tfp;
- extern char target[], *tp;
- /*
- * Process commands for comparing files to time stamp files.
- */
- dodcolon(filev, files, stamp, cmds)
- char **filev;
- struct namelist *files;
- char *stamp;
- struct subcmd *cmds;
- {
- register struct subcmd *sc;
- register struct namelist *f;
- register char **cpp;
- struct timeval tv[2];
- struct timezone tz;
- struct stat stb;
- if (debug)
- printf("dodcolon()\n");
- if (files == NULL) {
- error("no files to be updated\n");
- return;
- }
- if (stat(stamp, &stb) < 0) {
- error("%s: %s\n", stamp, sys_errlist[errno]);
- return;
- }
- if (debug)
- 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;
- }
- (void) gettimeofday(&tv[0], &tz);
- tv[1] = tv[0];
- (void) utimes(stamp, tv);
- }
- 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;
- continue;
- }
- found:
- tp = NULL;
- cmptime(f->n_name);
- }
- if (tfp != NULL)
- (void) fclose(tfp);
- 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);
- }
- /*
- * Compare the mtime of file to the list of time stamps.
- */
- cmptime(name)
- char *name;
- {
- struct stat stb;
- if (debug)
- printf("cmptime(%s)\n", name);
- if (except(name))
- return;
- if (nflag) {
- printf("comparing dates: %s\n", name);
- return;
- }
- /*
- * first time cmptime() is called?
- */
- if (tp == NULL) {
- if (exptilde(target, name) == NULL)
- return;
- tp = name = target;
- while (*tp)
- tp++;
- }
- if (access(name, 4) < 0 || stat(name, &stb) < 0) {
- error("%s: %s\n", name, sys_errlist[errno]);
- return;
- }
- switch (stb.st_mode & S_IFMT) {
- case S_IFREG:
- break;
- case S_IFDIR:
- rcmptime(&stb);
- return;
- default:
- error("%s: not a plain file\n", name);
- return;
- }
- if (stb.st_mtime > lastmod)
- log(tfp, "new: %s\n", name);
- }
- rcmptime(st)
- struct stat *st;
- {
- register DIR *d;
- register struct direct *dp;
- register char *cp;
- char *otp;
- int len;
- if (debug)
- printf("rcmptime(%x)\n", st);
- if ((d = opendir(target)) == NULL) {
- error("%s: %s\n", target, sys_errlist[errno]);
- return;
- }
- otp = tp;
- len = tp - target;
- while (dp = readdir(d)) {
- if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
- continue;
- if (len + 1 + strlen(dp->d_name) >= BUFSIZ - 1) {
- error("%s/%s: Name too long\n", target, dp->d_name);
- continue;
- }
- tp = otp;
- *tp++ = '/';
- cp = dp->d_name;
- while (*tp++ = *cp++)
- ;
- tp--;
- cmptime(target);
- }
- closedir(d);
- tp = otp;
- *tp = '\0';
- }
- /*
- * Notify the list of people the changes that were made.
- * rhost == NULL if we are mailing a list of changes compared to at time
- * stamp file.
- */
- notify(file, rhost, to, lmod)
- char *file, *rhost;
- register struct namelist *to;
- time_t lmod;
- {
- register int fd, len;
- FILE *pf, *popen();
- struct stat stb;
- if ((options & VERIFY) || to == NULL)
- return;
- if (!qflag) {
- printf("notify ");
- if (rhost)
- printf("@%s ", rhost);
- prnames(to);
- }
- if (nflag)
- return;
- if ((fd = open(file, 0)) < 0) {
- error("%s: %s\n", file, sys_errlist[errno]);
- return;
- }
- if (fstat(fd, &stb) < 0) {
- error("%s: %s\n", file, sys_errlist[errno]);
- (void) close(fd);
- return;
- }
- if (stb.st_size == 0) {
- (void) close(fd);
- return;
- }
- /*
- * Create a pipe to mailling program.
- */
- pf = popen(MAILCMD, "w");
- if (pf == NULL) {
- error("notify: \"%s\" failed\n", MAILCMD);
- (void) close(fd);
- return;
- }
- /*
- * Output the proper header information.
- */
- fprintf(pf, "From: rdist (Remote distribution program)\n");
- fprintf(pf, "To:");
- if (!any('@', to->n_name) && rhost != NULL)
- fprintf(pf, " %s@%s", to->n_name, rhost);
- else
- fprintf(pf, " %s", to->n_name);
- to = to->n_next;
- while (to != NULL) {
- if (!any('@', to->n_name) && rhost != NULL)
- fprintf(pf, ", %s@%s", to->n_name, rhost);
- else
- fprintf(pf, ", %s", to->n_name);
- to = to->n_next;
- }
- putc('\n', pf);
- if (rhost != NULL)
- fprintf(pf, "Subject: files updated by rdist from %s to %s\n",
- host, rhost);
- else
- fprintf(pf, "Subject: files updated after %s\n", ctime(&lmod));
- putc('\n', pf);
- while ((len = read(fd, buf, BUFSIZ)) > 0)
- (void) fwrite(buf, 1, len, pf);
- (void) close(fd);
- (void) pclose(pf);
- }
- /*
- * Return true if name is in the list.
- */
- inlist(list, file)
- struct namelist *list;
- char *file;
- {
- register struct namelist *nl;
- for (nl = list; nl != NULL; nl = nl->n_next)
- if (!strcmp(file, nl->n_name))
- return(1);
- return(0);
- }
- /*
- * Return TRUE if file is in the exception list.
- */
- except(file)
- char *file;
- {
- register struct subcmd *sc;
- register struct namelist *nl;
- if (debug)
- printf("except(%s)\n", file);
- for (sc = subcmds; sc != NULL; sc = sc->sc_next) {
- if (sc->sc_type != EXCEPT && sc->sc_type != PATTERN)
- continue;
- 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);
- }
- }
- return(0);
- }
- char *
- colon(cp)
- register char *cp;
- {
- while (*cp) {
- if (*cp == ':')
- return(cp);
- if (*cp == '/')
- return(0);
- cp++;
- }
- return(0);
- }
|