s3-lsasd: Listen on parent messages
[Samba/bjacke.git] / source3 / passdb / secrets.c
blobd2296407933d059ad13357acd31dbe08e45bdc09
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
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 */
25 #include "includes.h"
26 #include "system/filesys.h"
27 #include "passdb.h"
28 #include "../libcli/auth/libcli_auth.h"
29 #include "librpc/gen_ndr/ndr_secrets.h"
30 #include "secrets.h"
31 #include "dbwrap/dbwrap.h"
32 #include "dbwrap/dbwrap_open.h"
33 #include "../libcli/security/security.h"
34 #include "util_tdb.h"
36 #undef DBGC_CLASS
37 #define DBGC_CLASS DBGC_PASSDB
39 static struct db_context *db_ctx;
41 /**
42 * Use a TDB to store an incrementing random seed.
44 * Initialised to the current pid, the very first time Samba starts,
45 * and incremented by one each time it is needed.
47 * @note Not called by systems with a working /dev/urandom.
49 static void get_rand_seed(void *userdata, int *new_seed)
51 *new_seed = sys_getpid();
52 if (db_ctx) {
53 dbwrap_trans_change_int32_atomic(db_ctx, "INFO/random_seed",
54 new_seed, 1);
58 /* open up the secrets database with specified private_dir path */
59 bool secrets_init_path(const char *private_dir)
61 char *fname = NULL;
62 unsigned char dummy;
64 if (db_ctx != NULL) {
65 return True;
68 if (private_dir == NULL) {
69 return False;
72 fname = talloc_asprintf(talloc_tos(), "%s/secrets.tdb",
73 private_dir);
74 if (fname == NULL) {
75 return False;
78 db_ctx = db_open(NULL, fname, 0,
79 TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
81 if (db_ctx == NULL) {
82 DEBUG(0,("Failed to open %s\n", fname));
83 return False;
86 TALLOC_FREE(fname);
88 /**
89 * Set a reseed function for the crypto random generator
91 * This avoids a problem where systems without /dev/urandom
92 * could send the same challenge to multiple clients
94 set_rand_reseed_callback(get_rand_seed, NULL);
96 /* Ensure that the reseed is done now, while we are root, etc */
97 generate_random_buffer(&dummy, sizeof(dummy));
99 return True;
102 /* open up the secrets database */
103 bool secrets_init(void)
105 return secrets_init_path(lp_private_dir());
108 struct db_context *secrets_db_ctx(void)
110 if (!secrets_init()) {
111 return NULL;
114 return db_ctx;
118 * close secrets.tdb
120 void secrets_shutdown(void)
122 TALLOC_FREE(db_ctx);
125 /* read a entry from the secrets database - the caller must free the result
126 if size is non-null then the size of the entry is put in there
128 void *secrets_fetch(const char *key, size_t *size)
130 TDB_DATA dbuf;
131 void *result;
133 if (!secrets_init()) {
134 return NULL;
137 if (db_ctx->fetch(db_ctx, talloc_tos(), string_tdb_data(key),
138 &dbuf) != 0) {
139 return NULL;
142 result = memdup(dbuf.dptr, dbuf.dsize);
143 if (result == NULL) {
144 return NULL;
146 TALLOC_FREE(dbuf.dptr);
148 if (size) {
149 *size = dbuf.dsize;
152 return result;
155 /* store a secrets entry
157 bool secrets_store(const char *key, const void *data, size_t size)
159 NTSTATUS status;
161 if (!secrets_init()) {
162 return false;
165 status = dbwrap_trans_store(db_ctx, string_tdb_data(key),
166 make_tdb_data((const uint8 *)data, size),
167 TDB_REPLACE);
168 return NT_STATUS_IS_OK(status);
172 /* delete a secets database entry
174 bool secrets_delete(const char *key)
176 NTSTATUS status;
177 if (!secrets_init()) {
178 return false;
181 status = dbwrap_trans_delete(db_ctx, string_tdb_data(key));
183 return NT_STATUS_IS_OK(status);
187 * Form a key for fetching a trusted domain password
189 * @param domain trusted domain name
191 * @return stored password's key
193 static char *trustdom_keystr(const char *domain)
195 char *keystr;
197 keystr = talloc_asprintf_strupper_m(talloc_tos(), "%s/%s",
198 SECRETS_DOMTRUST_ACCT_PASS,
199 domain);
200 SMB_ASSERT(keystr != NULL);
201 return keystr;
204 /************************************************************************
205 Routine to get account password to trusted domain
206 ************************************************************************/
208 bool secrets_fetch_trusted_domain_password(const char *domain, char** pwd,
209 struct dom_sid *sid, time_t *pass_last_set_time)
211 struct TRUSTED_DOM_PASS pass;
212 enum ndr_err_code ndr_err;
214 /* unpacking structures */
215 DATA_BLOB blob;
217 /* fetching trusted domain password structure */
218 if (!(blob.data = (uint8_t *)secrets_fetch(trustdom_keystr(domain),
219 &blob.length))) {
220 DEBUG(5, ("secrets_fetch failed!\n"));
221 return False;
224 /* unpack trusted domain password */
225 ndr_err = ndr_pull_struct_blob(&blob, talloc_tos(), &pass,
226 (ndr_pull_flags_fn_t)ndr_pull_TRUSTED_DOM_PASS);
228 SAFE_FREE(blob.data);
230 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
231 return false;
235 /* the trust's password */
236 if (pwd) {
237 *pwd = SMB_STRDUP(pass.pass);
238 if (!*pwd) {
239 return False;
243 /* last change time */
244 if (pass_last_set_time) *pass_last_set_time = pass.mod_time;
246 /* domain sid */
247 if (sid != NULL) sid_copy(sid, &pass.domain_sid);
249 return True;
253 * Routine to store the password for trusted domain
255 * @param domain remote domain name
256 * @param pwd plain text password of trust relationship
257 * @param sid remote domain sid
259 * @return true if succeeded
262 bool secrets_store_trusted_domain_password(const char* domain, const char* pwd,
263 const struct dom_sid *sid)
265 bool ret;
267 /* packing structures */
268 DATA_BLOB blob;
269 enum ndr_err_code ndr_err;
270 struct TRUSTED_DOM_PASS pass;
271 ZERO_STRUCT(pass);
273 pass.uni_name = domain;
274 pass.uni_name_len = strlen(domain)+1;
276 /* last change time */
277 pass.mod_time = time(NULL);
279 /* password of the trust */
280 pass.pass_len = strlen(pwd);
281 pass.pass = pwd;
283 /* domain sid */
284 sid_copy(&pass.domain_sid, sid);
286 ndr_err = ndr_push_struct_blob(&blob, talloc_tos(), &pass,
287 (ndr_push_flags_fn_t)ndr_push_TRUSTED_DOM_PASS);
288 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
289 return false;
292 ret = secrets_store(trustdom_keystr(domain), blob.data, blob.length);
294 data_blob_free(&blob);
296 return ret;
299 /************************************************************************
300 Routine to delete the password for trusted domain
301 ************************************************************************/
303 bool trusted_domain_password_delete(const char *domain)
305 return secrets_delete(trustdom_keystr(domain));
308 bool secrets_store_ldap_pw(const char* dn, char* pw)
310 char *key = NULL;
311 bool ret;
313 if (asprintf(&key, "%s/%s", SECRETS_LDAP_BIND_PW, dn) < 0) {
314 DEBUG(0, ("secrets_store_ldap_pw: asprintf failed!\n"));
315 return False;
318 ret = secrets_store(key, pw, strlen(pw)+1);
320 SAFE_FREE(key);
321 return ret;
324 /*******************************************************************
325 Find the ldap password.
326 ******************************************************************/
328 bool fetch_ldap_pw(char **dn, char** pw)
330 char *key = NULL;
331 size_t size = 0;
333 *dn = smb_xstrdup(lp_ldap_admin_dn());
335 if (asprintf(&key, "%s/%s", SECRETS_LDAP_BIND_PW, *dn) < 0) {
336 SAFE_FREE(*dn);
337 DEBUG(0, ("fetch_ldap_pw: asprintf failed!\n"));
338 return false;
341 *pw=(char *)secrets_fetch(key, &size);
342 SAFE_FREE(key);
344 if (!size) {
345 /* Upgrade 2.2 style entry */
346 char *p;
347 char* old_style_key = SMB_STRDUP(*dn);
348 char *data;
349 fstring old_style_pw;
351 if (!old_style_key) {
352 DEBUG(0, ("fetch_ldap_pw: strdup failed!\n"));
353 return False;
356 for (p=old_style_key; *p; p++)
357 if (*p == ',') *p = '/';
359 data=(char *)secrets_fetch(old_style_key, &size);
360 if ((data == NULL) || (size < sizeof(old_style_pw))) {
361 DEBUG(0,("fetch_ldap_pw: neither ldap secret retrieved!\n"));
362 SAFE_FREE(old_style_key);
363 SAFE_FREE(*dn);
364 SAFE_FREE(data);
365 return False;
368 size = MIN(size, sizeof(fstring)-1);
369 strncpy(old_style_pw, data, size);
370 old_style_pw[size] = 0;
372 SAFE_FREE(data);
374 if (!secrets_store_ldap_pw(*dn, old_style_pw)) {
375 DEBUG(0,("fetch_ldap_pw: ldap secret could not be upgraded!\n"));
376 SAFE_FREE(old_style_key);
377 SAFE_FREE(*dn);
378 return False;
380 if (!secrets_delete(old_style_key)) {
381 DEBUG(0,("fetch_ldap_pw: old ldap secret could not be deleted!\n"));
384 SAFE_FREE(old_style_key);
386 *pw = smb_xstrdup(old_style_pw);
389 return True;
393 * Get trusted domains info from secrets.tdb.
396 struct list_trusted_domains_state {
397 uint32 num_domains;
398 struct trustdom_info **domains;
401 static int list_trusted_domain(struct db_record *rec, void *private_data)
403 const size_t prefix_len = strlen(SECRETS_DOMTRUST_ACCT_PASS);
404 struct TRUSTED_DOM_PASS pass;
405 enum ndr_err_code ndr_err;
406 DATA_BLOB blob;
407 struct trustdom_info *dom_info;
409 struct list_trusted_domains_state *state =
410 (struct list_trusted_domains_state *)private_data;
412 if ((rec->key.dsize < prefix_len)
413 || (strncmp((char *)rec->key.dptr, SECRETS_DOMTRUST_ACCT_PASS,
414 prefix_len) != 0)) {
415 return 0;
418 blob = data_blob_const(rec->value.dptr, rec->value.dsize);
420 ndr_err = ndr_pull_struct_blob(&blob, talloc_tos(), &pass,
421 (ndr_pull_flags_fn_t)ndr_pull_TRUSTED_DOM_PASS);
422 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
423 return false;
426 if (pass.domain_sid.num_auths != 4) {
427 DEBUG(0, ("SID %s is not a domain sid, has %d "
428 "auths instead of 4\n",
429 sid_string_dbg(&pass.domain_sid),
430 pass.domain_sid.num_auths));
431 return 0;
434 if (!(dom_info = talloc(state->domains, struct trustdom_info))) {
435 DEBUG(0, ("talloc failed\n"));
436 return 0;
439 dom_info->name = talloc_strdup(dom_info, pass.uni_name);
440 if (!dom_info->name) {
441 TALLOC_FREE(dom_info);
442 return 0;
445 sid_copy(&dom_info->sid, &pass.domain_sid);
447 ADD_TO_ARRAY(state->domains, struct trustdom_info *, dom_info,
448 &state->domains, &state->num_domains);
450 if (state->domains == NULL) {
451 state->num_domains = 0;
452 return -1;
454 return 0;
457 NTSTATUS secrets_trusted_domains(TALLOC_CTX *mem_ctx, uint32 *num_domains,
458 struct trustdom_info ***domains)
460 struct list_trusted_domains_state state;
462 if (!secrets_init()) {
463 return NT_STATUS_ACCESS_DENIED;
466 state.num_domains = 0;
469 * Make sure that a talloc context for the trustdom_info structs
470 * exists
473 if (!(state.domains = talloc_array(
474 mem_ctx, struct trustdom_info *, 1))) {
475 return NT_STATUS_NO_MEMORY;
478 db_ctx->traverse_read(db_ctx, list_trusted_domain, (void *)&state);
480 *num_domains = state.num_domains;
481 *domains = state.domains;
482 return NT_STATUS_OK;
485 /*******************************************************************************
486 Store a complete AFS keyfile into secrets.tdb.
487 *******************************************************************************/
489 bool secrets_store_afs_keyfile(const char *cell, const struct afs_keyfile *keyfile)
491 fstring key;
493 if ((cell == NULL) || (keyfile == NULL))
494 return False;
496 if (ntohl(keyfile->nkeys) > SECRETS_AFS_MAXKEYS)
497 return False;
499 slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_AFS_KEYFILE, cell);
500 return secrets_store(key, keyfile, sizeof(struct afs_keyfile));
503 /*******************************************************************************
504 Fetch the current (highest) AFS key from secrets.tdb
505 *******************************************************************************/
506 bool secrets_fetch_afs_key(const char *cell, struct afs_key *result)
508 fstring key;
509 struct afs_keyfile *keyfile;
510 size_t size = 0;
511 uint32 i;
513 slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_AFS_KEYFILE, cell);
515 keyfile = (struct afs_keyfile *)secrets_fetch(key, &size);
517 if (keyfile == NULL)
518 return False;
520 if (size != sizeof(struct afs_keyfile)) {
521 SAFE_FREE(keyfile);
522 return False;
525 i = ntohl(keyfile->nkeys);
527 if (i > SECRETS_AFS_MAXKEYS) {
528 SAFE_FREE(keyfile);
529 return False;
532 *result = keyfile->entry[i-1];
534 result->kvno = ntohl(result->kvno);
536 SAFE_FREE(keyfile);
538 return True;
541 /******************************************************************************
542 When kerberos is not available, choose between anonymous or
543 authenticated connections.
545 We need to use an authenticated connection if DCs have the
546 RestrictAnonymous registry entry set > 0, or the "Additional
547 restrictions for anonymous connections" set in the win2k Local
548 Security Policy.
550 Caller to free() result in domain, username, password
551 *******************************************************************************/
552 void secrets_fetch_ipc_userpass(char **username, char **domain, char **password)
554 *username = (char *)secrets_fetch(SECRETS_AUTH_USER, NULL);
555 *domain = (char *)secrets_fetch(SECRETS_AUTH_DOMAIN, NULL);
556 *password = (char *)secrets_fetch(SECRETS_AUTH_PASSWORD, NULL);
558 if (*username && **username) {
560 if (!*domain || !**domain)
561 *domain = smb_xstrdup(lp_workgroup());
563 if (!*password || !**password)
564 *password = smb_xstrdup("");
566 DEBUG(3, ("IPC$ connections done by user %s\\%s\n",
567 *domain, *username));
569 } else {
570 DEBUG(3, ("IPC$ connections done anonymously\n"));
571 *username = smb_xstrdup("");
572 *domain = smb_xstrdup("");
573 *password = smb_xstrdup("");
577 bool secrets_store_generic(const char *owner, const char *key, const char *secret)
579 char *tdbkey = NULL;
580 bool ret;
582 if (asprintf(&tdbkey, "SECRETS/GENERIC/%s/%s", owner, key) < 0) {
583 DEBUG(0, ("asprintf failed!\n"));
584 return False;
587 ret = secrets_store(tdbkey, secret, strlen(secret)+1);
589 SAFE_FREE(tdbkey);
590 return ret;
593 bool secrets_delete_generic(const char *owner, const char *key)
595 char *tdbkey = NULL;
596 bool ret;
598 if (asprintf(&tdbkey, "SECRETS/GENERIC/%s/%s", owner, key) < 0) {
599 DEBUG(0, ("asprintf failed!\n"));
600 return False;
603 ret = secrets_delete(tdbkey);
605 SAFE_FREE(tdbkey);
606 return ret;
609 /*******************************************************************
610 Find the ldap password.
611 ******************************************************************/
613 char *secrets_fetch_generic(const char *owner, const char *key)
615 char *secret = NULL;
616 char *tdbkey = NULL;
618 if (( ! owner) || ( ! key)) {
619 DEBUG(1, ("Invalid Parameters"));
620 return NULL;
623 if (asprintf(&tdbkey, "SECRETS/GENERIC/%s/%s", owner, key) < 0) {
624 DEBUG(0, ("Out of memory!\n"));
625 return NULL;
628 secret = (char *)secrets_fetch(tdbkey, NULL);
629 SAFE_FREE(tdbkey);
631 return secret;