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.h"
28 #include "smbd/service_stream.h"
29 #include "smbd/process_model.h"
30 #include "lib/events/events.h"
31 #include "lib/socket/socket.h"
33 #include "system/network.h"
34 #include "lib/util/dlinklist.h"
35 #include "lib/messaging/irpc.h"
36 #include "lib/stream/packet.h"
37 #include "librpc/gen_ndr/samr.h"
38 #include "lib/socket/netif.h"
40 /* hold all the info needed to send a reply */
42 struct kdc_reply *next, *prev;
43 struct socket_address *dest;
47 typedef BOOL (*kdc_process_fn_t)(struct kdc_server *kdc,
51 struct socket_address *peer_addr,
52 struct socket_address *my_addr,
55 /* hold information about one kdc socket */
57 struct socket_context *sock;
58 struct kdc_server *kdc;
61 /* a queue of outgoing replies that have been deferred */
62 struct kdc_reply *send_queue;
64 kdc_process_fn_t process;
67 state of an open tcp connection
69 struct kdc_tcp_connection {
70 /* stream connection we belong to */
71 struct stream_connection *conn;
73 /* the kdc_server the connection belongs to */
74 struct kdc_server *kdc;
76 struct packet_context *packet;
78 kdc_process_fn_t process;
82 handle fd send events on a KDC socket
84 static void kdc_send_handler(struct kdc_socket *kdc_socket)
86 while (kdc_socket->send_queue) {
87 struct kdc_reply *rep = kdc_socket->send_queue;
91 status = socket_sendto(kdc_socket->sock, &rep->packet, &sendlen,
93 if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
96 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_BUFFER_SIZE)) {
97 /* Replace with a krb err, response to big */
100 DLIST_REMOVE(kdc_socket->send_queue, rep);
104 if (kdc_socket->send_queue == NULL) {
105 EVENT_FD_NOT_WRITEABLE(kdc_socket->fde);
111 handle fd recv events on a KDC socket
113 static void kdc_recv_handler(struct kdc_socket *kdc_socket)
116 TALLOC_CTX *tmp_ctx = talloc_new(kdc_socket);
118 struct kdc_reply *rep;
121 struct socket_address *src;
122 struct socket_address *my_addr;
125 status = socket_pending(kdc_socket->sock, &dsize);
126 if (!NT_STATUS_IS_OK(status)) {
127 talloc_free(tmp_ctx);
131 blob = data_blob_talloc(tmp_ctx, NULL, dsize);
132 if (blob.data == NULL) {
133 /* hope this is a temporary low memory condition */
134 talloc_free(tmp_ctx);
138 status = socket_recvfrom(kdc_socket->sock, blob.data, blob.length, &nread,
140 if (!NT_STATUS_IS_OK(status)) {
141 talloc_free(tmp_ctx);
146 DEBUG(10,("Received krb5 UDP packet of length %lu from %s:%u\n",
147 (long)blob.length, src->addr, (uint16_t)src->port));
149 my_addr = socket_get_my_addr(kdc_socket->sock, tmp_ctx);
151 talloc_free(tmp_ctx);
157 ret = kdc_socket->process(kdc_socket->kdc,
164 talloc_free(tmp_ctx);
168 /* queue a pending reply */
169 rep = talloc(kdc_socket, struct kdc_reply);
171 talloc_free(tmp_ctx);
174 rep->dest = talloc_steal(rep, src);
176 talloc_steal(rep, reply.data);
178 if (rep->packet.data == NULL) {
180 talloc_free(tmp_ctx);
184 DLIST_ADD_END(kdc_socket->send_queue, rep, struct kdc_reply *);
185 EVENT_FD_WRITEABLE(kdc_socket->fde);
186 talloc_free(tmp_ctx);
190 handle fd events on a KDC socket
192 static void kdc_socket_handler(struct event_context *ev, struct fd_event *fde,
193 uint16_t flags, void *private)
195 struct kdc_socket *kdc_socket = talloc_get_type(private, struct kdc_socket);
196 if (flags & EVENT_FD_WRITE) {
197 kdc_send_handler(kdc_socket);
199 if (flags & EVENT_FD_READ) {
200 kdc_recv_handler(kdc_socket);
204 static void kdc_tcp_terminate_connection(struct kdc_tcp_connection *kdcconn, const char *reason)
206 stream_terminate_connection(kdcconn->conn, reason);
210 receive a full packet on a KDC connection
212 static NTSTATUS kdc_tcp_recv(void *private, DATA_BLOB blob)
214 struct kdc_tcp_connection *kdcconn = talloc_get_type(private,
215 struct kdc_tcp_connection);
216 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
217 TALLOC_CTX *tmp_ctx = talloc_new(kdcconn);
219 DATA_BLOB input, reply;
220 struct socket_address *src_addr;
221 struct socket_address *my_addr;
223 talloc_steal(tmp_ctx, blob.data);
225 src_addr = socket_get_peer_addr(kdcconn->conn->socket, tmp_ctx);
227 talloc_free(tmp_ctx);
228 return NT_STATUS_NO_MEMORY;
231 my_addr = socket_get_my_addr(kdcconn->conn->socket, tmp_ctx);
233 talloc_free(tmp_ctx);
234 return NT_STATUS_NO_MEMORY;
238 input = data_blob_const(blob.data + 4, blob.length - 4);
240 ret = kdcconn->process(kdcconn->kdc,
246 0 /* Not datagram */);
248 talloc_free(tmp_ctx);
249 return NT_STATUS_INTERNAL_ERROR;
252 /* and now encode the reply */
253 blob = data_blob_talloc(kdcconn, NULL, reply.length + 4);
255 talloc_free(tmp_ctx);
256 return NT_STATUS_NO_MEMORY;
259 RSIVAL(blob.data, 0, reply.length);
260 memcpy(blob.data + 4, reply.data, reply.length);
262 status = packet_send(kdcconn->packet, blob);
263 if (!NT_STATUS_IS_OK(status)) {
264 talloc_free(tmp_ctx);
268 /* the call isn't needed any more */
269 talloc_free(tmp_ctx);
274 receive some data on a KDC connection
276 static void kdc_tcp_recv_handler(struct stream_connection *conn, uint16_t flags)
278 struct kdc_tcp_connection *kdcconn = talloc_get_type(conn->private,
279 struct kdc_tcp_connection);
280 packet_recv(kdcconn->packet);
284 called on a tcp recv error
286 static void kdc_tcp_recv_error(void *private, NTSTATUS status)
288 struct kdc_tcp_connection *kdcconn = talloc_get_type(private, struct kdc_tcp_connection);
289 kdc_tcp_terminate_connection(kdcconn, nt_errstr(status));
293 called when we can write to a connection
295 static void kdc_tcp_send(struct stream_connection *conn, uint16_t flags)
297 struct kdc_tcp_connection *kdcconn = talloc_get_type(conn->private,
298 struct kdc_tcp_connection);
299 packet_queue_run(kdcconn->packet);
303 Wrapper for krb5_kdc_process_krb5_request, converting to/from Samba
307 static BOOL kdc_process(struct kdc_server *kdc,
311 struct socket_address *peer_addr,
312 struct socket_address *my_addr,
317 krb5_data_zero(&k5_reply);
319 DEBUG(10,("Received KDC packet of length %lu from %s:%d\n",
320 (long)input->length - 4, peer_addr->addr, peer_addr->port));
322 ret = krb5_kdc_process_krb5_request(kdc->smb_krb5_context->krb5_context,
324 input->data, input->length,
330 *reply = data_blob(NULL, 0);
333 if (k5_reply.length) {
334 *reply = data_blob_talloc(mem_ctx, k5_reply.data, k5_reply.length);
335 krb5_free_data_contents(kdc->smb_krb5_context->krb5_context, &k5_reply);
337 *reply = data_blob(NULL, 0);
343 called when we get a new connection
345 static void kdc_tcp_generic_accept(struct stream_connection *conn, kdc_process_fn_t process_fn)
347 struct kdc_server *kdc = talloc_get_type(conn->private, struct kdc_server);
348 struct kdc_tcp_connection *kdcconn;
350 kdcconn = talloc_zero(conn, struct kdc_tcp_connection);
352 stream_terminate_connection(conn, "kdc_tcp_accept: out of memory");
355 kdcconn->conn = conn;
357 kdcconn->process = process_fn;
358 conn->private = kdcconn;
360 kdcconn->packet = packet_init(kdcconn);
361 if (kdcconn->packet == NULL) {
362 kdc_tcp_terminate_connection(kdcconn, "kdc_tcp_accept: out of memory");
365 packet_set_private(kdcconn->packet, kdcconn);
366 packet_set_socket(kdcconn->packet, conn->socket);
367 packet_set_callback(kdcconn->packet, kdc_tcp_recv);
368 packet_set_full_request(kdcconn->packet, packet_full_request_u32);
369 packet_set_error_handler(kdcconn->packet, kdc_tcp_recv_error);
370 packet_set_event_context(kdcconn->packet, conn->event.ctx);
371 packet_set_fde(kdcconn->packet, conn->event.fde);
372 packet_set_serialise(kdcconn->packet);
375 static void kdc_tcp_accept(struct stream_connection *conn)
377 kdc_tcp_generic_accept(conn, kdc_process);
380 static const struct stream_server_ops kdc_tcp_stream_ops = {
382 .accept_connection = kdc_tcp_accept,
383 .recv_handler = kdc_tcp_recv_handler,
384 .send_handler = kdc_tcp_send
387 static void kpasswdd_tcp_accept(struct stream_connection *conn)
389 kdc_tcp_generic_accept(conn, kpasswdd_process);
392 static const struct stream_server_ops kpasswdd_tcp_stream_ops = {
393 .name = "kpasswdd_tcp",
394 .accept_connection = kpasswdd_tcp_accept,
395 .recv_handler = kdc_tcp_recv_handler,
396 .send_handler = kdc_tcp_send
400 start listening on the given address
402 static NTSTATUS kdc_add_socket(struct kdc_server *kdc, const char *address)
404 const struct model_ops *model_ops;
405 struct kdc_socket *kdc_socket;
406 struct kdc_socket *kpasswd_socket;
407 struct socket_address *kdc_address, *kpasswd_address;
409 uint16_t kdc_port = lp_krb5_port();
410 uint16_t kpasswd_port = lp_kpasswd_port();
412 kdc_socket = talloc(kdc, struct kdc_socket);
413 NT_STATUS_HAVE_NO_MEMORY(kdc_socket);
415 kpasswd_socket = talloc(kdc, struct kdc_socket);
416 NT_STATUS_HAVE_NO_MEMORY(kpasswd_socket);
418 status = socket_create("ip", SOCKET_TYPE_DGRAM, &kdc_socket->sock, 0);
419 if (!NT_STATUS_IS_OK(status)) {
420 talloc_free(kdc_socket);
424 status = socket_create("ip", SOCKET_TYPE_DGRAM, &kpasswd_socket->sock, 0);
425 if (!NT_STATUS_IS_OK(status)) {
426 talloc_free(kpasswd_socket);
430 kdc_socket->kdc = kdc;
431 kdc_socket->send_queue = NULL;
432 kdc_socket->process = kdc_process;
434 talloc_steal(kdc_socket, kdc_socket->sock);
436 kdc_socket->fde = event_add_fd(kdc->task->event_ctx, kdc,
437 socket_get_fd(kdc_socket->sock), EVENT_FD_READ,
438 kdc_socket_handler, kdc_socket);
440 kdc_address = socket_address_from_strings(kdc_socket, kdc_socket->sock->backend_name,
442 NT_STATUS_HAVE_NO_MEMORY(kdc_address);
444 status = socket_listen(kdc_socket->sock, kdc_address, 0, 0);
445 if (!NT_STATUS_IS_OK(status)) {
446 DEBUG(0,("Failed to bind to %s:%d UDP for kdc - %s\n",
447 address, kdc_port, nt_errstr(status)));
448 talloc_free(kdc_socket);
452 kpasswd_socket->kdc = kdc;
453 kpasswd_socket->send_queue = NULL;
454 kpasswd_socket->process = kpasswdd_process;
456 talloc_steal(kpasswd_socket, kpasswd_socket->sock);
458 kpasswd_socket->fde = event_add_fd(kdc->task->event_ctx, kdc,
459 socket_get_fd(kpasswd_socket->sock), EVENT_FD_READ,
460 kdc_socket_handler, kpasswd_socket);
462 kpasswd_address = socket_address_from_strings(kpasswd_socket, kpasswd_socket->sock->backend_name,
463 address, kpasswd_port);
464 NT_STATUS_HAVE_NO_MEMORY(kpasswd_address);
466 status = socket_listen(kpasswd_socket->sock, kpasswd_address, 0, 0);
467 if (!NT_STATUS_IS_OK(status)) {
468 DEBUG(0,("Failed to bind to %s:%d UDP for kpasswd - %s\n",
469 address, kpasswd_port, nt_errstr(status)));
470 talloc_free(kpasswd_socket);
474 /* within the kdc task we want to be a single process, so
475 ask for the single process model ops and pass these to the
476 stream_setup_socket() call. */
477 model_ops = process_model_byname("single");
479 DEBUG(0,("Can't find 'single' process model_ops\n"));
480 talloc_free(kdc_socket);
481 return NT_STATUS_INTERNAL_ERROR;
484 status = stream_setup_socket(kdc->task->event_ctx, model_ops,
486 "ip", address, &kdc_port, kdc);
487 if (!NT_STATUS_IS_OK(status)) {
488 DEBUG(0,("Failed to bind to %s:%u TCP - %s\n",
489 address, kdc_port, nt_errstr(status)));
490 talloc_free(kdc_socket);
494 status = stream_setup_socket(kdc->task->event_ctx, model_ops,
495 &kpasswdd_tcp_stream_ops,
496 "ip", address, &kpasswd_port, kdc);
497 if (!NT_STATUS_IS_OK(status)) {
498 DEBUG(0,("Failed to bind to %s:%u TCP - %s\n",
499 address, kpasswd_port, nt_errstr(status)));
500 talloc_free(kdc_socket);
509 setup our listening sockets on the configured network interfaces
511 static NTSTATUS kdc_startup_interfaces(struct kdc_server *kdc)
513 int num_interfaces = iface_count();
514 TALLOC_CTX *tmp_ctx = talloc_new(kdc);
519 for (i=0; i<num_interfaces; i++) {
520 const char *address = talloc_strdup(tmp_ctx, iface_n_ip(i));
521 status = kdc_add_socket(kdc, address);
522 NT_STATUS_NOT_OK_RETURN(status);
525 talloc_free(tmp_ctx);
533 static void kdc_task_init(struct task_server *task)
535 struct kdc_server *kdc;
539 switch (lp_server_role()) {
540 case ROLE_STANDALONE:
541 task_server_terminate(task, "kdc: no KDC required in standalone configuration");
543 case ROLE_DOMAIN_MEMBER:
544 task_server_terminate(task, "kdc: no KDC required in member server configuration");
546 case ROLE_DOMAIN_CONTROLLER:
547 /* Yes, we want a KDC */
551 if (iface_count() == 0) {
552 task_server_terminate(task, "kdc: no network interfaces configured");
556 task_server_set_title(task, "task[kdc]");
558 kdc = talloc(task, struct kdc_server);
560 task_server_terminate(task, "kdc: out of memory");
566 /* Setup the KDC configuration */
567 kdc->config = talloc(kdc, krb5_kdc_configuration);
569 task_server_terminate(task, "kdc: out of memory");
572 krb5_kdc_default_config(kdc->config);
574 initialize_krb5_error_table();
576 ret = smb_krb5_init_context(kdc, &kdc->smb_krb5_context);
578 DEBUG(1,("kdc_task_init: krb5_init_context failed (%s)\n",
579 error_message(ret)));
580 task_server_terminate(task, "kdc: krb5_init_context failed");
584 krb5_add_et_list(kdc->smb_krb5_context->krb5_context, initialize_hdb_error_table_r);
586 kdc->config->logf = kdc->smb_krb5_context->logf;
587 kdc->config->db = talloc(kdc->config, struct HDB *);
588 if (!kdc->config->db) {
589 task_server_terminate(task, "kdc: out of memory");
592 kdc->config->num_db = 1;
594 status = kdc_hdb_ldb_create(kdc, kdc->smb_krb5_context->krb5_context,
595 &kdc->config->db[0], NULL);
596 if (!NT_STATUS_IS_OK(status)) {
597 task_server_terminate(task, "kdc: hdb_ldb_create (setup KDC database) failed");
601 ret = krb5_kt_register(kdc->smb_krb5_context->krb5_context, &hdb_kt_ops);
603 task_server_terminate(task, "kdc: failed to register hdb keytab");
606 /* start listening on the configured network interfaces */
607 status = kdc_startup_interfaces(kdc);
608 if (!NT_STATUS_IS_OK(status)) {
609 task_server_terminate(task, "kdc failed to setup interfaces");
613 irpc_add_name(task->msg_ctx, "kdc_server");
618 called on startup of the KDC service
620 static NTSTATUS kdc_init(struct event_context *event_ctx,
621 const struct model_ops *model_ops)
623 return task_server_startup(event_ctx, model_ops, kdc_task_init);
626 /* called at smbd startup - register ourselves as a server service */
627 NTSTATUS server_service_kdc_init(void)
629 return register_server_service("kdc", kdc_init);