Samba-3.0.7-1.3E Active Directory Issues
Jeremy Allison
jra at samba.org
Thu Oct 28 22:32:11 GMT 2004
On Wed, Oct 27, 2004 at 04:10:54PM -0400, Nalin Dahyabhai wrote:
>
> It may not be quite that bad. The application server and the KDC do
> have to agree on the application server's key, but the client isn't
> expected to be able to decrypt things with that key. It doesn't even
> have to know how to encrypt things with that key, which places the
> entire interoperability burden on the application server and the KDC.
>
> A client can't dictate what kind of key the KDC will use to encrypt data
> meant for an application server. Unix-based KDCs can check which types
> of keys they have on-hand for the service principal to determine which
> kind of keys to use, and AD consults the userAccountControl attribute of
> the computer account to see if it needs to use DES for the application
> server, otherwise it seems to choose RC4.
>
> The "net" command requests credentials from the KDC to guess at the
> right key. If a computer's account is flagged as needing DES, the
> client will receive credentials from the KDC which are encrypted using
> DES, and it can attempt to decrypt them with various keys until it hits
> on one which works. Don't worry, this isn't a brute-force attack, it's
> more like guessing which key on a large-but-manageable chain is right
> for a given door, when it's still possible that none are.
>
> But if the computer account is not flagged as requiring DES, then the
> KDC's preference for RC4 will ensure that the incorrect DES keys in the
> keytab are never used by the application server.
Ok - here is a "work in progress" snapshot of what I have done with
your code. It doesn't compile (yet :-) but might give you a better
idea how I'm going about things. I'm still working on my version of
verify_service_password().
All comments welcome !
Thanks,
Jeremy.
-------------- next part --------------
Index: libads/kerberos.c
===================================================================
--- libads/kerberos.c (revision 3336)
+++ libads/kerberos.c (working copy)
@@ -3,8 +3,9 @@
kerberos utility library
Copyright (C) Andrew Tridgell 2001
Copyright (C) Remus Koos 2001
-
-
+ Copyright (C) Nalin Dahyabhai <nalin at redhat.com> 2004.
+ Copyright (C) Jeremy Allison 2004.
+
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
@@ -51,10 +52,15 @@
}
/*
- simulate a kinit, putting the tgt in the default cache location
+ simulate a kinit, putting the tgt in the given cache location. If cache_name == NULL
+ place in default cache location.
remus at snapserver.com
*/
-int kerberos_kinit_password(const char *principal, const char *password, int time_offset, time_t *expire_time)
+int kerberos_kinit_password(const char *principal,
+ const char *password,
+ int time_offset,
+ time_t *expire_time,
+ const char *cache_name)
{
krb5_context ctx = NULL;
krb5_error_code code = 0;
@@ -69,7 +75,8 @@
krb5_set_real_time(ctx, time(NULL) + time_offset, 0);
}
- if ((code = krb5_cc_default(ctx, &cc))) {
+ if ((code = krb5_cc_resolve(ctx, cache_name ?
+ cache_name : krb5_cc_default_name(ctx), &cc))) {
krb5_free_context(ctx);
return code;
}
@@ -129,7 +136,8 @@
return KRB5_LIBOS_CANTREADPWD;
}
- ret = kerberos_kinit_password(s, ads->auth.password, ads->auth.time_offset, &ads->auth.expire);
+ ret = kerberos_kinit_password(s, ads->auth.password, ads->auth.time_offset,
+ &ads->auth.expire, NULL);
if (ret) {
DEBUG(0,("kerberos_kinit_password %s failed: %s\n",
@@ -174,4 +182,581 @@
return code;
}
+/************************************************************************
+ Routine to fetch the salting principal for a service. Active
+ Directory may use a non-obvious principal name to generate the salt
+ when it determines the key to use for encrypting tickets for a service,
+ and hopefully we detected that when we joined the domain.
+ ************************************************************************/
+
+static char *kerberos_secrets_fetch_salting_principal(const char *service, int enctype)
+{
+ char *key = NULL;
+ char *ret = NULL;
+
+ asprintf(&key, "%s/%s/enctype=%d", SECRETS_SALTING_PRINCIPAL, service, enctype);
+ if (!key) {
+ return NULL;
+ }
+ ret = (char *)secrets_fetch(key, NULL);
+ SAFE_FREE(key);
+ return ret;
+}
+
+/************************************************************************
+ Routine to get the salting principal for this service. Active
+ Directory may use a non-obvious principal name to generate the salt
+ when it determines the key to use for encrypting tickets for a service,
+ and hopefully we detected that when we joined the domain.
+ Caller must free if return is not null.
+ ************************************************************************/
+
+krb5_principal kerberos_fetch_salt_princ_for_host_princ(krb5_context context,
+ krb5_principal host_princ,
+ int enctype)
+{
+ char *unparsed_name = NULL, *salt_princ_s = NULL;
+ krb5_principal ret_princ = NULL;
+
+ if (krb5_unparse_name(context, host_princ, &unparsed_name) != 0) {
+ return (krb5_principal)NULL;
+ }
+
+ if ((salt_princ_s = kerberos_secrets_fetch_salting_principal(unparsed_name, enctype)) == NULL) {
+ krb5_free_unparsed_name(context, unparsed_name);
+ return (krb5_principal)NULL;
+ }
+
+ if (krb5_parse_name(context, salt_princ_s, &ret_princ) != 0) {
+ krb5_free_unparsed_name(context, unparsed_name);
+ SAFE_FREE(salt_princ_s);
+ return (krb5_principal)NULL;
+ }
+ krb5_free_unparsed_name(context, unparsed_name);
+ SAFE_FREE(salt_princ_s);
+ return ret_princ;
+}
+
+/************************************************************************
+ Routine to set the salting principal for this service. Active
+ Directory may use a non-obvious principal name to generate the salt
+ when it determines the key to use for encrypting tickets for a service,
+ and hopefully we detected that when we joined the domain.
+ Setting principal to NULL deletes this entry.
+ ************************************************************************/
+
+BOOL kerberos_secrets_store_salting_principal(const char *service,
+ int enctype,
+ const char *principal)
+{
+ char *key = NULL;
+ BOOL ret = False;
+ krb5_context context = NULL;
+ krb5_principal princ = NULL;
+ char *princ_s = NULL;
+ char *unparsed_name = NULL;
+
+ krb5_init_context(&context);
+ if (!context) {
+ return False;
+ }
+ if (strchr_m(service, '@')) {
+ asprintf(&princ_s, "%s", service);
+ } else {
+ asprintf(&princ_s, "%s@%s", service, lp_realm());
+ }
+
+ if (krb5_parse_name(context, princ_s, &princ) != 0) {
+ goto out;
+
+ }
+ if (krb5_unparse_name(context, princ, &unparsed_name) != 0) {
+ goto out;
+ }
+
+ asprintf(&key, "%s/%s/enctype=%d", SECRETS_SALTING_PRINCIPAL, unparsed_name, enctype);
+ if (!key) {
+ goto out;
+ }
+
+ if ((principal != NULL) && (strlen(principal) > 0)) {
+ ret = secrets_store(key, principal, strlen(principal) + 1);
+ } else {
+ ret = secrets_delete(key);
+ }
+
+ out:
+
+ SAFE_FREE(key);
+ SAFE_FREE(princ_s);
+
+ if (unparsed_name) {
+ krb5_free_unparsed_name(context, unparsed_name);
+ }
+ if (context) {
+ krb5_free_context(context);
+ }
+
+ return ret;
+}
+
+/************************************************************************
+ Routine to get initial credentials as a service ticket for the local machine.
+ ************************************************************************/
+
+static krb5_error_code get_service_ticket(krb5_context ctx,
+ const char *cache_name,
+ const char *service_principal,
+ int enctype)
+{
+ krb5_creds creds, *new_creds = NULL;
+ krb5_ccache ccache = NULL;
+ char *service_s = NULL;
+ char *machine_account = NULL, *password = NULL;
+ krb5_error_code err = 0;
+
+ asprintf(&machine_account, "%s$@%s", global_myname(), lp_realm());
+ if (machine_account == NULL) {
+ goto out;
+ }
+ password = secrets_fetch_machine_password(lp_workgroup(), NULL, NULL);
+ if (password == NULL) {
+ goto out;
+ }
+ if ((err = kerberos_kinit_password(machine_account, password, 0, NULL, cache_name)) != 0) {
+ DEBUG(0,("get_service_ticket: kerberos_kinit_password %s@%s failed: %s\n",
+ machine_account,
+ lp_realm(),
+ error_message(err)));
+ goto out;
+ }
+
+ /* Ok - the above call has gotten a TGT. Now we need to get a service
+ ticket to ourselves. */
+
+ if ((err = krb5_cc_resolve(ctx, cache_name, &ccache)) != 0) {
+ DEBUG(3, ("get_service_ticket: krb5_cc_resolve for %s failed: %s\n",
+ cache_name, error_message(err)));
+ goto out;
+ }
+
+ /* Set up the enctype and client and server principal fields for krb5_get_credentials. */
+ memset(&creds, 0, sizeof(creds));
+ creds.keyblock.enctype = enctype;
+ if ((err = krb5_cc_get_principal(ctx, ccache, &creds.client))) {
+ DEBUG(3, ("get_service_ticket: krb5_cc_get_principal failed: %s\n",
+ error_message(err)));
+ goto out;
+ }
+
+ if (strchr_m(service_principal, '@')) {
+ asprintf(&service_s, "%s", service_principal);
+ } else {
+ asprintf(&service_s, "%s@%s", service_principal, lp_realm());
+ }
+
+ if ((err = krb5_parse_name(ctx, service_s, &creds.server))) {
+ DEBUG(0,("get_service_ticket: krb5_parse_name %s failed: %s\n",
+ service_s, error_message(err)));
+ goto out;
+ }
+
+ if ((err = krb5_get_credentials(ctx, 0, ccache, &creds, &new_creds))) {
+ DEBUG(5,("get_service_ticket: krb5_get_credentials for %s failed: %s\n",
+ service_s, error_message(err)));
+ }
+
+ out:
+
+ if (new_creds != NULL) {
+ krb5_free_creds(ctx, new_creds);
+ }
+ if (creds.server != NULL) {
+ krb5_free_principal(ctx, creds.server);
+ }
+ if (creds.client != NULL) {
+ krb5_free_principal(ctx, creds.client);
+ }
+ if (ccache != NULL) {
+ krb5_cc_close(ctx, ccache);
+ }
+
+ SAFE_FREE(service_s);
+ SAFE_FREE(password);
+ SAFE_FREE(machine_account);
+ return err;
+}
+
+/************************************************************************
+ Check if the machine password can be used in conjunction with the salting_principal
+ to generate a key which will successfully decrypt the credentials already
+ gotten as a service ticket to the local machine.
+ ************************************************************************/
+
+#if 0
+static BOOL verify_service_password(const char *service_principal,
+ const char *salting_principal,
+ int enctype)
+{
+ BOOL ret = False;
+ krb5_context ctx = NULL;
+ krb5_principal salting = NULL;
+ krb5_ccache ccache = NULL;
+ krb5_creds creds, *new_creds = NULL;
+ krb5_ticket *ticket = NULL;
+ krb5_keyblock key;
+ krb5_data passdata;
+ char *service_s = NULL, *salting_s = NULL;
+ char *machine_account = NULL, *password = NULL;
+ krb5_error_code code;
+ int i;
+
+ memset(&passdata, 0, sizeof(passdata));
+ memset(&key, 0, sizeof(key));
+
+ asprintf(&machine_account, "%s$@%s", global_myname(), lp_realm());
+ if (machine_account == NULL) {
+ goto out;
+ }
+ password = secrets_fetch_machine_password(lp_workgroup(), NULL, NULL);
+ if (password == NULL) {
+ goto out;
+ }
+ if ((err = kerberos_kinit_password(machine_account, password, 0, NULL, "MEMORY:libads")) != 0) {
+ DEBUG(0,("verify_service_password: kerberos_kinit_password %s@%s failed: %s\n",
+ machine_account,
+ lp_realm(),
+ error_message(code)));
+ goto out;
+ }
+
+ /* Ok - the above call has gotten a TGT. Now we need to get a service
+ ticket to ourselves. */
+
+ if ((err = krb5_init_context(&ctx)) != 0) {
+ DEBUG(3, ("verify_service_password: kdb5_init_context failed: %s\n",
+ error_message(err)));
+ goto out;
+ }
+
+ if ((err = krb5_cc_resolve(ctx, "MEMORY:libads", &ccache)) != 0) {
+ DEBUG(3, ("verify_service_password: krb5_cc_resolve failed: %s\n",
+ error_message(err)));
+ goto out;
+ }
+
+ /* Set up the enctype and client and server principal fields for krb5_get_credentials. */
+ memset(&creds, 0, sizeof(creds));
+ creds.keyblock.enctype = enctype;
+ if ((err = krb5_cc_get_principal(ctx, ccache, &creds.client))) {
+ DEBUG(3, ("verify_service_password: krb5_cc_get_principal failed: %s\n",
+ error_message(err)));
+ goto out;
+ }
+
+ if (strchr_m(service_principal, '@')) {
+ asprintf(&service_s, "%s", service_principal);
+ } else {
+ asprintf(&service_s, "%s@%s", service_principal, lp_realm());
+ }
+
+ if ((err = krb5_parse_name(ctx, service_s, &creds.server))) {
+ DEBUG(0,("verify_service_password: krb5_parse_name %s failed: %s\n",
+ service_s, error_message(err)));
+ goto out;
+ }
+
+ if ((i = krb5_get_credentials(ctx, 0, ccache, &creds, &new_creds))) {
+ /* silently ignore errors here */
+ goto out;
+ }
+
+ /* At this point we have a service ticket to ourselves stored in
+ the memory credentials cache, salted only the KDC knows how. We
+ have to work out what that salting is. */
+
+ if ((i = krb5_decode_ticket(&new_creds->ticket, &ticket))) {
+ DEBUG(0,("verify_service_password: krb5_decode_ticket failed: %s\n", error_message(i)));
+ goto out;
+ }
+
+ if (strchr_m(salting_principal, '@')) {
+ asprintf(&salting_s, "%s", salting_principal);
+ } else {
+ asprintf(&salting_s, "%s@%s", salting_principal, lp_realm());
+ }
+
+ if ((i = krb5_parse_name(ctx, salting_s, &salting))) {
+ DEBUG(0,("verify_service_password: krb5_parse_name %s failed: %s\n",
+ salting_s, error_message(i)));
+ goto out;
+ }
+
+ passdata.length = strlen(password);
+ passdata.data = (char*)password;
+ if ((i = create_kerberos_key_from_string_direct(ctx, salting, &passdata, &key, enctype))) {
+ DEBUG(0,("verify_service_password: create_kerberos_key_from_string %d failed: %s\n",
+ enctype, error_message(i)));
+ goto out;
+ }
+
+ /* WARNING MIT ONLY !!! */
+ if ((i = krb5_decrypt_tkt_part(ctx, &key, ticket))) {
+ ret = False;
+ } else {
+ ret = True;
+ }
+
+ out:
+ memset(&passdata, 0, sizeof(passdata));
+ krb5_free_keyblock_contents(ctx, &key);
+ if (ticket != NULL) {
+ krb5_free_ticket(ctx, ticket);
+ }
+ ticket = NULL;
+ SAFE_FREE(salting_s);
+ if (new_creds != NULL) {
+ krb5_free_creds(ctx, new_creds);
+ }
+ new_creds = NULL;
+ if (creds.server != NULL) {
+ krb5_free_principal(ctx, creds.server);
+ }
+ creds.server = NULL;
+ SAFE_FREE(service_s);
+ if (creds.client != NULL) {
+ krb5_free_principal(ctx, creds.client);
+ }
+ creds.client = NULL;
+ if (ccache != NULL) {
+ krb5_cc_close(ctx, ccache);
+ }
+ ccache = NULL;
+ if (ctx != NULL) {
+ krb5_free_context(ctx);
+ }
+ ctx = NULL;
+
+ ads_kdestroy(NULL);
+ SAFE_FREE(password);
+ SAFE_FREE(machine_account);
+ return ret;
+}
#endif
+
+/************************************************************************
+ *
+ * From the current draft of kerberos-clarifications:
+ *
+ * It is not possible to reliably generate a user's key given a pass
+ * phrase without contacting the KDC, since it will not be known
+ * whether alternate salt or parameter values are required.
+ *
+ * And because our server has a password, we have this exact problem. We
+ * make multiple guesses as to which principal name provides the salt which
+ * the KDC is using.
+ *
+ ************************************************************************/
+
+static void kerberos_derive_salting_principal_for_enctype(const char *service_principal,
+ krb5_context ctx,
+ krb5_enctype enctype,
+ krb5_enctype *enctypes)
+{
+ char *salting_principals[3] = {NULL, NULL, NULL}, *second_principal = NULL;
+ krb5_boolean similar;
+ krb5_error_code err = 0;
+ int i, j;
+
+ /* Check that the service_principal is useful. */
+ if ((service_principal == NULL) || (strlen(service_principal) == 0)) {
+ return;
+ }
+
+ /* Generate our first guess -- the principal as-given. */
+ asprintf(&salting_principals[0], "%s", service_principal);
+ if ((salting_principals[0] == NULL) || (strlen(salting_principals[0]) == 0)) {
+ return;
+ }
+
+ /* Generate our second guess -- the computer's principal, as Win2k3. */
+ asprintf(&second_principal, "host/%s.%s", global_myname(), lp_realm());
+ if (second_principal != NULL) {
+ strlower_m(second_principal);
+ asprintf(&salting_principals[1], "%s@%s", second_principal, lp_realm());
+ SAFE_FREE(second_principal);
+ }
+ if ((salting_principals[1] == NULL) || (strlen(salting_principals[1]) == 0)) {
+ goto out;
+ }
+
+ /* Generate our third guess -- the computer's principal, as Win2k. */
+ asprintf(&second_principal, "HOST/%s", global_myname());
+ if (second_principal != NULL) {
+ strlower_m(second_principal + 5);
+ asprintf(&salting_principals[2], "%s@%s",
+ second_principal, lp_realm());
+ SAFE_FREE(second_principal);
+ }
+ if ((salting_principals[2] == NULL) || (strlen(salting_principals[2]) == 0)) {
+ goto out;
+ }
+
+ /* Get a service ticket for ourselves into our memory ccache. */
+ /* This will commonly fail if there is no principal by that name (and we're trying
+ many names). So don't print a debug 0 error. */
+
+ if ((err = get_service_ticket(ctx, "MEMORY:libads", service_principal, enctype)) != 0) {
+ DEBUG(3, ("verify_service_password: get_service_ticket failed: %s\n",
+ error_message(err)));
+ goto out;
+ }
+
+ /* At this point we have a service ticket to ourselves stored in
+ the memory credentials cache, salted only the KDC knows how. We
+ have to work out what that salting is. */
+
+ /* Try and find the correct salting principal. */
+ for (i = 0; i < sizeof(salting_principals) / sizeof(salting_principals[i]); i++) {
+ if (verify_service_password(ctx, "MEMORY:libads", enctype, salting_principals[i])) {
+ break;
+ }
+ }
+
+ /* If we failed to get a match, return. */
+ if (i >= sizeof(salting_principals) / sizeof(salting_principals[i])) {
+ goto out;
+ }
+
+ /* If we succeeded, store the principal for use for all enctypes which
+ * share the same cipher and string-to-key function. Doing this here
+ * allows servers which just pass a keytab to krb5_rd_req() to work
+ * correctly. */
+ for (j = 0; enctypes[j] != 0; j++) {
+ if (enctype != enctypes[j]) {
+ /* If we couldn't compare the enctype in this list to
+ * the one which we used, skip it. */
+
+ /* WARNING MIT ONLY !!! */
+ if (krb5_c_enctype_compare(ctx,
+ enctypes[j], enctype,
+ &similar) != 0) {
+ continue;
+ }
+ /* If this enctype isn't compatible with the one which
+ * we used, skip it. */
+ if (!similar) {
+ continue;
+ }
+ }
+ /* If the principal which gives us the proper salt is the one
+ * which we would normally guess, don't bother noting anything
+ * in the secrets tdb. */
+ if (strcmp(service_principal, salting_principals[i]) != 0) {
+ kerberos_secrets_store_salting_principal(service_principal,
+ enctypes[j],
+ salting_principals[i]);
+ }
+ }
+
+ out :
+
+ SAFE_FREE(salting_principals[0]);
+ SAFE_FREE(salting_principals[1]);
+ SAFE_FREE(salting_principals[2]);
+ SAFE_FREE(second_principal);
+}
+
+void kerberos_derive_salting_principal(krb5_context context,
+ krb5_enctype *enctypes,
+ char *service_principal)
+{
+ int i;
+
+ /* Try for each enctype separately, because the rules are
+ * different for different enctypes. */
+ for (i = 0; enctypes[i] != 0; i++) {
+ /* Delete secrets entry first. */
+ kerberos_secrets_store_salting_principal(service_principal, 0, NULL);
+#ifdef ENCTYPE_ARCFOUR_HMAC
+ if (enctypes[i] == ENCTYPE_ARCFOUR_HMAC) {
+ /* Of course this'll always work, so just save
+ * ourselves the effort. */
+ continue;
+ }
+#endif
+ /* Try to figure out what's going on with this
+ * principal. */
+ kerberos_derive_salting_principal_for_enctype(service_principal,
+ context,
+ enctypes[i],
+ enctypes);
+ }
+}
+
+/************************************************************************
+ Core function to try and determine what salt is being used for any keytab
+ keys.
+ ************************************************************************/
+
+BOOL kerberos_derive_cifs_salting_principals(void)
+{
+ fstring my_fqdn;
+ char *service = NULL;
+ krb5_context context = NULL;
+ krb5_enctype *enctypes = NULL;
+ krb5_error_code ret = 0;
+
+ if ((ret = krb5_init_context(&context)) != 0) {
+ DEBUG(1,("kerberos_derive_cifs_salting_principals: krb5_init_context failed. %s\n",
+ error_message(ret)));
+ return False;
+ }
+ if ((ret = get_kerberos_allowed_etypes(context, &enctypes)) != 0) {
+ DEBUG(1,("kerberos_derive_cifs_salting_principals: get_kerberos_allowed_etypes failed. %s\n",
+ error_message(ret)));
+ krb5_free_context(context);
+ }
+
+ if (asprintf(&service, "%s$", global_myname()) != -1) {
+ strlower_m(service);
+ kerberos_derive_salting_principal(context, enctypes, service);
+ SAFE_FREE(service);
+ }
+ if (asprintf(&service, "cifs/%s", global_myname()) != -1) {
+ strlower_m(service);
+ kerberos_derive_salting_principal(context, enctypes, service);
+ SAFE_FREE(service);
+ }
+ if (asprintf(&service, "host/%s", global_myname()) != -1) {
+ strlower_m(service);
+ kerberos_derive_salting_principal(context, enctypes, service);
+ SAFE_FREE(service);
+ }
+ if (asprintf(&service, "cifs/%s.%s", global_myname(), lp_realm()) != -1) {
+ strlower_m(service);
+ kerberos_derive_salting_principal(context, enctypes, service);
+ SAFE_FREE(service);
+ }
+ if (asprintf(&service, "host/%s.%s", global_myname(), lp_realm()) != -1) {
+ strlower_m(service);
+ kerberos_derive_salting_principal(context, enctypes, service);
+ SAFE_FREE(service);
+ }
+ name_to_fqdn(my_fqdn, global_myname());
+ if (asprintf(&service, "cifs/%s", my_fqdn) != -1) {
+ strlower_m(service);
+ kerberos_derive_salting_principal(context, enctypes, service);
+ SAFE_FREE(service);
+ }
+ if (asprintf(&service, "host/%s", my_fqdn) != -1) {
+ strlower_m(service);
+ kerberos_derive_salting_principal(context, enctypes, service);
+ SAFE_FREE(service);
+ }
+
+ free_kerberos_etypes(context, enctypes);
+ krb5_free_context(context);
+ return True;
+}
+#endif
More information about the samba-technical
mailing list