2047f0e3b9b0a99e8d6017d3526fd0fdbe521f99
[samba.git] / source3 / libads / kerberos_verify.c
1 /*
2    Unix SMB/CIFS implementation.
3    kerberos utility library
4    Copyright (C) Andrew Tridgell 2001
5    Copyright (C) Remus Koos 2001
6    Copyright (C) Luke Howard 2003
7    Copyright (C) Guenther Deschner 2003, 2005
8    Copyright (C) Jim McDonough (jmcd@us.ibm.com) 2003
9    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005
10    Copyright (C) Jeremy Allison 2007
11
12    This program is free software; you can redistribute it and/or modify
13    it under the terms of the GNU General Public License as published by
14    the Free Software Foundation; either version 3 of the License, or
15    (at your option) any later version.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20    GNU General Public License for more details.
21
22    You should have received a copy of the GNU General Public License
23    along with this program.  If not, see <http://www.gnu.org/licenses/>.
24 */
25
26 #include "includes.h"
27 #include "smb_krb5.h"
28
29 #ifdef HAVE_KRB5
30
31 #if !defined(HAVE_KRB5_PRINC_COMPONENT)
32 const krb5_data *krb5_princ_component(krb5_context, krb5_principal, int );
33 #endif
34
35 static bool ads_dedicated_keytab_verify_ticket(krb5_context context,
36                                           krb5_auth_context auth_context,
37                                           const DATA_BLOB *ticket,
38                                           krb5_ticket **pp_tkt,
39                                           krb5_keyblock **keyblock,
40                                           krb5_error_code *perr)
41 {
42         krb5_error_code ret = 0;
43         bool auth_ok = false;
44         krb5_keytab keytab = NULL;
45         krb5_keytab_entry kt_entry;
46         krb5_ticket *dec_ticket = NULL;
47
48         krb5_data packet;
49         krb5_kvno kvno = 0;
50         krb5_enctype enctype;
51
52         *pp_tkt = NULL;
53         *keyblock = NULL;
54         *perr = 0;
55
56         ZERO_STRUCT(kt_entry);
57
58         ret = smb_krb5_open_keytab(context, lp_dedicated_keytab_file(), true,
59             &keytab);
60         if (ret) {
61                 DEBUG(1, ("smb_krb5_open_keytab failed (%s)\n",
62                         error_message(ret)));
63                 goto out;
64         }
65
66         packet.length = ticket->length;
67         packet.data = (char *)ticket->data;
68
69         ret = krb5_rd_req(context, &auth_context, &packet, NULL, keytab,
70             NULL, &dec_ticket);
71         if (ret) {
72                 DEBUG(0, ("krb5_rd_req failed (%s)\n", error_message(ret)));
73                 goto out;
74         }
75
76 #ifdef HAVE_ETYPE_IN_ENCRYPTEDDATA /* Heimdal */
77         enctype = dec_ticket->ticket.key.keytype;
78 #else /* MIT */
79         enctype = dec_ticket->enc_part.enctype;
80         kvno    = dec_ticket->enc_part.kvno;
81 #endif
82
83         /* Get the key for checking the pac signature */
84         ret = krb5_kt_get_entry(context, keytab, dec_ticket->server,
85                                 kvno, enctype, &kt_entry);
86         if (ret) {
87                 DEBUG(0, ("krb5_kt_get_entry failed (%s)\n",
88                           error_message(ret)));
89                 goto out;
90         }
91
92         ret = krb5_copy_keyblock(context, KRB5_KT_KEY(&kt_entry), keyblock);
93         smb_krb5_kt_free_entry(context, &kt_entry);
94
95         if (ret) {
96                 DEBUG(0, ("failed to copy key: %s\n",
97                           error_message(ret)));
98                 goto out;
99         }
100
101         auth_ok = true;
102         *pp_tkt = dec_ticket;
103         dec_ticket = NULL;
104
105   out:
106         if (dec_ticket)
107                 krb5_free_ticket(context, dec_ticket);
108
109         if (keytab)
110                 krb5_kt_close(context, keytab);
111
112         *perr = ret;
113         return auth_ok;
114 }
115
116 /******************************************************************************
117  Try to verify a ticket using the system keytab... the system keytab has
118  kvno -1 entries, so it's more like what microsoft does... see comment in
119  utils/net_ads.c in the ads_keytab_add_entry function for details.
120 ******************************************************************************/
121
122 static bool ads_keytab_verify_ticket(krb5_context context,
123                                         krb5_auth_context auth_context,
124                                         const DATA_BLOB *ticket,
125                                         krb5_ticket **pp_tkt,
126                                         krb5_keyblock **keyblock,
127                                         krb5_error_code *perr)
128 {
129         krb5_error_code ret = 0;
130         bool auth_ok = False;
131         krb5_keytab keytab = NULL;
132         krb5_kt_cursor kt_cursor;
133         krb5_keytab_entry kt_entry;
134         char *valid_princ_formats[7] = { NULL, NULL, NULL,
135                                          NULL, NULL, NULL, NULL };
136         char *entry_princ_s = NULL;
137         fstring my_name, my_fqdn;
138         int i;
139         int number_matched_principals = 0;
140         krb5_data packet;
141         int err;
142
143         *pp_tkt = NULL;
144         *keyblock = NULL;
145         *perr = 0;
146
147         /* Generate the list of principal names which we expect
148          * clients might want to use for authenticating to the file
149          * service.  We allow name$,{host,cifs}/{name,fqdn,name.REALM}. */
150
151         fstrcpy(my_name, global_myname());
152
153         my_fqdn[0] = '\0';
154         name_to_fqdn(my_fqdn, global_myname());
155
156         err = asprintf(&valid_princ_formats[0],
157                         "%s$@%s", my_name, lp_realm());
158         if (err == -1) {
159                 goto out;
160         }
161         err = asprintf(&valid_princ_formats[1],
162                         "host/%s@%s", my_name, lp_realm());
163         if (err == -1) {
164                 goto out;
165         }
166         err = asprintf(&valid_princ_formats[2],
167                         "host/%s@%s", my_fqdn, lp_realm());
168         if (err == -1) {
169                 goto out;
170         }
171         err = asprintf(&valid_princ_formats[3],
172                         "host/%s.%s@%s", my_name, lp_realm(), lp_realm());
173         if (err == -1) {
174                 goto out;
175         }
176         err = asprintf(&valid_princ_formats[4],
177                         "cifs/%s@%s", my_name, lp_realm());
178         if (err == -1) {
179                 goto out;
180         }
181         err = asprintf(&valid_princ_formats[5],
182                         "cifs/%s@%s", my_fqdn, lp_realm());
183         if (err == -1) {
184                 goto out;
185         }
186         err = asprintf(&valid_princ_formats[6],
187                         "cifs/%s.%s@%s", my_name, lp_realm(), lp_realm());
188         if (err == -1) {
189                 goto out;
190         }
191
192         ZERO_STRUCT(kt_entry);
193         ZERO_STRUCT(kt_cursor);
194
195         ret = smb_krb5_open_keytab(context, NULL, False, &keytab);
196         if (ret) {
197                 DEBUG(1, (__location__ ": smb_krb5_open_keytab failed (%s)\n",
198                           error_message(ret)));
199                 goto out;
200         }
201
202         /* Iterate through the keytab.  For each key, if the principal
203          * name case-insensitively matches one of the allowed formats,
204          * try verifying the ticket using that principal. */
205
206         ret = krb5_kt_start_seq_get(context, keytab, &kt_cursor);
207         if (ret) {
208                 DEBUG(1, (__location__ ": krb5_kt_start_seq_get failed (%s)\n",
209                           error_message(ret)));
210                 goto out;
211         }
212   
213         while (!auth_ok &&
214                (krb5_kt_next_entry(context, keytab,
215                                    &kt_entry, &kt_cursor) == 0)) {
216                 ret = smb_krb5_unparse_name(talloc_tos(), context,
217                                             kt_entry.principal,
218                                             &entry_princ_s);
219                 if (ret) {
220                         DEBUG(1, (__location__ ": smb_krb5_unparse_name "
221                                   "failed (%s)\n", error_message(ret)));
222                         goto out;
223                 }
224
225                 for (i = 0; i < ARRAY_SIZE(valid_princ_formats); i++) {
226
227                         if (!strequal(entry_princ_s, valid_princ_formats[i])) {
228                                 continue;
229                         }
230
231                         number_matched_principals++;
232                         packet.length = ticket->length;
233                         packet.data = (char *)ticket->data;
234                         *pp_tkt = NULL;
235
236                         ret = krb5_rd_req_return_keyblock_from_keytab(context,
237                                                 &auth_context, &packet,
238                                                 kt_entry.principal, keytab,
239                                                 NULL, pp_tkt, keyblock);
240
241                         if (ret) {
242                                 DEBUG(10, (__location__ ": krb5_rd_req_return"
243                                            "_keyblock_from_keytab(%s) "
244                                            "failed: %s\n", entry_princ_s,
245                                            error_message(ret)));
246
247                                 /* workaround for MIT:
248                                 * as krb5_ktfile_get_entry will explicitly
249                                 * close the krb5_keytab as soon as krb5_rd_req
250                                 * has successfully decrypted the ticket but the
251                                 * ticket is not valid yet (due to clockskew)
252                                 * there is no point in querying more keytab
253                                 * entries - Guenther */
254
255                                 if (ret == KRB5KRB_AP_ERR_TKT_NYV ||
256                                     ret == KRB5KRB_AP_ERR_TKT_EXPIRED ||
257                                     ret == KRB5KRB_AP_ERR_SKEW) {
258                                         break;
259                                 }
260                         } else {
261                                 DEBUG(3, (__location__ ": krb5_rd_req_return"
262                                           "_keyblock_from_keytab succeeded "
263                                           "for principal %s\n",
264                                           entry_princ_s));
265                                 auth_ok = True;
266                                 break;
267                         }
268                 }
269
270                 /* Free the name we parsed. */
271                 TALLOC_FREE(entry_princ_s);
272
273                 /* Free the entry we just read. */
274                 smb_krb5_kt_free_entry(context, &kt_entry);
275                 ZERO_STRUCT(kt_entry);
276         }
277         krb5_kt_end_seq_get(context, keytab, &kt_cursor);
278
279         ZERO_STRUCT(kt_cursor);
280
281 out:
282
283         for (i = 0; i < ARRAY_SIZE(valid_princ_formats); i++) {
284                 SAFE_FREE(valid_princ_formats[i]);
285         }
286
287         if (!auth_ok) {
288                 if (!number_matched_principals) {
289                         DEBUG(3, (__location__ ": no keytab principals "
290                                   "matched expected file service name.\n"));
291                 } else {
292                         DEBUG(3, (__location__ ": krb5_rd_req failed for "
293                                   "all %d matched keytab principals\n",
294                                   number_matched_principals));
295                 }
296         }
297
298         SAFE_FREE(entry_princ_s);
299
300         {
301                 krb5_keytab_entry zero_kt_entry;
302                 ZERO_STRUCT(zero_kt_entry);
303                 if (memcmp(&zero_kt_entry, &kt_entry,
304                            sizeof(krb5_keytab_entry))) {
305                         smb_krb5_kt_free_entry(context, &kt_entry);
306                 }
307         }
308
309         {
310                 krb5_kt_cursor zero_csr;
311                 ZERO_STRUCT(zero_csr);
312                 if ((memcmp(&kt_cursor, &zero_csr,
313                             sizeof(krb5_kt_cursor)) != 0) && keytab) {
314                         krb5_kt_end_seq_get(context, keytab, &kt_cursor);
315                 }
316         }
317
318         if (keytab) {
319                 krb5_kt_close(context, keytab);
320         }
321         *perr = ret;
322         return auth_ok;
323 }
324
325 /*****************************************************************************
326  Try to verify a ticket using the secrets.tdb.
327 ******************************************************************************/
328
329 static krb5_error_code ads_secrets_verify_ticket(krb5_context context,
330                                                 krb5_auth_context auth_context,
331                                                 krb5_principal host_princ,
332                                                 const DATA_BLOB *ticket,
333                                                 krb5_ticket **pp_tkt,
334                                                 krb5_keyblock **keyblock,
335                                                 krb5_error_code *perr)
336 {
337         krb5_error_code ret = 0;
338         bool auth_ok = False;
339         bool cont = true;
340         char *password_s = NULL;
341         /* Let's make some room for 2 password (old and new)*/
342         krb5_data passwords[2];
343         krb5_enctype enctypes[] = {
344 #if defined(ENCTYPE_ARCFOUR_HMAC)
345                 ENCTYPE_ARCFOUR_HMAC,
346 #endif
347                 ENCTYPE_DES_CBC_CRC,
348                 ENCTYPE_DES_CBC_MD5,
349                 ENCTYPE_NULL
350         };
351         krb5_data packet;
352         int i, j;
353
354         *pp_tkt = NULL;
355         *keyblock = NULL;
356         *perr = 0;
357
358         ZERO_STRUCT(passwords);
359
360         if (!secrets_init()) {
361                 DEBUG(1,("ads_secrets_verify_ticket: secrets_init failed\n"));
362                 *perr = KRB5_CONFIG_CANTOPEN;
363                 return False;
364         }
365
366         password_s = secrets_fetch_machine_password(lp_workgroup(),
367                                                     NULL, NULL);
368         if (!password_s) {
369                 DEBUG(1,(__location__ ": failed to fetch machine password\n"));
370                 *perr = KRB5_LIBOS_CANTREADPWD;
371                 return False;
372         }
373
374         passwords[0].data = password_s;
375         passwords[0].length = strlen(password_s);
376
377         password_s = secrets_fetch_prev_machine_password(lp_workgroup());
378         if (password_s) {
379                 DEBUG(10, (__location__ ": found previous password\n"));
380                 passwords[1].data = password_s;
381                 passwords[1].length = strlen(password_s);
382         }
383
384         /* CIFS doesn't use addresses in tickets. This would break NAT. JRA */
385
386         packet.length = ticket->length;
387         packet.data = (char *)ticket->data;
388
389         /* We need to setup a auth context with each possible encoding type
390          * in turn. */
391         for (j=0; j<2 && passwords[j].length; j++) {
392
393                 for (i=0;enctypes[i];i++) {
394                         krb5_keyblock *key = NULL;
395
396                         if (!(key = SMB_MALLOC_P(krb5_keyblock))) {
397                                 ret = ENOMEM;
398                                 goto out;
399                         }
400
401                         if (create_kerberos_key_from_string(context,
402                                                 host_princ, &passwords[j],
403                                                 key, enctypes[i], false)) {
404                                 SAFE_FREE(key);
405                                 continue;
406                         }
407
408                         krb5_auth_con_setuseruserkey(context,
409                                                         auth_context, key);
410
411                         if (!(ret = krb5_rd_req(context, &auth_context,
412                                                 &packet, NULL, NULL,
413                                                 NULL, pp_tkt))) {
414                                 DEBUG(10, (__location__ ": enc type [%u] "
415                                            "decrypted message !\n",
416                                            (unsigned int)enctypes[i]));
417                                 auth_ok = True;
418                                 cont = false;
419                                 krb5_copy_keyblock(context, key, keyblock);
420                                 krb5_free_keyblock(context, key);
421                                 break;
422                         }
423
424                         DEBUG((ret != KRB5_BAD_ENCTYPE) ? 3 : 10,
425                                 (__location__ ": enc type [%u] failed to "
426                                  "decrypt with error %s\n",
427                                  (unsigned int)enctypes[i],
428                                  error_message(ret)));
429
430                         /* successfully decrypted but ticket is just not
431                          * valid at the moment */
432                         if (ret == KRB5KRB_AP_ERR_TKT_NYV ||
433                             ret == KRB5KRB_AP_ERR_TKT_EXPIRED ||
434                             ret == KRB5KRB_AP_ERR_SKEW) {
435                                 krb5_free_keyblock(context, key);
436                                 cont = false;
437                                 break;
438                         }
439
440                         krb5_free_keyblock(context, key);
441                 }
442                 if (!cont) {
443                         /* If we found a valid pass then no need to try
444                          * the next one or we have invalid ticket so no need
445                          * to try next password*/
446                         break;
447                 }
448         }
449
450  out:
451         SAFE_FREE(passwords[0].data);
452         SAFE_FREE(passwords[1].data);
453         *perr = ret;
454         return auth_ok;
455 }
456
457 /*****************************************************************************
458  Verify an incoming ticket and parse out the principal name and
459  authorization_data if available.
460 ******************************************************************************/
461
462 NTSTATUS ads_verify_ticket(TALLOC_CTX *mem_ctx,
463                            const char *realm,
464                            time_t time_offset,
465                            const DATA_BLOB *ticket,
466                            char **principal,
467                            struct PAC_LOGON_INFO **logon_info,
468                            DATA_BLOB *ap_rep,
469                            DATA_BLOB *session_key,
470                            bool use_replay_cache)
471 {
472         NTSTATUS sret = NT_STATUS_LOGON_FAILURE;
473         NTSTATUS pac_ret;
474         DATA_BLOB auth_data;
475         krb5_context context = NULL;
476         krb5_auth_context auth_context = NULL;
477         krb5_data packet;
478         krb5_ticket *tkt = NULL;
479         krb5_rcache rcache = NULL;
480         krb5_keyblock *keyblock = NULL;
481         time_t authtime;
482         krb5_error_code ret = 0;
483         int flags = 0;
484         krb5_principal host_princ = NULL;
485         krb5_const_principal client_principal = NULL;
486         char *host_princ_s = NULL;
487         bool auth_ok = False;
488         bool got_auth_data = False;
489         struct named_mutex *mutex = NULL;
490
491         ZERO_STRUCT(packet);
492         ZERO_STRUCT(auth_data);
493
494         *principal = NULL;
495         *logon_info = NULL;
496         *ap_rep = data_blob_null;
497         *session_key = data_blob_null;
498
499         initialize_krb5_error_table();
500         ret = krb5_init_context(&context);
501         if (ret) {
502                 DEBUG(1, (__location__ ": krb5_init_context failed (%s)\n",
503                           error_message(ret)));
504                 return NT_STATUS_LOGON_FAILURE;
505         }
506
507         if (time_offset != 0) {
508                 krb5_set_real_time(context, time(NULL) + time_offset, 0);
509         }
510
511         ret = krb5_set_default_realm(context, realm);
512         if (ret) {
513                 DEBUG(1, (__location__ ": krb5_set_default_realm "
514                           "failed (%s)\n", error_message(ret)));
515                 goto out;
516         }
517
518         /* This whole process is far more complex than I would
519            like. We have to go through all this to allow us to store
520            the secret internally, instead of using /etc/krb5.keytab */
521
522         ret = krb5_auth_con_init(context, &auth_context);
523         if (ret) {
524                 DEBUG(1, (__location__ ": krb5_auth_con_init failed (%s)\n",
525                           error_message(ret)));
526                 goto out;
527         }
528
529         krb5_auth_con_getflags( context, auth_context, &flags );
530         if ( !use_replay_cache ) {
531                 /* Disable default use of a replay cache */
532                 flags &= ~KRB5_AUTH_CONTEXT_DO_TIME;
533                 krb5_auth_con_setflags( context, auth_context, flags );
534         }
535
536         if (asprintf(&host_princ_s, "%s$", global_myname()) == -1) {
537                 goto out;
538         }
539
540         strlower_m(host_princ_s);
541         ret = smb_krb5_parse_name(context, host_princ_s, &host_princ);
542         if (ret) {
543                 DEBUG(1, (__location__ ": smb_krb5_parse_name(%s) "
544                           "failed (%s)\n", host_princ_s, error_message(ret)));
545                 goto out;
546         }
547
548
549         if (use_replay_cache) {
550
551                 /* Lock a mutex surrounding the replay as there is no
552                    locking in the MIT krb5 code surrounding the replay
553                    cache... */
554
555                 mutex = grab_named_mutex(talloc_tos(),
556                                          "replay cache mutex", 10);
557                 if (mutex == NULL) {
558                         DEBUG(1, (__location__ ": unable to protect replay "
559                                   "cache with mutex.\n"));
560                         ret = KRB5_CC_IO;
561                         goto out;
562                 }
563
564                 /* JRA. We must set the rcache here. This will prevent
565                    replay attacks. */
566
567                 ret = krb5_get_server_rcache(
568                                 context,
569                                 krb5_princ_component(context, host_princ, 0),
570                                 &rcache);
571                 if (ret) {
572                         DEBUG(1, (__location__ ": krb5_get_server_rcache "
573                                   "failed (%s)\n", error_message(ret)));
574                         goto out;
575                 }
576
577                 ret = krb5_auth_con_setrcache(context, auth_context, rcache);
578                 if (ret) {
579                         DEBUG(1, (__location__ ": krb5_auth_con_setrcache "
580                                   "failed (%s)\n", error_message(ret)));
581                         goto out;
582                 }
583         }
584
585         switch (lp_kerberos_method()) {
586         default:
587         case KERBEROS_VERIFY_SECRETS:
588                 auth_ok = ads_secrets_verify_ticket(context, auth_context,
589                     host_princ, ticket, &tkt, &keyblock, &ret);
590                 break;
591         case KERBEROS_VERIFY_SYSTEM_KEYTAB:
592                 auth_ok = ads_keytab_verify_ticket(context, auth_context,
593                     ticket, &tkt, &keyblock, &ret);
594                 break;
595         case KERBEROS_VERIFY_DEDICATED_KEYTAB:
596                 auth_ok = ads_dedicated_keytab_verify_ticket(context,
597                     auth_context, ticket, &tkt, &keyblock, &ret);
598                 break;
599         case KERBEROS_VERIFY_SECRETS_AND_KEYTAB:
600                 /* First try secrets.tdb and fallback to the krb5.keytab if
601                    necessary.  This is the pre 3.4 behavior when
602                    "use kerberos keytab" was true.*/
603                 auth_ok = ads_secrets_verify_ticket(context, auth_context,
604                     host_princ, ticket, &tkt, &keyblock, &ret);
605
606                 if (!auth_ok) {
607                         /* Only fallback if we failed to decrypt the ticket */
608                         if (ret != KRB5KRB_AP_ERR_TKT_NYV &&
609                             ret != KRB5KRB_AP_ERR_TKT_EXPIRED &&
610                             ret != KRB5KRB_AP_ERR_SKEW) {
611                                 auth_ok = ads_keytab_verify_ticket(context,
612                                     auth_context, ticket, &tkt, &keyblock,
613                                     &ret);
614                         }
615                 }
616                 break;
617         }
618
619         if (use_replay_cache) {
620                 TALLOC_FREE(mutex);
621 #if 0
622                 /* Heimdal leaks here, if we fix the leak, MIT crashes */
623                 if (rcache) {
624                         krb5_rc_close(context, rcache);
625                 }
626 #endif
627         }
628
629         if (!auth_ok) {
630                 DEBUG(3, (__location__ ": krb5_rd_req with auth "
631                           "failed (%s)\n", error_message(ret)));
632                 /* Try map the error return in case it's something like
633                  * a clock skew error.
634                  */
635                 sret = krb5_to_nt_status(ret);
636                 if (NT_STATUS_IS_OK(sret) ||
637                     NT_STATUS_EQUAL(sret,NT_STATUS_UNSUCCESSFUL)) {
638                         sret = NT_STATUS_LOGON_FAILURE;
639                 }
640                 DEBUG(10, (__location__ ": returning error %s\n",
641                            nt_errstr(sret) ));
642                 goto out;
643         }
644
645         authtime = get_authtime_from_tkt(tkt);
646         client_principal = get_principal_from_tkt(tkt);
647
648         ret = krb5_mk_rep(context, auth_context, &packet);
649         if (ret) {
650                 DEBUG(3, (__location__ ": Failed to generate mutual "
651                           "authentication reply (%s)\n", error_message(ret)));
652                 goto out;
653         }
654
655         *ap_rep = data_blob(packet.data, packet.length);
656         if (packet.data) {
657                 kerberos_free_data_contents(context, &packet);
658                 ZERO_STRUCT(packet);
659         }
660
661         get_krb5_smb_session_key(mem_ctx, context,
662                                  auth_context, session_key, true);
663         dump_data_pw("SMB session key (from ticket)\n",
664                      session_key->data, session_key->length);
665
666 #if 0
667         file_save("/tmp/ticket.dat", ticket->data, ticket->length);
668 #endif
669
670         /* continue when no PAC is retrieved or we couldn't decode the PAC
671            (like accounts that have the UF_NO_AUTH_DATA_REQUIRED flag set, or
672            Kerberos tickets encrypted using a DES key) - Guenther */
673
674         got_auth_data = get_auth_data_from_tkt(mem_ctx, &auth_data, tkt);
675         if (!got_auth_data) {
676                 DEBUG(3, (__location__ ": did not retrieve auth data. "
677                           "continuing without PAC\n"));
678         }
679
680         if (got_auth_data) {
681                 struct PAC_DATA *pac_data;
682                 pac_ret = decode_pac_data(mem_ctx, &auth_data, context,
683                                           keyblock, client_principal,
684                                           authtime, &pac_data);
685                 data_blob_free(&auth_data);
686                 if (!NT_STATUS_IS_OK(pac_ret)) {
687                         DEBUG(3, (__location__ ": failed to decode "
688                                   "PAC_DATA: %s\n", nt_errstr(pac_ret)));
689                 } else {
690                         uint32_t i;
691                         for (i = 0; i < pac_data->num_buffers; i++) {
692
693                                 if (pac_data->buffers[i].type != PAC_TYPE_LOGON_INFO) {
694                                         continue;
695                                 }
696
697                                 *logon_info = pac_data->buffers[i].info->logon_info.info;
698                         }
699
700                         if (!*logon_info) {
701                                 DEBUG(1, ("correctly decoded PAC but found "
702                                           "no logon_info! "
703                                           "This should not happen\n"));
704                                 return NT_STATUS_INVALID_USER_BUFFER;
705                         }
706                 }
707         }
708
709 #if 0
710 #if defined(HAVE_KRB5_TKT_ENC_PART2)
711         /* MIT */
712         if (tkt->enc_part2) {
713                 file_save("/tmp/authdata.dat",
714                           tkt->enc_part2->authorization_data[0]->contents,
715                           tkt->enc_part2->authorization_data[0]->length);
716         }
717 #else
718         /* Heimdal */
719         if (tkt->ticket.authorization_data) {
720                 file_save("/tmp/authdata.dat",
721                           tkt->ticket.authorization_data->val->ad_data.data,
722                           tkt->ticket.authorization_data->val->ad_data.length);
723         }
724 #endif
725 #endif
726
727         ret = smb_krb5_unparse_name(mem_ctx, context,
728                                     client_principal, principal);
729         if (ret) {
730                 DEBUG(3, (__location__ ": smb_krb5_unparse_name "
731                           "failed (%s)\n", error_message(ret)));
732                 sret = NT_STATUS_LOGON_FAILURE;
733                 goto out;
734         }
735
736         sret = NT_STATUS_OK;
737
738 out:
739
740         TALLOC_FREE(mutex);
741
742         if (!NT_STATUS_IS_OK(sret)) {
743                 data_blob_free(&auth_data);
744         }
745
746         if (!NT_STATUS_IS_OK(sret)) {
747                 data_blob_free(ap_rep);
748         }
749
750         if (host_princ) {
751                 krb5_free_principal(context, host_princ);
752         }
753
754         if (keyblock) {
755                 krb5_free_keyblock(context, keyblock);
756         }
757
758         if (tkt != NULL) {
759                 krb5_free_ticket(context, tkt);
760         }
761
762         SAFE_FREE(host_princ_s);
763
764         if (auth_context) {
765                 krb5_auth_con_free(context, auth_context);
766         }
767
768         if (context) {
769                 krb5_free_context(context);
770         }
771
772         return sret;
773 }
774
775 #endif /* HAVE_KRB5 */