s3-kerberos: let smb_krb5_get_tkt_from_creds() compile with older heimdal libs.
[ira/wip.git] / source3 / libads / authdata.c
index ccaf82124c66eaeba0d023e01807030d0a0731df..f287b16b9d04ce2c256850eab441fad8f641db99 100644 (file)
-/* 
+/*
    Unix SMB/CIFS implementation.
    kerberos authorization data (PAC) utility library
-   Copyright (C) Jim McDonough <jmcd@us.ibm.com> 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"
 
 #ifdef HAVE_KRB5
 
-static DATA_BLOB unwrap_pac(DATA_BLOB *auth_data)
-{
-       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);
-       asn1_free(&data);
-       return pac_contents;
-}
+/****************************************************************
+****************************************************************/
 
-static BOOL pac_io_unknown_type_10(const char *desc, UNKNOWN_TYPE_10 *type_10,
-                                  prs_struct *ps, int depth)
+static krb5_error_code check_pac_checksum(TALLOC_CTX *mem_ctx,
+                                         DATA_BLOB pac_data,
+                                         struct PAC_SIGNATURE_DATA *sig,
+                                         krb5_context context,
+                                         krb5_keyblock *keyblock)
 {
-       if (NULL == type_10)
-               return False;
-
-       prs_debug(ps, depth, desc, "pac_io_unknown_type_10");
-       depth++;
-
-       if (!smb_io_time("unknown_time", &type_10->unknown_time, ps, depth))
-               return False;
-
-       if (!prs_uint16("len", ps, depth, &type_10->len))
-               return False;
+       krb5_error_code ret;
+       krb5_checksum cksum;
+       krb5_keyusage usage = 0;
+
+       smb_krb5_checksum_from_pac_sig(&cksum, sig);
+
+#ifdef HAVE_KRB5_KU_OTHER_CKSUM /* Heimdal */
+       usage = KRB5_KU_OTHER_CKSUM;
+#elif defined(HAVE_KRB5_KEYUSAGE_APP_DATA_CKSUM) /* MIT */
+       usage = KRB5_KEYUSAGE_APP_DATA_CKSUM;
+#else
+#error UNKNOWN_KRB5_KEYUSAGE
+#endif
 
-       if (UNMARSHALLING(ps) && type_10->len) {
-               type_10->username = PRS_ALLOC_MEM(ps, uint16, type_10->len);
-               if (!type_10->username) {
-                       DEBUG(3, ("No memory available\n"));
-                       return False;
-               }
+       ret = smb_krb5_verify_checksum(context,
+                                      keyblock,
+                                      usage,
+                                      &cksum,
+                                      pac_data.data,
+                                      pac_data.length);
+
+       if (ret) {
+               DEBUG(2,("check_pac_checksum: PAC Verification failed: %s (%d)\n",
+                       error_message(ret), ret));
+               return ret;
        }
 
-       if (!prs_uint16s(True, "name", ps, depth, type_10->username, 
-                        (type_10->len / sizeof(uint16))))
-               return False;
-
-       return True;
-
+       return ret;
 }
 
+/****************************************************************
+****************************************************************/
 
-static BOOL pac_io_krb_sids(const char *desc, KRB_SID_AND_ATTRS *sid_and_attr,
-                           prs_struct *ps, int depth)
+ NTSTATUS decode_pac_data(TALLOC_CTX *mem_ctx,
+                        DATA_BLOB *pac_data_blob,
+                        krb5_context context,
+                        krb5_keyblock *service_keyblock,
+                        krb5_const_principal client_principal,
+                        time_t tgs_authtime,
+                        struct PAC_DATA **pac_data_out)
 {
-       if (NULL == sid_and_attr)
-               return False;
+       NTSTATUS status;
+       enum ndr_err_code ndr_err;
+       krb5_error_code ret;
+       DATA_BLOB modified_pac_blob;
 
-       prs_debug(ps, depth, desc, "pac_io_krb_sids");
-       depth++;
+       NTTIME tgs_authtime_nttime;
+       krb5_principal client_principal_pac = NULL;
+       int i;
 
-       if (UNMARSHALLING(ps)) {
-               sid_and_attr->sid = PRS_ALLOC_MEM(ps, DOM_SID2, 1);
-               if (!sid_and_attr->sid) {
-                       DEBUG(3, ("No memory available\n"));
-                       return False;
-               }
+       struct PAC_SIGNATURE_DATA *srv_sig_ptr = NULL;
+       struct PAC_SIGNATURE_DATA *kdc_sig_ptr = NULL;
+       struct PAC_SIGNATURE_DATA *srv_sig_wipe = NULL;
+       struct PAC_SIGNATURE_DATA *kdc_sig_wipe = NULL;
+       struct PAC_LOGON_NAME *logon_name = NULL;
+       struct PAC_LOGON_INFO *logon_info = NULL;
+       struct PAC_DATA *pac_data = NULL;
+       struct PAC_DATA_RAW *pac_data_raw = NULL;
+
+       DATA_BLOB *srv_sig_blob = NULL;
+       DATA_BLOB *kdc_sig_blob = NULL;
+
+       *pac_data_out = NULL;
+
+       pac_data = TALLOC_ZERO_P(mem_ctx, struct PAC_DATA);
+       pac_data_raw = TALLOC_ZERO_P(mem_ctx, struct PAC_DATA_RAW);
+       kdc_sig_wipe = TALLOC_ZERO_P(mem_ctx, struct PAC_SIGNATURE_DATA);
+       srv_sig_wipe = TALLOC_ZERO_P(mem_ctx, struct PAC_SIGNATURE_DATA);
+       if (!pac_data_raw || !pac_data || !kdc_sig_wipe || !srv_sig_wipe) {
+               return NT_STATUS_NO_MEMORY;
        }
 
-       if(!smb_io_dom_sid2("sid", sid_and_attr->sid, ps, depth))
-               return False;
+       ndr_err = ndr_pull_struct_blob(pac_data_blob, pac_data,
+                       NULL, pac_data,
+                      (ndr_pull_flags_fn_t)ndr_pull_PAC_DATA);
+       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+               status = ndr_map_error2ntstatus(ndr_err);
+               DEBUG(0,("can't parse the PAC: %s\n",
+                       nt_errstr(status)));
+               return status;
+       }
 
-       return True;
-}
+       if (pac_data->num_buffers < 4) {
+               /* we need logon_ingo, service_key and kdc_key */
+               DEBUG(0,("less than 4 PAC buffers\n"));
+               return NT_STATUS_INVALID_PARAMETER;
+       }
 
+       ndr_err = ndr_pull_struct_blob(pac_data_blob, pac_data_raw,
+                                      NULL, pac_data_raw,
+                                      (ndr_pull_flags_fn_t)ndr_pull_PAC_DATA_RAW);
+       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+               status = ndr_map_error2ntstatus(ndr_err);
+               DEBUG(0,("can't parse the PAC: %s\n",
+                       nt_errstr(status)));
+               return status;
+       }
 
-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;
+       if (pac_data_raw->num_buffers < 4) {
+               /* we need logon_ingo, service_key and kdc_key */
+               DEBUG(0,("less than 4 PAC buffers\n"));
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
+       if (pac_data->num_buffers != pac_data_raw->num_buffers) {
+               /* we need logon_ingo, service_key and kdc_key */
+               DEBUG(0,("misparse!  PAC_DATA has %d buffers while PAC_DATA_RAW has %d\n",
+                        pac_data->num_buffers, pac_data_raw->num_buffers));
+               return NT_STATUS_INVALID_PARAMETER;
+       }
 
-       prs_debug(ps, depth, desc, "pac_io_krb_attrs");
-       depth++;
+       for (i=0; i < pac_data->num_buffers; i++) {
+               if (pac_data->buffers[i].type != pac_data_raw->buffers[i].type) {
+                       DEBUG(0,("misparse!  PAC_DATA buffer %d has type %d while PAC_DATA_RAW has %d\n",
+                                i, pac_data->buffers[i].type, pac_data->buffers[i].type));
+                       return NT_STATUS_INVALID_PARAMETER;
+               }
+               switch (pac_data->buffers[i].type) {
+                       case PAC_TYPE_LOGON_INFO:
+                               if (!pac_data->buffers[i].info) {
+                                       break;
+                               }
+                               logon_info = pac_data->buffers[i].info->logon_info.info;
+                               break;
+                       case PAC_TYPE_SRV_CHECKSUM:
+                               if (!pac_data->buffers[i].info) {
+                                       break;
+                               }
+                               srv_sig_ptr = &pac_data->buffers[i].info->srv_cksum;
+                               srv_sig_blob = &pac_data_raw->buffers[i].info->remaining;
+                               break;
+                       case PAC_TYPE_KDC_CHECKSUM:
+                               if (!pac_data->buffers[i].info) {
+                                       break;
+                               }
+                               kdc_sig_ptr = &pac_data->buffers[i].info->kdc_cksum;
+                               kdc_sig_blob = &pac_data_raw->buffers[i].info->remaining;
+                               break;
+                       case PAC_TYPE_LOGON_NAME:
+                               logon_name = &pac_data->buffers[i].info->logon_name;
+                               break;
+                       default:
+                               break;
+               }
+       }
 
-       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;
+       if (!logon_info) {
+               DEBUG(0,("PAC no logon_info\n"));
+               return NT_STATUS_INVALID_PARAMETER;
+       }
 
-       return True;
-}
+       if (!logon_name) {
+               DEBUG(0,("PAC no logon_name\n"));
+               return NT_STATUS_INVALID_PARAMETER;
+       }
 
-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)
-{
-       int i;
+       if (!srv_sig_ptr || !srv_sig_blob) {
+               DEBUG(0,("PAC no srv_key\n"));
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
+       if (!kdc_sig_ptr || !kdc_sig_blob) {
+               DEBUG(0,("PAC no kdc_key\n"));
+               return NT_STATUS_INVALID_PARAMETER;
+       }
 
-       if (NULL == array)
-               return False;
+       /* Find and zero out the signatures, as required by the signing algorithm */
+
+       /* We find the data blobs above, now we parse them to get at the exact portion we should zero */
+       ndr_err = ndr_pull_struct_blob(kdc_sig_blob, kdc_sig_wipe,
+                                      NULL, kdc_sig_wipe,
+                                      (ndr_pull_flags_fn_t)ndr_pull_PAC_SIGNATURE_DATA);
+       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+               status = ndr_map_error2ntstatus(ndr_err);
+               DEBUG(0,("can't parse the KDC signature: %s\n",
+                       nt_errstr(status)));
+               return status;
+       }
 
-       prs_debug(ps, depth, desc, "pac_io_krb_sid_and_attr_array");
-       depth++;
+       ndr_err = ndr_pull_struct_blob(srv_sig_blob, srv_sig_wipe,
+                                      NULL, srv_sig_wipe,
+                                      (ndr_pull_flags_fn_t)ndr_pull_PAC_SIGNATURE_DATA);
+       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+               status = ndr_map_error2ntstatus(ndr_err);
+               DEBUG(0,("can't parse the SRV signature: %s\n",
+                       nt_errstr(status)));
+               return status;
+       }
 
+       /* Now zero the decoded structure */
+       memset(kdc_sig_wipe->signature.data, '\0', kdc_sig_wipe->signature.length);
+       memset(srv_sig_wipe->signature.data, '\0', srv_sig_wipe->signature.length);
+
+       /* and reencode, back into the same place it came from */
+       ndr_err = ndr_push_struct_blob(kdc_sig_blob, pac_data_raw,
+                                      NULL, kdc_sig_wipe,
+                                      (ndr_push_flags_fn_t)ndr_push_PAC_SIGNATURE_DATA);
+       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+               status = ndr_map_error2ntstatus(ndr_err);
+               DEBUG(0,("can't repack the KDC signature: %s\n",
+                       nt_errstr(status)));
+               return status;
+       }
+       ndr_err = ndr_push_struct_blob(srv_sig_blob, pac_data_raw,
+                                      NULL, srv_sig_wipe,
+                                      (ndr_push_flags_fn_t)ndr_push_PAC_SIGNATURE_DATA);
+       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+               status = ndr_map_error2ntstatus(ndr_err);
+               DEBUG(0,("can't repack the SRV signature: %s\n",
+                       nt_errstr(status)));
+               return status;
+       }
 
-       if (!prs_uint32("count", ps, depth, &array->count))
-               return False;
+       /* push out the whole structure, but now with zero'ed signatures */
+       ndr_err = ndr_push_struct_blob(&modified_pac_blob, pac_data_raw,
+                                      NULL, pac_data_raw,
+                                      (ndr_push_flags_fn_t)ndr_push_PAC_DATA_RAW);
+       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+               status = ndr_map_error2ntstatus(ndr_err);
+               DEBUG(0,("can't repack the RAW PAC: %s\n",
+                       nt_errstr(status)));
+               return status;
+       }
 
-       if (UNMARSHALLING(ps)) {
-               array->krb_sid_and_attrs = PRS_ALLOC_MEM(ps, KRB_SID_AND_ATTRS, num);
-               if (!array->krb_sid_and_attrs) {
-                       DEBUG(3, ("No memory available\n"));
-                       return False;
-               }
+       /* verify by service_key */
+       ret = check_pac_checksum(mem_ctx,
+                                modified_pac_blob, srv_sig_ptr,
+                                context,
+                                service_keyblock);
+       if (ret) {
+               DEBUG(1, ("PAC Decode: Failed to verify the service signature: %s\n",
+                         error_message(ret)));
+               return NT_STATUS_ACCESS_DENIED;
        }
 
-       for (i=0; i<num; i++) {
-               if (!pac_io_krb_attrs(desc, 
-                                     &array->krb_sid_and_attrs[i],
-                                     ps, depth))
-                       return False;
+       /* Convert to NT time, so as not to loose accuracy in comparison */
+       unix_to_nt_time(&tgs_authtime_nttime, tgs_authtime);
 
+       if (tgs_authtime_nttime != logon_name->logon_time) {
+               DEBUG(2, ("PAC Decode: Logon time mismatch between ticket and PAC!\n"));
+               DEBUG(2, ("PAC Decode: PAC: %s\n", nt_time_string(mem_ctx, logon_name->logon_time)));
+               DEBUG(2, ("PAC Decode: Ticket: %s\n", nt_time_string(mem_ctx, tgs_authtime_nttime)));
+               return NT_STATUS_ACCESS_DENIED;
        }
-       for (i=0; i<num; i++) {
-               if (!pac_io_krb_sids(desc, 
-                                    &array->krb_sid_and_attrs[i],
-                                    ps, depth))
-                       return False;
 
+       ret = smb_krb5_parse_name_norealm(context, logon_name->account_name,
+                                   &client_principal_pac);
+       if (ret) {
+               DEBUG(2, ("Could not parse name from incoming PAC: [%s]: %s\n",
+                         logon_name->account_name,
+                         error_message(ret)));
+               return NT_STATUS_INVALID_PARAMETER;
        }
 
-       return True;
+       if (!smb_krb5_principal_compare_any_realm(context, client_principal, client_principal_pac)) {
+               DEBUG(2, ("Name in PAC [%s] does not match principal name in ticket\n",
+                         logon_name->account_name));
+               krb5_free_principal(context, client_principal_pac);
+               return NT_STATUS_ACCESS_DENIED;
+       }
 
-}
+       DEBUG(3,("Found account name from PAC: %s [%s]\n",
+                logon_info->info3.base.account_name.string,
+                logon_info->info3.base.full_name.string));
 
-static BOOL pac_io_group_membership(const char *desc, 
-                                   GROUP_MEMBERSHIP *membership,
-                                   prs_struct *ps, int depth)
-{
-       if (NULL == membership)
-               return False;
+       DEBUG(10,("Successfully validated Kerberos PAC\n"));
 
-       prs_debug(ps, depth, desc, "pac_io_group_membership");
-       depth++;
+       if (DEBUGLEVEL >= 10) {
+               const char *s;
+               s = NDR_PRINT_STRUCT_STRING(mem_ctx, PAC_DATA, pac_data);
+               if (s) {
+                       DEBUGADD(10,("%s\n", s));
+               }
+       }
 
-       if (!prs_uint32("rid", ps, depth, &membership->rid))
-               return False;
-       if (!prs_uint32("attrs", ps, depth, &membership->attrs))
-               return False;
+       *pac_data_out = pac_data;
 
-       return True;
+       return NT_STATUS_OK;
 }
 
+/****************************************************************
+****************************************************************/
 
-static BOOL pac_io_group_membership_array(const char *desc, 
-                                         GROUP_MEMBERSHIP_ARRAY *array,
-                                         uint32 num,
-                                         prs_struct *ps, int depth)
+struct PAC_LOGON_INFO *get_logon_info_from_pac(struct PAC_DATA *pac_data)
 {
        int i;
 
-       if (NULL == array)
-               return False;
+       for (i=0; i < pac_data->num_buffers; i++) {
 
-       prs_debug(ps, depth, desc, "pac_io_group_membership_array");
-       depth++;
+               if (pac_data->buffers[i].type != PAC_TYPE_LOGON_INFO) {
+                       continue;
+               }
 
+               return pac_data->buffers[i].info->logon_info.info;
+       }
 
-       if (!prs_uint32("count", ps, depth, &array->count))
-               return False;
+       return NULL;
+}
 
-       if (UNMARSHALLING(ps)) {
-               array->group_membership = PRS_ALLOC_MEM(ps, GROUP_MEMBERSHIP, num);
-               if (!array->group_membership) {
-                       DEBUG(3, ("No memory available\n"));
-                       return False;
-               }
+static krb5_error_code smb_krb5_get_tkt_from_creds(krb5_creds *creds,
+                                                  DATA_BLOB *tkt)
+{
+       krb5_error_code ret;
+       krb5_context context;
+       krb5_auth_context auth_context = NULL;
+       krb5_data inbuf, outbuf;
+
+       ret = krb5_init_context(&context);
+       if (ret) {
+               return ret;
        }
 
-       for (i=0; i<num; i++) {
-               if (!pac_io_group_membership(desc, 
-                                            &array->group_membership[i],
-                                            ps, depth))
-                       return False;
+       ret = krb5_auth_con_init(context, &auth_context);
+       if (ret) {
+               goto done;
+       }
 
+       ZERO_STRUCT(inbuf);
+
+       ret = krb5_mk_req_extended(context, &auth_context, AP_OPTS_USE_SUBKEY,
+                                  &inbuf, creds, &outbuf);
+       if (ret) {
+               goto done;
        }
 
-       return True;
+       *tkt = data_blob(outbuf.data, outbuf.length);
+ done:
+       if (!context) {
+               return ret;
+       }
+       kerberos_free_data_contents(context, &outbuf);
+       if (auth_context) {
+               krb5_auth_con_free(context, auth_context);
+       }
+       krb5_free_context(context);
 
+       return ret;
 }
 
-static BOOL pac_io_pac_logon_info(const char *desc, PAC_LOGON_INFO *info, 
-                                 prs_struct *ps, int depth)
+/****************************************************************
+****************************************************************/
+
+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,
+                            struct PAC_DATA **pac_ret)
 {
-       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_groups", ps, depth, &info->ptr_res_groups))
-               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;
-
-       if (info->group_membership_ptr) {
-               if (!pac_io_group_membership_array("group membership",
-                                                  &info->groups,
-                                                  info->group_count,
-                                                  ps, depth))
-                       return False;
-       }
-
-
-       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;
-
-       if (info->ptr_res_group_dom_sid)
-               if (!smb_io_dom_sid2("res_group_dom_sid", 
-                                    &info->res_group_dom_sid, ps, depth))
-                       return False;
-
-       if (info->ptr_res_groups)
-               if (!pac_io_group_membership_array("res group membership",
-                                                  &info->res_groups,
-                                                  info->res_group_count,
-                                                  ps, depth))
-                       return False;
-
-       return True;
-}
+       krb5_error_code ret;
+       NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
+       DATA_BLOB tkt, ap_rep, sesskey1, sesskey2;
+       struct PAC_DATA *pac_data = NULL;
+       char *client_princ_out = NULL;
+       const char *auth_princ = NULL;
+       const char *local_service = NULL;
+       const char *cc = "MEMORY:kerberos_return_pac";
+       krb5_creds *creds = NULL;
+
+       ZERO_STRUCT(tkt);
+       ZERO_STRUCT(ap_rep);
+       ZERO_STRUCT(sesskey1);
+       ZERO_STRUCT(sesskey2);
+
+       if (!name || !pass) {
+               return NT_STATUS_INVALID_PARAMETER;
+       }
 
+       if (cache_name) {
+               cc = cache_name;
+       }
 
-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, unsigned char, siglen);
-               if (!data->signature) {
-                       DEBUG(3, ("No memory available\n"));
-                       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);
+
+       local_service = talloc_asprintf(mem_ctx, "%s$@%s",
+                                       global_myname(), lp_realm());
+       NT_STATUS_HAVE_NO_MEMORY(local_service);
+
+       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;
        }
-       if (!prs_uint8s(False, "signature", ps, depth, data->signature,siglen))
-               return False;
 
-       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_info_hdr_ctr(const char *desc, PAC_INFO_HDR *hdr,
-                                   prs_struct *ps, int depth)
-{
-       if (NULL == hdr)
-               return False;
+       /* we cannot continue with krb5 when UF_DONT_REQUIRE_PREAUTH is set,
+        * in that case fallback to NTLM - gd */
 
-       prs_debug(ps, depth, desc, "pac_io_pac_info_hdr_ctr");
-       depth++;
+       if (expire_time && renew_till_time &&
+           (*expire_time == 0) && (*renew_till_time == 0)) {
+               return NT_STATUS_INVALID_LOGON_TYPE;
+       }
+#if 1
+       ret = smb_krb5_get_creds(local_service,
+                                time_offset,
+                                cc,
+                                impersonate_princ_s,
+                                &creds);
+       if (ret) {
+               DEBUG(1,("failed to get credentials for %s: %s\n",
+                       local_service, error_message(ret)));
+               status = krb5_to_nt_status(ret);
+               goto out;
+       }
 
-       if (!prs_align(ps))
-               return False;
+       ret = smb_krb5_get_tkt_from_creds(creds, &tkt);
+       if (ret) {
+               status = krb5_to_nt_status(ret);
+               goto out;
+       }
 
-       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);
+#else
+       ret = cli_krb5_get_ticket(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)));
+               status = krb5_to_nt_status(ret);
+               goto out;
+       }
+#endif
+       status = ads_verify_ticket(mem_ctx,
+                                  lp_realm(),
+                                  time_offset,
+                                  &tkt,
+                                  &client_princ_out,
+                                  &pac_data,
+                                  &ap_rep,
+                                  &sesskey2,
+                                  False);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(1,("ads_verify_ticket failed: %s\n",
+                       nt_errstr(status)));
+               goto out;
        }
 
-       if (UNMARSHALLING(ps) && hdr->size > 0) {
-               hdr->ctr = PRS_ALLOC_MEM(ps, PAC_INFO_CTR, 1);
-               if (!hdr->ctr) {
-                       DEBUG(3, ("No memory available\n"));
-                       return False;
-               }
+       if (!pac_data) {
+               DEBUG(1,("no PAC\n"));
+               status = NT_STATUS_INVALID_PARAMETER;
+               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 = PRS_ALLOC_MEM(ps, PAC_LOGON_INFO, 1);
-               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 = PRS_ALLOC_MEM(ps, PAC_SIGNATURE_DATA, 1);
-               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 = PRS_ALLOC_MEM(ps, PAC_SIGNATURE_DATA, 1);
-               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;
-
-       case PAC_TYPE_UNKNOWN_10:
-               DEBUG(5, ("PAC_TYPE_UNKNOWN_10\n"));
-               if (UNMARSHALLING(ps))
-                       hdr->ctr->pac.type_10 = PRS_ALLOC_MEM(ps, UNKNOWN_TYPE_10, 1);
-               if (!hdr->ctr->pac.type_10) {
-                       DEBUG(3, ("No memory available\n"));
-                       return False;
-               }
-               if (!pac_io_unknown_type_10(desc, hdr->ctr->pac.type_10,
-                                           ps, depth))
-                       return False;
-               break;
+       *pac_ret = pac_data;
 
-       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);
+out:
+       if (cc != cache_name) {
+               ads_kdestroy(cc);
        }
 
-       return True;
-}
+       data_blob_free(&tkt);
+       data_blob_free(&ap_rep);
+       data_blob_free(&sesskey1);
+       data_blob_free(&sesskey2);
 
-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;
+       TALLOC_FREE(client_princ_out);
+
+       return status;
 }
 
-static BOOL pac_io_pac_data(const char *desc, PAC_DATA *data, 
-                           prs_struct *ps, int depth)
+/****************************************************************
+****************************************************************/
+
+static NTSTATUS kerberos_return_pac_logon_info(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,
+                                              struct PAC_LOGON_INFO **logon_info)
 {
-       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 = PRS_ALLOC_MEM(ps, PAC_INFO_HDR, data->num_buffers)) == NULL) {
-                       return False;
-               }
+       NTSTATUS status;
+       struct PAC_DATA *pac_data = NULL;
+       struct PAC_LOGON_INFO *info = NULL;
+
+       status = kerberos_return_pac(mem_ctx,
+                                    name,
+                                    pass,
+                                    time_offset,
+                                    expire_time,
+                                    renew_till_time,
+                                    cache_name,
+                                    request_pac,
+                                    add_netbios_addr,
+                                    renewable_time,
+                                    impersonate_princ_s,
+                                    &pac_data);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
        }
 
-       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;
+       if (!pac_data) {
+               DEBUG(3,("no pac\n"));
+               return NT_STATUS_INVALID_USER_BUFFER;
        }
 
-       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;
+       info = get_logon_info_from_pac(pac_data);
+       if (!info) {
+               DEBUG(1,("no logon_info\n"));
+               return NT_STATUS_INVALID_USER_BUFFER;
        }
 
-       return True;
+       *logon_info = info;
+
+       return NT_STATUS_OK;
 }
 
-PAC_DATA *decode_pac_data(DATA_BLOB *auth_data, TALLOC_CTX *ctx)
+/****************************************************************
+****************************************************************/
+
+NTSTATUS kerberos_return_info3_from_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,
+                                       struct netr_SamInfo3 **info3)
 {
-       DATA_BLOB pac_data_blob = unwrap_pac(auth_data);
-       prs_struct ps;
-       PAC_DATA *pac_data;
-
-       DEBUG(5,("dump_pac_data\n"));
-       prs_init(&ps, pac_data_blob.length, ctx, UNMARSHALL);
-       prs_copy_data_in(&ps, (char *)pac_data_blob.data, pac_data_blob.length);
-       prs_set_offset(&ps, 0);
-
-       data_blob_free(&pac_data_blob);
-
-       pac_data = TALLOC_ZERO_P(ctx, PAC_DATA);
-       pac_io_pac_data("pac data", pac_data, &ps, 0);
+       NTSTATUS status;
+       struct PAC_LOGON_INFO *logon_info = NULL;
+
+       status = kerberos_return_pac_logon_info(mem_ctx,
+                                               name,
+                                               pass,
+                                               time_offset,
+                                               expire_time,
+                                               renew_till_time,
+                                               cache_name,
+                                               request_pac,
+                                               add_netbios_addr,
+                                               renewable_time,
+                                               impersonate_princ_s,
+                                               &logon_info);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
 
-       prs_mem_free(&ps);
+       *info3 = &logon_info->info3;
 
-       return pac_data;
+       return NT_STATUS_OK;
 }
-
 #endif