21RCSID(
"$Id: 120af6aa734dd642bb5ab6f36431a57c50d3a39c $")
23#include <freeradius-devel/server/base.h>
24#include <freeradius-devel/server/cf_file.h>
25#include <freeradius-devel/server/cf_util.h>
26#include <freeradius-devel/server/main_config.h>
27#include <freeradius-devel/server/module.h>
28#include <freeradius-devel/server/module_rlm.h>
29#include <freeradius-devel/server/virtual_servers.h>
30#include <freeradius-devel/unlang/base.h>
31#include <freeradius-devel/util/atexit.h>
32#include <freeradius-devel/util/debug.h>
33#include <freeradius-devel/util/syserror.h>
36# include <freeradius-devel/tls/base.h>
37# include <freeradius-devel/tls/version.h>
40#include <json-c/json.h>
46#define EXIT_WITH_FAILURE \
48 rcode = EXIT_FAILURE; \
61#define quote_name(_t) fr_token_to_enum_str(_t)
62#define op_name(_op) (((unsigned int)(_op) < T_TOKEN_LAST && fr_tokens[(_op)]) ? fr_tokens[(_op)] : "?")
66 struct json_object *loc;
68 if (!filename && lineno == 0)
return NULL;
70 loc = json_object_new_object();
71 json_object_object_add(loc,
"filename", filename ? json_object_new_string(filename) : NULL);
72 json_object_object_add(loc,
"lineno", json_object_new_int(lineno));
78 struct json_object *o = json_object_new_object();
82 json_object_object_add(o,
"type", json_object_new_string(
"comment"));
84 json_object_object_add(o,
"location",
build_location(filename, lineno));
90 struct json_object *o = json_object_new_object();
93 json_object_object_add(o,
"type", json_object_new_string(
"pair"));
97 json_object_object_add(o,
"value",
value ? json_object_new_string(
value) : NULL);
106 struct json_object *o = json_object_new_object();
107 struct json_object *children = json_object_new_array();
110 json_object_object_add(o,
"type", json_object_new_string(
"section"));
112 json_object_object_add(o,
"name2", name2 ? json_object_new_string(name2) : NULL);
127 json_object_object_add(o,
"children", children);
133 FILE *fp = (rcode == 0) ? stdout : stderr;
135 fprintf(fp,
"Usage: radconf2json [options]\n"
136 " -d <raddb> Set raddb directory.\n"
137 " -D <dict> Set dictionary directory.\n"
138 " -n <name> Read <name>.conf instead of radiusd.conf.\n"
139 " -o <file> Write JSON to <file> (default stdout).\n"
140 " -x Enable debug output (repeatable).\n"
141 " -X Verbose debug output.\n"
146int main(
int argc,
char *argv[])
149 int rcode = EXIT_SUCCESS;
150 char const *output_file = NULL;
154 struct json_object *root = NULL;
174 while ((c = getopt(argc, argv,
"d:D:hn:o:xX")) != -1) {
192 output_file = optarg;
209 if (fr_openssl_init() < 0) {
243 PERROR(
"Failed to initialize the dictionaries");
274 char const *json_str =
275 json_object_to_json_string_ext(root, JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_NOSLASHESCAPE);
278 FILE *
out = fopen(output_file,
"w");
280 fprintf(stderr,
"Failed opening %s: %s\n", output_file,
fr_syserror(errno));
283 fputs(json_str,
out);
287 fputs(json_str, stdout);
293 if (root) json_object_put(root);
#define UNCONST(_type, _ptr)
Remove const qualification from a pointer.
#define NEVER_RETURNS
Should be placed before the function return type.
Common header for all CONF_* types.
Configuration AVP similar to a fr_pair_t.
A section grouping multiple CONF_PAIR.
bool cf_item_is_pair(CONF_ITEM const *ci)
Determine if CONF_ITEM is a CONF_PAIR.
fr_token_t cf_pair_attr_quote(CONF_PAIR const *pair)
Return the value (lhs) quoting of a pair.
char const * cf_section_name2(CONF_SECTION const *cs)
Return the second identifier of a CONF_SECTION.
char const * cf_section_name1(CONF_SECTION const *cs)
Return the first identifier of a CONF_SECTION.
CONF_SECTION * cf_item_to_section(CONF_ITEM const *ci)
Cast a CONF_ITEM to a CONF_SECTION.
bool cf_item_is_comment(CONF_ITEM const *ci)
Determine if CONF_ITEM is a CONF_COMMENT.
void cf_preserve_comments_set(bool preserve)
Control whether the CF parser preserves # ... comments.
CONF_COMMENT * cf_item_to_comment(CONF_ITEM const *ci)
Cast a CONF_ITEM to a CONF_COMMENT (asserts on type mismatch).
fr_token_t cf_pair_operator(CONF_PAIR const *pair)
Return the operator of a pair.
fr_token_t cf_pair_value_quote(CONF_PAIR const *pair)
Return the value (rhs) quoting of a pair.
bool cf_item_is_section(CONF_ITEM const *ci)
Determine if CONF_ITEM is a CONF_SECTION.
char const * cf_comment_text(CONF_COMMENT const *c)
CONF_PAIR * cf_item_to_pair(CONF_ITEM const *ci)
Cast a CONF_ITEM to a CONF_PAIR.
fr_token_t cf_section_name2_quote(CONF_SECTION const *cs)
Return the quoting of the name2 identifier.
char const * cf_pair_value(CONF_PAIR const *pair)
Return the value of a CONF_PAIR.
char const * cf_pair_attr(CONF_PAIR const *pair)
Return the attr of a CONF_PAIR.
#define cf_item_foreach(_parent, _iter)
Iterate over every child item of a CONF_SECTION (or any CONF_ITEM that has children).
void fr_talloc_fault_setup(void)
Register talloc fault handlers.
#define fr_exit_now(_x)
Exit without calling atexit() handlers, producing a log message in debug builds.
static NEVER_RETURNS void usage(void)
int fr_dict_internal_afrom_file(fr_dict_t **out, char const *dict_subdir, char const *dependent))
(Re-)Initialize the special internal dictionary
int fr_dict_read(fr_dict_t *dict, char const *dict_dir, char const *filename))
Read supplementary attribute definitions into an existing dictionary.
fr_dict_gctx_t * fr_dict_global_ctx_init(TALLOC_CTX *ctx, bool free_at_exit, char const *dict_dir))
Initialise the global protocol hashes.
static TALLOC_CTX * autofree
int unlang_global_init(void)
@ L_DST_STDERR
Log to stderr.
int main_config_free(main_config_t **config)
main_config_t * main_config_alloc(TALLOC_CTX *ctx)
Allocate a main_config_t struct, setting defaults.
void main_config_name_set_default(main_config_t *config, char const *name, bool overwrite_config)
Set the server name.
int main_config_init(main_config_t *config)
void main_config_confdir_set(main_config_t *config, char const *name)
Set the global radius config directory.
void main_config_dict_dir_set(main_config_t *config, char const *name)
Set the global dictionary directory.
Main server configuration.
int modules_rlm_init(void)
Initialise the module list structure.
static const conf_parser_t config[]
int main(int argc, char *argv[])
static struct json_object * build_section(CONF_SECTION const *cs)
static struct json_object * build_location(char const *filename, int lineno)
static struct json_object * build_pair(CONF_PAIR const *cp)
char const * radiusd_version
#define EXIT_WITH_FAILURE
static struct json_object * build_comment(CONF_COMMENT const *c)
int request_global_init(void)
void modules_init(char const *lib_dir)
Perform global initialisation for modules.
fr_log_dst_t dst
Log destination.
int fd
File descriptor to write messages to.
bool print_level
sometimes we don't want log levels printed
char const * fr_syserror(int num)
Guaranteed to be thread-safe version of strerror.
#define talloc_autofree_context
The original function is deprecated, so replace it with our version.
int fr_time_start(void)
Initialize the local time.
#define FR_DICTIONARY_FILE
#define FR_DICTIONARY_INTERNAL_DIR
void fr_perror(char const *fmt,...)
Print the current error to stderr with a prefix.
int fr_check_lib_magic(uint64_t magic)
Check if the application linking to the library has the correct magic number.
#define RADIUSD_VERSION_BUILD(_x)
Create a version string for a utility in the suite of FreeRADIUS utilities.
#define RADIUSD_MAGIC_NUMBER
static size_t char ** out
int virtual_servers_init(void)
Performs global initialisation for the virtual server code.