s4-finddc: use NBT lookup for a 1C name if joining a short domain name
[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 "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_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
58
59 /*
60  * find a list of DCs via DNS/CLDAP
61  *
62  */
63 struct tevent_req *finddcs_cldap_send(TALLOC_CTX *mem_ctx,
64                                       struct finddcs *io,
65                                       struct resolve_context *resolve_ctx,
66                                       struct tevent_context *event_ctx)
67 {
68         struct finddcs_cldap_state *state;
69         struct tevent_req *req;
70
71         req = tevent_req_create(mem_ctx, &state, struct finddcs_cldap_state);
72         if (req == NULL) {
73                 return NULL;
74         }
75
76         state->req = req;
77         state->ev = event_ctx;
78         state->minimum_dc_flags = io->in.minimum_dc_flags;
79         state->domain_name = talloc_strdup(state, io->in.domain_name);
80         if (tevent_req_nomem(state->domain_name, req)) {
81                 return tevent_req_post(req, event_ctx);
82         }
83
84         if (io->in.domain_sid) {
85                 state->domain_sid = dom_sid_dup(state, io->in.domain_sid);
86                 if (tevent_req_nomem(state->domain_sid, req)) {
87                         return tevent_req_post(req, event_ctx);
88                 }
89         } else {
90                 state->domain_sid = NULL;
91         }
92
93         if (strchr(state->domain_name, '.')) {
94                 /* looks like a DNS name */
95                 if (!finddcs_cldap_srv_lookup(state, io, resolve_ctx, event_ctx)) {
96                         return tevent_req_post(req, event_ctx);
97                 }
98         } else {
99                 if (!finddcs_cldap_nbt_lookup(state, io, resolve_ctx, event_ctx)) {
100                         return tevent_req_post(req, event_ctx);
101                 }
102         }
103
104         return req;
105 }
106
107 /*
108   start a SRV DNS lookup
109  */
110 static bool finddcs_cldap_srv_lookup(struct finddcs_cldap_state *state,
111                                      struct finddcs *io,
112                                      struct resolve_context *resolve_ctx,
113                                      struct tevent_context *event_ctx)
114 {
115         const char *srv_name;
116         struct composite_context *creq;
117         struct nbt_name name;
118
119         if (io->in.site_name) {
120                 srv_name = talloc_asprintf(state, "_ldap._tcp.%s._sites.%s",
121                                            io->in.site_name, io->in.domain_name);
122         } else {
123                 srv_name = talloc_asprintf(state, "_ldap._tcp.%s", io->in.domain_name);
124         }
125
126         make_nbt_name(&name, 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                 return;
171         }
172
173         state->netlogon = talloc_zero(state, struct cldap_netlogon);
174         if (tevent_req_nomem(state->netlogon, state->req)) {
175                 return;
176         }
177
178         state->netlogon->in.dest_address = state->srv_addresses[state->srv_address_index];
179         /* we should get the port from the SRV response */
180         state->netlogon->in.dest_port = 389;
181         if (strchr(state->domain_name, '.')) {
182                 state->netlogon->in.realm = state->domain_name;
183         }
184         if (state->domain_sid) {
185                 state->netlogon->in.domain_sid = dom_sid_string(state, state->domain_sid);
186                 if (tevent_req_nomem(state->netlogon->in.domain_sid, state->req)) {
187                         return;
188                 }
189         }
190         state->netlogon->in.acct_control = -1;
191         state->netlogon->in.version =
192                 NETLOGON_NT_VERSION_5 |
193                 NETLOGON_NT_VERSION_5EX |
194                 NETLOGON_NT_VERSION_IP;
195         state->netlogon->in.map_response = true;
196
197         subreq = cldap_netlogon_send(state, state->cldap, state->netlogon);
198         if (tevent_req_nomem(subreq, state->req)) {
199                 return;
200         }
201
202         tevent_req_set_callback(subreq, finddcs_cldap_netlogon_replied, state);
203 }
204
205
206 /*
207   we have a response from a CLDAP server for a netlogon request
208  */
209 static void finddcs_cldap_netlogon_replied(struct tevent_req *subreq)
210 {
211         struct finddcs_cldap_state *state;
212         NTSTATUS status;
213
214         state = tevent_req_callback_data(subreq, struct finddcs_cldap_state);
215
216         status = cldap_netlogon_recv(subreq, state->netlogon, state->netlogon);
217         if (!NT_STATUS_IS_OK(status)) {
218                 state->srv_address_index++;
219                 finddcs_cldap_next_server(state);
220                 return;
221         }
222         if (state->minimum_dc_flags !=
223             (state->minimum_dc_flags & state->netlogon->out.netlogon.data.nt5_ex.server_type)) {
224                 /* the server didn't match the minimum requirements */
225                 DEBUG(4,(__location__ ": Skipping DC %s with server_type=0x%08x\n",
226                          state->srv_addresses[state->srv_address_index],
227                          state->netlogon->out.netlogon.data.nt5_ex.server_type));
228                 state->srv_address_index++;
229                 finddcs_cldap_next_server(state);
230                 return;
231         }
232
233         talloc_free(subreq);
234         tevent_req_done(state->req);
235 }
236
237 /*
238    handle NBT name lookup reply
239  */
240 static void finddcs_cldap_name_resolved(struct composite_context *ctx)
241 {
242         struct finddcs_cldap_state *state =
243                 talloc_get_type(ctx->async.private_data, struct finddcs_cldap_state);
244         const char *address;
245         NTSTATUS status;
246
247         status = resolve_name_recv(ctx, state, &address);
248         if (tevent_req_nterror(state->req, status)) {
249                 return;
250         }
251
252         state->srv_addresses = talloc_array(state, const char *, 2);
253         if (tevent_req_nomem(state->srv_addresses, state->req)) {
254                 return;
255         }
256         state->srv_addresses[0] = address;
257         state->srv_addresses[1] = NULL;
258
259         state->srv_address_index = 0;
260
261         status = cldap_socket_init(state, state->ev, NULL, NULL, &state->cldap);
262         if (tevent_req_nterror(state->req, status)) {
263                 return;
264         }
265
266         finddcs_cldap_next_server(state);
267 }
268
269
270 /*
271  * Having got a DNS SRV answer, fire off the first CLDAP request
272  */
273 static void finddcs_cldap_srv_resolved(struct composite_context *ctx)
274 {
275         struct finddcs_cldap_state *state =
276                 talloc_get_type(ctx->async.private_data, struct finddcs_cldap_state);
277         NTSTATUS status;
278
279         status = resolve_name_multiple_recv(ctx, state, &state->srv_addresses);
280         if (tevent_req_nterror(state->req, status)) {
281                 return;
282         }
283
284         state->srv_address_index = 0;
285
286         status = cldap_socket_init(state, state->ev, NULL, NULL, &state->cldap);
287         if (tevent_req_nterror(state->req, status)) {
288                 return;
289         }
290
291         finddcs_cldap_next_server(state);
292 }
293
294
295 NTSTATUS finddcs_cldap_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx, struct finddcs *io)
296 {
297         struct finddcs_cldap_state *state = tevent_req_data(req, struct finddcs_cldap_state);
298         bool ok;
299         NTSTATUS status;
300
301         ok = tevent_req_poll(req, state->ev);
302         if (!ok) {
303                 talloc_free(req);
304                 return NT_STATUS_INTERNAL_ERROR;
305         }
306         status = tevent_req_simple_recv_ntstatus(req);
307         if (NT_STATUS_IS_OK(status)) {
308                 talloc_steal(mem_ctx, state->netlogon);
309                 io->out.netlogon = state->netlogon->out.netlogon;
310                 io->out.address = talloc_steal(mem_ctx, state->srv_addresses[state->srv_address_index]);
311         }
312         tevent_req_received(req);
313         return status;
314 }
315
316 NTSTATUS finddcs_cldap(TALLOC_CTX *mem_ctx,
317                        struct finddcs *io,
318                        struct resolve_context *resolve_ctx,
319                        struct tevent_context *event_ctx)
320 {
321         NTSTATUS status;
322         struct tevent_req *req = finddcs_cldap_send(mem_ctx,
323                                                     io,
324                                                     resolve_ctx,
325                                                     event_ctx);
326         status = finddcs_cldap_recv(req, mem_ctx, io);
327         talloc_free(req);
328         return status;
329 }