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 "system/filesys.h"
28 #include "../libcli/auth/libcli_auth.h"
29 #include "librpc/gen_ndr/ndr_secrets.h"
31 #include "dbwrap/dbwrap.h"
32 #include "dbwrap/dbwrap_open.h"
33 #include "../libcli/security/security.h"
37 #define DBGC_CLASS DBGC_PASSDB
39 static struct db_context
*db_ctx
;
42 * Use a TDB to store an incrementing random seed.
44 * Initialised to the current pid, the very first time Samba starts,
45 * and incremented by one each time it is needed.
47 * @note Not called by systems with a working /dev/urandom.
49 static void get_rand_seed(void *userdata
, int *new_seed
)
51 *new_seed
= sys_getpid();
53 dbwrap_trans_change_int32_atomic(db_ctx
, "INFO/random_seed",
58 /* open up the secrets database with specified private_dir path */
59 bool secrets_init_path(const char *private_dir
)
68 if (private_dir
== NULL
) {
72 fname
= talloc_asprintf(talloc_tos(), "%s/secrets.tdb",
78 db_ctx
= db_open(NULL
, fname
, 0,
79 TDB_DEFAULT
, O_RDWR
|O_CREAT
, 0600);
82 DEBUG(0,("Failed to open %s\n", fname
));
89 * Set a reseed function for the crypto random generator
91 * This avoids a problem where systems without /dev/urandom
92 * could send the same challenge to multiple clients
94 set_rand_reseed_callback(get_rand_seed
, NULL
);
96 /* Ensure that the reseed is done now, while we are root, etc */
97 generate_random_buffer(&dummy
, sizeof(dummy
));
102 /* open up the secrets database */
103 bool secrets_init(void)
105 return secrets_init_path(lp_private_dir());
108 struct db_context
*secrets_db_ctx(void)
110 if (!secrets_init()) {
120 void secrets_shutdown(void)
125 /* read a entry from the secrets database - the caller must free the result
126 if size is non-null then the size of the entry is put in there
128 void *secrets_fetch(const char *key
, size_t *size
)
133 if (!secrets_init()) {
137 if (db_ctx
->fetch(db_ctx
, talloc_tos(), string_tdb_data(key
),
142 result
= memdup(dbuf
.dptr
, dbuf
.dsize
);
143 if (result
== NULL
) {
146 TALLOC_FREE(dbuf
.dptr
);
155 /* store a secrets entry
157 bool secrets_store(const char *key
, const void *data
, size_t size
)
161 if (!secrets_init()) {
165 status
= dbwrap_trans_store(db_ctx
, string_tdb_data(key
),
166 make_tdb_data((const uint8
*)data
, size
),
168 return NT_STATUS_IS_OK(status
);
172 /* delete a secets database entry
174 bool secrets_delete(const char *key
)
177 if (!secrets_init()) {
181 status
= dbwrap_trans_delete(db_ctx
, string_tdb_data(key
));
183 return NT_STATUS_IS_OK(status
);
187 * Form a key for fetching a trusted domain password
189 * @param domain trusted domain name
191 * @return stored password's key
193 static char *trustdom_keystr(const char *domain
)
197 keystr
= talloc_asprintf_strupper_m(talloc_tos(), "%s/%s",
198 SECRETS_DOMTRUST_ACCT_PASS
,
200 SMB_ASSERT(keystr
!= NULL
);
204 /************************************************************************
205 Routine to get account password to trusted domain
206 ************************************************************************/
208 bool secrets_fetch_trusted_domain_password(const char *domain
, char** pwd
,
209 struct dom_sid
*sid
, time_t *pass_last_set_time
)
211 struct TRUSTED_DOM_PASS pass
;
212 enum ndr_err_code ndr_err
;
214 /* unpacking structures */
217 /* fetching trusted domain password structure */
218 if (!(blob
.data
= (uint8_t *)secrets_fetch(trustdom_keystr(domain
),
220 DEBUG(5, ("secrets_fetch failed!\n"));
224 /* unpack trusted domain password */
225 ndr_err
= ndr_pull_struct_blob(&blob
, talloc_tos(), &pass
,
226 (ndr_pull_flags_fn_t
)ndr_pull_TRUSTED_DOM_PASS
);
228 SAFE_FREE(blob
.data
);
230 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
235 /* the trust's password */
237 *pwd
= SMB_STRDUP(pass
.pass
);
243 /* last change time */
244 if (pass_last_set_time
) *pass_last_set_time
= pass
.mod_time
;
247 if (sid
!= NULL
) sid_copy(sid
, &pass
.domain_sid
);
253 * Routine to store the password for trusted domain
255 * @param domain remote domain name
256 * @param pwd plain text password of trust relationship
257 * @param sid remote domain sid
259 * @return true if succeeded
262 bool secrets_store_trusted_domain_password(const char* domain
, const char* pwd
,
263 const struct dom_sid
*sid
)
267 /* packing structures */
269 enum ndr_err_code ndr_err
;
270 struct TRUSTED_DOM_PASS pass
;
273 pass
.uni_name
= domain
;
274 pass
.uni_name_len
= strlen(domain
)+1;
276 /* last change time */
277 pass
.mod_time
= time(NULL
);
279 /* password of the trust */
280 pass
.pass_len
= strlen(pwd
);
284 sid_copy(&pass
.domain_sid
, sid
);
286 ndr_err
= ndr_push_struct_blob(&blob
, talloc_tos(), &pass
,
287 (ndr_push_flags_fn_t
)ndr_push_TRUSTED_DOM_PASS
);
288 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
292 ret
= secrets_store(trustdom_keystr(domain
), blob
.data
, blob
.length
);
294 data_blob_free(&blob
);
299 /************************************************************************
300 Routine to delete the password for trusted domain
301 ************************************************************************/
303 bool trusted_domain_password_delete(const char *domain
)
305 return secrets_delete(trustdom_keystr(domain
));
308 bool secrets_store_ldap_pw(const char* dn
, char* pw
)
313 if (asprintf(&key
, "%s/%s", SECRETS_LDAP_BIND_PW
, dn
) < 0) {
314 DEBUG(0, ("secrets_store_ldap_pw: asprintf failed!\n"));
318 ret
= secrets_store(key
, pw
, strlen(pw
)+1);
324 /*******************************************************************
325 Find the ldap password.
326 ******************************************************************/
328 bool fetch_ldap_pw(char **dn
, char** pw
)
333 *dn
= smb_xstrdup(lp_ldap_admin_dn());
335 if (asprintf(&key
, "%s/%s", SECRETS_LDAP_BIND_PW
, *dn
) < 0) {
337 DEBUG(0, ("fetch_ldap_pw: asprintf failed!\n"));
341 *pw
=(char *)secrets_fetch(key
, &size
);
345 /* Upgrade 2.2 style entry */
347 char* old_style_key
= SMB_STRDUP(*dn
);
349 fstring old_style_pw
;
351 if (!old_style_key
) {
352 DEBUG(0, ("fetch_ldap_pw: strdup failed!\n"));
356 for (p
=old_style_key
; *p
; p
++)
357 if (*p
== ',') *p
= '/';
359 data
=(char *)secrets_fetch(old_style_key
, &size
);
360 if ((data
== NULL
) || (size
< sizeof(old_style_pw
))) {
361 DEBUG(0,("fetch_ldap_pw: neither ldap secret retrieved!\n"));
362 SAFE_FREE(old_style_key
);
368 size
= MIN(size
, sizeof(fstring
)-1);
369 strncpy(old_style_pw
, data
, size
);
370 old_style_pw
[size
] = 0;
374 if (!secrets_store_ldap_pw(*dn
, old_style_pw
)) {
375 DEBUG(0,("fetch_ldap_pw: ldap secret could not be upgraded!\n"));
376 SAFE_FREE(old_style_key
);
380 if (!secrets_delete(old_style_key
)) {
381 DEBUG(0,("fetch_ldap_pw: old ldap secret could not be deleted!\n"));
384 SAFE_FREE(old_style_key
);
386 *pw
= smb_xstrdup(old_style_pw
);
393 * Get trusted domains info from secrets.tdb.
396 struct list_trusted_domains_state
{
398 struct trustdom_info
**domains
;
401 static int list_trusted_domain(struct db_record
*rec
, void *private_data
)
403 const size_t prefix_len
= strlen(SECRETS_DOMTRUST_ACCT_PASS
);
404 struct TRUSTED_DOM_PASS pass
;
405 enum ndr_err_code ndr_err
;
407 struct trustdom_info
*dom_info
;
409 struct list_trusted_domains_state
*state
=
410 (struct list_trusted_domains_state
*)private_data
;
412 if ((rec
->key
.dsize
< prefix_len
)
413 || (strncmp((char *)rec
->key
.dptr
, SECRETS_DOMTRUST_ACCT_PASS
,
418 blob
= data_blob_const(rec
->value
.dptr
, rec
->value
.dsize
);
420 ndr_err
= ndr_pull_struct_blob(&blob
, talloc_tos(), &pass
,
421 (ndr_pull_flags_fn_t
)ndr_pull_TRUSTED_DOM_PASS
);
422 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
426 if (pass
.domain_sid
.num_auths
!= 4) {
427 DEBUG(0, ("SID %s is not a domain sid, has %d "
428 "auths instead of 4\n",
429 sid_string_dbg(&pass
.domain_sid
),
430 pass
.domain_sid
.num_auths
));
434 if (!(dom_info
= talloc(state
->domains
, struct trustdom_info
))) {
435 DEBUG(0, ("talloc failed\n"));
439 dom_info
->name
= talloc_strdup(dom_info
, pass
.uni_name
);
440 if (!dom_info
->name
) {
441 TALLOC_FREE(dom_info
);
445 sid_copy(&dom_info
->sid
, &pass
.domain_sid
);
447 ADD_TO_ARRAY(state
->domains
, struct trustdom_info
*, dom_info
,
448 &state
->domains
, &state
->num_domains
);
450 if (state
->domains
== NULL
) {
451 state
->num_domains
= 0;
457 NTSTATUS
secrets_trusted_domains(TALLOC_CTX
*mem_ctx
, uint32
*num_domains
,
458 struct trustdom_info
***domains
)
460 struct list_trusted_domains_state state
;
462 if (!secrets_init()) {
463 return NT_STATUS_ACCESS_DENIED
;
466 state
.num_domains
= 0;
469 * Make sure that a talloc context for the trustdom_info structs
473 if (!(state
.domains
= talloc_array(
474 mem_ctx
, struct trustdom_info
*, 1))) {
475 return NT_STATUS_NO_MEMORY
;
478 db_ctx
->traverse_read(db_ctx
, list_trusted_domain
, (void *)&state
);
480 *num_domains
= state
.num_domains
;
481 *domains
= state
.domains
;
485 /*******************************************************************************
486 Store a complete AFS keyfile into secrets.tdb.
487 *******************************************************************************/
489 bool secrets_store_afs_keyfile(const char *cell
, const struct afs_keyfile
*keyfile
)
493 if ((cell
== NULL
) || (keyfile
== NULL
))
496 if (ntohl(keyfile
->nkeys
) > SECRETS_AFS_MAXKEYS
)
499 slprintf(key
, sizeof(key
)-1, "%s/%s", SECRETS_AFS_KEYFILE
, cell
);
500 return secrets_store(key
, keyfile
, sizeof(struct afs_keyfile
));
503 /*******************************************************************************
504 Fetch the current (highest) AFS key from secrets.tdb
505 *******************************************************************************/
506 bool secrets_fetch_afs_key(const char *cell
, struct afs_key
*result
)
509 struct afs_keyfile
*keyfile
;
513 slprintf(key
, sizeof(key
)-1, "%s/%s", SECRETS_AFS_KEYFILE
, cell
);
515 keyfile
= (struct afs_keyfile
*)secrets_fetch(key
, &size
);
520 if (size
!= sizeof(struct afs_keyfile
)) {
525 i
= ntohl(keyfile
->nkeys
);
527 if (i
> SECRETS_AFS_MAXKEYS
) {
532 *result
= keyfile
->entry
[i
-1];
534 result
->kvno
= ntohl(result
->kvno
);
541 /******************************************************************************
542 When kerberos is not available, choose between anonymous or
543 authenticated connections.
545 We need to use an authenticated connection if DCs have the
546 RestrictAnonymous registry entry set > 0, or the "Additional
547 restrictions for anonymous connections" set in the win2k Local
550 Caller to free() result in domain, username, password
551 *******************************************************************************/
552 void secrets_fetch_ipc_userpass(char **username
, char **domain
, char **password
)
554 *username
= (char *)secrets_fetch(SECRETS_AUTH_USER
, NULL
);
555 *domain
= (char *)secrets_fetch(SECRETS_AUTH_DOMAIN
, NULL
);
556 *password
= (char *)secrets_fetch(SECRETS_AUTH_PASSWORD
, NULL
);
558 if (*username
&& **username
) {
560 if (!*domain
|| !**domain
)
561 *domain
= smb_xstrdup(lp_workgroup());
563 if (!*password
|| !**password
)
564 *password
= smb_xstrdup("");
566 DEBUG(3, ("IPC$ connections done by user %s\\%s\n",
567 *domain
, *username
));
570 DEBUG(3, ("IPC$ connections done anonymously\n"));
571 *username
= smb_xstrdup("");
572 *domain
= smb_xstrdup("");
573 *password
= smb_xstrdup("");
577 bool secrets_store_generic(const char *owner
, const char *key
, const char *secret
)
582 if (asprintf(&tdbkey
, "SECRETS/GENERIC/%s/%s", owner
, key
) < 0) {
583 DEBUG(0, ("asprintf failed!\n"));
587 ret
= secrets_store(tdbkey
, secret
, strlen(secret
)+1);
593 bool secrets_delete_generic(const char *owner
, const char *key
)
598 if (asprintf(&tdbkey
, "SECRETS/GENERIC/%s/%s", owner
, key
) < 0) {
599 DEBUG(0, ("asprintf failed!\n"));
603 ret
= secrets_delete(tdbkey
);
609 /*******************************************************************
610 Find the ldap password.
611 ******************************************************************/
613 char *secrets_fetch_generic(const char *owner
, const char *key
)
618 if (( ! owner
) || ( ! key
)) {
619 DEBUG(1, ("Invalid Parameters"));
623 if (asprintf(&tdbkey
, "SECRETS/GENERIC/%s/%s", owner
, key
) < 0) {
624 DEBUG(0, ("Out of memory!\n"));
628 secret
= (char *)secrets_fetch(tdbkey
, NULL
);