2 Unix SMB/CIFS implementation.
3 Copyright (C) Andrew Tridgell 1992-2001
4 Copyright (C) Andrew Bartlett 2002
5 Copyright (C) Rafal Szczesniak 2002
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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 /* open up the secrets database */
33 BOOL secrets_init(void)
40 pstrcpy(fname, lp_private_dir());
41 pstrcat(fname,"/secrets.tdb");
43 tdb = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
46 DEBUG(0,("Failed to open %s\n", fname));
52 /* read a entry from the secrets database - the caller must free the result
53 if size is non-null then the size of the entry is put in there
55 void *secrets_fetch(const char *key, size_t *size)
61 kbuf.dptr = strdup(key);
62 kbuf.dsize = strlen(key);
63 dbuf = tdb_fetch(tdb, kbuf);
70 /* store a secrets entry
72 BOOL secrets_store(const char *key, const void *data, size_t size)
80 kbuf.dptr = strdup(key);
81 kbuf.dsize = strlen(key);
82 dbuf.dptr = memdup(data, size);
85 ret = tdb_store(tdb, kbuf, dbuf, TDB_REPLACE);
94 /* delete a secets database entry
96 BOOL secrets_delete(const char *key)
104 kbuf.dptr = strdup(key);
105 kbuf.dsize = strlen(key);
106 ret = tdb_delete(tdb, kbuf);
111 BOOL secrets_store_domain_sid(const char *domain, const DOM_SID *sid)
115 slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_DOMAIN_SID, domain);
117 return secrets_store(key, sid, sizeof(DOM_SID));
120 BOOL secrets_fetch_domain_sid(const char *domain, DOM_SID *sid)
126 slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_DOMAIN_SID, domain);
128 dyn_sid = (DOM_SID *)secrets_fetch(key, &size);
133 if (size != sizeof(DOM_SID))
144 BOOL secrets_store_domain_guid(const char *domain, struct GUID *guid)
151 mem_ctx = talloc_init("secrets_store_domain_guid");
156 s = GUID_string(mem_ctx, guid);
158 talloc_destroy(mem_ctx);
163 slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_DOMAIN_GUID, domain);
165 ret = secrets_store(key, s, strlen(s)+1);
167 talloc_destroy(mem_ctx);
171 BOOL secrets_fetch_domain_guid(const char *domain, struct GUID *guid)
176 struct GUID new_guid;
179 slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_DOMAIN_GUID, domain);
181 dyn_guid = secrets_fetch(key, &size);
183 DEBUG(6,("key is %s, size is %d\n", key, (int)size));
185 if ((NULL == dyn_guid) && (ROLE_DOMAIN_PDC == lp_server_role())) {
186 uuid_generate_random(&new_guid);
187 if (!secrets_store_domain_guid(domain, &new_guid))
189 dyn_guid = secrets_fetch(key, &size);
190 if (dyn_guid == NULL)
194 status = GUID_from_string(dyn_guid, guid);
197 if (!NT_STATUS_IS_OK(status)) {
205 * Form a key for fetching the machine trust account password
207 * @param domain domain name
209 * @return stored password's key
211 const char *trust_keystr(const char *domain)
213 static fstring keystr;
215 slprintf(keystr,sizeof(keystr)-1,"%s/%s",
216 SECRETS_MACHINE_ACCT_PASS, domain);
223 * Form a key for fetching a trusted domain password
225 * @param domain trusted domain name
227 * @return stored password's key
229 char *trustdom_keystr(const char *domain)
233 asprintf(&keystr, "%s/%s", SECRETS_DOMTRUST_ACCT_PASS, domain);
239 /************************************************************************
240 Lock the trust password entry.
241 ************************************************************************/
243 BOOL secrets_lock_trust_account_password(const char *domain, BOOL dolock)
249 return (tdb_lock_bystring(tdb, trust_keystr(domain),0) == 0);
251 tdb_unlock_bystring(tdb, trust_keystr(domain));
255 /************************************************************************
256 Routine to get the trust account password for a domain.
257 The user of this function must have locked the trust password file using
259 ************************************************************************/
261 BOOL secrets_fetch_trust_account_password(const char *domain, uint8 ret_pwd[16],
262 time_t *pass_last_set_time)
264 struct machine_acct_pass *pass;
268 plaintext = secrets_fetch_machine_password();
270 /* we have an ADS password - use that */
271 DEBUG(4,("Using ADS machine password\n"));
272 E_md4hash(plaintext, ret_pwd);
273 SAFE_FREE(plaintext);
274 pass_last_set_time = 0;
278 if (!(pass = secrets_fetch(trust_keystr(domain), &size))) {
279 DEBUG(5, ("secrets_fetch failed!\n"));
283 if (size != sizeof(*pass)) {
284 DEBUG(0, ("secrets were of incorrect size!\n"));
288 if (pass_last_set_time) *pass_last_set_time = pass->mod_time;
289 memcpy(ret_pwd, pass->hash, 16);
294 /************************************************************************
295 Routine to get account password to trusted domain
296 ************************************************************************/
298 BOOL secrets_fetch_trusted_domain_password(const char *domain, char** pwd,
299 DOM_SID *sid, time_t *pass_last_set_time)
301 struct trusted_dom_pass *pass;
304 /* fetching trusted domain password structure */
305 if (!(pass = secrets_fetch(trustdom_keystr(domain), &size))) {
306 DEBUG(5, ("secrets_fetch failed!\n"));
310 if (size != sizeof(*pass)) {
311 DEBUG(0, ("secrets were of incorrect size!\n"));
315 /* the trust's password */
317 *pwd = strdup(pass->pass);
323 /* last change time */
324 if (pass_last_set_time) *pass_last_set_time = pass->mod_time;
327 memcpy(&sid, &(pass->domain_sid), sizeof(sid));
334 /************************************************************************
335 Routine to set the trust account password for a domain.
336 ************************************************************************/
338 BOOL secrets_store_trust_account_password(const char *domain, uint8 new_pwd[16])
340 struct machine_acct_pass pass;
342 pass.mod_time = time(NULL);
343 memcpy(pass.hash, new_pwd, 16);
345 return secrets_store(trust_keystr(domain), (void *)&pass, sizeof(pass));
349 * Routine to set the password for trusted domain
351 * @param domain remote domain name
352 * @param pwd plain text password of trust relationship
353 * @param sid remote domain sid
355 * @return true if succeeded
358 BOOL secrets_store_trusted_domain_password(const char* domain, smb_ucs2_t *uni_dom_name,
359 size_t uni_name_len, const char* pwd,
362 struct trusted_dom_pass pass;
365 /* unicode domain name and its length */
369 strncpy_w(pass.uni_name, uni_dom_name, sizeof(pass.uni_name) - 1);
370 pass.uni_name_len = uni_name_len;
372 /* last change time */
373 pass.mod_time = time(NULL);
375 /* password of the trust */
376 pass.pass_len = strlen(pwd);
377 fstrcpy(pass.pass, pwd);
380 memcpy(&(pass.domain_sid), &sid, sizeof(sid));
382 return secrets_store(trustdom_keystr(domain), (void *)&pass, sizeof(pass));
385 /************************************************************************
386 Routine to set the plaintext machine account password for a realm
387 the password is assumed to be a null terminated ascii string
388 ************************************************************************/
390 BOOL secrets_store_machine_password(const char *pass)
394 asprintf(&key, "%s/%s", SECRETS_MACHINE_PASSWORD, lp_workgroup());
396 ret = secrets_store(key, pass, strlen(pass)+1);
402 /************************************************************************
403 Routine to fetch the plaintext machine account password for a realm
404 the password is assumed to be a null terminated ascii string
405 ************************************************************************/
406 char *secrets_fetch_machine_password(void)
410 asprintf(&key, "%s/%s", SECRETS_MACHINE_PASSWORD, lp_workgroup());
412 ret = (char *)secrets_fetch(key, NULL);
419 /************************************************************************
420 Routine to delete the machine trust account password file for a domain.
421 ************************************************************************/
423 BOOL trust_password_delete(const char *domain)
425 return secrets_delete(trust_keystr(domain));
428 /************************************************************************
429 Routine to delete the password for trusted domain
430 ************************************************************************/
432 BOOL trusted_domain_password_delete(const char *domain)
434 return secrets_delete(trustdom_keystr(domain));
438 BOOL secrets_store_ldap_pw(const char* dn, char* pw)
443 if (asprintf(&key, "%s/%s", SECRETS_LDAP_BIND_PW, dn) < 0) {
444 DEBUG(0, ("secrets_store_ldap_pw: asprintf failed!\n"));
448 ret = secrets_store(key, pw, strlen(pw)+1);
456 * Get trusted domains info from secrets.tdb.
458 * The linked list is allocated on the supplied talloc context, caller gets to destroy
461 * @param ctx Allocation context
462 * @param enum_ctx Starting index, eg. we can start fetching at third
463 * or sixth trusted domain entry. Zero is the first index.
464 * Value it is set to is the enum context for the next enumeration.
465 * @param num_domains Number of domain entries to fetch at one call
466 * @param domains Pointer to array of trusted domain structs to be filled up
468 * @return nt status code of rpc response
471 NTSTATUS secrets_get_trusted_domains(TALLOC_CTX* ctx, int* enum_ctx, unsigned int max_num_domains, int *num_domains, TRUSTDOM ***domains)
473 TDB_LIST_NODE *keys, *k;
474 TRUSTDOM *dom = NULL;
476 unsigned int start_idx;
480 struct trusted_dom_pass *pass;
483 if (!secrets_init()) return NT_STATUS_ACCESS_DENIED;
486 start_idx = *enum_ctx;
488 /* generate searching pattern */
489 if (!(pattern = talloc_asprintf(ctx, "%s/*", SECRETS_DOMTRUST_ACCT_PASS))) {
490 DEBUG(0, ("secrets_get_trusted_domains: talloc_asprintf() failed!\n"));
491 return NT_STATUS_NO_MEMORY;
494 DEBUG(5, ("secrets_get_trusted_domains: looking for %d domains, starting at index %d\n",
495 max_num_domains, *enum_ctx));
497 *domains = talloc_zero(ctx, sizeof(**domains)*max_num_domains);
499 /* fetching trusted domains' data and collecting them in a list */
500 keys = tdb_search_keys(tdb, pattern);
503 * if there's no keys returned ie. no trusted domain,
504 * return "no more entries" code
506 status = NT_STATUS_NO_MORE_ENTRIES;
508 /* searching for keys in sectrets db -- way to go ... */
509 for (k = keys; k; k = k->next) {
512 /* important: ensure null-termination of the key string */
513 secrets_key = strndup(k->node_key.dptr, k->node_key.dsize);
515 DEBUG(0, ("strndup failed!\n"));
516 return NT_STATUS_NO_MEMORY;
519 pass = secrets_fetch(secrets_key, &size);
521 if (size != sizeof(*pass)) {
522 DEBUG(2, ("Secrets record %s is invalid!\n", secrets_key));
527 pull_ucs2_fstring(dom_name, pass->uni_name);
528 DEBUG(18, ("Fetched secret record num %d.\nDomain name: %s, SID: %s\n",
529 idx, dom_name, sid_string_talloc(ctx, &pass->domain_sid)));
531 SAFE_FREE(secrets_key);
533 if (idx >= start_idx && idx < start_idx + max_num_domains) {
534 dom = talloc_zero(ctx, sizeof(*dom));
536 /* free returned tdb record */
539 return NT_STATUS_NO_MEMORY;
542 /* copy domain sid */
543 SMB_ASSERT(sizeof(dom->sid) == sizeof(pass->domain_sid));
544 memcpy(&(dom->sid), &(pass->domain_sid), sizeof(dom->sid));
546 /* copy unicode domain name */
547 dom->name = talloc_strdup_w(ctx, pass->uni_name);
549 (*domains)[idx - start_idx] = dom;
551 DEBUG(18, ("Secret record is in required range.\n \
552 start_idx = %d, max_num_domains = %d. Added to returned array.\n",
553 start_idx, max_num_domains));
558 /* set proper status code to return */
560 /* there are yet some entries to enumerate */
561 status = STATUS_MORE_ENTRIES;
563 /* this is the last entry in the whole enumeration */
564 status = NT_STATUS_OK;
567 DEBUG(18, ("Secret is outside the required range.\n \
568 start_idx = %d, max_num_domains = %d. Not added to returned array\n",
569 start_idx, max_num_domains));
574 /* free returned tdb record */
578 DEBUG(5, ("secrets_get_trusted_domains: got %d domains\n", *num_domains));
580 /* free the results of searching the keys */
581 tdb_search_list_free(keys);
586 /*******************************************************************************
587 Lock the secrets tdb based on a string - this is used as a primitive form of mutex
588 between smbd instances.
589 *******************************************************************************/
591 BOOL secrets_named_mutex(const char *name, unsigned int timeout, size_t *p_ref_count)
593 size_t ref_count = *p_ref_count;
599 if (ref_count == 0) {
600 ret = tdb_lock_bystring(tdb, name, timeout);
602 DEBUG(10,("secrets_named_mutex: got mutex for %s\n", name ));
606 *p_ref_count = ++ref_count;
607 DEBUG(10,("secrets_named_mutex: ref_count for mutex %s = %u\n", name, (unsigned int)ref_count ));
612 /*******************************************************************************
613 Unlock a named mutex.
614 *******************************************************************************/
616 void secrets_named_mutex_release(const char *name, size_t *p_ref_count)
618 size_t ref_count = *p_ref_count;
620 SMB_ASSERT(ref_count != 0);
622 if (ref_count == 1) {
623 tdb_unlock_bystring(tdb, name);
624 DEBUG(10,("secrets_named_mutex: released mutex for %s\n", name ));
627 *p_ref_count = --ref_count;
628 DEBUG(10,("secrets_named_mutex_release: ref_count for mutex %s = %u\n", name, (unsigned int)ref_count ));