Merge branch 'v4-0-stable' into newmaster
[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 "lib/messaging/messaging.h"
27 #include "libcli/libcli.h"
28 #include "libcli/cldap/cldap.h"
29 #include "libcli/finddc.h"
30 #include "libcli/security/dom_sid.h"
31 #include "lib/util/tevent_ntstatus.h"
32 #include "libcli/composite/composite.h"
33
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;
45 };
46
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_name_resolved(struct composite_context *ctx);
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 (strchr(state->domain_name, '.')) {
95                 /* looks like a DNS name */
96                 if (!finddcs_cldap_srv_lookup(state, io, resolve_ctx, event_ctx)) {
97                         return tevent_req_post(req, event_ctx);
98                 }
99         } else {
100                 if (!finddcs_cldap_nbt_lookup(state, io, resolve_ctx, event_ctx)) {
101                         return tevent_req_post(req, event_ctx);
102                 }
103         }
104
105         return req;
106 }
107
108 /*
109   start a SRV DNS lookup
110  */
111 static bool finddcs_cldap_srv_lookup(struct finddcs_cldap_state *state,
112                                      struct finddcs *io,
113                                      struct resolve_context *resolve_ctx,
114                                      struct tevent_context *event_ctx)
115 {
116         struct composite_context *creq;
117         struct nbt_name name;
118
119         if (io->in.site_name) {
120                 state->srv_name = talloc_asprintf(state, "_ldap._tcp.%s._sites.%s",
121                                            io->in.site_name, io->in.domain_name);
122         } else {
123                 state->srv_name = talloc_asprintf(state, "_ldap._tcp.%s", io->in.domain_name);
124         }
125
126         make_nbt_name(&name, state->srv_name, 0);
127
128         creq = resolve_name_ex_send(resolve_ctx, state,
129                                     RESOLVE_NAME_FLAG_FORCE_DNS | RESOLVE_NAME_FLAG_DNS_SRV,
130                                     0, &name, event_ctx);
131         if (tevent_req_nomem(creq, state->req)) {
132                 return false;
133         }
134         creq->async.fn = finddcs_cldap_srv_resolved;
135         creq->async.private_data = state;
136
137         return true;
138 }
139
140 /*
141   start a NBT name lookup for domain<1C>
142  */
143 static bool finddcs_cldap_nbt_lookup(struct finddcs_cldap_state *state,
144                                      struct finddcs *io,
145                                      struct resolve_context *resolve_ctx,
146                                      struct tevent_context *event_ctx)
147 {
148         struct composite_context *creq;
149         struct nbt_name name;
150
151         make_nbt_name(&name, state->domain_name, NBT_NAME_LOGON);
152         creq = resolve_name_send(resolve_ctx, state, &name, event_ctx);
153         if (tevent_req_nomem(creq, state->req)) {
154                 return false;
155         }
156         creq->async.fn = finddcs_cldap_name_resolved;
157         creq->async.private_data = state;
158         return true;
159 }
160
161 /*
162   fire off a CLDAP query to the next server
163  */
164 static void finddcs_cldap_next_server(struct finddcs_cldap_state *state)
165 {
166         struct tevent_req *subreq;
167
168         if (state->srv_addresses[state->srv_address_index] == NULL) {
169                 tevent_req_nterror(state->req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
170                 DEBUG(2,("finddcs: No matching CLDAP server found\n"));
171                 return;
172         }
173
174         state->netlogon = talloc_zero(state, struct cldap_netlogon);
175         if (tevent_req_nomem(state->netlogon, state->req)) {
176                 return;
177         }
178
179         state->netlogon->in.dest_address = state->srv_addresses[state->srv_address_index];
180         /* we should get the port from the SRV response */
181         state->netlogon->in.dest_port = 389;
182         if (strchr(state->domain_name, '.')) {
183                 state->netlogon->in.realm = state->domain_name;
184         }
185         if (state->domain_sid) {
186                 state->netlogon->in.domain_sid = dom_sid_string(state, state->domain_sid);
187                 if (tevent_req_nomem(state->netlogon->in.domain_sid, state->req)) {
188                         return;
189                 }
190         }
191         state->netlogon->in.acct_control = -1;
192         state->netlogon->in.version =
193                 NETLOGON_NT_VERSION_5 |
194                 NETLOGON_NT_VERSION_5EX |
195                 NETLOGON_NT_VERSION_IP;
196         state->netlogon->in.map_response = true;
197
198         subreq = cldap_netlogon_send(state, state->cldap, state->netlogon);
199         if (tevent_req_nomem(subreq, state->req)) {
200                 return;
201         }
202
203         tevent_req_set_callback(subreq, finddcs_cldap_netlogon_replied, state);
204 }
205
206
207 /*
208   we have a response from a CLDAP server for a netlogon request
209  */
210 static void finddcs_cldap_netlogon_replied(struct tevent_req *subreq)
211 {
212         struct finddcs_cldap_state *state;
213         NTSTATUS status;
214
215         state = tevent_req_callback_data(subreq, struct finddcs_cldap_state);
216
217         status = cldap_netlogon_recv(subreq, state->netlogon, state->netlogon);
218         talloc_free(subreq);
219         if (!NT_STATUS_IS_OK(status)) {
220                 state->srv_address_index++;
221                 finddcs_cldap_next_server(state);
222                 return;
223         }
224         if (state->minimum_dc_flags !=
225             (state->minimum_dc_flags & state->netlogon->out.netlogon.data.nt5_ex.server_type)) {
226                 /* the server didn't match the minimum requirements */
227                 DEBUG(4,(__location__ ": Skipping DC %s with server_type=0x%08x - required 0x%08x\n",
228                          state->srv_addresses[state->srv_address_index],
229                          state->netlogon->out.netlogon.data.nt5_ex.server_type,
230                          state->minimum_dc_flags));
231                 state->srv_address_index++;
232                 finddcs_cldap_next_server(state);
233                 return;
234         }
235
236         tevent_req_done(state->req);
237 }
238
239 /*
240    handle NBT name lookup reply
241  */
242 static void finddcs_cldap_name_resolved(struct composite_context *ctx)
243 {
244         struct finddcs_cldap_state *state =
245                 talloc_get_type(ctx->async.private_data, struct finddcs_cldap_state);
246         const char *address;
247         NTSTATUS status;
248
249         status = resolve_name_recv(ctx, state, &address);
250         if (tevent_req_nterror(state->req, status)) {
251                 DEBUG(2,("finddcs: No matching NBT <1c> server found\n"));
252                 return;
253         }
254
255         DEBUG(4,("finddcs: Found NBT <1c> server at %s\n", address));
256
257         state->srv_addresses = talloc_array(state, const char *, 2);
258         if (tevent_req_nomem(state->srv_addresses, state->req)) {
259                 return;
260         }
261         state->srv_addresses[0] = address;
262         state->srv_addresses[1] = NULL;
263
264         state->srv_address_index = 0;
265
266         status = cldap_socket_init(state, state->ev, NULL, NULL, &state->cldap);
267         if (tevent_req_nterror(state->req, status)) {
268                 return;
269         }
270
271         finddcs_cldap_next_server(state);
272 }
273
274
275 /*
276  * Having got a DNS SRV answer, fire off the first CLDAP request
277  */
278 static void finddcs_cldap_srv_resolved(struct composite_context *ctx)
279 {
280         struct finddcs_cldap_state *state =
281                 talloc_get_type(ctx->async.private_data, struct finddcs_cldap_state);
282         NTSTATUS status;
283
284         status = resolve_name_multiple_recv(ctx, state, &state->srv_addresses);
285         if (tevent_req_nterror(state->req, status)) {
286                 DEBUG(2,("finddcs: Failed to find SRV record for %s\n", state->srv_name));
287                 return;
288         }
289
290         state->srv_address_index = 0;
291
292         status = cldap_socket_init(state, state->ev, NULL, NULL, &state->cldap);
293         if (tevent_req_nterror(state->req, status)) {
294                 return;
295         }
296
297         finddcs_cldap_next_server(state);
298 }
299
300
301 NTSTATUS finddcs_cldap_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx, struct finddcs *io)
302 {
303         struct finddcs_cldap_state *state = tevent_req_data(req, struct finddcs_cldap_state);
304         bool ok;
305         NTSTATUS status;
306
307         ok = tevent_req_poll(req, state->ev);
308         if (!ok) {
309                 talloc_free(req);
310                 return NT_STATUS_INTERNAL_ERROR;
311         }
312         status = tevent_req_simple_recv_ntstatus(req);
313         if (NT_STATUS_IS_OK(status)) {
314                 talloc_steal(mem_ctx, state->netlogon);
315                 io->out.netlogon = state->netlogon->out.netlogon;
316                 io->out.address = talloc_steal(mem_ctx, state->srv_addresses[state->srv_address_index]);
317         }
318         tevent_req_received(req);
319         return status;
320 }
321
322 NTSTATUS finddcs_cldap(TALLOC_CTX *mem_ctx,
323                        struct finddcs *io,
324                        struct resolve_context *resolve_ctx,
325                        struct tevent_context *event_ctx)
326 {
327         NTSTATUS status;
328         struct tevent_req *req = finddcs_cldap_send(mem_ctx,
329                                                     io,
330                                                     resolve_ctx,
331                                                     event_ctx);
332         status = finddcs_cldap_recv(req, mem_ctx, io);
333         talloc_free(req);
334         return status;
335 }