talloc: version 2.1.1
[Samba.git] / source4 / param / secrets.c
blob92e338a6baba2b8872f255a763899effc0d418af
1 /*
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 */
24 #include "includes.h"
25 #include "secrets.h"
26 #include "param/param.h"
27 #include "system/filesys.h"
28 #include "lib/tdb_wrap/tdb_wrap.h"
29 #include "lib/ldb-samba/ldb_wrap.h"
30 #include <ldb.h>
31 #include "../lib/util/util_tdb.h"
32 #include "librpc/gen_ndr/ndr_security.h"
33 #include "dsdb/samdb/samdb.h"
35 /**
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(struct tdb_wrap *secretsdb, int *new_seed)
45 *new_seed = getpid();
46 if (secretsdb != NULL) {
47 tdb_change_int32_atomic(secretsdb->tdb, "INFO/random_seed", new_seed, 1);
51 /**
52 * open up the randseed database and set the random number generator callback
54 bool randseed_init(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
56 char *fname;
57 uint8_t dummy;
58 struct tdb_wrap *tdb;
60 fname = lpcfg_private_path(mem_ctx, lp_ctx, "randseed.tdb");
62 tdb = tdb_wrap_open(mem_ctx, fname,
63 lpcfg_tdb_hash_size(lp_ctx, fname),
64 lpcfg_tdb_flags(lp_ctx, TDB_DEFAULT),
65 O_RDWR|O_CREAT, 0600);
67 if (!tdb) {
68 DEBUG(0,("Failed to open %s\n", fname));
69 talloc_free(fname);
70 return false;
72 talloc_free(fname);
74 /**
75 * Set a reseed function for the crypto random generator
77 * This avoids a problem where systems without /dev/urandom
78 * could send the same challenge to multiple clients
80 set_rand_reseed_callback((void (*) (void *, int *))get_rand_seed, tdb);
82 /* Ensure that the reseed is done now, while we are root, etc */
83 generate_random_buffer(&dummy, sizeof(dummy));
85 return true;
88 /**
89 connect to the secrets ldb
91 struct ldb_context *secrets_db_connect(TALLOC_CTX *mem_ctx,
92 struct loadparm_context *lp_ctx)
94 return ldb_wrap_connect(mem_ctx, NULL, lp_ctx, "secrets.ldb",
95 NULL, NULL, 0);
98 /**
99 * Retrieve the domain SID from the secrets database.
100 * @return pointer to a SID object if the SID could be obtained, NULL otherwise
102 struct dom_sid *secrets_get_domain_sid(TALLOC_CTX *mem_ctx,
103 struct loadparm_context *lp_ctx,
104 const char *domain,
105 enum netr_SchannelType *sec_channel_type,
106 char **errstring)
108 struct ldb_context *ldb;
109 struct ldb_message *msg;
110 int ldb_ret;
111 const char *attrs[] = { "objectSid", "secureChannelType", NULL };
112 struct dom_sid *result = NULL;
113 const struct ldb_val *v;
114 enum ndr_err_code ndr_err;
116 *errstring = NULL;
118 ldb = secrets_db_connect(mem_ctx, lp_ctx);
119 if (ldb == NULL) {
120 DEBUG(5, ("secrets_db_connect failed\n"));
121 return NULL;
124 ldb_ret = dsdb_search_one(ldb, ldb, &msg,
125 ldb_dn_new(mem_ctx, ldb, SECRETS_PRIMARY_DOMAIN_DN),
126 LDB_SCOPE_ONELEVEL,
127 attrs, 0, SECRETS_PRIMARY_DOMAIN_FILTER, domain);
129 if (ldb_ret != LDB_SUCCESS) {
130 *errstring = talloc_asprintf(mem_ctx, "Failed to find record for %s in %s: %s: %s",
131 domain, (char *) ldb_get_opaque(ldb, "ldb_url"),
132 ldb_strerror(ldb_ret), ldb_errstring(ldb));
133 return NULL;
135 v = ldb_msg_find_ldb_val(msg, "objectSid");
136 if (v == NULL) {
137 *errstring = talloc_asprintf(mem_ctx, "Failed to find a SID on record for %s in %s",
138 domain, (char *) ldb_get_opaque(ldb, "ldb_url"));
139 return NULL;
142 if (sec_channel_type) {
143 int t;
144 t = ldb_msg_find_attr_as_int(msg, "secureChannelType", -1);
145 if (t == -1) {
146 *errstring = talloc_asprintf(mem_ctx, "Failed to find secureChannelType for %s in %s",
147 domain, (char *) ldb_get_opaque(ldb, "ldb_url"));
148 return NULL;
150 *sec_channel_type = t;
153 result = talloc(mem_ctx, struct dom_sid);
154 if (result == NULL) {
155 talloc_free(ldb);
156 return NULL;
159 ndr_err = ndr_pull_struct_blob(v, result, result,
160 (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
161 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
162 *errstring = talloc_asprintf(mem_ctx, "Failed to parse SID on record for %s in %s",
163 domain, (char *) ldb_get_opaque(ldb, "ldb_url"));
164 talloc_free(result);
165 talloc_free(ldb);
166 return NULL;
169 return result;
172 char *keytab_name_from_msg(TALLOC_CTX *mem_ctx, struct ldb_context *ldb, struct ldb_message *msg)
174 const char *krb5keytab = ldb_msg_find_attr_as_string(msg, "krb5Keytab", NULL);
175 if (krb5keytab) {
176 return talloc_strdup(mem_ctx, krb5keytab);
177 } else {
178 char *file_keytab;
179 char *relative_path;
180 const char *privateKeytab = ldb_msg_find_attr_as_string(msg, "privateKeytab", NULL);
181 if (!privateKeytab) {
182 return NULL;
185 relative_path = ldb_relative_path(ldb, mem_ctx, privateKeytab);
186 if (!relative_path) {
187 return NULL;
189 file_keytab = talloc_asprintf(mem_ctx, "FILE:%s", relative_path);
190 talloc_free(relative_path);
191 return file_keytab;
193 return NULL;