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"
27 #include "../libcli/auth/libcli_auth.h"
28 #include "librpc/gen_ndr/ndr_secrets.h"
30 #include "dbwrap/dbwrap.h"
31 #include "dbwrap/dbwrap_open.h"
32 #include "../libcli/security/security.h"
34 #include "auth/credentials/credentials.h"
37 #define DBGC_CLASS DBGC_PASSDB
39 static struct db_context
*db_ctx
;
41 /* open up the secrets database with specified private_dir path */
42 bool secrets_init_path(const char *private_dir
)
51 if (private_dir
== NULL
) {
55 frame
= talloc_stackframe();
56 fname
= talloc_asprintf(frame
, "%s/secrets.tdb", private_dir
);
62 db_ctx
= db_open(NULL
, fname
, 0,
63 TDB_DEFAULT
, O_RDWR
|O_CREAT
, 0600,
64 DBWRAP_LOCK_ORDER_1
, DBWRAP_FLAG_NONE
);
67 DEBUG(0,("Failed to open %s\n", fname
));
76 /* open up the secrets database */
77 bool secrets_init(void)
79 return secrets_init_path(lp_private_dir());
82 struct db_context
*secrets_db_ctx(void)
84 if (!secrets_init()) {
94 void secrets_shutdown(void)
99 /* read a entry from the secrets database - the caller must free the result
100 if size is non-null then the size of the entry is put in there
102 void *secrets_fetch(const char *key
, size_t *size
)
108 if (!secrets_init()) {
112 status
= dbwrap_fetch(db_ctx
, talloc_tos(), string_tdb_data(key
),
114 if (!NT_STATUS_IS_OK(status
)) {
118 result
= smb_memdup(dbuf
.dptr
, dbuf
.dsize
);
119 if (result
== NULL
) {
123 * secrets_fetch() is a generic code and may be used for sensitive data,
124 * so clear the local dbuf.dptr memory via BURN_PTR_SIZE().
125 * The future plan is to convert secrets_fetch() to talloc.
126 * That would improve performance via:
127 * - avoid smb_memdup() above, instead directly return dbuf.dptr
128 * - BURN_PTR_SIZE() will be done not here but in the caller and only
129 * if the caller asks for sensitive data.
131 BURN_PTR_SIZE(dbuf
.dptr
, dbuf
.dsize
);
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_t *)data
, size
),
154 return NT_STATUS_IS_OK(status
);
157 bool secrets_store_creds(struct cli_credentials
*creds
)
159 const char *p
= NULL
;
162 p
= cli_credentials_get_username(creds
);
167 ok
= secrets_store(SECRETS_AUTH_USER
, p
, strlen(p
) + 1);
169 DBG_ERR("Failed storing auth user name\n");
174 p
= cli_credentials_get_domain(creds
);
179 ok
= secrets_store(SECRETS_AUTH_DOMAIN
, p
, strlen(p
) + 1);
181 DBG_ERR("Failed storing auth domain name\n");
186 p
= cli_credentials_get_password(creds
);
191 ok
= secrets_store(SECRETS_AUTH_PASSWORD
, p
, strlen(p
) + 1);
193 DBG_ERR("Failed storing auth password\n");
201 /* delete a secets database entry
203 bool secrets_delete_entry(const char *key
)
206 if (!secrets_init()) {
210 status
= dbwrap_trans_delete(db_ctx
, string_tdb_data(key
));
212 return NT_STATUS_IS_OK(status
);
216 * Deletes the key if it exists.
218 bool secrets_delete(const char *key
)
222 if (!secrets_init()) {
226 exists
= dbwrap_exists(db_ctx
, string_tdb_data(key
));
231 return secrets_delete_entry(key
);
235 * Form a key for fetching a trusted domain password
237 * @param domain trusted domain name
239 * @return stored password's key
241 static char *trustdom_keystr(const char *domain
)
245 keystr
= talloc_asprintf_strupper_m(talloc_tos(), "%s/%s",
246 SECRETS_DOMTRUST_ACCT_PASS
,
248 SMB_ASSERT(keystr
!= NULL
);
252 /************************************************************************
253 Routine to get account password to trusted domain
254 ************************************************************************/
256 bool secrets_fetch_trusted_domain_password(const char *domain
, char** pwd
,
257 struct dom_sid
*sid
, time_t *pass_last_set_time
)
259 struct TRUSTED_DOM_PASS pass
;
260 enum ndr_err_code ndr_err
;
262 /* unpacking structures */
265 /* fetching trusted domain password structure */
266 if (!(blob
.data
= (uint8_t *)secrets_fetch(trustdom_keystr(domain
),
268 DEBUG(5, ("secrets_fetch failed!\n"));
272 /* unpack trusted domain password */
273 ndr_err
= ndr_pull_struct_blob(&blob
, talloc_tos(), &pass
,
274 (ndr_pull_flags_fn_t
)ndr_pull_TRUSTED_DOM_PASS
);
276 /* This blob is NOT talloc based! */
277 BURN_FREE(blob
.data
, blob
.length
);
279 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
283 if (pass
.pass
!= NULL
) {
284 talloc_keep_secret(discard_const_p(char, pass
.pass
));
287 /* the trust's password */
289 *pwd
= SMB_STRDUP(pass
.pass
);
295 /* last change time */
296 if (pass_last_set_time
) *pass_last_set_time
= pass
.mod_time
;
299 if (sid
!= NULL
) sid_copy(sid
, &pass
.domain_sid
);
305 * Routine to store the password for trusted domain
307 * @param domain remote domain name
308 * @param pwd plain text password of trust relationship
309 * @param sid remote domain sid
311 * @return true if succeeded
314 bool secrets_store_trusted_domain_password(const char* domain
, const char* pwd
,
315 const struct dom_sid
*sid
)
319 /* packing structures */
321 enum ndr_err_code ndr_err
;
322 struct TRUSTED_DOM_PASS pass
;
325 pass
.uni_name
= domain
;
326 pass
.uni_name_len
= strlen(domain
)+1;
328 /* last change time */
329 pass
.mod_time
= time(NULL
);
331 /* password of the trust */
332 pass
.pass_len
= strlen(pwd
);
336 sid_copy(&pass
.domain_sid
, sid
);
338 ndr_err
= ndr_push_struct_blob(&blob
, talloc_tos(), &pass
,
339 (ndr_push_flags_fn_t
)ndr_push_TRUSTED_DOM_PASS
);
340 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
344 ret
= secrets_store(trustdom_keystr(domain
), blob
.data
, blob
.length
);
346 /* This blob is talloc based. */
347 data_blob_clear_free(&blob
);
352 /************************************************************************
353 Routine to delete the password for trusted domain
354 ************************************************************************/
356 bool trusted_domain_password_delete(const char *domain
)
358 return secrets_delete_entry(trustdom_keystr(domain
));
361 bool secrets_store_ldap_pw(const char* dn
, char* pw
)
366 if (asprintf(&key
, "%s/%s", SECRETS_LDAP_BIND_PW
, dn
) < 0) {
367 DEBUG(0, ("secrets_store_ldap_pw: asprintf failed!\n"));
371 ret
= secrets_store(key
, pw
, strlen(pw
)+1);
377 /*******************************************************************
378 Find the ldap password.
379 ******************************************************************/
381 bool fetch_ldap_pw(char **dn
, char** pw
)
386 *dn
= smb_xstrdup(lp_ldap_admin_dn());
388 if (asprintf(&key
, "%s/%s", SECRETS_LDAP_BIND_PW
, *dn
) < 0) {
390 DEBUG(0, ("fetch_ldap_pw: asprintf failed!\n"));
394 *pw
=(char *)secrets_fetch(key
, &size
);
397 if (*pw
== NULL
|| size
== 0 || (*pw
)[size
-1] != '\0') {
398 DBG_ERR("No valid password for %s\n", *dn
);
407 /*******************************************************************************
408 Store a complete AFS keyfile into secrets.tdb.
409 *******************************************************************************/
411 bool secrets_store_afs_keyfile(const char *cell
, const struct afs_keyfile
*keyfile
)
415 if ((cell
== NULL
) || (keyfile
== NULL
))
418 if (ntohl(keyfile
->nkeys
) > SECRETS_AFS_MAXKEYS
)
421 slprintf(key
, sizeof(key
)-1, "%s/%s", SECRETS_AFS_KEYFILE
, cell
);
422 return secrets_store(key
, keyfile
, sizeof(struct afs_keyfile
));
425 /*******************************************************************************
426 Fetch the current (highest) AFS key from secrets.tdb
427 *******************************************************************************/
428 bool secrets_fetch_afs_key(const char *cell
, struct afs_key
*result
)
431 struct afs_keyfile
*keyfile
;
435 slprintf(key
, sizeof(key
)-1, "%s/%s", SECRETS_AFS_KEYFILE
, cell
);
437 keyfile
= (struct afs_keyfile
*)secrets_fetch(key
, &size
);
442 if (size
!= sizeof(struct afs_keyfile
)) {
443 BURN_FREE(keyfile
, sizeof(*keyfile
));
447 i
= ntohl(keyfile
->nkeys
);
449 if (i
> SECRETS_AFS_MAXKEYS
) {
450 BURN_FREE(keyfile
, sizeof(*keyfile
));
454 *result
= keyfile
->entry
[i
-1];
456 result
->kvno
= ntohl(result
->kvno
);
458 BURN_FREE(keyfile
, sizeof(*keyfile
));
463 /******************************************************************************
464 When kerberos is not available, choose between anonymous or
465 authenticated connections.
467 We need to use an authenticated connection if DCs have the
468 RestrictAnonymous registry entry set > 0, or the "Additional
469 restrictions for anonymous connections" set in the win2k Local
472 Caller to free() result in domain, username, password
473 *******************************************************************************/
474 void secrets_fetch_ipc_userpass(char **username
, char **domain
, char **password
)
476 *username
= (char *)secrets_fetch(SECRETS_AUTH_USER
, NULL
);
477 *domain
= (char *)secrets_fetch(SECRETS_AUTH_DOMAIN
, NULL
);
478 *password
= (char *)secrets_fetch(SECRETS_AUTH_PASSWORD
, NULL
);
480 if (*username
&& **username
) {
482 if (!*domain
|| !**domain
) {
484 *domain
= smb_xstrdup(lp_workgroup());
487 if (!*password
|| !**password
) {
488 BURN_FREE_STR(*password
);
489 *password
= smb_xstrdup("");
492 DEBUG(3, ("IPC$ connections done by user %s\\%s\n",
493 *domain
, *username
));
496 DEBUG(3, ("IPC$ connections done anonymously\n"));
497 SAFE_FREE(*username
);
499 BURN_FREE_STR(*password
);
500 *username
= smb_xstrdup("");
501 *domain
= smb_xstrdup("");
502 *password
= smb_xstrdup("");
506 bool secrets_store_generic(const char *owner
, const char *key
, const char *secret
)
511 if (asprintf(&tdbkey
, "SECRETS/GENERIC/%s/%s", owner
, key
) < 0) {
512 DEBUG(0, ("asprintf failed!\n"));
516 ret
= secrets_store(tdbkey
, secret
, strlen(secret
)+1);
522 /*******************************************************************
523 Find the ldap password.
524 ******************************************************************/
526 char *secrets_fetch_generic(const char *owner
, const char *key
)
531 if (( ! owner
) || ( ! key
)) {
532 DEBUG(1, ("Invalid Parameters"));
536 if (asprintf(&tdbkey
, "SECRETS/GENERIC/%s/%s", owner
, key
) < 0) {
537 DEBUG(0, ("Out of memory!\n"));
541 secret
= (char *)secrets_fetch(tdbkey
, NULL
);