LCOV - code coverage report
Current view: top level - source3/libads - authdata.c (source / functions) Hit Total Coverage
Test: coverage report for master 98b443d9 Lines: 0 157 0.0 %
Date: 2024-05-31 13:13:24 Functions: 0 2 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 "../libcli/auth/spnego.h"
      34             : #include "lib/util/asn1.h"
      35             : 
      36             : #ifdef HAVE_KRB5
      37             : 
      38             : #include "auth/kerberos/pac_utils.h"
      39             : 
      40             : struct smb_krb5_context;
      41             : 
      42             : /*
      43             :   generate a krb5 GSS-API wrapper packet given a ticket
      44             : */
      45           0 : static DATA_BLOB spnego_gen_krb5_wrap(
      46             :         TALLOC_CTX *ctx, const DATA_BLOB ticket, const uint8_t tok_id[2])
      47             : {
      48           0 :         ASN1_DATA *data;
      49           0 :         DATA_BLOB ret = data_blob_null;
      50             : 
      51           0 :         data = asn1_init(talloc_tos(), ASN1_MAX_TREE_DEPTH);
      52           0 :         if (data == NULL) {
      53           0 :                 return data_blob_null;
      54             :         }
      55             : 
      56           0 :         if (!asn1_push_tag(data, ASN1_APPLICATION(0))) goto err;
      57           0 :         if (!asn1_write_OID(data, OID_KERBEROS5)) goto err;
      58             : 
      59           0 :         if (!asn1_write(data, tok_id, 2)) goto err;
      60           0 :         if (!asn1_write(data, ticket.data, ticket.length)) goto err;
      61           0 :         if (!asn1_pop_tag(data)) goto err;
      62             : 
      63           0 :         if (!asn1_extract_blob(data, ctx, &ret)) {
      64           0 :                 goto err;
      65             :         }
      66             : 
      67           0 :         asn1_free(data);
      68           0 :         data = NULL;
      69             : 
      70           0 :   err:
      71             : 
      72           0 :         if (data != NULL) {
      73           0 :                 if (asn1_has_error(data)) {
      74           0 :                         DEBUG(1, ("Failed to build krb5 wrapper at offset %d\n",
      75             :                                   (int)asn1_current_ofs(data)));
      76             :                 }
      77             : 
      78           0 :                 asn1_free(data);
      79             :         }
      80             : 
      81           0 :         return ret;
      82             : }
      83             : 
      84             : /*
      85             :  * Given the username/password, do a kinit, store the ticket in
      86             :  * cache_name if specified, and return the PAC_LOGON_INFO (the
      87             :  * structure containing the important user information such as
      88             :  * groups).
      89             :  */
      90           0 : NTSTATUS kerberos_return_pac(TALLOC_CTX *mem_ctx,
      91             :                              const char *name,
      92             :                              const char *pass,
      93             :                              time_t time_offset,
      94             :                              time_t *expire_time,
      95             :                              time_t *renew_till_time,
      96             :                              const char *cache_name,
      97             :                              bool request_pac,
      98             :                              bool add_netbios_addr,
      99             :                              time_t renewable_time,
     100             :                              const char *impersonate_princ_s,
     101             :                              const char *local_service,
     102             :                              char **_canon_principal,
     103             :                              char **_canon_realm,
     104             :                              struct PAC_DATA_CTR **_pac_data_ctr)
     105             : {
     106           0 :         krb5_error_code ret;
     107           0 :         NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
     108           0 :         DATA_BLOB tkt = data_blob_null;
     109           0 :         DATA_BLOB tkt_wrapped = data_blob_null;
     110           0 :         DATA_BLOB ap_rep = data_blob_null;
     111           0 :         DATA_BLOB sesskey1 = data_blob_null;
     112           0 :         const char *auth_princ = NULL;
     113           0 :         const char *cc = NULL;
     114           0 :         struct auth_session_info *session_info;
     115           0 :         struct gensec_security *gensec_server_context;
     116           0 :         const struct gensec_security_ops **backends;
     117           0 :         struct gensec_settings *gensec_settings;
     118           0 :         size_t idx = 0;
     119           0 :         struct auth4_context *auth_context;
     120           0 :         struct loadparm_context *lp_ctx;
     121           0 :         struct PAC_DATA_CTR *pac_data_ctr = NULL;
     122           0 :         char *canon_principal = NULL;
     123           0 :         char *canon_realm = NULL;
     124           0 :         krb5_context ctx = NULL;
     125           0 :         krb5_ccache ccid = NULL;
     126             : 
     127           0 :         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
     128           0 :         NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
     129             : 
     130           0 :         ZERO_STRUCT(tkt);
     131           0 :         ZERO_STRUCT(ap_rep);
     132           0 :         ZERO_STRUCT(sesskey1);
     133             : 
     134           0 :         if (!name || !pass) {
     135           0 :                 status = NT_STATUS_INVALID_PARAMETER;
     136           0 :                 goto out;
     137             :         }
     138             : 
     139           0 :         if (_canon_principal != NULL) {
     140           0 :                 *_canon_principal = NULL;
     141             :         }
     142             : 
     143           0 :         if (_canon_realm != NULL) {
     144           0 :                 *_canon_realm = NULL;
     145             :         }
     146             : 
     147           0 :         if (cache_name) {
     148           0 :                 cc = cache_name;
     149             :         } else {
     150           0 :                 char *ccname = NULL;
     151             : 
     152           0 :                 ret = smb_krb5_init_context_common(&ctx);
     153           0 :                 if (ret != 0) {
     154           0 :                         status = krb5_to_nt_status(ret);
     155           0 :                         goto out;
     156             :                 }
     157             : 
     158           0 :                 ret = smb_krb5_cc_new_unique_memory(ctx,
     159             :                                                     tmp_ctx,
     160             :                                                     &ccname,
     161             :                                                     &ccid);
     162           0 :                 if (ret != 0) {
     163           0 :                         status = krb5_to_nt_status(ret);
     164           0 :                         goto out;
     165             :                 }
     166           0 :                 cc = ccname;
     167             :         }
     168             : 
     169           0 :         if (!strchr_m(name, '@')) {
     170           0 :                 auth_princ = talloc_asprintf(mem_ctx, "%s@%s", name,
     171             :                         lp_realm());
     172             :         } else {
     173           0 :                 auth_princ = name;
     174             :         }
     175           0 :         NT_STATUS_HAVE_NO_MEMORY(auth_princ);
     176             : 
     177           0 :         ret = kerberos_kinit_password_ext(auth_princ,
     178             :                                           pass,
     179             :                                           time_offset,
     180             :                                           expire_time,
     181             :                                           renew_till_time,
     182             :                                           cc,
     183             :                                           request_pac,
     184             :                                           add_netbios_addr,
     185             :                                           renewable_time,
     186             :                                           tmp_ctx,
     187             :                                           &canon_principal,
     188             :                                           &canon_realm,
     189             :                                           &status);
     190           0 :         if (ret) {
     191           0 :                 DEBUG(1,("kinit failed for '%s' with: %s (%d)\n",
     192             :                         auth_princ, error_message(ret), ret));
     193             :                 /* status already set */
     194           0 :                 goto out;
     195             :         }
     196             : 
     197           0 :         DEBUG(10,("got TGT for %s in %s\n", auth_princ, cc));
     198           0 :         if (expire_time) {
     199           0 :                 DEBUGADD(10,("\tvalid until: %s (%d)\n",
     200             :                         http_timestring(talloc_tos(), *expire_time),
     201             :                         (int)*expire_time));
     202             :         }
     203           0 :         if (renew_till_time) {
     204           0 :                 DEBUGADD(10,("\trenewable till: %s (%d)\n",
     205             :                         http_timestring(talloc_tos(), *renew_till_time),
     206             :                         (int)*renew_till_time));
     207             :         }
     208             : 
     209             :         /* we cannot continue with krb5 when UF_DONT_REQUIRE_PREAUTH is set,
     210             :          * in that case fallback to NTLM - gd */
     211             : 
     212           0 :         if (expire_time && renew_till_time &&
     213           0 :             (*expire_time == 0) && (*renew_till_time == 0)) {
     214           0 :                 status = NT_STATUS_INVALID_LOGON_TYPE;
     215           0 :                 goto out;
     216             :         }
     217             : 
     218           0 :         ret = ads_krb5_cli_get_ticket(mem_ctx,
     219             :                                       local_service,
     220             :                                       time_offset,
     221             :                                       &tkt,
     222             :                                       &sesskey1,
     223             :                                       0,
     224             :                                       cc,
     225             :                                       NULL,
     226             :                                       impersonate_princ_s);
     227           0 :         if (ret) {
     228           0 :                 DEBUG(1,("failed to get ticket for %s: %s\n",
     229             :                         local_service, error_message(ret)));
     230           0 :                 if (impersonate_princ_s) {
     231           0 :                         DEBUGADD(1,("tried S4U2SELF impersonation as: %s\n",
     232             :                                 impersonate_princ_s));
     233             :                 }
     234           0 :                 status = krb5_to_nt_status(ret);
     235           0 :                 goto out;
     236             :         }
     237             : 
     238             :         /* wrap that up in a nice GSS-API wrapping */
     239           0 :         tkt_wrapped = spnego_gen_krb5_wrap(tmp_ctx, tkt, TOK_ID_KRB_AP_REQ);
     240           0 :         if (tkt_wrapped.data == NULL) {
     241           0 :                 status = NT_STATUS_NO_MEMORY;
     242           0 :                 goto out;
     243             :         }
     244             : 
     245           0 :         auth_context = auth4_context_for_PAC_DATA_CTR(tmp_ctx);
     246           0 :         if (auth_context == NULL) {
     247           0 :                 status = NT_STATUS_NO_MEMORY;
     248           0 :                 goto out;
     249             :         }
     250             : 
     251           0 :         lp_ctx = loadparm_init_s3(tmp_ctx, loadparm_s3_helpers());
     252           0 :         if (lp_ctx == NULL) {
     253           0 :                 status = NT_STATUS_INVALID_SERVER_STATE;
     254           0 :                 DEBUG(10, ("loadparm_init_s3 failed\n"));
     255           0 :                 goto out;
     256             :         }
     257             : 
     258           0 :         gensec_settings = lpcfg_gensec_settings(tmp_ctx, lp_ctx);
     259           0 :         if (gensec_settings == NULL) {
     260           0 :                 status = NT_STATUS_NO_MEMORY;
     261           0 :                 DEBUG(10, ("lpcfg_gensec_settings failed\n"));
     262           0 :                 goto out;
     263             :         }
     264             : 
     265           0 :         backends = talloc_zero_array(gensec_settings,
     266             :                                      const struct gensec_security_ops *, 2);
     267           0 :         if (backends == NULL) {
     268           0 :                 status = NT_STATUS_NO_MEMORY;
     269           0 :                 goto out;
     270             :         }
     271           0 :         gensec_settings->backends = backends;
     272             : 
     273           0 :         gensec_init();
     274             : 
     275           0 :         backends[idx++] = gensec_gse_security_by_oid(GENSEC_OID_KERBEROS5);
     276             : 
     277           0 :         status = gensec_server_start(tmp_ctx, gensec_settings,
     278             :                                         auth_context, &gensec_server_context);
     279             : 
     280           0 :         if (!NT_STATUS_IS_OK(status)) {
     281           0 :                 DEBUG(1, (__location__ "Failed to start server-side GENSEC to validate a Kerberos ticket: %s\n", nt_errstr(status)));
     282           0 :                 goto out;
     283             :         }
     284             : 
     285           0 :         talloc_unlink(tmp_ctx, lp_ctx);
     286           0 :         talloc_unlink(tmp_ctx, gensec_settings);
     287           0 :         talloc_unlink(tmp_ctx, auth_context);
     288             : 
     289             :         /* Session info is not complete, do not pass to auth log */
     290           0 :         gensec_want_feature(gensec_server_context, GENSEC_FEATURE_NO_AUTHZ_LOG);
     291             : 
     292           0 :         status = gensec_start_mech_by_oid(gensec_server_context, GENSEC_OID_KERBEROS5);
     293           0 :         if (!NT_STATUS_IS_OK(status)) {
     294           0 :                 DEBUG(1, (__location__ "Failed to start server-side GENSEC krb5 to validate a Kerberos ticket: %s\n", nt_errstr(status)));
     295           0 :                 goto out;
     296             :         }
     297             : 
     298             :         /* Do a client-server update dance */
     299           0 :         status = gensec_update(gensec_server_context, tmp_ctx, tkt_wrapped, &ap_rep);
     300           0 :         if (!NT_STATUS_IS_OK(status)) {
     301           0 :                 DEBUG(1, ("gensec_update() failed: %s\n", nt_errstr(status)));
     302           0 :                 goto out;
     303             :         }
     304             : 
     305             :         /* Now return the PAC information to the callers.  We ignore
     306             :          * the session_info and instead pick out the PAC via the
     307             :          * private_data on the auth_context */
     308           0 :         status = gensec_session_info(gensec_server_context, tmp_ctx, &session_info);
     309           0 :         if (!NT_STATUS_IS_OK(status)) {
     310           0 :                 DEBUG(1, ("Unable to obtain PAC via gensec_session_info\n"));
     311           0 :                 goto out;
     312             :         }
     313             : 
     314           0 :         pac_data_ctr = auth4_context_get_PAC_DATA_CTR(auth_context, mem_ctx);
     315           0 :         if (pac_data_ctr == NULL) {
     316           0 :                 DEBUG(1,("no PAC\n"));
     317           0 :                 status = NT_STATUS_INVALID_PARAMETER;
     318           0 :                 goto out;
     319             :         }
     320             : 
     321           0 :         *_pac_data_ctr = talloc_move(mem_ctx, &pac_data_ctr);
     322           0 :         if (_canon_principal != NULL) {
     323           0 :                 *_canon_principal = talloc_move(mem_ctx, &canon_principal);
     324             :         }
     325           0 :         if (_canon_realm != NULL) {
     326           0 :                 *_canon_realm = talloc_move(mem_ctx, &canon_realm);
     327             :         }
     328             : 
     329           0 : out:
     330           0 :         if (ccid != NULL) {
     331           0 :                 krb5_cc_destroy(ctx, ccid);
     332           0 :                 ccid = NULL;
     333             :         }
     334           0 :         if (ctx != NULL) {
     335           0 :                 krb5_free_context(ctx);
     336           0 :                 ctx = NULL;
     337             :         }
     338           0 :         talloc_free(tmp_ctx);
     339             : 
     340           0 :         data_blob_free(&tkt);
     341           0 :         data_blob_free(&ap_rep);
     342           0 :         data_blob_free(&sesskey1);
     343             : 
     344           0 :         return status;
     345             : }
     346             : 
     347             : #endif

Generated by: LCOV version 1.14