s3:auth Change winbindd -> auth interface to more standard structures
[ira/wip.git] / source3 / winbindd / winbindd_pam.c
index 61c8c298f1b615d1823e45ce6266184abf0a81cb..be3b2a5c775e7f32ceecc16364374c3abd112e4d 100644 (file)
 #include "winbindd.h"
 #include "../libcli/auth/libcli_auth.h"
 #include "../librpc/gen_ndr/cli_samr.h"
+#include "rpc_client/cli_samr.h"
+#include "../librpc/gen_ndr/ndr_netlogon.h"
+#include "rpc_client/cli_netlogon.h"
 #include "smb_krb5.h"
+#include "../lib/crypto/arcfour.h"
+#include "../libcli/security/dom_sid.h"
+#include "ads.h"
+#include "../librpc/gen_ndr/krb5pac.h"
 
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_WINBIND
@@ -121,7 +128,7 @@ static NTSTATUS append_info3_as_ndr(TALLOC_CTX *mem_ctx,
        DATA_BLOB blob;
        enum ndr_err_code ndr_err;
 
-       ndr_err = ndr_push_struct_blob(&blob, mem_ctx, NULL, info3,
+       ndr_err = ndr_push_struct_blob(&blob, mem_ctx, info3,
                                       (ndr_push_flags_fn_t)ndr_push_netr_SamInfo3);
        if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
                DEBUG(0,("append_info3_as_ndr: failed to append\n"));
@@ -192,7 +199,7 @@ static NTSTATUS append_afs_token(TALLOC_CTX *mem_ctx,
                                    "%U", name_user);
 
        {
-               DOM_SID user_sid;
+               struct dom_sid user_sid;
                fstring sidstr;
 
                sid_compose(&user_sid, info3->base.domain_sid,
@@ -248,11 +255,11 @@ static NTSTATUS check_info3_in_group(struct netr_SamInfo3 *info3,
  *    or other NT_STATUS_IS_ERR(status) for other kinds of failure.
  */
 {
-       DOM_SID *require_membership_of_sid;
+       struct dom_sid *require_membership_of_sid;
        size_t num_require_membership_of_sid;
        char *req_sid;
        const char *p;
-       DOM_SID sid;
+       struct dom_sid sid;
        size_t i;
        struct nt_user_token *token;
        TALLOC_CTX *frame = talloc_stackframe();
@@ -522,11 +529,11 @@ static void setup_return_cc_name(struct winbindd_cli_state *state, const char *c
 
 #endif
 
-static uid_t get_uid_from_state(struct winbindd_cli_state *state)
+uid_t get_uid_from_request(struct winbindd_request *request)
 {
        uid_t uid;
 
-       uid = state->request->data.auth.uid;
+       uid = request->data.auth.uid;
 
        if (uid < 0) {
                DEBUG(1,("invalid uid: '%u'\n", (unsigned int)uid));
@@ -535,6 +542,11 @@ static uid_t get_uid_from_state(struct winbindd_cli_state *state)
        return uid;
 }
 
+static uid_t get_uid_from_state(struct winbindd_cli_state *state)
+{
+       return get_uid_from_request(state->request);
+}
+
 /**********************************************************************
  Authenticate a user with a clear text password using Kerberos and fill up
  ccache if required
@@ -558,8 +570,7 @@ static NTSTATUS winbindd_raw_kerberos_login(struct winbindd_domain *domain,
        ADS_STRUCT *ads;
        time_t time_offset = 0;
        bool internal_ccache = true;
-
-       ZERO_STRUCTP(info3);
+       struct PAC_LOGON_INFO *logon_info = NULL;
 
        *info3 = NULL;
 
@@ -617,18 +628,18 @@ static NTSTATUS winbindd_raw_kerberos_login(struct winbindd_domain *domain,
                DEBUG(10,("winbindd_raw_kerberos_login: uid is %d\n", uid));
        }
 
-       result = kerberos_return_info3_from_pac(state->mem_ctx,
-                                               principal_s,
-                                               state->request->data.auth.pass,
-                                               time_offset,
-                                               &ticket_lifetime,
-                                               &renewal_until,
-                                               cc,
-                                               true,
-                                               true,
-                                               WINBINDD_PAM_AUTH_KRB5_RENEW_TIME,
-                                               NULL,
-                                               info3);
+       result = kerberos_return_pac(state->mem_ctx,
+                                    principal_s,
+                                    state->request->data.auth.pass,
+                                    time_offset,
+                                    &ticket_lifetime,
+                                    &renewal_until,
+                                    cc,
+                                    true,
+                                    true,
+                                    WINBINDD_PAM_AUTH_KRB5_RENEW_TIME,
+                                    NULL,
+                                    &logon_info);
        if (!internal_ccache) {
                gain_root_privilege();
        }
@@ -639,6 +650,8 @@ static NTSTATUS winbindd_raw_kerberos_login(struct winbindd_domain *domain,
                goto failed;
        }
 
+       *info3 = &logon_info->info3;
+
        DEBUG(10,("winbindd_raw_kerberos_login: winbindd validated ticket of %s\n",
                principal_s));
 
@@ -752,10 +765,11 @@ static NTSTATUS append_auth_data(struct winbindd_cli_state *state,
                       /* 8 */);
        }
 
-       if (flags & WBFLAG_PAM_INFO3_TEXT) {
-               result = append_info3_as_txt(state->mem_ctx, state, info3);
+       if (flags & WBFLAG_PAM_UNIX_NAME) {
+               result = append_unix_username(state->mem_ctx, state, info3,
+                                             name_domain, name_user);
                if (!NT_STATUS_IS_OK(result)) {
-                       DEBUG(10,("Failed to append INFO3 (TXT): %s\n",
+                       DEBUG(10,("Failed to append Unix Username: %s\n",
                                nt_errstr(result)));
                        return result;
                }
@@ -772,11 +786,10 @@ static NTSTATUS append_auth_data(struct winbindd_cli_state *state,
                }
        }
 
-       if (flags & WBFLAG_PAM_UNIX_NAME) {
-               result = append_unix_username(state->mem_ctx, state, info3,
-                                             name_domain, name_user);
+       if (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 Unix Username: %s\n",
+                       DEBUG(10,("Failed to append INFO3 (TXT): %s\n",
                                nt_errstr(result)));
                        return result;
                }
@@ -802,7 +815,7 @@ static NTSTATUS winbindd_dual_pam_auth_cached(struct winbindd_domain *domain,
        NTSTATUS result = NT_STATUS_LOGON_FAILURE;
        uint16 max_allowed_bad_attempts;
        fstring name_domain, name_user;
-       DOM_SID sid;
+       struct dom_sid sid;
        enum lsa_SidType type;
        uchar new_nt_pass[NT_HASH_LEN];
        const uint8 *cached_nt_pass;
@@ -1032,7 +1045,7 @@ static NTSTATUS winbindd_dual_pam_auth_cached(struct winbindd_domain *domain,
                        DEBUG(10,("winbindd_dual_pam_auth_cached: failed to get password properties.\n"));
                }
 
-               if ((my_info3->base.rid != DOMAIN_USER_RID_ADMIN) ||
+               if ((my_info3->base.rid != DOMAIN_RID_ADMINISTRATOR) ||
                    (password_properties & DOMAIN_PASSWORD_LOCKOUT_ADMINS)) {
                        my_info3->base.acct_flags |= ACB_AUTOLOCK;
                }
@@ -1120,42 +1133,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;
 }
 
@@ -1458,7 +1452,9 @@ 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 );
        }
@@ -1580,7 +1576,7 @@ process_result:
 
        if (NT_STATUS_IS_OK(result)) {
 
-               DOM_SID user_sid;
+               struct dom_sid user_sid;
 
                /* In all codepaths where result == NT_STATUS_OK info3 must have
                   been initialized. */
@@ -1623,30 +1619,12 @@ process_result:
 
                if ((state->request->flags & WBFLAG_PAM_CACHED_LOGIN)) {
 
-                       /* Store in-memory creds for single-signon using ntlm_auth. */
-                       result = winbindd_add_memory_creds(state->request->data.auth.user,
-                                                       get_uid_from_state(state),
-                                                       state->request->data.auth.pass);
-
-                       if (!NT_STATUS_IS_OK(result)) {
-                               DEBUG(10,("Failed to store memory creds: %s\n", nt_errstr(result)));
-                               goto done;
-                       }
-
                        if (lp_winbind_offline_logon()) {
                                result = winbindd_store_creds(domain,
                                                      state->mem_ctx,
                                                      state->request->data.auth.user,
                                                      state->request->data.auth.pass,
                                                      info3, NULL);
-                               if (!NT_STATUS_IS_OK(result)) {
-
-                                       /* Release refcount. */
-                                       winbindd_delete_memory_creds(state->request->data.auth.user);
-
-                                       DEBUG(10,("Failed to store creds: %s\n", nt_errstr(result)));
-                                       goto done;
-                               }
                        }
                }
 
@@ -1902,13 +1880,15 @@ enum winbindd_result winbindd_dual_pam_chauthtok(struct winbindd_domain *contact
        char *oldpass;
        char *newpass = NULL;
        struct policy_handle dom_pol;
-       struct rpc_pipe_client *cli;
+       struct rpc_pipe_client *cli = NULL;
        bool got_info = false;
        struct samr_DomInfo1 *info = NULL;
        struct userPwdChangeFailureInformation *reject = NULL;
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
        fstring domain, user;
 
+       ZERO_STRUCT(dom_pol);
+
        DEBUG(3, ("[%5lu]: dual pam chauthtok %s\n", (unsigned long)state->pid,
                  state->request->data.auth.user));
 
@@ -1979,26 +1959,6 @@ 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)) {
-
-               /* Update the single sign-on memory creds. */
-               result = winbindd_replace_memory_creds(state->request->data.chauthtok.user,
-                                                       newpass);
-
-               /* When we login from gdm or xdm and password expires,
-                * we change password, but there are no memory crendentials
-                * So, winbindd_replace_memory_creds() returns
-                * NT_STATUS_OBJECT_NAME_NOT_FOUND. This is not a failure.
-                * --- BoYang
-                * */
-               if (NT_STATUS_EQUAL(result, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
-                       result = NT_STATUS_OK;
-               }
-
-               if (!NT_STATUS_IS_OK(result)) {
-                       DEBUG(10,("Failed to replace memory creds: %s\n", nt_errstr(result)));
-                       goto process_result;
-               }
-
                if (lp_winbind_offline_logon()) {
                        result = winbindd_update_creds_by_name(contact_domain,
                                                         state->mem_ctx, user,
@@ -2039,6 +1999,16 @@ done:
 
 process_result:
 
+       if (strequal(contact_domain->name, get_global_sam_name())) {
+               /* FIXME: internal rpc pipe does not cache handles yet */
+               if (cli) {
+                       if (is_valid_policy_hnd(&dom_pol)) {
+                               rpccli_samr_Close(cli, state->mem_ctx, &dom_pol);
+                       }
+                       TALLOC_FREE(cli);
+               }
+       }
+
        set_auth_errors(state->response, result);
 
        DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2,
@@ -2051,72 +2021,6 @@ process_result:
        return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
 }
 
-void winbindd_pam_logoff(struct winbindd_cli_state *state)
-{
-       struct winbindd_domain *domain;
-       fstring name_domain, user;
-       uid_t caller_uid = (uid_t)-1;
-       uid_t request_uid = state->request->data.logoff.uid;
-
-       /* Ensure null termination */
-       state->request->data.logoff.user
-               [sizeof(state->request->data.logoff.user)-1]='\0';
-
-       state->request->data.logoff.krb5ccname
-               [sizeof(state->request->data.logoff.krb5ccname)-1]='\0';
-
-       DEBUG(3, ("[%5lu]: pam logoff %s\n", (unsigned long)state->pid,
-               state->request->data.logoff.user));
-
-       if (request_uid == (uid_t)-1) {
-               goto failed;
-       }
-
-       if (!canonicalize_username(state->request->data.logoff.user, name_domain, user)) {
-               goto failed;
-       }
-
-       if ((domain = find_auth_domain(state->request->flags,
-                                      name_domain)) == NULL) {
-               goto failed;
-       }
-
-       if ((sys_getpeereid(state->sock, &caller_uid)) != 0) {
-               DEBUG(1,("winbindd_pam_logoff: failed to check peerid: %s\n",
-                       strerror(errno)));
-               goto failed;
-       }
-
-       switch (caller_uid) {
-               case -1:
-                       goto failed;
-               case 0:
-                       /* root must be able to logoff any user - gd */
-                       state->request->data.logoff.uid = request_uid;
-                       break;
-               default:
-                       if (caller_uid != request_uid) {
-                               DEBUG(1,("winbindd_pam_logoff: caller requested invalid uid\n"));
-                               goto failed;
-                       }
-                       state->request->data.logoff.uid = caller_uid;
-                       break;
-       }
-
-       sendto_domain(state, domain);
-       return;
-
- failed:
-       set_auth_errors(state->response, NT_STATUS_NO_SUCH_USER);
-       DEBUG(5, ("Pam Logoff for %s returned %s "
-                 "(PAM: %d)\n",
-                 state->request->data.logoff.user,
-                 state->response->data.auth.nt_status_string,
-                 state->response->data.auth.pam_error));
-       request_error(state);
-       return;
-}
-
 enum winbindd_result winbindd_dual_pam_logoff(struct winbindd_domain *domain,
                                              struct winbindd_cli_state *state)
 {
@@ -2171,7 +2075,6 @@ enum winbindd_result winbindd_dual_pam_logoff(struct winbindd_domain *domain,
 
 process_result:
 
-       winbindd_delete_memory_creds(state->request->data.logoff.user);
 
        set_auth_errors(state->response, result);
 
@@ -2180,48 +2083,6 @@ process_result:
 
 /* Change user password with auth crap*/
 
-void winbindd_pam_chng_pswd_auth_crap(struct winbindd_cli_state *state)
-{
-       struct winbindd_domain *domain = NULL;
-       const char *domain_name = NULL;
-
-       /* Ensure null termination */
-       state->request->data.chng_pswd_auth_crap.user[
-               sizeof(state->request->data.chng_pswd_auth_crap.user)-1]=0;
-       state->request->data.chng_pswd_auth_crap.domain[
-               sizeof(state->request->data.chng_pswd_auth_crap.domain)-1]=0;
-
-       DEBUG(3, ("[%5lu]: pam change pswd auth crap domain: %s user: %s\n",
-                 (unsigned long)state->pid,
-                 state->request->data.chng_pswd_auth_crap.domain,
-                 state->request->data.chng_pswd_auth_crap.user));
-
-       if (*state->request->data.chng_pswd_auth_crap.domain != '\0') {
-               domain_name = state->request->data.chng_pswd_auth_crap.domain;
-       } else if (lp_winbind_use_default_domain()) {
-               domain_name = lp_workgroup();
-       }
-
-       if (domain_name != NULL)
-               domain = find_domain_from_name(domain_name);
-
-       if (domain != NULL) {
-               DEBUG(7, ("[%5lu]: pam auth crap changing pswd in domain: "
-                         "%s\n", (unsigned long)state->pid,domain->name));
-               sendto_domain(state, domain);
-               return;
-       }
-
-       set_auth_errors(state->response, NT_STATUS_NO_SUCH_USER);
-       DEBUG(5, ("CRAP change password  for %s\\%s returned %s (PAM: %d)\n",
-                 state->request->data.chng_pswd_auth_crap.domain,
-                 state->request->data.chng_pswd_auth_crap.user,
-                 state->response->data.auth.nt_status_string,
-                 state->response->data.auth.pam_error));
-       request_error(state);
-       return;
-}
-
 enum winbindd_result winbindd_dual_pam_chng_pswd_auth_crap(struct winbindd_domain *domainSt, struct winbindd_cli_state *state)
 {
        NTSTATUS result;
@@ -2232,7 +2093,9 @@ enum winbindd_result winbindd_dual_pam_chng_pswd_auth_crap(struct winbindd_domai
        fstring  domain,user;
        struct policy_handle dom_pol;
        struct winbindd_domain *contact_domain = domainSt;
-       struct rpc_pipe_client *cli;
+       struct rpc_pipe_client *cli = NULL;
+
+       ZERO_STRUCT(dom_pol);
 
        /* Ensure null termination */
        state->request->data.chng_pswd_auth_crap.user[
@@ -2281,24 +2144,20 @@ enum winbindd_result winbindd_dual_pam_chng_pswd_auth_crap(struct winbindd_domai
                  (unsigned long)state->pid, domain, user));
 
        /* Change password */
-       new_nt_password = data_blob_talloc(
-               state->mem_ctx,
+       new_nt_password = data_blob_const(
                state->request->data.chng_pswd_auth_crap.new_nt_pswd,
                state->request->data.chng_pswd_auth_crap.new_nt_pswd_len);
 
-       old_nt_hash_enc = data_blob_talloc(
-               state->mem_ctx,
+       old_nt_hash_enc = data_blob_const(
                state->request->data.chng_pswd_auth_crap.old_nt_hash_enc,
                state->request->data.chng_pswd_auth_crap.old_nt_hash_enc_len);
 
        if(state->request->data.chng_pswd_auth_crap.new_lm_pswd_len > 0)        {
-               new_lm_password = data_blob_talloc(
-                       state->mem_ctx,
+               new_lm_password = data_blob_const(
                        state->request->data.chng_pswd_auth_crap.new_lm_pswd,
                        state->request->data.chng_pswd_auth_crap.new_lm_pswd_len);
 
-               old_lm_hash_enc = data_blob_talloc(
-                       state->mem_ctx,
+               old_lm_hash_enc = data_blob_const(
                        state->request->data.chng_pswd_auth_crap.old_lm_hash_enc,
                        state->request->data.chng_pswd_auth_crap.old_lm_hash_enc_len);
        } else {
@@ -2320,6 +2179,16 @@ enum winbindd_result winbindd_dual_pam_chng_pswd_auth_crap(struct winbindd_domai
 
  done:
 
+       if (strequal(contact_domain->name, get_global_sam_name())) {
+               /* FIXME: internal rpc pipe does not cache handles yet */
+               if (cli) {
+                       if (is_valid_policy_hnd(&dom_pol)) {
+                               rpccli_samr_Close(cli, state->mem_ctx, &dom_pol);
+                       }
+                       TALLOC_FREE(cli);
+               }
+       }
+
        set_auth_errors(state->response, result);
 
        DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2,