LCOV - code coverage report
Current view: top level - source3/libads - authdata.c (source / functions) Hit Total Coverage
Test: coverage report for v4-17-test 1498b464 Lines: 0 113 0.0 %
Date: 2024-06-13 04:01:37 Functions: 0 1 0.0 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             :    kerberos authorization data (PAC) utility library
       4             :    Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2003
       5             :    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005
       6             :    Copyright (C) Andrew Tridgell 2001
       7             :    Copyright (C) Luke Howard 2002-2003
       8             :    Copyright (C) Stefan Metzmacher 2004-2005
       9             :    Copyright (C) Guenther Deschner 2005,2007,2008
      10             : 
      11             :    This program is free software; you can redistribute it and/or modify
      12             :    it under the terms of the GNU General Public License as published by
      13             :    the Free Software Foundation; either version 3 of the License, or
      14             :    (at your option) any later version.
      15             : 
      16             :    This program is distributed in the hope that it will be useful,
      17             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      18             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      19             :    GNU General Public License for more details.
      20             : 
      21             :    You should have received a copy of the GNU General Public License
      22             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      23             : */
      24             : 
      25             : #include "includes.h"
      26             : #include "librpc/gen_ndr/ndr_krb5pac.h"
      27             : #include "smb_krb5.h"
      28             : #include "libads/kerberos_proto.h"
      29             : #include "auth/common_auth.h"
      30             : #include "lib/param/param.h"
      31             : #include "librpc/crypto/gse.h"
      32             : #include "auth/gensec/gensec.h"
      33             : #include "auth/gensec/gensec_internal.h" /* TODO: remove this */
      34             : #include "../libcli/auth/spnego.h"
      35             : 
      36             : #ifdef HAVE_KRB5
      37             : 
      38             : #include "auth/kerberos/pac_utils.h"
      39             : 
      40             : struct smb_krb5_context;
      41             : 
      42             : /*
      43             :  * Given the username/password, do a kinit, store the ticket in
      44             :  * cache_name if specified, and return the PAC_LOGON_INFO (the
      45             :  * structure containing the important user information such as
      46             :  * groups).
      47             :  */
      48           0 : NTSTATUS kerberos_return_pac(TALLOC_CTX *mem_ctx,
      49             :                              const char *name,
      50             :                              const char *pass,
      51             :                              time_t time_offset,
      52             :                              time_t *expire_time,
      53             :                              time_t *renew_till_time,
      54             :                              const char *cache_name,
      55             :                              bool request_pac,
      56             :                              bool add_netbios_addr,
      57             :                              time_t renewable_time,
      58             :                              const char *impersonate_princ_s,
      59             :                              const char *local_service,
      60             :                              char **_canon_principal,
      61             :                              char **_canon_realm,
      62             :                              struct PAC_DATA_CTR **_pac_data_ctr)
      63             : {
      64             :         krb5_error_code ret;
      65           0 :         NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
      66           0 :         DATA_BLOB tkt = data_blob_null;
      67           0 :         DATA_BLOB tkt_wrapped = data_blob_null;
      68           0 :         DATA_BLOB ap_rep = data_blob_null;
      69           0 :         DATA_BLOB sesskey1 = data_blob_null;
      70           0 :         const char *auth_princ = NULL;
      71           0 :         const char *cc = "MEMORY:kerberos_return_pac";
      72             :         struct auth_session_info *session_info;
      73             :         struct gensec_security *gensec_server_context;
      74             :         const struct gensec_security_ops **backends;
      75             :         struct gensec_settings *gensec_settings;
      76           0 :         size_t idx = 0;
      77             :         struct auth4_context *auth_context;
      78             :         struct loadparm_context *lp_ctx;
      79           0 :         struct PAC_DATA_CTR *pac_data_ctr = NULL;
      80           0 :         char *canon_principal = NULL;
      81           0 :         char *canon_realm = NULL;
      82             : 
      83           0 :         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
      84           0 :         NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
      85             : 
      86           0 :         ZERO_STRUCT(tkt);
      87           0 :         ZERO_STRUCT(ap_rep);
      88           0 :         ZERO_STRUCT(sesskey1);
      89             : 
      90           0 :         if (!name || !pass) {
      91           0 :                 status = NT_STATUS_INVALID_PARAMETER;
      92           0 :                 goto out;
      93             :         }
      94             : 
      95           0 :         if (_canon_principal != NULL) {
      96           0 :                 *_canon_principal = NULL;
      97             :         }
      98             : 
      99           0 :         if (_canon_realm != NULL) {
     100           0 :                 *_canon_realm = NULL;
     101             :         }
     102             : 
     103           0 :         if (cache_name) {
     104           0 :                 cc = cache_name;
     105             :         }
     106             : 
     107           0 :         if (!strchr_m(name, '@')) {
     108           0 :                 auth_princ = talloc_asprintf(mem_ctx, "%s@%s", name,
     109             :                         lp_realm());
     110             :         } else {
     111           0 :                 auth_princ = name;
     112             :         }
     113           0 :         NT_STATUS_HAVE_NO_MEMORY(auth_princ);
     114             : 
     115           0 :         ret = kerberos_kinit_password_ext(auth_princ,
     116             :                                           pass,
     117             :                                           time_offset,
     118             :                                           expire_time,
     119             :                                           renew_till_time,
     120             :                                           cc,
     121             :                                           request_pac,
     122             :                                           add_netbios_addr,
     123             :                                           renewable_time,
     124             :                                           tmp_ctx,
     125             :                                           &canon_principal,
     126             :                                           &canon_realm,
     127             :                                           &status);
     128           0 :         if (ret) {
     129           0 :                 DEBUG(1,("kinit failed for '%s' with: %s (%d)\n",
     130             :                         auth_princ, error_message(ret), ret));
     131             :                 /* status already set */
     132           0 :                 goto out;
     133             :         }
     134             : 
     135           0 :         DEBUG(10,("got TGT for %s in %s\n", auth_princ, cc));
     136           0 :         if (expire_time) {
     137           0 :                 DEBUGADD(10,("\tvalid until: %s (%d)\n",
     138             :                         http_timestring(talloc_tos(), *expire_time),
     139             :                         (int)*expire_time));
     140             :         }
     141           0 :         if (renew_till_time) {
     142           0 :                 DEBUGADD(10,("\trenewable till: %s (%d)\n",
     143             :                         http_timestring(talloc_tos(), *renew_till_time),
     144             :                         (int)*renew_till_time));
     145             :         }
     146             : 
     147             :         /* we cannot continue with krb5 when UF_DONT_REQUIRE_PREAUTH is set,
     148             :          * in that case fallback to NTLM - gd */
     149             : 
     150           0 :         if (expire_time && renew_till_time &&
     151           0 :             (*expire_time == 0) && (*renew_till_time == 0)) {
     152           0 :                 status = NT_STATUS_INVALID_LOGON_TYPE;
     153           0 :                 goto out;
     154             :         }
     155             : 
     156           0 :         ret = ads_krb5_cli_get_ticket(mem_ctx,
     157             :                                       local_service,
     158             :                                       time_offset,
     159             :                                       &tkt,
     160             :                                       &sesskey1,
     161             :                                       0,
     162             :                                       cc,
     163             :                                       NULL,
     164             :                                       impersonate_princ_s);
     165           0 :         if (ret) {
     166           0 :                 DEBUG(1,("failed to get ticket for %s: %s\n",
     167             :                         local_service, error_message(ret)));
     168           0 :                 if (impersonate_princ_s) {
     169           0 :                         DEBUGADD(1,("tried S4U2SELF impersonation as: %s\n",
     170             :                                 impersonate_princ_s));
     171             :                 }
     172           0 :                 status = krb5_to_nt_status(ret);
     173           0 :                 goto out;
     174             :         }
     175             : 
     176             :         /* wrap that up in a nice GSS-API wrapping */
     177           0 :         tkt_wrapped = spnego_gen_krb5_wrap(tmp_ctx, tkt, TOK_ID_KRB_AP_REQ);
     178           0 :         if (tkt_wrapped.data == NULL) {
     179           0 :                 status = NT_STATUS_NO_MEMORY;
     180           0 :                 goto out;
     181             :         }
     182             : 
     183           0 :         auth_context = auth4_context_for_PAC_DATA_CTR(tmp_ctx);
     184           0 :         if (auth_context == NULL) {
     185           0 :                 status = NT_STATUS_NO_MEMORY;
     186           0 :                 goto out;
     187             :         }
     188             : 
     189           0 :         lp_ctx = loadparm_init_s3(tmp_ctx, loadparm_s3_helpers());
     190           0 :         if (lp_ctx == NULL) {
     191           0 :                 status = NT_STATUS_INVALID_SERVER_STATE;
     192           0 :                 DEBUG(10, ("loadparm_init_s3 failed\n"));
     193           0 :                 goto out;
     194             :         }
     195             : 
     196           0 :         gensec_settings = lpcfg_gensec_settings(tmp_ctx, lp_ctx);
     197           0 :         if (gensec_settings == NULL) {
     198           0 :                 status = NT_STATUS_NO_MEMORY;
     199           0 :                 DEBUG(10, ("lpcfg_gensec_settings failed\n"));
     200           0 :                 goto out;
     201             :         }
     202             : 
     203           0 :         backends = talloc_zero_array(gensec_settings,
     204             :                                      const struct gensec_security_ops *, 2);
     205           0 :         if (backends == NULL) {
     206           0 :                 status = NT_STATUS_NO_MEMORY;
     207           0 :                 goto out;
     208             :         }
     209           0 :         gensec_settings->backends = backends;
     210             : 
     211           0 :         gensec_init();
     212             : 
     213           0 :         backends[idx++] = &gensec_gse_krb5_security_ops;
     214             : 
     215           0 :         status = gensec_server_start(tmp_ctx, gensec_settings,
     216             :                                         auth_context, &gensec_server_context);
     217             : 
     218           0 :         if (!NT_STATUS_IS_OK(status)) {
     219           0 :                 DEBUG(1, (__location__ "Failed to start server-side GENSEC to validate a Kerberos ticket: %s\n", nt_errstr(status)));
     220           0 :                 goto out;
     221             :         }
     222             : 
     223           0 :         talloc_unlink(tmp_ctx, lp_ctx);
     224           0 :         talloc_unlink(tmp_ctx, gensec_settings);
     225           0 :         talloc_unlink(tmp_ctx, auth_context);
     226             : 
     227             :         /* Session info is not complete, do not pass to auth log */
     228           0 :         gensec_want_feature(gensec_server_context, GENSEC_FEATURE_NO_AUTHZ_LOG);
     229             : 
     230           0 :         status = gensec_start_mech_by_oid(gensec_server_context, GENSEC_OID_KERBEROS5);
     231           0 :         if (!NT_STATUS_IS_OK(status)) {
     232           0 :                 DEBUG(1, (__location__ "Failed to start server-side GENSEC krb5 to validate a Kerberos ticket: %s\n", nt_errstr(status)));
     233           0 :                 goto out;
     234             :         }
     235             : 
     236             :         /* Do a client-server update dance */
     237           0 :         status = gensec_update(gensec_server_context, tmp_ctx, tkt_wrapped, &ap_rep);
     238           0 :         if (!NT_STATUS_IS_OK(status)) {
     239           0 :                 DEBUG(1, ("gensec_update() failed: %s\n", nt_errstr(status)));
     240           0 :                 goto out;
     241             :         }
     242             : 
     243             :         /* Now return the PAC information to the callers.  We ingore
     244             :          * the session_info and instead pick out the PAC via the
     245             :          * private_data on the auth_context */
     246           0 :         status = gensec_session_info(gensec_server_context, tmp_ctx, &session_info);
     247           0 :         if (!NT_STATUS_IS_OK(status)) {
     248           0 :                 DEBUG(1, ("Unable to obtain PAC via gensec_session_info\n"));
     249           0 :                 goto out;
     250             :         }
     251             : 
     252           0 :         pac_data_ctr = auth4_context_get_PAC_DATA_CTR(auth_context, mem_ctx);
     253           0 :         if (pac_data_ctr == NULL) {
     254           0 :                 DEBUG(1,("no PAC\n"));
     255           0 :                 status = NT_STATUS_INVALID_PARAMETER;
     256           0 :                 goto out;
     257             :         }
     258             : 
     259           0 :         *_pac_data_ctr = talloc_move(mem_ctx, &pac_data_ctr);
     260           0 :         if (_canon_principal != NULL) {
     261           0 :                 *_canon_principal = talloc_move(mem_ctx, &canon_principal);
     262             :         }
     263           0 :         if (_canon_realm != NULL) {
     264           0 :                 *_canon_realm = talloc_move(mem_ctx, &canon_realm);
     265             :         }
     266             : 
     267           0 : out:
     268           0 :         talloc_free(tmp_ctx);
     269           0 :         if (cc != cache_name) {
     270           0 :                 ads_kdestroy(cc);
     271             :         }
     272             : 
     273           0 :         data_blob_free(&tkt);
     274           0 :         data_blob_free(&ap_rep);
     275           0 :         data_blob_free(&sesskey1);
     276             : 
     277           0 :         return status;
     278             : }
     279             : 
     280             : #endif

Generated by: LCOV version 1.13