s3: Remove a reference to "winbindd_cli_state" from append_info3_as_ndr
[samba.git] / source3 / winbindd / winbindd_pam.c
index bf113e8cce933305507208ba9efbd0902feaff17..71c9e951fd0e9829e31588a8aa55836231e291c6 100644 (file)
@@ -31,7 +31,9 @@
 #include "rpc_client/cli_netlogon.h"
 #include "smb_krb5.h"
 #include "../lib/crypto/arcfour.h"
-#include "../libcli/security/dom_sid.h"
+#include "../libcli/security/security.h"
+#include "ads.h"
+#include "../librpc/gen_ndr/krb5pac.h"
 
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_WINBIND
@@ -120,7 +122,7 @@ static NTSTATUS append_info3_as_txt(TALLOC_CTX *mem_ctx,
 }
 
 static NTSTATUS append_info3_as_ndr(TALLOC_CTX *mem_ctx,
-                                   struct winbindd_cli_state *state,
+                                   struct winbindd_response *resp,
                                    struct netr_SamInfo3 *info3)
 {
        DATA_BLOB blob;
@@ -133,14 +135,14 @@ static NTSTATUS append_info3_as_ndr(TALLOC_CTX *mem_ctx,
                return ndr_map_error2ntstatus(ndr_err);
        }
 
-       state->response->extra_data.data = blob.data;
-       state->response->length += blob.length;
+       resp->extra_data.data = blob.data;
+       resp->length += blob.length;
 
        return NT_STATUS_OK;
 }
 
 static NTSTATUS append_unix_username(TALLOC_CTX *mem_ctx,
-                                    struct winbindd_cli_state *state,
+                                    struct winbindd_response *resp,
                                     const struct netr_SamInfo3 *info3,
                                     const char *name_domain,
                                     const char *name_user)
@@ -164,11 +166,11 @@ static NTSTATUS append_unix_username(TALLOC_CTX *mem_ctx,
                nt_username = name_user;
        }
 
-       fill_domain_username(state->response->data.auth.unix_username,
+       fill_domain_username(resp->data.auth.unix_username,
                             nt_domain, nt_username, true);
 
-       DEBUG(5,("Setting unix username to [%s]\n",
-               state->response->data.auth.unix_username));
+       DEBUG(5, ("Setting unix username to [%s]\n",
+                 resp->data.auth.unix_username));
 
        return NT_STATUS_OK;
 }
@@ -254,12 +256,12 @@ static NTSTATUS check_info3_in_group(struct netr_SamInfo3 *info3,
  */
 {
        struct dom_sid *require_membership_of_sid;
-       size_t num_require_membership_of_sid;
+       uint32_t num_require_membership_of_sid;
        char *req_sid;
        const char *p;
        struct dom_sid sid;
        size_t i;
-       struct nt_user_token *token;
+       struct security_token *token;
        TALLOC_CTX *frame = talloc_stackframe();
        NTSTATUS status;
 
@@ -270,7 +272,7 @@ static NTSTATUS check_info3_in_group(struct netr_SamInfo3 *info3,
                return NT_STATUS_OK;
        }
 
-       token = talloc_zero(talloc_tos(), struct nt_user_token);
+       token = talloc_zero(talloc_tos(), struct security_token);
        if (token == NULL) {
                DEBUG(0, ("talloc failed\n"));
                TALLOC_FREE(frame);
@@ -301,7 +303,7 @@ static NTSTATUS check_info3_in_group(struct netr_SamInfo3 *info3,
        }
 
        status = sid_array_from_info3(talloc_tos(), info3,
-                                     &token->user_sids,
+                                     &token->sids,
                                      &token->num_sids,
                                      true, false);
        if (!NT_STATUS_IS_OK(status)) {
@@ -319,7 +321,7 @@ static NTSTATUS check_info3_in_group(struct netr_SamInfo3 *info3,
                return status;
        }
 
-       debug_nt_user_token(DBGC_CLASS, 10, token);
+       security_token_debug(DBGC_CLASS, 10, token);
 
        for (i=0; i<num_require_membership_of_sid; i++) {
                DEBUG(10, ("Checking SID %s\n", sid_string_dbg(
@@ -461,7 +463,7 @@ static NTSTATUS get_pwd_properties(struct winbindd_domain *domain,
 static const char *generate_krb5_ccache(TALLOC_CTX *mem_ctx,
                                        const char *type,
                                        uid_t uid,
-                                       bool *internal_ccache)
+                                       const char **user_ccache_file)
 {
        /* accept FILE and WRFILE as krb5_cc_type from the client and then
         * build the full ccname string based on the user's uid here -
@@ -469,62 +471,33 @@ static const char *generate_krb5_ccache(TALLOC_CTX *mem_ctx,
 
        const char *gen_cc = NULL;
 
-       *internal_ccache = true;
-
-       if (uid == -1) {
-               goto memory_ccache;
+       if (uid != -1) {
+               if (strequal(type, "FILE")) {
+                       gen_cc = talloc_asprintf(
+                               mem_ctx, "FILE:/tmp/krb5cc_%d", uid);
+               }
+               if (strequal(type, "WRFILE")) {
+                       gen_cc = talloc_asprintf(
+                               mem_ctx, "WRFILE:/tmp/krb5cc_%d", uid);
+               }
        }
 
-       if (!type || type[0] == '\0') {
-               goto memory_ccache;
-       }
+       *user_ccache_file = gen_cc;
 
-       if (strequal(type, "FILE")) {
-               gen_cc = talloc_asprintf(mem_ctx, "FILE:/tmp/krb5cc_%d", uid);
-       } else if (strequal(type, "WRFILE")) {
-               gen_cc = talloc_asprintf(mem_ctx, "WRFILE:/tmp/krb5cc_%d", uid);
-       } else {
-               DEBUG(10,("we don't allow to set a %s type ccache\n", type));
-               goto memory_ccache;
+       if (gen_cc == NULL) {
+               gen_cc = talloc_strdup(mem_ctx, "MEMORY:winbindd_pam_ccache");
        }
-
-       *internal_ccache = false;
-       goto done;
-
-  memory_ccache:
-       gen_cc = talloc_strdup(mem_ctx, "MEMORY:winbindd_pam_ccache");
-
-  done:
        if (gen_cc == NULL) {
                DEBUG(0,("out of memory\n"));
                return NULL;
        }
 
-       DEBUG(10,("using ccache: %s %s\n", gen_cc, *internal_ccache ? "(internal)":""));
+       DEBUG(10, ("using ccache: %s%s\n", gen_cc,
+                  (*user_ccache_file == NULL) ? " (internal)":""));
 
        return gen_cc;
 }
 
-static void setup_return_cc_name(struct winbindd_cli_state *state, const char *cc)
-{
-       const char *type = state->request->data.auth.krb5_cc_type;
-
-       state->response->data.auth.krb5ccname[0] = '\0';
-
-       if (type[0] == '\0') {
-               return;
-       }
-
-       if (!strequal(type, "FILE") &&
-           !strequal(type, "WRFILE")) {
-               DEBUG(10,("won't return krbccname for a %s type ccache\n",
-                       type));
-               return;
-       }
-
-       fstrcpy(state->response->data.auth.krb5ccname, cc);
-}
-
 #endif
 
 uid_t get_uid_from_request(struct winbindd_request *request)
@@ -567,7 +540,7 @@ static NTSTATUS winbindd_raw_kerberos_login(struct winbindd_domain *domain,
        uid_t uid = -1;
        ADS_STRUCT *ads;
        time_t time_offset = 0;
-       bool internal_ccache = true;
+       const char *user_ccache_file;
        struct PAC_LOGON_INFO *logon_info = NULL;
 
        *info3 = NULL;
@@ -583,7 +556,7 @@ static NTSTATUS winbindd_raw_kerberos_login(struct winbindd_domain *domain,
        cc = generate_krb5_ccache(state->mem_ctx,
                                  state->request->data.auth.krb5_cc_type,
                                  state->request->data.auth.uid,
-                                 &internal_ccache);
+                                 &user_ccache_file);
        if (cc == NULL) {
                return NT_STATUS_NO_MEMORY;
        }
@@ -621,7 +594,7 @@ static NTSTATUS winbindd_raw_kerberos_login(struct winbindd_domain *domain,
 
        /************************ ENTERING NON-ROOT **********************/
 
-       if (!internal_ccache) {
+       if (user_ccache_file != NULL) {
                set_effective_uid(uid);
                DEBUG(10,("winbindd_raw_kerberos_login: uid is %d\n", uid));
        }
@@ -638,7 +611,7 @@ static NTSTATUS winbindd_raw_kerberos_login(struct winbindd_domain *domain,
                                     WINBINDD_PAM_AUTH_KRB5_RENEW_TIME,
                                     NULL,
                                     &logon_info);
-       if (!internal_ccache) {
+       if (user_ccache_file != NULL) {
                gain_root_privilege();
        }
 
@@ -656,9 +629,10 @@ static NTSTATUS winbindd_raw_kerberos_login(struct winbindd_domain *domain,
        /* if we had a user's ccache then return that string for the pam
         * environment */
 
-       if (!internal_ccache) {
+       if (user_ccache_file != NULL) {
 
-               setup_return_cc_name(state, cc);
+               fstrcpy(state->response->data.auth.krb5ccname,
+                       user_ccache_file);
 
                result = add_ccache_to_list(principal_s,
                                            cc,
@@ -742,30 +716,30 @@ bool check_request_flags(uint32_t flags)
 ****************************************************************/
 
 static NTSTATUS append_auth_data(struct winbindd_cli_state *state,
+                                uint32_t request_flags,
                                 struct netr_SamInfo3 *info3,
                                 const char *name_domain,
                                 const char *name_user)
 {
        NTSTATUS result;
-       uint32_t flags = state->request->flags;
 
-       if (flags & WBFLAG_PAM_USER_SESSION_KEY) {
+       if (request_flags & WBFLAG_PAM_USER_SESSION_KEY) {
                memcpy(state->response->data.auth.user_session_key,
                       info3->base.key.key,
                       sizeof(state->response->data.auth.user_session_key)
                       /* 16 */);
        }
 
-       if (flags & WBFLAG_PAM_LMKEY) {
+       if (request_flags & WBFLAG_PAM_LMKEY) {
                memcpy(state->response->data.auth.first_8_lm_hash,
                       info3->base.LMSessKey.key,
                       sizeof(state->response->data.auth.first_8_lm_hash)
                       /* 8 */);
        }
 
-       if (flags & WBFLAG_PAM_UNIX_NAME) {
-               result = append_unix_username(state->mem_ctx, state, info3,
-                                             name_domain, name_user);
+       if (request_flags & WBFLAG_PAM_UNIX_NAME) {
+               result = append_unix_username(state->mem_ctx, state->response,
+                                             info3, name_domain, name_user);
                if (!NT_STATUS_IS_OK(result)) {
                        DEBUG(10,("Failed to append Unix Username: %s\n",
                                nt_errstr(result)));
@@ -775,8 +749,9 @@ static NTSTATUS append_auth_data(struct winbindd_cli_state *state,
 
        /* currently, anything from here on potentially overwrites extra_data. */
 
-       if (flags & WBFLAG_PAM_INFO3_NDR) {
-               result = append_info3_as_ndr(state->mem_ctx, state, info3);
+       if (request_flags & WBFLAG_PAM_INFO3_NDR) {
+               result = append_info3_as_ndr(state->mem_ctx, state->response,
+                                            info3);
                if (!NT_STATUS_IS_OK(result)) {
                        DEBUG(10,("Failed to append INFO3 (NDR): %s\n",
                                nt_errstr(result)));
@@ -784,7 +759,7 @@ static NTSTATUS append_auth_data(struct winbindd_cli_state *state,
                }
        }
 
-       if (flags & WBFLAG_PAM_INFO3_TEXT) {
+       if (request_flags & WBFLAG_PAM_INFO3_TEXT) {
                result = append_info3_as_txt(state->mem_ctx, state, info3);
                if (!NT_STATUS_IS_OK(result)) {
                        DEBUG(10,("Failed to append INFO3 (TXT): %s\n",
@@ -793,7 +768,7 @@ static NTSTATUS append_auth_data(struct winbindd_cli_state *state,
                }
        }
 
-       if (flags & WBFLAG_PAM_AFS_TOKEN) {
+       if (request_flags & WBFLAG_PAM_AFS_TOKEN) {
                result = append_afs_token(state->mem_ctx, state, info3,
                                          name_domain, name_user);
                if (!NT_STATUS_IS_OK(result)) {
@@ -836,8 +811,7 @@ static NTSTATUS winbindd_dual_pam_auth_cached(struct winbindd_domain *domain,
        parse_domain_user(state->request->data.auth.user, name_domain, name_user);
 
 
-       if (!lookup_cached_name(state->mem_ctx,
-                               name_domain,
+       if (!lookup_cached_name(name_domain,
                                name_user,
                                &sid,
                                &type)) {
@@ -943,7 +917,7 @@ static NTSTATUS winbindd_dual_pam_auth_cached(struct winbindd_domain *domain,
                        char *realm = NULL;
                        const char *principal_s = NULL;
                        const char *service = NULL;
-                       bool internal_ccache = false;
+                       const char *user_ccache_file;
 
                        uid = get_uid_from_state(state);
                        if (uid == -1) {
@@ -954,7 +928,7 @@ static NTSTATUS winbindd_dual_pam_auth_cached(struct winbindd_domain *domain,
                        cc = generate_krb5_ccache(state->mem_ctx,
                                                state->request->data.auth.krb5_cc_type,
                                                state->request->data.auth.uid,
-                                               &internal_ccache);
+                                               &user_ccache_file);
                        if (cc == NULL) {
                                return NT_STATUS_NO_MEMORY;
                        }
@@ -972,9 +946,10 @@ static NTSTATUS winbindd_dual_pam_auth_cached(struct winbindd_domain *domain,
                                return NT_STATUS_NO_MEMORY;
                        }
 
-                       if (!internal_ccache) {
+                       if (user_ccache_file != NULL) {
 
-                               setup_return_cc_name(state, cc);
+                               fstrcpy(state->response->data.auth.krb5ccname,
+                                       user_ccache_file);
 
                                result = add_ccache_to_list(principal_s,
                                                            cc,
@@ -1003,7 +978,6 @@ static NTSTATUS winbindd_dual_pam_auth_cached(struct winbindd_domain *domain,
                my_info3->base.bad_password_count = 0;
 
                result = winbindd_update_creds_by_info3(domain,
-                                                       state->mem_ctx,
                                                        state->request->data.auth.user,
                                                        state->request->data.auth.pass,
                                                        my_info3);
@@ -1051,7 +1025,6 @@ static NTSTATUS winbindd_dual_pam_auth_cached(struct winbindd_domain *domain,
 
 failed:
        result = winbindd_update_creds_by_info3(domain,
-                                               state->mem_ctx,
                                                state->request->data.auth.user,
                                                NULL,
                                                my_info3);
@@ -1131,42 +1104,23 @@ static NTSTATUS winbindd_dual_auth_passdb(TALLOC_CTX *mem_ctx,
                                          struct netr_SamInfo3 **pinfo3)
 {
        struct auth_usersupplied_info *user_info = NULL;
-       struct auth_serversupplied_info *server_info = NULL;
-       struct netr_SamInfo3 *info3;
        NTSTATUS status;
 
        status = make_user_info(&user_info, user, user, domain, domain,
                                global_myname(), lm_resp, nt_resp, NULL, NULL,
-                               NULL, True);
+                               NULL, AUTH_PASSWORD_RESPONSE);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(10, ("make_user_info failed: %s\n", nt_errstr(status)));
                return status;
        }
 
-       status = check_sam_security(challenge, talloc_tos(), user_info,
-                                   &server_info);
-       free_user_info(&user_info);
-
-       if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(10, ("check_ntlm_password failed: %s\n",
-                          nt_errstr(status)));
-               return status;
-       }
-
-       info3 = TALLOC_ZERO_P(mem_ctx, struct netr_SamInfo3);
-       if (info3 == NULL) {
-               return NT_STATUS_NO_MEMORY;
-       }
-
-       status = serverinfo_to_SamInfo3(server_info, NULL, 0, info3);
-       if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(10, ("serverinfo_to_SamInfo3 failed: %s\n",
-                          nt_errstr(status)));
-               return status;
-       }
+       /* We don't want any more mapping of the username */
+       user_info->mapped_state = True;
 
+       status = check_sam_security_info3(challenge, talloc_tos(), user_info,
+                                         pinfo3);
+       free_user_info(&user_info);
        DEBUG(10, ("Authenticated user %s\\%s successfully\n", domain, user));
-       *pinfo3 = info3;
        return NT_STATUS_OK;
 }
 
@@ -1331,7 +1285,6 @@ static NTSTATUS winbindd_dual_pam_auth_samlogon(struct winbindd_domain *domain,
                        DEBUG(3, ("Got a DC that can not do NetSamLogonEx, "
                                  "retrying with NetSamLogon\n"));
                        domain->can_do_samlogon_ex = false;
-                       retry = true;
                        continue;
                }
 
@@ -1340,7 +1293,6 @@ static NTSTATUS winbindd_dual_pam_auth_samlogon(struct winbindd_domain *domain,
                   our connection. */
 
                if (!rpccli_is_connected(netlogon_pipe)) {
-                       retry = true;
                        continue;
                }
 
@@ -1446,11 +1398,6 @@ enum winbindd_result winbindd_dual_pam_auth(struct winbindd_domain *domain,
        DEBUG(3, ("[%5lu]: dual pam auth %s\n", (unsigned long)state->pid,
                  state->request->data.auth.user));
 
-       if (!check_request_flags(state->request->flags)) {
-               result = NT_STATUS_INVALID_PARAMETER_MIX;
-               goto done;
-       }
-
        /* Parse domain and username */
 
        name_map_status = normalize_name_unmap(state->mem_ctx,
@@ -1469,12 +1416,14 @@ enum winbindd_result winbindd_dual_pam_auth(struct winbindd_domain *domain,
        parse_domain_user(mapped_user, name_domain, name_user);
 
        if ( mapped_user != state->request->data.auth.user ) {
-               fstr_sprintf( domain_user, "%s\\%s", name_domain, name_user );
+               fstr_sprintf( domain_user, "%s%c%s", name_domain,
+                       *lp_winbind_separator(),
+                       name_user );
                safe_strcpy( state->request->data.auth.user, domain_user,
                             sizeof(state->request->data.auth.user)-1 );
        }
 
-       if (domain->online == false) {
+       if (!domain->online) {
                result = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
                if (domain->startup) {
                        /* Logons are very important to users. If we're offline and
@@ -1626,24 +1575,21 @@ process_result:
                        goto done;
                }
 
-               result = append_auth_data(state, info3, name_domain,
-                                         name_user);
+               result = append_auth_data(state, state->request->flags, info3,
+                                         name_domain, name_user);
                if (!NT_STATUS_IS_OK(result)) {
                        goto done;
                }
 
-               if ((state->request->flags & WBFLAG_PAM_CACHED_LOGIN)) {
+               if ((state->request->flags & WBFLAG_PAM_CACHED_LOGIN)
+                   && lp_winbind_offline_logon()) {
 
-                       if (lp_winbind_offline_logon()) {
-                               result = winbindd_store_creds(domain,
-                                                     state->mem_ctx,
+                       result = winbindd_store_creds(domain,
                                                      state->request->data.auth.user,
                                                      state->request->data.auth.pass,
-                                                     info3, NULL);
-                       }
+                                                     info3);
                }
 
-
                if (state->request->flags & WBFLAG_PAM_GET_PWD_POLICY) {
                        struct winbindd_domain *our_domain = find_our_domain();
 
@@ -1708,33 +1654,13 @@ enum winbindd_result winbindd_dual_pam_auth_crap(struct winbindd_domain *domain,
        state->request->data.auth_crap.user[sizeof(state->request->data.auth_crap.user)-1]=0;
        state->request->data.auth_crap.domain[sizeof(state->request->data.auth_crap.domain)-1]=0;
 
-       if (!check_request_flags(state->request->flags)) {
-               result = NT_STATUS_INVALID_PARAMETER_MIX;
-               goto done;
-       }
-
        name_user = state->request->data.auth_crap.user;
-
-       if (*state->request->data.auth_crap.domain) {
-               name_domain = state->request->data.auth_crap.domain;
-       } else if (lp_winbind_use_default_domain()) {
-               name_domain = lp_workgroup();
-       } else {
-               DEBUG(5,("no domain specified with username (%s) - failing auth\n",
-                        name_user));
-               result = NT_STATUS_NO_SUCH_USER;
-               goto done;
-       }
+       name_domain = state->request->data.auth_crap.domain;
+       workstation = state->request->data.auth_crap.workstation;
 
        DEBUG(3, ("[%5lu]: pam auth crap domain: %s user: %s\n", (unsigned long)state->pid,
                  name_domain, name_user));
 
-       if (*state->request->data.auth_crap.workstation) {
-               workstation = state->request->data.auth_crap.workstation;
-       } else {
-               workstation = global_myname();
-       }
-
        if (state->request->data.auth_crap.lm_resp_len > sizeof(state->request->data.auth_crap.lm_resp)
                || state->request->data.auth_crap.nt_resp_len > sizeof(state->request->data.auth_crap.nt_resp)) {
                if (!(state->request->flags & WBFLAG_BIG_NTLMV2_BLOB) ||
@@ -1807,7 +1733,6 @@ enum winbindd_result winbindd_dual_pam_auth_crap(struct winbindd_domain *domain,
                        DEBUG(3, ("Got a DC that can not do NetSamLogonEx, "
                                  "retrying with NetSamLogon\n"));
                        domain->can_do_samlogon_ex = false;
-                       retry = true;
                        continue;
                }
 
@@ -1818,7 +1743,6 @@ enum winbindd_result winbindd_dual_pam_auth_crap(struct winbindd_domain *domain,
                   our connection. */
 
                if (!rpccli_is_connected(netlogon_pipe)) {
-                       retry = true;
                        continue;
                }
 
@@ -1858,8 +1782,8 @@ process_result:
                        goto done;
                }
 
-               result = append_auth_data(state, info3, name_domain,
-                                         name_user);
+               result = append_auth_data(state, state->request->flags, info3,
+                                         name_domain, name_user);
                if (!NT_STATUS_IS_OK(result)) {
                        goto done;
                }
@@ -1973,26 +1897,26 @@ enum winbindd_result winbindd_dual_pam_chauthtok(struct winbindd_domain *contact
 
 done:
 
-       if (NT_STATUS_IS_OK(result) && (state->request->flags & WBFLAG_PAM_CACHED_LOGIN)) {
-               if (lp_winbind_offline_logon()) {
-                       result = winbindd_update_creds_by_name(contact_domain,
-                                                        state->mem_ctx, user,
-                                                        newpass);
-                       /* Again, this happens when we login from gdm or xdm
-                        * and the password expires, *BUT* cached crendentials
-                        * doesn't exist. winbindd_update_creds_by_name()
-                        * returns NT_STATUS_NO_SUCH_USER.
-                        * This is not a failure.
-                        * --- BoYang
-                        * */
-                       if (NT_STATUS_EQUAL(result, NT_STATUS_NO_SUCH_USER)) {
-                               result = NT_STATUS_OK;
-                       }
+       if (NT_STATUS_IS_OK(result)
+           && (state->request->flags & WBFLAG_PAM_CACHED_LOGIN)
+           && lp_winbind_offline_logon()) {
+               result = winbindd_update_creds_by_name(contact_domain, user,
+                                                      newpass);
+               /* Again, this happens when we login from gdm or xdm
+                * and the password expires, *BUT* cached crendentials
+                * doesn't exist. winbindd_update_creds_by_name()
+                * returns NT_STATUS_NO_SUCH_USER.
+                * This is not a failure.
+                * --- BoYang
+                * */
+               if (NT_STATUS_EQUAL(result, NT_STATUS_NO_SUCH_USER)) {
+                       result = NT_STATUS_OK;
+               }
 
-                       if (!NT_STATUS_IS_OK(result)) {
-                               DEBUG(10,("Failed to store creds: %s\n", nt_errstr(result)));
-                               goto process_result;
-                       }
+               if (!NT_STATUS_IS_OK(result)) {
+                       DEBUG(10, ("Failed to store creds: %s\n",
+                                  nt_errstr(result)));
+                       goto process_result;
                }
        }