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 = 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 (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 krb5_free_unparsed_name(context, unparsed_name);
269 return (krb5_principal)NULL;
272 if (krb5_parse_name(context, salt_princ_s, &ret_princ) != 0) {
273 krb5_free_unparsed_name(context, unparsed_name);
274 SAFE_FREE(salt_princ_s);
275 return (krb5_principal)NULL;
277 krb5_free_unparsed_name(context, 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 (krb5_parse_name(context, princ_s, &princ) != 0) {
315 if (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);
336 krb5_free_unparsed_name(context, unparsed_name);
339 krb5_free_context(context);
345 /************************************************************************
346 Routine to get initial credentials as a service ticket for the local machine.
347 Returns a buffer initialized with krb5_mk_req_extended.
348 ************************************************************************/
350 static krb5_error_code get_service_ticket(krb5_context ctx,
352 const char *service_principal,
356 krb5_creds creds, *new_creds = NULL;
357 char *service_s = NULL;
358 char *machine_account = NULL, *password = NULL;
360 krb5_auth_context auth_context = NULL;
361 krb5_error_code err = 0;
365 asprintf(&machine_account, "%s$@%s", global_myname(), lp_realm());
366 if (machine_account == NULL) {
369 password = secrets_fetch_machine_password(lp_workgroup(), NULL, NULL);
370 if (password == NULL) {
373 if ((err = kerberos_kinit_password(machine_account, password,
374 0, LIBADS_CCACHE_NAME)) != 0) {
375 DEBUG(0,("get_service_ticket: kerberos_kinit_password %s failed: %s\n",
377 error_message(err)));
381 /* Ok - the above call has gotten a TGT. Now we need to get a service
382 ticket to ourselves. */
384 /* Set up the enctype and client and server principal fields for krb5_get_credentials. */
385 kerberos_set_creds_enctype(&creds, enctype);
387 if ((err = krb5_cc_get_principal(ctx, ccache, &creds.client))) {
388 DEBUG(3, ("get_service_ticket: krb5_cc_get_principal failed: %s\n",
389 error_message(err)));
393 if (strchr_m(service_principal, '@')) {
394 asprintf(&service_s, "%s", service_principal);
396 asprintf(&service_s, "%s@%s", service_principal, lp_realm());
399 if ((err = krb5_parse_name(ctx, service_s, &creds.server))) {
400 DEBUG(0,("get_service_ticket: krb5_parse_name %s failed: %s\n",
401 service_s, error_message(err)));
405 if ((err = krb5_get_credentials(ctx, 0, ccache, &creds, &new_creds))) {
406 DEBUG(5,("get_service_ticket: krb5_get_credentials for %s enctype %d failed: %s\n",
407 service_s, enctype, error_message(err)));
411 memset(&in_data, '\0', sizeof(in_data));
412 if ((err = krb5_mk_req_extended(ctx, &auth_context, 0, &in_data,
413 new_creds, p_outbuf)) != 0) {
414 DEBUG(0,("get_service_ticket: krb5_mk_req_extended failed: %s\n",
415 error_message(err)));
422 krb5_auth_con_free(ctx, auth_context);
425 krb5_free_creds(ctx, new_creds);
428 krb5_free_principal(ctx, creds.server);
431 krb5_free_principal(ctx, creds.client);
434 SAFE_FREE(service_s);
436 SAFE_FREE(machine_account);
440 /************************************************************************
441 Check if the machine password can be used in conjunction with the salting_principal
442 to generate a key which will successfully decrypt the AP_REQ already
443 gotten as a message to the local machine.
444 ************************************************************************/
446 static BOOL verify_service_password(krb5_context ctx,
448 const char *salting_principal,
452 krb5_principal salting_kprinc = NULL;
453 krb5_ticket *ticket = NULL;
456 char *salting_s = NULL;
457 char *machine_account = NULL, *password = NULL;
458 krb5_auth_context auth_context = NULL;
461 memset(&passdata, '\0', sizeof(passdata));
462 memset(&key, '\0', sizeof(key));
464 asprintf(&machine_account, "%s$@%s", global_myname(), lp_realm());
465 if (machine_account == NULL) {
468 password = secrets_fetch_machine_password(lp_workgroup(), NULL, NULL);
469 if (password == NULL) {
473 if (strchr_m(salting_principal, '@')) {
474 asprintf(&salting_s, "%s", salting_principal);
476 asprintf(&salting_s, "%s@%s", salting_principal, lp_realm());
479 if ((err = krb5_parse_name(ctx, salting_s, &salting_kprinc))) {
480 DEBUG(0,("verify_service_password: krb5_parse_name %s failed: %s\n",
481 salting_s, error_message(err)));
485 passdata.length = strlen(password);
486 passdata.data = (char*)password;
487 if ((err = create_kerberos_key_from_string_direct(ctx, salting_kprinc, &passdata, &key, enctype))) {
488 DEBUG(0,("verify_service_password: create_kerberos_key_from_string %d failed: %s\n",
489 enctype, error_message(err)));
493 if ((err = krb5_auth_con_init(ctx, &auth_context)) != 0) {
494 DEBUG(0,("verify_service_password: krb5_auth_con_init failed %s\n", error_message(err)));
498 if ((err = krb5_auth_con_setuseruserkey(ctx, auth_context, &key)) != 0) {
499 DEBUG(0,("verify_service_password: krb5_auth_con_setuseruserkey failed %s\n", error_message(err)));
503 if (!(err = krb5_rd_req(ctx, &auth_context, in_data, NULL, NULL, NULL, &ticket))) {
504 DEBUG(10,("verify_service_password: decrypted message with enctype %u salt %s!\n",
505 (unsigned int)enctype, salting_s));
511 memset(&passdata, 0, sizeof(passdata));
512 krb5_free_keyblock_contents(ctx, &key);
513 if (ticket != NULL) {
514 krb5_free_ticket(ctx, ticket);
516 if (salting_kprinc) {
517 krb5_free_principal(ctx, salting_kprinc);
519 SAFE_FREE(salting_s);
521 SAFE_FREE(machine_account);
525 /************************************************************************
527 * From the current draft of kerberos-clarifications:
529 * It is not possible to reliably generate a user's key given a pass
530 * phrase without contacting the KDC, since it will not be known
531 * whether alternate salt or parameter values are required.
533 * And because our server has a password, we have this exact problem. We
534 * make multiple guesses as to which principal name provides the salt which
537 ************************************************************************/
539 static void kerberos_derive_salting_principal_for_enctype(const char *service_principal,
542 krb5_enctype enctype,
543 krb5_enctype *enctypes)
545 char *salting_principals[3] = {NULL, NULL, NULL}, *second_principal = NULL;
546 krb5_error_code err = 0;
550 memset(&outbuf, '\0', sizeof(outbuf));
552 /* Check that the service_principal is useful. */
553 if ((service_principal == NULL) || (strlen(service_principal) == 0)) {
557 /* Generate our first guess -- the principal as-given. */
558 asprintf(&salting_principals[0], "%s", service_principal);
559 if ((salting_principals[0] == NULL) || (strlen(salting_principals[0]) == 0)) {
563 /* Generate our second guess -- the computer's principal, as Win2k3. */
564 asprintf(&second_principal, "host/%s.%s", global_myname(), lp_realm());
565 if (second_principal != NULL) {
566 strlower_m(second_principal);
567 asprintf(&salting_principals[1], "%s@%s", second_principal, lp_realm());
568 SAFE_FREE(second_principal);
570 if ((salting_principals[1] == NULL) || (strlen(salting_principals[1]) == 0)) {
574 /* Generate our third guess -- the computer's principal, as Win2k. */
575 asprintf(&second_principal, "HOST/%s", global_myname());
576 if (second_principal != NULL) {
577 strlower_m(second_principal + 5);
578 asprintf(&salting_principals[2], "%s@%s",
579 second_principal, lp_realm());
580 SAFE_FREE(second_principal);
582 if ((salting_principals[2] == NULL) || (strlen(salting_principals[2]) == 0)) {
586 /* Get a service ticket for ourselves into our memory ccache. */
587 /* This will commonly fail if there is no principal by that name (and we're trying
588 many names). So don't print a debug 0 error. */
590 if ((err = get_service_ticket(ctx, ccache, service_principal, enctype, &outbuf)) != 0) {
591 DEBUG(3, ("verify_service_password: get_service_ticket failed: %s\n",
592 error_message(err)));
596 /* At this point we have a message to ourselves, salted only the KDC knows how. We
597 have to work out what that salting is. */
599 /* Try and find the correct salting principal. */
600 for (i = 0; i < sizeof(salting_principals) / sizeof(salting_principals[i]); i++) {
601 if (verify_service_password(ctx, enctype, salting_principals[i], &outbuf)) {
606 /* If we failed to get a match, return. */
607 if (i >= sizeof(salting_principals) / sizeof(salting_principals[i])) {
611 /* If we succeeded, store the principal for use for all enctypes which
612 * share the same cipher and string-to-key function. Doing this here
613 * allows servers which just pass a keytab to krb5_rd_req() to work
615 for (j = 0; enctypes[j] != 0; j++) {
616 if (enctype != enctypes[j]) {
617 /* If this enctype isn't compatible with the one which
618 * we used, skip it. */
620 if (!kerberos_compatible_enctypes(ctx, enctypes[j], enctype))
623 /* If the principal which gives us the proper salt is the one
624 * which we would normally guess, don't bother noting anything
625 * in the secrets tdb. */
626 if (strcmp(service_principal, salting_principals[i]) != 0) {
627 kerberos_secrets_store_salting_principal(service_principal,
629 salting_principals[i]);
635 kerberos_free_data_contents(ctx, &outbuf);
636 SAFE_FREE(salting_principals[0]);
637 SAFE_FREE(salting_principals[1]);
638 SAFE_FREE(salting_principals[2]);
639 SAFE_FREE(second_principal);
642 /************************************************************************
643 Go through all the possible enctypes for this principal.
644 ************************************************************************/
646 static void kerberos_derive_salting_principal_direct(krb5_context context,
648 krb5_enctype *enctypes,
649 char *service_principal)
653 /* Try for each enctype separately, because the rules are
654 * different for different enctypes. */
655 for (i = 0; enctypes[i] != 0; i++) {
656 /* Delete secrets entry first. */
657 kerberos_secrets_store_salting_principal(service_principal, 0, NULL);
658 #ifdef ENCTYPE_ARCFOUR_HMAC
659 if (enctypes[i] == ENCTYPE_ARCFOUR_HMAC) {
660 /* Of course this'll always work, so just save
661 * ourselves the effort. */
665 /* Try to figure out what's going on with this
667 kerberos_derive_salting_principal_for_enctype(service_principal,
675 /************************************************************************
676 Wrapper function for the above.
677 ************************************************************************/
679 BOOL kerberos_derive_salting_principal(char *service_principal)
681 krb5_context context = NULL;
682 krb5_enctype *enctypes = NULL;
683 krb5_ccache ccache = NULL;
684 krb5_error_code ret = 0;
686 initialize_krb5_error_table();
687 if ((ret = krb5_init_context(&context)) != 0) {
688 DEBUG(1,("kerberos_derive_cifs_salting_principals: krb5_init_context failed. %s\n",
689 error_message(ret)));
692 if ((ret = get_kerberos_allowed_etypes(context, &enctypes)) != 0) {
693 DEBUG(1,("kerberos_derive_cifs_salting_principals: get_kerberos_allowed_etypes failed. %s\n",
694 error_message(ret)));
698 if ((ret = krb5_cc_resolve(context, LIBADS_CCACHE_NAME, &ccache)) != 0) {
699 DEBUG(3, ("get_service_ticket: krb5_cc_resolve for %s failed: %s\n",
700 LIBADS_CCACHE_NAME, error_message(ret)));
704 kerberos_derive_salting_principal_direct(context, ccache, enctypes, service_principal);
708 free_kerberos_etypes(context, enctypes);
711 krb5_cc_destroy(context, ccache);
714 krb5_free_context(context);
717 return ret ? False : True;
720 /************************************************************************
721 Core function to try and determine what salt is being used for any keytab
723 ************************************************************************/
725 BOOL kerberos_derive_cifs_salting_principals(void)
728 char *service = NULL;
729 krb5_context context = NULL;
730 krb5_enctype *enctypes = NULL;
731 krb5_ccache ccache = NULL;
732 krb5_error_code ret = 0;
735 initialize_krb5_error_table();
736 if ((ret = krb5_init_context(&context)) != 0) {
737 DEBUG(1,("kerberos_derive_cifs_salting_principals: krb5_init_context failed. %s\n",
738 error_message(ret)));
741 if ((ret = get_kerberos_allowed_etypes(context, &enctypes)) != 0) {
742 DEBUG(1,("kerberos_derive_cifs_salting_principals: get_kerberos_allowed_etypes failed. %s\n",
743 error_message(ret)));
747 if ((ret = krb5_cc_resolve(context, LIBADS_CCACHE_NAME, &ccache)) != 0) {
748 DEBUG(3, ("get_service_ticket: krb5_cc_resolve for %s failed: %s\n",
749 LIBADS_CCACHE_NAME, error_message(ret)));
753 if (asprintf(&service, "%s$", global_myname()) != -1) {
755 kerberos_derive_salting_principal_direct(context, ccache, enctypes, service);
758 if (asprintf(&service, "cifs/%s", global_myname()) != -1) {
760 kerberos_derive_salting_principal_direct(context, ccache, enctypes, service);
763 if (asprintf(&service, "host/%s", global_myname()) != -1) {
765 kerberos_derive_salting_principal_direct(context, ccache, enctypes, service);
768 if (asprintf(&service, "cifs/%s.%s", global_myname(), lp_realm()) != -1) {
770 kerberos_derive_salting_principal_direct(context, ccache, enctypes, service);
773 if (asprintf(&service, "host/%s.%s", global_myname(), lp_realm()) != -1) {
775 kerberos_derive_salting_principal_direct(context, ccache, enctypes, service);
778 name_to_fqdn(my_fqdn, global_myname());
779 if (asprintf(&service, "cifs/%s", my_fqdn) != -1) {
781 kerberos_derive_salting_principal_direct(context, ccache, enctypes, service);
784 if (asprintf(&service, "host/%s", my_fqdn) != -1) {
786 kerberos_derive_salting_principal_direct(context, ccache, enctypes, service);
794 free_kerberos_etypes(context, enctypes);
797 krb5_cc_destroy(context, ccache);
800 krb5_free_context(context);
805 int kerberos_kinit_password(const char *principal,
806 const char *password,
808 const char *cache_name)
810 return kerberos_kinit_password_ext(principal,