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/>.
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
;
38 struct cldap_socket
**cldap
;
39 struct tevent_req
**subreqs
;
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
,
63 struct tevent_req
*req
, *subreq
;
64 struct cldap_multi_netlogon_state
*state
;
67 req
= tevent_req_create(mem_ctx
, &state
,
68 struct cldap_multi_netlogon_state
);
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
,
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
*,
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
*,
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
,
109 if (tevent_req_nomem(state
->ios
, req
)) {
110 return tevent_req_post(req
, ev
);
113 for (i
=0; i
<num_servers
; i
++) {
116 status
= cldap_socket_init(state
->cldap
,
117 NULL
, /* local_addr */
120 if (!NT_STATUS_IS_OK(status
)) {
122 * Don't error out all sends just
123 * because one cldap_socket_init() failed.
124 * Log it here, and the cldap_netlogon_send()
125 * will catch it (with in.dest_address == NULL)
126 * and correctly error out in
127 * cldap_multi_netlogon_done(). This still allows
128 * the other requests to be concurrently sent.
130 DBG_NOTICE("cldap_socket_init failed for %s "
132 tsocket_address_string(state
->servers
[i
],
137 state
->ios
[i
].in
.dest_address
= NULL
;
138 state
->ios
[i
].in
.dest_port
= 0;
139 state
->ios
[i
].in
.realm
= domain
;
140 state
->ios
[i
].in
.host
= NULL
;
141 state
->ios
[i
].in
.user
= NULL
;
142 state
->ios
[i
].in
.domain_guid
= NULL
;
143 state
->ios
[i
].in
.domain_sid
= NULL
;
144 state
->ios
[i
].in
.acct_control
= 0;
145 state
->ios
[i
].in
.version
= ntversion
;
146 state
->ios
[i
].in
.map_response
= false;
149 for (i
=0; i
<min_servers
; i
++) {
150 state
->subreqs
[i
] = cldap_netlogon_send(state
->subreqs
,
154 if (tevent_req_nomem(state
->subreqs
[i
], req
)) {
155 return tevent_req_post(req
, ev
);
157 tevent_req_set_callback(
158 state
->subreqs
[i
], cldap_multi_netlogon_done
, req
);
160 state
->num_sent
= min_servers
;
162 if (state
->num_sent
< state
->num_servers
) {
164 * After 100 milliseconds fire the next one
166 subreq
= tevent_wakeup_send(state
, state
->ev
,
167 timeval_current_ofs(0, 100000));
168 if (tevent_req_nomem(subreq
, req
)) {
169 return tevent_req_post(req
, ev
);
171 tevent_req_set_callback(subreq
, cldap_multi_netlogon_next
,
178 static void cldap_multi_netlogon_done(struct tevent_req
*subreq
)
180 struct tevent_req
*req
= tevent_req_callback_data(
181 subreq
, struct tevent_req
);
182 struct cldap_multi_netlogon_state
*state
= tevent_req_data(
183 req
, struct cldap_multi_netlogon_state
);
185 struct netlogon_samlogon_response
*response
;
188 for (i
=0; i
<state
->num_sent
; i
++) {
189 if (state
->subreqs
[i
] == subreq
) {
193 if (i
== state
->num_sent
) {
195 * Got a response we did not fire...
197 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
200 state
->subreqs
[i
] = NULL
;
202 response
= talloc_zero(state
, struct netlogon_samlogon_response
);
203 if (tevent_req_nomem(response
, req
)) {
207 status
= cldap_netlogon_recv(subreq
, response
,
210 state
->num_received
+= 1;
212 if (NT_STATUS_IS_OK(status
)) {
213 *response
= state
->ios
[i
].out
.netlogon
;
214 state
->responses
[i
] = talloc_move(state
->responses
,
216 state
->num_good_received
+= 1;
219 if ((state
->num_received
== state
->num_servers
) ||
220 (state
->num_good_received
>= state
->min_servers
)) {
221 tevent_req_done(req
);
226 static void cldap_multi_netlogon_next(struct tevent_req
*subreq
)
228 struct tevent_req
*req
= tevent_req_callback_data(
229 subreq
, struct tevent_req
);
230 struct cldap_multi_netlogon_state
*state
= tevent_req_data(
231 req
, struct cldap_multi_netlogon_state
);
234 ret
= tevent_wakeup_recv(subreq
);
237 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
241 subreq
= cldap_netlogon_send(state
->subreqs
,
243 state
->cldap
[state
->num_sent
],
244 &state
->ios
[state
->num_sent
]);
245 if (tevent_req_nomem(subreq
, req
)) {
248 tevent_req_set_callback(subreq
, cldap_multi_netlogon_done
, req
);
249 state
->subreqs
[state
->num_sent
] = subreq
;
250 state
->num_sent
+= 1;
252 if (state
->num_sent
< state
->num_servers
) {
254 * After 100 milliseconds fire the next one
256 subreq
= tevent_wakeup_send(state
, state
->ev
,
257 timeval_current_ofs(0, 100000));
258 if (tevent_req_nomem(subreq
, req
)) {
261 tevent_req_set_callback(subreq
, cldap_multi_netlogon_next
,
266 NTSTATUS
cldap_multi_netlogon_recv(
267 struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
268 struct netlogon_samlogon_response
***responses
)
270 struct cldap_multi_netlogon_state
*state
= tevent_req_data(
271 req
, struct cldap_multi_netlogon_state
);
274 if (tevent_req_is_nterror(req
, &status
) &&
275 !NT_STATUS_EQUAL(status
, NT_STATUS_IO_TIMEOUT
)) {
279 * If we timeout, give back what we have so far
281 *responses
= talloc_move(mem_ctx
, &state
->responses
);
285 NTSTATUS
cldap_multi_netlogon(
287 const struct tsocket_address
* const *servers
,
289 const char *domain
, const char *hostname
, unsigned ntversion
,
290 int min_servers
, struct timeval timeout
,
291 struct netlogon_samlogon_response
***responses
)
293 struct tevent_context
*ev
;
294 struct tevent_req
*req
;
295 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
297 ev
= samba_tevent_context_init(talloc_tos());
301 req
= cldap_multi_netlogon_send(
302 ev
, ev
, servers
, num_servers
, domain
, hostname
, ntversion
,
307 if (!tevent_req_set_endtime(req
, ev
, timeout
)) {
310 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
313 status
= cldap_multi_netlogon_recv(req
, mem_ctx
, responses
);
319 /*******************************************************************
320 do a cldap netlogon query. Always 389/udp
321 *******************************************************************/
323 bool ads_cldap_netlogon(TALLOC_CTX
*mem_ctx
,
324 struct sockaddr_storage
*ss
,
327 struct netlogon_samlogon_response
**_reply
)
330 char addrstr
[INET6_ADDRSTRLEN
];
331 const char *dest_str
;
332 struct tsocket_address
*dest_addr
;
333 const struct tsocket_address
* const *dest_addrs
;
334 struct netlogon_samlogon_response
**responses
= NULL
;
337 dest_str
= print_sockaddr(addrstr
, sizeof(addrstr
), ss
);
339 ret
= tsocket_address_inet_from_strings(mem_ctx
, "ip",
343 status
= map_nt_error_from_unix(errno
);
344 DEBUG(2,("Failed to create cldap tsocket_address for %s - %s\n",
345 dest_str
, nt_errstr(status
)));
349 dest_addrs
= (const struct tsocket_address
* const *)&dest_addr
;
351 status
= cldap_multi_netlogon(talloc_tos(),
355 timeval_current_ofs(MAX(3,lp_ldap_timeout()/2), 0),
357 if (!NT_STATUS_IS_OK(status
)) {
358 DEBUG(2, ("ads_cldap_netlogon: cldap_multi_netlogon "
359 "failed: %s\n", nt_errstr(status
)));
362 if (responses
[0] == NULL
) {
363 DEBUG(2, ("ads_cldap_netlogon: did not get a reply\n"));
364 TALLOC_FREE(responses
);
367 *_reply
= talloc_move(mem_ctx
, &responses
[0]);
372 /*******************************************************************
373 do a cldap netlogon query. Always 389/udp
374 *******************************************************************/
376 bool ads_cldap_netlogon_5(TALLOC_CTX
*mem_ctx
,
377 struct sockaddr_storage
*ss
,
379 struct NETLOGON_SAM_LOGON_RESPONSE_EX
*reply5
)
381 uint32_t nt_version
= NETLOGON_NT_VERSION_5
| NETLOGON_NT_VERSION_5EX
;
382 struct netlogon_samlogon_response
*reply
= NULL
;
385 ret
= ads_cldap_netlogon(mem_ctx
, ss
, realm
, nt_version
, &reply
);
390 if (reply
->ntver
!= NETLOGON_NT_VERSION_5EX
) {
391 DEBUG(0,("ads_cldap_netlogon_5: nt_version mismatch: 0x%08x\n",
396 *reply5
= reply
->data
.nt5_ex
;