s3: Remove a call to procid_self()
[kai/samba.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 "lib/tsocket/tsocket.h"
31 #include "libcli/composite/composite.h"
32
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;
44 };
45
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);
59
60
61 /*
62  * find a list of DCs via DNS/CLDAP
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
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);
85                 }
86         } else {
87                 state->domain_name = NULL;
88         }
89
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);
94                 }
95         } else {
96                 state->domain_sid = NULL;
97         }
98
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);
103                 }
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);
111                         }
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);
117                         }
118                 }
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);
124         }
125
126         return req;
127 }
128
129
130 /*
131   we've been told the IP of the server, bypass name
132   resolution and go straight to CLDAP
133 */
134 static bool finddcs_cldap_ipaddress(struct finddcs_cldap_state *state, struct finddcs *io)
135 {
136         NTSTATUS status;
137
138         state->srv_addresses = talloc_array(state, const char *, 2);
139         if (tevent_req_nomem(state->srv_addresses, state->req)) {
140                 return false;
141         }
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;
145         }
146         state->srv_addresses[1] = NULL;
147         state->srv_address_index = 0;
148
149         finddcs_cldap_next_server(state);
150         return tevent_req_is_nterror(state->req, &status);
151 }
152
153 /*
154   start a SRV DNS lookup
155  */
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)
160 {
161         struct composite_context *creq;
162         struct nbt_name name;
163
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);
169         }
170
171         DEBUG(4,("finddcs: looking for SRV records for %s\n", state->srv_name));
172
173         make_nbt_name(&name, state->srv_name, 0);
174
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;
180         }
181         creq->async.fn = finddcs_cldap_srv_resolved;
182         creq->async.private_data = state;
183
184         return true;
185 }
186
187 /*
188   start a NBT name lookup for domain<1C>
189  */
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)
194 {
195         struct composite_context *creq;
196         struct nbt_name name;
197
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;
202         }
203         creq->async.fn = finddcs_cldap_name_resolved;
204         creq->async.private_data = state;
205         return true;
206 }
207
208 /*
209   fire off a CLDAP query to the next server
210  */
211 static void finddcs_cldap_next_server(struct finddcs_cldap_state *state)
212 {
213         struct tevent_req *subreq;
214         struct tsocket_address *dest;
215         int ret;
216         NTSTATUS status;
217
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;
222         }
223
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);
233         }
234         if (tevent_req_nterror(state->req, status)) {
235                 return;
236         }
237
238         status = cldap_socket_init(state, NULL, dest, &state->cldap);
239         if (tevent_req_nterror(state->req, status)) {
240                 return;
241         }
242
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;
247         }
248
249         if ((state->domain_name != NULL) && (strchr(state->domain_name, '.'))) {
250                 state->netlogon->in.realm = state->domain_name;
251         }
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;
256                 }
257         }
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;
264
265         DEBUG(4,("finddcs: performing CLDAP query on %s\n",
266                  state->srv_addresses[state->srv_address_index]));
267
268         subreq = cldap_netlogon_send(state, state->ev,
269                                      state->cldap, state->netlogon);
270         if (tevent_req_nomem(subreq, state->req)) {
271                 return;
272         }
273
274         tevent_req_set_callback(subreq, finddcs_cldap_netlogon_replied, state);
275 }
276
277
278 /*
279   we have a response from a CLDAP server for a netlogon request
280  */
281 static void finddcs_cldap_netlogon_replied(struct tevent_req *subreq)
282 {
283         struct finddcs_cldap_state *state;
284         NTSTATUS status;
285
286         state = tevent_req_callback_data(subreq, struct finddcs_cldap_state);
287
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;
295         }
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;
306         }
307
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));
311
312         tevent_req_done(state->req);
313 }
314
315 /*
316    handle NBT name lookup reply
317  */
318 static void finddcs_cldap_name_resolved(struct composite_context *ctx)
319 {
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;
324
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;
329         }
330
331         DEBUG(4,("finddcs: Found NBT <1c> server at %s\n", address));
332
333         state->srv_addresses = talloc_array(state, const char *, 2);
334         if (tevent_req_nomem(state->srv_addresses, state->req)) {
335                 return;
336         }
337         state->srv_addresses[0] = address;
338         state->srv_addresses[1] = NULL;
339
340         state->srv_address_index = 0;
341
342         finddcs_cldap_next_server(state);
343 }
344
345
346 /*
347  * Having got a DNS SRV answer, fire off the first CLDAP request
348  */
349 static void finddcs_cldap_srv_resolved(struct composite_context *ctx)
350 {
351         struct finddcs_cldap_state *state =
352                 talloc_get_type(ctx->async.private_data, struct finddcs_cldap_state);
353         NTSTATUS status;
354         unsigned i;
355
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;
360         }
361
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]));
364         }
365
366         state->srv_address_index = 0;
367
368         finddcs_cldap_next_server(state);
369 }
370
371
372 NTSTATUS finddcs_cldap_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx, struct finddcs *io)
373 {
374         struct finddcs_cldap_state *state = tevent_req_data(req, struct finddcs_cldap_state);
375         bool ok;
376         NTSTATUS status;
377
378         ok = tevent_req_poll(req, state->ev);
379         if (!ok) {
380                 talloc_free(req);
381                 return NT_STATUS_INTERNAL_ERROR;
382         }
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]);
388         }
389         tevent_req_received(req);
390         return status;
391 }
392
393 NTSTATUS finddcs_cldap(TALLOC_CTX *mem_ctx,
394                        struct finddcs *io,
395                        struct resolve_context *resolve_ctx,
396                        struct tevent_context *event_ctx)
397 {
398         NTSTATUS status;
399         struct tevent_req *req = finddcs_cldap_send(mem_ctx,
400                                                     io,
401                                                     resolve_ctx,
402                                                     event_ctx);
403         status = finddcs_cldap_recv(req, mem_ctx, io);
404         talloc_free(req);
405         return status;
406 }