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 3 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, see <http://www.gnu.org/licenses/>.
22 /* the Samba secrets database stores any generated, private information
23 such as the local SID and machine trust password */
26 #include "../libcli/auth/libcli_auth.h"
27 #include "librpc/gen_ndr/ndr_secrets.h"
30 #include "../libcli/security/security.h"
33 #define DBGC_CLASS DBGC_PASSDB
35 static struct db_context
*db_ctx
;
38 * Use a TDB to store an incrementing random seed.
40 * Initialised to the current pid, the very first time Samba starts,
41 * and incremented by one each time it is needed.
43 * @note Not called by systems with a working /dev/urandom.
45 static void get_rand_seed(void *userdata
, int *new_seed
)
47 *new_seed
= sys_getpid();
49 dbwrap_trans_change_int32_atomic(db_ctx
, "INFO/random_seed",
54 /* open up the secrets database */
55 bool secrets_init(void)
63 fname
= talloc_asprintf(talloc_tos(), "%s/secrets.tdb",
69 db_ctx
= db_open(NULL
, fname
, 0,
70 TDB_DEFAULT
, O_RDWR
|O_CREAT
, 0600);
73 DEBUG(0,("Failed to open %s\n", fname
));
81 * Set a reseed function for the crypto random generator
83 * This avoids a problem where systems without /dev/urandom
84 * could send the same challenge to multiple clients
86 set_rand_reseed_callback(get_rand_seed
, NULL
);
88 /* Ensure that the reseed is done now, while we are root, etc */
89 generate_random_buffer(&dummy
, sizeof(dummy
));
94 struct db_context
*secrets_db_ctx(void)
96 if (!secrets_init()) {
106 void secrets_shutdown(void)
111 /* read a entry from the secrets database - the caller must free the result
112 if size is non-null then the size of the entry is put in there
114 void *secrets_fetch(const char *key
, size_t *size
)
119 if (!secrets_init()) {
123 if (db_ctx
->fetch(db_ctx
, talloc_tos(), string_tdb_data(key
),
128 result
= memdup(dbuf
.dptr
, dbuf
.dsize
);
129 if (result
== NULL
) {
132 TALLOC_FREE(dbuf
.dptr
);
141 /* store a secrets entry
143 bool secrets_store(const char *key
, const void *data
, size_t size
)
147 if (!secrets_init()) {
151 status
= dbwrap_trans_store(db_ctx
, string_tdb_data(key
),
152 make_tdb_data((const uint8
*)data
, size
),
154 return NT_STATUS_IS_OK(status
);
158 /* delete a secets database entry
160 bool secrets_delete(const char *key
)
163 if (!secrets_init()) {
167 status
= dbwrap_trans_delete(db_ctx
, string_tdb_data(key
));
169 return NT_STATUS_IS_OK(status
);
172 bool secrets_store_local_schannel_key(uint8_t schannel_key
[16])
174 return secrets_store(SECRETS_LOCAL_SCHANNEL_KEY
, schannel_key
, 16);
177 bool secrets_fetch_local_schannel_key(uint8_t schannel_key
[16])
182 key
= (uint8_t *)secrets_fetch(SECRETS_LOCAL_SCHANNEL_KEY
, &size
);
192 memcpy(schannel_key
, key
, 16);
198 * Form a key for fetching a trusted domain password
200 * @param domain trusted domain name
202 * @return stored password's key
204 static char *trustdom_keystr(const char *domain
)
208 keystr
= talloc_asprintf_strupper_m(talloc_tos(), "%s/%s",
209 SECRETS_DOMTRUST_ACCT_PASS
,
211 SMB_ASSERT(keystr
!= NULL
);
215 /************************************************************************
216 Routine to get account password to trusted domain
217 ************************************************************************/
219 bool secrets_fetch_trusted_domain_password(const char *domain
, char** pwd
,
220 struct dom_sid
*sid
, time_t *pass_last_set_time
)
222 struct TRUSTED_DOM_PASS pass
;
223 enum ndr_err_code ndr_err
;
225 /* unpacking structures */
228 /* fetching trusted domain password structure */
229 if (!(blob
.data
= (uint8_t *)secrets_fetch(trustdom_keystr(domain
),
231 DEBUG(5, ("secrets_fetch failed!\n"));
235 /* unpack trusted domain password */
236 ndr_err
= ndr_pull_struct_blob(&blob
, talloc_tos(), &pass
,
237 (ndr_pull_flags_fn_t
)ndr_pull_TRUSTED_DOM_PASS
);
238 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
242 SAFE_FREE(blob
.data
);
244 /* the trust's password */
246 *pwd
= SMB_STRDUP(pass
.pass
);
252 /* last change time */
253 if (pass_last_set_time
) *pass_last_set_time
= pass
.mod_time
;
256 if (sid
!= NULL
) sid_copy(sid
, &pass
.domain_sid
);
262 * Routine to store the password for trusted domain
264 * @param domain remote domain name
265 * @param pwd plain text password of trust relationship
266 * @param sid remote domain sid
268 * @return true if succeeded
271 bool secrets_store_trusted_domain_password(const char* domain
, const char* pwd
,
272 const struct dom_sid
*sid
)
276 /* packing structures */
278 enum ndr_err_code ndr_err
;
279 struct TRUSTED_DOM_PASS pass
;
282 pass
.uni_name
= domain
;
283 pass
.uni_name_len
= strlen(domain
)+1;
285 /* last change time */
286 pass
.mod_time
= time(NULL
);
288 /* password of the trust */
289 pass
.pass_len
= strlen(pwd
);
293 sid_copy(&pass
.domain_sid
, sid
);
295 ndr_err
= ndr_push_struct_blob(&blob
, talloc_tos(), &pass
,
296 (ndr_push_flags_fn_t
)ndr_push_TRUSTED_DOM_PASS
);
297 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
301 ret
= secrets_store(trustdom_keystr(domain
), blob
.data
, blob
.length
);
303 data_blob_free(&blob
);
308 /************************************************************************
309 Routine to delete the password for trusted domain
310 ************************************************************************/
312 bool trusted_domain_password_delete(const char *domain
)
314 return secrets_delete(trustdom_keystr(domain
));
317 bool secrets_store_ldap_pw(const char* dn
, char* pw
)
322 if (asprintf(&key
, "%s/%s", SECRETS_LDAP_BIND_PW
, dn
) < 0) {
323 DEBUG(0, ("secrets_store_ldap_pw: asprintf failed!\n"));
327 ret
= secrets_store(key
, pw
, strlen(pw
)+1);
333 /*******************************************************************
334 Find the ldap password.
335 ******************************************************************/
337 bool fetch_ldap_pw(char **dn
, char** pw
)
342 *dn
= smb_xstrdup(lp_ldap_admin_dn());
344 if (asprintf(&key
, "%s/%s", SECRETS_LDAP_BIND_PW
, *dn
) < 0) {
346 DEBUG(0, ("fetch_ldap_pw: asprintf failed!\n"));
350 *pw
=(char *)secrets_fetch(key
, &size
);
354 /* Upgrade 2.2 style entry */
356 char* old_style_key
= SMB_STRDUP(*dn
);
358 fstring old_style_pw
;
360 if (!old_style_key
) {
361 DEBUG(0, ("fetch_ldap_pw: strdup failed!\n"));
365 for (p
=old_style_key
; *p
; p
++)
366 if (*p
== ',') *p
= '/';
368 data
=(char *)secrets_fetch(old_style_key
, &size
);
369 if ((data
== NULL
) || (size
< sizeof(old_style_pw
))) {
370 DEBUG(0,("fetch_ldap_pw: neither ldap secret retrieved!\n"));
371 SAFE_FREE(old_style_key
);
377 size
= MIN(size
, sizeof(fstring
)-1);
378 strncpy(old_style_pw
, data
, size
);
379 old_style_pw
[size
] = 0;
383 if (!secrets_store_ldap_pw(*dn
, old_style_pw
)) {
384 DEBUG(0,("fetch_ldap_pw: ldap secret could not be upgraded!\n"));
385 SAFE_FREE(old_style_key
);
389 if (!secrets_delete(old_style_key
)) {
390 DEBUG(0,("fetch_ldap_pw: old ldap secret could not be deleted!\n"));
393 SAFE_FREE(old_style_key
);
395 *pw
= smb_xstrdup(old_style_pw
);
402 * Get trusted domains info from secrets.tdb.
405 struct list_trusted_domains_state
{
407 struct trustdom_info
**domains
;
410 static int list_trusted_domain(struct db_record
*rec
, void *private_data
)
412 const size_t prefix_len
= strlen(SECRETS_DOMTRUST_ACCT_PASS
);
413 struct TRUSTED_DOM_PASS pass
;
414 enum ndr_err_code ndr_err
;
416 struct trustdom_info
*dom_info
;
418 struct list_trusted_domains_state
*state
=
419 (struct list_trusted_domains_state
*)private_data
;
421 if ((rec
->key
.dsize
< prefix_len
)
422 || (strncmp((char *)rec
->key
.dptr
, SECRETS_DOMTRUST_ACCT_PASS
,
427 blob
= data_blob_const(rec
->value
.dptr
, rec
->value
.dsize
);
429 ndr_err
= ndr_pull_struct_blob(&blob
, talloc_tos(), &pass
,
430 (ndr_pull_flags_fn_t
)ndr_pull_TRUSTED_DOM_PASS
);
431 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
435 if (pass
.domain_sid
.num_auths
!= 4) {
436 DEBUG(0, ("SID %s is not a domain sid, has %d "
437 "auths instead of 4\n",
438 sid_string_dbg(&pass
.domain_sid
),
439 pass
.domain_sid
.num_auths
));
443 if (!(dom_info
= TALLOC_P(state
->domains
, struct trustdom_info
))) {
444 DEBUG(0, ("talloc failed\n"));
448 dom_info
->name
= talloc_strdup(dom_info
, pass
.uni_name
);
449 if (!dom_info
->name
) {
450 TALLOC_FREE(dom_info
);
454 sid_copy(&dom_info
->sid
, &pass
.domain_sid
);
456 ADD_TO_ARRAY(state
->domains
, struct trustdom_info
*, dom_info
,
457 &state
->domains
, &state
->num_domains
);
459 if (state
->domains
== NULL
) {
460 state
->num_domains
= 0;
466 NTSTATUS
secrets_trusted_domains(TALLOC_CTX
*mem_ctx
, uint32
*num_domains
,
467 struct trustdom_info
***domains
)
469 struct list_trusted_domains_state state
;
473 if (db_ctx
== NULL
) {
474 return NT_STATUS_ACCESS_DENIED
;
477 state
.num_domains
= 0;
480 * Make sure that a talloc context for the trustdom_info structs
484 if (!(state
.domains
= TALLOC_ARRAY(
485 mem_ctx
, struct trustdom_info
*, 1))) {
486 return NT_STATUS_NO_MEMORY
;
489 db_ctx
->traverse_read(db_ctx
, list_trusted_domain
, (void *)&state
);
491 *num_domains
= state
.num_domains
;
492 *domains
= state
.domains
;
496 /*******************************************************************************
497 Store a complete AFS keyfile into secrets.tdb.
498 *******************************************************************************/
500 bool secrets_store_afs_keyfile(const char *cell
, const struct afs_keyfile
*keyfile
)
504 if ((cell
== NULL
) || (keyfile
== NULL
))
507 if (ntohl(keyfile
->nkeys
) > SECRETS_AFS_MAXKEYS
)
510 slprintf(key
, sizeof(key
)-1, "%s/%s", SECRETS_AFS_KEYFILE
, cell
);
511 return secrets_store(key
, keyfile
, sizeof(struct afs_keyfile
));
514 /*******************************************************************************
515 Fetch the current (highest) AFS key from secrets.tdb
516 *******************************************************************************/
517 bool secrets_fetch_afs_key(const char *cell
, struct afs_key
*result
)
520 struct afs_keyfile
*keyfile
;
524 slprintf(key
, sizeof(key
)-1, "%s/%s", SECRETS_AFS_KEYFILE
, cell
);
526 keyfile
= (struct afs_keyfile
*)secrets_fetch(key
, &size
);
531 if (size
!= sizeof(struct afs_keyfile
)) {
536 i
= ntohl(keyfile
->nkeys
);
538 if (i
> SECRETS_AFS_MAXKEYS
) {
543 *result
= keyfile
->entry
[i
-1];
545 result
->kvno
= ntohl(result
->kvno
);
552 /******************************************************************************
553 When kerberos is not available, choose between anonymous or
554 authenticated connections.
556 We need to use an authenticated connection if DCs have the
557 RestrictAnonymous registry entry set > 0, or the "Additional
558 restrictions for anonymous connections" set in the win2k Local
561 Caller to free() result in domain, username, password
562 *******************************************************************************/
563 void secrets_fetch_ipc_userpass(char **username
, char **domain
, char **password
)
565 *username
= (char *)secrets_fetch(SECRETS_AUTH_USER
, NULL
);
566 *domain
= (char *)secrets_fetch(SECRETS_AUTH_DOMAIN
, NULL
);
567 *password
= (char *)secrets_fetch(SECRETS_AUTH_PASSWORD
, NULL
);
569 if (*username
&& **username
) {
571 if (!*domain
|| !**domain
)
572 *domain
= smb_xstrdup(lp_workgroup());
574 if (!*password
|| !**password
)
575 *password
= smb_xstrdup("");
577 DEBUG(3, ("IPC$ connections done by user %s\\%s\n",
578 *domain
, *username
));
581 DEBUG(3, ("IPC$ connections done anonymously\n"));
582 *username
= smb_xstrdup("");
583 *domain
= smb_xstrdup("");
584 *password
= smb_xstrdup("");
588 bool secrets_store_generic(const char *owner
, const char *key
, const char *secret
)
593 if (asprintf(&tdbkey
, "SECRETS/GENERIC/%s/%s", owner
, key
) < 0) {
594 DEBUG(0, ("asprintf failed!\n"));
598 ret
= secrets_store(tdbkey
, secret
, strlen(secret
)+1);
604 bool secrets_delete_generic(const char *owner
, const char *key
)
609 if (asprintf(&tdbkey
, "SECRETS/GENERIC/%s/%s", owner
, key
) < 0) {
610 DEBUG(0, ("asprintf failed!\n"));
614 ret
= secrets_delete(tdbkey
);
620 /*******************************************************************
621 Find the ldap password.
622 ******************************************************************/
624 char *secrets_fetch_generic(const char *owner
, const char *key
)
629 if (( ! owner
) || ( ! key
)) {
630 DEBUG(1, ("Invalid Parameters"));
634 if (asprintf(&tdbkey
, "SECRETS/GENERIC/%s/%s", owner
, key
) < 0) {
635 DEBUG(0, ("Out of memory!\n"));
639 secret
= (char *)secrets_fetch(tdbkey
, NULL
);