]> arthur.barton.de Git - netatalk.git/blobdiff - etc/afpd/unix.c
- better error reporting, don't always return noobj.
[netatalk.git] / etc / afpd / unix.c
index c5459c1612aa84c95ce9f586cb37d30424e58de3..fa0ee0a722f0a37abe731603cd03e25c486f27e6 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: unix.c,v 1.35 2002-06-06 10:14:26 didg Exp $
+ * $Id: unix.c,v 1.41 2003-01-08 15:01:36 didg Exp $
  *
  * Copyright (c) 1990,1993 Regents of The University of Michigan.
  * All Rights Reserved.  See COPYRIGHT.
@@ -20,6 +20,7 @@
 #include <netatalk/endian.h>
 #include <dirent.h>
 #include <limits.h>
+#include <atalk/adouble.h>
 #include <atalk/afp.h>
 
 /* STDC check */
@@ -121,10 +122,9 @@ void utommode( stat, ma )
 struct stat            *stat;
 struct maccess *ma;
 {
-    mode_t             mode;
+mode_t mode;
 
     mode = stat->st_mode;
-
     ma->ma_world = utombits( mode );
     mode = mode >> 3;
 
@@ -133,15 +133,18 @@ struct maccess    *ma;
 
     ma->ma_owner = utombits( mode );
 
-    /* ma_user is a union of all permissions */
-
+    /* ma_user is a union of all permissions but we must follow
+     * unix perm
+    */
     if ( (uuid == stat->st_uid) || (uuid == 0)) {
         ma->ma_user = ma->ma_owner | AR_UOWN;
     }
-    if ( gmem( stat->st_gid )) {
-        ma->ma_user |= ma->ma_group;
-    } 
-    ma->ma_user |= ma->ma_world;
+    else if ( gmem( stat->st_gid )) {
+        ma->ma_user = ma->ma_group;
+    }
+    else {
+        ma->ma_user = ma->ma_world;
+    }
 
     /*
      * There are certain things the mac won't try if you don't have
@@ -164,16 +167,24 @@ struct maccess    *ma;
  *
  * Note: the previous method, using access(), does not work correctly
  * over NFS.
+ * FIXME what about ACL?
  */
-void accessmode( path, ma, dir )
+void accessmode( path, ma, dir, st )
 char           *path;
 struct maccess *ma;
-struct dir             *dir;
+struct dir     *dir;
+struct stat     *st;
+
 {
-    struct stat sb;
-    ma->ma_user = ma->ma_owner = 0;
-    if ( stat( path, &sb ) == 0 )
-        utommode( &sb, ma );
+struct stat     sb;
+
+    ma->ma_user = ma->ma_owner = ma->ma_world = ma->ma_group = 0;
+    if (!st) {
+        if (stat(path, &sb) != 0)
+            return;
+        st = &sb;
+    }
+    utommode( st, ma );
     return;
 }
 
@@ -227,53 +238,52 @@ struct maccess    *ma;
     return( mode );
 }
 
-int stickydirmode(name, mode, dropbox)
+/*
+   a dropbox is a folder where w is set but not r eg:
+   rwx-wx-wx or rwx-wx-- 
+   rwx----wx (is not asked by a Mac with OS >= 8.0 ?)
+*/
+static int stickydirmode(name, mode, dropbox)
 char * name;
 const mode_t mode;
 const int dropbox;
 {
-    int retval;
-#ifdef DROPKLUDGE
-    int uid;
-#endif /* DROPKLUDGE */
+    int retval = 0;
 
-    /* Turn on the sticky bit if this is a drop box, also turn off the setgid bit */
-    retval=0;
 #ifdef DROPKLUDGE
+    /* Turn on the sticky bit if this is a drop box, also turn off the setgid bit */
     if (dropbox) {
-        if (mode & S_IWOTH) {
-            if (mode & S_IROTH);
-            else { /* if S_IWOTH and not S_IROTH */
-                uid=geteuid();
-                if ( seteuid(0) < 0) {
-                    LOG(log_error, logtype_afpd, "stickydirmode: unable to seteuid root: %s", strerror(errno));
-                }
-                if ( retval=chmod( name, ( (DIRBITS | mode | S_ISVTX) & ~default_options.umask) ) < 0) {
-                    LOG(log_error, logtype_afpd, "stickydirmode: chmod \"%s\": %s", name, strerror(errno) );
-                    return(AFPERR_ACCESS);
-                } else {
+        int uid;
+
+        if ( ( (mode & S_IWOTH) && !(mode & S_IROTH)) ||
+             ( (mode & S_IWGRP) && !(mode & S_IRGRP)) )
+        {
+            uid=geteuid();
+            if ( seteuid(0) < 0) {
+                LOG(log_error, logtype_afpd, "stickydirmode: unable to seteuid root: %s", strerror(errno));
+            }
+            if ( (retval=chmod( name, ( (DIRBITS | mode | S_ISVTX) & ~default_options.umask) )) < 0) {
+                LOG(log_error, logtype_afpd, "stickydirmode: chmod \"%s\": %s", name, strerror(errno) );
+            } else {
 #ifdef DEBUG
-                    LOG(log_info, logtype_afpd, "stickydirmode: (debug) chmod \"%s\": %s", name, strerror(retval) );
+                LOG(log_info, logtype_afpd, "stickydirmode: (debug) chmod \"%s\": %s", name, strerror(retval) );
 #endif /* DEBUG */
-                    seteuid(uid);
-                } /* end getting retval */
-            } /* end if not & S_IROTH */
-        } else { /* end if S_IWOTH and not S_IROTH */
-#endif /* DROPKLUDGE */
-
-            /*
-            *  Ignore EPERM errors:  We may be dealing with a directory that is
-            *  group writable, in which case chmod will fail.
-            */
-            if ( (chmod( name, (DIRBITS | mode) & ~default_options.umask ) < 0) && errno != EPERM)  {
-                LOG(log_error, logtype_afpd, "stickydirmode: chmod \"%s\": %s",
-                    name, strerror(errno) );
-                retval = -1;
             }
-#ifdef DROPKLUDGE
-        } /* end if not mode */
-    } /* end checking for "dropbox" */
+            seteuid(uid);
+            return retval;
+        }
+    }
 #endif /* DROPKLUDGE */
+
+    /*
+     *  Ignore EPERM errors:  We may be dealing with a directory that is
+     *  group writable, in which case chmod will fail.
+     */
+    if ( (chmod( name, (DIRBITS | mode) & ~default_options.umask ) < 0) && errno != EPERM)  {
+        LOG(log_error, logtype_afpd, "stickydirmode: chmod \"%s\": %s",name, strerror(errno) );
+        retval = -1;
+    }
+
     return retval;
 }
 
@@ -353,6 +363,27 @@ const mode_t       mode;
     return( 0 );
 }
 
+int setfilmode(name, mode, st)
+char * name;
+mode_t mode;
+struct stat *st;
+{
+struct stat sb;
+mode_t mask = S_IRUSR |S_IWUSR | S_IRGRP | S_IWGRP |S_IROTH | S_IWOTH;
+
+    if (!st) {
+        if (stat(name, &sb) != 0)
+            return -1;
+        st = &sb;
+    }
+   mode &= mask;       /* keep only rw-rw-rw in mode */
+   mode |= st->st_mode & ~mask; /* keep other bits from previous mode */
+   if ( chmod( name,  mode & ~default_options.umask ) < 0 && errno != EPERM ) {
+       return -1;
+   }
+   return 0;
+}
+
 int setdirmode( mode, noadouble, dropbox )
 const mode_t mode;
 const int noadouble;
@@ -363,7 +394,7 @@ const int dropbox;
     char               *m;
     struct dirent      *dirp;
     DIR                        *dir;
-
+    
     if (( dir = opendir( "." )) == NULL ) {
         LOG(log_error, logtype_afpd, "setdirmode: opendir .: %s", strerror(errno) );
         return( -1 );
@@ -379,16 +410,27 @@ const int dropbox;
             continue;
         }
 
-        if (S_ISREG(st.st_mode)) {
+        if (!S_ISDIR(st.st_mode)) {
+           if (setfilmode(dirp->d_name, mode, &st) < 0) {
+                LOG(log_error, logtype_afpd, "setdirmode: chmod %s: %s",
+                    dirp->d_name, strerror(errno) );
+                return -1;
+           }
+        }
+#if 0
             /* XXX: need to preserve special modes */
-            if (S_ISDIR(st.st_mode)) {
+        else if (S_ISDIR(st.st_mode)) {
                 if (stickydirmode(dirp->d_name, DIRBITS | mode, dropbox) < 0)
                     return (-1);
             } else if (stickydirmode(dirp->d_name, mode, dropbox) < 0)
                 return (-1);
         }
+#endif
     }
     closedir( dir );
+
+    /* change perm of .AppleDouble's files
+    */
     if (( dir = opendir( ".AppleDouble" )) == NULL ) {
         if (noadouble)
             goto setdirmode_noadouble;
@@ -410,11 +452,11 @@ const int dropbox;
             LOG(log_error, logtype_afpd, "setdirmode: stat %s: %s", buf, strerror(errno) );
             continue;
         }
-
-        if (S_ISDIR(st.st_mode)) {
-            stickydirmode( buf, DIRBITS | mode, dropbox );
-        } else
-            stickydirmode( buf, mode, dropbox );
+        if (!S_ISDIR(st.st_mode)) {
+           if (setfilmode(buf, ad_hf_mode(mode), &st) < 0) {
+               /* FIXME what do we do then? */
+           }
+        }
     } /* end for */
     closedir( dir );
 
@@ -582,3 +624,89 @@ setdirowner_noadouble:
 
     return( 0 );
 }
+
+/* recursive chown()ing of a directory */
+static int recursive_chown(const char *path, uid_t uid, gid_t gid) {
+    struct stat sbuf;
+    DIR *odir = NULL;
+    struct dirent *entry;
+    char *name;
+    int ret = 0;
+    char newpath[PATH_MAX+1];
+    newpath[PATH_MAX] = '\0';
+    
+    if (chown(path, uid, gid) < 0) {
+        LOG(log_error, logtype_afpd, "cannot chown() file [%s] (uid = %d): %s\n", path, uid, strerror(errno));
+       return -1;
+    }
+
+    if (stat(path, &sbuf) < 0) {
+       LOG(log_error, logtype_afpd, "cannot chown() file [%s] (uid = %d): %s\n", path, uid, strerror(errno));
+       return -1;
+    }
+       
+    if (S_ISDIR(sbuf.st_mode)) {
+       odir = opendir(path);
+       if (odir == NULL) {
+           LOG(log_error, logtype_afpd, "cannot opendir() [%s] (uid = %d): %s\n", path, uid, strerror(errno));
+           goto recursive_chown_end;
+       }
+       while ((entry=readdir(odir)) != NULL) {
+           name = entry->d_name;
+           if (name[0] == '.' && name[1] == '\0')
+               continue;
+           if (name[0] == '.' && name[1] == '.' && name[2] == '\0')
+               continue;
+           sprintf(newpath, "%s/%s", path, name);
+           if (recursive_chown(newpath, uid, gid) < 0)
+               ret = -1;
+       } /* while */
+    } /* if */
+
+recursive_chown_end:
+    if (odir != NULL) {
+       closedir(odir);
+    }
+    return ret;
+}
+
+/* This is equivalent of unix rename(). */
+int unix_rename(const char *oldpath, const char *newpath)
+{
+       char pd_name[PATH_MAX+1];
+       int i;
+        struct stat pd_stat;
+        uid_t uid;
+
+       if (rename(oldpath, newpath) < 0)
+               return -1;
+
+       for (i = 0; i <= PATH_MAX && newpath[i] != '\0'; i++)
+               pd_name[i] = newpath[i];
+       pd_name[i] = '\0';
+
+       while (i > 0 && pd_name[i] != '/') i--;
+       if (pd_name[i] == '/') i++;
+
+        pd_name[i++] = '.'; pd_name[i++] = '\0';
+
+        if (stat(pd_name, &pd_stat) < 0) {
+           LOG(log_error, logtype_afpd, "stat() of parent dir failed: pd_name = %s, uid = %d: %s\n",
+               pd_name, geteuid(), strerror(errno));
+               return 0;
+       }
+
+       /* So we have SGID bit set... */
+        if ((S_ISGID & pd_stat.st_mode)        != 0) {
+            uid = geteuid();
+            if (seteuid(0) < 0)
+               LOG(log_error, logtype_afpd, "seteuid() failed: %s\n", strerror(errno));
+            if (recursive_chown(newpath, uid, pd_stat.st_gid) < 0)
+               LOG(log_error, logtype_afpd, "chown() of parent dir failed: newpath=%s, uid=%d: %s\n",
+                   pd_name, geteuid(), strerror(errno));
+            seteuid(uid);
+       }
+
+       return 0;
+}
+