2 * Copyright (c) 2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Portions (c) 2021, 2022 PADL Software Pty Ltd.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 static int have_plugin
= 0;
41 * Pick the first KDC plugin module that we find.
44 static const char *kdc_plugin_deps
[] = {
51 static struct heim_plugin_data kdc_plugin_data
= {
54 KRB5_PLUGIN_KDC_VERSION_12
,
59 static krb5_error_code KRB5_LIB_CALL
60 load(krb5_context context
, const void *plug
, void *plugctx
, void *userctx
)
63 return KRB5_PLUGIN_NO_HANDLE
;
66 KDC_LIB_FUNCTION krb5_error_code KDC_LIB_CALL
67 krb5_kdc_plugin_init(krb5_context context
)
69 (void)_krb5_plugin_run_f(context
, &kdc_plugin_data
, 0, NULL
, load
);
78 const krb5_keyblock
*reply_key
;
79 uint64_t pac_attributes
;
83 static krb5_error_code KRB5_LIB_CALL
84 generate(krb5_context context
, const void *plug
, void *plugctx
, void *userctx
)
86 const krb5plugin_kdc_ftable
*ft
= (const krb5plugin_kdc_ftable
*)plug
;
87 struct generate_uc
*uc
= (struct generate_uc
*)userctx
;
89 if (ft
->pac_generate
== NULL
)
90 return KRB5_PLUGIN_NO_HANDLE
;
92 return ft
->pac_generate((void *)plug
,
103 _kdc_pac_generate(astgs_request_t r
,
106 const krb5_keyblock
*reply_key
,
107 uint64_t pac_attributes
,
110 krb5_error_code ret
= 0;
111 struct generate_uc uc
;
115 if (krb5_config_get_bool_default(r
->context
, NULL
, FALSE
, "realms",
116 client
->principal
->realm
,
117 "disable_pac", NULL
))
124 uc
.reply_key
= reply_key
;
126 uc
.pac_attributes
= pac_attributes
;
128 ret
= _krb5_plugin_run_f(r
->context
, &kdc_plugin_data
,
130 if (ret
!= KRB5_PLUGIN_NO_HANDLE
)
136 ret
= krb5_pac_init(r
->context
, pac
);
143 krb5_const_principal client_principal
;
144 hdb_entry
*delegated_proxy
;
148 EncTicketPart
*ticket
;
152 static krb5_error_code KRB5_LIB_CALL
153 verify(krb5_context context
, const void *plug
, void *plugctx
, void *userctx
)
155 const krb5plugin_kdc_ftable
*ft
= (const krb5plugin_kdc_ftable
*)plug
;
156 struct verify_uc
*uc
= (struct verify_uc
*)userctx
;
159 if (ft
->pac_verify
== NULL
)
160 return KRB5_PLUGIN_NO_HANDLE
;
162 ret
= ft
->pac_verify((void *)plug
,
164 uc
->client_principal
,
166 uc
->client
, uc
->server
, uc
->krbtgt
,
167 uc
->ticket
, uc
->pac
);
172 _kdc_pac_verify(astgs_request_t r
,
173 krb5_const_principal client_principal
,
174 hdb_entry
*delegated_proxy
,
178 EncTicketPart
*ticket
,
184 return KRB5_PLUGIN_NO_HANDLE
;
187 uc
.client_principal
= client_principal
;
188 uc
.delegated_proxy
= delegated_proxy
;
195 return _krb5_plugin_run_f(r
->context
, &kdc_plugin_data
,
201 krb5_const_principal client_principal
;
202 hdb_entry
*delegated_proxy
;
203 krb5_const_pac delegated_proxy_pac
;
210 static krb5_error_code KRB5_LIB_CALL
211 update(krb5_context context
, const void *plug
, void *plugctx
, void *userctx
)
213 const krb5plugin_kdc_ftable
*ft
= (const krb5plugin_kdc_ftable
*)plug
;
214 struct update_uc
*uc
= (struct update_uc
*)userctx
;
217 if (ft
->pac_update
== NULL
)
218 return KRB5_PLUGIN_NO_HANDLE
;
220 ret
= ft
->pac_update((void *)plug
,
222 uc
->client_principal
,
224 uc
->delegated_proxy_pac
,
225 uc
->client
, uc
->server
, uc
->krbtgt
, uc
->pac
);
230 _kdc_pac_update(astgs_request_t r
,
231 krb5_const_principal client_principal
,
232 hdb_entry
*delegated_proxy
,
233 krb5_const_pac delegated_proxy_pac
,
242 return KRB5_PLUGIN_NO_HANDLE
;
245 uc
.client_principal
= client_principal
;
246 uc
.delegated_proxy
= delegated_proxy
;
247 uc
.delegated_proxy_pac
= delegated_proxy_pac
;
253 return _krb5_plugin_run_f(r
->context
, &kdc_plugin_data
,
257 static krb5_error_code KRB5_LIB_CALL
258 check(krb5_context context
, const void *plug
, void *plugctx
, void *userctx
)
260 const krb5plugin_kdc_ftable
*ft
= (const krb5plugin_kdc_ftable
*)plug
;
262 if (ft
->client_access
== NULL
)
263 return KRB5_PLUGIN_NO_HANDLE
;
264 return ft
->client_access((void *)plug
, userctx
);
268 _kdc_check_access(astgs_request_t r
)
270 krb5_error_code ret
= KRB5_PLUGIN_NO_HANDLE
;
273 ret
= _krb5_plugin_run_f(r
->context
, &kdc_plugin_data
,
277 if (ret
== KRB5_PLUGIN_NO_HANDLE
)
278 return kdc_check_flags(r
, r
->req
.msg_type
== krb_as_req
,
279 r
->client
, r
->server
);
283 static krb5_error_code KRB5_LIB_CALL
284 referral_policy(krb5_context context
, const void *plug
, void *plugctx
, void *userctx
)
286 const krb5plugin_kdc_ftable
*ft
= (const krb5plugin_kdc_ftable
*)plug
;
288 if (ft
->referral_policy
== NULL
)
289 return KRB5_PLUGIN_NO_HANDLE
;
290 return ft
->referral_policy((void *)plug
, userctx
);
294 _kdc_referral_policy(astgs_request_t r
)
296 krb5_error_code ret
= KRB5_PLUGIN_NO_HANDLE
;
299 ret
= _krb5_plugin_run_f(r
->context
, &kdc_plugin_data
, 0, r
, referral_policy
);
304 static krb5_error_code KRB5_LIB_CALL
305 hwauth_policy(krb5_context context
, const void *plug
, void *plugctx
, void *userctx
)
307 const krb5plugin_kdc_ftable
*ft
= plug
;
309 if (ft
->hwauth_policy
== NULL
) {
310 return KRB5_PLUGIN_NO_HANDLE
;
312 return ft
->hwauth_policy((void *)plug
, userctx
);
316 _kdc_hwauth_policy(astgs_request_t r
)
318 krb5_error_code ret
= KRB5_PLUGIN_NO_HANDLE
;
321 ret
= _krb5_plugin_run_f(r
->context
, &kdc_plugin_data
, 0, r
, hwauth_policy
);
324 if (ret
== KRB5_PLUGIN_NO_HANDLE
) {
331 static krb5_error_code KRB5_LIB_CALL
332 finalize_reply(krb5_context context
, const void *plug
, void *plugctx
, void *userctx
)
334 const krb5plugin_kdc_ftable
*ft
= (const krb5plugin_kdc_ftable
*)plug
;
336 if (ft
->finalize_reply
== NULL
)
337 return KRB5_PLUGIN_NO_HANDLE
;
338 return ft
->finalize_reply((void *)plug
, userctx
);
342 _kdc_finalize_reply(astgs_request_t r
)
344 krb5_error_code ret
= KRB5_PLUGIN_NO_HANDLE
;
347 ret
= _krb5_plugin_run_f(r
->context
, &kdc_plugin_data
, 0, r
, finalize_reply
);
349 if (ret
== KRB5_PLUGIN_NO_HANDLE
)
355 static krb5_error_code KRB5_LIB_CALL
356 audit(krb5_context context
, const void *plug
, void *plugctx
, void *userctx
)
358 const krb5plugin_kdc_ftable
*ft
= (const krb5plugin_kdc_ftable
*)plug
;
360 if (ft
->audit
== NULL
)
361 return KRB5_PLUGIN_NO_HANDLE
;
362 return ft
->audit((void *)plug
, userctx
);
366 _kdc_plugin_audit(astgs_request_t r
)
368 krb5_error_code ret
= KRB5_PLUGIN_NO_HANDLE
;
371 ret
= _krb5_plugin_run_f(r
->context
, &kdc_plugin_data
, 0, r
, audit
);
373 if (ret
== KRB5_PLUGIN_NO_HANDLE
)
379 KDC_LIB_FUNCTION
uintptr_t KDC_LIB_CALL
380 kdc_get_instance(const char *libname
)
382 static const char *instance
= "libkdc";
384 if (strcmp(libname
, "kdc") == 0)
385 return (uintptr_t)instance
;
386 else if (strcmp(libname
, "hdb") == 0)
387 return hdb_get_instance(libname
);
388 else if (strcmp(libname
, "krb5") == 0)
389 return krb5_get_instance(libname
);
395 * Minimum API surface wrapper for libheimbase object types so it
396 * may remain a private interface, yet plugins can interact with
400 KDC_LIB_FUNCTION kdc_object_t KDC_LIB_CALL
401 kdc_object_alloc(size_t size
, const char *name
, kdc_type_dealloc dealloc
)
403 return heim_alloc(size
, name
, dealloc
);
406 KDC_LIB_FUNCTION kdc_object_t KDC_LIB_CALL
407 kdc_object_retain(kdc_object_t o
)
409 return heim_retain(o
);
412 KDC_LIB_FUNCTION
void KDC_LIB_CALL
413 kdc_object_release(kdc_object_t o
)
418 KDC_LIB_FUNCTION kdc_object_t KDC_LIB_CALL
419 kdc_bool_create(krb5_boolean v
)
421 return heim_bool_create(v
);
424 KDC_LIB_FUNCTION krb5_boolean KDC_LIB_CALL
425 kdc_bool_get_value(kdc_object_t o
)
427 return heim_bool_val(o
);
430 struct kdc_array_iterator_trampoline_data
{
431 kdc_array_iterator_t iter
;
436 * Calling convention shim to avoid needing to update all internal
437 * consumers of heim_array_iterate_f()
440 _kdc_array_iterator_trampoline(kdc_object_t o
, void *data
, int *stop
)
442 struct kdc_array_iterator_trampoline_data
*t
= data
;
444 t
->iter(o
, t
->data
, stop
);
447 KDC_LIB_FUNCTION
void KDC_LIB_CALL
448 kdc_array_iterate(kdc_array_t a
, void *d
, kdc_array_iterator_t iter
)
450 struct kdc_array_iterator_trampoline_data t
;
455 heim_array_iterate_f((heim_array_t
)a
, &t
, _kdc_array_iterator_trampoline
);
458 KDC_LIB_FUNCTION
size_t KDC_LIB_CALL
459 kdc_array_get_length(kdc_array_t a
)
461 return heim_array_get_length((heim_array_t
)a
);
464 KDC_LIB_FUNCTION kdc_object_t KDC_LIB_CALL
465 kdc_array_get_value(heim_array_t a
, size_t i
)
467 return heim_array_get_value((heim_array_t
)a
, i
);
470 KDC_LIB_FUNCTION kdc_object_t KDC_LIB_CALL
471 kdc_array_copy_value(heim_array_t a
, size_t i
)
473 return heim_array_copy_value((heim_array_t
)a
, i
);
476 KDC_LIB_FUNCTION kdc_string_t KDC_LIB_CALL
477 kdc_string_create(const char *s
)
479 return (kdc_string_t
)heim_string_create(s
);
482 KDC_LIB_FUNCTION
const char * KDC_LIB_CALL
483 kdc_string_get_utf8(kdc_string_t s
)
485 return heim_string_get_utf8((heim_string_t
)s
);
488 KDC_LIB_FUNCTION kdc_data_t
489 kdc_data_create(const void *d
, size_t len
)
491 return (kdc_data_t
)heim_data_create(d
, len
);
494 KDC_LIB_FUNCTION
const krb5_data
* KDC_LIB_CALL
495 kdc_data_get_data(kdc_data_t d
)
497 return heim_data_get_data((heim_data_t
)d
);
500 KDC_LIB_FUNCTION kdc_number_t KDC_LIB_CALL
501 kdc_number_create(int64_t v
)
503 return (kdc_number_t
)heim_number_create(v
);
506 KDC_LIB_FUNCTION
int64_t KDC_LIB_CALL
507 kdc_number_get_value(kdc_number_t n
)
509 return heim_number_get_long((heim_number_t
)n
);
516 KDC_LIB_FUNCTION krb5_error_code KDC_LIB_CALL
517 kdc_request_add_reply_padata(astgs_request_t r
, PA_DATA
*md
)
519 heim_assert(r
->rep
.padata
!= NULL
, "reply padata not allocated");
520 return add_METHOD_DATA(r
->rep
.padata
, md
);
523 KDC_LIB_FUNCTION krb5_error_code KDC_LIB_CALL
524 kdc_request_add_encrypted_padata(astgs_request_t r
, PA_DATA
*md
)
526 if (r
->ek
.encrypted_pa_data
== NULL
) {
527 r
->ek
.encrypted_pa_data
= calloc(1, sizeof *(r
->ek
.encrypted_pa_data
));
528 if (r
->ek
.encrypted_pa_data
== NULL
) {
533 return add_METHOD_DATA(r
->ek
.encrypted_pa_data
, md
);
536 KDC_LIB_FUNCTION krb5_error_code KDC_LIB_CALL
537 kdc_request_add_pac_buffer(astgs_request_t r
,
544 if (r
->pac
== NULL
) {
545 ret
= krb5_pac_init(r
->context
, &pac
);
549 pac
= heim_retain(r
->pac
);
551 ret
= krb5_pac_add_buffer(r
->context
, pac
, pactype
, d
);
552 if (ret
== 0 && r
->pac
== NULL
)
561 * Override the e-data field to be returned in an error reply. The data will be
562 * owned by the KDC and eventually will be freed with krb5_data_free().
564 KDC_LIB_FUNCTION krb5_error_code KDC_LIB_CALL
565 kdc_request_set_e_data(astgs_request_t r
, heim_octet_string e_data
)
567 krb5_data_free(&r
->e_data
);
573 #undef _KDC_REQUEST_GET_ACCESSOR
574 #define _KDC_REQUEST_GET_ACCESSOR(R, T, f) \
575 KDC_LIB_FUNCTION T KDC_LIB_CALL \
576 kdc_request_get_ ## f(R r) \
581 #undef _KDC_REQUEST_SET_ACCESSOR
582 #define _KDC_REQUEST_SET_ACCESSOR(R, T, f) \
583 KDC_LIB_FUNCTION void KDC_LIB_CALL \
584 kdc_request_set_ ## f(R r, T v) \
589 #undef _KDC_REQUEST_GET_ACCESSOR_PTR
590 #define _KDC_REQUEST_GET_ACCESSOR_PTR(R, T, f) \
591 KDC_LIB_FUNCTION const T KDC_LIB_CALL \
592 kdc_request_get_ ## f(R r) \
597 #undef _KDC_REQUEST_SET_ACCESSOR_PTR
598 #define _KDC_REQUEST_SET_ACCESSOR_PTR(R, T, t, f) \
599 KDC_LIB_FUNCTION krb5_error_code KDC_LIB_CALL \
600 kdc_request_set_ ## f(R r, const T v) \
602 krb5_error_code ret; \
608 ret = copy_##t(v, &tmp); \
620 #undef _KDC_REQUEST_GET_ACCESSOR_STRUCT
621 #define _KDC_REQUEST_GET_ACCESSOR_STRUCT(R, T, f) \
622 KDC_LIB_FUNCTION const T * KDC_LIB_CALL \
623 kdc_request_get_ ## f(R r) \
628 #undef _KDC_REQUEST_SET_ACCESSOR_STRUCT
629 #define _KDC_REQUEST_SET_ACCESSOR_STRUCT(R, T, t, f) \
630 KDC_LIB_FUNCTION krb5_error_code KDC_LIB_CALL \
631 kdc_request_set_ ## f(R r, const T *v) \
633 krb5_error_code ret; \
638 else if (v == &r->f) \
641 ret = copy_##t(v, &tmp); \
651 static krb5_error_code
652 copy_string_ptr(const char *src
, char **dst
)
662 free_string_ptr(char *s
)
667 static krb5_error_code
668 copy_Principal_ptr(krb5_const_principal src
, krb5_principal
*dst
)
675 p
= calloc(1, sizeof(*p
));
679 ret
= copy_Principal(src
, p
);
689 free_Principal_ptr(krb5_principal p
)
697 static krb5_error_code
698 copy_pac(const struct krb5_pac_data
*src
, struct krb5_pac_data
**dst
)
700 /* FIXME use heim_copy() when it exists */
701 *dst
= (krb5_pac
)heim_retain((heim_object_t
)src
);
706 free_pac(struct krb5_pac_data
*o
)
711 static krb5_error_code
712 copy_keyblock(const EncryptionKey
*src
, EncryptionKey
*dst
)
714 return copy_EncryptionKey(src
, dst
);
718 free_keyblock(EncryptionKey
*key
)
720 krb5_free_keyblock_contents(NULL
, key
);
723 #undef HEIMDAL_KDC_KDC_ACCESSORS_H
724 #include "kdc-accessors.h"
726 #undef _KDC_REQUEST_GET_ACCESSOR
727 #undef _KDC_REQUEST_SET_ACCESSOR
729 #undef _KDC_REQUEST_GET_ACCESSOR_PTR
730 #undef _KDC_REQUEST_SET_ACCESSOR_PTR
731 #define _KDC_REQUEST_SET_ACCESSOR_PTR(R, T, t, f) \
733 _kdc_request_set_ ## f ## _nocopy(R r, T *v) \
742 #undef _KDC_REQUEST_GET_ACCESSOR_STRUCT
743 #undef _KDC_REQUEST_SET_ACCESSOR_STRUCT
744 #define _KDC_REQUEST_SET_ACCESSOR_STRUCT(R, T, t, f) \
746 _kdc_request_set_ ## f ## _nocopy(R r, T *v) \
752 memset(v, 0, sizeof(*v)); \
755 #undef HEIMDAL_KDC_KDC_ACCESSORS_H
756 #include "kdc-accessors.h"
758 KDC_LIB_FUNCTION
const HDB
* KDC_LIB_CALL
759 kdc_request_get_explicit_armor_clientdb(astgs_request_t r
)
761 return r
->explicit_armor_present
? r
->armor_clientdb
: NULL
;
764 KDC_LIB_FUNCTION
const hdb_entry
* KDC_LIB_CALL
765 kdc_request_get_explicit_armor_client(astgs_request_t r
)
767 return r
->explicit_armor_present
? r
->armor_client
: NULL
;
770 KDC_LIB_FUNCTION
const hdb_entry
* KDC_LIB_CALL
771 kdc_request_get_explicit_armor_server(astgs_request_t r
)
773 return r
->explicit_armor_present
? r
->armor_server
: NULL
;
776 KDC_LIB_FUNCTION krb5_const_pac KDC_LIB_CALL
777 kdc_request_get_explicit_armor_pac(astgs_request_t r
)
779 return r
->explicit_armor_present
? r
->armor_pac
: NULL
;