2 Unix SMB/Netbios implementation.
4 kerberos utility library
5 Copyright (C) Andrew Tridgell 2001
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 simulate a kinit, putting the tgt in the default cache location
30 int kerberos_kinit_password(const char *principal, const char *password)
33 krb5_error_code code = 0;
38 if ((code = krb5_init_context(&ctx)))
41 if ((code = krb5_cc_default(ctx, &cc))) {
42 krb5_free_context(ctx);
46 if ((code = krb5_parse_name(ctx, principal, &me))) {
47 krb5_free_context(ctx);
51 if ((code = krb5_get_init_creds_password(ctx, &my_creds, me, password, NULL,
52 NULL, 0, NULL, NULL))) {
53 krb5_free_principal(ctx, me);
54 krb5_free_context(ctx);
58 if ((code = krb5_cc_initialize(ctx, cc, me))) {
59 krb5_free_cred_contents(ctx, &my_creds);
60 krb5_free_principal(ctx, me);
61 krb5_free_context(ctx);
65 if ((code = krb5_cc_store_cred(ctx, cc, &my_creds))) {
66 krb5_cc_close(ctx, cc);
67 krb5_free_cred_contents(ctx, &my_creds);
68 krb5_free_principal(ctx, me);
69 krb5_free_context(ctx);
73 krb5_cc_close(ctx, cc);
74 krb5_free_cred_contents(ctx, &my_creds);
75 krb5_free_principal(ctx, me);
76 krb5_free_context(ctx);
83 /* run kinit to setup our ccache */
84 int ads_kinit_password(ADS_STRUCT *ads)
88 extern pstring global_myname;
91 /* we don't want this to affect the users ccache */
92 setenv("KRB5CCNAME", lock_path("winbindd_ccache"), 1);
94 fstrcpy(myname, global_myname);
96 asprintf(&s, "HOST/%s@%s", global_myname, ads->realm);
97 ret = kerberos_kinit_password(s, ads->password);
100 DEBUG(1,("kerberos_kinit_password failed: %s\n", error_message(ret)));
106 verify an incoming ticket and parse out the principal name and
107 authorization_data if available
109 NTSTATUS ads_verify_ticket(ADS_STRUCT *ads, const DATA_BLOB *ticket,
110 char **principal, DATA_BLOB *auth_data)
112 krb5_context context;
113 krb5_auth_context auth_context = NULL;
114 krb5_keytab keytab = NULL;
116 krb5_ticket *tkt = NULL;
118 krb5_encrypt_block eblock;
121 krb5_principal host_princ;
123 extern pstring global_myname;
128 if (!secrets_init()) {
129 DEBUG(1,("secrets_init failed\n"));
130 return NT_STATUS_LOGON_FAILURE;
133 password_s = secrets_fetch_machine_password();
135 DEBUG(1,("failed to fetch machine password\n"));
136 return NT_STATUS_LOGON_FAILURE;
139 password.data = password_s;
140 password.length = strlen(password_s);
142 ret = krb5_init_context(&context);
144 DEBUG(1,("krb5_init_context failed (%s)\n", error_message(ret)));
145 return NT_STATUS_LOGON_FAILURE;
148 ret = krb5_set_default_realm(context, ads->realm);
150 DEBUG(1,("krb5_set_default_realm failed (%s)\n", error_message(ret)));
152 return NT_STATUS_LOGON_FAILURE;
155 /* this whole process is far more complex than I would
156 like. We have to go through all this to allow us to store
157 the secret internally, instead of using /etc/krb5.keytab */
158 ret = krb5_auth_con_init(context, &auth_context);
160 DEBUG(1,("krb5_auth_con_init failed (%s)\n", error_message(ret)));
161 return NT_STATUS_LOGON_FAILURE;
164 fstrcpy(myname, global_myname);
166 asprintf(&host_princ_s, "HOST/%s@%s", myname, lp_realm());
167 ret = krb5_parse_name(context, host_princ_s, &host_princ);
169 DEBUG(1,("krb5_parse_name(%s) failed (%s)\n", host_princ_s, error_message(ret)));
170 return NT_STATUS_LOGON_FAILURE;
173 ret = krb5_principal2salt(context, host_princ, &salt);
175 DEBUG(1,("krb5_principal2salt failed (%s)\n", error_message(ret)));
176 return NT_STATUS_LOGON_FAILURE;
179 if (!(key = (krb5_keyblock *)malloc(sizeof(*key)))) {
180 return NT_STATUS_NO_MEMORY;
183 krb5_use_enctype(context, &eblock, ENCTYPE_DES_CBC_MD5);
185 ret = krb5_string_to_key(context, &eblock, key, &password, &salt);
187 DEBUG(1,("krb5_string_to_key failed (%s)\n", error_message(ret)));
188 return NT_STATUS_LOGON_FAILURE;
191 krb5_auth_con_setuseruserkey(context, auth_context, key);
193 packet.length = ticket->length;
194 packet.data = (krb5_pointer)ticket->data;
197 file_save("/tmp/ticket.dat", ticket->data, ticket->length);
200 if ((ret = krb5_rd_req(context, &auth_context, &packet,
201 NULL, keytab, NULL, &tkt))) {
202 DEBUG(3,("krb5_rd_req with auth failed (%s)\n",
203 error_message(ret)));
204 return NT_STATUS_LOGON_FAILURE;
207 if (tkt->enc_part2) {
208 *auth_data = data_blob(tkt->enc_part2->authorization_data[0]->contents,
209 tkt->enc_part2->authorization_data[0]->length);
213 if (tkt->enc_part2) {
214 file_save("/tmp/authdata.dat",
215 tkt->enc_part2->authorization_data[0]->contents,
216 tkt->enc_part2->authorization_data[0]->length);
220 if ((ret = krb5_unparse_name(context, tkt->enc_part2->client, principal))) {
221 DEBUG(3,("krb5_unparse_name failed (%s)\n",
222 error_message(ret)));
223 return NT_STATUS_LOGON_FAILURE;