26RCSID(
"$Id: 637cc829fd89adb5ae38fc23556feff1c301da94 $")
28#include <freeradius-devel/util/conf.h>
29#include <freeradius-devel/util/syserror.h>
30#include <freeradius-devel/util/atexit.h>
31#include <freeradius-devel/util/dict_priv.h>
54#define DEBUG2(fmt, ...) if (fr_log_fp && (fr_debug_lvl > 2)) fprintf(fr_log_fp , fmt "\n", ## __VA_ARGS__)
55#define DEBUG(fmt, ...) if (fr_log_fp && (fr_debug_lvl > 1)) fprintf(fr_log_fp , fmt "\n", ## __VA_ARGS__)
56#define INFO(fmt, ...) if (fr_log_fp && (fr_debug_lvl > 0)) fprintf(fr_log_fp , fmt "\n", ## __VA_ARGS__)
61 fprintf(stderr,
"usage: radict [OPTS] [attribute...]\n");
62 fprintf(stderr,
" -A Export aliases.\n");
63 fprintf(stderr,
" -c Print out in CSV format.\n");
64 fprintf(stderr,
" -D <dictdir> Set main dictionary directory (defaults to " DICTDIR
").\n");
65 fprintf(stderr,
" -f Export dictionary definitions in the normal dictionary format\n");
66 fprintf(stderr,
" -F <format> Set output format. Use 'csv', 'full', or 'dictionary'\n");
67 fprintf(stderr,
" -E Export dictionary definitions.\n");
68 fprintf(stderr,
" -h Print help text.\n");
69 fprintf(stderr,
" -H Show the headers of each field.\n");
70 fprintf(stderr,
" -p <protocol> Set protocol by name\n");
71 fprintf(stderr,
" -r Write out attributes recursively.\n");
72 fprintf(stderr,
" -V Write out all attribute values.\n");
73 fprintf(stderr,
" -x Debugging mode.\n");
74 fprintf(stderr,
"\n");
75 fprintf(stderr,
"Very simple interface to extract attribute definitions from FreeRADIUS dictionaries\n");
78static int load_dicts(
char const *dict_dir,
char const *protocol)
84 INFO(
"Reading directory %s", dict_dir);
86 dir = opendir(dict_dir);
92 while ((dp = readdir(dir)) != NULL) {
93 struct stat stat_buff;
96 if (dp->d_name[0] ==
'.')
continue;
101 if (protocol && (strcmp(dp->d_name, protocol) != 0))
continue;
106 if (strcmp(dp->d_name,
"freeradius") == 0)
continue;
108 file_str = talloc_asprintf(NULL,
"%s/%s", dict_dir, dp->d_name);
110 if (stat(file_str, &stat_buff) == -1) {
121 if ((stat_buff.st_mode & S_IFMT) == S_IFDIR) {
123 struct stat dict_stat_buff;
126 dict_file = talloc_asprintf(NULL,
"%s/dictionary", file_str);
127 ret = stat(dict_file, &dict_stat_buff);
140 INFO(
"Loading dictionary: %s/dictionary", file_str);
191 if (!da->flags.is_alias) {
204 printf(
"%s,%s,%s,%d,%s,%s\n",
215 printf(
"%s\t%s\t%s\t%d\t%s\t%s\n",
237 str =
fr_asprintf(NULL,
"%s,%s,%s,%d,%s,%s,%s,%pV",
250 str =
fr_asprintf(NULL,
"%s\t%s\t%s\t%d\t%s\t%s\t%s\t%pV",
271 namespace = dict_attr_namespace(da);
295 if (!da->flags.is_root) {
296 if (low && ((uintptr_t)da < *low)) {
297 *low = (uintptr_t)da;
299 if (high && ((uintptr_t)da > *high)) {
300 *high = (uintptr_t)da;
306 if (
count) (*count)++;
313 len = talloc_array_length(children);
314 for (i = 0; i < len; i++) {
315 for (p = children[i]; p; p = p->next) {
325 if (low) *low = UINTPTR_MAX;
335int main(
int argc,
char *argv[])
337 char const *dict_dir = DICTDIR;
342 bool file_export =
false;
344 char const *protocol = NULL;
362 talloc_set_log_stderr();
367 while ((c = getopt(argc, argv,
"AcfF:ED:p:rVxhH")) != -1)
switch (c) {
385 if (strcmp(optarg,
"csv") == 0) {
388 }
else if (strcmp(optarg,
"full") == 0) {
391 }
else if (strncmp(optarg,
"dict", 4) == 0) {
395 fprintf(stderr,
"Invalid output format '%s'\n", optarg);
442 fr_perror(
"radict - Global context init failed");
450 fr_perror(
"radict - Loading internal dictionary failed");
460 fr_perror(
"radict - Loading dictionaries failed");
466 fr_perror(
"radict - No dictionaries loaded");
473 printf(
"Dictionary,OID,Attribute,ID,Type,Flags\n");
478 printf(
"Dictionary\tOID\tAttribute\tID\tType\tFlags\n");
501 DEBUG2(
"Memory allocd %zu (bytes)", talloc_total_size(*dict_p));
502 DEBUG2(
"Memory spread %zu (bytes)", (
size_t) (high - low));
520 if (argc == 0)
goto finish;
541 fprintf(stderr,
"Dictionary %s does not contain attribute %s\n",
547 if (!found) ret = 64;
int fr_atexit_global_setup(void)
Setup the atexit handler, should be called at the start of a program's execution.
int fr_atexit_global_trigger_all(void)
Cause all global free triggers to fire.
static TALLOC_CTX * autofree
int fr_fault_setup(TALLOC_CTX *ctx, char const *cmd, char const *program)
Registers signal handlers to execute panic_action on fatal signal.
#define fr_exit(_x)
Exit, producing a log message in debug builds.
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.
int fr_dict_internal_afrom_file(fr_dict_t **out, char const *internal_name, char const *dependent)
(Re-)Initialize the special internal dictionary
fr_dict_t const * fr_dict_by_da(fr_dict_attr_t const *da)
Attempt to locate the protocol dictionary containing an attribute.
void fr_dict_alias_export(FILE *fp, fr_dict_attr_t const *parent)
int fr_dict_protocol_afrom_file(fr_dict_t **out, char const *proto_name, char const *proto_dir, char const *dependent)
(Re)-initialize a protocol dictionary
fr_dict_attr_t const * fr_dict_root(fr_dict_t const *dict)
Return the root attribute of a dictionary.
fr_value_box_t const * value
Enum value (what name maps to).
int fr_dict_free(fr_dict_t **dict, char const *dependent)
Decrement the reference count on a previously loaded dictionary.
@ FR_DICT_ATTR_EXT_ENUMV
Enumeration values.
fr_dict_attr_t const * fr_dict_attr_by_oid(fr_dict_attr_err_t *err, fr_dict_attr_t const *parent, char const *oid))
Resolve an attribute using an OID string.
char const * name
Enum name.
void fr_dict_export(FILE *fp, fr_dict_t const *dict)
Export in the standard form: ATTRIBUTE name oid flags.
Value of an enumerated attribute.
static void * fr_dict_attr_ext(fr_dict_attr_t const *da, fr_dict_attr_ext_t ext)
fr_hash_table_t * value_by_name
Lookup an enumeration value by name.
Attribute extension - Holds enumeration values.
static fr_dict_attr_t const ** dict_attr_children(fr_dict_attr_t const *da)
void * fr_hash_table_iter_next(fr_hash_table_t *ht, fr_hash_iter_t *iter)
Iterate over entries in a hash table.
void * fr_hash_table_iter_init(fr_hash_table_t *ht, fr_hash_iter_t *iter)
Initialise an iterator.
Stores the state of the current iteration operation.
ssize_t fr_dict_attr_flags_print(fr_sbuff_t *out, fr_dict_t const *dict, fr_type_t type, fr_dict_attr_flags_t const *flags)
ssize_t fr_dict_attr_oid_print(fr_sbuff_t *out, fr_dict_attr_t const *ancestor, fr_dict_attr_t const *da, bool numeric)
static uint8_t depth(fr_minmax_heap_index_t i)
int strcasecmp(char *s1, char *s2)
char * fr_asprintf(TALLOC_CTX *ctx, char const *fmt,...)
Special version of asprintf which implements custom format specifiers.
int main(int argc, char *argv[])
static fr_dict_t ** dict_end
static int load_dicts(char const *dict_dir, char const *protocol)
static void raddict_export(uint64_t *count, uintptr_t *low, uintptr_t *high, fr_dict_t *dict)
static bool print_recursive
static fr_dict_t * dicts[255]
static bool print_headers
static void da_print_info(fr_dict_t const *dict, fr_dict_attr_t const *da, int depth)
static void _raddict_export(fr_dict_t const *dict, uint64_t *count, uintptr_t *low, uintptr_t *high, fr_dict_attr_t const *da, unsigned int lvl)
static const char * spaces
static radict_out_t output_format
#define fr_sbuff_start(_sbuff_or_marker)
#define FR_SBUFF_OUT(_start, _len_or_end)
fr_aka_sim_id_type_t type
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.
#define FR_DICTIONARY_FILE
#define FR_DICTIONARY_INTERNAL_DIR
void fr_perror(char const *fmt,...)
Print the current error to stderr with a prefix.
void fr_strerror_clear(void)
Clears all pending messages from the talloc pools.
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
#define fr_strerror_const(_msg)
#define fr_type_is_structural(_x)
static char const * fr_type_to_str(fr_type_t type)
Return a static string containing the type name.
int fr_check_lib_magic(uint64_t magic)
Check if the application linking to the library has the correct magic number.
#define RADIUSD_MAGIC_NUMBER