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 (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
,
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
,
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
);
172 struct netlogon_samlogon_response
*response
;
175 for (i
=0; i
<state
->num_sent
; i
++) {
176 if (state
->subreqs
[i
] == subreq
) {
180 if (i
== state
->num_sent
) {
182 * Got a response we did not fire...
184 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
187 state
->subreqs
[i
] = NULL
;
189 response
= talloc_zero(state
, struct netlogon_samlogon_response
);
190 if (tevent_req_nomem(response
, req
)) {
194 status
= cldap_netlogon_recv(subreq
, response
,
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
,
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
);
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
);
221 ret
= tevent_wakeup_recv(subreq
);
224 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
228 subreq
= cldap_netlogon_send(state
->subreqs
,
230 state
->cldap
[state
->num_sent
],
231 &state
->ios
[state
->num_sent
]);
232 if (tevent_req_nomem(subreq
, req
)) {
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
)) {
248 tevent_req_set_callback(subreq
, cldap_multi_netlogon_next
,
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
);
261 if (tevent_req_is_nterror(req
, &status
) &&
262 !NT_STATUS_EQUAL(status
, NT_STATUS_IO_TIMEOUT
)) {
266 * If we timeout, give back what we have so far
268 *responses
= talloc_move(mem_ctx
, &state
->responses
);
272 NTSTATUS
cldap_multi_netlogon(
274 const struct tsocket_address
* const *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
= tevent_context_init(talloc_tos());
288 req
= cldap_multi_netlogon_send(
289 ev
, ev
, servers
, num_servers
, domain
, hostname
, ntversion
,
294 if (!tevent_req_set_endtime(req
, ev
, timeout
)) {
297 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
300 status
= cldap_multi_netlogon_recv(req
, mem_ctx
, responses
);
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
,
314 struct netlogon_samlogon_response
**_reply
)
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
;
324 dest_str
= print_sockaddr(addrstr
, sizeof(addrstr
), ss
);
326 ret
= tsocket_address_inet_from_strings(mem_ctx
, "ip",
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
)));
336 dest_addrs
= (const struct tsocket_address
* const *)&dest_addr
;
338 status
= cldap_multi_netlogon(talloc_tos(),
342 timeval_current_ofs(MAX(3,lp_ldap_timeout()/2), 0),
344 if (!NT_STATUS_IS_OK(status
)) {
345 DEBUG(2, ("ads_cldap_netlogon: cldap_multi_netlogon "
346 "failed: %s\n", nt_errstr(status
)));
349 if (responses
[0] == NULL
) {
350 DEBUG(2, ("ads_cldap_netlogon: did not get a reply\n"));
351 TALLOC_FREE(responses
);
354 *_reply
= talloc_move(mem_ctx
, &responses
[0]);
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
,
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
;
372 ret
= ads_cldap_netlogon(mem_ctx
, ss
, realm
, nt_version
, &reply
);
377 if (reply
->ntver
!= NETLOGON_NT_VERSION_5EX
) {
378 DEBUG(0,("ads_cldap_netlogon_5: nt_version mismatch: 0x%08x\n",
383 *reply5
= reply
->data
.nt5_ex
;