add utimes call; protection fixes galore
[unix-history] / usr / src / sys / ufs / lfs / lfs_vnops.c
index 3aa255e..3265194 100644 (file)
@@ -1,4 +1,4 @@
-/*     lfs_vnops.c     4.51    83/02/20        */
+/*     lfs_vnops.c     4.54    83/03/31        */
 
 #include "../h/param.h"
 #include "../h/systm.h"
 
 #include "../h/param.h"
 #include "../h/systm.h"
@@ -597,16 +597,10 @@ chmod1(ip, mode)
        ip->i_mode &= ~07777;
        if (u.u_uid) {
                mode &= ~ISVTX;
        ip->i_mode &= ~07777;
        if (u.u_uid) {
                mode &= ~ISVTX;
-               if (u.u_gid == ip->i_gid)
-                       goto ok;
-               for (gp = u.u_groups; gp < &u.u_groups[NGROUPS]; gp++)
-                       if (*gp == ip->i_gid)
-                               goto ok;
-               mode &= ~ISGID;
-ok:
-               ;
+               if (!groupmember(ip->i_gid))
+                       mode &= ~ISGID;
 #ifdef MUSH
 #ifdef MUSH
-               if (u.u_quota->q_syflags & QF_UMASK && u.u_uid != 0 &&
+               if (u.u_quota->q_syflags & QF_UMASK && 
                    (ip->i_mode & IFMT) != IFCHR)
                        mode &= ~u.u_cmask;
 #endif
                    (ip->i_mode & IFMT) != IFCHR)
                        mode &= ~u.u_cmask;
 #endif
@@ -630,9 +624,9 @@ chown()
        } *uap;
 
        uap = (struct a *)u.u_ap;
        } *uap;
 
        uap = (struct a *)u.u_ap;
-       if (!suser() || (ip = owner(0)) == NULL)
+       if ((ip = owner(0)) == NULL)
                return;
                return;
-       chown1(ip, uap->uid, uap->gid);
+       u.u_error = chown1(ip, uap->uid, uap->gid);
        iput(ip);
 }
 
        iput(ip);
 }
 
@@ -658,10 +652,10 @@ fchown()
                return;
        }
        ip = fp->f_inode;
                return;
        }
        ip = fp->f_inode;
-       if (!suser())
+       if (ip->i_uid != u.u_uid && !suser())
                return;
        ilock(ip);
                return;
        ilock(ip);
-       chown1(ip, uap->uid, uap->gid);
+       u.u_error = chown1(ip, uap->uid, uap->gid);
        iunlock(ip);
 }
 
        iunlock(ip);
 }
 
@@ -675,7 +669,15 @@ chown1(ip, uid, gid)
 {
 #ifdef QUOTA
        register long change;
 {
 #ifdef QUOTA
        register long change;
+#endif
 
 
+       if (uid == -1)
+               uid = ip->i_uid;
+       if (gid == -1)
+               gid = ip->i_gid;
+       if (u.u_uid && ip->i_gid != gid && !groupmember(gid))
+               return (EPERM);
+#ifdef QUOTA
        /*
         * This doesn't allow for holes in files (which hopefully don't
         * happen often in files that we chown), and is not accurate anyway
        /*
         * This doesn't allow for holes in files (which hopefully don't
         * happen often in files that we chown), and is not accurate anyway
@@ -704,14 +706,8 @@ chown1(ip, uid, gid)
        (void)chkiq(ip->i_dev, ip, ip->i_uid, 1);
        dqrele(ip->i_dquot);
 #endif
        (void)chkiq(ip->i_dev, ip, ip->i_uid, 1);
        dqrele(ip->i_dquot);
 #endif
-       /*
-        * keep uid/gid's in sane range -- no err,
-        * so chown(file, uid, -1) will do something useful
-        */
-       if (uid >= 0 && uid <= 32767)   /* should have a constant */
-               ip->i_uid = uid;
-       if (gid >= 0 && gid <= 32767)   /* same here */
-               ip->i_gid = gid;
+       ip->i_uid = uid;
+       ip->i_gid = gid;
        ip->i_flag |= ICHG;
        if (u.u_ruid != 0)
                ip->i_mode &= ~(ISUID|ISGID);
        ip->i_flag |= ICHG;
        if (u.u_ruid != 0)
                ip->i_mode &= ~(ISUID|ISGID);
@@ -719,9 +715,12 @@ chown1(ip, uid, gid)
        ip->i_dquot = inoquota(ip);
        (void)chkdq(ip, change, 1);
        (void)chkiq(ip->i_dev, (struct inode *)NULL, uid, 1);
        ip->i_dquot = inoquota(ip);
        (void)chkdq(ip, change, 1);
        (void)chkiq(ip->i_dev, (struct inode *)NULL, uid, 1);
+       return (u.u_error);
 #endif
 #endif
+       return (0);
 }
 
 }
 
+#ifndef NOCOMPAT
 /*
  * Set IUPD and IACC times on file.
  * Can't set ICHG.
 /*
  * Set IUPD and IACC times on file.
  * Can't set ICHG.
@@ -731,15 +730,14 @@ outime()
        register struct a {
                char    *fname;
                time_t  *tptr;
        register struct a {
                char    *fname;
                time_t  *tptr;
-       } *uap;
+       } *uap = (struct a *)u.u_ap;
        register struct inode *ip;
        time_t tv[2];
        struct timeval tv0, tv1;
 
        register struct inode *ip;
        time_t tv[2];
        struct timeval tv0, tv1;
 
-       uap = (struct a *)u.u_ap;
        if ((ip = owner(1)) == NULL)
                return;
        if ((ip = owner(1)) == NULL)
                return;
-       u.u_error = copyin((caddr_t)uap->tptr, (caddr_t)tv, sizeof(tv));
+       u.u_error = copyin((caddr_t)uap->tptr, (caddr_t)tv, sizeof (tv));
        if (u.u_error == 0) {
                ip->i_flag |= IACC|IUPD|ICHG;
                tv0.tv_sec = tv[0]; tv0.tv_usec = 0;
        if (u.u_error == 0) {
                ip->i_flag |= IACC|IUPD|ICHG;
                tv0.tv_sec = tv[0]; tv0.tv_usec = 0;
@@ -748,6 +746,26 @@ outime()
        }
        iput(ip);
 }
        }
        iput(ip);
 }
+#endif
+
+utimes()
+{
+       register struct a {
+               char    *fname;
+               struct  timeval *tptr;
+       } *uap = (struct a *)u.u_ap;
+       register struct inode *ip;
+       struct timeval tv[2];
+
+       if ((ip = owner(1)) == NULL)
+               return;
+       u.u_error = copyin((caddr_t)uap->tptr, (caddr_t)tv, sizeof (tv));
+       if (u.u_error == 0) {
+               ip->i_flag |= IACC|IUPD|ICHG;
+               iupdat(ip, &tv[0], &tv[1], 0);
+       }
+       iput(ip);
+}
 
 /*
  * Flush any pending I/O.
 
 /*
  * Flush any pending I/O.
@@ -914,7 +932,7 @@ rename()
        int error = 0;
 
        uap = (struct a *)u.u_ap;
        int error = 0;
 
        uap = (struct a *)u.u_ap;
-       ip = namei(uchar, LOOKUP | LOCKPARENT, 0);
+       ip = namei(uchar, DELETE | LOCKPARENT, 0);
        if (ip == NULL)
                return;
        dp = u.u_pdir;
        if (ip == NULL)
                return;
        dp = u.u_pdir;
@@ -924,20 +942,23 @@ rename()
 
                d = &u.u_dent;
                /*
 
                d = &u.u_dent;
                /*
-                * Avoid "." and ".." for obvious reasons.
+                * Avoid ".", "..", and aliases of "." for obvious reasons.
                 */
                 */
-               if (d->d_name[0] == '.') {
-                       if (d->d_namlen == 1 ||
-                           (d->d_namlen == 2 && d->d_name[1] == '.')) {
+               if ((d->d_namlen == 1 && d->d_name[0] == '.') ||
+                   (d->d_namlen == 2 && bcmp(d->d_name, "..", 2) == 0) ||
+                   (dp == ip)) {
+                       iput(dp);
+                       if (dp == ip)
+                               irele(ip);
+                       else
                                iput(ip);
                                iput(ip);
-                               u.u_error = EINVAL;
-                               return;
-                       }
+                       u.u_error = EINVAL;
+                       return;
                }
                oldparent = dp->i_number;
                doingdirectory++;
        }
                }
                oldparent = dp->i_number;
                doingdirectory++;
        }
-       irele(dp);
+       iput(dp);
 
        /*
         * 1) Bump link count while we're moving stuff
 
        /*
         * 1) Bump link count while we're moving stuff
@@ -961,6 +982,13 @@ rename()
                goto out;
        }
        dp = u.u_pdir;
                goto out;
        }
        dp = u.u_pdir;
+       /*
+        * If ".." must be changed (ie the directory gets a new
+        * parent) then the user must have write permission.
+        */
+       parentdifferent = oldparent != dp->i_number;
+       if (doingdirectory && parentdifferent && access(ip, IWRITE))
+               goto bad;
        /*
         * 2) If target doesn't exist, link the target
         *    to the source and unlink the source. 
        /*
         * 2) If target doesn't exist, link the target
         *    to the source and unlink the source. 
@@ -968,7 +996,6 @@ rename()
         *    entry to reference the source inode and
         *    expunge the original entry's existence.
         */
         *    entry to reference the source inode and
         *    expunge the original entry's existence.
         */
-       parentdifferent = oldparent != dp->i_number;
        if (xp == NULL) {
                if (dp->i_dev != ip->i_dev) {
                        error = EXDEV;
        if (xp == NULL) {
                if (dp->i_dev != ip->i_dev) {
                        error = EXDEV;
@@ -1173,6 +1200,8 @@ maknode(mode)
        ip->i_nlink = 1;
        ip->i_uid = u.u_uid;
        ip->i_gid = u.u_pdir->i_gid;
        ip->i_nlink = 1;
        ip->i_uid = u.u_uid;
        ip->i_gid = u.u_pdir->i_gid;
+       if (ip->i_mode & ISGID && !groupmember(ip->i_gid))
+               ip->i_mode &= ~ISGID;
 #ifdef QUOTA
        ip->i_dquot = inoquota(ip);
 #endif
 #ifdef QUOTA
        ip->i_dquot = inoquota(ip);
 #endif