2 Unix SMB/CIFS implementation.
3 kerberos utility library
4 Copyright (C) Andrew Tridgell 2001
5 Copyright (C) Remus Koos 2001
6 Copyright (C) Nalin Dahyabhai 2004.
7 Copyright (C) Jeremy Allison 2004.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "system/kerberos.h"
26 #include "libcli/auth/kerberos.h"
27 #include "system/time.h"
32 #define LIBADS_CCACHE_NAME "MEMORY:libads"
35 we use a prompter to avoid a crash bug in the kerberos libs when
36 dealing with empty passwords
37 this prompter is just a string copy ...
39 static krb5_error_code
40 kerb_prompter(krb5_context ctx, void *data,
44 krb5_prompt prompts[])
46 if (num_prompts == 0) return 0;
48 memset(prompts[0].reply->data, '\0', prompts[0].reply->length);
49 if (prompts[0].reply->length > 0) {
51 strncpy(prompts[0].reply->data, data, prompts[0].reply->length-1);
52 prompts[0].reply->length = strlen(prompts[0].reply->data);
54 prompts[0].reply->length = 0;
61 simulate a kinit, putting the tgt in the given credentials cache.
62 Orignally by remus@snapserver.com
64 int kerberos_kinit_password_cc(krb5_context ctx, krb5_ccache cc,
65 const char *principal, const char *password,
66 time_t *expire_time, time_t *kdc_time)
68 krb5_error_code code = 0;
71 krb5_get_init_creds_opt options;
73 if ((code = krb5_parse_name(ctx, principal, &me))) {
77 krb5_get_init_creds_opt_init(&options);
79 if ((code = krb5_get_init_creds_password(ctx, &my_creds, me, password,
81 NULL, 0, NULL, &options))) {
82 krb5_free_principal(ctx, me);
86 if ((code = krb5_cc_initialize(ctx, cc, me))) {
87 krb5_free_cred_contents(ctx, &my_creds);
88 krb5_free_principal(ctx, me);
92 if ((code = krb5_cc_store_cred(ctx, cc, &my_creds))) {
93 krb5_free_cred_contents(ctx, &my_creds);
94 krb5_free_principal(ctx, me);
99 *expire_time = (time_t) my_creds.times.endtime;
103 *kdc_time = (time_t) my_creds.times.starttime;
106 krb5_free_cred_contents(ctx, &my_creds);
107 krb5_free_principal(ctx, me);
113 simulate a kinit, putting the tgt in the given credentials cache.
114 If cache_name == NULL place in default cache location.
116 Orignally by remus@snapserver.com
118 int kerberos_kinit_password(const char *principal,
119 const char *password,
122 const char *cache_name,
126 krb5_context ctx = NULL;
127 krb5_ccache cc = NULL;
129 if ((code = krb5_init_context(&ctx)))
132 if (time_offset != 0) {
133 krb5_set_real_time(ctx, time(NULL) + time_offset, 0);
136 if ((code = krb5_cc_resolve(ctx, cache_name ?
137 cache_name : krb5_cc_default_name(ctx), &cc))) {
138 krb5_free_context(ctx);
142 code = kerberos_kinit_password_cc(ctx, cc, principal, password, expire_time, kdc_time);
144 krb5_cc_close(ctx, cc);
145 krb5_free_context(ctx);
150 /* run kinit to setup our ccache */
151 int ads_kinit_password(ADS_STRUCT *ads)
156 if (asprintf(&s, "%s@%s", ads->auth.user_name, ads->auth.realm) == -1) {
157 return KRB5_CC_NOMEM;
160 if (!ads->auth.password) {
161 return KRB5_LIBOS_CANTREADPWD;
164 ret = kerberos_kinit_password(s, ads->auth.password, ads->auth.time_offset,
165 &ads->auth.expire, NULL, NULL);
168 DEBUG(0,("kerberos_kinit_password %s failed: %s\n",
169 s, error_message(ret)));
175 int ads_kdestroy(const char *cc_name)
177 krb5_error_code code;
178 krb5_context ctx = NULL;
179 krb5_ccache cc = NULL;
181 if ((code = krb5_init_context (&ctx))) {
182 DEBUG(3, ("ads_kdestroy: kdb5_init_context failed: %s\n",
183 error_message(code)));
188 if ((code = krb5_cc_default(ctx, &cc))) {
189 krb5_free_context(ctx);
193 if ((code = krb5_cc_resolve(ctx, cc_name, &cc))) {
194 DEBUG(3, ("ads_kdestroy: krb5_cc_resolve failed: %s\n",
195 error_message(code)));
196 krb5_free_context(ctx);
201 if ((code = krb5_cc_destroy (ctx, cc))) {
202 DEBUG(3, ("ads_kdestroy: krb5_cc_destroy failed: %s\n",
203 error_message(code)));
206 krb5_free_context (ctx);
210 /************************************************************************
211 Routine to fetch the salting principal for a service. Active
212 Directory may use a non-obvious principal name to generate the salt
213 when it determines the key to use for encrypting tickets for a service,
214 and hopefully we detected that when we joined the domain.
215 ************************************************************************/
217 static char *kerberos_secrets_fetch_salting_principal(const char *service, int enctype)
222 asprintf(&key, "%s/%s/enctype=%d", SECRETS_SALTING_PRINCIPAL, service, enctype);
226 ret = (char *)secrets_fetch(key, NULL);
232 /************************************************************************
233 Routine to get the salting principal for this service. Active
234 Directory may use a non-obvious principal name to generate the salt
235 when it determines the key to use for encrypting tickets for a service,
236 and hopefully we detected that when we joined the domain.
237 Caller must free if return is not null.
238 ************************************************************************/
240 krb5_principal kerberos_fetch_salt_princ_for_host_princ(krb5_context context,
241 krb5_principal host_princ,
244 char *unparsed_name = NULL, *salt_princ_s = NULL;
245 krb5_principal ret_princ = NULL;
247 if (krb5_unparse_name(context, host_princ, &unparsed_name) != 0) {
248 return (krb5_principal)NULL;
251 if ((salt_princ_s = kerberos_secrets_fetch_salting_principal(unparsed_name, enctype)) == NULL) {
252 krb5_free_unparsed_name(context, unparsed_name);
253 return (krb5_principal)NULL;
256 if (krb5_parse_name(context, salt_princ_s, &ret_princ) != 0) {
257 krb5_free_unparsed_name(context, unparsed_name);
258 SAFE_FREE(salt_princ_s);
259 return (krb5_principal)NULL;
261 krb5_free_unparsed_name(context, unparsed_name);
262 SAFE_FREE(salt_princ_s);
266 /************************************************************************
267 Routine to set the salting principal for this service. Active
268 Directory may use a non-obvious principal name to generate the salt
269 when it determines the key to use for encrypting tickets for a service,
270 and hopefully we detected that when we joined the domain.
271 Setting principal to NULL deletes this entry.
272 ************************************************************************/
274 BOOL kerberos_secrets_store_salting_principal(const char *service,
276 const char *principal)
280 krb5_context context = NULL;
281 krb5_principal princ = NULL;
282 char *princ_s = NULL;
283 char *unparsed_name = NULL;
285 krb5_init_context(&context);
289 if (strchr_m(service, '@')) {
290 asprintf(&princ_s, "%s", service);
292 asprintf(&princ_s, "%s@%s", service, lp_realm());
295 if (krb5_parse_name(context, princ_s, &princ) != 0) {
299 if (krb5_unparse_name(context, princ, &unparsed_name) != 0) {
303 asprintf(&key, "%s/%s/enctype=%d", SECRETS_SALTING_PRINCIPAL, unparsed_name, enctype);
309 if ((principal != NULL) && (strlen(principal) > 0)) {
310 ret = secrets_store(key, principal, strlen(principal) + 1);
312 ret = secrets_delete(key);
322 krb5_free_unparsed_name(context, unparsed_name);
325 krb5_free_context(context);
331 /************************************************************************
332 Routine to get initial credentials as a service ticket for the local machine.
333 Returns a buffer initialized with krb5_mk_req_extended.
334 ************************************************************************/
336 static krb5_error_code get_service_ticket(krb5_context ctx,
338 const char *service_principal,
342 krb5_creds creds, *new_creds = NULL;
343 char *service_s = NULL;
344 char *machine_account = NULL, *password = NULL;
346 krb5_auth_context auth_context = NULL;
347 krb5_error_code err = 0;
351 asprintf(&machine_account, "%s$@%s", lp_netbios_name(), lp_realm());
352 if (machine_account == NULL) {
355 password = secrets_fetch_machine_password(lp_workgroup());
356 if (password == NULL) {
359 if ((err = kerberos_kinit_password(machine_account, password, 0, NULL, LIBADS_CCACHE_NAME, NULL)) != 0) {
360 DEBUG(0,("get_service_ticket: kerberos_kinit_password %s@%s failed: %s\n",
363 error_message(err)));
367 /* Ok - the above call has gotten a TGT. Now we need to get a service
368 ticket to ourselves. */
370 /* Set up the enctype and client and server principal fields for krb5_get_credentials. */
371 kerberos_set_creds_enctype(&creds, enctype);
373 if ((err = krb5_cc_get_principal(ctx, ccache, &creds.client))) {
374 DEBUG(3, ("get_service_ticket: krb5_cc_get_principal failed: %s\n",
375 error_message(err)));
379 if (strchr_m(service_principal, '@')) {
380 asprintf(&service_s, "%s", service_principal);
382 asprintf(&service_s, "%s@%s", service_principal, lp_realm());
385 if ((err = krb5_parse_name(ctx, service_s, &creds.server))) {
386 DEBUG(0,("get_service_ticket: krb5_parse_name %s failed: %s\n",
387 service_s, error_message(err)));
391 if ((err = krb5_get_credentials(ctx, 0, ccache, &creds, &new_creds))) {
392 DEBUG(5,("get_service_ticket: krb5_get_credentials for %s enctype %d failed: %s\n",
393 service_s, enctype, error_message(err)));
397 memset(&in_data, '\0', sizeof(in_data));
398 if ((err = krb5_mk_req_extended(ctx, &auth_context, 0, &in_data,
399 new_creds, p_outbuf)) != 0) {
400 DEBUG(0,("get_service_ticket: krb5_mk_req_extended failed: %s\n",
401 error_message(err)));
408 krb5_auth_con_free(ctx, auth_context);
411 krb5_free_creds(ctx, new_creds);
414 krb5_free_principal(ctx, creds.server);
417 krb5_free_principal(ctx, creds.client);
420 SAFE_FREE(service_s);
422 SAFE_FREE(machine_account);
426 /************************************************************************
427 Check if the machine password can be used in conjunction with the salting_principal
428 to generate a key which will successfully decrypt the AP_REQ already
429 gotten as a message to the local machine.
430 ************************************************************************/
432 static BOOL verify_service_password(krb5_context ctx,
434 const char *salting_principal,
438 krb5_principal salting_kprinc = NULL;
439 krb5_ticket *ticket = NULL;
442 char *salting_s = NULL;
443 char *machine_account = NULL, *password = NULL;
444 krb5_auth_context auth_context = NULL;
447 memset(&passdata, '\0', sizeof(passdata));
448 memset(&key, '\0', sizeof(key));
450 asprintf(&machine_account, "%s$@%s", lp_netbios_name(), lp_realm());
451 if (machine_account == NULL) {
454 password = secrets_fetch_machine_password(lp_workgroup());
455 if (password == NULL) {
459 if (strchr_m(salting_principal, '@')) {
460 asprintf(&salting_s, "%s", salting_principal);
462 asprintf(&salting_s, "%s@%s", salting_principal, lp_realm());
465 if ((err = krb5_parse_name(ctx, salting_s, &salting_kprinc))) {
466 DEBUG(0,("verify_service_password: krb5_parse_name %s failed: %s\n",
467 salting_s, error_message(err)));
471 passdata.length = strlen(password);
472 passdata.data = (char*)password;
473 if ((err = create_kerberos_key_from_string_direct(ctx, salting_kprinc, &passdata, &key, enctype))) {
474 DEBUG(0,("verify_service_password: create_kerberos_key_from_string %d failed: %s\n",
475 enctype, error_message(err)));
479 if ((err = krb5_auth_con_init(ctx, &auth_context)) != 0) {
480 DEBUG(0,("verify_service_password: krb5_auth_con_init failed %s\n", error_message(err)));
484 if ((err = krb5_auth_con_setuseruserkey(ctx, auth_context, &key)) != 0) {
485 DEBUG(0,("verify_service_password: krb5_auth_con_setuseruserkey failed %s\n", error_message(err)));
489 if (!(err = krb5_rd_req(ctx, &auth_context, in_data, NULL, NULL, NULL, &ticket))) {
490 DEBUG(10,("verify_service_password: decrypted message with enctype %u salt %s!\n",
491 (unsigned int)enctype, salting_s));
497 memset(&passdata, 0, sizeof(passdata));
498 krb5_free_keyblock_contents(ctx, &key);
499 if (ticket != NULL) {
500 krb5_free_ticket(ctx, ticket);
502 if (salting_kprinc) {
503 krb5_free_principal(ctx, salting_kprinc);
505 SAFE_FREE(salting_s);
507 SAFE_FREE(machine_account);
511 /************************************************************************
513 * From the current draft of kerberos-clarifications:
515 * It is not possible to reliably generate a user's key given a pass
516 * phrase without contacting the KDC, since it will not be known
517 * whether alternate salt or parameter values are required.
519 * And because our server has a password, we have this exact problem. We
520 * make multiple guesses as to which principal name provides the salt which
523 ************************************************************************/
525 static void kerberos_derive_salting_principal_for_enctype(const char *service_principal,
528 krb5_enctype enctype,
529 krb5_enctype *enctypes)
531 char *salting_principals[3] = {NULL, NULL, NULL}, *second_principal = NULL;
532 krb5_error_code err = 0;
536 memset(&outbuf, '\0', sizeof(outbuf));
538 /* Check that the service_principal is useful. */
539 if ((service_principal == NULL) || (strlen(service_principal) == 0)) {
543 /* Generate our first guess -- the principal as-given. */
544 asprintf(&salting_principals[0], "%s", service_principal);
545 if ((salting_principals[0] == NULL) || (strlen(salting_principals[0]) == 0)) {
549 /* Generate our second guess -- the computer's principal, as Win2k3. */
550 asprintf(&second_principal, "host/%s.%s", lp_netbios_name(), lp_realm());
551 if (second_principal != NULL) {
552 strlower_m(second_principal);
553 asprintf(&salting_principals[1], "%s@%s", second_principal, lp_realm());
554 SAFE_FREE(second_principal);
556 if ((salting_principals[1] == NULL) || (strlen(salting_principals[1]) == 0)) {
560 /* Generate our third guess -- the computer's principal, as Win2k. */
561 asprintf(&second_principal, "HOST/%s", lp_netbios_name());
562 if (second_principal != NULL) {
563 strlower_m(second_principal + 5);
564 asprintf(&salting_principals[2], "%s@%s",
565 second_principal, lp_realm());
566 SAFE_FREE(second_principal);
568 if ((salting_principals[2] == NULL) || (strlen(salting_principals[2]) == 0)) {
572 /* Get a service ticket for ourselves into our memory ccache. */
573 /* This will commonly fail if there is no principal by that name (and we're trying
574 many names). So don't print a debug 0 error. */
576 if ((err = get_service_ticket(ctx, ccache, service_principal, enctype, &outbuf)) != 0) {
577 DEBUG(3, ("verify_service_password: get_service_ticket failed: %s\n",
578 error_message(err)));
582 /* At this point we have a message to ourselves, salted only the KDC knows how. We
583 have to work out what that salting is. */
585 /* Try and find the correct salting principal. */
586 for (i = 0; i < sizeof(salting_principals) / sizeof(salting_principals[i]); i++) {
587 if (verify_service_password(ctx, enctype, salting_principals[i], &outbuf)) {
592 /* If we failed to get a match, return. */
593 if (i >= sizeof(salting_principals) / sizeof(salting_principals[i])) {
597 /* If we succeeded, store the principal for use for all enctypes which
598 * share the same cipher and string-to-key function. Doing this here
599 * allows servers which just pass a keytab to krb5_rd_req() to work
601 for (j = 0; enctypes[j] != 0; j++) {
602 if (enctype != enctypes[j]) {
603 /* If this enctype isn't compatible with the one which
604 * we used, skip it. */
606 if (!kerberos_compatible_enctypes(ctx, enctypes[j], enctype))
609 /* If the principal which gives us the proper salt is the one
610 * which we would normally guess, don't bother noting anything
611 * in the secrets tdb. */
612 if (strcmp(service_principal, salting_principals[i]) != 0) {
613 kerberos_secrets_store_salting_principal(service_principal,
615 salting_principals[i]);
621 kerberos_free_data_contents(ctx, &outbuf);
622 SAFE_FREE(salting_principals[0]);
623 SAFE_FREE(salting_principals[1]);
624 SAFE_FREE(salting_principals[2]);
625 SAFE_FREE(second_principal);
628 /************************************************************************
629 Go through all the possible enctypes for this principal.
630 ************************************************************************/
632 static void kerberos_derive_salting_principal_direct(krb5_context context,
634 krb5_enctype *enctypes,
635 char *service_principal)
639 /* Try for each enctype separately, because the rules are
640 * different for different enctypes. */
641 for (i = 0; enctypes[i] != 0; i++) {
642 /* Delete secrets entry first. */
643 kerberos_secrets_store_salting_principal(service_principal, 0, NULL);
644 #ifdef ENCTYPE_ARCFOUR_HMAC
645 if (enctypes[i] == ENCTYPE_ARCFOUR_HMAC) {
646 /* Of course this'll always work, so just save
647 * ourselves the effort. */
651 /* Try to figure out what's going on with this
653 kerberos_derive_salting_principal_for_enctype(service_principal,
661 /************************************************************************
662 Wrapper function for the above.
663 ************************************************************************/
665 BOOL kerberos_derive_salting_principal(char *service_principal)
667 krb5_context context = NULL;
668 krb5_enctype *enctypes = NULL;
669 krb5_ccache ccache = NULL;
670 krb5_error_code ret = 0;
672 initialize_krb5_error_table();
673 if ((ret = krb5_init_context(&context)) != 0) {
674 DEBUG(1,("kerberos_derive_cifs_salting_principals: krb5_init_context failed. %s\n",
675 error_message(ret)));
678 if ((ret = get_kerberos_allowed_etypes(context, &enctypes)) != 0) {
679 DEBUG(1,("kerberos_derive_cifs_salting_principals: get_kerberos_allowed_etypes failed. %s\n",
680 error_message(ret)));
684 if ((ret = krb5_cc_resolve(context, LIBADS_CCACHE_NAME, &ccache)) != 0) {
685 DEBUG(3, ("get_service_ticket: krb5_cc_resolve for %s failed: %s\n",
686 LIBADS_CCACHE_NAME, error_message(ret)));
690 kerberos_derive_salting_principal_direct(context, ccache, enctypes, service_principal);
694 free_kerberos_etypes(context, enctypes);
697 krb5_cc_destroy(context, ccache);
700 krb5_free_context(context);
703 return ret ? False : True;
706 /************************************************************************
707 Core function to try and determine what salt is being used for any keytab
709 ************************************************************************/
711 BOOL kerberos_derive_cifs_salting_principals(void)
714 char *service = NULL;
715 krb5_context context = NULL;
716 krb5_enctype *enctypes = NULL;
717 krb5_ccache ccache = NULL;
718 krb5_error_code ret = 0;
721 initialize_krb5_error_table();
722 if ((ret = krb5_init_context(&context)) != 0) {
723 DEBUG(1,("kerberos_derive_cifs_salting_principals: krb5_init_context failed. %s\n",
724 error_message(ret)));
727 if ((ret = get_kerberos_allowed_etypes(context, &enctypes)) != 0) {
728 DEBUG(1,("kerberos_derive_cifs_salting_principals: get_kerberos_allowed_etypes failed. %s\n",
729 error_message(ret)));
733 if ((ret = krb5_cc_resolve(context, LIBADS_CCACHE_NAME, &ccache)) != 0) {
734 DEBUG(3, ("get_service_ticket: krb5_cc_resolve for %s failed: %s\n",
735 LIBADS_CCACHE_NAME, error_message(ret)));
739 if (asprintf(&service, "%s$", lp_netbios_name()) != -1) {
741 kerberos_derive_salting_principal_direct(context, ccache, enctypes, service);
744 if (asprintf(&service, "cifs/%s", lp_netbios_name()) != -1) {
746 kerberos_derive_salting_principal_direct(context, ccache, enctypes, service);
749 if (asprintf(&service, "host/%s", lp_netbios_name()) != -1) {
751 kerberos_derive_salting_principal_direct(context, ccache, enctypes, service);
754 if (asprintf(&service, "cifs/%s.%s", lp_netbios_name(), lp_realm()) != -1) {
756 kerberos_derive_salting_principal_direct(context, ccache, enctypes, service);
759 if (asprintf(&service, "host/%s.%s", lp_netbios_name(), lp_realm()) != -1) {
761 kerberos_derive_salting_principal_direct(context, ccache, enctypes, service);
764 name_to_fqdn(my_fqdn, lp_netbios_name());
765 if (asprintf(&service, "cifs/%s", my_fqdn) != -1) {
767 kerberos_derive_salting_principal_direct(context, ccache, enctypes, service);
770 if (asprintf(&service, "host/%s", my_fqdn) != -1) {
772 kerberos_derive_salting_principal_direct(context, ccache, enctypes, service);
780 free_kerberos_etypes(context, enctypes);
783 krb5_cc_destroy(context, ccache);
786 krb5_free_context(context);