]> 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 13db7e5d1491ab3b07392a5756041908357cd840..fa0ee0a722f0a37abe731603cd03e25c486f27e6 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: unix.c,v 1.38 2002-09-29 15:42:14 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.
@@ -133,15 +133,18 @@ mode_t mode;
 
     ma->ma_owner = utombits( mode );
 
-    /* ma_user is a union of all permissions */
-    ma->ma_user = 0;
+    /* 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,6 +167,7 @@ mode_t mode;
  *
  * Note: the previous method, using access(), does not work correctly
  * over NFS.
+ * FIXME what about ACL?
  */
 void accessmode( path, ma, dir, st )
 char           *path;
@@ -406,7 +410,7 @@ 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) );
@@ -448,7 +452,7 @@ const int dropbox;
             LOG(log_error, logtype_afpd, "setdirmode: stat %s: %s", buf, strerror(errno) );
             continue;
         }
-        if (S_ISREG(st.st_mode)) {
+        if (!S_ISDIR(st.st_mode)) {
            if (setfilmode(buf, ad_hf_mode(mode), &st) < 0) {
                /* FIXME what do we do then? */
            }
@@ -620,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;
+}
+