]> arthur.barton.de Git - netatalk.git/blobdiff - etc/afpd/unix.c
remove gcc warnings and cleanup inline mess
[netatalk.git] / etc / afpd / unix.c
index cf4b10e26761c6bfbb7889dfd4b32d1be75e5cd9..58170528941c6f6f38ccfab6c28b511709c7b816 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: unix.c,v 1.39 2002-12-14 04:01:01 didg Exp $
+ * $Id: unix.c,v 1.43.2.1.2.10.2.4 2008-11-25 15:16:33 didg Exp $
  *
  * Copyright (c) 1990,1993 Regents of The University of Michigan.
  * All Rights Reserved.  See COPYRIGHT.
 
 #include <stdio.h>
 #include <stdlib.h>
-#include <unistd.h>
-#include <errno.h>
-#include <sys/types.h>
-#include <sys/param.h>
-#include <sys/stat.h>
-#include <atalk/logger.h>
-#include <netatalk/endian.h>
-#include <dirent.h>
-#include <limits.h>
-#include <atalk/adouble.h>
-#include <atalk/afp.h>
 
 /* STDC check */
-#if STDC_HEADERS
+#ifdef STDC_HEADERS
 #include <string.h>
 #else /* STDC_HEADERS */
+
 #ifndef HAVE_STRCHR
 #define strchr index
 #define strrchr index
 #endif /* HAVE_STRCHR */
 char *strchr (), *strrchr ();
+
 #ifndef HAVE_MEMCPY
 #define memcpy(d,s,n) bcopy ((s), (d), (n))
 #define memmove(d,s,n) bcopy ((s), (d), (n))
 #endif /* ! HAVE_MEMCPY */
 #endif /* STDC_HEADERS */
 
-#ifdef HAVE_FCNTL_H
-#include <fcntl.h>
-#endif /* HAVE_FCNTL_H */
+#include <errno.h>
+#include <dirent.h>
+#include <limits.h>
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <atalk/logger.h>
+#include <atalk/adouble.h>
+#include <atalk/afp.h>
+#include <atalk/util.h>
+
 #include "auth.h"
 #include "directory.h"
 #include "volume.h"
 #include "unix.h"
+#include "fork.h"
 
 /*
  * Get the free space on a partition.
@@ -100,17 +99,17 @@ u_int32_t   *bsize;
     return( AFP_OK );
 }
 
-static __inline__ int utombits( bits )
+static int utombits( bits )
 mode_t bits;
 {
     int                mbits;
 
     mbits = 0;
 
-    mbits |= ( bits & ( S_IREAD >> 6 )) ? (AR_UREAD | AR_USEARCH) : 0;
+    mbits |= ( bits & ( S_IREAD >> 6 ))  ? AR_UREAD  : 0;
     mbits |= ( bits & ( S_IWRITE >> 6 )) ? AR_UWRITE : 0;
-    /* Do we really need this?
-        mbits |= ( bits & ( S_IEXEC >> 6) ) ? AR_USEARCH : 0; */
+    /* Do we really need this? */
+    mbits |= ( bits & ( S_IEXEC >> 6) ) ? AR_USEARCH : 0;
 
     return( mbits );
 }
@@ -123,7 +122,6 @@ struct stat         *stat;
 struct maccess *ma;
 {
 mode_t mode;
-int    ok = 0;
 
     mode = stat->st_mode;
     ma->ma_world = utombits( mode );
@@ -134,48 +132,37 @@ int    ok = 0;
 
     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)) {
-        ok = 1;
         ma->ma_user = ma->ma_owner | AR_UOWN;
     }
-    if ( gmem( stat->st_gid )) {
-        ok = 1;
-        ma->ma_user |= ma->ma_group;
-    } 
-    if (ok) {
-       /* We are the directory owner or we are in the group owner.
-        * If we don't have a perm we need to remove it from ma_world.
-        *
-        * eg if perms are rwx--Sr-x and I'm not the user owner but I'm in the group
-        * I DON'T have read and search access on the directory.
-       */
-       if (!(ma->ma_user & AR_UWRITE))
-          ma->ma_world &= ~AR_UWRITE;
-       if (!(ma->ma_user & AR_UREAD))
-          ma->ma_world &= ~AR_UREAD;
-       if (!(ma->ma_user & AR_USEARCH))
-          ma->ma_world &= ~AR_USEARCH;
-    }
-
-    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
      * 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 ?
+     * FIXME 
+     * ditto seems to care if st_uid is 0 ?
+     * was ma->ma_user & AR_UWRITE
+     * but 0 as owner is a can of worms.
      */
-#if 0
-    if ( ma->ma_user & AR_UWRITE ) {
+    if ( !stat->st_uid ) {
         ma->ma_user |= AR_UOWN;
     }
-#endif    
 }
 
-
+#ifdef accessmode
+#undef accessmode
+#endif
 /*
  * Calculate the mode for a directory using a stat() call to
  * estimate permission.
@@ -183,11 +170,13 @@ int    ok = 0;
  * Note: the previous method, using access(), does not work correctly
  * over NFS.
  * FIXME what about ACL?
+ *
+ * dir parameter is used by AFS
  */
 void accessmode( path, ma, dir, st )
 char           *path;
 struct maccess *ma;
-struct dir     *dir;
+struct dir     *dir _U_;
 struct stat     *st;
 
 {
@@ -216,7 +205,7 @@ const gid_t gid;
     return( 0 );
 }
 
-static __inline__ mode_t mtoubits( bits )
+static mode_t mtoubits( bits )
 u_char bits;
 {
     mode_t     mode;
@@ -253,7 +242,22 @@ struct maccess     *ma;
     return( mode );
 }
 
-/*
+/* ----------------------------- */
+char *fullpathname(const char *name)
+{
+    static char wd[ MAXPATHLEN + 1];
+
+    if ( getcwd( wd , MAXPATHLEN) ) {
+        strlcat(wd, "/", MAXPATHLEN);
+        strlcat(wd, name, MAXPATHLEN);
+    }
+    else {
+        strlcpy(wd, name, MAXPATHLEN);
+    }
+    return wd;
+}
+
+/* -----------------------------
    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 ?)
@@ -278,10 +282,10 @@ const int dropbox;
                 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) );
+                LOG(log_error, logtype_afpd, "stickydirmode: chmod \"%s\": %s", fullpathname(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", fullpathname(name), strerror(retval) );
 #endif /* DEBUG */
             }
             seteuid(uid);
@@ -295,13 +299,21 @@ const int dropbox;
      *  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) );
+        LOG(log_error, logtype_afpd, "stickydirmode: chmod \"%s\": %s", fullpathname(name), strerror(errno) );
         retval = -1;
     }
 
     return retval;
 }
 
+/* ------------------------- */
+int dir_rx_set(mode_t mode)
+{
+    return (mode & (S_IXUSR | S_IRUSR)) == (S_IXUSR | S_IRUSR);
+}
+
+#define EXEC_MODE (S_IXGRP | S_IXUSR | S_IXOTH)
+
 int setdeskmode( mode )
 const mode_t   mode;
 {
@@ -311,6 +323,10 @@ const mode_t       mode;
     struct dirent      *deskp, *subp;
     DIR                        *desk, *sub;
 
+    if (!dir_rx_set(mode)) {
+        /* want to remove read and search access to owner it will screw the volume */
+        return -1 ;
+    }
     if ( getcwd( wd , MAXPATHLEN) == NULL ) {
         return( -1 );
     }
@@ -343,27 +359,23 @@ const mode_t      mode;
             strcat( modbuf, subp->d_name );
             /* XXX: need to preserve special modes */
             if (stat(modbuf, &st) < 0) {
-                LOG(log_error, logtype_afpd, "setdeskmode: stat %s: %s",
-                    modbuf, strerror(errno) );
+                LOG(log_error, logtype_afpd, "setdeskmode: stat %s: %s",fullpathname(modbuf), strerror(errno) );
                 continue;
             }
 
             if (S_ISDIR(st.st_mode)) {
                 if ( chmod( modbuf,  (DIRBITS | mode) & ~default_options.umask ) < 0 && errno != EPERM ) {
-                    LOG(log_error, logtype_afpd, "setdeskmode: chmod %s: %s",
-                        modbuf, strerror(errno) );
+                     LOG(log_error, logtype_afpd, "setdeskmode: chmod %s: %s",fullpathname(modbuf), strerror(errno) );
                 }
-            } else if ( chmod( modbuf,  mode & ~default_options.umask ) < 0 && errno != EPERM ) {
-                LOG(log_error, logtype_afpd, "setdeskmode: chmod %s: %s",
-                    modbuf, strerror(errno) );
+            } else if ( chmod( modbuf,  mode & ~(default_options.umask | EXEC_MODE) ) < 0 && errno != EPERM ) {
+                LOG(log_error, logtype_afpd, "setdeskmode: chmod %s: %s",fullpathname(modbuf), strerror(errno) );
             }
 
         }
         closedir( sub );
         /* XXX: need to preserve special modes */
         if ( chmod( deskp->d_name,  (DIRBITS | mode) & ~default_options.umask ) < 0 && errno != EPERM ) {
-            LOG(log_error, logtype_afpd, "setdeskmode: chmod %s: %s",
-                deskp->d_name, strerror(errno) );
+            LOG(log_error, logtype_afpd, "setdeskmode: chmod %s: %s",fullpathname(deskp->d_name), strerror(errno) );
         }
     }
     closedir( desk );
@@ -373,25 +385,48 @@ const mode_t      mode;
     }
     /* XXX: need to preserve special modes */
     if ( chmod( ".AppleDesktop",  (DIRBITS | mode) & ~default_options.umask ) < 0 && errno != EPERM ) {
-        LOG(log_error, logtype_afpd, "setdeskmode: chmod .AppleDesktop: %s", strerror(errno) );
+        LOG(log_error, logtype_afpd, "setdeskmode: chmod %s: %s", fullpathname(".AppleDesktop"),strerror(errno) );
     }
     return( 0 );
 }
 
+/* --------------------- */
+int setfilunixmode (vol, path, mode)
+const struct vol *vol;
+struct path* path;
+mode_t mode;
+{
+    if (!path->st_valid) {
+        of_stat(path);
+    }
+
+    if (path->st_errno) {
+        return -1;
+    }
+        
+    mode |= vol->v_perm;
+
+    if (setfilmode( path->u_name, mode, &path->st) < 0)
+        return -1;
+    /* we need to set write perm if read set for resource fork */
+    return setfilmode(vol->ad_path( path->u_name, ADFLAGS_HF ), ad_hf_mode(mode), &path->st);
+}
+
+/* --------------------- */
 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;
+mode_t mask = S_IRWXU | S_IRWXG | S_IRWXO;  /* rwx for owner group and other, by default */
 
     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;
@@ -399,41 +434,98 @@ mode_t mask = S_IRUSR |S_IWUSR | S_IRGRP | S_IWGRP |S_IROTH | S_IWOTH;
    return 0;
 }
 
-int setdirmode( mode, noadouble, dropbox )
-const mode_t mode;
-const int noadouble;
-const int dropbox;
+/* --------------------- */
+int setdirunixmode( vol, name, mode )
+const struct vol *vol;
+const char       *name;
+mode_t           mode;
+{
+char *adouble = vol->ad_path( name, ADFLAGS_DIR );
+
+    int dropbox = (vol->v_flags & AFPVOL_DROPBOX);
+    mode |= vol->v_perm;
+
+    if (dir_rx_set(mode)) {
+       /* extending right? dir first then .AppleDouble */
+       if ( stickydirmode(name, DIRBITS | mode, dropbox) < 0 )
+               return -1;
+       if (vol->v_adouble != AD_VERSION2_OSX) {
+            if (stickydirmode(ad_dir(adouble), DIRBITS | mode, dropbox) < 0 && !vol_noadouble(vol)) {
+                return  -1 ;
+            }
+        }
+    }
+    if (setfilmode(adouble, ad_hf_mode(mode), NULL) < 0 && !vol_noadouble(vol)) {
+        return  -1 ;
+    }
+    if (!dir_rx_set(mode)) {
+       if (vol->v_adouble != AD_VERSION2_OSX) {
+            if (stickydirmode(ad_dir(adouble), DIRBITS | mode, dropbox) < 0 && !vol_noadouble(vol)) {
+                return  -1 ;
+            }
+        }
+       if ( stickydirmode(name, DIRBITS | mode, dropbox) < 0 )
+            return -1;
+    }
+    return 0;
+}
+
+/* --------------------- */
+int setdirmode( vol, name, mode )
+const struct vol *vol;
+const char       *name;
+mode_t           mode;
 {
     char               buf[ MAXPATHLEN + 1];
     struct stat                st;
     char               *m;
     struct dirent      *dirp;
     DIR                        *dir;
+    mode_t              hf_mode;
+    int                 osx = vol->v_adouble == AD_VERSION2_OSX;
+    int                 dropbox = (vol->v_flags & AFPVOL_DROPBOX);
+    char                *adouble = vol->ad_path( name, ADFLAGS_DIR );
+    char                *adouble_p = ad_dir(adouble);
+    
+    mode |= vol->v_perm;
+    hf_mode = ad_hf_mode(mode);
+
+    if (dir_rx_set(mode)) {
+       /* extending right? dir first then .AppleDouble */
+       if ( stickydirmode(name, DIRBITS | mode, dropbox) < 0 )
+               return -1;
+       if (!osx) {
+            if (stickydirmode(adouble_p, DIRBITS | mode, dropbox) < 0 && !vol_noadouble(vol)) {
+                return  -1 ;
+            }
+        }
+    }
     
-    if (( dir = opendir( "." )) == NULL ) {
-        LOG(log_error, logtype_afpd, "setdirmode: opendir .: %s", strerror(errno) );
+    if (( dir = opendir( name )) == NULL ) {
+        LOG(log_error, logtype_afpd, "setdirmode: opendir: %s", fullpathname(name), strerror(errno) );
         return( -1 );
     }
 
     for ( dirp = readdir( dir ); dirp != NULL; dirp = readdir( dir )) {
-        if ( *dirp->d_name == '.' ) {
+        /* FIXME */
+        if ( *dirp->d_name == '.' && (!osx || dirp->d_name[1] != '_')) {
             continue;
         }
         if ( stat( dirp->d_name, &st ) < 0 ) {
-            LOG(log_error, logtype_afpd, "setdirmode: stat %s: %s",
-                dirp->d_name, strerror(errno) );
+            LOG(log_error, logtype_afpd, "setdirmode: stat %s: %s",dirp->d_name, strerror(errno) );
             continue;
         }
 
-        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) );
+        if (!S_ISDIR(st.st_mode)) {
+           int setmode = (osx && *dirp->d_name == '.')?hf_mode:mode;
+
+           if (setfilmode(dirp->d_name, setmode, &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 */
+        /* Don't change subdir perm */
         else if (S_ISDIR(st.st_mode)) {
                 if (stickydirmode(dirp->d_name, DIRBITS | mode, dropbox) < 0)
                     return (-1);
@@ -443,16 +535,20 @@ const int dropbox;
 #endif
     }
     closedir( dir );
-
+    
+    if (osx) {
+        goto setdirmode_noadouble;
+    }
+    
     /* change perm of .AppleDouble's files
     */
-    if (( dir = opendir( ".AppleDouble" )) == NULL ) {
-        if (noadouble)
+    if (( dir = opendir( adouble_p )) == NULL ) {
+        if (vol_noadouble(vol))
             goto setdirmode_noadouble;
-        LOG(log_error, logtype_afpd, "setdirmode: opendir .AppleDouble: %s", strerror(errno) );
+        LOG(log_error, logtype_afpd, "setdirmode: opendir %s: %s", fullpathname(".AppleDouble"),strerror(errno) );
         return( -1 );
     }
-    strcpy( buf, ".AppleDouble" );
+    strcpy( buf, adouble_p);
     strcat( buf, "/" );
     m = strchr( buf, '\0' );
     for ( dirp = readdir( dir ); dirp != NULL; dirp = readdir( dir )) {
@@ -467,24 +563,26 @@ const int dropbox;
             LOG(log_error, logtype_afpd, "setdirmode: stat %s: %s", buf, strerror(errno) );
             continue;
         }
-        if (S_ISREG(st.st_mode)) {
-           if (setfilmode(buf, ad_hf_mode(mode), &st) < 0) {
+        if (!S_ISDIR(st.st_mode)) {
+           if (setfilmode(buf, hf_mode , &st) < 0) {
                /* FIXME what do we do then? */
            }
         }
     } /* end for */
     closedir( dir );
 
-    /* XXX: use special bits to tag directory permissions */
-
-    /* XXX: need to preserve special modes */
-    if ( stickydirmode(".AppleDouble", DIRBITS | mode, dropbox) < 0 )
-        return( -1 );
+    if (!dir_rx_set(mode)) {
+        /* XXX: need to preserve special modes */
+        if (stickydirmode(adouble_p, DIRBITS | mode, dropbox) < 0 ) {
+                return  -1 ;
+        }
+    }
 
 setdirmode_noadouble:
-    /* XXX: need to preserve special modes */
-    if ( stickydirmode(".", DIRBITS | mode, dropbox) < 0 )
-        return( -1 );
+    if (!dir_rx_set(mode)) {
+       if ( stickydirmode(name, DIRBITS | mode, dropbox) < 0 )
+               return -1;
+    }
     return( 0 );
 }
 
@@ -530,8 +628,7 @@ const gid_t gid;
             strcat( modbuf, subp->d_name );
             /* XXX: add special any uid, ignore group bits */
             if ( chown( modbuf, uid, gid ) < 0 && errno != EPERM ) {
-                LOG(log_error, logtype_afpd, "setdeskown: chown %s: %s",
-                    modbuf, strerror(errno) );
+                LOG(log_error, logtype_afpd, "setdeskown: chown %s: %s", fullpathname(modbuf), strerror(errno) );
             }
         }
         closedir( sub );
@@ -547,55 +644,106 @@ const gid_t      gid;
         return -1;
     }
     if ( chown( ".AppleDesktop", uid, gid ) < 0 && errno != EPERM ) {
-        LOG(log_error, logtype_afpd, "setdeskowner: chown .AppleDesktop: %s",
-            strerror(errno) );
+        LOG(log_error, logtype_afpd, "setdeskowner: chown %s: %s", fullpathname(".AppleDouble"), strerror(errno) );
     }
     return( 0 );
 }
 
+/* ----------------------------- */
+int setfilowner(vol, uid, gid, path)
+const struct vol *vol;
+const uid_t    uid;
+const gid_t    gid;
+struct path* path;
+{
+    struct stat st;
+    char  *ad_p;
+
+    if (!path->st_valid) {
+        of_stat(path);
+    }
+
+    if (path->st_errno) {
+        return -1;
+    }
 
-/* uid/gid == 0 need to be handled as special cases. they really mean
+    if ( chown( path->u_name, uid, gid ) < 0 && errno != EPERM ) {
+        LOG(log_debug, logtype_afpd, "setfilowner: chown %d/%d %s: %s",
+            uid, gid, path->u_name, strerror(errno) );
+       return -1;
+    }
+
+    ad_p = vol->ad_path( path->u_name, ADFLAGS_HF );
+
+    if ( stat( ad_p, &st ) < 0 ) {
+       /* ignore */
+        return 0;
+    }
+    if ( chown( ad_p, uid, gid ) < 0 &&
+            errno != EPERM ) {
+        LOG(log_debug, logtype_afpd, "setfilowner: chown %d/%d %s: %s",
+            uid, gid, ad_p, strerror(errno) );
+        return -1;
+    }
+    return 0;
+}
+
+
+/* --------------------------------- 
+ * uid/gid == 0 need to be handled as special cases. they really mean
  * that user/group should inherit from other, but that doesn't fit
  * into the unix permission scheme. we can get around this by
  * co-opting some bits. */
-int setdirowner( uid, gid, noadouble )
+int setdirowner(vol, name, uid, gid )
+const struct vol *vol;
+const char      *name;
 const uid_t    uid;
 const gid_t    gid;
-const int   noadouble;
 {
     char               buf[ MAXPATHLEN + 1];
     struct stat                st;
     char               *m;
     struct dirent      *dirp;
     DIR                        *dir;
+    int                 osx = vol->v_adouble == AD_VERSION2_OSX;
+    int                 noadouble = vol_noadouble(vol);
+    char                *adouble; 
+    char                *adouble_p;
 
-    if (( dir = opendir( "." )) == NULL ) {
+    if (( dir = opendir( name )) == NULL ) {
         return( -1 );
     }
     for ( dirp = readdir( dir ); dirp != NULL; dirp = readdir( dir )) {
-        if ( *dirp->d_name == '.' ) {
+        if ( *dirp->d_name == '.' && (!osx || dirp->d_name[1] != '_')) {
             continue;
-        };
+        }
         if ( stat( dirp->d_name, &st ) < 0 ) {
             LOG(log_error, logtype_afpd, "setdirowner: stat %s: %s",
-                dirp->d_name, strerror(errno) );
+                fullpathname(dirp->d_name), strerror(errno) );
             continue;
         }
         if (( st.st_mode & S_IFMT ) == S_IFREG ) {
             if ( chown( dirp->d_name, uid, gid ) < 0 && errno != EPERM ) {
                 LOG(log_debug, logtype_afpd, "setdirowner: chown %s: %s",
-                    dirp->d_name, strerror(errno) );
+                    fullpathname(dirp->d_name), strerror(errno) );
                 /* return ( -1 ); Sometimes this is okay */
             }
         }
     }
     closedir( dir );
-    if (( dir = opendir( ".AppleDouble" )) == NULL ) {
+    
+    if (osx) {
+       goto setdirowner_noadouble;
+    }
+
+    adouble = vol->ad_path( name, ADFLAGS_DIR );
+    adouble_p = ad_dir(adouble);
+    if (( dir = opendir( adouble_p )) == NULL ) {
         if (noadouble)
             goto setdirowner_noadouble;
         return( -1 );
     }
-    strcpy( buf, ".AppleDouble" );
+    strcpy( buf, adouble_p );
     strcat( buf, "/" );
     m = strchr( buf, '\0' );
     for ( dirp = readdir( dir ); dirp != NULL; dirp = readdir( dir )) {
@@ -607,7 +755,7 @@ const int   noadouble;
         strcat( buf, dirp->d_name );
         if ( chown( buf, uid, gid ) < 0 && errno != EPERM ) {
             LOG(log_debug, logtype_afpd, "setdirowner: chown %d/%d %s: %s",
-                uid, gid, buf, strerror(errno) );
+                uid, gid, fullpathname(buf), strerror(errno) );
             /* return ( -1 ); Sometimes this is okay */
         }
     }
@@ -617,13 +765,13 @@ const int   noadouble;
      * We cheat: we know that chown doesn't do anything.
      */
     if ( stat( ".AppleDouble", &st ) < 0 ) {
-        LOG(log_error, logtype_afpd, "setdirowner: stat .AppleDouble: %s", strerror(errno) );
+        LOG(log_error, logtype_afpd, "setdirowner: stat %s: %s", fullpathname(".AppleDouble"), strerror(errno) );
         return( -1 );
     }
     if ( gid && gid != st.st_gid && chown( ".AppleDouble", uid, gid ) < 0 &&
             errno != EPERM ) {
-        LOG(log_debug, logtype_afpd, "setdirowner: chown %d/%d .AppleDouble: %s",
-            uid, gid, strerror(errno) );
+        LOG(log_debug, logtype_afpd, "setdirowner: chown %d/%d %s: %s",
+            uid, gid,fullpathname(".AppleDouble"), strerror(errno) );
         /* return ( -1 ); Sometimes this is okay */
     }
 
@@ -633,9 +781,99 @@ setdirowner_noadouble:
     }
     if ( gid && gid != st.st_gid && chown( ".", uid, gid ) < 0 &&
             errno != EPERM ) {
-        LOG(log_debug, logtype_afpd, "setdirowner: chown %d/%d .: %s",
-            uid, gid, strerror(errno) );
+        LOG(log_debug, logtype_afpd, "setdirowner: chown %d/%d %s: %s",
+            uid, gid, fullpathname("."), strerror(errno) );
     }
 
     return( 0 );
 }
+
+#if 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", path, uid, strerror(errno));
+       return -1;
+    }
+
+    if (stat(path, &sbuf) < 0) {
+       LOG(log_error, logtype_afpd, "cannot chown() file [%s] (uid = %d): %s", 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", path, uid, strerror(errno));
+           goto recursive_chown_end;
+       }
+       while (NULL != (entry=readdir(odir)) ) {
+           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;
+}
+#endif
+
+/* This is equivalent of unix rename(). */
+int unix_rename(const char *oldpath, const char *newpath)
+{
+#if 0
+       char pd_name[PATH_MAX+1];
+       int i;
+        struct stat pd_stat;
+        uid_t uid;
+#endif
+
+       if (rename(oldpath, newpath) < 0)
+               return -1;
+#if 0
+       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",
+               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", 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",
+                   pd_name, geteuid(), strerror(errno));
+            seteuid(uid);
+       }
+#endif
+       return 0;
+}
+