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 <nalin@redhat.com> 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.
28 #define LIBADS_CCACHE_NAME "MEMORY:libads"
31 we use a prompter to avoid a crash bug in the kerberos libs when
32 dealing with empty passwords
33 this prompter is just a string copy ...
35 static krb5_error_code
36 kerb_prompter(krb5_context ctx, void *data,
40 krb5_prompt prompts[])
42 if (num_prompts == 0) return 0;
44 memset(prompts[0].reply->data, '\0', prompts[0].reply->length);
45 if (prompts[0].reply->length > 0) {
47 strncpy(prompts[0].reply->data, data, prompts[0].reply->length-1);
48 prompts[0].reply->length = strlen(prompts[0].reply->data);
50 prompts[0].reply->length = 0;
57 simulate a kinit, putting the tgt in the given cache location. If cache_name == NULL
58 place in default cache location.
61 int kerberos_kinit_password_ext(const char *principal,
65 time_t *renew_till_time,
66 const char *cache_name,
68 time_t renewable_time)
70 krb5_context ctx = NULL;
71 krb5_error_code code = 0;
72 krb5_ccache cc = NULL;
75 krb5_get_init_creds_opt opt;
77 initialize_krb5_error_table();
78 if ((code = krb5_init_context(&ctx)))
81 if (time_offset != 0) {
82 krb5_set_real_time(ctx, time(NULL) + time_offset, 0);
85 DEBUG(10,("kerberos_kinit_password: using %s as ccache\n",
86 cache_name ? cache_name: krb5_cc_default_name(ctx)));
88 if ((code = krb5_cc_resolve(ctx, cache_name ? cache_name : krb5_cc_default_name(ctx), &cc))) {
89 krb5_free_context(ctx);
93 if ((code = smb_krb5_parse_name(ctx, principal, &me))) {
94 krb5_free_context(ctx);
98 krb5_get_init_creds_opt_init(&opt);
99 krb5_get_init_creds_opt_set_renew_life(&opt, renewable_time);
100 krb5_get_init_creds_opt_set_forwardable(&opt, 1);
103 #ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_SET_PAC_REQUEST
104 krb5_get_init_creds_opt_set_pac_request(ctx, &opt, True);
108 if ((code = krb5_get_init_creds_password(ctx, &my_creds, me, CONST_DISCARD(char *,password),
109 kerb_prompter, NULL, 0, NULL, &opt)))
111 krb5_free_principal(ctx, me);
112 krb5_free_context(ctx);
116 if ((code = krb5_cc_initialize(ctx, cc, me))) {
117 krb5_free_cred_contents(ctx, &my_creds);
118 krb5_free_principal(ctx, me);
119 krb5_free_context(ctx);
123 if ((code = krb5_cc_store_cred(ctx, cc, &my_creds))) {
124 krb5_cc_close(ctx, cc);
125 krb5_free_cred_contents(ctx, &my_creds);
126 krb5_free_principal(ctx, me);
127 krb5_free_context(ctx);
132 *expire_time = (time_t) my_creds.times.endtime;
135 if (renew_till_time) {
136 *renew_till_time = (time_t) my_creds.times.renew_till;
139 krb5_cc_close(ctx, cc);
140 krb5_free_cred_contents(ctx, &my_creds);
141 krb5_free_principal(ctx, me);
142 krb5_free_context(ctx);
149 /* run kinit to setup our ccache */
150 int ads_kinit_password(ADS_STRUCT *ads)
154 const char *account_name;
158 /* this will end up getting a ticket for DOMAIN@RUSTED.REA.LM */
159 account_name = lp_workgroup();
161 /* always use the sAMAccountName for security = domain */
162 /* global_myname()$@REA.LM */
163 if ( lp_security() == SEC_DOMAIN ) {
164 fstr_sprintf( acct_name, "%s$", global_myname() );
165 account_name = acct_name;
168 /* This looks like host/global_myname()@REA.LM */
169 account_name = ads->auth.user_name;
172 if (asprintf(&s, "%s@%s", account_name, ads->auth.realm) == -1) {
173 return KRB5_CC_NOMEM;
176 if (!ads->auth.password) {
177 return KRB5_LIBOS_CANTREADPWD;
180 ret = kerberos_kinit_password_ext(s, ads->auth.password, ads->auth.time_offset,
181 &ads->auth.expire, NULL, NULL, False, ads->auth.renewable);
184 DEBUG(0,("kerberos_kinit_password %s failed: %s\n",
185 s, error_message(ret)));
191 int ads_kdestroy(const char *cc_name)
193 krb5_error_code code;
194 krb5_context ctx = NULL;
195 krb5_ccache cc = NULL;
197 initialize_krb5_error_table();
198 if ((code = krb5_init_context (&ctx))) {
199 DEBUG(3, ("ads_kdestroy: kdb5_init_context failed: %s\n",
200 error_message(code)));
205 if ((code = krb5_cc_default(ctx, &cc))) {
206 krb5_free_context(ctx);
210 if ((code = krb5_cc_resolve(ctx, cc_name, &cc))) {
211 DEBUG(3, ("ads_kdestroy: krb5_cc_resolve failed: %s\n",
212 error_message(code)));
213 krb5_free_context(ctx);
218 if ((code = krb5_cc_destroy (ctx, cc))) {
219 DEBUG(3, ("ads_kdestroy: krb5_cc_destroy failed: %s\n",
220 error_message(code)));
223 krb5_free_context (ctx);
227 /************************************************************************
228 Routine to fetch the salting principal for a service. Active
229 Directory may use a non-obvious principal name to generate the salt
230 when it determines the key to use for encrypting tickets for a service,
231 and hopefully we detected that when we joined the domain.
232 ************************************************************************/
234 static char *kerberos_secrets_fetch_salting_principal(const char *service, int enctype)
239 asprintf(&key, "%s/%s/enctype=%d", SECRETS_SALTING_PRINCIPAL, service, enctype);
243 ret = (char *)secrets_fetch(key, NULL);
248 /************************************************************************
249 Routine to get the salting principal for this service. Active
250 Directory may use a non-obvious principal name to generate the salt
251 when it determines the key to use for encrypting tickets for a service,
252 and hopefully we detected that when we joined the domain.
253 Caller must free if return is not null.
254 ************************************************************************/
256 krb5_principal kerberos_fetch_salt_princ_for_host_princ(krb5_context context,
257 krb5_principal host_princ,
260 char *unparsed_name = NULL, *salt_princ_s = NULL;
261 krb5_principal ret_princ = NULL;
263 if (smb_krb5_unparse_name(context, host_princ, &unparsed_name) != 0) {
264 return (krb5_principal)NULL;
267 if ((salt_princ_s = kerberos_secrets_fetch_salting_principal(unparsed_name, enctype)) == NULL) {
268 SAFE_FREE(unparsed_name);
269 return (krb5_principal)NULL;
272 if (smb_krb5_parse_name(context, salt_princ_s, &ret_princ) != 0) {
273 SAFE_FREE(unparsed_name);
274 SAFE_FREE(salt_princ_s);
275 return (krb5_principal)NULL;
277 SAFE_FREE(unparsed_name);
278 SAFE_FREE(salt_princ_s);
282 /************************************************************************
283 Routine to set the salting principal for this service. Active
284 Directory may use a non-obvious principal name to generate the salt
285 when it determines the key to use for encrypting tickets for a service,
286 and hopefully we detected that when we joined the domain.
287 Setting principal to NULL deletes this entry.
288 ************************************************************************/
290 BOOL kerberos_secrets_store_salting_principal(const char *service,
292 const char *principal)
296 krb5_context context = NULL;
297 krb5_principal princ = NULL;
298 char *princ_s = NULL;
299 char *unparsed_name = NULL;
301 krb5_init_context(&context);
305 if (strchr_m(service, '@')) {
306 asprintf(&princ_s, "%s", service);
308 asprintf(&princ_s, "%s@%s", service, lp_realm());
311 if (smb_krb5_parse_name(context, princ_s, &princ) != 0) {
315 if (smb_krb5_unparse_name(context, princ, &unparsed_name) != 0) {
319 asprintf(&key, "%s/%s/enctype=%d", SECRETS_SALTING_PRINCIPAL, unparsed_name, enctype);
324 if ((principal != NULL) && (strlen(principal) > 0)) {
325 ret = secrets_store(key, principal, strlen(principal) + 1);
327 ret = secrets_delete(key);
334 SAFE_FREE(unparsed_name);
337 krb5_free_context(context);
343 /************************************************************************
344 Routine to get initial credentials as a service ticket for the local machine.
345 Returns a buffer initialized with krb5_mk_req_extended.
346 ************************************************************************/
348 static krb5_error_code get_service_ticket(krb5_context ctx,
350 const char *service_principal,
354 krb5_creds creds, *new_creds = NULL;
355 char *service_s = NULL;
356 char *machine_account = NULL, *password = NULL;
358 krb5_auth_context auth_context = NULL;
359 krb5_error_code err = 0;
363 asprintf(&machine_account, "%s$@%s", global_myname(), lp_realm());
364 if (machine_account == NULL) {
367 password = secrets_fetch_machine_password(lp_workgroup(), NULL, NULL);
368 if (password == NULL) {
371 if ((err = kerberos_kinit_password(machine_account, password,
372 0, LIBADS_CCACHE_NAME)) != 0) {
373 DEBUG(0,("get_service_ticket: kerberos_kinit_password %s failed: %s\n",
375 error_message(err)));
379 /* Ok - the above call has gotten a TGT. Now we need to get a service
380 ticket to ourselves. */
382 /* Set up the enctype and client and server principal fields for krb5_get_credentials. */
383 kerberos_set_creds_enctype(&creds, enctype);
385 if ((err = krb5_cc_get_principal(ctx, ccache, &creds.client))) {
386 DEBUG(3, ("get_service_ticket: krb5_cc_get_principal failed: %s\n",
387 error_message(err)));
391 if (strchr_m(service_principal, '@')) {
392 asprintf(&service_s, "%s", service_principal);
394 asprintf(&service_s, "%s@%s", service_principal, lp_realm());
397 if ((err = smb_krb5_parse_name(ctx, service_s, &creds.server))) {
398 DEBUG(0,("get_service_ticket: smb_krb5_parse_name %s failed: %s\n",
399 service_s, error_message(err)));
403 if ((err = krb5_get_credentials(ctx, 0, ccache, &creds, &new_creds))) {
404 DEBUG(5,("get_service_ticket: krb5_get_credentials for %s enctype %d failed: %s\n",
405 service_s, enctype, error_message(err)));
409 memset(&in_data, '\0', sizeof(in_data));
410 if ((err = krb5_mk_req_extended(ctx, &auth_context, 0, &in_data,
411 new_creds, p_outbuf)) != 0) {
412 DEBUG(0,("get_service_ticket: krb5_mk_req_extended failed: %s\n",
413 error_message(err)));
420 krb5_auth_con_free(ctx, auth_context);
423 krb5_free_creds(ctx, new_creds);
426 krb5_free_principal(ctx, creds.server);
429 krb5_free_principal(ctx, creds.client);
432 SAFE_FREE(service_s);
434 SAFE_FREE(machine_account);
438 /************************************************************************
439 Check if the machine password can be used in conjunction with the salting_principal
440 to generate a key which will successfully decrypt the AP_REQ already
441 gotten as a message to the local machine.
442 ************************************************************************/
444 static BOOL verify_service_password(krb5_context ctx,
446 const char *salting_principal,
450 krb5_principal salting_kprinc = NULL;
451 krb5_ticket *ticket = NULL;
454 char *salting_s = NULL;
455 char *machine_account = NULL, *password = NULL;
456 krb5_auth_context auth_context = NULL;
459 memset(&passdata, '\0', sizeof(passdata));
460 memset(&key, '\0', sizeof(key));
462 asprintf(&machine_account, "%s$@%s", global_myname(), lp_realm());
463 if (machine_account == NULL) {
466 password = secrets_fetch_machine_password(lp_workgroup(), NULL, NULL);
467 if (password == NULL) {
471 if (strchr_m(salting_principal, '@')) {
472 asprintf(&salting_s, "%s", salting_principal);
474 asprintf(&salting_s, "%s@%s", salting_principal, lp_realm());
477 if ((err = smb_krb5_parse_name(ctx, salting_s, &salting_kprinc))) {
478 DEBUG(0,("verify_service_password: smb_krb5_parse_name %s failed: %s\n",
479 salting_s, error_message(err)));
483 passdata.length = strlen(password);
484 passdata.data = (char*)password;
485 if ((err = create_kerberos_key_from_string_direct(ctx, salting_kprinc, &passdata, &key, enctype))) {
486 DEBUG(0,("verify_service_password: create_kerberos_key_from_string %d failed: %s\n",
487 enctype, error_message(err)));
491 if ((err = krb5_auth_con_init(ctx, &auth_context)) != 0) {
492 DEBUG(0,("verify_service_password: krb5_auth_con_init failed %s\n", error_message(err)));
496 if ((err = krb5_auth_con_setuseruserkey(ctx, auth_context, &key)) != 0) {
497 DEBUG(0,("verify_service_password: krb5_auth_con_setuseruserkey failed %s\n", error_message(err)));
501 if (!(err = krb5_rd_req(ctx, &auth_context, in_data, NULL, NULL, NULL, &ticket))) {
502 DEBUG(10,("verify_service_password: decrypted message with enctype %u salt %s!\n",
503 (unsigned int)enctype, salting_s));
509 memset(&passdata, 0, sizeof(passdata));
510 krb5_free_keyblock_contents(ctx, &key);
511 if (ticket != NULL) {
512 krb5_free_ticket(ctx, ticket);
514 if (salting_kprinc) {
515 krb5_free_principal(ctx, salting_kprinc);
517 SAFE_FREE(salting_s);
519 SAFE_FREE(machine_account);
523 /************************************************************************
525 * From the current draft of kerberos-clarifications:
527 * It is not possible to reliably generate a user's key given a pass
528 * phrase without contacting the KDC, since it will not be known
529 * whether alternate salt or parameter values are required.
531 * And because our server has a password, we have this exact problem. We
532 * make multiple guesses as to which principal name provides the salt which
535 ************************************************************************/
537 static void kerberos_derive_salting_principal_for_enctype(const char *service_principal,
540 krb5_enctype enctype,
541 krb5_enctype *enctypes)
543 char *salting_principals[3] = {NULL, NULL, NULL}, *second_principal = NULL;
544 krb5_error_code err = 0;
548 memset(&outbuf, '\0', sizeof(outbuf));
550 /* Check that the service_principal is useful. */
551 if ((service_principal == NULL) || (strlen(service_principal) == 0)) {
555 /* Generate our first guess -- the principal as-given. */
556 asprintf(&salting_principals[0], "%s", service_principal);
557 if ((salting_principals[0] == NULL) || (strlen(salting_principals[0]) == 0)) {
561 /* Generate our second guess -- the computer's principal, as Win2k3. */
562 asprintf(&second_principal, "host/%s.%s", global_myname(), lp_realm());
563 if (second_principal != NULL) {
564 strlower_m(second_principal);
565 asprintf(&salting_principals[1], "%s@%s", second_principal, lp_realm());
566 SAFE_FREE(second_principal);
568 if ((salting_principals[1] == NULL) || (strlen(salting_principals[1]) == 0)) {
572 /* Generate our third guess -- the computer's principal, as Win2k. */
573 asprintf(&second_principal, "HOST/%s", global_myname());
574 if (second_principal != NULL) {
575 strlower_m(second_principal + 5);
576 asprintf(&salting_principals[2], "%s@%s",
577 second_principal, lp_realm());
578 SAFE_FREE(second_principal);
580 if ((salting_principals[2] == NULL) || (strlen(salting_principals[2]) == 0)) {
584 /* Get a service ticket for ourselves into our memory ccache. */
585 /* This will commonly fail if there is no principal by that name (and we're trying
586 many names). So don't print a debug 0 error. */
588 if ((err = get_service_ticket(ctx, ccache, service_principal, enctype, &outbuf)) != 0) {
589 DEBUG(3, ("verify_service_password: get_service_ticket failed: %s\n",
590 error_message(err)));
594 /* At this point we have a message to ourselves, salted only the KDC knows how. We
595 have to work out what that salting is. */
597 /* Try and find the correct salting principal. */
598 for (i = 0; i < sizeof(salting_principals) / sizeof(salting_principals[i]); i++) {
599 if (verify_service_password(ctx, enctype, salting_principals[i], &outbuf)) {
604 /* If we failed to get a match, return. */
605 if (i >= sizeof(salting_principals) / sizeof(salting_principals[i])) {
609 /* If we succeeded, store the principal for use for all enctypes which
610 * share the same cipher and string-to-key function. Doing this here
611 * allows servers which just pass a keytab to krb5_rd_req() to work
613 for (j = 0; enctypes[j] != 0; j++) {
614 if (enctype != enctypes[j]) {
615 /* If this enctype isn't compatible with the one which
616 * we used, skip it. */
618 if (!kerberos_compatible_enctypes(ctx, enctypes[j], enctype))
621 /* If the principal which gives us the proper salt is the one
622 * which we would normally guess, don't bother noting anything
623 * in the secrets tdb. */
624 if (strcmp(service_principal, salting_principals[i]) != 0) {
625 kerberos_secrets_store_salting_principal(service_principal,
627 salting_principals[i]);
633 kerberos_free_data_contents(ctx, &outbuf);
634 SAFE_FREE(salting_principals[0]);
635 SAFE_FREE(salting_principals[1]);
636 SAFE_FREE(salting_principals[2]);
637 SAFE_FREE(second_principal);
640 /************************************************************************
641 Go through all the possible enctypes for this principal.
642 ************************************************************************/
644 static void kerberos_derive_salting_principal_direct(krb5_context context,
646 krb5_enctype *enctypes,
647 char *service_principal)
651 /* Try for each enctype separately, because the rules are
652 * different for different enctypes. */
653 for (i = 0; enctypes[i] != 0; i++) {
654 /* Delete secrets entry first. */
655 kerberos_secrets_store_salting_principal(service_principal, 0, NULL);
656 #ifdef ENCTYPE_ARCFOUR_HMAC
657 if (enctypes[i] == ENCTYPE_ARCFOUR_HMAC) {
658 /* Of course this'll always work, so just save
659 * ourselves the effort. */
663 /* Try to figure out what's going on with this
665 kerberos_derive_salting_principal_for_enctype(service_principal,
673 /************************************************************************
674 Wrapper function for the above.
675 ************************************************************************/
677 BOOL kerberos_derive_salting_principal(char *service_principal)
679 krb5_context context = NULL;
680 krb5_enctype *enctypes = NULL;
681 krb5_ccache ccache = NULL;
682 krb5_error_code ret = 0;
684 initialize_krb5_error_table();
685 if ((ret = krb5_init_context(&context)) != 0) {
686 DEBUG(1,("kerberos_derive_cifs_salting_principals: krb5_init_context failed. %s\n",
687 error_message(ret)));
690 if ((ret = get_kerberos_allowed_etypes(context, &enctypes)) != 0) {
691 DEBUG(1,("kerberos_derive_cifs_salting_principals: get_kerberos_allowed_etypes failed. %s\n",
692 error_message(ret)));
696 if ((ret = krb5_cc_resolve(context, LIBADS_CCACHE_NAME, &ccache)) != 0) {
697 DEBUG(3, ("get_service_ticket: krb5_cc_resolve for %s failed: %s\n",
698 LIBADS_CCACHE_NAME, error_message(ret)));
702 kerberos_derive_salting_principal_direct(context, ccache, enctypes, service_principal);
706 free_kerberos_etypes(context, enctypes);
709 krb5_cc_destroy(context, ccache);
712 krb5_free_context(context);
715 return ret ? False : True;
718 /************************************************************************
719 Core function to try and determine what salt is being used for any keytab
721 ************************************************************************/
723 BOOL kerberos_derive_cifs_salting_principals(void)
726 char *service = NULL;
727 krb5_context context = NULL;
728 krb5_enctype *enctypes = NULL;
729 krb5_ccache ccache = NULL;
730 krb5_error_code ret = 0;
733 initialize_krb5_error_table();
734 if ((ret = krb5_init_context(&context)) != 0) {
735 DEBUG(1,("kerberos_derive_cifs_salting_principals: krb5_init_context failed. %s\n",
736 error_message(ret)));
739 if ((ret = get_kerberos_allowed_etypes(context, &enctypes)) != 0) {
740 DEBUG(1,("kerberos_derive_cifs_salting_principals: get_kerberos_allowed_etypes failed. %s\n",
741 error_message(ret)));
745 if ((ret = krb5_cc_resolve(context, LIBADS_CCACHE_NAME, &ccache)) != 0) {
746 DEBUG(3, ("get_service_ticket: krb5_cc_resolve for %s failed: %s\n",
747 LIBADS_CCACHE_NAME, error_message(ret)));
751 if (asprintf(&service, "%s$", global_myname()) != -1) {
753 kerberos_derive_salting_principal_direct(context, ccache, enctypes, service);
756 if (asprintf(&service, "cifs/%s", global_myname()) != -1) {
758 kerberos_derive_salting_principal_direct(context, ccache, enctypes, service);
761 if (asprintf(&service, "host/%s", global_myname()) != -1) {
763 kerberos_derive_salting_principal_direct(context, ccache, enctypes, service);
766 if (asprintf(&service, "cifs/%s.%s", global_myname(), lp_realm()) != -1) {
768 kerberos_derive_salting_principal_direct(context, ccache, enctypes, service);
771 if (asprintf(&service, "host/%s.%s", global_myname(), lp_realm()) != -1) {
773 kerberos_derive_salting_principal_direct(context, ccache, enctypes, service);
776 name_to_fqdn(my_fqdn, global_myname());
777 if (asprintf(&service, "cifs/%s", my_fqdn) != -1) {
779 kerberos_derive_salting_principal_direct(context, ccache, enctypes, service);
782 if (asprintf(&service, "host/%s", my_fqdn) != -1) {
784 kerberos_derive_salting_principal_direct(context, ccache, enctypes, service);
792 free_kerberos_etypes(context, enctypes);
795 krb5_cc_destroy(context, ccache);
798 krb5_free_context(context);
803 int kerberos_kinit_password(const char *principal,
804 const char *password,
806 const char *cache_name)
808 return kerberos_kinit_password_ext(principal,