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 "libads/krb5_errs.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, in the case where we modified our domain */
143 if (dom_sid_equal(get_global_sam_sid(), sid
) == false) {
144 reset_global_sam_sid();
150 bool secrets_fetch_domain_sid(const char *domain
, struct dom_sid
*sid
)
152 struct dom_sid
*dyn_sid
;
155 dyn_sid
= (struct dom_sid
*)secrets_fetch(domain_sid_keystr(domain
), &size
);
160 if (size
!= sizeof(struct dom_sid
)) {
170 bool secrets_store_domain_guid(const char *domain
, const struct GUID
*guid
)
175 protect_ids
= secrets_fetch(protect_ids_keystr(domain
), NULL
);
177 if (strncmp(protect_ids
, "TRUE", 4)) {
178 DEBUG(0, ("Refusing to store a Domain SID, "
179 "it has been marked as protected!\n"));
180 SAFE_FREE(protect_ids
);
184 SAFE_FREE(protect_ids
);
186 key
= domain_guid_keystr(domain
);
187 return secrets_store(key
, guid
, sizeof(struct GUID
));
190 bool secrets_fetch_domain_guid(const char *domain
, struct GUID
*guid
)
192 struct GUID
*dyn_guid
;
195 struct GUID new_guid
;
197 key
= domain_guid_keystr(domain
);
198 dyn_guid
= (struct GUID
*)secrets_fetch(key
, &size
);
201 if (lp_server_role() == ROLE_DOMAIN_PDC
) {
202 new_guid
= GUID_random();
203 if (!secrets_store_domain_guid(domain
, &new_guid
))
205 dyn_guid
= (struct GUID
*)secrets_fetch(key
, &size
);
207 if (dyn_guid
== NULL
) {
212 if (size
!= sizeof(struct GUID
)) {
213 DEBUG(1,("UUID size %d is wrong!\n", (int)size
));
224 * Form a key for fetching the machine trust account sec channel type
226 * @param domain domain name
230 static const char *machine_sec_channel_type_keystr(const char *domain
)
234 keystr
= talloc_asprintf_strupper_m(talloc_tos(), "%s/%s",
235 SECRETS_MACHINE_SEC_CHANNEL_TYPE
,
237 SMB_ASSERT(keystr
!= NULL
);
242 * Form a key for fetching the machine trust account last change time
244 * @param domain domain name
248 static const char *machine_last_change_time_keystr(const char *domain
)
252 keystr
= talloc_asprintf_strupper_m(talloc_tos(), "%s/%s",
253 SECRETS_MACHINE_LAST_CHANGE_TIME
,
255 SMB_ASSERT(keystr
!= NULL
);
261 * Form a key for fetching the machine previous trust account password
263 * @param domain domain name
267 static const char *machine_prev_password_keystr(const char *domain
)
271 keystr
= talloc_asprintf_strupper_m(talloc_tos(), "%s/%s",
272 SECRETS_MACHINE_PASSWORD_PREV
, domain
);
273 SMB_ASSERT(keystr
!= NULL
);
278 * Form a key for fetching the machine trust account password
280 * @param domain domain name
284 static const char *machine_password_keystr(const char *domain
)
288 keystr
= talloc_asprintf_strupper_m(talloc_tos(), "%s/%s",
289 SECRETS_MACHINE_PASSWORD
, domain
);
290 SMB_ASSERT(keystr
!= NULL
);
295 * Form a key for fetching the machine trust account password
297 * @param domain domain name
299 * @return stored password's key
301 static const char *trust_keystr(const char *domain
)
305 keystr
= talloc_asprintf_strupper_m(talloc_tos(), "%s/%s",
306 SECRETS_MACHINE_ACCT_PASS
, domain
);
307 SMB_ASSERT(keystr
!= NULL
);
311 /************************************************************************
312 Routine to get the default secure channel type for trust accounts
313 ************************************************************************/
315 enum netr_SchannelType
get_default_sec_channel(void)
317 if (lp_server_role() == ROLE_DOMAIN_BDC
||
318 lp_server_role() == ROLE_DOMAIN_PDC
||
319 lp_server_role() == ROLE_ACTIVE_DIRECTORY_DC
) {
322 return SEC_CHAN_WKSTA
;
326 /************************************************************************
327 Routine to get the trust account password for a domain.
328 This only tries to get the legacy hashed version of the password.
329 The user of this function must have locked the trust password file using
330 the above secrets_lock_trust_account_password().
331 ************************************************************************/
333 bool secrets_fetch_trust_account_password_legacy(const char *domain
,
335 time_t *pass_last_set_time
,
336 enum netr_SchannelType
*channel
)
338 struct machine_acct_pass
*pass
;
341 if (!(pass
= (struct machine_acct_pass
*)secrets_fetch(
342 trust_keystr(domain
), &size
))) {
343 DEBUG(5, ("secrets_fetch failed!\n"));
347 if (size
!= sizeof(*pass
)) {
348 DEBUG(0, ("secrets were of incorrect size!\n"));
353 if (pass_last_set_time
) {
354 *pass_last_set_time
= pass
->mod_time
;
356 memcpy(ret_pwd
, pass
->hash
, 16);
359 *channel
= get_default_sec_channel();
366 /************************************************************************
367 Routine to get the trust account password for a domain.
368 The user of this function must have locked the trust password file using
369 the above secrets_lock_trust_account_password().
370 ************************************************************************/
372 bool secrets_fetch_trust_account_password(const char *domain
, uint8_t ret_pwd
[16],
373 time_t *pass_last_set_time
,
374 enum netr_SchannelType
*channel
)
378 plaintext
= secrets_fetch_machine_password(domain
, pass_last_set_time
,
381 DEBUG(4,("Using cleartext machine password\n"));
382 E_md4hash(plaintext
, ret_pwd
);
383 SAFE_FREE(plaintext
);
387 return secrets_fetch_trust_account_password_legacy(domain
, ret_pwd
,
392 /************************************************************************
393 Routine to delete all information related to the domain joined machine.
394 ************************************************************************/
396 bool secrets_delete_machine_password_ex(const char *domain
, const char *realm
)
398 const char *tmpkey
= NULL
;
401 tmpkey
= domain_info_keystr(domain
);
402 ok
= secrets_delete(tmpkey
);
408 tmpkey
= des_salt_key(domain
);
409 ok
= secrets_delete(tmpkey
);
415 tmpkey
= domain_guid_keystr(domain
);
416 ok
= secrets_delete(tmpkey
);
421 tmpkey
= machine_prev_password_keystr(domain
);
422 ok
= secrets_delete(tmpkey
);
427 tmpkey
= machine_password_keystr(domain
);
428 ok
= secrets_delete(tmpkey
);
433 tmpkey
= machine_sec_channel_type_keystr(domain
);
434 ok
= secrets_delete(tmpkey
);
439 tmpkey
= machine_last_change_time_keystr(domain
);
440 ok
= secrets_delete(tmpkey
);
445 tmpkey
= domain_sid_keystr(domain
);
446 ok
= secrets_delete(tmpkey
);
454 /************************************************************************
455 Routine to delete the domain sid
456 ************************************************************************/
458 bool secrets_delete_domain_sid(const char *domain
)
460 return secrets_delete_entry(domain_sid_keystr(domain
));
463 /************************************************************************
464 Set the machine trust account password, the old pw and last change
465 time, domain SID and salting principals based on values passed in
466 (added to supprt the secrets_tdb_sync module on secrets.ldb)
467 ************************************************************************/
469 bool secrets_store_machine_pw_sync(const char *pass
, const char *oldpass
, const char *domain
,
471 const char *salting_principal
, uint32_t supported_enc_types
,
472 const struct dom_sid
*domain_sid
, uint32_t last_change_time
,
473 uint32_t secure_channel_type
,
477 uint8_t last_change_time_store
[4];
478 TALLOC_CTX
*frame
= talloc_stackframe();
479 uint8_t sec_channel_bytes
[4];
482 secrets_delete_machine_password_ex(domain
, realm
);
487 ret
= secrets_store(machine_password_keystr(domain
), pass
, strlen(pass
)+1);
494 ret
= secrets_store(machine_prev_password_keystr(domain
), oldpass
, strlen(oldpass
)+1);
496 ret
= secrets_delete(machine_prev_password_keystr(domain
));
503 if (secure_channel_type
== 0) {
504 /* We delete this and instead have the read code fall back to
505 * a default based on server role, as our caller can't specify
506 * this with any more certainty */
507 ret
= secrets_delete(machine_sec_channel_type_keystr(domain
));
513 SIVAL(&sec_channel_bytes
, 0, secure_channel_type
);
514 ret
= secrets_store(machine_sec_channel_type_keystr(domain
),
515 &sec_channel_bytes
, sizeof(sec_channel_bytes
));
522 SIVAL(&last_change_time_store
, 0, last_change_time
);
523 ret
= secrets_store(machine_last_change_time_keystr(domain
),
524 &last_change_time_store
, sizeof(last_change_time
));
531 ret
= secrets_store_domain_sid(domain
, domain_sid
);
539 char *key
= des_salt_key(realm
);
541 if (salting_principal
!= NULL
) {
542 ret
= secrets_store(key
,
544 strlen(salting_principal
)+1);
546 ret
= secrets_delete(key
);
554 /************************************************************************
555 Return the standard DES salt key
556 ************************************************************************/
558 char* kerberos_standard_des_salt( void )
562 fstr_sprintf( salt
, "host/%s.%s@", lp_netbios_name(), lp_realm() );
563 (void)strlower_m( salt
);
564 fstrcat( salt
, lp_realm() );
566 return SMB_STRDUP( salt
);
569 /************************************************************************
570 ************************************************************************/
572 static char *des_salt_key(const char *realm
)
576 keystr
= talloc_asprintf_strupper_m(talloc_tos(), "%s/DES/%s",
577 SECRETS_SALTING_PRINCIPAL
,
579 SMB_ASSERT(keystr
!= NULL
);
583 /************************************************************************
584 ************************************************************************/
586 bool kerberos_secrets_store_des_salt( const char* salt
)
591 key
= des_salt_key(lp_realm());
593 DEBUG(0,("kerberos_secrets_store_des_salt: failed to generate key!\n"));
598 DEBUG(8,("kerberos_secrets_store_des_salt: deleting salt\n"));
599 secrets_delete_entry( key
);
603 DEBUG(3,("kerberos_secrets_store_des_salt: Storing salt \"%s\"\n", salt
));
605 ret
= secrets_store( key
, salt
, strlen(salt
)+1 );
612 /************************************************************************
613 ************************************************************************/
616 char* kerberos_secrets_fetch_des_salt( void )
620 key
= des_salt_key(lp_realm());
622 DEBUG(0,("kerberos_secrets_fetch_des_salt: failed to generate key!\n"));
626 salt
= (char*)secrets_fetch( key
, NULL
);
633 /************************************************************************
634 Routine to get the salting principal for this service.
635 Caller must free if return is not null.
636 ************************************************************************/
638 char *kerberos_secrets_fetch_salt_princ(void)
641 /* lookup new key first */
643 salt_princ_s
= kerberos_secrets_fetch_des_salt();
644 if (salt_princ_s
== NULL
) {
645 /* fall back to host/machine.realm@REALM */
646 salt_princ_s
= kerberos_standard_des_salt();
652 /************************************************************************
653 Routine to fetch the previous plaintext machine account password for a realm
654 the password is assumed to be a null terminated ascii string.
655 ************************************************************************/
657 char *secrets_fetch_prev_machine_password(const char *domain
)
659 return (char *)secrets_fetch(machine_prev_password_keystr(domain
), NULL
);
662 /************************************************************************
663 Routine to fetch the last change time of the machine account password
665 ************************************************************************/
667 time_t secrets_fetch_pass_last_set_time(const char *domain
)
669 uint32_t *last_set_time
;
670 time_t pass_last_set_time
;
672 last_set_time
= secrets_fetch(machine_last_change_time_keystr(domain
),
675 pass_last_set_time
= IVAL(last_set_time
,0);
676 SAFE_FREE(last_set_time
);
678 pass_last_set_time
= 0;
681 return pass_last_set_time
;
684 /************************************************************************
685 Routine to fetch the plaintext machine account password for a realm
686 the password is assumed to be a null terminated ascii string.
687 ************************************************************************/
689 char *secrets_fetch_machine_password(const char *domain
,
690 time_t *pass_last_set_time
,
691 enum netr_SchannelType
*channel
)
694 ret
= (char *)secrets_fetch(machine_password_keystr(domain
), NULL
);
696 if (pass_last_set_time
) {
697 *pass_last_set_time
= secrets_fetch_pass_last_set_time(domain
);
702 uint32_t *channel_type
;
703 channel_type
= (unsigned int *)secrets_fetch(machine_sec_channel_type_keystr(domain
), &size
);
705 *channel
= IVAL(channel_type
,0);
706 SAFE_FREE(channel_type
);
708 *channel
= get_default_sec_channel();
715 static char *domain_info_keystr(const char *domain
)
719 keystr
= talloc_asprintf_strupper_m(talloc_tos(), "%s/%s",
720 SECRETS_MACHINE_DOMAIN_INFO
,
722 SMB_ASSERT(keystr
!= NULL
);
726 /************************************************************************
727 Routine to get account password to trusted domain
728 ************************************************************************/
730 static NTSTATUS
secrets_fetch_domain_info1_by_key(const char *key
,
732 struct secrets_domain_info1
**_info1
)
734 struct secrets_domain_infoB sdib
= { .version
= 0, };
735 enum ndr_err_code ndr_err
;
736 /* unpacking structures */
739 /* fetching trusted domain password structure */
740 blob
.data
= (uint8_t *)secrets_fetch(key
, &blob
.length
);
741 if (blob
.data
== NULL
) {
742 DBG_NOTICE("secrets_fetch failed!\n");
743 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
746 /* unpack trusted domain password */
747 ndr_err
= ndr_pull_struct_blob(&blob
, mem_ctx
, &sdib
,
748 (ndr_pull_flags_fn_t
)ndr_pull_secrets_domain_infoB
);
749 SAFE_FREE(blob
.data
);
750 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
751 DBG_ERR("ndr_pull_struct_blob failed - %s!\n",
752 ndr_errstr(ndr_err
));
753 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
756 if (sdib
.version
!= SECRETS_DOMAIN_INFO_VERSION_1
) {
757 DBG_ERR("sdib.version = %u\n", (unsigned)sdib
.version
);
758 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
761 *_info1
= sdib
.info
.info1
;
762 return NT_STATUS_OK
;;
765 static NTSTATUS
secrets_fetch_domain_info(const char *domain
,
767 struct secrets_domain_info1
**pinfo
)
769 char *key
= domain_info_keystr(domain
);
770 return secrets_fetch_domain_info1_by_key(key
, mem_ctx
, pinfo
);
773 void secrets_debug_domain_info(int lvl
, const struct secrets_domain_info1
*info1
,
776 struct secrets_domain_infoB sdib
= {
777 .version
= SECRETS_DOMAIN_INFO_VERSION_1
,
780 sdib
.info
.info1
= discard_const_p(struct secrets_domain_info1
, info1
);
782 ndr_print_debug((ndr_print_fn_t
)ndr_print_secrets_domain_infoB
,
786 char *secrets_domain_info_string(TALLOC_CTX
*mem_ctx
, const struct secrets_domain_info1
*info1
,
787 const char *name
, bool include_secrets
)
789 TALLOC_CTX
*frame
= talloc_stackframe();
790 struct secrets_domain_infoB sdib
= {
791 .version
= SECRETS_DOMAIN_INFO_VERSION_1
,
793 struct ndr_print
*ndr
= NULL
;
796 sdib
.info
.info1
= discard_const_p(struct secrets_domain_info1
, info1
);
798 ndr
= talloc_zero(frame
, struct ndr_print
);
803 ndr
->private_data
= talloc_strdup(ndr
, "");
804 if (ndr
->private_data
== NULL
) {
808 ndr
->print
= ndr_print_string_helper
;
810 ndr
->print_secrets
= include_secrets
;
812 ndr_print_secrets_domain_infoB(ndr
, name
, &sdib
);
813 ret
= talloc_steal(mem_ctx
, (char *)ndr
->private_data
);
818 static NTSTATUS
secrets_store_domain_info1_by_key(const char *key
,
819 const struct secrets_domain_info1
*info1
)
821 struct secrets_domain_infoB sdib
= {
822 .version
= SECRETS_DOMAIN_INFO_VERSION_1
,
824 /* packing structures */
826 enum ndr_err_code ndr_err
;
829 sdib
.info
.info1
= discard_const_p(struct secrets_domain_info1
, info1
);
831 ndr_err
= ndr_push_struct_blob(&blob
, talloc_tos(), &sdib
,
832 (ndr_push_flags_fn_t
)ndr_push_secrets_domain_infoB
);
833 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
834 return ndr_map_error2ntstatus(ndr_err
);
837 ok
= secrets_store(key
, blob
.data
, blob
.length
);
838 data_blob_clear_free(&blob
);
840 return NT_STATUS_INTERNAL_DB_ERROR
;
846 static NTSTATUS
secrets_store_domain_info(const struct secrets_domain_info1
*info
,
849 TALLOC_CTX
*frame
= talloc_stackframe();
850 const char *domain
= info
->domain_info
.name
.string
;
851 const char *realm
= info
->domain_info
.dns_domain
.string
;
852 char *key
= domain_info_keystr(domain
);
853 struct db_context
*db
= NULL
;
854 struct timeval last_change_tv
;
855 const DATA_BLOB
*cleartext_blob
= NULL
;
856 DATA_BLOB pw_blob
= data_blob_null
;
857 DATA_BLOB old_pw_blob
= data_blob_null
;
858 const char *pw
= NULL
;
859 const char *old_pw
= NULL
;
863 int role
= lp_server_role();
865 switch (info
->secure_channel_type
) {
868 if (!upgrade
&& role
>= ROLE_ACTIVE_DIRECTORY_DC
) {
869 DBG_ERR("AD_DC not supported for %s\n",
872 return NT_STATUS_INTERNAL_ERROR
;
877 DBG_ERR("SEC_CHAN_* not supported for %s\n",
880 return NT_STATUS_INTERNAL_ERROR
;
883 db
= secrets_db_ctx();
885 ret
= dbwrap_transaction_start(db
);
887 DBG_ERR("dbwrap_transaction_start() failed for %s\n",
890 return NT_STATUS_INTERNAL_DB_ERROR
;
893 ok
= secrets_clear_domain_protection(domain
);
895 DBG_ERR("secrets_clear_domain_protection(%s) failed\n",
897 dbwrap_transaction_cancel(db
);
899 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
902 ok
= secrets_delete_machine_password_ex(domain
, realm
);
904 DBG_ERR("secrets_delete_machine_password_ex(%s) failed\n",
906 dbwrap_transaction_cancel(db
);
908 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
911 status
= secrets_store_domain_info1_by_key(key
, info
);
912 if (!NT_STATUS_IS_OK(status
)) {
913 DBG_ERR("secrets_store_domain_info1_by_key() failed "
914 "for %s - %s\n", domain
, nt_errstr(status
));
915 dbwrap_transaction_cancel(db
);
921 * We use info->password_last_change instead
922 * of info->password.change_time because
923 * we may want to defer the next change approach
924 * if the server rejected the change the last time,
925 * e.g. due to RefusePasswordChange=1.
927 nttime_to_timeval(&last_change_tv
, info
->password_last_change
);
929 cleartext_blob
= &info
->password
->cleartext_blob
;
930 ok
= convert_string_talloc(frame
, CH_UTF16MUNGED
, CH_UNIX
,
931 cleartext_blob
->data
,
932 cleartext_blob
->length
,
933 (void **)&pw_blob
.data
,
936 status
= NT_STATUS_UNMAPPABLE_CHARACTER
;
937 if (errno
== ENOMEM
) {
938 status
= NT_STATUS_NO_MEMORY
;
940 DBG_ERR("convert_string_talloc(CH_UTF16MUNGED, CH_UNIX) "
941 "failed for pw of %s - %s\n",
942 domain
, nt_errstr(status
));
943 dbwrap_transaction_cancel(db
);
947 pw
= (const char *)pw_blob
.data
;
948 if (info
->old_password
!= NULL
) {
949 cleartext_blob
= &info
->old_password
->cleartext_blob
;
950 ok
= convert_string_talloc(frame
, CH_UTF16MUNGED
, CH_UNIX
,
951 cleartext_blob
->data
,
952 cleartext_blob
->length
,
953 (void **)&old_pw_blob
.data
,
954 &old_pw_blob
.length
);
956 status
= NT_STATUS_UNMAPPABLE_CHARACTER
;
957 if (errno
== ENOMEM
) {
958 status
= NT_STATUS_NO_MEMORY
;
960 DBG_ERR("convert_string_talloc(CH_UTF16MUNGED, CH_UNIX) "
961 "failed for old_pw of %s - %s\n",
962 domain
, nt_errstr(status
));
963 dbwrap_transaction_cancel(db
);
964 data_blob_clear_free(&pw_blob
);
968 old_pw
= (const char *)old_pw_blob
.data
;
971 ok
= secrets_store_machine_pw_sync(pw
, old_pw
,
973 info
->salt_principal
,
974 info
->supported_enc_types
,
975 info
->domain_info
.sid
,
976 last_change_tv
.tv_sec
,
977 info
->secure_channel_type
,
978 false); /* delete_join */
979 data_blob_clear_free(&pw_blob
);
980 data_blob_clear_free(&old_pw_blob
);
982 DBG_ERR("secrets_store_machine_pw_sync(%s) failed\n",
984 dbwrap_transaction_cancel(db
);
986 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
989 if (!GUID_all_zero(&info
->domain_info
.domain_guid
)) {
990 ok
= secrets_store_domain_guid(domain
,
991 &info
->domain_info
.domain_guid
);
993 DBG_ERR("secrets_store_domain_guid(%s) failed\n",
995 dbwrap_transaction_cancel(db
);
997 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
1001 ok
= secrets_mark_domain_protected(domain
);
1003 DBG_ERR("secrets_mark_domain_protected(%s) failed\n",
1005 dbwrap_transaction_cancel(db
);
1007 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
1010 ret
= dbwrap_transaction_commit(db
);
1012 DBG_ERR("dbwrap_transaction_commit() failed for %s\n",
1015 return NT_STATUS_INTERNAL_DB_ERROR
;
1019 return NT_STATUS_OK
;
1022 static int secrets_domain_info_kerberos_keys(struct secrets_domain_info1_password
*p
,
1023 const char *salt_principal
)
1026 krb5_error_code krb5_ret
;
1027 krb5_context krb5_ctx
= NULL
;
1028 DATA_BLOB cleartext_utf8_b
= data_blob_null
;
1029 krb5_data cleartext_utf8
;
1032 DATA_BLOB aes_256_b
= data_blob_null
;
1033 DATA_BLOB aes_128_b
= data_blob_null
;
1034 DATA_BLOB des_md5_b
= data_blob_null
;
1036 #endif /* HAVE_ADS */
1037 DATA_BLOB arc4_b
= data_blob_null
;
1038 const uint16_t max_keys
= 4;
1039 struct secrets_domain_info1_kerberos_key
*keys
= NULL
;
1041 char *salt_data
= NULL
;
1045 * ENCTYPE_AES256_CTS_HMAC_SHA1_96
1046 * ENCTYPE_AES128_CTS_HMAC_SHA1_96
1047 * ENCTYPE_ARCFOUR_HMAC
1048 * ENCTYPE_DES_CBC_MD5
1050 * We don't include ENCTYPE_DES_CBC_CRC
1051 * as W2008R2 also doesn't store it anymore.
1053 * Note we store all enctypes we support,
1054 * including the weak encryption types,
1055 * but that's no problem as we also
1056 * store the cleartext password anyway.
1058 * Which values are then used to construct
1059 * a keytab is configured at runtime and the
1060 * configuration of msDS-SupportedEncryptionTypes.
1062 * If we don't have kerberos support or no
1063 * salt, we only generate an entry for arcfour-hmac-md5.
1065 keys
= talloc_zero_array(p
,
1066 struct secrets_domain_info1_kerberos_key
,
1072 arc4_b
= data_blob_talloc(keys
,
1074 sizeof(p
->nt_hash
.hash
));
1075 if (arc4_b
.data
== NULL
) {
1076 DBG_ERR("data_blob_talloc failed for arcfour-hmac-md5.\n");
1082 if (salt_principal
== NULL
) {
1086 krb5_ret
= smb_krb5_init_context_common(&krb5_ctx
);
1087 if (krb5_ret
!= 0) {
1088 DBG_ERR("kerberos init context failed (%s)\n",
1089 error_message(krb5_ret
));
1094 krb5_ret
= smb_krb5_salt_principal2data(krb5_ctx
, salt_principal
,
1096 if (krb5_ret
!= 0) {
1097 DBG_ERR("smb_krb5_salt_principal2data(%s) failed: %s\n",
1099 smb_get_krb5_error_message(krb5_ctx
, krb5_ret
, keys
));
1100 krb5_free_context(krb5_ctx
);
1105 salt
= (krb5_data
) {
1106 .data
= discard_const(salt_data
),
1107 .length
= strlen(salt_data
),
1110 ok
= convert_string_talloc(keys
, CH_UTF16MUNGED
, CH_UTF8
,
1111 p
->cleartext_blob
.data
,
1112 p
->cleartext_blob
.length
,
1113 (void **)&cleartext_utf8_b
.data
,
1114 &cleartext_utf8_b
.length
);
1121 krb5_free_context(krb5_ctx
);
1125 cleartext_utf8
.data
= (void *)cleartext_utf8_b
.data
;
1126 cleartext_utf8
.length
= cleartext_utf8_b
.length
;
1128 krb5_ret
= smb_krb5_create_key_from_string(krb5_ctx
,
1132 ENCTYPE_AES256_CTS_HMAC_SHA1_96
,
1134 if (krb5_ret
!= 0) {
1135 DBG_ERR("generation of a aes256-cts-hmac-sha1-96 key failed: %s\n",
1136 smb_get_krb5_error_message(krb5_ctx
, krb5_ret
, keys
));
1137 krb5_free_context(krb5_ctx
);
1139 TALLOC_FREE(salt_data
);
1142 aes_256_b
= data_blob_talloc(keys
,
1143 KRB5_KEY_DATA(&key
),
1144 KRB5_KEY_LENGTH(&key
));
1145 krb5_free_keyblock_contents(krb5_ctx
, &key
);
1146 if (aes_256_b
.data
== NULL
) {
1147 DBG_ERR("data_blob_talloc failed for aes-256.\n");
1148 krb5_free_context(krb5_ctx
);
1150 TALLOC_FREE(salt_data
);
1154 krb5_ret
= smb_krb5_create_key_from_string(krb5_ctx
,
1158 ENCTYPE_AES128_CTS_HMAC_SHA1_96
,
1160 if (krb5_ret
!= 0) {
1161 DBG_ERR("generation of a aes128-cts-hmac-sha1-96 key failed: %s\n",
1162 smb_get_krb5_error_message(krb5_ctx
, krb5_ret
, keys
));
1163 krb5_free_context(krb5_ctx
);
1165 TALLOC_FREE(salt_data
);
1168 aes_128_b
= data_blob_talloc(keys
,
1169 KRB5_KEY_DATA(&key
),
1170 KRB5_KEY_LENGTH(&key
));
1171 krb5_free_keyblock_contents(krb5_ctx
, &key
);
1172 if (aes_128_b
.data
== NULL
) {
1173 DBG_ERR("data_blob_talloc failed for aes-128.\n");
1174 krb5_free_context(krb5_ctx
);
1176 TALLOC_FREE(salt_data
);
1180 krb5_ret
= smb_krb5_create_key_from_string(krb5_ctx
,
1184 ENCTYPE_DES_CBC_MD5
,
1186 if (krb5_ret
!= 0) {
1187 DBG_ERR("generation of a des-cbc-md5 key failed: %s\n",
1188 smb_get_krb5_error_message(krb5_ctx
, krb5_ret
, keys
));
1189 krb5_free_context(krb5_ctx
);
1191 TALLOC_FREE(salt_data
);
1194 des_md5_b
= data_blob_talloc(keys
,
1195 KRB5_KEY_DATA(&key
),
1196 KRB5_KEY_LENGTH(&key
));
1197 krb5_free_keyblock_contents(krb5_ctx
, &key
);
1198 if (des_md5_b
.data
== NULL
) {
1199 DBG_ERR("data_blob_talloc failed for des-cbc-md5.\n");
1200 krb5_free_context(krb5_ctx
);
1202 TALLOC_FREE(salt_data
);
1206 krb5_free_context(krb5_ctx
);
1209 if (aes_256_b
.length
!= 0) {
1210 keys
[idx
].keytype
= ENCTYPE_AES256_CTS_HMAC_SHA1_96
;
1211 keys
[idx
].iteration_count
= 4096;
1212 keys
[idx
].value
= aes_256_b
;
1216 if (aes_128_b
.length
!= 0) {
1217 keys
[idx
].keytype
= ENCTYPE_AES128_CTS_HMAC_SHA1_96
;
1218 keys
[idx
].iteration_count
= 4096;
1219 keys
[idx
].value
= aes_128_b
;
1223 #endif /* HAVE_ADS */
1225 keys
[idx
].keytype
= ENCTYPE_ARCFOUR_HMAC
;
1226 keys
[idx
].iteration_count
= 4096;
1227 keys
[idx
].value
= arc4_b
;
1231 if (des_md5_b
.length
!= 0) {
1232 keys
[idx
].keytype
= ENCTYPE_DES_CBC_MD5
;
1233 keys
[idx
].iteration_count
= 4096;
1234 keys
[idx
].value
= des_md5_b
;
1237 #endif /* HAVE_ADS */
1239 p
->salt_data
= salt_data
;
1240 p
->default_iteration_count
= 4096;
1246 static NTSTATUS
secrets_domain_info_password_create(TALLOC_CTX
*mem_ctx
,
1247 const char *cleartext_unix
,
1248 const char *salt_principal
,
1250 const char *change_server
,
1251 struct secrets_domain_info1_password
**_p
)
1253 struct secrets_domain_info1_password
*p
= NULL
;
1258 if (change_server
== NULL
) {
1259 return NT_STATUS_INVALID_PARAMETER_MIX
;
1262 p
= talloc_zero(mem_ctx
, struct secrets_domain_info1_password
);
1264 return NT_STATUS_NO_MEMORY
;
1266 p
->change_time
= change_time
;
1267 p
->change_server
= talloc_strdup(p
, change_server
);
1268 if (p
->change_server
== NULL
) {
1270 return NT_STATUS_NO_MEMORY
;
1272 len
= strlen(cleartext_unix
);
1273 ok
= convert_string_talloc(p
, CH_UNIX
, CH_UTF16
,
1274 cleartext_unix
, len
,
1275 (void **)&p
->cleartext_blob
.data
,
1276 &p
->cleartext_blob
.length
);
1278 NTSTATUS status
= NT_STATUS_UNMAPPABLE_CHARACTER
;
1279 if (errno
== ENOMEM
) {
1280 status
= NT_STATUS_NO_MEMORY
;
1285 mdfour(p
->nt_hash
.hash
,
1286 p
->cleartext_blob
.data
,
1287 p
->cleartext_blob
.length
);
1289 ret
= secrets_domain_info_kerberos_keys(p
, salt_principal
);
1291 NTSTATUS status
= krb5_to_nt_status(ret
);
1297 return NT_STATUS_OK
;
1300 NTSTATUS
secrets_fetch_or_upgrade_domain_info(const char *domain
,
1301 TALLOC_CTX
*mem_ctx
,
1302 struct secrets_domain_info1
**pinfo
)
1304 TALLOC_CTX
*frame
= NULL
;
1305 struct secrets_domain_info1
*old
= NULL
;
1306 struct secrets_domain_info1
*info
= NULL
;
1307 const char *dns_domain
= NULL
;
1308 const char *server
= NULL
;
1309 struct db_context
*db
= NULL
;
1310 time_t last_set_time
;
1312 enum netr_SchannelType channel
;
1314 char *old_pw
= NULL
;
1315 struct dom_sid domain_sid
;
1316 struct GUID domain_guid
;
1321 ok
= strequal(domain
, lp_workgroup());
1323 dns_domain
= lp_dnsdomain();
1325 if (dns_domain
!= NULL
&& dns_domain
[0] == '\0') {
1330 last_set_time
= secrets_fetch_pass_last_set_time(domain
);
1331 if (last_set_time
== 0) {
1332 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
1334 unix_to_nt_time(&last_set_nt
, last_set_time
);
1336 frame
= talloc_stackframe();
1338 status
= secrets_fetch_domain_info(domain
, frame
, &old
);
1339 if (NT_STATUS_IS_OK(status
)) {
1340 if (old
->password_last_change
>= last_set_nt
) {
1341 *pinfo
= talloc_move(mem_ctx
, &old
);
1343 return NT_STATUS_OK
;
1348 info
= talloc_zero(frame
, struct secrets_domain_info1
);
1350 DBG_ERR("talloc_zero failed\n");
1352 return NT_STATUS_NO_MEMORY
;
1355 db
= secrets_db_ctx();
1357 ret
= dbwrap_transaction_start(db
);
1359 DBG_ERR("dbwrap_transaction_start() failed for %s\n",
1362 return NT_STATUS_INTERNAL_DB_ERROR
;
1365 pw
= secrets_fetch_machine_password(domain
,
1369 DBG_ERR("secrets_fetch_machine_password(%s) failed\n",
1371 dbwrap_transaction_cancel(db
);
1373 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
1375 unix_to_nt_time(&last_set_nt
, last_set_time
);
1377 old_pw
= secrets_fetch_prev_machine_password(domain
);
1379 ok
= secrets_fetch_domain_sid(domain
, &domain_sid
);
1381 DBG_ERR("secrets_fetch_domain_sid(%s) failed\n",
1383 dbwrap_transaction_cancel(db
);
1387 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
1390 ok
= secrets_fetch_domain_guid(domain
, &domain_guid
);
1392 domain_guid
= GUID_zero();
1395 info
->computer_name
= lp_netbios_name();
1396 info
->account_name
= talloc_asprintf(frame
, "%s$", info
->computer_name
);
1397 if (info
->account_name
== NULL
) {
1398 DBG_ERR("talloc_asprintf(%s$) failed\n", info
->computer_name
);
1399 dbwrap_transaction_cancel(db
);
1403 return NT_STATUS_NO_MEMORY
;
1405 info
->secure_channel_type
= channel
;
1407 info
->domain_info
.name
.string
= domain
;
1408 info
->domain_info
.dns_domain
.string
= dns_domain
;
1409 info
->domain_info
.dns_forest
.string
= dns_domain
;
1410 info
->domain_info
.domain_guid
= domain_guid
;
1411 info
->domain_info
.sid
= &domain_sid
;
1413 info
->trust_flags
= NETR_TRUST_FLAG_PRIMARY
;
1414 info
->trust_flags
|= NETR_TRUST_FLAG_OUTBOUND
;
1416 if (dns_domain
!= NULL
) {
1418 * We just assume all AD domains are
1419 * NETR_TRUST_FLAG_NATIVE these days.
1421 * This isn't used anyway for now.
1423 info
->trust_flags
|= NETR_TRUST_FLAG_NATIVE
;
1425 info
->trust_type
= LSA_TRUST_TYPE_UPLEVEL
;
1427 server
= info
->domain_info
.dns_domain
.string
;
1429 info
->trust_type
= LSA_TRUST_TYPE_DOWNLEVEL
;
1431 server
= talloc_asprintf(info
,
1435 if (server
== NULL
) {
1436 DBG_ERR("talloc_asprintf(%s#%02X) failed\n",
1437 domain
, NBT_NAME_PDC
);
1438 dbwrap_transaction_cancel(db
);
1442 return NT_STATUS_NO_MEMORY
;
1445 info
->trust_attributes
= LSA_TRUST_ATTRIBUTE_TREAT_AS_EXTERNAL
;
1447 info
->join_time
= 0;
1450 * We don't have enough information about the configured
1453 info
->supported_enc_types
= 0;
1454 info
->salt_principal
= NULL
;
1455 if (info
->trust_type
== LSA_TRUST_TYPE_UPLEVEL
) {
1458 p
= kerberos_secrets_fetch_salt_princ();
1460 dbwrap_transaction_cancel(db
);
1464 return NT_STATUS_INTERNAL_ERROR
;
1466 info
->salt_principal
= talloc_strdup(info
, p
);
1468 if (info
->salt_principal
== NULL
) {
1469 dbwrap_transaction_cancel(db
);
1473 return NT_STATUS_NO_MEMORY
;
1477 info
->password_last_change
= last_set_nt
;
1478 info
->password_changes
= 1;
1479 info
->next_change
= NULL
;
1481 status
= secrets_domain_info_password_create(info
,
1483 info
->salt_principal
,
1484 last_set_nt
, server
,
1487 if (!NT_STATUS_IS_OK(status
)) {
1488 DBG_ERR("secrets_domain_info_password_create(pw) failed "
1489 "for %s - %s\n", domain
, nt_errstr(status
));
1490 dbwrap_transaction_cancel(db
);
1497 * After a join we don't have old passwords.
1499 if (old_pw
!= NULL
) {
1500 status
= secrets_domain_info_password_create(info
,
1502 info
->salt_principal
,
1504 &info
->old_password
);
1506 if (!NT_STATUS_IS_OK(status
)) {
1507 DBG_ERR("secrets_domain_info_password_create(old) failed "
1508 "for %s - %s\n", domain
, nt_errstr(status
));
1509 dbwrap_transaction_cancel(db
);
1513 info
->password_changes
+= 1;
1515 info
->old_password
= NULL
;
1517 info
->older_password
= NULL
;
1519 secrets_debug_domain_info(DBGLVL_INFO
, info
, "upgrade");
1521 status
= secrets_store_domain_info(info
, true /* upgrade */);
1522 if (!NT_STATUS_IS_OK(status
)) {
1523 DBG_ERR("secrets_store_domain_info() failed "
1524 "for %s - %s\n", domain
, nt_errstr(status
));
1525 dbwrap_transaction_cancel(db
);
1531 * We now reparse it.
1533 status
= secrets_fetch_domain_info(domain
, frame
, &info
);
1534 if (!NT_STATUS_IS_OK(status
)) {
1535 DBG_ERR("secrets_fetch_domain_info() failed "
1536 "for %s - %s\n", domain
, nt_errstr(status
));
1537 dbwrap_transaction_cancel(db
);
1542 ret
= dbwrap_transaction_commit(db
);
1544 DBG_ERR("dbwrap_transaction_commit() failed for %s\n",
1546 dbwrap_transaction_cancel(db
);
1548 return NT_STATUS_INTERNAL_DB_ERROR
;
1551 *pinfo
= talloc_move(mem_ctx
, &info
);
1553 return NT_STATUS_OK
;
1556 NTSTATUS
secrets_store_JoinCtx(const struct libnet_JoinCtx
*r
)
1558 TALLOC_CTX
*frame
= talloc_stackframe();
1559 struct secrets_domain_info1
*old
= NULL
;
1560 struct secrets_domain_info1
*info
= NULL
;
1561 struct db_context
*db
= NULL
;
1562 struct timeval tv
= timeval_current();
1563 NTTIME now
= timeval_to_nttime(&tv
);
1564 const char *domain
= r
->out
.netbios_domain_name
;
1568 info
= talloc_zero(frame
, struct secrets_domain_info1
);
1570 DBG_ERR("talloc_zero failed\n");
1572 return NT_STATUS_NO_MEMORY
;
1575 info
->computer_name
= r
->in
.machine_name
;
1576 info
->account_name
= r
->out
.account_name
;
1577 info
->secure_channel_type
= r
->in
.secure_channel_type
;
1579 info
->domain_info
.name
.string
=
1580 r
->out
.netbios_domain_name
;
1581 info
->domain_info
.dns_domain
.string
=
1582 r
->out
.dns_domain_name
;
1583 info
->domain_info
.dns_forest
.string
=
1585 info
->domain_info
.domain_guid
= r
->out
.domain_guid
;
1586 info
->domain_info
.sid
= r
->out
.domain_sid
;
1588 info
->trust_flags
= NETR_TRUST_FLAG_PRIMARY
;
1589 info
->trust_flags
|= NETR_TRUST_FLAG_OUTBOUND
;
1590 if (r
->out
.domain_is_ad
) {
1592 * We just assume all AD domains are
1593 * NETR_TRUST_FLAG_NATIVE these days.
1595 * This isn't used anyway for now.
1597 info
->trust_flags
|= NETR_TRUST_FLAG_NATIVE
;
1599 info
->trust_type
= LSA_TRUST_TYPE_UPLEVEL
;
1601 info
->trust_type
= LSA_TRUST_TYPE_DOWNLEVEL
;
1603 info
->trust_attributes
= LSA_TRUST_ATTRIBUTE_TREAT_AS_EXTERNAL
;
1605 info
->join_time
= now
;
1607 info
->supported_enc_types
= r
->out
.set_encryption_types
;
1608 info
->salt_principal
= r
->out
.krb5_salt
;
1610 if (info
->salt_principal
== NULL
&& r
->out
.domain_is_ad
) {
1613 ret
= smb_krb5_salt_principal(info
->domain_info
.dns_domain
.string
,
1615 NULL
/* userPrincipalName */,
1616 UF_WORKSTATION_TRUST_ACCOUNT
,
1619 status
= krb5_to_nt_status(ret
);
1620 DBG_ERR("smb_krb5_salt_principal() failed "
1621 "for %s - %s\n", domain
, nt_errstr(status
));
1625 info
->salt_principal
= p
;
1628 info
->password_last_change
= now
;
1629 info
->password_changes
= 1;
1630 info
->next_change
= NULL
;
1632 status
= secrets_domain_info_password_create(info
,
1633 r
->in
.machine_password
,
1634 info
->salt_principal
,
1637 if (!NT_STATUS_IS_OK(status
)) {
1638 DBG_ERR("secrets_domain_info_password_create(pw) failed "
1639 "for %s - %s\n", domain
, nt_errstr(status
));
1644 db
= secrets_db_ctx();
1646 ret
= dbwrap_transaction_start(db
);
1648 DBG_ERR("dbwrap_transaction_start() failed for %s\n",
1651 return NT_STATUS_INTERNAL_DB_ERROR
;
1654 status
= secrets_fetch_or_upgrade_domain_info(domain
, frame
, &old
);
1655 if (NT_STATUS_EQUAL(status
, NT_STATUS_CANT_ACCESS_DOMAIN_INFO
)) {
1656 DBG_DEBUG("no old join for domain(%s) available\n",
1659 } else if (!NT_STATUS_IS_OK(status
)) {
1660 DBG_ERR("secrets_fetch_or_upgrade_domain_info(%s) failed\n",
1662 dbwrap_transaction_cancel(db
);
1668 * We reuse values from an old join, so that
1669 * we still accept already granted kerberos tickets.
1672 info
->old_password
= old
->password
;
1673 info
->older_password
= old
->old_password
;
1676 secrets_debug_domain_info(DBGLVL_INFO
, info
, "join");
1678 status
= secrets_store_domain_info(info
, false /* upgrade */);
1679 if (!NT_STATUS_IS_OK(status
)) {
1680 DBG_ERR("secrets_store_domain_info() failed "
1681 "for %s - %s\n", domain
, nt_errstr(status
));
1682 dbwrap_transaction_cancel(db
);
1687 ret
= dbwrap_transaction_commit(db
);
1689 DBG_ERR("dbwrap_transaction_commit() failed for %s\n",
1692 return NT_STATUS_INTERNAL_DB_ERROR
;
1696 return NT_STATUS_OK
;
1699 NTSTATUS
secrets_prepare_password_change(const char *domain
, const char *dcname
,
1700 const char *cleartext_unix
,
1701 TALLOC_CTX
*mem_ctx
,
1702 struct secrets_domain_info1
**pinfo
,
1703 struct secrets_domain_info1_change
**pprev
)
1705 TALLOC_CTX
*frame
= talloc_stackframe();
1706 struct db_context
*db
= NULL
;
1707 struct secrets_domain_info1
*info
= NULL
;
1708 struct secrets_domain_info1_change
*prev
= NULL
;
1709 struct secrets_domain_info1_change
*next
= NULL
;
1710 struct timeval tv
= timeval_current();
1711 NTTIME now
= timeval_to_nttime(&tv
);
1715 db
= secrets_db_ctx();
1717 ret
= dbwrap_transaction_start(db
);
1719 DBG_ERR("dbwrap_transaction_start() failed for %s\n",
1722 return NT_STATUS_INTERNAL_DB_ERROR
;
1725 status
= secrets_fetch_or_upgrade_domain_info(domain
, frame
, &info
);
1726 if (!NT_STATUS_IS_OK(status
)) {
1727 DBG_ERR("secrets_fetch_or_upgrade_domain_info(%s) failed\n",
1729 dbwrap_transaction_cancel(db
);
1734 prev
= info
->next_change
;
1735 info
->next_change
= NULL
;
1737 next
= talloc_zero(frame
, struct secrets_domain_info1_change
);
1739 DBG_ERR("talloc_zero failed\n");
1741 return NT_STATUS_NO_MEMORY
;
1747 status
= secrets_domain_info_password_create(next
,
1749 info
->salt_principal
,
1752 if (!NT_STATUS_IS_OK(status
)) {
1753 DBG_ERR("secrets_domain_info_password_create(next) failed "
1754 "for %s - %s\n", domain
, nt_errstr(status
));
1755 dbwrap_transaction_cancel(db
);
1761 next
->local_status
= NT_STATUS_OK
;
1762 next
->remote_status
= NT_STATUS_NOT_COMMITTED
;
1763 next
->change_time
= now
;
1764 next
->change_server
= dcname
;
1766 info
->next_change
= next
;
1768 secrets_debug_domain_info(DBGLVL_INFO
, info
, "prepare_change");
1770 status
= secrets_store_domain_info(info
, false /* upgrade */);
1771 if (!NT_STATUS_IS_OK(status
)) {
1772 DBG_ERR("secrets_store_domain_info() failed "
1773 "for %s - %s\n", domain
, nt_errstr(status
));
1774 dbwrap_transaction_cancel(db
);
1780 * We now reparse it.
1782 status
= secrets_fetch_domain_info(domain
, frame
, &info
);
1783 if (!NT_STATUS_IS_OK(status
)) {
1784 DBG_ERR("secrets_fetch_domain_info(%s) failed\n", domain
);
1785 dbwrap_transaction_cancel(db
);
1790 ret
= dbwrap_transaction_commit(db
);
1792 DBG_ERR("dbwrap_transaction_commit() failed for %s\n",
1795 return NT_STATUS_INTERNAL_DB_ERROR
;
1798 *pinfo
= talloc_move(mem_ctx
, &info
);
1800 *pprev
= talloc_move(mem_ctx
, &prev
);
1806 return NT_STATUS_OK
;
1809 static NTSTATUS
secrets_check_password_change(const struct secrets_domain_info1
*cookie
,
1810 TALLOC_CTX
*mem_ctx
,
1811 struct secrets_domain_info1
**pstored
)
1813 const char *domain
= cookie
->domain_info
.name
.string
;
1814 struct secrets_domain_info1
*stored
= NULL
;
1815 struct secrets_domain_info1_change
*sn
= NULL
;
1816 struct secrets_domain_info1_change
*cn
= NULL
;
1820 if (cookie
->next_change
== NULL
) {
1821 DBG_ERR("cookie->next_change == NULL for %s.\n", domain
);
1822 return NT_STATUS_INTERNAL_ERROR
;
1825 if (cookie
->next_change
->password
== NULL
) {
1826 DBG_ERR("cookie->next_change->password == NULL for %s.\n", domain
);
1827 return NT_STATUS_INTERNAL_ERROR
;
1830 if (cookie
->password
== NULL
) {
1831 DBG_ERR("cookie->password == NULL for %s.\n", domain
);
1832 return NT_STATUS_INTERNAL_ERROR
;
1836 * Here we check that the given strucure still contains the
1837 * same secrets_domain_info1_change as currently stored.
1839 * There's always a gap between secrets_prepare_password_change()
1840 * and the callers of secrets_check_password_change().
1843 status
= secrets_fetch_domain_info(domain
, mem_ctx
, &stored
);
1844 if (!NT_STATUS_IS_OK(status
)) {
1845 DBG_ERR("secrets_fetch_domain_info(%s) failed\n", domain
);
1849 if (stored
->next_change
== NULL
) {
1851 * We hit a race..., the administrator
1852 * rejoined or something similar happened.
1854 DBG_ERR("stored->next_change == NULL for %s.\n", domain
);
1855 TALLOC_FREE(stored
);
1856 return NT_STATUS_NETWORK_CREDENTIAL_CONFLICT
;
1859 if (stored
->password_last_change
!= cookie
->password_last_change
) {
1860 struct timeval store_tv
;
1861 struct timeval_buf store_buf
;
1862 struct timeval cookie_tv
;
1863 struct timeval_buf cookie_buf
;
1865 nttime_to_timeval(&store_tv
, stored
->password_last_change
);
1866 nttime_to_timeval(&cookie_tv
, cookie
->password_last_change
);
1868 DBG_ERR("password_last_change differs %s != %s for %s.\n",
1869 timeval_str_buf(&store_tv
, false, false, &store_buf
),
1870 timeval_str_buf(&cookie_tv
, false, false, &cookie_buf
),
1872 TALLOC_FREE(stored
);
1873 return NT_STATUS_NETWORK_CREDENTIAL_CONFLICT
;
1876 sn
= stored
->next_change
;
1877 cn
= cookie
->next_change
;
1879 if (sn
->change_time
!= cn
->change_time
) {
1880 struct timeval store_tv
;
1881 struct timeval_buf store_buf
;
1882 struct timeval cookie_tv
;
1883 struct timeval_buf cookie_buf
;
1885 nttime_to_timeval(&store_tv
, sn
->change_time
);
1886 nttime_to_timeval(&cookie_tv
, cn
->change_time
);
1888 DBG_ERR("next change_time differs %s != %s for %s.\n",
1889 timeval_str_buf(&store_tv
, false, false, &store_buf
),
1890 timeval_str_buf(&cookie_tv
, false, false, &cookie_buf
),
1892 TALLOC_FREE(stored
);
1893 return NT_STATUS_NETWORK_CREDENTIAL_CONFLICT
;
1896 if (sn
->password
->change_time
!= cn
->password
->change_time
) {
1897 struct timeval store_tv
;
1898 struct timeval_buf store_buf
;
1899 struct timeval cookie_tv
;
1900 struct timeval_buf cookie_buf
;
1902 nttime_to_timeval(&store_tv
, sn
->password
->change_time
);
1903 nttime_to_timeval(&cookie_tv
, cn
->password
->change_time
);
1905 DBG_ERR("next password.change_time differs %s != %s for %s.\n",
1906 timeval_str_buf(&store_tv
, false, false, &store_buf
),
1907 timeval_str_buf(&cookie_tv
, false, false, &cookie_buf
),
1909 TALLOC_FREE(stored
);
1910 return NT_STATUS_NETWORK_CREDENTIAL_CONFLICT
;
1913 cmp
= memcmp(sn
->password
->nt_hash
.hash
,
1914 cn
->password
->nt_hash
.hash
,
1917 DBG_ERR("next password.nt_hash differs for %s.\n",
1919 TALLOC_FREE(stored
);
1920 return NT_STATUS_NETWORK_CREDENTIAL_CONFLICT
;
1923 cmp
= memcmp(stored
->password
->nt_hash
.hash
,
1924 cookie
->password
->nt_hash
.hash
,
1927 DBG_ERR("password.nt_hash differs for %s.\n",
1929 TALLOC_FREE(stored
);
1930 return NT_STATUS_NETWORK_CREDENTIAL_CONFLICT
;
1934 return NT_STATUS_OK
;
1937 static NTSTATUS
secrets_abort_password_change(const char *change_server
,
1938 NTSTATUS local_status
,
1939 NTSTATUS remote_status
,
1940 const struct secrets_domain_info1
*cookie
,
1943 const char *domain
= cookie
->domain_info
.name
.string
;
1944 TALLOC_CTX
*frame
= talloc_stackframe();
1945 struct db_context
*db
= NULL
;
1946 struct secrets_domain_info1
*info
= NULL
;
1947 const char *reason
= defer
? "defer_change" : "failed_change";
1948 struct timeval tv
= timeval_current();
1949 NTTIME now
= timeval_to_nttime(&tv
);
1953 db
= secrets_db_ctx();
1955 ret
= dbwrap_transaction_start(db
);
1957 DBG_ERR("dbwrap_transaction_start() failed for %s\n",
1960 return NT_STATUS_INTERNAL_DB_ERROR
;
1964 * secrets_check_password_change()
1965 * checks that cookie->next_change
1966 * is valid and the same as store
1969 status
= secrets_check_password_change(cookie
, frame
, &info
);
1970 if (!NT_STATUS_IS_OK(status
)) {
1971 DBG_ERR("secrets_check_password_change(%s) failed\n", domain
);
1972 dbwrap_transaction_cancel(db
);
1978 * Remember the last server and error.
1980 info
->next_change
->change_server
= change_server
;
1981 info
->next_change
->change_time
= now
;
1982 info
->next_change
->local_status
= local_status
;
1983 info
->next_change
->remote_status
= remote_status
;
1986 * Make sure the next automatic change is deferred.
1989 info
->password_last_change
= now
;
1992 secrets_debug_domain_info(DBGLVL_WARNING
, info
, reason
);
1994 status
= secrets_store_domain_info(info
, false /* upgrade */);
1995 if (!NT_STATUS_IS_OK(status
)) {
1996 DBG_ERR("secrets_store_domain_info() failed "
1997 "for %s - %s\n", domain
, nt_errstr(status
));
1998 dbwrap_transaction_cancel(db
);
2003 ret
= dbwrap_transaction_commit(db
);
2005 DBG_ERR("dbwrap_transaction_commit() failed for %s\n",
2008 return NT_STATUS_INTERNAL_DB_ERROR
;
2012 return NT_STATUS_OK
;
2015 NTSTATUS
secrets_failed_password_change(const char *change_server
,
2016 NTSTATUS local_status
,
2017 NTSTATUS remote_status
,
2018 const struct secrets_domain_info1
*cookie
)
2020 static const bool defer
= false;
2021 return secrets_abort_password_change(change_server
,
2027 NTSTATUS
secrets_defer_password_change(const char *change_server
,
2028 NTSTATUS local_status
,
2029 NTSTATUS remote_status
,
2030 const struct secrets_domain_info1
*cookie
)
2032 static const bool defer
= true;
2033 return secrets_abort_password_change(change_server
,
2039 NTSTATUS
secrets_finish_password_change(const char *change_server
,
2041 const struct secrets_domain_info1
*cookie
)
2043 const char *domain
= cookie
->domain_info
.name
.string
;
2044 TALLOC_CTX
*frame
= talloc_stackframe();
2045 struct db_context
*db
= NULL
;
2046 struct secrets_domain_info1
*info
= NULL
;
2047 struct secrets_domain_info1_change
*nc
= NULL
;
2051 db
= secrets_db_ctx();
2053 ret
= dbwrap_transaction_start(db
);
2055 DBG_ERR("dbwrap_transaction_start() failed for %s\n",
2058 return NT_STATUS_INTERNAL_DB_ERROR
;
2062 * secrets_check_password_change() checks that cookie->next_change is
2063 * valid and the same as store in the database.
2065 status
= secrets_check_password_change(cookie
, frame
, &info
);
2066 if (!NT_STATUS_IS_OK(status
)) {
2067 DBG_ERR("secrets_check_password_change(%s) failed\n", domain
);
2068 dbwrap_transaction_cancel(db
);
2073 nc
= info
->next_change
;
2075 nc
->password
->change_server
= change_server
;
2076 nc
->password
->change_time
= change_time
;
2078 info
->password_last_change
= change_time
;
2079 info
->password_changes
+= 1;
2080 info
->next_change
= NULL
;
2082 info
->older_password
= info
->old_password
;
2083 info
->old_password
= info
->password
;
2084 info
->password
= nc
->password
;
2086 secrets_debug_domain_info(DBGLVL_WARNING
, info
, "finish_change");
2088 status
= secrets_store_domain_info(info
, false /* upgrade */);
2089 if (!NT_STATUS_IS_OK(status
)) {
2090 DBG_ERR("secrets_store_domain_info() failed "
2091 "for %s - %s\n", domain
, nt_errstr(status
));
2092 dbwrap_transaction_cancel(db
);
2097 ret
= dbwrap_transaction_commit(db
);
2099 DBG_ERR("dbwrap_transaction_commit() failed for %s\n",
2102 return NT_STATUS_INTERNAL_DB_ERROR
;
2106 return NT_STATUS_OK
;