LCOV - code coverage report
Current view: top level - source4/torture/winbind - winbind.c (source / functions) Hit Total Coverage
Test: coverage report for master 98b443d9 Lines: 128 157 81.5 %
Date: 2024-05-31 13:13:24 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          12 : 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           0 :         NTSTATUS nt_status;
      53           0 :         struct auth_user_info_dc *user_info_dc;
      54           0 :         TALLOC_CTX *tmp_ctx;
      55           0 :         struct pac_data *pac_data;
      56             : 
      57          12 :         if (pac_blob == NULL) {
      58           0 :                 DBG_ERR("pac_blob missing\n");
      59           0 :                 return NT_STATUS_NO_IMPERSONATION_TOKEN;
      60             :         }
      61             : 
      62          12 :         tmp_ctx = talloc_named(mem_ctx, 0, "gensec_gssapi_session_info context");
      63          12 :         NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
      64             : 
      65          12 :         auth_ctx->private_data = pac_data = talloc_zero(auth_ctx, struct pac_data); 
      66             : 
      67          12 :         pac_data->pac_blob = *pac_blob;
      68             : 
      69          12 :         talloc_steal(pac_data, pac_data->pac_blob.data);
      70          12 :         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          12 :         if (!NT_STATUS_IS_OK(nt_status)) {
      76           0 :                 talloc_free(tmp_ctx);
      77           0 :                 return nt_status;
      78             :         }
      79             : 
      80          12 :         if (!(user_info_dc->info->user_flags & NETLOGON_GUEST)) {
      81          12 :                 session_info_flags |= AUTH_SESSION_INFO_AUTHENTICATED;
      82             :         }
      83             : 
      84          12 :         session_info_flags |= AUTH_SESSION_INFO_SIMPLE_PRIVILEGES;
      85          12 :         nt_status = auth_generate_session_info(mem_ctx,
      86             :                                                NULL,
      87             :                                                NULL,
      88             :                                                user_info_dc, session_info_flags,
      89             :                                                session_info);
      90          12 :         if (!NT_STATUS_IS_OK(nt_status)) {
      91           0 :                 talloc_free(tmp_ctx);
      92           0 :                 return nt_status;
      93             :         }
      94             : 
      95          12 :         talloc_free(tmp_ctx);
      96          12 :         return nt_status;
      97             : }
      98             : 
      99          12 : static bool torture_decode_compare_pac(struct torture_context *tctx,
     100             :                                        DATA_BLOB pac)
     101             : {
     102           0 :         struct wbcAuthUserParams params;
     103           0 :         struct wbcAuthUserInfo *info;
     104           0 :         struct wbcAuthErrorInfo *error;
     105           0 :         struct PAC_LOGON_INFO *logon_info;
     106           0 :         struct netr_SamInfo3 *info3;
     107           0 :         struct netr_SamBaseInfo *base;
     108          12 :         struct PAC_DOMAIN_GROUP_MEMBERSHIP *resource_groups = NULL;
     109           0 :         wbcErr wbc_err;
     110           0 :         NTSTATUS status;
     111           0 :         int sid_idx, i;
     112           0 :         char sid_str[50];
     113             : 
     114             :         /* Let winbind decode the PAC */
     115          12 :         memset(&params, 0, sizeof(params));
     116          12 :         params.level = WBC_AUTH_USER_LEVEL_PAC;
     117          12 :         params.password.pac.data = pac.data;
     118          12 :         params.password.pac.length = pac.length;
     119             : 
     120          12 :         wbc_err = wbcAuthenticateUserEx(&params, &info, &error);
     121          12 :         torture_assert(tctx, WBC_ERROR_IS_OK(wbc_err), wbcErrorString(wbc_err));
     122             : 
     123             :         /* Decode the PAC internally */
     124          12 :         status = kerberos_pac_logon_info(tctx, pac, NULL, NULL, NULL, NULL, 0,
     125             :                                          &logon_info);
     126          12 :         torture_assert(tctx, NT_STATUS_IS_OK(status), "pac_logon_info");
     127          12 :         info3 = &logon_info->info3;
     128          12 :         base = &info3->base;
     129          12 :         resource_groups = &logon_info->resource_groups;
     130             : 
     131             :         /* Compare the decoded data from winbind and from internal call */
     132          12 :         torture_assert(tctx, info->user_flags == base->user_flags, "user_flags");
     133          12 :         torture_assert_str_equal(tctx, info->account_name, base->account_name.string, "account_name");
     134          12 :         torture_assert_str_equal(tctx, info->full_name, base->full_name.string, "full_name");
     135          12 :         torture_assert_str_equal(tctx, info->domain_name, base->logon_domain.string, "domain_name");
     136          12 :         torture_assert(tctx, info->acct_flags == base->acct_flags, "acct_flags");
     137          12 :         torture_assert(tctx, info->logon_count == base->logon_count, "logon_count");
     138          12 :         torture_assert(tctx, info->bad_password_count == base->bad_password_count, "bad_password_count");
     139          12 :         torture_assert(tctx, info->logon_time == nt_time_to_unix(base->logon_time), "logon_time");
     140          12 :         torture_assert(tctx, info->logoff_time == nt_time_to_unix(base->logoff_time), "logoff_time");
     141          12 :         torture_assert(tctx, info->kickoff_time == nt_time_to_unix(base->kickoff_time), "kickoff_time");
     142          12 :         torture_assert(tctx, info->pass_last_set_time == nt_time_to_unix(base->last_password_change), "last_password_change");
     143          12 :         torture_assert(tctx, info->pass_can_change_time == nt_time_to_unix(base->allow_password_change), "allow_password_change");
     144          12 :         torture_assert(tctx, info->pass_must_change_time == nt_time_to_unix(base->force_password_change), "force_password_change");
     145          12 :         torture_assert(tctx, info->num_sids == 2 + base->groups.count + info3->sidcount + resource_groups->groups.count, "num_sids");
     146             : 
     147          12 :         sid_idx = 0;
     148          12 :         wbcSidToStringBuf(&info->sids[sid_idx].sid, sid_str, sizeof(sid_str));
     149          12 :         torture_assert(tctx,
     150             :                        dom_sid_equal(dom_sid_parse_talloc(tctx, sid_str),
     151             :                                      dom_sid_add_rid(tctx, base->domain_sid, base->rid)),
     152             :                        sid_str);
     153             : 
     154          12 :         sid_idx++;
     155          12 :         wbcSidToStringBuf(&info->sids[sid_idx].sid, sid_str, sizeof(sid_str));
     156          12 :         torture_assert(tctx,
     157             :                        dom_sid_equal(dom_sid_parse_talloc(tctx, sid_str),
     158             :                                      dom_sid_add_rid(tctx, base->domain_sid, base->primary_gid)),
     159             :                        sid_str);
     160             : 
     161          24 :         for(i = 0; i < base->groups.count; i++ ) {
     162          12 :                 sid_idx++;
     163          12 :                 wbcSidToStringBuf(&info->sids[sid_idx].sid,
     164             :                                   sid_str, sizeof(sid_str));
     165          12 :                 torture_assert_sid_equal(tctx,
     166             :                                          dom_sid_parse_talloc(tctx, sid_str),
     167             :                                          dom_sid_add_rid(tctx, base->domain_sid,
     168             :                                                          base->groups.rids[i].rid),
     169             :                                          "base SID mismatch");
     170             :         }
     171             : 
     172          42 :         for(i = 0; i < info3->sidcount; i++) {
     173          30 :                 sid_idx++;
     174          30 :                 wbcSidToStringBuf(&info->sids[sid_idx].sid,
     175             :                                   sid_str, sizeof(sid_str));
     176          30 :                 torture_assert_sid_equal(tctx,
     177             :                                          dom_sid_parse_talloc(tctx, sid_str),
     178             :                                          info3->sids[i].sid,
     179             :                                          "extra SID mismatch");
     180             :         }
     181             : 
     182          18 :         for (i = 0; i < resource_groups->groups.count; i++) {
     183           6 :                 sid_idx++;
     184           6 :                 wbcSidToStringBuf(&info->sids[sid_idx].sid,
     185             :                                   sid_str, sizeof(sid_str));
     186           6 :                 torture_assert_sid_equal(tctx,
     187             :                                          dom_sid_parse_talloc(tctx, sid_str),
     188             :                                          dom_sid_add_rid(tctx, resource_groups->domain_sid,
     189             :                                                          resource_groups->groups.rids[i].rid),
     190             :                                          "resource SID mismatch");
     191             :         }
     192             : 
     193          12 :         sid_idx++;
     194          12 :         torture_assert_int_equal(tctx, sid_idx, info->num_sids, "some SIDs still unaccounted for");
     195             : 
     196          12 :         return true;
     197             : }
     198             : 
     199          18 : static bool torture_winbind_pac(struct torture_context *tctx,
     200             :                                 const char *sasl_mech,
     201             :                                 const char *mech)
     202             : {
     203           0 :         NTSTATUS status;
     204             : 
     205           0 :         struct gensec_security *gensec_client_context;
     206           0 :         struct gensec_security *gensec_server_context;
     207           0 :         struct cli_credentials *machine_credentials;
     208             : 
     209           0 :         DATA_BLOB client_to_server, server_to_client;   
     210             : 
     211           0 :         struct auth4_context *auth_context;
     212           0 :         struct auth_session_info *session_info;
     213           0 :         struct pac_data *pac_data;
     214             : 
     215          18 :         TALLOC_CTX *tmp_ctx = talloc_new(tctx);
     216          18 :         torture_assert(tctx, tmp_ctx != NULL, "talloc_new() failed");
     217             : 
     218          18 :         machine_credentials = cli_credentials_init_server(tmp_ctx,
     219             :                                                           tctx->lp_ctx);
     220          18 :         torture_assert(tctx, machine_credentials != NULL, "cli_credentials_init() failed");
     221             : 
     222          18 :         auth_context = talloc_zero(tmp_ctx, struct auth4_context);
     223          18 :         torture_assert(tctx, auth_context != NULL, "talloc_new() failed");
     224             : 
     225          18 :         auth_context->generate_session_info_pac = test_generate_session_info_pac;
     226             : 
     227          18 :         status = gensec_client_start(tctx, &gensec_client_context,
     228             :                                      lpcfg_gensec_settings(tctx, tctx->lp_ctx));
     229          18 :         torture_assert_ntstatus_ok(tctx, status, "gensec_client_start (client) failed");
     230             : 
     231          18 :         status = gensec_set_target_hostname(gensec_client_context, cli_credentials_get_workstation(machine_credentials));
     232          18 :         torture_assert_ntstatus_ok(tctx, status, "gensec_set_target_hostname (client) failed");
     233             : 
     234          18 :         status = gensec_set_credentials(gensec_client_context,
     235             :                         samba_cmdline_get_creds());
     236          18 :         torture_assert_ntstatus_ok(tctx, status, "gensec_set_credentials (client) failed");
     237             : 
     238          18 :         if (sasl_mech) {
     239          12 :                 status = gensec_start_mech_by_sasl_name(gensec_client_context, sasl_mech);
     240          12 :                 torture_assert_ntstatus_ok(tctx, status, "gensec_start_mech_by_sasl_name (client) failed");
     241             :         } else {
     242           6 :                 status = gensec_start_mech_by_name(gensec_client_context, mech);
     243           6 :                 torture_assert_ntstatus_ok(tctx, status, "gensec_start_mech_by_name (client) failed");
     244             :         }
     245             : 
     246             : 
     247          14 :         status = gensec_server_start(tctx,
     248             :                                      lpcfg_gensec_settings(tctx, tctx->lp_ctx),
     249             :                                      auth_context, &gensec_server_context);
     250          14 :         torture_assert_ntstatus_ok(tctx, status, "gensec_server_start (server) failed");
     251             : 
     252          14 :         status = gensec_set_credentials(gensec_server_context, machine_credentials);
     253          14 :         torture_assert_ntstatus_ok(tctx, status, "gensec_set_credentials (server) failed");
     254             : 
     255          14 :         if (sasl_mech) {
     256          10 :                 status = gensec_start_mech_by_sasl_name(gensec_server_context, sasl_mech);
     257          10 :                 torture_assert_ntstatus_ok(tctx, status, "gensec_start_mech_by_sasl_name (server) failed");
     258             :         } else {
     259           4 :                 status = gensec_start_mech_by_name(gensec_server_context, mech);
     260           4 :                 torture_assert_ntstatus_ok(tctx, status, "gensec_start_mech_by_name (server) failed");
     261             :         }
     262             : 
     263          14 :         server_to_client = data_blob(NULL, 0);
     264             :         
     265           0 :         do {
     266             :                 /* Do a client-server update dance */
     267          22 :                 status = gensec_update(gensec_client_context, tmp_ctx, server_to_client, &client_to_server);
     268          22 :                 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {;
     269           4 :                         torture_assert_ntstatus_ok(tctx, status, "gensec_update (client) failed");
     270             :                 }
     271             : 
     272          22 :                 status = gensec_update(gensec_server_context, tmp_ctx, client_to_server, &server_to_client);
     273          22 :                 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {;
     274          14 :                         torture_assert_ntstatus_ok(tctx, status, "gensec_update (server) failed");
     275             :                 }
     276             : 
     277          20 :                 if (NT_STATUS_IS_OK(status)) {
     278          12 :                         break;
     279             :                 }
     280             :         } while (1);
     281             : 
     282             :         /* Extract the PAC using Samba's code */
     283             : 
     284          12 :         status = gensec_session_info(gensec_server_context, gensec_server_context, &session_info);
     285          12 :         torture_assert_ntstatus_ok(tctx, status, "gensec_session_info failed");
     286             : 
     287          12 :         pac_data = talloc_get_type(auth_context->private_data, struct pac_data);
     288             : 
     289          12 :         torture_assert(tctx, pac_data != NULL, "gensec_update failed to fill in pac_data in auth_context");
     290          12 :         torture_assert(tctx, pac_data->pac_blob.data != NULL, "pac_blob not present");
     291          12 :         torture_decode_compare_pac(tctx, pac_data->pac_blob);
     292             : 
     293          12 :         return true;
     294             : }
     295             : 
     296           6 : static bool torture_winbind_pac_gssapi(struct torture_context *tctx)
     297             : {
     298           6 :         return torture_winbind_pac(tctx, "GSSAPI", NULL);
     299             : }       
     300             : 
     301           6 : static bool torture_winbind_pac_gss_spnego(struct torture_context *tctx)
     302             : {
     303           6 :         return torture_winbind_pac(tctx, "GSS-SPNEGO", NULL);
     304             : }       
     305             : 
     306           6 : static bool torture_winbind_pac_krb5(struct torture_context *tctx)
     307             : {
     308           6 :         return torture_winbind_pac(tctx, NULL, "krb5");
     309             : }       
     310             : 
     311        2338 : NTSTATUS torture_winbind_init(TALLOC_CTX *ctx)
     312             : {
     313        2338 :         struct torture_suite *suite = torture_suite_create(ctx, "winbind");
     314         125 :         struct torture_suite *pac_suite;
     315        2338 :         torture_suite_add_suite(suite, torture_winbind_struct_init(suite));
     316        2338 :         torture_suite_add_suite(suite, torture_wbclient(suite));
     317             : 
     318        2338 :         pac_suite = torture_suite_create(ctx, "pac");
     319        2338 :         torture_suite_add_simple_test(pac_suite,
     320             :                                       "GSSAPI", torture_winbind_pac_gssapi);
     321        2338 :         torture_suite_add_simple_test(pac_suite,
     322             :                                       "GSS-SPNEGO", torture_winbind_pac_gss_spnego);
     323        2338 :         torture_suite_add_simple_test(pac_suite,
     324             :                                       "krb5", torture_winbind_pac_krb5);
     325             : 
     326        2338 :         pac_suite->description = talloc_strdup(suite, "Winbind Kerberos PAC tests");
     327             : 
     328        2338 :         torture_suite_add_suite(suite, pac_suite);
     329             : 
     330        2338 :         suite->description = talloc_strdup(suite, "WINBIND tests");
     331             : 
     332        2338 :         torture_register_suite(ctx, suite);
     333             : 
     334        2338 :         return NT_STATUS_OK;
     335             : }

Generated by: LCOV version 1.14