/
xlat_tokenize.c
1017 lines (839 loc) · 22.9 KB
/
xlat_tokenize.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$
*
* @file xlat_tokenize.c
* @brief String expansion ("translation"). Tokenizes xlat expansion strings.
*
* @copyright 2000,2006 The FreeRADIUS server project
* @copyright 2000 Alan DeKok (aland@freeradius.org)
*/
RCSID("$Id$")
#include <freeradius-devel/server/rad_assert.h>
#include <freeradius-devel/server/request.h>
#include <freeradius-devel/server/regex.h>
#include <freeradius-devel/server/xlat_priv.h>
#include <ctype.h>
#undef XLAT_DEBUG
#ifdef DEBUG_XLAT
# define XLAT_DEBUG DEBUG3
#else
# define XLAT_DEBUG(...)
#endif
/** Allocate an xlat node
*
* @param[in] ctx to allocate node in.
* @param[in] type of the node.
* @param[in] in original input string.
* @param[in] inlen Portion of the input string that this node represents.
* @return A new xlat node.
*/
static inline xlat_exp_t *xlat_exp_alloc(TALLOC_CTX *ctx, xlat_type_t type, char const *in, size_t inlen)
{
xlat_exp_t *node;
MEM(node = talloc_zero(ctx, xlat_exp_t));
node->type = type;
if (in) node->fmt = talloc_bstrndup(node, in, inlen);
return node;
}
/** Try to convert an xlat to a tmpl for efficiency
*
* @param ctx to allocate new vp_tmpl_t in.
* @param node to convert.
* @return
* - NULL if unable to convert (not necessarily error).
* - A new #vp_tmpl_t.
*/
vp_tmpl_t *xlat_to_tmpl_attr(TALLOC_CTX *ctx, xlat_exp_t *node)
{
vp_tmpl_t *vpt;
if (node->next || (node->type != XLAT_ATTRIBUTE) || !tmpl_is_attr(node->attr)) return NULL;
/*
* Concat means something completely different as an attribute reference
* Count isn't implemented.
*/
if ((node->attr->tmpl_num == NUM_COUNT) || (node->attr->tmpl_num == NUM_ALL)) return NULL;
vpt = tmpl_alloc(ctx, TMPL_TYPE_ATTR, node->fmt, talloc_array_length(node->fmt) - 1, T_BARE_WORD);
if (!vpt) return NULL;
memcpy(&vpt->data, &node->attr->data, sizeof(vpt->data));
TMPL_VERIFY(vpt);
return vpt;
}
/** Convert attr tmpl to an xlat for &attr[*]
*
* @param ctx to allocate new xlat_expt_t in.
* @param vpt to convert.
* @return
* - NULL if unable to convert (not necessarily error).
* - a new #vp_tmpl_t.
*/
xlat_exp_t *xlat_from_tmpl_attr(TALLOC_CTX *ctx, vp_tmpl_t *vpt)
{
xlat_exp_t *node;
if (!tmpl_is_attr(vpt)) return NULL;
node = xlat_exp_alloc(ctx, XLAT_ATTRIBUTE, vpt->name, vpt->len);
node->attr = tmpl_alloc(node, TMPL_TYPE_ATTR, node->fmt, talloc_array_length(node->fmt) - 1, T_BARE_WORD);
memcpy(&node->attr->data, &vpt->data, sizeof(vpt->data));
return node;
}
static ssize_t xlat_tokenize_expansion(TALLOC_CTX *ctx, xlat_exp_t **head, char const *in, size_t inlen,
vp_tmpl_rules_t const *rules);
static ssize_t xlat_tokenize_literal(TALLOC_CTX *ctx, xlat_exp_t **head, char const *in, size_t inlen,
bool brace, vp_tmpl_rules_t const *rules);
static ssize_t xlat_tokenize_alternation(TALLOC_CTX *ctx, xlat_exp_t **head, char const *in, size_t inlen,
vp_tmpl_rules_t const *rules)
{
ssize_t slen;
char const *p = in;
xlat_exp_t *node;
rad_assert(in[0] == '%');
rad_assert(in[1] == '{');
rad_assert(in[2] == '%');
rad_assert(in[3] == '{');
XLAT_DEBUG("ALTERNATE <-- %s", in);
node = xlat_exp_alloc(ctx, XLAT_ALTERNATE, NULL, 0);
p += 2;
slen = xlat_tokenize_expansion(node, &node->child, p, in + inlen - p, rules);
if (slen <= 0) {
talloc_free(node);
return slen - (p - in);
}
p += slen;
if (p[0] != ':') {
talloc_free(node);
fr_strerror_printf("Expected ':' after first expansion, got '%pV'",
fr_box_strvalue_len(p, 1));
return -(p - in);
}
p++;
if (p[0] != '-') {
talloc_free(node);
fr_strerror_printf("Expected '-' after ':'");
return -(p - in);
}
p++;
/*
* Allow the RHS to be empty as a special case.
*/
switch (*p) {
case '}':
node->alternate = xlat_exp_alloc(node, XLAT_LITERAL, "", 0);
node->async_safe = node->child->async_safe;
*head = node;
return (p + 1) - in;
case '\0':
fr_strerror_printf("No matching closing brace");
talloc_free(node);
return -2;
}
/*
* Parse the alternate expansion.
*/
slen = xlat_tokenize_literal(node, &node->alternate, p, in + inlen - p, true, rules);
if (slen <= 0) {
talloc_free(node);
return slen - (p - in);
}
if (!node->alternate) {
talloc_free(node);
fr_strerror_printf("Empty expansion is invalid");
return -(p - in);
}
p += slen;
node->async_safe = (node->child->async_safe && node->alternate->async_safe);
*head = node;
return p - in;
}
#ifdef HAVE_REGEX
/** Parse an xlat reference
*
* Allows access to a subcapture groups
* @verbatim %{<num>} @endverbatim
*
*/
static inline ssize_t xlat_tokenize_regex(TALLOC_CTX *ctx, xlat_exp_t **head, char const *in, size_t inlen)
{
unsigned long num;
char const *p, *end;
xlat_exp_t *node;
/*
* Must be at least %{0} in length
*/
if (inlen < 4) return 0;
rad_assert(in[0] == '%');
rad_assert(in[1] == '{');
p = in + 2;
end = in + inlen;
if (*p == '}') {
fail:
fr_strerror_printf("Invalid regex reference. Must be in range 0-%u", REQUEST_MAX_REGEX);
return -(p - in); /* error */
}
num = 0;
while (p < end) {
if (*p == '}') break;
/*
* We allow numbers followed by letters for attributes such as 3GPP-...
*/
if (!((p[0] >= '0') && (p[0] <= '9'))) return 0;
num *= 10;
num += p[0] - '0';
p++;
if (num > REQUEST_MAX_REGEX) {
p = in + 2;
goto fail;
};
}
if (*p != '}') {
fr_strerror_printf("No matching closing brace");
return -1;
}
XLAT_DEBUG("REGEX <-- %pV", fr_box_strvalue_len(in, (p - in) + 1));
node = xlat_exp_alloc(ctx, XLAT_REGEX, p, p - (in + 2));
node->regex_index = num;
*head = node;
p++; /* Skip over '}' */
return p - in;
}
#endif
/** Parse an xlat function and its child arguments
*
* Parses a function call string in the format
* @verbatim %{<func>:<arguments} @endverbatim
*
*/
static inline ssize_t xlat_tokenize_function(TALLOC_CTX *ctx, xlat_exp_t **head, char const *in, size_t inlen,
vp_tmpl_rules_t const *rules)
{
ssize_t slen;
char const *p, *q, *end;
xlat_exp_t *node;
xlat_t *func;
if (inlen < 2) return 0;
rad_assert(in[0] == '%');
rad_assert(in[1] == '{');
p = in + 2;
end = in + inlen;
/*
* %{module:args}
*/
for (q = p; q < end; q++) {
if (*q == ':') break;
/*
* Special characters, spaces, etc. cannot be
* module names.
*/
if (*q < '0') return 0;
}
if (q >= end) return 0;
func = xlat_func_find(p, q - p);
if (!func) return 0;
/*
* Allocate a node to hold the function
*/
node = xlat_exp_alloc(ctx, XLAT_FUNC, p, q - p);
node->xlat = func;
p = q + 1;
XLAT_DEBUG("FUNC <-- %s ... %s", node->in, p);
/*
* Now parse the child nodes that form the
* function's arguments.
*/
slen = xlat_tokenize_literal(node, &node->child, p, in + inlen - p, true, rules);
if (slen < 0) {
talloc_free(node);
return slen - (p - in); /* error */
}
p += slen;
if (*(p - 1) != '}') { /* @todo: xlat_tokenize_literal should not consume the closing brace */
fr_strerror_printf("No matching closing brace");
talloc_free(node);
return -1; /* error at the second character of format string */
}
node->async_safe = (func->async_safe && (!node->child || node->child->async_safe));
*head = node;
return p - in;
}
/** Parse an attribute ref or a virtual attribute
*
*/
static inline ssize_t xlat_tokenize_attribute(TALLOC_CTX *ctx, xlat_exp_t **head, char const *in, size_t inlen,
vp_tmpl_rules_t const *rules)
{
ssize_t slen;
char const *p, *q;
attr_ref_error_t err;
vp_tmpl_t *vpt = NULL;
xlat_exp_t *node;
xlat_t *func;
/*
* We need a local copy as we always allow unknowns.
* This is because not all attribute references
* reference real attributes in the dictionaries,
* and instead are "virtual" attributes like
* Foreach-Variable-N.
*/
vp_tmpl_rules_t our_rules;
if (inlen < 2) return 0;
rad_assert(in[0] == '%');
rad_assert(in[1] == '{');
if (rules) {
memcpy(&our_rules, rules, sizeof(our_rules));
} else {
memset(&our_rules, 0, sizeof(our_rules));
}
p = in + 2;
our_rules.allow_undefined = true; /* So we can check for virtual attributes later */
our_rules.prefix = VP_ATTR_REF_PREFIX_NO; /* Must be NO to stop %{&User-Name} */
slen = tmpl_afrom_attr_substr(NULL, &err, &vpt, p, inlen - 2, &our_rules);
if (slen <= 0) {
/*
* If the parse error occurred before the ':'
* then the error is changed to 'Unknown module',
* as it was more likely to be a bad module name,
* than a request qualifier.
*/
if (err == ATTR_REF_ERROR_INVALID_LIST_QUALIFIER) {
fr_strerror_printf("Unknown expansion function or invalid list qualifier");
}
return slen - (p - in); /* error somewhere after second character */
}
q = p + slen;
if (*q != '}') {
fr_strerror_printf("No matching closing brace");
talloc_free(vpt);
return -1; /* error @ second character of format string */
}
q++; /* Skip over the closing brace */
/*
* Might be a virtual XLAT attribute, which is identical
* to a normal function but called without an argument
* list.
*/
if (tmpl_is_attr_undefined(vpt)) {
func = xlat_func_find(vpt->tmpl_unknown_name, -1);
if (func && (func->type == XLAT_FUNC_SYNC)) {
node = xlat_exp_alloc(ctx, XLAT_VIRTUAL,
vpt->tmpl_unknown_name, talloc_array_length(vpt->tmpl_unknown_name) - 1);
talloc_free(vpt); /* Free the tmpl, we don't need it */
XLAT_DEBUG("VIRTUAL <-- %s", node->fmt);
node->xlat = func;
node->async_safe = func->async_safe;
*head = node;
return q - in;
}
talloc_free(vpt);
fr_strerror_printf("Unknown attribute");
return -2; /* error @ third character of format string */
}
node = xlat_exp_alloc(ctx, XLAT_ATTRIBUTE, vpt->name, vpt->len);
node->attr = talloc_steal(node, vpt);
node->async_safe = true; /* attribute expansions are always async-safe */
*head = node;
return q - in;
}
static ssize_t xlat_tokenize_expansion(TALLOC_CTX *ctx, xlat_exp_t **head, char const *in, size_t inlen,
vp_tmpl_rules_t const *rules)
{
ssize_t slen;
char const *p = in, *q, *end;
XLAT_DEBUG("EXPANSION <-- %s", in);
if (inlen < 2) return 0;
rad_assert(in[0] == '%');
rad_assert(in[1] == '{');
/*
* %{%{...}:-bar}
*/
if ((in[2] == '%') && (in[3] == '{')) return xlat_tokenize_alternation(ctx, head, in, inlen, rules);
/*
* %{:-bar}
*/
if ((in[2] == ':') && (in[3] == '-')) {
fr_strerror_printf("First item in alternation cannot be empty");
return -2;
}
#ifdef HAVE_REGEX
/*
* Handle regex's %{<num>} specially.
*/
if (isdigit(in[2])) {
slen = xlat_tokenize_regex(ctx, head, in, inlen);
if (slen != 0) return slen; /* If slen == 0 means this wasn't a regex */
}
#endif /* HAVE_REGEX */
/*
* %{Attr-Name}
* %{Attr-Name[#]}
* %{Tunnel-Password:1}
* %{Tunnel-Password:1[#]}
* %{request:Attr-Name}
* %{request:Tunnel-Password:1}
* %{request:Tunnel-Password:1[#]}
* %{mod:foo}
*/
/*
* This is for efficiency, so we don't search for an xlat,
* when what's being referenced is obviously an attribute.
*/
p = in;
end = in + inlen;
for (q = p; (q < end) && *q; q++) {
if (*q == ':') break; /* First special token is a ':' i.e. '%{func:' */
if (isspace((int) *q)) break; /* First special token is a ' ' - Likely a syntax error */
if (*q == '[') break; /* First special token is a '[' i.e. '%{attr[<idx>]}' */
if (*q == '}') break; /* First special token is a '}' i.e. '%{<attrref>}' */
}
XLAT_DEBUG("EXPANSION HINT TOKEN '%c'", *q);
/*
* Check for empty expressions %{} %{: %{[
*/
if (q == (p + 2)) {
switch (*q) {
case '}':
fr_strerror_printf("Empty expression is invalid");
return -2; /* error @ third character of format string */
case ':':
fr_strerror_printf("Missing expansion function or list qualifier");
return -2;
case '[':
fr_strerror_printf("Missing attribute name");
return -2;
default:
break;
}
}
/*
* Hint token is a ':' it's either:
* - An xlat function %{<func>:<args}
* - An attribute reference with a list separator %{<list>:<attr>}
*/
switch (*q) {
case ':':
slen = xlat_tokenize_function(ctx, head, in, inlen, rules);
if (slen != 0) return slen;
/* FALL-THROUGH */
/*
* Hint token is a:
* - '[' - Which is an attribute index, so it must be an attribute.
* - '}' - The end of the expansion, which means it was a bareword.
*/
case '}':
case '[':
slen = xlat_tokenize_attribute(ctx, head, in, inlen, rules);
if (slen < 0) return slen;
rad_assert(slen != 0);
p += slen;
break;
/*
* Hint token is a '\0'
*
* This means the end of a string not containing any of the other
* tokens was reached.
*
* e.g. '%{myfirstxlat'
*/
case '\0':
fr_strerror_printf("No matching closing brace");
return -1; /* error @ second character of format string */
/*
* Hint token was whitespace
*
* e.g. '%{my '
*/
default:
/*
* Box is so we get \t \n etc..
*/
fr_strerror_printf("Invalid char '%pV' in expression", fr_box_strvalue_len(q, 1));
return -(q - in);
}
return (p - in);
}
static ssize_t xlat_tokenize_literal(TALLOC_CTX *ctx, xlat_exp_t **head, char const *in, size_t inlen,
bool brace, vp_tmpl_rules_t const *rules)
{
char const *p, *end;
xlat_exp_t *node;
char *start;
if (!*in) return 0;
XLAT_DEBUG("LITERAL <-- %s", in);
node = talloc_zero(ctx, xlat_exp_t);
node->fmt = start = talloc_bstrndup(node, in, inlen);
node->len = 0;
node->type = XLAT_LITERAL;
p = in;
end = in + inlen;
while (p < end) {
if (*p == '\\') {
if (((p + 1) >= end) || !p[1]) {
talloc_free(node);
fr_strerror_printf("Invalid escape at end of string");
return -(p - in);
}
p += 2;
node->len += 2;
continue;
}
if ((p[0] == '%') && (p + 1) >= end) goto invalid_variable;
/*
* Process the expansion. Which should be at least %{0} in length
*/
if ((p[0] == '%') && (p[1] == '{')) {
ssize_t slen;
XLAT_DEBUG("EXPANSION-2 <-- %s", node->fmt);
slen = xlat_tokenize_expansion(node, &node->next, p, end - p, rules);
if (slen <= 0) {
talloc_free(node);
return slen - (p - in);
}
p += slen;
rad_assert(node->next != NULL);
/*
* Short-circuit the recursive call.
* This saves another function call and
* memory allocation.
*/
if (!*p) break;
/*
* "foo %{User-Name} bar"
* LITERAL "foo "
* EXPANSION User-Name
* LITERAL " bar"
*/
slen = xlat_tokenize_literal(node->next, &(node->next->next), p, end - p, brace, rules);
rad_assert(slen != 0);
if (slen < 0) {
talloc_free(node);
return slen - (p - in);
}
brace = false; /* it was found above, or else the above code errored out */
p += slen;
break; /* stop processing the string */
}
/*
* Check for valid single-character expansions.
*/
if (p[0] == '%') {
ssize_t slen;
xlat_exp_t *next;
if (!p[1] || !strchr("%}cdlmnsetCDGHIMSTYv", p[1])) {
invalid_variable:
talloc_free(node);
fr_strerror_printf("Invalid variable expansion");
p++;
return -(p - in);
}
next = talloc_zero(node, xlat_exp_t);
next->len = 1;
switch (p[1]) {
case '%':
case '}':
next->fmt = talloc_bstrndup(next, p + 1, 1);
XLAT_DEBUG("LITERAL-ESCAPED <-- %s", next->fmt);
next->type = XLAT_LITERAL;
break;
default:
next->fmt = p + 1;
XLAT_DEBUG("PERCENT <-- %c", *next->fmt);
next->type = XLAT_ONE_LETTER;
break;
}
node->next = next;
p += 2;
if (!*p) break;
/*
* And recurse.
*/
slen = xlat_tokenize_literal(node->next, &(node->next->next), p, end - p, brace, rules);
rad_assert(slen != 0);
if (slen < 0) {
talloc_free(node);
return slen - (p - in);
}
brace = false; /* it was found above, or else the above code errored out */
p += slen;
break; /* stop processing the string */
}
/*
* If required, eat the brace.
*/
if (brace && (*p == '}')) {
brace = false;
p++;
break;
}
p++;
node->len++;
}
/*
* We were told to look for a brace, but we ran off of
* the end of the string before we found one.
*/
if (brace) {
fr_strerror_printf("Missing closing brace at end of string");
talloc_free(node);
return -(p - in);
}
/*
* Squash zero-width literals
*/
if (node->len <= 0) {
(void) talloc_steal(ctx, node->next);
*head = node->next;
talloc_free(node);
return p - in;
}
node->async_safe = true; /* literals are always true */
*head = node;
/*
* Shrink the buffer to the right size
*/
MEM(start = talloc_bstr_realloc(ctx, start, node->len));
node->fmt = start;
return p - in;
}
static void xlat_tokenize_debug(REQUEST *request, xlat_exp_t const *node)
{
rad_assert(node != NULL);
RINDENT();
while (node) {
switch (node->type) {
case XLAT_LITERAL:
RDEBUG3("literal --> %s", node->fmt);
break;
case XLAT_ONE_LETTER:
RDEBUG3("percent --> %c", node->fmt[0]);
break;
case XLAT_ATTRIBUTE:
rad_assert(node->attr->tmpl_da != NULL);
RDEBUG3("attribute --> %s", node->attr->tmpl_da->name);
rad_assert(node->child == NULL);
if ((node->attr->tmpl_tag != TAG_ANY) || (node->attr->tmpl_num != NUM_ANY)) {
RDEBUG3("{");
RINDENT();
RDEBUG3("ref %d", node->attr->tmpl_request);
RDEBUG3("list %d", node->attr->tmpl_list);
if (node->attr->tmpl_tag != TAG_ANY) {
RDEBUG3("tag %d", node->attr->tmpl_tag);
}
if (node->attr->tmpl_num != NUM_ANY) {
if (node->attr->tmpl_num == NUM_COUNT) {
RDEBUG3("[#]");
} else if (node->attr->tmpl_num == NUM_ALL) {
RDEBUG3("[*]");
} else {
RDEBUG3("[%d]", node->attr->tmpl_num);
}
}
REXDENT();
RDEBUG3("}");
}
break;
case XLAT_VIRTUAL:
rad_assert(node->fmt != NULL);
RDEBUG3("virtual --> %s", node->fmt);
break;
case XLAT_FUNC:
rad_assert(node->xlat != NULL);
RDEBUG3("xlat --> %s", node->xlat->name);
if (node->child) {
RDEBUG3("{");
xlat_tokenize_debug(request, node->child);
RDEBUG3("}");
}
break;
#ifdef HAVE_REGEX
case XLAT_REGEX:
RDEBUG3("regex-var --> %d", node->regex_index);
break;
#endif
case XLAT_ALTERNATE:
DEBUG("XLAT-IF {");
xlat_tokenize_debug(request, node->child);
DEBUG("}");
DEBUG("XLAT-ELSE {");
xlat_tokenize_debug(request, node->alternate);
DEBUG("}");
break;
}
node = node->next;
}
REXDENT();
}
size_t xlat_snprint(char *out, size_t outlen, xlat_exp_t const *node)
{
char *p, *end;
if (!node || (outlen <= 2)) {
*out = '\0';
return 0;
}
p = out;
end = out + outlen;
#define CHECK_SPACE(_p, _end) if (_p >= _end) goto oob
while (node) {
if (node->type == XLAT_LITERAL) {
p += strlcpy(p, node->fmt, end - p);
CHECK_SPACE(p, end);
goto next;
}
if (node->type == XLAT_ONE_LETTER) {
*(p++) = '%';
if (p >= end) {
oob:
out[outlen - 1] = '\0';
return outlen + 1;
}
*(p++) = node->fmt[0];
CHECK_SPACE(p, end);
goto next;
}
*(p++) = '%';
CHECK_SPACE(p, end);
*(p++) = '{';
CHECK_SPACE(p, end);
switch (node->type) {
case XLAT_ATTRIBUTE:
{
size_t need;
p += tmpl_snprint_attr_str(&need, p, end - p, node->attr);
if (need > 0) goto oob;
}
break;
#ifdef HAVE_REGEX
case XLAT_REGEX:
p += snprintf(p, end - p, "%i", node->regex_index);
CHECK_SPACE(p, end);
break;
#endif
case XLAT_VIRTUAL:
p += strlcpy(p, node->fmt, end - p);
CHECK_SPACE(p, end);
break;
case XLAT_FUNC:
p += strlcpy(p, node->xlat->name, end - p);
CHECK_SPACE(p, end);
*(p++) = ':';
CHECK_SPACE(p, end);
if (node->child) {
p += xlat_snprint(p, end - p, node->child);
CHECK_SPACE(p, end);
}
break;
case XLAT_ALTERNATE:
p += xlat_snprint(p, end - p, node->child);
CHECK_SPACE(p, end);
*(p++) = ':';
CHECK_SPACE(p, end);
*(p++) = '-';
CHECK_SPACE(p, end);
p += xlat_snprint(p, end - p, node->alternate);
CHECK_SPACE(p, end);
break;
default:
fr_assert_fail(NULL);
break;
}
*(p++) = '}';
CHECK_SPACE(p, end);
next:
node = node->next;
}
*p = '\0';
return p - out;
}
/** Tokenize an xlat expansion at runtime
*
* This is used for runtime parsing of xlat expansions, such as those we receive from datastores
* like LDAP or SQL.
*
* @param[in] ctx to allocate dynamic buffers in.
* @param[out] head the head of the xlat list / tree structure.
* @param[in] request the input request. Memory will be attached here.
* @param[in] fmt the format string to expand.
* @param[in] rules controlling how attribute references are parsed.
* @return
* - <= -1 on error. Return value is negative offset of where parsing
* error occured.
* - >= 0 on success. The number of bytes parsed.
*/
ssize_t xlat_tokenize_ephemeral(TALLOC_CTX *ctx, xlat_exp_t **head, REQUEST *request,
char const *fmt, vp_tmpl_rules_t const *rules)
{
ssize_t slen;
*head = NULL;
fr_strerror(); /* Clear error buffer */
slen = xlat_tokenize_literal(request, head, fmt, strlen(fmt), false, rules);
/*
* Zero length expansion, return a zero length node.
*/
if (slen == 0) {
MEM(*head = talloc_zero(ctx, xlat_exp_t));
(*head)->async_safe = true;
}
/*
* Output something like:
*
* "format string"
* " ^ error was here"
*/
if (slen < 0) {
return slen;
}
if (*head && RDEBUG_ENABLED3) {
RDEBUG3("%s", fmt);
RDEBUG3("Parsed xlat tree:");
xlat_tokenize_debug(request, *head);
}
/*
* Create ephemeral instance data for the xlat
*/
if (xlat_instantiate_ephemeral(*head) < 0) {
talloc_free(*head);
fr_strerror_printf("Failed performing ephemeral instantiation for xlat");
return -1;
}
return slen;
}
/** Tokenize an xlat expansion
*
* @param[in] ctx to allocate dynamic buffers in.
* @param[out] head the head of the xlat list / tree structure.
* @param[in] in the format string to expand.
* @param[in] inlen the length of the input string. If the string is \0 terminated, inlen may be -1.
* @param[in] rules controlling how attribute references are parsed.
* @return
* - <0 on error.
* - 0 on success.
*/
ssize_t xlat_tokenize(TALLOC_CTX *ctx, xlat_exp_t **head, char const *in, ssize_t inlen, vp_tmpl_rules_t const *rules)
{
int ret;
*head = NULL;
if (inlen < 0) inlen = strlen(in);