Finish removal of iconv_convenience in public API's.
[bbaumbach/samba-autobuild/.git] / source4 / torture / rpc / samlogon.c
index 24b2511bc75e98e77f1ce021ada80e327f6ee2d3..c59e4474435994fe5d07b55cf68988ad0d2dc855 100644 (file)
 #include "librpc/gen_ndr/ndr_netlogon.h"
 #include "librpc/gen_ndr/ndr_netlogon_c.h"
 #include "librpc/gen_ndr/ndr_samr_c.h"
-#include "auth/auth.h"
-#include "lib/crypto/crypto.h"
+#include "../lib/crypto/crypto.h"
 #include "lib/cmdline/popt_common.h"
-#include "torture/rpc/rpc.h"
-#include "auth/gensec/schannel_proto.h"
+#include "torture/rpc/torture_rpc.h"
 #include "auth/gensec/gensec.h"
 #include "libcli/auth/libcli_auth.h"
 #include "param/param.h"
@@ -53,7 +51,9 @@ struct samlogon_state {
        const char *comment;
        const char *account_name;
        const char *account_domain;
+       const char *netbios_name;
        const char *password;
+       const char *workgroup;
        struct dcerpc_pipe *p;
        int function_level;
        uint32_t parameter_control;
@@ -61,11 +61,10 @@ struct samlogon_state {
        struct netr_LogonSamLogonEx r_ex;
        struct netr_LogonSamLogonWithFlags r_flags;
        struct netr_Authenticator auth, auth2;
-       struct creds_CredentialState *creds;
+       struct netlogon_creds_CredentialState *creds;
        NTSTATUS expected_error;
        bool old_password; /* Allow an old password to be accepted or rejected without error, as well as session key bugs */
        DATA_BLOB chall;
-       struct smb_iconv_convenience *iconv_convenience;
 };
 
 /* 
@@ -90,9 +89,9 @@ static NTSTATUS check_samlogon(struct samlogon_state *samlogon_state,
        struct netr_SamBaseInfo *base = NULL;
        uint16_t validation_level = 0;
        
-       samlogon_state->r.in.logon.network = &ninfo;
-       samlogon_state->r_ex.in.logon.network = &ninfo;
-       samlogon_state->r_flags.in.logon.network = &ninfo;
+       samlogon_state->r.in.logon->network = &ninfo;
+       samlogon_state->r_ex.in.logon->network = &ninfo;
+       samlogon_state->r_flags.in.logon->network = &ninfo;
        
        ninfo.identity_info.domain_name.string = samlogon_state->account_domain;
        ninfo.identity_info.parameter_control = parameter_control;
@@ -151,92 +150,113 @@ static NTSTATUS check_samlogon(struct samlogon_state *samlogon_state,
        switch (samlogon_state->function_level) {
        case NDR_NETR_LOGONSAMLOGON: 
                ZERO_STRUCT(samlogon_state->auth2);
-               creds_client_authenticator(samlogon_state->creds, &samlogon_state->auth);
+               netlogon_creds_client_authenticator(samlogon_state->creds, &samlogon_state->auth);
 
                r->out.return_authenticator = NULL;
-               status = dcerpc_netr_LogonSamLogon(samlogon_state->p, samlogon_state->mem_ctx, r);
-               if (!r->out.return_authenticator || 
-                   !creds_client_check(samlogon_state->creds, &r->out.return_authenticator->cred)) {
-                       d_printf("Credential chaining failed\n");
-               }
+               status = dcerpc_netr_LogonSamLogon_r(samlogon_state->p->binding_handle,
+                                                    samlogon_state->mem_ctx, r);
                if (!NT_STATUS_IS_OK(status)) {
                        if (error_string) {
                                *error_string = strdup(nt_errstr(status));
                        }
                        return status;
                }
+               if (!r->out.return_authenticator || 
+                   !netlogon_creds_client_check(samlogon_state->creds, &r->out.return_authenticator->cred)) {
+                       d_printf("Credential chaining failed\n");
+               }
+               if (!NT_STATUS_IS_OK(r->out.result)) {
+                       if (error_string) {
+                               *error_string = strdup(nt_errstr(r->out.result));
+                       }
+                       return r->out.result;
+               }
 
                validation_level = r->in.validation_level;
 
-               creds_decrypt_samlogon(samlogon_state->creds, validation_level, &r->out.validation);
+               netlogon_creds_decrypt_samlogon(samlogon_state->creds, validation_level, r->out.validation);
 
                switch (validation_level) {
                case 2:
-                       base = &r->out.validation.sam2->base;
+                       base = &r->out.validation->sam2->base;
                        break;
                case 3:
-                       base = &r->out.validation.sam3->base;
+                       base = &r->out.validation->sam3->base;
                        break;
                case 6:
-                       base = &r->out.validation.sam6->base;
+                       base = &r->out.validation->sam6->base;
                        break;
                }
                break;
        case NDR_NETR_LOGONSAMLOGONEX: 
-               status = dcerpc_netr_LogonSamLogonEx(samlogon_state->p, samlogon_state->mem_ctx, r_ex);
+               status = dcerpc_netr_LogonSamLogonEx_r(samlogon_state->p->binding_handle,
+                                                      samlogon_state->mem_ctx, r_ex);
                if (!NT_STATUS_IS_OK(status)) {
                        if (error_string) {
                                *error_string = strdup(nt_errstr(status));
                        }
                        return status;
                }
+               if (!NT_STATUS_IS_OK(r_ex->out.result)) {
+                       if (error_string) {
+                               *error_string = strdup(nt_errstr(r_ex->out.result));
+                       }
+                       return r_ex->out.result;
+               }
 
                validation_level = r_ex->in.validation_level;
 
-               creds_decrypt_samlogon(samlogon_state->creds, validation_level, &r_ex->out.validation);
+               netlogon_creds_decrypt_samlogon(samlogon_state->creds, validation_level, r_ex->out.validation);
 
                switch (validation_level) {
                case 2:
-                       base = &r_ex->out.validation.sam2->base;
+                       base = &r_ex->out.validation->sam2->base;
                        break;
                case 3:
-                       base = &r_ex->out.validation.sam3->base;
+                       base = &r_ex->out.validation->sam3->base;
                        break;
                case 6:
-                       base = &r_ex->out.validation.sam6->base;
+                       base = &r_ex->out.validation->sam6->base;
                        break;
                }
                break;
        case NDR_NETR_LOGONSAMLOGONWITHFLAGS: 
                ZERO_STRUCT(samlogon_state->auth2);
-               creds_client_authenticator(samlogon_state->creds, &samlogon_state->auth);
+               netlogon_creds_client_authenticator(samlogon_state->creds, &samlogon_state->auth);
 
                r_flags->out.return_authenticator = NULL;
-               status = dcerpc_netr_LogonSamLogonWithFlags(samlogon_state->p, samlogon_state->mem_ctx, r_flags);
-               if (!r_flags->out.return_authenticator || 
-                   !creds_client_check(samlogon_state->creds, &r_flags->out.return_authenticator->cred)) {
-                       d_printf("Credential chaining failed\n");
-               }
+               status = dcerpc_netr_LogonSamLogonWithFlags_r(samlogon_state->p->binding_handle,
+                                                             samlogon_state->mem_ctx, r_flags);
                if (!NT_STATUS_IS_OK(status)) {
                        if (error_string) {
                                *error_string = strdup(nt_errstr(status));
                        }
                        return status;
                }
+               if (!r_flags->out.return_authenticator || 
+                   !netlogon_creds_client_check(samlogon_state->creds, &r_flags->out.return_authenticator->cred)) {
+                       d_printf("Credential chaining failed\n");
+               }
+               if (!NT_STATUS_IS_OK(r_flags->out.result)) {
+                       if (error_string) {
+                               *error_string = strdup(nt_errstr(r_flags->out.result));
+                       }
+                       return r_flags->out.result;
+               }
                
                validation_level = r_flags->in.validation_level;
 
-               creds_decrypt_samlogon(samlogon_state->creds, validation_level, &r_flags->out.validation);
+               netlogon_creds_decrypt_samlogon(samlogon_state->creds, validation_level, r_flags->out.validation);
 
                switch (validation_level) {
                case 2:
-                       base = &r_flags->out.validation.sam2->base;
+                       base = &r_flags->out.validation->sam2->base;
                        break;
                case 3:
-                       base = &r_flags->out.validation.sam3->base;
+                       base = &r_flags->out.validation->sam3->base;
                        break;
                case 6:
-                       base = &r_flags->out.validation.sam6->base;
+                       base = &r_flags->out.validation->sam6->base;
                        break;
                }
                break;
@@ -593,7 +613,7 @@ static bool test_lmv2_ntlmv2_broken(struct samlogon_state *samlogon_state,
        DATA_BLOB lmv2_response = data_blob(NULL, 0);
        DATA_BLOB lmv2_session_key = data_blob(NULL, 0);
        DATA_BLOB ntlmv2_session_key = data_blob(NULL, 0);
-       DATA_BLOB names_blob = NTLMv2_generate_names_blob(samlogon_state->mem_ctx, samlogon_state->iconv_convenience, TEST_MACHINE_NAME, lp_workgroup(global_loadparm));
+       DATA_BLOB names_blob = NTLMv2_generate_names_blob(samlogon_state->mem_ctx, TEST_MACHINE_NAME, samlogon_state->workgroup);
 
        uint8_t lm_session_key[8];
        uint8_t user_session_key[16];
@@ -741,7 +761,7 @@ static bool test_lmv2_ntlm_broken(struct samlogon_state *samlogon_state,
        DATA_BLOB lmv2_response = data_blob(NULL, 0);
        DATA_BLOB lmv2_session_key = data_blob(NULL, 0);
        DATA_BLOB ntlmv2_session_key = data_blob(NULL, 0);
-       DATA_BLOB names_blob = NTLMv2_generate_names_blob(samlogon_state->mem_ctx, samlogon_state->iconv_convenience, lp_netbios_name(global_loadparm), lp_workgroup(global_loadparm));
+       DATA_BLOB names_blob = NTLMv2_generate_names_blob(samlogon_state->mem_ctx, samlogon_state->netbios_name, samlogon_state->workgroup);
 
        DATA_BLOB ntlm_response = data_blob_talloc(samlogon_state->mem_ctx, NULL, 24);
        DATA_BLOB ntlm_session_key = data_blob_talloc(samlogon_state->mem_ctx, NULL, 16);
@@ -1148,7 +1168,7 @@ static bool test_plaintext(struct samlogon_state *samlogon_state, enum ntlm_brea
        DATA_BLOB lm_response = data_blob(NULL, 0);
        char *password;
        char *dospw;
-       void *unicodepw;
+       smb_ucs2_t *unicodepw;
 
        uint8_t user_session_key[16];
        uint8_t lm_key[16];
@@ -1159,9 +1179,8 @@ static bool test_plaintext(struct samlogon_state *samlogon_state, enum ntlm_brea
 
        ZERO_STRUCT(user_session_key);
        
-       if ((push_ucs2_talloc(samlogon_state->mem_ctx, 
-                             samlogon_state->iconv_convenience, 
-                             &unicodepw, samlogon_state->password)) == -1) {
+       if (!push_ucs2_talloc(samlogon_state->mem_ctx, 
+                             &unicodepw, samlogon_state->password, NULL)) {
                DEBUG(0, ("push_ucs2_allocate failed!\n"));
                exit(1);
        }
@@ -1170,11 +1189,10 @@ static bool test_plaintext(struct samlogon_state *samlogon_state, enum ntlm_brea
 
        password = strupper_talloc(samlogon_state->mem_ctx, samlogon_state->password);
 
-       if ((convert_string_talloc(samlogon_state->mem_ctx, 
-                                  samlogon_state->iconv_convenience,
+       if (!convert_string_talloc(samlogon_state->mem_ctx, 
                                   CH_UNIX, CH_DOS, 
                                   password, strlen(password)+1, 
-                                  (void**)&dospw)) == -1) {
+                                  (void**)&dospw, NULL, false)) {
                DEBUG(0, ("convert_string_talloc failed!\n"));
                exit(1);
        }
@@ -1313,7 +1331,7 @@ static const struct ntlm_tests {
 */
 static bool test_SamLogon(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
                          struct torture_context *tctx,
-                         struct creds_CredentialState *creds, 
+                         struct netlogon_creds_CredentialState *creds, 
                          const char *comment,
                          const char *account_domain, const char *account_name, 
                          const char *plain_pass, uint32_t parameter_control,
@@ -1330,36 +1348,56 @@ static bool test_SamLogon(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
                NDR_NETR_LOGONSAMLOGONEX,
                NDR_NETR_LOGONSAMLOGONWITHFLAGS };
        struct samlogon_state samlogon_state;
-       
-       d_printf("testing netr_LogonSamLogon and netr_LogonSamLogonWithFlags\n");
+
+       union netr_LogonLevel logon;
+       union netr_Validation validation;
+       uint8_t authoritative = 0;
+       uint32_t flags = 0;
+
+       ZERO_STRUCT(logon);
+
+       d_printf("Testing netr_LogonSamLogon and netr_LogonSamLogonWithFlags\n");
        
        samlogon_state.comment = comment;
        samlogon_state.account_name = account_name;
        samlogon_state.account_domain = account_domain;
        samlogon_state.password = plain_pass;
+       samlogon_state.workgroup = lp_workgroup(tctx->lp_ctx);
+       samlogon_state.netbios_name = lp_netbios_name(tctx->lp_ctx);
        samlogon_state.p = p;
        samlogon_state.creds = creds;
        samlogon_state.expected_error = expected_error;
        samlogon_state.chall = data_blob_talloc(fn_ctx, NULL, 8);
        samlogon_state.parameter_control = parameter_control;
        samlogon_state.old_password = old_password;
-       samlogon_state.iconv_convenience = lp_iconv_convenience(tctx->lp_ctx);
 
        generate_random_buffer(samlogon_state.chall.data, 8);
        samlogon_state.r_flags.in.server_name = talloc_asprintf(fn_ctx, "\\\\%s", dcerpc_server_name(p));
        samlogon_state.r_flags.in.computer_name = TEST_MACHINE_NAME;
        samlogon_state.r_flags.in.credential = &samlogon_state.auth;
        samlogon_state.r_flags.in.return_authenticator = &samlogon_state.auth2;
-       samlogon_state.r_flags.in.flags = 0;
+       samlogon_state.r_flags.in.flags = &flags;
+       samlogon_state.r_flags.in.logon = &logon;
+       samlogon_state.r_flags.out.validation = &validation;
+       samlogon_state.r_flags.out.authoritative = &authoritative;
+       samlogon_state.r_flags.out.flags = &flags;
 
        samlogon_state.r_ex.in.server_name = talloc_asprintf(fn_ctx, "\\\\%s", dcerpc_server_name(p));
        samlogon_state.r_ex.in.computer_name = TEST_MACHINE_NAME;
-       samlogon_state.r_ex.in.flags = 0;
+       samlogon_state.r_ex.in.flags = &flags;
+       samlogon_state.r_ex.in.logon = &logon;
+       samlogon_state.r_ex.out.validation = &validation;
+       samlogon_state.r_ex.out.authoritative = &authoritative;
+       samlogon_state.r_ex.out.flags = &flags;
 
        samlogon_state.r.in.server_name = talloc_asprintf(fn_ctx, "\\\\%s", dcerpc_server_name(p));
        samlogon_state.r.in.computer_name = TEST_MACHINE_NAME;
        samlogon_state.r.in.credential = &samlogon_state.auth;
        samlogon_state.r.in.return_authenticator = &samlogon_state.auth2;
+       samlogon_state.r.in.logon = &logon;
+       samlogon_state.r.out.validation = &validation;
+       samlogon_state.r.out.authoritative = &authoritative;
+
 
        for (f=0;f<ARRAY_SIZE(function_levels);f++) {
                for (i=0; test_table[i].fn; i++) {
@@ -1407,7 +1445,7 @@ static bool test_SamLogon(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
   test an ADS style interactive domain logon
 */
 bool test_InteractiveLogon(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
-                          struct creds_CredentialState *creds, 
+                          struct netlogon_creds_CredentialState *creds, 
                           const char *comment,
                           const char *workstation_name,
                           const char *account_domain, const char *account_name,
@@ -1419,21 +1457,35 @@ bool test_InteractiveLogon(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
        struct netr_LogonSamLogonWithFlags r;
        struct netr_Authenticator a, ra;
        struct netr_PasswordInfo pinfo;
+       uint32_t flags = 0;
+
+       union netr_LogonLevel logon;
+       union netr_Validation validation;
+       uint8_t authoritative = 0;
+       struct dcerpc_binding_handle *b = p->binding_handle;
 
        ZERO_STRUCT(a);
        ZERO_STRUCT(r);
        ZERO_STRUCT(ra);
 
-       creds_client_authenticator(creds, &a);
+       ZERO_STRUCT(logon);
+       ZERO_STRUCT(validation);
+
+       netlogon_creds_client_authenticator(creds, &a);
+
+       logon.password = &pinfo;
 
        r.in.server_name = talloc_asprintf(fn_ctx, "\\\\%s", dcerpc_server_name(p));
        r.in.computer_name = TEST_MACHINE_NAME;
        r.in.credential = &a;
        r.in.return_authenticator = &ra;
        r.in.logon_level = 5;
-       r.in.logon.password = &pinfo;
+       r.in.logon = &logon;
        r.in.validation_level = 6;
-       r.in.flags = 0;
+       r.in.flags = &flags;
+       r.out.validation = &validation;
+       r.out.authoritative = &authoritative;
+       r.out.flags = &flags;
 
        pinfo.identity_info.domain_name.string = account_domain;
        pinfo.identity_info.parameter_control = parameter_control;
@@ -1448,18 +1500,23 @@ bool test_InteractiveLogon(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
        E_md4hash(plain_pass, pinfo.ntpassword.hash);
 
        if (creds->negotiate_flags & NETLOGON_NEG_ARCFOUR) {
-               creds_arcfour_crypt(creds, pinfo.lmpassword.hash, 16);
-               creds_arcfour_crypt(creds, pinfo.ntpassword.hash, 16);
+               netlogon_creds_arcfour_crypt(creds, pinfo.lmpassword.hash, 16);
+               netlogon_creds_arcfour_crypt(creds, pinfo.ntpassword.hash, 16);
        } else {
-               creds_des_encrypt(creds, &pinfo.lmpassword);
-               creds_des_encrypt(creds, &pinfo.ntpassword);
+               netlogon_creds_des_encrypt(creds, &pinfo.lmpassword);
+               netlogon_creds_des_encrypt(creds, &pinfo.ntpassword);
        }
 
        d_printf("Testing netr_LogonSamLogonWithFlags '%s' (Interactive Logon)\n", comment);
 
-       status = dcerpc_netr_LogonSamLogonWithFlags(p, fn_ctx, &r);
+       status = dcerpc_netr_LogonSamLogonWithFlags_r(b, fn_ctx, &r);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_printf("%s: netr_LogonSamLogonWithFlags - %s\n",
+                        __location__, nt_errstr(status));
+               return false;
+       }
        if (!r.out.return_authenticator 
-           || !creds_client_check(creds, &r.out.return_authenticator->cred)) {
+           || !netlogon_creds_client_check(creds, &r.out.return_authenticator->cred)) {
                d_printf("Credential chaining failed\n");
                talloc_free(fn_ctx);
                return false;
@@ -1467,9 +1524,9 @@ bool test_InteractiveLogon(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
 
        talloc_free(fn_ctx);
 
-       if (!NT_STATUS_EQUAL(expected_error, status)) {
+       if (!NT_STATUS_EQUAL(expected_error, r.out.result)) {
                d_printf("[%s]\\[%s] netr_LogonSamLogonWithFlags - expected %s got %s\n", 
-                      account_domain, account_name, nt_errstr(expected_error), nt_errstr(status));
+                      account_domain, account_name, nt_errstr(expected_error), nt_errstr(r.out.result));
                return false;
        }
 
@@ -1505,7 +1562,8 @@ bool torture_rpc_samlogon(struct torture_context *torture)
                0 /* yes, this is a valid flag, causes the use of DES */ 
        };
 
-       struct creds_CredentialState *creds;
+       struct netlogon_creds_CredentialState *creds;
+       struct dcerpc_pipe *tmp_p = NULL;
 
        test_machine_account = talloc_asprintf(mem_ctx, "%s$", TEST_MACHINE_NAME);
        /* We only need to join as a workstation here, and in future,
@@ -1532,7 +1590,8 @@ bool torture_rpc_samlogon(struct torture_context *torture)
 
        old_user_password = user_password;
 
-       test_ChangePasswordUser3(torture_join_samr_pipe(user_ctx), mem_ctx,
+       tmp_p = torture_join_samr_pipe(user_ctx);
+       test_ChangePasswordUser3(tmp_p, torture,
                                 TEST_USER_NAME, 16 /* > 14 */, &user_password, 
                                 NULL, 0, false);
 
@@ -1554,12 +1613,18 @@ bool torture_rpc_samlogon(struct torture_context *torture)
        u.info21.fields_present = SAMR_FIELD_WORKSTATIONS;
        u.info21.workstations.string = "not" TEST_MACHINE_NAME;
 
-       status = dcerpc_samr_SetUserInfo(torture_join_samr_pipe(user_ctx_wrong_wks), mem_ctx, &s);
+       tmp_p = torture_join_samr_pipe(user_ctx_wrong_wks);
+       status = dcerpc_samr_SetUserInfo_r(tmp_p->binding_handle, mem_ctx, &s);
        if (!NT_STATUS_IS_OK(status)) {
                printf("SetUserInfo (list of workstations) failed - %s\n", nt_errstr(status));
                ret = false;
                goto failed;
        }
+       if (!NT_STATUS_IS_OK(s.out.result)) {
+               printf("SetUserInfo (list of workstations) failed - %s\n", nt_errstr(s.out.result));
+               ret = false;
+               goto failed;
+       }
 
        user_ctx_wrong_time
                = torture_create_testuser(torture, TEST_USER_NAME_WRONG_TIME,
@@ -1581,12 +1646,18 @@ bool torture_rpc_samlogon(struct torture_context *torture)
        u.info21.logon_hours.units_per_week = 168;
        u.info21.logon_hours.bits = talloc_zero_array(mem_ctx, uint8_t, 168);
 
-       status = dcerpc_samr_SetUserInfo(torture_join_samr_pipe(user_ctx_wrong_time), mem_ctx, &s);
+       tmp_p = torture_join_samr_pipe(user_ctx_wrong_time);
+       status = dcerpc_samr_SetUserInfo_r(tmp_p->binding_handle, mem_ctx, &s);
        if (!NT_STATUS_IS_OK(status)) {
                printf("SetUserInfo (logon times and list of workstations) failed - %s\n", nt_errstr(status));
                ret = false;
                goto failed;
        }
+       if (!NT_STATUS_IS_OK(s.out.result)) {
+               printf("SetUserInfo (list of workstations) failed - %s\n", nt_errstr(s.out.result));
+               ret = false;
+               goto failed;
+       }
 
        status = torture_rpc_binding(torture, &b);
        if (!NT_STATUS_IS_OK(status)) {
@@ -1602,7 +1673,7 @@ bool torture_rpc_samlogon(struct torture_context *torture)
 
        status = dcerpc_pipe_connect_b(mem_ctx, &p, b, 
                                       &ndr_table_netlogon,
-                                      machine_credentials, NULL, torture->lp_ctx);
+                                      machine_credentials, torture->ev, torture->lp_ctx);
 
        if (!NT_STATUS_IS_OK(status)) {
                d_printf("RPC pipe connect as domain member failed: %s\n", nt_errstr(status));
@@ -1847,9 +1918,9 @@ bool torture_rpc_samlogon(struct torture_context *torture)
 failed:
        talloc_free(mem_ctx);
 
-       torture_leave_domain(join_ctx);
-       torture_leave_domain(user_ctx);
-       torture_leave_domain(user_ctx_wrong_wks);
-       torture_leave_domain(user_ctx_wrong_time);
+       torture_leave_domain(torture, join_ctx);
+       torture_leave_domain(torture, user_ctx);
+       torture_leave_domain(torture, user_ctx_wrong_wks);
+       torture_leave_domain(torture, user_ctx_wrong_time);
        return ret;
 }