r12696: Reduce the size of include/structs.h
[ira/wip.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 *peer_address, int peer_port,
49                                  const char *my_address, int my_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         const char *my_addr;
120         int my_port;
121         int ret;
122
123         status = socket_pending(kdc_socket->sock, &dsize);
124         if (!NT_STATUS_IS_OK(status)) {
125                 talloc_free(tmp_ctx);
126                 return;
127         }
128
129         blob = data_blob_talloc(kdc_socket, NULL, dsize);
130         if (blob.data == NULL) {
131                 /* hope this is a temporary low memory condition */
132                 talloc_free(tmp_ctx);
133                 return;
134         }
135
136         status = socket_recvfrom(kdc_socket->sock, blob.data, blob.length, &nread, 0,
137                                  &src_addr, &src_port);
138         if (!NT_STATUS_IS_OK(status)) {
139                 talloc_free(tmp_ctx);
140                 return;
141         }
142         talloc_steal(tmp_ctx, src_addr);
143         blob.length = nread;
144         
145         DEBUG(10,("Received krb5 UDP packet of length %lu from %s:%u\n", 
146                  (long)blob.length, src_addr, (uint16_t)src_port));
147         
148         my_addr = socket_get_my_addr(kdc_socket->sock, tmp_ctx);
149         if (!my_addr) {
150                 talloc_free(tmp_ctx);
151                 return;
152         }
153         my_port = socket_get_my_port(kdc_socket->sock);
154
155
156         /* Call krb5 */
157         ret = kdc_socket->process(kdc_socket->kdc, 
158                                   tmp_ctx, 
159                                   &blob,  
160                                   &reply,
161                                   src_addr, src_port,
162                                   my_addr, my_port);
163         if (!ret) {
164                 talloc_free(tmp_ctx);
165                 return;
166         }
167
168         /* queue a pending reply */
169         rep = talloc(kdc_socket, struct kdc_reply);
170         if (rep == NULL) {
171                 talloc_free(tmp_ctx);
172                 return;
173         }
174         rep->dest_address = talloc_steal(rep, src_addr);
175         rep->dest_port    = src_port;
176         rep->packet       = reply;
177         talloc_steal(rep, reply.data);
178
179         if (rep->packet.data == NULL) {
180                 talloc_free(rep);
181                 talloc_free(tmp_ctx);
182                 return;
183         }
184
185         DLIST_ADD_END(kdc_socket->send_queue, rep, struct kdc_reply *);
186         EVENT_FD_WRITEABLE(kdc_socket->fde);
187         talloc_free(tmp_ctx);
188 }
189
190 /*
191   handle fd events on a KDC socket
192 */
193 static void kdc_socket_handler(struct event_context *ev, struct fd_event *fde,
194                                uint16_t flags, void *private)
195 {
196         struct kdc_socket *kdc_socket = talloc_get_type(private, struct kdc_socket);
197         if (flags & EVENT_FD_WRITE) {
198                 kdc_send_handler(kdc_socket);
199         } 
200         if (flags & EVENT_FD_READ) {
201                 kdc_recv_handler(kdc_socket);
202         }
203 }
204
205 static void kdc_tcp_terminate_connection(struct kdc_tcp_connection *kdcconn, const char *reason)
206 {
207         stream_terminate_connection(kdcconn->conn, reason);
208 }
209
210 /*
211   receive a full packet on a KDC connection
212 */
213 static NTSTATUS kdc_tcp_recv(void *private, DATA_BLOB blob)
214 {
215         struct kdc_tcp_connection *kdcconn = talloc_get_type(private, 
216                                                              struct kdc_tcp_connection);
217         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
218         TALLOC_CTX *tmp_ctx = talloc_new(kdcconn);
219         int ret;
220         DATA_BLOB input, reply;
221         const char *src_addr;
222         int src_port;
223         const char *my_addr;
224         int my_port;
225
226         talloc_steal(tmp_ctx, blob.data);
227
228         src_addr = socket_get_peer_addr(kdcconn->conn->socket, tmp_ctx);
229         if (!src_addr) {
230                 talloc_free(tmp_ctx);
231                 return NT_STATUS_NO_MEMORY;
232         }
233         src_port = socket_get_my_port(kdcconn->conn->socket);
234
235         my_addr = socket_get_my_addr(kdcconn->conn->socket, tmp_ctx);
236         if (!my_addr) {
237                 talloc_free(tmp_ctx);
238                 return NT_STATUS_NO_MEMORY;
239         }
240         my_port = socket_get_my_port(kdcconn->conn->socket);
241
242         /* Call krb5 */
243         input = data_blob_const(blob.data + 4, blob.length - 4); 
244
245         ret = kdcconn->process(kdcconn->kdc, 
246                                tmp_ctx,
247                                &input,
248                                &reply,
249                                src_addr, src_port,
250                                my_addr, my_port);
251         if (!ret) {
252                 talloc_free(tmp_ctx);
253                 return NT_STATUS_INTERNAL_ERROR;
254         }
255
256         /* and now encode the reply */
257         blob = data_blob_talloc(kdcconn, NULL, reply.length + 4);
258         if (!blob.data) {
259                 talloc_free(tmp_ctx);
260                 return NT_STATUS_NO_MEMORY;
261         }
262
263         RSIVAL(blob.data, 0, reply.length);
264         memcpy(blob.data + 4, reply.data, reply.length);        
265
266         status = packet_send(kdcconn->packet, blob);
267         if (!NT_STATUS_IS_OK(status)) {
268                 talloc_free(tmp_ctx);
269                 return status;
270         }
271
272         /* the call isn't needed any more */
273         talloc_free(tmp_ctx);
274         return NT_STATUS_OK;
275 }
276
277 /*
278   receive some data on a KDC connection
279 */
280 static void kdc_tcp_recv_handler(struct stream_connection *conn, uint16_t flags)
281 {
282         struct kdc_tcp_connection *kdcconn = talloc_get_type(conn->private, 
283                                                              struct kdc_tcp_connection);
284         packet_recv(kdcconn->packet);
285 }
286
287 /*
288   called on a tcp recv error
289 */
290 static void kdc_tcp_recv_error(void *private, NTSTATUS status)
291 {
292         struct kdc_tcp_connection *kdcconn = talloc_get_type(private, struct kdc_tcp_connection);
293         kdc_tcp_terminate_connection(kdcconn, nt_errstr(status));
294 }
295
296 /*
297   called when we can write to a connection
298 */
299 static void kdc_tcp_send(struct stream_connection *conn, uint16_t flags)
300 {
301         struct kdc_tcp_connection *kdcconn = talloc_get_type(conn->private, 
302                                                              struct kdc_tcp_connection);
303         packet_queue_run(kdcconn->packet);
304 }
305
306 /**
307    Wrapper for krb5_kdc_process_krb5_request, converting to/from Samba
308    calling conventions
309 */
310
311 static BOOL kdc_process(struct kdc_server *kdc,
312                         TALLOC_CTX *mem_ctx, 
313                         DATA_BLOB *input, 
314                         DATA_BLOB *reply,
315                         const char *peer_addr,
316                         int peer_port,
317                         const char *my_addr,
318                         int my_port)
319 {
320         int ret;        
321         krb5_data k5_reply;
322         struct ipv4_addr addr;
323         struct sockaddr_in peer_sock_addr;
324
325         /* TODO:  This really should be in a utility function somewhere */
326         ZERO_STRUCT(peer_sock_addr);
327 #ifdef HAVE_SOCK_SIN_LEN
328         peer_sock_addr.sin_len          = sizeof(peer_sock_addr);
329 #endif
330         addr                            = interpret_addr2(peer_addr);
331         peer_sock_addr.sin_addr.s_addr  = addr.addr;
332         peer_sock_addr.sin_port         = htons(peer_port);
333         peer_sock_addr.sin_family       = PF_INET;
334
335         DEBUG(10,("Received KDC packet of length %lu from %s\n", 
336                  (long)input->length - 4, peer_addr));
337
338         ret = krb5_kdc_process_krb5_request(kdc->smb_krb5_context->krb5_context, 
339                                             kdc->config,
340                                             input->data, input->length,
341                                             &k5_reply,
342                                             peer_addr,
343                                             (struct sockaddr *)&peer_sock_addr);
344         if (ret == -1) {
345                 *reply = data_blob(NULL, 0);
346                 return False;
347         }
348         *reply = data_blob_talloc(mem_ctx, k5_reply.data, k5_reply.length);
349         krb5_data_free(&k5_reply);
350         return True;
351 }
352
353 /*
354   called when we get a new connection
355 */
356 static void kdc_tcp_generic_accept(struct stream_connection *conn, kdc_process_fn_t process_fn)
357 {
358         struct kdc_server *kdc = talloc_get_type(conn->private, struct kdc_server);
359         struct kdc_tcp_connection *kdcconn;
360
361         kdcconn = talloc_zero(conn, struct kdc_tcp_connection);
362         if (!kdcconn) {
363                 stream_terminate_connection(conn, "kdc_tcp_accept: out of memory");
364                 return;
365         }
366         kdcconn->conn    = conn;
367         kdcconn->kdc     = kdc;
368         kdcconn->process = process_fn;
369         conn->private    = kdcconn;
370
371         kdcconn->packet = packet_init(kdcconn);
372         if (kdcconn->packet == NULL) {
373                 kdc_tcp_terminate_connection(kdcconn, "kdc_tcp_accept: out of memory");
374                 return;
375         }
376         packet_set_private(kdcconn->packet, kdcconn);
377         packet_set_socket(kdcconn->packet, conn->socket);
378         packet_set_callback(kdcconn->packet, kdc_tcp_recv);
379         packet_set_full_request(kdcconn->packet, packet_full_request_u32);
380         packet_set_error_handler(kdcconn->packet, kdc_tcp_recv_error);
381         packet_set_event_context(kdcconn->packet, conn->event.ctx);
382         packet_set_fde(kdcconn->packet, conn->event.fde);
383         packet_set_serialise(kdcconn->packet);
384 }
385
386 static void kdc_tcp_accept(struct stream_connection *conn)
387 {
388         kdc_tcp_generic_accept(conn, kdc_process);
389 }
390
391 static const struct stream_server_ops kdc_tcp_stream_ops = {
392         .name                   = "kdc_tcp",
393         .accept_connection      = kdc_tcp_accept,
394         .recv_handler           = kdc_tcp_recv_handler,
395         .send_handler           = kdc_tcp_send
396 };
397
398 static void kpasswdd_tcp_accept(struct stream_connection *conn)
399 {
400         kdc_tcp_generic_accept(conn, kpasswdd_process);
401 }
402
403 static const struct stream_server_ops kpasswdd_tcp_stream_ops = {
404         .name                   = "kpasswdd_tcp",
405         .accept_connection      = kpasswdd_tcp_accept,
406         .recv_handler           = kdc_tcp_recv_handler,
407         .send_handler           = kdc_tcp_send
408 };
409
410 /*
411   start listening on the given address
412 */
413 static NTSTATUS kdc_add_socket(struct kdc_server *kdc, const char *address)
414 {
415         const struct model_ops *model_ops;
416         struct kdc_socket *kdc_socket;
417         struct kdc_socket *kpasswd_socket;
418         NTSTATUS status;
419         uint16_t kdc_port = lp_krb5_port();
420         uint16_t kpasswd_port = lp_kpasswd_port();
421
422         kdc_socket = talloc(kdc, struct kdc_socket);
423         NT_STATUS_HAVE_NO_MEMORY(kdc_socket);
424
425         kpasswd_socket = talloc(kdc, struct kdc_socket);
426         NT_STATUS_HAVE_NO_MEMORY(kpasswd_socket);
427
428         status = socket_create("ip", SOCKET_TYPE_DGRAM, &kdc_socket->sock, 0);
429         if (!NT_STATUS_IS_OK(status)) {
430                 talloc_free(kdc_socket);
431                 return status;
432         }
433
434         status = socket_create("ip", SOCKET_TYPE_DGRAM, &kpasswd_socket->sock, 0);
435         if (!NT_STATUS_IS_OK(status)) {
436                 talloc_free(kpasswd_socket);
437                 return status;
438         }
439
440         kdc_socket->kdc = kdc;
441         kdc_socket->send_queue = NULL;
442         kdc_socket->process = kdc_process;
443
444         talloc_steal(kdc_socket, kdc_socket->sock);
445
446         kdc_socket->fde = event_add_fd(kdc->task->event_ctx, kdc, 
447                                        socket_get_fd(kdc_socket->sock), EVENT_FD_READ,
448                                        kdc_socket_handler, kdc_socket);
449
450         status = socket_listen(kdc_socket->sock, address, kdc_port, 0, 0);
451         if (!NT_STATUS_IS_OK(status)) {
452                 DEBUG(0,("Failed to bind to %s:%d UDP for kdc - %s\n", 
453                          address, kdc_port, nt_errstr(status)));
454                 talloc_free(kdc_socket);
455                 return status;
456         }
457
458         kpasswd_socket->kdc = kdc;
459         kpasswd_socket->send_queue = NULL;
460         kpasswd_socket->process = kpasswdd_process;
461
462         talloc_steal(kpasswd_socket, kpasswd_socket->sock);
463
464         kpasswd_socket->fde = event_add_fd(kdc->task->event_ctx, kdc, 
465                                            socket_get_fd(kpasswd_socket->sock), EVENT_FD_READ,
466                                            kdc_socket_handler, kpasswd_socket);
467         
468         status = socket_listen(kpasswd_socket->sock, address, kpasswd_port, 0, 0);
469         if (!NT_STATUS_IS_OK(status)) {
470                 DEBUG(0,("Failed to bind to %s:%d UDP for kpasswd - %s\n", 
471                          address, kpasswd_port, nt_errstr(status)));
472                 talloc_free(kpasswd_socket);
473                 return status;
474         }
475
476         /* within the kdc task we want to be a single process, so
477            ask for the single process model ops and pass these to the
478            stream_setup_socket() call. */
479         model_ops = process_model_byname("single");
480         if (!model_ops) {
481                 DEBUG(0,("Can't find 'single' process model_ops\n"));
482                 talloc_free(kdc_socket);
483                 return NT_STATUS_INTERNAL_ERROR;
484         }
485
486         status = stream_setup_socket(kdc->task->event_ctx, model_ops, 
487                                      &kdc_tcp_stream_ops, 
488                                      "ip", address, &kdc_port, kdc);
489         if (!NT_STATUS_IS_OK(status)) {
490                 DEBUG(0,("Failed to bind to %s:%u TCP - %s\n",
491                          address, kdc_port, nt_errstr(status)));
492                 talloc_free(kdc_socket);
493                 return status;
494         }
495
496         status = stream_setup_socket(kdc->task->event_ctx, model_ops, 
497                                      &kpasswdd_tcp_stream_ops, 
498                                      "ip", address, &kpasswd_port, kdc);
499         if (!NT_STATUS_IS_OK(status)) {
500                 DEBUG(0,("Failed to bind to %s:%u TCP - %s\n",
501                          address, kpasswd_port, nt_errstr(status)));
502                 talloc_free(kdc_socket);
503                 return status;
504         }
505
506         return NT_STATUS_OK;
507 }
508
509
510 /*
511   setup our listening sockets on the configured network interfaces
512 */
513 static NTSTATUS kdc_startup_interfaces(struct kdc_server *kdc)
514 {
515         int num_interfaces = iface_count();
516         TALLOC_CTX *tmp_ctx = talloc_new(kdc);
517         NTSTATUS status;
518
519         /* if we are allowing incoming packets from any address, then
520            we need to bind to the wildcard address */
521         if (!lp_bind_interfaces_only()) {
522                 status = kdc_add_socket(kdc, "0.0.0.0");
523                 NT_STATUS_NOT_OK_RETURN(status);
524         } else {
525                 int i;
526
527                 for (i=0; i<num_interfaces; i++) {
528                         const char *address = talloc_strdup(tmp_ctx, iface_n_ip(i));
529                         status = kdc_add_socket(kdc, address);
530                         NT_STATUS_NOT_OK_RETURN(status);
531                 }
532         }
533
534         talloc_free(tmp_ctx);
535
536         return NT_STATUS_OK;
537 }
538
539 /*
540   startup the kdc task
541 */
542 static void kdc_task_init(struct task_server *task)
543 {
544         struct kdc_server *kdc;
545         NTSTATUS status;
546         krb5_error_code ret;
547
548         if (iface_count() == 0) {
549                 task_server_terminate(task, "kdc: no network interfaces configured");
550                 return;
551         }
552
553         kdc = talloc(task, struct kdc_server);
554         if (kdc == NULL) {
555                 task_server_terminate(task, "kdc: out of memory");
556                 return;
557         }
558
559         kdc->task = task;
560
561         /* Setup the KDC configuration */
562         kdc->config = talloc(kdc, krb5_kdc_configuration);
563         if (!kdc->config) {
564                 task_server_terminate(task, "kdc: out of memory");
565                 return;
566         }
567         krb5_kdc_default_config(kdc->config);
568
569         initialize_krb5_error_table();
570
571         ret = smb_krb5_init_context(kdc, &kdc->smb_krb5_context);
572         if (ret) {
573                 DEBUG(1,("kdc_task_init: krb5_init_context failed (%s)\n", 
574                          error_message(ret)));
575                 task_server_terminate(task, "kdc: krb5_init_context failed");
576                 return; 
577         }
578
579         krb5_add_et_list(kdc->smb_krb5_context->krb5_context, initialize_hdb_error_table_r);
580
581         kdc->config->logf = kdc->smb_krb5_context->logf;
582         kdc->config->db = talloc(kdc->config, struct HDB *);
583         if (!kdc->config->db) {
584                 task_server_terminate(task, "kdc: out of memory");
585                 return;
586         }
587         kdc->config->num_db = 1;
588                 
589         status = hdb_ldb_create(kdc, kdc->smb_krb5_context->krb5_context, 
590                                    &kdc->config->db[0], NULL);
591         if (!NT_STATUS_IS_OK(status)) {
592                 task_server_terminate(task, "kdc: hdb_ldb_create (setup KDC database) failed");
593                 return; 
594         }
595
596         /* start listening on the configured network interfaces */
597         status = kdc_startup_interfaces(kdc);
598         if (!NT_STATUS_IS_OK(status)) {
599                 task_server_terminate(task, "kdc failed to setup interfaces");
600                 return;
601         }
602
603         irpc_add_name(task->msg_ctx, "kdc_server");
604 }
605
606
607 /*
608   called on startup of the KDC service 
609 */
610 static NTSTATUS kdc_init(struct event_context *event_ctx, 
611                          const struct model_ops *model_ops)
612 {       
613         return task_server_startup(event_ctx, model_ops, kdc_task_init);
614 }
615
616 /* called at smbd startup - register ourselves as a server service */
617 NTSTATUS server_service_kdc_init(void)
618 {
619         return register_server_service("kdc", kdc_init);
620 }