r7911: task_terminate() is defined in the macosx headers, so change the name
[ira/wip.git] / source / 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         int ret;
74
75         status = socket_pending(kdc_socket->sock, &dsize);
76         if (!NT_STATUS_IS_OK(status)) {
77                 talloc_free(tmp_ctx);
78                 return;
79         }
80
81         blob = data_blob_talloc(kdc_socket, NULL, dsize);
82         if (blob.data == NULL) {
83                 /* hope this is a temporary low memory condition */
84                 talloc_free(tmp_ctx);
85                 return;
86         }
87
88         status = socket_recvfrom(kdc_socket->sock, blob.data, blob.length, &nread, 0,
89                                  &src_addr, &src_port);
90         if (!NT_STATUS_IS_OK(status)) {
91                 talloc_free(tmp_ctx);
92                 return;
93         }
94         talloc_steal(tmp_ctx, src_addr);
95         blob.length = nread;
96         
97         DEBUG(2,("Received krb5 packet of length %d from %s:%d\n", 
98                  blob.length, src_addr, src_port));
99         
100         /* TODO:  This really should be in a utility function somewhere */
101         ZERO_STRUCT(src_sock_addr);
102 #ifdef HAVE_SOCK_SIN_LEN
103         src_sock_addr.sin_len         = sizeof(src_sock_addr);
104 #endif
105         addr                     = interpret_addr2(src_addr);
106         src_sock_addr.sin_addr.s_addr = addr.addr;
107         src_sock_addr.sin_port        = htons(src_port);
108         src_sock_addr.sin_family      = PF_INET;
109         
110         /* Call krb5 */
111         ret = krb5_kdc_process_krb5_request(kdc_socket->kdc->smb_krb5_context->krb5_context, 
112                                             kdc_socket->kdc->config,
113                                             blob.data, blob.length, 
114                                             &reply,
115                                             src_addr,
116                                             (struct sockaddr *)&src_sock_addr);
117         if (ret == -1) {
118                 talloc_free(tmp_ctx);
119                 return;
120         }
121
122         /* queue a pending reply */
123         rep = talloc(kdc_socket, struct kdc_reply);
124         if (rep == NULL) {
125                 krb5_data_free(&reply);
126                 talloc_free(tmp_ctx);
127                 return;
128         }
129         rep->dest_address = talloc_steal(rep, src_addr);
130         rep->dest_port    = src_port;
131         rep->packet       = data_blob_talloc(rep, reply.data, reply.length);
132         krb5_data_free(&reply);
133
134         if (rep->packet.data == NULL) {
135                 talloc_free(rep);
136                 talloc_free(tmp_ctx);
137                 return;
138         }
139
140         DLIST_ADD_END(kdc_socket->send_queue, rep, struct kdc_reply *);
141         EVENT_FD_WRITEABLE(kdc_socket->fde);
142         talloc_free(tmp_ctx);
143 }
144
145 /*
146   handle fd events on a KDC socket
147 */
148 static void kdc_socket_handler(struct event_context *ev, struct fd_event *fde,
149                                uint16_t flags, void *private)
150 {
151         struct kdc_socket *kdc_socket = talloc_get_type(private, struct kdc_socket);
152         if (flags & EVENT_FD_WRITE) {
153                 kdc_send_handler(kdc_socket);
154         } 
155         if (flags & EVENT_FD_READ) {
156                 kdc_recv_handler(kdc_socket);
157         }
158 }
159
160
161 /*
162   start listening on the given address
163 */
164 static NTSTATUS kdc_add_socket(struct kdc_server *kdc, const char *address)
165 {
166         struct kdc_socket *kdc_socket;
167         NTSTATUS status;
168
169         kdc_socket = talloc(kdc, struct kdc_socket);
170         NT_STATUS_HAVE_NO_MEMORY(kdc_socket);
171
172         status = socket_create("ip", SOCKET_TYPE_DGRAM, &kdc_socket->sock, 0);
173         if (!NT_STATUS_IS_OK(status)) {
174                 talloc_free(kdc_socket);
175                 return status;
176         }
177
178         kdc_socket->kdc = kdc;
179         kdc_socket->send_queue = NULL;
180
181         talloc_steal(kdc_socket, kdc_socket->sock);
182
183         kdc_socket->fde = event_add_fd(kdc->task->event_ctx, kdc, 
184                                        socket_get_fd(kdc_socket->sock), EVENT_FD_READ,
185                                        kdc_socket_handler, kdc_socket);
186
187         status = socket_listen(kdc_socket->sock, address, lp_krb5_port(), 0, 0);
188         if (!NT_STATUS_IS_OK(status)) {
189                 DEBUG(0,("Failed to bind to %s:%d - %s\n", 
190                          address, lp_krb5_port(), nt_errstr(status)));
191                 talloc_free(kdc_socket);
192                 return status;
193         }
194
195         return NT_STATUS_OK;
196 }
197
198
199 /*
200   setup our listening sockets on the configured network interfaces
201 */
202 NTSTATUS kdc_startup_interfaces(struct kdc_server *kdc)
203 {
204         int num_interfaces = iface_count();
205         TALLOC_CTX *tmp_ctx = talloc_new(kdc);
206         NTSTATUS status;
207
208         /* if we are allowing incoming packets from any address, then
209            we need to bind to the wildcard address */
210         if (!lp_bind_interfaces_only()) {
211                 status = kdc_add_socket(kdc, "0.0.0.0");
212                 NT_STATUS_NOT_OK_RETURN(status);
213         } else {
214                 int i;
215
216                 for (i=0; i<num_interfaces; i++) {
217                         const char *address = talloc_strdup(tmp_ctx, iface_n_ip(i));
218                         status = kdc_add_socket(kdc, address);
219                         NT_STATUS_NOT_OK_RETURN(status);
220                 }
221         }
222
223         talloc_free(tmp_ctx);
224
225         return NT_STATUS_OK;
226 }
227
228 /*
229   startup the kdc task
230 */
231 static void kdc_task_init(struct task_server *task)
232 {
233         struct kdc_server *kdc;
234         NTSTATUS status;
235         krb5_error_code ret;
236
237         if (iface_count() == 0) {
238                 task_server_terminate(task, "kdc: no network interfaces configured");
239                 return;
240         }
241
242         kdc = talloc(task, struct kdc_server);
243         if (kdc == NULL) {
244                 task_server_terminate(task, "kdc: out of memory");
245                 return;
246         }
247
248         kdc->task = task;
249
250         /* Setup the KDC configuration */
251         kdc->config = talloc(kdc, struct krb5_kdc_configuration);
252         if (!kdc->config) {
253                 task_server_terminate(task, "kdc: out of memory");
254                 return;
255         }
256         krb5_kdc_default_config(kdc->config);
257
258         /* NAT and the like make this pointless, and painful */
259         kdc->config->check_ticket_addresses = FALSE;
260
261         initialize_krb5_error_table();
262
263         ret = smb_krb5_init_context(kdc, &kdc->smb_krb5_context);
264         if (ret) {
265                 DEBUG(1,("kdc_task_init: krb5_init_context failed (%s)\n", 
266                          error_message(ret)));
267                 task_server_terminate(task, "kdc: krb5_init_context failed");
268                 return; 
269         }
270
271         kdc->config->logf = kdc->smb_krb5_context->logf;
272         kdc->config->db = talloc(kdc->config, struct HDB *);
273         if (!kdc->config->db) {
274                 task_server_terminate(task, "kdc: out of memory");
275                 return;
276         }
277         kdc->config->num_db = 1;
278                 
279         ret = hdb_ldb_create(kdc->smb_krb5_context->krb5_context, 
280                              &kdc->config->db[0], lp_sam_url());
281         if (ret != 0) {
282                 DEBUG(1, ("kdc_task_init: hdb_ldb_create fails: %s\n", 
283                           smb_get_krb5_error_message(kdc->smb_krb5_context->krb5_context, ret, kdc))); 
284                 task_server_terminate(task, "kdc: hdb_ldb_create failed");
285                 return; 
286         }
287
288         /* start listening on the configured network interfaces */
289         status = kdc_startup_interfaces(kdc);
290         if (!NT_STATUS_IS_OK(status)) {
291                 task_server_terminate(task, "kdc failed to setup interfaces");
292                 return;
293         }
294 }
295
296
297 /*
298   called on startup of the KDC service 
299 */
300 static NTSTATUS kdc_init(struct event_context *event_ctx, 
301                          const struct model_ops *model_ops)
302 {       
303         return task_server_startup(event_ctx, model_ops, kdc_task_init);
304 }
305
306 /* called at smbd startup - register ourselves as a server service */
307 NTSTATUS server_service_kdc_init(void)
308 {
309         return register_server_service("kdc", kdc_init);
310 }