2 Unix SMB/CIFS implementation.
3 kerberos utility library
4 Copyright (C) Andrew Tridgell 2001
5 Copyright (C) Remus Koos 2001
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 verify an incoming ticket and parse out the principal name and
29 authorization_data if available
31 NTSTATUS ads_verify_ticket(ADS_STRUCT *ads, const DATA_BLOB *ticket,
32 char **principal, DATA_BLOB *auth_data)
35 krb5_auth_context auth_context = NULL;
36 krb5_keytab keytab = NULL;
38 krb5_ticket *tkt = NULL;
41 krb5_principal host_princ;
47 if (!secrets_init()) {
48 DEBUG(1,("secrets_init failed\n"));
49 return NT_STATUS_LOGON_FAILURE;
52 password_s = secrets_fetch_machine_password();
54 DEBUG(1,("failed to fetch machine password\n"));
55 return NT_STATUS_LOGON_FAILURE;
58 password.data = password_s;
59 password.length = strlen(password_s);
61 ret = krb5_init_context(&context);
63 DEBUG(1,("krb5_init_context failed (%s)\n", error_message(ret)));
64 return NT_STATUS_LOGON_FAILURE;
67 ret = krb5_set_default_realm(context, ads->auth.realm);
69 DEBUG(1,("krb5_set_default_realm failed (%s)\n", error_message(ret)));
71 return NT_STATUS_LOGON_FAILURE;
74 /* this whole process is far more complex than I would
75 like. We have to go through all this to allow us to store
76 the secret internally, instead of using /etc/krb5.keytab */
77 ret = krb5_auth_con_init(context, &auth_context);
79 DEBUG(1,("krb5_auth_con_init failed (%s)\n", error_message(ret)));
80 return NT_STATUS_LOGON_FAILURE;
83 fstrcpy(myname, global_myname());
85 asprintf(&host_princ_s, "HOST/%s@%s", myname, lp_realm());
86 ret = krb5_parse_name(context, host_princ_s, &host_princ);
88 DEBUG(1,("krb5_parse_name(%s) failed (%s)\n", host_princ_s, error_message(ret)));
89 return NT_STATUS_LOGON_FAILURE;
92 if (!(key = (krb5_keyblock *)malloc(sizeof(*key)))) {
93 return NT_STATUS_NO_MEMORY;
96 if (create_kerberos_key_from_string(context, host_princ, &password, key)) {
98 return NT_STATUS_LOGON_FAILURE;
101 krb5_auth_con_setuseruserkey(context, auth_context, key);
103 packet.length = ticket->length;
104 packet.data = (krb5_pointer)ticket->data;
107 file_save("/tmp/ticket.dat", ticket->data, ticket->length);
110 if ((ret = krb5_rd_req(context, &auth_context, &packet,
111 NULL, keytab, NULL, &tkt))) {
112 DEBUG(3,("krb5_rd_req with auth failed (%s)\n",
113 error_message(ret)));
114 return NT_STATUS_LOGON_FAILURE;
117 if (tkt->enc_part2) {
118 *auth_data = data_blob(tkt->enc_part2->authorization_data[0]->contents,
119 tkt->enc_part2->authorization_data[0]->length);
123 if (tkt->enc_part2) {
124 file_save("/tmp/authdata.dat",
125 tkt->enc_part2->authorization_data[0]->contents,
126 tkt->enc_part2->authorization_data[0]->length);
130 if ((ret = krb5_unparse_name(context, tkt->enc_part2->client, principal))) {
131 DEBUG(3,("krb5_unparse_name failed (%s)\n",
132 error_message(ret)));
133 return NT_STATUS_LOGON_FAILURE;