The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
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); \
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 while (fr_type_is_structural(vp->vp_type) &&
167 (fr_pair_list_num_elements(&vp->vp_group) == 1)) {
168 parent = vp->da;
169 vp = fr_pair_list_head(&vp->vp_group);
170
172
173 FR_SBUFF_IN_CHAR_RETURN(&our_out, '.');
174 FR_DICT_ATTR_OID_PRINT_RETURN(&our_out, parent, vp->da, false);
175 }
176 }
177
178 /*
179 * Print the operator for the _last_ attribute, which is generally what we want.
180 */
181 if ((vp->op > T_INVALID) && (vp->op < T_TOKEN_LAST)) {
182 token = fr_tokens[vp->op];
183 } else {
184 token = "<INVALID-TOKEN>";
185 }
186
187 FR_SBUFF_IN_CHAR_RETURN(&our_out, ' ');
188 FR_SBUFF_IN_STRCPY_RETURN(&our_out, token);
189 FR_SBUFF_IN_CHAR_RETURN(&our_out, ' ');
190
191 *vp_p = vp;
192 FR_SBUFF_SET_RETURN(out, &our_out);
193}
194
195/** Print one attribute and value to a string
196 *
197 * Print a fr_pair_t in the format:
198@verbatim
199 <attribute_name> <op> <value>
200@endverbatim
201 * to a string.
202 *
203 * @param[in] out Where to write the string.
204 * @param[in] parent If not NULL, only print OID components from
205 * this parent to the VP.
206 * @param[in] vp to print.
207 * @return
208 * - Length of data written to out.
209 * - value >= outlen on truncation.
210 */
212{
213 fr_sbuff_t our_out = FR_SBUFF(out);
214
216
218
220
221 FR_SBUFF_SET_RETURN(out, &our_out);
222}
223
224/** Print one attribute and value to a string with escape rules
225 *
226 * Similar to fr_pair_print(), but secrets are omitted. This function duplicates parts of the functionality
227 * of fr_pair_print(). fr_pair_print_value_quoted(), and fr_value_box_print_quoted(), but for the special
228 * case of secure strings.
229 *
230 * Note that only secrets of type "string" and "octets" are omitted. Other "secret" data types are still
231 * printed as-is.
232 *
233 * "octets" are still printed as "<<< secret >>>". Which won't parse correctly, but that's fine. Because
234 * omitted data is not meant to be parsed into real data.
235 *
236 * @param[in] out Where to write the string.
237 * @param[in] parent If not NULL, only print OID components from
238 * this parent to the VP.
239 * @param[in] vp to print.
240
241 * @return
242 * - < 0 on error
243 * - Length of data written to out.
244 * - value >= outlen on truncation.
245 */
247{
248 fr_sbuff_t our_out = FR_SBUFF(out);
249
251
253
254 if (fr_type_is_leaf(vp->vp_type)) {
255 if (!vp->data.secret) {
257
258 } else {
259 switch (vp->vp_type) {
260 case FR_TYPE_STRING:
261 case FR_TYPE_OCTETS:
262 FR_SBUFF_IN_STRCPY_LITERAL_RETURN(&our_out, "<<< secret >>>");
263 break;
264
265 default:
266 fr_assert(0); /* see dict_tokenize.c, which enforces parsing of "secret" in dictionaries */
267 FR_SBUFF_IN_STRCPY_LITERAL_RETURN(&our_out, "<<< secret >>>");
268 break;
269 }
270 }
271 } else {
272 fr_pair_t *child;
273 fr_dcursor_t cursor;
274
276
277 FR_SBUFF_IN_CHAR_RETURN(&our_out, '{', ' ');
278 for (child = fr_pair_dcursor_init(&cursor, &vp->vp_group);
279 child != NULL;
280 child = fr_dcursor_next(&cursor)) {
281 FR_SBUFF_RETURN(fr_pair_print_secure, &our_out, vp->da, child);
282 if (fr_dcursor_next_peek(&cursor)) FR_SBUFF_IN_CHAR_RETURN(&our_out, ',', ' ');
283 }
284 FR_SBUFF_IN_CHAR_RETURN(&our_out, ' ', '}');
285 }
286
287 FR_SBUFF_SET_RETURN(out, &our_out);
288}
289
290/** Print a pair list
291 *
292 * @param[in] out Where to write the string.
293 * @param[in] parent parent da to start from
294 * @param[in] list pair list
295 * @return
296 * - Length of data written to out.
297 * - value >= outlen on truncation.
298 */
300{
301 fr_pair_t *vp;
302 fr_sbuff_t our_out = FR_SBUFF(out);
303
304 vp = fr_pair_list_head(list);
305 if (!vp) {
307 return fr_sbuff_used(out);
308 }
309
311
312 while (true) {
314 vp = fr_pair_list_next(list, vp);
315 if (!vp) break;
316
317 FR_SBUFF_IN_STRCPY_LITERAL_RETURN(&our_out, ", ");
318 }
319
320 FR_SBUFF_SET_RETURN(out, &our_out);
321}
322
323static void fr_pair_list_log_sbuff(fr_log_t const *log, int lvl, fr_pair_t *parent, fr_pair_list_t const *list, char const *file, int line, fr_sbuff_t *sbuff)
324{
325 fr_dict_attr_t const *parent_da = NULL;
326
327 fr_pair_list_foreach(list, vp) {
329
330 fr_sbuff_set_to_start(sbuff);
331
332 if (vp->vp_raw) (void) fr_sbuff_in_strcpy(sbuff, "raw.");
333
334 if (parent && (parent->vp_type != FR_TYPE_GROUP)) parent_da = parent->da;
335 if (fr_dict_attr_oid_print(sbuff, parent_da, vp->da, false) <= 0) return;
336
337 /*
338 * Recursively print grouped attributes.
339 */
340 switch (vp->vp_type) {
342 fr_log(log, L_DBG, file, line, "%*s%*s {", lvl * 2, "",
343 (int) fr_sbuff_used(sbuff), fr_sbuff_start(sbuff));
344 _fr_pair_list_log(log, lvl + 1, vp, &vp->vp_group, file, line);
345 fr_log(log, L_DBG, file, line, "%*s}", lvl * 2, "");
346 break;
347
348 default:
349 (void) fr_sbuff_in_strcpy(sbuff, " = ");
350 if (fr_pair_print_value(sbuff, vp) < 0) break;
351
352 fr_log(log, L_DBG, file, line, "%*s%*s", lvl * 2, "",
353 (int) fr_sbuff_used(sbuff), fr_sbuff_start(sbuff));
354 }
355 }
356}
357
358
359/** Print a list of attributes and enumv
360 *
361 * @param[in] log to output to.
362 * @param[in] lvl depth in structural attribute.
363 * @param[in] parent parent attribute
364 * @param[in] list to print.
365 * @param[in] file where the message originated
366 * @param[in] line where the message originated
367 */
368void _fr_pair_list_log(fr_log_t const *log, int lvl, fr_pair_t *parent, fr_pair_list_t const *list, char const *file, int line)
369{
370 fr_sbuff_t sbuff;
371 char buffer[1024];
372
373 buffer[0] = '\0';
374
375 fr_sbuff_init_out(&sbuff, buffer, sizeof(buffer));
376
377 fr_pair_list_log_sbuff(log, lvl, parent, list, file, line, &sbuff);
378}
379
380static void fr_pair_list_debug_sbuff(FILE *fp, int lvl, fr_pair_t *parent, fr_pair_list_t const *list, fr_sbuff_t *sbuff)
381{
382 fr_dict_attr_t const *parent_da = NULL;
383
384 fr_pair_list_foreach(list, vp) {
386
387 fr_sbuff_set_to_start(sbuff);
388
389 if (vp->vp_raw) (void) fr_sbuff_in_strcpy(sbuff, "raw.");
390
391 if (parent && (parent->vp_type != FR_TYPE_GROUP)) parent_da = parent->da;
392 if (fr_dict_attr_oid_print(sbuff, parent_da, vp->da, false) <= 0) return;
393
394 /*
395 * Recursively print grouped attributes.
396 */
397 switch (vp->vp_type) {
399 fprintf(fp, "%*s%*s {\n", lvl * 2, "", (int) fr_sbuff_used(sbuff), fr_sbuff_start(sbuff));
400 _fr_pair_list_debug(fp, lvl + 1, vp, &vp->vp_group);
401 fprintf(fp, "%*s}\n", lvl * 2, "");
402 break;
403
404 default:
405 (void) fr_sbuff_in_strcpy(sbuff, " = ");
406 if (fr_value_box_print_quoted(sbuff, &vp->data, T_DOUBLE_QUOTED_STRING)< 0) break;
407
408 fprintf(fp, "%*s%*s\n", lvl * 2, "", (int) fr_sbuff_used(sbuff), fr_sbuff_start(sbuff));
409 }
410 }
411}
412
413/** Print a list of attributes and enumv
414 *
415 * @param[in] fp to output to.
416 * @param[in] lvl depth in structural attribute.
417 * @param[in] parent parent attribute
418 * @param[in] list to print.
419 */
420void _fr_pair_list_debug(FILE *fp, int lvl, fr_pair_t *parent, fr_pair_list_t const *list)
421{
422 fr_sbuff_t sbuff;
423 char buffer[1024];
424
425 buffer[0] = '\0';
426
427 fr_sbuff_init_out(&sbuff, buffer, sizeof(buffer));
428
429 fr_pair_list_debug_sbuff(fp, lvl, parent, list, &sbuff);
430}
431
432/** Dumps a list to the default logging destination - Useful for calling from debuggers
433 *
434 */
435void fr_pair_list_debug(FILE *fp, fr_pair_list_t const *list)
436{
437 _fr_pair_list_debug(fp, 0, NULL, list);
438}
439
440
441/** Dumps a pair to the default logging destination - Useful for calling from debuggers
442 *
443 */
444void fr_pair_debug(FILE *fp, fr_pair_t const *pair)
445{
446 fr_sbuff_t sbuff;
447 char buffer[1024];
448
449 buffer[0] = '\0';
450
451 fr_sbuff_init_out(&sbuff, buffer, sizeof(buffer));
452
453 (void) fr_pair_print(&sbuff, NULL, pair);
454
455 fprintf(fp, "%pV\n", fr_box_strvalue_len(fr_sbuff_start(&sbuff), fr_sbuff_used(&sbuff)));
456}
457
458static const char spaces[] = " ";
459
460static void fprintf_pair_list(FILE *fp, fr_pair_list_t const *list, int depth)
461{
462 fr_pair_list_foreach(list, vp) {
463 fprintf(fp, "%.*s", depth, spaces);
464
465 if (fr_type_is_leaf(vp->vp_type)) {
466 fr_fprintf(fp, "%s %s %pV\n", vp->da->name, fr_tokens[vp->op], &vp->data);
467 continue;
468 }
469
471
472 fprintf(fp, "%s = {\n", vp->da->name);
473 fprintf_pair_list(fp, &vp->vp_group, depth + 1);
474 fprintf(fp, "%.*s}\n", depth, spaces);
475 }
476}
477
478void fr_fprintf_pair_list(FILE *fp, fr_pair_list_t const *list)
479{
480 fprintf_pair_list(fp, list, 0);
481}
482
483/*
484 * print.c doesn't include pair.h, and doing so causes too many knock-on effects.
485 */
486void fr_fprintf_pair(FILE *fp, char const *msg, fr_pair_t const *vp)
487{
488 if (msg) fputs(msg, fp);
489
490 if (fr_type_is_leaf(vp->vp_type)) {
491 fr_fprintf(fp, "%s %s %pV\n", vp->da->name, fr_tokens[vp->op], &vp->data);
492 } else {
494
495 fprintf(fp, "%s = {\n", vp->da->name);
496 fprintf_pair_list(fp, &vp->vp_group, 1);
497 fprintf(fp, "}\n");
498 }
499}
static int const char char buffer[256]
Definition acutest.h:578
int const char * file
Definition acutest.h:704
log_entry msg
Definition acutest.h:796
int const char int line
Definition acutest.h:704
static void * fr_dcursor_next(fr_dcursor_t *cursor)
Advanced the cursor to the next item.
Definition dcursor.h:290
static void * fr_dcursor_next_peek(fr_dcursor_t *cursor)
Return the next iterator item without advancing the cursor.
Definition dcursor.h:305
#define FR_DICT_ATTR_OID_PRINT_RETURN(...)
Definition dict.h:757
void fr_log(fr_log_t const *log, fr_log_type_t type, char const *file, int line, char const *fmt,...)
Send a server log message to its destination.
Definition log.c:577
@ L_DBG
Only displayed when debugging is enabled.
Definition log.h:59
@ FR_TYPE_STRING
String of printable characters.
@ FR_TYPE_OCTETS
Raw octets.
@ FR_TYPE_GROUP
A grouping of other attributes.
long int ssize_t
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)
Definition minmax_heap.c:83
void _fr_pair_list_debug(FILE *fp, int lvl, fr_pair_t *parent, fr_pair_list_t const *list)
Print a list of attributes and enumv.
Definition pair_print.c:420
static void fprintf_pair_list(FILE *fp, fr_pair_list_t const *list, int depth)
Definition pair_print.c:460
void fr_fprintf_pair_list(FILE *fp, fr_pair_list_t const *list)
Definition pair_print.c:478
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:444
static void fr_pair_list_debug_sbuff(FILE *fp, int lvl, fr_pair_t *parent, fr_pair_list_t const *list, fr_sbuff_t *sbuff)
Definition pair_print.c:380
#define fr_pair_reset_parent(parent)
Definition pair_print.c:22
static void fr_pair_list_log_sbuff(fr_log_t const *log, int lvl, fr_pair_t *parent, fr_pair_list_t const *list, char const *file, int line, fr_sbuff_t *sbuff)
Definition pair_print.c:323
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:435
void _fr_pair_list_log(fr_log_t const *log, int lvl, fr_pair_t *parent, fr_pair_list_t const *list, char const *file, int line)
Print a list of attributes and enumv.
Definition pair_print.c:368
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:211
void fr_fprintf_pair(FILE *fp, char const *msg, fr_pair_t const *vp)
Definition pair_print.c:486
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:246
static 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
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
static const char spaces[]
Definition pair_print.c:458
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:299
ssize_t fr_fprintf(FILE *fp, char const *fmt,...)
Special version of fprintf which implements custom format specifiers.
Definition print.c:907
#define fr_assert(_expr)
Definition rad_assert.h:38
static char const * name
ssize_t fr_sbuff_in_strcpy(fr_sbuff_t *sbuff, char const *str)
Copy bytes into the sbuff up to the first \0.
Definition sbuff.c:1459
#define fr_sbuff_start(_sbuff_or_marker)
#define FR_SBUFF_IN_CHAR_RETURN(_sbuff,...)
#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:96
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:79
enum fr_token fr_token_t
@ T_INVALID
Definition token.h:39
@ T_DOUBLE_QUOTED_STRING
Definition token.h:121
#define T_TOKEN_LAST
Definition token.h:129
bool fr_pair_list_empty(fr_pair_list_t const *list)
Is a valuepair list empty.
#define PAIR_VERIFY(_x)
Definition pair.h:204
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:279
#define PAIR_VERIFY_WITH_LIST(_l, _x)
Definition pair.h:205
#define fr_pair_dcursor_init(_cursor, _list)
Initialises a special dcursor with callbacks that will maintain the attr sublists correctly.
Definition pair.h:605
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:857
#define fr_type_is_structural(_x)
Definition types.h:393
@ FR_TYPE_UNION
A union of limited children.
Definition types.h:82
#define FR_TYPE_STRUCTURAL
Definition types.h:317
#define fr_type_is_leaf(_x)
Definition types.h:394
static char const * fr_type_to_str(fr_type_t type)
Return a static string containing the type name.
Definition types.h:455
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:6254
#define fr_box_strvalue_len(_val, _len)
Definition value.h:308
static char const * fr_value_box_enum_name(fr_value_box_t const *box)
Decide if we need an enum prefix.
Definition value.h:1137
static size_t char ** out
Definition value.h:1023