2014-03-10 13:51:48 +00:00
|
|
|
/*
|
2018-05-15 15:04:23 +00:00
|
|
|
* Copyright (C) 2014-2018 Tobias Brunner
|
2018-05-23 14:04:50 +00:00
|
|
|
* HSR Hochschule fuer Technik Rapperswil
|
2014-03-10 13:51:48 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation; either version 2 of the License, or (at your
|
|
|
|
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <library.h>
|
|
|
|
#include <settings/settings_types.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Defined in libstrongswan but not part of the public API
|
|
|
|
*/
|
|
|
|
bool settings_parser_parse_file(void *this, char *name);
|
|
|
|
|
|
|
|
/**
|
2018-05-15 15:04:23 +00:00
|
|
|
* Produce indentation for the given level
|
2014-03-10 13:51:48 +00:00
|
|
|
*/
|
2018-05-15 15:04:23 +00:00
|
|
|
static void get_indent(char indent[BUF_LEN], int level)
|
2014-03-10 13:51:48 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2018-05-15 15:04:23 +00:00
|
|
|
for (i = 0; i < level * 2 && i < BUF_LEN - 2; i += 2)
|
2014-03-10 13:51:48 +00:00
|
|
|
{
|
|
|
|
indent[i ] = ' ';
|
|
|
|
indent[i+1] = ' ';
|
|
|
|
}
|
|
|
|
indent[i] = '\0';
|
2018-05-15 15:04:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Recursively print the section and all subsections/settings
|
|
|
|
*/
|
|
|
|
static void print_section(section_t *section, int level)
|
|
|
|
{
|
|
|
|
section_t *sub;
|
|
|
|
section_ref_t *ref;
|
|
|
|
kv_t *kv;
|
|
|
|
char indent[BUF_LEN];
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
get_indent(indent, level);
|
2014-03-10 13:51:48 +00:00
|
|
|
|
|
|
|
for (i = 0; i < array_count(section->kv_order); i++)
|
|
|
|
{
|
|
|
|
array_get(section->kv_order, i, &kv);
|
|
|
|
printf("%s%s = %s\n", indent, kv->key, kv->value);
|
|
|
|
}
|
|
|
|
for (i = 0; i < array_count(section->sections_order); i++)
|
|
|
|
{
|
|
|
|
array_get(section->sections_order, i, &sub);
|
2018-05-15 15:04:23 +00:00
|
|
|
printf("%s%s", indent, sub->name);
|
|
|
|
if (array_count(sub->references))
|
|
|
|
{
|
|
|
|
for (j = 0; j < array_count(sub->references); j++)
|
|
|
|
{
|
|
|
|
array_get(sub->references, j, &ref);
|
|
|
|
printf("%s%s", j == 0 ? " : " : ", ", ref->name);
|
|
|
|
}
|
|
|
|
}
|
2018-09-17 15:43:18 +00:00
|
|
|
printf(" {\n");
|
2014-03-10 13:51:48 +00:00
|
|
|
print_section(sub, level + 1);
|
|
|
|
printf("%s}\n", indent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-15 15:04:23 +00:00
|
|
|
/**
|
|
|
|
* Recursively print a given section and all subsections/settings
|
|
|
|
*/
|
|
|
|
static void print_settings_section(settings_t *settings, char *section,
|
|
|
|
int level)
|
|
|
|
{
|
|
|
|
enumerator_t *enumerator;
|
|
|
|
char indent[BUF_LEN], buf[BUF_LEN], *key, *value;
|
|
|
|
|
|
|
|
get_indent(indent, level);
|
|
|
|
|
|
|
|
enumerator = settings->create_key_value_enumerator(settings, section);
|
|
|
|
while (enumerator->enumerate(enumerator, &key, &value))
|
|
|
|
{
|
|
|
|
printf("%s%s = %s\n", indent, key, value);
|
|
|
|
|
|
|
|
}
|
|
|
|
enumerator->destroy(enumerator);
|
|
|
|
|
|
|
|
enumerator = settings->create_section_enumerator(settings, section);
|
|
|
|
while (enumerator->enumerate(enumerator, &key))
|
|
|
|
{
|
|
|
|
printf("%s%s {\n", indent, key);
|
|
|
|
snprintf(buf, sizeof(buf), "%s%s%s", section,
|
|
|
|
strlen(section) ? "." : "", key);
|
|
|
|
print_settings_section(settings, buf, level + 1);
|
|
|
|
printf("%s}\n", indent);
|
|
|
|
}
|
|
|
|
enumerator->destroy(enumerator);
|
|
|
|
}
|
|
|
|
|
2014-03-10 13:51:48 +00:00
|
|
|
static void usage(FILE *out, char *name)
|
|
|
|
{
|
|
|
|
fprintf(out, "Test strongswan.conf parser\n\n");
|
|
|
|
fprintf(out, "%s [OPTIONS]\n\n", name);
|
|
|
|
fprintf(out, "Options:\n");
|
|
|
|
fprintf(out, " -h, --help print this help.\n");
|
|
|
|
fprintf(out, " -d, --debug enables debugging of the parser.\n");
|
2018-05-15 15:04:23 +00:00
|
|
|
fprintf(out, " -r, --resolve displays the settings with references/redefines resolved.\n");
|
2014-03-10 13:51:48 +00:00
|
|
|
fprintf(out, " -f, --file=FILE config file to load (default STDIN).\n");
|
|
|
|
fprintf(out, "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
char *file = NULL;
|
2018-05-15 15:04:23 +00:00
|
|
|
bool resolve = FALSE;
|
2014-03-10 13:51:48 +00:00
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
struct option long_opts[] = {
|
|
|
|
{"help", no_argument, NULL, 'h' },
|
|
|
|
{"debug", no_argument, NULL, 'd' },
|
|
|
|
{"file", required_argument, NULL, 'f' },
|
2018-05-15 15:04:23 +00:00
|
|
|
{"resolve", no_argument, NULL, 'r' },
|
2014-03-10 13:51:48 +00:00
|
|
|
{0,0,0,0 },
|
|
|
|
};
|
2018-05-15 15:04:23 +00:00
|
|
|
switch (getopt_long(argc, argv, "hdf:r", long_opts, NULL))
|
2014-03-10 13:51:48 +00:00
|
|
|
{
|
|
|
|
case EOF:
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
usage(stdout, argv[0]);
|
|
|
|
return 0;
|
|
|
|
case 'd':
|
|
|
|
setenv("DEBUG_SETTINGS_PARSER", "1", TRUE);
|
|
|
|
continue;
|
|
|
|
case 'f':
|
|
|
|
file = optarg;
|
|
|
|
continue;
|
2018-05-15 15:04:23 +00:00
|
|
|
case 'r':
|
|
|
|
resolve = TRUE;
|
|
|
|
continue;
|
2014-03-10 13:51:48 +00:00
|
|
|
default:
|
|
|
|
usage(stderr, argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-05-15 15:04:23 +00:00
|
|
|
/* don't load strongswan.conf */
|
|
|
|
library_init("", "settings-test");
|
|
|
|
atexit(library_deinit);
|
|
|
|
|
|
|
|
dbg_default_set_level(3);
|
|
|
|
|
2014-03-10 13:51:48 +00:00
|
|
|
if (file)
|
|
|
|
{
|
2018-05-15 15:04:23 +00:00
|
|
|
if (resolve)
|
|
|
|
{
|
|
|
|
settings_t *settings = settings_create(file);
|
|
|
|
|
|
|
|
print_settings_section(settings, "", 0);
|
|
|
|
|
|
|
|
settings->destroy(settings);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
section_t *root = settings_section_create(strdup("root"));
|
2014-03-10 13:51:48 +00:00
|
|
|
|
2018-05-15 15:04:23 +00:00
|
|
|
settings_parser_parse_file(root, file);
|
2014-03-10 13:51:48 +00:00
|
|
|
|
2018-05-15 15:04:23 +00:00
|
|
|
print_section(root, 0);
|
2014-03-10 13:51:48 +00:00
|
|
|
|
2018-05-15 15:04:23 +00:00
|
|
|
settings_section_destroy(root, NULL);
|
|
|
|
}
|
2014-03-10 13:51:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
usage(stderr, argv[0]);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|