2 Unix SMB/CIFS implementation.
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
7 Copyright (C) Andrew Tridgell 2005
8 Copyright (C) Stefan Metzmacher 2005
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 2 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, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "smbd/service_task.h"
27 #include "smbd/service_stream.h"
28 #include "lib/events/events.h"
29 #include "lib/socket/socket.h"
31 #include "system/network.h"
32 #include "dlinklist.h"
33 #include "lib/messaging/irpc.h"
35 /* hold all the info needed to send a reply */
37 struct kdc_reply *next, *prev;
38 const char *dest_address;
43 /* hold information about one kdc socket */
45 struct socket_context *sock;
46 struct kdc_server *kdc;
49 /* a queue of outgoing replies that have been deferred */
50 struct kdc_reply *send_queue;
52 BOOL (*process)(struct kdc_server *kdc,
60 state of an open tcp connection
62 struct kdc_tcp_connection {
63 /* stream connection we belong to */
64 struct stream_connection *conn;
66 /* the kdc_server the connection belongs to */
67 struct kdc_server *kdc;
69 /* the partial data we've receiced yet */
72 /* the amount that we used yet from the partial buffer */
73 uint32_t partial_read;
75 /* prevent loops when we use half async code, while processing a requuest */
78 /* a queue of outgoing replies that have been deferred */
79 struct data_blob_list_item *send_queue;
81 BOOL (*process)(struct kdc_server *kdc,
90 handle fd send events on a KDC socket
92 static void kdc_send_handler(struct kdc_socket *kdc_socket)
94 while (kdc_socket->send_queue) {
95 struct kdc_reply *rep = kdc_socket->send_queue;
99 status = socket_sendto(kdc_socket->sock, &rep->packet, &sendlen, 0,
100 rep->dest_address, rep->dest_port);
101 if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
105 DLIST_REMOVE(kdc_socket->send_queue, rep);
109 if (kdc_socket->send_queue == NULL) {
110 EVENT_FD_NOT_WRITEABLE(kdc_socket->fde);
116 handle fd recv events on a KDC socket
118 static void kdc_recv_handler(struct kdc_socket *kdc_socket)
121 TALLOC_CTX *tmp_ctx = talloc_new(kdc_socket);
123 struct kdc_reply *rep;
126 const char *src_addr;
130 status = socket_pending(kdc_socket->sock, &dsize);
131 if (!NT_STATUS_IS_OK(status)) {
132 talloc_free(tmp_ctx);
136 blob = data_blob_talloc(kdc_socket, NULL, dsize);
137 if (blob.data == NULL) {
138 /* hope this is a temporary low memory condition */
139 talloc_free(tmp_ctx);
143 status = socket_recvfrom(kdc_socket->sock, blob.data, blob.length, &nread, 0,
144 &src_addr, &src_port);
145 if (!NT_STATUS_IS_OK(status)) {
146 talloc_free(tmp_ctx);
149 talloc_steal(tmp_ctx, src_addr);
152 DEBUG(2,("Received krb5 UDP packet of length %u from %s:%u\n",
153 blob.length, src_addr, (uint16_t)src_port));
156 ret = kdc_socket->process(kdc_socket->kdc,
162 talloc_free(tmp_ctx);
166 /* queue a pending reply */
167 rep = talloc(kdc_socket, struct kdc_reply);
169 talloc_free(tmp_ctx);
172 rep->dest_address = talloc_steal(rep, src_addr);
173 rep->dest_port = src_port;
175 talloc_steal(rep, reply.data);
177 if (rep->packet.data == NULL) {
179 talloc_free(tmp_ctx);
183 DLIST_ADD_END(kdc_socket->send_queue, rep, struct kdc_reply *);
184 EVENT_FD_WRITEABLE(kdc_socket->fde);
185 talloc_free(tmp_ctx);
189 handle fd events on a KDC socket
191 static void kdc_socket_handler(struct event_context *ev, struct fd_event *fde,
192 uint16_t flags, void *private)
194 struct kdc_socket *kdc_socket = talloc_get_type(private, struct kdc_socket);
195 if (flags & EVENT_FD_WRITE) {
196 kdc_send_handler(kdc_socket);
198 if (flags & EVENT_FD_READ) {
199 kdc_recv_handler(kdc_socket);
203 static void kdc_tcp_terminate_connection(struct kdc_tcp_connection *kdcconn, const char *reason)
205 stream_terminate_connection(kdcconn->conn, reason);
209 receive some data on a KDC connection
211 static void kdc_tcp_recv(struct stream_connection *conn, uint16_t flags)
213 struct kdc_tcp_connection *kdcconn = talloc_get_type(conn->private, struct kdc_tcp_connection);
214 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
215 TALLOC_CTX *tmp_ctx = talloc_new(kdcconn);
216 struct data_blob_list_item *rep;
218 const char *src_addr;
221 DATA_BLOB input, reply;
223 /* avoid recursion, because of half async code */
224 if (kdcconn->processing) {
225 EVENT_FD_NOT_READABLE(conn->event.fde);
229 if (kdcconn->partial.length == 0) {
230 kdcconn->partial = data_blob_talloc(kdcconn, NULL, 4);
231 if (!kdcconn->partial.data) goto nomem;
233 kdcconn->partial_read = 0;
236 /* read in the packet length */
237 if (kdcconn->partial_read < 4) {
238 uint32_t packet_length;
240 status = socket_recv(conn->socket,
241 kdcconn->partial.data + kdcconn->partial_read,
242 4 - kdcconn->partial_read,
244 if (NT_STATUS_IS_ERR(status)) goto failed;
245 if (!NT_STATUS_IS_OK(status)) return;
247 kdcconn->partial_read += nread;
248 if (kdcconn->partial_read != 4) return;
250 packet_length = RIVAL(kdcconn->partial.data, 0) + 4;
252 kdcconn->partial.data = talloc_realloc(kdcconn, kdcconn->partial.data,
253 uint8_t, packet_length);
254 if (!kdcconn->partial.data) goto nomem;
256 kdcconn->partial.length = packet_length;
259 /* read in the body */
260 status = socket_recv(conn->socket,
261 kdcconn->partial.data + kdcconn->partial_read,
262 kdcconn->partial.length - kdcconn->partial_read,
264 if (NT_STATUS_IS_ERR(status)) goto failed;
265 if (!NT_STATUS_IS_OK(status)) return;
267 kdcconn->partial_read += nread;
268 if (kdcconn->partial_read != kdcconn->partial.length) return;
271 * we have parsed the request, so we can reset the kdcconn->partial_read,
272 * maybe we could also free kdcconn->partial, but for now we keep it,
273 * and overwrite it the next time
275 kdcconn->partial_read = 0;
277 src_addr = socket_get_peer_addr(kdcconn->conn->socket, tmp_ctx);
278 if (!src_addr) goto nomem;
279 src_port = socket_get_peer_port(kdcconn->conn->socket);
281 DEBUG(2,("Received krb5 TCP packet of length %u from %s:%u\n",
282 kdcconn->partial.length - 4, src_addr, src_port));
285 kdcconn->processing = True;
286 input = data_blob_const(kdcconn->partial.data + 4, kdcconn->partial.length - 4);
288 ret = kdcconn->process(kdcconn->kdc,
293 kdcconn->processing = False;
295 status = NT_STATUS_INTERNAL_ERROR;
299 /* and now encode the reply */
300 rep = talloc(kdcconn, struct data_blob_list_item);
305 rep->blob = data_blob_talloc(rep, NULL, reply.length + 4);
306 if (!rep->blob.data) {
310 RSIVAL(rep->blob.data, 0, reply.length);
311 memcpy(rep->blob.data + 4, reply.data, reply.length);
313 if (!kdcconn->send_queue) {
314 EVENT_FD_WRITEABLE(kdcconn->conn->event.fde);
316 DLIST_ADD_END(kdcconn->send_queue, rep, struct data_blob_list_item *);
318 EVENT_FD_READABLE(kdcconn->conn->event.fde);
320 /* the call isn't needed any more */
321 talloc_free(tmp_ctx);
324 status = NT_STATUS_NO_MEMORY;
326 kdc_tcp_terminate_connection(kdcconn, nt_errstr(status));
330 called when we can write to a connection
332 static void kdc_tcp_send(struct stream_connection *conn, uint16_t flags)
334 struct kdc_tcp_connection *kdcconn = talloc_get_type(conn->private, struct kdc_tcp_connection);
337 while (kdcconn->send_queue) {
338 struct data_blob_list_item *q = kdcconn->send_queue;
341 status = socket_send(conn->socket, &q->blob, &sendlen, 0);
342 if (NT_STATUS_IS_ERR(status)) goto failed;
343 if (!NT_STATUS_IS_OK(status)) return;
345 q->blob.length -= sendlen;
346 q->blob.data += sendlen;
348 if (q->blob.length == 0) {
349 DLIST_REMOVE(kdcconn->send_queue, q);
354 EVENT_FD_NOT_WRITEABLE(conn->event.fde);
357 kdc_tcp_terminate_connection(kdcconn, nt_errstr(status));
361 Wrapper for krb5_kdc_process_krb5_request, converting to/from Samba
365 static BOOL kdc_process(struct kdc_server *kdc,
369 const char *src_addr,
374 struct ipv4_addr addr;
375 struct sockaddr_in src_sock_addr;
377 /* TODO: This really should be in a utility function somewhere */
378 ZERO_STRUCT(src_sock_addr);
379 #ifdef HAVE_SOCK_SIN_LEN
380 src_sock_addr.sin_len = sizeof(src_sock_addr);
382 addr = interpret_addr2(src_addr);
383 src_sock_addr.sin_addr.s_addr = addr.addr;
384 src_sock_addr.sin_port = htons(src_port);
385 src_sock_addr.sin_family = PF_INET;
388 ret = krb5_kdc_process_krb5_request(kdc->smb_krb5_context->krb5_context,
390 input->data, input->length,
393 (struct sockaddr *)&src_sock_addr);
395 *reply = data_blob(NULL, 0);
398 *reply = data_blob_talloc(mem_ctx, k5_reply.data, k5_reply.length);
399 krb5_data_free(&k5_reply);
404 called when we get a new connection
406 static void kdc_tcp_accept(struct stream_connection *conn)
408 struct kdc_server *kdc = talloc_get_type(conn->private, struct kdc_server);
409 struct kdc_tcp_connection *kdcconn;
411 kdcconn = talloc_zero(conn, struct kdc_tcp_connection);
413 stream_terminate_connection(conn, "kdc_tcp_accept: out of memory");
416 kdcconn->conn = conn;
418 kdcconn->process = kdc_process;
419 conn->private = kdcconn;
422 static const struct stream_server_ops kdc_tcp_stream_ops = {
424 .accept_connection = kdc_tcp_accept,
425 .recv_handler = kdc_tcp_recv,
426 .send_handler = kdc_tcp_send
430 called when we get a new connection
432 void kpasswdd_tcp_accept(struct stream_connection *conn)
434 struct kdc_server *kdc = talloc_get_type(conn->private, struct kdc_server);
435 struct kdc_tcp_connection *kdcconn;
437 kdcconn = talloc_zero(conn, struct kdc_tcp_connection);
439 stream_terminate_connection(conn, "kdc_tcp_accept: out of memory");
442 kdcconn->conn = conn;
444 kdcconn->process = kpasswdd_process;
445 conn->private = kdcconn;
448 static const struct stream_server_ops kpasswdd_tcp_stream_ops = {
449 .name = "kpasswdd_tcp",
450 .accept_connection = kpasswdd_tcp_accept,
451 .recv_handler = kdc_tcp_recv,
452 .send_handler = kdc_tcp_send
456 start listening on the given address
458 static NTSTATUS kdc_add_socket(struct kdc_server *kdc, const char *address)
460 const struct model_ops *model_ops;
461 struct kdc_socket *kdc_socket;
462 struct kdc_socket *kpasswd_socket;
464 uint16_t kdc_port = lp_krb5_port();
465 uint16_t kpasswd_port = lp_kpasswd_port();
467 kdc_socket = talloc(kdc, struct kdc_socket);
468 NT_STATUS_HAVE_NO_MEMORY(kdc_socket);
470 kpasswd_socket = talloc(kdc, struct kdc_socket);
471 NT_STATUS_HAVE_NO_MEMORY(kpasswd_socket);
473 status = socket_create("ip", SOCKET_TYPE_DGRAM, &kdc_socket->sock, 0);
474 if (!NT_STATUS_IS_OK(status)) {
475 talloc_free(kdc_socket);
479 status = socket_create("ip", SOCKET_TYPE_DGRAM, &kpasswd_socket->sock, 0);
480 if (!NT_STATUS_IS_OK(status)) {
481 talloc_free(kpasswd_socket);
485 kdc_socket->kdc = kdc;
486 kdc_socket->send_queue = NULL;
487 kdc_socket->process = kdc_process;
489 talloc_steal(kdc_socket, kdc_socket->sock);
491 kdc_socket->fde = event_add_fd(kdc->task->event_ctx, kdc,
492 socket_get_fd(kdc_socket->sock), EVENT_FD_READ,
493 kdc_socket_handler, kdc_socket);
495 status = socket_listen(kdc_socket->sock, address, kdc_port, 0, 0);
496 if (!NT_STATUS_IS_OK(status)) {
497 DEBUG(0,("Failed to bind to %s:%d UDP for kdc - %s\n",
498 address, kdc_port, nt_errstr(status)));
499 talloc_free(kdc_socket);
503 kpasswd_socket->kdc = kdc;
504 kpasswd_socket->send_queue = NULL;
505 kpasswd_socket->process = kpasswdd_process;
507 talloc_steal(kpasswd_socket, kpasswd_socket->sock);
509 kpasswd_socket->fde = event_add_fd(kdc->task->event_ctx, kdc,
510 socket_get_fd(kpasswd_socket->sock), EVENT_FD_READ,
511 kdc_socket_handler, kpasswd_socket);
513 status = socket_listen(kpasswd_socket->sock, address, kpasswd_port, 0, 0);
514 if (!NT_STATUS_IS_OK(status)) {
515 DEBUG(0,("Failed to bind to %s:%d UDP for kpasswd - %s\n",
516 address, kpasswd_port, nt_errstr(status)));
517 talloc_free(kpasswd_socket);
521 /* within the kdc task we want to be a single process, so
522 ask for the single process model ops and pass these to the
523 stream_setup_socket() call. */
524 model_ops = process_model_byname("single");
526 DEBUG(0,("Can't find 'single' process model_ops\n"));
527 talloc_free(kdc_socket);
528 return NT_STATUS_INTERNAL_ERROR;
531 status = stream_setup_socket(kdc->task->event_ctx, model_ops,
533 "ip", address, &kdc_port, kdc);
534 if (!NT_STATUS_IS_OK(status)) {
535 DEBUG(0,("Failed to bind to %s:%u TCP - %s\n",
536 address, kdc_port, nt_errstr(status)));
537 talloc_free(kdc_socket);
541 status = stream_setup_socket(kdc->task->event_ctx, model_ops,
542 &kpasswdd_tcp_stream_ops,
543 "ip", address, &kpasswd_port, kdc);
544 if (!NT_STATUS_IS_OK(status)) {
545 DEBUG(0,("Failed to bind to %s:%u TCP - %s\n",
546 address, kpasswd_port, nt_errstr(status)));
547 talloc_free(kdc_socket);
556 setup our listening sockets on the configured network interfaces
558 static NTSTATUS kdc_startup_interfaces(struct kdc_server *kdc)
560 int num_interfaces = iface_count();
561 TALLOC_CTX *tmp_ctx = talloc_new(kdc);
564 /* if we are allowing incoming packets from any address, then
565 we need to bind to the wildcard address */
566 if (!lp_bind_interfaces_only()) {
567 status = kdc_add_socket(kdc, "0.0.0.0");
568 NT_STATUS_NOT_OK_RETURN(status);
572 for (i=0; i<num_interfaces; i++) {
573 const char *address = talloc_strdup(tmp_ctx, iface_n_ip(i));
574 status = kdc_add_socket(kdc, address);
575 NT_STATUS_NOT_OK_RETURN(status);
579 talloc_free(tmp_ctx);
587 static void kdc_task_init(struct task_server *task)
589 struct kdc_server *kdc;
593 if (iface_count() == 0) {
594 task_server_terminate(task, "kdc: no network interfaces configured");
598 kdc = talloc(task, struct kdc_server);
600 task_server_terminate(task, "kdc: out of memory");
606 /* Setup the KDC configuration */
607 kdc->config = talloc(kdc, krb5_kdc_configuration);
609 task_server_terminate(task, "kdc: out of memory");
612 krb5_kdc_default_config(kdc->config);
614 /* NAT and the like make this pointless, and painful */
615 kdc->config->check_ticket_addresses = FALSE;
617 initialize_krb5_error_table();
619 ret = smb_krb5_init_context(kdc, &kdc->smb_krb5_context);
621 DEBUG(1,("kdc_task_init: krb5_init_context failed (%s)\n",
622 error_message(ret)));
623 task_server_terminate(task, "kdc: krb5_init_context failed");
627 krb5_add_et_list(kdc->smb_krb5_context->krb5_context, initialize_hdb_error_table_r);
629 kdc->config->logf = kdc->smb_krb5_context->logf;
630 kdc->config->db = talloc(kdc->config, struct HDB *);
631 if (!kdc->config->db) {
632 task_server_terminate(task, "kdc: out of memory");
635 kdc->config->num_db = 1;
637 ret = hdb_ldb_create(kdc, kdc->smb_krb5_context->krb5_context,
638 &kdc->config->db[0], NULL);
640 DEBUG(1, ("kdc_task_init: hdb_ldb_create fails: %s\n",
641 smb_get_krb5_error_message(kdc->smb_krb5_context->krb5_context, ret, kdc)));
642 task_server_terminate(task, "kdc: hdb_ldb_create failed");
646 /* start listening on the configured network interfaces */
647 status = kdc_startup_interfaces(kdc);
648 if (!NT_STATUS_IS_OK(status)) {
649 task_server_terminate(task, "kdc failed to setup interfaces");
653 irpc_add_name(task->msg_ctx, "kdc_server");
658 called on startup of the KDC service
660 static NTSTATUS kdc_init(struct event_context *event_ctx,
661 const struct model_ops *model_ops)
663 return task_server_startup(event_ctx, model_ops, kdc_task_init);
666 /* called at smbd startup - register ourselves as a server service */
667 NTSTATUS server_service_kdc_init(void)
669 return register_server_service("kdc", kdc_init);