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)
62 kbuf.dsize = strlen(key);
63 dbuf = tdb_fetch(tdb, kbuf);
69 /* store a secrets entry
71 BOOL secrets_store(const char *key, const void *data, size_t size)
78 kbuf.dsize = strlen(key);
81 return tdb_store(tdb, kbuf, dbuf, TDB_REPLACE) == 0;
85 /* delete a secets database entry
87 BOOL secrets_delete(const char *key)
94 kbuf.dsize = strlen(key);
95 return tdb_delete(tdb, kbuf) == 0;
98 BOOL secrets_store_domain_sid(const char *domain, const DOM_SID *sid)
102 slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_DOMAIN_SID, domain);
104 return secrets_store(key, sid, sizeof(DOM_SID));
107 BOOL secrets_fetch_domain_sid(const char *domain, DOM_SID *sid)
113 slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_DOMAIN_SID, domain);
115 dyn_sid = (DOM_SID *)secrets_fetch(key, &size);
120 if (size != sizeof(DOM_SID))
131 BOOL secrets_store_domain_guid(const char *domain, GUID *guid)
135 slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_DOMAIN_GUID, domain);
137 return secrets_store(key, guid, sizeof(GUID));
140 BOOL secrets_fetch_domain_guid(const char *domain, GUID *guid)
147 slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_DOMAIN_GUID, domain);
149 dyn_guid = (GUID *)secrets_fetch(key, &size);
151 DEBUG(6,("key is %s, size is %d\n", key, (int)size));
153 if ((NULL == dyn_guid) && (ROLE_DOMAIN_PDC == lp_server_role())) {
154 uuid_generate_random(&new_guid);
155 if (!secrets_store_domain_guid(domain, &new_guid))
157 dyn_guid = (GUID *)secrets_fetch(key, &size);
158 if (dyn_guid == NULL)
162 if (size != sizeof(GUID))
174 * Form a key for fetching the machine trust account password
176 * @param domain domain name
178 * @return stored password's key
180 const char *trust_keystr(const char *domain)
182 static fstring keystr;
184 slprintf(keystr,sizeof(keystr)-1,"%s/%s",
185 SECRETS_MACHINE_ACCT_PASS, domain);
192 * Form a key for fetching a trusted domain password
194 * @param domain trusted domain name
196 * @return stored password's key
198 char *trustdom_keystr(const char *domain)
202 asprintf(&keystr, "%s/%s", SECRETS_DOMTRUST_ACCT_PASS, domain);
208 /************************************************************************
209 Lock the trust password entry.
210 ************************************************************************/
212 BOOL secrets_lock_trust_account_password(const char *domain, BOOL dolock)
218 return (tdb_lock_bystring(tdb, trust_keystr(domain),0) == 0);
220 tdb_unlock_bystring(tdb, trust_keystr(domain));
224 /************************************************************************
225 Routine to get the trust account password for a domain.
226 The user of this function must have locked the trust password file using
228 ************************************************************************/
230 BOOL secrets_fetch_trust_account_password(const char *domain, uint8 ret_pwd[16],
231 time_t *pass_last_set_time)
233 struct machine_acct_pass *pass;
237 plaintext = secrets_fetch_machine_password();
239 /* we have an ADS password - use that */
240 DEBUG(4,("Using ADS machine password\n"));
241 E_md4hash(plaintext, ret_pwd);
242 SAFE_FREE(plaintext);
243 pass_last_set_time = 0;
247 if (!(pass = secrets_fetch(trust_keystr(domain), &size))) {
248 DEBUG(5, ("secrets_fetch failed!\n"));
252 if (size != sizeof(*pass)) {
253 DEBUG(0, ("secrets were of incorrect size!\n"));
257 if (pass_last_set_time) *pass_last_set_time = pass->mod_time;
258 memcpy(ret_pwd, pass->hash, 16);
263 /************************************************************************
264 Routine to get account password to trusted domain
265 ************************************************************************/
267 BOOL secrets_fetch_trusted_domain_password(const char *domain, char** pwd,
268 DOM_SID *sid, time_t *pass_last_set_time)
270 struct trusted_dom_pass pass;
273 /* unpacking structures */
279 /* fetching trusted domain password structure */
280 if (!(pass_buf = secrets_fetch(trustdom_keystr(domain), &size))) {
281 DEBUG(5, ("secrets_fetch failed!\n"));
285 /* unpack trusted domain password */
286 pass_len = tdb_trusted_dom_pass_unpack(pass_buf, size, &pass);
287 if (pass_len != size) {
288 DEBUG(5, ("Invalid secrets size. Unpacked data doesn't match trusted_dom_pass structure.\n"));
292 /* the trust's password */
294 *pwd = strdup(pass.pass);
300 /* last change time */
301 if (pass_last_set_time) *pass_last_set_time = pass.mod_time;
304 sid_copy(sid, &pass.domain_sid);
309 /************************************************************************
310 Routine to set the trust account password for a domain.
311 ************************************************************************/
313 BOOL secrets_store_trust_account_password(const char *domain, uint8 new_pwd[16])
315 struct machine_acct_pass pass;
317 pass.mod_time = time(NULL);
318 memcpy(pass.hash, new_pwd, 16);
320 return secrets_store(trust_keystr(domain), (void *)&pass, sizeof(pass));
324 * Routine to store the password for trusted domain
326 * @param domain remote domain name
327 * @param pwd plain text password of trust relationship
328 * @param sid remote domain sid
330 * @return true if succeeded
333 BOOL secrets_store_trusted_domain_password(const char* domain, smb_ucs2_t *uni_dom_name,
334 size_t uni_name_len, const char* pwd,
337 /* packing structures */
340 int pass_buf_len = sizeof(pass_buf);
342 struct trusted_dom_pass pass;
345 /* unicode domain name and its length */
349 strncpy_w(pass.uni_name, uni_dom_name, sizeof(pass.uni_name) - 1);
350 pass.uni_name_len = uni_name_len;
352 /* last change time */
353 pass.mod_time = time(NULL);
355 /* password of the trust */
356 pass.pass_len = strlen(pwd);
357 fstrcpy(pass.pass, pwd);
360 sid_copy(&pass.domain_sid, &sid);
362 pass_len = tdb_trusted_dom_pass_pack(pass_buf, pass_buf_len, &pass);
364 return secrets_store(trustdom_keystr(domain), (void *)&pass_buf, pass_len);
367 /************************************************************************
368 Routine to set the plaintext machine account password for a realm
369 the password is assumed to be a null terminated ascii string
370 ************************************************************************/
372 BOOL secrets_store_machine_password(const char *pass)
376 asprintf(&key, "%s/%s", SECRETS_MACHINE_PASSWORD, lp_workgroup());
378 ret = secrets_store(key, pass, strlen(pass)+1);
384 /************************************************************************
385 Routine to fetch the plaintext machine account password for a realm
386 the password is assumed to be a null terminated ascii string
387 ************************************************************************/
388 char *secrets_fetch_machine_password(void)
392 asprintf(&key, "%s/%s", SECRETS_MACHINE_PASSWORD, lp_workgroup());
394 ret = (char *)secrets_fetch(key, NULL);
401 /************************************************************************
402 Routine to delete the machine trust account password file for a domain.
403 ************************************************************************/
405 BOOL trust_password_delete(const char *domain)
407 return secrets_delete(trust_keystr(domain));
410 /************************************************************************
411 Routine to delete the password for trusted domain
412 ************************************************************************/
414 BOOL trusted_domain_password_delete(const char *domain)
416 return secrets_delete(trustdom_keystr(domain));
420 /*******************************************************************
421 Reset the 'done' variables so after a client process is created
422 from a fork call these calls will be re-done. This should be
423 expanded if more variables need reseting.
424 ******************************************************************/
426 void reset_globals_after_fork(void)
433 * Increment the global seed value to ensure every smbd starts
434 * with a new random seed.
438 uint32 initial_val = sys_getpid();
439 tdb_change_int32_atomic(tdb, "INFO/random_seed", (int *)&initial_val, 1);
440 set_rand_reseed_data((unsigned char *)&initial_val, sizeof(initial_val));
444 * Re-seed the random crypto generator, so all smbd's
445 * started from the same parent won't generate the same
448 generate_random_buffer( &dummy, 1, True);
451 BOOL secrets_store_ldap_pw(const char* dn, char* pw)
456 if (asprintf(&key, "%s/%s", SECRETS_LDAP_BIND_PW, dn) < 0) {
457 DEBUG(0, ("secrets_store_ldap_pw: asprintf failed!\n"));
461 ret = secrets_store(key, pw, strlen(pw)+1);
469 * Get trusted domains info from secrets.tdb.
471 * The linked list is allocated on the supplied talloc context, caller gets to destroy
474 * @param ctx Allocation context
475 * @param enum_ctx Starting index, eg. we can start fetching at third
476 * or sixth trusted domain entry. Zero is the first index.
477 * Value it is set to is the enum context for the next enumeration.
478 * @param num_domains Number of domain entries to fetch at one call
479 * @param domains Pointer to array of trusted domain structs to be filled up
481 * @return nt status code of rpc response
484 NTSTATUS secrets_get_trusted_domains(TALLOC_CTX* ctx, int* enum_ctx, unsigned int max_num_domains, int *num_domains, TRUSTDOM ***domains)
486 TDB_LIST_NODE *keys, *k;
487 TRUSTDOM *dom = NULL;
489 unsigned int start_idx;
491 size_t size, packed_size = 0;
494 struct trusted_dom_pass *pass = talloc_zero(ctx, sizeof(struct trusted_dom_pass));
497 if (!secrets_init()) return NT_STATUS_ACCESS_DENIED;
500 start_idx = *enum_ctx;
502 /* generate searching pattern */
503 if (!(pattern = talloc_asprintf(ctx, "%s/*", SECRETS_DOMTRUST_ACCT_PASS))) {
504 DEBUG(0, ("secrets_get_trusted_domains: talloc_asprintf() failed!\n"));
505 return NT_STATUS_NO_MEMORY;
508 DEBUG(5, ("secrets_get_trusted_domains: looking for %d domains, starting at index %d\n",
509 max_num_domains, *enum_ctx));
511 *domains = talloc_zero(ctx, sizeof(**domains)*max_num_domains);
513 /* fetching trusted domains' data and collecting them in a list */
514 keys = tdb_search_keys(tdb, pattern);
517 * if there's no keys returned ie. no trusted domain,
518 * return "no more entries" code
520 status = NT_STATUS_NO_MORE_ENTRIES;
522 /* searching for keys in secrets db -- way to go ... */
523 for (k = keys; k; k = k->next) {
526 /* important: ensure null-termination of the key string */
527 secrets_key = strndup(k->node_key.dptr, k->node_key.dsize);
529 DEBUG(0, ("strndup failed!\n"));
530 return NT_STATUS_NO_MEMORY;
533 packed_pass = secrets_fetch(secrets_key, &size);
534 packed_size = tdb_trusted_dom_pass_unpack(packed_pass, size, pass);
536 if (size != packed_size) {
537 DEBUG(2, ("Secrets record %s is invalid!\n", secrets_key));
538 if (size) SAFE_FREE(packed_pass);
543 /* packed representation isn't needed anymore */
544 SAFE_FREE(packed_pass);
546 pull_ucs2_fstring(dom_name, pass->uni_name);
547 DEBUG(18, ("Fetched secret record num %d.\nDomain name: %s, SID: %s\n",
548 idx, dom_name, sid_string_static(&pass->domain_sid)));
550 SAFE_FREE(secrets_key);
552 if (idx >= start_idx && idx < start_idx + max_num_domains) {
553 dom = talloc_zero(ctx, sizeof(*dom));
555 /* free returned tdb record */
556 return NT_STATUS_NO_MEMORY;
559 /* copy domain sid */
560 SMB_ASSERT(sizeof(dom->sid) == sizeof(pass->domain_sid));
561 memcpy(&(dom->sid), &(pass->domain_sid), sizeof(dom->sid));
563 /* copy unicode domain name */
564 dom->name = talloc_strdup_w(ctx, pass->uni_name);
566 (*domains)[idx - start_idx] = dom;
568 DEBUG(18, ("Secret record is in required range.\n \
569 start_idx = %d, max_num_domains = %d. Added to returned array.\n",
570 start_idx, max_num_domains));
575 /* set proper status code to return */
577 /* there are yet some entries to enumerate */
578 status = STATUS_MORE_ENTRIES;
580 /* this is the last entry in the whole enumeration */
581 status = NT_STATUS_OK;
584 DEBUG(18, ("Secret is outside the required range.\n \
585 start_idx = %d, max_num_domains = %d. Not added to returned array\n",
586 start_idx, max_num_domains));
592 DEBUG(5, ("secrets_get_trusted_domains: got %d domains\n", *num_domains));
594 /* free the results of searching the keys */
595 tdb_search_list_free(keys);
600 /*******************************************************************************
601 Lock the secrets tdb based on a string - this is used as a primitive form of mutex
602 between smbd instances.
603 *******************************************************************************/
605 BOOL secrets_named_mutex(const char *name, unsigned int timeout)
612 ret = tdb_lock_bystring(tdb, name, timeout);
614 DEBUG(10,("secrets_named_mutex: got mutex for %s\n", name ));
619 /*******************************************************************************
620 Unlock a named mutex.
621 *******************************************************************************/
623 void secrets_named_mutex_release(const char *name)
625 tdb_unlock_bystring(tdb, name);
626 DEBUG(10,("secrets_named_mutex: released mutex for %s\n", name ));
629 /*********************************************************
630 Check to see if we must talk to the PDC to avoid sam
632 ********************************************************/
634 BOOL must_use_pdc( const char *domain )
636 time_t now = time(NULL);
637 time_t last_change_time;
638 unsigned char passwd[16];
640 if ( !secrets_fetch_trust_account_password(domain, passwd, &last_change_time) )
644 * If the time the machine password has changed
645 * was less than about 15 minutes then we need to contact
646 * the PDC only, as we cannot be sure domain replication
647 * has yet taken place. Bug found by Gerald (way to go
651 if ( now - last_change_time < SAM_SYNC_WINDOW )