-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
pair.c
2520 lines (2140 loc) · 56.3 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
/*
* pair.c Functions to handle VALUE_PAIRs
*
* Version: $Id$
*
* 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
*
* Copyright 2000,2006 The FreeRADIUS server project
*/
RCSID("$Id$")
#include <freeradius-devel/libradius.h>
#include <freeradius-devel/regex.h>
#include <ctype.h>
/** Free a VALUE_PAIR
*
* @note Do not call directly, use talloc_free instead.
*
* @param vp to free.
* @return 0
*/
static int _fr_pair_free(VALUE_PAIR *vp) {
#ifndef NDEBUG
vp->vp_integer = 0xf4eef4ee;
#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.
*
* @param[in] ctx for allocated memory, usually a pointer to a RADIUS_PACKET
* @param[in] da Specifies the dictionary attribute to build the VP from.
* @return a new value pair or NULL if an error occurred.
*/
VALUE_PAIR *fr_pair_afrom_da(TALLOC_CTX *ctx, DICT_ATTR 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;
}
/*
* Use the 'da' to initialize more fields.
*/
vp->da = da;
vp->vp_length = da->flags.length;
return vp;
}
/** Create a new valuepair
*
* If attr and vendor match a dictionary entry then a VP with that DICT_ATTR
* will be returned.
*
* If attr or vendor are uknown will call dict_attruknown to create a dynamic
* DICT_ATTR of PW_TYPE_OCTETS.
*
* Which type of DICT_ATTR 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 the new valuepair or NULL on error.
*/
VALUE_PAIR *fr_pair_afrom_num(TALLOC_CTX *ctx, unsigned int attr, unsigned int vendor)
{
DICT_ATTR const *da;
da = dict_attrbyvalue(attr, vendor);
if (!da) return NULL;
return fr_pair_afrom_da(ctx, da);
}
/** 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;
vp_cursor_t cursor;
if (!vps || !*vps) {
return;
}
for (vp = fr_cursor_init(&cursor, vps);
vp;
vp = fr_cursor_next(&cursor)) {
VERIFY_VP(vp);
talloc_free(vp);
}
*vps = NULL;
}
/** Mark malformed or unrecognised attributed as unknown
*
* @param vp to change DICT_ATTR of.
* @return 0 on success (or if already unknown) else -1 on error.
*/
int fr_pair_to_unknown(VALUE_PAIR *vp)
{
DICT_ATTR const *da;
VERIFY_VP(vp);
if (vp->da->flags.is_unknown) {
return 0;
}
da = dict_unknown_afrom_fields(vp, vp->da->attr, vp->da->vendor);
if (!da) {
return -1;
}
vp->da = da;
return 0;
}
/** Find the pair with the matching DAs
*
*/
VALUE_PAIR *fr_pair_find_by_da(VALUE_PAIR *vp, DICT_ATTR const *da, int8_t tag)
{
vp_cursor_t cursor;
if(!fr_assert(da)) {
return NULL;
}
(void) fr_cursor_init(&cursor, &vp);
return fr_cursor_next_by_da(&cursor, da, tag);
}
/** 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 *vp, unsigned int attr, unsigned int vendor, int8_t tag)
{
vp_cursor_t cursor;
/* List head may be NULL if it contains no VPs */
if (!vp) return NULL;
VERIFY_LIST(vp, "");
(void) fr_cursor_init(&cursor, &vp);
return fr_cursor_next_by_num(&cursor, attr, vendor, tag);
}
/** Delete matching pairs
*
* Delete matching pairs from the attribute list.
*
* @param[in,out] first VP in list.
* @param[in] attr to match.
* @param[in] vendor to match.
* @param[in] tag to match. TAG_ANY matches any tag, TAG_NONE matches tagless VPs.
*
* @todo should take DAs and do a point comparison.
*/
void fr_pair_delete_by_num(VALUE_PAIR **first, unsigned int attr, unsigned int vendor, int8_t tag)
{
VALUE_PAIR *i, *next;
VALUE_PAIR **last = first;
for(i = *first; i; i = next) {
VERIFY_VP(i);
next = i->next;
if ((i->da->attr == attr) && (i->da->vendor == vendor) &&
(!i->da->flags.has_tag || TAG_EQ(tag, i->tag))) {
*last = next;
talloc_free(i);
} else {
last = &i->next;
}
}
}
/** Delete matching pairs by da
*
* Delete matching pairs from the attribute list.
*
* @param[in,out] first VP in list.
* @param[in] da to match.
*/
void fr_pair_delete_by_da(VALUE_PAIR **first, DICT_ATTR const *da)
{
VALUE_PAIR *i, *next;
VALUE_PAIR **last = first;
for(i = *first; i; i = next) {
VERIFY_VP(i);
next = i->next;
if (i->da == da) {
*last = next;
talloc_free(i);
} else {
last = &i->next;
}
}
}
/** Add a VP to the end of the list.
*
* Locates the end of 'first', and links an additional VP 'add' at the end.
*
* @param[in] first 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 **first, VALUE_PAIR *add)
{
VALUE_PAIR *i;
if (!add) return;
VERIFY_VP(add);
if (*first == NULL) {
*first = add;
return;
}
for (i = *first; i->next; i = i->next) {
#ifdef WITH_VERIFY_PTR
VERIFY_VP(i);
/*
* The same VP should never by added multiple times
* to the same list.
*/
fr_assert(i != add);
#endif
}
i->next = add;
}
/** Add a VP to the start of the list.
*
* Links the new VP to 'first', then points 'first' at the new VP.
*
* @param[in] first VP in linked list. Will add new VP to the start of this list.
* @param[in] add VP to add to list.
*/
void fr_pair_prepend(VALUE_PAIR **first, VALUE_PAIR *add)
{
VALUE_PAIR *i;
if (!add) return;
VERIFY_VP(add);
if (*first == NULL) {
*first = add;
return;
}
/*
* Find the end of the list to be prepended
*/
for (i = add; i->next; i = i->next) {
#ifdef WITH_VERIFY_PTR
VERIFY_VP(i);
/*
* The same VP should never by added multiple times
* to the same list.
*/
fr_assert(*first != i);
#endif
}
i->next = *first;
*first = add;
}
/** Replace all matching VPs
*
* Walks over 'first', and replaces the first 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] first VP in linked list. Will search and replace in this list.
* @param[in] replace VP to replace.
*/
void fr_pair_replace(VALUE_PAIR **first, VALUE_PAIR *replace)
{
VALUE_PAIR *i, *next;
VALUE_PAIR **prev = first;
VERIFY_VP(replace);
if (*first == NULL) {
*first = replace;
return;
}
/*
* Not an empty list, so find item if it is there, and
* replace it. Note, we always replace the first one, and
* we ignore any others that might exist.
*/
for(i = *first; i; i = next) {
VERIFY_VP(i);
next = i->next;
/*
* Found the first attribute, replace it,
* and return.
*/
if ((i->da == replace->da) && (!i->da->flags.has_tag || TAG_EQ(replace->tag, i->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;
}
int8_t fr_pair_cmp_by_da_tag(void const *a, void const *b)
{
VALUE_PAIR const *my_a = a;
VALUE_PAIR const *my_b = b;
VERIFY_VP(my_a);
VERIFY_VP(my_b);
uint8_t cmp;
cmp = fr_pointer_cmp(my_a->da, my_b->da);
if (cmp != 0) return cmp;
if (my_a->tag < my_b->tag) return -1;
if (my_a->tag > my_b->tag) return 1;
return 0;
}
static void fr_pair_list_sort_split(VALUE_PAIR *source, VALUE_PAIR **front, VALUE_PAIR **back)
{
VALUE_PAIR *fast;
VALUE_PAIR *slow;
/*
* Stopping condition - no more elements left to split
*/
if (!source || !source->next) {
*front = source;
*back = NULL;
return;
}
/*
* Fast advances twice as fast as slow, so when it gets to the end,
* slow will point to the middle of the linked list.
*/
slow = source;
fast = source->next;
while (fast) {
fast = fast->next;
if (fast) {
slow = slow->next;
fast = fast->next;
}
}
*front = source;
*back = slow->next;
slow->next = NULL;
}
static VALUE_PAIR *fr_pair_list_sort_merge(VALUE_PAIR *a, VALUE_PAIR *b, fr_cmp_t cmp)
{
VALUE_PAIR *result = NULL;
if (!a) return b;
if (!b) return a;
/*
* Compare the DICT_ATTRs and tags
*/
if (cmp(a, b) <= 0) {
result = a;
result->next = fr_pair_list_sort_merge(a->next, b, cmp);
} else {
result = b;
result->next = fr_pair_list_sort_merge(a, b->next, cmp);
}
return result;
}
/** Sort a linked list of VALUE_PAIRs using merge sort
*
* @param[in,out] vps List of VALUE_PAIRs to sort.
* @param[in] cmp to sort with
*/
void fr_pair_list_sort(VALUE_PAIR **vps, fr_cmp_t cmp)
{
VALUE_PAIR *head = *vps;
VALUE_PAIR *a;
VALUE_PAIR *b;
/*
* If there's 0-1 elements it must already be sorted.
*/
if (!head || !head->next) {
return;
}
fr_pair_list_sort_split(head, &a, &b); /* Split into sublists */
fr_pair_list_sort(&a, cmp); /* Traverse left */
fr_pair_list_sort(&b, cmp); /* Traverse right */
/*
* merge the two sorted lists together
*/
*vps = fr_pair_list_sort_merge(a, b, cmp);
}
/** Write an error to the library errorbuff detailing the mismatch
*
* Retrieve output with fr_strerror();
*
* @todo add thread specific talloc contexts.
*
* @param ctx a hack until we have thread specific talloc contexts.
* @param failed pair of attributes which didn't match.
*/
void fr_pair_validate_debug(TALLOC_CTX *ctx, VALUE_PAIR const *failed[2])
{
VALUE_PAIR const *filter = failed[0];
VALUE_PAIR const *list = failed[1];
char *value, *str;
(void) fr_strerror(); /* Clear any existing messages */
if (!fr_assert(!(!filter && !list))) return;
if (!list) {
if (!filter) return;
fr_strerror_printf("Attribute \"%s\" not found in list", filter->da->name);
return;
}
if (!filter || (filter->da != list->da)) {
fr_strerror_printf("Attribute \"%s\" not found in filter", list->da->name);
return;
}
if (!TAG_EQ(filter->tag, list->tag)) {
fr_strerror_printf("Attribute \"%s\" tag \"%i\" didn't match filter tag \"%i\"",
list->da->name, list->tag, filter->tag);
return;
}
value = vp_aprints_value(ctx, list, '"');
str = vp_aprints(ctx, filter, '"');
fr_strerror_printf("Attribute value \"%s\" didn't match filter: %s", value, str);
talloc_free(str);
talloc_free(value);
return;
}
/** Uses fr_pair_cmp to verify all VALUE_PAIRs in list match the filter defined by check
*
* @note will sort both filter and list in place.
*
* @param failed pointer to an array to write the pointers of the filter/list attributes that didn't match.
* May be NULL.
* @param filter attributes to check list against.
* @param list attributes, probably a request or reply
*/
bool fr_pair_validate(VALUE_PAIR const *failed[2], VALUE_PAIR *filter, VALUE_PAIR *list)
{
vp_cursor_t filter_cursor;
vp_cursor_t list_cursor;
VALUE_PAIR *check, *match;
if (!filter && !list) {
return true;
}
/*
* This allows us to verify the sets of validate and reply are equal
* i.e. we have a validate rule which matches every reply attribute.
*
* @todo this should be removed one we have sets and lists
*/
fr_pair_list_sort(&filter, fr_pair_cmp_by_da_tag);
fr_pair_list_sort(&list, fr_pair_cmp_by_da_tag);
check = fr_cursor_init(&filter_cursor, &filter);
match = fr_cursor_init(&list_cursor, &list);
while (match || check) {
/*
* Lists are of different lengths
*/
if (!match || !check) goto mismatch;
/*
* The lists are sorted, so if the first
* attributes aren't of the same type, then we're
* done.
*/
if (!ATTRIBUTE_EQ(check, match)) goto mismatch;
/*
* They're of the same type, but don't have the
* same values. This is a problem.
*
* Note that the RFCs say that for attributes of
* the same type, order is important.
*/
if (fr_pair_cmp(check, match) != 1) goto mismatch;
check = fr_cursor_next(&filter_cursor);
match = fr_cursor_next(&list_cursor);
}
return true;
mismatch:
if (failed) {
failed[0] = check;
failed[1] = match;
}
return false;
}
/** Uses fr_pair_cmp to verify all VALUE_PAIRs in list match the filter defined by check
*
* @note will sort both filter and list in place.
*
* @param failed pointer to an array to write the pointers of the filter/list attributes that didn't match.
* May be NULL.
* @param filter attributes to check list against.
* @param list attributes, probably a request or reply
*/
bool fr_pair_validate_relaxed(VALUE_PAIR const *failed[2], VALUE_PAIR *filter, VALUE_PAIR *list)
{
vp_cursor_t filter_cursor;
vp_cursor_t list_cursor;
VALUE_PAIR *check, *last_check = NULL, *match = NULL;
if (!filter && !list) {
return true;
}
/*
* This allows us to verify the sets of validate and reply are equal
* i.e. we have a validate rule which matches every reply attribute.
*
* @todo this should be removed one we have sets and lists
*/
fr_pair_list_sort(&filter, fr_pair_cmp_by_da_tag);
fr_pair_list_sort(&list, fr_pair_cmp_by_da_tag);
fr_cursor_init(&list_cursor, &list);
for (check = fr_cursor_init(&filter_cursor, &filter);
check;
check = fr_cursor_next(&filter_cursor)) {
/*
* Were processing check attributes of a new type.
*/
if (!ATTRIBUTE_EQ(last_check, check)) {
/*
* Record the start of the matching attributes in the pair list
* For every other operator we require the match to be present
*/
match = fr_cursor_next_by_da(&list_cursor, check->da, check->tag);
if (!match) {
if (check->op == T_OP_CMP_FALSE) continue;
goto mismatch;
}
fr_cursor_init(&list_cursor, &match);
last_check = check;
}
/*
* Now iterate over all attributes of the same type.
*/
for (match = fr_cursor_first(&list_cursor);
ATTRIBUTE_EQ(match, check);
match = fr_cursor_next(&list_cursor)) {
/*
* This attribute passed the filter
*/
if (!fr_pair_cmp(check, match)) goto mismatch;
}
}
return true;
mismatch:
if (failed) {
failed[0] = check;
failed[1] = match;
}
return false;
}
/** 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 or NULL on error.
*/
VALUE_PAIR *fr_pair_copy(TALLOC_CTX *ctx, VALUE_PAIR const *vp)
{
VALUE_PAIR *n;
if (!vp) return NULL;
VERIFY_VP(vp);
n = fr_pair_afrom_da(ctx, vp->da);
if (!n) return NULL;
memcpy(n, vp, sizeof(*n));
/*
* If the DA is unknown, steal "n" to "ctx". This does
* nothing for "n", but will also copy the unknown "da".
*/
if (n->da->flags.is_unknown) {
fr_pair_steal(ctx, n);
}
n->next = 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->value.xlat = talloc_typed_strdup(n, n->value.xlat);
return n;
}
switch (vp->da->type) {
case PW_TYPE_OCTETS:
n->vp_octets = NULL; /* else fr_pair_value_memcpy will free vp's value */
fr_pair_value_memcpy(n, vp->vp_octets, n->vp_length);
break;
case PW_TYPE_STRING:
n->vp_strvalue = NULL; /* else pairstrnpy will free vp's value */
fr_pair_value_bstrncpy(n, vp->vp_strvalue, n->vp_length);
break;
default:
break;
}
return n;
}
/** Copy a pairlist.
*
* Copy all pairs from 'from' regardless of tag, attribute or vendor.
*
* @param[in] ctx for new VALUE_PAIRs to be allocated in.
* @param[in] from whence to copy VALUE_PAIRs.
* @return the head of the new VALUE_PAIR list or NULL on error.
*/
VALUE_PAIR *fr_pair_list_copy(TALLOC_CTX *ctx, VALUE_PAIR *from)
{
vp_cursor_t src, dst;
VALUE_PAIR *out = NULL, *vp;
fr_cursor_init(&dst, &out);
for (vp = fr_cursor_init(&src, &from);
vp;
vp = fr_cursor_next(&src)) {
VERIFY_VP(vp);
vp = fr_pair_copy(ctx, vp);
if (!vp) {
fr_pair_list_free(&out);
return NULL;
}
fr_cursor_insert(&dst, vp); /* fr_pair_list_copy sets next pointer to NULL */
}
return out;
}
/** Copy matching pairs
*
* Copy pairs of a matching attribute number, vendor number and tag from the
* the input list to a new list, and returns the head of this list.
*
* @param[in] ctx for talloc
* @param[in] from whence to copy VALUE_PAIRs.
* @param[in] attr to match. If attribute PW_VENDOR_SPECIFIC and vendor 0,
* will match (and therefore copy) only VSAs.
* If attribute 0 and vendor 0 will match (and therefore copy) all
* attributes.
* @param[in] vendor to match.
* @param[in] tag to match, TAG_ANY matches any tag, TAG_NONE matches tagless VPs.
* @return the head of the new VALUE_PAIR list or NULL on error.
*/
VALUE_PAIR *fr_pair_list_copy_by_num(TALLOC_CTX *ctx, VALUE_PAIR *from,
unsigned int attr, unsigned int vendor, int8_t tag)
{
vp_cursor_t src, dst;
VALUE_PAIR *out = NULL, *vp;
fr_cursor_init(&dst, &out);
for (vp = fr_cursor_init(&src, &from);
vp;
vp = fr_cursor_next(&src)) {
VERIFY_VP(vp);
if (vp->da->flags.has_tag && !TAG_EQ(tag, vp->tag)) {
continue;
}
/*
* Attr/vendor of 0 means "move them all".
* It's better than "fr_pair_copy(foo,bar);bar=NULL"
*/
if ((attr == 0) && (vendor == 0)) {
goto do_copy;
}
/*
* vendor=0, attr = PW_VENDOR_SPECIFIC means
* "match any vendor attribute".
*/
if ((vendor == 0) && (attr == PW_VENDOR_SPECIFIC)) {
/*
* It's a VSA: copy it over.
*/
if (vp->da->vendor != 0) goto do_copy;
/*
* It's Vendor-Specific: copy it over.
*/
if (vp->da->attr == attr) goto do_copy;
/*
* It's not a VSA: ignore it.
*/
continue;
}
if ((vp->da->attr != attr) || (vp->da->vendor != vendor)) {
continue;
}
do_copy:
vp = fr_pair_copy(ctx, vp);
if (!vp) {
fr_pair_list_free(&out);
return NULL;
}
fr_cursor_insert(&dst, vp);
}
return out;
}
/** 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) {
DICT_ATTR *da;
char *p;
size_t size;
size = talloc_get_size(vp->da);
p = talloc_zero_array(vp, char, size);
da = (DICT_ATTR *) p;
talloc_set_type(p, DICT_ATTR);
memcpy(da, vp->da, size);
vp->da = da;
}
}
/** Move pairs from source list to destination list respecting operator
*
* @note This function does some additional magic that's probably not needed
* in most places. Consider using radius_pairmove in server code.
*
* @note fr_pair_list_free should be called on the head of the source list to free
* unmoved attributes (if they're no longer needed).
*
* @note Does not respect tags when matching.
*
* @param[in] ctx for talloc
* @param[in,out] to destination list.
* @param[in,out] from source list.
* @param[in] op operator for move.
*
* @see radius_pairmove
*/
void fr_pair_list_move(TALLOC_CTX *ctx, VALUE_PAIR **to, VALUE_PAIR **from, FR_TOKEN op)
{
VALUE_PAIR *i, *found;
VALUE_PAIR *head_new, **tail_new;
VALUE_PAIR *head_prepend;
VALUE_PAIR **tail_from;
if (!to || !from || !*from) return;
/*
* We're editing the "to" list while we're adding new
* attributes to it. We don't want the new attributes to
* be edited, so we create an intermediate list to hold
* them during the editing process.
*/
head_new = NULL;
tail_new = &head_new;
/*
* Any attributes that are requested to be prepended
* are added to a temporary list here
*/
head_prepend = NULL;
/*
* We're looping over the "from" list, moving some
* attributes out, but leaving others in place.
*/
tail_from = from;
while ((i = *tail_from) != NULL) {
VALUE_PAIR *j;
VERIFY_VP(i);
/*
* We never move Fall-Through.
*/
if (!i->da->vendor && i->da->attr == PW_FALL_THROUGH) {
tail_from = &(i->next);
continue;
}
/*
* Unlike previous versions, we treat all other
* attributes as normal. i.e. there's no special
* treatment for passwords or Hint.
*/
switch (i->op) {
/*
* Anything else are operators which
* shouldn't occur. We ignore them, and
* leave them in place.
*/
default:
tail_from = &(i->next);
continue;
/*
* Add it to the "to" list, but only if
* it doesn't already exist.
*/
case T_OP_EQ:
found = fr_pair_find_by_da(*to, i->da, TAG_ANY);
if (!found) goto do_add;
tail_from = &(i->next);
continue;
/*
* Add it to the "to" list, and delete any attribute
* of the same vendor/attr which already exists.
*/
case T_OP_SET:
found = fr_pair_find_by_da(*to, i->da, TAG_ANY);
if (!found) goto do_add;
/*
* Do NOT call fr_pair_delete_by_num() here,
* due to issues with re-writing
* "request->username".
*
* Everybody calls fr_pair_move, and
* expects it to work. We can't
* update request->username here,
* so instead we over-write the
* vp that it's pointing to.
*/
switch (found->da->type) {
default:
j = found->next;
memcpy(found, i, sizeof(*found));
found->next = j;
break;
case PW_TYPE_OCTETS:
fr_pair_value_memsteal(found, i->vp_octets);
i->vp_octets = NULL;
break;