sync'ing up for 3.0alpha20 release
[Samba.git] / source / passdb / secrets.c
blob08a0e9c9acfa9a7b1d704498744e4f1f1cd0fa4b
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
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 */
25 #include "includes.h"
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_PASSDB
30 static TDB_CONTEXT *tdb;
32 /* open up the secrets database */
33 BOOL secrets_init(void)
35 pstring fname;
37 if (tdb)
38 return True;
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);
45 if (!tdb) {
46 DEBUG(0,("Failed to open %s\n", fname));
47 return False;
49 return True;
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)
57 TDB_DATA kbuf, dbuf;
58 secrets_init();
59 if (!tdb)
60 return NULL;
61 kbuf.dptr = key;
62 kbuf.dsize = strlen(key);
63 dbuf = tdb_fetch(tdb, kbuf);
64 if (size)
65 *size = dbuf.dsize;
66 return dbuf.dptr;
69 /* store a secrets entry
71 BOOL secrets_store(const char *key, const void *data, size_t size)
73 TDB_DATA kbuf, dbuf;
74 secrets_init();
75 if (!tdb)
76 return False;
77 kbuf.dptr = key;
78 kbuf.dsize = strlen(key);
79 dbuf.dptr = data;
80 dbuf.dsize = size;
81 return tdb_store(tdb, kbuf, dbuf, TDB_REPLACE) == 0;
85 /* delete a secets database entry
87 BOOL secrets_delete(const char *key)
89 TDB_DATA kbuf;
90 secrets_init();
91 if (!tdb)
92 return False;
93 kbuf.dptr = key;
94 kbuf.dsize = strlen(key);
95 return tdb_delete(tdb, kbuf) == 0;
98 BOOL secrets_store_domain_sid(char *domain, const DOM_SID *sid)
100 fstring key;
102 slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_DOMAIN_SID, domain);
103 strupper(key);
104 return secrets_store(key, sid, sizeof(DOM_SID));
107 BOOL secrets_fetch_domain_sid(char *domain, DOM_SID *sid)
109 DOM_SID *dyn_sid;
110 fstring key;
111 size_t size;
113 slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_DOMAIN_SID, domain);
114 strupper(key);
115 dyn_sid = (DOM_SID *)secrets_fetch(key, &size);
117 if (dyn_sid == NULL)
118 return False;
120 if (size != sizeof(DOM_SID))
122 SAFE_FREE(dyn_sid);
123 return False;
126 *sid = *dyn_sid;
127 SAFE_FREE(dyn_sid);
128 return True;
131 BOOL secrets_store_domain_guid(char *domain, GUID *guid)
133 fstring key;
135 slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_DOMAIN_GUID, domain);
136 strupper(key);
137 return secrets_store(key, guid, sizeof(GUID));
140 BOOL secrets_fetch_domain_guid(char *domain, GUID *guid)
142 GUID *dyn_guid;
143 fstring key;
144 size_t size;
145 GUID new_guid;
147 slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_DOMAIN_GUID, domain);
148 strupper(key);
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))
156 return False;
157 dyn_guid = (GUID *)secrets_fetch(key, &size);
158 if (dyn_guid == NULL)
159 return False;
162 if (size != sizeof(GUID))
164 SAFE_FREE(dyn_guid);
165 return False;
168 *guid = *dyn_guid;
169 SAFE_FREE(dyn_guid);
170 return True;
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);
186 strupper(keystr);
188 return keystr;
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)
200 static char* keystr;
202 asprintf(&keystr, "%s/%s", SECRETS_DOMTRUST_ACCT_PASS, domain);
203 strupper(keystr);
205 return keystr;
208 /************************************************************************
209 Lock the trust password entry.
210 ************************************************************************/
212 BOOL secrets_lock_trust_account_password(char *domain, BOOL dolock)
214 if (!tdb)
215 return False;
217 if (dolock)
218 return (tdb_lock_bystring(tdb, trust_keystr(domain)) == 0);
219 else
220 tdb_unlock_bystring(tdb, trust_keystr(domain));
221 return True;
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
227 the above call.
228 ************************************************************************/
230 BOOL secrets_fetch_trust_account_password(char *domain, uint8 ret_pwd[16],
231 time_t *pass_last_set_time)
233 struct machine_acct_pass *pass;
234 char *plaintext;
235 size_t size;
237 plaintext = secrets_fetch_machine_password();
238 if (plaintext) {
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 return True;
246 if (!(pass = secrets_fetch(trust_keystr(domain), &size))) {
247 DEBUG(5, ("secrets_fetch failed!\n"));
248 return False;
251 if (size != sizeof(*pass)) {
252 DEBUG(0, ("secrets were of incorrect size!\n"));
253 return False;
256 if (pass_last_set_time) *pass_last_set_time = pass->mod_time;
257 memcpy(ret_pwd, pass->hash, 16);
258 SAFE_FREE(pass);
259 return True;
262 /************************************************************************
263 Routine to get account password to trusted domain
264 ************************************************************************/
266 BOOL secrets_fetch_trusted_domain_password(char *domain, char** pwd,
267 DOM_SID *sid, time_t *pass_last_set_time)
269 struct trusted_dom_pass *pass;
270 size_t size;
272 /* fetching trusted domain password structure */
273 if (!(pass = secrets_fetch(trustdom_keystr(domain), &size))) {
274 DEBUG(5, ("secrets_fetch failed!\n"));
275 return False;
278 if (size != sizeof(*pass)) {
279 DEBUG(0, ("secrets were of incorrect size!\n"));
280 return False;
283 /* the trust's password */
284 if (pwd) {
285 *pwd = strdup(pass->pass);
286 if (!*pwd) {
287 return False;
291 /* last change time */
292 if (pass_last_set_time) *pass_last_set_time = pass->mod_time;
294 /* domain sid */
295 memcpy(&sid, &(pass->domain_sid), sizeof(sid));
297 SAFE_FREE(pass);
299 return True;
302 /************************************************************************
303 Routine to set the trust account password for a domain.
304 ************************************************************************/
305 BOOL secrets_store_trust_account_password(char *domain, uint8 new_pwd[16])
307 struct machine_acct_pass pass;
309 pass.mod_time = time(NULL);
310 memcpy(pass.hash, new_pwd, 16);
312 return secrets_store(trust_keystr(domain), (void *)&pass, sizeof(pass));
316 * Routine to set the password for trusted domain
318 * @param domain remote domain name
319 * @param pwd plain text password of trust relationship
320 * @param sid remote domain sid
322 * @return true if succeeded
325 BOOL secrets_store_trusted_domain_password(char* domain, smb_ucs2_t *uni_dom_name,
326 size_t uni_name_len, char* pwd,
327 DOM_SID sid)
329 struct trusted_dom_pass pass;
330 ZERO_STRUCT(pass);
332 /* unicode domain name and its length */
333 if (!uni_dom_name)
334 return False;
336 strncpy_w(pass.uni_name, uni_dom_name, sizeof(pass.uni_name) - 1);
337 pass.uni_name_len = uni_name_len;
339 /* last change time */
340 pass.mod_time = time(NULL);
342 /* password of the trust */
343 pass.pass_len = strlen(pwd);
344 fstrcpy(pass.pass, pwd);
346 /* domain sid */
347 memcpy(&(pass.domain_sid), &sid, sizeof(sid));
349 return secrets_store(trustdom_keystr(domain), (void *)&pass, sizeof(pass));
352 /************************************************************************
353 Routine to set the plaintext machine account password for a realm
354 the password is assumed to be a null terminated ascii string
355 ************************************************************************/
356 BOOL secrets_store_machine_password(char *pass)
358 char *key;
359 BOOL ret;
360 asprintf(&key, "%s/%s", SECRETS_MACHINE_PASSWORD, lp_workgroup());
361 strupper(key);
362 ret = secrets_store(key, pass, strlen(pass)+1);
363 free(key);
364 return ret;
368 /************************************************************************
369 Routine to fetch the plaintext machine account password for a realm
370 the password is assumed to be a null terminated ascii string
371 ************************************************************************/
372 char *secrets_fetch_machine_password(void)
374 char *key;
375 char *ret;
376 asprintf(&key, "%s/%s", SECRETS_MACHINE_PASSWORD, lp_workgroup());
377 strupper(key);
378 ret = (char *)secrets_fetch(key, NULL);
379 free(key);
380 return ret;
385 /************************************************************************
386 Routine to delete the machine trust account password file for a domain.
387 ************************************************************************/
389 BOOL trust_password_delete(const char *domain)
391 return secrets_delete(trust_keystr(domain));
394 /************************************************************************
395 Routine to delete the password for trusted domain
396 ************************************************************************/
397 BOOL trusted_domain_password_delete(const char *domain)
399 return secrets_delete(trustdom_keystr(domain));
403 /*******************************************************************
404 Reset the 'done' variables so after a client process is created
405 from a fork call these calls will be re-done. This should be
406 expanded if more variables need reseting.
407 ******************************************************************/
409 void reset_globals_after_fork(void)
411 unsigned char dummy;
413 secrets_init();
416 * Increment the global seed value to ensure every smbd starts
417 * with a new random seed.
420 if (tdb) {
421 uint32 initial_val = sys_getpid();
422 tdb_change_int32_atomic(tdb, "INFO/random_seed", (int *)&initial_val, 1);
423 set_rand_reseed_data((unsigned char *)&initial_val, sizeof(initial_val));
427 * Re-seed the random crypto generator, so all smbd's
428 * started from the same parent won't generate the same
429 * sequence.
431 generate_random_buffer( &dummy, 1, True);
434 BOOL secrets_store_ldap_pw(const char* dn, char* pw)
436 char *key = NULL;
437 BOOL ret;
439 if (asprintf(&key, "%s/%s", SECRETS_LDAP_BIND_PW, dn) < 0) {
440 DEBUG(0, ("secrets_store_ldap_pw: asprintf failed!\n"));
441 return False;
444 ret = secrets_store(key, pw, strlen(pw)+1);
446 SAFE_FREE(key);
447 return ret;
452 * Get trusted domains info from secrets.tdb.
454 * The linked list is allocated on the supplied talloc context, caller gets to destroy
455 * when done.
457 * @param ctx Allocation context
458 * @param enum_ctx Starting index, eg. we can start fetching at third
459 * or sixth trusted domain entry. Zero is the first index.
460 * Value it is set to is the enum context for the next enumeration.
461 * @param num_domains Number of domain entries to fetch at one call
462 * @param domains Pointer to array of trusted domain structs to be filled up
464 * @return nt status code of rpc response
465 **/
467 NTSTATUS secrets_get_trusted_domains(TALLOC_CTX* ctx, int* enum_ctx, int max_num_domains, int *num_domains, TRUSTDOM ***domains)
469 TDB_LIST_NODE *keys, *k;
470 TRUSTDOM *dom = NULL;
471 char *pattern;
472 int start_idx;
473 uint32 idx = 0;
474 size_t size;
475 fstring dom_name;
476 struct trusted_dom_pass *pass;
477 NTSTATUS status;
479 if (!secrets_init()) return NT_STATUS_ACCESS_DENIED;
481 *num_domains = 0;
482 start_idx = *enum_ctx;
484 /* generate searching pattern */
485 if (!(pattern = talloc_asprintf(ctx, "%s/*", SECRETS_DOMTRUST_ACCT_PASS))) {
486 DEBUG(0, ("secrets_get_trusted_domains: talloc_asprintf() failed!\n"));
487 return NT_STATUS_NO_MEMORY;
490 DEBUG(5, ("secrets_get_trusted_domains: looking for %d domains, starting at index %d\n",
491 max_num_domains, *enum_ctx));
493 *domains = talloc_zero(ctx, sizeof(**domains)*max_num_domains);
495 /* fetching trusted domains' data and collecting them in a list */
496 keys = tdb_search_keys(tdb, pattern);
499 * if there's no keys returned ie. no trusted domain,
500 * return "no more entries" code
502 status = NT_STATUS_NO_MORE_ENTRIES;
504 /* searching for keys in sectrets db -- way to go ... */
505 for (k = keys; k; k = k->next) {
506 char *secrets_key;
508 /* important: ensure null-termination of the key string */
509 secrets_key = strndup(k->node_key.dptr, k->node_key.dsize);
510 if (!secrets_key) {
511 DEBUG(0, ("strndup failed!\n"));
512 return NT_STATUS_NO_MEMORY;
515 pass = secrets_fetch(secrets_key, &size);
517 if (size != sizeof(*pass)) {
518 DEBUG(2, ("Secrets record %s is invalid!\n", secrets_key));
519 SAFE_FREE(pass);
520 continue;
523 pull_ucs2_fstring(dom_name, pass->uni_name);
524 DEBUG(18, ("Fetched secret record num %d.\nDomain name: %s, SID: %s\n",
525 idx, dom_name, sid_string_static(&pass->domain_sid)));
527 SAFE_FREE(secrets_key);
529 if (idx >= start_idx && idx < start_idx + max_num_domains) {
530 dom = talloc_zero(ctx, sizeof(*dom));
531 if (!dom) {
532 /* free returned tdb record */
533 SAFE_FREE(pass);
535 return NT_STATUS_NO_MEMORY;
538 /* copy domain sid */
539 SMB_ASSERT(sizeof(dom->sid) == sizeof(pass->domain_sid));
540 memcpy(&(dom->sid), &(pass->domain_sid), sizeof(dom->sid));
542 /* copy unicode domain name */
543 dom->name = talloc_strdup_w(ctx, pass->uni_name);
545 (*domains)[idx - start_idx] = dom;
547 DEBUG(18, ("Secret record is in required range.\n \
548 start_idx = %d, max_num_domains = %d. Added to returned array.\n",
549 start_idx, max_num_domains));
551 *enum_ctx = idx + 1;
552 (*num_domains)++;
554 /* set proper status code to return */
555 if (k->next) {
556 /* there are yet some entries to enumerate */
557 status = STATUS_MORE_ENTRIES;
558 } else {
559 /* this is the last entry in the whole enumeration */
560 status = NT_STATUS_OK;
562 } else {
563 DEBUG(18, ("Secret is outside the required range.\n \
564 start_idx = %d, max_num_domains = %d. Not added to returned array\n",
565 start_idx, max_num_domains));
568 idx++;
570 /* free returned tdb record */
571 SAFE_FREE(pass);
574 DEBUG(5, ("secrets_get_trusted_domains: got %d domains\n", *num_domains));
576 /* free the results of searching the keys */
577 tdb_search_list_free(keys);
579 return status;
582 static SIG_ATOMIC_T gotalarm;
584 /***************************************************************
585 Signal function to tell us we timed out.
586 ****************************************************************/
588 static void gotalarm_sig(void)
590 gotalarm = 1;
594 lock the secrets tdb based on a string - this is used as a primitive form of mutex
595 between smbd instances.
597 BOOL secrets_named_mutex(const char *name, unsigned int timeout)
599 TDB_DATA key;
600 int ret;
602 if (!message_init())
603 return False;
605 key.dptr = (char *)name;
606 key.dsize = strlen(name)+1;
608 /* Allow tdb_chainlock to be interrupted by an alarm. */
609 gotalarm = 0;
610 tdb_set_lock_alarm(&gotalarm);
612 if (timeout) {
613 CatchSignal(SIGALRM, SIGNAL_CAST gotalarm_sig);
614 alarm(timeout);
617 ret = tdb_chainlock(tdb, key);
619 /* Prevent tdb_chainlock from being interrupted by an alarm. */
620 tdb_set_lock_alarm(NULL);
622 if (timeout) {
623 alarm(0);
624 CatchSignal(SIGALRM, SIGNAL_CAST SIG_IGN);
625 if (gotalarm)
626 return False;
629 if (ret == 0)
630 DEBUG(10,("secrets_named_mutex: got mutex for %s\n", name ));
632 return (ret == 0);
636 unlock a named mutex
638 void secrets_named_mutex_release(char *name)
640 TDB_DATA key;
642 key.dptr = name;
643 key.dsize = strlen(name)+1;
645 tdb_chainunlock(tdb, key);
646 DEBUG(10,("secrets_named_mutex: released mutex for %s\n", name ));