r4339: - rename auth_guest to auth_anonymous
[samba.git] / source4 / auth / auth_util.c
index 652c157c7ab1e74b47a37938192e093cb70b49cc..9af4410a93a63f0927e27d6e428d0d8e142e31f6 100644 (file)
 */
 
 #include "includes.h"
+#include "librpc/gen_ndr/ndr_samr.h"
+#include "librpc/gen_ndr/ndr_netlogon.h"
+#include "librpc/gen_ndr/ndr_security.h"
+#include "auth/auth.h"
 
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_AUTH
@@ -294,127 +298,119 @@ BOOL make_user_info_guest(TALLOC_CTX *mem_ctx,
 }
 
 /****************************************************************************
- prints a NT_USER_TOKEN to debug output.
+ prints a struct security_token to debug output.
 ****************************************************************************/
-
-void debug_nt_user_token(int dbg_class, int dbg_lev, const NT_USER_TOKEN *token)
+void debug_security_token(int dbg_class, int dbg_lev, const struct security_token *token)
 {
        TALLOC_CTX *mem_ctx;
 
        size_t     i;
        
        if (!token) {
-               DEBUGC(dbg_class, dbg_lev, ("NT user token: (NULL)\n"));
+               DEBUGC(dbg_class, dbg_lev, ("Security token: (NULL)\n"));
                return;
        }
        
-       mem_ctx = talloc_init("debug_nt_user_token()");
+       mem_ctx = talloc_init("debug_security_token()");
        if (!mem_ctx) {
                return;
        }
 
-       DEBUGC(dbg_class, dbg_lev, ("NT user token of user %s\n",
-                                   dom_sid_string(mem_ctx, token->user_sids[0]) ));
-       DEBUGADDC(dbg_class, dbg_lev, ("contains %lu SIDs\n", (unsigned long)token->num_sids));
-       for (i = 0; i < token->num_sids; i++)
-               DEBUGADDC(dbg_class, dbg_lev, ("SID[%3lu]: %s\n", (unsigned long)i, 
-                                              dom_sid_string(mem_ctx, token->user_sids[i])));
+       DEBUGC(dbg_class, dbg_lev, ("Security token of user %s\n",
+                                   dom_sid_string(mem_ctx, token->user_sid) ));
+       DEBUGADDC(dbg_class, dbg_lev, ("contains %lu SIDs\n", 
+                                      (unsigned long)token->num_sids));
+       for (i = 0; i < token->num_sids; i++) {
+               DEBUGADDC(dbg_class, dbg_lev, 
+                         ("SID[%3lu]: %s\n", (unsigned long)i, 
+                          dom_sid_string(mem_ctx, token->sids[i])));
+       }
 
        talloc_destroy(mem_ctx);
 }
 
 /****************************************************************************
- prints a NT_USER_TOKEN to debug output.
+ prints a struct auth_session_info security token to debug output.
 ****************************************************************************/
-
-void debug_session_info(int dbg_class, int dbg_lev, const struct auth_session_info *session_info)
+void debug_session_info(int dbg_class, int dbg_lev, 
+                       const struct auth_session_info *session_info)
 {
        if (!session_info) {
                DEBUGC(dbg_class, dbg_lev, ("Session Info: (NULL)\n"));
                return; 
        }
 
-       debug_nt_user_token(dbg_class, dbg_lev, session_info->nt_user_token);
+       debug_security_token(dbg_class, dbg_lev, session_info->security_token);
 }
 
 /****************************************************************************
  Create the SID list for this user.
 ****************************************************************************/
-
-NTSTATUS create_nt_user_token(TALLOC_CTX *mem_ctx, 
-                             struct dom_sid *user_sid, struct dom_sid *group_sid, 
-                             int n_groupSIDs, struct dom_sid **groupSIDs, 
-                             BOOL is_guest, struct nt_user_token **token)
+NTSTATUS create_security_token(TALLOC_CTX *mem_ctx, 
+                              struct dom_sid *user_sid, struct dom_sid *group_sid, 
+                              int n_groupSIDs, struct dom_sid **groupSIDs, 
+                              BOOL is_guest, struct security_token **token)
 {
-       NTSTATUS       nt_status = NT_STATUS_OK;
-       struct nt_user_token *ptoken;
+       struct security_token *ptoken;
        int i;
-       int sid_ndx;
-       
-       if (!(ptoken = talloc_p(mem_ctx, struct nt_user_token))) {
-               DEBUG(0, ("create_nt_token: Out of memory allocating token\n"));
-               nt_status = NT_STATUS_NO_MEMORY;
-               return nt_status;
-       }
+       NTSTATUS status;
 
-       ptoken->num_sids = 0;
+       ptoken = security_token_initialise(mem_ctx);
+       if (ptoken == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
 
-       if (!(ptoken->user_sids = talloc_array_p(mem_ctx, struct dom_sid*, n_groupSIDs + 5))) {
-               DEBUG(0, ("create_nt_token: Out of memory allocating SIDs\n"));
-               nt_status = NT_STATUS_NO_MEMORY;
-               return nt_status;
+       ptoken->sids = talloc_array_p(ptoken, struct dom_sid *, n_groupSIDs + 5);
+       if (!ptoken->sids) {
+               return NT_STATUS_NO_MEMORY;
        }
-       
-       /*
-        * Note - user SID *MUST* be first in token !
-        * se_access_check depends on this.
-        *
-        * Primary group SID is second in token. Convention.
-        */
 
-       ptoken->user_sids[PRIMARY_USER_SID_INDEX] = user_sid;
-       ptoken->num_sids++;
-       ptoken->user_sids[PRIMARY_GROUP_SID_INDEX] = group_sid;
-       ptoken->num_sids++;
+       ptoken->user_sid = user_sid;
+       ptoken->group_sid = group_sid;
+       ptoken->privilege_mask = 0;
+
+       ptoken->sids[0] = user_sid;
+       ptoken->sids[1] = group_sid;
 
        /*
         * Finally add the "standard" SIDs.
         * The only difference between guest and "anonymous" (which we
         * don't really support) is the addition of Authenticated_Users.
         */
-       ptoken->user_sids[2] = dom_sid_parse_talloc(mem_ctx, SID_WORLD);
-       ptoken->user_sids[3] = dom_sid_parse_talloc(mem_ctx, SID_NETWORK);
-
-       if (is_guest) {
-               ptoken->user_sids[4] = dom_sid_parse_talloc(mem_ctx, SID_BUILTIN_GUESTS);
-               ptoken->num_sids++;
-       } else {
-               ptoken->user_sids[4] = dom_sid_parse_talloc(mem_ctx, SID_AUTHENTICATED_USERS);
-               ptoken->num_sids++;
-       }
-
-       sid_ndx = 5; /* next available spot */
+       ptoken->sids[2] = dom_sid_parse_talloc(mem_ctx, SID_WORLD);
+       ptoken->sids[3] = dom_sid_parse_talloc(mem_ctx, SID_NT_NETWORK);
+       ptoken->sids[4] = dom_sid_parse_talloc(mem_ctx, 
+                                              is_guest?SID_BUILTIN_GUESTS:
+                                              SID_NT_AUTHENTICATED_USERS);
+       ptoken->num_sids = 5;
 
        for (i = 0; i < n_groupSIDs; i++) {
                size_t check_sid_idx;
-               for (check_sid_idx = 1; check_sid_idx < ptoken->num_sids; check_sid_idx++) {
-                       if (sid_equal(ptoken->user_sids[check_sid_idx], 
-                                     groupSIDs[i])) {
+               for (check_sid_idx = 1; 
+                    check_sid_idx < ptoken->num_sids; 
+                    check_sid_idx++) {
+                       if (dom_sid_equal(ptoken->sids[check_sid_idx], groupSIDs[i])) {
                                break;
                        }
                }
                
-               if (check_sid_idx >= ptoken->num_sids) /* Not found already */ {
-                       ptoken->user_sids[sid_ndx++] = groupSIDs[i];
-                       ptoken->num_sids++;
+               if (check_sid_idx == ptoken->num_sids) {
+                       ptoken->sids[ptoken->num_sids++] = groupSIDs[i];
                }
        }
+
+       /* setup the privilege mask for this token */
+       status = samdb_privilege_setup(ptoken);
+       if (!NT_STATUS_IS_OK(status)) {
+               talloc_free(ptoken);
+               return status;
+       }
        
-       debug_nt_user_token(DBGC_AUTH, 10, ptoken);
+       debug_security_token(DBGC_AUTH, 10, ptoken);
        
        *token = ptoken;
 
-       return nt_status;
+       return NT_STATUS_OK;
 }
 
 /***************************************************************************
@@ -435,52 +431,143 @@ NTSTATUS make_server_info(const TALLOC_CTX *mem_ctx,
 }
 
 /***************************************************************************
- Make (and fill) a user_info struct for a guest login.
+ Make a server_info struct from the info3 returned by a domain logon 
 ***************************************************************************/
-NTSTATUS make_server_info_guest(const TALLOC_CTX *mem_ctx, struct auth_serversupplied_info **server_info)
+
+NTSTATUS make_server_info_netlogon_validation(TALLOC_CTX *mem_ctx, 
+                                             const char *internal_username,
+                                             struct auth_serversupplied_info **server_info, 
+                                             uint16 validation_level, 
+                                             union netr_Validation *validation) 
 {
        NTSTATUS nt_status;
-       static const char zeros[16];
+       struct netr_SamBaseInfo *base = NULL;
+       switch (validation_level) {
+       case 2:
+               if (!validation || !validation->sam2) {
+                       return NT_STATUS_INVALID_PARAMETER;
+               }
+               base = &validation->sam2->base;
+               break;
+       case 3:
+               if (!validation || !validation->sam3) {
+                       return NT_STATUS_INVALID_PARAMETER;
+               }
+               base = &validation->sam3->base;
+               break;
+       case 6:
+               if (!validation || !validation->sam6) {
+                       return NT_STATUS_INVALID_PARAMETER;
+               }
+               base = &validation->sam6->base;
+               break;
+       default:
+               return NT_STATUS_INVALID_LEVEL;
+       }
 
-       nt_status = make_server_info(mem_ctx, server_info, "");
+       nt_status = make_server_info(mem_ctx, server_info, internal_username);
 
        if (!NT_STATUS_IS_OK(nt_status)) {
                return nt_status;
        }
        
-       (*server_info)->guest = True;
+       (*server_info)->guest = False;
+
+       /* 
+          Here is where we should check the list of
+          trusted domains, and verify that the SID 
+          matches.
+       */
+
+       (*server_info)->user_sid = dom_sid_add_rid(*server_info, dom_sid_dup(*server_info, base->domain_sid), base->rid);
+       (*server_info)->primary_group_sid = dom_sid_add_rid(*server_info, dom_sid_dup(*server_info, base->domain_sid), base->primary_gid);
+
+       (*server_info)->domain_groups = talloc_array_p((*server_info), struct dom_sid*, base->group_count);
+       if (!(*server_info)->domain_groups) {
+               return NT_STATUS_NO_MEMORY;
+       }
+       
+       for ((*server_info)->n_domain_groups = 0;
+            (*server_info)->n_domain_groups < base->group_count; 
+            (*server_info)->n_domain_groups++) {
+               struct dom_sid *sid;
+               sid = dom_sid_dup((*server_info)->domain_groups, base->domain_sid);
+               if (!sid) {
+                       return NT_STATUS_NO_MEMORY;
+               }
+               (*server_info)->domain_groups[(*server_info)->n_domain_groups]
+                       = dom_sid_add_rid(*server_info, sid, 
+                                         base->groupids[(*server_info)->n_domain_groups].rid);
+               if (!(*server_info)->domain_groups[(*server_info)->n_domain_groups]) {
+                       return NT_STATUS_NO_MEMORY;
+               }
+       }
 
-       (*server_info)->user_sid = dom_sid_parse_talloc((*server_info), SID_ANONYMOUS);
-       (*server_info)->primary_group_sid = dom_sid_parse_talloc((*server_info), SID_BUILTIN_GUESTS);
-       (*server_info)->n_domain_groups = 0;
-       (*server_info)->domain_groups = NULL;
+       /* Copy 'other' sids.  We need to do sid filtering here to
+          prevent possible elevation of privileges.  See:
+
+           http://www.microsoft.com/windows2000/techinfo/administration/security/sidfilter.asp
+         */
+
+       if (validation_level == 3) {
+               int i;
+               (*server_info)->domain_groups
+                       = talloc_realloc_p((*server_info), 
+                                          (*server_info)->domain_groups, 
+                                          struct dom_sid*, 
+                                          base->group_count + validation->sam3->sidcount);
+               
+               if (!(*server_info)->domain_groups) {
+                       return NT_STATUS_NO_MEMORY;
+               }
        
-       /* annoying, but the Guest really does have a session key, 
-          and it is all zeros! */
-       (*server_info)->user_session_key = data_blob(zeros, sizeof(zeros));
-       (*server_info)->lm_session_key = data_blob(zeros, sizeof(zeros));
-
-       (*server_info)->account_name = "";
-       (*server_info)->domain = "";
-       (*server_info)->full_name = "Anonymous";
-       (*server_info)->logon_script = "";
-       (*server_info)->profile_path = "";
-       (*server_info)->home_directory = "";
-       (*server_info)->home_drive = "";
-
-       (*server_info)->last_logon = 0;
-       (*server_info)->last_logoff = 0;
-       (*server_info)->acct_expiry = 0;
-       (*server_info)->last_password_change = 0;
-       (*server_info)->allow_password_change = 0;
-       (*server_info)->force_password_change = 0;
-
-       (*server_info)->logon_count = 0;
-       (*server_info)->bad_password_count = 0;
-
-       (*server_info)->acct_flags = ACB_NORMAL;
+               for (i = 0; i < validation->sam3->sidcount; i++) {
+                       (*server_info)->domain_groups[(*server_info)->n_domain_groups + i] = 
+                               dom_sid_dup((*server_info)->domain_groups, 
+                                           validation->sam3->sids[i].sid);
+               }
 
-       return nt_status;
+               /* Where are the 'global' sids?... */
+       }
+
+       if (base->account_name.string) {
+               (*server_info)->account_name = talloc_reference(*server_info, base->account_name.string);
+       } else {
+               (*server_info)->account_name = talloc_strdup(*server_info, internal_username);
+       }
+       
+       (*server_info)->domain = talloc_reference(*server_info, base->domain.string);
+       (*server_info)->full_name = talloc_reference(*server_info, base->full_name.string);
+       (*server_info)->logon_script = talloc_reference(*server_info, base->logon_script.string);
+       (*server_info)->profile_path = talloc_reference(*server_info, base->profile_path.string);
+       (*server_info)->home_directory = talloc_reference(*server_info, base->home_directory.string);
+       (*server_info)->home_drive = talloc_reference(*server_info, base->home_drive.string);
+       (*server_info)->last_logon = base->last_logon;
+       (*server_info)->last_logoff = base->last_logoff;
+       (*server_info)->acct_expiry = base->acct_expiry;
+       (*server_info)->last_password_change = base->last_password_change;
+       (*server_info)->allow_password_change = base->allow_password_change;
+       (*server_info)->force_password_change = base->force_password_change;
+
+       (*server_info)->logon_count = base->logon_count;
+       (*server_info)->bad_password_count = base->bad_password_count;
+
+       (*server_info)->acct_flags = base->acct_flags;
+
+       /* ensure we are never given NULL session keys */
+       
+       if (all_zero(base->key.key, sizeof(base->key.key))) {
+               (*server_info)->user_session_key = data_blob(NULL, 0);
+       } else {
+               (*server_info)->user_session_key = data_blob_talloc((*server_info), base->key.key, sizeof(base->key.key));
+       }
+
+       if (all_zero(base->LMSessKey.key, sizeof(base->LMSessKey.key))) {
+               (*server_info)->lm_session_key = data_blob(NULL, 0);
+       } else {
+               (*server_info)->lm_session_key = data_blob_talloc((*server_info), base->LMSessKey.key, sizeof(base->LMSessKey.key));
+       }
+       return NT_STATUS_OK;
 }
 
 /***************************************************************************
@@ -532,31 +619,34 @@ BOOL make_auth_methods(struct auth_context *auth_context, struct auth_methods **
        return True;
 }
 
-NTSTATUS make_session_info(struct auth_serversupplied_info *server_info, 
+NTSTATUS make_session_info(TALLOC_CTX *mem_ctx, 
+                          struct auth_serversupplied_info *server_info, 
                           struct auth_session_info **session_info) 
 {
        NTSTATUS nt_status;
 
-       *session_info = talloc_p(server_info, struct auth_session_info);
+       *session_info = talloc_p(mem_ctx, struct auth_session_info);
        if (!*session_info) {
                return NT_STATUS_NO_MEMORY;
        }
        
-       (*session_info)->refcount = 1;
        (*session_info)->server_info = server_info;
+       talloc_reference(*session_info, (*session_info)->server_info);
 
        /* unless set otherwise, the session key is the user session
         * key from the auth subsystem */
  
        (*session_info)->session_key = server_info->user_session_key;
+
+       /* we should search for local groups here */
        
-       nt_status = create_nt_user_token((*session_info), 
-                                        server_info->user_sid, 
-                                        server_info->primary_group_sid, 
-                                        server_info->n_domain_groups, 
-                                        server_info->domain_groups,
-                                        False, 
-                                        &(*session_info)->nt_user_token);
+       nt_status = create_security_token((*session_info), 
+                                         server_info->user_sid, 
+                                         server_info->primary_group_sid, 
+                                         server_info->n_domain_groups, 
+                                         server_info->domain_groups,
+                                         False, 
+                                         &(*session_info)->security_token);
        
        return nt_status;
 }
@@ -568,12 +658,7 @@ NTSTATUS make_session_info(struct auth_serversupplied_info *server_info,
 void free_session_info(struct auth_session_info **session_info)
 {
        DEBUG(5,("attempting to free a session_info structure\n"));
-       if (*session_info) {
-               (*session_info)->refcount--;
-               if ((*session_info)->refcount <= 0) {
-                       talloc_free((*session_info));
-               }
-       }
+       talloc_free((*session_info));
        *session_info = NULL;
 }