r7221: Add the start of a KDC service (to be built on a 'libkdc' from a to be
[bbaumbach/samba-autobuild/.git] / source4 / kdc / kdc.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    KDC Server startup
5
6    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
7    Copyright (C) Andrew Tridgell        2005
8
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.
13    
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.
18    
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.
22 */
23
24 #include "includes.h"
25 #include "smbd/service_task.h"
26 #include "lib/events/events.h"
27 #include "lib/socket/socket.h"
28 #include "kdc/kdc.h"
29
30
31 /*
32   handle fd events on a cldap_socket
33 */
34 static void kdc_socket_handler(struct event_context *ev, struct fd_event *fde,
35                                uint16_t flags, void *private)
36 {
37         NTSTATUS status;
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);
44                 size_t nread;
45                 const char *src_addr;
46                 int src_port;
47
48                 DEBUG(0, ("incoming!\n"));
49
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)) {
53                         talloc_free(tmp_ctx);
54                         return;
55                 }
56                 talloc_steal(tmp_ctx, src_addr);
57                 blob.length = nread;
58                 
59                 DEBUG(2,("Received krb5 packet of length %d from %s:%d\n", 
60                          blob.length, src_addr, src_port));
61                 
62
63         }
64 }
65
66 /*
67   start listening on the given address
68 */
69 static NTSTATUS kdc_add_socket(struct kdc_server *kdc, const char *address)
70 {
71         struct kdc_socket *kdc_socket;
72         NTSTATUS status;
73
74         kdc_socket = talloc(kdc, struct kdc_socket);
75         NT_STATUS_HAVE_NO_MEMORY(kdc_socket);
76
77         status = socket_create("ip", SOCKET_TYPE_DGRAM, &kdc_socket->sock, 0);
78         if (!NT_STATUS_IS_OK(status)) {
79                 talloc_free(kdc_socket);
80                 return status;
81         }
82
83         kdc_socket->kdc = kdc;
84
85         talloc_steal(kdc_socket, kdc_socket->sock);
86
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);
90
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);
96                 return status;
97         }
98
99         return NT_STATUS_OK;
100 }
101
102
103 /*
104   setup our listening sockets on the configured network interfaces
105 */
106 NTSTATUS kdc_startup_interfaces(struct kdc_server *kdc)
107 {
108         int num_interfaces = iface_count();
109         TALLOC_CTX *tmp_ctx = talloc_new(kdc);
110         NTSTATUS status;
111
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);
117         } else {
118                 int i;
119
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);
124                 }
125         }
126
127         talloc_free(tmp_ctx);
128
129         return NT_STATUS_OK;
130 }
131
132 /*
133   startup the kdc task
134 */
135 static void kdc_task_init(struct task_server *task)
136 {
137         struct kdc_server *kdc;
138         NTSTATUS status;
139
140         if (iface_count() == 0) {
141                 task_terminate(task, "kdc: no network interfaces configured");
142                 return;
143         }
144
145         kdc = talloc(task, struct kdc_server);
146         if (kdc == NULL) {
147                 task_terminate(task, "kdc: out of memory");
148                 return;
149         }
150
151         kdc->task = task;
152
153         /* Setup the KDC configuration */
154         kdc->config = talloc(kdc, struct krb5_kdc_configuration);
155         if (!kdc->config) {
156                 task_terminate(task, "kdc: out of memory");
157                 return;
158         }
159         krb5_kdc_default_config(kdc->config);
160
161         /* TODO: Fill in the hdb and logging details */
162
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");
167                 return;
168         }
169
170         DEBUG(0, ("When I grow up, I want to be a KDC!\n"));
171 }
172
173
174 /*
175   called on startup of the KDC service 
176 */
177 static NTSTATUS kdc_init(struct event_context *event_ctx, 
178                          const struct model_ops *model_ops)
179 {       
180         return task_server_startup(event_ctx, model_ops, kdc_task_init);
181 }
182
183 /* called at smbd startup - register ourselves as a server service */
184 NTSTATUS server_service_kdc_init(void)
185 {
186         return register_server_service("kdc", kdc_init);
187 }