r13519: Fix the credentials chaining across netlogon pipe disconnects.
[Samba/gbeck.git] / source3 / passdb / secrets.c
blob6e46ea57febe49c45cb97dd97a08c787afe4b8ba
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
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 */
26 #include "includes.h"
28 #undef DBGC_CLASS
29 #define DBGC_CLASS DBGC_PASSDB
31 static TDB_CONTEXT *tdb;
33 /* Urrrg. global.... */
34 BOOL global_machine_password_needs_changing;
36 /**
37 * Use a TDB to store an incrementing random seed.
39 * Initialised to the current pid, the very first time Samba starts,
40 * and incremented by one each time it is needed.
42 * @note Not called by systems with a working /dev/urandom.
44 static void get_rand_seed(int *new_seed)
46 *new_seed = sys_getpid();
47 if (tdb) {
48 tdb_change_int32_atomic(tdb, "INFO/random_seed", new_seed, 1);
52 /* open up the secrets database */
53 BOOL secrets_init(void)
55 pstring fname;
56 unsigned char dummy;
58 if (tdb)
59 return True;
61 pstrcpy(fname, lp_private_dir());
62 pstrcat(fname,"/secrets.tdb");
64 tdb = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
66 if (!tdb) {
67 DEBUG(0,("Failed to open %s\n", fname));
68 return False;
71 /**
72 * Set a reseed function for the crypto random generator
74 * This avoids a problem where systems without /dev/urandom
75 * could send the same challenge to multiple clients
77 set_rand_reseed_callback(get_rand_seed);
79 /* Ensure that the reseed is done now, while we are root, etc */
80 generate_random_buffer(&dummy, sizeof(dummy));
82 return True;
85 /* read a entry from the secrets database - the caller must free the result
86 if size is non-null then the size of the entry is put in there
88 void *secrets_fetch(const char *key, size_t *size)
90 TDB_DATA dbuf;
91 secrets_init();
92 if (!tdb)
93 return NULL;
94 dbuf = tdb_fetch(tdb, string_tdb_data(key));
95 if (size)
96 *size = dbuf.dsize;
97 return dbuf.dptr;
100 /* store a secrets entry
102 BOOL secrets_store(const char *key, const void *data, size_t size)
104 secrets_init();
105 if (!tdb)
106 return False;
107 return tdb_store(tdb, string_tdb_data(key), make_tdb_data(data, size),
108 TDB_REPLACE) == 0;
112 /* delete a secets database entry
114 BOOL secrets_delete(const char *key)
116 secrets_init();
117 if (!tdb)
118 return False;
119 return tdb_delete(tdb, string_tdb_data(key)) == 0;
122 BOOL secrets_store_domain_sid(const char *domain, const DOM_SID *sid)
124 fstring key;
125 BOOL ret;
127 slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_DOMAIN_SID, domain);
128 strupper_m(key);
129 ret = secrets_store(key, sid, sizeof(DOM_SID));
131 /* Force a re-query, in case we modified our domain */
132 if (ret)
133 reset_global_sam_sid();
134 return ret;
137 BOOL secrets_fetch_domain_sid(const char *domain, DOM_SID *sid)
139 DOM_SID *dyn_sid;
140 fstring key;
141 size_t size = 0;
143 slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_DOMAIN_SID, domain);
144 strupper_m(key);
145 dyn_sid = (DOM_SID *)secrets_fetch(key, &size);
147 if (dyn_sid == NULL)
148 return False;
150 if (size != sizeof(DOM_SID)) {
151 SAFE_FREE(dyn_sid);
152 return False;
155 *sid = *dyn_sid;
156 SAFE_FREE(dyn_sid);
157 return True;
160 BOOL secrets_store_domain_guid(const char *domain, struct uuid *guid)
162 fstring key;
164 slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_DOMAIN_GUID, domain);
165 strupper_m(key);
166 return secrets_store(key, guid, sizeof(struct uuid));
169 BOOL secrets_fetch_domain_guid(const char *domain, struct uuid *guid)
171 struct uuid *dyn_guid;
172 fstring key;
173 size_t size = 0;
174 struct uuid new_guid;
176 slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_DOMAIN_GUID, domain);
177 strupper_m(key);
178 dyn_guid = (struct uuid *)secrets_fetch(key, &size);
180 if ((!dyn_guid) && (lp_server_role() == ROLE_DOMAIN_PDC)) {
181 smb_uuid_generate_random(&new_guid);
182 if (!secrets_store_domain_guid(domain, &new_guid))
183 return False;
184 dyn_guid = (struct uuid *)secrets_fetch(key, &size);
185 if (dyn_guid == NULL)
186 return False;
189 if (size != sizeof(struct uuid)) {
190 DEBUG(1,("UUID size %d is wrong!\n", (int)size));
191 SAFE_FREE(dyn_guid);
192 return False;
195 *guid = *dyn_guid;
196 SAFE_FREE(dyn_guid);
197 return True;
201 * Form a key for fetching the machine trust account password
203 * @param domain domain name
205 * @return stored password's key
207 const char *trust_keystr(const char *domain)
209 static fstring keystr;
211 slprintf(keystr,sizeof(keystr)-1,"%s/%s",
212 SECRETS_MACHINE_ACCT_PASS, domain);
213 strupper_m(keystr);
215 return keystr;
219 * Form a key for fetching a trusted domain password
221 * @param domain trusted domain name
223 * @return stored password's key
225 static char *trustdom_keystr(const char *domain)
227 static pstring keystr;
229 pstr_sprintf(keystr, "%s/%s", SECRETS_DOMTRUST_ACCT_PASS, domain);
230 strupper_m(keystr);
232 return keystr;
235 /************************************************************************
236 Lock the trust password entry.
237 ************************************************************************/
239 BOOL secrets_lock_trust_account_password(const char *domain, BOOL dolock)
241 if (!tdb)
242 return False;
244 if (dolock)
245 return (tdb_lock_bystring(tdb, trust_keystr(domain),0) == 0);
246 else
247 tdb_unlock_bystring(tdb, trust_keystr(domain));
248 return True;
251 /************************************************************************
252 Routine to get the default secure channel type for trust accounts
253 ************************************************************************/
255 uint32 get_default_sec_channel(void)
257 if (lp_server_role() == ROLE_DOMAIN_BDC ||
258 lp_server_role() == ROLE_DOMAIN_PDC) {
259 return SEC_CHAN_BDC;
260 } else {
261 return SEC_CHAN_WKSTA;
265 /************************************************************************
266 Routine to get the trust account password for a domain.
267 The user of this function must have locked the trust password file using
268 the above secrets_lock_trust_account_password().
269 ************************************************************************/
271 BOOL secrets_fetch_trust_account_password(const char *domain, uint8 ret_pwd[16],
272 time_t *pass_last_set_time,
273 uint32 *channel)
275 struct machine_acct_pass *pass;
276 char *plaintext;
277 size_t size = 0;
279 plaintext = secrets_fetch_machine_password(domain, pass_last_set_time,
280 channel);
281 if (plaintext) {
282 DEBUG(4,("Using cleartext machine password\n"));
283 E_md4hash(plaintext, ret_pwd);
284 SAFE_FREE(plaintext);
285 return True;
288 if (!(pass = secrets_fetch(trust_keystr(domain), &size))) {
289 DEBUG(5, ("secrets_fetch failed!\n"));
290 return False;
293 if (size != sizeof(*pass)) {
294 DEBUG(0, ("secrets were of incorrect size!\n"));
295 return False;
298 if (pass_last_set_time) {
299 *pass_last_set_time = pass->mod_time;
301 memcpy(ret_pwd, pass->hash, 16);
303 if (channel) {
304 *channel = get_default_sec_channel();
307 /* Test if machine password has expired and needs to be changed */
308 if (lp_machine_password_timeout()) {
309 if (pass->mod_time > 0 && time(NULL) > (pass->mod_time +
310 lp_machine_password_timeout())) {
311 global_machine_password_needs_changing = True;
315 SAFE_FREE(pass);
316 return True;
319 /************************************************************************
320 Routine to get account password to trusted domain
321 ************************************************************************/
323 BOOL secrets_fetch_trusted_domain_password(const char *domain, char** pwd,
324 DOM_SID *sid, time_t *pass_last_set_time)
326 struct trusted_dom_pass pass;
327 size_t size = 0;
329 /* unpacking structures */
330 char* pass_buf;
331 int pass_len = 0;
333 ZERO_STRUCT(pass);
335 /* fetching trusted domain password structure */
336 if (!(pass_buf = secrets_fetch(trustdom_keystr(domain), &size))) {
337 DEBUG(5, ("secrets_fetch failed!\n"));
338 return False;
341 /* unpack trusted domain password */
342 pass_len = tdb_trusted_dom_pass_unpack(pass_buf, size, &pass);
343 SAFE_FREE(pass_buf);
345 if (pass_len != size) {
346 DEBUG(5, ("Invalid secrets size. Unpacked data doesn't match trusted_dom_pass structure.\n"));
347 return False;
350 /* the trust's password */
351 if (pwd) {
352 *pwd = SMB_STRDUP(pass.pass);
353 if (!*pwd) {
354 return False;
358 /* last change time */
359 if (pass_last_set_time) *pass_last_set_time = pass.mod_time;
361 /* domain sid */
362 if (sid != NULL) sid_copy(sid, &pass.domain_sid);
364 return True;
367 /************************************************************************
368 Routine to set the trust account password for a domain.
369 ************************************************************************/
371 BOOL secrets_store_trust_account_password(const char *domain, uint8 new_pwd[16])
373 struct machine_acct_pass pass;
375 pass.mod_time = time(NULL);
376 memcpy(pass.hash, new_pwd, 16);
378 return secrets_store(trust_keystr(domain), (void *)&pass, sizeof(pass));
382 * Routine to store the password for trusted domain
384 * @param domain remote domain name
385 * @param pwd plain text password of trust relationship
386 * @param sid remote domain sid
388 * @return true if succeeded
391 BOOL secrets_store_trusted_domain_password(const char* domain, const char* pwd,
392 const DOM_SID *sid)
394 smb_ucs2_t *uni_dom_name;
396 /* packing structures */
397 pstring pass_buf;
398 int pass_len = 0;
399 int pass_buf_len = sizeof(pass_buf);
401 struct trusted_dom_pass pass;
402 ZERO_STRUCT(pass);
404 if (push_ucs2_allocate(&uni_dom_name, domain) < 0) {
405 DEBUG(0, ("Could not convert domain name %s to unicode\n",
406 domain));
407 return False;
410 strncpy_w(pass.uni_name, uni_dom_name, sizeof(pass.uni_name) - 1);
411 pass.uni_name_len = strlen_w(uni_dom_name)+1;
413 /* last change time */
414 pass.mod_time = time(NULL);
416 /* password of the trust */
417 pass.pass_len = strlen(pwd);
418 fstrcpy(pass.pass, pwd);
420 /* domain sid */
421 sid_copy(&pass.domain_sid, sid);
423 pass_len = tdb_trusted_dom_pass_pack(pass_buf, pass_buf_len, &pass);
425 return secrets_store(trustdom_keystr(domain), (void *)&pass_buf, pass_len);
428 /************************************************************************
429 Routine to set the plaintext machine account password for a realm
430 the password is assumed to be a null terminated ascii string
431 ************************************************************************/
433 BOOL secrets_store_machine_password(const char *pass, const char *domain, uint32 sec_channel)
435 char *key = NULL;
436 BOOL ret;
437 uint32 last_change_time;
438 uint32 sec_channel_type;
440 asprintf(&key, "%s/%s", SECRETS_MACHINE_PASSWORD, domain);
441 if (!key)
442 return False;
443 strupper_m(key);
445 ret = secrets_store(key, pass, strlen(pass)+1);
446 SAFE_FREE(key);
448 if (!ret)
449 return ret;
451 asprintf(&key, "%s/%s", SECRETS_MACHINE_LAST_CHANGE_TIME, domain);
452 if (!key)
453 return False;
454 strupper_m(key);
456 SIVAL(&last_change_time, 0, time(NULL));
457 ret = secrets_store(key, &last_change_time, sizeof(last_change_time));
458 SAFE_FREE(key);
460 asprintf(&key, "%s/%s", SECRETS_MACHINE_SEC_CHANNEL_TYPE, domain);
461 if (!key)
462 return False;
463 strupper_m(key);
465 SIVAL(&sec_channel_type, 0, sec_channel);
466 ret = secrets_store(key, &sec_channel_type, sizeof(sec_channel_type));
467 SAFE_FREE(key);
469 return ret;
472 /************************************************************************
473 Routine to fetch the plaintext machine account password for a realm
474 the password is assumed to be a null terminated ascii string.
475 ************************************************************************/
477 char *secrets_fetch_machine_password(const char *domain,
478 time_t *pass_last_set_time,
479 uint32 *channel)
481 char *key = NULL;
482 char *ret;
483 asprintf(&key, "%s/%s", SECRETS_MACHINE_PASSWORD, domain);
484 strupper_m(key);
485 ret = (char *)secrets_fetch(key, NULL);
486 SAFE_FREE(key);
488 if (pass_last_set_time) {
489 size_t size;
490 uint32 *last_set_time;
491 asprintf(&key, "%s/%s", SECRETS_MACHINE_LAST_CHANGE_TIME, domain);
492 strupper_m(key);
493 last_set_time = secrets_fetch(key, &size);
494 if (last_set_time) {
495 *pass_last_set_time = IVAL(last_set_time,0);
496 SAFE_FREE(last_set_time);
497 } else {
498 *pass_last_set_time = 0;
500 SAFE_FREE(key);
503 if (channel) {
504 size_t size;
505 uint32 *channel_type;
506 asprintf(&key, "%s/%s", SECRETS_MACHINE_SEC_CHANNEL_TYPE, domain);
507 strupper_m(key);
508 channel_type = secrets_fetch(key, &size);
509 if (channel_type) {
510 *channel = IVAL(channel_type,0);
511 SAFE_FREE(channel_type);
512 } else {
513 *channel = get_default_sec_channel();
515 SAFE_FREE(key);
518 return ret;
521 /*******************************************************************
522 Wrapper around retrieving the trust account password
523 *******************************************************************/
525 BOOL get_trust_pw(const char *domain, uint8 ret_pwd[16], uint32 *channel)
527 DOM_SID sid;
528 char *pwd;
529 time_t last_set_time;
531 /* if we are a DC and this is not our domain, then lookup an account
532 for the domain trust */
534 if ( IS_DC && !strequal(domain, lp_workgroup()) && lp_allow_trusted_domains() ) {
535 if (!secrets_fetch_trusted_domain_password(domain, &pwd, &sid,
536 &last_set_time)) {
537 DEBUG(0, ("get_trust_pw: could not fetch trust "
538 "account password for trusted domain %s\n",
539 domain));
540 return False;
543 *channel = SEC_CHAN_DOMAIN;
544 E_md4hash(pwd, ret_pwd);
545 SAFE_FREE(pwd);
547 return True;
550 /* Just get the account for the requested domain. In the future this
551 * might also cover to be member of more than one domain. */
553 if (secrets_fetch_trust_account_password(domain, ret_pwd,
554 &last_set_time, channel))
555 return True;
557 DEBUG(5, ("get_trust_pw: could not fetch trust account "
558 "password for domain %s\n", domain));
559 return False;
562 /************************************************************************
563 Routine to delete the machine trust account password file for a domain.
564 ************************************************************************/
566 BOOL trust_password_delete(const char *domain)
568 return secrets_delete(trust_keystr(domain));
571 /************************************************************************
572 Routine to delete the password for trusted domain
573 ************************************************************************/
575 BOOL trusted_domain_password_delete(const char *domain)
577 return secrets_delete(trustdom_keystr(domain));
580 BOOL secrets_store_ldap_pw(const char* dn, char* pw)
582 char *key = NULL;
583 BOOL ret;
585 if (asprintf(&key, "%s/%s", SECRETS_LDAP_BIND_PW, dn) < 0) {
586 DEBUG(0, ("secrets_store_ldap_pw: asprintf failed!\n"));
587 return False;
590 ret = secrets_store(key, pw, strlen(pw)+1);
592 SAFE_FREE(key);
593 return ret;
596 /*******************************************************************
597 Find the ldap password.
598 ******************************************************************/
600 BOOL fetch_ldap_pw(char **dn, char** pw)
602 char *key = NULL;
603 size_t size = 0;
605 *dn = smb_xstrdup(lp_ldap_admin_dn());
607 if (asprintf(&key, "%s/%s", SECRETS_LDAP_BIND_PW, *dn) < 0) {
608 SAFE_FREE(*dn);
609 DEBUG(0, ("fetch_ldap_pw: asprintf failed!\n"));
612 *pw=secrets_fetch(key, &size);
613 SAFE_FREE(key);
615 if (!size) {
616 /* Upgrade 2.2 style entry */
617 char *p;
618 char* old_style_key = SMB_STRDUP(*dn);
619 char *data;
620 fstring old_style_pw;
622 if (!old_style_key) {
623 DEBUG(0, ("fetch_ldap_pw: strdup failed!\n"));
624 return False;
627 for (p=old_style_key; *p; p++)
628 if (*p == ',') *p = '/';
630 data=secrets_fetch(old_style_key, &size);
631 if (!size && size < sizeof(old_style_pw)) {
632 DEBUG(0,("fetch_ldap_pw: neither ldap secret retrieved!\n"));
633 SAFE_FREE(old_style_key);
634 SAFE_FREE(*dn);
635 return False;
638 size = MIN(size, sizeof(fstring)-1);
639 strncpy(old_style_pw, data, size);
640 old_style_pw[size] = 0;
642 SAFE_FREE(data);
644 if (!secrets_store_ldap_pw(*dn, old_style_pw)) {
645 DEBUG(0,("fetch_ldap_pw: ldap secret could not be upgraded!\n"));
646 SAFE_FREE(old_style_key);
647 SAFE_FREE(*dn);
648 return False;
650 if (!secrets_delete(old_style_key)) {
651 DEBUG(0,("fetch_ldap_pw: old ldap secret could not be deleted!\n"));
654 SAFE_FREE(old_style_key);
656 *pw = smb_xstrdup(old_style_pw);
659 return True;
663 * Get trusted domains info from secrets.tdb.
664 **/
666 NTSTATUS secrets_trusted_domains(TALLOC_CTX *mem_ctx, uint32 *num_domains,
667 struct trustdom_info ***domains)
669 TDB_LIST_NODE *keys, *k;
670 char *pattern;
672 if (!secrets_init()) return NT_STATUS_ACCESS_DENIED;
674 /* generate searching pattern */
675 pattern = talloc_asprintf(mem_ctx, "%s/*", SECRETS_DOMTRUST_ACCT_PASS);
676 if (pattern == NULL) {
677 DEBUG(0, ("secrets_trusted_domains: talloc_asprintf() "
678 "failed!\n"));
679 return NT_STATUS_NO_MEMORY;
682 *domains = NULL;
683 *num_domains = 0;
685 /* fetching trusted domains' data and collecting them in a list */
686 keys = tdb_search_keys(tdb, pattern);
688 /* searching for keys in secrets db -- way to go ... */
689 for (k = keys; k; k = k->next) {
690 char *packed_pass;
691 size_t size = 0, packed_size = 0;
692 struct trusted_dom_pass pass;
693 char *secrets_key;
694 struct trustdom_info *dom_info;
696 /* important: ensure null-termination of the key string */
697 secrets_key = talloc_strndup(mem_ctx,
698 k->node_key.dptr,
699 k->node_key.dsize);
700 if (!secrets_key) {
701 DEBUG(0, ("strndup failed!\n"));
702 return NT_STATUS_NO_MEMORY;
705 packed_pass = secrets_fetch(secrets_key, &size);
706 packed_size = tdb_trusted_dom_pass_unpack(packed_pass, size,
707 &pass);
708 /* packed representation isn't needed anymore */
709 SAFE_FREE(packed_pass);
711 if (size != packed_size) {
712 DEBUG(2, ("Secrets record %s is invalid!\n",
713 secrets_key));
714 continue;
717 if (pass.domain_sid.num_auths != 4) {
718 DEBUG(0, ("SID %s is not a domain sid, has %d "
719 "auths instead of 4\n",
720 sid_string_static(&pass.domain_sid),
721 pass.domain_sid.num_auths));
722 continue;
725 dom_info = TALLOC_P(mem_ctx, struct trustdom_info);
726 if (dom_info == NULL) {
727 DEBUG(0, ("talloc failed\n"));
728 return NT_STATUS_NO_MEMORY;
731 if (pull_ucs2_talloc(mem_ctx, &dom_info->name,
732 pass.uni_name) < 0) {
733 DEBUG(2, ("pull_ucs2_talloc failed\n"));
734 return NT_STATUS_NO_MEMORY;
737 sid_copy(&dom_info->sid, &pass.domain_sid);
739 ADD_TO_ARRAY(mem_ctx, struct trustdom_info *, dom_info,
740 domains, num_domains);
742 if (*domains == NULL) {
743 return NT_STATUS_NO_MEMORY;
745 talloc_steal(*domains, dom_info);
748 DEBUG(5, ("secrets_get_trusted_domains: got %d domains\n",
749 *num_domains));
751 /* free the results of searching the keys */
752 tdb_search_list_free(keys);
754 return NT_STATUS_OK;
757 /*******************************************************************************
758 Lock the secrets tdb based on a string - this is used as a primitive form of mutex
759 between smbd instances.
760 *******************************************************************************/
762 BOOL secrets_named_mutex(const char *name, unsigned int timeout)
764 int ret = 0;
766 if (!secrets_init())
767 return False;
769 ret = tdb_lock_bystring(tdb, name, timeout);
770 if (ret == 0)
771 DEBUG(10,("secrets_named_mutex: got mutex for %s\n", name ));
773 return (ret == 0);
776 /*******************************************************************************
777 Unlock a named mutex.
778 *******************************************************************************/
780 void secrets_named_mutex_release(const char *name)
782 tdb_unlock_bystring(tdb, name);
783 DEBUG(10,("secrets_named_mutex: released mutex for %s\n", name ));
786 /*******************************************************************************
787 Store a complete AFS keyfile into secrets.tdb.
788 *******************************************************************************/
790 BOOL secrets_store_afs_keyfile(const char *cell, const struct afs_keyfile *keyfile)
792 fstring key;
794 if ((cell == NULL) || (keyfile == NULL))
795 return False;
797 if (ntohl(keyfile->nkeys) > SECRETS_AFS_MAXKEYS)
798 return False;
800 slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_AFS_KEYFILE, cell);
801 return secrets_store(key, keyfile, sizeof(struct afs_keyfile));
804 /*******************************************************************************
805 Fetch the current (highest) AFS key from secrets.tdb
806 *******************************************************************************/
807 BOOL secrets_fetch_afs_key(const char *cell, struct afs_key *result)
809 fstring key;
810 struct afs_keyfile *keyfile;
811 size_t size = 0;
812 uint32 i;
814 slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_AFS_KEYFILE, cell);
816 keyfile = (struct afs_keyfile *)secrets_fetch(key, &size);
818 if (keyfile == NULL)
819 return False;
821 if (size != sizeof(struct afs_keyfile)) {
822 SAFE_FREE(keyfile);
823 return False;
826 i = ntohl(keyfile->nkeys);
828 if (i > SECRETS_AFS_MAXKEYS) {
829 SAFE_FREE(keyfile);
830 return False;
833 *result = keyfile->entry[i-1];
835 result->kvno = ntohl(result->kvno);
837 return True;
840 /******************************************************************************
841 When kerberos is not available, choose between anonymous or
842 authenticated connections.
844 We need to use an authenticated connection if DCs have the
845 RestrictAnonymous registry entry set > 0, or the "Additional
846 restrictions for anonymous connections" set in the win2k Local
847 Security Policy.
849 Caller to free() result in domain, username, password
850 *******************************************************************************/
851 void secrets_fetch_ipc_userpass(char **username, char **domain, char **password)
853 *username = secrets_fetch(SECRETS_AUTH_USER, NULL);
854 *domain = secrets_fetch(SECRETS_AUTH_DOMAIN, NULL);
855 *password = secrets_fetch(SECRETS_AUTH_PASSWORD, NULL);
857 if (*username && **username) {
859 if (!*domain || !**domain)
860 *domain = smb_xstrdup(lp_workgroup());
862 if (!*password || !**password)
863 *password = smb_xstrdup("");
865 DEBUG(3, ("IPC$ connections done by user %s\\%s\n",
866 *domain, *username));
868 } else {
869 DEBUG(3, ("IPC$ connections done anonymously\n"));
870 *username = smb_xstrdup("");
871 *domain = smb_xstrdup("");
872 *password = smb_xstrdup("");
876 /******************************************************************************
877 Open or create the schannel session store tdb.
878 *******************************************************************************/
880 static TDB_CONTEXT *open_schannel_session_store(TALLOC_CTX *mem_ctx)
882 TDB_DATA vers;
883 uint32 ver;
884 TDB_CONTEXT *tdb_sc = NULL;
885 char *fname = talloc_asprintf(mem_ctx, "%s/schannel_store.tdb", lp_private_dir());
887 if (!fname) {
888 return NULL;
891 tdb_sc = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
893 if (!tdb_sc) {
894 DEBUG(0,("open_schannel_session_store: Failed to open %s\n", fname));
895 talloc_free(fname);
896 return NULL;
899 vers = tdb_fetch_bystring(tdb_sc, "SCHANNEL_STORE_VERSION");
900 if (vers.dptr == NULL) {
901 /* First opener, no version. */
902 SIVAL(&ver,0,1);
903 vers.dptr = (char *)&ver;
904 vers.dsize = 4;
905 tdb_store_bystring(tdb_sc, "SCHANNEL_STORE_VERSION", vers, TDB_REPLACE);
906 vers.dptr = NULL;
907 } else if (vers.dsize == 4) {
908 ver = IVAL(vers.dptr,0);
909 if (ver != 1) {
910 tdb_close(tdb_sc);
911 tdb_sc = NULL;
912 DEBUG(0,("open_schannel_session_store: wrong version number %d in %s\n",
913 (int)ver, fname ));
915 } else {
916 tdb_close(tdb_sc);
917 tdb_sc = NULL;
918 DEBUG(0,("open_schannel_session_store: wrong version number size %d in %s\n",
919 (int)vers.dsize, fname ));
922 SAFE_FREE(vers.dptr);
923 talloc_free(fname);
925 return tdb_sc;
928 /******************************************************************************
929 Store the schannel state after an AUTH2 call.
930 Note we must be root here.
931 *******************************************************************************/
933 BOOL secrets_store_schannel_session_info(TALLOC_CTX *mem_ctx, const struct dcinfo *pdc)
935 TDB_CONTEXT *tdb_sc = NULL;
936 TDB_DATA value;
937 BOOL ret;
938 char *keystr = talloc_asprintf(mem_ctx, "%s/%s", SECRETS_SCHANNEL_STATE,
939 pdc->remote_machine);
940 if (!keystr) {
941 return False;
944 strupper_m(keystr);
946 /* Work out how large the record is. */
947 value.dsize = tdb_pack(NULL, 0, "dBBBBBfff",
948 pdc->sequence,
949 8, pdc->seed_chal.data,
950 8, pdc->clnt_chal.data,
951 8, pdc->srv_chal.data,
952 8, pdc->sess_key,
953 16, pdc->mach_pw,
954 pdc->mach_acct,
955 pdc->remote_machine,
956 pdc->domain);
958 value.dptr = TALLOC(mem_ctx, value.dsize);
959 if (!value.dptr) {
960 talloc_free(keystr);
961 return False;
964 value.dsize = tdb_pack(value.dptr, value.dsize, "dBBBBBfff",
965 pdc->sequence,
966 8, pdc->seed_chal.data,
967 8, pdc->clnt_chal.data,
968 8, pdc->srv_chal.data,
969 8, pdc->sess_key,
970 16, pdc->mach_pw,
971 pdc->mach_acct,
972 pdc->remote_machine,
973 pdc->domain);
975 tdb_sc = open_schannel_session_store(mem_ctx);
976 if (!tdb_sc) {
977 talloc_free(keystr);
978 talloc_free(value.dptr);
979 return False;
982 ret = (tdb_store_bystring(tdb_sc, keystr, value, TDB_REPLACE) == 0 ? True : False);
984 DEBUG(3,("secrets_store_schannel_session_info: stored schannel info with key %s\n",
985 keystr ));
987 tdb_close(tdb_sc);
988 talloc_free(keystr);
989 talloc_free(value.dptr);
990 return ret;
993 /******************************************************************************
994 Restore the schannel state on a client reconnect.
995 Note we must be root here.
996 *******************************************************************************/
998 BOOL secrets_restore_schannel_session_info(TALLOC_CTX *mem_ctx,
999 const char *remote_machine,
1000 struct dcinfo **ppdc)
1002 TDB_CONTEXT *tdb_sc = NULL;
1003 TDB_DATA value;
1004 unsigned char *pseed_chal = NULL;
1005 unsigned char *pclnt_chal = NULL;
1006 unsigned char *psrv_chal = NULL;
1007 unsigned char *psess_key = NULL;
1008 unsigned char *pmach_pw = NULL;
1009 uint32 l1, l2, l3, l4, l5;
1010 int ret;
1011 struct dcinfo *pdc = NULL;
1012 char *keystr = talloc_asprintf(mem_ctx, "%s/%s", SECRETS_SCHANNEL_STATE,
1013 remote_machine);
1015 *ppdc = NULL;
1017 if (!keystr) {
1018 return False;
1021 strupper_m(keystr);
1023 tdb_sc = open_schannel_session_store(mem_ctx);
1024 if (!tdb_sc) {
1025 talloc_free(keystr);
1026 return False;
1029 value = tdb_fetch_bystring(tdb_sc, keystr);
1030 if (!value.dptr) {
1031 DEBUG(0,("secrets_restore_schannel_session_info: Failed to find entry with key %s\n",
1032 keystr ));
1033 tdb_close(tdb_sc);
1034 return False;
1037 tdb_close(tdb_sc);
1039 pdc = TALLOC_ZERO_P(mem_ctx, struct dcinfo);
1041 /* Retrieve the record. */
1042 ret = tdb_unpack(value.dptr, value.dsize, "dBBBBBfff",
1043 &pdc->sequence,
1044 &l1, &pseed_chal,
1045 &l2, &pclnt_chal,
1046 &l3, &psrv_chal,
1047 &l4, &psess_key,
1048 &l5, &pmach_pw,
1049 &pdc->mach_acct,
1050 &pdc->remote_machine,
1051 &pdc->domain);
1053 if (ret == -1 || l1 != 8 || l2 != 8 || l3 != 8 || l4 != 8 || l5 != 16) {
1054 talloc_free(keystr);
1055 talloc_free(pdc);
1056 SAFE_FREE(pseed_chal);
1057 SAFE_FREE(pclnt_chal);
1058 SAFE_FREE(psrv_chal);
1059 SAFE_FREE(psess_key);
1060 SAFE_FREE(pmach_pw);
1061 SAFE_FREE(value.dptr);
1062 return False;
1065 memcpy(pdc->seed_chal.data, pseed_chal, 8);
1066 memcpy(pdc->clnt_chal.data, pclnt_chal, 8);
1067 memcpy(pdc->srv_chal.data, psrv_chal, 8);
1068 memcpy(pdc->sess_key, psess_key, 8);
1069 memset(&pdc->sess_key[8], '\0', 8); /* key followed by 8 bytes of zero. */
1070 memcpy(pdc->mach_pw, pmach_pw, 16);
1072 /* We know these are true so didn't bother to store them. */
1073 pdc->challenge_sent = True;
1074 pdc->authenticated = True;
1076 DEBUG(3,("secrets_restore_schannel_session_info: restored schannel info key %s\n",
1077 keystr ));
1079 SAFE_FREE(pseed_chal);
1080 SAFE_FREE(pclnt_chal);
1081 SAFE_FREE(psrv_chal);
1082 SAFE_FREE(psess_key);
1083 SAFE_FREE(pmach_pw);
1085 talloc_free(keystr);
1086 SAFE_FREE(value.dptr);
1088 *ppdc = pdc;
1090 return True;