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"
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
;
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
,
51 struct resolve_context
*resolve_ctx
,
52 struct tevent_context
*event_ctx
);
53 static bool finddcs_cldap_nbt_lookup(struct finddcs_cldap_state
*state
,
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
,
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
,
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
);
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
);
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
);
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
);
113 if (!finddcs_cldap_name_lookup(state
, io
, resolve_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
,
124 return tevent_req_post(req
, event_ctx
);
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
,
130 return tevent_req_post(req
, event_ctx
);
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
);
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
)
152 state
->srv_addresses
= talloc_array(state
, const char *, 2);
153 if (tevent_req_nomem(state
->srv_addresses
, state
->req
)) {
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
)) {
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
,
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
);
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
)) {
195 creq
->async
.fn
= finddcs_cldap_srv_resolved
;
196 creq
->async
.private_data
= state
;
202 start a NBT name lookup for domain<1C>
204 static bool finddcs_cldap_nbt_lookup(struct finddcs_cldap_state
*state
,
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
)) {
217 creq
->async
.fn
= finddcs_cldap_nbt_resolved
;
218 creq
->async
.private_data
= state
;
222 static bool finddcs_cldap_name_lookup(struct finddcs_cldap_state
*state
,
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
)) {
235 creq
->async
.fn
= finddcs_cldap_name_resolved
;
236 creq
->async
.private_data
= state
;
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
;
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"));
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
],
262 status
= NT_STATUS_OK
;
264 status
= map_nt_error_from_unix_common(errno
);
266 if (tevent_req_nterror(state
->req
, status
)) {
270 status
= cldap_socket_init(state
, NULL
, dest
, &state
->cldap
);
271 if (tevent_req_nterror(state
->req
, status
)) {
275 TALLOC_FREE(state
->netlogon
);
276 state
->netlogon
= talloc_zero(state
, struct cldap_netlogon
);
277 if (tevent_req_nomem(state
->netlogon
, state
->req
)) {
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
)) {
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
)) {
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
;
318 state
= tevent_req_callback_data(subreq
, struct finddcs_cldap_state
);
320 status
= cldap_netlogon_recv(subreq
, state
->netlogon
, state
->netlogon
);
322 TALLOC_FREE(state
->cldap
);
323 if (!NT_STATUS_IS_OK(status
)) {
324 state
->srv_address_index
++;
325 finddcs_cldap_next_server(state
);
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
);
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
);
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"));
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
);
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"));
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
);
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
));
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
);
429 ok
= tevent_req_poll(req
, state
->ev
);
432 return NT_STATUS_INTERNAL_ERROR
;
434 if (tevent_req_is_nterror(req
, &status
)) {
435 tevent_req_received(req
);
439 talloc_steal(mem_ctx
, state
->netlogon
);
440 io
->out
.netlogon
= state
->netlogon
->out
.netlogon
;
441 io
->out
.address
= talloc_steal(
442 mem_ctx
, state
->srv_addresses
[state
->srv_address_index
]);
444 tevent_req_received(req
);
448 NTSTATUS
finddcs_cldap(TALLOC_CTX
*mem_ctx
,
450 struct resolve_context
*resolve_ctx
,
451 struct tevent_context
*event_ctx
)
454 struct tevent_req
*req
= finddcs_cldap_send(mem_ctx
,
458 status
= finddcs_cldap_recv(req
, mem_ctx
, io
);