]> arthur.barton.de Git - netatalk.git/blobdiff - etc/afpd/unix.c
From trunk: preserve special file permissions.
[netatalk.git] / etc / afpd / unix.c
index e9ec3193df406da3bc70a312e283f14b0ddea95b..63b58c69ea2fbbc447b61ffc330a272232fc9b1c 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: unix.c,v 1.24.2.1 2001-12-03 05:01:04 jmarcus Exp $
+ * $Id: unix.c,v 1.24.2.6 2002-03-12 15:25:07 srittau Exp $
  *
  * Copyright (c) 1990,1993 Regents of The University of Michigan.
  * All Rights Reserved.  See COPYRIGHT.
@@ -16,7 +16,7 @@
 #include <sys/types.h>
 #include <sys/param.h>
 #include <sys/stat.h>
-#include <sys/syslog.h>
+#include <syslog.h>
 #include <netatalk/endian.h>
 #include <dirent.h>
 #include <limits.h>
@@ -150,35 +150,22 @@ struct maccess    *ma;
 
 
 /*
- * Calculate the mode for a directory using Posix access() calls to
- * estimate permission, a la mdw.
+ * Calculate the mode for a directory using a stat() call to
+ * estimate permission.
+ *
+ * Note: the previous method, using access(), does not work correctly
+ * over NFS.
  */
 void accessmode( path, ma, dir )
 char           *path;
 struct maccess *ma;
 struct dir             *dir;
 {
-    if ( access( path, R_OK|W_OK|X_OK ) == 0 ) {
-        ma->ma_user = AR_UREAD|AR_UWRITE|AR_USEARCH|AR_UOWN;
-        ma->ma_owner = AR_UREAD|AR_UWRITE|AR_USEARCH;
-    } else if ( access( path, R_OK|X_OK ) == 0 ) {
-        ma->ma_user = AR_UREAD|AR_USEARCH;
-        ma->ma_owner = AR_UREAD|AR_USEARCH;
-    } else {
-        ma->ma_user = ma->ma_owner = 0;
-        if ( access( path, R_OK ) == 0 ) {
-            ma->ma_user |= AR_UREAD;
-            ma->ma_owner |= AR_UREAD;
-        }
-        if ( access( path, X_OK ) == 0 ) {
-            ma->ma_user |= AR_USEARCH;
-            ma->ma_owner |= AR_USEARCH;
-        }
-        if ( access( path, W_OK ) == 0 ) {
-            ma->ma_user |= AR_UWRITE|AR_UOWN;
-            ma->ma_owner |= AR_UWRITE;
-        }
-    }
+    struct stat sb;
+    ma->ma_user = ma->ma_owner = 0;
+    if ( stat( path, &sb ) == 0 )
+        utommode( &sb, ma );
+    return;
 }
 
 int gmem( gid )
@@ -245,14 +232,14 @@ const int dropbox;
             else { /* if S_IWOTH and not S_IROTH */
                 uid=geteuid();
                 if ( seteuid(0) < 0) {
-                    syslog( LOG_ERR, "stickydirmode: unable to seteuid root: %m");
+                    syslog( LOG_ERR, "stickydirmode: unable to seteuid root: %s", strerror(errno));
                 }
-                if ( retval=chmod( name, ( (DIRBITS | mode | S_ISVTX) & 0777 & ~default_options.umask) ) < 0) {
-                    syslog( LOG_ERR, "stickydirmode: chmod \"%s\": %m", name );
+                if ( retval=chmod( name, ( (DIRBITS | mode | S_ISVTX) & ~default_options.umask) ) < 0) {
+                    syslog( LOG_ERR, "stickydirmode: chmod \"%s\": %s", name, strerror(errno) );
                     return(AFPERR_ACCESS);
                 } else {
 #ifdef DEBUG
-                    syslog( LOG_INFO, "stickydirmode: (debug) chmod \"%s\": %m", name );
+                    syslog( LOG_INFO, "stickydirmode: (debug) chmod \"%s\"", name );
 #endif /* DEBUG */
                     seteuid(uid);
                 } /* end getting retval */
@@ -264,7 +251,7 @@ const int dropbox;
             *  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) & 0777 & ~default_options.umask ) < 0) && errno != EPERM)  {
+            if ( (chmod( name, (DIRBITS | mode) & ~default_options.umask ) < 0) && errno != EPERM)  {
                 syslog( LOG_ERR, "stickydirmode: chmod \"%s\": %s",
                         name, strerror(errno) );
                 retval = -1;
@@ -323,19 +310,18 @@ const mode_t      mode;
             }
 
             if (S_ISDIR(st.st_mode)) {
-                if ( chmod( modbuf,  (DIRBITS | mode) & 0777 & ~default_options.umask ) < 0 && errno != EPERM ) {
+                if ( chmod( modbuf,  (DIRBITS | mode) & ~default_options.umask ) < 0 && errno != EPERM ) {
                     syslog( LOG_ERR, "setdeskmode: chmod %s: %s",
                             modbuf, strerror(errno) );
                 }
-            } else if ( chmod( modbuf,  mode & 0777 & ~default_options.umask ) < 0 && errno != EPERM ) {
+            } else if ( chmod( modbuf,  mode & ~default_options.umask ) < 0 && errno != EPERM ) {
                 syslog( LOG_ERR, "setdeskmode: chmod %s: %s",
                         modbuf, strerror(errno) );
             }
 
         }
         closedir( sub );
-        /* XXX: need to preserve special modes */
-        if ( chmod( deskp->d_name,  (DIRBITS | mode) & 0777 & ~default_options.umask ) < 0 && errno != EPERM ) {
+        if ( chmod( deskp->d_name,  (DIRBITS | mode) & ~default_options.umask ) < 0 && errno != EPERM ) {
             syslog( LOG_ERR, "setdeskmode: chmod %s: %s",
                     deskp->d_name, strerror(errno) );
         }
@@ -345,8 +331,7 @@ const mode_t        mode;
         syslog( LOG_ERR, "setdeskmode: chdir %s: %s", wd, strerror(errno) );
         return -1;
     }
-    /* XXX: need to preserve special modes */
-    if ( chmod( ".AppleDesktop",  (DIRBITS | mode) & 0777 & ~default_options.umask ) < 0 && errno != EPERM ) {
+    if ( chmod( ".AppleDesktop",  (DIRBITS | mode) & ~default_options.umask ) < 0 && errno != EPERM ) {
         syslog( LOG_ERR, "setdeskmode: chmod .AppleDesktop: %s", strerror(errno) );
     }
     return( 0 );