The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
pair_legacy.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/** AVP manipulation and search API
18 *
19 * @file src/lib/util/pair_legacy.c
20 *
21 * @copyright 2000,2006,2015 The FreeRADIUS server project
22 */
23
24RCSID("$Id: 1bfd0dc6734a8f774c27348d59da16112a2824d3 $")
25
26#include <freeradius-devel/util/dict.h>
27#include <freeradius-devel/util/pair.h>
28#include <freeradius-devel/util/pair_legacy.h>
29#include <freeradius-devel/util/misc.h>
30#include <freeradius-devel/util/proto.h>
31#include <freeradius-devel/util/regex.h>
32
33#include <freeradius-devel/protocol/radius/rfc2865.h>
34#include <freeradius-devel/protocol/freeradius/freeradius.internal.h>
35
38 L("\t"),
39 L("\n"),
40 L(" "),
41 L("!*"),
42 L("!="),
43 L("!~"),
44 L("&&"), /* Logical operator */
45 L(")"), /* Close condition/sub-condition */
46 L("+="),
47 L("-="),
48 L(":="),
49 L("<"),
50 L("<="),
51 L("=*"),
52 L("=="),
53 L("=~"),
54 L(">"),
55 L(">="),
56 L("||"), /* Logical operator */
57 );
58
60 { L("+="), T_OP_ADD_EQ },
61 { L(":="), T_OP_EQ },
62 { L("="), T_OP_EQ },
63};
65
67 { L("!*"), T_OP_CMP_FALSE },
68 { L("!="), T_OP_NE },
69 { L("!~"), T_OP_REG_NE },
70 { L("+="), T_OP_ADD_EQ },
71 { L(":="), T_OP_SET },
72 { L("<"), T_OP_LT },
73 { L("<="), T_OP_LE },
74 { L("="), T_OP_EQ },
75 { L("=*"), T_OP_CMP_TRUE },
76 { L("=="), T_OP_CMP_EQ },
77 { L("=~"), T_OP_REG_EQ },
78 { L(">"), T_OP_GT },
79 { L(">="), T_OP_GE }
80};
82
83/*
84 * Stop parsing bare words at whitespace, comma, or end of list.
85 *
86 * Note that we don't allow escaping of bare words here, as that screws up parsing of raw attributes with
87 * 0x... prefixes.
88 */
89static fr_sbuff_parse_rules_t const bareword_unquoted = {
90 .terminals = &FR_SBUFF_TERMS(
91 L(""),
92 L("\t"),
93 L("\n"),
94 L("\r"),
95 L(" "),
96 L(","),
97 L("}")
98 )
99};
100
101
103{
104 char quote;
105 ssize_t slen;
106 fr_sbuff_parse_rules_t const *rules;
107
108 if (fr_sbuff_next_if_char(in, '"')) {
110 quote = '"';
111
112 } else if (fr_sbuff_next_if_char(in, '\'')) {
114 quote = '\'';
115
116 /*
117 * We don't support backticks here.
118 */
119 } else if (fr_sbuff_is_char(in, '\'')) {
120 fr_strerror_const("Backticks are not supported here");
121 return 0;
122
123 } else {
124 rules = &bareword_unquoted;
125 quote = '\0';
126 }
127
128 slen = fr_value_box_from_substr(vp, &vp->data, vp->da->type, vp->da, in, rules);
129 if (slen < 0) {
130 fr_assert(slen >= -((ssize_t) 1 << 20));
131 return slen - (quote != 0);
132 }
133
134 if (quote && !fr_sbuff_next_if_char(in, quote)) {
135 fr_strerror_const("Unterminated string");
136 return 0;
137 }
138
139 fr_assert(slen <= ((ssize_t) 1 << 20));
140
141 return slen + ((quote != 0) << 1);
142}
143
144/** Parse a #fr_pair_list_t from a substring
145 *
146 * @param[in] root where we start parsing from
147 * @param[in,out] relative where we left off, or where we should continue from
148 * @param[in] in input sbuff
149 * @return
150 * - <0 on error
151 * - 0 on no input
152 * - >0 on how many bytes of input we read
153 *
154 */
156 fr_sbuff_t *in)
157{
158 int i, components;
159 bool raw;
160 bool was_relative = false;
161 bool append;
162 bool keep_going;
163 fr_type_t raw_type;
164 fr_token_t op;
165 fr_slen_t slen;
166 fr_pair_t *vp;
167 fr_dict_attr_t const *internal = NULL;
168 fr_sbuff_marker_t lhs_m, rhs_m;
169 fr_sbuff_t our_in = FR_SBUFF(in);
170
171 if (unlikely(!root->ctx)) {
172 fr_strerror_const("Missing ctx fr_pair_parse_t");
173 return -1;
174 }
175
176 if (unlikely(!root->da)) {
177 fr_strerror_const("Missing namespace attribute");
178 return -1;
179 }
180
181 if (unlikely(!root->list)) {
182 fr_strerror_const("Missing list");
183 return -1;
184 }
185
187 if (internal == root->da) internal = NULL;
188
189 if (fr_sbuff_remaining(&our_in) == 0) return 0;
190
191redo:
192 append = true;
193 raw = false;
194 raw_type = FR_TYPE_NULL;
195 relative->last_char = 0;
196 vp = NULL;
197
198 fr_sbuff_adv_past_whitespace(&our_in, SIZE_MAX, NULL);
199
200 /*
201 * Relative attributes start from the input list / parent.
202 *
203 * Absolute attributes start from the root list / parent.
204 *
205 * Once we decide where we are coming from, all subsequent operations are on the "relative"
206 * structure.
207 */
208 if (!fr_sbuff_next_if_char(&our_in, '.')) {
209 *relative = *root;
210
211 append = !was_relative;
212 was_relative = false;
213
214 /*
215 * Be nice to people who expect to see '&' everywhere.
216 */
217 (void) fr_sbuff_next_if_char(&our_in, '&');
218
219 /*
220 * Raw attributes can only be at our root.
221 *
222 * "raw.foo" means that SOME component of the OID is raw. But the starting bits might be known.
223 */
224 if (fr_sbuff_is_str_literal(&our_in, "raw.")) {
225 raw = true;
226 fr_sbuff_advance(&our_in, 4);
227 }
228 } else if (!relative->ctx || !relative->da || !relative->list) {
229 fr_strerror_const("The '.Attribute' syntax can only be used if the previous attribute is structural, and the line ends with ','");
230 return -1;
231 } else {
232 was_relative = true;
233 }
234
235 /*
236 * Set the LHS marker to be after any initial '.'
237 */
238 fr_sbuff_marker(&lhs_m, &our_in);
239
240 /*
241 * Skip over the attribute name. We need to get the operator _before_ creating the VPs.
242 */
243 components = 0;
244 do {
245 if (fr_sbuff_adv_past_allowed(&our_in, SIZE_MAX, fr_dict_attr_allowed_chars, NULL) == 0) break;
246 components++;
247 } while (fr_sbuff_next_if_char(&our_in, '.'));
248
249 /*
250 * Couldn't find anything.
251 */
252 if (!components) {
253 fr_strerror_const("Empty input");
254 return 0;
255 }
256
257 fr_sbuff_adv_past_whitespace(&our_in, SIZE_MAX, NULL);
258
259 /*
260 * Look for the operator.
261 */
262 if (relative->allow_compare) {
264 } else {
266 }
267 if (op == T_INVALID) {
268 fr_strerror_const("Expecting operator");
269 return fr_sbuff_error(&our_in);
270 }
271
272 /*
273 * Skip past whitespace, and set a marker at the RHS. Then reset the input to the LHS attribute
274 * name, so that we can go back and parse / create the attributes.
275 */
276 fr_sbuff_adv_past_whitespace(&our_in, SIZE_MAX, NULL);
277
278 fr_sbuff_marker(&rhs_m, &our_in);
279
280 /*
281 * If the value of the attribute is 0x..., then we always force the raw type to be octets, even
282 * if the attribute is named and known. e.g. raw.Framed-IP-Address = 0x01
283 *
284 * OR if the attribute is entirely unknown (and not a raw version of a known one), then we allow a
285 * cast to set the data type.
286 */
287 if (raw) {
288 if (fr_sbuff_is_str_literal(&our_in, "0x")) {
289 raw_type = FR_TYPE_OCTETS;
290
291 } else if (fr_sbuff_next_if_char(&our_in, '(')) {
293
294 fr_sbuff_marker(&m, &our_in);
295
296 fr_sbuff_out_by_longest_prefix(&slen, &raw_type, fr_type_table, &our_in, FR_TYPE_NULL);
297 if ((raw_type == FR_TYPE_NULL) || !fr_type_is_leaf(raw_type)) {
298 fr_sbuff_set(&our_in, &rhs_m);
299 fr_strerror_const("Invalid data type in cast");
300 return fr_sbuff_error(&our_in);
301 }
302
303 if (!fr_sbuff_next_if_char(&our_in, ')')) {
304 fr_strerror_const("Missing ')' in cast");
305 return fr_sbuff_error(&our_in);
306 }
307
308 fr_sbuff_adv_past_whitespace(&our_in, SIZE_MAX, NULL);
309 fr_sbuff_marker(&rhs_m, &our_in);
310
311 } else if (fr_sbuff_is_char(&our_in, '{')) {
312 raw_type = FR_TYPE_TLV;
313 }
314 }
315
316 fr_sbuff_set(&our_in, &lhs_m);
317
318 /*
319 * Parse each OID component, creating pairs along the way.
320 */
321 i = 1;
322 do {
324 fr_dict_attr_t const *da = NULL;
325 fr_dict_attr_t const *da_unknown = NULL;
326
327 slen = fr_dict_oid_component(&err, &da, relative->da, &our_in, &bareword_terminals);
328 if (err == FR_DICT_ATTR_NOTFOUND) {
329 if (raw) {
330 /*
331 * We looked up raw.FOO, and FOO wasn't found. The component must be a number.
332 */
333 if (!fr_sbuff_is_digit(&our_in)) goto notfound;
334
335 if (raw_type == FR_TYPE_NULL) {
336 raw_type = FR_TYPE_OCTETS;
337
338 } else if (raw_type == FR_TYPE_TLV) {
339 /*
340 * Reset the type based on the parent.
341 */
342 if (relative->da->type == FR_TYPE_VSA) {
343 raw_type = FR_TYPE_VENDOR;
344 }
345 }
346
347 slen = fr_dict_attr_unknown_afrom_oid_substr(root->ctx, &da_unknown, relative->da, &our_in, raw_type);
348 if (slen < 0) return fr_sbuff_error(&our_in) + slen;
349
350 fr_assert(da_unknown);
351
352 /*
353 * Append from the root list, starting at the root depth.
354 */
355 vp = fr_pair_afrom_da_depth_nested(root->ctx, root->list, da_unknown,
356 root->da->depth);
357 fr_dict_attr_unknown_free(&da_unknown);
358
359 if (!vp) return fr_sbuff_error(&our_in);
360
362
363 /*
364 * The above function MAY have jumped ahead a few levels. Ensure
365 * that the relative structure is set correctly for the parent,
366 * but only if the parent changed.
367 */
368 if (relative->da != vp->da->parent) {
369 fr_pair_t *parent_vp;
370
371 parent_vp = fr_pair_parent(vp);
372 fr_assert(parent_vp);
373
374 relative->ctx = parent_vp;
375 relative->da = parent_vp->da;
376 relative->list = &parent_vp->vp_group;
377 }
378
379 /*
380 * Update the new relative information for the current VP, which
381 * may be structural, or a key field.
382 */
383 fr_assert(!fr_sbuff_is_char(&our_in, '.')); /* be sure the loop exits */
384 goto update_relative;
385 }
386
387 if (internal) {
388 slen = fr_dict_oid_component(&err, &da, internal, &our_in, &bareword_terminals);
389 }
390
391 } else if (raw && fr_type_is_structural(da->type) && (raw_type != FR_TYPE_OCTETS)) {
392 /*
393 * We were asked to do a "raw" thing, but we found a known attribute matching
394 * that description.
395 *
396 * @todo - this is only allowed because we can't distinguish between "raw.1" and
397 * "raw.User-Name".
398 */
399 raw = false;
400 raw_type = FR_TYPE_NULL;
401 }
402
403 if (err != FR_DICT_ATTR_OK) {
404 notfound:
405 fr_sbuff_marker(&rhs_m, &our_in);
407
408 fr_strerror_printf("Unknown attribute \"%.*s\" for parent \"%s\"",
409 (int) fr_sbuff_diff(&our_in, &rhs_m), fr_sbuff_current(&rhs_m),
410 relative->da->name);
411 return fr_sbuff_error(&our_in);
412 }
413 fr_assert(da != NULL);
414
415#if 0
416 /*
417 * @todo - If we're at the root, then aliases can cause us to jump over intermediate
418 * attributes. In which case we have to create the intermediate attributes, too.
419 */
420 if (relative->da) {
421 if (relative->da->flags.is_root) {
422 fr_assert(da->depth == 1);
423 }
424 }
425#endif
426
427 /*
428 * Intermediate components are always found / created. The final component is
429 * always appended, no matter the operator.
430 */
431 if (i < components) {
432 if (append) {
433 vp = fr_pair_find_last_by_da(relative->list, NULL, da);
434 if (!vp) {
435 if (fr_pair_append_by_da(relative->ctx, &vp, relative->list, da) < 0) {
436 return fr_sbuff_error(&our_in);
437 }
438 }
439 } else {
440 vp = fr_pair_afrom_da(relative->ctx, da);
441 if (!vp) return fr_sbuff_error(&our_in);
442
443 fr_pair_append(relative->list, vp);
444 }
445
446 /*
447 * We had a raw type and we're passing
448 * raw octets to it. We don't care if
449 * its structural or anything else. Just
450 * create the raw attribute.
451 */
452 } else if (raw_type != FR_TYPE_NULL) {
453 /*
454 * We have parsed the full OID tree, *and* found a known attribute. e.g. raw.Vendor-Specific = ...
455 *
456 * For some reason, we allow: raw.Vendor-Specific = { ... }
457 *
458 * But this is what we really want: raw.Vendor-Specific = 0xabcdef
459 */
460 fr_assert(!da_unknown);
461
462 if ((raw_type != FR_TYPE_OCTETS) && (raw_type != da->type)) {
463 fr_strerror_printf("Cannot create raw attribute %s which changes data type from %s to %s",
464 da->name, fr_type_to_str(da->type), fr_type_to_str(raw_type));
465 return fr_sbuff_error(&our_in);
466 }
467
468 /*
469 * If we're parsing raw octets, create a raw octets attribute.
470 *
471 * Otherwise create one of type 'tlv', and then parse the children.
472 */
473 if (raw_type == FR_TYPE_OCTETS) {
474 da_unknown = fr_dict_attr_unknown_raw_afrom_da(root->ctx, da);
475 } else {
476 da_unknown = fr_dict_attr_unknown_afrom_da(root->ctx, da);
477 }
478 if (!da_unknown) return fr_sbuff_error(&our_in);
479
480 if (fr_pair_append_by_da(relative->ctx, &vp, relative->list, da_unknown) < 0) {
481 fr_dict_attr_unknown_free(&da_unknown);
482 return fr_sbuff_error(&our_in);
483 }
484
485 fr_dict_attr_unknown_free(&da_unknown);
486
487 /*
488 * Just create the leaf attribute.
489 */
490 } else if (da->parent->type == FR_TYPE_STRUCT) {
491 fr_pair_t *tail = fr_pair_list_tail(relative->list);
492
493 /*
494 * If the structure member is _less_ than the last one, go create a new structure
495 * in the grandparent.
496 */
497 if (tail && (tail->da->attr >= da->attr) && !da->flags.array) {
498 fr_pair_t *parent_vp, *grand_vp;
499
500 parent_vp = fr_pair_list_parent(relative->list);
501 if (!parent_vp) goto leaf;
502
503 fr_assert(da->parent == parent_vp->da);
504
505 grand_vp = fr_pair_parent(parent_vp);
506 if (!grand_vp) goto leaf;
507
508 /*
509 * Create a new parent in the context of the grandparent.
510 */
511 if (fr_pair_append_by_da(grand_vp, &vp, &grand_vp->vp_group, parent_vp->da) < 0) {
512 return fr_sbuff_error(&our_in);
513 }
514
515 relative->ctx = vp;
516 fr_assert(relative->da == vp->da);
517 relative->list = &vp->vp_group;
518 }
519
520 goto leaf;
521 } else {
522 leaf:
523 if (fr_pair_append_by_da(relative->ctx, &vp, relative->list, da) < 0) {
524 return fr_sbuff_error(&our_in);
525 }
526 }
527
528 fr_assert(vp != NULL);
529
530 update_relative:
531 /*
532 * Reset the parsing to the new namespace if necessary.
533 */
534 switch (vp->vp_type) {
535 case FR_TYPE_TLV:
536 case FR_TYPE_STRUCT:
537 case FR_TYPE_VSA:
538 case FR_TYPE_VENDOR:
539 relative->ctx = vp;
540 relative->da = vp->da;
541 relative->list = &vp->vp_group;
542 break;
543
544 /*
545 * Groups reset the namespace to the da referenced by the group.
546 *
547 * Internal groups get their namespace to the root namespace.
548 */
549 case FR_TYPE_GROUP:
550 relative->ctx = vp;
551 relative->da = fr_dict_attr_ref(vp->da);
552 if (relative->da == internal) {
553 relative->da = fr_dict_root(root->da->dict);
554 }
555 fr_assert(relative->da != NULL);
556 relative->list = &vp->vp_group;
557 break;
558
559 default:
560 /*
561 * Key fields have children in their namespace, but the children go into the
562 * parents context and list.
563 */
565 fr_pair_t *parent_vp;
566
567 parent_vp = fr_pair_parent(vp);
568 fr_assert(parent_vp);
569
570 relative->ctx = parent_vp;
571 relative->da = vp->da;
572 relative->list = &parent_vp->vp_group;
573 }
574 break;
575 }
576
577 i++;
578 } while (fr_sbuff_next_if_char(&our_in, '.'));
579
580 if (relative->allow_compare) {
581 vp->op = op;
582 } else {
583 vp->op = T_OP_EQ;
584 }
585
586 /*
587 * Reset the parser to the RHS so that we can parse the value.
588 */
589 fr_sbuff_set(&our_in, &rhs_m);
590
591 /*
592 * The RHS is a list, go parse the nested attributes.
593 */
594 if (fr_sbuff_next_if_char(&our_in, '{')) {
597 };
598
599 if (!fr_type_is_structural(vp->vp_type)) {
600 fr_strerror_printf("Cannot assign list to leaf data type %s for attribute %s",
601 fr_type_to_str(vp->vp_type), vp->da->name);
602 return fr_sbuff_error(&our_in);
603 }
604
605 while (true) {
606 fr_sbuff_adv_past_whitespace(&our_in, SIZE_MAX, NULL);
607
608 if (fr_sbuff_is_char(&our_in, '}')) {
609 break;
610 }
611
612 slen = fr_pair_list_afrom_substr(relative, &child, &our_in);
613 if (!slen) break;
614
615 if (slen < 0) return fr_sbuff_error(&our_in) + slen;
616 }
617
618 if (!fr_sbuff_next_if_char(&our_in, '}')) {
619 fr_strerror_const("Failed to end list with '}'");
620 return fr_sbuff_error(&our_in);
621 }
622
623 goto done;
624 }
625
626 if (fr_type_is_structural(vp->vp_type)) {
627 fr_strerror_printf("Group list for %s MUST start with '{'", vp->da->name);
628 return fr_sbuff_error(&our_in);
629 }
630
631 slen = fr_pair_value_from_substr(vp, &our_in, relative->tainted);
632 if (slen <= 0) return fr_sbuff_error(&our_in) + slen;
633
634done:
636
637 keep_going = false;
638 if (fr_sbuff_next_if_char(&our_in, ',')) {
639 keep_going = true;
640 relative->last_char = ',';
641 }
642
643 if (relative->allow_crlf) {
644 size_t len;
645
646 len = fr_sbuff_adv_past_allowed(&our_in, SIZE_MAX, sbuff_char_line_endings, NULL);
647 if (len) {
648 keep_going |= true;
649 if (!relative->last_char) relative->last_char = '\n';
650 }
651 }
652
653 keep_going &= ((fr_sbuff_remaining(&our_in) > 0) || (fr_sbuff_extend(&our_in) > 0));
654
655 if (keep_going) goto redo;
656
657 FR_SBUFF_SET_RETURN(in, &our_in);
658}
659
660/** Read valuepairs from the fp up to End-Of-File.
661 *
662 * @param[in] ctx for talloc
663 * @param[in] dict to resolve attributes in.
664 * @param[in,out] out where the parsed fr_pair_ts will be appended.
665 * @param[in] fp to read valuepairs from.
666 * @param[out] pfiledone true if file parsing complete;
667 * @return
668 * - 0 on success
669 * - -1 on error
670 */
671int fr_pair_list_afrom_file(TALLOC_CTX *ctx, fr_dict_t const *dict, fr_pair_list_t *out, FILE *fp, bool *pfiledone)
672{
673 fr_pair_list_t tmp_list;
674 fr_pair_parse_t root, relative;
675 bool found = false;
676 char buf[8192];
677
678 /*
679 * Read all of the attributes on the current line.
680 *
681 * If we get nothing but an EOL, it's likely OK.
682 */
683 fr_pair_list_init(&tmp_list);
684
685 root = (fr_pair_parse_t) {
686 .ctx = ctx,
687 .da = fr_dict_root(dict),
688 .list = &tmp_list,
689 .allow_crlf = true,
690 .allow_compare = true,
691 };
692 relative = (fr_pair_parse_t) { };
693
694 while (fgets(buf, sizeof(buf), fp) != NULL) {
695 /*
696 * If we get a '\n' by itself, we assume that's
697 * the end of that VP list.
698 */
699 if ((buf[0] == '\n') || (buf[0] == '\r')) {
700 if (found) {
701 *pfiledone = false;
702 break;
703 }
704 continue;
705 }
706
707 /*
708 * Comments get ignored
709 */
710 if (buf[0] == '#') continue;
711
712 /*
713 * Leave "relative" between calls, so that we can do:
714 *
715 * foo = {}
716 * .bar = baz
717 *
718 * and get
719 *
720 * foo = { bar = baz }
721 */
722 if (fr_pair_list_afrom_substr(&root, &relative, &FR_SBUFF_IN(buf, strlen(buf))) < 0) {
723 *pfiledone = false;
724 fr_pair_list_free(&tmp_list);
725 return -1;
726 }
727
728 found = true;
729 }
730
731 fr_pair_list_append(out, &tmp_list);
732
733 *pfiledone = true;
734 return 0;
735}
736
737
738/** Move pairs from source list to destination list respecting operator
739 *
740 * @note This function does some additional magic that's probably not needed in most places. Consider using
741 * radius_legacy_map_cmp() and radius_legacy_map_apply() instead.
742 *
743 * @note fr_pair_list_free should be called on the head of the source list to free
744 * unmoved attributes (if they're no longer needed).
745 *
746 * @param[in,out] to destination list.
747 * @param[in,out] from source list.
748 * @param[in] op operator for list move.
749 */
751{
752 fr_pair_t *vp, *next, *found;
753 fr_pair_list_t head_append, head_prepend;
754
755 if (!to || fr_pair_list_empty(from)) return;
756
757 /*
758 * We're editing the "to" list while we're adding new
759 * attributes to it. We don't want the new attributes to
760 * be edited, so we create an intermediate list to hold
761 * them during the editing process.
762 */
763 fr_pair_list_init(&head_append);
764
765 /*
766 * Any attributes that are requested to be prepended
767 * are added to a temporary list here
768 */
769 fr_pair_list_init(&head_prepend);
770
771 /*
772 * We're looping over the "from" list, moving some
773 * attributes out, but leaving others in place.
774 */
775 for (vp = fr_pair_list_head(from); vp != NULL; vp = next) {
777 next = fr_pair_list_next(from, vp);
778
779 /*
780 * We never move Fall-Through.
781 */
782 if (fr_dict_attr_is_top_level(vp->da) && (vp->da->attr == FR_FALL_THROUGH) &&
784 continue;
785 }
786
787 /*
788 * Unlike previous versions, we treat all other
789 * attributes as normal. i.e. there's no special
790 * treatment for passwords or Hint.
791 */
792
793 switch (vp->op) {
794 /*
795 * Anything else are operators which
796 * shouldn't occur. We ignore them, and
797 * leave them in place.
798 */
799 default:
800 continue;
801
802 /*
803 * Add it to the "to" list, but only if
804 * it doesn't already exist.
805 */
806 case T_OP_EQ:
807 found = fr_pair_find_by_da(to, NULL, vp->da);
808 if (!found) goto do_add;
809 continue;
810
811 /*
812 * Add it to the "to" list, and delete any attribute
813 * of the same vendor/attr which already exists.
814 */
815 case T_OP_SET:
816 found = fr_pair_find_by_da(to, NULL, vp->da);
817 if (!found) goto do_add;
818
819 /*
820 * Delete *all* matching attributes.
821 */
822 fr_pair_delete_by_da(to, found->da);
823 goto do_add;
824
825 /*
826 * Move it from the old list and add it
827 * to the new list.
828 */
829 case T_OP_ADD_EQ:
830 do_add:
831 fr_pair_remove(from, vp);
832 fr_pair_append(&head_append, vp);
833 continue;
834
835 case T_OP_PREPEND:
836 fr_pair_remove(from, vp);
837 fr_pair_prepend(&head_prepend, vp);
838 continue;
839 }
840 } /* loop over the "from" list. */
841
842 /*
843 * If the op parameter was prepend, add the "new list
844 * attributes first as those whose individual operator
845 * is prepend should be prepended to the resulting list
846 */
847 if (op == T_OP_PREPEND) fr_pair_list_prepend(to, &head_append);
848
849 /*
850 * If there are any items in the prepend list prepend
851 * it to the "to" list
852 */
853 fr_pair_list_prepend(to, &head_prepend);
854
855 /*
856 * If the op parameter was not prepend, take the "new"
857 * list, and append it to the "to" list.
858 */
859 if (op != T_OP_PREPEND) fr_pair_list_append(to, &head_append);
860
861 fr_pair_list_free(from);
862}
#define RCSID(id)
Definition build.h:485
#define L(_str)
Helper for initialising arrays of string literals.
Definition build.h:209
#define unlikely(_x)
Definition build.h:383
#define UNUSED
Definition build.h:317
#define NUM_ELEMENTS(_t)
Definition build.h:339
fr_dict_t const * fr_dict_by_da(fr_dict_attr_t const *da)
Attempt to locate the protocol dictionary containing an attribute.
Definition dict_util.c:2613
static fr_slen_t err
Definition dict.h:831
bool const fr_dict_attr_allowed_chars[UINT8_MAX+1]
Characters that are allowed in dictionary attribute names.
Definition dict_util.c:51
fr_dict_attr_t * fr_dict_attr_unknown_afrom_da(TALLOC_CTX *ctx, fr_dict_attr_t const *da))
Copy a known or unknown attribute to produce an unknown attribute with the specified name.
fr_dict_attr_t const * fr_dict_root(fr_dict_t const *dict)
Return the root attribute of a dictionary.
Definition dict_util.c:2407
fr_dict_attr_t * fr_dict_attr_unknown_raw_afrom_da(TALLOC_CTX *ctx, fr_dict_attr_t const *da))
Initialise an octets type attribute from a da.
void fr_dict_attr_unknown_free(fr_dict_attr_t const **da)
Free dynamically allocated (unknown attributes)
fr_dict_t const * fr_dict_internal(void)
Definition dict_util.c:4617
fr_slen_t fr_dict_attr_unknown_afrom_oid_substr(TALLOC_CTX *ctx, fr_dict_attr_t const **out, fr_dict_attr_t const *parent, fr_sbuff_t *in, fr_type_t type))
Create a fr_dict_attr_t from an ASCII attribute and value.
static bool fr_dict_attr_is_top_level(fr_dict_attr_t const *da)
Return true if this attribute is parented directly off the dictionary root.
Definition dict.h:764
fr_dict_attr_err_t
Errors returned by attribute lookup functions.
Definition dict.h:294
@ FR_DICT_ATTR_OK
No error.
Definition dict.h:295
@ FR_DICT_ATTR_NOTFOUND
Attribute couldn't be found.
Definition dict.h:296
fr_slen_t fr_dict_oid_component(fr_dict_attr_err_t *err, fr_dict_attr_t const **out, fr_dict_attr_t const *parent, fr_sbuff_t *in, fr_sbuff_term_t const *tt))
Parse an OID component, resolving it to a defined attribute.
Definition dict_util.c:2236
#define fr_dict_attr_is_key_field(_da)
Definition dict.h:157
static fr_slen_t in
Definition dict.h:831
static fr_dict_attr_t const * fr_dict_attr_ref(fr_dict_attr_t const *da)
Return the reference associated with a group type attribute.
Definition dict_ext.h:184
fr_type_t
@ FR_TYPE_TLV
Contains nested attributes.
@ FR_TYPE_NULL
Invalid (uninitialised) attribute type.
@ FR_TYPE_STRUCT
like TLV, but without T or L, and fixed-width children
@ FR_TYPE_VENDOR
Attribute that represents a vendor in the attribute tree.
@ FR_TYPE_VSA
Vendor-Specific, for RADIUS attribute 26.
@ FR_TYPE_OCTETS
Raw octets.
@ FR_TYPE_GROUP
A grouping of other attributes.
long int ssize_t
ssize_t fr_slen_t
fr_pair_t * fr_pair_list_parent(fr_pair_list_t const *list)
Return a pointer to the parent pair which contains this list.
Definition pair.c:958
int fr_pair_append_by_da(TALLOC_CTX *ctx, fr_pair_t **out, fr_pair_list_t *list, fr_dict_attr_t const *da)
Alloc a new fr_pair_t (and append)
Definition pair.c:1468
fr_pair_t * fr_pair_find_by_da(fr_pair_list_t const *list, fr_pair_t const *prev, fr_dict_attr_t const *da)
Find the first pair with a matching da.
Definition pair.c:695
int fr_pair_append(fr_pair_list_t *list, fr_pair_t *to_add)
Add a VP to the end of the list.
Definition pair.c:1347
int fr_pair_delete_by_da(fr_pair_list_t *list, fr_dict_attr_t const *da)
Delete matching pairs from the specified list.
Definition pair.c:1691
fr_pair_t * fr_pair_parent(fr_pair_t const *vp)
Return a pointer to the parent pair.
Definition pair.c:944
fr_pair_t * fr_pair_afrom_da(TALLOC_CTX *ctx, fr_dict_attr_t const *da)
Dynamically allocate a new attribute and assign a fr_dict_attr_t.
Definition pair.c:285
void fr_pair_list_init(fr_pair_list_t *list)
Initialise a pair list header.
Definition pair.c:46
int fr_pair_prepend(fr_pair_list_t *list, fr_pair_t *to_add)
Add a VP to the start of the list.
Definition pair.c:1316
fr_pair_t * fr_pair_afrom_da_depth_nested(TALLOC_CTX *ctx, fr_pair_list_t *list, fr_dict_attr_t const *da, int start)
Create a pair (and all intermediate parents), and append it to the list.
Definition pair.c:410
fr_pair_t * fr_pair_find_last_by_da(fr_pair_list_t const *list, fr_pair_t const *prev, fr_dict_attr_t const *da)
Find the last pair with a matching da.
Definition pair.c:719
static fr_sbuff_parse_rules_t const bareword_unquoted
Definition pair_legacy.c:89
static fr_table_num_sorted_t const pair_assignment_op_table[]
Definition pair_legacy.c:59
fr_slen_t fr_pair_list_afrom_substr(fr_pair_parse_t const *root, fr_pair_parse_t *relative, fr_sbuff_t *in)
Parse a fr_pair_list_t from a substring.
int fr_pair_list_afrom_file(TALLOC_CTX *ctx, fr_dict_t const *dict, fr_pair_list_t *out, FILE *fp, bool *pfiledone)
Read valuepairs from the fp up to End-Of-File.
void fr_pair_list_move_op(fr_pair_list_t *to, fr_pair_list_t *from, fr_token_t op)
Move pairs from source list to destination list respecting operator.
static size_t pair_comparison_op_table_len
Definition pair_legacy.c:81
static ssize_t fr_pair_value_from_substr(fr_pair_t *vp, fr_sbuff_t *in, UNUSED bool tainted)
static fr_table_num_sorted_t const pair_comparison_op_table[]
Definition pair_legacy.c:66
static fr_sbuff_term_t const bareword_terminals
Definition pair_legacy.c:36
static ssize_t pair_assignment_op_table_len
Definition pair_legacy.c:64
struct fr_pair_parse_s fr_pair_parse_t
fr_pair_list_t * list
list where output is placed
Definition pair_legacy.h:45
TALLOC_CTX * ctx
Definition pair_legacy.h:43
bool allow_compare
allow comparison operators
Definition pair_legacy.h:46
fr_dict_attr_t const * da
root da to start parsing from
Definition pair_legacy.h:44
bool tainted
source is tainted
Definition pair_legacy.h:48
bool allow_crlf
allow CRLF, and treat like comma
Definition pair_legacy.h:47
char last_char
last character we read - ',', ' ', or 0 for EOF
Definition pair_legacy.h:49
#define fr_assert(_expr)
Definition rad_assert.h:38
static bool done
Definition radclient.c:80
size_t fr_sbuff_adv_past_allowed(fr_sbuff_t *sbuff, size_t len, bool const allowed[static UINT8_MAX+1], fr_sbuff_term_t const *tt)
Wind position past characters in the allowed set.
Definition sbuff.c:1777
bool const sbuff_char_line_endings[UINT8_MAX+1]
Definition sbuff.c:104
bool fr_sbuff_next_if_char(fr_sbuff_t *sbuff, char c)
Return true if the current char matches, and if it does, advance.
Definition sbuff.c:2088
#define fr_sbuff_out_by_longest_prefix(_match_len, _out, _table, _sbuff, _def)
#define fr_sbuff_is_str_literal(_sbuff, _str)
#define fr_sbuff_set(_dst, _src)
#define fr_sbuff_diff(_a, _b)
#define FR_SBUFF_IN(_start, _len_or_end)
#define fr_sbuff_adv_past_whitespace(_sbuff, _len, _tt)
#define fr_sbuff_current(_sbuff_or_marker)
#define FR_SBUFF_TERMS(...)
Initialise a terminal structure with a list of sorted strings.
Definition sbuff.h:192
#define fr_sbuff_extend(_sbuff_or_marker)
#define fr_sbuff_is_char(_sbuff_or_marker, _c)
#define FR_SBUFF_SET_RETURN(_dst, _src)
#define fr_sbuff_is_digit(_sbuff_or_marker)
#define fr_sbuff_error(_sbuff_or_marker)
#define FR_SBUFF(_sbuff_or_marker)
#define fr_sbuff_advance(_sbuff_or_marker, _len)
#define fr_sbuff_remaining(_sbuff_or_marker)
Set of terminal elements.
fr_pair_t * vp
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
An element in a lexicographically sorted array of name to num mappings.
Definition table.h:49
enum fr_token fr_token_t
@ T_INVALID
Definition token.h:39
@ T_OP_CMP_TRUE
Definition token.h:104
@ T_OP_EQ
Definition token.h:83
@ T_OP_SET
Definition token.h:84
@ T_OP_NE
Definition token.h:97
@ T_OP_ADD_EQ
Definition token.h:69
@ T_OP_CMP_FALSE
Definition token.h:105
@ T_OP_REG_EQ
Definition token.h:102
@ T_OP_CMP_EQ
Definition token.h:106
@ T_OP_LE
Definition token.h:100
@ T_OP_GE
Definition token.h:98
@ T_OP_GT
Definition token.h:99
@ T_OP_LT
Definition token.h:101
@ T_OP_REG_NE
Definition token.h:103
@ T_OP_PREPEND
Definition token.h:85
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
fr_pair_t * fr_pair_list_tail(fr_pair_list_t const *list)
Get the tail of a valuepair list.
Definition pair_inline.c:56
fr_pair_t * fr_pair_remove(fr_pair_list_t *list, fr_pair_t *vp)
Remove fr_pair_t from a list without freeing.
Definition pair_inline.c:94
void fr_pair_list_free(fr_pair_list_t *list)
Free memory used by a valuepair list.
void fr_pair_list_append(fr_pair_list_t *dst, fr_pair_list_t *src)
Appends a list of fr_pair_t from a temporary list to a destination list.
void fr_pair_list_prepend(fr_pair_list_t *dst, fr_pair_list_t *src)
Move a list of fr_pair_t from a temporary list to the head of a destination list.
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
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
Definition strerror.h:64
#define fr_strerror_const(_msg)
Definition strerror.h:223
fr_table_num_ordered_t const fr_type_table[]
Map data types to names representing those types.
Definition types.c:31
#define fr_type_is_structural(_x)
Definition types.h:371
#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
fr_sbuff_parse_rules_t const value_parse_rules_single_quoted
Definition value.c:553
fr_sbuff_parse_rules_t const value_parse_rules_double_quoted
Definition value.c:547
ssize_t fr_value_box_from_substr(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_type_t dst_type, fr_dict_attr_t const *dst_enumv, fr_sbuff_t *in, fr_sbuff_parse_rules_t const *rules)
Convert string value to a fr_value_box_t type.
Definition value.c:4686
static size_t char ** out
Definition value.h:1012