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 */
28 #define DBGC_CLASS DBGC_PASSDB
30 static TDB_CONTEXT
*tdb
;
32 /* Urrrg. global.... */
33 bool global_machine_password_needs_changing
;
36 * Use a TDB to store an incrementing random seed.
38 * Initialised to the current pid, the very first time Samba starts,
39 * and incremented by one each time it is needed.
41 * @note Not called by systems with a working /dev/urandom.
43 static void get_rand_seed(int *new_seed
)
45 *new_seed
= sys_getpid();
47 tdb_change_int32_atomic(tdb
, "INFO/random_seed", new_seed
, 1);
51 /* open up the secrets database */
52 bool secrets_init(void)
60 pstrcpy(fname
, lp_private_dir());
61 pstrcat(fname
,"/secrets.tdb");
63 tdb
= tdb_open_log(fname
, 0, TDB_DEFAULT
, O_RDWR
|O_CREAT
, 0600);
66 DEBUG(0,("Failed to open %s\n", fname
));
71 * Set a reseed function for the crypto random generator
73 * This avoids a problem where systems without /dev/urandom
74 * could send the same challenge to multiple clients
76 set_rand_reseed_callback(get_rand_seed
);
78 /* Ensure that the reseed is done now, while we are root, etc */
79 generate_random_buffer(&dummy
, sizeof(dummy
));
84 /* read a entry from the secrets database - the caller must free the result
85 if size is non-null then the size of the entry is put in there
87 void *secrets_fetch(const char *key
, size_t *size
)
93 dbuf
= tdb_fetch(tdb
, string_tdb_data(key
));
99 /* store a secrets entry
101 bool secrets_store(const char *key
, const void *data
, size_t size
)
106 return tdb_trans_store(tdb
, string_tdb_data(key
),
107 make_tdb_data((const uint8
*)data
, size
),
112 /* delete a secets database entry
114 bool secrets_delete(const char *key
)
119 return tdb_trans_delete(tdb
, string_tdb_data(key
)) == 0;
122 bool secrets_store_domain_sid(const char *domain
, const DOM_SID
*sid
)
127 slprintf(key
, sizeof(key
)-1, "%s/%s", SECRETS_DOMAIN_SID
, domain
);
129 ret
= secrets_store(key
, sid
, sizeof(DOM_SID
));
131 /* Force a re-query, in case we modified our domain */
133 reset_global_sam_sid();
137 bool secrets_fetch_domain_sid(const char *domain
, DOM_SID
*sid
)
143 slprintf(key
, sizeof(key
)-1, "%s/%s", SECRETS_DOMAIN_SID
, domain
);
145 dyn_sid
= (DOM_SID
*)secrets_fetch(key
, &size
);
150 if (size
!= sizeof(DOM_SID
)) {
160 bool secrets_store_domain_guid(const char *domain
, struct GUID
*guid
)
164 slprintf(key
, sizeof(key
)-1, "%s/%s", SECRETS_DOMAIN_GUID
, domain
);
166 return secrets_store(key
, guid
, sizeof(struct GUID
));
169 bool secrets_fetch_domain_guid(const char *domain
, struct GUID
*guid
)
171 struct GUID
*dyn_guid
;
174 struct GUID new_guid
;
176 slprintf(key
, sizeof(key
)-1, "%s/%s", SECRETS_DOMAIN_GUID
, domain
);
178 dyn_guid
= (struct GUID
*)secrets_fetch(key
, &size
);
181 if (lp_server_role() == ROLE_DOMAIN_PDC
) {
182 smb_uuid_generate_random(&new_guid
);
183 if (!secrets_store_domain_guid(domain
, &new_guid
))
185 dyn_guid
= (struct GUID
*)secrets_fetch(key
, &size
);
187 if (dyn_guid
== NULL
) {
192 if (size
!= sizeof(struct GUID
)) {
193 DEBUG(1,("UUID size %d is wrong!\n", (int)size
));
204 * Form a key for fetching the machine trust account password
206 * @param domain domain name
208 * @return stored password's key
210 static const char *trust_keystr(const char *domain
)
214 keystr
= talloc_asprintf(talloc_tos(), "%s/%s",
215 SECRETS_MACHINE_ACCT_PASS
, domain
);
216 SMB_ASSERT(keystr
!= NULL
);
224 * Form a key for fetching a trusted domain password
226 * @param domain trusted domain name
228 * @return stored password's key
230 static char *trustdom_keystr(const char *domain
)
234 keystr
= talloc_asprintf(talloc_tos(), "%s/%s",
235 SECRETS_DOMTRUST_ACCT_PASS
, domain
);
236 SMB_ASSERT(keystr
!= NULL
);
242 /************************************************************************
243 Lock the trust password entry.
244 ************************************************************************/
246 bool secrets_lock_trust_account_password(const char *domain
, bool dolock
)
252 return (tdb_lock_bystring(tdb
, trust_keystr(domain
)) == 0);
254 tdb_unlock_bystring(tdb
, trust_keystr(domain
));
258 /************************************************************************
259 Routine to get the default secure channel type for trust accounts
260 ************************************************************************/
262 uint32
get_default_sec_channel(void)
264 if (lp_server_role() == ROLE_DOMAIN_BDC
||
265 lp_server_role() == ROLE_DOMAIN_PDC
) {
268 return SEC_CHAN_WKSTA
;
272 /************************************************************************
273 Routine to get the trust account password for a domain.
274 The user of this function must have locked the trust password file using
275 the above secrets_lock_trust_account_password().
276 ************************************************************************/
278 bool secrets_fetch_trust_account_password(const char *domain
, uint8 ret_pwd
[16],
279 time_t *pass_last_set_time
,
282 struct machine_acct_pass
*pass
;
286 plaintext
= secrets_fetch_machine_password(domain
, pass_last_set_time
,
289 DEBUG(4,("Using cleartext machine password\n"));
290 E_md4hash(plaintext
, ret_pwd
);
291 SAFE_FREE(plaintext
);
295 if (!(pass
= (struct machine_acct_pass
*)secrets_fetch(
296 trust_keystr(domain
), &size
))) {
297 DEBUG(5, ("secrets_fetch failed!\n"));
301 if (size
!= sizeof(*pass
)) {
302 DEBUG(0, ("secrets were of incorrect size!\n"));
306 if (pass_last_set_time
) {
307 *pass_last_set_time
= pass
->mod_time
;
309 memcpy(ret_pwd
, pass
->hash
, 16);
312 *channel
= get_default_sec_channel();
315 /* Test if machine password has expired and needs to be changed */
316 if (lp_machine_password_timeout()) {
317 if (pass
->mod_time
> 0 && time(NULL
) > (pass
->mod_time
+
318 (time_t)lp_machine_password_timeout())) {
319 global_machine_password_needs_changing
= True
;
328 * Pack SID passed by pointer
330 * @param pack_buf pointer to buffer which is to be filled with packed data
331 * @param bufsize size of packing buffer
332 * @param sid pointer to sid to be packed
334 * @return length of the packed representation of the whole structure
336 static size_t tdb_sid_pack(uint8
*pack_buf
, int bufsize
, DOM_SID
* sid
)
341 if (!sid
|| !pack_buf
) return -1;
343 len
+= tdb_pack(pack_buf
+ len
, bufsize
- len
, "bb", sid
->sid_rev_num
,
346 for (idx
= 0; idx
< 6; idx
++) {
347 len
+= tdb_pack(pack_buf
+ len
, bufsize
- len
, "b",
351 for (idx
= 0; idx
< MAXSUBAUTHS
; idx
++) {
352 len
+= tdb_pack(pack_buf
+ len
, bufsize
- len
, "d",
353 sid
->sub_auths
[idx
]);
360 * Unpack SID into a pointer
362 * @param pack_buf pointer to buffer with packed representation
363 * @param bufsize size of the buffer
364 * @param sid pointer to sid structure to be filled with unpacked data
366 * @return size of structure unpacked from buffer
368 static size_t tdb_sid_unpack(uint8
*pack_buf
, int bufsize
, DOM_SID
* sid
)
372 if (!sid
|| !pack_buf
) return -1;
374 len
+= tdb_unpack(pack_buf
+ len
, bufsize
- len
, "bb",
375 &sid
->sid_rev_num
, &sid
->num_auths
);
377 for (idx
= 0; idx
< 6; idx
++) {
378 len
+= tdb_unpack(pack_buf
+ len
, bufsize
- len
, "b",
382 for (idx
= 0; idx
< MAXSUBAUTHS
; idx
++) {
383 len
+= tdb_unpack(pack_buf
+ len
, bufsize
- len
, "d",
384 &sid
->sub_auths
[idx
]);
391 * Pack TRUSTED_DOM_PASS passed by pointer
393 * @param pack_buf pointer to buffer which is to be filled with packed data
394 * @param bufsize size of the buffer
395 * @param pass pointer to trusted domain password to be packed
397 * @return length of the packed representation of the whole structure
399 static size_t tdb_trusted_dom_pass_pack(uint8
*pack_buf
, int bufsize
,
400 TRUSTED_DOM_PASS
* pass
)
404 if (!pack_buf
|| !pass
) return -1;
406 /* packing unicode domain name and password */
407 len
+= tdb_pack(pack_buf
+ len
, bufsize
- len
, "d",
410 for (idx
= 0; idx
< 32; idx
++)
411 len
+= tdb_pack(pack_buf
+ len
, bufsize
- len
, "w",
412 pass
->uni_name
[idx
]);
414 len
+= tdb_pack(pack_buf
+ len
, bufsize
- len
, "dPd", pass
->pass_len
,
415 pass
->pass
, pass
->mod_time
);
417 /* packing SID structure */
418 len
+= tdb_sid_pack(pack_buf
+ len
, bufsize
- len
, &pass
->domain_sid
);
425 * Unpack TRUSTED_DOM_PASS passed by pointer
427 * @param pack_buf pointer to buffer with packed representation
428 * @param bufsize size of the buffer
429 * @param pass pointer to trusted domain password to be filled with unpacked data
431 * @return size of structure unpacked from buffer
433 static size_t tdb_trusted_dom_pass_unpack(uint8
*pack_buf
, int bufsize
,
434 TRUSTED_DOM_PASS
* pass
)
438 if (!pack_buf
|| !pass
) return -1;
440 /* unpack unicode domain name and plaintext password */
441 len
+= tdb_unpack(pack_buf
, bufsize
- len
, "d", &pass
->uni_name_len
);
443 for (idx
= 0; idx
< 32; idx
++)
444 len
+= tdb_unpack(pack_buf
+ len
, bufsize
- len
, "w",
445 &pass
->uni_name
[idx
]);
447 len
+= tdb_unpack(pack_buf
+ len
, bufsize
- len
, "dPd",
448 &pass
->pass_len
, &pass
->pass
, &pass
->mod_time
);
450 /* unpack domain sid */
451 len
+= tdb_sid_unpack(pack_buf
+ len
, bufsize
- len
,
457 /************************************************************************
458 Routine to get account password to trusted domain
459 ************************************************************************/
461 bool secrets_fetch_trusted_domain_password(const char *domain
, char** pwd
,
462 DOM_SID
*sid
, time_t *pass_last_set_time
)
464 struct trusted_dom_pass pass
;
467 /* unpacking structures */
473 /* fetching trusted domain password structure */
474 if (!(pass_buf
= (uint8
*)secrets_fetch(trustdom_keystr(domain
),
476 DEBUG(5, ("secrets_fetch failed!\n"));
480 /* unpack trusted domain password */
481 pass_len
= tdb_trusted_dom_pass_unpack(pass_buf
, size
, &pass
);
484 if (pass_len
!= size
) {
485 DEBUG(5, ("Invalid secrets size. Unpacked data doesn't match trusted_dom_pass structure.\n"));
489 /* the trust's password */
491 *pwd
= SMB_STRDUP(pass
.pass
);
497 /* last change time */
498 if (pass_last_set_time
) *pass_last_set_time
= pass
.mod_time
;
501 if (sid
!= NULL
) sid_copy(sid
, &pass
.domain_sid
);
506 /************************************************************************
507 Routine to set the trust account password for a domain.
508 ************************************************************************/
510 bool secrets_store_trust_account_password(const char *domain
, uint8 new_pwd
[16])
512 struct machine_acct_pass pass
;
514 pass
.mod_time
= time(NULL
);
515 memcpy(pass
.hash
, new_pwd
, 16);
517 return secrets_store(trust_keystr(domain
), (void *)&pass
, sizeof(pass
));
521 * Routine to store the password for trusted domain
523 * @param domain remote domain name
524 * @param pwd plain text password of trust relationship
525 * @param sid remote domain sid
527 * @return true if succeeded
530 bool secrets_store_trusted_domain_password(const char* domain
, const char* pwd
,
533 smb_ucs2_t
*uni_dom_name
;
535 /* packing structures */
538 int pass_buf_len
= sizeof(pass_buf
);
540 struct trusted_dom_pass pass
;
543 if (push_ucs2_allocate(&uni_dom_name
, domain
) == (size_t)-1) {
544 DEBUG(0, ("Could not convert domain name %s to unicode\n",
549 strncpy_w(pass
.uni_name
, uni_dom_name
, sizeof(pass
.uni_name
) - 1);
550 pass
.uni_name_len
= strlen_w(uni_dom_name
)+1;
551 SAFE_FREE(uni_dom_name
);
553 /* last change time */
554 pass
.mod_time
= time(NULL
);
556 /* password of the trust */
557 pass
.pass_len
= strlen(pwd
);
558 fstrcpy(pass
.pass
, pwd
);
561 sid_copy(&pass
.domain_sid
, sid
);
563 pass_len
= tdb_trusted_dom_pass_pack((uint8
*)pass_buf
, pass_buf_len
, &pass
);
565 return secrets_store(trustdom_keystr(domain
), (void *)&pass_buf
, pass_len
);
568 /************************************************************************
569 Routine to set the plaintext machine account password for a realm
570 the password is assumed to be a null terminated ascii string
571 ************************************************************************/
573 bool secrets_store_machine_password(const char *pass
, const char *domain
, uint32 sec_channel
)
577 uint32 last_change_time
;
578 uint32 sec_channel_type
;
580 asprintf(&key
, "%s/%s", SECRETS_MACHINE_PASSWORD
, domain
);
585 ret
= secrets_store(key
, pass
, strlen(pass
)+1);
591 asprintf(&key
, "%s/%s", SECRETS_MACHINE_LAST_CHANGE_TIME
, domain
);
596 SIVAL(&last_change_time
, 0, time(NULL
));
597 ret
= secrets_store(key
, &last_change_time
, sizeof(last_change_time
));
600 asprintf(&key
, "%s/%s", SECRETS_MACHINE_SEC_CHANNEL_TYPE
, domain
);
605 SIVAL(&sec_channel_type
, 0, sec_channel
);
606 ret
= secrets_store(key
, &sec_channel_type
, sizeof(sec_channel_type
));
612 /************************************************************************
613 Routine to fetch the plaintext machine account password for a realm
614 the password is assumed to be a null terminated ascii string.
615 ************************************************************************/
617 char *secrets_fetch_machine_password(const char *domain
,
618 time_t *pass_last_set_time
,
623 asprintf(&key
, "%s/%s", SECRETS_MACHINE_PASSWORD
, domain
);
625 ret
= (char *)secrets_fetch(key
, NULL
);
628 if (pass_last_set_time
) {
630 uint32
*last_set_time
;
631 asprintf(&key
, "%s/%s", SECRETS_MACHINE_LAST_CHANGE_TIME
, domain
);
633 last_set_time
= (unsigned int *)secrets_fetch(key
, &size
);
635 *pass_last_set_time
= IVAL(last_set_time
,0);
636 SAFE_FREE(last_set_time
);
638 *pass_last_set_time
= 0;
645 uint32
*channel_type
;
646 asprintf(&key
, "%s/%s", SECRETS_MACHINE_SEC_CHANNEL_TYPE
, domain
);
648 channel_type
= (unsigned int *)secrets_fetch(key
, &size
);
650 *channel
= IVAL(channel_type
,0);
651 SAFE_FREE(channel_type
);
653 *channel
= get_default_sec_channel();
661 /************************************************************************
662 Routine to delete the machine trust account password file for a domain.
663 ************************************************************************/
665 bool trust_password_delete(const char *domain
)
667 return secrets_delete(trust_keystr(domain
));
670 /************************************************************************
671 Routine to delete the password for trusted domain
672 ************************************************************************/
674 bool trusted_domain_password_delete(const char *domain
)
676 return secrets_delete(trustdom_keystr(domain
));
679 bool secrets_store_ldap_pw(const char* dn
, char* pw
)
684 if (asprintf(&key
, "%s/%s", SECRETS_LDAP_BIND_PW
, dn
) < 0) {
685 DEBUG(0, ("secrets_store_ldap_pw: asprintf failed!\n"));
689 ret
= secrets_store(key
, pw
, strlen(pw
)+1);
695 /*******************************************************************
696 Find the ldap password.
697 ******************************************************************/
699 bool fetch_ldap_pw(char **dn
, char** pw
)
704 *dn
= smb_xstrdup(lp_ldap_admin_dn());
706 if (asprintf(&key
, "%s/%s", SECRETS_LDAP_BIND_PW
, *dn
) < 0) {
708 DEBUG(0, ("fetch_ldap_pw: asprintf failed!\n"));
711 *pw
=(char *)secrets_fetch(key
, &size
);
715 /* Upgrade 2.2 style entry */
717 char* old_style_key
= SMB_STRDUP(*dn
);
719 fstring old_style_pw
;
721 if (!old_style_key
) {
722 DEBUG(0, ("fetch_ldap_pw: strdup failed!\n"));
726 for (p
=old_style_key
; *p
; p
++)
727 if (*p
== ',') *p
= '/';
729 data
=(char *)secrets_fetch(old_style_key
, &size
);
730 if (!size
&& size
< sizeof(old_style_pw
)) {
731 DEBUG(0,("fetch_ldap_pw: neither ldap secret retrieved!\n"));
732 SAFE_FREE(old_style_key
);
737 size
= MIN(size
, sizeof(fstring
)-1);
738 strncpy(old_style_pw
, data
, size
);
739 old_style_pw
[size
] = 0;
743 if (!secrets_store_ldap_pw(*dn
, old_style_pw
)) {
744 DEBUG(0,("fetch_ldap_pw: ldap secret could not be upgraded!\n"));
745 SAFE_FREE(old_style_key
);
749 if (!secrets_delete(old_style_key
)) {
750 DEBUG(0,("fetch_ldap_pw: old ldap secret could not be deleted!\n"));
753 SAFE_FREE(old_style_key
);
755 *pw
= smb_xstrdup(old_style_pw
);
762 * Get trusted domains info from secrets.tdb.
765 NTSTATUS
secrets_trusted_domains(TALLOC_CTX
*mem_ctx
, uint32
*num_domains
,
766 struct trustdom_info
***domains
)
768 TDB_LIST_NODE
*keys
, *k
;
772 if (!(tmp_ctx
= talloc_new(mem_ctx
))) {
773 return NT_STATUS_NO_MEMORY
;
776 if (!secrets_init()) return NT_STATUS_ACCESS_DENIED
;
778 /* generate searching pattern */
779 pattern
= talloc_asprintf(tmp_ctx
, "%s/*", SECRETS_DOMTRUST_ACCT_PASS
);
780 if (pattern
== NULL
) {
781 DEBUG(0, ("secrets_trusted_domains: talloc_asprintf() "
783 TALLOC_FREE(tmp_ctx
);
784 return NT_STATUS_NO_MEMORY
;
790 * Make sure that a talloc context for the trustdom_info structs
794 if (!(*domains
= TALLOC_ARRAY(mem_ctx
, struct trustdom_info
*, 1))) {
795 TALLOC_FREE(tmp_ctx
);
796 return NT_STATUS_NO_MEMORY
;
799 /* fetching trusted domains' data and collecting them in a list */
800 keys
= tdb_search_keys(tdb
, pattern
);
802 /* searching for keys in secrets db -- way to go ... */
803 for (k
= keys
; k
; k
= k
->next
) {
805 size_t size
= 0, packed_size
= 0;
806 struct trusted_dom_pass pass
;
808 struct trustdom_info
*dom_info
;
810 /* important: ensure null-termination of the key string */
811 secrets_key
= talloc_strndup(tmp_ctx
,
812 (const char *)k
->node_key
.dptr
,
815 DEBUG(0, ("strndup failed!\n"));
816 tdb_search_list_free(keys
);
817 TALLOC_FREE(tmp_ctx
);
818 return NT_STATUS_NO_MEMORY
;
821 packed_pass
= (uint8
*)secrets_fetch(secrets_key
, &size
);
822 packed_size
= tdb_trusted_dom_pass_unpack(packed_pass
, size
,
824 /* packed representation isn't needed anymore */
825 SAFE_FREE(packed_pass
);
827 if (size
!= packed_size
) {
828 DEBUG(2, ("Secrets record %s is invalid!\n",
833 if (pass
.domain_sid
.num_auths
!= 4) {
834 DEBUG(0, ("SID %s is not a domain sid, has %d "
835 "auths instead of 4\n",
836 sid_string_static(&pass
.domain_sid
),
837 pass
.domain_sid
.num_auths
));
841 if (!(dom_info
= TALLOC_P(*domains
, struct trustdom_info
))) {
842 DEBUG(0, ("talloc failed\n"));
843 tdb_search_list_free(keys
);
844 TALLOC_FREE(tmp_ctx
);
845 return NT_STATUS_NO_MEMORY
;
848 if (pull_ucs2_talloc(dom_info
, &dom_info
->name
,
849 pass
.uni_name
) == (size_t)-1) {
850 DEBUG(2, ("pull_ucs2_talloc failed\n"));
851 tdb_search_list_free(keys
);
852 TALLOC_FREE(tmp_ctx
);
853 return NT_STATUS_NO_MEMORY
;
856 sid_copy(&dom_info
->sid
, &pass
.domain_sid
);
858 ADD_TO_ARRAY(*domains
, struct trustdom_info
*, dom_info
,
859 domains
, num_domains
);
861 if (*domains
== NULL
) {
862 tdb_search_list_free(keys
);
863 TALLOC_FREE(tmp_ctx
);
864 return NT_STATUS_NO_MEMORY
;
868 DEBUG(5, ("secrets_get_trusted_domains: got %d domains\n",
871 /* free the results of searching the keys */
872 tdb_search_list_free(keys
);
873 TALLOC_FREE(tmp_ctx
);
878 /*******************************************************************************
879 Lock the secrets tdb based on a string - this is used as a primitive form of mutex
880 between smbd instances.
881 *******************************************************************************/
883 bool secrets_named_mutex(const char *name
, unsigned int timeout
)
890 ret
= tdb_lock_bystring_with_timeout(tdb
, name
, timeout
);
892 DEBUG(10,("secrets_named_mutex: got mutex for %s\n", name
));
897 /*******************************************************************************
898 Unlock a named mutex.
899 *******************************************************************************/
901 void secrets_named_mutex_release(const char *name
)
903 tdb_unlock_bystring(tdb
, name
);
904 DEBUG(10,("secrets_named_mutex: released mutex for %s\n", name
));
907 /*******************************************************************************
908 Store a complete AFS keyfile into secrets.tdb.
909 *******************************************************************************/
911 bool secrets_store_afs_keyfile(const char *cell
, const struct afs_keyfile
*keyfile
)
915 if ((cell
== NULL
) || (keyfile
== NULL
))
918 if (ntohl(keyfile
->nkeys
) > SECRETS_AFS_MAXKEYS
)
921 slprintf(key
, sizeof(key
)-1, "%s/%s", SECRETS_AFS_KEYFILE
, cell
);
922 return secrets_store(key
, keyfile
, sizeof(struct afs_keyfile
));
925 /*******************************************************************************
926 Fetch the current (highest) AFS key from secrets.tdb
927 *******************************************************************************/
928 bool secrets_fetch_afs_key(const char *cell
, struct afs_key
*result
)
931 struct afs_keyfile
*keyfile
;
935 slprintf(key
, sizeof(key
)-1, "%s/%s", SECRETS_AFS_KEYFILE
, cell
);
937 keyfile
= (struct afs_keyfile
*)secrets_fetch(key
, &size
);
942 if (size
!= sizeof(struct afs_keyfile
)) {
947 i
= ntohl(keyfile
->nkeys
);
949 if (i
> SECRETS_AFS_MAXKEYS
) {
954 *result
= keyfile
->entry
[i
-1];
956 result
->kvno
= ntohl(result
->kvno
);
961 /******************************************************************************
962 When kerberos is not available, choose between anonymous or
963 authenticated connections.
965 We need to use an authenticated connection if DCs have the
966 RestrictAnonymous registry entry set > 0, or the "Additional
967 restrictions for anonymous connections" set in the win2k Local
970 Caller to free() result in domain, username, password
971 *******************************************************************************/
972 void secrets_fetch_ipc_userpass(char **username
, char **domain
, char **password
)
974 *username
= (char *)secrets_fetch(SECRETS_AUTH_USER
, NULL
);
975 *domain
= (char *)secrets_fetch(SECRETS_AUTH_DOMAIN
, NULL
);
976 *password
= (char *)secrets_fetch(SECRETS_AUTH_PASSWORD
, NULL
);
978 if (*username
&& **username
) {
980 if (!*domain
|| !**domain
)
981 *domain
= smb_xstrdup(lp_workgroup());
983 if (!*password
|| !**password
)
984 *password
= smb_xstrdup("");
986 DEBUG(3, ("IPC$ connections done by user %s\\%s\n",
987 *domain
, *username
));
990 DEBUG(3, ("IPC$ connections done anonymously\n"));
991 *username
= smb_xstrdup("");
992 *domain
= smb_xstrdup("");
993 *password
= smb_xstrdup("");
997 /******************************************************************************
998 Open or create the schannel session store tdb.
999 *******************************************************************************/
1001 static TDB_CONTEXT
*open_schannel_session_store(TALLOC_CTX
*mem_ctx
)
1005 TDB_CONTEXT
*tdb_sc
= NULL
;
1006 char *fname
= talloc_asprintf(mem_ctx
, "%s/schannel_store.tdb", lp_private_dir());
1012 tdb_sc
= tdb_open_log(fname
, 0, TDB_DEFAULT
, O_RDWR
|O_CREAT
, 0600);
1015 DEBUG(0,("open_schannel_session_store: Failed to open %s\n", fname
));
1020 vers
= tdb_fetch_bystring(tdb_sc
, "SCHANNEL_STORE_VERSION");
1021 if (vers
.dptr
== NULL
) {
1022 /* First opener, no version. */
1024 vers
.dptr
= (uint8
*)&ver
;
1026 tdb_store_bystring(tdb_sc
, "SCHANNEL_STORE_VERSION", vers
, TDB_REPLACE
);
1028 } else if (vers
.dsize
== 4) {
1029 ver
= IVAL(vers
.dptr
,0);
1033 DEBUG(0,("open_schannel_session_store: wrong version number %d in %s\n",
1039 DEBUG(0,("open_schannel_session_store: wrong version number size %d in %s\n",
1040 (int)vers
.dsize
, fname
));
1043 SAFE_FREE(vers
.dptr
);
1049 /******************************************************************************
1050 Store the schannel state after an AUTH2 call.
1051 Note we must be root here.
1052 *******************************************************************************/
1054 bool secrets_store_schannel_session_info(TALLOC_CTX
*mem_ctx
,
1055 const char *remote_machine
,
1056 const struct dcinfo
*pdc
)
1058 TDB_CONTEXT
*tdb_sc
= NULL
;
1061 char *keystr
= talloc_asprintf(mem_ctx
, "%s/%s", SECRETS_SCHANNEL_STATE
,
1069 /* Work out how large the record is. */
1070 value
.dsize
= tdb_pack(NULL
, 0, "dBBBBBfff",
1072 8, pdc
->seed_chal
.data
,
1073 8, pdc
->clnt_chal
.data
,
1074 8, pdc
->srv_chal
.data
,
1078 pdc
->remote_machine
,
1081 value
.dptr
= TALLOC_ARRAY(mem_ctx
, uint8
, value
.dsize
);
1083 TALLOC_FREE(keystr
);
1087 value
.dsize
= tdb_pack(value
.dptr
, value
.dsize
, "dBBBBBfff",
1089 8, pdc
->seed_chal
.data
,
1090 8, pdc
->clnt_chal
.data
,
1091 8, pdc
->srv_chal
.data
,
1095 pdc
->remote_machine
,
1098 tdb_sc
= open_schannel_session_store(mem_ctx
);
1100 TALLOC_FREE(keystr
);
1101 TALLOC_FREE(value
.dptr
);
1105 ret
= (tdb_store_bystring(tdb_sc
, keystr
, value
, TDB_REPLACE
) == 0 ? True
: False
);
1107 DEBUG(3,("secrets_store_schannel_session_info: stored schannel info with key %s\n",
1111 TALLOC_FREE(keystr
);
1112 TALLOC_FREE(value
.dptr
);
1116 /******************************************************************************
1117 Restore the schannel state on a client reconnect.
1118 Note we must be root here.
1119 *******************************************************************************/
1121 bool secrets_restore_schannel_session_info(TALLOC_CTX
*mem_ctx
,
1122 const char *remote_machine
,
1123 struct dcinfo
**ppdc
)
1125 TDB_CONTEXT
*tdb_sc
= NULL
;
1127 unsigned char *pseed_chal
= NULL
;
1128 unsigned char *pclnt_chal
= NULL
;
1129 unsigned char *psrv_chal
= NULL
;
1130 unsigned char *psess_key
= NULL
;
1131 unsigned char *pmach_pw
= NULL
;
1132 uint32 l1
, l2
, l3
, l4
, l5
;
1134 struct dcinfo
*pdc
= NULL
;
1135 char *keystr
= talloc_asprintf(mem_ctx
, "%s/%s", SECRETS_SCHANNEL_STATE
,
1146 tdb_sc
= open_schannel_session_store(mem_ctx
);
1148 TALLOC_FREE(keystr
);
1152 value
= tdb_fetch_bystring(tdb_sc
, keystr
);
1154 DEBUG(0,("secrets_restore_schannel_session_info: Failed to find entry with key %s\n",
1160 pdc
= TALLOC_ZERO_P(mem_ctx
, struct dcinfo
);
1162 /* Retrieve the record. */
1163 ret
= tdb_unpack(value
.dptr
, value
.dsize
, "dBBBBBfff",
1171 &pdc
->remote_machine
,
1174 if (ret
== -1 || l1
!= 8 || l2
!= 8 || l3
!= 8 || l4
!= 16 || l5
!= 16) {
1175 /* Bad record - delete it. */
1176 tdb_delete_bystring(tdb_sc
, keystr
);
1178 TALLOC_FREE(keystr
);
1180 SAFE_FREE(pseed_chal
);
1181 SAFE_FREE(pclnt_chal
);
1182 SAFE_FREE(psrv_chal
);
1183 SAFE_FREE(psess_key
);
1184 SAFE_FREE(pmach_pw
);
1185 SAFE_FREE(value
.dptr
);
1191 memcpy(pdc
->seed_chal
.data
, pseed_chal
, 8);
1192 memcpy(pdc
->clnt_chal
.data
, pclnt_chal
, 8);
1193 memcpy(pdc
->srv_chal
.data
, psrv_chal
, 8);
1194 memcpy(pdc
->sess_key
, psess_key
, 16);
1195 memcpy(pdc
->mach_pw
, pmach_pw
, 16);
1197 /* We know these are true so didn't bother to store them. */
1198 pdc
->challenge_sent
= True
;
1199 pdc
->authenticated
= True
;
1201 DEBUG(3,("secrets_restore_schannel_session_info: restored schannel info key %s\n",
1204 SAFE_FREE(pseed_chal
);
1205 SAFE_FREE(pclnt_chal
);
1206 SAFE_FREE(psrv_chal
);
1207 SAFE_FREE(psess_key
);
1208 SAFE_FREE(pmach_pw
);
1210 TALLOC_FREE(keystr
);
1211 SAFE_FREE(value
.dptr
);
1218 bool secrets_store_generic(const char *owner
, const char *key
, const char *secret
)
1220 char *tdbkey
= NULL
;
1223 if (asprintf(&tdbkey
, "SECRETS/GENERIC/%s/%s", owner
, key
) < 0) {
1224 DEBUG(0, ("asprintf failed!\n"));
1228 ret
= secrets_store(tdbkey
, secret
, strlen(secret
)+1);
1234 /*******************************************************************
1235 Find the ldap password.
1236 ******************************************************************/
1238 char *secrets_fetch_generic(const char *owner
, const char *key
)
1240 char *secret
= NULL
;
1241 char *tdbkey
= NULL
;
1243 if (( ! owner
) || ( ! key
)) {
1244 DEBUG(1, ("Invalid Paramters"));
1248 if (asprintf(&tdbkey
, "SECRETS/GENERIC/%s/%s", owner
, key
) < 0) {
1249 DEBUG(0, ("Out of memory!\n"));
1253 secret
= (char *)secrets_fetch(tdbkey
, NULL
);