As described in http://davenport.sourceforge.net/ntlm.html add NTLM2
[Samba/gebeck_regimport.git] / source3 / passdb / secrets.c
blob23413e4026c45601b0c656e309adfb9113935aad
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(const char *domain, const DOM_SID *sid)
100 fstring key;
102 slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_DOMAIN_SID, domain);
103 strupper_m(key);
104 return secrets_store(key, sid, sizeof(DOM_SID));
107 BOOL secrets_fetch_domain_sid(const 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_m(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(const char *domain, GUID *guid)
133 fstring key;
135 slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_DOMAIN_GUID, domain);
136 strupper_m(key);
137 return secrets_store(key, guid, sizeof(GUID));
140 BOOL secrets_fetch_domain_guid(const 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_m(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 smb_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_m(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 static char *trustdom_keystr(const char *domain)
200 static pstring keystr;
202 pstr_sprintf(keystr, "%s/%s", SECRETS_DOMTRUST_ACCT_PASS, domain);
203 strupper_m(keystr);
205 return keystr;
208 /************************************************************************
209 Lock the trust password entry.
210 ************************************************************************/
212 BOOL secrets_lock_trust_account_password(const char *domain, BOOL dolock)
214 if (!tdb)
215 return False;
217 if (dolock)
218 return (tdb_lock_bystring(tdb, trust_keystr(domain),0) == 0);
219 else
220 tdb_unlock_bystring(tdb, trust_keystr(domain));
221 return True;
224 /************************************************************************
225 Routine to get the default secure channel type for trust accounts
226 ************************************************************************/
228 uint32 get_default_sec_channel(void)
230 if (lp_server_role() == ROLE_DOMAIN_BDC ||
231 lp_server_role() == ROLE_DOMAIN_PDC) {
232 return SEC_CHAN_BDC;
233 } else {
234 return SEC_CHAN_WKSTA;
238 /************************************************************************
239 Routine to get the trust account password for a domain.
240 The user of this function must have locked the trust password file using
241 the above call.
242 ************************************************************************/
244 BOOL secrets_fetch_trust_account_password(const char *domain, uint8 ret_pwd[16],
245 time_t *pass_last_set_time,
246 uint32 *channel)
248 struct machine_acct_pass *pass;
249 char *plaintext;
250 size_t size;
252 plaintext = secrets_fetch_machine_password(domain, pass_last_set_time,
253 channel);
254 if (plaintext) {
255 DEBUG(4,("Using cleartext machine password\n"));
256 E_md4hash(plaintext, ret_pwd);
257 SAFE_FREE(plaintext);
258 return True;
261 if (!(pass = secrets_fetch(trust_keystr(domain), &size))) {
262 DEBUG(5, ("secrets_fetch failed!\n"));
263 return False;
266 if (size != sizeof(*pass)) {
267 DEBUG(0, ("secrets were of incorrect size!\n"));
268 return False;
271 if (pass_last_set_time) *pass_last_set_time = pass->mod_time;
272 memcpy(ret_pwd, pass->hash, 16);
273 SAFE_FREE(pass);
275 if (channel)
276 *channel = get_default_sec_channel();
278 return True;
281 /************************************************************************
282 Routine to get account password to trusted domain
283 ************************************************************************/
285 BOOL secrets_fetch_trusted_domain_password(const char *domain, char** pwd,
286 DOM_SID *sid, time_t *pass_last_set_time)
288 struct trusted_dom_pass pass;
289 size_t size;
291 /* unpacking structures */
292 char* pass_buf;
293 int pass_len = 0;
295 ZERO_STRUCT(pass);
297 /* fetching trusted domain password structure */
298 if (!(pass_buf = secrets_fetch(trustdom_keystr(domain), &size))) {
299 DEBUG(5, ("secrets_fetch failed!\n"));
300 return False;
303 /* unpack trusted domain password */
304 pass_len = tdb_trusted_dom_pass_unpack(pass_buf, size, &pass);
305 SAFE_FREE(pass_buf);
307 if (pass_len != size) {
308 DEBUG(5, ("Invalid secrets size. Unpacked data doesn't match trusted_dom_pass structure.\n"));
309 return False;
312 /* the trust's password */
313 if (pwd) {
314 *pwd = strdup(pass.pass);
315 if (!*pwd) {
316 return False;
320 /* last change time */
321 if (pass_last_set_time) *pass_last_set_time = pass.mod_time;
323 /* domain sid */
324 sid_copy(sid, &pass.domain_sid);
326 return True;
329 /************************************************************************
330 Routine to set the trust account password for a domain.
331 ************************************************************************/
333 BOOL secrets_store_trust_account_password(const char *domain, uint8 new_pwd[16])
335 struct machine_acct_pass pass;
337 pass.mod_time = time(NULL);
338 memcpy(pass.hash, new_pwd, 16);
340 return secrets_store(trust_keystr(domain), (void *)&pass, sizeof(pass));
344 * Routine to store the password for trusted domain
346 * @param domain remote domain name
347 * @param pwd plain text password of trust relationship
348 * @param sid remote domain sid
350 * @return true if succeeded
353 BOOL secrets_store_trusted_domain_password(const char* domain, smb_ucs2_t *uni_dom_name,
354 size_t uni_name_len, const char* pwd,
355 DOM_SID sid)
357 /* packing structures */
358 pstring pass_buf;
359 int pass_len = 0;
360 int pass_buf_len = sizeof(pass_buf);
362 struct trusted_dom_pass pass;
363 ZERO_STRUCT(pass);
365 /* unicode domain name and its length */
366 if (!uni_dom_name)
367 return False;
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);
379 /* domain sid */
380 sid_copy(&pass.domain_sid, &sid);
382 pass_len = tdb_trusted_dom_pass_pack(pass_buf, pass_buf_len, &pass);
384 return secrets_store(trustdom_keystr(domain), (void *)&pass_buf, pass_len);
387 /************************************************************************
388 Routine to set the plaintext machine account password for a realm
389 the password is assumed to be a null terminated ascii string
390 ************************************************************************/
392 BOOL secrets_store_machine_password(const char *pass, const char *domain, uint32 sec_channel)
394 char *key = NULL;
395 BOOL ret;
396 uint32 last_change_time;
397 uint32 sec_channel_type;
399 asprintf(&key, "%s/%s", SECRETS_MACHINE_PASSWORD, domain);
400 if (!key)
401 return False;
402 strupper_m(key);
404 ret = secrets_store(key, pass, strlen(pass)+1);
405 SAFE_FREE(key);
407 if (!ret)
408 return ret;
410 asprintf(&key, "%s/%s", SECRETS_MACHINE_LAST_CHANGE_TIME, domain);
411 if (!key)
412 return False;
413 strupper_m(key);
415 SIVAL(&last_change_time, 0, time(NULL));
416 ret = secrets_store(key, &last_change_time, sizeof(last_change_time));
417 SAFE_FREE(key);
419 asprintf(&key, "%s/%s", SECRETS_MACHINE_SEC_CHANNEL_TYPE, domain);
420 if (!key)
421 return False;
422 strupper_m(key);
424 SIVAL(&sec_channel_type, 0, sec_channel);
425 ret = secrets_store(key, &sec_channel_type, sizeof(sec_channel_type));
426 SAFE_FREE(key);
428 return ret;
432 /************************************************************************
433 Routine to fetch the plaintext machine account password for a realm
434 the password is assumed to be a null terminated ascii string
435 ************************************************************************/
436 char *secrets_fetch_machine_password(const char *domain,
437 time_t *pass_last_set_time,
438 uint32 *channel)
440 char *key = NULL;
441 char *ret;
442 asprintf(&key, "%s/%s", SECRETS_MACHINE_PASSWORD, domain);
443 strupper_m(key);
444 ret = (char *)secrets_fetch(key, NULL);
445 SAFE_FREE(key);
447 if (pass_last_set_time) {
448 size_t size;
449 uint32 *last_set_time;
450 asprintf(&key, "%s/%s", SECRETS_MACHINE_LAST_CHANGE_TIME, domain);
451 strupper_m(key);
452 last_set_time = secrets_fetch(key, &size);
453 if (last_set_time) {
454 *pass_last_set_time = IVAL(last_set_time,0);
455 SAFE_FREE(last_set_time);
456 } else {
457 *pass_last_set_time = 0;
459 SAFE_FREE(key);
462 if (channel) {
463 size_t size;
464 uint32 *channel_type;
465 asprintf(&key, "%s/%s", SECRETS_MACHINE_SEC_CHANNEL_TYPE, domain);
466 strupper_m(key);
467 channel_type = secrets_fetch(key, &size);
468 if (channel_type) {
469 *channel = IVAL(channel_type,0);
470 SAFE_FREE(channel_type);
471 } else {
472 *channel = get_default_sec_channel();
474 SAFE_FREE(key);
477 return ret;
482 /************************************************************************
483 Routine to delete the machine trust account password file for a domain.
484 ************************************************************************/
486 BOOL trust_password_delete(const char *domain)
488 return secrets_delete(trust_keystr(domain));
491 /************************************************************************
492 Routine to delete the password for trusted domain
493 ************************************************************************/
495 BOOL trusted_domain_password_delete(const char *domain)
497 return secrets_delete(trustdom_keystr(domain));
501 /*******************************************************************
502 Reset the 'done' variables so after a client process is created
503 from a fork call these calls will be re-done. This should be
504 expanded if more variables need reseting.
505 ******************************************************************/
507 void reset_globals_after_fork(void)
509 unsigned char dummy;
511 secrets_init();
514 * Increment the global seed value to ensure every smbd starts
515 * with a new random seed.
518 if (tdb) {
519 uint32 initial_val = sys_getpid();
520 tdb_change_int32_atomic(tdb, "INFO/random_seed", (int *)&initial_val, 1);
521 set_rand_reseed_data((unsigned char *)&initial_val, sizeof(initial_val));
525 * Re-seed the random crypto generator, so all smbd's
526 * started from the same parent won't generate the same
527 * sequence.
529 generate_random_buffer( &dummy, 1, True);
532 BOOL secrets_store_ldap_pw(const char* dn, char* pw)
534 char *key = NULL;
535 BOOL ret;
537 if (asprintf(&key, "%s/%s", SECRETS_LDAP_BIND_PW, dn) < 0) {
538 DEBUG(0, ("secrets_store_ldap_pw: asprintf failed!\n"));
539 return False;
542 ret = secrets_store(key, pw, strlen(pw)+1);
544 SAFE_FREE(key);
545 return ret;
550 * Get trusted domains info from secrets.tdb.
552 * The linked list is allocated on the supplied talloc context, caller gets to destroy
553 * when done.
555 * @param ctx Allocation context
556 * @param enum_ctx Starting index, eg. we can start fetching at third
557 * or sixth trusted domain entry. Zero is the first index.
558 * Value it is set to is the enum context for the next enumeration.
559 * @param num_domains Number of domain entries to fetch at one call
560 * @param domains Pointer to array of trusted domain structs to be filled up
562 * @return nt status code of rpc response
563 **/
565 NTSTATUS secrets_get_trusted_domains(TALLOC_CTX* ctx, int* enum_ctx, unsigned int max_num_domains, int *num_domains, TRUSTDOM ***domains)
567 TDB_LIST_NODE *keys, *k;
568 TRUSTDOM *dom = NULL;
569 char *pattern;
570 unsigned int start_idx;
571 uint32 idx = 0;
572 size_t size, packed_size = 0;
573 fstring dom_name;
574 char *packed_pass;
575 struct trusted_dom_pass *pass = talloc_zero(ctx, sizeof(struct trusted_dom_pass));
576 NTSTATUS status;
578 if (!secrets_init()) return NT_STATUS_ACCESS_DENIED;
580 if (!pass) {
581 DEBUG(0, ("talloc_zero failed!\n"));
582 return NT_STATUS_NO_MEMORY;
585 *num_domains = 0;
586 start_idx = *enum_ctx;
588 /* generate searching pattern */
589 if (!(pattern = talloc_asprintf(ctx, "%s/*", SECRETS_DOMTRUST_ACCT_PASS))) {
590 DEBUG(0, ("secrets_get_trusted_domains: talloc_asprintf() failed!\n"));
591 return NT_STATUS_NO_MEMORY;
594 DEBUG(5, ("secrets_get_trusted_domains: looking for %d domains, starting at index %d\n",
595 max_num_domains, *enum_ctx));
597 *domains = talloc_zero(ctx, sizeof(**domains)*max_num_domains);
599 /* fetching trusted domains' data and collecting them in a list */
600 keys = tdb_search_keys(tdb, pattern);
603 * if there's no keys returned ie. no trusted domain,
604 * return "no more entries" code
606 status = NT_STATUS_NO_MORE_ENTRIES;
608 /* searching for keys in secrets db -- way to go ... */
609 for (k = keys; k; k = k->next) {
610 char *secrets_key;
612 /* important: ensure null-termination of the key string */
613 secrets_key = strndup(k->node_key.dptr, k->node_key.dsize);
614 if (!secrets_key) {
615 DEBUG(0, ("strndup failed!\n"));
616 return NT_STATUS_NO_MEMORY;
619 packed_pass = secrets_fetch(secrets_key, &size);
620 packed_size = tdb_trusted_dom_pass_unpack(packed_pass, size, pass);
621 /* packed representation isn't needed anymore */
622 SAFE_FREE(packed_pass);
624 if (size != packed_size) {
625 DEBUG(2, ("Secrets record %s is invalid!\n", secrets_key));
626 continue;
629 pull_ucs2_fstring(dom_name, pass->uni_name);
630 DEBUG(18, ("Fetched secret record num %d.\nDomain name: %s, SID: %s\n",
631 idx, dom_name, sid_string_static(&pass->domain_sid)));
633 SAFE_FREE(secrets_key);
635 if (idx >= start_idx && idx < start_idx + max_num_domains) {
636 dom = talloc_zero(ctx, sizeof(*dom));
637 if (!dom) {
638 /* free returned tdb record */
639 return NT_STATUS_NO_MEMORY;
642 /* copy domain sid */
643 SMB_ASSERT(sizeof(dom->sid) == sizeof(pass->domain_sid));
644 memcpy(&(dom->sid), &(pass->domain_sid), sizeof(dom->sid));
646 /* copy unicode domain name */
647 dom->name = talloc_strdup_w(ctx, pass->uni_name);
649 (*domains)[idx - start_idx] = dom;
651 DEBUG(18, ("Secret record is in required range.\n \
652 start_idx = %d, max_num_domains = %d. Added to returned array.\n",
653 start_idx, max_num_domains));
655 *enum_ctx = idx + 1;
656 (*num_domains)++;
658 /* set proper status code to return */
659 if (k->next) {
660 /* there are yet some entries to enumerate */
661 status = STATUS_MORE_ENTRIES;
662 } else {
663 /* this is the last entry in the whole enumeration */
664 status = NT_STATUS_OK;
666 } else {
667 DEBUG(18, ("Secret is outside the required range.\n \
668 start_idx = %d, max_num_domains = %d. Not added to returned array\n",
669 start_idx, max_num_domains));
672 idx++;
675 DEBUG(5, ("secrets_get_trusted_domains: got %d domains\n", *num_domains));
677 /* free the results of searching the keys */
678 tdb_search_list_free(keys);
680 return status;
683 /*******************************************************************************
684 Lock the secrets tdb based on a string - this is used as a primitive form of mutex
685 between smbd instances.
686 *******************************************************************************/
688 BOOL secrets_named_mutex(const char *name, unsigned int timeout)
690 int ret = 0;
692 if (!message_init())
693 return False;
695 ret = tdb_lock_bystring(tdb, name, timeout);
696 if (ret == 0)
697 DEBUG(10,("secrets_named_mutex: got mutex for %s\n", name ));
699 return (ret == 0);
702 /*******************************************************************************
703 Unlock a named mutex.
704 *******************************************************************************/
706 void secrets_named_mutex_release(const char *name)
708 tdb_unlock_bystring(tdb, name);
709 DEBUG(10,("secrets_named_mutex: released mutex for %s\n", name ));
712 /*********************************************************
713 Check to see if we must talk to the PDC to avoid sam
714 sync delays
715 ********************************************************/
717 BOOL must_use_pdc( const char *domain )
719 time_t now = time(NULL);
720 time_t last_change_time;
721 unsigned char passwd[16];
723 if ( !secrets_fetch_trust_account_password(domain, passwd, &last_change_time, NULL) )
724 return False;
727 * If the time the machine password has changed
728 * was less than about 15 minutes then we need to contact
729 * the PDC only, as we cannot be sure domain replication
730 * has yet taken place. Bug found by Gerald (way to go
731 * Gerald !). JRA.
734 if ( now - last_change_time < SAM_SYNC_WINDOW )
735 return True;
737 return False;