The FreeRADIUS server $Id: f3670dba8951ca10eb4948feb3dc3db9423a334f $
Loading...
Searching...
No Matches
pair_print.c
Go to the documentation of this file.
1/*
2 * This library is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation; either
5 * version 2.1 of the License, or (at your option) any later version.
6 *
7 * This library is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
11 *
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with this library; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17/*
18 * Groups are printed from the referenced attribute.
19 *
20 * @todo - parent should _never_ be vp->da.
21 */
22#define fr_pair_reset_parent(parent) do { \
23 if (!parent) break; \
24 fr_assert((parent != vp->da) || (parent->type == FR_TYPE_GROUP)); \
25 fr_assert(fr_type_is_structural(parent->type)); \
26 if (parent->type == FR_TYPE_GROUP) { \
27 parent = fr_dict_attr_ref(parent); \
28 if (parent->flags.is_root) { \
29 parent = NULL; \
30 break; \
31 } \
32 } \
33 if (parent->dict != vp->da->dict) parent = NULL; \
34 } while (0)
35
36/** Pair serialisation API
37 *
38 * @file src/lib/util/pair_print.c
39 *
40 * @copyright 2020 The FreeRADIUS server project
41 */
42#include <freeradius-devel/util/pair.h>
43#include <freeradius-devel/util/talloc.h>
44#include <freeradius-devel/util/proto.h>
45#include <freeradius-devel/util/pair_legacy.h>
46
47/** Print the value of an attribute to a string
48 *
49 * @param[in] out Where to write the string.
50 * @param[in] vp to print.
51 * @param[in] quote Char to add before and after printed value,
52 * if 0 no char will be added, if < 0 raw string
53 * will be added.
54 * @return
55 * - >= 0 length of data written to out.
56 * - <0 the number of bytes we would have needed to write
57 * the complete string to out.
58 */
60{
61 fr_sbuff_t our_out;
62 ssize_t slen;
63
65
66 our_out = FR_SBUFF(out);
67
68 switch (vp->vp_type) {
69 /*
70 * For structural types descend down
71 */
73 if (fr_pair_list_empty(&vp->vp_group)) {
74 FR_SBUFF_IN_CHAR_RETURN(&our_out, '{', ' ', '}');
75
76 } else {
77 FR_SBUFF_IN_CHAR_RETURN(&our_out, '{', ' ');
78
79 FR_SBUFF_RETURN(fr_pair_list_print, &our_out, vp->da, &vp->vp_group);
80
81 FR_SBUFF_IN_CHAR_RETURN(&our_out, ' ', '}');
82 }
83
84 FR_SBUFF_SET_RETURN(out, &our_out);
85
86 /*
87 * For simple types just print the box
88 */
89 default:
90 /*
91 * If it's raw / unknown and not octets, print the cast before the type.
92 *
93 * Otherwise on parsing, we don't know how to interpret the value. :(
94 */
95 if ((vp->da->flags.is_raw || vp->da->flags.is_unknown) &&
96 (vp->vp_type != FR_TYPE_OCTETS)) {
97 FR_SBUFF_IN_SPRINTF_RETURN(&our_out, "(%s) ", fr_type_to_str(vp->vp_type));
98 }
99
100 slen = fr_value_box_print_quoted(&our_out, &vp->data, quote);
101 if (slen <= 0) return slen;
102 }
103
104 FR_SBUFF_SET_RETURN(out, &our_out);
105}
106
107/** Print either a quoted value, an enum, or a normal value.
108 *
109 */
111{
112 fr_sbuff_t our_out = FR_SBUFF(out);
113 char const *name;
114
115 if ((name = fr_value_box_enum_name(&vp->data)) != NULL) {
116 FR_SBUFF_IN_CHAR_RETURN(&our_out, ':', ':');
118 } else {
119
121 }
122
123 FR_SBUFF_SET_RETURN(out, &our_out);
124}
125
126/** Print an attribute name.
127 *
128 * @param[in] out Where to write the string.
129 * @param[in] parent If not NULL, only print OID components from
130 * this parent to the VP.
131 * @param[in,out] vp_p to print.
132 * @return
133 * - Length of data written to out.
134 * - value >= outlen on truncation.
135 */
137{
138 char const *token;
139 fr_pair_t const *vp = *vp_p;
140 fr_sbuff_t our_out = FR_SBUFF(out);
141
142 /*
143 * Omit the union if we can. But if the child is raw, then always print it. That way it's
144 * clearer what's going on.
145 */
146 if (vp->vp_type == FR_TYPE_UNION) {
147 fr_pair_t *child = fr_pair_list_head(&vp->vp_group);
148
149 if (!child->da->flags.is_unknown &&
150 (fr_pair_list_num_elements(&vp->vp_group) == 1)) {
151 parent = vp->da;
152 vp = fr_pair_list_head(&vp->vp_group);
153 }
154 }
155
157
158 if (vp->vp_raw) FR_SBUFF_IN_STRCPY_LITERAL_RETURN(&our_out, "raw.");
159 FR_DICT_ATTR_OID_PRINT_RETURN(&our_out, parent, vp->da, false);
160
161 /*
162 * Mash the nesting levels if we're asked to do that, and if each structural child has only one
163 * member.
164 */
165 if (vp->da->flags.allow_flat) {
166 bool raw = vp->vp_raw;
167 fr_dict_attr_t const *root = parent;
169
170 fr_sbuff_marker(&m, &our_out);
171
172 while (fr_type_is_structural(vp->vp_type) &&
173 (fr_pair_list_num_elements(&vp->vp_group) == 1)) {
174 parent = vp->da;
175 vp = fr_pair_list_head(&vp->vp_group);
176
178
179 FR_SBUFF_IN_CHAR_RETURN(&our_out, '.');
180 FR_DICT_ATTR_OID_PRINT_RETURN(&our_out, parent, vp->da, false);
181 }
182
183 /*
184 * If the root attribute is an internal group, then look for aliases in the protocol
185 * root.
186 *
187 * Otherwise the root attribute is a protocol group. The protocol dictionary or library
188 * can add aliases.
189 */
190 if (!root || root->flags.internal) root = fr_dict_root(vp->da->dict);
191
192 if (!raw && (vp->da->depth > (root->depth + 1)) && (fr_dict_attr_by_name(NULL, root, vp->da->name) == vp->da)) {
193 fr_sbuff_set(&our_out, &m);
194 FR_SBUFF_IN_CHAR_RETURN(&our_out, '.');
195 FR_SBUFF_IN_STRCPY_RETURN(&our_out, vp->da->name);
196 }
197 }
198
199 /*
200 * Print the operator for the _last_ attribute, which is generally what we want.
201 */
202 if ((vp->op > T_INVALID) && (vp->op < T_TOKEN_LAST)) {
203 token = fr_tokens[vp->op];
204 } else {
205 token = "<INVALID-TOKEN>";
206 }
207
208 FR_SBUFF_IN_CHAR_RETURN(&our_out, ' ');
209 FR_SBUFF_IN_STRCPY_RETURN(&our_out, token);
210 FR_SBUFF_IN_CHAR_RETURN(&our_out, ' ');
211
212 *vp_p = vp;
213 FR_SBUFF_SET_RETURN(out, &our_out);
214}
215
216/** Print one attribute and value to a string
217 *
218 * Print a fr_pair_t in the format:
219@verbatim
220 <attribute_name> <op> <value>
221@endverbatim
222 * to a string.
223 *
224 * @param[in] out Where to write the string.
225 * @param[in] parent If not NULL, only print OID components from
226 * this parent to the VP.
227 * @param[in] vp to print.
228 * @return
229 * - Length of data written to out.
230 * - value >= outlen on truncation.
231 */
233{
234 fr_sbuff_t our_out = FR_SBUFF(out);
235
237
239
241
242 FR_SBUFF_SET_RETURN(out, &our_out);
243}
244
245/** Print one attribute and value to a string with escape rules
246 *
247 * Similar to fr_pair_print(), but secrets are omitted. This function duplicates parts of the functionality
248 * of fr_pair_print(). fr_pair_print_value_quoted(), and fr_value_box_print_quoted(), but for the special
249 * case of secure strings.
250 *
251 * Note that only secrets of type "string" and "octets" are omitted. Other "secret" data types are still
252 * printed as-is.
253 *
254 * "octets" are still printed as "<<< secret >>>". Which won't parse correctly, but that's fine. Because
255 * omitted data is not meant to be parsed into real data.
256 *
257 * @param[in] out Where to write the string.
258 * @param[in] parent If not NULL, only print OID components from
259 * this parent to the VP.
260 * @param[in] vp to print.
261
262 * @return
263 * - < 0 on error
264 * - Length of data written to out.
265 * - value >= outlen on truncation.
266 */
268{
269 fr_sbuff_t our_out = FR_SBUFF(out);
270
272
274
275 if (fr_type_is_leaf(vp->vp_type)) {
276 if (!fr_value_box_is_secret(&vp->data)) {
278
279 } else {
280 switch (vp->vp_type) {
281 case FR_TYPE_STRING:
282 case FR_TYPE_OCTETS:
283 FR_SBUFF_IN_STRCPY_LITERAL_RETURN(&our_out, "<<< secret >>>");
284 break;
285
286 default:
287 fr_assert(0); /* see dict_tokenize.c, which enforces parsing of "secret" in dictionaries */
288 FR_SBUFF_IN_STRCPY_LITERAL_RETURN(&our_out, "<<< secret >>>");
289 break;
290 }
291 }
292 } else {
294
295 FR_SBUFF_IN_CHAR_RETURN(&our_out, '{', ' ');
296
297 fr_pair_list_foreach(&vp->vp_group, child) {
298 FR_SBUFF_RETURN(fr_pair_print_secure, &our_out, vp->da, child);
299 if (_nextchild) FR_SBUFF_IN_CHAR_RETURN(&our_out, ',', ' ');
300 }
301 FR_SBUFF_IN_CHAR_RETURN(&our_out, ' ', '}');
302 }
303
304 FR_SBUFF_SET_RETURN(out, &our_out);
305}
306
307/** Print a pair list
308 *
309 * @param[in] out Where to write the string.
310 * @param[in] parent parent da to start from
311 * @param[in] list pair list
312 * @return
313 * - Length of data written to out.
314 * - value >= outlen on truncation.
315 */
317{
318 fr_pair_t *vp;
319 fr_sbuff_t our_out = FR_SBUFF(out);
320
321 vp = fr_pair_list_head(list);
322 if (!vp) {
324 return fr_sbuff_used(out);
325 }
326
328
329 while (true) {
331 vp = fr_pair_list_next(list, vp);
332 if (!vp) break;
333
334 FR_SBUFF_IN_STRCPY_LITERAL_RETURN(&our_out, ", ");
335 }
336
337 FR_SBUFF_SET_RETURN(out, &our_out);
338}
339
340static void fr_pair_list_log_sbuff(fr_log_t const *log, int lvl, fr_pair_t const *parent, fr_pair_list_t const *list, char const *file, int line, fr_sbuff_t *sbuff)
341{
342 fr_pair_list_foreach(list, vp) {
343 fr_pair_t const *child = vp;
344
346
347 fr_sbuff_set_to_start(sbuff);
348
349 if (fr_pair_print_name(sbuff, parent ? parent->da : NULL, &child) <= 0) return;
350
351 /*
352 * Recursively print grouped attributes.
353 */
354 switch (child->vp_type) {
356 fr_log(log, L_DBG, file, line, "%*s%*s{", lvl * 2, "",
357 (int) fr_sbuff_used(sbuff), fr_sbuff_start(sbuff));
358 _fr_pair_list_log(log, lvl + 1, child, &child->vp_group, file, line);
359 fr_log(log, L_DBG, file, line, "%*s}", lvl * 2, "");
360 break;
361
362 default:
363 if (fr_pair_print_value(sbuff, child) < 0) break;
364
365 fr_log(log, L_DBG, file, line, "%*s%*s", lvl * 2, "",
366 (int) fr_sbuff_used(sbuff), fr_sbuff_start(sbuff));
367 }
368 }
369}
370
371
372/** Print a list of attributes and enumv
373 *
374 * @param[in] log to output to.
375 * @param[in] lvl depth in structural attribute.
376 * @param[in] parent parent attribute
377 * @param[in] list to print.
378 * @param[in] file where the message originated
379 * @param[in] line where the message originated
380 */
381void _fr_pair_list_log(fr_log_t const *log, int lvl, fr_pair_t const *parent, fr_pair_list_t const *list, char const *file, int line)
382{
383 fr_sbuff_t sbuff;
384 char buffer[1024] = "";
385
386 fr_sbuff_init_out(&sbuff, buffer, sizeof(buffer));
387
388 fr_pair_list_log_sbuff(log, lvl, parent, list, file, line, &sbuff);
389}
390
391static void fr_pair_list_debug_sbuff(FILE *fp, int lvl, fr_pair_t const *parent, fr_pair_list_t const *list, fr_sbuff_t *sbuff)
392{
393 fr_pair_list_foreach(list, vp) {
394 fr_pair_t const *child = vp;
395
397
398 fr_sbuff_set_to_start(sbuff);
399
400 if (fr_pair_print_name(sbuff, parent ? parent->da : NULL, &child) <= 0) return;
401
402 /*
403 * Recursively print grouped attributes.
404 */
405 switch (child->vp_type) {
407 fprintf(fp, "%*s%*s{\n", lvl * 2, "", (int) fr_sbuff_used(sbuff), fr_sbuff_start(sbuff));
408 _fr_pair_list_debug(fp, lvl + 1, child, &child->vp_group);
409 fprintf(fp, "%*s}\n", lvl * 2, "");
410 break;
411
412 default:
413 if (fr_value_box_print_quoted(sbuff, &child->data, T_DOUBLE_QUOTED_STRING) < 0) break;
414
415 fprintf(fp, "%*s%*s\n", lvl * 2, "", (int) fr_sbuff_used(sbuff), fr_sbuff_start(sbuff));
416 }
417 }
418}
419
420/** Print a list of attributes and enumv
421 *
422 * @param[in] fp to output to.
423 * @param[in] lvl depth in structural attribute.
424 * @param[in] parent parent attribute
425 * @param[in] list to print.
426 */
427void _fr_pair_list_debug(FILE *fp, int lvl, fr_pair_t const *parent, fr_pair_list_t const *list)
428{
429 fr_sbuff_t sbuff;
430 char buffer[1024] = "";
431
432 fr_sbuff_init_out(&sbuff, buffer, sizeof(buffer));
433
434 fr_pair_list_debug_sbuff(fp, lvl, parent, list, &sbuff);
435}
436
437/** Dumps a list to the default logging destination - Useful for calling from debuggers
438 *
439 */
440void fr_pair_list_debug(FILE *fp, fr_pair_list_t const *list)
441{
442 _fr_pair_list_debug(fp, 0, NULL, list);
443}
444
445
446/** Dumps a pair to the default logging destination - Useful for calling from debuggers
447 *
448 */
449void fr_pair_debug(FILE *fp, fr_pair_t const *pair)
450{
451 fr_sbuff_t sbuff;
452 char buffer[1024] = "";
453
454 fr_sbuff_init_out(&sbuff, buffer, sizeof(buffer));
455
456 (void) fr_pair_print(&sbuff, NULL, pair);
457
458 fr_fprintf(fp, "%pV\n", fr_box_strvalue_len(fr_sbuff_start(&sbuff), fr_sbuff_used(&sbuff)));
459}
460
461static const char spaces[] = " ";
462
463static void fprintf_pair_list(FILE *fp, fr_pair_list_t const *list, int depth)
464{
465 fr_pair_list_foreach(list, vp) {
466 fprintf(fp, "%.*s", depth, spaces);
467
468 if (fr_type_is_leaf(vp->vp_type)) {
469 fr_fprintf(fp, "%s %s %pV\n", vp->da->name, fr_tokens[vp->op], &vp->data);
470 continue;
471 }
472
474
475 fprintf(fp, "%s = {\n", vp->da->name);
476 fprintf_pair_list(fp, &vp->vp_group, depth + 1);
477 fprintf(fp, "%.*s}\n", depth, spaces);
478 }
479}
480
481void fr_fprintf_pair_list(FILE *fp, fr_pair_list_t const *list)
482{
483 fprintf_pair_list(fp, list, 0);
484}
485
486/*
487 * print.c doesn't include pair.h, and doing so causes too many knock-on effects.
488 */
489void fr_fprintf_pair(FILE *fp, char const *msg, fr_pair_t const *vp)
490{
491 if (msg) fputs(msg, fp);
492
493 if (fr_type_is_leaf(vp->vp_type)) {
494 fr_fprintf(fp, "%s %s %pV\n", vp->da->name, fr_tokens[vp->op], &vp->data);
495 } else {
497
498 fprintf(fp, "%s = {\n", vp->da->name);
499 fprintf_pair_list(fp, &vp->vp_group, 1);
500 fprintf(fp, "}\n");
501 }
502}
static int const char char buffer[256]
Definition acutest.h:576
int const char * file
Definition acutest.h:702
log_entry msg
Definition acutest.h:794
fr_dict_attr_t const * fr_dict_attr_by_name(fr_dict_attr_err_t *err, fr_dict_attr_t const *parent, char const *attr))
Locate a fr_dict_attr_t by its name.
Definition dict_util.c:3601
fr_dict_attr_t const * fr_dict_root(fr_dict_t const *dict)
Return the root attribute of a dictionary.
Definition dict_util.c:2720
#define FR_DICT_ATTR_OID_PRINT_RETURN(...)
Definition dict.h:774
Definition dwarf.c:563
#define fr_log(_log, _lvl, _file, _line, _fmt,...)
Definition log.h:172
@ L_DBG
Only displayed when debugging is enabled.
Definition log.h:56
@ FR_TYPE_STRING
String of printable characters.
@ FR_TYPE_OCTETS
Raw octets.
long int ssize_t
static uint8_t depth(fr_minmax_heap_index_t i)
Definition minmax_heap.c:83
void _fr_pair_list_log(fr_log_t const *log, int lvl, fr_pair_t const *parent, fr_pair_list_t const *list, char const *file, int line)
Print a list of attributes and enumv.
Definition pair_print.c:381
static void fprintf_pair_list(FILE *fp, fr_pair_list_t const *list, int depth)
Definition pair_print.c:463
void fr_fprintf_pair_list(FILE *fp, fr_pair_list_t const *list)
Definition pair_print.c:481
void fr_pair_debug(FILE *fp, fr_pair_t const *pair)
Dumps a pair to the default logging destination - Useful for calling from debuggers.
Definition pair_print.c:449
void _fr_pair_list_debug(FILE *fp, int lvl, fr_pair_t const *parent, fr_pair_list_t const *list)
Print a list of attributes and enumv.
Definition pair_print.c:427
static void fr_pair_list_debug_sbuff(FILE *fp, int lvl, fr_pair_t const *parent, fr_pair_list_t const *list, fr_sbuff_t *sbuff)
Definition pair_print.c:391
#define fr_pair_reset_parent(parent)
Definition pair_print.c:22
void fr_pair_list_debug(FILE *fp, fr_pair_list_t const *list)
Dumps a list to the default logging destination - Useful for calling from debuggers.
Definition pair_print.c:440
ssize_t fr_pair_print(fr_sbuff_t *out, fr_dict_attr_t const *parent, fr_pair_t const *vp)
Print one attribute and value to a string.
Definition pair_print.c:232
void fr_fprintf_pair(FILE *fp, char const *msg, fr_pair_t const *vp)
Definition pair_print.c:489
ssize_t fr_pair_print_secure(fr_sbuff_t *out, fr_dict_attr_t const *parent, fr_pair_t const *vp)
Print one attribute and value to a string with escape rules.
Definition pair_print.c:267
ssize_t fr_pair_print_value_quoted(fr_sbuff_t *out, fr_pair_t const *vp, fr_token_t quote)
Print the value of an attribute to a string.
Definition pair_print.c:59
ssize_t fr_pair_print_name(fr_sbuff_t *out, fr_dict_attr_t const *parent, fr_pair_t const **vp_p)
Print an attribute name.
Definition pair_print.c:136
static void fr_pair_list_log_sbuff(fr_log_t const *log, int lvl, fr_pair_t const *parent, fr_pair_list_t const *list, char const *file, int line, fr_sbuff_t *sbuff)
Definition pair_print.c:340
static const char spaces[]
Definition pair_print.c:461
static ssize_t fr_pair_print_value(fr_sbuff_t *out, fr_pair_t const *vp)
Print either a quoted value, an enum, or a normal value.
Definition pair_print.c:110
ssize_t fr_pair_list_print(fr_sbuff_t *out, fr_dict_attr_t const *parent, fr_pair_list_t const *list)
Print a pair list.
Definition pair_print.c:316
#define fr_assert(_expr)
Definition rad_assert.h:37
static char const * name
#define fr_sbuff_start(_sbuff_or_marker)
#define FR_SBUFF_IN_CHAR_RETURN(_sbuff,...)
#define fr_sbuff_set(_dst, _src)
#define FR_SBUFF_IN_STRCPY_LITERAL_RETURN(_sbuff, _str)
#define fr_sbuff_init_out(_out, _start, _len_or_end)
#define FR_SBUFF_RETURN(_func, _sbuff,...)
#define FR_SBUFF_SET_RETURN(_dst, _src)
#define FR_SBUFF_IN_SPRINTF_RETURN(...)
#define FR_SBUFF(_sbuff_or_marker)
#define fr_sbuff_used(_sbuff_or_marker)
#define FR_SBUFF_IN_STRCPY_RETURN(...)
fr_pair_t * vp
Definition log.h:93
Stores an attribute, a value and various bits of other data.
Definition pair.h:68
fr_dict_attr_t const *_CONST da
Dictionary attribute defines the attribute number, vendor and type of the pair.
Definition pair.h:69
char const * fr_tokens[T_TOKEN_LAST]
Definition token.c:146
enum fr_token fr_token_t
@ T_INVALID
Definition token.h:37
@ T_DOUBLE_QUOTED_STRING
Definition token.h:119
#define T_TOKEN_LAST
Definition token.h:127
bool fr_pair_list_empty(fr_pair_list_t const *list)
Is a valuepair list empty.
#define PAIR_VERIFY(_x)
Definition pair.h:205
fr_pair_t * fr_pair_list_next(fr_pair_list_t const *list, fr_pair_t const *item))
Get the next item in a valuepair list after a specific entry.
Definition pair_inline.c:69
#define fr_pair_list_foreach(_list_head, _iter)
Iterate over the contents of a fr_pair_list_t.
Definition pair.h:281
#define PAIR_VERIFY_WITH_LIST(_l, _x)
Definition pair.h:206
fr_pair_t * fr_pair_list_head(fr_pair_list_t const *list)
Get the head of a valuepair list.
Definition pair_inline.c:42
size_t fr_pair_list_num_elements(fr_pair_list_t const *list)
Get the length of a list of fr_pair_t.
static fr_slen_t parent
Definition pair.h:860
ssize_t fr_fprintf(FILE *fp, char const *fmt,...)
Special version of fprintf which implements custom format specifiers.
Definition print.c:906
@ FR_TYPE_UNION
A union of limited children.
Definition types.h:81
#define fr_type_is_structural(_x)
Definition types.h:392
#define FR_TYPE_STRUCTURAL
Definition types.h:316
#define fr_type_is_leaf(_x)
Definition types.h:393
static char const * fr_type_to_str(fr_type_t type)
Return a static string containing the type name.
Definition types.h:454
ssize_t fr_value_box_print_quoted(fr_sbuff_t *out, fr_value_box_t const *data, fr_token_t quote)
Print one boxed value to a string with quotes (where needed)
Definition value.c:6409
static bool fr_value_box_is_secret(fr_value_box_t const *box)
Definition value.h:1144
#define fr_box_strvalue_len(_val, _len)
Definition value.h:334
static char const * fr_value_box_enum_name(fr_value_box_t const *box)
Decide if we need an enum prefix.
Definition value.h:1179
static size_t char ** out
Definition value.h:1062