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