don't redefine _PATH_TMP; don't assume it begins "/tmp/";
[unix-history] / usr / src / usr.bin / rdist / server.c
index 6f44383..534bf09 100644 (file)
@@ -2,16 +2,11 @@
  * Copyright (c) 1983 Regents of the University of California.
  * All rights reserved.
  *
  * Copyright (c) 1983 Regents of the University of California.
  * All rights reserved.
  *
- * Redistribution and use in source and binary forms are permitted
- * provided that this notice is preserved and that due credit is given
- * to the University of California at 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'' without express or implied warranty.
+ * %sccs.include.redist.c%
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)server.c   5.6 (Berkeley) %G%";
+static char sccsid[] = "@(#)server.c   5.13 (Berkeley) %G%";
 #endif /* not lint */
 
 #include "defs.h"
 #endif /* not lint */
 
 #include "defs.h"
@@ -278,7 +273,7 @@ sendf(rname, opts)
        if (except(target))
                return;
        if ((opts & FOLLOW ? stat(target, &stb) : lstat(target, &stb)) < 0) {
        if (except(target))
                return;
        if ((opts & FOLLOW ? stat(target, &stb) : lstat(target, &stb)) < 0) {
-               error("%s: %s\n", target, sys_errlist[errno]);
+               error("%s: %s\n", target, strerror(errno));
                return;
        }
        if ((u = update(rname, opts, &stb)) == 0) {
                return;
        }
        if ((u = update(rname, opts, &stb)) == 0) {
@@ -313,7 +308,7 @@ sendf(rname, opts)
        switch (stb.st_mode & S_IFMT) {
        case S_IFDIR:
                if ((d = opendir(target)) == NULL) {
        switch (stb.st_mode & S_IFMT) {
        case S_IFDIR:
                if ((d = opendir(target)) == NULL) {
-                       error("%s: %s\n", target, sys_errlist[errno]);
+                       error("%s: %s\n", target, strerror(errno));
                        return;
                }
                (void) sprintf(buf, "D%o %04o 0 0 %s %s %s\n", opts,
                        return;
                }
                (void) sprintf(buf, "D%o %04o 0 0 %s %s %s\n", opts,
@@ -426,7 +421,7 @@ sendf(rname, opts)
        }
 
        if ((f = open(target, 0)) < 0) {
        }
 
        if ((f = open(target, 0)) < 0) {
-               error("%s: %s\n", target, sys_errlist[errno]);
+               error("%s: %s\n", target, strerror(errno));
                return;
        }
        (void) sprintf(buf, "R%o %o %ld %ld %s %s %s\n", opts,
                return;
        }
        (void) sprintf(buf, "R%o %o %ld %ld %s %s %s\n", opts,
@@ -622,7 +617,7 @@ query(name)
                if (errno == ENOENT)
                        (void) write(rem, "N\n", 2);
                else
                if (errno == ENOENT)
                        (void) write(rem, "N\n", 2);
                else
-                       error("%s:%s: %s\n", host, target, sys_errlist[errno]);
+                       error("%s:%s: %s\n", host, target, strerror(errno));
                *tp = '\0';
                return;
        }
                *tp = '\0';
                return;
        }
@@ -657,7 +652,7 @@ recvf(cmd, type)
        struct timeval tvp[2];
        char *owner, *group;
        char new[BUFSIZ];
        struct timeval tvp[2];
        char *owner, *group;
        char new[BUFSIZ];
-       extern char *tmpname;
+       extern char *tempname;
 
        cp = cmd;
        opts = 0;
 
        cp = cmd;
        opts = 0;
@@ -742,7 +737,7 @@ recvf(cmd, type)
                                ack();
                        return;
                }
                                ack();
                        return;
                }
-               error("%s:%s: %s\n", host, target, sys_errlist[errno]);
+               error("%s:%s: %s\n", host, target, strerror(errno));
                tp = stp[--catname];
                *tp = '\0';
                return;
                tp = stp[--catname];
                *tp = '\0';
                return;
@@ -752,12 +747,12 @@ recvf(cmd, type)
                (void) sprintf(tp, "/%s", cp);
        cp = rindex(target, '/');
        if (cp == NULL)
                (void) sprintf(tp, "/%s", cp);
        cp = rindex(target, '/');
        if (cp == NULL)
-               strcpy(new, tmpname);
+               strcpy(new, tempname);
        else if (cp == target)
        else if (cp == target)
-               (void) sprintf(new, "/%s", tmpname);
+               (void) sprintf(new, "/%s", tempname);
        else {
                *cp = '\0';
        else {
                *cp = '\0';
-               (void) sprintf(new, "%s/%s", target, tmpname);
+               (void) sprintf(new, "%s/%s", target, tempname);
                *cp = '/';
        }
 
                *cp = '/';
        }
 
@@ -837,7 +832,7 @@ recvf(cmd, type)
                return;
        }
        if (wrerr) {
                return;
        }
        if (wrerr) {
-               error("%s:%s: %s\n", host, new, sys_errlist[olderrno]);
+               error("%s:%s: %s\n", host, new, strerror(errno));
                (void) unlink(new);
                return;
        }
                (void) unlink(new);
                return;
        }
@@ -849,7 +844,7 @@ recvf(cmd, type)
                        goto badt;
                if ((f2 = fopen(new, "r")) == NULL) {
                badn:
                        goto badt;
                if ((f2 = fopen(new, "r")) == NULL) {
                badn:
-                       error("%s:%s: %s\n", host, new, sys_errlist[errno]);
+                       error("%s:%s: %s\n", host, new, strerror(errno));
                        (void) unlink(new);
                        return;
                }
                        (void) unlink(new);
                        return;
                }
@@ -881,7 +876,7 @@ recvf(cmd, type)
        tvp[1].tv_sec = mtime;
        tvp[1].tv_usec = 0;
        if (utimes(new, tvp) < 0) {
        tvp[1].tv_sec = mtime;
        tvp[1].tv_usec = 0;
        if (utimes(new, tvp) < 0) {
-               note("%s:utimes failed %s: %s\n", host, new, sys_errlist[errno]);
+               note("%s:utimes failed %s: %s\n", host, new, strerror(errno));
        }
        if (chog(new, owner, group, mode) < 0) {
                (void) unlink(new);
        }
        if (chog(new, owner, group, mode) < 0) {
                (void) unlink(new);
@@ -890,7 +885,7 @@ recvf(cmd, type)
 fixup:
        if (rename(new, target) < 0) {
 badt:
 fixup:
        if (rename(new, target) < 0) {
 badt:
-               error("%s:%s: %s\n", host, target, sys_errlist[errno]);
+               error("%s:%s: %s\n", host, target, strerror(errno));
                (void) unlink(new);
                return;
        }
                (void) unlink(new);
                return;
        }
@@ -943,12 +938,12 @@ hardlink(cmd)
        }
        if (chkparent(target) < 0 ) {
                error("%s:%s: %s (no parent)\n",
        }
        if (chkparent(target) < 0 ) {
                error("%s:%s: %s (no parent)\n",
-                       host, target, sys_errlist[errno]);
+                       host, target, strerror(errno));
                return;
        }
        if (exists && (unlink(target) < 0)) {
                error("%s:%s: %s (unlink)\n",
                return;
        }
        if (exists && (unlink(target) < 0)) {
                error("%s:%s: %s (unlink)\n",
-                       host, target, sys_errlist[errno]);
+                       host, target, strerror(errno));
                return;
        }
        if (link(oldname, target) < 0) {
                return;
        }
        if (link(oldname, target) < 0) {
@@ -1043,9 +1038,9 @@ ok:
        if (userid)
                setreuid(userid, 0);
        if (chown(file, uid, gid) < 0 ||
        if (userid)
                setreuid(userid, 0);
        if (chown(file, uid, gid) < 0 ||
-           (mode & 06000) && chmod(file, mode) < 0) {
+           (mode & 07000) && chmod(file, mode) < 0) {
                note("%s: chown or chmod failed: file %s:  %s",
                note("%s: chown or chmod failed: file %s:  %s",
-                            host, file, sys_errlist[errno]);
+                            host, file, strerror(errno));
        }
        if (userid)
                setreuid(0, userid);
        }
        if (userid)
                setreuid(0, userid);
@@ -1154,7 +1149,7 @@ clean(cp)
                return;
        }
        if ((d = opendir(target)) == NULL) {
                return;
        }
        if ((d = opendir(target)) == NULL) {
-               error("%s:%s: %s\n", host, target, sys_errlist[errno]);
+               error("%s:%s: %s\n", host, target, strerror(errno));
                return;
        }
        ack();
                return;
        }
        ack();
@@ -1176,7 +1171,7 @@ clean(cp)
                        ;
                tp--;
                if (lstat(target, &stb) < 0) {
                        ;
                tp--;
                if (lstat(target, &stb) < 0) {
-                       error("%s:%s: %s\n", host, target, sys_errlist[errno]);
+                       error("%s:%s: %s\n", host, target, strerror(errno));
                        continue;
                }
                (void) sprintf(buf, "Q%s\n", dp->d_name);
                        continue;
                }
                (void) sprintf(buf, "Q%s\n", dp->d_name);
@@ -1254,7 +1249,7 @@ remove(stp)
                        ;
                tp--;
                if (lstat(target, &stb) < 0) {
                        ;
                tp--;
                if (lstat(target, &stb) < 0) {
-                       error("%s:%s: %s\n", host, target, sys_errlist[errno]);
+                       error("%s:%s: %s\n", host, target, strerror(errno));
                        continue;
                }
                remove(&stb);
                        continue;
                }
                remove(&stb);
@@ -1264,7 +1259,7 @@ remove(stp)
        *tp = '\0';
        if (rmdir(target) < 0) {
 bad:
        *tp = '\0';
        if (rmdir(target) < 0) {
 bad:
-               error("%s:%s: %s\n", host, target, sys_errlist[errno]);
+               error("%s:%s: %s\n", host, target, strerror(errno));
                return;
        }
 removed:
                return;
        }
 removed:
@@ -1286,7 +1281,7 @@ dospecial(cmd)
        extern int userid, groupid;
 
        if (pipe(fd) < 0) {
        extern int userid, groupid;
 
        if (pipe(fd) < 0) {
-               error("%s\n", sys_errlist[errno]);
+               error("%s\n", strerror(errno));
                return;
        }
        if ((pid = fork()) == 0) {
                return;
        }
        if ((pid = fork()) == 0) {
@@ -1296,14 +1291,14 @@ dospecial(cmd)
                (void) close(0);
                (void) close(1);
                (void) close(2);
                (void) close(0);
                (void) close(1);
                (void) close(2);
-               (void) open("/dev/null", 0);
+               (void) open(_PATH_DEVNULL, O_RDONLY);
                (void) dup(fd[1]);
                (void) dup(fd[1]);
                (void) close(fd[0]);
                (void) close(fd[1]);
                setgid(groupid);
                setuid(userid);
                (void) dup(fd[1]);
                (void) dup(fd[1]);
                (void) close(fd[0]);
                (void) close(fd[1]);
                setgid(groupid);
                setuid(userid);
-               execl("/bin/sh", "sh", "-c", cmd, 0);
+               execl(_PATH_BSHELL, "sh", "-c", cmd, 0);
                _exit(127);
        }
        (void) close(fd[1]);
                _exit(127);
        }
        (void) close(fd[1]);
@@ -1364,16 +1359,27 @@ error(fmt, a1, a2, a3)
        char *fmt;
        int a1, a2, a3;
 {
        char *fmt;
        int a1, a2, a3;
 {
-       nerrs++;
-       strcpy(buf, "\1rdist: ");
-       (void) sprintf(buf+8, fmt, a1, a2, a3);
-       if (!iamremote) {
+       static FILE *fp;
+
+       ++nerrs;
+       if (!fp && !(fp = fdopen(rem, "w")))
+               return;
+       if (iamremote) {
+               (void)fprintf(fp, "%crdist: ", 0x01);
+               (void)fprintf(fp, fmt, a1, a2, a3);
+               fflush(fp);
+       }
+       else {
                fflush(stdout);
                fflush(stdout);
-               (void) write(2, buf+1, strlen(buf+1));
-       } else
-               (void) write(rem, buf, strlen(buf));
-       if (lfp != NULL)
-               (void) fwrite(buf+1, 1, strlen(buf+1), lfp);
+               (void)fprintf(stderr, "rdist: ");
+               (void)fprintf(stderr, fmt, a1, a2, a3);
+               fflush(stderr);
+       }
+       if (lfp != NULL) {
+               (void)fprintf(lfp, "rdist: ");
+               (void)fprintf(lfp, fmt, a1, a2, a3);
+               fflush(lfp);
+       }
 }
 
 /*VARARGS1*/
 }
 
 /*VARARGS1*/
@@ -1381,16 +1387,27 @@ fatal(fmt, a1, a2,a3)
        char *fmt;
        int a1, a2, a3;
 {
        char *fmt;
        int a1, a2, a3;
 {
-       nerrs++;
-       strcpy(buf, "\2rdist: ");
-       (void) sprintf(buf+8, fmt, a1, a2, a3);
-       if (!iamremote) {
+       static FILE *fp;
+
+       ++nerrs;
+       if (!fp && !(fp = fdopen(rem, "w")))
+               return;
+       if (iamremote) {
+               (void)fprintf(fp, "%crdist: ", 0x02);
+               (void)fprintf(fp, fmt, a1, a2, a3);
+               fflush(fp);
+       }
+       else {
                fflush(stdout);
                fflush(stdout);
-               (void) write(2, buf+1, strlen(buf+1));
-       } else
-               (void) write(rem, buf, strlen(buf));
-       if (lfp != NULL)
-               (void) fwrite(buf+1, 1, strlen(buf+1), lfp);
+               (void)fprintf(stderr, "rdist: ");
+               (void)fprintf(stderr, fmt, a1, a2, a3);
+               fflush(stderr);
+       }
+       if (lfp != NULL) {
+               (void)fprintf(lfp, "rdist: ");
+               (void)fprintf(lfp, fmt, a1, a2, a3);
+               fflush(lfp);
+       }
        cleanup();
 }
 
        cleanup();
 }
 
@@ -1446,7 +1463,7 @@ response()
  */
 cleanup()
 {
  */
 cleanup()
 {
-       (void) unlink(tmpfile);
+       (void) unlink(tempfile);
        exit(1);
 }
 
        exit(1);
 }