]> arthur.barton.de Git - netatalk.git/commitdiff
Moving things between two folders on the same volume but different group ownership...
authorrlewczuk <rlewczuk>
Tue, 7 Jan 2003 15:55:21 +0000 (15:55 +0000)
committerrlewczuk <rlewczuk>
Tue, 7 Jan 2003 15:55:21 +0000 (15:55 +0000)
If destination directory has SGID, group ownership for is changed while renam()ing.
This is very ugly hack, IMO should be implemented along with force gid/uid stuff.

etc/afpd/directory.c
etc/afpd/file.c
etc/afpd/unix.c
etc/afpd/unix.h

index 1bc84cbe74f9f70d992a72a3410f29e089de783a..f2d4b8e0f138cf62c82ff8531ce5211c8e850cb5 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: directory.c,v 1.53 2002-12-14 04:01:01 didg Exp $
+ * $Id: directory.c,v 1.54 2003-01-07 15:55:21 rlewczuk Exp $
  *
  * Copyright (c) 1990,1993 Regents of The University of Michigan.
  * All Rights Reserved.  See COPYRIGHT.
@@ -1747,7 +1747,7 @@ const int noadouble;
     int                        len, err;
         
     /* existence check moved to afp_moveandrename */
-    if ( rename( src, dst ) < 0 ) {
+    if ( unix_rename( src, dst ) < 0 ) {
         switch ( errno ) {
         case ENOENT :
             return( AFPERR_NOOBJ );
index c3e0780c2131bb3425823219760760b5f166a6fa..76072c9c77cb08f72915571bb8fe076371689209 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: file.c,v 1.68 2002-10-25 11:10:48 didg Exp $
+ * $Id: file.c,v 1.69 2003-01-07 15:55:21 rlewczuk Exp $
  *
  * Copyright (c) 1990,1993 Regents of The University of Michigan.
  * All Rights Reserved.  See COPYRIGHT.
@@ -908,7 +908,7 @@ struct adouble    *adp;
     LOG(log_info, logtype_afpd, "begin renamefile:");
 #endif /* DEBUG */
 
-    if ( rename( src, dst ) < 0 ) {
+    if ( unix_rename( src, dst ) < 0 ) {
         switch ( errno ) {
         case ENOENT :
             return( AFPERR_NOOBJ );
@@ -931,7 +931,7 @@ struct adouble    *adp;
     strcpy( adsrc, ad_path( src, 0 ));
     rc = 0;
 rename_retry:
-    if (rename( adsrc, ad_path( dst, 0 )) < 0 ) {
+    if (unix_rename( adsrc, ad_path( dst, 0 )) < 0 ) {
         struct stat st;
 
         switch ( errno ) {
index cf4b10e26761c6bfbb7889dfd4b32d1be75e5cd9..c8bb933aa7305a015987492bdb136bc7645d6aeb 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.40 2003-01-07 15:55:22 rlewczuk Exp $
  *
  * Copyright (c) 1990,1993 Regents of The University of Michigan.
  * All Rights Reserved.  See COPYRIGHT.
@@ -639,3 +639,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;
+}
+
index a82a4dae77845dd38fe97b1d5322c4c305ff98a4..3eba6c471b38fa5b292de546aeea935de47c8035 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: unix.h,v 1.11 2002-08-30 10:19:19 didg Exp $
+ * $Id: unix.h,v 1.12 2003-01-07 15:55:22 rlewczuk Exp $
  */
 
 #ifndef AFPD_UNIX_H
@@ -104,6 +104,7 @@ extern int setdirmode   __P((const mode_t, const int, const int));
 extern int setdeskowner __P((const uid_t, const gid_t));
 extern int setdirowner  __P((const uid_t, const gid_t, const int));
 extern int setfilmode   __P((char *, mode_t , struct stat *));
+extern int unix_rename  __P((const char *oldpath, const char *newpath));
 
 extern void accessmode  __P((char *, struct maccess *, struct dir *, struct stat *));