030eb23c10d8d8f33775a679fc8356e7c001d2c1
[kai/samba.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-2008
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 3 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, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "includes.h"
25 #include "smbd/service_task.h"
26 #include "smbd/service.h"
27 #include "smbd/service_stream.h"
28 #include "smbd/process_model.h"
29 #include "lib/events/events.h"
30 #include "lib/socket/socket.h"
31 #include "system/network.h"
32 #include "lib/util/dlinklist.h"
33 #include "lib/messaging/irpc.h"
34 #include "lib/stream/packet.h"
35 #include "librpc/gen_ndr/samr.h"
36 #include "librpc/gen_ndr/ndr_irpc.h"
37 #include "librpc/gen_ndr/ndr_krb5pac.h"
38 #include "lib/socket/netif.h"
39 #include "param/param.h"
40 #include "kdc/kdc.h"
41 #include "librpc/gen_ndr/ndr_misc.h"
42
43
44 /* Disgusting hack to get a mem_ctx and lp_ctx into the hdb plugin, when 
45  * used as a keytab */
46 TALLOC_CTX *kdc_mem_ctx;
47 struct loadparm_context *kdc_lp_ctx;
48
49 /* hold all the info needed to send a reply */
50 struct kdc_reply {
51         struct kdc_reply *next, *prev;
52         struct socket_address *dest;
53         DATA_BLOB packet;
54 };
55
56 typedef bool (*kdc_process_fn_t)(struct kdc_server *kdc,
57                                  TALLOC_CTX *mem_ctx, 
58                                  DATA_BLOB *input, 
59                                  DATA_BLOB *reply,
60                                  struct socket_address *peer_addr, 
61                                  struct socket_address *my_addr, 
62                                  int datagram);
63
64 /* hold information about one kdc socket */
65 struct kdc_socket {
66         struct socket_context *sock;
67         struct kdc_server *kdc;
68         struct fd_event *fde;
69
70         /* a queue of outgoing replies that have been deferred */
71         struct kdc_reply *send_queue;
72
73         kdc_process_fn_t process;
74 };
75 /*
76   state of an open tcp connection
77 */
78 struct kdc_tcp_connection {
79         /* stream connection we belong to */
80         struct stream_connection *conn;
81
82         /* the kdc_server the connection belongs to */
83         struct kdc_server *kdc;
84
85         struct packet_context *packet;
86
87         kdc_process_fn_t process;
88 };
89
90 /*
91   handle fd send events on a KDC socket
92 */
93 static void kdc_send_handler(struct kdc_socket *kdc_socket)
94 {
95         while (kdc_socket->send_queue) {
96                 struct kdc_reply *rep = kdc_socket->send_queue;
97                 NTSTATUS status;
98                 size_t sendlen;
99
100                 status = socket_sendto(kdc_socket->sock, &rep->packet, &sendlen,
101                                        rep->dest);
102                 if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
103                         break;
104                 }
105                 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_BUFFER_SIZE)) {
106                         /* Replace with a krb err, response to big */
107                 }
108                 
109                 DLIST_REMOVE(kdc_socket->send_queue, rep);
110                 talloc_free(rep);
111         }
112
113         if (kdc_socket->send_queue == NULL) {
114                 EVENT_FD_NOT_WRITEABLE(kdc_socket->fde);
115         }
116 }
117
118
119 /*
120   handle fd recv events on a KDC socket
121 */
122 static void kdc_recv_handler(struct kdc_socket *kdc_socket)
123 {
124         NTSTATUS status;
125         TALLOC_CTX *tmp_ctx = talloc_new(kdc_socket);
126         DATA_BLOB blob;
127         struct kdc_reply *rep;
128         DATA_BLOB reply;
129         size_t nread, dsize;
130         struct socket_address *src;
131         struct socket_address *my_addr;
132         int ret;
133
134         status = socket_pending(kdc_socket->sock, &dsize);
135         if (!NT_STATUS_IS_OK(status)) {
136                 talloc_free(tmp_ctx);
137                 return;
138         }
139
140         blob = data_blob_talloc(tmp_ctx, NULL, dsize);
141         if (blob.data == NULL) {
142                 /* hope this is a temporary low memory condition */
143                 talloc_free(tmp_ctx);
144                 return;
145         }
146
147         status = socket_recvfrom(kdc_socket->sock, blob.data, blob.length, &nread,
148                                  tmp_ctx, &src);
149         if (!NT_STATUS_IS_OK(status)) {
150                 talloc_free(tmp_ctx);
151                 return;
152         }
153         blob.length = nread;
154         
155         DEBUG(10,("Received krb5 UDP packet of length %lu from %s:%u\n", 
156                  (long)blob.length, src->addr, (uint16_t)src->port));
157         
158         my_addr = socket_get_my_addr(kdc_socket->sock, tmp_ctx);
159         if (!my_addr) {
160                 talloc_free(tmp_ctx);
161                 return;
162         }
163
164
165         /* Call krb5 */
166         ret = kdc_socket->process(kdc_socket->kdc, 
167                                   tmp_ctx, 
168                                   &blob,  
169                                   &reply,
170                                   src, my_addr,
171                                   1 /* Datagram */);
172         if (!ret) {
173                 talloc_free(tmp_ctx);
174                 return;
175         }
176
177         /* queue a pending reply */
178         rep = talloc(kdc_socket, struct kdc_reply);
179         if (rep == NULL) {
180                 talloc_free(tmp_ctx);
181                 return;
182         }
183         rep->dest         = talloc_steal(rep, src);
184         rep->packet       = reply;
185         talloc_steal(rep, reply.data);
186
187         if (rep->packet.data == NULL) {
188                 talloc_free(rep);
189                 talloc_free(tmp_ctx);
190                 return;
191         }
192
193         DLIST_ADD_END(kdc_socket->send_queue, rep, struct kdc_reply *);
194         EVENT_FD_WRITEABLE(kdc_socket->fde);
195         talloc_free(tmp_ctx);
196 }
197
198 /*
199   handle fd events on a KDC socket
200 */
201 static void kdc_socket_handler(struct event_context *ev, struct fd_event *fde,
202                                uint16_t flags, void *private)
203 {
204         struct kdc_socket *kdc_socket = talloc_get_type(private, struct kdc_socket);
205         if (flags & EVENT_FD_WRITE) {
206                 kdc_send_handler(kdc_socket);
207         } 
208         if (flags & EVENT_FD_READ) {
209                 kdc_recv_handler(kdc_socket);
210         }
211 }
212
213 static void kdc_tcp_terminate_connection(struct kdc_tcp_connection *kdcconn, const char *reason)
214 {
215         stream_terminate_connection(kdcconn->conn, reason);
216 }
217
218 /*
219   receive a full packet on a KDC connection
220 */
221 static NTSTATUS kdc_tcp_recv(void *private, DATA_BLOB blob)
222 {
223         struct kdc_tcp_connection *kdcconn = talloc_get_type(private, 
224                                                              struct kdc_tcp_connection);
225         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
226         TALLOC_CTX *tmp_ctx = talloc_new(kdcconn);
227         int ret;
228         DATA_BLOB input, reply;
229         struct socket_address *src_addr;
230         struct socket_address *my_addr;
231
232         talloc_steal(tmp_ctx, blob.data);
233
234         src_addr = socket_get_peer_addr(kdcconn->conn->socket, tmp_ctx);
235         if (!src_addr) {
236                 talloc_free(tmp_ctx);
237                 return NT_STATUS_NO_MEMORY;
238         }
239
240         my_addr = socket_get_my_addr(kdcconn->conn->socket, tmp_ctx);
241         if (!my_addr) {
242                 talloc_free(tmp_ctx);
243                 return NT_STATUS_NO_MEMORY;
244         }
245
246         /* Call krb5 */
247         input = data_blob_const(blob.data + 4, blob.length - 4); 
248
249         ret = kdcconn->process(kdcconn->kdc, 
250                                tmp_ctx,
251                                &input,
252                                &reply,
253                                src_addr,
254                                my_addr,
255                                0 /* Not datagram */);
256         if (!ret) {
257                 talloc_free(tmp_ctx);
258                 return NT_STATUS_INTERNAL_ERROR;
259         }
260
261         /* and now encode the reply */
262         blob = data_blob_talloc(kdcconn, NULL, reply.length + 4);
263         if (!blob.data) {
264                 talloc_free(tmp_ctx);
265                 return NT_STATUS_NO_MEMORY;
266         }
267
268         RSIVAL(blob.data, 0, reply.length);
269         memcpy(blob.data + 4, reply.data, reply.length);        
270
271         status = packet_send(kdcconn->packet, blob);
272         if (!NT_STATUS_IS_OK(status)) {
273                 talloc_free(tmp_ctx);
274                 return status;
275         }
276
277         /* the call isn't needed any more */
278         talloc_free(tmp_ctx);
279         return NT_STATUS_OK;
280 }
281
282 /*
283   receive some data on a KDC connection
284 */
285 static void kdc_tcp_recv_handler(struct stream_connection *conn, uint16_t flags)
286 {
287         struct kdc_tcp_connection *kdcconn = talloc_get_type(conn->private, 
288                                                              struct kdc_tcp_connection);
289         packet_recv(kdcconn->packet);
290 }
291
292 /*
293   called on a tcp recv error
294 */
295 static void kdc_tcp_recv_error(void *private, NTSTATUS status)
296 {
297         struct kdc_tcp_connection *kdcconn = talloc_get_type(private, struct kdc_tcp_connection);
298         kdc_tcp_terminate_connection(kdcconn, nt_errstr(status));
299 }
300
301 /*
302   called when we can write to a connection
303 */
304 static void kdc_tcp_send(struct stream_connection *conn, uint16_t flags)
305 {
306         struct kdc_tcp_connection *kdcconn = talloc_get_type(conn->private, 
307                                                              struct kdc_tcp_connection);
308         packet_queue_run(kdcconn->packet);
309 }
310
311 /**
312    Wrapper for krb5_kdc_process_krb5_request, converting to/from Samba
313    calling conventions
314 */
315
316 static bool kdc_process(struct kdc_server *kdc,
317                         TALLOC_CTX *mem_ctx, 
318                         DATA_BLOB *input, 
319                         DATA_BLOB *reply,
320                         struct socket_address *peer_addr, 
321                         struct socket_address *my_addr,
322                         int datagram_reply)
323 {
324         int ret;        
325         krb5_data k5_reply;
326         krb5_data_zero(&k5_reply);
327
328         krb5_kdc_update_time(NULL);
329
330         DEBUG(10,("Received KDC packet of length %lu from %s:%d\n", 
331                   (long)input->length - 4, peer_addr->addr, peer_addr->port));
332
333         ret = krb5_kdc_process_krb5_request(kdc->smb_krb5_context->krb5_context, 
334                                             kdc->config,
335                                             input->data, input->length,
336                                             &k5_reply,
337                                             peer_addr->addr,
338                                             peer_addr->sockaddr,
339                                             datagram_reply);
340         if (ret == -1) {
341                 *reply = data_blob(NULL, 0);
342                 return false;
343         }
344         if (k5_reply.length) {
345                 *reply = data_blob_talloc(mem_ctx, k5_reply.data, k5_reply.length);
346                 krb5_free_data_contents(kdc->smb_krb5_context->krb5_context, &k5_reply);
347         } else {
348                 *reply = data_blob(NULL, 0);    
349         }
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                                uint16_t kdc_port, uint16_t kpasswd_port)
415 {
416         const struct model_ops *model_ops;
417         struct kdc_socket *kdc_socket;
418         struct kdc_socket *kpasswd_socket;
419         struct socket_address *kdc_address, *kpasswd_address;
420         NTSTATUS status;
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         kdc_address = socket_address_from_strings(kdc_socket, kdc_socket->sock->backend_name, 
451                                                   address, kdc_port);
452         NT_STATUS_HAVE_NO_MEMORY(kdc_address);
453
454         status = socket_listen(kdc_socket->sock, kdc_address, 0, 0);
455         if (!NT_STATUS_IS_OK(status)) {
456                 DEBUG(0,("Failed to bind to %s:%d UDP for kdc - %s\n", 
457                          address, kdc_port, nt_errstr(status)));
458                 talloc_free(kdc_socket);
459                 return status;
460         }
461
462         kpasswd_socket->kdc = kdc;
463         kpasswd_socket->send_queue = NULL;
464         kpasswd_socket->process = kpasswdd_process;
465
466         talloc_steal(kpasswd_socket, kpasswd_socket->sock);
467
468         kpasswd_socket->fde = event_add_fd(kdc->task->event_ctx, kdc, 
469                                            socket_get_fd(kpasswd_socket->sock), EVENT_FD_READ,
470                                            kdc_socket_handler, kpasswd_socket);
471         
472         kpasswd_address = socket_address_from_strings(kpasswd_socket, kpasswd_socket->sock->backend_name, 
473                                                       address, kpasswd_port);
474         NT_STATUS_HAVE_NO_MEMORY(kpasswd_address);
475
476         status = socket_listen(kpasswd_socket->sock, kpasswd_address, 0, 0);
477         if (!NT_STATUS_IS_OK(status)) {
478                 DEBUG(0,("Failed to bind to %s:%d UDP for kpasswd - %s\n", 
479                          address, kpasswd_port, nt_errstr(status)));
480                 talloc_free(kpasswd_socket);
481                 return status;
482         }
483
484         /* within the kdc task we want to be a single process, so
485            ask for the single process model ops and pass these to the
486            stream_setup_socket() call. */
487         model_ops = process_model_startup(kdc->task->event_ctx, "single");
488         if (!model_ops) {
489                 DEBUG(0,("Can't find 'single' process model_ops\n"));
490                 talloc_free(kdc_socket);
491                 return NT_STATUS_INTERNAL_ERROR;
492         }
493
494         status = stream_setup_socket(kdc->task->event_ctx, 
495                                      kdc->task->lp_ctx,
496                                      model_ops, 
497                                      &kdc_tcp_stream_ops, 
498                                      "ip", address, &kdc_port, 
499                                      lp_socket_options(kdc->task->lp_ctx), 
500                                      kdc);
501         if (!NT_STATUS_IS_OK(status)) {
502                 DEBUG(0,("Failed to bind to %s:%u TCP - %s\n",
503                          address, kdc_port, nt_errstr(status)));
504                 talloc_free(kdc_socket);
505                 return status;
506         }
507
508         status = stream_setup_socket(kdc->task->event_ctx, 
509                                      kdc->task->lp_ctx,
510                                      model_ops, 
511                                      &kpasswdd_tcp_stream_ops, 
512                                      "ip", address, &kpasswd_port, 
513                                      lp_socket_options(kdc->task->lp_ctx), 
514                                      kdc);
515         if (!NT_STATUS_IS_OK(status)) {
516                 DEBUG(0,("Failed to bind to %s:%u TCP - %s\n",
517                          address, kpasswd_port, nt_errstr(status)));
518                 talloc_free(kdc_socket);
519                 return status;
520         }
521
522         return NT_STATUS_OK;
523 }
524
525
526 /*
527   setup our listening sockets on the configured network interfaces
528 */
529 static NTSTATUS kdc_startup_interfaces(struct kdc_server *kdc, struct loadparm_context *lp_ctx,
530                                        struct interface *ifaces)
531 {
532         int num_interfaces;
533         TALLOC_CTX *tmp_ctx = talloc_new(kdc);
534         NTSTATUS status;
535         int i;
536
537         num_interfaces = iface_count(ifaces);
538         
539         for (i=0; i<num_interfaces; i++) {
540                 const char *address = talloc_strdup(tmp_ctx, iface_n_ip(ifaces, i));
541                 status = kdc_add_socket(kdc, address, lp_krb5_port(lp_ctx), 
542                                         lp_kpasswd_port(lp_ctx));
543                 NT_STATUS_NOT_OK_RETURN(status);
544         }
545
546         talloc_free(tmp_ctx);
547
548         return NT_STATUS_OK;
549 }
550
551 static struct krb5plugin_windc_ftable windc_plugin_table = {
552         .minor_version = KRB5_WINDC_PLUGING_MINOR,
553         .init = samba_kdc_plugin_init,
554         .fini = samba_kdc_plugin_fini,
555         .pac_generate = samba_kdc_get_pac,
556         .pac_verify = samba_kdc_reget_pac,
557         .client_access = samba_kdc_check_client_access,
558 };
559
560
561 static NTSTATUS kdc_check_generic_kerberos(struct irpc_message *msg, 
562                                  struct kdc_check_generic_kerberos *r)
563 {
564         struct PAC_Validate pac_validate;
565         DATA_BLOB srv_sig;
566         struct PAC_SIGNATURE_DATA kdc_sig;
567         struct kdc_server *kdc = talloc_get_type(msg->private, struct kdc_server);
568         enum ndr_err_code ndr_err;
569         krb5_enctype etype;
570         int ret;
571         hdb_entry_ex ent;
572         krb5_principal principal;
573         krb5_keyblock keyblock;
574         Key *key;
575
576         /* There is no reply to this request */
577         r->out.generic_reply = data_blob(NULL, 0);
578
579         ndr_err = ndr_pull_struct_blob(&r->in.generic_request, msg, 
580                                        lp_iconv_convenience(kdc->task->lp_ctx), 
581                                        &pac_validate,
582                                        (ndr_pull_flags_fn_t)ndr_pull_PAC_Validate);
583         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
584                 return NT_STATUS_INVALID_PARAMETER;
585         }
586         
587         if (pac_validate.MessageType != 3) {
588                 /* We don't implement any other message types - such as certificate validation - yet */
589                 return NT_STATUS_INVALID_PARAMETER;
590         }
591
592         if (pac_validate.ChecksumAndSignature.length != (pac_validate.ChecksumLength + pac_validate.SignatureLength)
593             || pac_validate.ChecksumAndSignature.length < pac_validate.ChecksumLength
594             || pac_validate.ChecksumAndSignature.length < pac_validate.SignatureLength ) {
595                 return NT_STATUS_INVALID_PARAMETER;
596         }
597         
598         srv_sig = data_blob_const(pac_validate.ChecksumAndSignature.data, 
599                                   pac_validate.ChecksumLength);
600         
601         if (pac_validate.SignatureType == CKSUMTYPE_HMAC_MD5) {
602                 etype = ETYPE_ARCFOUR_HMAC_MD5;
603         } else {
604                 ret = krb5_cksumtype_to_enctype(kdc->smb_krb5_context->krb5_context, pac_validate.SignatureType,
605                                                 &etype);
606                 if (ret != 0) {
607                         return NT_STATUS_LOGON_FAILURE;
608                 }
609         }
610
611         ret = krb5_make_principal(kdc->smb_krb5_context->krb5_context, &principal, 
612                                   lp_realm(kdc->task->lp_ctx), 
613                                   "krbtgt", lp_realm(kdc->task->lp_ctx), 
614                                   NULL);
615
616         if (ret != 0) {
617                 return NT_STATUS_NO_MEMORY;
618         }
619
620         ret = kdc->config->db[0]->hdb_fetch(kdc->smb_krb5_context->krb5_context, 
621                                             kdc->config->db[0],
622                                             principal,
623                                             HDB_F_GET_KRBTGT | HDB_F_DECRYPT,
624                                             &ent);
625
626         if (ret != 0) {
627                 hdb_free_entry(kdc->smb_krb5_context->krb5_context, &ent);
628                 krb5_free_principal(kdc->smb_krb5_context->krb5_context, principal);
629         
630                 return NT_STATUS_LOGON_FAILURE;
631         }
632         
633         ret = hdb_enctype2key(kdc->smb_krb5_context->krb5_context, &ent.entry, etype, &key);
634
635         if (ret != 0) {
636                 hdb_free_entry(kdc->smb_krb5_context->krb5_context, &ent);
637                 krb5_free_principal(kdc->smb_krb5_context->krb5_context, principal);
638                 return NT_STATUS_LOGON_FAILURE;
639         }
640
641         keyblock = key->key;
642         
643         kdc_sig.type = pac_validate.SignatureType;
644         kdc_sig.signature = data_blob_const(&pac_validate.ChecksumAndSignature.data[pac_validate.ChecksumLength],
645                                             pac_validate.SignatureLength);
646         ret = check_pac_checksum(msg, srv_sig, &kdc_sig, 
647                            kdc->smb_krb5_context->krb5_context, &keyblock);
648
649         hdb_free_entry(kdc->smb_krb5_context->krb5_context, &ent);
650         krb5_free_principal(kdc->smb_krb5_context->krb5_context, principal);
651
652         if (ret != 0) {
653                 return NT_STATUS_LOGON_FAILURE;
654         }
655         
656         return NT_STATUS_OK;
657 }
658
659
660
661 /*
662   startup the kdc task
663 */
664 static void kdc_task_init(struct task_server *task)
665 {
666         struct kdc_server *kdc;
667         NTSTATUS status;
668         krb5_error_code ret;
669         struct interface *ifaces;
670
671         switch (lp_server_role(task->lp_ctx)) {
672         case ROLE_STANDALONE:
673                 task_server_terminate(task, "kdc: no KDC required in standalone configuration");
674                 return;
675         case ROLE_DOMAIN_MEMBER:
676                 task_server_terminate(task, "kdc: no KDC required in member server configuration");
677                 return;
678         case ROLE_DOMAIN_CONTROLLER:
679                 /* Yes, we want a KDC */
680                 break;
681         }
682
683         load_interfaces(task, lp_interfaces(task->lp_ctx), &ifaces);
684
685         if (iface_count(ifaces) == 0) {
686                 task_server_terminate(task, "kdc: no network interfaces configured");
687                 return;
688         }
689
690         task_server_set_title(task, "task[kdc]");
691
692         kdc = talloc(task, struct kdc_server);
693         if (kdc == NULL) {
694                 task_server_terminate(task, "kdc: out of memory");
695                 return;
696         }
697
698         kdc->task = task;
699
700         initialize_krb5_error_table();
701
702         ret = smb_krb5_init_context(kdc, task->event_ctx, task->lp_ctx, &kdc->smb_krb5_context);
703         if (ret) {
704                 DEBUG(1,("kdc_task_init: krb5_init_context failed (%s)\n", 
705                          error_message(ret)));
706                 task_server_terminate(task, "kdc: krb5_init_context failed");
707                 return; 
708         }
709
710         krb5_add_et_list(kdc->smb_krb5_context->krb5_context, initialize_hdb_error_table_r);
711
712         ret = krb5_kdc_get_config(kdc->smb_krb5_context->krb5_context, 
713                                   &kdc->config);
714         if(ret) {
715                 task_server_terminate(task, "kdc: failed to get KDC configuration");
716                 return;
717         }
718
719         kdc->config->logf = kdc->smb_krb5_context->logf;
720         kdc->config->db = talloc(kdc, struct HDB *);
721         if (!kdc->config->db) {
722                 task_server_terminate(task, "kdc: out of memory");
723                 return;
724         }
725         kdc->config->num_db = 1;
726                 
727         status = kdc_hdb_ldb_create(kdc, task->event_ctx, task->lp_ctx, 
728                                     kdc->smb_krb5_context->krb5_context, 
729                                     &kdc->config->db[0], NULL);
730         if (!NT_STATUS_IS_OK(status)) {
731                 task_server_terminate(task, "kdc: hdb_ldb_create (setup KDC database) failed");
732                 return; 
733         }
734
735         ret = krb5_kt_register(kdc->smb_krb5_context->krb5_context, &hdb_kt_ops);
736         if(ret) {
737                 task_server_terminate(task, "kdc: failed to register hdb keytab");
738                 return;
739         }
740
741         /* Registar WinDC hooks */
742         ret = krb5_plugin_register(kdc->smb_krb5_context->krb5_context, 
743                                    PLUGIN_TYPE_DATA, "windc",
744                                    &windc_plugin_table);
745         if(ret) {
746                 task_server_terminate(task, "kdc: failed to register hdb keytab");
747                 return;
748         }
749
750         krb5_kdc_windc_init(kdc->smb_krb5_context->krb5_context);
751
752         kdc_mem_ctx = kdc->smb_krb5_context;
753         kdc_lp_ctx = task->lp_ctx;
754
755         /* start listening on the configured network interfaces */
756         status = kdc_startup_interfaces(kdc, task->lp_ctx, ifaces);
757         if (!NT_STATUS_IS_OK(status)) {
758                 task_server_terminate(task, "kdc failed to setup interfaces");
759                 return;
760         }
761
762         status = IRPC_REGISTER(task->msg_ctx, irpc, KDC_CHECK_GENERIC_KERBEROS, 
763                                kdc_check_generic_kerberos, kdc);
764         if (!NT_STATUS_IS_OK(status)) {
765                 task_server_terminate(task, "nbtd failed to setup monitoring");
766                 return;
767         }
768
769         irpc_add_name(task->msg_ctx, "kdc_server");
770 }
771
772
773 /* called at smbd startup - register ourselves as a server service */
774 NTSTATUS server_service_kdc_init(void)
775 {
776         return register_server_service("kdc", kdc_task_init);
777 }