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