2 Unix SMB/CIFS implementation.
6 Copyright (c) 2005-2008 Andrew Bartlett <abartlet@samba.org>
7 Copyright (c) 2005 Andrew Tridgell <tridge@samba.org>
8 Copyright (c) 2005 Stefan Metzmacher <metze@samba.org>
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 "param/param.h"
26 #include "smbd/process_model.h"
27 #include "lib/tsocket/tsocket.h"
28 #include "libcli/util/tstream.h"
29 #include "kdc/kdc-server.h"
30 #include "kdc/kdc-proxy.h"
31 #include "lib/stream/packet.h"
34 * State of an open tcp connection
36 struct kdc_tcp_connection {
37 /* stream connection we belong to */
38 struct stream_connection *conn;
40 /* the kdc_server the connection belongs to */
41 struct kdc_socket *kdc_socket;
43 struct tstream_context *tstream;
45 struct tevent_queue *send_queue;
49 struct kdc_tcp_connection *kdc_conn;
53 struct iovec out_iov[2];
57 struct kdc_udp_socket *sock;
58 struct tsocket_address *src;
63 static void kdc_udp_call_proxy_done(struct tevent_req *subreq);
64 static void kdc_udp_call_sendto_done(struct tevent_req *subreq);
66 static void kdc_tcp_call_writev_done(struct tevent_req *subreq);
67 static void kdc_tcp_call_proxy_done(struct tevent_req *subreq);
69 static void kdc_tcp_terminate_connection(struct kdc_tcp_connection *kdc_conn,
72 stream_terminate_connection(kdc_conn->conn, reason);
75 static NTSTATUS kdc_proxy_unavailable_error(struct kdc_server *kdc,
82 code = smb_krb5_mk_error(kdc->smb_krb5_context->krb5_context,
83 KRB5KDC_ERR_SVC_UNAVAILABLE,
88 DBG_WARNING("Unable to form krb5 error reply\n");
89 return NT_STATUS_INTERNAL_ERROR;
92 *out = data_blob_talloc(mem_ctx, enc_error.data, enc_error.length);
93 kerberos_free_data_contents(kdc->smb_krb5_context->krb5_context,
96 return NT_STATUS_NO_MEMORY;
102 static void kdc_udp_call_loop(struct tevent_req *subreq)
104 struct kdc_udp_socket *sock = tevent_req_callback_data(subreq,
105 struct kdc_udp_socket);
106 struct kdc_udp_call *call;
112 call = talloc(sock, struct kdc_udp_call);
119 len = tdgram_recvfrom_recv(subreq, &sys_errno,
120 call, &buf, &call->src);
128 call->in.length = len;
130 DEBUG(10,("Received krb5 UDP packet of length %lu from %s\n",
131 (long)call->in.length,
132 tsocket_address_string(call->src, call)));
135 ret = sock->kdc_socket->process(sock->kdc_socket->kdc,
140 sock->kdc_socket->local_address,
142 if (ret == KDC_ERROR) {
147 if (ret == KDC_PROXY_REQUEST) {
150 if (!sock->kdc_socket->kdc->am_rodc) {
151 DEBUG(0,("kdc_udp_call_loop: proxying requested when not RODC"));
156 port = tsocket_address_inet_port(sock->kdc_socket->local_address);
158 subreq = kdc_udp_proxy_send(call,
159 sock->kdc_socket->kdc->task->event_ctx,
160 sock->kdc_socket->kdc,
163 if (subreq == NULL) {
167 tevent_req_set_callback(subreq, kdc_udp_call_proxy_done, call);
171 subreq = tdgram_sendto_queue_send(call,
172 sock->kdc_socket->kdc->task->event_ctx,
178 if (subreq == NULL) {
182 tevent_req_set_callback(subreq, kdc_udp_call_sendto_done, call);
185 subreq = tdgram_recvfrom_send(sock,
186 sock->kdc_socket->kdc->task->event_ctx,
188 if (subreq == NULL) {
189 task_server_terminate(sock->kdc_socket->kdc->task,
190 "no memory for tdgram_recvfrom_send",
194 tevent_req_set_callback(subreq, kdc_udp_call_loop, sock);
197 static void kdc_udp_call_proxy_done(struct tevent_req *subreq)
199 struct kdc_udp_call *call =
200 tevent_req_callback_data(subreq,
201 struct kdc_udp_call);
204 status = kdc_udp_proxy_recv(subreq, call, &call->out);
206 if (!NT_STATUS_IS_OK(status)) {
207 /* generate an error packet */
208 status = kdc_proxy_unavailable_error(call->sock->kdc_socket->kdc,
212 if (!NT_STATUS_IS_OK(status)) {
217 subreq = tdgram_sendto_queue_send(call,
218 call->sock->kdc_socket->kdc->task->event_ctx,
220 call->sock->send_queue,
224 if (subreq == NULL) {
229 tevent_req_set_callback(subreq, kdc_udp_call_sendto_done, call);
232 static void kdc_udp_call_sendto_done(struct tevent_req *subreq)
234 struct kdc_udp_call *call = tevent_req_callback_data(subreq,
235 struct kdc_udp_call);
238 tdgram_sendto_queue_recv(subreq, &sys_errno);
240 /* We don't care about errors */
245 static void kdc_tcp_call_loop(struct tevent_req *subreq)
247 struct kdc_tcp_connection *kdc_conn = tevent_req_callback_data(subreq,
248 struct kdc_tcp_connection);
249 struct kdc_tcp_call *call;
253 call = talloc(kdc_conn, struct kdc_tcp_call);
255 kdc_tcp_terminate_connection(kdc_conn, "kdc_tcp_call_loop: "
256 "no memory for kdc_tcp_call");
259 call->kdc_conn = kdc_conn;
261 status = tstream_read_pdu_blob_recv(subreq,
265 if (!NT_STATUS_IS_OK(status)) {
268 reason = talloc_asprintf(call, "kdc_tcp_call_loop: "
269 "tstream_read_pdu_blob_recv() - %s",
272 reason = nt_errstr(status);
275 kdc_tcp_terminate_connection(kdc_conn, reason);
279 DEBUG(10,("Received krb5 TCP packet of length %lu from %s\n",
280 (long) call->in.length,
281 tsocket_address_string(kdc_conn->conn->remote_address, call)));
283 /* skip length header */
285 call->in.length -= 4;
288 ret = kdc_conn->kdc_socket->process(kdc_conn->kdc_socket->kdc,
292 kdc_conn->conn->remote_address,
293 kdc_conn->conn->local_address,
295 if (ret == KDC_ERROR) {
296 kdc_tcp_terminate_connection(kdc_conn,
297 "kdc_tcp_call_loop: process function failed");
301 if (ret == KDC_PROXY_REQUEST) {
304 if (!kdc_conn->kdc_socket->kdc->am_rodc) {
305 kdc_tcp_terminate_connection(kdc_conn,
306 "kdc_tcp_call_loop: proxying requested when not RODC");
309 port = tsocket_address_inet_port(kdc_conn->conn->local_address);
311 subreq = kdc_tcp_proxy_send(call,
312 kdc_conn->conn->event.ctx,
313 kdc_conn->kdc_socket->kdc,
316 if (subreq == NULL) {
317 kdc_tcp_terminate_connection(kdc_conn,
318 "kdc_tcp_call_loop: kdc_tcp_proxy_send failed");
321 tevent_req_set_callback(subreq, kdc_tcp_call_proxy_done, call);
325 /* First add the length of the out buffer */
326 RSIVAL(call->out_hdr, 0, call->out.length);
327 call->out_iov[0].iov_base = (char *) call->out_hdr;
328 call->out_iov[0].iov_len = 4;
330 call->out_iov[1].iov_base = (char *) call->out.data;
331 call->out_iov[1].iov_len = call->out.length;
333 subreq = tstream_writev_queue_send(call,
334 kdc_conn->conn->event.ctx,
336 kdc_conn->send_queue,
338 if (subreq == NULL) {
339 kdc_tcp_terminate_connection(kdc_conn, "kdc_tcp_call_loop: "
340 "no memory for tstream_writev_queue_send");
343 tevent_req_set_callback(subreq, kdc_tcp_call_writev_done, call);
346 * The krb5 tcp pdu's has the length as 4 byte (initial_read_size),
347 * packet_full_request_u32 provides the pdu length then.
349 subreq = tstream_read_pdu_blob_send(kdc_conn,
350 kdc_conn->conn->event.ctx,
352 4, /* initial_read_size */
353 packet_full_request_u32,
355 if (subreq == NULL) {
356 kdc_tcp_terminate_connection(kdc_conn, "kdc_tcp_call_loop: "
357 "no memory for tstream_read_pdu_blob_send");
360 tevent_req_set_callback(subreq, kdc_tcp_call_loop, kdc_conn);
363 static void kdc_tcp_call_proxy_done(struct tevent_req *subreq)
365 struct kdc_tcp_call *call = tevent_req_callback_data(subreq,
366 struct kdc_tcp_call);
367 struct kdc_tcp_connection *kdc_conn = call->kdc_conn;
370 status = kdc_tcp_proxy_recv(subreq, call, &call->out);
372 if (!NT_STATUS_IS_OK(status)) {
373 /* generate an error packet */
374 status = kdc_proxy_unavailable_error(kdc_conn->kdc_socket->kdc,
378 if (!NT_STATUS_IS_OK(status)) {
381 reason = talloc_asprintf(call, "kdc_tcp_call_proxy_done: "
382 "kdc_proxy_unavailable_error - %s",
385 reason = "kdc_tcp_call_proxy_done: kdc_proxy_unavailable_error() failed";
388 kdc_tcp_terminate_connection(call->kdc_conn, reason);
392 /* First add the length of the out buffer */
393 RSIVAL(call->out_hdr, 0, call->out.length);
394 call->out_iov[0].iov_base = (char *) call->out_hdr;
395 call->out_iov[0].iov_len = 4;
397 call->out_iov[1].iov_base = (char *) call->out.data;
398 call->out_iov[1].iov_len = call->out.length;
400 subreq = tstream_writev_queue_send(call,
401 kdc_conn->conn->event.ctx,
403 kdc_conn->send_queue,
405 if (subreq == NULL) {
406 kdc_tcp_terminate_connection(kdc_conn, "kdc_tcp_call_loop: "
407 "no memory for tstream_writev_queue_send");
410 tevent_req_set_callback(subreq, kdc_tcp_call_writev_done, call);
413 * The krb5 tcp pdu's has the length as 4 byte (initial_read_size),
414 * packet_full_request_u32 provides the pdu length then.
416 subreq = tstream_read_pdu_blob_send(kdc_conn,
417 kdc_conn->conn->event.ctx,
419 4, /* initial_read_size */
420 packet_full_request_u32,
422 if (subreq == NULL) {
423 kdc_tcp_terminate_connection(kdc_conn, "kdc_tcp_call_loop: "
424 "no memory for tstream_read_pdu_blob_send");
427 tevent_req_set_callback(subreq, kdc_tcp_call_loop, kdc_conn);
430 static void kdc_tcp_call_writev_done(struct tevent_req *subreq)
432 struct kdc_tcp_call *call = tevent_req_callback_data(subreq,
433 struct kdc_tcp_call);
437 rc = tstream_writev_queue_recv(subreq, &sys_errno);
442 reason = talloc_asprintf(call, "kdc_tcp_call_writev_done: "
443 "tstream_writev_queue_recv() - %d:%s",
444 sys_errno, strerror(sys_errno));
446 reason = "kdc_tcp_call_writev_done: tstream_writev_queue_recv() failed";
449 kdc_tcp_terminate_connection(call->kdc_conn, reason);
453 /* We don't care about errors */
459 called when we get a new connection
461 static void kdc_tcp_accept(struct stream_connection *conn)
463 struct kdc_socket *kdc_socket;
464 struct kdc_tcp_connection *kdc_conn;
465 struct tevent_req *subreq;
468 kdc_conn = talloc_zero(conn, struct kdc_tcp_connection);
469 if (kdc_conn == NULL) {
470 stream_terminate_connection(conn,
471 "kdc_tcp_accept: out of memory");
475 kdc_conn->send_queue = tevent_queue_create(conn, "kdc_tcp_accept");
476 if (kdc_conn->send_queue == NULL) {
477 stream_terminate_connection(conn,
478 "kdc_tcp_accept: out of memory");
482 kdc_socket = talloc_get_type(conn->private_data, struct kdc_socket);
484 TALLOC_FREE(conn->event.fde);
486 rc = tstream_bsd_existing_socket(kdc_conn,
487 socket_get_fd(conn->socket),
490 stream_terminate_connection(conn,
491 "kdc_tcp_accept: out of memory");
495 kdc_conn->conn = conn;
496 kdc_conn->kdc_socket = kdc_socket;
497 conn->private_data = kdc_conn;
500 * The krb5 tcp pdu's has the length as 4 byte (initial_read_size),
501 * packet_full_request_u32 provides the pdu length then.
503 subreq = tstream_read_pdu_blob_send(kdc_conn,
504 kdc_conn->conn->event.ctx,
506 4, /* initial_read_size */
507 packet_full_request_u32,
509 if (subreq == NULL) {
510 kdc_tcp_terminate_connection(kdc_conn, "kdc_tcp_accept: "
511 "no memory for tstream_read_pdu_blob_send");
514 tevent_req_set_callback(subreq, kdc_tcp_call_loop, kdc_conn);
517 static void kdc_tcp_recv(struct stream_connection *conn, uint16_t flags)
519 struct kdc_tcp_connection *kdcconn = talloc_get_type(conn->private_data,
520 struct kdc_tcp_connection);
521 /* this should never be triggered! */
522 kdc_tcp_terminate_connection(kdcconn, "kdc_tcp_recv: called");
525 static void kdc_tcp_send(struct stream_connection *conn, uint16_t flags)
527 struct kdc_tcp_connection *kdcconn = talloc_get_type(conn->private_data,
528 struct kdc_tcp_connection);
529 /* this should never be triggered! */
530 kdc_tcp_terminate_connection(kdcconn, "kdc_tcp_send: called");
533 static const struct stream_server_ops kdc_tcp_stream_ops = {
535 .accept_connection = kdc_tcp_accept,
536 .recv_handler = kdc_tcp_recv,
537 .send_handler = kdc_tcp_send
541 * Start listening on the given address
543 NTSTATUS kdc_add_socket(struct kdc_server *kdc,
544 const struct model_ops *model_ops,
548 kdc_process_fn_t process,
551 struct kdc_socket *kdc_socket;
552 struct kdc_udp_socket *kdc_udp_socket;
553 struct tevent_req *udpsubreq;
557 kdc_socket = talloc(kdc, struct kdc_socket);
558 NT_STATUS_HAVE_NO_MEMORY(kdc_socket);
560 kdc_socket->kdc = kdc;
561 kdc_socket->process = process;
563 ret = tsocket_address_inet_from_strings(kdc_socket, "ip",
565 &kdc_socket->local_address);
567 status = map_nt_error_from_unix_common(errno);
572 status = stream_setup_socket(kdc->task,
573 kdc->task->event_ctx,
577 "ip", address, &port,
578 lpcfg_socket_options(kdc->task->lp_ctx),
580 if (!NT_STATUS_IS_OK(status)) {
581 DEBUG(0,("Failed to bind to %s:%u TCP - %s\n",
582 address, port, nt_errstr(status)));
583 talloc_free(kdc_socket);
588 kdc_udp_socket = talloc(kdc_socket, struct kdc_udp_socket);
589 NT_STATUS_HAVE_NO_MEMORY(kdc_udp_socket);
591 kdc_udp_socket->kdc_socket = kdc_socket;
593 ret = tdgram_inet_udp_socket(kdc_socket->local_address,
596 &kdc_udp_socket->dgram);
598 status = map_nt_error_from_unix_common(errno);
599 DEBUG(0,("Failed to bind to %s:%u UDP - %s\n",
600 address, port, nt_errstr(status)));
604 kdc_udp_socket->send_queue = tevent_queue_create(kdc_udp_socket,
605 "kdc_udp_send_queue");
606 NT_STATUS_HAVE_NO_MEMORY(kdc_udp_socket->send_queue);
608 udpsubreq = tdgram_recvfrom_send(kdc_udp_socket,
609 kdc->task->event_ctx,
610 kdc_udp_socket->dgram);
611 NT_STATUS_HAVE_NO_MEMORY(udpsubreq);
612 tevent_req_set_callback(udpsubreq, kdc_udp_call_loop, kdc_udp_socket);