LCOV - code coverage report
Current view: top level - source4/torture/winbind - winbind.c (source / functions) Hit Total Coverage
Test: coverage report for v4-17-test 1498b464 Lines: 119 125 95.2 %
Date: 2024-06-13 04:01:37 Functions: 7 7 100.0 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             :    SMB torture tester
       4             :    Copyright (C) Stefan Metzmacher 2007
       5             :    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2012
       6             :    Copyright (C) Christof Schmit <christof.schmitt@us.ibm.com> 2012
       7             : 
       8             :    This program is free software; you can redistribute it and/or modify
       9             :    it under the terms of the GNU General Public License as published by
      10             :    the Free Software Foundation; either version 3 of the License, or
      11             :    (at your option) any later version.
      12             : 
      13             :    This program is distributed in the hope that it will be useful,
      14             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      16             :    GNU General Public License for more details.
      17             : 
      18             :    You should have received a copy of the GNU General Public License
      19             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      20             : */
      21             : 
      22             : #include "includes.h"
      23             : #include "torture/smbtorture.h"
      24             : #include "torture/winbind/proto.h"
      25             : #include "auth/auth.h"
      26             : #include "auth/auth_sam_reply.h"
      27             : #include "auth/gensec/gensec.h"
      28             : #include "system/kerberos.h"
      29             : #include "auth/kerberos/kerberos.h"
      30             : #include "auth/credentials/credentials.h"
      31             : #include "param/param.h"
      32             : #include "lib/cmdline/cmdline.h"
      33             : #include "auth/kerberos/pac_utils.h"
      34             : #include "wbclient.h"
      35             : 
      36             : struct pac_data {
      37             :         DATA_BLOB pac_blob;
      38             : };
      39             : 
      40             : /* A helper function which avoids touching the local databases to
      41             :  * generate the session info, as we just want to verify the PAC
      42             :  * details, not the full local token */
      43           6 : static NTSTATUS test_generate_session_info_pac(struct auth4_context *auth_ctx,
      44             :                                                TALLOC_CTX *mem_ctx,
      45             :                                                struct smb_krb5_context *smb_krb5_context,
      46             :                                                DATA_BLOB *pac_blob,
      47             :                                                const char *principal_name,
      48             :                                                const struct tsocket_address *remote_address,
      49             :                                                uint32_t session_info_flags,
      50             :                                                struct auth_session_info **session_info)
      51             : {
      52             :         NTSTATUS nt_status;
      53             :         struct auth_user_info_dc *user_info_dc;
      54             :         TALLOC_CTX *tmp_ctx;
      55             :         struct pac_data *pac_data;
      56             : 
      57           6 :         if (pac_blob == NULL) {
      58           0 :                 DBG_ERR("pac_blob missing\n");
      59           0 :                 return NT_STATUS_NO_IMPERSONATION_TOKEN;
      60             :         }
      61             : 
      62           6 :         tmp_ctx = talloc_named(mem_ctx, 0, "gensec_gssapi_session_info context");
      63           6 :         NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
      64             : 
      65           6 :         auth_ctx->private_data = pac_data = talloc_zero(auth_ctx, struct pac_data); 
      66             : 
      67           6 :         pac_data->pac_blob = *pac_blob;
      68             : 
      69           6 :         talloc_steal(pac_data, pac_data->pac_blob.data);
      70           6 :         nt_status = kerberos_pac_blob_to_user_info_dc(tmp_ctx,
      71             :                                                       *pac_blob,
      72             :                                                       smb_krb5_context->krb5_context,
      73             :                                                       &user_info_dc,
      74             :                                                       NULL, NULL);
      75           6 :         if (!NT_STATUS_IS_OK(nt_status)) {
      76           0 :                 talloc_free(tmp_ctx);
      77           0 :                 return nt_status;
      78             :         }
      79             : 
      80           6 :         if (user_info_dc->info->authenticated) {
      81           6 :                 session_info_flags |= AUTH_SESSION_INFO_AUTHENTICATED;
      82             :         }
      83             : 
      84           6 :         session_info_flags |= AUTH_SESSION_INFO_SIMPLE_PRIVILEGES;
      85           6 :         nt_status = auth_generate_session_info(mem_ctx,
      86             :                                                NULL,
      87             :                                                NULL,
      88             :                                                user_info_dc, session_info_flags,
      89             :                                                session_info);
      90           6 :         if (!NT_STATUS_IS_OK(nt_status)) {
      91           0 :                 talloc_free(tmp_ctx);
      92           0 :                 return nt_status;
      93             :         }
      94             : 
      95           6 :         talloc_free(tmp_ctx);
      96           6 :         return nt_status;
      97             : }
      98             : 
      99           6 : static bool torture_decode_compare_pac(struct torture_context *tctx,
     100             :                                        DATA_BLOB pac)
     101             : {
     102             :         struct wbcAuthUserParams params;
     103             :         struct wbcAuthUserInfo *info;
     104             :         struct wbcAuthErrorInfo *error;
     105             :         struct PAC_LOGON_INFO *logon_info;
     106             :         struct netr_SamInfo3 *info3;
     107             :         struct netr_SamBaseInfo *base;
     108             :         wbcErr wbc_err;
     109             :         NTSTATUS status;
     110             :         int sid_idx, i;
     111             :         char sid_str[50];
     112             : 
     113             :         /* Let winbind decode the PAC */
     114           6 :         memset(&params, 0, sizeof(params));
     115           6 :         params.level = WBC_AUTH_USER_LEVEL_PAC;
     116           6 :         params.password.pac.data = pac.data;
     117           6 :         params.password.pac.length = pac.length;
     118             : 
     119           6 :         wbc_err = wbcAuthenticateUserEx(&params, &info, &error);
     120           6 :         torture_assert(tctx, WBC_ERROR_IS_OK(wbc_err), wbcErrorString(wbc_err));
     121             : 
     122             :         /* Decode the PAC internally */
     123           6 :         status = kerberos_pac_logon_info(tctx, pac, NULL, NULL, NULL, NULL, 0,
     124             :                                          &logon_info);
     125           6 :         torture_assert(tctx, NT_STATUS_IS_OK(status), "pac_logon_info");
     126           6 :         info3 = &logon_info->info3;
     127           6 :         base = &info3->base;
     128             : 
     129             :         /* Compare the decoded data from winbind and from internal call */
     130           6 :         torture_assert(tctx, info->user_flags == base->user_flags, "user_flags");
     131           6 :         torture_assert_str_equal(tctx, info->account_name, base->account_name.string, "account_name");
     132           6 :         torture_assert_str_equal(tctx, info->full_name, base->full_name.string, "full_name");
     133           6 :         torture_assert_str_equal(tctx, info->domain_name, base->logon_domain.string, "domain_name");
     134           6 :         torture_assert(tctx, info->acct_flags == base->acct_flags, "acct_flags");
     135           6 :         torture_assert(tctx, info->logon_count == base->logon_count, "logon_count");
     136           6 :         torture_assert(tctx, info->bad_password_count == base->bad_password_count, "bad_password_count");
     137           6 :         torture_assert(tctx, info->logon_time == nt_time_to_unix(base->logon_time), "logon_time");
     138           6 :         torture_assert(tctx, info->logoff_time == nt_time_to_unix(base->logoff_time), "logoff_time");
     139           6 :         torture_assert(tctx, info->kickoff_time == nt_time_to_unix(base->kickoff_time), "kickoff_time");
     140           6 :         torture_assert(tctx, info->pass_last_set_time == nt_time_to_unix(base->last_password_change), "last_password_change");
     141           6 :         torture_assert(tctx, info->pass_can_change_time == nt_time_to_unix(base->allow_password_change), "allow_password_change");
     142           6 :         torture_assert(tctx, info->pass_must_change_time == nt_time_to_unix(base->force_password_change), "force_password_change");
     143           6 :         torture_assert(tctx, info->num_sids == 2 + base->groups.count + info3->sidcount, "num_sids");
     144             : 
     145           6 :         sid_idx = 0;
     146           6 :         wbcSidToStringBuf(&info->sids[sid_idx].sid, sid_str, sizeof(sid_str));
     147           6 :         torture_assert(tctx,
     148             :                        dom_sid_equal(dom_sid_parse_talloc(tctx, sid_str),
     149             :                                      dom_sid_add_rid(tctx, base->domain_sid, base->rid)),
     150             :                        sid_str);
     151             : 
     152           6 :         sid_idx++;
     153           6 :         wbcSidToStringBuf(&info->sids[sid_idx].sid, sid_str, sizeof(sid_str));
     154           6 :         torture_assert(tctx,
     155             :                        dom_sid_equal(dom_sid_parse_talloc(tctx, sid_str),
     156             :                                      dom_sid_add_rid(tctx, base->domain_sid, base->primary_gid)),
     157             :                        sid_str);
     158             : 
     159          12 :         for(i = 0; i < base->groups.count; i++ ) {
     160           6 :                 sid_idx++;
     161           6 :                 wbcSidToStringBuf(&info->sids[sid_idx].sid,
     162             :                                   sid_str, sizeof(sid_str));
     163           6 :                 torture_assert(tctx,
     164             :                                dom_sid_equal(dom_sid_parse_talloc(tctx, sid_str),
     165             :                                              dom_sid_add_rid(tctx, base->domain_sid,
     166             :                                                              base->groups.rids[i].rid)),
     167             :                                sid_str);
     168             :         }
     169             : 
     170          12 :         for(i = 0; i < info3->sidcount; i++) {
     171           6 :                 sid_idx++;
     172           6 :                 wbcSidToStringBuf(&info->sids[sid_idx].sid,
     173             :                                   sid_str, sizeof(sid_str));
     174           6 :                 torture_assert(tctx,
     175             :                                dom_sid_equal(dom_sid_parse_talloc(tctx, sid_str),
     176             :                                              info3->sids[i].sid),
     177             :                                sid_str);
     178             :         }
     179             : 
     180           6 :         return true;
     181             : }
     182             : 
     183           6 : static bool torture_winbind_pac(struct torture_context *tctx,
     184             :                                 const char *sasl_mech,
     185             :                                 const char *mech)
     186             : {
     187             :         NTSTATUS status;
     188             : 
     189             :         struct gensec_security *gensec_client_context;
     190             :         struct gensec_security *gensec_server_context;
     191             :         struct cli_credentials *machine_credentials;
     192             : 
     193             :         DATA_BLOB client_to_server, server_to_client;   
     194             : 
     195             :         struct auth4_context *auth_context;
     196             :         struct auth_session_info *session_info;
     197             :         struct pac_data *pac_data;
     198             : 
     199           6 :         TALLOC_CTX *tmp_ctx = talloc_new(tctx);
     200           6 :         torture_assert(tctx, tmp_ctx != NULL, "talloc_new() failed");
     201             : 
     202           6 :         machine_credentials = cli_credentials_init_server(tmp_ctx,
     203             :                                                           tctx->lp_ctx);
     204           6 :         torture_assert(tctx, machine_credentials != NULL, "cli_credentials_init() failed");
     205             : 
     206           6 :         auth_context = talloc_zero(tmp_ctx, struct auth4_context);
     207           6 :         torture_assert(tctx, auth_context != NULL, "talloc_new() failed");
     208             : 
     209           6 :         auth_context->generate_session_info_pac = test_generate_session_info_pac;
     210             : 
     211           6 :         status = gensec_client_start(tctx, &gensec_client_context,
     212             :                                      lpcfg_gensec_settings(tctx, tctx->lp_ctx));
     213           6 :         torture_assert_ntstatus_ok(tctx, status, "gensec_client_start (client) failed");
     214             : 
     215           6 :         status = gensec_set_target_hostname(gensec_client_context, cli_credentials_get_workstation(machine_credentials));
     216           6 :         torture_assert_ntstatus_ok(tctx, status, "gensec_set_target_hostname (client) failed");
     217             : 
     218           6 :         status = gensec_set_credentials(gensec_client_context,
     219             :                         samba_cmdline_get_creds());
     220           6 :         torture_assert_ntstatus_ok(tctx, status, "gensec_set_credentials (client) failed");
     221             : 
     222           6 :         if (sasl_mech) {
     223           4 :                 status = gensec_start_mech_by_sasl_name(gensec_client_context, sasl_mech);
     224           4 :                 torture_assert_ntstatus_ok(tctx, status, "gensec_start_mech_by_sasl_name (client) failed");
     225             :         } else {
     226           2 :                 status = gensec_start_mech_by_name(gensec_client_context, mech);
     227           2 :                 torture_assert_ntstatus_ok(tctx, status, "gensec_start_mech_by_name (client) failed");
     228             :         }
     229             : 
     230             : 
     231           6 :         status = gensec_server_start(tctx,
     232             :                                      lpcfg_gensec_settings(tctx, tctx->lp_ctx),
     233             :                                      auth_context, &gensec_server_context);
     234           6 :         torture_assert_ntstatus_ok(tctx, status, "gensec_server_start (server) failed");
     235             : 
     236           6 :         status = gensec_set_credentials(gensec_server_context, machine_credentials);
     237           6 :         torture_assert_ntstatus_ok(tctx, status, "gensec_set_credentials (server) failed");
     238             : 
     239           6 :         if (sasl_mech) {
     240           4 :                 status = gensec_start_mech_by_sasl_name(gensec_server_context, sasl_mech);
     241           4 :                 torture_assert_ntstatus_ok(tctx, status, "gensec_start_mech_by_sasl_name (server) failed");
     242             :         } else {
     243           2 :                 status = gensec_start_mech_by_name(gensec_server_context, mech);
     244           2 :                 torture_assert_ntstatus_ok(tctx, status, "gensec_start_mech_by_name (server) failed");
     245             :         }
     246             : 
     247           6 :         server_to_client = data_blob(NULL, 0);
     248             :         
     249             :         do {
     250             :                 /* Do a client-server update dance */
     251          12 :                 status = gensec_update(gensec_client_context, tmp_ctx, server_to_client, &client_to_server);
     252          10 :                 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {;
     253           2 :                         torture_assert_ntstatus_ok(tctx, status, "gensec_update (client) failed");
     254             :                 }
     255             : 
     256          10 :                 status = gensec_update(gensec_server_context, tmp_ctx, client_to_server, &server_to_client);
     257          10 :                 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {;
     258           6 :                         torture_assert_ntstatus_ok(tctx, status, "gensec_update (server) failed");
     259             :                 }
     260             : 
     261          10 :                 if (NT_STATUS_IS_OK(status)) {
     262           6 :                         break;
     263             :                 }
     264             :         } while (1);
     265             : 
     266             :         /* Extract the PAC using Samba's code */
     267             : 
     268           6 :         status = gensec_session_info(gensec_server_context, gensec_server_context, &session_info);
     269           6 :         torture_assert_ntstatus_ok(tctx, status, "gensec_session_info failed");
     270             : 
     271           6 :         pac_data = talloc_get_type(auth_context->private_data, struct pac_data);
     272             : 
     273           6 :         torture_assert(tctx, pac_data != NULL, "gensec_update failed to fill in pac_data in auth_context");
     274           6 :         torture_assert(tctx, pac_data->pac_blob.data != NULL, "pac_blob not present");
     275           6 :         torture_decode_compare_pac(tctx, pac_data->pac_blob);
     276             : 
     277           6 :         return true;
     278             : }
     279             : 
     280           2 : static bool torture_winbind_pac_gssapi(struct torture_context *tctx)
     281             : {
     282           2 :         return torture_winbind_pac(tctx, "GSSAPI", NULL);
     283             : }       
     284             : 
     285           2 : static bool torture_winbind_pac_gss_spnego(struct torture_context *tctx)
     286             : {
     287           2 :         return torture_winbind_pac(tctx, "GSS-SPNEGO", NULL);
     288             : }       
     289             : 
     290           2 : static bool torture_winbind_pac_krb5(struct torture_context *tctx)
     291             : {
     292           2 :         return torture_winbind_pac(tctx, NULL, "krb5");
     293             : }       
     294             : 
     295         964 : NTSTATUS torture_winbind_init(TALLOC_CTX *ctx)
     296             : {
     297         964 :         struct torture_suite *suite = torture_suite_create(ctx, "winbind");
     298             :         struct torture_suite *pac_suite;
     299         964 :         torture_suite_add_suite(suite, torture_winbind_struct_init(suite));
     300         964 :         torture_suite_add_suite(suite, torture_wbclient(suite));
     301             : 
     302         964 :         pac_suite = torture_suite_create(ctx, "pac");
     303         964 :         torture_suite_add_simple_test(pac_suite,
     304             :                                       "GSSAPI", torture_winbind_pac_gssapi);
     305         964 :         torture_suite_add_simple_test(pac_suite,
     306             :                                       "GSS-SPNEGO", torture_winbind_pac_gss_spnego);
     307         964 :         torture_suite_add_simple_test(pac_suite,
     308             :                                       "krb5", torture_winbind_pac_krb5);
     309             : 
     310         964 :         pac_suite->description = talloc_strdup(suite, "Winbind Kerberos PAC tests");
     311             : 
     312         964 :         torture_suite_add_suite(suite, pac_suite);
     313             : 
     314         964 :         suite->description = talloc_strdup(suite, "WINBIND tests");
     315             : 
     316         964 :         torture_register_suite(ctx, suite);
     317             : 
     318         964 :         return NT_STATUS_OK;
     319             : }

Generated by: LCOV version 1.13