s4-auth rework session_info handling not to require an auth context
[kai/samba.git] / source4 / auth / session.c
index 0557187199a27a9ab398e686545cdec4d5499ea7..1028aa83201aef4a04f0cde97c6aaaeb9f76c0f3 100644 (file)
@@ -2,7 +2,7 @@
    Unix SMB/CIFS implementation.
    Authentication utility functions
    Copyright (C) Andrew Tridgell 1992-1998
-   Copyright (C) Andrew Bartlett 2001
+   Copyright (C) Andrew Bartlett 2001-2010
    Copyright (C) Jeremy Allison 2000-2001
    Copyright (C) Rafal Szczesniak 2002
    Copyright (C) Stefan Metzmacher 2005
 #include "libcli/security/security.h"
 #include "libcli/auth/libcli_auth.h"
 #include "dsdb/samdb/samdb.h"
-#include "auth/credentials/credentials.h"
-#include "param/param.h"
+#include "auth/session_proto.h"
 
-struct auth_session_info *anonymous_session(TALLOC_CTX *mem_ctx) 
+_PUBLIC_ struct auth_session_info *anonymous_session(TALLOC_CTX *mem_ctx, 
+                                           struct loadparm_context *lp_ctx)
 {
        NTSTATUS nt_status;
        struct auth_session_info *session_info = NULL;
-       nt_status = auth_anonymous_session_info(mem_ctx, global_loadparm, &session_info);
+       nt_status = auth_anonymous_session_info(mem_ctx, lp_ctx, &session_info);
        if (!NT_STATUS_IS_OK(nt_status)) {
                return NULL;
        }
        return session_info;
 }
 
-NTSTATUS auth_anonymous_session_info(TALLOC_CTX *parent_ctx, 
-                                    struct loadparm_context *lp_ctx,
-                                    struct auth_session_info **_session_info) 
+_PUBLIC_ NTSTATUS auth_generate_session_info(TALLOC_CTX *mem_ctx,
+                                            struct loadparm_context *lp_ctx, /* Optional, if you don't want privilages */
+                                            struct ldb_context *sam_ctx, /* Optional, if you don't want local groups */
+                                            struct auth_serversupplied_info *server_info,
+                                            uint32_t session_info_flags,
+                                            struct auth_session_info **_session_info)
 {
+       struct auth_session_info *session_info;
        NTSTATUS nt_status;
-       struct auth_serversupplied_info *server_info = NULL;
-       struct auth_session_info *session_info = NULL;
-       TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
-       
-       nt_status = auth_anonymous_server_info(mem_ctx,
-                                              lp_netbios_name(lp_ctx),
-                                              &server_info);
-       if (!NT_STATUS_IS_OK(nt_status)) {
-               talloc_free(mem_ctx);
-               return nt_status;
-       }
-
-       /* references the server_info into the session_info */
-       nt_status = auth_generate_session_info(parent_ctx, server_info, &session_info);
-       talloc_free(mem_ctx);
-
-       NT_STATUS_NOT_OK_RETURN(nt_status);
-
-       session_info->credentials = cli_credentials_init(session_info);
-       if (!session_info->credentials) {
-               return NT_STATUS_NO_MEMORY;
-       }
-
-       cli_credentials_set_conf(session_info->credentials, lp_ctx);
-       cli_credentials_set_anonymous(session_info->credentials);
-       
-       *_session_info = session_info;
-
-       return NT_STATUS_OK;
-}
-
-NTSTATUS auth_anonymous_server_info(TALLOC_CTX *mem_ctx, 
-                                   const char *netbios_name,
-                                   struct auth_serversupplied_info **_server_info) 
-{
-       struct auth_serversupplied_info *server_info;
-       server_info = talloc(mem_ctx, struct auth_serversupplied_info);
-       NT_STATUS_HAVE_NO_MEMORY(server_info);
-
-       server_info->account_sid = dom_sid_parse_talloc(server_info, SID_NT_ANONYMOUS);
-       NT_STATUS_HAVE_NO_MEMORY(server_info->account_sid);
-
-       /* is this correct? */
-       server_info->primary_group_sid = dom_sid_parse_talloc(server_info, SID_BUILTIN_GUESTS);
-       NT_STATUS_HAVE_NO_MEMORY(server_info->primary_group_sid);
-
-       server_info->n_domain_groups = 0;
-       server_info->domain_groups = NULL;
-
-       /* annoying, but the Anonymous really does have a session key, 
-          and it is all zeros! */
-       server_info->user_session_key = data_blob_talloc(server_info, NULL, 16);
-       NT_STATUS_HAVE_NO_MEMORY(server_info->user_session_key.data);
-
-       server_info->lm_session_key = data_blob_talloc(server_info, NULL, 16);
-       NT_STATUS_HAVE_NO_MEMORY(server_info->lm_session_key.data);
-
-       data_blob_clear(&server_info->user_session_key);
-       data_blob_clear(&server_info->lm_session_key);
-
-       server_info->account_name = talloc_strdup(server_info, "ANONYMOUS LOGON");
-       NT_STATUS_HAVE_NO_MEMORY(server_info->account_name);
+       unsigned int i, num_groupSIDs = 0;
+       const char *account_sid_string;
+       const char *account_sid_dn;
+       DATA_BLOB account_sid_blob;
+       const char *primary_group_string;
+       const char *primary_group_dn;
+       DATA_BLOB primary_group_blob;
 
-       server_info->domain_name = talloc_strdup(server_info, "NT AUTHORITY");
-       NT_STATUS_HAVE_NO_MEMORY(server_info->domain_name);
+       const char *filter;
 
-       server_info->full_name = talloc_strdup(server_info, "Anonymous Logon");
-       NT_STATUS_HAVE_NO_MEMORY(server_info->full_name);
+       struct dom_sid **groupSIDs = NULL;
+       const struct dom_sid *anonymous_sid, *system_sid;
 
-       server_info->logon_script = talloc_strdup(server_info, "");
-       NT_STATUS_HAVE_NO_MEMORY(server_info->logon_script);
+       TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
+       NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
 
-       server_info->profile_path = talloc_strdup(server_info, "");
-       NT_STATUS_HAVE_NO_MEMORY(server_info->profile_path);
-
-       server_info->home_directory = talloc_strdup(server_info, "");
-       NT_STATUS_HAVE_NO_MEMORY(server_info->home_directory);
-
-       server_info->home_drive = talloc_strdup(server_info, "");
-       NT_STATUS_HAVE_NO_MEMORY(server_info->home_drive);
-
-       server_info->logon_server = talloc_strdup(server_info, netbios_name);
-       NT_STATUS_HAVE_NO_MEMORY(server_info->logon_server);
-
-       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;
-
-       server_info->authenticated = false;
-
-       *_server_info = server_info;
-
-       return NT_STATUS_OK;
-}
-
-NTSTATUS auth_generate_session_info(TALLOC_CTX *mem_ctx, 
-                                   struct auth_serversupplied_info *server_info, 
-                                   struct auth_session_info **_session_info) 
-{
-       struct auth_session_info *session_info;
-       NTSTATUS nt_status;
-
-       session_info = talloc(mem_ctx, struct auth_session_info);
-       NT_STATUS_HAVE_NO_MEMORY(session_info);
+       session_info = talloc(tmp_ctx, struct auth_session_info);
+       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(session_info, tmp_ctx);
 
        session_info->server_info = talloc_reference(session_info, server_info);
 
@@ -164,19 +74,124 @@ NTSTATUS auth_generate_session_info(TALLOC_CTX *mem_ctx,
         * key from the auth subsystem */ 
        session_info->session_key = server_info->user_session_key;
 
+       anonymous_sid = dom_sid_parse_talloc(tmp_ctx, SID_NT_ANONYMOUS);
+       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(anonymous_sid, tmp_ctx);
+
+       system_sid = dom_sid_parse_talloc(tmp_ctx, SID_NT_SYSTEM);
+       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(system_sid, tmp_ctx);
+
+       if (dom_sid_equal(anonymous_sid, server_info->account_sid)) {
+               /* Don't expand nested groups of system, anonymous etc*/
+       } else if (dom_sid_equal(system_sid, server_info->account_sid)) {
+               /* Don't expand nested groups of system, anonymous etc*/
+       } else if (sam_ctx) {
+               groupSIDs = talloc_array(tmp_ctx, struct dom_sid *, server_info->n_domain_groups);
+               NT_STATUS_HAVE_NO_MEMORY_AND_FREE(groupSIDs, tmp_ctx);
+               if (!groupSIDs) {
+                       talloc_free(tmp_ctx);
+                       return NT_STATUS_NO_MEMORY;
+               }
+               
+               num_groupSIDs = server_info->n_domain_groups;
+               
+               for (i=0; i < server_info->n_domain_groups; i++) {
+                       groupSIDs[i] = server_info->domain_groups[i];
+               }
+               
+               filter = talloc_asprintf(tmp_ctx, "(&(objectClass=group)(groupType:1.2.840.113556.1.4.803:=%u))",
+                                        GROUP_TYPE_BUILTIN_LOCAL_GROUP);
+
+               /* Search for each group in the token */
+
+               /* Expands the account SID - this function takes in
+                * memberOf-like values, so we fake one up with the
+                * <SID=S-...> format of DN and then let it expand
+                * them, as long as they meet the filter - so only
+                * builtin groups
+                *
+                * We already have the primary group in the token, so set
+                * 'only childs' flag to true
+                */
+               account_sid_string = dom_sid_string(tmp_ctx, server_info->account_sid);
+               NT_STATUS_HAVE_NO_MEMORY_AND_FREE(account_sid_string, server_info);
+               
+               account_sid_dn = talloc_asprintf(tmp_ctx, "<SID=%s>", account_sid_string);
+               NT_STATUS_HAVE_NO_MEMORY_AND_FREE(account_sid_dn, server_info);
+               
+               account_sid_blob = data_blob_string_const(account_sid_dn);
+               
+               nt_status = authsam_expand_nested_groups(sam_ctx, &account_sid_blob, true, filter,
+                                                        tmp_ctx, &groupSIDs, &num_groupSIDs);
+               if (!NT_STATUS_IS_OK(nt_status)) {
+                       talloc_free(tmp_ctx);
+                       return nt_status;
+               }
+
+               /* Expands the primary group - this function takes in
+                * memberOf-like values, so we fake one up with the
+                * <SID=S-...> format of DN and then let it expand
+                * them, as long as they meet the filter - so only
+                * builtin groups
+                *
+                * We already have the primary group in the token, so set
+                * 'only childs' flag to true
+                */
+               primary_group_string = dom_sid_string(tmp_ctx, server_info->primary_group_sid);
+               NT_STATUS_HAVE_NO_MEMORY_AND_FREE(primary_group_string, server_info);
+               
+               primary_group_dn = talloc_asprintf(tmp_ctx, "<SID=%s>", primary_group_string);
+               NT_STATUS_HAVE_NO_MEMORY_AND_FREE(primary_group_dn, server_info);
+               
+               primary_group_blob = data_blob_string_const(primary_group_dn);
+               
+               nt_status = authsam_expand_nested_groups(sam_ctx, &primary_group_blob, true, filter,
+                                                        tmp_ctx, &groupSIDs, &num_groupSIDs);
+               if (!NT_STATUS_IS_OK(nt_status)) {
+                       talloc_free(tmp_ctx);
+                       return nt_status;
+               }
+               
+               for (i = 0; i < server_info->n_domain_groups; i++) {
+                       char *group_string;
+                       const char *group_dn;
+                       DATA_BLOB group_blob;
+                       
+                       group_string = dom_sid_string(tmp_ctx,
+                                                     server_info->domain_groups[i]);
+                       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(group_string, server_info);
+                       
+                       group_dn = talloc_asprintf(tmp_ctx, "<SID=%s>", group_string);
+                       talloc_free(group_string);
+                       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(group_dn, server_info);
+                       group_blob = data_blob_string_const(group_dn);
+                       
+                       /* This function takes in memberOf values and expands
+                        * them, as long as they meet the filter - so only
+                        * builtin groups */
+                       nt_status = authsam_expand_nested_groups(sam_ctx, &group_blob, true, filter,
+                                                                tmp_ctx, &groupSIDs, &num_groupSIDs);
+                       if (!NT_STATUS_IS_OK(nt_status)) {
+                               talloc_free(tmp_ctx);
+                               return nt_status;
+                       }
+               }
+       }
+
        nt_status = security_token_create(session_info,
-                                         global_loadparm,
+                                         lp_ctx,
                                          server_info->account_sid,
                                          server_info->primary_group_sid,
-                                         server_info->n_domain_groups,
-                                         server_info->domain_groups,
-                                         server_info->authenticated,
+                                         num_groupSIDs,
+                                         groupSIDs,
+                                         session_info_flags,
                                          &session_info->security_token);
-       NT_STATUS_NOT_OK_RETURN(nt_status);
+       NT_STATUS_NOT_OK_RETURN_AND_FREE(nt_status, tmp_ctx);
 
        session_info->credentials = NULL;
 
+       talloc_steal(mem_ctx, session_info);
        *_session_info = session_info;
+       talloc_free(tmp_ctx);
        return NT_STATUS_OK;
 }
 
@@ -191,143 +206,6 @@ void auth_session_info_debug(int dbg_lev,
                return; 
        }
 
-       security_token_debug(dbg_lev, session_info->security_token);
+       security_token_debug(0, dbg_lev, session_info->security_token);
 }
 
-/**
- * Make a server_info struct from the info3 returned by a domain logon 
- */
-NTSTATUS make_server_info_netlogon_validation(TALLOC_CTX *mem_ctx,
-                                             const char *account_name,
-                                             uint16_t validation_level,
-                                             union netr_Validation *validation,
-                                             struct auth_serversupplied_info **_server_info)
-{
-       struct auth_serversupplied_info *server_info;
-       struct netr_SamBaseInfo *base = NULL;
-       int i;
-
-       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;
-       }
-
-       server_info = talloc(mem_ctx, struct auth_serversupplied_info);
-       NT_STATUS_HAVE_NO_MEMORY(server_info);
-
-       /*
-          Here is where we should check the list of
-          trusted domains, and verify that the SID 
-          matches.
-       */
-       server_info->account_sid = dom_sid_add_rid(server_info, base->domain_sid, base->rid);
-       NT_STATUS_HAVE_NO_MEMORY(server_info->account_sid);
-
-
-       server_info->primary_group_sid = dom_sid_add_rid(server_info, base->domain_sid, base->primary_gid);
-       NT_STATUS_HAVE_NO_MEMORY(server_info->primary_group_sid);
-
-       server_info->n_domain_groups = base->groups.count;
-       if (base->groups.count) {
-               server_info->domain_groups = talloc_array(server_info, struct dom_sid*, base->groups.count);
-               NT_STATUS_HAVE_NO_MEMORY(server_info->domain_groups);
-       } else {
-               server_info->domain_groups = NULL;
-       }
-
-       for (i = 0; i < base->groups.count; i++) {
-               server_info->domain_groups[i] = dom_sid_add_rid(server_info, base->domain_sid, base->groups.rids[i].rid);
-               NT_STATUS_HAVE_NO_MEMORY(server_info->domain_groups[i]);
-       }
-
-       /* 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) {
-               struct dom_sid **dgrps = server_info->domain_groups;
-               size_t sidcount = server_info->n_domain_groups + validation->sam3->sidcount;
-               size_t n_dgrps = server_info->n_domain_groups;
-
-               if (validation->sam3->sidcount > 0) {
-                       dgrps = talloc_realloc(server_info, dgrps, struct dom_sid*, sidcount);
-                       NT_STATUS_HAVE_NO_MEMORY(dgrps);
-
-                       for (i = 0; i < validation->sam3->sidcount; i++) {
-                               dgrps[n_dgrps + i] = talloc_reference(dgrps, validation->sam3->sids[i].sid);
-                       }
-               }
-
-               server_info->n_domain_groups = sidcount;
-               server_info->domain_groups = dgrps;
-
-               /* 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, account_name);
-               NT_STATUS_HAVE_NO_MEMORY(server_info->account_name);
-       }
-
-       server_info->domain_name = 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->logon_server = talloc_reference(server_info, base->logon_server.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;
-
-       server_info->authenticated = true;
-
-       /* 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));
-               NT_STATUS_HAVE_NO_MEMORY(server_info->user_session_key.data);
-       }
-
-       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));
-               NT_STATUS_HAVE_NO_MEMORY(server_info->lm_session_key.data);
-       }
-
-       *_server_info = server_info;
-       return NT_STATUS_OK;
-}
-
-