The FreeRADIUS server $Id: a4e7f8c94e8038b7627bcf52356b7ed15955c1d7 $
Loading...
Searching...
No Matches
base.h
Go to the documentation of this file.
1#pragma once
2/*
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or (at
6 * your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
16 */
17
18/**
19 * $Id: 98c1a20e0e1ab926c435b3172ef25bdd16fdbb28 $
20 * @file lib/json/base.h
21 * @brief Implements the evaluation and parsing functions for the FreeRADIUS version of jpath.
22 *
23 * @author Arran Cudbard-Bell
24 * @author Matthew Newton
25 *
26 * @copyright 2015 Arran Cudbard-Bell (a.cudbardb@freeradius.org)
27 * @copyright 2015,2020 Network RADIUS SAS (legal@networkradius.com)
28 * @copyright 2015 The FreeRADIUS Server Project
29 */
30RCSIDH(json_h, "$Id: 98c1a20e0e1ab926c435b3172ef25bdd16fdbb28 $")
31
32#include "config.h"
33
34#ifdef HAVE_JSON
35# if defined(HAVE_JSONMC_JSON_H)
36
37DIAG_OFF_OPTIONAL(documentation-deprecated-sync)
38# include <json-c/json.h>
39DIAG_ON_OPTIONAL(documentation-deprecated-sync)
40
41# elif defined(HAVE_JSON_JSON_H)
42# include <json/json.h>
43# else
44# error "Need json-c headers"
45# endif
46# include <freeradius-devel/server/base.h>
47
49extern size_t fr_json_format_table_len;
50
51/** How to encode FR_TYPE_OCTETS values in JSON output.
52 */
53typedef enum : uint8_t {
54 JSON_BINARY_FORMAT_RAW = 0, //!< Current behaviour - raw bytes as JSON string.
55 JSON_BINARY_FORMAT_BASE16, //!< Base16-encode octets values.
56 JSON_BINARY_FORMAT_BASE64 //!< Base64-encode octets values.
58
61
62/** List of possible JSON format output modes.
63 *
64 * @see fr_json_format_s
65 */
74
75
76/** Attribute formatting options for fr_json_afrom_pair_list()
77 *
78 * Controls how attributes are formatted in JSON documents
79 * produced from fr_json_afrom_pair_list().
80 *
81 * **prefix** adds a string prefix to all attribute names in the
82 * JSON document, with a colon delimiter.
83 *
84 * Example, when prefix is NULL:
85@verbatim
86{"User-Name":{"type":"string","value":["john"]}}
87@endverbatim
88 *
89 * Example, when prefix is set to `foo`:
90@verbatim
91{"foo:User-Name":{"type":"string","value":["john"]}}
92@endverbatim
93 *
94 * @see struct fr_json_format_s
95 *
96 */
97typedef struct {
98 char const *prefix; //!< Prefix to add to all attribute names
100
101
102/** Value formatting options for fr_json_afrom_pair_list()
103 *
104 * Controls how values are formatted in JSON documents
105 * produced from fr_json_afrom_pair_list().
106 *
107 * Not all these options are valid for all output modes.
108 * @see fr_json_format_verify(), fr_json_format_s
109 *
110 *
111 * If an attribute appears only once then the value will normally
112 * be written as an object. When an attribute appears more than
113 * once then the values will be added as an array instead. Setting
114 * **value_is_always_array** will ensure that values are always written as
115 * an array, even if containing only a single entry.
116 *
117 * Example with output_mode `JSON_MODE_OBJECT_SIMPLE` and `value_is_always_array` is false:
118@verbatim
119{"User-Name":"john","Filter-Id":["f1","f2"]}
120@endverbatim
121 *
122 * Example with output_mode `JSON_MODE_OBJECT_SIMPLE` and `value_is_always_array` is true:
123@verbatim
124{"User-Name":["john"],"Filter-Id":["f1","f2"]}
125@endverbatim
126 *
127 *
128 * Set **enum_as_int** to write enumerated values in their integer form.
129 *
130 * When false, the string form is output:
131@verbatim
132{"Service-Type":{"type":"uint32","value":"Login-User"}}
133@endverbatim
134 *
135 * When true, the integer is output:
136@verbatim
137{"Service-Type":{"type":"uint32","value":1}}
138@endverbatim
139 *
140 *
141 * Numeric data types will usually be written to the JSON document
142 * as numbers. **always_string** ensures that all values are written as
143 * strings:
144 *
145 * Example when `always_string` is false:
146@verbatim
147{"NAS-Port":{"type":"uint32","value":999}}
148@endverbatim
149 *
150 * Example when `always_string` is true:
151@verbatim
152{"NAS-Port":{"type":"uint32","value":"999"}}
153@endverbatim
154 *
155 */
156typedef struct {
157 bool value_is_always_array; //!< Use JSON array for multiple attribute values.
158 bool enum_as_int; //!< Output enums as value, not their string representation.
159 bool always_string; //!< Output all data types as strings.
160 fr_json_binary_format_t binary_format; //!< How to encode FR_TYPE_OCTETS values.
162
163
164/** JSON document formatting options
165 *
166 * These options control the format of JSON document which is
167 * produced by fr_json_afrom_pair_list().
168 *
169 * The **output_mode** determines the format of JSON that is created:
170 *
171 * When JSON_MODE_OBJECT:
172@verbatim
173{
174 "<attribute0>": {
175 "type":"<str-type0>",
176 "value":["value0"]
177 },
178 "<attribute1>": {
179 "type":"<str-type1>",
180 "value":["value1.0", "value1.1"]
181 },
182 "<attribute2>": {
183 "type":"<int-type2>",
184 "value":[2]
185 },
186}
187@endverbatim
188 *
189 * When JSON_MODE_OBJECT_SIMPLE:
190@verbatim
191{
192 "<attribute0>":"<value0>",
193 "<attribute1>":["<value1.0>","<value1.1>"],
194 "<attribute2>":2
195}
196@endverbatim
197 *
198 * When JSON_MODE_ARRAY:
199@verbatim
200[
201 {"name":"<attribute0>","type":"<str-type0>","value":"<value0>"},
202 {"name":"<attribute1>","type":"<str-type1>","value":"<value1.0>"},
203 {"name":"<attribute1>","type":"<str-type1>","value":"<value1.1>"},
204 {"name":"<attribute2>","type":"<int-type2>","value":2}
205]
206@endverbatim
207 *
208 * When JSON_MODE_ARRAY_OF_VALUES:
209@verbatim
210[
211 <value0>,
212 <value1.0>,
213 <value1.1>,
214 <value2>
215]
216@endverbatim
217 *
218 * When JSON_MODE_ARRAY_OF_NAMES:
219@verbatim
220[
221 <attribute0>,
222 <attribute1>,
223 <attribute1>,
224 <attribute2>
225]
226@endverbatim
227 *
228 */
230 char const *output_mode_str; //!< For conf_parser_t only.
231
232 json_mode_type_t output_mode; //!< Determine the format of JSON document
233 //!< to generate.
234
235 fr_json_format_attr_t attr; //!< Formatting options for attribute names.
236 fr_json_format_value_t value; //!< Formatting options for attribute values.
237
238 bool include_type; //!< Include attribute type where possible.
239};
240
242
244
245
246/* jpath .c */
248
250 char const *in, UNUSED void *arg);
251
252int fr_jpath_evaluate_leaf(TALLOC_CTX *ctx, fr_value_box_list_t *out,
253 fr_type_t dst_type, fr_dict_attr_t const *dst_enumv,
254 json_object *root, fr_jpath_node_t const *jpath);
255
256char *fr_jpath_asprint(TALLOC_CTX *ctx, fr_jpath_node_t const *head);
257
258ssize_t fr_jpath_parse(TALLOC_CTX *ctx, fr_jpath_node_t **head, char const *in, size_t inlen);
259
260/* json.c */
261int fr_json_object_to_value_box(TALLOC_CTX *ctx, fr_value_box_t *out, json_object *object,
262 fr_dict_attr_t const *enumv, bool tainted);
263
265
266fr_slen_t fr_json_str_from_value(fr_sbuff_t *out, fr_value_box_t const *vb, bool include_quotes);
267
268void fr_json_version_print(void);
269
270char *fr_json_afrom_pair_list(TALLOC_CTX *ctx, fr_pair_list_t *vps,
271 fr_json_format_t const *format);
272
273bool fr_json_format_verify(fr_json_format_t const *format, bool verbose);
274#endif
static uint8_t request[4]
#define DIAG_OFF_OPTIONAL(_x)
Definition build.h:577
#define RCSIDH(h, id)
Definition build.h:604
#define UNUSED
Definition build.h:415
#define DIAG_ON_OPTIONAL(_x)
Definition build.h:578
Defines a CONF_PAIR to C data type mapping.
Definition cf_parse.h:649
struct fr_jpath_node_s fr_jpath_node_t
Definition fuzzer_json.c:40
Node in a jpath selector sequence.
Definition jpath.c:87
fr_table_num_sorted_t const fr_json_binary_format_table[]
Definition json.c:46
bool value_is_always_array
Use JSON array for multiple attribute values.
Definition base.h:157
json_mode_type_t output_mode
Determine the format of JSON document to generate.
Definition base.h:232
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.
Definition json.c:1330
size_t fr_json_format_table_len
Definition json.c:44
bool include_type
Include attribute type where possible.
Definition base.h:238
fr_json_format_attr_t attr
Formatting options for attribute names.
Definition base.h:235
char * fr_jpath_asprint(TALLOC_CTX *ctx, fr_jpath_node_t const *head)
Print a node list to a string for debugging.
Definition jpath.c:411
json_object * json_object_from_value_box(fr_value_box_t const *data)
Convert boxed value_box to a JSON object.
Definition json.c:213
fr_json_binary_format_t binary_format
How to encode FR_TYPE_OCTETS values.
Definition base.h:160
fr_json_format_value_t value
Formatting options for attribute values.
Definition base.h:236
size_t fr_json_binary_format_table_len
Definition json.c:51
json_mode_type_t
List of possible JSON format output modes.
Definition base.h:66
@ JSON_MODE_ARRAY
Definition base.h:70
@ JSON_MODE_UNSET
Definition base.h:67
@ JSON_MODE_OBJECT
Definition base.h:68
@ JSON_MODE_ARRAY_OF_NAMES
Definition base.h:72
@ JSON_MODE_OBJECT_SIMPLE
Definition base.h:69
@ JSON_MODE_ARRAY_OF_VALUES
Definition base.h:71
char const * output_mode_str
For conf_parser_t only.
Definition base.h:230
fr_slen_t fr_json_str_from_value(fr_sbuff_t *out, fr_value_box_t const *vb, bool include_quotes)
Print a value box as its equivalent JSON format without going via a struct json_object (in most cases...
Definition json.c:300
conf_parser_t const fr_json_format_config[]
Definition json.c:74
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.
Definition jpath.c:383
ssize_t fr_jpath_escape_func(UNUSED request_t *request, char *out, size_t outlen, char const *in, UNUSED void *arg)
Escapes special chars.
Definition jpath.c:106
char const * prefix
Prefix to add to all attribute names.
Definition base.h:98
void fr_json_version_print(void)
Print JSON-C version.
Definition json.c:484
fr_table_num_sorted_t const fr_json_format_table[]
Definition json.c:37
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.
Definition json.c:116
bool enum_as_int
Output enums as value, not their string representation.
Definition base.h:158
bool always_string
Output all data types as strings.
Definition base.h:159
bool fr_json_format_verify(fr_json_format_t const *format, bool verbose)
Verify that the options in fr_json_format_t are valid.
Definition json.c:641
fr_json_binary_format_t
How to encode FR_TYPE_OCTETS values in JSON output.
Definition base.h:53
@ JSON_BINARY_FORMAT_BASE64
Base64-encode octets values.
Definition base.h:56
@ JSON_BINARY_FORMAT_RAW
Current behaviour - raw bytes as JSON string.
Definition base.h:54
@ JSON_BINARY_FORMAT_BASE16
Base16-encode octets values.
Definition base.h:55
ssize_t fr_jpath_parse(TALLOC_CTX *ctx, fr_jpath_node_t **head, char const *in, size_t inlen)
Parse a jpath string.
Definition jpath.c:813
Attribute formatting options for fr_json_afrom_pair_list()
Definition base.h:97
JSON document formatting options.
Definition base.h:229
Value formatting options for fr_json_afrom_pair_list()
Definition base.h:156
fr_type_t
long int ssize_t
unsigned char uint8_t
ssize_t fr_slen_t
An element in a lexicographically sorted array of name to num mappings.
Definition table.h:49
static fr_slen_t head
Definition xlat.h:410
static fr_sbuff_err_t char size_t fr_sbuff_t * in
Definition value.h:1061
static fr_slen_t data
Definition value.h:1366
static fr_sbuff_err_t char ** out
Definition value.h:1061