The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Data Structures | Typedefs | Enumerations | Functions | Variables
base.h File Reference

Implements the evaluation and parsing functions for the FreeRADIUS version of jpath. More...

#include "config.h"
#include <freeradius-devel/server/base.h>
+ Include dependency graph for base.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  fr_json_format_attr_t
 Attribute formatting options for fr_json_afrom_pair_list() More...
 
struct  fr_json_format_s
 JSON document formatting options. More...
 
struct  fr_json_format_value_t
 Value formatting options for fr_json_afrom_pair_list() More...
 

Typedefs

typedef struct fr_jpath_node fr_jpath_node_t
 
typedef struct fr_json_format_s fr_json_format_t
 

Enumerations

enum  json_mode_type_t {
  JSON_MODE_UNSET = 0 ,
  JSON_MODE_OBJECT ,
  JSON_MODE_OBJECT_SIMPLE ,
  JSON_MODE_ARRAY ,
  JSON_MODE_ARRAY_OF_VALUES ,
  JSON_MODE_ARRAY_OF_NAMES
}
 List of possible JSON format output modes. More...
 

Functions

char * fr_jpath_asprint (TALLOC_CTX *ctx, fr_jpath_node_t const *head)
 Print a node list to a string for debugging. More...
 
size_t fr_jpath_escape_func (UNUSED request_t *request, char *out, size_t outlen, char const *in, UNUSED void *arg)
 Escapes special chars. More...
 
int fr_jpath_evaluate_leaf (TALLOC_CTX *ctx, fr_value_box_list_t *out, fr_type_t dst_type, fr_dict_attr_t const *dst_enumv, json_object *root, fr_jpath_node_t const *jpath)
 Evaluate a parsed jpath expression against a json-c tree. More...
 
ssize_t fr_jpath_parse (TALLOC_CTX *ctx, fr_jpath_node_t **head, char const *in, size_t inlen)
 Parse a jpath string. More...
 
char * fr_json_afrom_pair_list (TALLOC_CTX *ctx, fr_pair_list_t *vps, fr_json_format_t const *format)
 Returns a JSON string of a list of value pairs. More...
 
bool fr_json_format_verify (fr_json_format_t const *format, bool verbose)
 Verify that the options in fr_json_format_t are valid. More...
 
int fr_json_object_to_value_box (TALLOC_CTX *ctx, fr_value_box_t *out, json_object *object, fr_dict_attr_t const *enumv, bool tainted)
 Convert json object to fr_value_box_t. More...
 
fr_slen_t fr_json_str_from_value (fr_sbuff_t *out, fr_value_box_t *vb, bool include_quotes)
 Print a value box as its equivalent JSON format without going via a struct json_object (in most cases) More...
 
void fr_json_version_print (void)
 Print JSON-C version. More...
 
json_object * json_object_from_value_box (fr_value_box_t const *data)
 Convert boxed value_box to a JSON object. More...
 

Variables

conf_parser_t const fr_json_format_config []
 
fr_table_num_sorted_t const fr_json_format_table []
 
size_t fr_json_format_table_len
 

Detailed Description

Implements the evaluation and parsing functions for the FreeRADIUS version of jpath.

Id
61fbb5752c1a73c5e6fed653cd781dc0fb97bba6
Author
Arran Cudbard-Bell
Matthew Newton

Definition in file base.h.


Data Structure Documentation

◆ fr_json_format_attr_t

struct fr_json_format_attr_t

Attribute formatting options for fr_json_afrom_pair_list()

Controls how attributes are formatted in JSON documents produced from fr_json_afrom_pair_list().

prefix adds a string prefix to all attribute names in the JSON document, with a colon delimiter.

Example, when prefix is NULL:

{"User-Name":{"type":"string","value":["john"]}}

Example, when prefix is set to foo:

{"foo:User-Name":{"type":"string","value":["john"]}}
See also
struct fr_json_format_s

Definition at line 88 of file base.h.

Data Fields
char const * prefix Prefix to add to all attribute names.

◆ fr_json_format_s

struct fr_json_format_s

JSON document formatting options.

These options control the format of JSON document which is produced by fr_json_afrom_pair_list().

The output_mode determines the format of JSON that is created:

When JSON_MODE_OBJECT:

{
        "<attribute0>": {
                "type":"<str-type0>",
                "value":["value0"]
        },
        "<attribute1>": {
                "type":"<str-type1>",
                "value":["value1.0", "value1.1"]
        },
        "<attribute2>": {
                "type":"<int-type2>",
                "value":[2]
        },
}

When JSON_MODE_OBJECT_SIMPLE:

{
        "<attribute0>":"<value0>",
        "<attribute1>":["<value1.0>","<value1.1>"],
        "<attribute2>":2
}

When JSON_MODE_ARRAY:

[
        {"name":"<attribute0>","type":"<str-type0>","value":"<value0>"},
        {"name":"<attribute1>","type":"<str-type1>","value":"<value1.0>"},
        {"name":"<attribute1>","type":"<str-type1>","value":"<value1.1>"},
        {"name":"<attribute2>","type":"<int-type2>","value":2}
]

When JSON_MODE_ARRAY_OF_VALUES:

[
        <value0>,
        <value1.0>,
        <value1.1>,
        <value2>
]

When JSON_MODE_ARRAY_OF_NAMES:

[
        <attribute0>,
        <attribute1>,
        <attribute1>,
        <attribute2>
]

Definition at line 219 of file base.h.

+ Collaboration diagram for fr_json_format_s:
Data Fields
fr_json_format_attr_t attr Formatting options for attribute names.
bool include_type Include attribute type where possible.
json_mode_type_t output_mode Determine the format of JSON document to generate.
char const * output_mode_str For conf_parser_t only.
fr_json_format_value_t value Formatting options for attribute values.

◆ fr_json_format_value_t

struct fr_json_format_value_t

Value formatting options for fr_json_afrom_pair_list()

Controls how values are formatted in JSON documents produced from fr_json_afrom_pair_list().

Not all these options are valid for all output modes.

See also
fr_json_format_verify(), fr_json_format_s

If an attribute appears only once then the value will normally be written as an object. When an attribute appears more than once then the values will be added as an array instead. Setting value_is_always_array will ensure that values are always written as an array, even if containing only a single entry.

Example with output_mode JSON_MODE_OBJECT_SIMPLE and value_is_always_array is false:

{"User-Name":"john","Filter-Id":["f1","f2"]}

Example with output_mode JSON_MODE_OBJECT_SIMPLE and value_is_always_array is true:

{"User-Name":["john"],"Filter-Id":["f1","f2"]}

Set enum_as_int to write enumerated values in their integer form.

When false, the string form is output:

{"Service-Type":{"type":"uint32","value":"Login-User"}}

When true, the integer is output:

{"Service-Type":{"type":"uint32","value":1}}

Numeric data types will usually be written to the JSON document as numbers. always_string ensures that all values are written as strings:

Example when always_string is false:

{"NAS-Port":{"type":"uint32","value":999}}

Example when always_string is true:

{"NAS-Port":{"type":"uint32","value":"999"}}

Definition at line 147 of file base.h.

Data Fields
bool always_string Output all data types as strings.
bool enum_as_int Output enums as value, not their string representation.
bool value_is_always_array Use JSON array for multiple attribute values.

Typedef Documentation

◆ fr_jpath_node_t

Definition at line 233 of file base.h.

◆ fr_json_format_t

Definition at line 51 of file base.h.

Enumeration Type Documentation

◆ json_mode_type_t

List of possible JSON format output modes.

See also
fr_json_format_s
Enumerator
JSON_MODE_UNSET 
JSON_MODE_OBJECT 
JSON_MODE_OBJECT_SIMPLE 
JSON_MODE_ARRAY 
JSON_MODE_ARRAY_OF_VALUES 
JSON_MODE_ARRAY_OF_NAMES 

Definition at line 57 of file base.h.

Function Documentation

◆ fr_jpath_asprint()

char* fr_jpath_asprint ( TALLOC_CTX *  ctx,
fr_jpath_node_t const *  head 
)

Print a node list to a string for debugging.

Will not be identical to the original parsed string, but should be sufficient for testing purposes.

Parameters
ctxto allocate string in.
headof the node list.
Returns
the string representation of the node list.

Definition at line 411 of file jpath.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_jpath_escape_func()

size_t fr_jpath_escape_func ( UNUSED request_t request,
char *  out,
size_t  outlen,
char const *  in,
UNUSED void *  arg 
)

Escapes special chars.

Escapes any characters that may have special meaning within a jpath expression

Parameters
requestCurrent request (unused may be NULL).
outWhere to write the escaped string.
outlenLength of the output buffer.
indata to escape.
arguctx data, not used.
Returns
the number of chars written to out.

Definition at line 106 of file jpath.c.

+ Here is the caller graph for this function:

◆ fr_jpath_evaluate_leaf()

int fr_jpath_evaluate_leaf ( TALLOC_CTX *  ctx,
fr_value_box_list_t *  out,
fr_type_t  dst_type,
fr_dict_attr_t const *  dst_enumv,
json_object *  root,
fr_jpath_node_t const *  jpath 
)

Evaluate a parsed jpath expression against a json-c tree.

Will produce one or more fr_value_box_t structures of the desired type, or error out if the conversion between types fails.

Parameters
[in,out]ctxto allocate fr_value_box_t in.
[out]outWhere to write fr_value_box_t.
[in]dst_typeFreeRADIUS type to convert to.
[in]dst_enumvEnumeration values to allow string to integer conversions.
[in]rootof the json-c tree.
[in]jpathto evaluate.
Returns
  • 1 on match.
  • 0 on no match.
  • -1 on error.

Definition at line 383 of file jpath.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_jpath_parse()

ssize_t fr_jpath_parse ( TALLOC_CTX *  ctx,
fr_jpath_node_t **  head,
char const *  in,
size_t  inlen 
)

Parse a jpath string.

Based on the syntax described here http://goessner.net/articles/JsonPath/

Implements parser for everything except unions and expressions

Returns
  • > 0 on success.
  • <= 0 on error (* -1 to get offset error occurred at).

Definition at line 813 of file jpath.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_json_afrom_pair_list()

char* fr_json_afrom_pair_list ( TALLOC_CTX *  ctx,
fr_pair_list_t vps,
fr_json_format_t const *  format 
)

Returns a JSON string of a list of value pairs.

The result is a talloc-ed string, freeing the string is the responsibility of the caller.

The 'format' struct contains settings to configure the output JSON document format.

See also
fr_json_format_s

Default output, when format is NULL, is:

{
        "<attribute0>":{
                "type":"<type0>",
                "value":[<value0>,<value1>,<valueN>]
        },
        "<attribute1>":{
                "type":"<type1>",
                "value":[...]
        },
        "<attributeN>":{
                "type":"<typeN>",
                "value":[...]
        }
}
Parameters
[in]ctxTalloc context.
[in]vpsa list of value pairs.
[in]formatFormatting control, can be NULL to use default format.
Returns
JSON string representation of the value pairs

Definition at line 1239 of file json.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_json_format_verify()

bool fr_json_format_verify ( fr_json_format_t const *  format,
bool  verbose 
)

Verify that the options in fr_json_format_t are valid.

Warnings are optional, will fatal error if the format is corrupt.

Parameters
[in]formatthe format structure to check
[in]verboseprint out warnings if set
Returns
true if format is good, otherwise false

Definition at line 559 of file json.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_json_object_to_value_box()

int fr_json_object_to_value_box ( TALLOC_CTX *  ctx,
fr_value_box_t out,
json_object *  object,
fr_dict_attr_t const *  enumv,
bool  tainted 
)

Convert json object to fr_value_box_t.

Parameters
[in]ctxto allocate any value buffers in (should usually be the same as out).
[in]outWhere to write value. Must be initialised.
[in]objectto convert.
[in]enumvAny string values are assumed to be in PRESENTATION format, meaning that if an enumv is specified, they'll be checked against the list of aliases for that enumeration, and possibly converted into one of the enumeration values (which may not be a string).
[in]taintedWhether the data source is untrusted.
Returns
  • 0 on success.
  • -1 on failure.

Definition at line 96 of file json.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_json_str_from_value()

fr_slen_t fr_json_str_from_value ( fr_sbuff_t out,
fr_value_box_t vb,
bool  include_quotes 
)

Print a value box as its equivalent JSON format without going via a struct json_object (in most cases)

Parameters
[out]outbuffer to write to.
[in]vbto print.
[in]include_quoteswhether we should wrap string values, or non-native types like IPv4 addresses in quotes.
Returns
  • <0 on error.
  • >= number of bytes written.

Definition at line 279 of file json.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_json_version_print()

void fr_json_version_print ( void  )

Print JSON-C version.

Definition at line 459 of file json.c.

+ Here is the caller graph for this function:

◆ json_object_from_value_box()

json_object* json_object_from_value_box ( fr_value_box_t const *  data)

Convert boxed value_box to a JSON object.

Parameters
[in]datato convert.

Definition at line 193 of file json.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ fr_json_format_config

conf_parser_t const fr_json_format_config[]
extern

Definition at line 63 of file json.c.

◆ fr_json_format_table

fr_table_num_sorted_t const fr_json_format_table[]
extern

Definition at line 36 of file json.c.

◆ fr_json_format_table_len

size_t fr_json_format_table_len
extern

Definition at line 43 of file json.c.