2 Unix SMB/CIFS implementation.
3 Copyright (C) Andrew Tridgell 1992-2001
4 Copyright (C) Andrew Bartlett 2002
5 Copyright (C) Rafal Szczesniak 2002
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 /* the Samba secrets database stores any generated, private information
22 such as the local SID and machine trust password */
26 #include "param/param.h"
27 #include "system/filesys.h"
30 #include "lib/ldb/include/ldb.h"
31 #include "lib/tdb/include/tdb.h"
32 #include "lib/util/util_tdb.h"
33 #include "lib/util/util_ldb.h"
34 #include "dsdb/samdb/samdb.h"
36 static struct tdb_wrap *tdb;
39 * Use a TDB to store an incrementing random seed.
41 * Initialised to the current pid, the very first time Samba starts,
42 * and incremented by one each time it is needed.
44 * @note Not called by systems with a working /dev/urandom.
46 static void get_rand_seed(int *new_seed)
50 tdb_change_int32_atomic(tdb->tdb, "INFO/random_seed", new_seed, 1);
55 * close the secrets database
57 void secrets_shutdown(void)
63 * open up the secrets database
65 bool secrets_init(void)
73 fname = private_path(NULL, global_loadparm,
76 tdb = tdb_wrap_open(talloc_autofree_context(), fname, 0, TDB_DEFAULT,
77 O_RDWR|O_CREAT, 0600);
80 DEBUG(0,("Failed to open %s\n", fname));
87 * Set a reseed function for the crypto random generator
89 * This avoids a problem where systems without /dev/urandom
90 * could send the same challenge to multiple clients
92 set_rand_reseed_callback(get_rand_seed);
94 /* Ensure that the reseed is done now, while we are root, etc */
95 generate_random_buffer(&dummy, sizeof(dummy));
101 connect to the secrets ldb
103 struct ldb_context *secrets_db_connect(TALLOC_CTX *mem_ctx)
107 struct ldb_context *ldb;
109 url = lp_secrets_url(global_loadparm);
110 if (!url || !url[0]) {
114 path = private_path(mem_ctx, global_loadparm, url);
119 /* Secrets.ldb *must* always be local. If we call for a
120 * system_session() we will recurse */
121 ldb = ldb_wrap_connect(mem_ctx, global_loadparm, path, NULL, NULL, 0, NULL);
131 * Retrieve the domain SID from the secrets database.
132 * @return pointer to a SID object if the SID could be obtained, NULL otherwise
134 struct dom_sid *secrets_get_domain_sid(TALLOC_CTX *mem_ctx,
137 struct ldb_context *ldb;
138 struct ldb_message **msgs;
140 const char *attrs[] = { "objectSid", NULL };
141 struct dom_sid *result = NULL;
143 ldb = secrets_db_connect(mem_ctx);
145 DEBUG(5, ("secrets_db_connect failed\n"));
149 ldb_ret = gendb_search(ldb, ldb,
150 ldb_dn_new(mem_ctx, ldb, SECRETS_PRIMARY_DOMAIN_DN),
152 SECRETS_PRIMARY_DOMAIN_FILTER, domain);
155 DEBUG(5, ("Error searching for domain SID for %s: %s",
156 domain, ldb_errstring(ldb)));
162 DEBUG(5, ("Did not find domain record for %s\n", domain));
168 DEBUG(5, ("Found more than one (%d) domain records for %s\n",
174 result = samdb_result_dom_sid(mem_ctx, msgs[0], "objectSid");
175 if (result == NULL) {
176 DEBUG(0, ("Domain object for %s does not contain a SID!\n",