2 Unix SMB/CIFS implementation.
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
7 Copyright (C) Andrew Tridgell 2005
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 2 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, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "smbd/service_task.h"
26 #include "lib/events/events.h"
27 #include "lib/socket/socket.h"
32 handle fd events on a cldap_socket
34 static void kdc_socket_handler(struct event_context *ev, struct fd_event *fde,
35 uint16_t flags, void *private)
38 struct kdc_socket *kdc_socket = talloc_get_type(private, struct kdc_socket);
39 if (flags & EVENT_FD_WRITE) {
40 /* this should not happen */
41 } else if (flags & EVENT_FD_READ) {
42 TALLOC_CTX *tmp_ctx = talloc_new(kdc_socket);
43 DATA_BLOB blob = data_blob_talloc(tmp_ctx, NULL, 64 * 1024);
48 DEBUG(0, ("incoming!\n"));
50 status = socket_recvfrom(kdc_socket->sock, blob.data, blob.length, &nread, 0,
51 &src_addr, &src_port);
52 if (!NT_STATUS_IS_OK(status)) {
56 talloc_steal(tmp_ctx, src_addr);
59 DEBUG(2,("Received krb5 packet of length %d from %s:%d\n",
60 blob.length, src_addr, src_port));
67 start listening on the given address
69 static NTSTATUS kdc_add_socket(struct kdc_server *kdc, const char *address)
71 struct kdc_socket *kdc_socket;
74 kdc_socket = talloc(kdc, struct kdc_socket);
75 NT_STATUS_HAVE_NO_MEMORY(kdc_socket);
77 status = socket_create("ip", SOCKET_TYPE_DGRAM, &kdc_socket->sock, 0);
78 if (!NT_STATUS_IS_OK(status)) {
79 talloc_free(kdc_socket);
83 kdc_socket->kdc = kdc;
85 talloc_steal(kdc_socket, kdc_socket->sock);
87 kdc_socket->fde = event_add_fd(kdc->task->event_ctx, kdc,
88 socket_get_fd(kdc_socket->sock), 0,
89 kdc_socket_handler, kdc_socket);
91 status = socket_listen(kdc_socket->sock, address, lp_krb5_port(), 0, 0);
92 if (!NT_STATUS_IS_OK(status)) {
93 DEBUG(0,("Failed to bind to %s:%d - %s\n",
94 address, lp_krb5_port(), nt_errstr(status)));
95 talloc_free(kdc_socket);
104 setup our listening sockets on the configured network interfaces
106 NTSTATUS kdc_startup_interfaces(struct kdc_server *kdc)
108 int num_interfaces = iface_count();
109 TALLOC_CTX *tmp_ctx = talloc_new(kdc);
112 /* if we are allowing incoming packets from any address, then
113 we need to bind to the wildcard address */
114 if (!lp_bind_interfaces_only()) {
115 status = kdc_add_socket(kdc, "0.0.0.0");
116 NT_STATUS_NOT_OK_RETURN(status);
120 for (i=0; i<num_interfaces; i++) {
121 const char *address = talloc_strdup(tmp_ctx, iface_n_ip(i));
122 status = kdc_add_socket(kdc, address);
123 NT_STATUS_NOT_OK_RETURN(status);
127 talloc_free(tmp_ctx);
135 static void kdc_task_init(struct task_server *task)
137 struct kdc_server *kdc;
140 if (iface_count() == 0) {
141 task_terminate(task, "kdc: no network interfaces configured");
145 kdc = talloc(task, struct kdc_server);
147 task_terminate(task, "kdc: out of memory");
153 /* Setup the KDC configuration */
154 kdc->config = talloc(kdc, struct krb5_kdc_configuration);
156 task_terminate(task, "kdc: out of memory");
159 krb5_kdc_default_config(kdc->config);
161 /* TODO: Fill in the hdb and logging details */
163 /* start listening on the configured network interfaces */
164 status = kdc_startup_interfaces(kdc);
165 if (!NT_STATUS_IS_OK(status)) {
166 task_terminate(task, "kdc failed to setup interfaces");
170 DEBUG(0, ("When I grow up, I want to be a KDC!\n"));
175 called on startup of the KDC service
177 static NTSTATUS kdc_init(struct event_context *event_ctx,
178 const struct model_ops *model_ops)
180 return task_server_startup(event_ctx, model_ops, kdc_task_init);
183 /* called at smbd startup - register ourselves as a server service */
184 NTSTATUS server_service_kdc_init(void)
186 return register_server_service("kdc", kdc_init);