Line data Source code
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 "samba/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>
42 :
43 : NTSTATUS server_service_kdc_init(TALLOC_CTX *);
44 :
45 : extern struct krb5plugin_kdc_ftable kdc_plugin_table;
46 :
47 : /**
48 : Wrapper for krb5_kdc_process_krb5_request, converting to/from Samba
49 : calling conventions
50 : */
51 :
52 71023 : 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)
59 : {
60 : int ret;
61 : char *pa;
62 : struct sockaddr_storage ss;
63 : krb5_data k5_reply;
64 71023 : krb5_kdc_configuration *kdc_config =
65 : (krb5_kdc_configuration *)kdc->private_data;
66 :
67 71023 : krb5_data_zero(&k5_reply);
68 :
69 71023 : krb5_kdc_update_time(NULL);
70 :
71 71023 : ret = tsocket_address_bsd_sockaddr(peer_addr, (struct sockaddr *) &ss,
72 : sizeof(struct sockaddr_storage));
73 71023 : if (ret < 0) {
74 0 : return KDC_ERROR;
75 : }
76 71023 : pa = tsocket_address_string(peer_addr, mem_ctx);
77 71023 : if (pa == NULL) {
78 0 : return KDC_ERROR;
79 : }
80 :
81 71023 : DBG_DEBUG("Received KDC packet of length %lu from %s\n",
82 : (long)input->length - 4, pa);
83 :
84 142046 : ret = krb5_kdc_process_krb5_request(kdc->smb_krb5_context->krb5_context,
85 : kdc_config,
86 71023 : input->data, input->length,
87 : &k5_reply,
88 : pa,
89 : (struct sockaddr *) &ss,
90 : datagram_reply);
91 71023 : if (ret == -1) {
92 0 : *reply = data_blob(NULL, 0);
93 0 : return KDC_ERROR;
94 : }
95 :
96 71023 : if (ret == HDB_ERR_NOT_FOUND_HERE) {
97 3053 : *reply = data_blob(NULL, 0);
98 3053 : return KDC_PROXY_REQUEST;
99 : }
100 :
101 67970 : if (k5_reply.length) {
102 67970 : *reply = data_blob_talloc(mem_ctx, k5_reply.data, k5_reply.length);
103 67970 : krb5_data_free(&k5_reply);
104 : } else {
105 0 : *reply = data_blob(NULL, 0);
106 : }
107 67970 : return KDC_OK;
108 : }
109 :
110 : /*
111 : setup our listening sockets on the configured network interfaces
112 : */
113 35 : static NTSTATUS kdc_startup_interfaces(struct kdc_server *kdc,
114 : struct loadparm_context *lp_ctx,
115 : struct interface *ifaces,
116 : const struct model_ops *model_ops)
117 : {
118 : int num_interfaces;
119 35 : TALLOC_CTX *tmp_ctx = talloc_new(kdc);
120 : NTSTATUS status;
121 : int i;
122 35 : uint16_t kdc_port = lpcfg_krb5_port(lp_ctx);
123 35 : uint16_t kpasswd_port = lpcfg_kpasswd_port(lp_ctx);
124 35 : bool done_wildcard = false;
125 :
126 35 : num_interfaces = iface_list_count(ifaces);
127 :
128 : /* if we are allowing incoming packets from any address, then
129 : we need to bind to the wildcard address */
130 35 : if (!lpcfg_bind_interfaces_only(lp_ctx)) {
131 35 : size_t num_binds = 0;
132 35 : char **wcard = iface_list_wildcard(kdc);
133 35 : NT_STATUS_HAVE_NO_MEMORY(wcard);
134 105 : for (i=0; wcard[i]; i++) {
135 70 : if (kdc_port) {
136 140 : status = kdc_add_socket(kdc, model_ops,
137 70 : "kdc", wcard[i], kdc_port,
138 : kdc_process, false);
139 70 : if (NT_STATUS_IS_OK(status)) {
140 70 : num_binds++;
141 : }
142 : }
143 :
144 70 : if (kpasswd_port) {
145 140 : status = kdc_add_socket(kdc, model_ops,
146 70 : "kpasswd", wcard[i], kpasswd_port,
147 : kpasswd_process, false);
148 70 : if (NT_STATUS_IS_OK(status)) {
149 70 : num_binds++;
150 : }
151 : }
152 : }
153 35 : talloc_free(wcard);
154 35 : if (num_binds == 0) {
155 0 : return NT_STATUS_INVALID_PARAMETER_MIX;
156 : }
157 35 : done_wildcard = true;
158 : }
159 :
160 105 : for (i=0; i<num_interfaces; i++) {
161 70 : const char *address = talloc_strdup(tmp_ctx, iface_list_n_ip(ifaces, i));
162 :
163 70 : if (kdc_port) {
164 70 : status = kdc_add_socket(kdc, model_ops,
165 : "kdc", address, kdc_port,
166 : kdc_process, done_wildcard);
167 70 : NT_STATUS_NOT_OK_RETURN(status);
168 : }
169 :
170 70 : if (kpasswd_port) {
171 70 : status = kdc_add_socket(kdc, model_ops,
172 : "kpasswd", address, kpasswd_port,
173 : kpasswd_process, done_wildcard);
174 70 : NT_STATUS_NOT_OK_RETURN(status);
175 : }
176 : }
177 :
178 35 : talloc_free(tmp_ctx);
179 :
180 35 : return NT_STATUS_OK;
181 : }
182 :
183 120 : static NTSTATUS kdc_check_generic_kerberos(struct irpc_message *msg,
184 : struct kdc_check_generic_kerberos *r)
185 : {
186 : struct PAC_Validate pac_validate;
187 : DATA_BLOB srv_sig;
188 : struct PAC_SIGNATURE_DATA kdc_sig;
189 120 : struct kdc_server *kdc = talloc_get_type(msg->private_data, struct kdc_server);
190 120 : krb5_kdc_configuration *kdc_config =
191 : (krb5_kdc_configuration *)kdc->private_data;
192 : enum ndr_err_code ndr_err;
193 : int ret;
194 : hdb_entry ent;
195 : krb5_principal principal;
196 :
197 :
198 : /* There is no reply to this request */
199 120 : r->out.generic_reply = data_blob(NULL, 0);
200 :
201 120 : ndr_err = ndr_pull_struct_blob(&r->in.generic_request, msg, &pac_validate,
202 : (ndr_pull_flags_fn_t)ndr_pull_PAC_Validate);
203 120 : if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
204 0 : return NT_STATUS_INVALID_PARAMETER;
205 : }
206 :
207 120 : if (pac_validate.MessageType != NETLOGON_GENERIC_KRB5_PAC_VALIDATE) {
208 : /* We don't implement any other message types - such as certificate validation - yet */
209 0 : return NT_STATUS_INVALID_PARAMETER;
210 : }
211 :
212 120 : if (pac_validate.ChecksumAndSignature.length != (pac_validate.ChecksumLength + pac_validate.SignatureLength)
213 72 : || pac_validate.ChecksumAndSignature.length < pac_validate.ChecksumLength
214 72 : || pac_validate.ChecksumAndSignature.length < pac_validate.SignatureLength ) {
215 48 : return NT_STATUS_INVALID_PARAMETER;
216 : }
217 :
218 72 : srv_sig = data_blob_const(pac_validate.ChecksumAndSignature.data,
219 72 : pac_validate.ChecksumLength);
220 :
221 144 : ret = krb5_make_principal(kdc->smb_krb5_context->krb5_context, &principal,
222 72 : lpcfg_realm(kdc->task->lp_ctx),
223 72 : "krbtgt", lpcfg_realm(kdc->task->lp_ctx),
224 : NULL);
225 :
226 72 : if (ret != 0) {
227 0 : return NT_STATUS_NO_MEMORY;
228 : }
229 :
230 144 : ret = kdc_config->db[0]->hdb_fetch_kvno(kdc->smb_krb5_context->krb5_context,
231 72 : kdc_config->db[0],
232 : principal,
233 : HDB_F_GET_KRBTGT | HDB_F_DECRYPT,
234 : 0,
235 : &ent);
236 :
237 72 : if (ret != 0) {
238 0 : hdb_free_entry(kdc->smb_krb5_context->krb5_context, kdc_config->db[0], &ent);
239 0 : krb5_free_principal(kdc->smb_krb5_context->krb5_context, principal);
240 :
241 0 : return NT_STATUS_LOGON_FAILURE;
242 : }
243 :
244 72 : kdc_sig.type = pac_validate.SignatureType;
245 72 : kdc_sig.signature = data_blob_const(&pac_validate.ChecksumAndSignature.data[pac_validate.ChecksumLength],
246 72 : pac_validate.SignatureLength);
247 :
248 72 : ret = kdc_check_pac(kdc->smb_krb5_context->krb5_context, srv_sig, &kdc_sig, &ent);
249 :
250 72 : hdb_free_entry(kdc->smb_krb5_context->krb5_context, kdc_config->db[0], &ent);
251 72 : krb5_free_principal(kdc->smb_krb5_context->krb5_context, principal);
252 :
253 72 : if (ret != 0) {
254 48 : return NT_STATUS_LOGON_FAILURE;
255 : }
256 :
257 24 : return NT_STATUS_OK;
258 : }
259 :
260 :
261 : /*
262 : startup the kdc task
263 : */
264 38 : static NTSTATUS kdc_task_init(struct task_server *task)
265 : {
266 : struct kdc_server *kdc;
267 : NTSTATUS status;
268 : struct interface *ifaces;
269 :
270 38 : switch (lpcfg_server_role(task->lp_ctx)) {
271 0 : case ROLE_STANDALONE:
272 0 : task_server_terminate(task, "kdc: no KDC required in standalone configuration", false);
273 0 : return NT_STATUS_INVALID_DOMAIN_ROLE;
274 3 : case ROLE_DOMAIN_MEMBER:
275 3 : task_server_terminate(task, "kdc: no KDC required in member server configuration", false);
276 0 : return NT_STATUS_INVALID_DOMAIN_ROLE;
277 0 : case ROLE_DOMAIN_PDC:
278 : case ROLE_DOMAIN_BDC:
279 : case ROLE_IPA_DC:
280 0 : task_server_terminate(
281 : task, "Cannot start KDC as a 'classic Samba' DC", false);
282 0 : return NT_STATUS_INVALID_DOMAIN_ROLE;
283 35 : case ROLE_ACTIVE_DIRECTORY_DC:
284 : /* Yes, we want a KDC */
285 35 : break;
286 : }
287 :
288 35 : load_interface_list(task, task->lp_ctx, &ifaces);
289 :
290 35 : if (iface_list_count(ifaces) == 0) {
291 0 : task_server_terminate(task, "kdc: no network interfaces configured", false);
292 0 : return NT_STATUS_UNSUCCESSFUL;
293 : }
294 :
295 35 : task_server_set_title(task, "task[kdc]");
296 :
297 35 : kdc = talloc_zero(task, struct kdc_server);
298 35 : if (kdc == NULL) {
299 0 : task_server_terminate(task, "kdc: out of memory", true);
300 0 : return NT_STATUS_NO_MEMORY;
301 : }
302 :
303 35 : kdc->task = task;
304 35 : task->private_data = kdc;
305 :
306 : /* start listening on the configured network interfaces */
307 35 : status = kdc_startup_interfaces(kdc, task->lp_ctx, ifaces,
308 : task->model_ops);
309 35 : if (!NT_STATUS_IS_OK(status)) {
310 0 : task_server_terminate(task, "kdc failed to setup interfaces", true);
311 0 : return status;
312 : }
313 :
314 :
315 35 : return NT_STATUS_OK;
316 : }
317 :
318 : /*
319 : initialise the kdc task after a fork
320 : */
321 71 : static void kdc_post_fork(struct task_server *task, struct process_details *pd)
322 : {
323 : struct kdc_server *kdc;
324 71 : krb5_kdc_configuration *kdc_config = NULL;
325 : NTSTATUS status;
326 : krb5_error_code ret;
327 : int ldb_ret;
328 :
329 71 : if (task == NULL) {
330 0 : task_server_terminate(task, "kdc: Null task", true);
331 0 : return;
332 : }
333 71 : if (task->private_data == NULL) {
334 0 : task_server_terminate(task, "kdc: No kdc_server info", true);
335 0 : return;
336 : }
337 71 : kdc = talloc_get_type_abort(task->private_data, struct kdc_server);
338 :
339 : /* get a samdb connection */
340 213 : kdc->samdb = samdb_connect(kdc,
341 71 : kdc->task->event_ctx,
342 71 : kdc->task->lp_ctx,
343 71 : system_session(kdc->task->lp_ctx),
344 : NULL,
345 : 0);
346 71 : if (!kdc->samdb) {
347 0 : DBG_WARNING("kdc_task_init: unable to connect to samdb\n");
348 0 : task_server_terminate(task, "kdc: krb5_init_context samdb connect failed", true);
349 0 : return;
350 : }
351 :
352 71 : ldb_ret = samdb_rodc(kdc->samdb, &kdc->am_rodc);
353 71 : if (ldb_ret != LDB_SUCCESS) {
354 0 : DBG_WARNING("kdc_task_init: "
355 : "Cannot determine if we are an RODC: %s\n",
356 : ldb_errstring(kdc->samdb));
357 0 : task_server_terminate(task, "kdc: krb5_init_context samdb RODC connect failed", true);
358 0 : return;
359 : }
360 :
361 71 : kdc->proxy_timeout = lpcfg_parm_int(kdc->task->lp_ctx, NULL, "kdc", "proxy timeout", 5);
362 :
363 71 : initialize_krb5_error_table();
364 :
365 71 : ret = smb_krb5_init_context(kdc, task->lp_ctx, &kdc->smb_krb5_context);
366 71 : if (ret) {
367 0 : DBG_WARNING("kdc_task_init: krb5_init_context failed (%s)\n",
368 : error_message(ret));
369 0 : task_server_terminate(task, "kdc: krb5_init_context failed", true);
370 0 : return;
371 : }
372 :
373 71 : krb5_add_et_list(kdc->smb_krb5_context->krb5_context, initialize_hdb_error_table_r);
374 :
375 71 : ret = krb5_kdc_get_config(kdc->smb_krb5_context->krb5_context,
376 : &kdc_config);
377 71 : if(ret) {
378 0 : task_server_terminate(task, "kdc: failed to get KDC configuration", true);
379 0 : return;
380 : }
381 :
382 71 : kdc_config->logf = (krb5_log_facility *)kdc->smb_krb5_context->pvt_log_data;
383 71 : kdc_config->db = talloc(kdc, struct HDB *);
384 71 : if (!kdc_config->db) {
385 0 : task_server_terminate(task, "kdc: out of memory", true);
386 0 : return;
387 : }
388 71 : kdc_config->num_db = 1;
389 :
390 : /*
391 : * Note with the CVE-2022-37966 patches,
392 : * see https://bugzilla.samba.org/show_bug.cgi?id=15219
393 : * and https://bugzilla.samba.org/show_bug.cgi?id=15237
394 : * we want to use the strongest keys for everything.
395 : *
396 : * Some of these don't have any real effect anymore,
397 : * but it is better to have them as true...
398 : */
399 71 : kdc_config->tgt_use_strongest_session_key = true;
400 71 : kdc_config->preauth_use_strongest_session_key = true;
401 71 : kdc_config->svc_use_strongest_session_key = true;
402 71 : kdc_config->use_strongest_server_key = true;
403 :
404 71 : kdc_config->force_include_pa_etype_salt = true;
405 :
406 : /*
407 : * For Samba CVE-2020-25719 Require PAC to be present
408 : * This instructs Heimdal to match AD behaviour,
409 : * as seen after Microsoft's CVE-2021-42287 when
410 : * PacRequestorEnforcement is set to 2.
411 : *
412 : * Samba BUG: https://bugzilla.samba.org/show_bug.cgi?id=14686
413 : * REF: https://support.microsoft.com/en-au/topic/kb5008380-authentication-updates-cve-2021-42287-9dafac11-e0d0-4cb8-959a-143bd0201041
414 : */
415 :
416 71 : kdc_config->require_pac = true;
417 :
418 : /*
419 : * By default we enable RFC6113/FAST support,
420 : * but we have an option to disable in order to
421 : * test against a KDC with FAST support.
422 : */
423 71 : kdc_config->enable_fast = lpcfg_kdc_enable_fast(task->lp_ctx);
424 :
425 : /*
426 : * Match Windows and RFC6113 and Windows but break older
427 : * Heimdal clients.
428 : */
429 71 : kdc_config->enable_armored_pa_enc_timestamp = false;
430 :
431 : /* Register hdb-samba4 hooks for use as a keytab */
432 :
433 71 : kdc->base_ctx = talloc_zero(kdc, struct samba_kdc_base_context);
434 71 : if (!kdc->base_ctx) {
435 0 : task_server_terminate(task, "kdc: out of memory", true);
436 0 : return;
437 : }
438 :
439 71 : kdc->base_ctx->ev_ctx = task->event_ctx;
440 71 : kdc->base_ctx->lp_ctx = task->lp_ctx;
441 71 : kdc->base_ctx->msg_ctx = task->msg_ctx;
442 :
443 142 : status = hdb_samba4_create_kdc(kdc->base_ctx,
444 71 : kdc->smb_krb5_context->krb5_context,
445 71 : &kdc_config->db[0]);
446 71 : if (!NT_STATUS_IS_OK(status)) {
447 0 : task_server_terminate(task, "kdc: hdb_samba4_create_kdc (setup KDC database) failed", true);
448 0 : return;
449 : }
450 :
451 71 : ret = krb5_plugin_register(kdc->smb_krb5_context->krb5_context,
452 : PLUGIN_TYPE_DATA, "hdb_samba4_interface",
453 : &hdb_samba4_interface);
454 71 : if(ret) {
455 0 : task_server_terminate(task, "kdc: failed to register hdb plugin", true);
456 0 : return;
457 : }
458 :
459 71 : kdc->kpasswd_keytab_name = talloc_asprintf(kdc, "HDBGET:samba4:&%p", kdc->base_ctx);
460 71 : if (kdc->kpasswd_keytab_name == NULL) {
461 0 : task_server_terminate(task,
462 : "kdc: Failed to set keytab name",
463 : true);
464 0 : return;
465 : }
466 :
467 71 : ret = krb5_kt_register(kdc->smb_krb5_context->krb5_context, &hdb_get_kt_ops);
468 71 : if(ret) {
469 0 : task_server_terminate(task, "kdc: failed to register keytab plugin", true);
470 0 : return;
471 : }
472 :
473 : /* Register KDC hooks */
474 71 : ret = krb5_plugin_register(kdc->smb_krb5_context->krb5_context,
475 : PLUGIN_TYPE_DATA, "kdc",
476 : &kdc_plugin_table);
477 71 : if(ret) {
478 0 : task_server_terminate(task, "kdc: failed to register kdc plugin", true);
479 0 : return;
480 : }
481 :
482 71 : ret = krb5_kdc_plugin_init(kdc->smb_krb5_context->krb5_context);
483 :
484 71 : if(ret) {
485 0 : task_server_terminate(task, "kdc: failed to init kdc plugin", true);
486 0 : return;
487 : }
488 :
489 71 : ret = krb5_kdc_pkinit_config(kdc->smb_krb5_context->krb5_context, kdc_config);
490 :
491 71 : if(ret) {
492 0 : task_server_terminate(task, "kdc: failed to init kdc pkinit subsystem", true);
493 0 : return;
494 : }
495 71 : kdc->private_data = kdc_config;
496 :
497 71 : status = IRPC_REGISTER(task->msg_ctx, irpc, KDC_CHECK_GENERIC_KERBEROS,
498 : kdc_check_generic_kerberos, kdc);
499 71 : if (!NT_STATUS_IS_OK(status)) {
500 0 : task_server_terminate(task, "kdc failed to setup monitoring", true);
501 0 : return;
502 : }
503 :
504 71 : irpc_add_name(task->msg_ctx, "kdc_server");
505 : }
506 :
507 :
508 : /* called at smbd startup - register ourselves as a server service */
509 38 : NTSTATUS server_service_kdc_init(TALLOC_CTX *ctx)
510 : {
511 : static const struct service_details details = {
512 : .inhibit_fork_on_accept = true,
513 : .inhibit_pre_fork = false,
514 : .task_init = kdc_task_init,
515 : .post_fork = kdc_post_fork
516 : };
517 38 : return register_server_service(ctx, "kdc", &details);
518 : }
|