dbwrap: add dbwrap_parse_record_send/recv
[Samba.git] / source4 / kdc / kdc-heimdal.c
blob25bb3916038595c5c048e1849559388cd1f70469
1 /*
2 Unix SMB/CIFS implementation.
4 KDC Server startup
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005-2008
7 Copyright (C) Andrew Tridgell 2005
8 Copyright (C) Stefan Metzmacher 2005
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.
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.
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/>.
24 #include "includes.h"
25 #include "smbd/process_model.h"
26 #include "lib/tsocket/tsocket.h"
27 #include "lib/messaging/irpc.h"
28 #include "librpc/gen_ndr/ndr_irpc.h"
29 #include "librpc/gen_ndr/ndr_krb5pac.h"
30 #include "lib/socket/netif.h"
31 #include "param/param.h"
32 #include "kdc/kdc-server.h"
33 #include "kdc/kdc-proxy.h"
34 #include "kdc/kdc-glue.h"
35 #include "kdc/pac-glue.h"
36 #include "kdc/kpasswd-service.h"
37 #include "dsdb/samdb/samdb.h"
38 #include "auth/session.h"
39 #include "libds/common/roles.h"
40 #include <kdc.h>
41 #include <hdb.h>
43 NTSTATUS server_service_kdc_init(void);
45 extern struct krb5plugin_windc_ftable windc_plugin_table;
47 /**
48 Wrapper for krb5_kdc_process_krb5_request, converting to/from Samba
49 calling conventions
52 static kdc_code kdc_process(struct kdc_server *kdc,
53 TALLOC_CTX *mem_ctx,
54 DATA_BLOB *input,
55 DATA_BLOB *reply,
56 struct tsocket_address *peer_addr,
57 struct tsocket_address *my_addr,
58 int datagram_reply)
60 int ret;
61 char *pa;
62 struct sockaddr_storage ss;
63 krb5_data k5_reply;
64 krb5_kdc_configuration *kdc_config =
65 (krb5_kdc_configuration *)kdc->private_data;
67 krb5_data_zero(&k5_reply);
69 krb5_kdc_update_time(NULL);
71 ret = tsocket_address_bsd_sockaddr(peer_addr, (struct sockaddr *) &ss,
72 sizeof(struct sockaddr_storage));
73 if (ret < 0) {
74 return KDC_ERROR;
76 pa = tsocket_address_string(peer_addr, mem_ctx);
77 if (pa == NULL) {
78 return KDC_ERROR;
81 DEBUG(10,("Received KDC packet of length %lu from %s\n",
82 (long)input->length - 4, pa));
84 ret = krb5_kdc_process_krb5_request(kdc->smb_krb5_context->krb5_context,
85 kdc_config,
86 input->data, input->length,
87 &k5_reply,
88 pa,
89 (struct sockaddr *) &ss,
90 datagram_reply);
91 if (ret == -1) {
92 *reply = data_blob(NULL, 0);
93 return KDC_ERROR;
96 if (ret == HDB_ERR_NOT_FOUND_HERE) {
97 *reply = data_blob(NULL, 0);
98 return KDC_PROXY_REQUEST;
101 if (k5_reply.length) {
102 *reply = data_blob_talloc(mem_ctx, k5_reply.data, k5_reply.length);
103 krb5_data_free(&k5_reply);
104 } else {
105 *reply = data_blob(NULL, 0);
107 return KDC_OK;
111 setup our listening sockets on the configured network interfaces
113 static NTSTATUS kdc_startup_interfaces(struct kdc_server *kdc, struct loadparm_context *lp_ctx,
114 struct interface *ifaces)
116 const struct model_ops *model_ops;
117 int num_interfaces;
118 TALLOC_CTX *tmp_ctx = talloc_new(kdc);
119 NTSTATUS status;
120 int i;
121 uint16_t kdc_port = lpcfg_krb5_port(lp_ctx);
122 uint16_t kpasswd_port = lpcfg_kpasswd_port(lp_ctx);
123 bool done_wildcard = false;
125 /* within the kdc task we want to be a single process, so
126 ask for the single process model ops and pass these to the
127 stream_setup_socket() call. */
128 model_ops = process_model_startup("single");
129 if (!model_ops) {
130 DEBUG(0,("Can't find 'single' process model_ops\n"));
131 return NT_STATUS_INTERNAL_ERROR;
134 num_interfaces = iface_list_count(ifaces);
136 /* if we are allowing incoming packets from any address, then
137 we need to bind to the wildcard address */
138 if (!lpcfg_bind_interfaces_only(lp_ctx)) {
139 int num_binds = 0;
140 char **wcard = iface_list_wildcard(kdc);
141 NT_STATUS_HAVE_NO_MEMORY(wcard);
142 for (i=0; wcard[i]; i++) {
143 if (kdc_port) {
144 status = kdc_add_socket(kdc, model_ops,
145 "kdc", wcard[i], kdc_port,
146 kdc_process, false);
147 if (NT_STATUS_IS_OK(status)) {
148 num_binds++;
152 if (kpasswd_port) {
153 status = kdc_add_socket(kdc, model_ops,
154 "kpasswd", wcard[i], kpasswd_port,
155 kpasswd_process, false);
156 if (NT_STATUS_IS_OK(status)) {
157 num_binds++;
161 talloc_free(wcard);
162 if (num_binds == 0) {
163 return NT_STATUS_INVALID_PARAMETER_MIX;
165 done_wildcard = true;
168 for (i=0; i<num_interfaces; i++) {
169 const char *address = talloc_strdup(tmp_ctx, iface_list_n_ip(ifaces, i));
171 if (kdc_port) {
172 status = kdc_add_socket(kdc, model_ops,
173 "kdc", address, kdc_port,
174 kdc_process, done_wildcard);
175 NT_STATUS_NOT_OK_RETURN(status);
178 if (kpasswd_port) {
179 status = kdc_add_socket(kdc, model_ops,
180 "kpasswd", address, kpasswd_port,
181 kpasswd_process, done_wildcard);
182 NT_STATUS_NOT_OK_RETURN(status);
186 talloc_free(tmp_ctx);
188 return NT_STATUS_OK;
191 static NTSTATUS kdc_check_generic_kerberos(struct irpc_message *msg,
192 struct kdc_check_generic_kerberos *r)
194 struct PAC_Validate pac_validate;
195 DATA_BLOB srv_sig;
196 struct PAC_SIGNATURE_DATA kdc_sig;
197 struct kdc_server *kdc = talloc_get_type(msg->private_data, struct kdc_server);
198 krb5_kdc_configuration *kdc_config =
199 (krb5_kdc_configuration *)kdc->private_data;
200 enum ndr_err_code ndr_err;
201 int ret;
202 hdb_entry_ex ent;
203 krb5_principal principal;
206 /* There is no reply to this request */
207 r->out.generic_reply = data_blob(NULL, 0);
209 ndr_err = ndr_pull_struct_blob(&r->in.generic_request, msg, &pac_validate,
210 (ndr_pull_flags_fn_t)ndr_pull_PAC_Validate);
211 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
212 return NT_STATUS_INVALID_PARAMETER;
215 if (pac_validate.MessageType != NETLOGON_GENERIC_KRB5_PAC_VALIDATE) {
216 /* We don't implement any other message types - such as certificate validation - yet */
217 return NT_STATUS_INVALID_PARAMETER;
220 if (pac_validate.ChecksumAndSignature.length != (pac_validate.ChecksumLength + pac_validate.SignatureLength)
221 || pac_validate.ChecksumAndSignature.length < pac_validate.ChecksumLength
222 || pac_validate.ChecksumAndSignature.length < pac_validate.SignatureLength ) {
223 return NT_STATUS_INVALID_PARAMETER;
226 srv_sig = data_blob_const(pac_validate.ChecksumAndSignature.data,
227 pac_validate.ChecksumLength);
229 ret = krb5_make_principal(kdc->smb_krb5_context->krb5_context, &principal,
230 lpcfg_realm(kdc->task->lp_ctx),
231 "krbtgt", lpcfg_realm(kdc->task->lp_ctx),
232 NULL);
234 if (ret != 0) {
235 return NT_STATUS_NO_MEMORY;
238 ret = kdc_config->db[0]->hdb_fetch_kvno(kdc->smb_krb5_context->krb5_context,
239 kdc_config->db[0],
240 principal,
241 HDB_F_GET_KRBTGT | HDB_F_DECRYPT,
243 &ent);
245 if (ret != 0) {
246 hdb_free_entry(kdc->smb_krb5_context->krb5_context, &ent);
247 krb5_free_principal(kdc->smb_krb5_context->krb5_context, principal);
249 return NT_STATUS_LOGON_FAILURE;
252 kdc_sig.type = pac_validate.SignatureType;
253 kdc_sig.signature = data_blob_const(&pac_validate.ChecksumAndSignature.data[pac_validate.ChecksumLength],
254 pac_validate.SignatureLength);
256 ret = kdc_check_pac(kdc->smb_krb5_context->krb5_context, srv_sig, &kdc_sig, &ent);
258 hdb_free_entry(kdc->smb_krb5_context->krb5_context, &ent);
259 krb5_free_principal(kdc->smb_krb5_context->krb5_context, principal);
261 if (ret != 0) {
262 return NT_STATUS_LOGON_FAILURE;
265 return NT_STATUS_OK;
270 startup the kdc task
272 static void kdc_task_init(struct task_server *task)
274 struct kdc_server *kdc;
275 krb5_kdc_configuration *kdc_config = NULL;
276 NTSTATUS status;
277 krb5_error_code ret;
278 struct interface *ifaces;
279 int ldb_ret;
281 switch (lpcfg_server_role(task->lp_ctx)) {
282 case ROLE_STANDALONE:
283 task_server_terminate(task, "kdc: no KDC required in standalone configuration", false);
284 return;
285 case ROLE_DOMAIN_MEMBER:
286 task_server_terminate(task, "kdc: no KDC required in member server configuration", false);
287 return;
288 case ROLE_DOMAIN_PDC:
289 case ROLE_DOMAIN_BDC:
290 task_server_terminate(task, "Cannot start KDC as a 'classic Samba' DC", true);
291 return;
292 case ROLE_ACTIVE_DIRECTORY_DC:
293 /* Yes, we want a KDC */
294 break;
297 load_interface_list(task, task->lp_ctx, &ifaces);
299 if (iface_list_count(ifaces) == 0) {
300 task_server_terminate(task, "kdc: no network interfaces configured", false);
301 return;
304 task_server_set_title(task, "task[kdc]");
306 kdc = talloc_zero(task, struct kdc_server);
307 if (kdc == NULL) {
308 task_server_terminate(task, "kdc: out of memory", true);
309 return;
312 kdc->task = task;
315 /* get a samdb connection */
316 kdc->samdb = samdb_connect(kdc, kdc->task->event_ctx, kdc->task->lp_ctx,
317 system_session(kdc->task->lp_ctx), 0);
318 if (!kdc->samdb) {
319 DEBUG(1,("kdc_task_init: unable to connect to samdb\n"));
320 task_server_terminate(task, "kdc: krb5_init_context samdb connect failed", true);
321 return;
324 ldb_ret = samdb_rodc(kdc->samdb, &kdc->am_rodc);
325 if (ldb_ret != LDB_SUCCESS) {
326 DEBUG(1, ("kdc_task_init: Cannot determine if we are an RODC: %s\n",
327 ldb_errstring(kdc->samdb)));
328 task_server_terminate(task, "kdc: krb5_init_context samdb RODC connect failed", true);
329 return;
332 kdc->proxy_timeout = lpcfg_parm_int(kdc->task->lp_ctx, NULL, "kdc", "proxy timeout", 5);
334 initialize_krb5_error_table();
336 ret = smb_krb5_init_context(kdc, task->lp_ctx, &kdc->smb_krb5_context);
337 if (ret) {
338 DEBUG(1,("kdc_task_init: krb5_init_context failed (%s)\n",
339 error_message(ret)));
340 task_server_terminate(task, "kdc: krb5_init_context failed", true);
341 return;
344 krb5_add_et_list(kdc->smb_krb5_context->krb5_context, initialize_hdb_error_table_r);
346 ret = krb5_kdc_get_config(kdc->smb_krb5_context->krb5_context,
347 &kdc_config);
348 if(ret) {
349 task_server_terminate(task, "kdc: failed to get KDC configuration", true);
350 return;
353 kdc_config->logf = (krb5_log_facility *)kdc->smb_krb5_context->pvt_log_data;
354 kdc_config->db = talloc(kdc, struct HDB *);
355 if (!kdc_config->db) {
356 task_server_terminate(task, "kdc: out of memory", true);
357 return;
359 kdc_config->num_db = 1;
362 * This restores the behavior before
363 * commit 255e3e18e00f717d99f3bc57c8a8895ff624f3c3
364 * s4:heimdal: import lorikeet-heimdal-201107150856
365 * (commit 48936803fae4a2fb362c79365d31f420c917b85b)
367 * as_use_strongest_session_key,preauth_use_strongest_session_key
368 * and tgs_use_strongest_session_key are input to the
369 * _kdc_find_etype() function. The old bahavior is in
370 * the use_strongest_session_key=FALSE code path.
371 * (The only remaining difference in _kdc_find_etype()
372 * is the is_preauth parameter.)
374 * The old behavior in the _kdc_get_preferred_key()
375 * function is use_strongest_server_key=TRUE.
377 kdc_config->as_use_strongest_session_key = false;
378 kdc_config->preauth_use_strongest_session_key = false;
379 kdc_config->tgs_use_strongest_session_key = false;
380 kdc_config->use_strongest_server_key = true;
382 kdc_config->autodetect_referrals = false;
384 /* Register hdb-samba4 hooks for use as a keytab */
386 kdc->base_ctx = talloc_zero(kdc, struct samba_kdc_base_context);
387 if (!kdc->base_ctx) {
388 task_server_terminate(task, "kdc: out of memory", true);
389 return;
392 kdc->base_ctx->ev_ctx = task->event_ctx;
393 kdc->base_ctx->lp_ctx = task->lp_ctx;
394 kdc->base_ctx->msg_ctx = task->msg_ctx;
396 status = hdb_samba4_create_kdc(kdc->base_ctx,
397 kdc->smb_krb5_context->krb5_context,
398 &kdc_config->db[0]);
399 if (!NT_STATUS_IS_OK(status)) {
400 task_server_terminate(task, "kdc: hdb_samba4_create_kdc (setup KDC database) failed", true);
401 return;
404 ret = krb5_plugin_register(kdc->smb_krb5_context->krb5_context,
405 PLUGIN_TYPE_DATA, "hdb",
406 &hdb_samba4_interface);
407 if(ret) {
408 task_server_terminate(task, "kdc: failed to register hdb plugin", true);
409 return;
412 ret = krb5_kt_register(kdc->smb_krb5_context->krb5_context, &hdb_kt_ops);
413 if(ret) {
414 task_server_terminate(task, "kdc: failed to register keytab plugin", true);
415 return;
418 kdc->keytab_name = talloc_asprintf(kdc, "HDB:samba4&%p", kdc->base_ctx);
419 if (kdc->keytab_name == NULL) {
420 task_server_terminate(task,
421 "kdc: Failed to set keytab name",
422 true);
423 return;
426 /* Register WinDC hooks */
427 ret = krb5_plugin_register(kdc->smb_krb5_context->krb5_context,
428 PLUGIN_TYPE_DATA, "windc",
429 &windc_plugin_table);
430 if(ret) {
431 task_server_terminate(task, "kdc: failed to register windc plugin", true);
432 return;
435 ret = krb5_kdc_windc_init(kdc->smb_krb5_context->krb5_context);
437 if(ret) {
438 task_server_terminate(task, "kdc: failed to init windc plugin", true);
439 return;
442 ret = krb5_kdc_pkinit_config(kdc->smb_krb5_context->krb5_context, kdc_config);
444 if(ret) {
445 task_server_terminate(task, "kdc: failed to init kdc pkinit subsystem", true);
446 return;
448 kdc->private_data = kdc_config;
450 /* start listening on the configured network interfaces */
451 status = kdc_startup_interfaces(kdc, task->lp_ctx, ifaces);
452 if (!NT_STATUS_IS_OK(status)) {
453 task_server_terminate(task, "kdc failed to setup interfaces", true);
454 return;
457 status = IRPC_REGISTER(task->msg_ctx, irpc, KDC_CHECK_GENERIC_KERBEROS,
458 kdc_check_generic_kerberos, kdc);
459 if (!NT_STATUS_IS_OK(status)) {
460 task_server_terminate(task, "kdc failed to setup monitoring", true);
461 return;
464 irpc_add_name(task->msg_ctx, "kdc_server");
468 /* called at smbd startup - register ourselves as a server service */
469 NTSTATUS server_service_kdc_init(void)
471 return register_server_service("kdc", kdc_task_init);