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 && (parent->type == FR_TYPE_GROUP)) { \
24 parent = fr_dict_attr_ref(parent); \
25 if (parent->flags.is_root) parent = NULL; \
26 } \
27 if (parent && (parent == vp->da)) parent = NULL; \
28 } while (0)
29
30/** Pair serialisation API
31 *
32 * @file src/lib/util/pair_print.c
33 *
34 * @copyright 2020 The FreeRADIUS server project
35 */
36#include <freeradius-devel/util/pair.h>
37#include <freeradius-devel/util/talloc.h>
38#include <freeradius-devel/util/proto.h>
39#include <freeradius-devel/util/pair_legacy.h>
40
41/** Print the value of an attribute to a string
42 *
43 * @param[in] out Where to write the string.
44 * @param[in] vp to print.
45 * @param[in] quote Char to add before and after printed value,
46 * if 0 no char will be added, if < 0 raw string
47 * will be added.
48 * @return
49 * - >= 0 length of data written to out.
50 * - <0 the number of bytes we would have needed to write
51 * the complete string to out.
52 */
54{
55 fr_sbuff_t our_out;
56 ssize_t slen;
57
59
60 our_out = FR_SBUFF(out);
61
62 switch (vp->vp_type) {
63 /*
64 * For structural types descend down
65 */
67 if (fr_pair_list_empty(&vp->vp_group)) {
68 FR_SBUFF_IN_CHAR_RETURN(&our_out, '{', ' ', '}');
69
70 } else {
71 FR_SBUFF_IN_CHAR_RETURN(&our_out, '{', ' ');
72
73 FR_SBUFF_RETURN(fr_pair_list_print, &our_out, vp->da, &vp->vp_group);
74
75 FR_SBUFF_IN_CHAR_RETURN(&our_out, ' ', '}');
76 }
77
78 FR_SBUFF_SET_RETURN(out, &our_out);
79
80 /*
81 * For simple types just print the box
82 */
83 default:
84 /*
85 * If it's raw / unknown and not octets, print the cast before the type.
86 *
87 * Otherwise on parsing, we don't know how to interpret the value. :(
88 */
89 if ((vp->da->flags.is_raw || vp->da->flags.is_unknown) &&
90 (vp->vp_type != FR_TYPE_OCTETS)) {
91 FR_SBUFF_IN_SPRINTF_RETURN(&our_out, "(%s) ", fr_type_to_str(vp->vp_type));
92 }
93
94 slen = fr_value_box_print_quoted(&our_out, &vp->data, quote);
95 if (slen <= 0) return slen;
96 }
97
98 FR_SBUFF_SET_RETURN(out, &our_out);
99}
100
101/** Print either a quoted value, an enum, or a normal value.
102 *
103 */
105{
106 fr_sbuff_t our_out = FR_SBUFF(out);
107 char const *name;
108
109 if ((name = fr_value_box_enum_name(&vp->data)) != NULL) {
110 FR_SBUFF_IN_CHAR_RETURN(&our_out, ':', ':');
112 } else {
113
115 }
116
117 FR_SBUFF_SET_RETURN(out, &our_out);
118}
119
120/** Print one attribute and value to a string
121 *
122 * Print a fr_pair_t in the format:
123@verbatim
124 <attribute_name> <op> <value>
125@endverbatim
126 * to a string.
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] vp to print.
132 * @return
133 * - Length of data written to out.
134 * - value >= outlen on truncation.
135 */
137{
138 char const *token = NULL;
139 fr_sbuff_t our_out = FR_SBUFF(out);
140
142
143 /*
144 * Omit the union if we can.
145 */
146 if ((vp->vp_type == FR_TYPE_UNION) &&
147 (fr_pair_list_num_elements(&vp->vp_group) == 1)) {
148 parent = vp->da;
149 vp = fr_pair_list_head(&vp->vp_group);
150 }
151
152 if ((vp->op > T_INVALID) && (vp->op < T_TOKEN_LAST)) {
153 token = fr_tokens[vp->op];
154 } else {
155 token = "<INVALID-TOKEN>";
156 }
157
159
160 if (vp->vp_raw) FR_SBUFF_IN_STRCPY_LITERAL_RETURN(&our_out, "raw.");
161 FR_DICT_ATTR_OID_PRINT_RETURN(&our_out, parent, vp->da, false);
162 FR_SBUFF_IN_CHAR_RETURN(&our_out, ' ');
163 FR_SBUFF_IN_STRCPY_RETURN(&our_out, token);
164 FR_SBUFF_IN_CHAR_RETURN(&our_out, ' ');
165
167
168 FR_SBUFF_SET_RETURN(out, &our_out);
169}
170
171/** Print one attribute and value to a string with escape rules
172 *
173 * Similar to fr_pair_print(), but secrets are omitted. This function duplicates parts of the functionality
174 * of fr_pair_print(). fr_pair_print_value_quoted(), and fr_value_box_print_quoted(), but for the special
175 * case of secure strings.
176 *
177 * Note that only secrets of type "string" and "octets" are omitted. Other "secret" data types are still
178 * printed as-is.
179 *
180 * "octets" are still printed as "<<< secret >>>". Which won't parse correctly, but that's fine. Because
181 * omitted data is not meant to be parsed into real data.
182 *
183 * @param[in] out Where to write the string.
184 * @param[in] parent If not NULL, only print OID components from
185 * this parent to the VP.
186 * @param[in] vp to print.
187
188 * @return
189 * - < 0 on error
190 * - Length of data written to out.
191 * - value >= outlen on truncation.
192 */
194{
195 char const *token = NULL;
196 fr_sbuff_t our_out = FR_SBUFF(out);
197
199
200 if ((vp->op > T_INVALID) && (vp->op < T_TOKEN_LAST)) {
201 token = fr_tokens[vp->op];
202 } else {
203 token = "<INVALID-TOKEN>";
204 }
205
207
208 if (vp->vp_raw) FR_SBUFF_IN_STRCPY_LITERAL_RETURN(&our_out, "raw.");
209 FR_DICT_ATTR_OID_PRINT_RETURN(&our_out, parent, vp->da, false);
210 FR_SBUFF_IN_CHAR_RETURN(&our_out, ' ');
211 FR_SBUFF_IN_STRCPY_RETURN(&our_out, token);
212 FR_SBUFF_IN_CHAR_RETURN(&our_out, ' ');
213
214 if (fr_type_is_leaf(vp->vp_type)) {
215 if (!vp->data.secret) {
217
218 } else {
219 switch (vp->vp_type) {
220 case FR_TYPE_STRING:
221 case FR_TYPE_OCTETS:
222 FR_SBUFF_IN_STRCPY_LITERAL_RETURN(&our_out, "<<< secret >>>");
223 break;
224
225 default:
226 fr_assert(0); /* see dict_tokenize.c, which enforces parsing of "secret" in dictionaries */
227 FR_SBUFF_IN_STRCPY_LITERAL_RETURN(&our_out, "<<< secret >>>");
228 break;
229 }
230 }
231 } else {
232 fr_pair_t *child;
233 fr_dcursor_t cursor;
234
236
237 FR_SBUFF_IN_CHAR_RETURN(&our_out, '{', ' ');
238 for (child = fr_pair_dcursor_init(&cursor, &vp->vp_group);
239 child != NULL;
240 child = fr_dcursor_next(&cursor)) {
241 FR_SBUFF_RETURN(fr_pair_print_secure, &our_out, vp->da, child);
242 if (fr_dcursor_next_peek(&cursor)) FR_SBUFF_IN_CHAR_RETURN(&our_out, ',', ' ');
243 }
244 FR_SBUFF_IN_CHAR_RETURN(&our_out, ' ', '}');
245 }
246
247 FR_SBUFF_SET_RETURN(out, &our_out);
248}
249
250/** Print a pair list
251 *
252 * @param[in] out Where to write the string.
253 * @param[in] parent parent da to start from
254 * @param[in] list pair list
255 * @return
256 * - Length of data written to out.
257 * - value >= outlen on truncation.
258 */
260{
261 fr_pair_t *vp;
262 fr_sbuff_t our_out = FR_SBUFF(out);
263
264 vp = fr_pair_list_head(list);
265 if (!vp) {
267 return fr_sbuff_used(out);
268 }
269
271
272 while (true) {
274 vp = fr_pair_list_next(list, vp);
275 if (!vp) break;
276
277 FR_SBUFF_IN_STRCPY_LITERAL_RETURN(&our_out, ", ");
278 }
279
280 FR_SBUFF_SET_RETURN(out, &our_out);
281}
282
283static 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)
284{
285 fr_dict_attr_t const *parent_da = NULL;
286
287 fr_pair_list_foreach(list, vp) {
289
290 fr_sbuff_set_to_start(sbuff);
291
292 if (vp->vp_raw) (void) fr_sbuff_in_strcpy(sbuff, "raw.");
293
294 if (parent && (parent->vp_type != FR_TYPE_GROUP)) parent_da = parent->da;
295 if (fr_dict_attr_oid_print(sbuff, parent_da, vp->da, false) <= 0) return;
296
297 /*
298 * Recursively print grouped attributes.
299 */
300 switch (vp->vp_type) {
302 fr_log(log, L_DBG, file, line, "%*s%*s {", lvl * 2, "",
303 (int) fr_sbuff_used(sbuff), fr_sbuff_start(sbuff));
304 _fr_pair_list_log(log, lvl + 1, vp, &vp->vp_group, file, line);
305 fr_log(log, L_DBG, file, line, "%*s}", lvl * 2, "");
306 break;
307
308 default:
309 (void) fr_sbuff_in_strcpy(sbuff, " = ");
310 if (fr_pair_print_value(sbuff, vp) < 0) break;
311
312 fr_log(log, L_DBG, file, line, "%*s%*s", lvl * 2, "",
313 (int) fr_sbuff_used(sbuff), fr_sbuff_start(sbuff));
314 }
315 }
316}
317
318
319/** Print a list of attributes and enumv
320 *
321 * @param[in] log to output to.
322 * @param[in] lvl depth in structural attribute.
323 * @param[in] parent parent attribute
324 * @param[in] list to print.
325 * @param[in] file where the message originated
326 * @param[in] line where the message originated
327 */
328void _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)
329{
330 fr_sbuff_t sbuff;
331 char buffer[1024];
332
333 fr_sbuff_init_out(&sbuff, buffer, sizeof(buffer));
334
335 fr_pair_list_log_sbuff(log, lvl, parent, list, file, line, &sbuff);
336}
337
338static void fr_pair_list_debug_sbuff(FILE *fp, int lvl, fr_pair_t *parent, fr_pair_list_t const *list, fr_sbuff_t *sbuff)
339{
340 fr_dict_attr_t const *parent_da = NULL;
341
342 fr_pair_list_foreach(list, vp) {
344
345 fr_sbuff_set_to_start(sbuff);
346
347 if (vp->vp_raw) (void) fr_sbuff_in_strcpy(sbuff, "raw.");
348
349 if (parent && (parent->vp_type != FR_TYPE_GROUP)) parent_da = parent->da;
350 if (fr_dict_attr_oid_print(sbuff, parent_da, vp->da, false) <= 0) return;
351
352 /*
353 * Recursively print grouped attributes.
354 */
355 switch (vp->vp_type) {
357 fprintf(fp, "%*s%*s {\n", lvl * 2, "", (int) fr_sbuff_used(sbuff), fr_sbuff_start(sbuff));
358 _fr_pair_list_debug(fp, lvl + 1, vp, &vp->vp_group);
359 fprintf(fp, "%*s}\n", lvl * 2, "");
360 break;
361
362 default:
363 (void) fr_sbuff_in_strcpy(sbuff, " = ");
364 if (fr_value_box_print_quoted(sbuff, &vp->data, T_DOUBLE_QUOTED_STRING)< 0) break;
365
366 fprintf(fp, "%*s%*s\n", lvl * 2, "", (int) fr_sbuff_used(sbuff), fr_sbuff_start(sbuff));
367 }
368 }
369}
370
371/** Print a list of attributes and enumv
372 *
373 * @param[in] fp to output to.
374 * @param[in] lvl depth in structural attribute.
375 * @param[in] parent parent attribute
376 * @param[in] list to print.
377 */
378void _fr_pair_list_debug(FILE *fp, int lvl, fr_pair_t *parent, fr_pair_list_t const *list)
379{
380 fr_sbuff_t sbuff;
381 char buffer[1024];
382
383 fr_sbuff_init_out(&sbuff, buffer, sizeof(buffer));
384
385 fr_pair_list_debug_sbuff(fp, lvl, parent, list, &sbuff);
386}
387
388/** Dumps a list to the default logging destination - Useful for calling from debuggers
389 *
390 */
391void fr_pair_list_debug(FILE *fp, fr_pair_list_t const *list)
392{
393 _fr_pair_list_debug(fp, 0, NULL, list);
394}
395
396
397/** Dumps a pair to the default logging destination - Useful for calling from debuggers
398 *
399 */
400void fr_pair_debug(FILE *fp, fr_pair_t const *pair)
401{
402 fr_sbuff_t sbuff;
403 char buffer[1024];
404
405 fr_sbuff_init_out(&sbuff, buffer, sizeof(buffer));
406
407 (void) fr_pair_print(&sbuff, NULL, pair);
408
409 fprintf(fp, "%pV\n", fr_box_strvalue_len(fr_sbuff_start(&sbuff), fr_sbuff_used(&sbuff)));
410}
static int const char char buffer[256]
Definition acutest.h:578
int const char * file
Definition acutest.h:704
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:734
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)
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:378
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:400
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:338
#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:283
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:391
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:328
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:136
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:193
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:53
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:104
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:259
#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:193
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:263
#define PAIR_VERIFY_WITH_LIST(_l, _x)
Definition pair.h:194
#define fr_pair_dcursor_init(_cursor, _list)
Initialises a special dcursor with callbacks that will maintain the attr sublists correctly.
Definition pair.h:589
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:841
#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:6015
#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:1130
static size_t char ** out
Definition value.h:1023