2 * Retrieve configuration settings from krb5.conf.
4 * Provided here are functions to retrieve boolean, numeric, and string
5 * settings from krb5.conf. This wraps the somewhat awkward
6 * krb5_appdefaults_* functions.
8 * Written by Russ Allbery <eagle@eyrie.org>
10 * The Board of Trustees of the Leland Stanford Junior University
12 * See LICENSE for licensing terms.
16 #include <portable/krb5.h>
17 #include <portable/system.h>
21 #include <plugin/internal.h>
22 #include <util/macros.h>
24 /* The representation of the realm differs between MIT and Kerberos. */
25 #ifdef HAVE_KRB5_REALM
26 typedef krb5_realm realm_type;
28 typedef krb5_data *realm_type;
33 * Obtain the default realm and translate it into the format required by
34 * krb5_appdefault_*. This is obnoxious for MIT Kerberos, which returns the
35 * default realm as a string but expects the realm as a krb5_data type when
36 * calling krb5_appdefault_*.
38 #ifdef HAVE_KRB5_REALM
41 default_realm(krb5_context ctx)
46 code = krb5_get_default_realm(ctx, &realm);
52 #else /* !HAVE_KRB5_REALM */
55 default_realm(krb5_context ctx)
59 krb5_data *realm_data;
61 realm_data = calloc(1, sizeof(krb5_data));
62 if (realm_data == NULL)
64 code = krb5_get_default_realm(ctx, &realm);
69 realm_data->magic = KV5M_DATA;
70 realm_data->data = strdup(realm);
71 if (realm_data->data == NULL) {
73 krb5_free_default_realm(ctx, realm);
76 realm_data->length = strlen(realm);
77 krb5_free_default_realm(ctx, realm);
81 #endif /* !HAVE_KRB5_REALM */
85 * Free the default realm data in whatever form it was generated for the calls
86 * to krb5_appdefault_*.
88 #ifdef HAVE_KRB5_REALM
91 free_default_realm(krb5_context ctx UNUSED, realm_type realm)
93 krb5_free_default_realm(ctx, realm);
96 #else /* !HAVE_KRB5_REALM */
99 free_default_realm(krb5_context ctx UNUSED, realm_type realm)
105 #endif /* !HAVE_KRB5_REALM */
109 * Load a boolean option from Kerberos appdefaults. Takes the Kerberos
110 * context, the option, and the result location.
113 strength_config_boolean(krb5_context ctx, const char *opt, bool *result)
119 * The MIT version of krb5_appdefault_boolean takes an int * and the
120 * Heimdal version takes a krb5_boolean *, so hope that Heimdal always
121 * defines krb5_boolean to int or this will require more portability work.
123 realm = default_realm(ctx);
124 krb5_appdefault_boolean(ctx, "krb5-strength", realm, opt, *result, &tmp);
126 free_default_realm(ctx, realm);
131 * Load a number option from Kerberos appdefaults. Takes the Kerberos
132 * context, the option, and the result location. The native interface doesn't
133 * support numbers, so we actually read a string and then convert.
136 strength_config_number(krb5_context ctx, const char *opt, long *result)
143 /* Obtain the setting in string form from [appdefaults]. */
144 realm = default_realm(ctx);
145 krb5_appdefault_string(ctx, "krb5-strength", realm, opt, "", &tmp);
146 free_default_realm(ctx, realm);
149 * If we found anything, convert it to a number. Currently, we ignore
152 if (tmp != NULL && tmp[0] != '\0') {
154 value = strtol(tmp, &end, 10);
155 if (errno == 0 && *end == '\0')
159 krb5_free_string(ctx, tmp);
164 * Load a string option from Kerberos appdefaults. Takes the Kerberos
165 * context, the option, and the result location.
167 * This requires an annoying workaround because one cannot specify a default
168 * value of NULL with MIT Kerberos, since MIT Kerberos unconditionally calls
169 * strdup on the default value. There's also no way to determine if memory
170 * allocation failed while parsing or while setting the default value, so we
171 * don't return an error code.
174 strength_config_string(krb5_context ctx, const char *opt, char **result)
179 /* Obtain the string from [appdefaults]. */
180 realm = default_realm(ctx);
181 krb5_appdefault_string(ctx, "krb5-strength", realm, opt, "", &value);
182 free_default_realm(ctx, realm);
184 /* If we got something back, store it in result. */
186 if (value[0] != '\0') {
188 *result = strdup(value);
190 krb5_free_string(ctx, value);