use seteuid instead of setreuid
[unix-history] / usr / src / usr.bin / rdist / docmd.c
index 22a7938..4f0decf 100644 (file)
@@ -1,25 +1,37 @@
+/*
+ * Copyright (c) 1983 Regents of the University of California.
+ * All rights reserved.
+ *
+ * %sccs.include.redist.c%
+ */
+
 #ifndef lint
 #ifndef lint
-static char *sccsid = "@(#)docmd.c     4.26 (Berkeley) 85/03/05";
-#endif
+static char sccsid[] = "@(#)docmd.c    5.11 (Berkeley) %G%";
+#endif /* not lint */
 
 #include "defs.h"
 #include <setjmp.h>
 #include <netdb.h>
 
 
 #include "defs.h"
 #include <setjmp.h>
 #include <netdb.h>
 
-#ifndef RDIST
-#define RDIST "/usr/ucb/rdist"
-#endif
-
 FILE   *lfp;                   /* log file for recording files updated */
 struct subcmd *subcmds;        /* list of sub-commands for current cmd */
 jmp_buf        env;
 
 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();
+static int      makeconn __P((char *));
+static int      okname __P((char *));
+static void     closeconn __P((void));
+static void     cmptime __P((char *));
+static void     doarrow __P((char **,
+                   struct namelist *, char *, struct subcmd *));
+static void     dodcolon __P((char **,
+                   struct namelist *, char *, struct subcmd *));
+static void     notify __P((char *, char *, struct namelist *, time_t));
+static void     rcmptime __P((struct stat *));
 
 /*
  * Do the commands in cmds (initialized by yyparse).
  */
 
 /*
  * Do the commands in cmds (initialized by yyparse).
  */
+void
 docmds(dhosts, argc, argv)
        char **dhosts;
        int argc;
 docmds(dhosts, argc, argv)
        char **dhosts;
        int argc;
@@ -75,6 +87,7 @@ docmds(dhosts, argc, argv)
 /*
  * Process commands for sending files to other machines.
  */
 /*
  * Process commands for sending files to other machines.
  */
+static void
 doarrow(filev, files, rhost, cmds)
        char **filev;
        struct namelist *files;
 doarrow(filev, files, rhost, cmds)
        char **filev;
        struct namelist *files;
@@ -104,8 +117,8 @@ doarrow(filev, files, rhost, cmds)
                signal(SIGPIPE, lostconn);
                if (!makeconn(rhost))
                        return;
                signal(SIGPIPE, lostconn);
                if (!makeconn(rhost))
                        return;
-               if ((lfp = fopen(tmpfile, "w")) == NULL) {
-                       fatal("cannot open %s\n", tmpfile);
+               if ((lfp = fopen(tempfile, "w")) == NULL) {
+                       fatal("cannot open %s\n", tempfile);
                        exit(1);
                }
        }
                        exit(1);
                }
        }
@@ -139,9 +152,9 @@ done:
        }
        for (sc = cmds; sc != NULL; sc = sc->sc_next)
                if (sc->sc_type == NOTIFY)
        }
        for (sc = cmds; sc != NULL; sc = sc->sc_next)
                if (sc->sc_type == NOTIFY)
-                       notify(tmpfile, rhost, sc->sc_args, 0);
+                       notify(tempfile, rhost, sc->sc_args, 0);
        if (!nflag) {
        if (!nflag) {
-               (void) unlink(tmpfile);
+               (void) unlink(tempfile);
                for (; ihead != NULL; ihead = ihead->nextp) {
                        free(ihead);
                        if ((opts & IGNLNKS) || ihead->count == 0)
                for (; ihead != NULL; ihead = ihead->nextp) {
                        free(ihead);
                        if ((opts & IGNLNKS) || ihead->count == 0)
@@ -155,6 +168,7 @@ done:
 /*
  * Create a connection to the rdist server on the machine rhost.
  */
 /*
  * Create a connection to the rdist server on the machine rhost.
  */
+static int
 makeconn(rhost)
        char *rhost;
 {
 makeconn(rhost)
        char *rhost;
 {
@@ -192,7 +206,7 @@ makeconn(rhost)
                ruser = user;
        if (!qflag)
                printf("updating host %s\n", rhost);
                ruser = user;
        if (!qflag)
                printf("updating host %s\n", rhost);
-       (void) sprintf(buf, "%s -Server%s", RDIST, qflag ? " -q" : "");
+       (void) sprintf(buf, "%s -Server%s", _PATH_RDIST, qflag ? " -q" : "");
        if (port < 0) {
                struct servent *sp;
 
        if (port < 0) {
                struct servent *sp;
 
@@ -207,18 +221,18 @@ makeconn(rhost)
        }
 
        fflush(stdout);
        }
 
        fflush(stdout);
-       setreuid(userid, 0);
+       seteuid(0);
        rem = rcmd(&rhost, port, user, ruser, buf, 0);
        setreuid(0, userid);
        if (rem < 0)
                return(0);
        cp = buf;
        if (read(rem, cp, 1) != 1)
        rem = rcmd(&rhost, port, user, ruser, buf, 0);
        setreuid(0, userid);
        if (rem < 0)
                return(0);
        cp = buf;
        if (read(rem, cp, 1) != 1)
-               lostconn();
+               lostconn(0);
        if (*cp == 'V') {
                do {
                        if (read(rem, cp, 1) != 1)
        if (*cp == 'V') {
                do {
                        if (read(rem, cp, 1) != 1)
-                               lostconn();
+                               lostconn(0);
                } while (*cp++ != '\n' && cp < &buf[BUFSIZ]);
                *--cp = '\0';
                cp = buf;
                } while (*cp++ != '\n' && cp < &buf[BUFSIZ]);
                *--cp = '\0';
                cp = buf;
@@ -228,8 +242,13 @@ makeconn(rhost)
                if (*cp == '\0' && n == VERSION)
                        return(1);
                error("connection failed: version numbers don't match (local %d, remote %d)\n", VERSION, n);
                if (*cp == '\0' && n == VERSION)
                        return(1);
                error("connection failed: version numbers don't match (local %d, remote %d)\n", VERSION, n);
-       } else
+       } else {
                error("connection failed: version numbers don't match\n");
                error("connection failed: version numbers don't match\n");
+               error("got unexpected input:");
+               do {
+                       error("%c", *cp);
+               } while (*cp != '\n' && read(rem, cp, 1) == 1);
+       }
        closeconn();
        return(0);
 }
        closeconn();
        return(0);
 }
@@ -237,6 +256,7 @@ makeconn(rhost)
 /*
  * Signal end of previous connection.
  */
 /*
  * Signal end of previous connection.
  */
+static void
 closeconn()
 {
        if (debug)
 closeconn()
 {
        if (debug)
@@ -249,14 +269,17 @@ closeconn()
        }
 }
 
        }
 }
 
-lostconn()
+void
+lostconn(signo)
+       int signo;
 {
        if (iamremote)
 {
        if (iamremote)
-               cleanup();
+               cleanup(0);
        log(lfp, "rdist: lost connection\n");
        longjmp(env, 1);
 }
 
        log(lfp, "rdist: lost connection\n");
        longjmp(env, 1);
 }
 
+static int
 okname(name)
        register char *name;
 {
 okname(name)
        register char *name;
 {
@@ -284,6 +307,7 @@ extern      char target[], *tp;
 /*
  * Process commands for comparing files to time stamp files.
  */
 /*
  * Process commands for comparing files to time stamp files.
  */
+static void
 dodcolon(filev, files, stamp, cmds)
        char **filev;
        struct namelist *files;
 dodcolon(filev, files, stamp, cmds)
        char **filev;
        struct namelist *files;
@@ -305,19 +329,19 @@ dodcolon(filev, files, stamp, cmds)
                return;
        }
        if (stat(stamp, &stb) < 0) {
                return;
        }
        if (stat(stamp, &stb) < 0) {
-               error("%s: %s\n", stamp, sys_errlist[errno]);
+               error("%s: %s\n", stamp, strerror(errno));
                return;
        }
        if (debug)
                return;
        }
        if (debug)
-               printf("%s: %d\n", stamp, stb.st_mtime);
+               printf("%s: %ld\n", stamp, stb.st_mtime);
 
        subcmds = cmds;
        lastmod = stb.st_mtime;
        if (nflag || (options & VERIFY))
                tfp = NULL;
        else {
 
        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]);
+               if ((tfp = fopen(tempfile, "w")) == NULL) {
+                       error("%s: %s\n", stamp, strerror(errno));
                        return;
                }
                (void) gettimeofday(&tv[0], &tz);
                        return;
                }
                (void) gettimeofday(&tv[0], &tz);
@@ -341,14 +365,15 @@ dodcolon(filev, files, stamp, cmds)
                (void) fclose(tfp);
        for (sc = cmds; sc != NULL; sc = sc->sc_next)
                if (sc->sc_type == NOTIFY)
                (void) fclose(tfp);
        for (sc = cmds; sc != NULL; sc = sc->sc_next)
                if (sc->sc_type == NOTIFY)
-                       notify(tmpfile, NULL, sc->sc_args, lastmod);
+                       notify(tempfile, NULL, sc->sc_args, lastmod);
        if (!nflag && !(options & VERIFY))
        if (!nflag && !(options & VERIFY))
-               (void) unlink(tmpfile);
+               (void) unlink(tempfile);
 }
 
 /*
  * Compare the mtime of file to the list of time stamps.
  */
 }
 
 /*
  * Compare the mtime of file to the list of time stamps.
  */
+static void
 cmptime(name)
        char *name;
 {
 cmptime(name)
        char *name;
 {
@@ -376,7 +401,7 @@ cmptime(name)
                        tp++;
        }
        if (access(name, 4) < 0 || stat(name, &stb) < 0) {
                        tp++;
        }
        if (access(name, 4) < 0 || stat(name, &stb) < 0) {
-               error("%s: %s\n", name, sys_errlist[errno]);
+               error("%s: %s\n", name, strerror(errno));
                return;
        }
 
                return;
        }
 
@@ -397,6 +422,7 @@ cmptime(name)
                log(tfp, "new: %s\n", name);
 }
 
                log(tfp, "new: %s\n", name);
 }
 
+static void
 rcmptime(st)
        struct stat *st;
 {
 rcmptime(st)
        struct stat *st;
 {
@@ -410,7 +436,7 @@ rcmptime(st)
                printf("rcmptime(%x)\n", st);
 
        if ((d = opendir(target)) == NULL) {
                printf("rcmptime(%x)\n", st);
 
        if ((d = opendir(target)) == NULL) {
-               error("%s: %s\n", target, sys_errlist[errno]);
+               error("%s: %s\n", target, strerror(errno));
                return;
        }
        otp = tp;
                return;
        }
        otp = tp;
@@ -440,14 +466,15 @@ rcmptime(st)
  * rhost == NULL if we are mailing a list of changes compared to at time
  * stamp file.
  */
  * rhost == NULL if we are mailing a list of changes compared to at time
  * stamp file.
  */
+static void
 notify(file, rhost, to, lmod)
        char *file, *rhost;
        register struct namelist *to;
        time_t lmod;
 {
        register int fd, len;
 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;
        struct stat stb;
+       FILE *pf;
 
        if ((options & VERIFY) || to == NULL)
                return;
 
        if ((options & VERIFY) || to == NULL)
                return;
@@ -461,11 +488,11 @@ notify(file, rhost, to, lmod)
                return;
 
        if ((fd = open(file, 0)) < 0) {
                return;
 
        if ((fd = open(file, 0)) < 0) {
-               error("%s: %s\n", file, sys_errlist[errno]);
+               error("%s: %s\n", file, strerror(errno));
                return;
        }
        if (fstat(fd, &stb) < 0) {
                return;
        }
        if (fstat(fd, &stb) < 0) {
-               error("%s: %s\n", file, sys_errlist[errno]);
+               error("%s: %s\n", file, strerror(errno));
                (void) close(fd);
                return;
        }
                (void) close(fd);
                return;
        }
@@ -476,9 +503,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;
        }
@@ -516,6 +544,7 @@ notify(file, rhost, to, lmod)
 /*
  * Return true if name is in the list.
  */
 /*
  * Return true if name is in the list.
  */
+int
 inlist(list, file)
        struct namelist *list;
        char *file;
 inlist(list, file)
        struct namelist *list;
        char *file;
@@ -531,6 +560,7 @@ inlist(list, file)
 /*
  * Return TRUE if file is in the exception list.
  */
 /*
  * Return TRUE if file is in the exception list.
  */
+int
 except(file)
        char *file;
 {
 except(file)
        char *file;
 {