2 Unix SMB/CIFS implementation.
4 User credentials handling (as regards on-disk files)
6 Copyright (C) Jelmer Vernooij 2005
7 Copyright (C) Tim Potter 2001
8 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "lib/events/events.h"
27 #include "librpc/gen_ndr/samr.h" /* for struct samrPassword */
28 #include "param/secrets.h"
29 #include "system/filesys.h"
30 #include "auth/credentials/credentials.h"
31 #include "auth/credentials/credentials_internal.h"
32 #include "auth/credentials/credentials_proto.h"
33 #include "auth/credentials/credentials_krb5.h"
34 #include "auth/kerberos/kerberos_util.h"
35 #include "param/param.h"
36 #include "lib/events/events.h"
37 #include "dsdb/samdb/samdb.h"
38 #include "source3/include/secrets.h"
39 #include "dbwrap/dbwrap.h"
40 #include "dbwrap/dbwrap_open.h"
41 #include "lib/util/util_tdb.h"
42 #include "libds/common/roles.h"
45 #define DBGC_CLASS DBGC_AUTH
48 * Fill in credentials for the machine trust account, from the secrets database.
50 * @param cred Credentials structure to fill in
51 * @retval NTSTATUS error detailing any failure
53 static NTSTATUS cli_credentials_set_secrets_lct(struct cli_credentials *cred,
54 struct loadparm_context *lp_ctx,
55 struct ldb_context *ldb,
58 time_t secrets_tdb_last_change_time,
59 const char *secrets_tdb_password,
65 struct ldb_message *msg;
67 const char *machine_account;
71 enum netr_SchannelType sct;
72 const char *salt_principal;
74 const struct ldb_val *whenChanged;
77 /* ok, we are going to get it now, don't recurse back here */
78 cred->machine_account_pending = false;
80 /* some other parts of the system will key off this */
81 cred->machine_account = true;
83 mem_ctx = talloc_named(cred, 0, "cli_credentials_set_secrets from ldb");
86 /* Local secrets are stored in secrets.ldb */
87 ldb = secrets_db_connect(mem_ctx, lp_ctx);
89 *error_string = talloc_strdup(cred, "Could not open secrets.ldb");
91 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
95 ldb_ret = dsdb_search_one(ldb, mem_ctx, &msg,
96 ldb_dn_new(mem_ctx, ldb, base),
98 NULL, 0, "%s", filter);
100 if (ldb_ret != LDB_SUCCESS) {
101 *error_string = talloc_asprintf(cred, "Could not find entry to match filter: '%s' base: '%s': %s: %s",
102 filter, base ? base : "",
103 ldb_strerror(ldb_ret), ldb_errstring(ldb));
104 talloc_free(mem_ctx);
105 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
108 password = ldb_msg_find_attr_as_string(msg, "secret", NULL);
110 whenChanged = ldb_msg_find_ldb_val(msg, "whenChanged");
111 if (!whenChanged || ldb_val_to_time(whenChanged, &lct) != LDB_SUCCESS) {
112 /* This attribute is mandetory */
113 talloc_free(mem_ctx);
114 return NT_STATUS_NOT_FOUND;
117 /* Don't set secrets.ldb info if the secrets.tdb entry was more recent */
118 if (lct < secrets_tdb_last_change_time) {
119 talloc_free(mem_ctx);
120 return NT_STATUS_NOT_FOUND;
123 if (lct == secrets_tdb_last_change_time && secrets_tdb_password && strcmp(password, secrets_tdb_password) != 0) {
124 talloc_free(mem_ctx);
125 return NT_STATUS_NOT_FOUND;
128 cli_credentials_set_password_last_changed_time(cred, lct);
130 machine_account = ldb_msg_find_attr_as_string(msg, "samAccountName", NULL);
132 if (!machine_account) {
133 machine_account = ldb_msg_find_attr_as_string(msg, "servicePrincipalName", NULL);
135 if (!machine_account) {
136 const char *ldap_bind_dn = ldb_msg_find_attr_as_string(msg, "ldapBindDn", NULL);
138 *error_string = talloc_asprintf(cred,
139 "Could not find 'samAccountName', "
140 "'servicePrincipalName' or "
141 "'ldapBindDn' in secrets record: %s",
142 ldb_dn_get_linearized(msg->dn));
143 talloc_free(mem_ctx);
144 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
146 /* store bind dn in credentials */
147 cli_credentials_set_bind_dn(cred, ldap_bind_dn);
152 salt_principal = ldb_msg_find_attr_as_string(msg, "saltPrincipal", NULL);
153 cli_credentials_set_salt_principal(cred, salt_principal);
155 sct = ldb_msg_find_attr_as_int(msg, "secureChannelType", 0);
157 cli_credentials_set_secure_channel_type(cred, sct);
161 const struct ldb_val *nt_password_hash = ldb_msg_find_ldb_val(msg, "unicodePwd");
162 struct samr_Password hash;
164 if (nt_password_hash) {
165 memcpy(hash.hash, nt_password_hash->data,
166 MIN(nt_password_hash->length, sizeof(hash.hash)));
168 cli_credentials_set_nt_hash(cred, &hash, CRED_SPECIFIED);
170 cli_credentials_set_password(cred, NULL, CRED_SPECIFIED);
173 cli_credentials_set_password(cred, password, CRED_SPECIFIED);
176 domain = ldb_msg_find_attr_as_string(msg, "flatname", NULL);
178 cli_credentials_set_domain(cred, domain, CRED_SPECIFIED);
181 realm = ldb_msg_find_attr_as_string(msg, "realm", NULL);
183 cli_credentials_set_realm(cred, realm, CRED_SPECIFIED);
186 if (machine_account) {
187 cli_credentials_set_username(cred, machine_account, CRED_SPECIFIED);
190 cli_credentials_set_kvno(cred, ldb_msg_find_attr_as_int(msg, "msDS-KeyVersionNumber", 0));
192 /* If there was an external keytab specified by reference in
193 * the LDB, then use this. Otherwise we will make one up
194 * (chewing CPU time) from the password */
195 keytab = keytab_name_from_msg(cred, ldb, msg);
197 cli_credentials_set_keytab_name(cred, lp_ctx, keytab, CRED_SPECIFIED);
200 talloc_free(mem_ctx);
207 * Fill in credentials for the machine trust account, from the secrets database.
209 * @param cred Credentials structure to fill in
210 * @retval NTSTATUS error detailing any failure
212 _PUBLIC_ NTSTATUS cli_credentials_set_secrets(struct cli_credentials *cred,
213 struct loadparm_context *lp_ctx,
214 struct ldb_context *ldb,
219 NTSTATUS status = cli_credentials_set_secrets_lct(cred, lp_ctx, ldb, base, filter, 0, NULL, error_string);
220 if (!NT_STATUS_IS_OK(status)) {
221 /* set anonymous as the fallback, if the machine account won't work */
222 cli_credentials_set_anonymous(cred);
228 * Fill in credentials for the machine trust account, from the secrets database.
230 * @param cred Credentials structure to fill in
231 * @retval NTSTATUS error detailing any failure
233 _PUBLIC_ NTSTATUS cli_credentials_set_machine_account(struct cli_credentials *cred,
234 struct loadparm_context *lp_ctx)
236 struct db_context *db_ctx;
237 char *secrets_tdb_path;
239 secrets_tdb_path = lpcfg_private_db_path(cred, lp_ctx, "secrets");
240 if (secrets_tdb_path == NULL) {
241 return NT_STATUS_NO_MEMORY;
244 db_ctx = dbwrap_local_open(cred, lp_ctx, secrets_tdb_path, 0,
245 TDB_DEFAULT, O_RDWR, 0600,
248 TALLOC_FREE(secrets_tdb_path);
251 * We do not check for errors here, we might not have a
252 * secrets.tdb at all, and so we just need to check the
255 return cli_credentials_set_machine_account_db_ctx(cred, lp_ctx, db_ctx);
259 * Fill in credentials for the machine trust account, from the
260 * secrets.ldb or passed in handle to secrets.tdb (perhaps in CTDB).
262 * This version is used in parts of the code that can link in the
263 * CTDB dbwrap backend, by passing down the already open handle.
265 * @param cred Credentials structure to fill in
266 * @param db_ctx dbwrap context for secrets.tdb
267 * @retval NTSTATUS error detailing any failure
269 _PUBLIC_ NTSTATUS cli_credentials_set_machine_account_db_ctx(struct cli_credentials *cred,
270 struct loadparm_context *lp_ctx,
271 struct db_context *db_ctx)
275 char *error_string = NULL;
277 bool secrets_tdb_password_more_recent;
278 time_t secrets_tdb_lct = 0;
279 char *secrets_tdb_password = NULL;
280 char *secrets_tdb_old_password = NULL;
281 uint32_t secrets_tdb_secure_channel_type = SEC_CHAN_NULL;
282 int server_role = lpcfg_server_role(lp_ctx);
283 int security = lpcfg_security(lp_ctx);
285 char *keystr_upper = NULL;
286 TALLOC_CTX *tmp_ctx = talloc_named(cred, 0, "cli_credentials_set_secrets from ldb");
288 return NT_STATUS_NO_MEMORY;
291 /* Bleh, nasty recursion issues: We are setting a machine
292 * account here, so we don't want the 'pending' flag around
294 cred->machine_account_pending = false;
296 /* We have to do this, as the fallback in
297 * cli_credentials_set_secrets is to run as anonymous, so the domain is wiped */
298 domain = cli_credentials_get_domain(cred);
302 keystr = talloc_asprintf(tmp_ctx, "%s/%s",
303 SECRETS_MACHINE_LAST_CHANGE_TIME,
305 keystr_upper = strupper_talloc(tmp_ctx, keystr);
306 status = dbwrap_fetch(db_ctx, tmp_ctx, string_tdb_data(keystr_upper),
308 if (NT_STATUS_IS_OK(status) && dbuf.dsize == 4) {
309 secrets_tdb_lct = IVAL(dbuf.dptr,0);
312 keystr = talloc_asprintf(tmp_ctx, "%s/%s",
313 SECRETS_MACHINE_PASSWORD,
315 keystr_upper = strupper_talloc(tmp_ctx, keystr);
316 status = dbwrap_fetch(db_ctx, tmp_ctx, string_tdb_data(keystr_upper),
318 if (NT_STATUS_IS_OK(status)) {
319 secrets_tdb_password = (char *)dbuf.dptr;
322 keystr = talloc_asprintf(tmp_ctx, "%s/%s",
323 SECRETS_MACHINE_PASSWORD_PREV,
325 keystr_upper = strupper_talloc(tmp_ctx, keystr);
326 status = dbwrap_fetch(db_ctx, tmp_ctx, string_tdb_data(keystr_upper),
328 if (NT_STATUS_IS_OK(status)) {
329 secrets_tdb_old_password = (char *)dbuf.dptr;
332 keystr = talloc_asprintf(tmp_ctx, "%s/%s",
333 SECRETS_MACHINE_SEC_CHANNEL_TYPE,
335 keystr_upper = strupper_talloc(tmp_ctx, keystr);
336 status = dbwrap_fetch(db_ctx, tmp_ctx, string_tdb_data(keystr_upper),
338 if (NT_STATUS_IS_OK(status) && dbuf.dsize == 4) {
339 secrets_tdb_secure_channel_type = IVAL(dbuf.dptr,0);
343 filter = talloc_asprintf(cred, SECRETS_PRIMARY_DOMAIN_FILTER,
345 status = cli_credentials_set_secrets_lct(cred, lp_ctx, NULL,
346 SECRETS_PRIMARY_DOMAIN_DN,
347 filter, secrets_tdb_lct, secrets_tdb_password, &error_string);
348 if (secrets_tdb_password == NULL) {
349 secrets_tdb_password_more_recent = false;
350 } else if (NT_STATUS_EQUAL(NT_STATUS_CANT_ACCESS_DOMAIN_INFO, status)
351 || NT_STATUS_EQUAL(NT_STATUS_NOT_FOUND, status)) {
352 secrets_tdb_password_more_recent = true;
353 } else if (secrets_tdb_lct > cli_credentials_get_password_last_changed_time(cred)) {
354 secrets_tdb_password_more_recent = true;
355 } else if (secrets_tdb_lct == cli_credentials_get_password_last_changed_time(cred)) {
356 secrets_tdb_password_more_recent = strcmp(secrets_tdb_password, cli_credentials_get_password(cred)) != 0;
358 secrets_tdb_password_more_recent = false;
361 if (secrets_tdb_password_more_recent) {
362 enum credentials_use_kerberos use_kerberos = CRED_DONT_USE_KERBEROS;
363 char *machine_account = talloc_asprintf(tmp_ctx, "%s$", lpcfg_netbios_name(lp_ctx));
364 cli_credentials_set_password(cred, secrets_tdb_password, CRED_SPECIFIED);
365 cli_credentials_set_old_password(cred, secrets_tdb_old_password, CRED_SPECIFIED);
366 cli_credentials_set_domain(cred, domain, CRED_SPECIFIED);
367 if (strequal(domain, lpcfg_workgroup(lp_ctx))) {
368 cli_credentials_set_realm(cred, lpcfg_realm(lp_ctx), CRED_SPECIFIED);
370 switch (server_role) {
371 case ROLE_DOMAIN_MEMBER:
372 if (security != SEC_ADS) {
377 case ROLE_ACTIVE_DIRECTORY_DC:
378 use_kerberos = CRED_AUTO_USE_KERBEROS;
382 cli_credentials_set_kerberos_state(cred, use_kerberos);
383 cli_credentials_set_username(cred, machine_account, CRED_SPECIFIED);
384 cli_credentials_set_password_last_changed_time(cred, secrets_tdb_lct);
385 cli_credentials_set_secure_channel_type(cred, secrets_tdb_secure_channel_type);
386 status = NT_STATUS_OK;
387 } else if (!NT_STATUS_IS_OK(status)) {
390 = talloc_asprintf(cred,
391 "Failed to fetch machine account password for %s from both "
392 "secrets.ldb (%s) and from %s",
394 error_string == NULL ? "error" : error_string,
395 dbwrap_name(db_ctx));
397 char *secrets_tdb_path;
399 secrets_tdb_path = lpcfg_private_db_path(tmp_ctx,
402 if (secrets_tdb_path == NULL) {
403 return NT_STATUS_NO_MEMORY;
406 error_string = talloc_asprintf(cred,
407 "Failed to fetch machine account password from "
408 "secrets.ldb: %s and failed to open %s",
409 error_string == NULL ? "error" : error_string,
412 DEBUG(1, ("Could not find machine account in secrets database: %s: %s\n",
413 error_string == NULL ? "error" : error_string,
415 /* set anonymous as the fallback, if the machine account won't work */
416 cli_credentials_set_anonymous(cred);
419 TALLOC_FREE(tmp_ctx);
424 * Fill in credentials for a particular principal, from the secrets database.
426 * @param cred Credentials structure to fill in
427 * @retval NTSTATUS error detailing any failure
429 _PUBLIC_ NTSTATUS cli_credentials_set_stored_principal(struct cli_credentials *cred,
430 struct loadparm_context *lp_ctx,
431 const char *serviceprincipal)
435 char *error_string = NULL;
436 /* Bleh, nasty recursion issues: We are setting a machine
437 * account here, so we don't want the 'pending' flag around
439 cred->machine_account_pending = false;
440 filter = talloc_asprintf(cred, SECRETS_PRINCIPAL_SEARCH,
441 cli_credentials_get_realm(cred),
442 cli_credentials_get_domain(cred),
444 status = cli_credentials_set_secrets_lct(cred, lp_ctx, NULL,
445 SECRETS_PRINCIPALS_DN, filter,
446 0, NULL, &error_string);
447 if (!NT_STATUS_IS_OK(status)) {
448 DEBUG(1, ("Could not find %s principal in secrets database: %s: %s\n",
449 serviceprincipal, nt_errstr(status),
450 error_string ? error_string : "<no error>"));
456 * Ask that when required, the credentials system will be filled with
457 * machine trust account, from the secrets database.
459 * @param cred Credentials structure to fill in
460 * @note This function is used to call the above function after, rather
461 * than during, popt processing.
464 _PUBLIC_ void cli_credentials_set_machine_account_pending(struct cli_credentials *cred,
465 struct loadparm_context *lp_ctx)
467 cred->machine_account_pending = true;
468 cred->machine_account_pending_lp_ctx = lp_ctx;