From 410ca7311ada99112120a46f70e71f99c4b5e0e0 Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Mon, 12 Mar 2012 18:21:10 +1100 Subject: s3-libads: Rework kerberos_return_pac() to use GENSEC for the server-side This removes the last user of ads_verify_ticket(), and means that we only have one code path to verify an incoming krb5 (GSSAPI) ticket. Andrew Bartlett Signed-off-by: Stefan Metzmacher --- source3/libads/authdata.c | 189 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 167 insertions(+), 22 deletions(-) (limited to 'source3/libads') diff --git a/source3/libads/authdata.c b/source3/libads/authdata.c index 44279a24d7..5a8ca28b14 100644 --- a/source3/libads/authdata.c +++ b/source3/libads/authdata.c @@ -26,15 +26,90 @@ #include "librpc/gen_ndr/ndr_krb5pac.h" #include "smb_krb5.h" #include "libads/kerberos_proto.h" +#include "auth/common_auth.h" +#include "lib/param/param.h" +#include "librpc/crypto/gse.h" +#include "auth/gensec/gensec.h" +#include "../libcli/auth/spnego.h" #ifdef HAVE_KRB5 +struct smb_krb5_context; + /**************************************************************** -Given a username, password and other details, return the -PAC_LOGON_INFO (the structure containing the important user -information such as groups). +Callback to get the PAC_LOGON_INFO from the blob ****************************************************************/ +static NTSTATUS kerberos_fetch_pac(struct auth4_context *auth_ctx, + TALLOC_CTX *mem_ctx, + struct smb_krb5_context *smb_krb5_context, + DATA_BLOB *pac_blob, + const char *princ_name, + const struct tsocket_address *remote_address, + uint32_t session_info_flags, + struct auth_session_info **session_info) +{ + TALLOC_CTX *tmp_ctx; + struct PAC_DATA *pac_data = NULL; + struct PAC_LOGON_INFO *logon_info = NULL; + unsigned int i; + NTSTATUS status = NT_STATUS_INTERNAL_ERROR; + + tmp_ctx = talloc_new(mem_ctx); + if (!tmp_ctx) { + return NT_STATUS_NO_MEMORY; + } + + if (pac_blob) { + status = kerberos_decode_pac(tmp_ctx, + *pac_blob, + NULL, NULL, NULL, NULL, 0, &pac_data); + if (!NT_STATUS_IS_OK(status)) { + goto done; + } + + /* get logon name and logon info */ + for (i = 0; i < pac_data->num_buffers; i++) { + struct PAC_BUFFER *data_buf = &pac_data->buffers[i]; + + switch (data_buf->type) { + case PAC_TYPE_LOGON_INFO: + if (!data_buf->info) { + break; + } + logon_info = data_buf->info->logon_info.info; + break; + default: + break; + } + } + if (!logon_info) { + DEBUG(1, ("Invalid PAC data, missing logon info!\n")); + status = NT_STATUS_NOT_FOUND; + goto done; + } + } + talloc_set_name_const(logon_info, "struct PAC_LOGON_INFO"); + + auth_ctx->private_data = talloc_steal(auth_ctx, logon_info); + *session_info = talloc_zero(mem_ctx, struct auth_session_info); + if (!*session_info) { + status = NT_STATUS_NO_MEMORY; + goto done; + } + status = NT_STATUS_OK; + +done: + TALLOC_FREE(tmp_ctx); + return status; +} + +/* + * Given the username/password, do a kinit, store the ticket in + * cache_name if specified, and return the PAC_LOGON_INFO (the + * structure containing the important user information such as + * groups). + */ NTSTATUS kerberos_return_pac(TALLOC_CTX *mem_ctx, const char *name, const char *pass, @@ -46,20 +121,29 @@ NTSTATUS kerberos_return_pac(TALLOC_CTX *mem_ctx, bool add_netbios_addr, time_t renewable_time, const char *impersonate_princ_s, - struct PAC_LOGON_INFO **logon_info) + struct PAC_LOGON_INFO **_logon_info) { krb5_error_code ret; NTSTATUS status = NT_STATUS_INVALID_PARAMETER; - DATA_BLOB tkt, ap_rep, sesskey1, sesskey2; - char *client_princ_out = NULL; + DATA_BLOB tkt, tkt_wrapped, ap_rep, sesskey1; const char *auth_princ = NULL; const char *local_service = NULL; const char *cc = "MEMORY:kerberos_return_pac"; + struct auth_session_info *session_info; + struct gensec_security *gensec_server_context; + + struct gensec_settings *gensec_settings; + size_t idx = 0; + struct auth4_context *auth_context; + struct loadparm_context *lp_ctx; + struct PAC_LOGON_INFO *logon_info = NULL; + + TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx); + NT_STATUS_HAVE_NO_MEMORY(tmp_ctx); ZERO_STRUCT(tkt); ZERO_STRUCT(ap_rep); ZERO_STRUCT(sesskey1); - ZERO_STRUCT(sesskey2); if (!name || !pass) { return NT_STATUS_INVALID_PARAMETER; @@ -137,28 +221,92 @@ NTSTATUS kerberos_return_pac(TALLOC_CTX *mem_ctx, status = krb5_to_nt_status(ret); goto out; } - status = ads_verify_ticket(mem_ctx, - lp_realm(), - time_offset, - &tkt, - &client_princ_out, - logon_info, - &ap_rep, - &sesskey2, - False); + + /* wrap that up in a nice GSS-API wrapping */ + tkt_wrapped = spnego_gen_krb5_wrap(tmp_ctx, tkt, TOK_ID_KRB_AP_REQ); + if (tkt_wrapped.data == NULL) { + status = NT_STATUS_NO_MEMORY; + goto out; + } + + auth_context = talloc_zero(tmp_ctx, struct auth4_context); + if (auth_context == NULL) { + status = NT_STATUS_NO_MEMORY; + goto out; + } + auth_context->generate_session_info_pac = kerberos_fetch_pac; + + lp_ctx = loadparm_init_s3(tmp_ctx, loadparm_s3_context()); + if (lp_ctx == NULL) { + status = NT_STATUS_INVALID_SERVER_STATE; + DEBUG(10, ("loadparm_init_s3 failed\n")); + goto out; + } + + gensec_settings = lpcfg_gensec_settings(tmp_ctx, lp_ctx); + if (lp_ctx == NULL) { + status = NT_STATUS_NO_MEMORY; + DEBUG(10, ("lpcfg_gensec_settings failed\n")); + goto out; + } + + gensec_settings->backends = talloc_zero_array(gensec_settings, + struct gensec_security_ops *, 2); + if (gensec_settings->backends == NULL) { + status = NT_STATUS_NO_MEMORY; + goto out; + } + + gensec_init(); + + gensec_settings->backends[idx++] = &gensec_gse_krb5_security_ops; + + status = gensec_server_start(tmp_ctx, gensec_settings, + auth_context, &gensec_server_context); + if (!NT_STATUS_IS_OK(status)) { - DEBUG(1,("ads_verify_ticket failed: %s\n", - nt_errstr(status))); + DEBUG(1, (__location__ "Failed to start server-side GENSEC to validate a Kerberos ticket: %s\n", nt_errstr(status))); goto out; } - if (!*logon_info) { + talloc_unlink(tmp_ctx, lp_ctx); + talloc_unlink(tmp_ctx, gensec_settings); + talloc_unlink(tmp_ctx, auth_context); + + status = gensec_start_mech_by_oid(gensec_server_context, GENSEC_OID_KERBEROS5); + if (!NT_STATUS_IS_OK(status)) { + DEBUG(1, (__location__ "Failed to start server-side GENSEC krb5 to validate a Kerberos ticket: %s\n", nt_errstr(status))); + goto out; + } + + /* Do a client-server update dance */ + status = gensec_update(gensec_server_context, tmp_ctx, NULL, tkt_wrapped, &ap_rep); + if (!NT_STATUS_IS_OK(status)) { + DEBUG(1, ("gensec_update() failed: %s\n", nt_errstr(status))); + goto out; + } + + /* Now return the PAC information to the callers. We ingore + * the session_info and instead pick out the PAC via the + * private_data on the auth_context */ + status = gensec_session_info(gensec_server_context, tmp_ctx, &session_info); + if (!NT_STATUS_IS_OK(status)) { + DEBUG(1, ("Unable to obtain PAC via gensec_session_info\n")); + goto out; + } + + logon_info = talloc_get_type_abort(gensec_server_context->auth_context->private_data, + struct PAC_LOGON_INFO); + if (logon_info == NULL) { DEBUG(1,("no PAC\n")); status = NT_STATUS_INVALID_PARAMETER; goto out; } + *_logon_info = talloc_move(mem_ctx, &logon_info); + out: + talloc_free(tmp_ctx); if (cc != cache_name) { ads_kdestroy(cc); } @@ -166,9 +314,6 @@ out: data_blob_free(&tkt); data_blob_free(&ap_rep); data_blob_free(&sesskey1); - data_blob_free(&sesskey2); - - TALLOC_FREE(client_princ_out); return status; } -- cgit