Merge branch 'master' of ssh://jra@git.samba.org/data/git/samba
[kai/samba-autobuild/.git] / source4 / ntvfs / unixuid / vfs_unixuid.c
index 448d610819ec54045acdbf5efe9247af87254ecd..70ad6ee253411b15d4f8a91ca337982232ddd022 100644 (file)
@@ -26,6 +26,8 @@
 #include "auth/auth.h"
 #include "ntvfs/ntvfs.h"
 #include "libcli/wbclient/wbclient.h"
+#define TEVENT_DEPRECATED
+#include <tevent.h>
 
 struct unixuid_private {
        struct wbc_context *wbc_ctx;
@@ -91,6 +93,64 @@ static NTSTATUS set_unix_security(struct unix_sec_ctx *sec)
        return NT_STATUS_OK;
 }
 
+static int unixuid_nesting_level;
+
+/*
+  called at the start and end of a tevent nesting loop. Needs to save/restore
+  unix security context
+ */
+static int unixuid_event_nesting_hook(struct tevent_context *ev,
+                                     void *private_data,
+                                     uint32_t level,
+                                     bool begin,
+                                     void *stack_ptr,
+                                     const char *location)
+{
+       struct unix_sec_ctx *sec_ctx;
+
+       if (unixuid_nesting_level == 0) {
+               /* we don't need to do anything unless we are nested
+                  inside of a call in this module */
+               return 0;
+       }
+
+       if (begin) {
+               sec_ctx = save_unix_security(ev);
+               if (sec_ctx == NULL) {
+                       DEBUG(0,("%s: Failed to save security context\n", location));
+                       return -1;
+               }
+               *(struct unix_sec_ctx **)stack_ptr = sec_ctx;
+               if (seteuid(0) != 0 || setegid(0) != 0) {
+                       DEBUG(0,("%s: Failed to change to root\n", location));
+                       return -1;                      
+               }
+       } else {
+               /* called when we come out of a nesting level */
+               NTSTATUS status;
+
+               sec_ctx = *(struct unix_sec_ctx **)stack_ptr;
+               if (sec_ctx == NULL) {
+                       /* this happens the first time this function
+                          is called, as we install the hook while
+                          inside an event in unixuid_connect() */
+                       return 0;
+               }
+
+               sec_ctx = talloc_get_type_abort(sec_ctx, struct unix_sec_ctx);
+               status = set_unix_security(sec_ctx);
+               talloc_free(sec_ctx);
+               if (!NT_STATUS_IS_OK(status)) {
+                       DEBUG(0,("%s: Failed to revert security context (%s)\n", 
+                                location, nt_errstr(status)));
+                       return -1;
+               }
+       }
+
+       return 0;
+}
+
+
 /*
   form a unix_sec_ctx from the current security_token
 */
@@ -170,12 +230,15 @@ static NTSTATUS nt_token_to_unix_security(struct ntvfs_module_context *ntvfs,
 static NTSTATUS unixuid_setup_security(struct ntvfs_module_context *ntvfs,
                                       struct ntvfs_request *req, struct unix_sec_ctx **sec)
 {
-       struct unixuid_private *private = ntvfs->private_data;
+       struct unixuid_private *priv = ntvfs->private_data;
        struct security_token *token;
        struct unix_sec_ctx *newsec;
        NTSTATUS status;
 
-       if (req->session_info == NULL) {
+       /* If we are asked to set up, but have not had a successful
+        * session setup or tree connect, then these may not be filled
+        * in.  ACCESS_DENIED is the right error code here */
+       if (req->session_info == NULL || priv == NULL) {
                return NT_STATUS_ACCESS_DENIED;
        }
 
@@ -186,20 +249,20 @@ static NTSTATUS unixuid_setup_security(struct ntvfs_module_context *ntvfs,
                return NT_STATUS_NO_MEMORY;
        }
 
-       if (token == private->last_token) {
-               newsec = private->last_sec_ctx;
+       if (token == priv->last_token) {
+               newsec = priv->last_sec_ctx;
        } else {
                status = nt_token_to_unix_security(ntvfs, req, token, &newsec);
                if (!NT_STATUS_IS_OK(status)) {
                        talloc_free(*sec);
                        return status;
                }
-               if (private->last_sec_ctx) {
-                       talloc_free(private->last_sec_ctx);
+               if (priv->last_sec_ctx) {
+                       talloc_free(priv->last_sec_ctx);
                }
-               private->last_sec_ctx = newsec;
-               private->last_token = token;
-               talloc_steal(private, newsec);
+               priv->last_sec_ctx = newsec;
+               priv->last_token = token;
+               talloc_steal(priv, newsec);
        }
 
        status = set_unix_security(newsec);
@@ -219,7 +282,9 @@ static NTSTATUS unixuid_setup_security(struct ntvfs_module_context *ntvfs,
        struct unix_sec_ctx *sec; \
        status = unixuid_setup_security(ntvfs, req, &sec); \
        NT_STATUS_NOT_OK_RETURN(status); \
+       unixuid_nesting_level++; \
        status = ntvfs_next_##op args; \
+       unixuid_nesting_level--; \
        status2 = set_unix_security(sec); \
        talloc_free(sec); \
        if (!NT_STATUS_IS_OK(status2)) smb_panic("Unable to reset security context"); \
@@ -231,31 +296,35 @@ static NTSTATUS unixuid_setup_security(struct ntvfs_module_context *ntvfs,
   connect to a share - used when a tree_connect operation comes in.
 */
 static NTSTATUS unixuid_connect(struct ntvfs_module_context *ntvfs,
-                               struct ntvfs_request *req, const char *sharename)
+                               struct ntvfs_request *req, union smb_tcon *tcon)
 {
-       struct unixuid_private *private;
+       struct unixuid_private *priv;
        NTSTATUS status;
 
-       private = talloc(ntvfs, struct unixuid_private);
-       if (!private) {
+       priv = talloc(ntvfs, struct unixuid_private);
+       if (!priv) {
                return NT_STATUS_NO_MEMORY;
        }
 
-       private->wbc_ctx = wbc_init(private, ntvfs->ctx->msg_ctx,
+       priv->wbc_ctx = wbc_init(priv, ntvfs->ctx->msg_ctx,
                                    ntvfs->ctx->event_ctx);
-       if (private->wbc_ctx == NULL) {
-               talloc_free(private);
+       if (priv->wbc_ctx == NULL) {
+               talloc_free(priv);
                return NT_STATUS_INTERNAL_ERROR;
        }
 
-       ntvfs->private_data = private;
-       private->last_sec_ctx = NULL;
-       private->last_token = NULL;
+       priv->last_sec_ctx = NULL;
+       priv->last_token = NULL;
+       ntvfs->private_data = priv;
+
+       tevent_loop_set_nesting_hook(ntvfs->ctx->event_ctx, 
+                                    unixuid_event_nesting_hook,
+                                    &unixuid_nesting_level);
 
        /* we don't use PASS_THRU_REQ here, as the connect operation runs with 
           root privileges. This allows the backends to setup any database
           links they might need during the connect. */
-       status = ntvfs_next_connect(ntvfs, req, sharename);
+       status = ntvfs_next_connect(ntvfs, req, tcon);
 
        return status;
 }
@@ -265,10 +334,10 @@ static NTSTATUS unixuid_connect(struct ntvfs_module_context *ntvfs,
 */
 static NTSTATUS unixuid_disconnect(struct ntvfs_module_context *ntvfs)
 {
-       struct unixuid_private *private = ntvfs->private_data;
+       struct unixuid_private *priv = ntvfs->private_data;
        NTSTATUS status;
 
-       talloc_free(private);
+       talloc_free(priv);
        ntvfs->private_data = NULL;
 
        status = ntvfs_next_disconnect(ntvfs);
@@ -509,12 +578,12 @@ static NTSTATUS unixuid_exit(struct ntvfs_module_context *ntvfs,
 static NTSTATUS unixuid_logoff(struct ntvfs_module_context *ntvfs,
                              struct ntvfs_request *req)
 {
-       struct unixuid_private *private = ntvfs->private_data;
+       struct unixuid_private *priv = ntvfs->private_data;
        NTSTATUS status;
 
        PASS_THRU_REQ(ntvfs, req, logoff, (ntvfs, req));
 
-       private->last_token = NULL;
+       priv->last_token = NULL;
 
        return status;
 }
@@ -524,11 +593,11 @@ static NTSTATUS unixuid_logoff(struct ntvfs_module_context *ntvfs,
 */
 static NTSTATUS unixuid_async_setup(struct ntvfs_module_context *ntvfs,
                                    struct ntvfs_request *req, 
-                                   void *private)
+                                   void *private_data)
 {
        NTSTATUS status;
 
-       PASS_THRU_REQ(ntvfs, req, async_setup, (ntvfs, req, private));
+       PASS_THRU_REQ(ntvfs, req, async_setup, (ntvfs, req, private_data));
 
        return status;
 }