/
pair.c
3130 lines (2626 loc) · 73.8 KB
/
pair.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
/** AVP manipulation and search API
*
* @file src/lib/util/pair.c
*
* @copyright 2000,2006,2015 The FreeRADIUS server project
*/
RCSID("$Id$")
#include <freeradius-devel/util/misc.h>
#include <freeradius-devel/util/pair_cursor.h>
#include <freeradius-devel/util/pair.h>
#include <freeradius-devel/util/print.h>
#include <freeradius-devel/util/proto.h>
#include <freeradius-devel/util/regex.h>
#include <freeradius-devel/util/talloc.h>
#include <freeradius-devel/protocol/radius/rfc2865.h>
#include <freeradius-devel/protocol/freeradius/freeradius.internal.h>
#include <ctype.h>
#ifndef NDEBUG
# define FREE_MAGIC (0xF4EEF4EE)
#endif
/** Free a VALUE_PAIR
*
* @note Do not call directly, use talloc_free instead.
*
* @param vp to free.
* @return 0
*/
static int _fr_pair_free(NDEBUG_UNUSED VALUE_PAIR *vp)
{
#ifndef NDEBUG
vp->vp_uint32 = FREE_MAGIC;
#endif
#ifdef TALLOC_DEBUG
talloc_report_depth_cb(NULL, 0, -1, fr_talloc_verify_cb, NULL);
#endif
return 0;
}
VALUE_PAIR *fr_pair_alloc(TALLOC_CTX *ctx)
{
VALUE_PAIR *vp;
vp = talloc_zero(ctx, VALUE_PAIR);
if (!vp) {
fr_strerror_printf("Out of memory");
return NULL;
}
vp->op = T_OP_EQ;
vp->tag = TAG_ANY;
vp->type = VT_NONE;
talloc_set_destructor(vp, _fr_pair_free);
return vp;
}
/** Dynamically allocate a new attribute
*
* Allocates a new attribute and a new dictionary attr if no DA is provided.
*
* @note Doesn't require qualification with a dictionary as fr_dict_attr_t are unique.
*
* @param[in] ctx for allocated memory, usually a pointer to a #RADIUS_PACKET
* @param[in] da Specifies the dictionary attribute to build the #VALUE_PAIR from.
* @return
* - A new #VALUE_PAIR.
* - NULL if an error occurred.
*/
VALUE_PAIR *fr_pair_afrom_da(TALLOC_CTX *ctx, fr_dict_attr_t const *da)
{
VALUE_PAIR *vp;
/*
* Caller must specify a da else we don't know what the attribute type is.
*/
if (!da) {
fr_strerror_printf("Invalid arguments");
return NULL;
}
vp = fr_pair_alloc(ctx);
if (!vp) {
fr_strerror_printf("Out of memory");
return NULL;
}
/*
* If we get passed an unknown da, we need to ensure that
* it's parented by "vp".
*/
if (da->flags.is_unknown) {
fr_dict_attr_t const *unknown;
unknown = fr_dict_unknown_acopy(vp, da);
da = unknown;
}
/*
* Use the 'da' to initialize more fields.
*/
vp->da = da;
vp->vp_type = da->type;
vp->data.enumv = da;
return vp;
}
/** Create a new valuepair
*
* If attr and vendor match a dictionary entry then a VP with that #fr_dict_attr_t
* will be returned.
*
* If attr or vendor are uknown will call dict_attruknown to create a dynamic
* #fr_dict_attr_t of #FR_TYPE_OCTETS.
*
* Which type of #fr_dict_attr_t the #VALUE_PAIR was created with can be determined by
* checking @verbatim vp->da->flags.is_unknown @endverbatim.
*
* @param[in] ctx for allocated memory, usually a pointer to a #RADIUS_PACKET.
* @param[in] attr number.
* @param[in] vendor number.
* @return
* - A new #VALUE_PAIR.
* - NULL on error.
*/
VALUE_PAIR *fr_pair_afrom_num(TALLOC_CTX *ctx, unsigned int vendor, unsigned int attr)
{
fr_dict_attr_t const *da;
fr_dict_attr_t const *parent;
if (vendor == 0) {
da = fr_dict_attr_child_by_num(fr_dict_root(fr_dict_internal()), attr);
goto alloc;
}
parent = fr_dict_attr_child_by_num(fr_dict_root(fr_dict_internal()), FR_VENDOR_SPECIFIC);
if (!parent) return NULL;
parent = fr_dict_attr_child_by_num(parent, vendor);
if (!parent) return NULL;
da = fr_dict_attr_child_by_num(parent, attr);
alloc:
if (!da) {
VALUE_PAIR *vp;
vp = fr_pair_alloc(ctx);
if (!vp) return NULL;
/*
* Ensure that the DA is parented by the VP.
*/
da = fr_dict_unknown_afrom_fields(vp, fr_dict_root(fr_dict_internal()), vendor, attr);
if (!da) {
talloc_free(vp);
return NULL;
}
vp->da = da;
vp->vp_type = da->type; /* FR_TYPE_OCTETS */
return vp;
}
return fr_pair_afrom_da(ctx, da);
}
/** Create a new valuepair
*
* If attr and vendor match a dictionary entry then a VP with that #fr_dict_attr_t
* will be returned.
*
* If attr or vendor are uknown will call dict_attruknown to create a dynamic
* #fr_dict_attr_t of #FR_TYPE_OCTETS.
*
* Which type of #fr_dict_attr_t the #VALUE_PAIR was created with can be determined by
* checking @verbatim vp->da->flags.is_unknown @endverbatim.
*
* @param[in] ctx for allocated memory, usually a pointer to a #RADIUS_PACKET.
* @param[in] parent of the attribute being allocated (usually a dictionary or vendor).
* @param[in] attr number.
* @return
* - A new #VALUE_PAIR.
* - NULL on error.
*/
VALUE_PAIR *fr_pair_afrom_child_num(TALLOC_CTX *ctx, fr_dict_attr_t const *parent, unsigned int attr)
{
fr_dict_attr_t const *da;
VALUE_PAIR *vp;
da = fr_dict_attr_child_by_num(parent, attr);
if (!da) {
unsigned int vendor_id = 0;
fr_dict_attr_t const *vendor;
/*
* If parent is a vendor, that's fine. If parent
* is a TLV attribute parented by a vendor, that's
* also fine...
*/
vendor = fr_dict_vendor_attr_by_da(parent);
if (vendor) vendor_id = vendor->attr;
da = fr_dict_unknown_afrom_fields(ctx, parent,
vendor_id, attr);
if (!da) return NULL;
}
vp = fr_pair_afrom_da(ctx, da);
fr_dict_unknown_free(&da);
return vp;
}
/** Deserialise a value pair from a string
*
* Input must be in the format:
* @verbatim <attribute> = [<qu>]<value>[<qu>] @endverbatim
*
* This is intended to be use as a light weight way of deserialising complete
* attribute/value pairs. For more complex operations see the map API.
*
* This is the companion function for #fr_pair_snprint and #fr_pair_asprint.
*
* @param[in] ctx to allocate pair in.
* @param[out] out the VALUE_PAIR we allocated.
* @param[in] dict to search for attributes in.
* @param[in] in string to convert to VALUE_PAIR.
* @param[in] tainted Whether the source of in was untrusted.
* @return
* - <= 0 on failure.
* - The number of bytes of name consumed on success.
*/
ssize_t fr_pair_afrom_substr(TALLOC_CTX *ctx, VALUE_PAIR **out,
fr_dict_t const *dict, char const *in, bool tainted)
{
ssize_t slen;
VALUE_PAIR *vp;
fr_dict_attr_t const *da;
char const *p, *q;
char quote;
p = in;
slen = fr_dict_attr_by_name_substr(NULL, &da, dict, p);
if (slen <= 0) return slen;
p += slen;
fr_skip_whitespace(p);
if (*p != '=') {
fr_strerror_printf("Invalid operator '%c'", *p);
return -(p - in);
}
fr_skip_whitespace(p);
switch (*p) {
/*
* Note - Unescaping is handled by fr_pair_value_from_str
*/
case '"':
case '\'':
quote = *p++;
/*
* Figure out the end of the quoted string
* ignoring escaped quotes.
*/
for (q = p; *q != '\0'; q++) {
if (*q == '\'') {
switch (*(q + 1)) {
case '\\':
case '"':
case '\'':
q++;
continue;
default:
break;
}
}
}
if (*q != quote) {
fr_strerror_printf("No matching terminating quote");
return -(p - in);
}
break;
default:
quote = '\0';
q = p;
while (fr_dict_attr_allowed_chars[(uint8_t)*q] != '\0') q++;
break;
}
vp = fr_pair_afrom_da(ctx, da);
if (!vp) return 0;
if (fr_pair_value_from_str(vp, p, q - p, quote, tainted) < 0) {
talloc_free(vp);
return -(p - in);
}
if (quote != '\0') q++; /* Advance past trailing quote */
*out = vp;
return (q - in);
}
/** Copy a single valuepair
*
* Allocate a new valuepair and copy the da from the old vp.
*
* @param[in] ctx for talloc
* @param[in] vp to copy.
* @return
* - A copy of the input VP.
* - NULL on error.
*/
VALUE_PAIR *fr_pair_copy(TALLOC_CTX *ctx, VALUE_PAIR const *vp)
{
VALUE_PAIR *n;
if (!vp) return NULL;
VP_VERIFY(vp);
n = fr_pair_afrom_da(ctx, vp->da);
if (!n) return NULL;
n->op = vp->op;
n->tag = vp->tag;
n->next = NULL;
n->type = vp->type;
/*
* Copy the unknown attribute hierarchy
*/
if (n->da->flags.is_unknown) {
n->da = fr_dict_unknown_acopy(n, n->da);
if (!n->da) {
talloc_free(n);
return NULL;
}
}
/*
* If it's an xlat, copy the raw string and return
* early, so we don't pre-expand or otherwise mangle
* the VALUE_PAIR.
*/
if (vp->type == VT_XLAT) {
n->xlat = talloc_typed_strdup(n, vp->xlat);
return n;
}
/*
* Groups are special.
*/
if (n->da->type == FR_TYPE_GROUP) {
if (fr_pair_list_copy(n, (VALUE_PAIR **) &n->vp_ptr, vp->vp_ptr) < 0) {
talloc_free(n);
return NULL;
}
return n;
}
fr_value_box_copy(n, &n->data, &vp->data);
return n;
}
/** Steal one VP
*
* @param[in] ctx to move VALUE_PAIR into
* @param[in] vp VALUE_PAIR to move into the new context.
*/
void fr_pair_steal(TALLOC_CTX *ctx, VALUE_PAIR *vp)
{
(void) talloc_steal(ctx, vp);
/*
* The DA may be unknown. If we're stealing the VPs to a
* different context, copy the unknown DA. We use the VP
* as a context here instead of "ctx", so that when the
* VP is freed, so is the DA.
*
* Since we have no introspection into OTHER VPs using
* the same DA, we can't have multiple VPs use the same
* DA. So we might as well tie it to this VP.
*/
if (vp->da->flags.is_unknown) {
vp->da = fr_dict_unknown_acopy(vp, vp->da);
}
}
/** Create a valuepair from an ASCII attribute and value
*
* Where the attribute name is in the form:
* - Attr-%d
* - Attr-%d.%d.%d...
*
* @param ctx for talloc
* @param dict to user for partial resolution.
* @param attribute name to parse.
* @param value to parse (must be a hex string).
* @param op to assign to new valuepair.
* @return new #VALUE_PAIR or NULL on error.
*/
static VALUE_PAIR *fr_pair_make_unknown(TALLOC_CTX *ctx, fr_dict_t const *dict,
char const *attribute, char const *value,
FR_TOKEN op)
{
VALUE_PAIR *vp;
fr_dict_attr_t *n;
vp = fr_pair_alloc(ctx);
if (!vp) return NULL;
if (fr_dict_unknown_afrom_oid_str(vp, &n, fr_dict_root(dict), attribute) <= 0) {
talloc_free(vp);
return NULL;
}
vp->da = n;
/*
* No value. Nothing more to do.
*/
if (!value) return vp;
/*
* Unknown attributes MUST be of type 'octets'
*/
if (strncasecmp(value, "0x", 2) != 0) {
fr_strerror_printf("Unknown attribute \"%s\" requires a hex "
"string, not \"%s\"", attribute, value);
talloc_free(vp);
return NULL;
}
if (fr_pair_value_from_str(vp, value, -1, '"', false) < 0) {
talloc_free(vp);
return NULL;
}
vp->op = (op == 0) ? T_OP_EQ : op;
return vp;
}
/** Create a #VALUE_PAIR from ASCII strings
*
* Converts an attribute string identifier (with an optional tag qualifier)
* and value string into a #VALUE_PAIR.
*
* The string value is parsed according to the type of #VALUE_PAIR being created.
*
* @param[in] ctx for talloc.
* @param[in] dict to look attributes up in.
* @param[in] vps list where the attribute will be added (optional)
* @param[in] attribute name.
* @param[in] value attribute value (may be NULL if value will be set later).
* @param[in] op to assign to new #VALUE_PAIR.
* @return a new #VALUE_PAIR.
*/
VALUE_PAIR *fr_pair_make(TALLOC_CTX *ctx, fr_dict_t const *dict, VALUE_PAIR **vps,
char const *attribute, char const *value, FR_TOKEN op)
{
fr_dict_attr_t const *da;
VALUE_PAIR *vp;
char *p;
int8_t tag;
char const *attrname = attribute;
char buffer[FR_DICT_ATTR_MAX_NAME_LEN + 1 + 32];
/*
* Check for tags in 'Attribute:Tag' format.
*/
tag = TAG_NONE;
p = strchr(attribute, ':');
if (p) {
char *end;
if (!p[1]) {
fr_strerror_printf("Invalid tag for attribute %s", attribute);
return NULL;
}
strlcpy(buffer, attribute, sizeof(buffer));
p = buffer + (p - attrname);
attrname = buffer;
/* Colon found with something behind it */
if ((p[1] == '*') && !p[2]) {
/* Wildcard tag for check items */
tag = TAG_ANY;
} else {
/* It's not a wild card tag */
tag = strtol(p + 1, &end, 10);
if (*end) {
fr_strerror_printf("Unexpected text after tag for attribute %s", attribute);
return NULL;
}
if (!TAG_VALID_ZERO(tag)) {
fr_strerror_printf("Invalid tag for attribute %s", attribute);
return NULL;
}
}
/*
* Leave only the attribute name in the buffer.
*/
*p = '\0';
}
/*
* It's not found in the dictionary, so we use
* another method to create the attribute.
*/
if (fr_dict_attr_by_qualified_name(&da, dict, attrname, true) != FR_DICT_ATTR_OK) {
if (tag != TAG_NONE) {
fr_strerror_printf("Invalid tag for attribute %s", attribute);
return NULL;
}
vp = fr_pair_make_unknown(ctx, dict, attrname, value, op);
if (!vp) return NULL;
if (vps) fr_pair_add(vps, vp);
return vp;
}
#ifndef NDEBUG
if (!da) return NULL;
#endif
/*
* Untagged attributes can't have a tag.
*/
if (!da->flags.has_tag && (tag != TAG_NONE)) {
fr_strerror_printf("Invalid tag for attribute %s", attribute);
return NULL;
}
if (da->type == FR_TYPE_GROUP) {
fr_strerror_printf("Attributes of type 'group' are not supported");
return NULL;
}
vp = fr_pair_afrom_da(ctx, da);
if (!vp) return NULL;
vp->op = (op == 0) ? T_OP_EQ : op;
vp->tag = tag;
switch (vp->op) {
case T_OP_CMP_TRUE:
case T_OP_CMP_FALSE:
vp->vp_strvalue = NULL;
vp->vp_length = 0;
value = NULL; /* ignore it! */
break;
/*
* Regular expression comparison of integer attributes
* does a STRING comparison of the names of their
* integer attributes.
*/
case T_OP_REG_EQ: /* =~ */
case T_OP_REG_NE: /* !~ */
{
#ifndef HAVE_REGEX
fr_strerror_printf("Regular expressions are not supported");
return NULL;
#else
ssize_t slen;
regex_t *preg;
/*
* Someone else will fill in the value.
*/
if (!value) break;
talloc_free(vp);
slen = regex_compile(ctx, &preg, value, strlen(value), NULL, false, true);
if (slen <= 0) {
fr_strerror_printf_push("Error at offset %zu compiling regex for %s", -slen, attribute);
return NULL;
}
talloc_free(preg);
vp = fr_pair_make(ctx, dict, NULL, attribute, NULL, op);
if (!vp) return NULL;
if (fr_pair_mark_xlat(vp, value) < 0) {
talloc_free(vp);
return NULL;
}
value = NULL; /* ignore it */
break;
#endif
}
default:
break;
}
/*
* We probably want to fix fr_pair_value_from_str to accept
* octets as values for any attribute.
*/
if (value && (fr_pair_value_from_str(vp, value, -1, '\"', true) < 0)) {
talloc_free(vp);
return NULL;
}
if (vps) fr_pair_add(vps, vp);
return vp;
}
/** Free memory used by a valuepair list.
*
* @todo TLV: needs to free all dependents of each VP freed.
*/
void fr_pair_list_free(VALUE_PAIR **vps)
{
VALUE_PAIR *vp, *next;
if (!vps || !*vps) return;
for (vp = *vps; vp != NULL; vp = next) {
next = vp->next;
VP_VERIFY(vp);
talloc_free(vp);
}
*vps = NULL;
}
/** Mark malformed or unrecognised attributed as unknown
*
* @param vp to change fr_dict_attr_t of.
* @return
* - 0 on success (or if already unknown).
* - -1 on failure.
*/
int fr_pair_to_unknown(VALUE_PAIR *vp)
{
fr_dict_attr_t const *da;
VP_VERIFY(vp);
if (vp->da->flags.is_unknown) return 0;
if (!fr_cond_assert(vp->da->parent != NULL)) return -1;
da = fr_dict_unknown_afrom_fields(vp, vp->da->parent, fr_dict_vendor_num_by_da(vp->da), vp->da->attr);
if (!da) return -1;
fr_dict_unknown_free(&vp->da); /* Only frees unknown attributes */
vp->da = da;
return 0;
}
/** Mark a valuepair for xlat expansion
*
* Copies xlat source (unprocessed) string to valuepair value, and sets value type.
*
* @param vp to mark for expansion.
* @param value to expand.
* @return
* - 0 if marking succeeded.
* - -1 if #VALUE_PAIR already had a value, or OOM.
*/
int fr_pair_mark_xlat(VALUE_PAIR *vp, char const *value)
{
char *raw;
/*
* valuepair should not already have a value.
*/
if (vp->type != VT_NONE) {
fr_strerror_printf("Pair already has a value");
return -1;
}
raw = talloc_typed_strdup(vp, value);
if (!raw) {
fr_strerror_printf("Out of memory");
return -1;
}
vp->type = VT_XLAT;
vp->xlat = raw;
vp->vp_length = 0;
return 0;
}
/** Iterate over pairs with a specified da
*
* @param[in,out] prev The VALUE_PAIR before curr. Will be updated to point to the
* pair before the one returned, or the last pair in the list
* if no matching pairs found.
* @param[in] to_eval The VALUE_PAIR after cursor->current. Will be checked to
* see if it matches the specified fr_dict_attr_t.
* @param[in] uctx The fr_dict_attr_t to search for.
* @return
* - Next matching VALUE_PAIR.
* - NULL if not more matching VALUE_PAIRs could be found.
*/
void *fr_pair_iter_next_by_da(void **prev, void *to_eval, void *uctx)
{
VALUE_PAIR *c, *p;
fr_dict_attr_t *da = uctx;
if (!to_eval) return NULL;
for (p = *prev, c = to_eval; c; p = c, c = c->next) {
VP_VERIFY(c);
if (c->da == da) break;
}
*prev = p;
return c;
}
/** Iterate over pairs which are decedents of the specified da
*
* @param[in,out] prev The VALUE_PAIR before curr. Will be updated to point to the
* pair before the one returned, or the last pair in the list
* if no matching pairs found.
* @param[in] to_eval The VALUE_PAIR after cursor->current. Will be checked to
* see if it matches the specified fr_dict_attr_t.
* @param[in] uctx The fr_dict_attr_t to search for.
* @return
* - Next matching VALUE_PAIR.
* - NULL if not more matching VALUE_PAIRs could be found.
*/
void *fr_pair_iter_next_by_ancestor(void **prev, void *to_eval, void *uctx)
{
VALUE_PAIR *c, *p;
fr_dict_attr_t *da = uctx;
if (!to_eval) return NULL;
for (p = *prev, c = to_eval; c; p = c, c = c->next) {
VP_VERIFY(c);
if (fr_dict_parent_common(da, c->da, true)) break;
}
*prev = p;
return c;
}
/** Find the pair with the matching DAs
*
*/
VALUE_PAIR *fr_pair_find_by_da(VALUE_PAIR *head, fr_dict_attr_t const *da, int8_t tag)
{
VALUE_PAIR *vp;
/* List head may be NULL if it contains no VPs */
if (!head) return NULL;
LIST_VERIFY(head);
if (!da) return NULL;
for (vp = head; vp != NULL; vp = vp->next) {
if ((da == vp->da) && TAG_EQ(tag, vp->tag)) return vp;
}
return NULL;
}
/** Find the pair with the matching attribute
*
* @todo should take DAs and do a pointer comparison.
*/
VALUE_PAIR *fr_pair_find_by_num(VALUE_PAIR *head, unsigned int vendor, unsigned int attr, int8_t tag)
{
VALUE_PAIR *vp;
/* List head may be NULL if it contains no VPs */
if (!head) return NULL;
LIST_VERIFY(head);
for (vp = head; vp != NULL; vp = vp->next) {
if (!fr_dict_attr_is_top_level(vp->da)) continue;
if (vendor > 0) {
fr_dict_vendor_t const *dv;
dv = fr_dict_vendor_by_da(vp->da);
if (!dv) continue;
if (dv->pen != vendor) continue;
}
if ((attr == vp->da->attr) && TAG_EQ(tag, vp->tag)) return vp;
}
return NULL;
}
/** Find the pair with the matching attribute
*
*/
VALUE_PAIR *fr_pair_find_by_child_num(VALUE_PAIR *head, fr_dict_attr_t const *parent, unsigned int attr, int8_t tag)
{
fr_dict_attr_t const *da;
VALUE_PAIR *vp;
/* List head may be NULL if it contains no VPs */
if (!head) return NULL;
LIST_VERIFY(head);
da = fr_dict_attr_child_by_num(parent, attr);
if (!da) return NULL;
for (vp = head; vp != NULL; vp = vp->next) {
if ((da == vp->da) && TAG_EQ(tag, vp->tag)) return vp;
}
return NULL;
}
/** Add a VP to the end of the list.
*
* Locates the end of 'head', and links an additional VP 'add' at the end.
*
* @param[in] head VP in linked list. Will add new VP to the end of this list.
* @param[in] add VP to add to list.
*/
void fr_pair_add(VALUE_PAIR **head, VALUE_PAIR *add)
{
VALUE_PAIR *i;
if (!add) return;
VP_VERIFY(add);
if (*head == NULL) {
*head = add;
return;
}
for (i = *head; i->next; i = i->next) {
#ifdef WITH_VERIFY_PTR
VP_VERIFY(i);
/*
* The same VP should never by added multiple times
* to the same list.
*/
(void)fr_cond_assert(i != add);
#endif
}
i->next = add;
}
/** Replace all matching VPs
*
* Walks over 'head', and replaces the head VP that matches 'replace'.
*
* @note Memory used by the VP being replaced will be freed.
* @note Will not work with unknown attributes.
*
* @param[in,out] head VP in linked list. Will search and replace in this list.
* @param[in] replace VP to replace.
*/
void fr_pair_replace(VALUE_PAIR **head, VALUE_PAIR *replace)
{
VALUE_PAIR *i, *next;
VALUE_PAIR **prev = head;
VP_VERIFY(replace);
if (*head == NULL) {
*head = replace;
return;
}
/*
* Not an empty list, so find item if it is there, and
* replace it. Note, we always replace the head one, and
* we ignore any others that might exist.
*/
for(i = *head; i; i = next) {
VP_VERIFY(i);
next = i->next;
/*
* Found the head attribute, replace it,
* and return.
*/
if ((i->da == replace->da) && ATTR_TAG_MATCH(i, replace->tag)) {
*prev = replace;
/*
* Should really assert that replace->next == NULL
*/
replace->next = next;
talloc_free(i);
return;
}
/*
* Point to where the attribute should go.
*/
prev = &i->next;
}
/*
* If we got here, we didn't find anything to replace, so
* stopped at the last item, which we just append to.
*/
*prev = replace;
}
/** Delete matching pairs
*
* Delete matching pairs from the attribute list.
*
* @param[in,out] head VP in list.
* @param[in] attr to match.
* @param[in] parent to match.
* @param[in] tag to match. TAG_ANY matches any tag, TAG_NONE matches tagless VPs.
*/
void fr_pair_delete_by_child_num(VALUE_PAIR **head, fr_dict_attr_t const *parent, unsigned int attr, int8_t tag)
{
VALUE_PAIR *i, *next;
VALUE_PAIR **last = head;
fr_dict_attr_t const *da;
da = fr_dict_attr_child_by_num(parent, attr);
if (!da) return;
for (i = *head; i; i = next) {
VP_VERIFY(i);
next = i->next;
if ((i->da == da) && (!i->da->flags.has_tag || TAG_EQ(tag, i->tag))) {
*last = next;
talloc_free(i);
} else {
last = &i->next;
}
}
}
/** Alloc a new VALUE_PAIR (and prepend)
*
* @param[in] ctx to allocate new #VALUE_PAIR in.
* @param[out] out Pair we allocated. May be NULL if the caller doesn't
* care about manipulating the VALUE_PAIR.
* @param[in,out] list in search and insert into.
* @param[in] da of attribute to update.
* @return