r7264: fix up the socket handling for abartlet. Still only udp, but it won't
[kai/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 #include "system/network.h"
30 #include "dlinklist.h"
31
32 /*
33   handle fd send events on a KDC socket
34 */
35 static void kdc_send_handler(struct kdc_socket *kdc_socket)
36 {
37         while (kdc_socket->send_queue) {
38                 struct kdc_reply *rep = kdc_socket->send_queue;
39                 NTSTATUS status;
40                 size_t sendlen;
41
42                 status = socket_sendto(kdc_socket->sock, &rep->packet, &sendlen, 0,
43                                        rep->dest_address, rep->dest_port);
44                 if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
45                         break;
46                 }
47                 
48                 DLIST_REMOVE(kdc_socket->send_queue, rep);
49                 talloc_free(rep);
50         }
51
52         if (kdc_socket->send_queue == NULL) {
53                 EVENT_FD_NOT_WRITEABLE(kdc_socket->fde);
54         }
55 }
56
57
58 /*
59   handle fd recv events on a KDC socket
60 */
61 static void kdc_recv_handler(struct kdc_socket *kdc_socket)
62 {
63         NTSTATUS status;
64         TALLOC_CTX *tmp_ctx = talloc_new(kdc_socket);
65         DATA_BLOB blob;
66         struct kdc_reply *rep;
67         krb5_data reply;
68         size_t nread, dsize;
69         const char *src_addr;
70         int src_port;
71         struct sockaddr_in src_sock_addr;
72         struct ipv4_addr addr;
73
74         status = socket_pending(kdc_socket->sock, &dsize);
75         if (!NT_STATUS_IS_OK(status)) {
76                 talloc_free(tmp_ctx);
77                 return;
78         }
79
80         blob = data_blob_talloc(kdc_socket, NULL, dsize);
81         if (blob.data == NULL) {
82                 /* hope this is a temporary low memory condition */
83                 talloc_free(tmp_ctx);
84                 return;
85         }
86
87         status = socket_recvfrom(kdc_socket->sock, blob.data, blob.length, &nread, 0,
88                                  &src_addr, &src_port);
89         if (!NT_STATUS_IS_OK(status)) {
90                 talloc_free(tmp_ctx);
91                 return;
92         }
93         talloc_steal(tmp_ctx, src_addr);
94         blob.length = nread;
95         
96         DEBUG(2,("Received krb5 packet of length %d from %s:%d\n", 
97                  blob.length, src_addr, src_port));
98         
99         /* TODO:  This really should be in a utility function somewhere */
100         ZERO_STRUCT(src_sock_addr);
101 #ifdef HAVE_SOCK_SIN_LEN
102         src_sock_addr.sin_len         = sizeof(src_sock_addr);
103 #endif
104         addr                     = interpret_addr2(src_addr);
105         src_sock_addr.sin_addr.s_addr = addr.addr;
106         src_sock_addr.sin_port        = htons(src_port);
107         src_sock_addr.sin_family      = PF_INET;
108         
109         /* Call krb5 */
110         if (krb5_kdc_process_krb5_request(kdc_socket->kdc->krb5_context, 
111                                           kdc_socket->kdc->config,
112                                           blob.data, blob.length, 
113                                           &reply,
114                                           src_addr,
115                                           (struct sockaddr *)&src_sock_addr) == -1) {
116                 talloc_free(tmp_ctx);
117                 return;
118         }
119
120         /* queue a pending reply */
121         rep = talloc(kdc_socket, struct kdc_reply);
122         if (rep == NULL) {
123                 talloc_free(tmp_ctx);
124                 return;
125         }
126         rep->dest_address = talloc_steal(rep, src_addr);
127         rep->dest_port    = src_port;
128         rep->packet       = data_blob_talloc(rep, reply.data, reply.length);
129         if (rep->packet.data == NULL) {
130                 talloc_free(rep);
131                 talloc_free(tmp_ctx);
132                 return;
133         }
134
135         DLIST_ADD_END(kdc_socket->send_queue, rep, struct kdc_reply *);
136         EVENT_FD_WRITEABLE(kdc_socket->fde);
137         talloc_free(tmp_ctx);
138 }
139
140 /*
141   handle fd events on a KDC socket
142 */
143 static void kdc_socket_handler(struct event_context *ev, struct fd_event *fde,
144                                uint16_t flags, void *private)
145 {
146         struct kdc_socket *kdc_socket = talloc_get_type(private, struct kdc_socket);
147         if (flags & EVENT_FD_WRITE) {
148                 kdc_send_handler(kdc_socket);
149         } else if (flags & EVENT_FD_READ) {
150                 kdc_recv_handler(kdc_socket);
151         }
152 }
153
154
155 /*
156   start listening on the given address
157 */
158 static NTSTATUS kdc_add_socket(struct kdc_server *kdc, const char *address)
159 {
160         struct kdc_socket *kdc_socket;
161         NTSTATUS status;
162
163         kdc_socket = talloc(kdc, struct kdc_socket);
164         NT_STATUS_HAVE_NO_MEMORY(kdc_socket);
165
166         status = socket_create("ip", SOCKET_TYPE_DGRAM, &kdc_socket->sock, 0);
167         if (!NT_STATUS_IS_OK(status)) {
168                 talloc_free(kdc_socket);
169                 return status;
170         }
171
172         kdc_socket->kdc = kdc;
173         kdc_socket->send_queue = NULL;
174
175         talloc_steal(kdc_socket, kdc_socket->sock);
176
177         kdc_socket->fde = event_add_fd(kdc->task->event_ctx, kdc, 
178                                        socket_get_fd(kdc_socket->sock), EVENT_FD_READ,
179                                        kdc_socket_handler, kdc_socket);
180
181         status = socket_listen(kdc_socket->sock, address, lp_krb5_port(), 0, 0);
182         if (!NT_STATUS_IS_OK(status)) {
183                 DEBUG(0,("Failed to bind to %s:%d - %s\n", 
184                          address, lp_krb5_port(), nt_errstr(status)));
185                 talloc_free(kdc_socket);
186                 return status;
187         }
188
189         return NT_STATUS_OK;
190 }
191
192
193 /*
194   setup our listening sockets on the configured network interfaces
195 */
196 NTSTATUS kdc_startup_interfaces(struct kdc_server *kdc)
197 {
198         int num_interfaces = iface_count();
199         TALLOC_CTX *tmp_ctx = talloc_new(kdc);
200         NTSTATUS status;
201
202         /* if we are allowing incoming packets from any address, then
203            we need to bind to the wildcard address */
204         if (!lp_bind_interfaces_only()) {
205                 status = kdc_add_socket(kdc, "0.0.0.0");
206                 NT_STATUS_NOT_OK_RETURN(status);
207         } else {
208                 int i;
209
210                 for (i=0; i<num_interfaces; i++) {
211                         const char *address = talloc_strdup(tmp_ctx, iface_n_ip(i));
212                         status = kdc_add_socket(kdc, address);
213                         NT_STATUS_NOT_OK_RETURN(status);
214                 }
215         }
216
217         talloc_free(tmp_ctx);
218
219         return NT_STATUS_OK;
220 }
221
222 /*
223   startup the kdc task
224 */
225 static void kdc_task_init(struct task_server *task)
226 {
227         struct kdc_server *kdc;
228         NTSTATUS status;
229         krb5_error_code ret;
230
231         if (iface_count() == 0) {
232                 task_terminate(task, "kdc: no network interfaces configured");
233                 return;
234         }
235
236         kdc = talloc(task, struct kdc_server);
237         if (kdc == NULL) {
238                 task_terminate(task, "kdc: out of memory");
239                 return;
240         }
241
242         kdc->task = task;
243
244         /* Setup the KDC configuration */
245         kdc->config = talloc(kdc, struct krb5_kdc_configuration);
246         if (!kdc->config) {
247                 task_terminate(task, "kdc: out of memory");
248                 return;
249         }
250         krb5_kdc_default_config(kdc->config);
251
252         initialize_krb5_error_table();
253
254         ret = krb5_init_context(&kdc->krb5_context);
255         if (ret) {
256                 DEBUG(1,("kdc_task_init: krb5_init_context failed (%s)\n", 
257                          error_message(ret)));
258                 task_terminate(task, "kdc: krb5_init_context failed");
259                 return; 
260         }
261
262         /* TODO: Fill in the hdb and logging details */
263         kdc_openlog(kdc->krb5_context, kdc->config);
264         
265         kdc->config->db = talloc(kdc->config, struct HDB *);
266         if (!kdc->config->db) {
267                 task_terminate(task, "kdc: out of memory");
268                 return;
269         }
270         kdc->config->num_db = 1;
271                 
272         ret = hdb_ldb_create(kdc->krb5_context, &kdc->config->db[0], lp_sam_url());
273         if (ret != 0) {
274                 DEBUG(1, ("kdc_task_init: hdb_ldb_create fails: %s\n", 
275                           smb_get_krb5_error_message(kdc->krb5_context, ret, kdc))); 
276                 task_terminate(task, "kdc: hdb_ldb_create failed");
277                 return; 
278         }
279
280         /* start listening on the configured network interfaces */
281         status = kdc_startup_interfaces(kdc);
282         if (!NT_STATUS_IS_OK(status)) {
283                 task_terminate(task, "kdc failed to setup interfaces");
284                 return;
285         }
286 }
287
288
289 /*
290   called on startup of the KDC service 
291 */
292 static NTSTATUS kdc_init(struct event_context *event_ctx, 
293                          const struct model_ops *model_ops)
294 {       
295         return task_server_startup(event_ctx, model_ops, kdc_task_init);
296 }
297
298 /* called at smbd startup - register ourselves as a server service */
299 NTSTATUS server_service_kdc_init(void)
300 {
301         return register_server_service("kdc", kdc_init);
302 }