The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
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: 0923e41d671dbbfe25d476634a616c765dc50df4 $
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: 0923e41d671dbbfe25d476634a616c765dc50df4 $")
31
32#include "config.h"
33
34#ifdef HAVE_JSON
35# if defined(HAVE_JSONMC_JSON_H)
36
38DIAG_OFF(documentation-deprecated-sync)
39# include <json-c/json.h>
40DIAG_ON(documentation-deprecated-sync)
42
43# elif defined(HAVE_JSON_JSON_H)
44# include <json/json.h>
45# else
46# error "Need json-c headers"
47# endif
48# include <freeradius-devel/server/base.h>
49
51extern size_t fr_json_format_table_len;
52
53/** How to encode FR_TYPE_OCTETS values in JSON output.
54 */
55typedef enum {
56 JSON_BINARY_FORMAT_RAW = 0, //!< Current behaviour - raw bytes as JSON string.
57 JSON_BINARY_FORMAT_BASE16, //!< Base16-encode octets values.
58 JSON_BINARY_FORMAT_BASE64 //!< Base64-encode octets values.
60
63
64/** List of possible JSON format output modes.
65 *
66 * @see fr_json_format_s
67 */
76
77
78/** Attribute formatting options for fr_json_afrom_pair_list()
79 *
80 * Controls how attributes are formatted in JSON documents
81 * produced from fr_json_afrom_pair_list().
82 *
83 * **prefix** adds a string prefix to all attribute names in the
84 * JSON document, with a colon delimiter.
85 *
86 * Example, when prefix is NULL:
87@verbatim
88{"User-Name":{"type":"string","value":["john"]}}
89@endverbatim
90 *
91 * Example, when prefix is set to `foo`:
92@verbatim
93{"foo:User-Name":{"type":"string","value":["john"]}}
94@endverbatim
95 *
96 * @see struct fr_json_format_s
97 *
98 */
99typedef struct {
100 char const *prefix; //!< Prefix to add to all attribute names
102
103
104/** Value formatting options for fr_json_afrom_pair_list()
105 *
106 * Controls how values are formatted in JSON documents
107 * produced from fr_json_afrom_pair_list().
108 *
109 * Not all these options are valid for all output modes.
110 * @see fr_json_format_verify(), fr_json_format_s
111 *
112 *
113 * If an attribute appears only once then the value will normally
114 * be written as an object. When an attribute appears more than
115 * once then the values will be added as an array instead. Setting
116 * **value_is_always_array** will ensure that values are always written as
117 * an array, even if containing only a single entry.
118 *
119 * Example with output_mode `JSON_MODE_OBJECT_SIMPLE` and `value_is_always_array` is false:
120@verbatim
121{"User-Name":"john","Filter-Id":["f1","f2"]}
122@endverbatim
123 *
124 * Example with output_mode `JSON_MODE_OBJECT_SIMPLE` and `value_is_always_array` is true:
125@verbatim
126{"User-Name":["john"],"Filter-Id":["f1","f2"]}
127@endverbatim
128 *
129 *
130 * Set **enum_as_int** to write enumerated values in their integer form.
131 *
132 * When false, the string form is output:
133@verbatim
134{"Service-Type":{"type":"uint32","value":"Login-User"}}
135@endverbatim
136 *
137 * When true, the integer is output:
138@verbatim
139{"Service-Type":{"type":"uint32","value":1}}
140@endverbatim
141 *
142 *
143 * Numeric data types will usually be written to the JSON document
144 * as numbers. **always_string** ensures that all values are written as
145 * strings:
146 *
147 * Example when `always_string` is false:
148@verbatim
149{"NAS-Port":{"type":"uint32","value":999}}
150@endverbatim
151 *
152 * Example when `always_string` is true:
153@verbatim
154{"NAS-Port":{"type":"uint32","value":"999"}}
155@endverbatim
156 *
157 */
158typedef struct {
159 bool value_is_always_array; //!< Use JSON array for multiple attribute values.
160 bool enum_as_int; //!< Output enums as value, not their string representation.
161 bool always_string; //!< Output all data types as strings.
162 fr_json_binary_format_t binary_format; //!< How to encode FR_TYPE_OCTETS values.
164
165
166/** JSON document formatting options
167 *
168 * These options control the format of JSON document which is
169 * produced by fr_json_afrom_pair_list().
170 *
171 * The **output_mode** determines the format of JSON that is created:
172 *
173 * When JSON_MODE_OBJECT:
174@verbatim
175{
176 "<attribute0>": {
177 "type":"<str-type0>",
178 "value":["value0"]
179 },
180 "<attribute1>": {
181 "type":"<str-type1>",
182 "value":["value1.0", "value1.1"]
183 },
184 "<attribute2>": {
185 "type":"<int-type2>",
186 "value":[2]
187 },
188}
189@endverbatim
190 *
191 * When JSON_MODE_OBJECT_SIMPLE:
192@verbatim
193{
194 "<attribute0>":"<value0>",
195 "<attribute1>":["<value1.0>","<value1.1>"],
196 "<attribute2>":2
197}
198@endverbatim
199 *
200 * When JSON_MODE_ARRAY:
201@verbatim
202[
203 {"name":"<attribute0>","type":"<str-type0>","value":"<value0>"},
204 {"name":"<attribute1>","type":"<str-type1>","value":"<value1.0>"},
205 {"name":"<attribute1>","type":"<str-type1>","value":"<value1.1>"},
206 {"name":"<attribute2>","type":"<int-type2>","value":2}
207]
208@endverbatim
209 *
210 * When JSON_MODE_ARRAY_OF_VALUES:
211@verbatim
212[
213 <value0>,
214 <value1.0>,
215 <value1.1>,
216 <value2>
217]
218@endverbatim
219 *
220 * When JSON_MODE_ARRAY_OF_NAMES:
221@verbatim
222[
223 <attribute0>,
224 <attribute1>,
225 <attribute1>,
226 <attribute2>
227]
228@endverbatim
229 *
230 */
232 char const *output_mode_str; //!< For conf_parser_t only.
233
234 json_mode_type_t output_mode; //!< Determine the format of JSON document
235 //!< to generate.
236
237 fr_json_format_attr_t attr; //!< Formatting options for attribute names.
238 fr_json_format_value_t value; //!< Formatting options for attribute values.
239
240 bool include_type; //!< Include attribute type where possible.
241};
242
244
246
247
248/* jpath .c */
250
251ssize_t fr_jpath_escape_func(UNUSED request_t *request, char *out, size_t outlen,
252 char const *in, UNUSED void *arg);
253
254int fr_jpath_evaluate_leaf(TALLOC_CTX *ctx, fr_value_box_list_t *out,
255 fr_type_t dst_type, fr_dict_attr_t const *dst_enumv,
256 json_object *root, fr_jpath_node_t const *jpath);
257
258char *fr_jpath_asprint(TALLOC_CTX *ctx, fr_jpath_node_t const *head);
259
260ssize_t fr_jpath_parse(TALLOC_CTX *ctx, fr_jpath_node_t **head, char const *in, size_t inlen);
261
262/* json.c */
263int fr_json_object_to_value_box(TALLOC_CTX *ctx, fr_value_box_t *out, json_object *object,
264 fr_dict_attr_t const *enumv, bool tainted);
265
267
268fr_slen_t fr_json_str_from_value(fr_sbuff_t *out, fr_value_box_t const *vb, bool include_quotes);
269
270void fr_json_version_print(void);
271
272char *fr_json_afrom_pair_list(TALLOC_CTX *ctx, fr_pair_list_t *vps,
273 fr_json_format_t const *format);
274
275bool fr_json_format_verify(fr_json_format_t const *format, bool verbose);
276#endif
#define DIAG_UNKNOWN_PRAGMAS
Definition build.h:461
#define DIAG_ON(_x)
Definition build.h:463
#define RCSIDH(h, id)
Definition build.h:489
#define UNUSED
Definition build.h:318
#define DIAG_OFF(_x)
Definition build.h:462
Defines a CONF_PAIR to C data type mapping.
Definition cf_parse.h:594
static fr_slen_t in
Definition dict.h:882
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:159
json_mode_type_t output_mode
Determine the format of JSON document to generate.
Definition base.h:234
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:1321
size_t fr_json_format_table_len
Definition json.c:44
json_mode_type_t
List of possible JSON format output modes.
Definition base.h:68
@ JSON_MODE_ARRAY
Definition base.h:72
@ JSON_MODE_UNSET
Definition base.h:69
@ JSON_MODE_OBJECT
Definition base.h:70
@ JSON_MODE_ARRAY_OF_NAMES
Definition base.h:74
@ JSON_MODE_OBJECT_SIMPLE
Definition base.h:71
@ JSON_MODE_ARRAY_OF_VALUES
Definition base.h:73
bool include_type
Include attribute type where possible.
Definition base.h:240
fr_json_format_attr_t attr
Formatting options for attribute names.
Definition base.h:237
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:204
fr_json_binary_format_t binary_format
How to encode FR_TYPE_OCTETS values.
Definition base.h:162
fr_json_format_value_t value
Formatting options for attribute values.
Definition base.h:238
size_t fr_json_binary_format_table_len
Definition json.c:51
fr_json_binary_format_t
How to encode FR_TYPE_OCTETS values in JSON output.
Definition base.h:55
@ JSON_BINARY_FORMAT_BASE64
Base64-encode octets values.
Definition base.h:58
@ JSON_BINARY_FORMAT_RAW
Current behaviour - raw bytes as JSON string.
Definition base.h:56
@ JSON_BINARY_FORMAT_BASE16
Base16-encode octets values.
Definition base.h:57
char const * output_mode_str
For conf_parser_t only.
Definition base.h:232
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:291
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:100
void fr_json_version_print(void)
Print JSON-C version.
Definition json.c:475
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:107
bool enum_as_int
Output enums as value, not their string representation.
Definition base.h:160
bool always_string
Output all data types as strings.
Definition base.h:161
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:632
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:99
JSON document formatting options.
Definition base.h:231
Value formatting options for fr_json_afrom_pair_list()
Definition base.h:158
fr_type_t
long int ssize_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:420
static fr_slen_t data
Definition value.h:1340
static size_t char fr_sbuff_t size_t inlen
Definition value.h:1030
static size_t char ** out
Definition value.h:1030