2 Unix SMB/CIFS implementation.
3 Copyright (C) Andrew Tridgell 1992-2001
4 Copyright (C) Andrew Bartlett 2002
5 Copyright (C) Rafal Szczesniak 2002
6 Copyright (C) Tim Potter 2001
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 /* the Samba secrets database stores any generated, private information
23 such as the local SID and machine trust password */
27 #include "../libcli/auth/libcli_auth.h"
29 #include "dbwrap/dbwrap.h"
30 #include "../librpc/ndr/libndr.h"
32 #include "libcli/security/security.h"
34 #include "librpc/gen_ndr/libnet_join.h"
35 #include "librpc/gen_ndr/ndr_secrets.h"
36 #include "lib/crypto/crypto.h"
37 #include "lib/krb5_wrap/krb5_samba.h"
38 #include "lib/util/time_basic.h"
39 #include "../libds/common/flags.h"
40 #include "lib/util/string_wrappers.h"
43 #define DBGC_CLASS DBGC_PASSDB
45 static char *domain_info_keystr(const char *domain
);
47 static char *des_salt_key(const char *realm
);
50 * Form a key for fetching the domain sid
52 * @param domain domain name
56 static const char *domain_sid_keystr(const char *domain
)
60 keystr
= talloc_asprintf_strupper_m(talloc_tos(), "%s/%s",
61 SECRETS_DOMAIN_SID
, domain
);
62 SMB_ASSERT(keystr
!= NULL
);
66 static const char *domain_guid_keystr(const char *domain
)
70 keystr
= talloc_asprintf_strupper_m(talloc_tos(), "%s/%s",
71 SECRETS_DOMAIN_GUID
, domain
);
72 SMB_ASSERT(keystr
!= NULL
);
76 static const char *protect_ids_keystr(const char *domain
)
80 keystr
= talloc_asprintf_strupper_m(talloc_tos(), "%s/%s",
81 SECRETS_PROTECT_IDS
, domain
);
82 SMB_ASSERT(keystr
!= NULL
);
86 /* N O T E: never use this outside of passdb modules that store the SID on their own */
87 bool secrets_mark_domain_protected(const char *domain
)
91 ret
= secrets_store(protect_ids_keystr(domain
), "TRUE", 5);
93 DEBUG(0, ("Failed to protect the Domain IDs\n"));
98 bool secrets_clear_domain_protection(const char *domain
)
101 void *protection
= secrets_fetch(protect_ids_keystr(domain
), NULL
);
104 SAFE_FREE(protection
);
105 ret
= secrets_delete_entry(protect_ids_keystr(domain
));
107 DEBUG(0, ("Failed to remove Domain IDs protection\n"));
114 bool secrets_store_domain_sid(const char *domain
, const struct dom_sid
*sid
)
118 struct dom_sid clean_sid
= { 0 };
120 protect_ids
= secrets_fetch(protect_ids_keystr(domain
), NULL
);
122 if (strncmp(protect_ids
, "TRUE", 4)) {
123 DEBUG(0, ("Refusing to store a Domain SID, "
124 "it has been marked as protected!\n"));
125 SAFE_FREE(protect_ids
);
129 SAFE_FREE(protect_ids
);
132 * use a copy to prevent uninitialized memory from being carried over
135 sid_copy(&clean_sid
, sid
);
137 ret
= secrets_store(domain_sid_keystr(domain
),
139 sizeof(struct dom_sid
));
141 /* Force a re-query */
144 * Do not call get_global_domain_sid() here, or we will call it
147 reset_global_sam_sid();
152 bool secrets_fetch_domain_sid(const char *domain
, struct dom_sid
*sid
)
154 struct dom_sid
*dyn_sid
;
157 dyn_sid
= (struct dom_sid
*)secrets_fetch(domain_sid_keystr(domain
), &size
);
162 if (size
!= sizeof(struct dom_sid
)) {
172 bool secrets_store_domain_guid(const char *domain
, const struct GUID
*guid
)
177 protect_ids
= secrets_fetch(protect_ids_keystr(domain
), NULL
);
179 if (strncmp(protect_ids
, "TRUE", 4)) {
180 DEBUG(0, ("Refusing to store a Domain SID, "
181 "it has been marked as protected!\n"));
182 SAFE_FREE(protect_ids
);
186 SAFE_FREE(protect_ids
);
188 key
= domain_guid_keystr(domain
);
189 return secrets_store(key
, guid
, sizeof(struct GUID
));
192 bool secrets_fetch_domain_guid(const char *domain
, struct GUID
*guid
)
194 struct GUID
*dyn_guid
;
197 struct GUID new_guid
;
199 key
= domain_guid_keystr(domain
);
200 dyn_guid
= (struct GUID
*)secrets_fetch(key
, &size
);
203 if (lp_server_role() == ROLE_DOMAIN_PDC
||
204 lp_server_role() == ROLE_IPA_DC
) {
205 new_guid
= GUID_random();
206 if (!secrets_store_domain_guid(domain
, &new_guid
))
208 dyn_guid
= (struct GUID
*)secrets_fetch(key
, &size
);
210 if (dyn_guid
== NULL
) {
215 if (size
!= sizeof(struct GUID
)) {
216 DEBUG(1,("UUID size %d is wrong!\n", (int)size
));
227 * Form a key for fetching the machine trust account sec channel type
229 * @param domain domain name
233 static const char *machine_sec_channel_type_keystr(const char *domain
)
237 keystr
= talloc_asprintf_strupper_m(talloc_tos(), "%s/%s",
238 SECRETS_MACHINE_SEC_CHANNEL_TYPE
,
240 SMB_ASSERT(keystr
!= NULL
);
245 * Form a key for fetching the machine trust account last change time
247 * @param domain domain name
251 static const char *machine_last_change_time_keystr(const char *domain
)
255 keystr
= talloc_asprintf_strupper_m(talloc_tos(), "%s/%s",
256 SECRETS_MACHINE_LAST_CHANGE_TIME
,
258 SMB_ASSERT(keystr
!= NULL
);
264 * Form a key for fetching the machine previous trust account password
266 * @param domain domain name
270 static const char *machine_prev_password_keystr(const char *domain
)
274 keystr
= talloc_asprintf_strupper_m(talloc_tos(), "%s/%s",
275 SECRETS_MACHINE_PASSWORD_PREV
, domain
);
276 SMB_ASSERT(keystr
!= NULL
);
281 * Form a key for fetching the machine trust account password
283 * @param domain domain name
287 static const char *machine_password_keystr(const char *domain
)
291 keystr
= talloc_asprintf_strupper_m(talloc_tos(), "%s/%s",
292 SECRETS_MACHINE_PASSWORD
, domain
);
293 SMB_ASSERT(keystr
!= NULL
);
298 * Form a key for fetching the machine trust account password
300 * @param domain domain name
302 * @return stored password's key
304 static const char *trust_keystr(const char *domain
)
308 keystr
= talloc_asprintf_strupper_m(talloc_tos(), "%s/%s",
309 SECRETS_MACHINE_ACCT_PASS
, domain
);
310 SMB_ASSERT(keystr
!= NULL
);
314 /************************************************************************
315 Routine to get the default secure channel type for trust accounts
316 ************************************************************************/
318 enum netr_SchannelType
get_default_sec_channel(void)
323 return SEC_CHAN_WKSTA
;
327 /************************************************************************
328 Routine to get the trust account password for a domain.
329 This only tries to get the legacy hashed version of the password.
330 The user of this function must have locked the trust password file using
331 the above secrets_lock_trust_account_password().
332 ************************************************************************/
334 bool secrets_fetch_trust_account_password_legacy(const char *domain
,
336 time_t *pass_last_set_time
,
337 enum netr_SchannelType
*channel
)
339 struct machine_acct_pass
*pass
;
342 if (!(pass
= (struct machine_acct_pass
*)secrets_fetch(
343 trust_keystr(domain
), &size
))) {
344 DEBUG(5, ("secrets_fetch failed!\n"));
348 if (size
!= sizeof(*pass
)) {
349 DEBUG(0, ("secrets were of incorrect size!\n"));
350 BURN_FREE(pass
, size
);
354 if (pass_last_set_time
) {
355 *pass_last_set_time
= pass
->mod_time
;
357 memcpy(ret_pwd
, pass
->hash
, 16);
360 *channel
= get_default_sec_channel();
363 BURN_FREE(pass
, size
);
367 /************************************************************************
368 Routine to delete all information related to the domain joined machine.
369 ************************************************************************/
371 bool secrets_delete_machine_password_ex(const char *domain
, const char *realm
)
373 const char *tmpkey
= NULL
;
376 tmpkey
= domain_info_keystr(domain
);
377 ok
= secrets_delete(tmpkey
);
383 tmpkey
= des_salt_key(domain
);
384 ok
= secrets_delete(tmpkey
);
390 tmpkey
= domain_guid_keystr(domain
);
391 ok
= secrets_delete(tmpkey
);
396 tmpkey
= machine_prev_password_keystr(domain
);
397 ok
= secrets_delete(tmpkey
);
402 tmpkey
= machine_password_keystr(domain
);
403 ok
= secrets_delete(tmpkey
);
408 tmpkey
= machine_sec_channel_type_keystr(domain
);
409 ok
= secrets_delete(tmpkey
);
414 tmpkey
= machine_last_change_time_keystr(domain
);
415 ok
= secrets_delete(tmpkey
);
420 tmpkey
= domain_sid_keystr(domain
);
421 ok
= secrets_delete(tmpkey
);
429 /************************************************************************
430 Routine to delete the domain sid
431 ************************************************************************/
433 bool secrets_delete_domain_sid(const char *domain
)
435 return secrets_delete_entry(domain_sid_keystr(domain
));
438 /************************************************************************
439 Set the machine trust account password, the old pw and last change
440 time, domain SID and salting principals based on values passed in
441 (added to support the secrets_tdb_sync module on secrets.ldb)
442 ************************************************************************/
444 bool secrets_store_machine_pw_sync(const char *pass
, const char *oldpass
, const char *domain
,
446 const char *salting_principal
, uint32_t supported_enc_types
,
447 const struct dom_sid
*domain_sid
, uint32_t last_change_time
,
448 uint32_t secure_channel_type
,
452 uint8_t last_change_time_store
[4];
453 TALLOC_CTX
*frame
= talloc_stackframe();
454 uint8_t sec_channel_bytes
[4];
457 secrets_delete_machine_password_ex(domain
, realm
);
462 ret
= secrets_store(machine_password_keystr(domain
), pass
, strlen(pass
)+1);
469 ret
= secrets_store(machine_prev_password_keystr(domain
), oldpass
, strlen(oldpass
)+1);
471 ret
= secrets_delete(machine_prev_password_keystr(domain
));
478 if (secure_channel_type
== 0) {
479 /* We delete this and instead have the read code fall back to
480 * a default based on server role, as our caller can't specify
481 * this with any more certainty */
482 ret
= secrets_delete(machine_sec_channel_type_keystr(domain
));
488 SIVAL(&sec_channel_bytes
, 0, secure_channel_type
);
489 ret
= secrets_store(machine_sec_channel_type_keystr(domain
),
490 &sec_channel_bytes
, sizeof(sec_channel_bytes
));
497 SIVAL(&last_change_time_store
, 0, last_change_time
);
498 ret
= secrets_store(machine_last_change_time_keystr(domain
),
499 &last_change_time_store
, sizeof(last_change_time
));
506 ret
= secrets_store_domain_sid(domain
, domain_sid
);
514 char *key
= des_salt_key(realm
);
516 if (salting_principal
!= NULL
) {
517 ret
= secrets_store(key
,
519 strlen(salting_principal
)+1);
521 ret
= secrets_delete(key
);
529 /************************************************************************
530 Return the standard DES salt key
531 ************************************************************************/
533 char* kerberos_standard_des_salt( void )
537 fstr_sprintf( salt
, "host/%s.%s@", lp_netbios_name(), lp_realm() );
538 (void)strlower_m( salt
);
539 fstrcat( salt
, lp_realm() );
541 return SMB_STRDUP( salt
);
544 /************************************************************************
545 ************************************************************************/
547 static char *des_salt_key(const char *realm
)
551 keystr
= talloc_asprintf_strupper_m(talloc_tos(), "%s/DES/%s",
552 SECRETS_SALTING_PRINCIPAL
,
554 SMB_ASSERT(keystr
!= NULL
);
558 /************************************************************************
559 ************************************************************************/
561 bool kerberos_secrets_store_des_salt( const char* salt
)
566 key
= des_salt_key(lp_realm());
568 DEBUG(0,("kerberos_secrets_store_des_salt: failed to generate key!\n"));
573 DEBUG(8,("kerberos_secrets_store_des_salt: deleting salt\n"));
574 secrets_delete_entry( key
);
578 DEBUG(3,("kerberos_secrets_store_des_salt: Storing salt \"%s\"\n", salt
));
580 ret
= secrets_store( key
, salt
, strlen(salt
)+1 );
587 /************************************************************************
588 ************************************************************************/
591 char* kerberos_secrets_fetch_des_salt( void )
595 key
= des_salt_key(lp_realm());
597 DEBUG(0,("kerberos_secrets_fetch_des_salt: failed to generate key!\n"));
601 salt
= (char*)secrets_fetch( key
, NULL
);
608 /************************************************************************
609 Routine to get the salting principal for this service.
610 Caller must free if return is not null.
611 ************************************************************************/
613 char *kerberos_secrets_fetch_salt_princ(void)
616 /* lookup new key first */
618 salt_princ_s
= kerberos_secrets_fetch_des_salt();
619 if (salt_princ_s
== NULL
) {
620 /* fall back to host/machine.realm@REALM */
621 salt_princ_s
= kerberos_standard_des_salt();
627 /************************************************************************
628 Routine to fetch the previous plaintext machine account password for a realm
629 the password is assumed to be a null terminated ascii string.
630 ************************************************************************/
632 char *secrets_fetch_prev_machine_password(const char *domain
)
634 return (char *)secrets_fetch(machine_prev_password_keystr(domain
), NULL
);
637 /************************************************************************
638 Routine to fetch the last change time of the machine account password
640 ************************************************************************/
642 time_t secrets_fetch_pass_last_set_time(const char *domain
)
644 uint32_t *last_set_time
;
645 time_t pass_last_set_time
;
647 last_set_time
= secrets_fetch(machine_last_change_time_keystr(domain
),
650 pass_last_set_time
= IVAL(last_set_time
,0);
651 SAFE_FREE(last_set_time
);
653 pass_last_set_time
= 0;
656 return pass_last_set_time
;
659 /************************************************************************
660 Routine to fetch the plaintext machine account password for a realm
661 the password is assumed to be a null terminated ascii string.
662 ************************************************************************/
664 char *secrets_fetch_machine_password(const char *domain
,
665 time_t *pass_last_set_time
,
666 enum netr_SchannelType
*channel
)
669 ret
= (char *)secrets_fetch(machine_password_keystr(domain
), NULL
);
671 if (pass_last_set_time
) {
672 *pass_last_set_time
= secrets_fetch_pass_last_set_time(domain
);
677 uint32_t *channel_type
;
678 channel_type
= (unsigned int *)secrets_fetch(machine_sec_channel_type_keystr(domain
), &size
);
680 *channel
= IVAL(channel_type
,0);
681 SAFE_FREE(channel_type
);
683 *channel
= get_default_sec_channel();
690 static int password_nt_hash_destructor(struct secrets_domain_info1_password
*pw
)
692 ZERO_STRUCT(pw
->nt_hash
);
697 static int setup_password_zeroing(struct secrets_domain_info1_password
*pw
)
702 talloc_keep_secret(pw
->cleartext_blob
.data
);
703 talloc_set_destructor(pw
, password_nt_hash_destructor
);
704 for (i
= 0; i
< pw
->num_keys
; i
++) {
705 talloc_keep_secret(pw
->keys
[i
].value
.data
);
712 static char *domain_info_keystr(const char *domain
)
716 keystr
= talloc_asprintf_strupper_m(talloc_tos(), "%s/%s",
717 SECRETS_MACHINE_DOMAIN_INFO
,
719 SMB_ASSERT(keystr
!= NULL
);
723 /************************************************************************
724 Routine to get account password to trusted domain
725 ************************************************************************/
727 static NTSTATUS
secrets_fetch_domain_info1_by_key(const char *key
,
729 struct secrets_domain_info1
**_info1
)
731 struct secrets_domain_infoB sdib
= { .version
= 0, };
732 enum ndr_err_code ndr_err
;
733 /* unpacking structures */
736 /* fetching trusted domain password structure */
737 blob
.data
= (uint8_t *)secrets_fetch(key
, &blob
.length
);
738 if (blob
.data
== NULL
) {
739 DBG_NOTICE("secrets_fetch failed!\n");
740 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
743 /* unpack trusted domain password */
744 ndr_err
= ndr_pull_struct_blob(&blob
, mem_ctx
, &sdib
,
745 (ndr_pull_flags_fn_t
)ndr_pull_secrets_domain_infoB
);
746 BURN_FREE(blob
.data
, blob
.length
);
747 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
748 DBG_ERR("ndr_pull_struct_blob failed - %s!\n",
749 ndr_errstr(ndr_err
));
750 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
753 if (sdib
.info
.info1
->next_change
!= NULL
) {
754 setup_password_zeroing(sdib
.info
.info1
->next_change
->password
);
756 setup_password_zeroing(sdib
.info
.info1
->password
);
757 setup_password_zeroing(sdib
.info
.info1
->old_password
);
758 setup_password_zeroing(sdib
.info
.info1
->older_password
);
760 if (sdib
.version
!= SECRETS_DOMAIN_INFO_VERSION_1
) {
761 DBG_ERR("sdib.version = %u\n", (unsigned)sdib
.version
);
762 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
765 *_info1
= sdib
.info
.info1
;
766 return NT_STATUS_OK
;;
769 static NTSTATUS
secrets_fetch_domain_info(const char *domain
,
771 struct secrets_domain_info1
**pinfo
)
773 char *key
= domain_info_keystr(domain
);
774 return secrets_fetch_domain_info1_by_key(key
, mem_ctx
, pinfo
);
777 void secrets_debug_domain_info(int lvl
, const struct secrets_domain_info1
*info1
,
780 struct secrets_domain_infoB sdib
= {
781 .version
= SECRETS_DOMAIN_INFO_VERSION_1
,
784 sdib
.info
.info1
= discard_const_p(struct secrets_domain_info1
, info1
);
786 NDR_PRINT_DEBUG_LEVEL(lvl
, secrets_domain_infoB
, &sdib
);
789 char *secrets_domain_info_string(TALLOC_CTX
*mem_ctx
, const struct secrets_domain_info1
*info1
,
790 const char *name
, bool include_secrets
)
792 TALLOC_CTX
*frame
= talloc_stackframe();
793 struct secrets_domain_infoB sdib
= {
794 .version
= SECRETS_DOMAIN_INFO_VERSION_1
,
796 struct ndr_print
*ndr
= NULL
;
799 sdib
.info
.info1
= discard_const_p(struct secrets_domain_info1
, info1
);
801 ndr
= talloc_zero(frame
, struct ndr_print
);
806 ndr
->private_data
= talloc_strdup(ndr
, "");
807 if (ndr
->private_data
== NULL
) {
811 ndr
->print
= ndr_print_string_helper
;
813 ndr
->print_secrets
= include_secrets
;
815 ndr_print_secrets_domain_infoB(ndr
, name
, &sdib
);
816 ret
= talloc_steal(mem_ctx
, (char *)ndr
->private_data
);
821 static NTSTATUS
secrets_store_domain_info1_by_key(const char *key
,
822 const struct secrets_domain_info1
*info1
)
824 struct secrets_domain_infoB sdib
= {
825 .version
= SECRETS_DOMAIN_INFO_VERSION_1
,
827 /* packing structures */
829 enum ndr_err_code ndr_err
;
832 sdib
.info
.info1
= discard_const_p(struct secrets_domain_info1
, info1
);
834 ndr_err
= ndr_push_struct_blob(&blob
, talloc_tos(), &sdib
,
835 (ndr_push_flags_fn_t
)ndr_push_secrets_domain_infoB
);
836 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
837 return ndr_map_error2ntstatus(ndr_err
);
840 ok
= secrets_store(key
, blob
.data
, blob
.length
);
841 data_blob_clear_free(&blob
);
843 return NT_STATUS_INTERNAL_DB_ERROR
;
849 static NTSTATUS
secrets_store_domain_info(const struct secrets_domain_info1
*info
,
852 TALLOC_CTX
*frame
= talloc_stackframe();
853 const char *domain
= info
->domain_info
.name
.string
;
854 const char *realm
= info
->domain_info
.dns_domain
.string
;
855 char *key
= domain_info_keystr(domain
);
856 struct db_context
*db
= NULL
;
857 struct timeval last_change_tv
;
858 const DATA_BLOB
*cleartext_blob
= NULL
;
859 DATA_BLOB pw_blob
= data_blob_null
;
860 DATA_BLOB old_pw_blob
= data_blob_null
;
861 const char *pw
= NULL
;
862 const char *old_pw
= NULL
;
866 int role
= lp_server_role();
868 switch (info
->secure_channel_type
) {
871 if (!upgrade
&& role
>= ROLE_ACTIVE_DIRECTORY_DC
) {
872 DBG_ERR("AD_DC not supported for %s\n",
875 return NT_STATUS_INTERNAL_ERROR
;
880 DBG_ERR("SEC_CHAN_* not supported for %s\n",
883 return NT_STATUS_INTERNAL_ERROR
;
886 db
= secrets_db_ctx();
888 ret
= dbwrap_transaction_start(db
);
890 DBG_ERR("dbwrap_transaction_start() failed for %s\n",
893 return NT_STATUS_INTERNAL_DB_ERROR
;
896 ok
= secrets_clear_domain_protection(domain
);
898 DBG_ERR("secrets_clear_domain_protection(%s) failed\n",
900 dbwrap_transaction_cancel(db
);
902 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
905 ok
= secrets_delete_machine_password_ex(domain
, realm
);
907 DBG_ERR("secrets_delete_machine_password_ex(%s) failed\n",
909 dbwrap_transaction_cancel(db
);
911 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
914 status
= secrets_store_domain_info1_by_key(key
, info
);
915 if (!NT_STATUS_IS_OK(status
)) {
916 DBG_ERR("secrets_store_domain_info1_by_key() failed "
917 "for %s - %s\n", domain
, nt_errstr(status
));
918 dbwrap_transaction_cancel(db
);
924 * We use info->password_last_change instead
925 * of info->password.change_time because
926 * we may want to defer the next change approach
927 * if the server rejected the change the last time,
928 * e.g. due to RefusePasswordChange=1.
930 nttime_to_timeval(&last_change_tv
, info
->password_last_change
);
932 cleartext_blob
= &info
->password
->cleartext_blob
;
933 ok
= convert_string_talloc(frame
, CH_UTF16MUNGED
, CH_UNIX
,
934 cleartext_blob
->data
,
935 cleartext_blob
->length
,
936 (void **)&pw_blob
.data
,
939 status
= NT_STATUS_UNMAPPABLE_CHARACTER
;
940 if (errno
== ENOMEM
) {
941 status
= NT_STATUS_NO_MEMORY
;
943 DBG_ERR("convert_string_talloc(CH_UTF16MUNGED, CH_UNIX) "
944 "failed for pw of %s - %s\n",
945 domain
, nt_errstr(status
));
946 dbwrap_transaction_cancel(db
);
950 pw
= (const char *)pw_blob
.data
;
951 if (info
->old_password
!= NULL
) {
952 cleartext_blob
= &info
->old_password
->cleartext_blob
;
953 ok
= convert_string_talloc(frame
, CH_UTF16MUNGED
, CH_UNIX
,
954 cleartext_blob
->data
,
955 cleartext_blob
->length
,
956 (void **)&old_pw_blob
.data
,
957 &old_pw_blob
.length
);
959 status
= NT_STATUS_UNMAPPABLE_CHARACTER
;
960 if (errno
== ENOMEM
) {
961 status
= NT_STATUS_NO_MEMORY
;
963 DBG_ERR("convert_string_talloc(CH_UTF16MUNGED, CH_UNIX) "
964 "failed for old_pw of %s - %s\n",
965 domain
, nt_errstr(status
));
966 dbwrap_transaction_cancel(db
);
967 data_blob_clear_free(&pw_blob
);
971 old_pw
= (const char *)old_pw_blob
.data
;
974 ok
= secrets_store_machine_pw_sync(pw
, old_pw
,
976 info
->salt_principal
,
977 info
->supported_enc_types
,
978 info
->domain_info
.sid
,
979 last_change_tv
.tv_sec
,
980 info
->secure_channel_type
,
981 false); /* delete_join */
982 data_blob_clear_free(&pw_blob
);
983 data_blob_clear_free(&old_pw_blob
);
985 DBG_ERR("secrets_store_machine_pw_sync(%s) failed\n",
987 dbwrap_transaction_cancel(db
);
989 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
992 if (!GUID_all_zero(&info
->domain_info
.domain_guid
)) {
993 ok
= secrets_store_domain_guid(domain
,
994 &info
->domain_info
.domain_guid
);
996 DBG_ERR("secrets_store_domain_guid(%s) failed\n",
998 dbwrap_transaction_cancel(db
);
1000 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
1004 ok
= secrets_mark_domain_protected(domain
);
1006 DBG_ERR("secrets_mark_domain_protected(%s) failed\n",
1008 dbwrap_transaction_cancel(db
);
1010 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
1013 ret
= dbwrap_transaction_commit(db
);
1015 DBG_ERR("dbwrap_transaction_commit() failed for %s\n",
1018 return NT_STATUS_INTERNAL_DB_ERROR
;
1022 return NT_STATUS_OK
;
1025 static int secrets_domain_info_kerberos_keys(struct secrets_domain_info1_password
*p
,
1026 const char *salt_principal
)
1029 krb5_error_code krb5_ret
;
1030 krb5_context krb5_ctx
= NULL
;
1031 DATA_BLOB cleartext_utf8_b
= data_blob_null
;
1032 krb5_data cleartext_utf8
;
1035 DATA_BLOB aes_256_b
= data_blob_null
;
1036 DATA_BLOB aes_128_b
= data_blob_null
;
1038 #endif /* HAVE_ADS */
1039 DATA_BLOB arc4_b
= data_blob_null
;
1040 const uint16_t max_keys
= 3;
1041 struct secrets_domain_info1_kerberos_key
*keys
= NULL
;
1043 char *salt_data
= NULL
;
1047 * ENCTYPE_AES256_CTS_HMAC_SHA1_96
1048 * ENCTYPE_AES128_CTS_HMAC_SHA1_96
1049 * ENCTYPE_ARCFOUR_HMAC
1051 * We don't include ENCTYPE_DES_CBC_CRC
1052 * and ENCTYPE_DES_CBC_MD5
1053 * as they are no longer supported.
1055 * Note we store all enctypes we support,
1056 * including the weak encryption types,
1057 * but that's no problem as we also
1058 * store the cleartext password anyway.
1060 * Which values are then used to construct
1061 * a keytab is configured at runtime and the
1062 * configuration of msDS-SupportedEncryptionTypes.
1064 * If we don't have kerberos support or no
1065 * salt, we only generate an entry for arcfour-hmac-md5.
1067 keys
= talloc_zero_array(p
,
1068 struct secrets_domain_info1_kerberos_key
,
1074 arc4_b
= data_blob_talloc(keys
,
1076 sizeof(p
->nt_hash
.hash
));
1077 if (arc4_b
.data
== NULL
) {
1078 DBG_ERR("data_blob_talloc failed for arcfour-hmac-md5.\n");
1082 talloc_keep_secret(arc4_b
.data
);
1085 if (salt_principal
== NULL
) {
1089 krb5_ret
= smb_krb5_init_context_common(&krb5_ctx
);
1090 if (krb5_ret
!= 0) {
1091 DBG_ERR("kerberos init context failed (%s)\n",
1092 error_message(krb5_ret
));
1097 krb5_ret
= smb_krb5_salt_principal2data(krb5_ctx
, salt_principal
,
1099 if (krb5_ret
!= 0) {
1100 DBG_ERR("smb_krb5_salt_principal2data(%s) failed: %s\n",
1102 smb_get_krb5_error_message(krb5_ctx
, krb5_ret
, keys
));
1103 krb5_free_context(krb5_ctx
);
1108 salt
= (krb5_data
) {
1109 .data
= discard_const(salt_data
),
1110 .length
= strlen(salt_data
),
1113 ok
= convert_string_talloc(keys
, CH_UTF16MUNGED
, CH_UTF8
,
1114 p
->cleartext_blob
.data
,
1115 p
->cleartext_blob
.length
,
1116 (void **)&cleartext_utf8_b
.data
,
1117 &cleartext_utf8_b
.length
);
1124 krb5_free_context(krb5_ctx
);
1128 talloc_keep_secret(cleartext_utf8_b
.data
);
1129 cleartext_utf8
.data
= (void *)cleartext_utf8_b
.data
;
1130 cleartext_utf8
.length
= cleartext_utf8_b
.length
;
1132 krb5_ret
= smb_krb5_create_key_from_string(krb5_ctx
,
1136 ENCTYPE_AES256_CTS_HMAC_SHA1_96
,
1138 if (krb5_ret
!= 0) {
1139 DBG_ERR("generation of a aes256-cts-hmac-sha1-96 key failed: %s\n",
1140 smb_get_krb5_error_message(krb5_ctx
, krb5_ret
, keys
));
1141 krb5_free_context(krb5_ctx
);
1143 TALLOC_FREE(salt_data
);
1146 aes_256_b
= data_blob_talloc(keys
,
1147 KRB5_KEY_DATA(&key
),
1148 KRB5_KEY_LENGTH(&key
));
1149 krb5_free_keyblock_contents(krb5_ctx
, &key
);
1150 if (aes_256_b
.data
== NULL
) {
1151 DBG_ERR("data_blob_talloc failed for aes-256.\n");
1152 krb5_free_context(krb5_ctx
);
1154 TALLOC_FREE(salt_data
);
1157 talloc_keep_secret(aes_256_b
.data
);
1159 krb5_ret
= smb_krb5_create_key_from_string(krb5_ctx
,
1163 ENCTYPE_AES128_CTS_HMAC_SHA1_96
,
1165 if (krb5_ret
!= 0) {
1166 DBG_ERR("generation of a aes128-cts-hmac-sha1-96 key failed: %s\n",
1167 smb_get_krb5_error_message(krb5_ctx
, krb5_ret
, keys
));
1168 krb5_free_context(krb5_ctx
);
1170 TALLOC_FREE(salt_data
);
1173 aes_128_b
= data_blob_talloc(keys
,
1174 KRB5_KEY_DATA(&key
),
1175 KRB5_KEY_LENGTH(&key
));
1176 krb5_free_keyblock_contents(krb5_ctx
, &key
);
1177 if (aes_128_b
.data
== NULL
) {
1178 DBG_ERR("data_blob_talloc failed for aes-128.\n");
1179 krb5_free_context(krb5_ctx
);
1181 TALLOC_FREE(salt_data
);
1184 talloc_keep_secret(aes_128_b
.data
);
1186 krb5_free_context(krb5_ctx
);
1189 if (aes_256_b
.length
!= 0) {
1190 keys
[idx
].keytype
= ENCTYPE_AES256_CTS_HMAC_SHA1_96
;
1191 keys
[idx
].iteration_count
= 4096;
1192 keys
[idx
].value
= aes_256_b
;
1196 if (aes_128_b
.length
!= 0) {
1197 keys
[idx
].keytype
= ENCTYPE_AES128_CTS_HMAC_SHA1_96
;
1198 keys
[idx
].iteration_count
= 4096;
1199 keys
[idx
].value
= aes_128_b
;
1203 #endif /* HAVE_ADS */
1205 keys
[idx
].keytype
= ENCTYPE_ARCFOUR_HMAC
;
1206 keys
[idx
].iteration_count
= 4096;
1207 keys
[idx
].value
= arc4_b
;
1210 p
->salt_data
= salt_data
;
1211 p
->default_iteration_count
= 4096;
1217 static NTSTATUS
secrets_domain_info_password_create(TALLOC_CTX
*mem_ctx
,
1218 const char *cleartext_unix
,
1219 const char *salt_principal
,
1221 const char *change_server
,
1222 struct secrets_domain_info1_password
**_p
)
1224 struct secrets_domain_info1_password
*p
= NULL
;
1229 if (change_server
== NULL
) {
1230 return NT_STATUS_INVALID_PARAMETER_MIX
;
1233 p
= talloc_zero(mem_ctx
, struct secrets_domain_info1_password
);
1235 return NT_STATUS_NO_MEMORY
;
1237 p
->change_time
= change_time
;
1238 p
->change_server
= talloc_strdup(p
, change_server
);
1239 if (p
->change_server
== NULL
) {
1241 return NT_STATUS_NO_MEMORY
;
1243 len
= strlen(cleartext_unix
);
1244 ok
= convert_string_talloc(p
, CH_UNIX
, CH_UTF16
,
1245 cleartext_unix
, len
,
1246 (void **)&p
->cleartext_blob
.data
,
1247 &p
->cleartext_blob
.length
);
1249 NTSTATUS status
= NT_STATUS_UNMAPPABLE_CHARACTER
;
1250 if (errno
== ENOMEM
) {
1251 status
= NT_STATUS_NO_MEMORY
;
1256 talloc_keep_secret(p
->cleartext_blob
.data
);
1257 mdfour(p
->nt_hash
.hash
,
1258 p
->cleartext_blob
.data
,
1259 p
->cleartext_blob
.length
);
1261 talloc_set_destructor(p
, password_nt_hash_destructor
);
1262 ret
= secrets_domain_info_kerberos_keys(p
, salt_principal
);
1264 NTSTATUS status
= krb5_to_nt_status(ret
);
1270 return NT_STATUS_OK
;
1273 NTSTATUS
secrets_fetch_or_upgrade_domain_info(const char *domain
,
1274 TALLOC_CTX
*mem_ctx
,
1275 struct secrets_domain_info1
**pinfo
)
1277 TALLOC_CTX
*frame
= NULL
;
1278 struct secrets_domain_info1
*old
= NULL
;
1279 struct secrets_domain_info1
*info
= NULL
;
1280 const char *dns_domain
= NULL
;
1281 const char *server
= NULL
;
1282 struct db_context
*db
= NULL
;
1283 time_t last_set_time
;
1285 enum netr_SchannelType channel
;
1287 char *old_pw
= NULL
;
1288 struct dom_sid domain_sid
;
1289 struct GUID domain_guid
;
1294 ok
= strequal(domain
, lp_workgroup());
1296 dns_domain
= lp_dnsdomain();
1298 if (dns_domain
!= NULL
&& dns_domain
[0] == '\0') {
1303 last_set_time
= secrets_fetch_pass_last_set_time(domain
);
1304 if (last_set_time
== 0) {
1305 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
1307 unix_to_nt_time(&last_set_nt
, last_set_time
);
1309 frame
= talloc_stackframe();
1311 status
= secrets_fetch_domain_info(domain
, frame
, &old
);
1312 if (NT_STATUS_IS_OK(status
)) {
1313 if (old
->password_last_change
>= last_set_nt
) {
1314 *pinfo
= talloc_move(mem_ctx
, &old
);
1316 return NT_STATUS_OK
;
1321 info
= talloc_zero(frame
, struct secrets_domain_info1
);
1323 DBG_ERR("talloc_zero failed\n");
1325 return NT_STATUS_NO_MEMORY
;
1328 db
= secrets_db_ctx();
1330 ret
= dbwrap_transaction_start(db
);
1332 DBG_ERR("dbwrap_transaction_start() failed for %s\n",
1335 return NT_STATUS_INTERNAL_DB_ERROR
;
1338 pw
= secrets_fetch_machine_password(domain
,
1342 DBG_ERR("secrets_fetch_machine_password(%s) failed\n",
1344 dbwrap_transaction_cancel(db
);
1346 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
1348 unix_to_nt_time(&last_set_nt
, last_set_time
);
1350 old_pw
= secrets_fetch_prev_machine_password(domain
);
1352 ok
= secrets_fetch_domain_sid(domain
, &domain_sid
);
1354 DBG_ERR("secrets_fetch_domain_sid(%s) failed\n",
1356 dbwrap_transaction_cancel(db
);
1357 BURN_FREE_STR(old_pw
);
1360 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
1363 ok
= secrets_fetch_domain_guid(domain
, &domain_guid
);
1365 domain_guid
= GUID_zero();
1368 info
->computer_name
= lp_netbios_name();
1369 info
->account_name
= talloc_asprintf(frame
, "%s$", info
->computer_name
);
1370 if (info
->account_name
== NULL
) {
1371 DBG_ERR("talloc_asprintf(%s$) failed\n", info
->computer_name
);
1372 dbwrap_transaction_cancel(db
);
1373 BURN_FREE_STR(old_pw
);
1376 return NT_STATUS_NO_MEMORY
;
1378 info
->secure_channel_type
= channel
;
1380 info
->domain_info
.name
.string
= domain
;
1381 info
->domain_info
.dns_domain
.string
= dns_domain
;
1382 info
->domain_info
.dns_forest
.string
= dns_domain
;
1383 info
->domain_info
.domain_guid
= domain_guid
;
1384 info
->domain_info
.sid
= &domain_sid
;
1386 info
->trust_flags
= NETR_TRUST_FLAG_PRIMARY
;
1387 info
->trust_flags
|= NETR_TRUST_FLAG_OUTBOUND
;
1389 if (dns_domain
!= NULL
) {
1391 * We just assume all AD domains are
1392 * NETR_TRUST_FLAG_NATIVE these days.
1394 * This isn't used anyway for now.
1396 info
->trust_flags
|= NETR_TRUST_FLAG_NATIVE
;
1398 info
->trust_type
= LSA_TRUST_TYPE_UPLEVEL
;
1400 server
= info
->domain_info
.dns_domain
.string
;
1402 info
->trust_type
= LSA_TRUST_TYPE_DOWNLEVEL
;
1404 server
= talloc_asprintf(info
,
1408 if (server
== NULL
) {
1409 DBG_ERR("talloc_asprintf(%s#%02X) failed\n",
1410 domain
, NBT_NAME_PDC
);
1411 dbwrap_transaction_cancel(db
);
1413 BURN_FREE_STR(old_pw
);
1415 return NT_STATUS_NO_MEMORY
;
1418 info
->trust_attributes
= LSA_TRUST_ATTRIBUTE_TREAT_AS_EXTERNAL
;
1420 info
->join_time
= 0;
1423 * We don't have enough information about the configured
1426 info
->supported_enc_types
= 0;
1427 info
->salt_principal
= NULL
;
1428 if (info
->trust_type
== LSA_TRUST_TYPE_UPLEVEL
) {
1431 p
= kerberos_secrets_fetch_salt_princ();
1433 dbwrap_transaction_cancel(db
);
1434 BURN_FREE_STR(old_pw
);
1437 return NT_STATUS_INTERNAL_ERROR
;
1439 info
->salt_principal
= talloc_strdup(info
, p
);
1441 if (info
->salt_principal
== NULL
) {
1442 dbwrap_transaction_cancel(db
);
1444 BURN_FREE_STR(old_pw
);
1446 return NT_STATUS_NO_MEMORY
;
1450 info
->password_last_change
= last_set_nt
;
1451 info
->password_changes
= 1;
1452 info
->next_change
= NULL
;
1454 status
= secrets_domain_info_password_create(info
,
1456 info
->salt_principal
,
1457 last_set_nt
, server
,
1460 if (!NT_STATUS_IS_OK(status
)) {
1461 DBG_ERR("secrets_domain_info_password_create(pw) failed "
1462 "for %s - %s\n", domain
, nt_errstr(status
));
1463 dbwrap_transaction_cancel(db
);
1464 BURN_FREE_STR(old_pw
);
1470 * After a join we don't have old passwords.
1472 if (old_pw
!= NULL
) {
1473 status
= secrets_domain_info_password_create(info
,
1475 info
->salt_principal
,
1477 &info
->old_password
);
1478 BURN_FREE_STR(old_pw
);
1479 if (!NT_STATUS_IS_OK(status
)) {
1480 DBG_ERR("secrets_domain_info_password_create(old) failed "
1481 "for %s - %s\n", domain
, nt_errstr(status
));
1482 dbwrap_transaction_cancel(db
);
1486 info
->password_changes
+= 1;
1488 info
->old_password
= NULL
;
1490 info
->older_password
= NULL
;
1492 secrets_debug_domain_info(DBGLVL_INFO
, info
, "upgrade");
1494 status
= secrets_store_domain_info(info
, true /* upgrade */);
1495 if (!NT_STATUS_IS_OK(status
)) {
1496 DBG_ERR("secrets_store_domain_info() failed "
1497 "for %s - %s\n", domain
, nt_errstr(status
));
1498 dbwrap_transaction_cancel(db
);
1504 * We now reparse it.
1506 status
= secrets_fetch_domain_info(domain
, frame
, &info
);
1507 if (!NT_STATUS_IS_OK(status
)) {
1508 DBG_ERR("secrets_fetch_domain_info() failed "
1509 "for %s - %s\n", domain
, nt_errstr(status
));
1510 dbwrap_transaction_cancel(db
);
1515 ret
= dbwrap_transaction_commit(db
);
1517 DBG_ERR("dbwrap_transaction_commit() failed for %s\n",
1519 dbwrap_transaction_cancel(db
);
1521 return NT_STATUS_INTERNAL_DB_ERROR
;
1524 *pinfo
= talloc_move(mem_ctx
, &info
);
1526 return NT_STATUS_OK
;
1529 NTSTATUS
secrets_store_JoinCtx(const struct libnet_JoinCtx
*r
)
1531 TALLOC_CTX
*frame
= talloc_stackframe();
1532 struct secrets_domain_info1
*old
= NULL
;
1533 struct secrets_domain_info1
*info
= NULL
;
1534 struct db_context
*db
= NULL
;
1535 struct timeval tv
= timeval_current();
1536 NTTIME now
= timeval_to_nttime(&tv
);
1537 const char *domain
= r
->out
.netbios_domain_name
;
1541 info
= talloc_zero(frame
, struct secrets_domain_info1
);
1543 DBG_ERR("talloc_zero failed\n");
1545 return NT_STATUS_NO_MEMORY
;
1548 info
->computer_name
= r
->in
.machine_name
;
1549 info
->account_name
= r
->out
.account_name
;
1550 info
->secure_channel_type
= r
->in
.secure_channel_type
;
1552 info
->domain_info
.name
.string
=
1553 r
->out
.netbios_domain_name
;
1554 info
->domain_info
.dns_domain
.string
=
1555 r
->out
.dns_domain_name
;
1556 info
->domain_info
.dns_forest
.string
=
1558 info
->domain_info
.domain_guid
= r
->out
.domain_guid
;
1559 info
->domain_info
.sid
= r
->out
.domain_sid
;
1561 info
->trust_flags
= NETR_TRUST_FLAG_PRIMARY
;
1562 info
->trust_flags
|= NETR_TRUST_FLAG_OUTBOUND
;
1563 if (r
->out
.domain_is_ad
) {
1565 * We just assume all AD domains are
1566 * NETR_TRUST_FLAG_NATIVE these days.
1568 * This isn't used anyway for now.
1570 info
->trust_flags
|= NETR_TRUST_FLAG_NATIVE
;
1572 info
->trust_type
= LSA_TRUST_TYPE_UPLEVEL
;
1574 info
->trust_type
= LSA_TRUST_TYPE_DOWNLEVEL
;
1576 info
->trust_attributes
= LSA_TRUST_ATTRIBUTE_TREAT_AS_EXTERNAL
;
1578 info
->join_time
= now
;
1580 info
->supported_enc_types
= r
->out
.set_encryption_types
;
1581 info
->salt_principal
= r
->out
.krb5_salt
;
1583 if (info
->salt_principal
== NULL
&& r
->out
.domain_is_ad
) {
1586 ret
= smb_krb5_salt_principal_str(info
->domain_info
.dns_domain
.string
,
1588 NULL
/* userPrincipalName */,
1589 UF_WORKSTATION_TRUST_ACCOUNT
,
1592 status
= krb5_to_nt_status(ret
);
1593 DBG_ERR("smb_krb5_salt_principal() failed "
1594 "for %s - %s\n", domain
, nt_errstr(status
));
1598 info
->salt_principal
= p
;
1601 info
->password_last_change
= now
;
1602 info
->password_changes
= 1;
1603 info
->next_change
= NULL
;
1605 status
= secrets_domain_info_password_create(info
,
1606 r
->in
.machine_password
,
1607 info
->salt_principal
,
1610 if (!NT_STATUS_IS_OK(status
)) {
1611 DBG_ERR("secrets_domain_info_password_create(pw) failed "
1612 "for %s - %s\n", domain
, nt_errstr(status
));
1617 db
= secrets_db_ctx();
1619 ret
= dbwrap_transaction_start(db
);
1621 DBG_ERR("dbwrap_transaction_start() failed for %s\n",
1624 return NT_STATUS_INTERNAL_DB_ERROR
;
1627 status
= secrets_fetch_or_upgrade_domain_info(domain
, frame
, &old
);
1628 if (NT_STATUS_EQUAL(status
, NT_STATUS_CANT_ACCESS_DOMAIN_INFO
)) {
1629 DBG_DEBUG("no old join for domain(%s) available\n",
1632 } else if (!NT_STATUS_IS_OK(status
)) {
1633 DBG_ERR("secrets_fetch_or_upgrade_domain_info(%s) failed\n",
1635 dbwrap_transaction_cancel(db
);
1641 * We reuse values from an old join, so that
1642 * we still accept already granted kerberos tickets.
1645 info
->old_password
= old
->password
;
1646 info
->older_password
= old
->old_password
;
1649 secrets_debug_domain_info(DBGLVL_INFO
, info
, "join");
1651 status
= secrets_store_domain_info(info
, false /* upgrade */);
1652 if (!NT_STATUS_IS_OK(status
)) {
1653 DBG_ERR("secrets_store_domain_info() failed "
1654 "for %s - %s\n", domain
, nt_errstr(status
));
1655 dbwrap_transaction_cancel(db
);
1660 ret
= dbwrap_transaction_commit(db
);
1662 DBG_ERR("dbwrap_transaction_commit() failed for %s\n",
1665 return NT_STATUS_INTERNAL_DB_ERROR
;
1669 return NT_STATUS_OK
;
1672 NTSTATUS
secrets_prepare_password_change(const char *domain
, const char *dcname
,
1673 const char *cleartext_unix
,
1674 TALLOC_CTX
*mem_ctx
,
1675 struct secrets_domain_info1
**pinfo
,
1676 struct secrets_domain_info1_change
**pprev
,
1677 NTSTATUS (*sync_pw2keytabs_fn
)(void))
1679 TALLOC_CTX
*frame
= talloc_stackframe();
1680 struct db_context
*db
= NULL
;
1681 struct secrets_domain_info1
*info
= NULL
;
1682 struct secrets_domain_info1_change
*prev
= NULL
;
1683 struct secrets_domain_info1_change
*next
= NULL
;
1684 struct timeval tv
= timeval_current();
1685 NTTIME now
= timeval_to_nttime(&tv
);
1689 db
= secrets_db_ctx();
1691 ret
= dbwrap_transaction_start(db
);
1693 DBG_ERR("dbwrap_transaction_start() failed for %s\n",
1696 return NT_STATUS_INTERNAL_DB_ERROR
;
1699 status
= secrets_fetch_or_upgrade_domain_info(domain
, frame
, &info
);
1700 if (!NT_STATUS_IS_OK(status
)) {
1701 DBG_ERR("secrets_fetch_or_upgrade_domain_info(%s) failed\n",
1703 dbwrap_transaction_cancel(db
);
1708 prev
= info
->next_change
;
1709 info
->next_change
= NULL
;
1711 next
= talloc_zero(frame
, struct secrets_domain_info1_change
);
1713 DBG_ERR("talloc_zero failed\n");
1715 return NT_STATUS_NO_MEMORY
;
1721 status
= secrets_domain_info_password_create(next
,
1723 info
->salt_principal
,
1726 if (!NT_STATUS_IS_OK(status
)) {
1727 DBG_ERR("secrets_domain_info_password_create(next) failed "
1728 "for %s - %s\n", domain
, nt_errstr(status
));
1729 dbwrap_transaction_cancel(db
);
1735 next
->local_status
= NT_STATUS_OK
;
1736 next
->remote_status
= NT_STATUS_NOT_COMMITTED
;
1737 next
->change_time
= now
;
1738 next
->change_server
= dcname
;
1740 info
->next_change
= next
;
1742 secrets_debug_domain_info(DBGLVL_INFO
, info
, "prepare_change");
1744 status
= secrets_store_domain_info(info
, false /* upgrade */);
1745 if (!NT_STATUS_IS_OK(status
)) {
1746 DBG_ERR("secrets_store_domain_info() failed "
1747 "for %s - %s\n", domain
, nt_errstr(status
));
1748 dbwrap_transaction_cancel(db
);
1754 * We now reparse it.
1756 status
= secrets_fetch_domain_info(domain
, frame
, &info
);
1757 if (!NT_STATUS_IS_OK(status
)) {
1758 DBG_ERR("secrets_fetch_domain_info(%s) failed\n", domain
);
1759 dbwrap_transaction_cancel(db
);
1764 ret
= dbwrap_transaction_commit(db
);
1766 DBG_ERR("dbwrap_transaction_commit() failed for %s\n",
1769 return NT_STATUS_INTERNAL_DB_ERROR
;
1772 if (prev
== NULL
&& sync_pw2keytabs_fn
!= NULL
) {
1773 status
= sync_pw2keytabs_fn();
1774 if (!NT_STATUS_IS_OK(status
)) {
1775 DBG_ERR("Sync of machine password failed.\n");
1776 dbwrap_transaction_cancel(db
);
1782 *pinfo
= talloc_move(mem_ctx
, &info
);
1784 *pprev
= talloc_move(mem_ctx
, &prev
);
1790 return NT_STATUS_OK
;
1793 static NTSTATUS
secrets_check_password_change(const struct secrets_domain_info1
*cookie
,
1794 TALLOC_CTX
*mem_ctx
,
1795 struct secrets_domain_info1
**pstored
)
1797 const char *domain
= cookie
->domain_info
.name
.string
;
1798 struct secrets_domain_info1
*stored
= NULL
;
1799 struct secrets_domain_info1_change
*sn
= NULL
;
1800 struct secrets_domain_info1_change
*cn
= NULL
;
1804 if (cookie
->next_change
== NULL
) {
1805 DBG_ERR("cookie->next_change == NULL for %s.\n", domain
);
1806 return NT_STATUS_INTERNAL_ERROR
;
1809 if (cookie
->next_change
->password
== NULL
) {
1810 DBG_ERR("cookie->next_change->password == NULL for %s.\n", domain
);
1811 return NT_STATUS_INTERNAL_ERROR
;
1814 if (cookie
->password
== NULL
) {
1815 DBG_ERR("cookie->password == NULL for %s.\n", domain
);
1816 return NT_STATUS_INTERNAL_ERROR
;
1820 * Here we check that the given structure still contains the
1821 * same secrets_domain_info1_change as currently stored.
1823 * There's always a gap between secrets_prepare_password_change()
1824 * and the callers of secrets_check_password_change().
1827 status
= secrets_fetch_domain_info(domain
, mem_ctx
, &stored
);
1828 if (!NT_STATUS_IS_OK(status
)) {
1829 DBG_ERR("secrets_fetch_domain_info(%s) failed\n", domain
);
1833 if (stored
->next_change
== NULL
) {
1835 * We hit a race..., the administrator
1836 * rejoined or something similar happened.
1838 DBG_ERR("stored->next_change == NULL for %s.\n", domain
);
1839 TALLOC_FREE(stored
);
1840 return NT_STATUS_NETWORK_CREDENTIAL_CONFLICT
;
1843 if (stored
->password_last_change
!= cookie
->password_last_change
) {
1844 struct timeval store_tv
;
1845 struct timeval_buf store_buf
;
1846 struct timeval cookie_tv
;
1847 struct timeval_buf cookie_buf
;
1849 nttime_to_timeval(&store_tv
, stored
->password_last_change
);
1850 nttime_to_timeval(&cookie_tv
, cookie
->password_last_change
);
1852 DBG_ERR("password_last_change differs %s != %s for %s.\n",
1853 timeval_str_buf(&store_tv
, false, false, &store_buf
),
1854 timeval_str_buf(&cookie_tv
, false, false, &cookie_buf
),
1856 TALLOC_FREE(stored
);
1857 return NT_STATUS_NETWORK_CREDENTIAL_CONFLICT
;
1860 sn
= stored
->next_change
;
1861 cn
= cookie
->next_change
;
1863 if (sn
->change_time
!= cn
->change_time
) {
1864 struct timeval store_tv
;
1865 struct timeval_buf store_buf
;
1866 struct timeval cookie_tv
;
1867 struct timeval_buf cookie_buf
;
1869 nttime_to_timeval(&store_tv
, sn
->change_time
);
1870 nttime_to_timeval(&cookie_tv
, cn
->change_time
);
1872 DBG_ERR("next change_time differs %s != %s for %s.\n",
1873 timeval_str_buf(&store_tv
, false, false, &store_buf
),
1874 timeval_str_buf(&cookie_tv
, false, false, &cookie_buf
),
1876 TALLOC_FREE(stored
);
1877 return NT_STATUS_NETWORK_CREDENTIAL_CONFLICT
;
1880 if (sn
->password
->change_time
!= cn
->password
->change_time
) {
1881 struct timeval store_tv
;
1882 struct timeval_buf store_buf
;
1883 struct timeval cookie_tv
;
1884 struct timeval_buf cookie_buf
;
1886 nttime_to_timeval(&store_tv
, sn
->password
->change_time
);
1887 nttime_to_timeval(&cookie_tv
, cn
->password
->change_time
);
1889 DBG_ERR("next password.change_time differs %s != %s for %s.\n",
1890 timeval_str_buf(&store_tv
, false, false, &store_buf
),
1891 timeval_str_buf(&cookie_tv
, false, false, &cookie_buf
),
1893 TALLOC_FREE(stored
);
1894 return NT_STATUS_NETWORK_CREDENTIAL_CONFLICT
;
1897 cmp
= mem_equal_const_time(sn
->password
->nt_hash
.hash
,
1898 cn
->password
->nt_hash
.hash
,
1901 DBG_ERR("next password.nt_hash differs for %s.\n",
1903 TALLOC_FREE(stored
);
1904 return NT_STATUS_NETWORK_CREDENTIAL_CONFLICT
;
1907 cmp
= mem_equal_const_time(stored
->password
->nt_hash
.hash
,
1908 cookie
->password
->nt_hash
.hash
,
1911 DBG_ERR("password.nt_hash differs for %s.\n",
1913 TALLOC_FREE(stored
);
1914 return NT_STATUS_NETWORK_CREDENTIAL_CONFLICT
;
1918 return NT_STATUS_OK
;
1921 static NTSTATUS
secrets_abort_password_change(const char *change_server
,
1922 NTSTATUS local_status
,
1923 NTSTATUS remote_status
,
1924 const struct secrets_domain_info1
*cookie
,
1927 const char *domain
= cookie
->domain_info
.name
.string
;
1928 TALLOC_CTX
*frame
= talloc_stackframe();
1929 struct db_context
*db
= NULL
;
1930 struct secrets_domain_info1
*info
= NULL
;
1931 const char *reason
= defer
? "defer_change" : "failed_change";
1932 struct timeval tv
= timeval_current();
1933 NTTIME now
= timeval_to_nttime(&tv
);
1937 db
= secrets_db_ctx();
1939 ret
= dbwrap_transaction_start(db
);
1941 DBG_ERR("dbwrap_transaction_start() failed for %s\n",
1944 return NT_STATUS_INTERNAL_DB_ERROR
;
1948 * secrets_check_password_change()
1949 * checks that cookie->next_change
1950 * is valid and the same as store
1953 status
= secrets_check_password_change(cookie
, frame
, &info
);
1954 if (!NT_STATUS_IS_OK(status
)) {
1955 DBG_ERR("secrets_check_password_change(%s) failed\n", domain
);
1956 dbwrap_transaction_cancel(db
);
1962 * Remember the last server and error.
1964 info
->next_change
->change_server
= change_server
;
1965 info
->next_change
->change_time
= now
;
1966 info
->next_change
->local_status
= local_status
;
1967 info
->next_change
->remote_status
= remote_status
;
1970 * Make sure the next automatic change is deferred.
1973 info
->password_last_change
= now
;
1976 secrets_debug_domain_info(DBGLVL_WARNING
, info
, reason
);
1978 status
= secrets_store_domain_info(info
, false /* upgrade */);
1979 if (!NT_STATUS_IS_OK(status
)) {
1980 DBG_ERR("secrets_store_domain_info() failed "
1981 "for %s - %s\n", domain
, nt_errstr(status
));
1982 dbwrap_transaction_cancel(db
);
1987 ret
= dbwrap_transaction_commit(db
);
1989 DBG_ERR("dbwrap_transaction_commit() failed for %s\n",
1992 return NT_STATUS_INTERNAL_DB_ERROR
;
1996 return NT_STATUS_OK
;
1999 NTSTATUS
secrets_failed_password_change(const char *change_server
,
2000 NTSTATUS local_status
,
2001 NTSTATUS remote_status
,
2002 const struct secrets_domain_info1
*cookie
)
2004 static const bool defer
= false;
2005 return secrets_abort_password_change(change_server
,
2011 NTSTATUS
secrets_defer_password_change(const char *change_server
,
2012 NTSTATUS local_status
,
2013 NTSTATUS remote_status
,
2014 const struct secrets_domain_info1
*cookie
)
2016 static const bool defer
= true;
2017 return secrets_abort_password_change(change_server
,
2023 NTSTATUS
secrets_finish_password_change(const char *change_server
,
2025 const struct secrets_domain_info1
*cookie
,
2026 NTSTATUS (*sync_pw2keytabs_fn
)(void))
2028 const char *domain
= cookie
->domain_info
.name
.string
;
2029 TALLOC_CTX
*frame
= talloc_stackframe();
2030 struct db_context
*db
= NULL
;
2031 struct secrets_domain_info1
*info
= NULL
;
2032 struct secrets_domain_info1_change
*nc
= NULL
;
2036 db
= secrets_db_ctx();
2038 ret
= dbwrap_transaction_start(db
);
2040 DBG_ERR("dbwrap_transaction_start() failed for %s\n",
2043 return NT_STATUS_INTERNAL_DB_ERROR
;
2047 * secrets_check_password_change() checks that cookie->next_change is
2048 * valid and the same as store in the database.
2050 status
= secrets_check_password_change(cookie
, frame
, &info
);
2051 if (!NT_STATUS_IS_OK(status
)) {
2052 DBG_ERR("secrets_check_password_change(%s) failed\n", domain
);
2053 dbwrap_transaction_cancel(db
);
2058 nc
= info
->next_change
;
2060 nc
->password
->change_server
= change_server
;
2061 nc
->password
->change_time
= change_time
;
2063 info
->password_last_change
= change_time
;
2064 info
->password_changes
+= 1;
2065 info
->next_change
= NULL
;
2067 info
->older_password
= info
->old_password
;
2068 info
->old_password
= info
->password
;
2069 info
->password
= nc
->password
;
2071 secrets_debug_domain_info(DBGLVL_WARNING
, info
, "finish_change");
2073 status
= secrets_store_domain_info(info
, false /* upgrade */);
2074 if (!NT_STATUS_IS_OK(status
)) {
2075 DBG_ERR("secrets_store_domain_info() failed "
2076 "for %s - %s\n", domain
, nt_errstr(status
));
2077 dbwrap_transaction_cancel(db
);
2083 * For the clustered samba, it is important to have following order:
2084 * 1. dbwrap_transaction_commit()
2085 * 2. sync_pw2keytabs()
2086 * Only this order ensures a correct behavior of
2087 * the 'sync machine password script' that does:
2088 * 'onnode all net ads keytab create'
2090 * If we would call sync_pw2keytabs() before committing the changes to
2091 * the secrets.tdb, it will not be updated on other nodes, so triggering
2092 * 'net ads keytab create' will not see the new password yet.
2094 * This applies also to secrets_prepare_password_change().
2096 ret
= dbwrap_transaction_commit(db
);
2098 DBG_ERR("dbwrap_transaction_commit() failed for %s\n",
2101 return NT_STATUS_INTERNAL_DB_ERROR
;
2104 if (sync_pw2keytabs_fn
!= NULL
) {
2105 status
= sync_pw2keytabs_fn();
2106 if (!NT_STATUS_IS_OK(status
)) {
2107 DBG_ERR("Sync of machine password failed.\n");
2114 return NT_STATUS_OK
;