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