s4-auth Move conversion of security_token to unix_token to auth
[amitay/samba.git] / source4 / ntvfs / unixuid / vfs_unixuid.c
index db22a85492ed8d69683a0dcddb7c0e0b8eb6ecf3..0221b4391c1de1caa807dc74595e044946bb9dca 100644 (file)
 #include "auth/auth.h"
 #include "ntvfs/ntvfs.h"
 #include "libcli/wbclient/wbclient.h"
+#define TEVENT_DEPRECATED
+#include <tevent.h>
+
+#if defined(UID_WRAPPER)
+#if !defined(UID_WRAPPER_REPLACE) && !defined(UID_WRAPPER_NOT_REPLACE)
+#define UID_WRAPPER_REPLACE
+#include "../uid_wrapper/uid_wrapper.h"
+#endif
+#else
+#define uwrap_enabled() 0
+#endif
+
+
+NTSTATUS ntvfs_unixuid_init(void);
 
 struct unixuid_private {
        struct wbc_context *wbc_ctx;
-       struct unix_sec_ctx *last_sec_ctx;
+       struct security_unix_token *last_sec_ctx;
        struct security_token *last_token;
 };
 
 
-
-struct unix_sec_ctx {
-       uid_t uid;
-       gid_t gid;
-       uint_t ngroups;
-       gid_t *groups;
-};
-
 /*
-  pull the current security context into a unix_sec_ctx
+  pull the current security context into a security_unix_token
 */
-static struct unix_sec_ctx *save_unix_security(TALLOC_CTX *mem_ctx)
+static struct security_unix_token *save_unix_security(TALLOC_CTX *mem_ctx)
 {
-       struct unix_sec_ctx *sec = talloc(mem_ctx, struct unix_sec_ctx);
+       struct security_unix_token *sec = talloc(mem_ctx, struct security_unix_token);
        if (sec == NULL) {
                return NULL;
        }
@@ -73,9 +79,9 @@ static struct unix_sec_ctx *save_unix_security(TALLOC_CTX *mem_ctx)
 }
 
 /*
-  set the current security context from a unix_sec_ctx
+  set the current security context from a security_unix_token
 */
-static NTSTATUS set_unix_security(struct unix_sec_ctx *sec)
+static NTSTATUS set_unix_security(struct security_unix_token *sec)
 {
        seteuid(0);
 
@@ -91,91 +97,94 @@ static NTSTATUS set_unix_security(struct unix_sec_ctx *sec)
        return NT_STATUS_OK;
 }
 
-/*
-  form a unix_sec_ctx from the current security_token
-*/
-static NTSTATUS nt_token_to_unix_security(struct ntvfs_module_context *ntvfs,
-                                         struct ntvfs_request *req,
-                                         struct security_token *token,
-                                         struct unix_sec_ctx **sec)
-{
-       struct unixuid_private *priv = ntvfs->private_data;
-       int i;
-       NTSTATUS status;
-       struct id_mapping *ids;
-       struct composite_context *ctx;
-       *sec = talloc(req, struct unix_sec_ctx);
+static int unixuid_nesting_level;
 
-       /* we can't do unix security without a user and group */
-       if (token->num_sids < 2) {
-               return NT_STATUS_ACCESS_DENIED;
+/*
+  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 security_unix_token *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;
        }
 
-       ids = talloc_array(req, struct id_mapping, token->num_sids);
-       NT_STATUS_HAVE_NO_MEMORY(ids);
-
-       ids[0].unixid = NULL;
-       ids[0].sid = token->user_sid;
-       ids[0].status = NT_STATUS_NONE_MAPPED;
-
-       ids[1].unixid = NULL;
-       ids[1].sid = token->group_sid;
-       ids[1].status = NT_STATUS_NONE_MAPPED;
-
-       (*sec)->ngroups = token->num_sids - 2;
-       (*sec)->groups = talloc_array(*sec, gid_t, (*sec)->ngroups);
-       NT_STATUS_HAVE_NO_MEMORY((*sec)->groups);
+       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 security_unix_token **)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 security_unix_token **)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;
+               }
 
-       for (i=0;i<(*sec)->ngroups;i++) {
-               ids[i+2].unixid = NULL;
-               ids[i+2].sid = token->sids[i+2];
-               ids[i+2].status = NT_STATUS_NONE_MAPPED;
+               sec_ctx = talloc_get_type_abort(sec_ctx, struct security_unix_token);
+               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;
+               }
        }
 
-       ctx = wbc_sids_to_xids_send(priv->wbc_ctx, ids, token->num_sids, ids);
-       NT_STATUS_HAVE_NO_MEMORY(ctx);
-
-       status = wbc_sids_to_xids_recv(ctx, &ids);
-       NT_STATUS_NOT_OK_RETURN(status);
-
-       if (ids[0].unixid->type == ID_TYPE_BOTH ||
-           ids[0].unixid->type == ID_TYPE_UID) {
-               (*sec)->uid = ids[0].unixid->id;
-       } else {
-               return NT_STATUS_INVALID_SID;
-       }
+       return 0;
+}
 
-       if (ids[1].unixid->type == ID_TYPE_BOTH ||
-           ids[1].unixid->type == ID_TYPE_GID) {
-               (*sec)->gid = ids[1].unixid->id;
-       } else {
-               return NT_STATUS_INVALID_SID;
-       }
 
-       for (i=0;i<(*sec)->ngroups;i++) {
-               if (ids[i+2].unixid->type == ID_TYPE_BOTH ||
-                   ids[i+2].unixid->type == ID_TYPE_GID) {
-                       (*sec)->groups[i] = ids[i+2].unixid->id;
-               } else {
-                       return NT_STATUS_INVALID_SID;
-               }
-       }
+/*
+  form a security_unix_token from the current security_token
+*/
+static NTSTATUS nt_token_to_unix_security(struct ntvfs_module_context *ntvfs,
+                                         struct ntvfs_request *req,
+                                         struct security_token *token,
+                                         struct security_unix_token **sec)
+{
+       struct unixuid_private *priv = ntvfs->private_data;
 
-       return NT_STATUS_OK;
+       return security_token_to_unix_token(req,
+                                           priv->wbc_ctx,
+                                           token, sec);
 }
 
 /*
   setup our unix security context according to the session authentication info
 */
 static NTSTATUS unixuid_setup_security(struct ntvfs_module_context *ntvfs,
-                                      struct ntvfs_request *req, struct unix_sec_ctx **sec)
+                                      struct ntvfs_request *req, struct security_unix_token **sec)
 {
        struct unixuid_private *priv = ntvfs->private_data;
        struct security_token *token;
-       struct unix_sec_ctx *newsec;
+       struct security_unix_token *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;
        }
 
@@ -216,10 +225,12 @@ static NTSTATUS unixuid_setup_security(struct ntvfs_module_context *ntvfs,
 */
 #define PASS_THRU_REQ(ntvfs, req, op, args) do { \
        NTSTATUS status2; \
-       struct unix_sec_ctx *sec; \
+       struct security_unix_token *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,7 +242,7 @@ 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 *priv;
        NTSTATUS status;
@@ -248,14 +259,18 @@ static NTSTATUS unixuid_connect(struct ntvfs_module_context *ntvfs,
                return NT_STATUS_INTERNAL_ERROR;
        }
 
-       ntvfs->private_data = priv;
        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;
 }