sync with the sec_desc parsing fix from APP_HEAD. I will probably need
[Samba.git] / source / passdb / secrets.c
blob3ecaf52e58674ab9a611b5f0a2701a655aa4756a
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 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 */
25 #include "includes.h"
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_PASSDB
30 static TDB_CONTEXT *tdb;
32 /* open up the secrets database */
33 BOOL secrets_init(void)
35 pstring fname;
37 if (tdb)
38 return True;
40 pstrcpy(fname, lp_private_dir());
41 pstrcat(fname,"/secrets.tdb");
43 tdb = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
45 if (!tdb) {
46 DEBUG(0,("Failed to open %s\n", fname));
47 return False;
49 return True;
52 /* read a entry from the secrets database - the caller must free the result
53 if size is non-null then the size of the entry is put in there
55 void *secrets_fetch(const char *key, size_t *size)
57 TDB_DATA kbuf, dbuf;
58 secrets_init();
59 if (!tdb)
60 return NULL;
61 kbuf.dptr = key;
62 kbuf.dsize = strlen(key);
63 dbuf = tdb_fetch(tdb, kbuf);
64 if (size)
65 *size = dbuf.dsize;
66 return dbuf.dptr;
69 /* store a secrets entry
71 BOOL secrets_store(const char *key, void *data, size_t size)
73 TDB_DATA kbuf, dbuf;
74 secrets_init();
75 if (!tdb)
76 return False;
77 kbuf.dptr = key;
78 kbuf.dsize = strlen(key);
79 dbuf.dptr = data;
80 dbuf.dsize = size;
81 return tdb_store(tdb, kbuf, dbuf, TDB_REPLACE) == 0;
85 /* delete a secets database entry
87 BOOL secrets_delete(const char *key)
89 TDB_DATA kbuf;
90 secrets_init();
91 if (!tdb)
92 return False;
93 kbuf.dptr = key;
94 kbuf.dsize = strlen(key);
95 return tdb_delete(tdb, kbuf) == 0;
98 BOOL secrets_store_domain_sid(char *domain, DOM_SID *sid)
100 fstring key;
102 slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_DOMAIN_SID, domain);
103 strupper(key);
104 return secrets_store(key, sid, sizeof(DOM_SID));
107 BOOL secrets_fetch_domain_sid(char *domain, DOM_SID *sid)
109 DOM_SID *dyn_sid;
110 fstring key;
111 size_t size;
113 slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_DOMAIN_SID, domain);
114 strupper(key);
115 dyn_sid = (DOM_SID *)secrets_fetch(key, &size);
117 if (dyn_sid == NULL)
118 return False;
120 if (size != sizeof(DOM_SID))
122 SAFE_FREE(dyn_sid);
123 return False;
126 *sid = *dyn_sid;
127 SAFE_FREE(dyn_sid);
128 return True;
133 * Form a key for fetching the machine trust account password
135 * @param domain domain name
137 * @return stored password's key
139 const char *trust_keystr(const char *domain)
141 static fstring keystr;
143 slprintf(keystr,sizeof(keystr)-1,"%s/%s",
144 SECRETS_MACHINE_ACCT_PASS, domain);
145 strupper(keystr);
147 return keystr;
151 * Form a key for fetching a trusted domain password
153 * @param domain trusted domain name
155 * @return stored password's key
157 char *trustdom_keystr(const char *domain)
159 static char* keystr;
161 asprintf(&keystr, "%s/%s", SECRETS_DOMTRUST_ACCT_PASS, domain);
162 strupper(keystr);
164 return keystr;
167 /************************************************************************
168 Routine to get the machine trust account password for a domain.
169 ************************************************************************/
170 BOOL secrets_fetch_trust_account_password(char *domain, uint8 ret_pwd[16],
171 time_t *pass_last_set_time)
173 struct machine_acct_pass *pass;
174 char *plaintext;
175 size_t size;
177 plaintext = secrets_fetch_machine_password();
178 if (plaintext) {
179 /* we have an ADS password - use that */
180 DEBUG(4,("Using ADS machine password\n"));
181 E_md4hash((uchar *)plaintext, ret_pwd);
182 SAFE_FREE(plaintext);
183 return True;
186 if (!(pass = secrets_fetch(trust_keystr(domain), &size))) {
187 DEBUG(5, ("secrets_fetch failed!\n"));
188 return False;
191 if (size != sizeof(*pass)) {
192 DEBUG(0, ("secrets were of incorrect size!\n"));
193 return False;
196 if (pass_last_set_time) *pass_last_set_time = pass->mod_time;
197 memcpy(ret_pwd, pass->hash, 16);
198 SAFE_FREE(pass);
199 return True;
202 /************************************************************************
203 Routine to get account password to trusted domain
204 ************************************************************************/
205 BOOL secrets_fetch_trusted_domain_password(char *domain, char** pwd,
206 DOM_SID *sid, time_t *pass_last_set_time)
208 struct trusted_dom_pass *pass;
209 size_t size;
211 /* fetching trusted domain password structure */
212 if (!(pass = secrets_fetch(trustdom_keystr(domain), &size))) {
213 DEBUG(5, ("secrets_fetch failed!\n"));
214 return False;
217 if (size != sizeof(*pass)) {
218 DEBUG(0, ("secrets were of incorrect size!\n"));
219 return False;
222 /* the trust's password */
223 if (pwd) {
224 *pwd = strdup(pass->pass);
225 if (!*pwd) {
226 return False;
230 /* last change time */
231 if (pass_last_set_time) *pass_last_set_time = pass->mod_time;
233 /* domain sid */
234 memcpy(&sid, &(pass->domain_sid), sizeof(sid));
236 SAFE_FREE(pass);
238 return True;
241 /************************************************************************
242 Routine to set the trust account password for a domain.
243 ************************************************************************/
244 BOOL secrets_store_trust_account_password(char *domain, uint8 new_pwd[16])
246 struct machine_acct_pass pass;
248 pass.mod_time = time(NULL);
249 memcpy(pass.hash, new_pwd, 16);
251 return secrets_store(trust_keystr(domain), (void *)&pass, sizeof(pass));
255 * Routine to set the password for trusted domain
257 * @param domain remote domain name
258 * @param pwd plain text password of trust relationship
259 * @param sid remote domain sid
261 * @return true if succeeded
264 BOOL secrets_store_trusted_domain_password(char* domain, smb_ucs2_t *uni_dom_name,
265 size_t uni_name_len, char* pwd,
266 DOM_SID sid)
268 struct trusted_dom_pass pass;
269 ZERO_STRUCT(pass);
271 /* unicode domain name and its length */
272 if (!uni_dom_name)
273 return False;
275 strncpy_w(pass.uni_name, uni_dom_name, sizeof(pass.uni_name) - 1);
276 pass.uni_name_len = uni_name_len;
278 /* last change time */
279 pass.mod_time = time(NULL);
281 /* password of the trust */
282 pass.pass_len = strlen(pwd);
283 fstrcpy(pass.pass, pwd);
285 /* domain sid */
286 memcpy(&(pass.domain_sid), &sid, sizeof(sid));
288 return secrets_store(trustdom_keystr(domain), (void *)&pass, sizeof(pass));
291 /************************************************************************
292 Routine to set the plaintext machine account password for a realm
293 the password is assumed to be a null terminated ascii string
294 ************************************************************************/
295 BOOL secrets_store_machine_password(char *pass)
297 char *key;
298 BOOL ret;
299 asprintf(&key, "%s/%s", SECRETS_MACHINE_PASSWORD, lp_workgroup());
300 strupper(key);
301 ret = secrets_store(key, pass, strlen(pass)+1);
302 free(key);
303 return ret;
307 /************************************************************************
308 Routine to fetch the plaintext machine account password for a realm
309 the password is assumed to be a null terminated ascii string
310 ************************************************************************/
311 char *secrets_fetch_machine_password(void)
313 char *key;
314 char *ret;
315 asprintf(&key, "%s/%s", SECRETS_MACHINE_PASSWORD, lp_workgroup());
316 strupper(key);
317 ret = (char *)secrets_fetch(key, NULL);
318 free(key);
319 return ret;
324 /************************************************************************
325 Routine to delete the machine trust account password file for a domain.
326 ************************************************************************/
328 BOOL trust_password_delete(const char *domain)
330 return secrets_delete(trust_keystr(domain));
333 /************************************************************************
334 Routine to delete the password for trusted domain
335 ************************************************************************/
336 BOOL trusted_domain_password_delete(const char *domain)
338 return secrets_delete(trustdom_keystr(domain));
342 /*******************************************************************
343 Reset the 'done' variables so after a client process is created
344 from a fork call these calls will be re-done. This should be
345 expanded if more variables need reseting.
346 ******************************************************************/
348 void reset_globals_after_fork(void)
350 unsigned char dummy;
352 secrets_init();
355 * Increment the global seed value to ensure every smbd starts
356 * with a new random seed.
359 if (tdb) {
360 uint32 initial_val = sys_getpid();
361 tdb_change_int32_atomic(tdb, "INFO/random_seed", (int *)&initial_val, 1);
362 set_rand_reseed_data((unsigned char *)&initial_val, sizeof(initial_val));
366 * Re-seed the random crypto generator, so all smbd's
367 * started from the same parent won't generate the same
368 * sequence.
370 generate_random_buffer( &dummy, 1, True);
373 BOOL secrets_store_ldap_pw(const char* dn, char* pw)
375 char *key = NULL;
376 BOOL ret;
378 if (asprintf(&key, "%s/%s", SECRETS_LDAP_BIND_PW, dn) < 0) {
379 DEBUG(0, ("secrets_store_ldap_pw: asprintf failed!\n"));
380 return False;
383 ret = secrets_store(key, pw, strlen(pw)+1);
385 SAFE_FREE(key);
386 return ret;
391 * The linked list is allocated on the supplied talloc context, caller gets to destory
392 * when done.
394 * @param ctx Allocation context
395 * @param enum_ctx Starting index, eg. we can start fetching at third
396 * or sixth trusted domain entry. Zero is the first index.
397 * Value it is set to is the enum context for the next enumeration.
398 * @param num_domains Number of domain entries to fetch at one call
399 * @param domains Pointer to array of trusted domain structs to be filled up
401 * @return nt status code of rpc response
402 **/
404 NTSTATUS secrets_get_trusted_domains(TALLOC_CTX* ctx, int* enum_ctx, int max_num_domains, int *num_domains, TRUSTDOM ***domains)
406 TDB_LIST_NODE *keys, *k;
407 TRUSTDOM *dom = NULL;
408 char *pattern;
409 int start_idx;
410 uint32 idx = 0;
411 size_t size;
412 struct trusted_dom_pass *pass;
413 NTSTATUS status;
415 secrets_init();
417 *num_domains = 0;
418 start_idx = *enum_ctx;
420 /* generate searching pattern */
421 if (!(pattern = talloc_asprintf(ctx, "%s/*", SECRETS_DOMTRUST_ACCT_PASS))) {
422 DEBUG(0, ("secrets_get_trusted_domains: talloc_asprintf() failed!\n"));
423 return NT_STATUS_NO_MEMORY;
426 DEBUG(5, ("secrets_get_trusted_domains: looking for %d domains, starting at index %d\n",
427 max_num_domains, *enum_ctx));
429 *domains = talloc_zero(ctx, sizeof(**domains)*max_num_domains);
431 /* fetching trusted domains' data and collecting them in a list */
432 keys = tdb_search_keys(tdb, pattern);
435 * if there's no keys returned ie. no trusted domain,
436 * return "no more entries" code
438 status = NT_STATUS_NO_MORE_ENTRIES;
440 /* searching for keys in sectrets db -- way to go ... */
441 for (k = keys; k; k = k->next) {
442 char *secrets_key;
444 /* important: ensure null-termination of the key string */
445 secrets_key = strndup(k->node_key.dptr, k->node_key.dsize);
446 if (!secrets_key) {
447 DEBUG(0, ("strndup failed!\n"));
448 return NT_STATUS_NO_MEMORY;
451 pass = secrets_fetch(secrets_key, &size);
453 if (size != sizeof(*pass)) {
454 DEBUG(2, ("Secrets record %s is invalid!\n", secrets_key));
455 SAFE_FREE(pass);
456 continue;
459 SAFE_FREE(secrets_key);
461 if (idx >= start_idx && idx < start_idx + max_num_domains) {
462 dom = talloc_zero(ctx, sizeof(*dom));
463 if (!dom) {
464 /* free returned tdb record */
465 SAFE_FREE(pass);
467 return NT_STATUS_NO_MEMORY;
470 /* copy domain sid */
471 SMB_ASSERT(sizeof(dom->sid) == sizeof(pass->domain_sid));
472 memcpy(&(dom->sid), &(pass->domain_sid), sizeof(dom->sid));
474 /* copy unicode domain name */
475 dom->name = talloc_strdup_w(ctx, pass->uni_name);
477 (*domains)[idx - start_idx] = dom;
479 *enum_ctx = idx + 1;
480 (*num_domains)++;
482 /* set proper status code to return */
483 if (k->next) {
484 /* there are yet some entries to enumerate */
485 status = STATUS_MORE_ENTRIES;
486 } else {
487 /* this is the last entry in the whole enumeration */
488 status = NT_STATUS_OK;
492 idx++;
494 /* free returned tdb record */
495 SAFE_FREE(pass);
498 DEBUG(5, ("secrets_get_trusted_domains: got %d domains\n", *num_domains));
500 /* free the results of searching the keys */
501 tdb_search_list_free(keys);
503 return status;