libcli/cldap: pass tevent_context to cldap_netlogon_send()
[sharpe/samba-autobuild/.git] / source4 / libcli / finddcs_cldap.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    a composite API for finding a DC and its name via CLDAP
5
6    Copyright (C) Andrew Tridgell 2010
7    Copyright (C) Andrew Bartlett 2010
8
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.
13
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.
18
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/>.
21 */
22
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 "libcli/composite/composite.h"
31
32 struct finddcs_cldap_state {
33         struct tevent_context *ev;
34         struct tevent_req *req;
35         const char *domain_name;
36         struct dom_sid *domain_sid;
37         const char *srv_name;
38         const char **srv_addresses;
39         uint32_t minimum_dc_flags;
40         uint32_t srv_address_index;
41         struct cldap_socket *cldap;
42         struct cldap_netlogon *netlogon;
43 };
44
45 static void finddcs_cldap_srv_resolved(struct composite_context *ctx);
46 static void finddcs_cldap_netlogon_replied(struct tevent_req *req);
47 static bool finddcs_cldap_srv_lookup(struct finddcs_cldap_state *state,
48                                      struct finddcs *io,
49                                      struct resolve_context *resolve_ctx,
50                                      struct tevent_context *event_ctx);
51 static bool finddcs_cldap_nbt_lookup(struct finddcs_cldap_state *state,
52                                      struct finddcs *io,
53                                      struct resolve_context *resolve_ctx,
54                                      struct tevent_context *event_ctx);
55 static void finddcs_cldap_name_resolved(struct composite_context *ctx);
56 static void finddcs_cldap_next_server(struct finddcs_cldap_state *state);
57 static bool finddcs_cldap_ipaddress(struct finddcs_cldap_state *state, struct finddcs *io);
58
59
60 /*
61  * find a list of DCs via DNS/CLDAP
62  *
63  */
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)
68 {
69         struct finddcs_cldap_state *state;
70         struct tevent_req *req;
71
72         req = tevent_req_create(mem_ctx, &state, struct finddcs_cldap_state);
73         if (req == NULL) {
74                 return NULL;
75         }
76
77         state->req = req;
78         state->ev = event_ctx;
79         state->minimum_dc_flags = io->in.minimum_dc_flags;
80         state->domain_name = talloc_strdup(state, io->in.domain_name);
81         if (tevent_req_nomem(state->domain_name, req)) {
82                 return tevent_req_post(req, event_ctx);
83         }
84
85         if (io->in.domain_sid) {
86                 state->domain_sid = dom_sid_dup(state, io->in.domain_sid);
87                 if (tevent_req_nomem(state->domain_sid, req)) {
88                         return tevent_req_post(req, event_ctx);
89                 }
90         } else {
91                 state->domain_sid = NULL;
92         }
93
94         if (io->in.server_address) {
95                 DEBUG(4,("finddcs: searching for a DC by IP %s\n", io->in.server_address));
96                 if (!finddcs_cldap_ipaddress(state, io)) {
97                         return tevent_req_post(req, event_ctx);
98                 }
99         } else if (strchr(state->domain_name, '.')) {
100                 /* looks like a DNS name */
101                 DEBUG(4,("finddcs: searching for a DC by DNS domain %s\n", state->domain_name));
102                 if (!finddcs_cldap_srv_lookup(state, io, resolve_ctx, event_ctx)) {
103                         return tevent_req_post(req, event_ctx);
104                 }
105         } else {
106                 DEBUG(4,("finddcs: searching for a DC by NBT lookup %s\n", state->domain_name));
107                 if (!finddcs_cldap_nbt_lookup(state, io, resolve_ctx, event_ctx)) {
108                         return tevent_req_post(req, event_ctx);
109                 }
110         }
111
112         return req;
113 }
114
115
116 /*
117   we've been told the IP of the server, bypass name
118   resolution and go straight to CLDAP
119 */
120 static bool finddcs_cldap_ipaddress(struct finddcs_cldap_state *state, struct finddcs *io)
121 {
122         NTSTATUS status;
123
124         state->srv_addresses = talloc_array(state, const char *, 2);
125         if (tevent_req_nomem(state->srv_addresses, state->req)) {
126                 return false;
127         }
128         state->srv_addresses[0] = talloc_strdup(state->srv_addresses, io->in.server_address);
129         if (tevent_req_nomem(state->srv_addresses[0], state->req)) {
130                 return false;
131         }
132         state->srv_addresses[1] = NULL;
133         state->srv_address_index = 0;
134         status = cldap_socket_init(state, state->ev, NULL, NULL, &state->cldap);
135         if (tevent_req_nterror(state->req, status)) {
136                 return false;
137         }
138
139         finddcs_cldap_next_server(state);
140         return tevent_req_is_nterror(state->req, &status);
141 }
142
143 /*
144   start a SRV DNS lookup
145  */
146 static bool finddcs_cldap_srv_lookup(struct finddcs_cldap_state *state,
147                                      struct finddcs *io,
148                                      struct resolve_context *resolve_ctx,
149                                      struct tevent_context *event_ctx)
150 {
151         struct composite_context *creq;
152         struct nbt_name name;
153
154         if (io->in.site_name) {
155                 state->srv_name = talloc_asprintf(state, "_ldap._tcp.%s._sites.%s",
156                                            io->in.site_name, io->in.domain_name);
157         } else {
158                 state->srv_name = talloc_asprintf(state, "_ldap._tcp.%s", io->in.domain_name);
159         }
160
161         DEBUG(4,("finddcs: looking for SRV records for %s\n", state->srv_name));
162
163         make_nbt_name(&name, state->srv_name, 0);
164
165         creq = resolve_name_ex_send(resolve_ctx, state,
166                                     RESOLVE_NAME_FLAG_FORCE_DNS | RESOLVE_NAME_FLAG_DNS_SRV,
167                                     0, &name, event_ctx);
168         if (tevent_req_nomem(creq, state->req)) {
169                 return false;
170         }
171         creq->async.fn = finddcs_cldap_srv_resolved;
172         creq->async.private_data = state;
173
174         return true;
175 }
176
177 /*
178   start a NBT name lookup for domain<1C>
179  */
180 static bool finddcs_cldap_nbt_lookup(struct finddcs_cldap_state *state,
181                                      struct finddcs *io,
182                                      struct resolve_context *resolve_ctx,
183                                      struct tevent_context *event_ctx)
184 {
185         struct composite_context *creq;
186         struct nbt_name name;
187
188         make_nbt_name(&name, state->domain_name, NBT_NAME_LOGON);
189         creq = resolve_name_send(resolve_ctx, state, &name, event_ctx);
190         if (tevent_req_nomem(creq, state->req)) {
191                 return false;
192         }
193         creq->async.fn = finddcs_cldap_name_resolved;
194         creq->async.private_data = state;
195         return true;
196 }
197
198 /*
199   fire off a CLDAP query to the next server
200  */
201 static void finddcs_cldap_next_server(struct finddcs_cldap_state *state)
202 {
203         struct tevent_req *subreq;
204
205         if (state->srv_addresses[state->srv_address_index] == NULL) {
206                 tevent_req_nterror(state->req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
207                 DEBUG(2,("finddcs: No matching CLDAP server found\n"));
208                 return;
209         }
210
211         state->netlogon = talloc_zero(state, struct cldap_netlogon);
212         if (tevent_req_nomem(state->netlogon, state->req)) {
213                 return;
214         }
215
216         state->netlogon->in.dest_address = state->srv_addresses[state->srv_address_index];
217         /* we should get the port from the SRV response */
218         state->netlogon->in.dest_port = 389;
219         if (strchr(state->domain_name, '.')) {
220                 state->netlogon->in.realm = state->domain_name;
221         }
222         if (state->domain_sid) {
223                 state->netlogon->in.domain_sid = dom_sid_string(state, state->domain_sid);
224                 if (tevent_req_nomem(state->netlogon->in.domain_sid, state->req)) {
225                         return;
226                 }
227         }
228         state->netlogon->in.acct_control = -1;
229         state->netlogon->in.version =
230                 NETLOGON_NT_VERSION_5 |
231                 NETLOGON_NT_VERSION_5EX |
232                 NETLOGON_NT_VERSION_IP;
233         state->netlogon->in.map_response = true;
234
235         DEBUG(4,("finddcs: performing CLDAP query on %s\n", state->netlogon->in.dest_address));
236
237         subreq = cldap_netlogon_send(state, state->ev,
238                                      state->cldap, state->netlogon);
239         if (tevent_req_nomem(subreq, state->req)) {
240                 return;
241         }
242
243         tevent_req_set_callback(subreq, finddcs_cldap_netlogon_replied, state);
244 }
245
246
247 /*
248   we have a response from a CLDAP server for a netlogon request
249  */
250 static void finddcs_cldap_netlogon_replied(struct tevent_req *subreq)
251 {
252         struct finddcs_cldap_state *state;
253         NTSTATUS status;
254
255         state = tevent_req_callback_data(subreq, struct finddcs_cldap_state);
256
257         status = cldap_netlogon_recv(subreq, state->netlogon, state->netlogon);
258         talloc_free(subreq);
259         if (!NT_STATUS_IS_OK(status)) {
260                 state->srv_address_index++;
261                 finddcs_cldap_next_server(state);
262                 return;
263         }
264         if (state->minimum_dc_flags !=
265             (state->minimum_dc_flags & state->netlogon->out.netlogon.data.nt5_ex.server_type)) {
266                 /* the server didn't match the minimum requirements */
267                 DEBUG(4,("finddcs: Skipping DC %s with server_type=0x%08x - required 0x%08x\n",
268                          state->srv_addresses[state->srv_address_index],
269                          state->netlogon->out.netlogon.data.nt5_ex.server_type,
270                          state->minimum_dc_flags));
271                 state->srv_address_index++;
272                 finddcs_cldap_next_server(state);
273                 return;
274         }
275
276         DEBUG(4,("finddcs: Found matching DC %s with server_type=0x%08x\n",
277                  state->srv_addresses[state->srv_address_index],
278                  state->netlogon->out.netlogon.data.nt5_ex.server_type));
279
280         tevent_req_done(state->req);
281 }
282
283 /*
284    handle NBT name lookup reply
285  */
286 static void finddcs_cldap_name_resolved(struct composite_context *ctx)
287 {
288         struct finddcs_cldap_state *state =
289                 talloc_get_type(ctx->async.private_data, struct finddcs_cldap_state);
290         const char *address;
291         NTSTATUS status;
292
293         status = resolve_name_recv(ctx, state, &address);
294         if (tevent_req_nterror(state->req, status)) {
295                 DEBUG(2,("finddcs: No matching NBT <1c> server found\n"));
296                 return;
297         }
298
299         DEBUG(4,("finddcs: Found NBT <1c> server at %s\n", address));
300
301         state->srv_addresses = talloc_array(state, const char *, 2);
302         if (tevent_req_nomem(state->srv_addresses, state->req)) {
303                 return;
304         }
305         state->srv_addresses[0] = address;
306         state->srv_addresses[1] = NULL;
307
308         state->srv_address_index = 0;
309
310         status = cldap_socket_init(state, state->ev, NULL, NULL, &state->cldap);
311         if (tevent_req_nterror(state->req, status)) {
312                 return;
313         }
314
315         finddcs_cldap_next_server(state);
316 }
317
318
319 /*
320  * Having got a DNS SRV answer, fire off the first CLDAP request
321  */
322 static void finddcs_cldap_srv_resolved(struct composite_context *ctx)
323 {
324         struct finddcs_cldap_state *state =
325                 talloc_get_type(ctx->async.private_data, struct finddcs_cldap_state);
326         NTSTATUS status;
327         unsigned i;
328
329         status = resolve_name_multiple_recv(ctx, state, &state->srv_addresses);
330         if (tevent_req_nterror(state->req, status)) {
331                 DEBUG(2,("finddcs: Failed to find SRV record for %s\n", state->srv_name));
332                 return;
333         }
334
335         for (i=0; state->srv_addresses[i]; i++) {
336                 DEBUG(4,("finddcs: DNS SRV response %u at '%s'\n", i, state->srv_addresses[i]));
337         }
338
339         state->srv_address_index = 0;
340
341         status = cldap_socket_init(state, state->ev, NULL, NULL, &state->cldap);
342         if (tevent_req_nterror(state->req, status)) {
343                 return;
344         }
345
346         finddcs_cldap_next_server(state);
347 }
348
349
350 NTSTATUS finddcs_cldap_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx, struct finddcs *io)
351 {
352         struct finddcs_cldap_state *state = tevent_req_data(req, struct finddcs_cldap_state);
353         bool ok;
354         NTSTATUS status;
355
356         ok = tevent_req_poll(req, state->ev);
357         if (!ok) {
358                 talloc_free(req);
359                 return NT_STATUS_INTERNAL_ERROR;
360         }
361         status = tevent_req_simple_recv_ntstatus(req);
362         if (NT_STATUS_IS_OK(status)) {
363                 talloc_steal(mem_ctx, state->netlogon);
364                 io->out.netlogon = state->netlogon->out.netlogon;
365                 io->out.address = talloc_steal(mem_ctx, state->srv_addresses[state->srv_address_index]);
366         }
367         tevent_req_received(req);
368         return status;
369 }
370
371 NTSTATUS finddcs_cldap(TALLOC_CTX *mem_ctx,
372                        struct finddcs *io,
373                        struct resolve_context *resolve_ctx,
374                        struct tevent_context *event_ctx)
375 {
376         NTSTATUS status;
377         struct tevent_req *req = finddcs_cldap_send(mem_ctx,
378                                                     io,
379                                                     resolve_ctx,
380                                                     event_ctx);
381         status = finddcs_cldap_recv(req, mem_ctx, io);
382         talloc_free(req);
383         return status;
384 }