Rework recursive waf build to be a selftest-enabled not a developer build
[Samba/gebeck_regimport.git] / source4 / libcli / finddcs_cldap.c
blob38e828fa987f534f09adbdc9e595b43c0209764f
1 /*
2 Unix SMB/CIFS implementation.
4 a composite API for finding a DC and its name via CLDAP
6 Copyright (C) Andrew Tridgell 2010
7 Copyright (C) Andrew Bartlett 2010
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "include/includes.h"
24 #include <tevent.h>
25 #include "libcli/resolve/resolve.h"
26 #include "libcli/cldap/cldap.h"
27 #include "libcli/finddc.h"
28 #include "libcli/security/security.h"
29 #include "lib/util/tevent_ntstatus.h"
30 #include "lib/tsocket/tsocket.h"
31 #include "libcli/composite/composite.h"
33 struct finddcs_cldap_state {
34 struct tevent_context *ev;
35 struct tevent_req *req;
36 const char *domain_name;
37 struct dom_sid *domain_sid;
38 const char *srv_name;
39 const char **srv_addresses;
40 uint32_t minimum_dc_flags;
41 uint32_t srv_address_index;
42 struct cldap_socket *cldap;
43 struct cldap_netlogon *netlogon;
46 static void finddcs_cldap_srv_resolved(struct composite_context *ctx);
47 static void finddcs_cldap_netlogon_replied(struct tevent_req *req);
48 static bool finddcs_cldap_srv_lookup(struct finddcs_cldap_state *state,
49 struct finddcs *io,
50 struct resolve_context *resolve_ctx,
51 struct tevent_context *event_ctx);
52 static bool finddcs_cldap_nbt_lookup(struct finddcs_cldap_state *state,
53 struct finddcs *io,
54 struct resolve_context *resolve_ctx,
55 struct tevent_context *event_ctx);
56 static void finddcs_cldap_name_resolved(struct composite_context *ctx);
57 static void finddcs_cldap_next_server(struct finddcs_cldap_state *state);
58 static bool finddcs_cldap_ipaddress(struct finddcs_cldap_state *state, struct finddcs *io);
62 * find a list of DCs via DNS/CLDAP
64 struct tevent_req *finddcs_cldap_send(TALLOC_CTX *mem_ctx,
65 struct finddcs *io,
66 struct resolve_context *resolve_ctx,
67 struct tevent_context *event_ctx)
69 struct finddcs_cldap_state *state;
70 struct tevent_req *req;
72 req = tevent_req_create(mem_ctx, &state, struct finddcs_cldap_state);
73 if (req == NULL) {
74 return NULL;
77 state->req = req;
78 state->ev = event_ctx;
79 state->minimum_dc_flags = io->in.minimum_dc_flags;
81 if (io->in.domain_name) {
82 state->domain_name = talloc_strdup(state, io->in.domain_name);
83 if (tevent_req_nomem(state->domain_name, req)) {
84 return tevent_req_post(req, event_ctx);
86 } else {
87 state->domain_name = NULL;
90 if (io->in.domain_sid) {
91 state->domain_sid = dom_sid_dup(state, io->in.domain_sid);
92 if (tevent_req_nomem(state->domain_sid, req)) {
93 return tevent_req_post(req, event_ctx);
95 } else {
96 state->domain_sid = NULL;
99 if (io->in.server_address) {
100 DEBUG(4,("finddcs: searching for a DC by IP %s\n", io->in.server_address));
101 if (!finddcs_cldap_ipaddress(state, io)) {
102 return tevent_req_post(req, event_ctx);
104 } else if (io->in.domain_name) {
105 if (strchr(state->domain_name, '.')) {
106 /* looks like a DNS name */
107 DEBUG(4,("finddcs: searching for a DC by DNS domain %s\n", state->domain_name));
108 if (!finddcs_cldap_srv_lookup(state, io, resolve_ctx,
109 event_ctx)) {
110 return tevent_req_post(req, event_ctx);
112 } else {
113 DEBUG(4,("finddcs: searching for a DC by NBT lookup %s\n", state->domain_name));
114 if (!finddcs_cldap_nbt_lookup(state, io, resolve_ctx,
115 event_ctx)) {
116 return tevent_req_post(req, event_ctx);
119 } else {
120 /* either we have the domain name or the IP address */
121 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
122 DEBUG(2,("finddcs: Please specify at least the domain name or the IP address! \n"));
123 return tevent_req_post(req, event_ctx);
126 return req;
131 we've been told the IP of the server, bypass name
132 resolution and go straight to CLDAP
134 static bool finddcs_cldap_ipaddress(struct finddcs_cldap_state *state, struct finddcs *io)
136 NTSTATUS status;
138 state->srv_addresses = talloc_array(state, const char *, 2);
139 if (tevent_req_nomem(state->srv_addresses, state->req)) {
140 return false;
142 state->srv_addresses[0] = talloc_strdup(state->srv_addresses, io->in.server_address);
143 if (tevent_req_nomem(state->srv_addresses[0], state->req)) {
144 return false;
146 state->srv_addresses[1] = NULL;
147 state->srv_address_index = 0;
149 finddcs_cldap_next_server(state);
150 return tevent_req_is_nterror(state->req, &status);
154 start a SRV DNS lookup
156 static bool finddcs_cldap_srv_lookup(struct finddcs_cldap_state *state,
157 struct finddcs *io,
158 struct resolve_context *resolve_ctx,
159 struct tevent_context *event_ctx)
161 struct composite_context *creq;
162 struct nbt_name name;
164 if (io->in.site_name) {
165 state->srv_name = talloc_asprintf(state, "_ldap._tcp.%s._sites.%s",
166 io->in.site_name, io->in.domain_name);
167 } else {
168 state->srv_name = talloc_asprintf(state, "_ldap._tcp.%s", io->in.domain_name);
171 DEBUG(4,("finddcs: looking for SRV records for %s\n", state->srv_name));
173 make_nbt_name(&name, state->srv_name, 0);
175 creq = resolve_name_ex_send(resolve_ctx, state,
176 RESOLVE_NAME_FLAG_FORCE_DNS | RESOLVE_NAME_FLAG_DNS_SRV,
177 0, &name, event_ctx);
178 if (tevent_req_nomem(creq, state->req)) {
179 return false;
181 creq->async.fn = finddcs_cldap_srv_resolved;
182 creq->async.private_data = state;
184 return true;
188 start a NBT name lookup for domain<1C>
190 static bool finddcs_cldap_nbt_lookup(struct finddcs_cldap_state *state,
191 struct finddcs *io,
192 struct resolve_context *resolve_ctx,
193 struct tevent_context *event_ctx)
195 struct composite_context *creq;
196 struct nbt_name name;
198 make_nbt_name(&name, state->domain_name, NBT_NAME_LOGON);
199 creq = resolve_name_send(resolve_ctx, state, &name, event_ctx);
200 if (tevent_req_nomem(creq, state->req)) {
201 return false;
203 creq->async.fn = finddcs_cldap_name_resolved;
204 creq->async.private_data = state;
205 return true;
209 fire off a CLDAP query to the next server
211 static void finddcs_cldap_next_server(struct finddcs_cldap_state *state)
213 struct tevent_req *subreq;
214 struct tsocket_address *dest;
215 int ret;
216 NTSTATUS status;
218 if (state->srv_addresses[state->srv_address_index] == NULL) {
219 tevent_req_nterror(state->req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
220 DEBUG(2,("finddcs: No matching CLDAP server found\n"));
221 return;
224 /* we should get the port from the SRV response */
225 ret = tsocket_address_inet_from_strings(state, "ip",
226 state->srv_addresses[state->srv_address_index],
227 389,
228 &dest);
229 if (ret == 0) {
230 status = NT_STATUS_OK;
231 } else {
232 status = map_nt_error_from_unix_common(errno);
234 if (tevent_req_nterror(state->req, status)) {
235 return;
238 status = cldap_socket_init(state, NULL, dest, &state->cldap);
239 if (tevent_req_nterror(state->req, status)) {
240 return;
243 TALLOC_FREE(state->netlogon);
244 state->netlogon = talloc_zero(state, struct cldap_netlogon);
245 if (tevent_req_nomem(state->netlogon, state->req)) {
246 return;
249 if ((state->domain_name != NULL) && (strchr(state->domain_name, '.'))) {
250 state->netlogon->in.realm = state->domain_name;
252 if (state->domain_sid) {
253 state->netlogon->in.domain_sid = dom_sid_string(state, state->domain_sid);
254 if (tevent_req_nomem(state->netlogon->in.domain_sid, state->req)) {
255 return;
258 state->netlogon->in.acct_control = -1;
259 state->netlogon->in.version =
260 NETLOGON_NT_VERSION_5 |
261 NETLOGON_NT_VERSION_5EX |
262 NETLOGON_NT_VERSION_IP;
263 state->netlogon->in.map_response = true;
265 DEBUG(4,("finddcs: performing CLDAP query on %s\n",
266 state->srv_addresses[state->srv_address_index]));
268 subreq = cldap_netlogon_send(state, state->ev,
269 state->cldap, state->netlogon);
270 if (tevent_req_nomem(subreq, state->req)) {
271 return;
274 tevent_req_set_callback(subreq, finddcs_cldap_netlogon_replied, state);
279 we have a response from a CLDAP server for a netlogon request
281 static void finddcs_cldap_netlogon_replied(struct tevent_req *subreq)
283 struct finddcs_cldap_state *state;
284 NTSTATUS status;
286 state = tevent_req_callback_data(subreq, struct finddcs_cldap_state);
288 status = cldap_netlogon_recv(subreq, state->netlogon, state->netlogon);
289 TALLOC_FREE(subreq);
290 TALLOC_FREE(state->cldap);
291 if (!NT_STATUS_IS_OK(status)) {
292 state->srv_address_index++;
293 finddcs_cldap_next_server(state);
294 return;
296 if (state->minimum_dc_flags !=
297 (state->minimum_dc_flags & state->netlogon->out.netlogon.data.nt5_ex.server_type)) {
298 /* the server didn't match the minimum requirements */
299 DEBUG(4,("finddcs: Skipping DC %s with server_type=0x%08x - required 0x%08x\n",
300 state->srv_addresses[state->srv_address_index],
301 state->netlogon->out.netlogon.data.nt5_ex.server_type,
302 state->minimum_dc_flags));
303 state->srv_address_index++;
304 finddcs_cldap_next_server(state);
305 return;
308 DEBUG(4,("finddcs: Found matching DC %s with server_type=0x%08x\n",
309 state->srv_addresses[state->srv_address_index],
310 state->netlogon->out.netlogon.data.nt5_ex.server_type));
312 tevent_req_done(state->req);
316 handle NBT name lookup reply
318 static void finddcs_cldap_name_resolved(struct composite_context *ctx)
320 struct finddcs_cldap_state *state =
321 talloc_get_type(ctx->async.private_data, struct finddcs_cldap_state);
322 const char *address;
323 NTSTATUS status;
325 status = resolve_name_recv(ctx, state, &address);
326 if (tevent_req_nterror(state->req, status)) {
327 DEBUG(2,("finddcs: No matching NBT <1c> server found\n"));
328 return;
331 DEBUG(4,("finddcs: Found NBT <1c> server at %s\n", address));
333 state->srv_addresses = talloc_array(state, const char *, 2);
334 if (tevent_req_nomem(state->srv_addresses, state->req)) {
335 return;
337 state->srv_addresses[0] = address;
338 state->srv_addresses[1] = NULL;
340 state->srv_address_index = 0;
342 finddcs_cldap_next_server(state);
347 * Having got a DNS SRV answer, fire off the first CLDAP request
349 static void finddcs_cldap_srv_resolved(struct composite_context *ctx)
351 struct finddcs_cldap_state *state =
352 talloc_get_type(ctx->async.private_data, struct finddcs_cldap_state);
353 NTSTATUS status;
354 unsigned i;
356 status = resolve_name_multiple_recv(ctx, state, &state->srv_addresses);
357 if (tevent_req_nterror(state->req, status)) {
358 DEBUG(2,("finddcs: Failed to find SRV record for %s\n", state->srv_name));
359 return;
362 for (i=0; state->srv_addresses[i]; i++) {
363 DEBUG(4,("finddcs: DNS SRV response %u at '%s'\n", i, state->srv_addresses[i]));
366 state->srv_address_index = 0;
368 finddcs_cldap_next_server(state);
372 NTSTATUS finddcs_cldap_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx, struct finddcs *io)
374 struct finddcs_cldap_state *state = tevent_req_data(req, struct finddcs_cldap_state);
375 bool ok;
376 NTSTATUS status;
378 ok = tevent_req_poll(req, state->ev);
379 if (!ok) {
380 talloc_free(req);
381 return NT_STATUS_INTERNAL_ERROR;
383 status = tevent_req_simple_recv_ntstatus(req);
384 if (NT_STATUS_IS_OK(status)) {
385 talloc_steal(mem_ctx, state->netlogon);
386 io->out.netlogon = state->netlogon->out.netlogon;
387 io->out.address = talloc_steal(mem_ctx, state->srv_addresses[state->srv_address_index]);
389 tevent_req_received(req);
390 return status;
393 NTSTATUS finddcs_cldap(TALLOC_CTX *mem_ctx,
394 struct finddcs *io,
395 struct resolve_context *resolve_ctx,
396 struct tevent_context *event_ctx)
398 NTSTATUS status;
399 struct tevent_req *req = finddcs_cldap_send(mem_ctx,
401 resolve_ctx,
402 event_ctx);
403 status = finddcs_cldap_recv(req, mem_ctx, io);
404 talloc_free(req);
405 return status;