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 kbuf.dptr = (char *)key;
92 kbuf.dsize = strlen(key);
93 dbuf = tdb_fetch(tdb, kbuf);
99 /* store a secrets entry
101 BOOL secrets_store(const char *key, const void *data, size_t size)
107 kbuf.dptr = (char *)key;
108 kbuf.dsize = strlen(key);
109 dbuf.dptr = (char *)data;
111 return tdb_store(tdb, kbuf, dbuf, TDB_REPLACE) == 0;
115 /* delete a secets database entry
117 BOOL secrets_delete(const char *key)
123 kbuf.dptr = (char *)key;
124 kbuf.dsize = strlen(key);
125 return tdb_delete(tdb, kbuf) == 0;
128 BOOL secrets_store_domain_sid(const char *domain, const DOM_SID *sid)
133 slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_DOMAIN_SID, domain);
135 ret = secrets_store(key, sid, sizeof(DOM_SID));
137 /* Force a re-query, in case we modified our domain */
139 reset_global_sam_sid();
143 BOOL secrets_fetch_domain_sid(const char *domain, DOM_SID *sid)
149 slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_DOMAIN_SID, domain);
151 dyn_sid = (DOM_SID *)secrets_fetch(key, &size);
156 if (size != sizeof(DOM_SID))
167 BOOL secrets_store_domain_guid(const char *domain, struct uuid *guid)
171 slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_DOMAIN_GUID, domain);
173 return secrets_store(key, guid, sizeof(struct uuid));
176 BOOL secrets_fetch_domain_guid(const char *domain, struct uuid *guid)
178 struct uuid *dyn_guid;
181 struct uuid new_guid;
183 slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_DOMAIN_GUID, domain);
185 dyn_guid = (struct uuid *)secrets_fetch(key, &size);
187 if ((!dyn_guid) && (lp_server_role() == ROLE_DOMAIN_PDC)) {
188 smb_uuid_generate_random(&new_guid);
189 if (!secrets_store_domain_guid(domain, &new_guid))
191 dyn_guid = (struct uuid *)secrets_fetch(key, &size);
192 if (dyn_guid == NULL)
196 if (size != sizeof(struct uuid))
198 DEBUG(1,("UUID size %d is wrong!\n", (int)size));
209 * Form a key for fetching the machine trust account password
211 * @param domain domain name
213 * @return stored password's key
215 const char *trust_keystr(const char *domain)
217 static fstring keystr;
219 slprintf(keystr,sizeof(keystr)-1,"%s/%s",
220 SECRETS_MACHINE_ACCT_PASS, domain);
227 * Form a key for fetching a trusted domain password
229 * @param domain trusted domain name
231 * @return stored password's key
233 static char *trustdom_keystr(const char *domain)
235 static pstring keystr;
237 pstr_sprintf(keystr, "%s/%s", SECRETS_DOMTRUST_ACCT_PASS, domain);
243 /************************************************************************
244 Lock the trust password entry.
245 ************************************************************************/
247 BOOL secrets_lock_trust_account_password(const char *domain, BOOL dolock)
253 return (tdb_lock_bystring(tdb, trust_keystr(domain),0) == 0);
255 tdb_unlock_bystring(tdb, trust_keystr(domain));
259 /************************************************************************
260 Routine to get the default secure channel type for trust accounts
261 ************************************************************************/
263 uint32 get_default_sec_channel(void)
265 if (lp_server_role() == ROLE_DOMAIN_BDC ||
266 lp_server_role() == ROLE_DOMAIN_PDC) {
269 return SEC_CHAN_WKSTA;
273 /************************************************************************
274 Routine to get the trust account password for a domain.
275 The user of this function must have locked the trust password file using
276 the above secrets_lock_trust_account_password().
277 ************************************************************************/
279 BOOL secrets_fetch_trust_account_password(const char *domain, uint8 ret_pwd[16],
280 time_t *pass_last_set_time,
283 struct machine_acct_pass *pass;
287 plaintext = secrets_fetch_machine_password(domain, pass_last_set_time,
290 DEBUG(4,("Using cleartext machine password\n"));
291 E_md4hash(plaintext, ret_pwd);
292 SAFE_FREE(plaintext);
296 if (!(pass = secrets_fetch(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) *pass_last_set_time = pass->mod_time;
307 memcpy(ret_pwd, pass->hash, 16);
311 *channel = get_default_sec_channel();
316 /************************************************************************
317 Routine to get account password to trusted domain
318 ************************************************************************/
320 BOOL secrets_fetch_trusted_domain_password(const char *domain, char** pwd,
321 DOM_SID *sid, time_t *pass_last_set_time)
323 struct trusted_dom_pass pass;
326 /* unpacking structures */
332 /* fetching trusted domain password structure */
333 if (!(pass_buf = secrets_fetch(trustdom_keystr(domain), &size))) {
334 DEBUG(5, ("secrets_fetch failed!\n"));
338 /* unpack trusted domain password */
339 pass_len = tdb_trusted_dom_pass_unpack(pass_buf, size, &pass);
342 if (pass_len != size) {
343 DEBUG(5, ("Invalid secrets size. Unpacked data doesn't match trusted_dom_pass structure.\n"));
347 /* the trust's password */
349 *pwd = strdup(pass.pass);
355 /* last change time */
356 if (pass_last_set_time) *pass_last_set_time = pass.mod_time;
359 sid_copy(sid, &pass.domain_sid);
364 /************************************************************************
365 Routine to set the trust account password for a domain.
366 ************************************************************************/
368 BOOL secrets_store_trust_account_password(const char *domain, uint8 new_pwd[16])
370 struct machine_acct_pass pass;
372 pass.mod_time = time(NULL);
373 memcpy(pass.hash, new_pwd, 16);
375 return secrets_store(trust_keystr(domain), (void *)&pass, sizeof(pass));
379 * Routine to store the password for trusted domain
381 * @param domain remote domain name
382 * @param pwd plain text password of trust relationship
383 * @param sid remote domain sid
385 * @return true if succeeded
388 BOOL secrets_store_trusted_domain_password(const char* domain, smb_ucs2_t *uni_dom_name,
389 size_t uni_name_len, const char* pwd,
392 /* packing structures */
395 int pass_buf_len = sizeof(pass_buf);
397 struct trusted_dom_pass pass;
400 /* unicode domain name and its length */
404 strncpy_w(pass.uni_name, uni_dom_name, sizeof(pass.uni_name) - 1);
405 pass.uni_name_len = uni_name_len;
407 /* last change time */
408 pass.mod_time = time(NULL);
410 /* password of the trust */
411 pass.pass_len = strlen(pwd);
412 fstrcpy(pass.pass, pwd);
415 sid_copy(&pass.domain_sid, &sid);
417 pass_len = tdb_trusted_dom_pass_pack(pass_buf, pass_buf_len, &pass);
419 return secrets_store(trustdom_keystr(domain), (void *)&pass_buf, pass_len);
422 /************************************************************************
423 Routine to set the plaintext machine account password for a realm
424 the password is assumed to be a null terminated ascii string
425 ************************************************************************/
427 BOOL secrets_store_machine_password(const char *pass, const char *domain, uint32 sec_channel)
431 uint32 last_change_time;
432 uint32 sec_channel_type;
434 asprintf(&key, "%s/%s", SECRETS_MACHINE_PASSWORD, domain);
439 ret = secrets_store(key, pass, strlen(pass)+1);
445 asprintf(&key, "%s/%s", SECRETS_MACHINE_LAST_CHANGE_TIME, domain);
450 SIVAL(&last_change_time, 0, time(NULL));
451 ret = secrets_store(key, &last_change_time, sizeof(last_change_time));
454 asprintf(&key, "%s/%s", SECRETS_MACHINE_SEC_CHANNEL_TYPE, domain);
459 SIVAL(&sec_channel_type, 0, sec_channel);
460 ret = secrets_store(key, &sec_channel_type, sizeof(sec_channel_type));
467 /************************************************************************
468 Routine to fetch the plaintext machine account password for a realm
469 the password is assumed to be a null terminated ascii string
470 ************************************************************************/
471 char *secrets_fetch_machine_password(const char *domain,
472 time_t *pass_last_set_time,
477 asprintf(&key, "%s/%s", SECRETS_MACHINE_PASSWORD, domain);
479 ret = (char *)secrets_fetch(key, NULL);
482 if (pass_last_set_time) {
484 uint32 *last_set_time;
485 asprintf(&key, "%s/%s", SECRETS_MACHINE_LAST_CHANGE_TIME, domain);
487 last_set_time = secrets_fetch(key, &size);
489 *pass_last_set_time = IVAL(last_set_time,0);
490 SAFE_FREE(last_set_time);
492 *pass_last_set_time = 0;
499 uint32 *channel_type;
500 asprintf(&key, "%s/%s", SECRETS_MACHINE_SEC_CHANNEL_TYPE, domain);
502 channel_type = secrets_fetch(key, &size);
504 *channel = IVAL(channel_type,0);
505 SAFE_FREE(channel_type);
507 *channel = get_default_sec_channel();
517 /************************************************************************
518 Routine to delete the machine trust account password file for a domain.
519 ************************************************************************/
521 BOOL trust_password_delete(const char *domain)
523 return secrets_delete(trust_keystr(domain));
526 /************************************************************************
527 Routine to delete the password for trusted domain
528 ************************************************************************/
530 BOOL trusted_domain_password_delete(const char *domain)
532 return secrets_delete(trustdom_keystr(domain));
536 BOOL secrets_store_ldap_pw(const char* dn, char* pw)
541 if (asprintf(&key, "%s/%s", SECRETS_LDAP_BIND_PW, dn) < 0) {
542 DEBUG(0, ("secrets_store_ldap_pw: asprintf failed!\n"));
546 ret = secrets_store(key, pw, strlen(pw)+1);
554 * Get trusted domains info from secrets.tdb.
556 * The linked list is allocated on the supplied talloc context, caller gets to destroy
559 * @param ctx Allocation context
560 * @param enum_ctx Starting index, eg. we can start fetching at third
561 * or sixth trusted domain entry. Zero is the first index.
562 * Value it is set to is the enum context for the next enumeration.
563 * @param num_domains Number of domain entries to fetch at one call
564 * @param domains Pointer to array of trusted domain structs to be filled up
566 * @return nt status code of rpc response
569 NTSTATUS secrets_get_trusted_domains(TALLOC_CTX* ctx, int* enum_ctx, unsigned int max_num_domains,
570 int *num_domains, TRUSTDOM ***domains)
572 TDB_LIST_NODE *keys, *k;
573 TRUSTDOM *dom = NULL;
575 unsigned int start_idx;
577 size_t size, packed_size = 0;
580 struct trusted_dom_pass *pass = talloc_zero(ctx, sizeof(struct trusted_dom_pass));
583 if (!secrets_init()) return NT_STATUS_ACCESS_DENIED;
586 DEBUG(0, ("talloc_zero failed!\n"));
587 return NT_STATUS_NO_MEMORY;
591 start_idx = *enum_ctx;
593 /* generate searching pattern */
594 if (!(pattern = talloc_asprintf(ctx, "%s/*", SECRETS_DOMTRUST_ACCT_PASS))) {
595 DEBUG(0, ("secrets_get_trusted_domains: talloc_asprintf() failed!\n"));
596 return NT_STATUS_NO_MEMORY;
599 DEBUG(5, ("secrets_get_trusted_domains: looking for %d domains, starting at index %d\n",
600 max_num_domains, *enum_ctx));
602 *domains = talloc_zero(ctx, sizeof(**domains)*max_num_domains);
604 /* fetching trusted domains' data and collecting them in a list */
605 keys = tdb_search_keys(tdb, pattern);
608 * if there's no keys returned ie. no trusted domain,
609 * return "no more entries" code
611 status = NT_STATUS_NO_MORE_ENTRIES;
613 /* searching for keys in secrets db -- way to go ... */
614 for (k = keys; k; k = k->next) {
617 /* important: ensure null-termination of the key string */
618 secrets_key = strndup(k->node_key.dptr, k->node_key.dsize);
620 DEBUG(0, ("strndup failed!\n"));
621 return NT_STATUS_NO_MEMORY;
624 packed_pass = secrets_fetch(secrets_key, &size);
625 packed_size = tdb_trusted_dom_pass_unpack(packed_pass, size, pass);
626 /* packed representation isn't needed anymore */
627 SAFE_FREE(packed_pass);
629 if (size != packed_size) {
630 DEBUG(2, ("Secrets record %s is invalid!\n", secrets_key));
634 pull_ucs2_fstring(dom_name, pass->uni_name);
635 DEBUG(18, ("Fetched secret record num %d.\nDomain name: %s, SID: %s\n",
636 idx, dom_name, sid_string_static(&pass->domain_sid)));
638 SAFE_FREE(secrets_key);
640 if (idx >= start_idx && idx < start_idx + max_num_domains) {
641 dom = talloc_zero(ctx, sizeof(*dom));
643 /* free returned tdb record */
644 return NT_STATUS_NO_MEMORY;
647 /* copy domain sid */
648 SMB_ASSERT(sizeof(dom->sid) == sizeof(pass->domain_sid));
649 memcpy(&(dom->sid), &(pass->domain_sid), sizeof(dom->sid));
651 /* copy unicode domain name */
652 dom->name = talloc_strdup_w(ctx, pass->uni_name);
654 (*domains)[idx - start_idx] = dom;
656 DEBUG(18, ("Secret record is in required range.\n \
657 start_idx = %d, max_num_domains = %d. Added to returned array.\n",
658 start_idx, max_num_domains));
663 /* set proper status code to return */
665 /* there are yet some entries to enumerate */
666 status = STATUS_MORE_ENTRIES;
668 /* this is the last entry in the whole enumeration */
669 status = NT_STATUS_OK;
672 DEBUG(18, ("Secret is outside the required range.\n \
673 start_idx = %d, max_num_domains = %d. Not added to returned array\n",
674 start_idx, max_num_domains));
680 DEBUG(5, ("secrets_get_trusted_domains: got %d domains\n", *num_domains));
682 /* free the results of searching the keys */
683 tdb_search_list_free(keys);
688 /*******************************************************************************
689 Lock the secrets tdb based on a string - this is used as a primitive form of mutex
690 between smbd instances.
691 *******************************************************************************/
693 BOOL secrets_named_mutex(const char *name, unsigned int timeout)
700 ret = tdb_lock_bystring(tdb, name, timeout);
702 DEBUG(10,("secrets_named_mutex: got mutex for %s\n", name ));
707 /*******************************************************************************
708 Unlock a named mutex.
709 *******************************************************************************/
711 void secrets_named_mutex_release(const char *name)
713 tdb_unlock_bystring(tdb, name);
714 DEBUG(10,("secrets_named_mutex: released mutex for %s\n", name ));
717 /*********************************************************
718 Check to see if we must talk to the PDC to avoid sam
720 ********************************************************/
722 BOOL must_use_pdc( const char *domain )
724 time_t now = time(NULL);
725 time_t last_change_time;
726 unsigned char passwd[16];
728 if ( !secrets_fetch_trust_account_password(domain, passwd, &last_change_time, NULL) )
732 * If the time the machine password has changed
733 * was less than about 15 minutes then we need to contact
734 * the PDC only, as we cannot be sure domain replication
735 * has yet taken place. Bug found by Gerald (way to go
739 if ( now - last_change_time < SAM_SYNC_WINDOW )
746 /*******************************************************************************
747 Store a complete AFS keyfile into secrets.tdb.
748 *******************************************************************************/
750 BOOL secrets_store_afs_keyfile(const char *cell, const struct afs_keyfile *keyfile)
754 if ((cell == NULL) || (keyfile == NULL))
757 if (ntohl(keyfile->nkeys) > SECRETS_AFS_MAXKEYS)
760 slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_AFS_KEYFILE, cell);
761 return secrets_store(key, keyfile, sizeof(struct afs_keyfile));
764 /*******************************************************************************
765 Fetch the current (highest) AFS key from secrets.tdb
766 *******************************************************************************/
767 BOOL secrets_fetch_afs_key(const char *cell, struct afs_key *result)
770 struct afs_keyfile *keyfile;
774 slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_AFS_KEYFILE, cell);
776 keyfile = (struct afs_keyfile *)secrets_fetch(key, &size);
781 if (size != sizeof(struct afs_keyfile)) {
786 i = ntohl(keyfile->nkeys);
788 if (i > SECRETS_AFS_MAXKEYS) {
793 *result = keyfile->entry[i-1];
795 result->kvno = ntohl(result->kvno);
800 /******************************************************************************
801 When kerberos is not available, choose between anonymous or
802 authenticated connections.
804 We need to use an authenticated connection if DCs have the
805 RestrictAnonymous registry entry set > 0, or the "Additional
806 restrictions for anonymous connections" set in the win2k Local
809 Caller to free() result in domain, username, password
810 *******************************************************************************/
811 void secrets_fetch_ipc_userpass(char **username, char **domain, char **password)
813 *username = secrets_fetch(SECRETS_AUTH_USER, NULL);
814 *domain = secrets_fetch(SECRETS_AUTH_DOMAIN, NULL);
815 *password = secrets_fetch(SECRETS_AUTH_PASSWORD, NULL);
817 if (*username && **username) {
819 if (!*domain || !**domain)
820 *domain = smb_xstrdup(lp_workgroup());
822 if (!*password || !**password)
823 *password = smb_xstrdup("");
825 DEBUG(3, ("IPC$ connections done by user %s\\%s\n",
826 *domain, *username));
829 DEBUG(3, ("IPC$ connections done anonymously\n"));
830 *username = smb_xstrdup("");
831 *domain = smb_xstrdup("");
832 *password = smb_xstrdup("");