]> arthur.barton.de Git - netatalk.git/commitdiff
Add become_root() capability to nfsv4_chmod()
authorFrank Lahm <franklahm@googlemail.com>
Tue, 31 Jan 2012 10:58:10 +0000 (11:58 +0100)
committerFrank Lahm <franklahm@googlemail.com>
Tue, 31 Jan 2012 10:58:10 +0000 (11:58 +0100)
include/atalk/unix.h
libatalk/acl/unix.c
libatalk/util/unix.c

index 05d71059a2b7693ea4c61773c651ef8c7c8b4f8e..62303692b2668c6fa2f3daffd631d192bb1caaa1 100644 (file)
@@ -43,4 +43,8 @@ extern int unix_rename(int sfd, const char *oldpath, int dfd, const char *newpat
 extern int copy_file(int sfd, const char *src, const char *dst, mode_t mode);
 extern int copy_file_fd(int sfd, int dfd);
 extern int copy_ea(const char *ea, int sfd, const char *src, const char *dst, mode_t mode);
+
+extern void become_root(void);
+extern void unbecome_root(void);
+
 #endif  /* ATALK_UNIX_H */
index 88cc77d23ce0546e04581de225a88e5dca6275bf..0d03488e2e8ee68801274bd6b0cde73c05fcc263 100644 (file)
@@ -33,6 +33,7 @@
 #include <atalk/afp.h>
 #include <atalk/util.h>
 #include <atalk/acl.h>
+#include <atalk/unix.h>
 
 #ifdef HAVE_SOLARIS_ACLS
 
@@ -234,8 +235,16 @@ int nfsv4_chmod(char *name, mode_t mode)
     if (chmod(name, mode) != 0) /* (3) */
         goto exit;
 
-    if ((nnaces = get_nfsv4_acl(name, &nacl)) == -1) /* (4) */
-        goto exit;
+    if ((nnaces = get_nfsv4_acl(name, &nacl)) == -1) {/* (4) */
+        if (errno != EACCES)
+            goto exit;
+        become_root();
+        nnaces = get_nfsv4_acl(name, &nacl);
+        unbecome_root();
+        if (nnaces == -1)
+            goto exit;
+    }
+
     if ((nnaces = strip_nontrivial_aces(&nacl, nnaces)) == -1) /* (5) */
         goto exit;
 
@@ -243,8 +252,17 @@ int nfsv4_chmod(char *name, mode_t mode)
         goto exit;
 
     if ((ret = acl(name, ACE_SETACL, noaces + nnaces, cacl)) != 0) {
-        LOG(log_error, logtype_afpd, "nfsv4_chmod: error setting acl: %s", strerror(errno));
-        goto exit;
+        if (errno != EACCES) {
+            LOG(log_error, logtype_afpd, "nfsv4_chmod: error setting acl: %s", strerror(errno));
+            goto exit;
+        }
+        become_root();
+        ret = acl(name, ACE_SETACL, noaces + nnaces, cacl);
+        unbecome_root();
+        if (ret != 0) {
+            LOG(log_error, logtype_afpd, "nfsv4_chmod: error setting acl: %s", strerror(errno));
+            goto exit;
+        }
     }
 
 exit:
@@ -252,7 +270,7 @@ exit:
     if (nacl) free(nacl);
     if (cacl) free(cacl);
 
-    LOG(log_debug, logtype_afpd, "nfsv4_chmod(\"%s/%s\", %04o): result: %u",
+    LOG(log_debug, logtype_afpd, "nfsv4_chmod(\"%s/%s\", %04o): result: %d",
         getcwdpath(), name, mode, ret);
 
     return ret;
index 0ac210ec2c8d4e3e1d99ab16a33f7067cfabd24f..88371e8a082f397f22d4ecbf4b7ee7eefbd0753c 100644 (file)
@@ -95,6 +95,22 @@ int daemonize(int nochdir, int noclose)
     return 0;
 }
 
+static uid_t saved_uid = -1;
+
+void become_root(void)
+{
+    saved_uid = geteuid();
+    if (seteuid(0) != 0)
+        AFP_PANIC("Can't seteuid(0)");
+}
+
+void unbecome_root(void)
+{
+    if (saved_uid == -1 || seteuid(saved_uid) < 0)
+        AFP_PANIC("Can't seteuid back");
+    saved_uid = -1;
+}
+
 /*!
  * @brief get cwd in static buffer
  *