Start to tidy-up check_user_ok().
[Samba/bb.git] / source4 / libcli / finddcs_cldap.c
blobbf8da4e483e615d5044f990cd79dd501e6a26b76
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"
32 #include "lib/util/util_net.h"
34 struct finddcs_cldap_state {
35 struct tevent_context *ev;
36 struct tevent_req *req;
37 const char *domain_name;
38 struct dom_sid *domain_sid;
39 const char *srv_name;
40 const char **srv_addresses;
41 uint32_t minimum_dc_flags;
42 uint32_t srv_address_index;
43 struct cldap_socket *cldap;
44 struct cldap_netlogon *netlogon;
47 static void finddcs_cldap_srv_resolved(struct composite_context *ctx);
48 static void finddcs_cldap_netlogon_replied(struct tevent_req *req);
49 static bool finddcs_cldap_srv_lookup(struct finddcs_cldap_state *state,
50 struct finddcs *io,
51 struct resolve_context *resolve_ctx,
52 struct tevent_context *event_ctx);
53 static bool finddcs_cldap_nbt_lookup(struct finddcs_cldap_state *state,
54 struct finddcs *io,
55 struct resolve_context *resolve_ctx,
56 struct tevent_context *event_ctx);
57 static void finddcs_cldap_nbt_resolved(struct composite_context *ctx);
58 static bool finddcs_cldap_name_lookup(struct finddcs_cldap_state *state,
59 struct finddcs *io,
60 struct resolve_context *resolve_ctx,
61 struct tevent_context *event_ctx);
62 static void finddcs_cldap_name_resolved(struct composite_context *ctx);
63 static void finddcs_cldap_next_server(struct finddcs_cldap_state *state);
64 static bool finddcs_cldap_ipaddress(struct finddcs_cldap_state *state, struct finddcs *io);
68 * find a list of DCs via DNS/CLDAP
70 struct tevent_req *finddcs_cldap_send(TALLOC_CTX *mem_ctx,
71 struct finddcs *io,
72 struct resolve_context *resolve_ctx,
73 struct tevent_context *event_ctx)
75 struct finddcs_cldap_state *state;
76 struct tevent_req *req;
78 req = tevent_req_create(mem_ctx, &state, struct finddcs_cldap_state);
79 if (req == NULL) {
80 return NULL;
83 state->req = req;
84 state->ev = event_ctx;
85 state->minimum_dc_flags = io->in.minimum_dc_flags;
87 if (io->in.domain_name) {
88 state->domain_name = talloc_strdup(state, io->in.domain_name);
89 if (tevent_req_nomem(state->domain_name, req)) {
90 return tevent_req_post(req, event_ctx);
92 } else {
93 state->domain_name = NULL;
96 if (io->in.domain_sid) {
97 state->domain_sid = dom_sid_dup(state, io->in.domain_sid);
98 if (tevent_req_nomem(state->domain_sid, req)) {
99 return tevent_req_post(req, event_ctx);
101 } else {
102 state->domain_sid = NULL;
105 if (io->in.server_address) {
106 if (is_ipaddress(io->in.server_address)) {
107 DEBUG(4,("finddcs: searching for a DC by IP %s\n",
108 io->in.server_address));
109 if (!finddcs_cldap_ipaddress(state, io)) {
110 return tevent_req_post(req, event_ctx);
112 } else {
113 if (!finddcs_cldap_name_lookup(state, io, resolve_ctx,
114 event_ctx)) {
115 return tevent_req_post(req, event_ctx);
118 } else if (io->in.domain_name) {
119 if (strchr(state->domain_name, '.')) {
120 /* looks like a DNS name */
121 DEBUG(4,("finddcs: searching for a DC by DNS domain %s\n", state->domain_name));
122 if (!finddcs_cldap_srv_lookup(state, io, resolve_ctx,
123 event_ctx)) {
124 return tevent_req_post(req, event_ctx);
126 } else {
127 DEBUG(4,("finddcs: searching for a DC by NBT lookup %s\n", state->domain_name));
128 if (!finddcs_cldap_nbt_lookup(state, io, resolve_ctx,
129 event_ctx)) {
130 return tevent_req_post(req, event_ctx);
133 } else {
134 /* either we have the domain name or the IP address */
135 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
136 DEBUG(2,("finddcs: Please specify at least the domain name or the IP address! \n"));
137 return tevent_req_post(req, event_ctx);
140 return req;
145 we've been told the IP of the server, bypass name
146 resolution and go straight to CLDAP
148 static bool finddcs_cldap_ipaddress(struct finddcs_cldap_state *state, struct finddcs *io)
150 NTSTATUS status;
152 state->srv_addresses = talloc_array(state, const char *, 2);
153 if (tevent_req_nomem(state->srv_addresses, state->req)) {
154 return false;
156 state->srv_addresses[0] = talloc_strdup(state->srv_addresses, io->in.server_address);
157 if (tevent_req_nomem(state->srv_addresses[0], state->req)) {
158 return false;
160 state->srv_addresses[1] = NULL;
161 state->srv_address_index = 0;
163 finddcs_cldap_next_server(state);
164 return tevent_req_is_nterror(state->req, &status);
168 start a SRV DNS lookup
170 static bool finddcs_cldap_srv_lookup(struct finddcs_cldap_state *state,
171 struct finddcs *io,
172 struct resolve_context *resolve_ctx,
173 struct tevent_context *event_ctx)
175 struct composite_context *creq;
176 struct nbt_name name;
178 if (io->in.site_name) {
179 state->srv_name = talloc_asprintf(state, "_ldap._tcp.%s._sites.%s",
180 io->in.site_name, io->in.domain_name);
181 } else {
182 state->srv_name = talloc_asprintf(state, "_ldap._tcp.%s", io->in.domain_name);
185 DEBUG(4,("finddcs: looking for SRV records for %s\n", state->srv_name));
187 make_nbt_name(&name, state->srv_name, 0);
189 creq = resolve_name_ex_send(resolve_ctx, state,
190 RESOLVE_NAME_FLAG_FORCE_DNS | RESOLVE_NAME_FLAG_DNS_SRV,
191 0, &name, event_ctx);
192 if (tevent_req_nomem(creq, state->req)) {
193 return false;
195 creq->async.fn = finddcs_cldap_srv_resolved;
196 creq->async.private_data = state;
198 return true;
202 start a NBT name lookup for domain<1C>
204 static bool finddcs_cldap_nbt_lookup(struct finddcs_cldap_state *state,
205 struct finddcs *io,
206 struct resolve_context *resolve_ctx,
207 struct tevent_context *event_ctx)
209 struct composite_context *creq;
210 struct nbt_name name;
212 make_nbt_name(&name, state->domain_name, NBT_NAME_LOGON);
213 creq = resolve_name_send(resolve_ctx, state, &name, event_ctx);
214 if (tevent_req_nomem(creq, state->req)) {
215 return false;
217 creq->async.fn = finddcs_cldap_nbt_resolved;
218 creq->async.private_data = state;
219 return true;
222 static bool finddcs_cldap_name_lookup(struct finddcs_cldap_state *state,
223 struct finddcs *io,
224 struct resolve_context *resolve_ctx,
225 struct tevent_context *event_ctx)
227 struct composite_context *creq;
228 struct nbt_name name;
230 make_nbt_name(&name, io->in.server_address, NBT_NAME_SERVER);
231 creq = resolve_name_send(resolve_ctx, state, &name, event_ctx);
232 if (tevent_req_nomem(creq, state->req)) {
233 return false;
235 creq->async.fn = finddcs_cldap_name_resolved;
236 creq->async.private_data = state;
237 return true;
241 fire off a CLDAP query to the next server
243 static void finddcs_cldap_next_server(struct finddcs_cldap_state *state)
245 struct tevent_req *subreq;
246 struct tsocket_address *dest;
247 int ret;
248 NTSTATUS status;
250 if (state->srv_addresses[state->srv_address_index] == NULL) {
251 tevent_req_nterror(state->req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
252 DEBUG(2,("finddcs: No matching CLDAP server found\n"));
253 return;
256 /* we should get the port from the SRV response */
257 ret = tsocket_address_inet_from_strings(state, "ip",
258 state->srv_addresses[state->srv_address_index],
259 389,
260 &dest);
261 if (ret == 0) {
262 status = NT_STATUS_OK;
263 } else {
264 status = map_nt_error_from_unix_common(errno);
266 if (tevent_req_nterror(state->req, status)) {
267 return;
270 status = cldap_socket_init(state, NULL, dest, &state->cldap);
271 if (tevent_req_nterror(state->req, status)) {
272 return;
275 TALLOC_FREE(state->netlogon);
276 state->netlogon = talloc_zero(state, struct cldap_netlogon);
277 if (tevent_req_nomem(state->netlogon, state->req)) {
278 return;
281 if ((state->domain_name != NULL) && (strchr(state->domain_name, '.'))) {
282 state->netlogon->in.realm = state->domain_name;
284 if (state->domain_sid) {
285 state->netlogon->in.domain_sid = dom_sid_string(state, state->domain_sid);
286 if (tevent_req_nomem(state->netlogon->in.domain_sid, state->req)) {
287 return;
290 state->netlogon->in.acct_control = -1;
291 state->netlogon->in.version =
292 NETLOGON_NT_VERSION_5 |
293 NETLOGON_NT_VERSION_5EX |
294 NETLOGON_NT_VERSION_IP;
295 state->netlogon->in.map_response = true;
297 DEBUG(4,("finddcs: performing CLDAP query on %s\n",
298 state->srv_addresses[state->srv_address_index]));
300 subreq = cldap_netlogon_send(state, state->ev,
301 state->cldap, state->netlogon);
302 if (tevent_req_nomem(subreq, state->req)) {
303 return;
306 tevent_req_set_callback(subreq, finddcs_cldap_netlogon_replied, state);
311 we have a response from a CLDAP server for a netlogon request
313 static void finddcs_cldap_netlogon_replied(struct tevent_req *subreq)
315 struct finddcs_cldap_state *state;
316 NTSTATUS status;
318 state = tevent_req_callback_data(subreq, struct finddcs_cldap_state);
320 status = cldap_netlogon_recv(subreq, state->netlogon, state->netlogon);
321 TALLOC_FREE(subreq);
322 TALLOC_FREE(state->cldap);
323 if (!NT_STATUS_IS_OK(status)) {
324 state->srv_address_index++;
325 finddcs_cldap_next_server(state);
326 return;
328 if (state->minimum_dc_flags !=
329 (state->minimum_dc_flags & state->netlogon->out.netlogon.data.nt5_ex.server_type)) {
330 /* the server didn't match the minimum requirements */
331 DEBUG(4,("finddcs: Skipping DC %s with server_type=0x%08x - required 0x%08x\n",
332 state->srv_addresses[state->srv_address_index],
333 state->netlogon->out.netlogon.data.nt5_ex.server_type,
334 state->minimum_dc_flags));
335 state->srv_address_index++;
336 finddcs_cldap_next_server(state);
337 return;
340 DEBUG(4,("finddcs: Found matching DC %s with server_type=0x%08x\n",
341 state->srv_addresses[state->srv_address_index],
342 state->netlogon->out.netlogon.data.nt5_ex.server_type));
344 tevent_req_done(state->req);
347 static void finddcs_cldap_name_resolved(struct composite_context *ctx)
349 struct finddcs_cldap_state *state =
350 talloc_get_type(ctx->async.private_data, struct finddcs_cldap_state);
351 NTSTATUS status;
352 unsigned i;
354 status = resolve_name_multiple_recv(ctx, state, &state->srv_addresses);
355 if (tevent_req_nterror(state->req, status)) {
356 DEBUG(2,("finddcs: No matching server found\n"));
357 return;
360 for (i=0; state->srv_addresses[i]; i++) {
361 DEBUG(4,("finddcs: response %u at '%s'\n",
362 i, state->srv_addresses[i]));
365 state->srv_address_index = 0;
367 finddcs_cldap_next_server(state);
371 handle NBT name lookup reply
373 static void finddcs_cldap_nbt_resolved(struct composite_context *ctx)
375 struct finddcs_cldap_state *state =
376 talloc_get_type(ctx->async.private_data, struct finddcs_cldap_state);
377 NTSTATUS status;
378 unsigned i;
380 status = resolve_name_multiple_recv(ctx, state, &state->srv_addresses);
381 if (tevent_req_nterror(state->req, status)) {
382 DEBUG(2,("finddcs: No matching NBT <1c> server found\n"));
383 return;
386 for (i=0; state->srv_addresses[i]; i++) {
387 DEBUG(4,("finddcs: NBT <1c> response %u at '%s'\n",
388 i, state->srv_addresses[i]));
391 state->srv_address_index = 0;
393 finddcs_cldap_next_server(state);
398 * Having got a DNS SRV answer, fire off the first CLDAP request
400 static void finddcs_cldap_srv_resolved(struct composite_context *ctx)
402 struct finddcs_cldap_state *state =
403 talloc_get_type(ctx->async.private_data, struct finddcs_cldap_state);
404 NTSTATUS status;
405 unsigned i;
407 status = resolve_name_multiple_recv(ctx, state, &state->srv_addresses);
408 if (tevent_req_nterror(state->req, status)) {
409 DEBUG(2,("finddcs: Failed to find SRV record for %s\n", state->srv_name));
410 return;
413 for (i=0; state->srv_addresses[i]; i++) {
414 DEBUG(4,("finddcs: DNS SRV response %u at '%s'\n", i, state->srv_addresses[i]));
417 state->srv_address_index = 0;
419 finddcs_cldap_next_server(state);
423 NTSTATUS finddcs_cldap_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx, struct finddcs *io)
425 struct finddcs_cldap_state *state = tevent_req_data(req, struct finddcs_cldap_state);
426 bool ok;
427 NTSTATUS status;
429 ok = tevent_req_poll(req, state->ev);
430 if (!ok) {
431 talloc_free(req);
432 return NT_STATUS_INTERNAL_ERROR;
434 status = tevent_req_simple_recv_ntstatus(req);
435 if (NT_STATUS_IS_OK(status)) {
436 talloc_steal(mem_ctx, state->netlogon);
437 io->out.netlogon = state->netlogon->out.netlogon;
438 io->out.address = talloc_steal(mem_ctx, state->srv_addresses[state->srv_address_index]);
440 tevent_req_received(req);
441 return status;
444 NTSTATUS finddcs_cldap(TALLOC_CTX *mem_ctx,
445 struct finddcs *io,
446 struct resolve_context *resolve_ctx,
447 struct tevent_context *event_ctx)
449 NTSTATUS status;
450 struct tevent_req *req = finddcs_cldap_send(mem_ctx,
452 resolve_ctx,
453 event_ctx);
454 status = finddcs_cldap_recv(req, mem_ctx, io);
455 talloc_free(req);
456 return status;