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