]> arthur.barton.de Git - netatalk.git/blobdiff - etc/afpd/unix.c
try to make afpd directory access right works the way Macs expect
[netatalk.git] / etc / afpd / unix.c
index e1c7c8dd52d2e3c5d2ba3cafb118af5a0bb3da0d..7f948bc907a8492f53fc956fca1e2fd1dd999e9a 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: unix.c,v 1.32 2002-03-24 01:23:41 sibaz Exp $
+ * $Id: unix.c,v 1.36 2002-08-29 18:57:26 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 */
@@ -114,14 +115,16 @@ mode_t    bits;
     return( mbits );
 }
 
+/* --------------------------------
+    cf AFP 3.0 page 63
+*/
 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;
 
@@ -130,22 +133,28 @@ struct maccess    *ma;
 
     ma->ma_owner = utombits( mode );
 
+    /* ma_user is a union of all permissions */
+    ma->ma_user = 0;
     if ( (uuid == stat->st_uid) || (uuid == 0)) {
         ma->ma_user = ma->ma_owner | AR_UOWN;
-    } else if ( gmem( stat->st_gid )) {
-        ma->ma_user = ma->ma_group;
-    } else {
-        ma->ma_user = ma->ma_world;
     }
+    if ( gmem( stat->st_gid )) {
+        ma->ma_user |= ma->ma_group;
+    } 
+    ma->ma_user |= ma->ma_world;
 
     /*
      * There are certain things the mac won't try if you don't have
      * the "owner" bit set, even tho you can do these things on unix wiht
      * only write permission.  What were the things?
+     * 
+     * FIXME and so what ?
      */
+#if 0
     if ( ma->ma_user & AR_UWRITE ) {
         ma->ma_user |= AR_UOWN;
     }
+#endif    
 }
 
 
@@ -156,15 +165,22 @@ struct maccess    *ma;
  * Note: the previous method, using access(), does not work correctly
  * over NFS.
  */
-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;
 }
 
@@ -196,16 +212,21 @@ u_char    bits;
     return( mode );
 }
 
+/* ----------------------------------
+   from the finder's share windows (menu--> File--> sharing...)
+   and from AFP 3.0 spec page 63 
+   the mac mode should be save somewhere 
+*/
 mode_t mtoumode( ma )
 struct maccess *ma;
 {
     mode_t             mode;
 
     mode = 0;
-    mode |= mtoubits( ma->ma_owner );
+    mode |= mtoubits( ma->ma_owner |ma->ma_world);
     mode = mode << 3;
 
-    mode |= mtoubits( ma->ma_group );
+    mode |= mtoubits( ma->ma_group |ma->ma_world);
     mode = mode << 3;
 
     mode |= mtoubits( ma->ma_world );
@@ -213,7 +234,7 @@ struct maccess      *ma;
     return( mode );
 }
 
-inline int stickydirmode(name, mode, dropbox)
+int stickydirmode(name, mode, dropbox)
 char * name;
 const mode_t mode;
 const int dropbox;
@@ -339,6 +360,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;
+        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;
@@ -349,7 +391,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 );
@@ -366,15 +408,26 @@ const int dropbox;
         }
 
         if (S_ISREG(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;
@@ -396,11 +449,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_ISREG(st.st_mode)) {
+           if (setfilmode(dirp->d_name, ad_hf_mode(mode), &st) < 0) {
+               /* FIXME what do we do then? */
+           }
+        }
     } /* end for */
     closedir( dir );