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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 /* the Samba secrets database stores any generated, private information
24 such as the local SID and machine trust password */
29 #define DBGC_CLASS DBGC_PASSDB
31 static TDB_CONTEXT *tdb;
34 * Use a TDB to store an incrementing random seed.
36 * Initialised to the current pid, the very first time Samba starts,
37 * and incremented by one each time it is needed.
39 * @note Not called by systems with a working /dev/urandom.
41 static void get_rand_seed(int *new_seed)
43 *new_seed = sys_getpid();
45 tdb_change_int32_atomic(tdb, "INFO/random_seed", new_seed, 1);
49 /* open up the secrets database */
50 BOOL secrets_init(void)
58 pstrcpy(fname, lp_private_dir());
59 pstrcat(fname,"/secrets.tdb");
61 tdb = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
64 DEBUG(0,("Failed to open %s\n", fname));
69 * Set a reseed function for the crypto random generator
71 * This avoids a problem where systems without /dev/urandom
72 * could send the same challenge to multiple clients
74 set_rand_reseed_callback(get_rand_seed);
76 /* Ensure that the reseed is done now, while we are root, etc */
77 generate_random_buffer(&dummy, sizeof(dummy));
82 /* read a entry from the secrets database - the caller must free the result
83 if size is non-null then the size of the entry is put in there
85 void *secrets_fetch(const char *key, size_t *size)
91 dbuf = tdb_fetch(tdb, string_tdb_data(key));
97 /* store a secrets entry
99 BOOL secrets_store(const char *key, const void *data, size_t size)
104 return tdb_store(tdb, string_tdb_data(key), make_tdb_data(data, size),
109 /* delete a secets database entry
111 BOOL secrets_delete(const char *key)
116 return tdb_delete(tdb, string_tdb_data(key)) == 0;
119 BOOL secrets_store_domain_sid(const char *domain, const DOM_SID *sid)
124 slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_DOMAIN_SID, domain);
126 ret = secrets_store(key, sid, sizeof(DOM_SID));
128 /* Force a re-query, in case we modified our domain */
130 reset_global_sam_sid();
134 BOOL secrets_fetch_domain_sid(const char *domain, DOM_SID *sid)
140 slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_DOMAIN_SID, domain);
142 dyn_sid = (DOM_SID *)secrets_fetch(key, &size);
147 if (size != sizeof(DOM_SID))
158 BOOL secrets_store_domain_guid(const char *domain, struct uuid *guid)
162 slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_DOMAIN_GUID, domain);
164 return secrets_store(key, guid, sizeof(struct uuid));
167 BOOL secrets_fetch_domain_guid(const char *domain, struct uuid *guid)
169 struct uuid *dyn_guid;
172 struct uuid new_guid;
174 slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_DOMAIN_GUID, domain);
176 dyn_guid = (struct uuid *)secrets_fetch(key, &size);
178 if ((!dyn_guid) && (lp_server_role() == ROLE_DOMAIN_PDC)) {
179 smb_uuid_generate_random(&new_guid);
180 if (!secrets_store_domain_guid(domain, &new_guid))
182 dyn_guid = (struct uuid *)secrets_fetch(key, &size);
183 if (dyn_guid == NULL)
187 if (size != sizeof(struct uuid))
189 DEBUG(1,("UUID size %d is wrong!\n", (int)size));
200 * Form a key for fetching the machine trust account password
202 * @param domain domain name
204 * @return stored password's key
206 const char *trust_keystr(const char *domain)
208 static fstring keystr;
210 slprintf(keystr,sizeof(keystr)-1,"%s/%s",
211 SECRETS_MACHINE_ACCT_PASS, domain);
218 * Form a key for fetching a trusted domain password
220 * @param domain trusted domain name
222 * @return stored password's key
224 static char *trustdom_keystr(const char *domain)
226 static pstring keystr;
228 pstr_sprintf(keystr, "%s/%s", SECRETS_DOMTRUST_ACCT_PASS, domain);
234 /************************************************************************
235 Lock the trust password entry.
236 ************************************************************************/
238 BOOL secrets_lock_trust_account_password(const char *domain, BOOL dolock)
244 return (tdb_lock_bystring(tdb, trust_keystr(domain),0) == 0);
246 tdb_unlock_bystring(tdb, trust_keystr(domain));
250 /************************************************************************
251 Routine to get the default secure channel type for trust accounts
252 ************************************************************************/
254 uint32 get_default_sec_channel(void)
256 if (lp_server_role() == ROLE_DOMAIN_BDC ||
257 lp_server_role() == ROLE_DOMAIN_PDC) {
260 return SEC_CHAN_WKSTA;
264 /************************************************************************
265 Routine to get the trust account password for a domain.
266 The user of this function must have locked the trust password file using
267 the above secrets_lock_trust_account_password().
268 ************************************************************************/
270 BOOL secrets_fetch_trust_account_password(const char *domain, uint8 ret_pwd[16],
271 time_t *pass_last_set_time,
274 struct machine_acct_pass *pass;
278 plaintext = secrets_fetch_machine_password(domain, pass_last_set_time,
281 DEBUG(4,("Using cleartext machine password\n"));
282 E_md4hash(plaintext, ret_pwd);
283 SAFE_FREE(plaintext);
287 if (!(pass = secrets_fetch(trust_keystr(domain), &size))) {
288 DEBUG(5, ("secrets_fetch failed!\n"));
292 if (size != sizeof(*pass)) {
293 DEBUG(0, ("secrets were of incorrect size!\n"));
297 if (pass_last_set_time) *pass_last_set_time = pass->mod_time;
298 memcpy(ret_pwd, pass->hash, 16);
302 *channel = get_default_sec_channel();
307 /************************************************************************
308 Routine to get account password to trusted domain
309 ************************************************************************/
311 BOOL secrets_fetch_trusted_domain_password(const char *domain, char** pwd,
312 DOM_SID *sid, time_t *pass_last_set_time)
314 struct trusted_dom_pass pass;
317 /* unpacking structures */
323 /* fetching trusted domain password structure */
324 if (!(pass_buf = secrets_fetch(trustdom_keystr(domain), &size))) {
325 DEBUG(5, ("secrets_fetch failed!\n"));
329 /* unpack trusted domain password */
330 pass_len = tdb_trusted_dom_pass_unpack(pass_buf, size, &pass);
333 if (pass_len != size) {
334 DEBUG(5, ("Invalid secrets size. Unpacked data doesn't match trusted_dom_pass structure.\n"));
338 /* the trust's password */
340 *pwd = SMB_STRDUP(pass.pass);
346 /* last change time */
347 if (pass_last_set_time) *pass_last_set_time = pass.mod_time;
350 sid_copy(sid, &pass.domain_sid);
355 /************************************************************************
356 Routine to set the trust account password for a domain.
357 ************************************************************************/
359 BOOL secrets_store_trust_account_password(const char *domain, uint8 new_pwd[16])
361 struct machine_acct_pass pass;
363 pass.mod_time = time(NULL);
364 memcpy(pass.hash, new_pwd, 16);
366 return secrets_store(trust_keystr(domain), (void *)&pass, sizeof(pass));
370 * Routine to store the password for trusted domain
372 * @param domain remote domain name
373 * @param pwd plain text password of trust relationship
374 * @param sid remote domain sid
376 * @return true if succeeded
379 BOOL secrets_store_trusted_domain_password(const char* domain, smb_ucs2_t *uni_dom_name,
380 size_t uni_name_len, const char* pwd,
383 /* packing structures */
386 int pass_buf_len = sizeof(pass_buf);
388 struct trusted_dom_pass pass;
391 /* unicode domain name and its length */
395 strncpy_w(pass.uni_name, uni_dom_name, sizeof(pass.uni_name) - 1);
396 pass.uni_name_len = uni_name_len;
398 /* last change time */
399 pass.mod_time = time(NULL);
401 /* password of the trust */
402 pass.pass_len = strlen(pwd);
403 fstrcpy(pass.pass, pwd);
406 sid_copy(&pass.domain_sid, &sid);
408 pass_len = tdb_trusted_dom_pass_pack(pass_buf, pass_buf_len, &pass);
410 return secrets_store(trustdom_keystr(domain), (void *)&pass_buf, pass_len);
413 /************************************************************************
414 Routine to set the plaintext machine account password for a realm
415 the password is assumed to be a null terminated ascii string
416 ************************************************************************/
418 BOOL secrets_store_machine_password(const char *pass, const char *domain, uint32 sec_channel)
422 uint32 last_change_time;
423 uint32 sec_channel_type;
425 asprintf(&key, "%s/%s", SECRETS_MACHINE_PASSWORD, domain);
430 ret = secrets_store(key, pass, strlen(pass)+1);
436 asprintf(&key, "%s/%s", SECRETS_MACHINE_LAST_CHANGE_TIME, domain);
441 SIVAL(&last_change_time, 0, time(NULL));
442 ret = secrets_store(key, &last_change_time, sizeof(last_change_time));
445 asprintf(&key, "%s/%s", SECRETS_MACHINE_SEC_CHANNEL_TYPE, domain);
450 SIVAL(&sec_channel_type, 0, sec_channel);
451 ret = secrets_store(key, &sec_channel_type, sizeof(sec_channel_type));
458 /************************************************************************
459 Routine to fetch the plaintext machine account password for a realm
460 the password is assumed to be a null terminated ascii string
461 ************************************************************************/
462 char *secrets_fetch_machine_password(const char *domain,
463 time_t *pass_last_set_time,
468 asprintf(&key, "%s/%s", SECRETS_MACHINE_PASSWORD, domain);
470 ret = (char *)secrets_fetch(key, NULL);
473 if (pass_last_set_time) {
475 uint32 *last_set_time;
476 asprintf(&key, "%s/%s", SECRETS_MACHINE_LAST_CHANGE_TIME, domain);
478 last_set_time = secrets_fetch(key, &size);
480 *pass_last_set_time = IVAL(last_set_time,0);
481 SAFE_FREE(last_set_time);
483 *pass_last_set_time = 0;
490 uint32 *channel_type;
491 asprintf(&key, "%s/%s", SECRETS_MACHINE_SEC_CHANNEL_TYPE, domain);
493 channel_type = secrets_fetch(key, &size);
495 *channel = IVAL(channel_type,0);
496 SAFE_FREE(channel_type);
498 *channel = get_default_sec_channel();
508 /************************************************************************
509 Routine to delete the machine trust account password file for a domain.
510 ************************************************************************/
512 BOOL trust_password_delete(const char *domain)
514 return secrets_delete(trust_keystr(domain));
517 /************************************************************************
518 Routine to delete the password for trusted domain
519 ************************************************************************/
521 BOOL trusted_domain_password_delete(const char *domain)
523 return secrets_delete(trustdom_keystr(domain));
527 BOOL secrets_store_ldap_pw(const char* dn, char* pw)
532 if (asprintf(&key, "%s/%s", SECRETS_LDAP_BIND_PW, dn) < 0) {
533 DEBUG(0, ("secrets_store_ldap_pw: asprintf failed!\n"));
537 ret = secrets_store(key, pw, strlen(pw)+1);
543 /*******************************************************************
544 find the ldap password
545 ******************************************************************/
546 BOOL fetch_ldap_pw(char **dn, char** pw)
551 *dn = smb_xstrdup(lp_ldap_admin_dn());
553 if (asprintf(&key, "%s/%s", SECRETS_LDAP_BIND_PW, *dn) < 0) {
555 DEBUG(0, ("fetch_ldap_pw: asprintf failed!\n"));
558 *pw=secrets_fetch(key, &size);
562 /* Upgrade 2.2 style entry */
564 char* old_style_key = SMB_STRDUP(*dn);
566 fstring old_style_pw;
568 if (!old_style_key) {
569 DEBUG(0, ("fetch_ldap_pw: strdup failed!\n"));
573 for (p=old_style_key; *p; p++)
574 if (*p == ',') *p = '/';
576 data=secrets_fetch(old_style_key, &size);
577 if (!size && size < sizeof(old_style_pw)) {
578 DEBUG(0,("fetch_ldap_pw: neither ldap secret retrieved!\n"));
579 SAFE_FREE(old_style_key);
584 size = MIN(size, sizeof(fstring)-1);
585 strncpy(old_style_pw, data, size);
586 old_style_pw[size] = 0;
590 if (!secrets_store_ldap_pw(*dn, old_style_pw)) {
591 DEBUG(0,("fetch_ldap_pw: ldap secret could not be upgraded!\n"));
592 SAFE_FREE(old_style_key);
596 if (!secrets_delete(old_style_key)) {
597 DEBUG(0,("fetch_ldap_pw: old ldap secret could not be deleted!\n"));
600 SAFE_FREE(old_style_key);
602 *pw = smb_xstrdup(old_style_pw);
610 * Get trusted domains info from secrets.tdb.
612 * The linked list is allocated on the supplied talloc context, caller gets to destroy
615 * @param ctx Allocation context
616 * @param enum_ctx Starting index, eg. we can start fetching at third
617 * or sixth trusted domain entry. Zero is the first index.
618 * Value it is set to is the enum context for the next enumeration.
619 * @param num_domains Number of domain entries to fetch at one call
620 * @param domains Pointer to array of trusted domain structs to be filled up
622 * @return nt status code of rpc response
625 NTSTATUS secrets_get_trusted_domains(TALLOC_CTX* ctx, int* enum_ctx, unsigned int max_num_domains,
626 int *num_domains, TRUSTDOM ***domains)
628 TDB_LIST_NODE *keys, *k;
629 TRUSTDOM *dom = NULL;
631 unsigned int start_idx;
633 size_t size, packed_size = 0;
636 struct trusted_dom_pass *pass = TALLOC_ZERO_P(ctx, struct trusted_dom_pass);
639 if (!secrets_init()) return NT_STATUS_ACCESS_DENIED;
642 DEBUG(0, ("talloc_zero failed!\n"));
643 return NT_STATUS_NO_MEMORY;
647 start_idx = *enum_ctx;
649 /* generate searching pattern */
650 if (!(pattern = talloc_asprintf(ctx, "%s/*", SECRETS_DOMTRUST_ACCT_PASS))) {
651 DEBUG(0, ("secrets_get_trusted_domains: talloc_asprintf() failed!\n"));
652 return NT_STATUS_NO_MEMORY;
655 DEBUG(5, ("secrets_get_trusted_domains: looking for %d domains, starting at index %d\n",
656 max_num_domains, *enum_ctx));
658 *domains = TALLOC_ZERO_ARRAY(ctx, TRUSTDOM *, max_num_domains);
660 /* fetching trusted domains' data and collecting them in a list */
661 keys = tdb_search_keys(tdb, pattern);
664 * if there's no keys returned ie. no trusted domain,
665 * return "no more entries" code
667 status = NT_STATUS_NO_MORE_ENTRIES;
669 /* searching for keys in secrets db -- way to go ... */
670 for (k = keys; k; k = k->next) {
673 /* important: ensure null-termination of the key string */
674 secrets_key = SMB_STRNDUP(k->node_key.dptr, k->node_key.dsize);
676 DEBUG(0, ("strndup failed!\n"));
677 return NT_STATUS_NO_MEMORY;
680 packed_pass = secrets_fetch(secrets_key, &size);
681 packed_size = tdb_trusted_dom_pass_unpack(packed_pass, size, pass);
682 /* packed representation isn't needed anymore */
683 SAFE_FREE(packed_pass);
685 if (size != packed_size) {
686 DEBUG(2, ("Secrets record %s is invalid!\n", secrets_key));
690 pull_ucs2_fstring(dom_name, pass->uni_name);
691 DEBUG(18, ("Fetched secret record num %d.\nDomain name: %s, SID: %s\n",
692 idx, dom_name, sid_string_static(&pass->domain_sid)));
694 SAFE_FREE(secrets_key);
696 if (idx >= start_idx && idx < start_idx + max_num_domains) {
697 dom = TALLOC_ZERO_P(ctx, TRUSTDOM);
699 /* free returned tdb record */
700 return NT_STATUS_NO_MEMORY;
703 /* copy domain sid */
704 SMB_ASSERT(sizeof(dom->sid) == sizeof(pass->domain_sid));
705 memcpy(&(dom->sid), &(pass->domain_sid), sizeof(dom->sid));
707 /* copy unicode domain name */
708 dom->name = TALLOC_MEMDUP(ctx, pass->uni_name,
709 (strlen_w(pass->uni_name) + 1) * sizeof(smb_ucs2_t));
711 (*domains)[idx - start_idx] = dom;
713 DEBUG(18, ("Secret record is in required range.\n \
714 start_idx = %d, max_num_domains = %d. Added to returned array.\n",
715 start_idx, max_num_domains));
720 /* set proper status code to return */
722 /* there are yet some entries to enumerate */
723 status = STATUS_MORE_ENTRIES;
725 /* this is the last entry in the whole enumeration */
726 status = NT_STATUS_OK;
729 DEBUG(18, ("Secret is outside the required range.\n \
730 start_idx = %d, max_num_domains = %d. Not added to returned array\n",
731 start_idx, max_num_domains));
737 DEBUG(5, ("secrets_get_trusted_domains: got %d domains\n", *num_domains));
739 /* free the results of searching the keys */
740 tdb_search_list_free(keys);
745 /*******************************************************************************
746 Lock the secrets tdb based on a string - this is used as a primitive form of mutex
747 between smbd instances.
748 *******************************************************************************/
750 BOOL secrets_named_mutex(const char *name, unsigned int timeout)
757 ret = tdb_lock_bystring(tdb, name, timeout);
759 DEBUG(10,("secrets_named_mutex: got mutex for %s\n", name ));
764 /*******************************************************************************
765 Unlock a named mutex.
766 *******************************************************************************/
768 void secrets_named_mutex_release(const char *name)
770 tdb_unlock_bystring(tdb, name);
771 DEBUG(10,("secrets_named_mutex: released mutex for %s\n", name ));
774 /*********************************************************
775 Check to see if we must talk to the PDC to avoid sam
777 ********************************************************/
779 BOOL must_use_pdc( const char *domain )
781 time_t now = time(NULL);
782 time_t last_change_time;
783 unsigned char passwd[16];
785 if ( !secrets_fetch_trust_account_password(domain, passwd, &last_change_time, NULL) )
789 * If the time the machine password has changed
790 * was less than about 15 minutes then we need to contact
791 * the PDC only, as we cannot be sure domain replication
792 * has yet taken place. Bug found by Gerald (way to go
796 if ( now - last_change_time < SAM_SYNC_WINDOW )
803 /*******************************************************************************
804 Store a complete AFS keyfile into secrets.tdb.
805 *******************************************************************************/
807 BOOL secrets_store_afs_keyfile(const char *cell, const struct afs_keyfile *keyfile)
811 if ((cell == NULL) || (keyfile == NULL))
814 if (ntohl(keyfile->nkeys) > SECRETS_AFS_MAXKEYS)
817 slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_AFS_KEYFILE, cell);
818 return secrets_store(key, keyfile, sizeof(struct afs_keyfile));
821 /*******************************************************************************
822 Fetch the current (highest) AFS key from secrets.tdb
823 *******************************************************************************/
824 BOOL secrets_fetch_afs_key(const char *cell, struct afs_key *result)
827 struct afs_keyfile *keyfile;
831 slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_AFS_KEYFILE, cell);
833 keyfile = (struct afs_keyfile *)secrets_fetch(key, &size);
838 if (size != sizeof(struct afs_keyfile)) {
843 i = ntohl(keyfile->nkeys);
845 if (i > SECRETS_AFS_MAXKEYS) {
850 *result = keyfile->entry[i-1];
852 result->kvno = ntohl(result->kvno);
857 /******************************************************************************
858 When kerberos is not available, choose between anonymous or
859 authenticated connections.
861 We need to use an authenticated connection if DCs have the
862 RestrictAnonymous registry entry set > 0, or the "Additional
863 restrictions for anonymous connections" set in the win2k Local
866 Caller to free() result in domain, username, password
867 *******************************************************************************/
868 void secrets_fetch_ipc_userpass(char **username, char **domain, char **password)
870 *username = secrets_fetch(SECRETS_AUTH_USER, NULL);
871 *domain = secrets_fetch(SECRETS_AUTH_DOMAIN, NULL);
872 *password = secrets_fetch(SECRETS_AUTH_PASSWORD, NULL);
874 if (*username && **username) {
876 if (!*domain || !**domain)
877 *domain = smb_xstrdup(lp_workgroup());
879 if (!*password || !**password)
880 *password = smb_xstrdup("");
882 DEBUG(3, ("IPC$ connections done by user %s\\%s\n",
883 *domain, *username));
886 DEBUG(3, ("IPC$ connections done anonymously\n"));
887 *username = smb_xstrdup("");
888 *domain = smb_xstrdup("");
889 *password = smb_xstrdup("");