/
map.c
1364 lines (1171 loc) · 33.5 KB
/
map.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 program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
/*
* $Id$
*
* @brief map / template functions
* @file main/map.c
*
* @ingroup AVP
*
* @copyright 2013 The FreeRADIUS server project
* @copyright 2013 Alan DeKok <aland@freeradius.org>
*/
RCSID("$Id$")
#include <freeradius-devel/radiusd.h>
#include <freeradius-devel/rad_assert.h>
#include <ctype.h>
static bool map_cast_from_hex(value_pair_map_t *map, FR_TOKEN rhs_type, char const *rhs)
{
size_t len;
ssize_t rlen;
uint8_t *ptr;
DICT_ATTR const *da;
VALUE_PAIR *vp;
value_data_t *data;
value_pair_tmpl_t *vpt;
rad_assert(map != NULL);
rad_assert(map->dst != NULL);
rad_assert(map->src == NULL);
rad_assert(rhs != NULL);
/*
* If the attribute is still unknown, go parse the RHS.
*/
da = dict_attrbyvalue(map->dst->tmpl_da->attr, map->dst->tmpl_da->vendor);
if (!da || da->flags.is_unknown) return false;
/*
* If the RHS is something OTHER than an octet
* string, go parse it as that.
*/
if (rhs_type != T_BARE_WORD) return false;
if ((rhs[0] != '0') || (tolower((int)rhs[1]) != 'x')) return false;
if (!rhs[2]) return false;
len = strlen(rhs + 2);
ptr = talloc_array(map, uint8_t, len >> 1);
if (!ptr) return false;
len = fr_hex2bin(ptr, len >> 1, rhs + 2, len);
/*
* If we can't parse it, or if it's malformed,
* it's still unknown.
*/
rlen = data2vp(NULL, NULL, NULL, NULL, da, ptr, len, len, &vp);
talloc_free(ptr);
if (rlen < 0) return false;
if ((size_t) rlen < len) {
free_vp:
pairfree(&vp);
return false;
}
/*
* Was still parsed as an unknown attribute.
*/
if (vp->da->flags.is_unknown) goto free_vp;
/*
* Manually create a value_template_t
*/
map->src = vpt = talloc_zero(map, value_pair_tmpl_t);
if (!map->src) goto free_vp;
vpt->type = TMPL_TYPE_DATA;
vpt->tmpl_da = da;
vpt->tmpl_length = vp->length;
vpt->tmpl_value = data = talloc(vpt, value_data_t);
if (!vpt->tmpl_value) goto free_vp;
if (vp->da->flags.is_pointer) {
data->ptr = talloc_memdup(vpt, vp->data.ptr, vp->length);
} else {
memcpy(data, &vp->data, sizeof(*data));
}
/*
* Set the RHS to the PARSED name, not the crap
* octet string which was input.
*/
vpt->name = vp_aprint_value(vpt, vp, true);
dict_attr_free(&map->dst->tmpl_da);
/*
* Set the LHS to the REAL attribute name.
*/
memcpy(&ptr, &map->dst->name, sizeof(ptr));
talloc_free(ptr);
map->dst->tmpl_da = da;
map->dst->name = talloc_strdup(map->dst, map->dst->tmpl_da->name);
pairfree(&vp);
return true;
}
/** Convert CONFIG_PAIR (which may contain refs) to value_pair_map_t.
*
* Treats the left operand as an attribute reference
* @verbatim<request>.<list>.<attribute>@endverbatim
*
* Treatment of left operand depends on quotation, barewords are treated as
* attribute references, double quoted values are treated as expandable strings,
* single quoted values are treated as literal strings.
*
* Return must be freed with talloc_free
*
* @param[in] ctx for talloc
* @param[in] cp to convert to map.
* @param[in] dst_request_def The default request to insert unqualified
* attributes into.
* @param[in] dst_list_def The default list to insert unqualified attributes
* into.
* @param[in] src_request_def The default request to resolve attribute
* references in.
* @param[in] src_list_def The default list to resolve unqualified attributes
* in.
* @return value_pair_map_t if successful or NULL on error.
*/
value_pair_map_t *map_from_cp(TALLOC_CTX *ctx, CONF_PAIR *cp,
request_refs_t dst_request_def, pair_lists_t dst_list_def,
request_refs_t src_request_def, pair_lists_t src_list_def)
{
value_pair_map_t *map;
char const *attr;
char const *value;
FR_TOKEN type;
CONF_ITEM *ci = cf_pairtoitem(cp);
if (!cp) return NULL;
map = talloc_zero(ctx, value_pair_map_t);
map->op = cf_pair_operator(cp);
map->ci = ci;
attr = cf_pair_attr(cp);
value = cf_pair_value(cp);
if (!value) {
cf_log_err(ci, "Missing attribute value");
goto error;
}
/*
* LHS must always be an attribute reference.
*/
map->dst = tmpl_afrom_attr_str(map, attr, dst_request_def, dst_list_def);
if (!map->dst) {
cf_log_err(ci, "%s", fr_strerror());
goto error;
}
/*
* RHS might be an attribute reference.
*/
type = cf_pair_value_type(cp);
if ((type == T_BARE_WORD) && (value[0] == '0') && (tolower((int)value[1] == 'x')) &&
map_cast_from_hex(map, type, value)) {
/* do nothing */
} else {
map->src = tmpl_afrom_str(map, value, type, src_request_def, src_list_def);
}
if (!map->src) {
cf_log_err(ci, "%s", fr_strerror());
goto error;
}
/*
* Anal-retentive checks.
*/
if (debug_flag > 2) {
if ((map->dst->type == TMPL_TYPE_ATTR) && (*attr != '&')) {
WARN("%s[%d]: Please change attribute reference to '&%s %s ...'",
cf_pair_filename(cp), cf_pair_lineno(cp),
attr, fr_int2str(fr_tokens, map->op, "<INVALID>"));
}
if ((map->src->type == TMPL_TYPE_ATTR) && (*value != '&')) {
WARN("%s[%d]: Please change attribute reference to '... %s &%s'",
cf_pair_filename(cp), cf_pair_lineno(cp),
fr_int2str(fr_tokens, map->op, "<INVALID>"), value);
}
}
/*
* Values used by unary operators should be literal ANY
*
* We then free the template and alloc a NULL one instead.
*/
if (map->op == T_OP_CMP_FALSE) {
if ((map->src->type != TMPL_TYPE_LITERAL) || (strcmp(map->src->name, "ANY") != 0)) {
WARN("%s[%d] Wildcard deletion MUST use '!* ANY'", cf_pair_filename(cp), cf_pair_lineno(cp));
}
tmpl_free(&map->src);
map->src = talloc_zero(map, value_pair_tmpl_t);
map->src->type = TMPL_TYPE_NULL;
}
/*
* Lots of sanity checks for insane people...
*/
/*
* We don't support implicit type conversion,
* except for "octets"
*/
if (map->dst->tmpl_da && map->src->tmpl_da &&
(map->src->tmpl_da->type != map->dst->tmpl_da->type) &&
(map->src->tmpl_da->type != PW_TYPE_OCTETS) &&
(map->dst->tmpl_da->type != PW_TYPE_OCTETS)) {
cf_log_err(ci, "Attribute type mismatch");
goto error;
}
/*
* What exactly where you expecting to happen here?
*/
if ((map->dst->type == TMPL_TYPE_ATTR) &&
(map->src->type == TMPL_TYPE_LIST)) {
cf_log_err(ci, "Can't copy list into an attribute");
goto error;
}
/*
* Depending on the attribute type, some operators are
* disallowed.
*/
if (map->dst->type == TMPL_TYPE_ATTR) {
switch (map->op) {
default:
cf_log_err(ci, "Invalid operator for attribute");
goto error;
case T_OP_EQ:
case T_OP_CMP_EQ:
case T_OP_ADD:
case T_OP_SUB:
case T_OP_LE:
case T_OP_GE:
case T_OP_CMP_FALSE:
case T_OP_SET:
break;
}
}
if (map->dst->type == TMPL_TYPE_LIST) {
/*
* Only += and :=, and !* operators are supported
* for lists.
*/
switch (map->op) {
case T_OP_CMP_FALSE:
break;
case T_OP_ADD:
if ((map->src->type != TMPL_TYPE_LIST) &&
(map->src->type != TMPL_TYPE_EXEC)) {
cf_log_err(ci, "Invalid source for list '+='");
goto error;
}
break;
case T_OP_SET:
if (map->src->type == TMPL_TYPE_EXEC) {
WARN("%s[%d] Please change ':=' to '=' for list assignment",
cf_pair_filename(cp), cf_pair_lineno(cp));
break;
}
if (map->src->type != TMPL_TYPE_LIST) {
cf_log_err(ci, "Invalid source for ':=' operator");
goto error;
}
break;
case T_OP_EQ:
if (map->src->type != TMPL_TYPE_EXEC) {
cf_log_err(ci, "Invalid source for '=' operator");
goto error;
}
break;
default:
cf_log_err(ci, "Operator \"%s\" not allowed for list assignment",
fr_int2str(fr_tokens, map->op, "<INVALID>"));
goto error;
}
}
return map;
error:
talloc_free(map);
return NULL;
}
/** Convert an 'update' config section into an attribute map.
*
* Uses 'name2' of section to set default request and lists.
*
* @param[in] cs the update section
* @param[out] head Where to store the head of the map.
* @param[in] dst_list_def The default destination list, usually dictated by
* the section the module is being called in.
* @param[in] src_list_def The default source list, usually dictated by the
* section the module is being called in.
* @param[in] max number of mappings to process.
* @return -1 on error, else 0.
*/
int map_from_cs(CONF_SECTION *cs, value_pair_map_t **head,
pair_lists_t dst_list_def, pair_lists_t src_list_def,
unsigned int max)
{
char const *cs_list, *p;
request_refs_t request_def = REQUEST_CURRENT;
CONF_ITEM *ci;
CONF_PAIR *cp;
unsigned int total = 0;
value_pair_map_t **tail, *map;
TALLOC_CTX *ctx;
*head = NULL;
tail = head;
if (!cs) return 0;
/*
* The first map has cs as the parent.
* The rest have the previous map as the parent.
*/
ctx = cs;
ci = cf_sectiontoitem(cs);
cs_list = p = cf_section_name2(cs);
if (cs_list) {
request_def = radius_request_name(&p, REQUEST_CURRENT);
if (request_def == REQUEST_UNKNOWN) {
cf_log_err(ci, "Default request specified "
"in mapping section is invalid");
return -1;
}
dst_list_def = fr_str2int(pair_lists, p, PAIR_LIST_UNKNOWN);
if (dst_list_def == PAIR_LIST_UNKNOWN) {
cf_log_err(ci, "Default list \"%s\" specified "
"in mapping section is invalid", p);
return -1;
}
}
for (ci = cf_item_find_next(cs, NULL);
ci != NULL;
ci = cf_item_find_next(cs, ci)) {
if (total++ == max) {
cf_log_err(ci, "Map size exceeded");
goto error;
}
if (!cf_item_is_pair(ci)) {
cf_log_err(ci, "Entry is not in \"attribute = "
"value\" format");
goto error;
}
cp = cf_itemtopair(ci);
map = map_from_cp(ctx, cp, request_def, dst_list_def,
REQUEST_CURRENT, src_list_def);
if (!map) {
goto error;
}
ctx = *tail = map;
tail = &(map->next);
}
return 0;
error:
TALLOC_FREE(*head);
return -1;
}
/** Convert strings to value_pair_map_t
*
* Treatment of operands depends on quotation, barewords are treated
* as attribute references, double quoted values are treated as
* expandable strings, single quoted values are treated as literal
* strings.
*
* Return must be freed with talloc_free
*
* @param[in] ctx for talloc
* @param[in] lhs of the operation
* @param[in] lhs_type type of the LHS string
* @param[in] op the operation to perform
* @param[in] rhs of the operation
* @param[in] rhs_type type of the RHS string
* @param[in] dst_request_def The default request to insert unqualified
* attributes into.
* @param[in] dst_list_def The default list to insert unqualified attributes
* into.
* @param[in] src_request_def The default request to resolve attribute
* references in.
* @param[in] src_list_def The default list to resolve unqualified attributes
* in.
* @return value_pair_map_t if successful or NULL on error.
*/
value_pair_map_t *map_from_fields(TALLOC_CTX *ctx, char const *lhs, FR_TOKEN lhs_type,
FR_TOKEN op, char const *rhs, FR_TOKEN rhs_type,
request_refs_t dst_request_def,
pair_lists_t dst_list_def,
request_refs_t src_request_def,
pair_lists_t src_list_def)
{
value_pair_map_t *map;
map = talloc_zero(ctx, value_pair_map_t);
map->dst = tmpl_afrom_str(map, lhs, lhs_type, dst_request_def, dst_list_def);
if (!map->dst) {
error:
talloc_free(map);
return NULL;
}
map->op = op;
if ((map->dst->type == TMPL_TYPE_ATTR) &&
map->dst->tmpl_da->flags.is_unknown &&
map_cast_from_hex(map, rhs_type, rhs)) {
return map;
}
map->src = tmpl_afrom_str(map, rhs, rhs_type, src_request_def, src_list_def);
if (!map->src) goto error;
return map;
}
/** Convert a value pair string to valuepair map
*
* Takes a valuepair string with list and request qualifiers and converts it into a
* value_pair_map_t.
*
* @param out Where to write the new map (must be freed with talloc_free()).
* @param request Current request.
* @param vp_str string to parse.
* @param dst_request_def to use if attribute isn't qualified.
* @param dst_list_def to use if attribute isn't qualified.
* @param src_request_def to use if attribute isn't qualified.
* @param src_list_def to use if attribute isn't qualified.
* @return 0 on success, < 0 on error.
*/
int map_from_vp_str(value_pair_map_t **out, REQUEST *request, char const *vp_str,
request_refs_t dst_request_def, pair_lists_t dst_list_def,
request_refs_t src_request_def, pair_lists_t src_list_def)
{
char const *p = vp_str;
FR_TOKEN quote;
VALUE_PAIR_RAW raw;
value_pair_map_t *map;
quote = gettoken(&p, raw.l_opand, sizeof(raw.l_opand), false);
switch (quote) {
case T_BARE_WORD:
break;
case T_INVALID:
error:
REDEBUG("Failed tokenising attribute string: %s", fr_strerror());
return -1;
default:
REDEBUG("Failed tokenising attribute string: Left operand must be an attribute");
return -1;
}
raw.op = getop(&p);
if (raw.op == T_INVALID) goto error;
raw.quote = gettoken(&p, raw.r_opand, sizeof(raw.r_opand), false);
if (raw.quote == T_INVALID) goto error;
if (!fr_str_tok[raw.quote]) {
REDEBUG("Failed tokenising attribute string: Right operand must be an attribute or string");
return -1;
}
map = map_from_fields(request, raw.l_opand, T_BARE_WORD, raw.op, raw.r_opand, raw.quote,
dst_request_def, dst_list_def, src_request_def, src_list_def);
if (!map) {
REDEBUG("Failed parsing attribute string: %s", fr_strerror());
return -1;
}
*out = map;
return 0;
}
/** Process map which has exec as a src
*
* Evaluate maps which specify exec as a src. This may be used by various sorts of update sections, and so
* has been broken out into it's own function.
*
* @param[out] out Where to write the VALUE_PAIR(s).
* @param[in] request structure (used only for talloc).
* @param[in] map the map. The LHS (dst) must be TMPL_TYPE_ATTR or TMPL_TYPE_LIST. The RHS (src) must be TMPL_TYPE_EXEC.
* @return -1 on failure, 0 on success.
*/
static int map_exec_to_vp(VALUE_PAIR **out, REQUEST *request, value_pair_map_t const *map)
{
int result;
char *expanded = NULL;
char answer[1024];
VALUE_PAIR **input_pairs = NULL;
VALUE_PAIR *output_pairs = NULL;
*out = NULL;
rad_assert(map->src->type == TMPL_TYPE_EXEC);
rad_assert((map->dst->type == TMPL_TYPE_ATTR) || (map->dst->type == TMPL_TYPE_LIST));
/*
* We always put the request pairs into the environment
*/
input_pairs = radius_list(request, PAIR_LIST_REQUEST);
/*
* Automagically switch output type depending on our destination
* If dst is a list, then we create attributes from the output of the program
* if dst is an attribute, then we create an attribute of that type and then
* call pairparsevalue on the output of the script.
*/
result = radius_exec_program(request, map->src->name, true, true,
answer, sizeof(answer), EXEC_TIMEOUT,
input_pairs ? *input_pairs : NULL,
(map->dst->type == TMPL_TYPE_LIST) ? &output_pairs : NULL);
talloc_free(expanded);
if (result != 0) {
talloc_free(output_pairs);
return -1;
}
switch (map->dst->type) {
case TMPL_TYPE_LIST:
if (!output_pairs) {
REDEBUG("No valid attributes received from program");
return -2;
}
*out = output_pairs;
return 0;
case TMPL_TYPE_ATTR:
{
VALUE_PAIR *vp;
vp = pairalloc(request, map->dst->tmpl_da);
if (!vp) return -1;
vp->op = map->op;
if (pairparsevalue(vp, answer, 0) < 0) {
pairfree(&vp);
return -2;
}
*out = vp;
return 0;
}
default:
rad_assert(0);
}
return -1;
}
/** Convert a map to a VALUE_PAIR.
*
* @param[out] out Where to write the VALUE_PAIR(s), which may be NULL if not found
* @param[in] request structure (used only for talloc)
* @param[in] map the map. The LHS (dst) has to be TMPL_TYPE_ATTR or TMPL_TYPE_LIST.
* @param[in] ctx unused
* @return 0 on success, -1 on failure
*/
int map_to_vp(VALUE_PAIR **out, REQUEST *request, value_pair_map_t const *map, UNUSED void *ctx)
{
int rcode = 0;
VALUE_PAIR *vp = NULL, *new, *found = NULL;
DICT_ATTR const *da;
REQUEST *context = request;
vp_cursor_t cursor;
*out = NULL;
/*
* Special case for !*, we don't need to parse RHS as this is a unary operator.
*/
if (map->op == T_OP_CMP_FALSE) return 0;
/*
* List to list found, this is a special case because we don't need
* to allocate any attributes, just finding the current list, and change
* the op.
*/
if ((map->dst->type == TMPL_TYPE_LIST) && (map->src->type == TMPL_TYPE_LIST)) {
VALUE_PAIR **from = NULL;
if (radius_request(&context, map->src->tmpl_request) == 0) {
from = radius_list(context, map->src->tmpl_list);
}
if (!from) return 0;
found = paircopy(request, *from);
/*
* List to list copy is empty if the src list has no attributes.
*/
if (!found) return 0;
for (vp = fr_cursor_init(&cursor, &found);
vp;
vp = fr_cursor_next(&cursor)) {
vp->op = T_OP_ADD;
}
*out = found;
return 0;
}
/*
* Deal with all non-list operations.
*/
da = map->dst->tmpl_da ? map->dst->tmpl_da : map->src->tmpl_da;
/*
* And parse the RHS
*/
switch (map->src->type) {
ssize_t slen;
char *str;
case TMPL_TYPE_XLAT_STRUCT:
rad_assert(map->dst->tmpl_da); /* Need to know where were going to write the new attribute */
rad_assert(map->src->tmpl_xlat != NULL);
new = pairalloc(request, da);
if (!new) return -1;
str = NULL;
slen = radius_axlat_struct(&str, request, map->src->tmpl_xlat, NULL, NULL);
if (slen < 0) {
rcode = slen;
goto error;
}
/*
* We do the debug printing because radius_axlat_struct
* doesn't have access to the original string. It's been
* mangled during the parsing to xlat_exp_t
*/
RDEBUG2("EXPAND %s", map->src->name);
RDEBUG2(" --> %s", str);
rcode = pairparsevalue(new, str, 0);
talloc_free(str);
if (rcode < 0) {
pairfree(&new);
goto error;
}
new->op = map->op;
*out = new;
break;
case TMPL_TYPE_XLAT:
rad_assert(map->dst->tmpl_da); /* Need to know where were going to write the new attribute */
new = pairalloc(request, da);
if (!new) return -1;
str = NULL;
slen = radius_axlat(&str, request, map->src->name, NULL, NULL);
if (slen < 0) {
rcode = slen;
goto error;
}
rcode = pairparsevalue(new, str, 0);
talloc_free(str);
if (rcode < 0) {
pairfree(&new);
goto error;
}
new->op = map->op;
*out = new;
break;
case TMPL_TYPE_LITERAL:
new = pairalloc(request, da);
if (!new) return -1;
if (pairparsevalue(new, map->src->name, 0) < 0) {
rcode = 0;
goto error;
}
new->op = map->op;
*out = new;
break;
case TMPL_TYPE_ATTR:
{
vp_cursor_t from;
if (map->dst->type != TMPL_TYPE_ATTR) {
rad_assert(map->dst->tmpl_da == NULL);
} else {
rad_assert(map->dst->tmpl_da != NULL);
/*
* Matching type, OR src/dst is octets.
*/
rad_assert((map->src->tmpl_da->type == map->dst->tmpl_da->type) ||
(map->src->tmpl_da->type == PW_TYPE_OCTETS) ||
(map->dst->tmpl_da->type == PW_TYPE_OCTETS));
}
/*
* @todo should log error, and return -1 for v3.1 (causes update to fail)
*/
if (tmpl_copy_vps(request, &found, request, map->src) < 0) return 0;
vp = fr_cursor_init(&from, &found);
/*
* Src/Dst attributes don't match, convert src attributes
* to match dst.
*/
if ((map->dst->type == TMPL_TYPE_ATTR) &&
(map->src->tmpl_da->type != map->dst->tmpl_da->type)) {
vp_cursor_t to;
(void) fr_cursor_init(&to, out);
for (; vp; vp = fr_cursor_next(&from)) {
new = pairalloc(request, da);
if (!new) return -1;
if (pairdatacpy(new, vp->da, &vp->data, vp->length) < 0) {
REDEBUG("Attribute conversion failed: %s", fr_strerror());
pairfree(&found);
pairfree(&new);
return -1;
}
vp = fr_cursor_remove(&from);
talloc_free(vp);
new->op = map->op;
fr_cursor_insert(&to, new);
}
return 0;
}
/*
* Otherwise we just need to fixup the attribute types
* and operators
*/
for (; vp; vp = fr_cursor_next(&from)) {
vp->da = da;
vp->op = map->op;
}
*out = found;
}
break;
case TMPL_TYPE_DATA:
rad_assert(map->src && map->src->tmpl_da);
rad_assert(map->dst && map->dst->tmpl_da);
rad_assert(map->src->tmpl_da->type == map->dst->tmpl_da->type);
new = pairalloc(request, da);
if (!new) return -1;
if (pairdatacpy(new, map->src->tmpl_da, map->src->tmpl_value, map->src->tmpl_length) < 0) goto error;
new->op = map->op;
*out = new;
break;
/*
* This essentially does the same as rlm_exec xlat, except it's non-configurable.
* It's only really here as a convenience for people who expect the contents of
* backticks to be executed in a shell.
*
* exec string is xlat expanded and arguments are shell escaped.
*/
case TMPL_TYPE_EXEC:
return map_exec_to_vp(out, request, map);
default:
rad_assert(0); /* Should have been caught at parse time */
error:
pairfree(&vp);
return rcode;
}
return 0;
}
#define DEBUG_OVERWRITE(_old, _new) \
do {\
if (RDEBUG_ENABLED3) {\
char *old = vp_aprint_value(request, _old, true);\
char *new = vp_aprint_value(request, _new, true);\
RDEBUG3("Overwriting value \"%s\" with \"%s\"", old, new);\
talloc_free(old);\
talloc_free(new);\
}\
} while (0)
/** Convert value_pair_map_t to VALUE_PAIR(s) and add them to a REQUEST.
*
* Takes a single value_pair_map_t, resolves request and list identifiers
* to pointers in the current request, then attempts to retrieve module
* specific value(s) using callback, and adds the resulting values to the
* correct request/list.
*
* @param request The current request.
* @param map specifying destination attribute and location and src identifier.
* @param func to retrieve module specific values and convert them to
* VALUE_PAIRS.
* @param ctx to be passed to func.
* @return -1 if the operation failed, -2 in the source attribute wasn't valid, 0 on success.
*/
int map_to_request(REQUEST *request, value_pair_map_t const *map, radius_map_getvalue_t func, void *ctx)
{
int rcode = 0;
int num;
VALUE_PAIR **list, *vp, *dst, *head = NULL;
bool found = false;
REQUEST *context;
TALLOC_CTX *parent;
vp_cursor_t dst_list, src_list;
/*
* Sanity check inputs. We can have a list or attribute
* as a destination.
*/
if ((map->dst->type != TMPL_TYPE_LIST) &&
(map->dst->type != TMPL_TYPE_ATTR)) {
REDEBUG("Invalid mapping destination");
return -2;
}
context = request;
if (radius_request(&context, map->dst->tmpl_request) < 0) {
REDEBUG("Mapping \"%s\" -> \"%s\" invalid in this context", map->src->name, map->dst->name);
return -2;
}
/*
* If there's no CoA packet and we're updating it,
* auto-allocate it.
*/
if (((map->dst->tmpl_list == PAIR_LIST_COA) ||
(map->dst->tmpl_list == PAIR_LIST_DM)) && !request->coa) {
request_alloc_coa(context);
context->coa->proxy->code = (map->dst->tmpl_list == PAIR_LIST_COA) ?
PW_CODE_COA_REQUEST :
PW_CODE_DISCONNECT_REQUEST;
}
list = radius_list(context, map->dst->tmpl_list);
if (!list) {
REDEBUG("Mapping \"%s\" -> \"%s\" invalid in this context", map->src->name, map->dst->name);
return -2;
}
parent = radius_list_ctx(context, map->dst->tmpl_list);
rad_assert(parent);
/*
* The callback should either return -1 to signify operations error,
* -2 when it can't find the attribute or list being referenced, or
* 0 to signify success. It may return "sucess", but still have no
* VPs to work with.
*/
if (map->src->type != TMPL_TYPE_NULL) {
rcode = func(&head, request, map, ctx);
if (rcode < 0) {
rad_assert(!head);
return rcode;
}
if (!head) return rcode;
} else {
if (debug_flag) map_debug_log(request, map, NULL);
}
/*
* Reparent the VPs (func may return multiple)
*/
for (vp = fr_cursor_init(&src_list, &head);
vp;
vp = fr_cursor_next(&src_list)) {
VERIFY_VP(vp);
if (debug_flag) map_debug_log(request, map, vp);
pairsteal(parent, vp);
}
/*
* The destination is a list (which is a completely different set of operations)
*/
if (map->dst->type == TMPL_TYPE_LIST) {
switch (map->op) {
case T_OP_CMP_FALSE:
/* We don't need the src VPs (should just be 'ANY') */
rad_assert(!head);
/* Clear the entire dst list */
pairfree(list);
if (map->dst->tmpl_list == PAIR_LIST_REQUEST) {
context->username = NULL;
context->password = NULL;
}
return 0;
case T_OP_SET:
if (map->src->type == TMPL_TYPE_LIST) {
pairfree(list);
*list = head;
} else {
case T_OP_EQ:
rad_assert(map->src->type == TMPL_TYPE_EXEC);
pairmove(parent, list, &head);
pairfree(&head);
}
goto finish;
case T_OP_ADD:
pairadd(list, head);
head = NULL;
goto finish;
default:
pairfree(&head);
return -1;
}
}
/*
* Find the destination attribute. We leave with either
* the dst_list and vp pointing to the attribute or the VP
* being NULL (no attribute at that index).
*/
num = map->dst->tmpl_num;
(void) fr_cursor_init(&dst_list, list);
if (num != NUM_ANY) {
while ((dst = fr_cursor_next_by_da(&dst_list, map->dst->tmpl_da, map->dst->tmpl_tag))) {
if (num-- == 0) break;