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