kconf.c revision c48f4690176e54195652a6ecbbe3014ee39e7341
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi/*
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * CDDL HEADER START
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi *
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * The contents of this file are subject to the terms of the
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * Common Development and Distribution License (the "License").
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * You may not use this file except in compliance with the License.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi *
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * or http://www.opensolaris.org/os/licensing.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * See the License for the specific language governing permissions
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * and limitations under the License.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi *
724365f7556fc4201fdb11766ebc6bd918523130sethg * When distributing Covered Code, include this CDDL HEADER in each
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
724365f7556fc4201fdb11766ebc6bd918523130sethg * If applicable, add the following below this CDDL HEADER, with the
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * fields enclosed by brackets "[]" replaced with your own identifying
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * information: Portions Copyright [yyyy] [name of copyright owner]
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi *
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * CDDL HEADER END
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi */
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi/*
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi */
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <stdio.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <stdlib.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <strings.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <locale.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <errno.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <krb5.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <profile.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <com_err.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindistruct profile_string_list {
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi char **list;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi int num;
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi int max;
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi};
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi/*
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * From prof_get.c as the following four functions are private in mech_krb5.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi */
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi/*
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi * Initialize the string list abstraction.
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi */
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindistatic errcode_t
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindiinit_list(struct profile_string_list *list)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi{
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi list->num = 0;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi list->max = 10;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi list->list = malloc(list->max * sizeof (char *));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if (list->list == NULL)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return (ENOMEM);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi list->list[0] = NULL;
724365f7556fc4201fdb11766ebc6bd918523130sethg return (0);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi}
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi/*
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * If re_list is non-NULL then pass the list header to the caller else free
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * the previously allocated list.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi */
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindistatic void
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindiend_list(struct profile_string_list *list, char ***ret_list)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi{
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if (list == NULL)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return;
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi if (ret_list) {
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi *ret_list = list->list;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi } else
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi profile_free_list(list->list);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi list->num = list->max = 0;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi list->list = NULL;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi}
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi/*
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * Add a string to the list.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi */
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindistatic errcode_t
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindiadd_to_list(struct profile_string_list *list, const char *str)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi{
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi char *newstr, **newlist;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi int newmax;
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if (list->num + 1 >= list->max) {
724365f7556fc4201fdb11766ebc6bd918523130sethg newmax = list->max + 10;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi newlist = realloc(list->list, newmax * sizeof (char *));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if (newlist == NULL)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return (ENOMEM);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi list->max = newmax;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi list->list = newlist;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi }
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi newstr = strdup(str);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if (newstr == NULL)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return (ENOMEM);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi list->list[list->num++] = newstr;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi list->list[list->num] = NULL;
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi return (0);
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi}
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindistatic void
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindiusage()
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi{
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi (void) fprintf(stderr, gettext("kconf -f <file> -r <realm> "
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi "-k <kdc[,kdc]> -m <master_kdc>\n -p <kpasswd_protocol> "
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi "-d <domain>\n"));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi exit(1);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi}
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindiint
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindimain(int argc, char **argv)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi{
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi profile_t profile;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi errcode_t code;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi char c, *realm, *kdcs, *master, *domain, *token, *lasts;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi char *file, **ret_values = NULL;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi boolean_t set_change = FALSE;
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi struct profile_string_list values;
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi
724365f7556fc4201fdb11766ebc6bd918523130sethg (void) setlocale(LC_ALL, "");
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#if !defined(TEXT_DOMAIN)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#define TEXT_DOMAIN "SYS_TEST"
724365f7556fc4201fdb11766ebc6bd918523130sethg#endif /* TEXT_DOMAIN */
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi
724365f7556fc4201fdb11766ebc6bd918523130sethg (void) textdomain(TEXT_DOMAIN);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi /*
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * kconf -f <file> -r <realm> -k <kdc[,kdc]> -m <master_kdc>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * -p <kpasswd_protocol> -d <domain>
724365f7556fc4201fdb11766ebc6bd918523130sethg */
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi while ((c = getopt(argc, argv, "f:r:k:a:s:p:d:m:")) != -1) {
724365f7556fc4201fdb11766ebc6bd918523130sethg switch (c) {
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi case 'f':
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi file = optarg;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi break;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi case 'r':
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi realm = optarg;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi break;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi 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);
/*
* Release profile, which will subsequently flush new profile to file.
* If this fails then at least free profile memory.
*/
if ((code = __profile_release(profile)) != NULL)
__profile_abandon(profile);
return (code);
}