libads: Give krb5_errs.c its own header
[nivanova/samba-autobuild/.git] / source3 / libads / authdata.c
index 3d25aec20546954f50432ab76d183f89c0b958cc..86a1be71bf986d3ba33566120a40143a02ebf7b2 100644 (file)
-/* 
+/*
    Unix SMB/CIFS implementation.
    kerberos authorization data (PAC) utility library
-   Copyright (C) Jim McDonough 2003   
-   
+   Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2003
+   Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005
+   Copyright (C) Andrew Tridgell 2001
+   Copyright (C) Luke Howard 2002-2003
+   Copyright (C) Stefan Metzmacher 2004-2005
+   Copyright (C) Guenther Deschner 2005,2007,2008
+
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
-   
+
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
-   
+
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
+#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 "auth/gensec/gensec_internal.h" /* TODO: remove this */
+#include "../libcli/auth/spnego.h"
+#include "krb5_errs.h"
 
 #ifdef HAVE_KRB5
 
-static DATA_BLOB unwrap_pac(DATA_BLOB *auth_data)
+#include "auth/kerberos/pac_utils.h"
+
+struct smb_krb5_context;
+
+/****************************************************************
+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)
 {
-       DATA_BLOB pac_contents;
-       ASN1_DATA data;
-       int data_type;
-
-       asn1_load(&data, *auth_data);
-       asn1_start_tag(&data, ASN1_SEQUENCE(0));
-       asn1_start_tag(&data, ASN1_SEQUENCE(0));
-       asn1_start_tag(&data, ASN1_CONTEXT(0));
-       asn1_read_Integer(&data, &data_type);
-       asn1_end_tag(&data);
-       asn1_start_tag(&data, ASN1_CONTEXT(1));
-       asn1_read_OctetString(&data, &pac_contents);
-       asn1_end_tag(&data);
-       asn1_end_tag(&data);
-       asn1_end_tag(&data);
-       return pac_contents;
-}
+       TALLOC_CTX *tmp_ctx;
+       struct PAC_DATA *pac_data = NULL;
+       struct PAC_DATA_CTR *pac_data_ctr = NULL;
+       NTSTATUS status = NT_STATUS_INTERNAL_ERROR;
+
+       tmp_ctx = talloc_new(mem_ctx);
+       if (!tmp_ctx) {
+               return NT_STATUS_NO_MEMORY;
+       }
 
-static BOOL pac_io_krb_sids(const char *desc, KRB_SID_AND_ATTRS *sid_and_attr,
-                           prs_struct *ps, int depth)
-{
-       if (NULL == sid_and_attr)
-               return False;
-
-       prs_debug(ps, depth, desc, "pac_io_krb_sids");
-       depth++;
-
-       if (UNMARSHALLING(ps)) {
-               sid_and_attr->sid = 
-                       (DOM_SID2 * ) prs_alloc_mem(ps, sizeof(DOM_SID2));
-               if (!sid_and_attr->sid) {
-                       DEBUG(3, ("No memory available\n"));
-                       return False;
+       if (pac_blob != NULL) {
+               status = kerberos_decode_pac(tmp_ctx,
+                                            *pac_blob,
+                                            NULL,
+                                            NULL,
+                                            NULL,
+                                            NULL,
+                                            0,
+                                            &pac_data);
+               if (!NT_STATUS_IS_OK(status)) {
+                       goto done;
                }
-       }
 
-       if(!smb_io_dom_sid2("sid", sid_and_attr->sid, ps, depth))
-               return False;
+               pac_data_ctr = talloc(mem_ctx, struct PAC_DATA_CTR);
+               if (pac_data_ctr == NULL) {
+                       status = NT_STATUS_NO_MEMORY;
+                       goto done;
+               }
 
-       return True;
-}
+               talloc_set_name_const(pac_data_ctr, "struct PAC_DATA_CTR");
 
+               pac_data_ctr->pac_data = talloc_steal(pac_data_ctr, pac_data);
+               pac_data_ctr->pac_blob = data_blob_talloc(pac_data_ctr,
+                                                         pac_blob->data,
+                                                         pac_blob->length);
 
-static BOOL pac_io_krb_attrs(const char *desc, KRB_SID_AND_ATTRS *sid_and_attr,
-                            prs_struct *ps, int depth)
-{
-       if (NULL == sid_and_attr)
-               return False;
+               auth_ctx->private_data = talloc_steal(auth_ctx, pac_data_ctr);
+       }
 
-       prs_debug(ps, depth, desc, "pac_io_krb_attrs");
-       depth++;
+       *session_info = talloc_zero(mem_ctx, struct auth_session_info);
+       if (!*session_info) {
+               status = NT_STATUS_NO_MEMORY;
+               goto done;
+       }
+       status = NT_STATUS_OK;
 
-       if (!prs_uint32("sid_ptr", ps, depth, &sid_and_attr->sid_ptr))
-               return False;
-       if (!prs_uint32("attrs", ps, depth, &sid_and_attr->attrs))
-               return False;
+done:
+       TALLOC_FREE(tmp_ctx);
 
-       return True;
+       return status;
 }
 
-static BOOL pac_io_krb_sid_and_attr_array(const char *desc, 
-                                         KRB_SID_AND_ATTR_ARRAY *array,
-                                         uint32 num,
-                                         prs_struct *ps, int depth)
+/*
+ * 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,
+                            time_t time_offset,
+                            time_t *expire_time,
+                            time_t *renew_till_time,
+                            const char *cache_name,
+                            bool request_pac,
+                            bool add_netbios_addr,
+                            time_t renewable_time,
+                            const char *impersonate_princ_s,
+                            const char *local_service,
+                            struct PAC_DATA_CTR **_pac_data_ctr)
 {
-       int i;
-
-       if (NULL == array)
-               return False;
-
-       prs_debug(ps, depth, desc, "pac_io_krb_sid_and_attr_array");
-       depth++;
-
-
-       if (!prs_uint32("count", ps, depth, &array->count))
-               return False;
-
-       if (UNMARSHALLING(ps)) {
-               array->krb_sid_and_attrs = (KRB_SID_AND_ATTRS *)
-                       prs_alloc_mem(ps, sizeof(KRB_SID_AND_ATTRS) * num);
-               if (!array->krb_sid_and_attrs) {
-                       DEBUG(3, ("No memory available\n"));
-                       return False;
-               }
+       krb5_error_code ret;
+       NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
+       DATA_BLOB tkt, tkt_wrapped, ap_rep, sesskey1;
+       const char *auth_princ = NULL;
+       const char *cc = "MEMORY:kerberos_return_pac";
+       struct auth_session_info *session_info;
+       struct gensec_security *gensec_server_context;
+       const struct gensec_security_ops **backends;
+       struct gensec_settings *gensec_settings;
+       size_t idx = 0;
+       struct auth4_context *auth_context;
+       struct loadparm_context *lp_ctx;
+       struct PAC_DATA_CTR *pac_data_ctr = 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);
+
+       if (!name || !pass) {
+               return NT_STATUS_INVALID_PARAMETER;
        }
 
-       for (i=0; i<num; i++) {
-               if (!pac_io_krb_attrs(desc, 
-                                     &array->krb_sid_and_attrs[i],
-                                     ps, depth))
-                       return False;
-
+       if (cache_name) {
+               cc = cache_name;
        }
-       for (i=0; i<num; i++) {
-               if (!pac_io_krb_sids(desc, 
-                                    &array->krb_sid_and_attrs[i],
-                                    ps, depth))
-                       return False;
 
+       if (!strchr_m(name, '@')) {
+               auth_princ = talloc_asprintf(mem_ctx, "%s@%s", name,
+                       lp_realm());
+       } else {
+               auth_princ = name;
+       }
+       NT_STATUS_HAVE_NO_MEMORY(auth_princ);
+
+       ret = kerberos_kinit_password_ext(auth_princ,
+                                         pass,
+                                         time_offset,
+                                         expire_time,
+                                         renew_till_time,
+                                         cc,
+                                         request_pac,
+                                         add_netbios_addr,
+                                         renewable_time,
+                                         &status);
+       if (ret) {
+               DEBUG(1,("kinit failed for '%s' with: %s (%d)\n",
+                       auth_princ, error_message(ret), ret));
+               /* status already set */
+               goto out;
        }
 
-       return True;
-
-}
+       DEBUG(10,("got TGT for %s in %s\n", auth_princ, cc));
+       if (expire_time) {
+               DEBUGADD(10,("\tvalid until: %s (%d)\n",
+                       http_timestring(talloc_tos(), *expire_time),
+                       (int)*expire_time));
+       }
+       if (renew_till_time) {
+               DEBUGADD(10,("\trenewable till: %s (%d)\n",
+                       http_timestring(talloc_tos(), *renew_till_time),
+                       (int)*renew_till_time));
+       }
 
-static BOOL pac_io_pac_logon_info(const char *desc, PAC_LOGON_INFO *info, 
-                                 prs_struct *ps, int depth)
-{
-       uint32 garbage;
-       if (NULL == info)
-               return False;
-
-       prs_debug(ps, depth, desc, "pac_io_pac_logon_info");
-       depth++;
-
-       if (!prs_uint32("unknown", ps, depth, &garbage))
-               return False;
-       if (!prs_uint32("unknown", ps, depth, &garbage))
-               return False;
-       if (!prs_uint32("bufferlen", ps, depth, &garbage))
-               return False;
-       if (!prs_uint32("bufferlenhi", ps, depth, &garbage))
-               return False;
-       if (!prs_uint32("pointer", ps, depth, &garbage))
-               return False;
-
-       if (!smb_io_time("logon_time", &info->logon_time, ps, depth))
-               return False;
-       if (!smb_io_time("logoff_time", &info->logoff_time, ps, depth))
-               return False;
-       if (!smb_io_time("kickoff_time", &info->kickoff_time, ps, depth))
-               return False;
-       if (!smb_io_time("pass_last_set_time", &info->pass_last_set_time, 
-                        ps, depth))
-               return False;
-       if (!smb_io_time("pass_can_change_time", &info->pass_can_change_time, 
-                        ps, depth))
-               return False;
-       if (!smb_io_time("pass_must_change_time", &info->pass_must_change_time,
-                        ps, depth))
-               return False;
-
-       if (!smb_io_unihdr("hdr_user_name", &info->hdr_user_name, ps, depth))
-               return False;
-       if (!smb_io_unihdr("hdr_full_name", &info->hdr_full_name, ps, depth))
-               return False;
-       if (!smb_io_unihdr("hdr_logon_script", &info->hdr_logon_script, 
-                          ps, depth))
-               return False;
-       if (!smb_io_unihdr("hdr_profile_path", &info->hdr_profile_path, 
-                          ps, depth))
-               return False;
-       if (!smb_io_unihdr("hdr_home_dir", &info->hdr_home_dir, ps, depth))
-               return False;
-       if (!smb_io_unihdr("hdr_dir_drive", &info->hdr_dir_drive, ps, depth))
-               return False;
-
-       if (!prs_uint16("logon_count", ps, depth, &info->logon_count))
-               return False;
-       if (!prs_uint16("reserved12", ps, depth, &info->reserved12))
-               return False;
-       if (!prs_uint32("user_rid", ps, depth, &info->user_rid))
-               return False;
-       if (!prs_uint32("group_rid", ps, depth, &info->group_rid))
-               return False;
-       if (!prs_uint32("group_count", ps, depth, &info->group_count))
-               return False;
-       /* I haven't seen this contain anything yet, but when it does
-          we will have to make sure we decode the contents in the middle
-          all the unistr2s ... */
-       if (!prs_uint32("group_mem_ptr", ps, depth, 
-                       &info->group_membership_ptr))
-               return False;
-       if (!prs_uint32("user_flags", ps, depth, &info->user_flags))
-               return False;
-
-       if (!prs_uint32("reserved13.0", ps, depth, &info->reserved13[0]))
-               return False;
-       if (!prs_uint32("reserved13.1", ps, depth, &info->reserved13[1]))
-               return False;
-       if (!prs_uint32("reserved13.2", ps, depth, &info->reserved13[2]))
-               return False;
-       if (!prs_uint32("reserved13.3", ps, depth, &info->reserved13[3]))
-               return False;
-       
-       if (!smb_io_unihdr("hdr_dom_controller", 
-                          &info->hdr_dom_controller, ps, depth))
-               return False;
-       if (!smb_io_unihdr("hdr_dom_name", &info->hdr_dom_name, ps, depth))
-               return False;
-
-       /* this should be followed, but just get ptr for now */
-       if (!prs_uint32("ptr_dom_sid", ps, depth, &info->ptr_dom_sid))
-               return False;
-
-       if (!prs_uint32("reserved16.0", ps, depth, &info->reserved16[0]))
-               return False;
-       if (!prs_uint32("reserved16.1", ps, depth, &info->reserved16[1]))
-               return False;
-
-       /* might be acb_info */
-       if (!prs_uint32("reserved17", ps, depth, &info->reserved17))
-               return False;
-
-
-       if (!prs_uint32("reserved18.0", ps, depth, &info->reserved18[0]))
-               return False;
-       if (!prs_uint32("reserved18.1", ps, depth, &info->reserved18[1]))
-               return False;
-       if (!prs_uint32("reserved18.2", ps, depth, &info->reserved18[2]))
-               return False;
-       if (!prs_uint32("reserved18.3", ps, depth, &info->reserved18[3]))
-               return False;
-       if (!prs_uint32("reserved18.4", ps, depth, &info->reserved18[4]))
-               return False;
-       if (!prs_uint32("reserved18.5", ps, depth, &info->reserved18[5]))
-               return False;
-       if (!prs_uint32("reserved18.6", ps, depth, &info->reserved18[6]))
-               return False;
-
-       if (!prs_uint32("sid_count", ps, depth, &info->sid_count))
-               return False;
-       if (!prs_uint32("ptr_extra_sids", ps, depth, &info->ptr_extra_sids))
-               return False;
-       if (!prs_uint32("ptr_res_group_dom_sid", ps, depth, 
-                       &info->ptr_res_group_dom_sid))
-               return False;
-       if (!prs_uint32("res_group_count", ps, depth, &info->res_group_count))
-               return False;
-       if (!prs_uint32("ptr_res_group_sids", ps, depth, 
-                       &info->ptr_res_group_sids))
-               return False;
-
-       if(!smb_io_unistr2("uni_user_name", &info->uni_user_name, 
-                          info->hdr_user_name.buffer, ps, depth))
-               return False;
-       if(!smb_io_unistr2("uni_full_name", &info->uni_full_name, 
-                          info->hdr_full_name.buffer, ps, depth))
-               return False;
-       if(!smb_io_unistr2("uni_logon_script", &info->uni_logon_script, 
-                          info->hdr_logon_script.buffer, ps, depth))
-               return False;
-       if(!smb_io_unistr2("uni_profile_path", &info->uni_profile_path,
-                          info->hdr_profile_path.buffer, ps, depth))
-               return False;
-       if(!smb_io_unistr2("uni_home_dir", &info->uni_home_dir,
-                          info->hdr_home_dir.buffer, ps, depth))
-               return False;
-       if(!smb_io_unistr2("uni_dir_drive", &info->uni_dir_drive,
-                          info->hdr_dir_drive.buffer, ps, depth))
-               return False;
-
-       /* the group membership list will need to be handled here */
-
-       if(!smb_io_unistr2("uni_dom_controller", &info->uni_dom_controller,
-                          info->hdr_dom_controller.buffer, ps, depth))
-               return False;
-       if(!smb_io_unistr2("uni_dom_name", &info->uni_dom_name, 
-                          info->hdr_dom_name.buffer, ps, depth))
-               return False;
-
-       if(info->ptr_dom_sid)
-               if(!smb_io_dom_sid2("dom_sid", &info->dom_sid, ps, depth))
-                       return False;
-
-       
-       if (info->sid_count && info->ptr_extra_sids) {
-               if (!pac_io_krb_sid_and_attr_array("extra_sids", 
-                                                  &info->extra_sids,
-                                                  info->sid_count,
-                                                  ps, depth))
-                       return False;
-       }               
-
-
-       return True;
-}
+       /* we cannot continue with krb5 when UF_DONT_REQUIRE_PREAUTH is set,
+        * in that case fallback to NTLM - gd */
 
+       if (expire_time && renew_till_time &&
+           (*expire_time == 0) && (*renew_till_time == 0)) {
+               return NT_STATUS_INVALID_LOGON_TYPE;
+       }
 
-static BOOL pac_io_pac_signature_data(const char *desc, 
-                                     PAC_SIGNATURE_DATA *data, uint32 length,
-                                     prs_struct *ps, int depth)
-{
-       uint32 siglen = length - sizeof(uint32);
-       if (NULL == data)
-               return False;
-
-       prs_debug(ps, depth, desc, "pac_io_pac_signature_data");
-       depth++;
-
-       if (!prs_uint32("type", ps, depth, &data->type))
-               return False;
-       if (UNMARSHALLING(ps)) {
-               data->signature = prs_alloc_mem(ps, siglen);
-               if (!data->signature) {
-                       DEBUG(3, ("No memory available\n"));
-                       return False;
+       ret = ads_krb5_cli_get_ticket(mem_ctx,
+                                     local_service,
+                                     time_offset,
+                                     &tkt,
+                                     &sesskey1,
+                                     0,
+                                     cc,
+                                     NULL,
+                                     impersonate_princ_s);
+       if (ret) {
+               DEBUG(1,("failed to get ticket for %s: %s\n",
+                       local_service, error_message(ret)));
+               if (impersonate_princ_s) {
+                       DEBUGADD(1,("tried S4U2SELF impersonation as: %s\n",
+                               impersonate_princ_s));
                }
+               status = krb5_to_nt_status(ret);
+               goto out;
        }
-       if (!prs_uint8s(False, "signature", ps, depth, data->signature,siglen))
-               return False;
-
-       return True;
-}
 
-static BOOL pac_io_pac_info_hdr_ctr(const char *desc, PAC_INFO_HDR *hdr,
-                                   prs_struct *ps, int depth)
-{
-       if (NULL == hdr)
-               return False;
-
-       prs_debug(ps, depth, desc, "pac_io_pac_info_hdr_ctr");
-       depth++;
+       /* 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;
+       }
 
-       if (!prs_align(ps))
-               return False;
+       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;
 
-       if (hdr->offset != prs_offset(ps)) {
-               DEBUG(5, ("offset in header(x%x) and data(x%x) do not match\n",
-                         hdr->offset, prs_offset(ps)));
-               prs_set_offset(ps, hdr->offset);
+       lp_ctx = loadparm_init_s3(tmp_ctx, loadparm_s3_helpers());
+       if (lp_ctx == NULL) {
+               status = NT_STATUS_INVALID_SERVER_STATE;
+               DEBUG(10, ("loadparm_init_s3 failed\n"));
+               goto out;
        }
 
-       if (UNMARSHALLING(ps) && hdr->size > 0) {
-               hdr->ctr = (PAC_INFO_CTR *) 
-                       prs_alloc_mem(ps, sizeof(PAC_INFO_CTR));
-               if (!hdr->ctr) {
-                       DEBUG(3, ("No memory available\n"));
-                       return False;
-               }
+       gensec_settings = lpcfg_gensec_settings(tmp_ctx, lp_ctx);
+       if (gensec_settings == NULL) {
+               status = NT_STATUS_NO_MEMORY;
+               DEBUG(10, ("lpcfg_gensec_settings failed\n"));
+               goto out;
        }
 
-       switch(hdr->type) {
-       case PAC_TYPE_LOGON_INFO:
-               DEBUG(5, ("PAC_TYPE_LOGON_INFO\n"));
-               if (UNMARSHALLING(ps))
-                       hdr->ctr->pac.logon_info = (PAC_LOGON_INFO *)
-                               prs_alloc_mem(ps, sizeof(PAC_LOGON_INFO));
-               if (!hdr->ctr->pac.logon_info) {
-                       DEBUG(3, ("No memory available\n"));
-                       return False;
-               }
-               if (!pac_io_pac_logon_info(desc, hdr->ctr->pac.logon_info,
-                                          ps, depth))
-                       return False;
-               break;
-
-       case PAC_TYPE_SERVER_CHECKSUM:
-               DEBUG(5, ("PAC_TYPE_SERVER_CHECKSUM\n"));
-               if (UNMARSHALLING(ps))
-                       hdr->ctr->pac.srv_cksum = (PAC_SIGNATURE_DATA *)
-                               prs_alloc_mem(ps, sizeof(PAC_SIGNATURE_DATA));
-               if (!hdr->ctr->pac.srv_cksum) {
-                       DEBUG(3, ("No memory available\n"));
-                       return False;
-               }
-               if (!pac_io_pac_signature_data(desc, hdr->ctr->pac.srv_cksum,
-                                              hdr->size, ps, depth))
-                       return False;
-               break;
-
-       case PAC_TYPE_PRIVSVR_CHECKSUM:
-               DEBUG(5, ("PAC_TYPE_PRIVSVR_CHECKSUM\n"));
-               if (UNMARSHALLING(ps))
-                       hdr->ctr->pac.privsrv_cksum = (PAC_SIGNATURE_DATA *)
-                               prs_alloc_mem(ps, sizeof(PAC_SIGNATURE_DATA));
-               if (!hdr->ctr->pac.privsrv_cksum) {
-                       DEBUG(3, ("No memory available\n"));
-                       return False;
-               }
-               if (!pac_io_pac_signature_data(desc, 
-                                              hdr->ctr->pac.privsrv_cksum,
-                                              hdr->size, ps, depth))
-                       return False;
-               break;
-
-       default:
-               /* dont' know, so we need to skip it */
-               DEBUG(3, ("unknown PAC type %d\n", hdr->type));
-               prs_set_offset(ps, prs_offset(ps) + hdr->size);
+       backends = talloc_zero_array(gensec_settings,
+                                    const struct gensec_security_ops *, 2);
+       if (backends == NULL) {
+               status = NT_STATUS_NO_MEMORY;
+               goto out;
        }
+       gensec_settings->backends = backends;
 
-       return True;
-}
+       gensec_init();
 
-static BOOL pac_io_pac_info_hdr(const char *desc, PAC_INFO_HDR *hdr, 
-                               prs_struct *ps, int depth)
-{
-       if (NULL == hdr)
-               return False;
-
-       prs_debug(ps, depth, desc, "pac_io_pac_info_hdr");
-       depth++;
-
-       if (!prs_align(ps))
-               return False;
-       if (!prs_uint32("type", ps, depth, &hdr->type))
-               return False;
-       if (!prs_uint32("size", ps, depth, &hdr->size))
-               return False;
-       if (!prs_uint32("offset", ps, depth, &hdr->offset))
-               return False;
-       if (!prs_uint32("offsethi", ps, depth, &hdr->offsethi))
-               return False;
-
-       return True;
-}
+       backends[idx++] = &gensec_gse_krb5_security_ops;
 
-static BOOL pac_io_pac_data(const char *desc, PAC_DATA *data, 
-                           prs_struct *ps, int depth)
-{
-       int i;
-
-       if (NULL == data)
-               return False;
-
-       prs_debug(ps, depth, desc, "pac_io_pac_data");
-       depth++;
-
-       if (!prs_align(ps))
-               return False;
-       if (!prs_uint32("num_buffers", ps, depth, &data->num_buffers))
-               return False;
-       if (!prs_uint32("version", ps, depth, &data->version))
-               return False;
-
-       if (UNMARSHALLING(ps) && data->num_buffers > 0) {
-               if ((data->pac_info_hdr_ptr = (PAC_INFO_HDR *) 
-                    prs_alloc_mem(ps, sizeof(PAC_INFO_HDR) * 
-                                  data->num_buffers)) == NULL) {
-                       return False;
-               }
+       status = gensec_server_start(tmp_ctx, gensec_settings,
+                                       auth_context, &gensec_server_context);
+
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(1, (__location__ "Failed to start server-side GENSEC to validate a Kerberos ticket: %s\n", nt_errstr(status)));
+               goto out;
        }
 
-       for (i=0; i<data->num_buffers; i++) {
-               if (!pac_io_pac_info_hdr(desc, &data->pac_info_hdr_ptr[i], ps, 
-                                        depth))
-                       return False;
+       talloc_unlink(tmp_ctx, lp_ctx);
+       talloc_unlink(tmp_ctx, gensec_settings);
+       talloc_unlink(tmp_ctx, auth_context);
+
+       /* Session info is not complete, do not pass to auth log */
+       gensec_want_feature(gensec_server_context, GENSEC_FEATURE_NO_AUTHZ_LOG);
+
+       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;
        }
 
-       for (i=0; i<data->num_buffers; i++) {
-               if (!pac_io_pac_info_hdr_ctr(desc, &data->pac_info_hdr_ptr[i],
-                                            ps, depth))
-                       return False;
+       /* Do a client-server update dance */
+       status = gensec_update(gensec_server_context, tmp_ctx, tkt_wrapped, &ap_rep);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(1, ("gensec_update() failed: %s\n", nt_errstr(status)));
+               goto out;
        }
 
-       return True;
-}
+       /* 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;
+       }
 
-PAC_DATA *decode_pac_data(DATA_BLOB *auth_data, TALLOC_CTX *ctx)
-{
-       DATA_BLOB pac_data_blob = unwrap_pac(auth_data);
-       prs_struct ps;
-       PAC_DATA *pac_data;
+       pac_data_ctr = talloc_get_type_abort(gensec_server_context->auth_context->private_data,
+                                            struct PAC_DATA_CTR);
+       if (pac_data_ctr == NULL) {
+               DEBUG(1,("no PAC\n"));
+               status = NT_STATUS_INVALID_PARAMETER;
+               goto out;
+       }
 
-       DEBUG(5,("dump_pac_data\n"));
-       prs_init(&ps, pac_data_blob.length, ctx, UNMARSHALL);
-       prs_copy_data_in(&ps, pac_data_blob.data, pac_data_blob.length);
-       prs_set_offset(&ps, 0);
+       *_pac_data_ctr = talloc_move(mem_ctx, &pac_data_ctr);
 
-       pac_data = (PAC_DATA *) talloc_zero(ctx, sizeof(PAC_DATA));
-       pac_io_pac_data("pac data", pac_data, &ps, 0);
+out:
+       talloc_free(tmp_ctx);
+       if (cc != cache_name) {
+               ads_kdestroy(cc);
+       }
 
-       prs_mem_free(&ps);
+       data_blob_free(&tkt);
+       data_blob_free(&ap_rep);
+       data_blob_free(&sesskey1);
 
-       return pac_data;
+       return status;
 }
 
 #endif