r11713: separate out the setting of the fde in the packet context from the
[jelmer/samba4-debian.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    Copyright (C) Stefan Metzmacher      2005
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25 #include "includes.h"
26 #include "smbd/service_task.h"
27 #include "smbd/service_stream.h"
28 #include "lib/events/events.h"
29 #include "lib/socket/socket.h"
30 #include "kdc/kdc.h"
31 #include "system/network.h"
32 #include "dlinklist.h"
33 #include "lib/messaging/irpc.h"
34 #include "lib/stream/packet.h"
35
36 /* hold all the info needed to send a reply */
37 struct kdc_reply {
38         struct kdc_reply *next, *prev;
39         const char *dest_address;
40         int dest_port;
41         DATA_BLOB packet;
42 };
43
44 /* hold information about one kdc socket */
45 struct kdc_socket {
46         struct socket_context *sock;
47         struct kdc_server *kdc;
48         struct fd_event *fde;
49
50         /* a queue of outgoing replies that have been deferred */
51         struct kdc_reply *send_queue;
52
53         BOOL (*process)(struct kdc_server *kdc,
54                         TALLOC_CTX *mem_ctx, 
55                         DATA_BLOB *input, 
56                         DATA_BLOB *reply,
57                         const char *from,
58                         int src_port);
59 };
60 /*
61   state of an open tcp connection
62 */
63 struct kdc_tcp_connection {
64         /* stream connection we belong to */
65         struct stream_connection *conn;
66
67         /* the kdc_server the connection belongs to */
68         struct kdc_server *kdc;
69
70         struct packet_context *packet;
71
72         BOOL (*process)(struct kdc_server *kdc,
73                          TALLOC_CTX *mem_ctx, 
74                          DATA_BLOB *input, 
75                          DATA_BLOB *reply,
76                          const char *from,
77                          int src_port);
78 };
79
80 /*
81   handle fd send events on a KDC socket
82 */
83 static void kdc_send_handler(struct kdc_socket *kdc_socket)
84 {
85         while (kdc_socket->send_queue) {
86                 struct kdc_reply *rep = kdc_socket->send_queue;
87                 NTSTATUS status;
88                 size_t sendlen;
89
90                 status = socket_sendto(kdc_socket->sock, &rep->packet, &sendlen, 0,
91                                        rep->dest_address, rep->dest_port);
92                 if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
93                         break;
94                 }
95                 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_BUFFER_SIZE)) {
96                         /* Replace with a krb err, response to big */
97                 }
98                 
99                 DLIST_REMOVE(kdc_socket->send_queue, rep);
100                 talloc_free(rep);
101         }
102
103         if (kdc_socket->send_queue == NULL) {
104                 EVENT_FD_NOT_WRITEABLE(kdc_socket->fde);
105         }
106 }
107
108
109 /*
110   handle fd recv events on a KDC socket
111 */
112 static void kdc_recv_handler(struct kdc_socket *kdc_socket)
113 {
114         NTSTATUS status;
115         TALLOC_CTX *tmp_ctx = talloc_new(kdc_socket);
116         DATA_BLOB blob;
117         struct kdc_reply *rep;
118         DATA_BLOB reply;
119         size_t nread, dsize;
120         const char *src_addr;
121         int src_port;
122         int ret;
123
124         status = socket_pending(kdc_socket->sock, &dsize);
125         if (!NT_STATUS_IS_OK(status)) {
126                 talloc_free(tmp_ctx);
127                 return;
128         }
129
130         blob = data_blob_talloc(kdc_socket, NULL, dsize);
131         if (blob.data == NULL) {
132                 /* hope this is a temporary low memory condition */
133                 talloc_free(tmp_ctx);
134                 return;
135         }
136
137         status = socket_recvfrom(kdc_socket->sock, blob.data, blob.length, &nread, 0,
138                                  &src_addr, &src_port);
139         if (!NT_STATUS_IS_OK(status)) {
140                 talloc_free(tmp_ctx);
141                 return;
142         }
143         talloc_steal(tmp_ctx, src_addr);
144         blob.length = nread;
145         
146         DEBUG(2,("Received krb5 UDP packet of length %u from %s:%u\n", 
147                  blob.length, src_addr, (uint16_t)src_port));
148         
149         /* Call krb5 */
150         ret = kdc_socket->process(kdc_socket->kdc, 
151                                   tmp_ctx, 
152                                   &blob,  
153                                   &reply,
154                                   src_addr, src_port);
155         if (!ret) {
156                 talloc_free(tmp_ctx);
157                 return;
158         }
159
160         /* queue a pending reply */
161         rep = talloc(kdc_socket, struct kdc_reply);
162         if (rep == NULL) {
163                 talloc_free(tmp_ctx);
164                 return;
165         }
166         rep->dest_address = talloc_steal(rep, src_addr);
167         rep->dest_port    = src_port;
168         rep->packet       = reply;
169         talloc_steal(rep, reply.data);
170
171         if (rep->packet.data == NULL) {
172                 talloc_free(rep);
173                 talloc_free(tmp_ctx);
174                 return;
175         }
176
177         DLIST_ADD_END(kdc_socket->send_queue, rep, struct kdc_reply *);
178         EVENT_FD_WRITEABLE(kdc_socket->fde);
179         talloc_free(tmp_ctx);
180 }
181
182 /*
183   handle fd events on a KDC socket
184 */
185 static void kdc_socket_handler(struct event_context *ev, struct fd_event *fde,
186                                uint16_t flags, void *private)
187 {
188         struct kdc_socket *kdc_socket = talloc_get_type(private, struct kdc_socket);
189         if (flags & EVENT_FD_WRITE) {
190                 kdc_send_handler(kdc_socket);
191         } 
192         if (flags & EVENT_FD_READ) {
193                 kdc_recv_handler(kdc_socket);
194         }
195 }
196
197 static void kdc_tcp_terminate_connection(struct kdc_tcp_connection *kdcconn, const char *reason)
198 {
199         stream_terminate_connection(kdcconn->conn, reason);
200 }
201
202 /*
203   receive a full packet on a KDC connection
204 */
205 static NTSTATUS kdc_tcp_recv(void *private, DATA_BLOB blob)
206 {
207         struct kdc_tcp_connection *kdcconn = talloc_get_type(private, 
208                                                              struct kdc_tcp_connection);
209         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
210         TALLOC_CTX *tmp_ctx = talloc_new(kdcconn);
211         const char *src_addr;
212         int src_port;
213         int ret;
214         DATA_BLOB input, reply;
215
216         talloc_steal(tmp_ctx, blob.data);
217
218         src_addr = socket_get_peer_addr(kdcconn->conn->socket, tmp_ctx);
219         if (!src_addr) goto nomem;
220         src_port = socket_get_peer_port(kdcconn->conn->socket);
221
222         DEBUG(2,("Received krb5 TCP packet of length %u from %s:%u\n", 
223                  blob.length - 4, src_addr, src_port));
224
225         /* Call krb5 */
226         input = data_blob_const(blob.data + 4, blob.length - 4); 
227
228         ret = kdcconn->process(kdcconn->kdc, 
229                                tmp_ctx,
230                                &input,
231                                &reply,
232                                src_addr, src_port);
233         if (!ret) {
234                 status = NT_STATUS_INTERNAL_ERROR;
235                 goto failed;
236         }
237
238         /* and now encode the reply */
239         blob = data_blob_talloc(kdcconn, NULL, reply.length + 4);
240         if (!blob.data) {
241                 goto nomem;
242         }
243
244         RSIVAL(blob.data, 0, reply.length);
245         memcpy(blob.data + 4, reply.data, reply.length);        
246
247         status = packet_send(kdcconn->packet, blob);
248         if (!NT_STATUS_IS_OK(status)) {
249                 goto failed;
250         }
251
252         /* the call isn't needed any more */
253         talloc_free(tmp_ctx);
254         return NT_STATUS_OK;
255 nomem:
256         status = NT_STATUS_NO_MEMORY;
257
258 failed:
259         return status;
260 }
261
262 /*
263   receive some data on a KDC connection
264 */
265 static void kdc_tcp_recv_handler(struct stream_connection *conn, uint16_t flags)
266 {
267         struct kdc_tcp_connection *kdcconn = talloc_get_type(conn->private, 
268                                                              struct kdc_tcp_connection);
269         packet_recv(kdcconn->packet);
270 }
271
272 /*
273   called on a tcp recv error
274 */
275 static void kdc_tcp_recv_error(void *private, NTSTATUS status)
276 {
277         struct kdc_tcp_connection *kdcconn = talloc_get_type(private, struct kdc_tcp_connection);
278         kdc_tcp_terminate_connection(kdcconn, nt_errstr(status));
279 }
280
281 /*
282   called when we can write to a connection
283 */
284 static void kdc_tcp_send(struct stream_connection *conn, uint16_t flags)
285 {
286         struct kdc_tcp_connection *kdcconn = talloc_get_type(conn->private, 
287                                                              struct kdc_tcp_connection);
288         packet_queue_run(kdcconn->packet);
289 }
290
291 /**
292    Wrapper for krb5_kdc_process_krb5_request, converting to/from Samba
293    calling conventions
294 */
295
296 static BOOL kdc_process(struct kdc_server *kdc,
297                         TALLOC_CTX *mem_ctx, 
298                         DATA_BLOB *input, 
299                         DATA_BLOB *reply,
300                         const char *src_addr,
301                         int src_port)
302 {
303         int ret;        
304         krb5_data k5_reply;
305         struct ipv4_addr addr;
306         struct sockaddr_in src_sock_addr;
307
308         /* TODO:  This really should be in a utility function somewhere */
309         ZERO_STRUCT(src_sock_addr);
310 #ifdef HAVE_SOCK_SIN_LEN
311         src_sock_addr.sin_len           = sizeof(src_sock_addr);
312 #endif
313         addr                            = interpret_addr2(src_addr);
314         src_sock_addr.sin_addr.s_addr   = addr.addr;
315         src_sock_addr.sin_port          = htons(src_port);
316         src_sock_addr.sin_family        = PF_INET;
317
318         
319         ret = krb5_kdc_process_krb5_request(kdc->smb_krb5_context->krb5_context, 
320                                             kdc->config,
321                                             input->data, input->length,
322                                             &k5_reply,
323                                             src_addr,
324                                             (struct sockaddr *)&src_sock_addr);
325         if (ret == -1) {
326                 *reply = data_blob(NULL, 0);
327                 return False;
328         }
329         *reply = data_blob_talloc(mem_ctx, k5_reply.data, k5_reply.length);
330         krb5_data_free(&k5_reply);
331         return True;
332 }
333
334 /*
335   called when we get a new connection
336 */
337 static void kdc_tcp_accept(struct stream_connection *conn)
338 {
339         struct kdc_server *kdc = talloc_get_type(conn->private, struct kdc_server);
340         struct kdc_tcp_connection *kdcconn;
341
342         kdcconn = talloc_zero(conn, struct kdc_tcp_connection);
343         if (!kdcconn) {
344                 stream_terminate_connection(conn, "kdc_tcp_accept: out of memory");
345                 return;
346         }
347         kdcconn->conn    = conn;
348         kdcconn->kdc     = kdc;
349         kdcconn->process = kdc_process;
350         conn->private    = kdcconn;
351
352         kdcconn->packet = packet_init(kdcconn);
353         if (kdcconn->packet == NULL) {
354                 stream_terminate_connection(conn, "kdc_tcp_accept: out of memory");
355                 return;
356         }
357         packet_set_private(kdcconn->packet, kdcconn);
358         packet_set_socket(kdcconn->packet, conn->socket);
359         packet_set_callback(kdcconn->packet, kdc_tcp_recv);
360         packet_set_full_request(kdcconn->packet, packet_full_request_u32);
361         packet_set_error_handler(kdcconn->packet, kdc_tcp_recv_error);
362         packet_set_event_context(kdcconn->packet, conn->event.ctx);
363         packet_set_fde(kdcconn->packet, conn->event.fde);
364         packet_set_serialise(kdcconn->packet);
365 }
366
367 static const struct stream_server_ops kdc_tcp_stream_ops = {
368         .name                   = "kdc_tcp",
369         .accept_connection      = kdc_tcp_accept,
370         .recv_handler           = kdc_tcp_recv_handler,
371         .send_handler           = kdc_tcp_send
372 };
373
374 /*
375   called when we get a new connection
376 */
377 void kpasswdd_tcp_accept(struct stream_connection *conn)
378 {
379         struct kdc_server *kdc = talloc_get_type(conn->private, struct kdc_server);
380         struct kdc_tcp_connection *kdcconn;
381
382         kdcconn = talloc_zero(conn, struct kdc_tcp_connection);
383         if (!kdcconn) {
384                 stream_terminate_connection(conn, "kdc_tcp_accept: out of memory");
385                 return;
386         }
387         kdcconn->conn    = conn;
388         kdcconn->kdc     = kdc;
389         kdcconn->process = kpasswdd_process;
390         conn->private    = kdcconn;
391 }
392
393 static const struct stream_server_ops kpasswdd_tcp_stream_ops = {
394         .name                   = "kpasswdd_tcp",
395         .accept_connection      = kpasswdd_tcp_accept,
396         .recv_handler           = kdc_tcp_recv_handler,
397         .send_handler           = kdc_tcp_send
398 };
399
400 /*
401   start listening on the given address
402 */
403 static NTSTATUS kdc_add_socket(struct kdc_server *kdc, const char *address)
404 {
405         const struct model_ops *model_ops;
406         struct kdc_socket *kdc_socket;
407         struct kdc_socket *kpasswd_socket;
408         NTSTATUS status;
409         uint16_t kdc_port = lp_krb5_port();
410         uint16_t kpasswd_port = lp_kpasswd_port();
411
412         kdc_socket = talloc(kdc, struct kdc_socket);
413         NT_STATUS_HAVE_NO_MEMORY(kdc_socket);
414
415         kpasswd_socket = talloc(kdc, struct kdc_socket);
416         NT_STATUS_HAVE_NO_MEMORY(kpasswd_socket);
417
418         status = socket_create("ip", SOCKET_TYPE_DGRAM, &kdc_socket->sock, 0);
419         if (!NT_STATUS_IS_OK(status)) {
420                 talloc_free(kdc_socket);
421                 return status;
422         }
423
424         status = socket_create("ip", SOCKET_TYPE_DGRAM, &kpasswd_socket->sock, 0);
425         if (!NT_STATUS_IS_OK(status)) {
426                 talloc_free(kpasswd_socket);
427                 return status;
428         }
429
430         kdc_socket->kdc = kdc;
431         kdc_socket->send_queue = NULL;
432         kdc_socket->process = kdc_process;
433
434         talloc_steal(kdc_socket, kdc_socket->sock);
435
436         kdc_socket->fde = event_add_fd(kdc->task->event_ctx, kdc, 
437                                        socket_get_fd(kdc_socket->sock), EVENT_FD_READ,
438                                        kdc_socket_handler, kdc_socket);
439
440         status = socket_listen(kdc_socket->sock, address, kdc_port, 0, 0);
441         if (!NT_STATUS_IS_OK(status)) {
442                 DEBUG(0,("Failed to bind to %s:%d UDP for kdc - %s\n", 
443                          address, kdc_port, nt_errstr(status)));
444                 talloc_free(kdc_socket);
445                 return status;
446         }
447
448         kpasswd_socket->kdc = kdc;
449         kpasswd_socket->send_queue = NULL;
450         kpasswd_socket->process = kpasswdd_process;
451
452         talloc_steal(kpasswd_socket, kpasswd_socket->sock);
453
454         kpasswd_socket->fde = event_add_fd(kdc->task->event_ctx, kdc, 
455                                            socket_get_fd(kpasswd_socket->sock), EVENT_FD_READ,
456                                            kdc_socket_handler, kpasswd_socket);
457         
458         status = socket_listen(kpasswd_socket->sock, address, kpasswd_port, 0, 0);
459         if (!NT_STATUS_IS_OK(status)) {
460                 DEBUG(0,("Failed to bind to %s:%d UDP for kpasswd - %s\n", 
461                          address, kpasswd_port, nt_errstr(status)));
462                 talloc_free(kpasswd_socket);
463                 return status;
464         }
465
466         /* within the kdc task we want to be a single process, so
467            ask for the single process model ops and pass these to the
468            stream_setup_socket() call. */
469         model_ops = process_model_byname("single");
470         if (!model_ops) {
471                 DEBUG(0,("Can't find 'single' process model_ops\n"));
472                 talloc_free(kdc_socket);
473                 return NT_STATUS_INTERNAL_ERROR;
474         }
475
476         status = stream_setup_socket(kdc->task->event_ctx, model_ops, 
477                                      &kdc_tcp_stream_ops, 
478                                      "ip", address, &kdc_port, kdc);
479         if (!NT_STATUS_IS_OK(status)) {
480                 DEBUG(0,("Failed to bind to %s:%u TCP - %s\n",
481                          address, kdc_port, nt_errstr(status)));
482                 talloc_free(kdc_socket);
483                 return status;
484         }
485
486         status = stream_setup_socket(kdc->task->event_ctx, model_ops, 
487                                      &kpasswdd_tcp_stream_ops, 
488                                      "ip", address, &kpasswd_port, kdc);
489         if (!NT_STATUS_IS_OK(status)) {
490                 DEBUG(0,("Failed to bind to %s:%u TCP - %s\n",
491                          address, kpasswd_port, nt_errstr(status)));
492                 talloc_free(kdc_socket);
493                 return status;
494         }
495
496         return NT_STATUS_OK;
497 }
498
499
500 /*
501   setup our listening sockets on the configured network interfaces
502 */
503 static NTSTATUS kdc_startup_interfaces(struct kdc_server *kdc)
504 {
505         int num_interfaces = iface_count();
506         TALLOC_CTX *tmp_ctx = talloc_new(kdc);
507         NTSTATUS status;
508
509         /* if we are allowing incoming packets from any address, then
510            we need to bind to the wildcard address */
511         if (!lp_bind_interfaces_only()) {
512                 status = kdc_add_socket(kdc, "0.0.0.0");
513                 NT_STATUS_NOT_OK_RETURN(status);
514         } else {
515                 int i;
516
517                 for (i=0; i<num_interfaces; i++) {
518                         const char *address = talloc_strdup(tmp_ctx, iface_n_ip(i));
519                         status = kdc_add_socket(kdc, address);
520                         NT_STATUS_NOT_OK_RETURN(status);
521                 }
522         }
523
524         talloc_free(tmp_ctx);
525
526         return NT_STATUS_OK;
527 }
528
529 /*
530   startup the kdc task
531 */
532 static void kdc_task_init(struct task_server *task)
533 {
534         struct kdc_server *kdc;
535         NTSTATUS status;
536         krb5_error_code ret;
537
538         if (iface_count() == 0) {
539                 task_server_terminate(task, "kdc: no network interfaces configured");
540                 return;
541         }
542
543         kdc = talloc(task, struct kdc_server);
544         if (kdc == NULL) {
545                 task_server_terminate(task, "kdc: out of memory");
546                 return;
547         }
548
549         kdc->task = task;
550
551         /* Setup the KDC configuration */
552         kdc->config = talloc(kdc, krb5_kdc_configuration);
553         if (!kdc->config) {
554                 task_server_terminate(task, "kdc: out of memory");
555                 return;
556         }
557         krb5_kdc_default_config(kdc->config);
558
559         /* NAT and the like make this pointless, and painful */
560         kdc->config->check_ticket_addresses = FALSE;
561
562         initialize_krb5_error_table();
563
564         ret = smb_krb5_init_context(kdc, &kdc->smb_krb5_context);
565         if (ret) {
566                 DEBUG(1,("kdc_task_init: krb5_init_context failed (%s)\n", 
567                          error_message(ret)));
568                 task_server_terminate(task, "kdc: krb5_init_context failed");
569                 return; 
570         }
571
572         krb5_add_et_list(kdc->smb_krb5_context->krb5_context, initialize_hdb_error_table_r);
573
574         kdc->config->logf = kdc->smb_krb5_context->logf;
575         kdc->config->db = talloc(kdc->config, struct HDB *);
576         if (!kdc->config->db) {
577                 task_server_terminate(task, "kdc: out of memory");
578                 return;
579         }
580         kdc->config->num_db = 1;
581                 
582         ret = hdb_ldb_create(kdc, kdc->smb_krb5_context->krb5_context, 
583                              &kdc->config->db[0], NULL);
584         if (ret != 0) {
585                 DEBUG(1, ("kdc_task_init: hdb_ldb_create fails: %s\n", 
586                           smb_get_krb5_error_message(kdc->smb_krb5_context->krb5_context, ret, kdc))); 
587                 task_server_terminate(task, "kdc: hdb_ldb_create failed");
588                 return; 
589         }
590
591         /* start listening on the configured network interfaces */
592         status = kdc_startup_interfaces(kdc);
593         if (!NT_STATUS_IS_OK(status)) {
594                 task_server_terminate(task, "kdc failed to setup interfaces");
595                 return;
596         }
597
598         irpc_add_name(task->msg_ctx, "kdc_server");
599 }
600
601
602 /*
603   called on startup of the KDC service 
604 */
605 static NTSTATUS kdc_init(struct event_context *event_ctx, 
606                          const struct model_ops *model_ops)
607 {       
608         return task_server_startup(event_ctx, model_ops, kdc_task_init);
609 }
610
611 /* called at smbd startup - register ourselves as a server service */
612 NTSTATUS server_service_kdc_init(void)
613 {
614         return register_server_service("kdc", kdc_init);
615 }