try setuid bits for -p again -- lose bits if chown/chgrp fails
authorKeith Bostic <bostic@ucbvax.Berkeley.EDU>
Fri, 8 Jun 1990 23:46:02 +0000 (15:46 -0800)
committerKeith Bostic <bostic@ucbvax.Berkeley.EDU>
Fri, 8 Jun 1990 23:46:02 +0000 (15:46 -0800)
SCCS-vsn: bin/cp/cp.c 5.18

usr/src/bin/cp/cp.c

index cc8b439..807f959 100644 (file)
@@ -15,7 +15,7 @@ char copyright[] =
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)cp.c       5.17 (Berkeley) %G%";
+static char sccsid[] = "@(#)cp.c       5.18 (Berkeley) %G%";
 #endif /* not lint */
 
 /*
 #endif /* not lint */
 
 /*
@@ -468,33 +468,24 @@ setfile(fs, fd)
 {
        static struct timeval tv[2];
 
 {
        static struct timeval tv[2];
 
+       fs->st_mode &= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
+
        tv[0].tv_sec = fs->st_atime;
        tv[1].tv_sec = fs->st_mtime;
        if (utimes(to.p_path, tv))
                error(to.p_path);
        /*
         * Changing the ownership probably won't succeed, unless we're root
        tv[0].tv_sec = fs->st_atime;
        tv[1].tv_sec = fs->st_mtime;
        if (utimes(to.p_path, tv))
                error(to.p_path);
        /*
         * Changing the ownership probably won't succeed, unless we're root
-        * or POSIX_CHOWN_RESTRICTED is not set.  Set uid before setting the
-        * mode; current BSD behavior is to remove all setuid bits on chown.
-        * If setuid, lose the bits if chown fails.
-        * If setgid, lose the bits if chgrp fails.
-        * If both, lose the bits if either fails.
+        * or POSIX_CHOWN_RESTRICTED is not set.  Set uid/gid before setting
+        * the mode; current BSD behavior is to remove all setuid bits on
+        * chown.  If chown fails, lose setuid/setgid bits.
         */
         */
-       if ((fd ?
-           fchown(fd, fs->st_uid, -1) : chown(to.p_path, fs->st_uid, -1))) {
-               if (errno != EPERM)
-                       error(to.p_path);
-               if (fs->st_mode & S_ISUID)
-                       fs->st_mode &= ~(S_ISUID|S_ISGID);
-       }
-       if ((fd ?
-           fchown(fd, -1, fs->st_gid) : chown(to.p_path, -1, fs->st_gid))) {
+       if (fd ? fchown(fd, fs->st_uid, fs->st_gid) :
+           chown(to.p_path, fs->st_uid, fs->st_gid)) {
                if (errno != EPERM)
                        error(to.p_path);
                if (errno != EPERM)
                        error(to.p_path);
-               if (fs->st_mode & S_ISGID)
-                       fs->st_mode &= ~(S_ISUID|S_ISGID);
+               fs->st_mode &= ~(S_ISUID|S_ISGID);
        }
        }
-       fs->st_mode &= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
        if (fd ? fchmod(fd, fs->st_mode) : chmod(to.p_path, fs->st_mode))
                error(to.p_path);
 }
        if (fd ? fchmod(fd, fs->st_mode) : chmod(to.p_path, fs->st_mode))
                error(to.p_path);
 }