#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include "k5-int.h"
#include "pkinit.h"
static int _krb5_conf_boolean(const char *s);
static const char *const conf_yes[] = {
"y", "yes", "true", "t", "1", "on",
0,
};
static const char *const conf_no[] = {
"n", "no", "false", "nil", "0", "off",
0,
};
static int
_krb5_conf_boolean(const char *s)
{
const char *const *p;
for(p=conf_yes; *p; p++) {
if (strcasecmp(*p,s) == 0)
return 1;
}
for(p=conf_no; *p; p++) {
if (strcasecmp(*p,s) == 0)
return 0;
}
return 0;
}
krb5_error_code
pkinit_kdcdefault_strings(krb5_context context, const char *realmname,
const char *option, char ***ret_value)
{
profile_t profile = NULL;
const char *names[5];
char **values = NULL;
krb5_error_code retval;
if (context == NULL)
return KV5M_CONTEXT;
profile = context->profile;
if (realmname != NULL) {
names[0] = "realms";
names[1] = realmname;
names[2] = option;
names[3] = 0;
retval = profile_get_values(profile, names, &values);
if (retval == 0 && values != NULL)
goto goodbye;
}
names[0] = "kdcdefaults";
names[1] = option;
names[2] = 0;
retval = profile_get_values(profile, names, &values);
if (retval == 0 && values != NULL)
goto goodbye;
goodbye:
if (values == NULL)
retval = ENOENT;
*ret_value = values;
return retval;
}
krb5_error_code
pkinit_kdcdefault_string(krb5_context context, const char *realmname,
const char *option, char **ret_value)
{
krb5_error_code retval;
char **values = NULL;
retval = pkinit_kdcdefault_strings(context, realmname, option, &values);
if (retval)
return retval;
if (values[0] == NULL) {
retval = ENOENT;
} else {
*ret_value = strdup(values[0]);
if (*ret_value == NULL)
retval = ENOMEM;
}
profile_free_list(values);
return retval;
}
krb5_error_code
pkinit_kdcdefault_boolean(krb5_context context, const char *realmname,
const char *option, int default_value, int *ret_value)
{
char *string = NULL;
krb5_error_code retval;
retval = pkinit_kdcdefault_string(context, realmname, option, &string);
if (retval == 0) {
*ret_value = _krb5_conf_boolean(string);
free(string);
} else
*ret_value = default_value;
return 0;
}
krb5_error_code
pkinit_kdcdefault_integer(krb5_context context, const char *realmname,
const char *option, int default_value, int *ret_value)
{
char *string = NULL;
krb5_error_code retval;
retval = pkinit_kdcdefault_string(context, realmname, option, &string);
if (retval == 0) {
char *endptr;
long l;
l = strtol(string, &endptr, 0);
if (endptr == string)
*ret_value = default_value;
else
*ret_value = l;
free(string);
} else
*ret_value = default_value;
return 0;
}
krb5_error_code
pkinit_libdefault_strings(krb5_context context, const krb5_data *realm,
const char *option, char ***ret_value)
{
profile_t profile;
const char *names[5];
char **values = NULL;
krb5_error_code retval;
char realmstr[1024];
if (realm != NULL && realm->length > sizeof(realmstr)-1)
return EINVAL;
if (realm != NULL) {
strncpy(realmstr, realm->data, realm->length);
realmstr[realm->length] = '\0';
}
if (!context || (context->magic != KV5M_CONTEXT))
return KV5M_CONTEXT;
profile = context->profile;
if (realm != NULL) {
names[0] = "libdefaults";
names[1] = realmstr;
names[2] = option;
names[3] = 0;
retval = profile_get_values(profile, names, &values);
if (retval == 0 && values != NULL && values[0] != NULL)
goto goodbye;
names[0] = "realms";
names[1] = realmstr;
names[2] = option;
names[3] = 0;
retval = profile_get_values(profile, names, &values);
if (retval == 0 && values != NULL && values[0] != NULL)
goto goodbye;
}
names[0] = "libdefaults";
names[1] = option;
names[2] = 0;
retval = profile_get_values(profile, names, &values);
if (retval == 0 && values != NULL && values[0] != NULL)
goto goodbye;
goodbye:
if (values == NULL)
return ENOENT;
*ret_value = values;
return retval;
}
krb5_error_code
pkinit_libdefault_string(krb5_context context, const krb5_data *realm,
const char *option, char **ret_value)
{
krb5_error_code retval;
char **values = NULL;
retval = pkinit_libdefault_strings(context, realm, option, &values);
if (retval)
return retval;
if (values[0] == NULL) {
retval = ENOENT;
} else {
*ret_value = strdup(values[0]);
if (*ret_value == NULL)
retval = ENOMEM;
}
profile_free_list(values);
return retval;
}
krb5_error_code
pkinit_libdefault_boolean(krb5_context context, const krb5_data *realm,
const char *option, int default_value,
int *ret_value)
{
char *string = NULL;
krb5_error_code retval;
retval = pkinit_libdefault_string(context, realm, option, &string);
if (retval == 0) {
*ret_value = _krb5_conf_boolean(string);
free(string);
} else
*ret_value = default_value;
return 0;
}
krb5_error_code
pkinit_libdefault_integer(krb5_context context, const krb5_data *realm,
const char *option, int default_value,
int *ret_value)
{
char *string = NULL;
krb5_error_code retval;
retval = pkinit_libdefault_string(context, realm, option, &string);
if (retval == 0) {
char *endptr;
long l;
l = strtol(string, &endptr, 0);
if (endptr == string)
*ret_value = default_value;
else
*ret_value = l;
free(string);
}
return retval;
}