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 one attribute and value to a string
102 *
103 * Print a fr_pair_t in the format:
104@verbatim
105 <attribute_name> <op> <value>
106@endverbatim
107 * to a string.
108 *
109 * @param[in] out Where to write the string.
110 * @param[in] parent If not NULL, only print OID components from
111 * this parent to the VP.
112 * @param[in] vp to print.
113 * @return
114 * - Length of data written to out.
115 * - value >= outlen on truncation.
116 */
118{
119 char const *token = NULL;
120 fr_sbuff_t our_out = FR_SBUFF(out);
121
123
124 if ((vp->op > T_INVALID) && (vp->op < T_TOKEN_LAST)) {
125 token = fr_tokens[vp->op];
126 } else {
127 token = "<INVALID-TOKEN>";
128 }
129
131
132 if (vp->vp_raw) FR_SBUFF_IN_STRCPY_LITERAL_RETURN(&our_out, "raw.");
133 FR_DICT_ATTR_OID_PRINT_RETURN(&our_out, parent, vp->da, false);
134 FR_SBUFF_IN_CHAR_RETURN(&our_out, ' ');
135 FR_SBUFF_IN_STRCPY_RETURN(&our_out, token);
136 FR_SBUFF_IN_CHAR_RETURN(&our_out, ' ');
137
138 if (fr_type_is_leaf(vp->vp_type) && vp->data.enumv && vp->data.enumv->flags.has_value) {
139 char const *name;
140
141 name = fr_dict_enum_name_by_value(vp->data.enumv, &vp->data);
142 if (!name) goto no_enumv;
143
144 FR_SBUFF_IN_CHAR_RETURN(&our_out, ':', ':');
146 } else {
147 no_enumv:
149 }
150
151 FR_SBUFF_SET_RETURN(out, &our_out);
152}
153
154/** Print one attribute and value to a string with escape rules
155 *
156 * Similar to fr_pair_print(), but secrets are omitted. This function duplicates parts of the functionality
157 * of fr_pair_print(). fr_pair_print_value_quoted(), and fr_value_box_print_quoted(), but for the special
158 * case of secure strings.
159 *
160 * Note that only secrets of type "string" and "octets" are omitted. Other "secret" data types are still
161 * printed as-is.
162 *
163 * "octets" are still printed as "<<< secret >>>". Which won't parse correctly, but that's fine. Because
164 * omitted data is not meant to be parsed into real data.
165 *
166 * @param[in] out Where to write the string.
167 * @param[in] parent If not NULL, only print OID components from
168 * this parent to the VP.
169 * @param[in] vp to print.
170
171 * @return
172 * - < 0 on error
173 * - Length of data written to out.
174 * - value >= outlen on truncation.
175 */
177{
178 char const *token = NULL;
179 fr_sbuff_t our_out = FR_SBUFF(out);
180
182
183 if ((vp->op > T_INVALID) && (vp->op < T_TOKEN_LAST)) {
184 token = fr_tokens[vp->op];
185 } else {
186 token = "<INVALID-TOKEN>";
187 }
188
190
191 if (vp->vp_raw) FR_SBUFF_IN_STRCPY_LITERAL_RETURN(&our_out, "raw.");
192 FR_DICT_ATTR_OID_PRINT_RETURN(&our_out, parent, vp->da, false);
193 FR_SBUFF_IN_CHAR_RETURN(&our_out, ' ');
194 FR_SBUFF_IN_STRCPY_RETURN(&our_out, token);
195 FR_SBUFF_IN_CHAR_RETURN(&our_out, ' ');
196
197 if (fr_type_is_leaf(vp->vp_type)) {
198 if (!vp->data.secret) {
200
201 } else {
202 switch (vp->vp_type) {
203 case FR_TYPE_STRING:
204 case FR_TYPE_OCTETS:
205 FR_SBUFF_IN_STRCPY_LITERAL_RETURN(&our_out, "<<< secret >>>");
206 break;
207
208 default:
209 fr_assert(0); /* see dict_tokenize.c, which enforces parsing of "secret" in dictionaries */
210 FR_SBUFF_IN_STRCPY_LITERAL_RETURN(&our_out, "<<< secret >>>");
211 break;
212 }
213 }
214 } else {
215 fr_pair_t *child;
216 fr_dcursor_t cursor;
217
218 FR_SBUFF_IN_CHAR_RETURN(&our_out, '{', ' ');
219 for (child = fr_pair_dcursor_init(&cursor, &vp->vp_group);
220 child != NULL;
221 child = fr_dcursor_next(&cursor)) {
222 FR_SBUFF_RETURN(fr_pair_print_secure, &our_out, vp->da, child);
223 if (fr_dcursor_next_peek(&cursor)) FR_SBUFF_IN_CHAR_RETURN(&our_out, ',', ' ');
224 }
225 FR_SBUFF_IN_CHAR_RETURN(&our_out, ' ', '}');
226 }
227
228 FR_SBUFF_SET_RETURN(out, &our_out);
229}
230
231/** Print a pair list
232 *
233 * @param[in] out Where to write the string.
234 * @param[in] parent parent da to start from
235 * @param[in] list pair list
236 * @return
237 * - Length of data written to out.
238 * - value >= outlen on truncation.
239 */
241{
242 fr_pair_t *vp;
243 fr_sbuff_t our_out = FR_SBUFF(out);
244
245 vp = fr_pair_list_head(list);
246 if (!vp) {
248 return fr_sbuff_used(out);
249 }
250
252
253 while (true) {
255 vp = fr_pair_list_next(list, vp);
256 if (!vp) break;
257
258 FR_SBUFF_IN_STRCPY_LITERAL_RETURN(&our_out, ", ");
259 }
260
261 FR_SBUFF_SET_RETURN(out, &our_out);
262}
263
264static 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)
265{
266 fr_dict_attr_t const *parent_da = NULL;
267
268 fr_pair_list_foreach(list, vp) {
270
271 fr_sbuff_set_to_start(sbuff);
272
273 if (vp->vp_raw) (void) fr_sbuff_in_strcpy(sbuff, "raw.");
274
275 if (parent && (parent->vp_type != FR_TYPE_GROUP)) parent_da = parent->da;
276 if (fr_dict_attr_oid_print(sbuff, parent_da, vp->da, false) <= 0) return;
277
278 /*
279 * Recursively print grouped attributes.
280 */
281 switch (vp->vp_type) {
283 fr_log(log, L_DBG, file, line, "%*s%*s {", lvl * 2, "",
284 (int) fr_sbuff_used(sbuff), fr_sbuff_start(sbuff));
285 _fr_pair_list_log(log, lvl + 1, vp, &vp->vp_group, file, line);
286 fr_log(log, L_DBG, file, line, "%*s}", lvl * 2, "");
287 break;
288
289 default:
290 (void) fr_sbuff_in_strcpy(sbuff, " = ");
291 if (fr_value_box_print_quoted(sbuff, &vp->data, T_DOUBLE_QUOTED_STRING)< 0) break;
292
293 fr_log(log, L_DBG, file, line, "%*s%*s", lvl * 2, "",
294 (int) fr_sbuff_used(sbuff), fr_sbuff_start(sbuff));
295 }
296 }
297}
298
299
300/** Print a list of attributes and enumv
301 *
302 * @param[in] log to output to.
303 * @param[in] lvl depth in structural attribute.
304 * @param[in] parent parent attribute
305 * @param[in] list to print.
306 * @param[in] file where the message originated
307 * @param[in] line where the message originated
308 */
309void _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)
310{
311 fr_sbuff_t sbuff;
312 char buffer[1024];
313
314 fr_sbuff_init_out(&sbuff, buffer, sizeof(buffer));
315
316 fr_pair_list_log_sbuff(log, lvl, parent, list, file, line, &sbuff);
317}
318
319/** Dumps a list to the default logging destination - Useful for calling from debuggers
320 *
321 */
323{
324 _fr_pair_list_log(&default_log, 0, NULL, list, "<internal>", 0);
325}
326
327
328/** Dumps a pair to the default logging destination - Useful for calling from debuggers
329 *
330 */
331void fr_pair_debug(fr_pair_t const *pair)
332{
333 fr_sbuff_t sbuff;
334 char buffer[1024];
335
336 fr_sbuff_init_out(&sbuff, buffer, sizeof(buffer));
337
338 (void) fr_pair_print(&sbuff, NULL, pair);
339
340 fr_log(&default_log, L_DBG, __FILE__, __LINE__, "%pV",
342}
static int const char char buffer[256]
Definition acutest.h:576
int const char * file
Definition acutest.h:702
int const char int line
Definition acutest.h:702
static void * fr_dcursor_next(fr_dcursor_t *cursor)
Advanced the cursor to the next item.
Definition dcursor.h:288
static void * fr_dcursor_next_peek(fr_dcursor_t *cursor)
Return the next iterator item without advancing the cursor.
Definition dcursor.h:303
char const * fr_dict_enum_name_by_value(fr_dict_attr_t const *da, fr_value_box_t const *value)
Lookup the name of an enum value in a fr_dict_attr_t.
Definition dict_util.c:3386
#define FR_DICT_ATTR_OID_PRINT_RETURN(...)
Definition dict.h:699
fr_log_t default_log
Definition log.c:291
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:583
@ 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(fr_pair_list_t const *list)
Dumps a list to the default logging destination - Useful for calling from debuggers.
Definition pair_print.c:322
#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:264
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:309
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:117
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:176
void fr_pair_debug(fr_pair_t const *pair)
Dumps a pair to the default logging destination - Useful for calling from debuggers.
Definition pair_print.c:331
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
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:240
#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:1454
#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:78
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:191
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:70
#define fr_pair_list_foreach(_list_head, _iter)
Iterate over the contents of a fr_pair_list_t.
Definition pair.h:261
#define PAIR_VERIFY_WITH_LIST(_l, _x)
Definition pair.h:192
#define fr_pair_dcursor_init(_cursor, _list)
Initialises a special dcursor with callbacks that will maintain the attr sublists correctly.
Definition pair.h:591
fr_pair_t * fr_pair_list_head(fr_pair_list_t const *list)
Get the head of a valuepair list.
Definition pair_inline.c:43
static fr_slen_t parent
Definition pair.h:851
#define FR_TYPE_STRUCTURAL
Definition types.h:296
#define fr_type_is_leaf(_x)
Definition types.h:372
static char const * fr_type_to_str(fr_type_t type)
Return a static string containing the type name.
Definition types.h:433
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:5586
#define fr_box_strvalue_len(_val, _len)
Definition value.h:286
static size_t char ** out
Definition value.h:997