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 */
29 #define DBGC_CLASS DBGC_PASSDB
31 static TDB_CONTEXT
*tdb
;
33 /* open up the secrets database */
34 BOOL
secrets_init(void)
41 pstrcpy(fname
, lp_private_dir());
42 pstrcat(fname
,"/secrets.tdb");
44 tdb
= tdb_open_log(fname
, 0, TDB_DEFAULT
, O_RDWR
|O_CREAT
, 0600);
47 DEBUG(0,("Failed to open %s\n", fname
));
53 /* read a entry from the secrets database - the caller must free the result
54 if size is non-null then the size of the entry is put in there
56 void *secrets_fetch(const char *key
, size_t *size
)
62 kbuf
.dptr
= (char *)key
;
63 kbuf
.dsize
= strlen(key
);
64 dbuf
= tdb_fetch(tdb
, kbuf
);
70 /* store a secrets entry
72 BOOL
secrets_store(const char *key
, const void *data
, size_t size
)
78 kbuf
.dptr
= (char *)key
;
79 kbuf
.dsize
= strlen(key
);
80 dbuf
.dptr
= (char *)data
;
82 return tdb_store(tdb
, kbuf
, dbuf
, TDB_REPLACE
) == 0;
86 /* delete a secets database entry
88 BOOL
secrets_delete(const char *key
)
94 kbuf
.dptr
= (char *)key
;
95 kbuf
.dsize
= strlen(key
);
96 return tdb_delete(tdb
, kbuf
) == 0;
99 BOOL
secrets_store_domain_sid(const char *domain
, const DOM_SID
*sid
)
104 slprintf(key
, sizeof(key
)-1, "%s/%s", SECRETS_DOMAIN_SID
, domain
);
106 ret
= secrets_store(key
, sid
, sizeof(DOM_SID
));
108 /* Force a re-query, in case we modified our domain */
110 reset_global_sam_sid();
114 BOOL
secrets_fetch_domain_sid(const char *domain
, DOM_SID
*sid
)
120 slprintf(key
, sizeof(key
)-1, "%s/%s", SECRETS_DOMAIN_SID
, domain
);
122 dyn_sid
= (DOM_SID
*)secrets_fetch(key
, &size
);
127 if (size
!= sizeof(DOM_SID
))
138 BOOL
secrets_store_domain_guid(const char *domain
, GUID
*guid
)
142 slprintf(key
, sizeof(key
)-1, "%s/%s", SECRETS_DOMAIN_GUID
, domain
);
144 return secrets_store(key
, guid
, sizeof(GUID
));
147 BOOL
secrets_fetch_domain_guid(const char *domain
, GUID
*guid
)
154 slprintf(key
, sizeof(key
)-1, "%s/%s", SECRETS_DOMAIN_GUID
, domain
);
156 dyn_guid
= (GUID
*)secrets_fetch(key
, &size
);
158 DEBUG(6,("key is %s, size is %d\n", key
, (int)size
));
160 if ((NULL
== dyn_guid
) && (ROLE_DOMAIN_PDC
== lp_server_role())) {
161 smb_uuid_generate_random(&new_guid
);
162 if (!secrets_store_domain_guid(domain
, &new_guid
))
164 dyn_guid
= (GUID
*)secrets_fetch(key
, &size
);
165 if (dyn_guid
== NULL
)
169 if (size
!= sizeof(GUID
))
181 * Form a key for fetching the machine trust account password
183 * @param domain domain name
185 * @return stored password's key
187 const char *trust_keystr(const char *domain
)
189 static fstring keystr
;
191 slprintf(keystr
,sizeof(keystr
)-1,"%s/%s",
192 SECRETS_MACHINE_ACCT_PASS
, domain
);
199 * Form a key for fetching a trusted domain password
201 * @param domain trusted domain name
203 * @return stored password's key
205 static char *trustdom_keystr(const char *domain
)
207 static pstring keystr
;
209 pstr_sprintf(keystr
, "%s/%s", SECRETS_DOMTRUST_ACCT_PASS
, domain
);
215 /************************************************************************
216 Lock the trust password entry.
217 ************************************************************************/
219 BOOL
secrets_lock_trust_account_password(const char *domain
, BOOL dolock
)
225 return (tdb_lock_bystring(tdb
, trust_keystr(domain
),0) == 0);
227 tdb_unlock_bystring(tdb
, trust_keystr(domain
));
231 /************************************************************************
232 Routine to get the default secure channel type for trust accounts
233 ************************************************************************/
235 uint32
get_default_sec_channel(void)
237 if (lp_server_role() == ROLE_DOMAIN_BDC
||
238 lp_server_role() == ROLE_DOMAIN_PDC
) {
241 return SEC_CHAN_WKSTA
;
245 /************************************************************************
246 Routine to get the trust account password for a domain.
247 The user of this function must have locked the trust password file using
249 ************************************************************************/
251 BOOL
secrets_fetch_trust_account_password(const char *domain
, uint8 ret_pwd
[16],
252 time_t *pass_last_set_time
,
255 struct machine_acct_pass
*pass
;
259 plaintext
= secrets_fetch_machine_password(domain
, pass_last_set_time
,
262 DEBUG(4,("Using cleartext machine password\n"));
263 E_md4hash(plaintext
, ret_pwd
);
264 SAFE_FREE(plaintext
);
268 if (!(pass
= secrets_fetch(trust_keystr(domain
), &size
))) {
269 DEBUG(5, ("secrets_fetch failed!\n"));
273 if (size
!= sizeof(*pass
)) {
274 DEBUG(0, ("secrets were of incorrect size!\n"));
278 if (pass_last_set_time
) *pass_last_set_time
= pass
->mod_time
;
279 memcpy(ret_pwd
, pass
->hash
, 16);
283 *channel
= get_default_sec_channel();
288 /************************************************************************
289 Routine to get account password to trusted domain
290 ************************************************************************/
292 BOOL
secrets_fetch_trusted_domain_password(const char *domain
, char** pwd
,
293 DOM_SID
*sid
, time_t *pass_last_set_time
)
295 struct trusted_dom_pass pass
;
298 /* unpacking structures */
304 /* fetching trusted domain password structure */
305 if (!(pass_buf
= secrets_fetch(trustdom_keystr(domain
), &size
))) {
306 DEBUG(5, ("secrets_fetch failed!\n"));
310 /* unpack trusted domain password */
311 pass_len
= tdb_trusted_dom_pass_unpack(pass_buf
, size
, &pass
);
314 if (pass_len
!= size
) {
315 DEBUG(5, ("Invalid secrets size. Unpacked data doesn't match trusted_dom_pass structure.\n"));
319 /* the trust's password */
321 *pwd
= strdup(pass
.pass
);
327 /* last change time */
328 if (pass_last_set_time
) *pass_last_set_time
= pass
.mod_time
;
331 sid_copy(sid
, &pass
.domain_sid
);
336 /************************************************************************
337 Routine to set the trust account password for a domain.
338 ************************************************************************/
340 BOOL
secrets_store_trust_account_password(const char *domain
, uint8 new_pwd
[16])
342 struct machine_acct_pass pass
;
344 pass
.mod_time
= time(NULL
);
345 memcpy(pass
.hash
, new_pwd
, 16);
347 return secrets_store(trust_keystr(domain
), (void *)&pass
, sizeof(pass
));
351 * Routine to store the password for trusted domain
353 * @param domain remote domain name
354 * @param pwd plain text password of trust relationship
355 * @param sid remote domain sid
357 * @return true if succeeded
360 BOOL
secrets_store_trusted_domain_password(const char* domain
, smb_ucs2_t
*uni_dom_name
,
361 size_t uni_name_len
, const char* pwd
,
364 /* packing structures */
367 int pass_buf_len
= sizeof(pass_buf
);
369 struct trusted_dom_pass pass
;
372 /* unicode domain name and its length */
376 strncpy_w(pass
.uni_name
, uni_dom_name
, sizeof(pass
.uni_name
) - 1);
377 pass
.uni_name_len
= uni_name_len
;
379 /* last change time */
380 pass
.mod_time
= time(NULL
);
382 /* password of the trust */
383 pass
.pass_len
= strlen(pwd
);
384 fstrcpy(pass
.pass
, pwd
);
387 sid_copy(&pass
.domain_sid
, &sid
);
389 pass_len
= tdb_trusted_dom_pass_pack(pass_buf
, pass_buf_len
, &pass
);
391 return secrets_store(trustdom_keystr(domain
), (void *)&pass_buf
, pass_len
);
394 /************************************************************************
395 Routine to set the plaintext machine account password for a realm
396 the password is assumed to be a null terminated ascii string
397 ************************************************************************/
399 BOOL
secrets_store_machine_password(const char *pass
, const char *domain
, uint32 sec_channel
)
403 uint32 last_change_time
;
404 uint32 sec_channel_type
;
406 asprintf(&key
, "%s/%s", SECRETS_MACHINE_PASSWORD
, domain
);
411 ret
= secrets_store(key
, pass
, strlen(pass
)+1);
417 asprintf(&key
, "%s/%s", SECRETS_MACHINE_LAST_CHANGE_TIME
, domain
);
422 SIVAL(&last_change_time
, 0, time(NULL
));
423 ret
= secrets_store(key
, &last_change_time
, sizeof(last_change_time
));
426 asprintf(&key
, "%s/%s", SECRETS_MACHINE_SEC_CHANNEL_TYPE
, domain
);
431 SIVAL(&sec_channel_type
, 0, sec_channel
);
432 ret
= secrets_store(key
, &sec_channel_type
, sizeof(sec_channel_type
));
439 /************************************************************************
440 Routine to fetch the plaintext machine account password for a realm
441 the password is assumed to be a null terminated ascii string
442 ************************************************************************/
443 char *secrets_fetch_machine_password(const char *domain
,
444 time_t *pass_last_set_time
,
449 asprintf(&key
, "%s/%s", SECRETS_MACHINE_PASSWORD
, domain
);
451 ret
= (char *)secrets_fetch(key
, NULL
);
454 if (pass_last_set_time
) {
456 uint32
*last_set_time
;
457 asprintf(&key
, "%s/%s", SECRETS_MACHINE_LAST_CHANGE_TIME
, domain
);
459 last_set_time
= secrets_fetch(key
, &size
);
461 *pass_last_set_time
= IVAL(last_set_time
,0);
462 SAFE_FREE(last_set_time
);
464 *pass_last_set_time
= 0;
471 uint32
*channel_type
;
472 asprintf(&key
, "%s/%s", SECRETS_MACHINE_SEC_CHANNEL_TYPE
, domain
);
474 channel_type
= secrets_fetch(key
, &size
);
476 *channel
= IVAL(channel_type
,0);
477 SAFE_FREE(channel_type
);
479 *channel
= get_default_sec_channel();
489 /************************************************************************
490 Routine to delete the machine trust account password file for a domain.
491 ************************************************************************/
493 BOOL
trust_password_delete(const char *domain
)
495 return secrets_delete(trust_keystr(domain
));
498 /************************************************************************
499 Routine to delete the password for trusted domain
500 ************************************************************************/
502 BOOL
trusted_domain_password_delete(const char *domain
)
504 return secrets_delete(trustdom_keystr(domain
));
508 /*******************************************************************
509 Reset the 'done' variables so after a client process is created
510 from a fork call these calls will be re-done. This should be
511 expanded if more variables need reseting.
512 ******************************************************************/
514 void reset_globals_after_fork(void)
521 * Increment the global seed value to ensure every smbd starts
522 * with a new random seed.
526 uint32 initial_val
= sys_getpid();
527 tdb_change_int32_atomic(tdb
, "INFO/random_seed", (int *)&initial_val
, 1);
528 set_rand_reseed_data((unsigned char *)&initial_val
, sizeof(initial_val
));
532 * Re-seed the random crypto generator, so all smbd's
533 * started from the same parent won't generate the same
536 generate_random_buffer( &dummy
, 1, True
);
539 BOOL
secrets_store_ldap_pw(const char* dn
, char* pw
)
544 if (asprintf(&key
, "%s/%s", SECRETS_LDAP_BIND_PW
, dn
) < 0) {
545 DEBUG(0, ("secrets_store_ldap_pw: asprintf failed!\n"));
549 ret
= secrets_store(key
, pw
, strlen(pw
)+1);
557 * Get trusted domains info from secrets.tdb.
559 * The linked list is allocated on the supplied talloc context, caller gets to destroy
562 * @param ctx Allocation context
563 * @param enum_ctx Starting index, eg. we can start fetching at third
564 * or sixth trusted domain entry. Zero is the first index.
565 * Value it is set to is the enum context for the next enumeration.
566 * @param num_domains Number of domain entries to fetch at one call
567 * @param domains Pointer to array of trusted domain structs to be filled up
569 * @return nt status code of rpc response
572 NTSTATUS
secrets_get_trusted_domains(TALLOC_CTX
* ctx
, int* enum_ctx
, unsigned int max_num_domains
, int *num_domains
, TRUSTDOM
***domains
)
574 TDB_LIST_NODE
*keys
, *k
;
575 TRUSTDOM
*dom
= NULL
;
577 unsigned int start_idx
;
579 size_t size
, packed_size
= 0;
582 struct trusted_dom_pass
*pass
= talloc_zero(ctx
, sizeof(struct trusted_dom_pass
));
585 if (!secrets_init()) return NT_STATUS_ACCESS_DENIED
;
588 DEBUG(0, ("talloc_zero failed!\n"));
589 return NT_STATUS_NO_MEMORY
;
593 start_idx
= *enum_ctx
;
595 /* generate searching pattern */
596 if (!(pattern
= talloc_asprintf(ctx
, "%s/*", SECRETS_DOMTRUST_ACCT_PASS
))) {
597 DEBUG(0, ("secrets_get_trusted_domains: talloc_asprintf() failed!\n"));
598 return NT_STATUS_NO_MEMORY
;
601 DEBUG(5, ("secrets_get_trusted_domains: looking for %d domains, starting at index %d\n",
602 max_num_domains
, *enum_ctx
));
604 *domains
= talloc_zero(ctx
, sizeof(**domains
)*max_num_domains
);
606 /* fetching trusted domains' data and collecting them in a list */
607 keys
= tdb_search_keys(tdb
, pattern
);
610 * if there's no keys returned ie. no trusted domain,
611 * return "no more entries" code
613 status
= NT_STATUS_NO_MORE_ENTRIES
;
615 /* searching for keys in secrets db -- way to go ... */
616 for (k
= keys
; k
; k
= k
->next
) {
619 /* important: ensure null-termination of the key string */
620 secrets_key
= strndup(k
->node_key
.dptr
, k
->node_key
.dsize
);
622 DEBUG(0, ("strndup failed!\n"));
623 return NT_STATUS_NO_MEMORY
;
626 packed_pass
= secrets_fetch(secrets_key
, &size
);
627 packed_size
= tdb_trusted_dom_pass_unpack(packed_pass
, size
, pass
);
628 /* packed representation isn't needed anymore */
629 SAFE_FREE(packed_pass
);
631 if (size
!= packed_size
) {
632 DEBUG(2, ("Secrets record %s is invalid!\n", secrets_key
));
636 pull_ucs2_fstring(dom_name
, pass
->uni_name
);
637 DEBUG(18, ("Fetched secret record num %d.\nDomain name: %s, SID: %s\n",
638 idx
, dom_name
, sid_string_static(&pass
->domain_sid
)));
640 SAFE_FREE(secrets_key
);
642 if (idx
>= start_idx
&& idx
< start_idx
+ max_num_domains
) {
643 dom
= talloc_zero(ctx
, sizeof(*dom
));
645 /* free returned tdb record */
646 return NT_STATUS_NO_MEMORY
;
649 /* copy domain sid */
650 SMB_ASSERT(sizeof(dom
->sid
) == sizeof(pass
->domain_sid
));
651 memcpy(&(dom
->sid
), &(pass
->domain_sid
), sizeof(dom
->sid
));
653 /* copy unicode domain name */
654 dom
->name
= talloc_strdup_w(ctx
, pass
->uni_name
);
656 (*domains
)[idx
- start_idx
] = dom
;
658 DEBUG(18, ("Secret record is in required range.\n \
659 start_idx = %d, max_num_domains = %d. Added to returned array.\n",
660 start_idx
, max_num_domains
));
665 /* set proper status code to return */
667 /* there are yet some entries to enumerate */
668 status
= STATUS_MORE_ENTRIES
;
670 /* this is the last entry in the whole enumeration */
671 status
= NT_STATUS_OK
;
674 DEBUG(18, ("Secret is outside the required range.\n \
675 start_idx = %d, max_num_domains = %d. Not added to returned array\n",
676 start_idx
, max_num_domains
));
682 DEBUG(5, ("secrets_get_trusted_domains: got %d domains\n", *num_domains
));
684 /* free the results of searching the keys */
685 tdb_search_list_free(keys
);
690 /*******************************************************************************
691 Lock the secrets tdb based on a string - this is used as a primitive form of mutex
692 between smbd instances.
693 *******************************************************************************/
695 BOOL
secrets_named_mutex(const char *name
, unsigned int timeout
)
702 ret
= tdb_lock_bystring(tdb
, name
, timeout
);
704 DEBUG(10,("secrets_named_mutex: got mutex for %s\n", name
));
709 /*******************************************************************************
710 Unlock a named mutex.
711 *******************************************************************************/
713 void secrets_named_mutex_release(const char *name
)
715 tdb_unlock_bystring(tdb
, name
);
716 DEBUG(10,("secrets_named_mutex: released mutex for %s\n", name
));
719 /*********************************************************
720 Check to see if we must talk to the PDC to avoid sam
722 ********************************************************/
724 BOOL
must_use_pdc( const char *domain
)
726 time_t now
= time(NULL
);
727 time_t last_change_time
;
728 unsigned char passwd
[16];
730 if ( !secrets_fetch_trust_account_password(domain
, passwd
, &last_change_time
, NULL
) )
734 * If the time the machine password has changed
735 * was less than about 15 minutes then we need to contact
736 * the PDC only, as we cannot be sure domain replication
737 * has yet taken place. Bug found by Gerald (way to go
741 if ( now
- last_change_time
< SAM_SYNC_WINDOW
)
748 /*******************************************************************************
749 Store a complete AFS keyfile into secrets.tdb.
750 *******************************************************************************/
752 BOOL
secrets_store_afs_keyfile(const char *cell
, const struct afs_keyfile
*keyfile
)
756 if ((cell
== NULL
) || (keyfile
== NULL
))
759 if (ntohl(keyfile
->nkeys
) > SECRETS_AFS_MAXKEYS
)
762 slprintf(key
, sizeof(key
)-1, "%s/%s", SECRETS_AFS_KEYFILE
, cell
);
763 return secrets_store(key
, keyfile
, sizeof(struct afs_keyfile
));
766 /*******************************************************************************
767 Fetch the current (highest) AFS key from secrets.tdb
768 *******************************************************************************/
769 BOOL
secrets_fetch_afs_key(const char *cell
, struct afs_key
*result
)
772 struct afs_keyfile
*keyfile
;
776 slprintf(key
, sizeof(key
)-1, "%s/%s", SECRETS_AFS_KEYFILE
, cell
);
778 keyfile
= (struct afs_keyfile
*)secrets_fetch(key
, &size
);
783 if (size
!= sizeof(struct afs_keyfile
)) {
788 i
= ntohl(keyfile
->nkeys
);
790 if (i
> SECRETS_AFS_MAXKEYS
) {
795 *result
= keyfile
->entry
[i
-1];
797 result
->kvno
= ntohl(result
->kvno
);
802 /******************************************************************************
803 When kerberos is not available, choose between anonymous or
804 authenticated connections.
806 We need to use an authenticated connection if DCs have the
807 RestrictAnonymous registry entry set > 0, or the "Additional
808 restrictions for anonymous connections" set in the win2k Local
811 Caller to free() result in domain, username, password
812 *******************************************************************************/
813 void secrets_fetch_ipc_userpass(char **username
, char **domain
, char **password
)
815 *username
= secrets_fetch(SECRETS_AUTH_USER
, NULL
);
816 *domain
= secrets_fetch(SECRETS_AUTH_DOMAIN
, NULL
);
817 *password
= secrets_fetch(SECRETS_AUTH_PASSWORD
, NULL
);
819 if (*username
&& **username
) {
821 if (!*domain
|| !**domain
)
822 *domain
= smb_xstrdup(lp_workgroup());
824 if (!*password
|| !**password
)
825 *password
= smb_xstrdup("");
827 DEBUG(3, ("IPC$ connections done by user %s\\%s\n",
828 *domain
, *username
));
831 DEBUG(3, ("IPC$ connections done anonymously\n"));
832 *username
= smb_xstrdup("");
833 *domain
= smb_xstrdup("");
834 *password
= smb_xstrdup("");