#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <locale.h>
#include <errno.h>
#include <krb5.h>
#include <profile.h>
#include <com_err.h>
struct profile_string_list {
char **list;
int num;
int max;
};
static errcode_t
init_list(struct profile_string_list *list)
{
list->num = 0;
list->max = 10;
list->list = malloc(list->max * sizeof (char *));
if (list->list == NULL)
return (ENOMEM);
list->list[0] = NULL;
return (0);
}
static void
end_list(struct profile_string_list *list, char ***ret_list)
{
if (list == NULL)
return;
if (ret_list) {
*ret_list = list->list;
return;
} else
profile_free_list(list->list);
list->num = list->max = 0;
list->list = NULL;
}
static errcode_t
add_to_list(struct profile_string_list *list, const char *str)
{
char *newstr, **newlist;
int newmax;
if (list->num + 1 >= list->max) {
newmax = list->max + 10;
newlist = realloc(list->list, newmax * sizeof (char *));
if (newlist == NULL)
return (ENOMEM);
list->max = newmax;
list->list = newlist;
}
newstr = strdup(str);
if (newstr == NULL)
return (ENOMEM);
list->list[list->num++] = newstr;
list->list[list->num] = NULL;
return (0);
}
static void
usage()
{
(void) fprintf(stderr, gettext("kconf -f <file> -r <realm> "
"-k <kdc[,kdc]> -m <master_kdc>\n -p <kpasswd_protocol> "
"-d <domain>\n"));
exit(1);
}
int
main(int argc, char **argv)
{
profile_t profile;
errcode_t code;
int c;
char *realm, *kdcs, *master, *domain, *token, *lasts;
char *file, **ret_values = NULL;
boolean_t set_change = FALSE;
struct profile_string_list values;
file = NULL;
domain = NULL;
master = NULL;
kdcs = NULL;
realm = NULL;
(void) setlocale(LC_ALL, "");
#if !defined(TEXT_DOMAIN)
#define TEXT_DOMAIN "SYS_TEST"
#endif
(void) textdomain(TEXT_DOMAIN);
while ((c = getopt(argc, argv, "f:r:k:a:s:p:d:m:")) != -1) {
switch (c) {
case 'f':
file = optarg;
break;
case 'r':
realm = optarg;
break;
case 'k':
kdcs = optarg;
break;
case 'm':
master = optarg;
break;
case 'p':
if (strcmp(optarg, "SET_CHANGE") == 0)
set_change = TRUE;
break;
case 'd':
domain = optarg;
break;
default:
usage();
break;
}
}
code = __profile_init(file, &profile);
if (code != 0) {
fprintf(stderr, gettext("Wasn't able to initialize profile\n"));
exit(code);
}
if (code = init_list(&values)) {
fprintf(stderr, gettext("Can not initialize list %d\n"), code);
goto error;
}
token = strtok_r(kdcs, ",", &lasts);
do {
if (token != NULL) {
code = add_to_list(&values, token);
if (code != 0) {
fprintf(stderr, gettext("Can not add to list "
"%d\n"), code);
goto error;
}
} else {
fprintf(stderr, gettext("Couldn't parse kdc list %d\n"),
code);
goto error;
}
} while ((token = strtok_r(NULL, ",", &lasts)) != NULL);
end_list(&values, &ret_values);
code = __profile_add_realm(profile, realm, master, ret_values,
set_change, TRUE);
if (code != 0) {
fprintf(stderr, gettext("Wasn't able to add realm "
"information\n"));
goto error;
}
code = __profile_add_domain_mapping(profile, domain, realm);
if (code != 0) {
fprintf(stderr, gettext("Wasn't able to add domain mapping\n"));
goto error;
}
error:
if (ret_values != NULL)
profile_free_list(ret_values);
if ((code = __profile_release(profile)) != 0)
__profile_abandon(profile);
return (code);
}