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) {
122 TALLOC_FREE(dbuf.dptr);
131 /* store a secrets entry
133 bool secrets_store(const char *key, const void *data, size_t size)
137 if (!secrets_init()) {
141 status = dbwrap_trans_store(db_ctx, string_tdb_data(key),
142 make_tdb_data((const uint8_t *)data, size),
144 return NT_STATUS_IS_OK(status);
147 bool secrets_store_creds(struct cli_credentials *creds)
149 const char *p = NULL;
152 p = cli_credentials_get_username(creds);
157 ok = secrets_store(SECRETS_AUTH_USER, p, strlen(p) + 1);
159 DBG_ERR("Failed storing auth user name\n");
164 p = cli_credentials_get_domain(creds);
169 ok = secrets_store(SECRETS_AUTH_DOMAIN, p, strlen(p) + 1);
171 DBG_ERR("Failed storing auth domain name\n");
176 p = cli_credentials_get_password(creds);
181 ok = secrets_store(SECRETS_AUTH_PASSWORD, p, strlen(p) + 1);
183 DBG_ERR("Failed storing auth password\n");
191 /* delete a secets database entry
193 bool secrets_delete_entry(const char *key)
196 if (!secrets_init()) {
200 status = dbwrap_trans_delete(db_ctx, string_tdb_data(key));
202 return NT_STATUS_IS_OK(status);
206 * Deletes the key if it exists.
208 bool secrets_delete(const char *key)
212 if (!secrets_init()) {
216 exists = dbwrap_exists(db_ctx, string_tdb_data(key));
221 return secrets_delete_entry(key);
225 * Form a key for fetching a trusted domain password
227 * @param domain trusted domain name
229 * @return stored password's key
231 static char *trustdom_keystr(const char *domain)
235 keystr = talloc_asprintf_strupper_m(talloc_tos(), "%s/%s",
236 SECRETS_DOMTRUST_ACCT_PASS,
238 SMB_ASSERT(keystr != NULL);
242 /************************************************************************
243 Routine to get account password to trusted domain
244 ************************************************************************/
246 bool secrets_fetch_trusted_domain_password(const char *domain, char** pwd,
247 struct dom_sid *sid, time_t *pass_last_set_time)
249 struct TRUSTED_DOM_PASS pass;
250 enum ndr_err_code ndr_err;
252 /* unpacking structures */
255 /* fetching trusted domain password structure */
256 if (!(blob.data = (uint8_t *)secrets_fetch(trustdom_keystr(domain),
258 DEBUG(5, ("secrets_fetch failed!\n"));
262 /* unpack trusted domain password */
263 ndr_err = ndr_pull_struct_blob(&blob, talloc_tos(), &pass,
264 (ndr_pull_flags_fn_t)ndr_pull_TRUSTED_DOM_PASS);
266 SAFE_FREE(blob.data);
268 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
273 /* the trust's password */
275 *pwd = SMB_STRDUP(pass.pass);
281 /* last change time */
282 if (pass_last_set_time) *pass_last_set_time = pass.mod_time;
285 if (sid != NULL) sid_copy(sid, &pass.domain_sid);
291 * Routine to store the password for trusted domain
293 * @param domain remote domain name
294 * @param pwd plain text password of trust relationship
295 * @param sid remote domain sid
297 * @return true if succeeded
300 bool secrets_store_trusted_domain_password(const char* domain, const char* pwd,
301 const struct dom_sid *sid)
305 /* packing structures */
307 enum ndr_err_code ndr_err;
308 struct TRUSTED_DOM_PASS pass;
311 pass.uni_name = domain;
312 pass.uni_name_len = strlen(domain)+1;
314 /* last change time */
315 pass.mod_time = time(NULL);
317 /* password of the trust */
318 pass.pass_len = strlen(pwd);
322 sid_copy(&pass.domain_sid, sid);
324 ndr_err = ndr_push_struct_blob(&blob, talloc_tos(), &pass,
325 (ndr_push_flags_fn_t)ndr_push_TRUSTED_DOM_PASS);
326 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
330 ret = secrets_store(trustdom_keystr(domain), blob.data, blob.length);
332 data_blob_free(&blob);
337 /************************************************************************
338 Routine to delete the password for trusted domain
339 ************************************************************************/
341 bool trusted_domain_password_delete(const char *domain)
343 return secrets_delete_entry(trustdom_keystr(domain));
346 bool secrets_store_ldap_pw(const char* dn, char* pw)
351 if (asprintf(&key, "%s/%s", SECRETS_LDAP_BIND_PW, dn) < 0) {
352 DEBUG(0, ("secrets_store_ldap_pw: asprintf failed!\n"));
356 ret = secrets_store(key, pw, strlen(pw)+1);
362 /*******************************************************************
363 Find the ldap password.
364 ******************************************************************/
366 bool fetch_ldap_pw(char **dn, char** pw)
371 *dn = smb_xstrdup(lp_ldap_admin_dn());
373 if (asprintf(&key, "%s/%s", SECRETS_LDAP_BIND_PW, *dn) < 0) {
375 DEBUG(0, ("fetch_ldap_pw: asprintf failed!\n"));
379 *pw=(char *)secrets_fetch(key, &size);
382 if ((size != 0) && ((*pw)[size-1] != '\0')) {
383 DBG_ERR("Non 0-terminated password for dn %s\n", *dn);
390 /* Upgrade 2.2 style entry */
392 char* old_style_key = SMB_STRDUP(*dn);
394 fstring old_style_pw;
396 if (!old_style_key) {
397 DEBUG(0, ("fetch_ldap_pw: strdup failed!\n"));
403 for (p=old_style_key; *p; p++)
404 if (*p == ',') *p = '/';
406 data=(char *)secrets_fetch(old_style_key, &size);
407 if ((data == NULL) || (size < sizeof(old_style_pw))) {
408 DEBUG(0,("fetch_ldap_pw: neither ldap secret retrieved!\n"));
409 SAFE_FREE(old_style_key);
416 size = MIN(size, sizeof(fstring)-1);
417 strncpy(old_style_pw, data, size);
418 old_style_pw[size] = 0;
422 if (!secrets_store_ldap_pw(*dn, old_style_pw)) {
423 DEBUG(0,("fetch_ldap_pw: ldap secret could not be upgraded!\n"));
424 SAFE_FREE(old_style_key);
429 if (!secrets_delete_entry(old_style_key)) {
430 DEBUG(0,("fetch_ldap_pw: old ldap secret could not be deleted!\n"));
433 SAFE_FREE(old_style_key);
435 *pw = smb_xstrdup(old_style_pw);
441 /*******************************************************************************
442 Store a complete AFS keyfile into secrets.tdb.
443 *******************************************************************************/
445 bool secrets_store_afs_keyfile(const char *cell, const struct afs_keyfile *keyfile)
449 if ((cell == NULL) || (keyfile == NULL))
452 if (ntohl(keyfile->nkeys) > SECRETS_AFS_MAXKEYS)
455 slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_AFS_KEYFILE, cell);
456 return secrets_store(key, keyfile, sizeof(struct afs_keyfile));
459 /*******************************************************************************
460 Fetch the current (highest) AFS key from secrets.tdb
461 *******************************************************************************/
462 bool secrets_fetch_afs_key(const char *cell, struct afs_key *result)
465 struct afs_keyfile *keyfile;
469 slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_AFS_KEYFILE, cell);
471 keyfile = (struct afs_keyfile *)secrets_fetch(key, &size);
476 if (size != sizeof(struct afs_keyfile)) {
481 i = ntohl(keyfile->nkeys);
483 if (i > SECRETS_AFS_MAXKEYS) {
488 *result = keyfile->entry[i-1];
490 result->kvno = ntohl(result->kvno);
497 /******************************************************************************
498 When kerberos is not available, choose between anonymous or
499 authenticated connections.
501 We need to use an authenticated connection if DCs have the
502 RestrictAnonymous registry entry set > 0, or the "Additional
503 restrictions for anonymous connections" set in the win2k Local
506 Caller to free() result in domain, username, password
507 *******************************************************************************/
508 void secrets_fetch_ipc_userpass(char **username, char **domain, char **password)
510 *username = (char *)secrets_fetch(SECRETS_AUTH_USER, NULL);
511 *domain = (char *)secrets_fetch(SECRETS_AUTH_DOMAIN, NULL);
512 *password = (char *)secrets_fetch(SECRETS_AUTH_PASSWORD, NULL);
514 if (*username && **username) {
516 if (!*domain || !**domain)
517 *domain = smb_xstrdup(lp_workgroup());
519 if (!*password || !**password)
520 *password = smb_xstrdup("");
522 DEBUG(3, ("IPC$ connections done by user %s\\%s\n",
523 *domain, *username));
526 DEBUG(3, ("IPC$ connections done anonymously\n"));
527 *username = smb_xstrdup("");
528 *domain = smb_xstrdup("");
529 *password = smb_xstrdup("");
533 bool secrets_store_generic(const char *owner, const char *key, const char *secret)
538 if (asprintf(&tdbkey, "SECRETS/GENERIC/%s/%s", owner, key) < 0) {
539 DEBUG(0, ("asprintf failed!\n"));
543 ret = secrets_store(tdbkey, secret, strlen(secret)+1);
549 /*******************************************************************
550 Find the ldap password.
551 ******************************************************************/
553 char *secrets_fetch_generic(const char *owner, const char *key)
558 if (( ! owner) || ( ! key)) {
559 DEBUG(1, ("Invalid Parameters"));
563 if (asprintf(&tdbkey, "SECRETS/GENERIC/%s/%s", owner, key) < 0) {
564 DEBUG(0, ("Out of memory!\n"));
568 secret = (char *)secrets_fetch(tdbkey, NULL);