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_10
,
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_principal client_principal
;
144 krb5_principal delegated_proxy_principal
;
151 static krb5_error_code KRB5_LIB_CALL
152 verify(krb5_context context
, const void *plug
, void *plugctx
, void *userctx
)
154 const krb5plugin_kdc_ftable
*ft
= (const krb5plugin_kdc_ftable
*)plug
;
155 struct verify_uc
*uc
= (struct verify_uc
*)userctx
;
158 if (ft
->pac_verify
== NULL
)
159 return KRB5_PLUGIN_NO_HANDLE
;
161 ret
= ft
->pac_verify((void *)plug
,
163 uc
->client_principal
,
164 uc
->delegated_proxy_principal
,
165 uc
->client
, uc
->server
, uc
->krbtgt
, uc
->pac
);
170 _kdc_pac_verify(astgs_request_t r
,
171 const krb5_principal client_principal
,
172 const krb5_principal delegated_proxy_principal
,
181 return KRB5_PLUGIN_NO_HANDLE
;
184 uc
.client_principal
= client_principal
;
185 uc
.delegated_proxy_principal
= delegated_proxy_principal
;
191 return _krb5_plugin_run_f(r
->context
, &kdc_plugin_data
,
195 static krb5_error_code KRB5_LIB_CALL
196 check(krb5_context context
, const void *plug
, void *plugctx
, void *userctx
)
198 const krb5plugin_kdc_ftable
*ft
= (const krb5plugin_kdc_ftable
*)plug
;
200 if (ft
->client_access
== NULL
)
201 return KRB5_PLUGIN_NO_HANDLE
;
202 return ft
->client_access((void *)plug
, userctx
);
206 _kdc_check_access(astgs_request_t r
)
208 krb5_error_code ret
= KRB5_PLUGIN_NO_HANDLE
;
211 ret
= _krb5_plugin_run_f(r
->context
, &kdc_plugin_data
,
215 if (ret
== KRB5_PLUGIN_NO_HANDLE
)
216 return kdc_check_flags(r
, r
->req
.msg_type
== krb_as_req
,
217 r
->client
, r
->server
);
221 static krb5_error_code KRB5_LIB_CALL
222 referral_policy(krb5_context context
, const void *plug
, void *plugctx
, void *userctx
)
224 const krb5plugin_kdc_ftable
*ft
= (const krb5plugin_kdc_ftable
*)plug
;
226 if (ft
->referral_policy
== NULL
)
227 return KRB5_PLUGIN_NO_HANDLE
;
228 return ft
->referral_policy((void *)plug
, userctx
);
232 _kdc_referral_policy(astgs_request_t r
)
234 krb5_error_code ret
= KRB5_PLUGIN_NO_HANDLE
;
237 ret
= _krb5_plugin_run_f(r
->context
, &kdc_plugin_data
, 0, r
, referral_policy
);
242 static krb5_error_code KRB5_LIB_CALL
243 finalize_reply(krb5_context context
, const void *plug
, void *plugctx
, void *userctx
)
245 const krb5plugin_kdc_ftable
*ft
= (const krb5plugin_kdc_ftable
*)plug
;
247 if (ft
->finalize_reply
== NULL
)
248 return KRB5_PLUGIN_NO_HANDLE
;
249 return ft
->finalize_reply((void *)plug
, userctx
);
253 _kdc_finalize_reply(astgs_request_t r
)
255 krb5_error_code ret
= KRB5_PLUGIN_NO_HANDLE
;
258 ret
= _krb5_plugin_run_f(r
->context
, &kdc_plugin_data
, 0, r
, finalize_reply
);
260 if (ret
== KRB5_PLUGIN_NO_HANDLE
)
266 static krb5_error_code KRB5_LIB_CALL
267 audit(krb5_context context
, const void *plug
, void *plugctx
, void *userctx
)
269 const krb5plugin_kdc_ftable
*ft
= (const krb5plugin_kdc_ftable
*)plug
;
271 if (ft
->audit
== NULL
)
272 return KRB5_PLUGIN_NO_HANDLE
;
273 return ft
->audit((void *)plug
, userctx
);
277 _kdc_plugin_audit(astgs_request_t r
)
279 krb5_error_code ret
= KRB5_PLUGIN_NO_HANDLE
;
282 ret
= _krb5_plugin_run_f(r
->context
, &kdc_plugin_data
, 0, r
, audit
);
284 if (ret
== KRB5_PLUGIN_NO_HANDLE
)
290 KDC_LIB_FUNCTION
uintptr_t KDC_LIB_CALL
291 kdc_get_instance(const char *libname
)
293 static const char *instance
= "libkdc";
295 if (strcmp(libname
, "kdc") == 0)
296 return (uintptr_t)instance
;
297 else if (strcmp(libname
, "hdb") == 0)
298 return hdb_get_instance(libname
);
299 else if (strcmp(libname
, "krb5") == 0)
300 return krb5_get_instance(libname
);
306 * Minimum API surface wrapper for libheimbase object types so it
307 * may remain a private interface, yet plugins can interact with
311 KDC_LIB_FUNCTION kdc_object_t KDC_LIB_CALL
312 kdc_object_alloc(size_t size
, const char *name
, kdc_type_dealloc dealloc
)
314 return heim_alloc(size
, name
, dealloc
);
317 KDC_LIB_FUNCTION kdc_object_t KDC_LIB_CALL
318 kdc_object_retain(kdc_object_t o
)
320 return heim_retain(o
);
323 KDC_LIB_FUNCTION
void KDC_LIB_CALL
324 kdc_object_release(kdc_object_t o
)
329 KDC_LIB_FUNCTION kdc_object_t KDC_LIB_CALL
330 kdc_bool_create(krb5_boolean v
)
332 return heim_bool_create(v
);
335 KDC_LIB_FUNCTION krb5_boolean KDC_LIB_CALL
336 kdc_bool_get_value(kdc_object_t o
)
338 return heim_bool_val(o
);
341 struct kdc_array_iterator_trampoline_data
{
342 kdc_array_iterator_t iter
;
347 * Calling convention shim to avoid needing to update all internal
348 * consumers of heim_array_iterate_f()
351 _kdc_array_iterator_trampoline(kdc_object_t o
, void *data
, int *stop
)
353 struct kdc_array_iterator_trampoline_data
*t
= data
;
355 t
->iter(o
, t
->data
, stop
);
358 KDC_LIB_FUNCTION
void KDC_LIB_CALL
359 kdc_array_iterate(kdc_array_t a
, void *d
, kdc_array_iterator_t iter
)
361 struct kdc_array_iterator_trampoline_data t
;
366 heim_array_iterate_f((heim_array_t
)a
, &t
, _kdc_array_iterator_trampoline
);
369 KDC_LIB_FUNCTION
size_t KDC_LIB_CALL
370 kdc_array_get_length(kdc_array_t a
)
372 return heim_array_get_length((heim_array_t
)a
);
375 KDC_LIB_FUNCTION kdc_object_t KDC_LIB_CALL
376 kdc_array_get_value(heim_array_t a
, size_t i
)
378 return heim_array_get_value((heim_array_t
)a
, i
);
381 KDC_LIB_FUNCTION kdc_object_t KDC_LIB_CALL
382 kdc_array_copy_value(heim_array_t a
, size_t i
)
384 return heim_array_copy_value((heim_array_t
)a
, i
);
387 KDC_LIB_FUNCTION kdc_string_t KDC_LIB_CALL
388 kdc_string_create(const char *s
)
390 return (kdc_string_t
)heim_string_create(s
);
393 KDC_LIB_FUNCTION
const char * KDC_LIB_CALL
394 kdc_string_get_utf8(kdc_string_t s
)
396 return heim_string_get_utf8((heim_string_t
)s
);
399 KDC_LIB_FUNCTION kdc_data_t
400 kdc_data_create(const void *d
, size_t len
)
402 return (kdc_data_t
)heim_data_create(d
, len
);
405 KDC_LIB_FUNCTION
const krb5_data
* KDC_LIB_CALL
406 kdc_data_get_data(kdc_data_t d
)
408 return heim_data_get_data((heim_data_t
)d
);
411 KDC_LIB_FUNCTION kdc_number_t KDC_LIB_CALL
412 kdc_number_create(int64_t v
)
414 return (kdc_number_t
)heim_number_create(v
);
417 KDC_LIB_FUNCTION
int64_t KDC_LIB_CALL
418 kdc_number_get_value(kdc_number_t n
)
420 return heim_number_get_long((heim_number_t
)n
);
427 KDC_LIB_FUNCTION krb5_error_code KDC_LIB_CALL
428 kdc_request_add_reply_padata(astgs_request_t r
, PA_DATA
*md
)
430 heim_assert(r
->rep
.padata
!= NULL
, "reply padata not allocated");
431 return add_METHOD_DATA(r
->rep
.padata
, md
);
434 KDC_LIB_FUNCTION krb5_error_code KDC_LIB_CALL
435 kdc_request_add_encrypted_padata(astgs_request_t r
, PA_DATA
*md
)
437 if (r
->ek
.encrypted_pa_data
== NULL
) {
438 r
->ek
.encrypted_pa_data
= calloc(1, sizeof *(r
->ek
.encrypted_pa_data
));
439 if (r
->ek
.encrypted_pa_data
== NULL
) {
444 return add_METHOD_DATA(r
->ek
.encrypted_pa_data
, md
);
447 KDC_LIB_FUNCTION krb5_error_code KDC_LIB_CALL
448 kdc_request_add_pac_buffer(astgs_request_t r
,
455 if (r
->pac
== NULL
) {
456 ret
= krb5_pac_init(r
->context
, &pac
);
460 pac
= heim_retain(r
->pac
);
462 ret
= krb5_pac_add_buffer(r
->context
, pac
, pactype
, d
);
463 if (ret
== 0 && r
->pac
== NULL
)
471 #undef _KDC_REQUEST_GET_ACCESSOR
472 #define _KDC_REQUEST_GET_ACCESSOR(R, T, f) \
473 KDC_LIB_FUNCTION T KDC_LIB_CALL \
474 kdc_request_get_ ## f(R r) \
479 #undef _KDC_REQUEST_SET_ACCESSOR
480 #define _KDC_REQUEST_SET_ACCESSOR(R, T, f) \
481 KDC_LIB_FUNCTION void KDC_LIB_CALL \
482 kdc_request_set_ ## f(R r, T v) \
487 #undef _KDC_REQUEST_GET_ACCESSOR_PTR
488 #define _KDC_REQUEST_GET_ACCESSOR_PTR(R, T, f) \
489 KDC_LIB_FUNCTION const T KDC_LIB_CALL \
490 kdc_request_get_ ## f(R r) \
495 #undef _KDC_REQUEST_SET_ACCESSOR_PTR
496 #define _KDC_REQUEST_SET_ACCESSOR_PTR(R, T, t, f) \
497 KDC_LIB_FUNCTION krb5_error_code KDC_LIB_CALL \
498 kdc_request_set_ ## f(R r, const T v) \
500 krb5_error_code ret; \
506 ret = copy_##t(v, &tmp); \
518 #undef _KDC_REQUEST_GET_ACCESSOR_STRUCT
519 #define _KDC_REQUEST_GET_ACCESSOR_STRUCT(R, T, f) \
520 KDC_LIB_FUNCTION const T * KDC_LIB_CALL \
521 kdc_request_get_ ## f(R r) \
526 #undef _KDC_REQUEST_SET_ACCESSOR_STRUCT
527 #define _KDC_REQUEST_SET_ACCESSOR_STRUCT(R, T, t, f) \
528 KDC_LIB_FUNCTION krb5_error_code KDC_LIB_CALL \
529 kdc_request_set_ ## f(R r, const T *v) \
531 krb5_error_code ret; \
536 else if (v == &r->f) \
539 ret = copy_##t(v, &tmp); \
549 static krb5_error_code
550 copy_string_ptr(const char *src
, char **dst
)
560 free_string_ptr(char *s
)
565 static krb5_error_code
566 copy_Principal_ptr(krb5_const_principal src
, krb5_principal
*dst
)
573 p
= calloc(1, sizeof(*p
));
577 ret
= copy_Principal(src
, p
);
587 free_Principal_ptr(krb5_principal p
)
595 static krb5_error_code
596 copy_pac(const struct krb5_pac_data
*src
, struct krb5_pac_data
**dst
)
598 /* FIXME use heim_copy() when it exists */
599 *dst
= (krb5_pac
)heim_retain((heim_object_t
)src
);
604 free_pac(struct krb5_pac_data
*o
)
609 static krb5_error_code
610 copy_keyblock(const EncryptionKey
*src
, EncryptionKey
*dst
)
612 return copy_EncryptionKey(src
, dst
);
616 free_keyblock(EncryptionKey
*key
)
618 krb5_free_keyblock_contents(NULL
, key
);
621 #undef HEIMDAL_KDC_KDC_ACCESSORS_H
622 #include "kdc-accessors.h"
624 #undef _KDC_REQUEST_GET_ACCESSOR
625 #undef _KDC_REQUEST_SET_ACCESSOR
627 #undef _KDC_REQUEST_GET_ACCESSOR_PTR
628 #undef _KDC_REQUEST_SET_ACCESSOR_PTR
629 #define _KDC_REQUEST_SET_ACCESSOR_PTR(R, T, t, f) \
631 _kdc_request_set_ ## f ## _nocopy(R r, T *v) \
640 #undef _KDC_REQUEST_GET_ACCESSOR_STRUCT
641 #undef _KDC_REQUEST_SET_ACCESSOR_STRUCT
642 #define _KDC_REQUEST_SET_ACCESSOR_STRUCT(R, T, t, f) \
644 _kdc_request_set_ ## f ## _nocopy(R r, T *v) \
650 memset(v, 0, sizeof(*v)); \
653 #undef HEIMDAL_KDC_KDC_ACCESSORS_H
654 #include "kdc-accessors.h"