r22969: fix some more places where we could end up with more than one event
[sfrench/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    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.h"
28 #include "smbd/service_stream.h"
29 #include "smbd/process_model.h"
30 #include "lib/events/events.h"
31 #include "lib/socket/socket.h"
32 #include "kdc/kdc.h"
33 #include "system/network.h"
34 #include "lib/util/dlinklist.h"
35 #include "lib/messaging/irpc.h"
36 #include "lib/stream/packet.h"
37 #include "librpc/gen_ndr/samr.h"
38 #include "lib/socket/netif.h"
39 #include "heimdal/kdc/windc_plugin.h"
40 #include "heimdal/lib/krb5/krb5_locl.h"
41 #include "heimdal/kdc/kdc_locl.h"
42
43
44 /* Disgusting hack to get a mem_ctx into the hdb plugin, when used as a keytab */
45 TALLOC_CTX *kdc_mem_ctx;
46
47 /* hold all the info needed to send a reply */
48 struct kdc_reply {
49         struct kdc_reply *next, *prev;
50         struct socket_address *dest;
51         DATA_BLOB packet;
52 };
53
54 typedef BOOL (*kdc_process_fn_t)(struct kdc_server *kdc,
55                                  TALLOC_CTX *mem_ctx, 
56                                  DATA_BLOB *input, 
57                                  DATA_BLOB *reply,
58                                  struct socket_address *peer_addr, 
59                                  struct socket_address *my_addr, 
60                                  int datagram);
61
62 /* hold information about one kdc socket */
63 struct kdc_socket {
64         struct socket_context *sock;
65         struct kdc_server *kdc;
66         struct fd_event *fde;
67
68         /* a queue of outgoing replies that have been deferred */
69         struct kdc_reply *send_queue;
70
71         kdc_process_fn_t process;
72 };
73 /*
74   state of an open tcp connection
75 */
76 struct kdc_tcp_connection {
77         /* stream connection we belong to */
78         struct stream_connection *conn;
79
80         /* the kdc_server the connection belongs to */
81         struct kdc_server *kdc;
82
83         struct packet_context *packet;
84
85         kdc_process_fn_t process;
86 };
87
88 /*
89   handle fd send events on a KDC socket
90 */
91 static void kdc_send_handler(struct kdc_socket *kdc_socket)
92 {
93         while (kdc_socket->send_queue) {
94                 struct kdc_reply *rep = kdc_socket->send_queue;
95                 NTSTATUS status;
96                 size_t sendlen;
97
98                 status = socket_sendto(kdc_socket->sock, &rep->packet, &sendlen,
99                                        rep->dest);
100                 if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
101                         break;
102                 }
103                 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_BUFFER_SIZE)) {
104                         /* Replace with a krb err, response to big */
105                 }
106                 
107                 DLIST_REMOVE(kdc_socket->send_queue, rep);
108                 talloc_free(rep);
109         }
110
111         if (kdc_socket->send_queue == NULL) {
112                 EVENT_FD_NOT_WRITEABLE(kdc_socket->fde);
113         }
114 }
115
116
117 /*
118   handle fd recv events on a KDC socket
119 */
120 static void kdc_recv_handler(struct kdc_socket *kdc_socket)
121 {
122         NTSTATUS status;
123         TALLOC_CTX *tmp_ctx = talloc_new(kdc_socket);
124         DATA_BLOB blob;
125         struct kdc_reply *rep;
126         DATA_BLOB reply;
127         size_t nread, dsize;
128         struct socket_address *src;
129         struct socket_address *my_addr;
130         int ret;
131
132         status = socket_pending(kdc_socket->sock, &dsize);
133         if (!NT_STATUS_IS_OK(status)) {
134                 talloc_free(tmp_ctx);
135                 return;
136         }
137
138         blob = data_blob_talloc(tmp_ctx, NULL, dsize);
139         if (blob.data == NULL) {
140                 /* hope this is a temporary low memory condition */
141                 talloc_free(tmp_ctx);
142                 return;
143         }
144
145         status = socket_recvfrom(kdc_socket->sock, blob.data, blob.length, &nread,
146                                  tmp_ctx, &src);
147         if (!NT_STATUS_IS_OK(status)) {
148                 talloc_free(tmp_ctx);
149                 return;
150         }
151         blob.length = nread;
152         
153         DEBUG(10,("Received krb5 UDP packet of length %lu from %s:%u\n", 
154                  (long)blob.length, src->addr, (uint16_t)src->port));
155         
156         my_addr = socket_get_my_addr(kdc_socket->sock, tmp_ctx);
157         if (!my_addr) {
158                 talloc_free(tmp_ctx);
159                 return;
160         }
161
162
163         /* Call krb5 */
164         ret = kdc_socket->process(kdc_socket->kdc, 
165                                   tmp_ctx, 
166                                   &blob,  
167                                   &reply,
168                                   src, my_addr,
169                                   1 /* Datagram */);
170         if (!ret) {
171                 talloc_free(tmp_ctx);
172                 return;
173         }
174
175         /* queue a pending reply */
176         rep = talloc(kdc_socket, struct kdc_reply);
177         if (rep == NULL) {
178                 talloc_free(tmp_ctx);
179                 return;
180         }
181         rep->dest         = talloc_steal(rep, src);
182         rep->packet       = reply;
183         talloc_steal(rep, reply.data);
184
185         if (rep->packet.data == NULL) {
186                 talloc_free(rep);
187                 talloc_free(tmp_ctx);
188                 return;
189         }
190
191         DLIST_ADD_END(kdc_socket->send_queue, rep, struct kdc_reply *);
192         EVENT_FD_WRITEABLE(kdc_socket->fde);
193         talloc_free(tmp_ctx);
194 }
195
196 /*
197   handle fd events on a KDC socket
198 */
199 static void kdc_socket_handler(struct event_context *ev, struct fd_event *fde,
200                                uint16_t flags, void *private)
201 {
202         struct kdc_socket *kdc_socket = talloc_get_type(private, struct kdc_socket);
203         if (flags & EVENT_FD_WRITE) {
204                 kdc_send_handler(kdc_socket);
205         } 
206         if (flags & EVENT_FD_READ) {
207                 kdc_recv_handler(kdc_socket);
208         }
209 }
210
211 static void kdc_tcp_terminate_connection(struct kdc_tcp_connection *kdcconn, const char *reason)
212 {
213         stream_terminate_connection(kdcconn->conn, reason);
214 }
215
216 /*
217   receive a full packet on a KDC connection
218 */
219 static NTSTATUS kdc_tcp_recv(void *private, DATA_BLOB blob)
220 {
221         struct kdc_tcp_connection *kdcconn = talloc_get_type(private, 
222                                                              struct kdc_tcp_connection);
223         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
224         TALLOC_CTX *tmp_ctx = talloc_new(kdcconn);
225         int ret;
226         DATA_BLOB input, reply;
227         struct socket_address *src_addr;
228         struct socket_address *my_addr;
229
230         talloc_steal(tmp_ctx, blob.data);
231
232         src_addr = socket_get_peer_addr(kdcconn->conn->socket, tmp_ctx);
233         if (!src_addr) {
234                 talloc_free(tmp_ctx);
235                 return NT_STATUS_NO_MEMORY;
236         }
237
238         my_addr = socket_get_my_addr(kdcconn->conn->socket, tmp_ctx);
239         if (!my_addr) {
240                 talloc_free(tmp_ctx);
241                 return NT_STATUS_NO_MEMORY;
242         }
243
244         /* Call krb5 */
245         input = data_blob_const(blob.data + 4, blob.length - 4); 
246
247         ret = kdcconn->process(kdcconn->kdc, 
248                                tmp_ctx,
249                                &input,
250                                &reply,
251                                src_addr,
252                                my_addr,
253                                0 /* Not datagram */);
254         if (!ret) {
255                 talloc_free(tmp_ctx);
256                 return NT_STATUS_INTERNAL_ERROR;
257         }
258
259         /* and now encode the reply */
260         blob = data_blob_talloc(kdcconn, NULL, reply.length + 4);
261         if (!blob.data) {
262                 talloc_free(tmp_ctx);
263                 return NT_STATUS_NO_MEMORY;
264         }
265
266         RSIVAL(blob.data, 0, reply.length);
267         memcpy(blob.data + 4, reply.data, reply.length);        
268
269         status = packet_send(kdcconn->packet, blob);
270         if (!NT_STATUS_IS_OK(status)) {
271                 talloc_free(tmp_ctx);
272                 return status;
273         }
274
275         /* the call isn't needed any more */
276         talloc_free(tmp_ctx);
277         return NT_STATUS_OK;
278 }
279
280 /*
281   receive some data on a KDC connection
282 */
283 static void kdc_tcp_recv_handler(struct stream_connection *conn, uint16_t flags)
284 {
285         struct kdc_tcp_connection *kdcconn = talloc_get_type(conn->private, 
286                                                              struct kdc_tcp_connection);
287         packet_recv(kdcconn->packet);
288 }
289
290 /*
291   called on a tcp recv error
292 */
293 static void kdc_tcp_recv_error(void *private, NTSTATUS status)
294 {
295         struct kdc_tcp_connection *kdcconn = talloc_get_type(private, struct kdc_tcp_connection);
296         kdc_tcp_terminate_connection(kdcconn, nt_errstr(status));
297 }
298
299 /*
300   called when we can write to a connection
301 */
302 static void kdc_tcp_send(struct stream_connection *conn, uint16_t flags)
303 {
304         struct kdc_tcp_connection *kdcconn = talloc_get_type(conn->private, 
305                                                              struct kdc_tcp_connection);
306         packet_queue_run(kdcconn->packet);
307 }
308
309 /**
310    Wrapper for krb5_kdc_process_krb5_request, converting to/from Samba
311    calling conventions
312 */
313
314 static BOOL kdc_process(struct kdc_server *kdc,
315                         TALLOC_CTX *mem_ctx, 
316                         DATA_BLOB *input, 
317                         DATA_BLOB *reply,
318                         struct socket_address *peer_addr, 
319                         struct socket_address *my_addr,
320                         int datagram_reply)
321 {
322         int ret;        
323         krb5_data k5_reply;
324         krb5_data_zero(&k5_reply);
325
326         DEBUG(10,("Received KDC packet of length %lu from %s:%d\n", 
327                   (long)input->length - 4, peer_addr->addr, peer_addr->port));
328
329         ret = krb5_kdc_process_krb5_request(kdc->smb_krb5_context->krb5_context, 
330                                             kdc->config,
331                                             input->data, input->length,
332                                             &k5_reply,
333                                             peer_addr->addr,
334                                             peer_addr->sockaddr,
335                                             datagram_reply);
336         if (ret == -1) {
337                 *reply = data_blob(NULL, 0);
338                 return False;
339         }
340         if (k5_reply.length) {
341                 *reply = data_blob_talloc(mem_ctx, k5_reply.data, k5_reply.length);
342                 krb5_free_data_contents(kdc->smb_krb5_context->krb5_context, &k5_reply);
343         } else {
344                 *reply = data_blob(NULL, 0);    
345         }
346         return True;
347 }
348
349 /*
350   called when we get a new connection
351 */
352 static void kdc_tcp_generic_accept(struct stream_connection *conn, kdc_process_fn_t process_fn)
353 {
354         struct kdc_server *kdc = talloc_get_type(conn->private, struct kdc_server);
355         struct kdc_tcp_connection *kdcconn;
356
357         kdcconn = talloc_zero(conn, struct kdc_tcp_connection);
358         if (!kdcconn) {
359                 stream_terminate_connection(conn, "kdc_tcp_accept: out of memory");
360                 return;
361         }
362         kdcconn->conn    = conn;
363         kdcconn->kdc     = kdc;
364         kdcconn->process = process_fn;
365         conn->private    = kdcconn;
366
367         kdcconn->packet = packet_init(kdcconn);
368         if (kdcconn->packet == NULL) {
369                 kdc_tcp_terminate_connection(kdcconn, "kdc_tcp_accept: out of memory");
370                 return;
371         }
372         packet_set_private(kdcconn->packet, kdcconn);
373         packet_set_socket(kdcconn->packet, conn->socket);
374         packet_set_callback(kdcconn->packet, kdc_tcp_recv);
375         packet_set_full_request(kdcconn->packet, packet_full_request_u32);
376         packet_set_error_handler(kdcconn->packet, kdc_tcp_recv_error);
377         packet_set_event_context(kdcconn->packet, conn->event.ctx);
378         packet_set_fde(kdcconn->packet, conn->event.fde);
379         packet_set_serialise(kdcconn->packet);
380 }
381
382 static void kdc_tcp_accept(struct stream_connection *conn)
383 {
384         kdc_tcp_generic_accept(conn, kdc_process);
385 }
386
387 static const struct stream_server_ops kdc_tcp_stream_ops = {
388         .name                   = "kdc_tcp",
389         .accept_connection      = kdc_tcp_accept,
390         .recv_handler           = kdc_tcp_recv_handler,
391         .send_handler           = kdc_tcp_send
392 };
393
394 static void kpasswdd_tcp_accept(struct stream_connection *conn)
395 {
396         kdc_tcp_generic_accept(conn, kpasswdd_process);
397 }
398
399 static const struct stream_server_ops kpasswdd_tcp_stream_ops = {
400         .name                   = "kpasswdd_tcp",
401         .accept_connection      = kpasswdd_tcp_accept,
402         .recv_handler           = kdc_tcp_recv_handler,
403         .send_handler           = kdc_tcp_send
404 };
405
406 /*
407   start listening on the given address
408 */
409 static NTSTATUS kdc_add_socket(struct kdc_server *kdc, const char *address)
410 {
411         const struct model_ops *model_ops;
412         struct kdc_socket *kdc_socket;
413         struct kdc_socket *kpasswd_socket;
414         struct socket_address *kdc_address, *kpasswd_address;
415         NTSTATUS status;
416         uint16_t kdc_port = lp_krb5_port();
417         uint16_t kpasswd_port = lp_kpasswd_port();
418
419         kdc_socket = talloc(kdc, struct kdc_socket);
420         NT_STATUS_HAVE_NO_MEMORY(kdc_socket);
421
422         kpasswd_socket = talloc(kdc, struct kdc_socket);
423         NT_STATUS_HAVE_NO_MEMORY(kpasswd_socket);
424
425         status = socket_create("ip", SOCKET_TYPE_DGRAM, &kdc_socket->sock, 0);
426         if (!NT_STATUS_IS_OK(status)) {
427                 talloc_free(kdc_socket);
428                 return status;
429         }
430
431         status = socket_create("ip", SOCKET_TYPE_DGRAM, &kpasswd_socket->sock, 0);
432         if (!NT_STATUS_IS_OK(status)) {
433                 talloc_free(kpasswd_socket);
434                 return status;
435         }
436
437         kdc_socket->kdc = kdc;
438         kdc_socket->send_queue = NULL;
439         kdc_socket->process = kdc_process;
440
441         talloc_steal(kdc_socket, kdc_socket->sock);
442
443         kdc_socket->fde = event_add_fd(kdc->task->event_ctx, kdc, 
444                                        socket_get_fd(kdc_socket->sock), EVENT_FD_READ,
445                                        kdc_socket_handler, kdc_socket);
446
447         kdc_address = socket_address_from_strings(kdc_socket, kdc_socket->sock->backend_name, 
448                                                   address, kdc_port);
449         NT_STATUS_HAVE_NO_MEMORY(kdc_address);
450
451         status = socket_listen(kdc_socket->sock, kdc_address, 0, 0);
452         if (!NT_STATUS_IS_OK(status)) {
453                 DEBUG(0,("Failed to bind to %s:%d UDP for kdc - %s\n", 
454                          address, kdc_port, nt_errstr(status)));
455                 talloc_free(kdc_socket);
456                 return status;
457         }
458
459         kpasswd_socket->kdc = kdc;
460         kpasswd_socket->send_queue = NULL;
461         kpasswd_socket->process = kpasswdd_process;
462
463         talloc_steal(kpasswd_socket, kpasswd_socket->sock);
464
465         kpasswd_socket->fde = event_add_fd(kdc->task->event_ctx, kdc, 
466                                            socket_get_fd(kpasswd_socket->sock), EVENT_FD_READ,
467                                            kdc_socket_handler, kpasswd_socket);
468         
469         kpasswd_address = socket_address_from_strings(kpasswd_socket, kpasswd_socket->sock->backend_name, 
470                                                       address, kpasswd_port);
471         NT_STATUS_HAVE_NO_MEMORY(kpasswd_address);
472
473         status = socket_listen(kpasswd_socket->sock, kpasswd_address, 0, 0);
474         if (!NT_STATUS_IS_OK(status)) {
475                 DEBUG(0,("Failed to bind to %s:%d UDP for kpasswd - %s\n", 
476                          address, kpasswd_port, nt_errstr(status)));
477                 talloc_free(kpasswd_socket);
478                 return status;
479         }
480
481         /* within the kdc task we want to be a single process, so
482            ask for the single process model ops and pass these to the
483            stream_setup_socket() call. */
484         model_ops = process_model_byname("single");
485         if (!model_ops) {
486                 DEBUG(0,("Can't find 'single' process model_ops\n"));
487                 talloc_free(kdc_socket);
488                 return NT_STATUS_INTERNAL_ERROR;
489         }
490
491         status = stream_setup_socket(kdc->task->event_ctx, model_ops, 
492                                      &kdc_tcp_stream_ops, 
493                                      "ip", address, &kdc_port, kdc);
494         if (!NT_STATUS_IS_OK(status)) {
495                 DEBUG(0,("Failed to bind to %s:%u TCP - %s\n",
496                          address, kdc_port, nt_errstr(status)));
497                 talloc_free(kdc_socket);
498                 return status;
499         }
500
501         status = stream_setup_socket(kdc->task->event_ctx, model_ops, 
502                                      &kpasswdd_tcp_stream_ops, 
503                                      "ip", address, &kpasswd_port, kdc);
504         if (!NT_STATUS_IS_OK(status)) {
505                 DEBUG(0,("Failed to bind to %s:%u TCP - %s\n",
506                          address, kpasswd_port, nt_errstr(status)));
507                 talloc_free(kdc_socket);
508                 return status;
509         }
510
511         return NT_STATUS_OK;
512 }
513
514
515 /*
516   setup our listening sockets on the configured network interfaces
517 */
518 static NTSTATUS kdc_startup_interfaces(struct kdc_server *kdc)
519 {
520         int num_interfaces = iface_count();
521         TALLOC_CTX *tmp_ctx = talloc_new(kdc);
522         NTSTATUS status;
523         
524         int i;
525         
526         for (i=0; i<num_interfaces; i++) {
527                 const char *address = talloc_strdup(tmp_ctx, iface_n_ip(i));
528                 status = kdc_add_socket(kdc, address);
529                 NT_STATUS_NOT_OK_RETURN(status);
530         }
531
532         talloc_free(tmp_ctx);
533
534         return NT_STATUS_OK;
535 }
536
537 static struct krb5plugin_windc_ftable windc_plugin_table = {
538         .minor_version = KRB5_WINDC_PLUGING_MINOR,
539         .init = samba_kdc_plugin_init,
540         .fini = samba_kdc_plugin_fini,
541         .pac_generate = samba_kdc_get_pac,
542         .pac_verify = samba_kdc_reget_pac,
543         .client_access = samba_kdc_check_client_access,
544 };
545
546
547 /*
548   startup the kdc task
549 */
550 static void kdc_task_init(struct task_server *task)
551 {
552         struct kdc_server *kdc;
553         NTSTATUS status;
554         krb5_error_code ret;
555
556         switch (lp_server_role()) {
557         case ROLE_STANDALONE:
558                 task_server_terminate(task, "kdc: no KDC required in standalone configuration");
559                 return;
560         case ROLE_DOMAIN_MEMBER:
561                 task_server_terminate(task, "kdc: no KDC required in member server configuration");
562                 return;
563         case ROLE_DOMAIN_CONTROLLER:
564                 /* Yes, we want a KDC */
565                 break;
566         }
567
568         if (iface_count() == 0) {
569                 task_server_terminate(task, "kdc: no network interfaces configured");
570                 return;
571         }
572
573         task_server_set_title(task, "task[kdc]");
574
575         kdc = talloc(task, struct kdc_server);
576         if (kdc == NULL) {
577                 task_server_terminate(task, "kdc: out of memory");
578                 return;
579         }
580
581         kdc->task = task;
582
583         initialize_krb5_error_table();
584
585         ret = smb_krb5_init_context(kdc, task->event_ctx, &kdc->smb_krb5_context);
586         if (ret) {
587                 DEBUG(1,("kdc_task_init: krb5_init_context failed (%s)\n", 
588                          error_message(ret)));
589                 task_server_terminate(task, "kdc: krb5_init_context failed");
590                 return; 
591         }
592
593         krb5_add_et_list(kdc->smb_krb5_context->krb5_context, initialize_hdb_error_table_r);
594
595         /* Registar WinDC hooks */
596         ret = _krb5_plugin_register(kdc->smb_krb5_context->krb5_context, 
597                                     PLUGIN_TYPE_DATA, "windc",
598                                     &windc_plugin_table);
599         if(ret) {
600                 task_server_terminate(task, "kdc: failed to register hdb keytab");
601                 return;
602         }
603
604         /* Setup the KDC configuration */
605         kdc->config = talloc(kdc, krb5_kdc_configuration);
606         if (!kdc->config) {
607                 task_server_terminate(task, "kdc: out of memory");
608                 return;
609         }
610         krb5_kdc_default_config(kdc->config);
611
612         kdc->config->logf = kdc->smb_krb5_context->logf;
613         kdc->config->db = talloc(kdc->config, struct HDB *);
614         if (!kdc->config->db) {
615                 task_server_terminate(task, "kdc: out of memory");
616                 return;
617         }
618         kdc->config->num_db = 1;
619                 
620         status = kdc_hdb_ldb_create(kdc, kdc->smb_krb5_context->krb5_context, 
621                                     &kdc->config->db[0], NULL);
622         if (!NT_STATUS_IS_OK(status)) {
623                 task_server_terminate(task, "kdc: hdb_ldb_create (setup KDC database) failed");
624                 return; 
625         }
626
627         ret = krb5_kt_register(kdc->smb_krb5_context->krb5_context, &hdb_kt_ops);
628         if(ret) {
629                 task_server_terminate(task, "kdc: failed to register hdb keytab");
630                 return;
631         }
632
633         krb5_kdc_configure(kdc->smb_krb5_context->krb5_context, kdc->config);
634
635         kdc_mem_ctx = kdc->smb_krb5_context;
636
637         /* start listening on the configured network interfaces */
638         status = kdc_startup_interfaces(kdc);
639         if (!NT_STATUS_IS_OK(status)) {
640                 task_server_terminate(task, "kdc failed to setup interfaces");
641                 return;
642         }
643
644         irpc_add_name(task->msg_ctx, "kdc_server");
645 }
646
647
648 /*
649   called on startup of the KDC service 
650 */
651 static NTSTATUS kdc_init(struct event_context *event_ctx, 
652                          const struct model_ops *model_ops)
653 {       
654         return task_server_startup(event_ctx, model_ops, kdc_task_init);
655 }
656
657 /* called at smbd startup - register ourselves as a server service */
658 NTSTATUS server_service_kdc_init(void)
659 {
660         return register_server_service("kdc", kdc_init);
661 }