#include <ctype.h>
#include "prof_int.h"
#include "k5-int.h"
errcode_t
__profile_iter_name_value(profile_t profile, char *section, char *key,
char ***retvals)
{
const char *hierarchy[4];
errcode_t code, code2;
char *name = NULL, *value = NULL, **ret_values = NULL;
void *state = NULL;
struct profile_string_list values;
boolean_t found = FALSE;
hierarchy[0] = section;
hierarchy[1] = NULL;
if (code = init_list(&values))
return (code);
code = profile_iterator_create(profile, hierarchy,
PROFILE_ITER_LIST_SECTION, &state);
while (code == 0) {
code = profile_iterator(&state, &name, &value);
if (code == 0 && name != NULL) {
if ((key == NULL) || (strcmp(value, key) == 0)) {
code2 = add_to_list(&values, name);
if (code2 != 0) {
end_list(&values, &ret_values);
profile_free_list(ret_values);
code2 = code;
goto cleanup;
}
found = TRUE;
}
}
if (name != NULL) {
profile_release_string(name);
name = NULL;
}
if (value != NULL) {
profile_release_string(value);
value = NULL;
}
}
code = 0;
if (found == TRUE)
end_list(&values, &ret_values);
cleanup:
if (state != NULL)
profile_iterator_free(&state);
if (name != NULL)
profile_release_string(name);
if (value != NULL)
profile_release_string(value);
*retvals = ret_values;
return (code);
}
errcode_t
__profile_get_domain_realm(profile_t profile, char *realm, char ***domains)
{
if (profile == NULL || realm == NULL || domains == NULL)
return (EINVAL);
return (__profile_iter_name_value(profile, "domain_realm", realm,
domains));
}
errcode_t
__profile_set_appdefaults(profile_t profile)
{
const char *hierarchy[4];
errcode_t code;
if (profile == NULL)
return (EINVAL);
hierarchy[0] = "appdefaults";
hierarchy[1] = "kinit";
hierarchy[3] = NULL;
hierarchy[2] = "renewable";
(void) profile_clear_relation(profile, hierarchy);
code = profile_add_relation(profile, hierarchy, "true");
if (code != 0)
return (code);
hierarchy[2] = "forwardable";
(void) profile_clear_relation(profile, hierarchy);
code = profile_add_relation(profile, hierarchy, "true");
return (code);
}
errcode_t
__profile_set_logging(profile_t profile)
{
const char *hierarchy[4];
errcode_t code;
if (profile == NULL)
return (EINVAL);
hierarchy[0] = "logging";
hierarchy[2] = NULL;
hierarchy[3] = NULL;
hierarchy[1] = "default";
(void) profile_clear_relation(profile, hierarchy);
code = profile_add_relation(profile, hierarchy,
"FILE:/var/krb5/kdc.log");
if (code != 0)
return (code);
hierarchy[1] = "kdc";
(void) profile_clear_relation(profile, hierarchy);
code = profile_add_relation(profile, hierarchy,
"FILE:/var/krb5/kdc.log");
if (code != 0)
return (code);
hierarchy[1] = "kdc_rotate";
hierarchy[2] = "period";
(void) profile_clear_relation(profile, hierarchy);
code = profile_add_relation(profile, hierarchy, "1d");
if (code != 0)
return (code);
hierarchy[2] = "versions";
(void) profile_clear_relation(profile, hierarchy);
code = profile_add_relation(profile, hierarchy, "10");
return (code);
}
errcode_t
__profile_set_libdefaults(profile_t profile, char *realm)
{
const char *hierarchy[4];
errcode_t code;
if (profile == NULL || realm == NULL)
return (EINVAL);
hierarchy[0] = "libdefaults";
hierarchy[1] = "default_realm";
hierarchy[2] = NULL;
(void) profile_clear_relation(profile, hierarchy);
code = profile_add_relation(profile, hierarchy, realm);
return (code);
}
errcode_t
__profile_set_kdc(profile_t profile, char *realm, char *kdc,
boolean_t overwrite)
{
const char *hierarchy[4];
errcode_t code;
if (profile == NULL || realm == NULL || kdc == NULL)
return (EINVAL);
hierarchy[0] = "realms";
hierarchy[1] = realm;
hierarchy[3] = NULL;
hierarchy[2] = "kdc";
if (overwrite == TRUE) {
(void) profile_clear_relation(profile, hierarchy);
}
code = profile_add_relation(profile, hierarchy, kdc);
return (code);
}
errcode_t
__profile_release(profile_t profile)
{
prf_file_t p, next;
errcode_t code;
if (profile == NULL || profile->magic != PROF_MAGIC_PROFILE)
return (EINVAL);
for (p = profile->first_file; p; p = next) {
next = p->next;
if ((code = profile_close_file(p)) != 0)
return (code);
}
profile->magic = 0;
free(profile);
return (0);
}
void
__profile_abandon(profile_t profile)
{
profile_abandon(profile);
}
errcode_t
__profile_add_domain_mapping(profile_t profile, char *domain, char *realm)
{
const char *hierarchy[4];
errcode_t code = 0;
if (profile == NULL || domain == NULL || realm == NULL)
return (EINVAL);
hierarchy[0] = "domain_realm";
hierarchy[1] = domain;
hierarchy[2] = NULL;
(void) profile_clear_relation(profile, hierarchy);
code = profile_add_relation(profile, hierarchy, realm);
return (code);
}
errcode_t
__profile_remove_domain_mapping(profile_t profile, char *realm)
{
const char *hierarchy[4];
errcode_t code;
char **domains = NULL, **domain = NULL;
if (profile == NULL || realm == NULL)
return (EINVAL);
hierarchy[0] = "domain_realm";
hierarchy[1] = NULL;
hierarchy[2] = NULL;
code = __profile_get_domain_realm(profile, realm, &domains);
if (code == 0 && domains != NULL) {
for (domain = domains; *domain; domain++) {
hierarchy[1] = *domain;
code = profile_clear_relation(profile, hierarchy);
if (code != 0)
goto error;
}
}
error:
if (domains != NULL)
profile_free_list(domains);
return (code);
}
errcode_t
__profile_get_realm_entry(profile_t profile, char *realm, char *name,
char ***ret_value)
{
const char *hierarchy[4];
errcode_t code;
char **values = NULL;
if (profile == NULL || realm == NULL || name == NULL ||
ret_value == NULL)
return (EINVAL);
hierarchy[0] = "realms";
hierarchy[1] = realm;
hierarchy[2] = name;
hierarchy[3] = NULL;
code = profile_get_values(profile, hierarchy, &values);
if (code == 0 && values != NULL)
*ret_value = values;
if (code == PROF_NO_RELATION)
code = 0;
return (code);
}
errcode_t
__profile_add_realm_entry(profile_t profile, char *realm, char *name,
char **values)
{
const char *hierarchy[4];
errcode_t code;
char **tvalue = NULL;
if (profile == NULL || realm == NULL || name == NULL || values == NULL)
return (EINVAL);
hierarchy[0] = "realms";
hierarchy[1] = realm;
hierarchy[2] = name;
hierarchy[3] = NULL;
(void) profile_clear_relation(profile, hierarchy);
for (tvalue = values; *tvalue; tvalue++) {
code = profile_add_relation(profile, hierarchy, *tvalue);
if (code != 0)
return (code);
}
return (0);
}
errcode_t
__profile_get_default_realm(profile_t profile, char **realm)
{
errcode_t code;
char *value = NULL;
if (profile == NULL || realm == NULL)
return (EINVAL);
code = profile_get_string(profile, "libdefaults", "default_realm", 0, 0,
&value);
if (code == 0 && value != NULL)
*realm = value;
if (code == PROF_NO_RELATION)
code = 0;
return (code);
}
errcode_t
__profile_get_realms(profile_t profile, char ***realms)
{
if (profile == NULL || realms == NULL)
return (EINVAL);
return (__profile_iter_name_value(profile, "realms", NULL, realms));
}
errcode_t
__profile_add_realm(profile_t profile, char *realm, char *master, char **kdcs,
boolean_t set_change, boolean_t default_realm)
{
const char *hierarchy[4];
errcode_t code;
boolean_t ow = TRUE;
char **tkdcs;
if (profile == NULL || realm == NULL || master == NULL || kdcs == NULL)
return (EINVAL);
if (default_realm == TRUE) {
if (code = __profile_set_libdefaults(profile, realm))
return (code);
}
hierarchy[0] = "realms";
hierarchy[1] = realm;
hierarchy[3] = NULL;
hierarchy[2] = "admin_server";
(void) profile_clear_relation(profile, hierarchy);
if (code = profile_add_relation(profile, hierarchy, master))
return (code);
if (set_change == TRUE) {
hierarchy[2] = "kpasswd_protocol";
(void) profile_clear_relation(profile, hierarchy);
code = profile_add_relation(profile, hierarchy, "SET_CHANGE");
if (code != 0)
return (code);
}
for (tkdcs = kdcs; *tkdcs; tkdcs++) {
if (code = __profile_set_kdc(profile, realm, *tkdcs, ow))
return (code);
ow = FALSE;
}
code = __profile_set_logging(profile);
if (code != 0)
return (code);
code = __profile_set_appdefaults(profile);
return (code);
}
errcode_t
__profile_remove_xrealm_mapping(profile_t profile, char *realm)
{
const char *hierarchy[4];
errcode_t code, code2, code3;
void *state = NULL, *state2 = NULL;
char *source = NULL, *dummy_val = NULL, *target = NULL;
char *inter = NULL;
if (profile == NULL || realm == NULL)
return (EINVAL);
hierarchy[0] = "capaths";
hierarchy[1] = realm;
hierarchy[2] = NULL;
hierarchy[3] = NULL;
code = profile_rename_section(profile, hierarchy, NULL);
hierarchy[1] = NULL;
code = profile_iterator_create(profile, hierarchy,
PROFILE_ITER_LIST_SECTION, &state);
while (code == 0) {
code = profile_iterator(&state, &source, &dummy_val);
if (code == 0 && source != NULL) {
hierarchy[1] = source;
code2 = profile_iterator_create(profile, hierarchy,
PROFILE_ITER_LIST_SECTION, &state2);
while (code2 == 0) {
code2 = profile_iterator(&state2, &target,
&inter);
if (code2 == 0 && target != NULL &&
inter != NULL) {
if (strcmp(realm, target) == 0 ||
strcmp(realm, inter) == 0) {
hierarchy[2] = target;
code3 =
profile_clear_relation(
profile, hierarchy);
if (code3 != 0) {
code = code3;
goto error;
}
}
}
if (target != NULL) {
profile_release_string(target);
target = NULL;
}
if (inter != NULL) {
profile_release_string(inter);
inter = NULL;
}
}
}
if (source != NULL) {
profile_release_string(source);
source = NULL;
}
if (dummy_val != NULL) {
profile_release_string(dummy_val);
dummy_val = NULL;
}
}
code = 0;
error:
if (state != NULL)
profile_iterator_free(&state);
if (state2 != NULL)
profile_iterator_free(&state2);
if (target != NULL)
profile_release_string(target);
if (inter != NULL)
profile_release_string(inter);
if (source != NULL)
profile_release_string(source);
if (dummy_val != NULL)
profile_release_string(dummy_val);
return (code);
}
errcode_t
__profile_remove_realm(profile_t profile, char *realm)
{
const char *hierarchy[4];
errcode_t code;
char *drealm = NULL;
if (profile == NULL || realm == NULL)
return (EINVAL);
hierarchy[0] = "libdefaults";
hierarchy[1] = "default_realm";
hierarchy[2] = NULL;
code = __profile_get_default_realm(profile, &drealm);
if (code != 0)
return (code);
else if (drealm != NULL) {
if (strcmp(drealm, realm) == 0) {
code = profile_clear_relation(profile, hierarchy);
if (code != 0) {
free(drealm);
return (code);
}
}
free(drealm);
}
hierarchy[0] = "realms";
hierarchy[1] = realm;
hierarchy[2] = NULL;
code = profile_rename_section(profile, hierarchy, NULL);
if (code != 0)
return (code);
code = __profile_remove_domain_mapping(profile, realm);
if (code != 0)
return (code);
code = __profile_remove_xrealm_mapping(profile, realm);
if (code != 0)
return (code);
return (0);
}
errcode_t
__profile_add_xrealm_mapping(profile_t profile, char *source, char *target,
char *inter)
{
const char *hierarchy[4];
errcode_t code;
if (profile == NULL || source == NULL || target == NULL ||
inter == NULL)
return (EINVAL);
hierarchy[0] = "capaths";
hierarchy[1] = source;
hierarchy[2] = target;
hierarchy[3] = NULL;
(void) profile_clear_relation(profile, hierarchy);
code = profile_add_relation(profile, hierarchy, inter);
return (code);
}
errcode_t
__profile_validate(profile_t profile, int *val_err, char **val)
{
errcode_t code;
int c;
boolean_t found = FALSE;
char *default_realm = NULL, **realms = NULL, *tr = NULL;
char **trealms = NULL, **domains = NULL, **ret_vals = NULL;
if (profile == NULL || val_err == NULL || val == NULL)
return (EINVAL);
*val_err = 0;
*val = NULL;
code = __profile_get_default_realm(profile, &default_realm);
if (code == 0 && default_realm != NULL) {
tr = default_realm;
while ((c = *tr++) != 0) {
if (islower(c)) {
*val_err = 1;
*val = strdup(default_realm);
if (*val == NULL)
code = ENOMEM;
goto cleanup;
}
}
} else if (code == 0 && default_realm == NULL) {
*val_err = 4;
goto cleanup;
} else
goto cleanup;
code = __profile_get_realms(profile, &realms);
if (code == 0 && realms != NULL) {
for (trealms = realms; *trealms; trealms++) {
tr = *trealms;
while ((c = *tr++) != 0) {
if (islower(c)) {
*val_err = 2;
*val = strdup(*trealms);
if (*val == NULL)
code = ENOMEM;
goto cleanup;
}
}
if (strcmp(default_realm, *trealms) == 0)
found = TRUE;
code = __profile_get_domain_realm(profile, *trealms,
&domains);
if (code == 0 && domains != NULL) {
profile_free_list(domains);
domains = NULL;
} else if (code == 0 && domains == NULL) {
*val_err = 6;
*val = strdup(*trealms);
if (*val == NULL)
code = ENOMEM;
goto cleanup;
} else
goto cleanup;
code = __profile_get_realm_entry(profile, *trealms,
"kdc", &ret_vals);
if (code == 0 && ret_vals != NULL) {
profile_free_list(ret_vals);
ret_vals = NULL;
} else if (code == 0 && ret_vals == NULL) {
*val_err = 7;
*val = strdup(*trealms);
if (*val == NULL)
code = ENOMEM;
goto cleanup;
} else
goto cleanup;
code = __profile_get_realm_entry(profile, *trealms,
"admin_server", &ret_vals);
if (code == 0 && ret_vals != NULL) {
profile_free_list(ret_vals);
ret_vals = NULL;
} else if (code == 0 && ret_vals == NULL) {
*val_err = 8;
*val = strdup(*trealms);
if (*val == NULL)
code = ENOMEM;
goto cleanup;
} else
goto cleanup;
}
if (found == FALSE) {
*val_err = 3;
*val = strdup(default_realm);
if (*val == NULL)
code = ENOMEM;
goto cleanup;
}
} else if (code == 0 && realms == NULL)
*val_err = 5;
cleanup:
if (realms != NULL)
profile_free_list(realms);
if (ret_vals != NULL)
profile_free_list(ret_vals);
if (default_realm != NULL)
profile_release_string(default_realm);
if (domains != NULL)
profile_free_list(domains);
return (code);
}
errcode_t
__profile_init(char *filename, profile_t *profile)
{
profile_filespec_t *filenames = NULL;
krb5_error_code ret = 0;
errcode_t code = 0;
int err = 0, fd;
mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
if (profile == NULL)
return (EINVAL);
if (filename != NULL) {
filenames = malloc(2 * sizeof (char *));
if (filenames == NULL)
return (ENOMEM);
filenames[0] = strdup(filename);
if (filenames[0] == NULL) {
free(filenames);
return (ENOMEM);
}
filenames[1] = NULL;
} else {
ret = krb5_get_default_config_files(&filenames);
if (ret != 0)
return (ret);
}
fd = open(*filenames, O_RDWR|O_CREAT|O_NOFOLLOW|O_NOLINKS, mode);
if (fd < 0) {
err = errno;
krb5_free_config_files(filenames);
return (err);
} else
close(fd);
code = profile_init((const_profile_filespec_t *)filenames, profile);
krb5_free_config_files(filenames);
return (code);
}