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 we use a prompter to avoid a crash bug in the kerberos libs when
29 dealing with empty passwords
30 this prompter is just a string copy ...
32 static krb5_error_code
33 kerb_prompter(krb5_context ctx, void *data,
37 krb5_prompt prompts[])
39 if (num_prompts == 0) return 0;
41 memset(prompts[0].reply->data, 0, prompts[0].reply->length);
42 if (prompts[0].reply->length > 0) {
43 strncpy(prompts[0].reply->data, data, prompts[0].reply->length-1);
44 prompts[0].reply->length = strlen(prompts[0].reply->data);
50 simulate a kinit, putting the tgt in the default cache location
53 int kerberos_kinit_password(const char *principal, const char *password)
56 krb5_error_code code = 0;
61 if ((code = krb5_init_context(&ctx)))
64 if ((code = krb5_cc_default(ctx, &cc))) {
65 krb5_free_context(ctx);
69 if ((code = krb5_parse_name(ctx, principal, &me))) {
70 krb5_free_context(ctx);
74 if ((code = krb5_get_init_creds_password(ctx, &my_creds, me, NULL,
76 password, 0, NULL, NULL))) {
77 krb5_free_principal(ctx, me);
78 krb5_free_context(ctx);
82 if ((code = krb5_cc_initialize(ctx, cc, me))) {
83 krb5_free_cred_contents(ctx, &my_creds);
84 krb5_free_principal(ctx, me);
85 krb5_free_context(ctx);
89 if ((code = krb5_cc_store_cred(ctx, cc, &my_creds))) {
90 krb5_cc_close(ctx, cc);
91 krb5_free_cred_contents(ctx, &my_creds);
92 krb5_free_principal(ctx, me);
93 krb5_free_context(ctx);
97 krb5_cc_close(ctx, cc);
98 krb5_free_cred_contents(ctx, &my_creds);
99 krb5_free_principal(ctx, me);
100 krb5_free_context(ctx);
107 /* run kinit to setup our ccache */
108 int ads_kinit_password(ADS_STRUCT *ads)
113 if (!ads->user_name) {
114 /* by default use the machine account */
115 extern pstring global_myname;
117 fstrcpy(myname, global_myname);
119 asprintf(&ads->user_name, "HOST/%s", global_myname);
121 asprintf(&s, "%s@%s", ads->user_name, ads->realm);
122 ret = kerberos_kinit_password(s, ads->password);
125 DEBUG(0,("kerberos_kinit_password %s failed: %s\n",
126 s, error_message(ret)));
133 verify an incoming ticket and parse out the principal name and
134 authorization_data if available
136 NTSTATUS ads_verify_ticket(ADS_STRUCT *ads, const DATA_BLOB *ticket,
137 char **principal, DATA_BLOB *auth_data)
139 krb5_context context;
140 krb5_auth_context auth_context = NULL;
141 krb5_keytab keytab = NULL;
143 krb5_ticket *tkt = NULL;
145 krb5_encrypt_block eblock;
148 krb5_principal host_princ;
150 extern pstring global_myname;
155 if (!secrets_init()) {
156 DEBUG(1,("secrets_init failed\n"));
157 return NT_STATUS_LOGON_FAILURE;
160 password_s = secrets_fetch_machine_password();
162 DEBUG(1,("failed to fetch machine password\n"));
163 return NT_STATUS_LOGON_FAILURE;
166 password.data = password_s;
167 password.length = strlen(password_s);
169 ret = krb5_init_context(&context);
171 DEBUG(1,("krb5_init_context failed (%s)\n", error_message(ret)));
172 return NT_STATUS_LOGON_FAILURE;
175 ret = krb5_set_default_realm(context, ads->realm);
177 DEBUG(1,("krb5_set_default_realm failed (%s)\n", error_message(ret)));
179 return NT_STATUS_LOGON_FAILURE;
182 /* this whole process is far more complex than I would
183 like. We have to go through all this to allow us to store
184 the secret internally, instead of using /etc/krb5.keytab */
185 ret = krb5_auth_con_init(context, &auth_context);
187 DEBUG(1,("krb5_auth_con_init failed (%s)\n", error_message(ret)));
188 return NT_STATUS_LOGON_FAILURE;
191 fstrcpy(myname, global_myname);
193 asprintf(&host_princ_s, "HOST/%s@%s", myname, lp_realm());
194 ret = krb5_parse_name(context, host_princ_s, &host_princ);
196 DEBUG(1,("krb5_parse_name(%s) failed (%s)\n", host_princ_s, error_message(ret)));
197 return NT_STATUS_LOGON_FAILURE;
200 ret = krb5_principal2salt(context, host_princ, &salt);
202 DEBUG(1,("krb5_principal2salt failed (%s)\n", error_message(ret)));
203 return NT_STATUS_LOGON_FAILURE;
206 if (!(key = (krb5_keyblock *)malloc(sizeof(*key)))) {
207 return NT_STATUS_NO_MEMORY;
210 krb5_use_enctype(context, &eblock, ENCTYPE_DES_CBC_MD5);
212 ret = krb5_string_to_key(context, &eblock, key, &password, &salt);
214 DEBUG(1,("krb5_string_to_key failed (%s)\n", error_message(ret)));
215 return NT_STATUS_LOGON_FAILURE;
218 krb5_auth_con_setuseruserkey(context, auth_context, key);
220 packet.length = ticket->length;
221 packet.data = (krb5_pointer)ticket->data;
224 file_save("/tmp/ticket.dat", ticket->data, ticket->length);
227 if ((ret = krb5_rd_req(context, &auth_context, &packet,
228 NULL, keytab, NULL, &tkt))) {
229 DEBUG(3,("krb5_rd_req with auth failed (%s)\n",
230 error_message(ret)));
231 return NT_STATUS_LOGON_FAILURE;
234 if (tkt->enc_part2) {
235 *auth_data = data_blob(tkt->enc_part2->authorization_data[0]->contents,
236 tkt->enc_part2->authorization_data[0]->length);
240 if (tkt->enc_part2) {
241 file_save("/tmp/authdata.dat",
242 tkt->enc_part2->authorization_data[0]->contents,
243 tkt->enc_part2->authorization_data[0]->length);
247 if ((ret = krb5_unparse_name(context, tkt->enc_part2->client, principal))) {
248 DEBUG(3,("krb5_unparse_name failed (%s)\n",
249 error_message(ret)));
250 return NT_STATUS_LOGON_FAILURE;