s3:smbd: move global smbd_msg_state to smbXsrv_connection
[Samba/gebeck_regimport.git] / source3 / passdb / pdb_secrets.c
blob30262c999f2fcf4356859a609917bd1b8844d390
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 "passdb.h"
27 #include "passdb/pdb_secrets.h"
28 #include "librpc/gen_ndr/ndr_secrets.h"
29 #include "secrets.h"
30 #include "dbwrap/dbwrap.h"
31 #include "dbwrap/dbwrap_open.h"
32 #include "../libcli/security/security.h"
33 #include "util_tdb.h"
35 #undef DBGC_CLASS
36 #define DBGC_CLASS DBGC_PASSDB
38 /**
39 * Get trusted domains info from secrets.tdb.
40 **/
42 struct list_trusted_domains_state {
43 uint32 num_domains;
44 struct trustdom_info **domains;
47 static int list_trusted_domain(struct db_record *rec, void *private_data)
49 const size_t prefix_len = strlen(SECRETS_DOMTRUST_ACCT_PASS);
50 struct TRUSTED_DOM_PASS pass;
51 enum ndr_err_code ndr_err;
52 DATA_BLOB blob;
53 struct trustdom_info *dom_info;
54 TDB_DATA key;
55 TDB_DATA value;
57 struct list_trusted_domains_state *state =
58 (struct list_trusted_domains_state *)private_data;
60 key = dbwrap_record_get_key(rec);
61 value = dbwrap_record_get_value(rec);
63 if ((key.dsize < prefix_len)
64 || (strncmp((char *)key.dptr, SECRETS_DOMTRUST_ACCT_PASS,
65 prefix_len) != 0)) {
66 return 0;
69 blob = data_blob_const(value.dptr, value.dsize);
71 ndr_err = ndr_pull_struct_blob(&blob, talloc_tos(), &pass,
72 (ndr_pull_flags_fn_t)ndr_pull_TRUSTED_DOM_PASS);
73 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
74 return false;
77 if (pass.domain_sid.num_auths != 4) {
78 DEBUG(0, ("SID %s is not a domain sid, has %d "
79 "auths instead of 4\n",
80 sid_string_dbg(&pass.domain_sid),
81 pass.domain_sid.num_auths));
82 return 0;
85 if (!(dom_info = talloc(state->domains, struct trustdom_info))) {
86 DEBUG(0, ("talloc failed\n"));
87 return 0;
90 dom_info->name = talloc_strdup(dom_info, pass.uni_name);
91 if (!dom_info->name) {
92 TALLOC_FREE(dom_info);
93 return 0;
96 sid_copy(&dom_info->sid, &pass.domain_sid);
98 ADD_TO_ARRAY(state->domains, struct trustdom_info *, dom_info,
99 &state->domains, &state->num_domains);
101 if (state->domains == NULL) {
102 state->num_domains = 0;
103 return -1;
105 return 0;
108 NTSTATUS secrets_trusted_domains(TALLOC_CTX *mem_ctx, uint32 *num_domains,
109 struct trustdom_info ***domains)
111 struct list_trusted_domains_state state;
112 struct db_context *db_ctx;
114 if (!secrets_init()) {
115 return NT_STATUS_ACCESS_DENIED;
118 db_ctx = secrets_db_ctx();
120 state.num_domains = 0;
123 * Make sure that a talloc context for the trustdom_info structs
124 * exists
127 if (!(state.domains = talloc_array(
128 mem_ctx, struct trustdom_info *, 1))) {
129 return NT_STATUS_NO_MEMORY;
132 dbwrap_traverse_read(db_ctx, list_trusted_domain, (void *)&state, NULL);
134 *num_domains = state.num_domains;
135 *domains = state.domains;
136 return NT_STATUS_OK;