util:datablob: data_blob_pad checks its alignment assumption
[samba.git] / source3 / winbindd / wb_dsgetdcname.c
blob14d5ee34d8419f442dfce76bc23844e02f6c7f22
1 /*
2 Unix SMB/CIFS implementation.
3 async dsgetdcname
4 Copyright (C) Volker Lendecke 2009
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "winbindd.h"
22 #include "librpc/gen_ndr/ndr_winbind_c.h"
23 #include "librpc/gen_ndr/ndr_netlogon.h"
24 #include "lib/gencache.h"
26 struct wb_dsgetdcname_state {
27 const char *domain_name;
28 struct GUID domain_guid;
29 struct netr_DsRGetDCNameInfo *dcinfo;
32 static void wb_dsgetdcname_done(struct tevent_req *subreq);
34 struct tevent_req *wb_dsgetdcname_send(TALLOC_CTX *mem_ctx,
35 struct tevent_context *ev,
36 const char *domain_name,
37 const struct GUID *domain_guid,
38 const char *site_name,
39 uint32_t flags)
41 struct tevent_req *req, *subreq;
42 struct wb_dsgetdcname_state *state;
43 struct dcerpc_binding_handle *child_binding_handle = NULL;
44 struct GUID *guid_ptr = NULL;
46 req = tevent_req_create(mem_ctx, &state, struct wb_dsgetdcname_state);
47 if (req == NULL) {
48 return NULL;
51 D_INFO("WB command dsgetdcname start.\n"
52 "Search domain name %s and site name %s.\n",
53 domain_name,
54 site_name);
55 if (strequal(domain_name, "BUILTIN")) {
57 * This makes no sense
59 tevent_req_nterror(req, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND);
60 return tevent_req_post(req, ev);
63 if (strequal(domain_name, get_global_sam_name())) {
64 int role = lp_server_role();
65 if ( role != ROLE_ACTIVE_DIRECTORY_DC ) {
67 * Two options here: Give back our own address, or say there's
68 * nobody around. Right now opting for the latter, one measure
69 * to prevent the loopback connects. This might change if
70 * needed.
72 tevent_req_nterror(req, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND);
73 return tevent_req_post(req, ev);
77 if (IS_DC) {
79 * We have to figure out the DC ourselves
81 child_binding_handle = locator_child_handle();
83 if (IS_AD_DC) {
84 struct winbindd_domain *domain = NULL;
87 * If we know about the domain
88 * we replace a possible netbios domain name to
89 * a dns domain name, which means we'll always use
90 * dns+cldap because we have a fixed known number
91 * of trusted domains on a DC.
93 domain = find_domain_from_name_noinit(domain_name);
94 if (domain != NULL && domain->active_directory) {
95 domain_name = domain->alt_name;
98 } else {
99 struct winbindd_domain *domain = find_our_domain();
100 child_binding_handle = dom_child_handle(domain);
103 if (domain_guid != NULL) {
104 /* work around a const issue in rpccli_ autogenerated code */
105 state->domain_guid = *domain_guid;
106 guid_ptr = &state->domain_guid;
109 state->domain_name = talloc_strdup(state, domain_name);
110 if (tevent_req_nomem(state->domain_name, req)) {
111 return tevent_req_post(req, ev);
114 subreq = dcerpc_wbint_DsGetDcName_send(
115 state, ev, child_binding_handle, domain_name, guid_ptr, site_name,
116 flags, &state->dcinfo);
117 if (tevent_req_nomem(subreq, req)) {
118 return tevent_req_post(req, ev);
120 tevent_req_set_callback(subreq, wb_dsgetdcname_done, req);
121 return req;
124 static void wb_dsgetdcname_done(struct tevent_req *subreq)
126 struct tevent_req *req = tevent_req_callback_data(
127 subreq, struct tevent_req);
128 struct wb_dsgetdcname_state *state = tevent_req_data(
129 req, struct wb_dsgetdcname_state);
130 NTSTATUS status, result;
132 status = dcerpc_wbint_DsGetDcName_recv(subreq, state, &result);
133 TALLOC_FREE(subreq);
134 if (any_nt_status_not_ok(status, result, &status)) {
135 tevent_req_nterror(req, status);
136 return;
138 tevent_req_done(req);
141 NTSTATUS wb_dsgetdcname_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
142 struct netr_DsRGetDCNameInfo **pdcinfo)
144 struct wb_dsgetdcname_state *state = tevent_req_data(
145 req, struct wb_dsgetdcname_state);
146 NTSTATUS status;
148 D_INFO("WB command dsgetdcname for %s end.\n",
149 state->domain_name);
150 if (tevent_req_is_nterror(req, &status)) {
151 D_NOTICE("Failed for %s with %s.\n",
152 state->domain_name,
153 nt_errstr(status));
154 return status;
156 *pdcinfo = talloc_move(mem_ctx, &state->dcinfo);
157 return NT_STATUS_OK;
160 NTSTATUS wb_dsgetdcname_gencache_set(const char *domname,
161 struct netr_DsRGetDCNameInfo *dcinfo)
163 DATA_BLOB blob;
164 enum ndr_err_code ndr_err;
165 char *key;
166 bool ok;
168 key = talloc_asprintf_strupper_m(talloc_tos(), "DCINFO/%s", domname);
169 if (key == NULL) {
170 return NT_STATUS_NO_MEMORY;
173 if (DEBUGLEVEL >= DBGLVL_DEBUG) {
174 NDR_PRINT_DEBUG(netr_DsRGetDCNameInfo, dcinfo);
177 ndr_err = ndr_push_struct_blob(
178 &blob, key, dcinfo,
179 (ndr_push_flags_fn_t)ndr_push_netr_DsRGetDCNameInfo);
180 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
181 NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
182 DBG_WARNING("ndr_push_struct_blob failed: %s\n",
183 ndr_errstr(ndr_err));
184 TALLOC_FREE(key);
185 return status;
188 ok = gencache_set_data_blob(key, blob, time(NULL)+3600);
190 if (!ok) {
191 DBG_WARNING("gencache_set_data_blob for key %s failed\n", key);
192 TALLOC_FREE(key);
193 return NT_STATUS_UNSUCCESSFUL;
196 TALLOC_FREE(key);
197 return NT_STATUS_OK;
200 struct dcinfo_parser_state {
201 NTSTATUS status;
202 TALLOC_CTX *mem_ctx;
203 struct netr_DsRGetDCNameInfo *dcinfo;
206 static void dcinfo_parser(const struct gencache_timeout *timeout,
207 DATA_BLOB blob,
208 void *private_data)
210 struct dcinfo_parser_state *state = private_data;
211 enum ndr_err_code ndr_err;
213 if (gencache_timeout_expired(timeout)) {
214 return;
217 state->dcinfo = talloc(state->mem_ctx, struct netr_DsRGetDCNameInfo);
218 if (state->dcinfo == NULL) {
219 state->status = NT_STATUS_NO_MEMORY;
220 return;
223 ndr_err = ndr_pull_struct_blob_all(
224 &blob, state->dcinfo, state->dcinfo,
225 (ndr_pull_flags_fn_t)ndr_pull_netr_DsRGetDCNameInfo);
227 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
228 DBG_ERR("ndr_pull_struct_blob failed\n");
229 state->status = ndr_map_error2ntstatus(ndr_err);
230 TALLOC_FREE(state->dcinfo);
231 return;
234 state->status = NT_STATUS_OK;
237 NTSTATUS wb_dsgetdcname_gencache_get(TALLOC_CTX *mem_ctx,
238 const char *domname,
239 struct netr_DsRGetDCNameInfo **dcinfo)
241 struct dcinfo_parser_state state;
242 char *key;
243 bool ok;
245 key = talloc_asprintf_strupper_m(mem_ctx, "DCINFO/%s", domname);
246 if (key == NULL) {
247 return NT_STATUS_NO_MEMORY;
250 state = (struct dcinfo_parser_state) {
251 .status = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND,
252 .mem_ctx = mem_ctx,
255 ok = gencache_parse(key, dcinfo_parser, &state);
256 TALLOC_FREE(key);
257 if (!ok) {
258 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
261 if (!NT_STATUS_IS_OK(state.status)) {
262 return state.status;
265 if (DEBUGLEVEL >= DBGLVL_DEBUG) {
266 NDR_PRINT_DEBUG(netr_DsRGetDCNameInfo, state.dcinfo);
269 *dcinfo = state.dcinfo;
270 return NT_STATUS_OK;