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 2 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, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 /* the Samba secrets database stores any generated, private information
23 such as the local SID and machine trust password */
26 #include "lib/tdb/include/tdbutil.h"
28 #include "system/filesys.h"
31 #include "lib/ldb/include/ldb.h"
33 static struct tdb_wrap *tdb;
36 * Use a TDB to store an incrementing random seed.
38 * Initialised to the current pid, the very first time Samba starts,
39 * and incremented by one each time it is needed.
41 * @note Not called by systems with a working /dev/urandom.
43 static void get_rand_seed(int *new_seed)
47 tdb_change_int32_atomic(tdb->tdb, "INFO/random_seed", new_seed, 1);
51 /* close the secrets database */
52 void secrets_shutdown(void)
57 /* open up the secrets database */
58 BOOL secrets_init(void)
66 pstrcpy(fname, lp_private_dir());
67 pstrcat(fname,"/secrets.tdb");
69 tdb = tdb_wrap_open(talloc_autofree_context(), fname, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
72 DEBUG(0,("Failed to open %s\n", fname));
77 * Set a reseed function for the crypto random generator
79 * This avoids a problem where systems without /dev/urandom
80 * could send the same challenge to multiple clients
82 set_rand_reseed_callback(get_rand_seed);
84 /* Ensure that the reseed is done now, while we are root, etc */
85 generate_random_buffer(&dummy, sizeof(dummy));
90 /* read a entry from the secrets database - the caller must free the result
91 if size is non-null then the size of the entry is put in there
93 static void *secrets_fetch(const char *key, size_t *size)
99 kbuf.dptr = strdup(key);
100 kbuf.dsize = strlen(key);
101 dbuf = tdb_fetch(tdb->tdb, kbuf);
108 /************************************************************************
109 Routine to fetch the plaintext machine account password for a realm
110 the password is assumed to be a null terminated ascii string
111 ************************************************************************/
112 char *secrets_fetch_machine_password(const char *domain)
116 asprintf(&key, "%s/%s", SECRETS_MACHINE_PASSWORD, domain);
118 ret = (char *)secrets_fetch(key, NULL);
125 connect to the schannel ldb
127 struct ldb_context *secrets_db_connect(TALLOC_CTX *mem_ctx)
130 struct ldb_context *ldb;
132 const char *init_ldif =
133 "dn: @ATTRIBUTES\n" \
134 "computerName: CASE_INSENSITIVE\n" \
135 "flatname: CASE_INSENSITIVE\n";
137 path = private_path(mem_ctx, "secrets.ldb");
142 existed = file_exists(path);
144 ldb = ldb_wrap_connect(mem_ctx, path, 0, NULL);
151 gendb_add_ldif(ldb, init_ldif);
157 struct dom_sid *secrets_get_domain_sid(TALLOC_CTX *mem_ctx,
160 struct ldb_context *ldb;
161 struct ldb_message **msgs;
163 const char *attrs[] = { "objectSid", NULL };
164 struct dom_sid *result = NULL;
166 ldb = secrets_db_connect(mem_ctx);
168 DEBUG(5, ("secrets_db_connect failed\n"));
172 ldb_ret = gendb_search(ldb, ldb,
173 ldb_dn_explode(mem_ctx, SECRETS_PRIMARY_DOMAIN_DN),
175 SECRETS_PRIMARY_DOMAIN_FILTER, domain);
178 DEBUG(5, ("Did not find domain record for %s\n", domain));
183 DEBUG(5, ("Found more than one (%d) domain records for %s\n",
188 result = samdb_result_dom_sid(mem_ctx, msgs[0], "objectSid");
189 if (result == NULL) {
190 DEBUG(0, ("Domain object for %s does not contain a SID!\n",