static pstring removal
[Samba.git] / source3 / passdb / secrets.c
bloba7221df6df117287eeca6b4e75faa02fe8ed1df7
1 /*
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 */
25 #include "includes.h"
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_PASSDB
30 static TDB_CONTEXT *tdb;
32 /* Urrrg. global.... */
33 bool global_machine_password_needs_changing;
35 /**
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();
46 if (tdb) {
47 tdb_change_int32_atomic(tdb, "INFO/random_seed", new_seed, 1);
51 /* open up the secrets database */
52 bool secrets_init(void)
54 pstring fname;
55 unsigned char dummy;
57 if (tdb)
58 return True;
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);
65 if (!tdb) {
66 DEBUG(0,("Failed to open %s\n", fname));
67 return False;
70 /**
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));
81 return True;
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)
89 TDB_DATA dbuf;
90 secrets_init();
91 if (!tdb)
92 return NULL;
93 dbuf = tdb_fetch(tdb, string_tdb_data(key));
94 if (size)
95 *size = dbuf.dsize;
96 return dbuf.dptr;
99 /* store a secrets entry
101 bool secrets_store(const char *key, const void *data, size_t size)
103 secrets_init();
104 if (!tdb)
105 return False;
106 return tdb_trans_store(tdb, string_tdb_data(key),
107 make_tdb_data((const uint8 *)data, size),
108 TDB_REPLACE) == 0;
112 /* delete a secets database entry
114 bool secrets_delete(const char *key)
116 secrets_init();
117 if (!tdb)
118 return False;
119 return tdb_trans_delete(tdb, string_tdb_data(key)) == 0;
122 bool secrets_store_domain_sid(const char *domain, const DOM_SID *sid)
124 fstring key;
125 bool ret;
127 slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_DOMAIN_SID, domain);
128 strupper_m(key);
129 ret = secrets_store(key, sid, sizeof(DOM_SID));
131 /* Force a re-query, in case we modified our domain */
132 if (ret)
133 reset_global_sam_sid();
134 return ret;
137 bool secrets_fetch_domain_sid(const char *domain, DOM_SID *sid)
139 DOM_SID *dyn_sid;
140 fstring key;
141 size_t size = 0;
143 slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_DOMAIN_SID, domain);
144 strupper_m(key);
145 dyn_sid = (DOM_SID *)secrets_fetch(key, &size);
147 if (dyn_sid == NULL)
148 return False;
150 if (size != sizeof(DOM_SID)) {
151 SAFE_FREE(dyn_sid);
152 return False;
155 *sid = *dyn_sid;
156 SAFE_FREE(dyn_sid);
157 return True;
160 bool secrets_store_domain_guid(const char *domain, struct GUID *guid)
162 fstring key;
164 slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_DOMAIN_GUID, domain);
165 strupper_m(key);
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;
172 fstring key;
173 size_t size = 0;
174 struct GUID new_guid;
176 slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_DOMAIN_GUID, domain);
177 strupper_m(key);
178 dyn_guid = (struct GUID *)secrets_fetch(key, &size);
180 if (!dyn_guid) {
181 if (lp_server_role() == ROLE_DOMAIN_PDC) {
182 smb_uuid_generate_random(&new_guid);
183 if (!secrets_store_domain_guid(domain, &new_guid))
184 return False;
185 dyn_guid = (struct GUID *)secrets_fetch(key, &size);
187 if (dyn_guid == NULL) {
188 return False;
192 if (size != sizeof(struct GUID)) {
193 DEBUG(1,("UUID size %d is wrong!\n", (int)size));
194 SAFE_FREE(dyn_guid);
195 return False;
198 *guid = *dyn_guid;
199 SAFE_FREE(dyn_guid);
200 return True;
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)
212 char *keystr;
214 keystr = talloc_asprintf(talloc_tos(), "%s/%s",
215 SECRETS_MACHINE_ACCT_PASS, domain);
216 SMB_ASSERT(keystr != NULL);
218 strupper_m(keystr);
220 return keystr;
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)
232 char *keystr;
234 keystr = talloc_asprintf(talloc_tos(), "%s/%s",
235 SECRETS_DOMTRUST_ACCT_PASS, domain);
236 SMB_ASSERT(keystr != NULL);
237 strupper_m(keystr);
239 return keystr;
242 /************************************************************************
243 Lock the trust password entry.
244 ************************************************************************/
246 bool secrets_lock_trust_account_password(const char *domain, bool dolock)
248 if (!tdb)
249 return False;
251 if (dolock)
252 return (tdb_lock_bystring(tdb, trust_keystr(domain)) == 0);
253 else
254 tdb_unlock_bystring(tdb, trust_keystr(domain));
255 return True;
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) {
266 return SEC_CHAN_BDC;
267 } else {
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,
280 uint32 *channel)
282 struct machine_acct_pass *pass;
283 char *plaintext;
284 size_t size = 0;
286 plaintext = secrets_fetch_machine_password(domain, pass_last_set_time,
287 channel);
288 if (plaintext) {
289 DEBUG(4,("Using cleartext machine password\n"));
290 E_md4hash(plaintext, ret_pwd);
291 SAFE_FREE(plaintext);
292 return True;
295 if (!(pass = (struct machine_acct_pass *)secrets_fetch(
296 trust_keystr(domain), &size))) {
297 DEBUG(5, ("secrets_fetch failed!\n"));
298 return False;
301 if (size != sizeof(*pass)) {
302 DEBUG(0, ("secrets were of incorrect size!\n"));
303 return False;
306 if (pass_last_set_time) {
307 *pass_last_set_time = pass->mod_time;
309 memcpy(ret_pwd, pass->hash, 16);
311 if (channel) {
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;
323 SAFE_FREE(pass);
324 return 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)
338 int idx;
339 size_t len = 0;
341 if (!sid || !pack_buf) return -1;
343 len += tdb_pack(pack_buf + len, bufsize - len, "bb", sid->sid_rev_num,
344 sid->num_auths);
346 for (idx = 0; idx < 6; idx++) {
347 len += tdb_pack(pack_buf + len, bufsize - len, "b",
348 sid->id_auth[idx]);
351 for (idx = 0; idx < MAXSUBAUTHS; idx++) {
352 len += tdb_pack(pack_buf + len, bufsize - len, "d",
353 sid->sub_auths[idx]);
356 return len;
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)
370 int idx, len = 0;
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",
379 &sid->id_auth[idx]);
382 for (idx = 0; idx < MAXSUBAUTHS; idx++) {
383 len += tdb_unpack(pack_buf + len, bufsize - len, "d",
384 &sid->sub_auths[idx]);
387 return len;
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)
402 int idx, len = 0;
404 if (!pack_buf || !pass) return -1;
406 /* packing unicode domain name and password */
407 len += tdb_pack(pack_buf + len, bufsize - len, "d",
408 pass->uni_name_len);
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);
420 return len;
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)
436 int idx, len = 0;
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,
452 &pass->domain_sid);
454 return 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;
465 size_t size = 0;
467 /* unpacking structures */
468 uint8 *pass_buf;
469 int pass_len = 0;
471 ZERO_STRUCT(pass);
473 /* fetching trusted domain password structure */
474 if (!(pass_buf = (uint8 *)secrets_fetch(trustdom_keystr(domain),
475 &size))) {
476 DEBUG(5, ("secrets_fetch failed!\n"));
477 return False;
480 /* unpack trusted domain password */
481 pass_len = tdb_trusted_dom_pass_unpack(pass_buf, size, &pass);
482 SAFE_FREE(pass_buf);
484 if (pass_len != size) {
485 DEBUG(5, ("Invalid secrets size. Unpacked data doesn't match trusted_dom_pass structure.\n"));
486 return False;
489 /* the trust's password */
490 if (pwd) {
491 *pwd = SMB_STRDUP(pass.pass);
492 if (!*pwd) {
493 return False;
497 /* last change time */
498 if (pass_last_set_time) *pass_last_set_time = pass.mod_time;
500 /* domain sid */
501 if (sid != NULL) sid_copy(sid, &pass.domain_sid);
503 return True;
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,
531 const DOM_SID *sid)
533 smb_ucs2_t *uni_dom_name;
535 /* packing structures */
536 pstring pass_buf;
537 int pass_len = 0;
538 int pass_buf_len = sizeof(pass_buf);
540 struct trusted_dom_pass pass;
541 ZERO_STRUCT(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",
545 domain));
546 return False;
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);
560 /* domain sid */
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)
575 char *key = NULL;
576 bool ret;
577 uint32 last_change_time;
578 uint32 sec_channel_type;
580 asprintf(&key, "%s/%s", SECRETS_MACHINE_PASSWORD, domain);
581 if (!key)
582 return False;
583 strupper_m(key);
585 ret = secrets_store(key, pass, strlen(pass)+1);
586 SAFE_FREE(key);
588 if (!ret)
589 return ret;
591 asprintf(&key, "%s/%s", SECRETS_MACHINE_LAST_CHANGE_TIME, domain);
592 if (!key)
593 return False;
594 strupper_m(key);
596 SIVAL(&last_change_time, 0, time(NULL));
597 ret = secrets_store(key, &last_change_time, sizeof(last_change_time));
598 SAFE_FREE(key);
600 asprintf(&key, "%s/%s", SECRETS_MACHINE_SEC_CHANNEL_TYPE, domain);
601 if (!key)
602 return False;
603 strupper_m(key);
605 SIVAL(&sec_channel_type, 0, sec_channel);
606 ret = secrets_store(key, &sec_channel_type, sizeof(sec_channel_type));
607 SAFE_FREE(key);
609 return ret;
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,
619 uint32 *channel)
621 char *key = NULL;
622 char *ret;
623 asprintf(&key, "%s/%s", SECRETS_MACHINE_PASSWORD, domain);
624 strupper_m(key);
625 ret = (char *)secrets_fetch(key, NULL);
626 SAFE_FREE(key);
628 if (pass_last_set_time) {
629 size_t size;
630 uint32 *last_set_time;
631 asprintf(&key, "%s/%s", SECRETS_MACHINE_LAST_CHANGE_TIME, domain);
632 strupper_m(key);
633 last_set_time = (unsigned int *)secrets_fetch(key, &size);
634 if (last_set_time) {
635 *pass_last_set_time = IVAL(last_set_time,0);
636 SAFE_FREE(last_set_time);
637 } else {
638 *pass_last_set_time = 0;
640 SAFE_FREE(key);
643 if (channel) {
644 size_t size;
645 uint32 *channel_type;
646 asprintf(&key, "%s/%s", SECRETS_MACHINE_SEC_CHANNEL_TYPE, domain);
647 strupper_m(key);
648 channel_type = (unsigned int *)secrets_fetch(key, &size);
649 if (channel_type) {
650 *channel = IVAL(channel_type,0);
651 SAFE_FREE(channel_type);
652 } else {
653 *channel = get_default_sec_channel();
655 SAFE_FREE(key);
658 return ret;
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)
681 char *key = NULL;
682 bool ret;
684 if (asprintf(&key, "%s/%s", SECRETS_LDAP_BIND_PW, dn) < 0) {
685 DEBUG(0, ("secrets_store_ldap_pw: asprintf failed!\n"));
686 return False;
689 ret = secrets_store(key, pw, strlen(pw)+1);
691 SAFE_FREE(key);
692 return ret;
695 /*******************************************************************
696 Find the ldap password.
697 ******************************************************************/
699 bool fetch_ldap_pw(char **dn, char** pw)
701 char *key = NULL;
702 size_t size = 0;
704 *dn = smb_xstrdup(lp_ldap_admin_dn());
706 if (asprintf(&key, "%s/%s", SECRETS_LDAP_BIND_PW, *dn) < 0) {
707 SAFE_FREE(*dn);
708 DEBUG(0, ("fetch_ldap_pw: asprintf failed!\n"));
711 *pw=(char *)secrets_fetch(key, &size);
712 SAFE_FREE(key);
714 if (!size) {
715 /* Upgrade 2.2 style entry */
716 char *p;
717 char* old_style_key = SMB_STRDUP(*dn);
718 char *data;
719 fstring old_style_pw;
721 if (!old_style_key) {
722 DEBUG(0, ("fetch_ldap_pw: strdup failed!\n"));
723 return False;
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);
733 SAFE_FREE(*dn);
734 return False;
737 size = MIN(size, sizeof(fstring)-1);
738 strncpy(old_style_pw, data, size);
739 old_style_pw[size] = 0;
741 SAFE_FREE(data);
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);
746 SAFE_FREE(*dn);
747 return False;
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);
758 return True;
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;
769 char *pattern;
770 TALLOC_CTX *tmp_ctx;
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() "
782 "failed!\n"));
783 TALLOC_FREE(tmp_ctx);
784 return NT_STATUS_NO_MEMORY;
787 *num_domains = 0;
790 * Make sure that a talloc context for the trustdom_info structs
791 * exists
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) {
804 uint8 *packed_pass;
805 size_t size = 0, packed_size = 0;
806 struct trusted_dom_pass pass;
807 char *secrets_key;
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,
813 k->node_key.dsize);
814 if (!secrets_key) {
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,
823 &pass);
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",
829 secrets_key));
830 continue;
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));
838 continue;
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",
869 *num_domains));
871 /* free the results of searching the keys */
872 tdb_search_list_free(keys);
873 TALLOC_FREE(tmp_ctx);
875 return NT_STATUS_OK;
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)
885 int ret = 0;
887 if (!secrets_init())
888 return False;
890 ret = tdb_lock_bystring_with_timeout(tdb, name, timeout);
891 if (ret == 0)
892 DEBUG(10,("secrets_named_mutex: got mutex for %s\n", name ));
894 return (ret == 0);
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)
913 fstring key;
915 if ((cell == NULL) || (keyfile == NULL))
916 return False;
918 if (ntohl(keyfile->nkeys) > SECRETS_AFS_MAXKEYS)
919 return False;
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)
930 fstring key;
931 struct afs_keyfile *keyfile;
932 size_t size = 0;
933 uint32 i;
935 slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_AFS_KEYFILE, cell);
937 keyfile = (struct afs_keyfile *)secrets_fetch(key, &size);
939 if (keyfile == NULL)
940 return False;
942 if (size != sizeof(struct afs_keyfile)) {
943 SAFE_FREE(keyfile);
944 return False;
947 i = ntohl(keyfile->nkeys);
949 if (i > SECRETS_AFS_MAXKEYS) {
950 SAFE_FREE(keyfile);
951 return False;
954 *result = keyfile->entry[i-1];
956 result->kvno = ntohl(result->kvno);
958 return True;
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
968 Security Policy.
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));
989 } else {
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)
1003 TDB_DATA vers;
1004 uint32 ver;
1005 TDB_CONTEXT *tdb_sc = NULL;
1006 char *fname = talloc_asprintf(mem_ctx, "%s/schannel_store.tdb", lp_private_dir());
1008 if (!fname) {
1009 return NULL;
1012 tdb_sc = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
1014 if (!tdb_sc) {
1015 DEBUG(0,("open_schannel_session_store: Failed to open %s\n", fname));
1016 TALLOC_FREE(fname);
1017 return NULL;
1020 vers = tdb_fetch_bystring(tdb_sc, "SCHANNEL_STORE_VERSION");
1021 if (vers.dptr == NULL) {
1022 /* First opener, no version. */
1023 SIVAL(&ver,0,1);
1024 vers.dptr = (uint8 *)&ver;
1025 vers.dsize = 4;
1026 tdb_store_bystring(tdb_sc, "SCHANNEL_STORE_VERSION", vers, TDB_REPLACE);
1027 vers.dptr = NULL;
1028 } else if (vers.dsize == 4) {
1029 ver = IVAL(vers.dptr,0);
1030 if (ver != 1) {
1031 tdb_close(tdb_sc);
1032 tdb_sc = NULL;
1033 DEBUG(0,("open_schannel_session_store: wrong version number %d in %s\n",
1034 (int)ver, fname ));
1036 } else {
1037 tdb_close(tdb_sc);
1038 tdb_sc = NULL;
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);
1044 TALLOC_FREE(fname);
1046 return tdb_sc;
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;
1059 TDB_DATA value;
1060 bool ret;
1061 char *keystr = talloc_asprintf(mem_ctx, "%s/%s", SECRETS_SCHANNEL_STATE,
1062 remote_machine);
1063 if (!keystr) {
1064 return False;
1067 strupper_m(keystr);
1069 /* Work out how large the record is. */
1070 value.dsize = tdb_pack(NULL, 0, "dBBBBBfff",
1071 pdc->sequence,
1072 8, pdc->seed_chal.data,
1073 8, pdc->clnt_chal.data,
1074 8, pdc->srv_chal.data,
1075 16, pdc->sess_key,
1076 16, pdc->mach_pw,
1077 pdc->mach_acct,
1078 pdc->remote_machine,
1079 pdc->domain);
1081 value.dptr = TALLOC_ARRAY(mem_ctx, uint8, value.dsize);
1082 if (!value.dptr) {
1083 TALLOC_FREE(keystr);
1084 return False;
1087 value.dsize = tdb_pack(value.dptr, value.dsize, "dBBBBBfff",
1088 pdc->sequence,
1089 8, pdc->seed_chal.data,
1090 8, pdc->clnt_chal.data,
1091 8, pdc->srv_chal.data,
1092 16, pdc->sess_key,
1093 16, pdc->mach_pw,
1094 pdc->mach_acct,
1095 pdc->remote_machine,
1096 pdc->domain);
1098 tdb_sc = open_schannel_session_store(mem_ctx);
1099 if (!tdb_sc) {
1100 TALLOC_FREE(keystr);
1101 TALLOC_FREE(value.dptr);
1102 return False;
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",
1108 keystr ));
1110 tdb_close(tdb_sc);
1111 TALLOC_FREE(keystr);
1112 TALLOC_FREE(value.dptr);
1113 return ret;
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;
1126 TDB_DATA value;
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;
1133 int ret;
1134 struct dcinfo *pdc = NULL;
1135 char *keystr = talloc_asprintf(mem_ctx, "%s/%s", SECRETS_SCHANNEL_STATE,
1136 remote_machine);
1138 *ppdc = NULL;
1140 if (!keystr) {
1141 return False;
1144 strupper_m(keystr);
1146 tdb_sc = open_schannel_session_store(mem_ctx);
1147 if (!tdb_sc) {
1148 TALLOC_FREE(keystr);
1149 return False;
1152 value = tdb_fetch_bystring(tdb_sc, keystr);
1153 if (!value.dptr) {
1154 DEBUG(0,("secrets_restore_schannel_session_info: Failed to find entry with key %s\n",
1155 keystr ));
1156 tdb_close(tdb_sc);
1157 return False;
1160 pdc = TALLOC_ZERO_P(mem_ctx, struct dcinfo);
1162 /* Retrieve the record. */
1163 ret = tdb_unpack(value.dptr, value.dsize, "dBBBBBfff",
1164 &pdc->sequence,
1165 &l1, &pseed_chal,
1166 &l2, &pclnt_chal,
1167 &l3, &psrv_chal,
1168 &l4, &psess_key,
1169 &l5, &pmach_pw,
1170 &pdc->mach_acct,
1171 &pdc->remote_machine,
1172 &pdc->domain);
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);
1177 tdb_close(tdb_sc);
1178 TALLOC_FREE(keystr);
1179 TALLOC_FREE(pdc);
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);
1186 return False;
1189 tdb_close(tdb_sc);
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",
1202 keystr ));
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);
1213 *ppdc = pdc;
1215 return True;
1218 bool secrets_store_generic(const char *owner, const char *key, const char *secret)
1220 char *tdbkey = NULL;
1221 bool ret;
1223 if (asprintf(&tdbkey, "SECRETS/GENERIC/%s/%s", owner, key) < 0) {
1224 DEBUG(0, ("asprintf failed!\n"));
1225 return False;
1228 ret = secrets_store(tdbkey, secret, strlen(secret)+1);
1230 SAFE_FREE(tdbkey);
1231 return ret;
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"));
1245 return NULL;
1248 if (asprintf(&tdbkey, "SECRETS/GENERIC/%s/%s", owner, key) < 0) {
1249 DEBUG(0, ("Out of memory!\n"));
1250 return NULL;
1253 secret = (char *)secrets_fetch(tdbkey, NULL);
1254 SAFE_FREE(tdbkey);
1256 return secret;