s3:libads: make use of samba_tevent_context_init()
[Samba/gebeck_regimport.git] / source3 / libads / cldap.c
blobbd77943455f1c67527175d0b22c4a018af9b5a50
1 /*
2 Samba Unix/Linux SMB client library
3 net ads cldap functions
4 Copyright (C) 2001 Andrew Tridgell (tridge@samba.org)
5 Copyright (C) 2003 Jim McDonough (jmcd@us.ibm.com)
6 Copyright (C) 2008 Guenther Deschner (gd@samba.org)
7 Copyright (C) 2009 Stefan Metzmacher (metze@samba.org)
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.
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.
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/>.
23 #include "includes.h"
24 #include "../libcli/cldap/cldap.h"
25 #include "../lib/tsocket/tsocket.h"
26 #include "../lib/util/tevent_ntstatus.h"
27 #include "libads/cldap.h"
29 struct cldap_multi_netlogon_state {
30 struct tevent_context *ev;
31 const struct tsocket_address * const *servers;
32 int num_servers;
33 const char *domain;
34 const char *hostname;
35 unsigned ntversion;
36 int min_servers;
38 struct cldap_socket **cldap;
39 struct tevent_req **subreqs;
40 int num_sent;
41 int num_received;
42 int num_good_received;
43 struct cldap_netlogon *ios;
44 struct netlogon_samlogon_response **responses;
47 static void cldap_multi_netlogon_done(struct tevent_req *subreq);
48 static void cldap_multi_netlogon_next(struct tevent_req *subreq);
51 * Do a parallel cldap ping to the servers. The first "min_servers"
52 * are fired directly, the remaining ones in 100msec intervals. If
53 * "min_servers" responses came in successfully, we immediately reply,
54 * not waiting for the remaining ones.
57 struct tevent_req *cldap_multi_netlogon_send(
58 TALLOC_CTX *mem_ctx, struct tevent_context *ev,
59 const struct tsocket_address * const *servers, int num_servers,
60 const char *domain, const char *hostname, unsigned ntversion,
61 int min_servers)
63 struct tevent_req *req, *subreq;
64 struct cldap_multi_netlogon_state *state;
65 int i;
67 req = tevent_req_create(mem_ctx, &state,
68 struct cldap_multi_netlogon_state);
69 if (req == NULL) {
70 return NULL;
72 state->ev = ev;
73 state->servers = servers;
74 state->num_servers = num_servers;
75 state->domain = domain;
76 state->hostname = hostname;
77 state->ntversion = ntversion;
78 state->min_servers = min_servers;
80 if (min_servers > num_servers) {
81 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
82 return tevent_req_post(req, ev);
85 state->subreqs = talloc_zero_array(state,
86 struct tevent_req *,
87 num_servers);
88 if (tevent_req_nomem(state->subreqs, req)) {
89 return tevent_req_post(req, ev);
92 state->cldap = talloc_zero_array(state,
93 struct cldap_socket *,
94 num_servers);
95 if (tevent_req_nomem(state->cldap, req)) {
96 return tevent_req_post(req, ev);
99 state->responses = talloc_zero_array(state,
100 struct netlogon_samlogon_response *,
101 num_servers);
102 if (tevent_req_nomem(state->responses, req)) {
103 return tevent_req_post(req, ev);
106 state->ios = talloc_zero_array(state->responses,
107 struct cldap_netlogon,
108 num_servers);
109 if (tevent_req_nomem(state->ios, req)) {
110 return tevent_req_post(req, ev);
113 for (i=0; i<num_servers; i++) {
114 NTSTATUS status;
116 status = cldap_socket_init(state->cldap,
117 NULL, /* local_addr */
118 state->servers[i],
119 &state->cldap[i]);
120 if (tevent_req_nterror(req, status)) {
121 return tevent_req_post(req, ev);
124 state->ios[i].in.dest_address = NULL;
125 state->ios[i].in.dest_port = 0;
126 state->ios[i].in.realm = domain;
127 state->ios[i].in.host = NULL;
128 state->ios[i].in.user = NULL;
129 state->ios[i].in.domain_guid = NULL;
130 state->ios[i].in.domain_sid = NULL;
131 state->ios[i].in.acct_control = 0;
132 state->ios[i].in.version = ntversion;
133 state->ios[i].in.map_response = false;
136 for (i=0; i<min_servers; i++) {
137 state->subreqs[i] = cldap_netlogon_send(state->subreqs,
138 state->ev,
139 state->cldap[i],
140 &state->ios[i]);
141 if (tevent_req_nomem(state->subreqs[i], req)) {
142 return tevent_req_post(req, ev);
144 tevent_req_set_callback(
145 state->subreqs[i], cldap_multi_netlogon_done, req);
147 state->num_sent = min_servers;
149 if (state->num_sent < state->num_servers) {
151 * After 100 milliseconds fire the next one
153 subreq = tevent_wakeup_send(state, state->ev,
154 timeval_current_ofs(0, 100000));
155 if (tevent_req_nomem(subreq, req)) {
156 return tevent_req_post(req, ev);
158 tevent_req_set_callback(subreq, cldap_multi_netlogon_next,
159 req);
162 return req;
165 static void cldap_multi_netlogon_done(struct tevent_req *subreq)
167 struct tevent_req *req = tevent_req_callback_data(
168 subreq, struct tevent_req);
169 struct cldap_multi_netlogon_state *state = tevent_req_data(
170 req, struct cldap_multi_netlogon_state);
171 NTSTATUS status;
172 struct netlogon_samlogon_response *response;
173 int i;
175 for (i=0; i<state->num_sent; i++) {
176 if (state->subreqs[i] == subreq) {
177 break;
180 if (i == state->num_sent) {
182 * Got a response we did not fire...
184 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
185 return;
187 state->subreqs[i] = NULL;
189 response = talloc_zero(state, struct netlogon_samlogon_response);
190 if (tevent_req_nomem(response, req)) {
191 return;
194 status = cldap_netlogon_recv(subreq, response,
195 &state->ios[i]);
196 TALLOC_FREE(subreq);
197 state->num_received += 1;
199 if (NT_STATUS_IS_OK(status)) {
200 *response = state->ios[i].out.netlogon;
201 state->responses[i] = talloc_move(state->responses,
202 &response);
203 state->num_good_received += 1;
206 if ((state->num_received == state->num_servers) ||
207 (state->num_good_received >= state->min_servers)) {
208 tevent_req_done(req);
209 return;
213 static void cldap_multi_netlogon_next(struct tevent_req *subreq)
215 struct tevent_req *req = tevent_req_callback_data(
216 subreq, struct tevent_req);
217 struct cldap_multi_netlogon_state *state = tevent_req_data(
218 req, struct cldap_multi_netlogon_state);
219 bool ret;
221 ret = tevent_wakeup_recv(subreq);
222 TALLOC_FREE(subreq);
223 if (!ret) {
224 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
225 return;
228 subreq = cldap_netlogon_send(state->subreqs,
229 state->ev,
230 state->cldap[state->num_sent],
231 &state->ios[state->num_sent]);
232 if (tevent_req_nomem(subreq, req)) {
233 return;
235 tevent_req_set_callback(subreq, cldap_multi_netlogon_done, req);
236 state->subreqs[state->num_sent] = subreq;
237 state->num_sent += 1;
239 if (state->num_sent < state->num_servers) {
241 * After 100 milliseconds fire the next one
243 subreq = tevent_wakeup_send(state, state->ev,
244 timeval_current_ofs(0, 100000));
245 if (tevent_req_nomem(subreq, req)) {
246 return;
248 tevent_req_set_callback(subreq, cldap_multi_netlogon_next,
249 req);
253 NTSTATUS cldap_multi_netlogon_recv(
254 struct tevent_req *req, TALLOC_CTX *mem_ctx,
255 struct netlogon_samlogon_response ***responses)
257 struct cldap_multi_netlogon_state *state = tevent_req_data(
258 req, struct cldap_multi_netlogon_state);
259 NTSTATUS status;
261 if (tevent_req_is_nterror(req, &status) &&
262 !NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
263 return status;
266 * If we timeout, give back what we have so far
268 *responses = talloc_move(mem_ctx, &state->responses);
269 return NT_STATUS_OK;
272 NTSTATUS cldap_multi_netlogon(
273 TALLOC_CTX *mem_ctx,
274 const struct tsocket_address * const *servers,
275 int num_servers,
276 const char *domain, const char *hostname, unsigned ntversion,
277 int min_servers, struct timeval timeout,
278 struct netlogon_samlogon_response ***responses)
280 struct tevent_context *ev;
281 struct tevent_req *req;
282 NTSTATUS status = NT_STATUS_NO_MEMORY;
284 ev = samba_tevent_context_init(talloc_tos());
285 if (ev == NULL) {
286 goto fail;
288 req = cldap_multi_netlogon_send(
289 ev, ev, servers, num_servers, domain, hostname, ntversion,
290 min_servers);
291 if (req == NULL) {
292 goto fail;
294 if (!tevent_req_set_endtime(req, ev, timeout)) {
295 goto fail;
297 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
298 goto fail;
300 status = cldap_multi_netlogon_recv(req, mem_ctx, responses);
301 fail:
302 TALLOC_FREE(ev);
303 return status;
306 /*******************************************************************
307 do a cldap netlogon query. Always 389/udp
308 *******************************************************************/
310 bool ads_cldap_netlogon(TALLOC_CTX *mem_ctx,
311 struct sockaddr_storage *ss,
312 const char *realm,
313 uint32_t nt_version,
314 struct netlogon_samlogon_response **_reply)
316 NTSTATUS status;
317 char addrstr[INET6_ADDRSTRLEN];
318 const char *dest_str;
319 struct tsocket_address *dest_addr;
320 const struct tsocket_address * const *dest_addrs;
321 struct netlogon_samlogon_response **responses = NULL;
322 int ret;
324 dest_str = print_sockaddr(addrstr, sizeof(addrstr), ss);
326 ret = tsocket_address_inet_from_strings(mem_ctx, "ip",
327 dest_str, LDAP_PORT,
328 &dest_addr);
329 if (ret != 0) {
330 status = map_nt_error_from_unix(errno);
331 DEBUG(2,("Failed to create cldap tsocket_address for %s - %s\n",
332 dest_str, nt_errstr(status)));
333 return false;
336 dest_addrs = (const struct tsocket_address * const *)&dest_addr;
338 status = cldap_multi_netlogon(talloc_tos(),
339 dest_addrs, 1,
340 realm, NULL,
341 nt_version, 1,
342 timeval_current_ofs(MAX(3,lp_ldap_timeout()/2), 0),
343 &responses);
344 if (!NT_STATUS_IS_OK(status)) {
345 DEBUG(2, ("ads_cldap_netlogon: cldap_multi_netlogon "
346 "failed: %s\n", nt_errstr(status)));
347 return false;
349 if (responses[0] == NULL) {
350 DEBUG(2, ("ads_cldap_netlogon: did not get a reply\n"));
351 TALLOC_FREE(responses);
352 return false;
354 *_reply = talloc_move(mem_ctx, &responses[0]);
356 return true;
359 /*******************************************************************
360 do a cldap netlogon query. Always 389/udp
361 *******************************************************************/
363 bool ads_cldap_netlogon_5(TALLOC_CTX *mem_ctx,
364 struct sockaddr_storage *ss,
365 const char *realm,
366 struct NETLOGON_SAM_LOGON_RESPONSE_EX *reply5)
368 uint32_t nt_version = NETLOGON_NT_VERSION_5 | NETLOGON_NT_VERSION_5EX;
369 struct netlogon_samlogon_response *reply = NULL;
370 bool ret;
372 ret = ads_cldap_netlogon(mem_ctx, ss, realm, nt_version, &reply);
373 if (!ret) {
374 return false;
377 if (reply->ntver != NETLOGON_NT_VERSION_5EX) {
378 DEBUG(0,("ads_cldap_netlogon_5: nt_version mismatch: 0x%08x\n",
379 reply->ntver));
380 return false;
383 *reply5 = reply->data.nt5_ex;
385 return true;