2 Unix SMB/CIFS implementation.
4 KDC Server request proxying
6 Copyright (C) Andrew Tridgell 2010
7 Copyright (C) Andrew Bartlett 2010
8 Copyright (C) Stefan Metzmacher 2011
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "smbd/process_model.h"
26 #include "lib/tsocket/tsocket.h"
27 #include "libcli/util/tstream.h"
28 #include "lib/util/tevent_ntstatus.h"
29 #include "lib/stream/packet.h"
30 #include "kdc/kdc-glue.h"
31 #include "dsdb/samdb/samdb.h"
32 #include "libcli/composite/composite.h"
33 #include "libcli/resolve/resolve.h"
37 get a list of our replication partners from repsFrom, returning it in *proxy_list
39 static WERROR
kdc_proxy_get_writeable_dcs(struct kdc_server
*kdc
, TALLOC_CTX
*mem_ctx
, char ***proxy_list
)
43 struct repsFromToBlob
*reps
;
45 werr
= dsdb_loadreps(kdc
->samdb
, mem_ctx
, ldb_get_default_basedn(kdc
->samdb
), "repsFrom", &reps
, &count
);
46 W_ERROR_NOT_OK_RETURN(werr
);
49 /* we don't have any DCs to replicate with. Very
51 DEBUG(1,(__location__
": No replication sources for RODC in KDC proxy\n"));
53 return WERR_DS_DRA_NO_REPLICA
;
56 (*proxy_list
) = talloc_array(mem_ctx
, char *, count
+1);
57 W_ERROR_HAVE_NO_MEMORY_AND_FREE(*proxy_list
, reps
);
59 talloc_steal(*proxy_list
, reps
);
61 for (i
=0; i
<count
; i
++) {
62 const char *dns_name
= NULL
;
63 if (reps
->version
== 1) {
64 dns_name
= reps
->ctr
.ctr1
.other_info
->dns_name
;
65 } else if (reps
->version
== 2) {
66 dns_name
= reps
->ctr
.ctr2
.other_info
->dns_name1
;
68 (*proxy_list
)[i
] = talloc_strdup(*proxy_list
, dns_name
);
69 W_ERROR_HAVE_NO_MEMORY_AND_FREE((*proxy_list
)[i
], *proxy_list
);
71 (*proxy_list
)[i
] = NULL
;
79 struct kdc_udp_proxy_state
{
80 struct tevent_context
*ev
;
81 struct kdc_server
*kdc
;
90 struct tdgram_context
*dgram
;
95 static void kdc_udp_next_proxy(struct tevent_req
*req
);
97 struct tevent_req
*kdc_udp_proxy_send(TALLOC_CTX
*mem_ctx
,
98 struct tevent_context
*ev
,
99 struct kdc_server
*kdc
,
103 struct tevent_req
*req
;
104 struct kdc_udp_proxy_state
*state
;
107 req
= tevent_req_create(mem_ctx
, &state
,
108 struct kdc_udp_proxy_state
);
117 werr
= kdc_proxy_get_writeable_dcs(kdc
, state
, &state
->proxy_list
);
118 if (!W_ERROR_IS_OK(werr
)) {
119 NTSTATUS status
= werror_to_ntstatus(werr
);
120 tevent_req_nterror(req
, status
);
121 return tevent_req_post(req
, ev
);
124 kdc_udp_next_proxy(req
);
125 if (!tevent_req_is_in_progress(req
)) {
126 return tevent_req_post(req
, ev
);
132 static void kdc_udp_proxy_resolve_done(struct composite_context
*csubreq
);
135 try the next proxy in the list
137 static void kdc_udp_next_proxy(struct tevent_req
*req
)
139 struct kdc_udp_proxy_state
*state
=
141 struct kdc_udp_proxy_state
);
142 const char *proxy_dnsname
= state
->proxy_list
[state
->next_proxy
];
143 struct composite_context
*csubreq
;
145 if (proxy_dnsname
== NULL
) {
146 tevent_req_nterror(req
, NT_STATUS_NO_LOGON_SERVERS
);
152 /* make sure we close the socket of the last try */
153 TALLOC_FREE(state
->proxy
.dgram
);
154 ZERO_STRUCT(state
->proxy
);
156 make_nbt_name(&state
->proxy
.name
, proxy_dnsname
, 0);
158 csubreq
= resolve_name_ex_send(lpcfg_resolve_context(state
->kdc
->task
->lp_ctx
),
160 RESOLVE_NAME_FLAG_FORCE_DNS
,
164 if (tevent_req_nomem(csubreq
, req
)) {
167 csubreq
->async
.fn
= kdc_udp_proxy_resolve_done
;
168 csubreq
->async
.private_data
= req
;
171 static void kdc_udp_proxy_sendto_done(struct tevent_req
*subreq
);
172 static void kdc_udp_proxy_recvfrom_done(struct tevent_req
*subreq
);
174 static void kdc_udp_proxy_resolve_done(struct composite_context
*csubreq
)
176 struct tevent_req
*req
=
177 talloc_get_type_abort(csubreq
->async
.private_data
,
179 struct kdc_udp_proxy_state
*state
=
181 struct kdc_udp_proxy_state
);
183 struct tevent_req
*subreq
;
184 struct tsocket_address
*local_addr
, *proxy_addr
;
187 status
= resolve_name_recv(csubreq
, state
, &state
->proxy
.ip
);
188 if (!NT_STATUS_IS_OK(status
)) {
189 DEBUG(0,("Unable to resolve proxy[%s] - %s\n",
190 state
->proxy
.name
.name
, nt_errstr(status
)));
191 kdc_udp_next_proxy(req
);
195 /* get an address for us to use locally */
196 ret
= tsocket_address_inet_from_strings(state
, "ip", NULL
, 0, &local_addr
);
198 kdc_udp_next_proxy(req
);
202 ret
= tsocket_address_inet_from_strings(state
, "ip",
207 kdc_udp_next_proxy(req
);
211 /* create a socket for us to work on */
212 ret
= tdgram_inet_udp_socket(local_addr
, proxy_addr
,
213 state
, &state
->proxy
.dgram
);
215 kdc_udp_next_proxy(req
);
219 subreq
= tdgram_sendto_send(state
,
225 if (tevent_req_nomem(subreq
, req
)) {
228 tevent_req_set_callback(subreq
, kdc_udp_proxy_sendto_done
, req
);
230 /* setup to receive the reply from the proxy */
231 subreq
= tdgram_recvfrom_send(state
, state
->ev
, state
->proxy
.dgram
);
232 if (tevent_req_nomem(subreq
, req
)) {
235 tevent_req_set_callback(subreq
, kdc_udp_proxy_recvfrom_done
, req
);
236 tevent_req_set_endtime(subreq
, state
->ev
,
237 timeval_current_ofs(state
->kdc
->proxy_timeout
, 0));
239 DEBUG(4,("kdc_udp_proxy: proxying request to %s[%s]\n",
240 state
->proxy
.name
.name
, state
->proxy
.ip
));
244 called when the send of the call to the proxy is complete
245 this is used to get an errors from the sendto()
247 static void kdc_udp_proxy_sendto_done(struct tevent_req
*subreq
)
249 struct tevent_req
*req
=
250 tevent_req_callback_data(subreq
,
252 struct kdc_udp_proxy_state
*state
=
254 struct kdc_udp_proxy_state
);
258 ret
= tdgram_sendto_recv(subreq
, &sys_errno
);
261 DEBUG(4,("kdc_udp_proxy: sendto for %s[%s] gave %d : %s\n",
262 state
->proxy
.name
.name
, state
->proxy
.ip
,
263 sys_errno
, strerror(sys_errno
)));
264 kdc_udp_next_proxy(req
);
269 called when the proxy replies
271 static void kdc_udp_proxy_recvfrom_done(struct tevent_req
*subreq
)
273 struct tevent_req
*req
=
274 tevent_req_callback_data(subreq
,
276 struct kdc_udp_proxy_state
*state
=
278 struct kdc_udp_proxy_state
);
283 len
= tdgram_recvfrom_recv(subreq
, &sys_errno
,
287 DEBUG(4,("kdc_udp_proxy: reply from %s[%s] gave %d : %s\n",
288 state
->proxy
.name
.name
, state
->proxy
.ip
,
289 sys_errno
, strerror(sys_errno
)));
290 kdc_udp_next_proxy(req
);
295 * Check the reply came from the right IP?
296 * As we use connected udp sockets, that should not be needed...
299 state
->out
.length
= len
;
300 state
->out
.data
= buf
;
302 tevent_req_done(req
);
305 NTSTATUS
kdc_udp_proxy_recv(struct tevent_req
*req
,
309 struct kdc_udp_proxy_state
*state
=
311 struct kdc_udp_proxy_state
);
314 if (tevent_req_is_nterror(req
, &status
)) {
315 tevent_req_received(req
);
319 out
->data
= talloc_move(mem_ctx
, &state
->out
.data
);
320 out
->length
= state
->out
.length
;
322 tevent_req_received(req
);
326 struct kdc_tcp_proxy_state
{
327 struct tevent_context
*ev
;
328 struct kdc_server
*kdc
;
332 struct iovec in_iov
[2];
337 struct nbt_name name
;
339 struct tstream_context
*stream
;
343 static void kdc_tcp_next_proxy(struct tevent_req
*req
);
345 struct tevent_req
*kdc_tcp_proxy_send(TALLOC_CTX
*mem_ctx
,
346 struct tevent_context
*ev
,
347 struct kdc_server
*kdc
,
351 struct tevent_req
*req
;
352 struct kdc_tcp_proxy_state
*state
;
355 req
= tevent_req_create(mem_ctx
, &state
,
356 struct kdc_tcp_proxy_state
);
365 werr
= kdc_proxy_get_writeable_dcs(kdc
, state
, &state
->proxy_list
);
366 if (!W_ERROR_IS_OK(werr
)) {
367 NTSTATUS status
= werror_to_ntstatus(werr
);
368 tevent_req_nterror(req
, status
);
369 return tevent_req_post(req
, ev
);
372 RSIVAL(state
->in_hdr
, 0, state
->in
.length
);
373 state
->in_iov
[0].iov_base
= (char *)state
->in_hdr
;
374 state
->in_iov
[0].iov_len
= 4;
375 state
->in_iov
[1].iov_base
= (char *)state
->in
.data
;
376 state
->in_iov
[1].iov_len
= state
->in
.length
;
378 kdc_tcp_next_proxy(req
);
379 if (!tevent_req_is_in_progress(req
)) {
380 return tevent_req_post(req
, ev
);
386 static void kdc_tcp_proxy_resolve_done(struct composite_context
*csubreq
);
389 try the next proxy in the list
391 static void kdc_tcp_next_proxy(struct tevent_req
*req
)
393 struct kdc_tcp_proxy_state
*state
=
395 struct kdc_tcp_proxy_state
);
396 const char *proxy_dnsname
= state
->proxy_list
[state
->next_proxy
];
397 struct composite_context
*csubreq
;
399 if (proxy_dnsname
== NULL
) {
400 tevent_req_nterror(req
, NT_STATUS_NO_LOGON_SERVERS
);
406 /* make sure we close the socket of the last try */
407 TALLOC_FREE(state
->proxy
.stream
);
408 ZERO_STRUCT(state
->proxy
);
410 make_nbt_name(&state
->proxy
.name
, proxy_dnsname
, 0);
412 csubreq
= resolve_name_ex_send(lpcfg_resolve_context(state
->kdc
->task
->lp_ctx
),
414 RESOLVE_NAME_FLAG_FORCE_DNS
,
418 if (tevent_req_nomem(csubreq
, req
)) {
421 csubreq
->async
.fn
= kdc_tcp_proxy_resolve_done
;
422 csubreq
->async
.private_data
= req
;
425 static void kdc_tcp_proxy_connect_done(struct tevent_req
*subreq
);
427 static void kdc_tcp_proxy_resolve_done(struct composite_context
*csubreq
)
429 struct tevent_req
*req
=
430 talloc_get_type_abort(csubreq
->async
.private_data
,
432 struct kdc_tcp_proxy_state
*state
=
434 struct kdc_tcp_proxy_state
);
436 struct tevent_req
*subreq
;
437 struct tsocket_address
*local_addr
, *proxy_addr
;
440 status
= resolve_name_recv(csubreq
, state
, &state
->proxy
.ip
);
441 if (!NT_STATUS_IS_OK(status
)) {
442 DEBUG(0,("Unable to resolve proxy[%s] - %s\n",
443 state
->proxy
.name
.name
, nt_errstr(status
)));
444 kdc_tcp_next_proxy(req
);
448 /* get an address for us to use locally */
449 ret
= tsocket_address_inet_from_strings(state
, "ip", NULL
, 0, &local_addr
);
451 kdc_tcp_next_proxy(req
);
455 ret
= tsocket_address_inet_from_strings(state
, "ip",
460 kdc_tcp_next_proxy(req
);
464 subreq
= tstream_inet_tcp_connect_send(state
, state
->ev
,
465 local_addr
, proxy_addr
);
466 if (tevent_req_nomem(subreq
, req
)) {
469 tevent_req_set_callback(subreq
, kdc_tcp_proxy_connect_done
, req
);
470 tevent_req_set_endtime(subreq
, state
->ev
,
471 timeval_current_ofs(state
->kdc
->proxy_timeout
, 0));
474 static void kdc_tcp_proxy_writev_done(struct tevent_req
*subreq
);
475 static void kdc_tcp_proxy_read_pdu_done(struct tevent_req
*subreq
);
477 static void kdc_tcp_proxy_connect_done(struct tevent_req
*subreq
)
479 struct tevent_req
*req
=
480 tevent_req_callback_data(subreq
,
482 struct kdc_tcp_proxy_state
*state
=
484 struct kdc_tcp_proxy_state
);
487 ret
= tstream_inet_tcp_connect_recv(subreq
, &sys_errno
,
488 state
, &state
->proxy
.stream
, NULL
);
491 kdc_tcp_next_proxy(req
);
495 subreq
= tstream_writev_send(state
,
499 if (tevent_req_nomem(subreq
, req
)) {
502 tevent_req_set_callback(subreq
, kdc_tcp_proxy_writev_done
, req
);
504 subreq
= tstream_read_pdu_blob_send(state
,
507 4, /* initial_read_size */
508 packet_full_request_u32
,
510 if (tevent_req_nomem(subreq
, req
)) {
513 tevent_req_set_callback(subreq
, kdc_tcp_proxy_read_pdu_done
, req
);
514 tevent_req_set_endtime(subreq
, state
->kdc
->task
->event_ctx
,
515 timeval_current_ofs(state
->kdc
->proxy_timeout
, 0));
517 DEBUG(4,("kdc_tcp_proxy: proxying request to %s[%s]\n",
518 state
->proxy
.name
.name
, state
->proxy
.ip
));
521 static void kdc_tcp_proxy_writev_done(struct tevent_req
*subreq
)
523 struct tevent_req
*req
=
524 tevent_req_callback_data(subreq
,
528 ret
= tstream_writev_recv(subreq
, &sys_errno
);
531 kdc_tcp_next_proxy(req
);
535 static void kdc_tcp_proxy_read_pdu_done(struct tevent_req
*subreq
)
537 struct tevent_req
*req
=
538 tevent_req_callback_data(subreq
,
540 struct kdc_tcp_proxy_state
*state
=
542 struct kdc_tcp_proxy_state
);
546 status
= tstream_read_pdu_blob_recv(subreq
, state
, &raw
);
548 if (!NT_STATUS_IS_OK(status
)) {
549 kdc_tcp_next_proxy(req
);
554 * raw blob has the length in the first 4 bytes,
555 * which we do not need here.
557 state
->out
= data_blob_talloc(state
, raw
.data
+ 4, raw
.length
- 4);
558 if (state
->out
.length
!= raw
.length
- 4) {
563 tevent_req_done(req
);
566 NTSTATUS
kdc_tcp_proxy_recv(struct tevent_req
*req
,
570 struct kdc_tcp_proxy_state
*state
=
572 struct kdc_tcp_proxy_state
);
575 if (tevent_req_is_nterror(req
, &status
)) {
576 tevent_req_received(req
);
580 out
->data
= talloc_move(mem_ctx
, &state
->out
.data
);
581 out
->length
= state
->out
.length
;
583 tevent_req_received(req
);