auth/credentials: try to use kerberos with the machine account unless we're in an...
[kai/samba-autobuild/.git] / auth / credentials / credentials_secrets.c
index 6c1cded4cbc52ad5fbd3cd29c5f8db7427765d43..ed148fd356686f13cd2041d1dd80521c75a8ceb9 100644 (file)
@@ -1,4 +1,4 @@
-/* 
+/*
    Unix SMB/CIFS implementation.
 
    User credentials handling (as regards on-disk files)
@@ -6,17 +6,17 @@
    Copyright (C) Jelmer Vernooij 2005
    Copyright (C) Tim Potter 2001
    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
-   
+
    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 3 of the License, or
    (at your option) any later version.
-   
+
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
-   
+
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 #include "dbwrap/dbwrap.h"
 #include "dbwrap/dbwrap_open.h"
 #include "lib/util/util_tdb.h"
-
+#include "libds/common/roles.h"
 
 /**
  * Fill in credentials for the machine trust account, from the secrets database.
- * 
+ *
  * @param cred Credentials structure to fill in
  * @retval NTSTATUS error detailing any failure
  */
-static NTSTATUS cli_credentials_set_secrets_lct(struct cli_credentials *cred, 
+static NTSTATUS cli_credentials_set_secrets_lct(struct cli_credentials *cred,
                                                struct loadparm_context *lp_ctx,
                                                struct ldb_context *ldb,
                                                const char *base,
-                                               const char *filter, 
+                                               const char *filter,
                                                time_t secrets_tdb_last_change_time,
                                                const char *secrets_tdb_password,
                                                char **error_string)
 {
        TALLOC_CTX *mem_ctx;
-       
+
        int ldb_ret;
        struct ldb_message *msg;
-       
+
        const char *machine_account;
        const char *password;
        const char *domain;
@@ -116,23 +116,23 @@ static NTSTATUS cli_credentials_set_secrets_lct(struct cli_credentials *cred,
                talloc_free(mem_ctx);
                return NT_STATUS_NOT_FOUND;
        }
-       
+
        if (lct == secrets_tdb_last_change_time && secrets_tdb_password && strcmp(password, secrets_tdb_password) != 0) {
                talloc_free(mem_ctx);
                return NT_STATUS_NOT_FOUND;
        }
-       
+
        cli_credentials_set_password_last_changed_time(cred, lct);
-       
+
        machine_account = ldb_msg_find_attr_as_string(msg, "samAccountName", NULL);
 
        if (!machine_account) {
                machine_account = ldb_msg_find_attr_as_string(msg, "servicePrincipalName", NULL);
-               
+
                if (!machine_account) {
                        const char *ldap_bind_dn = ldb_msg_find_attr_as_string(msg, "ldapBindDn", NULL);
                        if (!ldap_bind_dn) {
-                               *error_string = talloc_asprintf(cred, 
+                               *error_string = talloc_asprintf(cred,
                                                                "Could not find 'samAccountName', "
                                                                "'servicePrincipalName' or "
                                                                "'ldapBindDn' in secrets record: %s",
@@ -148,20 +148,20 @@ static NTSTATUS cli_credentials_set_secrets_lct(struct cli_credentials *cred,
 
        salt_principal = ldb_msg_find_attr_as_string(msg, "saltPrincipal", NULL);
        cli_credentials_set_salt_principal(cred, salt_principal);
-       
+
        sct = ldb_msg_find_attr_as_int(msg, "secureChannelType", 0);
-       if (sct) { 
+       if (sct) {
                cli_credentials_set_secure_channel_type(cred, sct);
        }
-       
+
        if (!password) {
                const struct ldb_val *nt_password_hash = ldb_msg_find_ldb_val(msg, "unicodePwd");
                struct samr_Password hash;
                ZERO_STRUCT(hash);
                if (nt_password_hash) {
-                       memcpy(hash.hash, nt_password_hash->data, 
+                       memcpy(hash.hash, nt_password_hash->data,
                               MIN(nt_password_hash->length, sizeof(hash.hash)));
-               
+
                        cli_credentials_set_nt_hash(cred, &hash, CRED_SPECIFIED);
                } else {
                        cli_credentials_set_password(cred, NULL, CRED_SPECIFIED);
@@ -170,7 +170,6 @@ static NTSTATUS cli_credentials_set_secrets_lct(struct cli_credentials *cred,
                cli_credentials_set_password(cred, password, CRED_SPECIFIED);
        }
 
-       
        domain = ldb_msg_find_attr_as_string(msg, "flatname", NULL);
        if (domain) {
                cli_credentials_set_domain(cred, domain, CRED_SPECIFIED);
@@ -196,22 +195,22 @@ static NTSTATUS cli_credentials_set_secrets_lct(struct cli_credentials *cred,
                talloc_free(keytab);
        }
        talloc_free(mem_ctx);
-       
+
        return NT_STATUS_OK;
 }
 
 
 /**
  * Fill in credentials for the machine trust account, from the secrets database.
- * 
+ *
  * @param cred Credentials structure to fill in
  * @retval NTSTATUS error detailing any failure
  */
-_PUBLIC_ NTSTATUS cli_credentials_set_secrets(struct cli_credentials *cred, 
+_PUBLIC_ NTSTATUS cli_credentials_set_secrets(struct cli_credentials *cred,
                                              struct loadparm_context *lp_ctx,
                                              struct ldb_context *ldb,
                                              const char *base,
-                                             const char *filter, 
+                                             const char *filter,
                                              char **error_string)
 {
        NTSTATUS status = cli_credentials_set_secrets_lct(cred, lp_ctx, ldb, base, filter, 0, NULL, error_string);
@@ -224,38 +223,68 @@ _PUBLIC_ NTSTATUS cli_credentials_set_secrets(struct cli_credentials *cred,
 
 /**
  * Fill in credentials for the machine trust account, from the secrets database.
- * 
+ *
  * @param cred Credentials structure to fill in
  * @retval NTSTATUS error detailing any failure
  */
 _PUBLIC_ NTSTATUS cli_credentials_set_machine_account(struct cli_credentials *cred,
                                                      struct loadparm_context *lp_ctx)
+{
+       struct db_context *db_ctx;
+       char *secrets_tdb_path;
+
+       secrets_tdb_path = lpcfg_private_db_path(cred, lp_ctx, "secrets");
+       if (secrets_tdb_path == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       db_ctx = dbwrap_local_open(cred, lp_ctx, secrets_tdb_path, 0,
+                                  TDB_DEFAULT, O_RDWR, 0600,
+                                  DBWRAP_LOCK_ORDER_1,
+                                  DBWRAP_FLAG_NONE);
+       TALLOC_FREE(secrets_tdb_path);
+
+       /*
+        * We do not check for errors here, we might not have a
+        * secrets.tdb at all, and so we just need to check the
+        * secrets.ldb
+        */
+       return cli_credentials_set_machine_account_db_ctx(cred, lp_ctx, db_ctx);
+}
+
+/**
+ * Fill in credentials for the machine trust account, from the
+ * secrets.ldb or passed in handle to secrets.tdb (perhaps in CTDB).
+ *
+ * This version is used in parts of the code that can link in the
+ * CTDB dbwrap backend, by passing down the already open handle.
+ *
+ * @param cred Credentials structure to fill in
+ * @param db_ctx dbwrap context for secrets.tdb
+ * @retval NTSTATUS error detailing any failure
+ */
+_PUBLIC_ NTSTATUS cli_credentials_set_machine_account_db_ctx(struct cli_credentials *cred,
+                                                            struct loadparm_context *lp_ctx,
+                                                            struct db_context *db_ctx)
 {
        NTSTATUS status;
        char *filter;
-       char *error_string;
+       char *error_string = NULL;
        const char *domain;
        bool secrets_tdb_password_more_recent;
        time_t secrets_tdb_lct = 0;
        char *secrets_tdb_password = NULL;
        char *secrets_tdb_old_password = NULL;
+       uint32_t secrets_tdb_secure_channel_type = SEC_CHAN_NULL;
+       int server_role = lpcfg_server_role(lp_ctx);
+       int security = lpcfg_security(lp_ctx);
        char *keystr;
        char *keystr_upper = NULL;
-       char *secrets_tdb;
-       struct db_context *db_ctx;
        TALLOC_CTX *tmp_ctx = talloc_named(cred, 0, "cli_credentials_set_secrets from ldb");
        if (!tmp_ctx) {
                return NT_STATUS_NO_MEMORY;
        }
-       secrets_tdb = lpcfg_private_db_path(cred, lp_ctx, "secrets");
-       if (!secrets_tdb) {
-               TALLOC_FREE(tmp_ctx);
-               return NT_STATUS_NO_MEMORY;
-       }
-               
-       db_ctx = dbwrap_local_open(cred, lp_ctx, secrets_tdb, 0,
-                                  TDB_DEFAULT, O_RDWR, 0600,
-                                  DBWRAP_LOCK_ORDER_1);
+
        /* Bleh, nasty recursion issues: We are setting a machine
         * account here, so we don't want the 'pending' flag around
         * any more */
@@ -286,6 +315,7 @@ _PUBLIC_ NTSTATUS cli_credentials_set_machine_account(struct cli_credentials *cr
                if (NT_STATUS_IS_OK(status)) {
                        secrets_tdb_password = (char *)dbuf.dptr;
                }
+
                keystr = talloc_asprintf(tmp_ctx, "%s/%s",
                                         SECRETS_MACHINE_PASSWORD_PREV,
                                         domain);
@@ -295,6 +325,16 @@ _PUBLIC_ NTSTATUS cli_credentials_set_machine_account(struct cli_credentials *cr
                if (NT_STATUS_IS_OK(status)) {
                        secrets_tdb_old_password = (char *)dbuf.dptr;
                }
+
+               keystr = talloc_asprintf(tmp_ctx, "%s/%s",
+                                        SECRETS_MACHINE_SEC_CHANNEL_TYPE,
+                                        domain);
+               keystr_upper = strupper_talloc(tmp_ctx, keystr);
+               status = dbwrap_fetch(db_ctx, tmp_ctx, string_tdb_data(keystr_upper),
+                                     &dbuf);
+               if (NT_STATUS_IS_OK(status) && dbuf.dsize == 4) {
+                       secrets_tdb_secure_channel_type = IVAL(dbuf.dptr,0);
+               }
        }
 
        filter = talloc_asprintf(cred, SECRETS_PRIMARY_DOMAIN_FILTER, 
@@ -316,36 +356,69 @@ _PUBLIC_ NTSTATUS cli_credentials_set_machine_account(struct cli_credentials *cr
        }
 
        if (secrets_tdb_password_more_recent) {
+               enum credentials_use_kerberos use_kerberos = CRED_DONT_USE_KERBEROS;
                char *machine_account = talloc_asprintf(tmp_ctx, "%s$", lpcfg_netbios_name(lp_ctx));
                cli_credentials_set_password(cred, secrets_tdb_password, CRED_SPECIFIED);
                cli_credentials_set_old_password(cred, secrets_tdb_old_password, CRED_SPECIFIED);
                cli_credentials_set_domain(cred, domain, CRED_SPECIFIED);
+               if (strequal(domain, lpcfg_workgroup(lp_ctx))) {
+                       cli_credentials_set_realm(cred, lpcfg_realm(lp_ctx), CRED_SPECIFIED);
+
+                       switch (server_role) {
+                       case ROLE_DOMAIN_MEMBER:
+                               if (security != SEC_ADS) {
+                                       break;
+                               }
+                               /* fall through */
+                       case ROLE_ACTIVE_DIRECTORY_DC:
+                               use_kerberos = CRED_AUTO_USE_KERBEROS;
+                               break;
+                       }
+               }
+               cli_credentials_set_kerberos_state(cred, use_kerberos);
                cli_credentials_set_username(cred, machine_account, CRED_SPECIFIED);
+               cli_credentials_set_password_last_changed_time(cred, secrets_tdb_lct);
+               cli_credentials_set_secure_channel_type(cred, secrets_tdb_secure_channel_type);
+               status = NT_STATUS_OK;
        } else if (!NT_STATUS_IS_OK(status)) {
                if (db_ctx) {
-                       error_string = talloc_asprintf(cred,
-                                                      "Failed to fetch machine account password from "
-                                                      "secrets.ldb: %s and failed to fetch %s from %s",
-                                                      error_string, keystr_upper, secrets_tdb);
+                       error_string
+                               = talloc_asprintf(cred,
+                                                 "Failed to fetch machine account password for %s from both "
+                                                 "secrets.ldb (%s) and from %s",
+                                                 domain,
+                                                 error_string == NULL ? "error" : error_string,
+                                                 dbwrap_name(db_ctx));
                } else {
+                       char *secrets_tdb_path;
+
+                       secrets_tdb_path = lpcfg_private_db_path(tmp_ctx,
+                                                                lp_ctx,
+                                                                "secrets");
+                       if (secrets_tdb_path == NULL) {
+                               return NT_STATUS_NO_MEMORY;
+                       }
+
                        error_string = talloc_asprintf(cred,
                                                       "Failed to fetch machine account password from "
                                                       "secrets.ldb: %s and failed to open %s",
-                                                      error_string, secrets_tdb);
+                                                      error_string == NULL ? "error" : error_string,
+                                                      secrets_tdb_path);
                }
-               DEBUG(1, ("Could not find machine account in secrets database: %s: %s\n", 
-                         error_string, nt_errstr(status)));
+               DEBUG(1, ("Could not find machine account in secrets database: %s: %s\n",
+                         error_string == NULL ? "error" : error_string,
+                         nt_errstr(status)));
                /* set anonymous as the fallback, if the machine account won't work */
                cli_credentials_set_anonymous(cred);
        }
-       
+
        TALLOC_FREE(tmp_ctx);
        return status;
 }
 
 /**
- * Fill in credentials for a particular prinicpal, from the secrets database.
- * 
+ * Fill in credentials for a particular principal, from the secrets database.
+ *
  * @param cred Credentials structure to fill in
  * @retval NTSTATUS error detailing any failure
  */
@@ -355,7 +428,7 @@ _PUBLIC_ NTSTATUS cli_credentials_set_stored_principal(struct cli_credentials *c
 {
        NTSTATUS status;
        char *filter;
-       char *error_string;
+       char *error_string = NULL;
        /* Bleh, nasty recursion issues: We are setting a machine
         * account here, so we don't want the 'pending' flag around
         * any more */
@@ -368,7 +441,9 @@ _PUBLIC_ NTSTATUS cli_credentials_set_stored_principal(struct cli_credentials *c
                                             SECRETS_PRINCIPALS_DN, filter,
                                             0, NULL, &error_string);
        if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(1, ("Could not find %s principal in secrets database: %s: %s\n", serviceprincipal, nt_errstr(status), error_string));
+               DEBUG(1, ("Could not find %s principal in secrets database: %s: %s\n",
+                         serviceprincipal, nt_errstr(status),
+                         error_string ? error_string : "<no error>"));
        }
        return status;
 }
@@ -376,9 +451,9 @@ _PUBLIC_ NTSTATUS cli_credentials_set_stored_principal(struct cli_credentials *c
 /**
  * Ask that when required, the credentials system will be filled with
  * machine trust account, from the secrets database.
- * 
+ *
  * @param cred Credentials structure to fill in
- * @note This function is used to call the above function after, rather 
+ * @note This function is used to call the above function after, rather
  *       than during, popt processing.
  *
  */