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
8 Copyright (C) Jim McDonough (jmcd@us.ibm.com) 2003
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 static void free_keytab(krb5_context context, krb5_keytab keytab)
34 ret = krb5_kt_close(context, keytab);
36 DEBUG(3, ("krb5_kt_close failed (%s)\n",
41 #ifdef HAVE_MEMORY_KEYTAB
42 static krb5_error_code create_keytab(krb5_context context,
43 krb5_principal host_princ,
46 krb5_enctype *enctypes,
50 krb5_keytab_entry entry;
56 DEBUG(10,("creating keytab: %s\n", keytab_name));
57 ret = krb5_kt_resolve(context, keytab_name, keytab);
61 if (!(key = (krb5_keyblock *)malloc(sizeof(*key)))) {
65 /* add keytab entries for all encryption types */
66 for ( i=0; enctypes[i]; i++ ) {
68 if (create_kerberos_key_from_string(context, host_princ, &password, key, enctypes[i])) {
72 entry.principal = host_princ;
74 /* this will have to be detected in configure...heimdal
75 calls it keyblock, MIT calls it key, but it does not
76 matter we are creating keytabs with MIT */
77 entry.keyblock = *key;
79 DEBUG(10,("adding keytab-entry for (%s) with encryption type (%d)\n",
80 host_princ_s, enctypes[i]));
81 ret = krb5_kt_add_entry(context, *keytab, &entry);
83 DEBUG(1,("adding entry to keytab failed (%s)\n",
85 free_keytab(context, *keytab);
89 krb5_free_keyblock(context, key);
95 static BOOL setup_keytab(krb5_context context,
96 krb5_principal host_princ,
99 krb5_enctype *enctypes,
102 char *keytab_name = NULL;
105 /* check if we have to setup a keytab - not currently enabled
106 I've put this in so that the else block below functions
107 the same way that it will when this code is turned on */
108 if (0 /* will later be *lp_keytab() */) {
110 /* use a file-keytab */
111 asprintf(&keytab_name, "%s:%s",
113 /* KRB5_KT_FILE_PREFIX, "FILE" or
114 "WRFILE" depending on HEeimdal or MIT */,
115 "" /* will later be lp_keytab() */);
117 DEBUG(10,("will use filebased keytab: %s\n", keytab_name));
118 ret = krb5_kt_resolve(context, keytab_name, keytab);
120 DEBUG(3,("cannot resolve keytab name %s (%s)\n",
122 error_message(ret)));
123 SAFE_FREE(keytab_name);
129 #if defined(HAVE_MEMORY_KEYTAB)
132 /* setup a in-memory-keytab */
133 asprintf(&keytab_name, "MEMORY:");
135 ret = create_keytab(context, host_princ, host_princ_s, password, enctypes,
136 keytab, keytab_name);
138 DEBUG(3,("unable to create MEMORY: keytab (%s)\n",
139 error_message(ret)));
140 SAFE_FREE(keytab_name);
145 SAFE_FREE(keytab_name);
151 verify an incoming ticket and parse out the principal name and
152 authorization_data if available
154 NTSTATUS ads_verify_ticket(const char *realm, const DATA_BLOB *ticket,
155 char **principal, DATA_BLOB *auth_data,
157 uint8 session_key[16])
159 NTSTATUS sret = NT_STATUS_LOGON_FAILURE;
160 krb5_context context = NULL;
161 krb5_auth_context auth_context = NULL;
162 krb5_keytab keytab = NULL;
164 krb5_ticket *tkt = NULL;
165 krb5_rcache rcache = NULL;
167 krb5_keyblock *key = NULL;
168 krb5_principal host_princ;
169 char *host_princ_s = NULL;
171 char *password_s = NULL;
173 krb5_enctype *enctypes = NULL;
175 krb5_address local_addr;
176 krb5_address remote_addr;
178 BOOL auth_ok = False;
181 ZERO_STRUCT(password);
182 ZERO_STRUCTP(auth_data);
183 ZERO_STRUCTP(ap_rep);
185 if (!secrets_init()) {
186 DEBUG(1,("ads_verify_ticket: secrets_init failed\n"));
187 return NT_STATUS_LOGON_FAILURE;
190 password_s = secrets_fetch_machine_password(lp_workgroup(), NULL, NULL);
192 DEBUG(1,("ads_verify_ticket: failed to fetch machine password\n"));
193 return NT_STATUS_LOGON_FAILURE;
196 password.data = password_s;
197 password.length = strlen(password_s);
199 ret = krb5_init_context(&context);
201 DEBUG(1,("ads_verify_ticket: krb5_init_context failed (%s)\n", error_message(ret)));
202 return NT_STATUS_LOGON_FAILURE;
205 ret = krb5_set_default_realm(context, realm);
207 DEBUG(1,("ads_verify_ticket: krb5_set_default_realm failed (%s)\n", error_message(ret)));
208 sret = NT_STATUS_LOGON_FAILURE;
212 /* This whole process is far more complex than I would
213 like. We have to go through all this to allow us to store
214 the secret internally, instead of using /etc/krb5.keytab */
216 ret = krb5_auth_con_init(context, &auth_context);
218 DEBUG(1,("ads_verify_ticket: krb5_auth_con_init failed (%s)\n", error_message(ret)));
219 sret = NT_STATUS_LOGON_FAILURE;
223 fstrcpy(myname, global_myname());
225 asprintf(&host_princ_s, "HOST/%s@%s", myname, lp_realm());
226 ret = krb5_parse_name(context, host_princ_s, &host_princ);
228 DEBUG(1,("ads_verify_ticket: krb5_parse_name(%s) failed (%s)\n",
229 host_princ_s, error_message(ret)));
230 sret = NT_STATUS_LOGON_FAILURE;
235 * JRA. We must set the rcache here. This will prevent replay attacks.
238 ret = krb5_get_server_rcache(context, krb5_princ_component(context, host_princ, 0), &rcache);
240 DEBUG(1,("ads_verify_ticket: krb5_get_server_rcache failed (%s)\n", error_message(ret)));
241 sret = NT_STATUS_LOGON_FAILURE;
245 ret = krb5_auth_con_setrcache(context, auth_context, rcache);
247 DEBUG(1,("ads_verify_ticket: krb5_auth_con_setrcache failed (%s)\n", error_message(ret)));
248 sret = NT_STATUS_LOGON_FAILURE;
252 /* CIFS doesn't use addresses in tickets. This would breat NAT. JRA */
254 if ((ret = get_kerberos_allowed_etypes(context, &enctypes))) {
255 DEBUG(1,("ads_verify_ticket: krb5_get_permitted_enctypes failed (%s)\n",
256 error_message(ret)));
257 sret = NT_STATUS_LOGON_FAILURE;
261 /* Lock a mutex surrounding the replay as there is no locking in the MIT krb5
262 * code surrounding the replay cache... */
264 if (!grab_server_mutex("replay cache mutex")) {
265 DEBUG(1,("ads_verify_ticket: unable to protect replay cache with mutex.\n"));
266 sret = NT_STATUS_LOGON_FAILURE;
270 if (!setup_keytab(context, host_princ, host_princ_s, password,
271 enctypes, &keytab)) {
272 DEBUG(3,("ads_verify_ticket: unable to setup keytab\n"));
273 sret = NT_STATUS_LOGON_FAILURE;
277 /* We need to setup a auth context with each possible encoding type in turn. */
278 for (i=0;enctypes[i];i++) {
279 if (!(key = (krb5_keyblock *)malloc(sizeof(*key)))) {
280 sret = NT_STATUS_NO_MEMORY;
284 if (create_kerberos_key_from_string(context, host_princ, &password, key, enctypes[i])) {
288 krb5_auth_con_setuseruserkey(context, auth_context, key);
290 krb5_free_keyblock(context, key);
292 packet.length = ticket->length;
293 packet.data = (krb5_pointer)ticket->data;
295 if (!(ret = krb5_rd_req(context, &auth_context, &packet,
296 #ifdef HAVE_MEMORY_KEYTAB
301 keytab, NULL, &tkt))) {
302 DEBUG(10,("ads_verify_ticket: enc type [%u] decrypted message !\n",
303 (unsigned int)enctypes[i] ));
308 DEBUG((ret != KRB5_BAD_ENCTYPE) ? 3 : 10,
309 ("ads_verify_ticket: enc type [%u] failed to decrypt with error %s\n",
310 (unsigned int)enctypes[i], error_message(ret)));
313 release_server_mutex();
316 DEBUG(3,("ads_verify_ticket: krb5_rd_req with auth failed (%s)\n",
317 error_message(ret)));
318 sret = NT_STATUS_LOGON_FAILURE;
322 ret = krb5_mk_rep(context, auth_context, &packet);
324 DEBUG(3,("ads_verify_ticket: Failed to generate mutual authentication reply (%s)\n",
325 error_message(ret)));
326 sret = NT_STATUS_LOGON_FAILURE;
330 *ap_rep = data_blob(packet.data, packet.length);
333 get_krb5_smb_session_key(context, auth_context, session_key, True);
334 #ifdef DEBUG_PASSWORD
335 DEBUG(10,("SMB session key (from ticket) follows:\n"));
336 dump_data(10, session_key, 16);
340 file_save("/tmp/ticket.dat", ticket->data, ticket->length);
343 get_auth_data_from_tkt(auth_data, tkt);
346 TALLOC_CTX *ctx = talloc_init("pac data");
347 decode_pac_data(auth_data, ctx);
352 if (tkt->enc_part2) {
353 file_save("/tmp/authdata.dat",
354 tkt->enc_part2->authorization_data[0]->contents,
355 tkt->enc_part2->authorization_data[0]->length);
360 /* get rid of all resources associated with the keytab */
361 if (keytab) free_keytab(context, keytab);
363 if ((ret = krb5_unparse_name(context, get_principal_from_tkt(tkt),
365 DEBUG(3,("ads_verify_ticket: krb5_unparse_name failed (%s)\n",
366 error_message(ret)));
367 sret = NT_STATUS_LOGON_FAILURE;
375 if (!NT_STATUS_IS_OK(sret))
376 data_blob_free(auth_data);
378 if (!NT_STATUS_IS_OK(sret))
379 data_blob_free(ap_rep);
381 krb5_free_principal(context, host_princ);
383 krb5_free_ticket(context, tkt);
384 free_kerberos_etypes(context, enctypes);
385 SAFE_FREE(password_s);
386 SAFE_FREE(host_princ_s);
389 krb5_auth_con_free(context, auth_context);
392 krb5_free_context(context);
397 #endif /* HAVE_KRB5 */