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