/
xlat.c
1958 lines (1617 loc) · 41.1 KB
/
xlat.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.c
* @brief String expansion ("translation"). Implements %Attribute -> value
*
* @copyright 2000,2006 The FreeRADIUS server project
* @copyright 2000 Alan DeKok <aland@ox.org>
*/
RCSID("$Id$")
#include <freeradius-devel/radiusd.h>
#include <freeradius-devel/parser.h>
#include <freeradius-devel/rad_assert.h>
#include <freeradius-devel/base64.h>
#include <ctype.h>
#include <limits.h>
typedef struct xlat_t {
char name[MAX_STRING_LEN]; //!< Name of the xlat expansion.
int length; //!< Length of name.
void *instance; //!< Module instance passed to xlat and escape functions.
RAD_XLAT_FUNC func; //!< xlat function.
RADIUS_ESCAPE_STRING escape; //!< Escape function to apply to dynamic input to func.
int internal; //!< If true, cannot be redefined.
} xlat_t;
typedef enum {
XLAT_LITERAL, //!< Literal string
XLAT_PERCENT, //!< Literal string with %v
XLAT_MODULE, //!< xlat module
XLAT_VIRTUAL, //!< virtual attribute
XLAT_ATTRIBUTE, //!< xlat attribute
#ifdef HAVE_REGEX_H
XLAT_REGEX, //!< regex reference
#endif
XLAT_ALTERNATE //!< xlat conditional syntax :-
} xlat_state_t;
struct xlat_exp {
char const *fmt; //!< The format string.
size_t len; //!< Length of the format string.
const DICT_ATTR *da; //!< the name of the dictionary attribute
int num; //!< attribute number
int tag; //!< attribute tag
pair_lists_t list; //!< list of which attribute
request_refs_t ref; //!< outer / this / ...
xlat_state_t type; //!< type of this expansion
xlat_exp_t *next; //!< Next in the list.
xlat_exp_t *child; //!< Nested expansion.
xlat_exp_t *alternate; //!< Alternative expansion if this one expanded to a zero length string.
const xlat_t *xlat; //!< The xlat expansion to expand format with.
};
typedef struct xlat_out {
char const *out; //!< Output data.
size_t len; //!< Length of the output string.
} xlat_out_t;
static rbtree_t *xlat_root = NULL;
#ifdef WITH_UNLANG
static char const * const xlat_foreach_names[] = {"Foreach-Variable-0",
"Foreach-Variable-1",
"Foreach-Variable-2",
"Foreach-Variable-3",
"Foreach-Variable-4",
"Foreach-Variable-5",
"Foreach-Variable-6",
"Foreach-Variable-7",
"Foreach-Variable-8",
"Foreach-Variable-9",
NULL};
#endif
#if REQUEST_MAX_REGEX > 8
#error Please fix the following line
#endif
static int xlat_inst[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 }; /* up to 8 for regex */
/** Convert the value on a VALUE_PAIR to string
*
*/
static int valuepair2str(char * out,int outlen,VALUE_PAIR * pair, int type)
{
if (pair != NULL) {
vp_prints_value(out, outlen, pair, -1);
return strlen(out);
}
switch (type) {
case PW_TYPE_STRING :
strlcpy(out,"_",outlen);
break;
case PW_TYPE_INTEGER64:
case PW_TYPE_SIGNED:
case PW_TYPE_INTEGER:
strlcpy(out,"0",outlen);
break;
case PW_TYPE_IPADDR :
strlcpy(out,"?.?.?.?",outlen);
break;
case PW_TYPE_IPV6ADDR :
strlcpy(out,":?:",outlen);
break;
case PW_TYPE_DATE :
strlcpy(out,"0",outlen);
break;
default :
strlcpy(out,"unknown_type",outlen);
}
return strlen(out);
}
/** Print data as integer, not as VALUE.
*
*/
static ssize_t xlat_integer(UNUSED void *instance, REQUEST *request,
char const *fmt, char *out, size_t outlen)
{
VALUE_PAIR *vp;
uint64_t integer;
while (isspace((int) *fmt)) fmt++;
if ((radius_get_vp(request, fmt, &vp) < 0) || !vp) {
*out = '\0';
return 0;
}
switch (vp->da->type) {
case PW_TYPE_OCTETS:
case PW_TYPE_STRING:
if (vp->length > 8) {
break;
}
memcpy(&integer, &(vp->vp_octets), vp->length);
return snprintf(out, outlen, "%" PRIu64, ntohll(integer));
case PW_TYPE_INTEGER64:
return snprintf(out, outlen, "%" PRIu64, vp->vp_integer64);
case PW_TYPE_IPADDR:
case PW_TYPE_INTEGER:
case PW_TYPE_SHORT:
case PW_TYPE_BYTE:
case PW_TYPE_DATE:
return snprintf(out, outlen, "%u", vp->vp_integer);
default:
break;
}
REDEBUG("Type \"%s\" cannot be converted to integer",
fr_int2str(dict_attr_types, vp->da->type, PW_TYPE_INVALID));
*out = '\0';
return -1;
}
/** Print data as hex, not as VALUE.
*
*/
static ssize_t xlat_hex(UNUSED void *instance, REQUEST *request,
char const *fmt, char *out, size_t outlen)
{
size_t i;
VALUE_PAIR *vp;
uint8_t buffer[MAX_STRING_LEN];
ssize_t ret;
size_t len;
while (isspace((int) *fmt)) fmt++;
if ((radius_get_vp(request, fmt, &vp) < 0) || !vp) {
*out = '\0';
return -1;
}
ret = rad_vp2data(vp, buffer, sizeof(buffer));
len = (size_t) ret;
/*
* Don't truncate the data.
*/
if ((ret < 0 ) || (outlen < (len * 2))) {
*out = 0;
return 0;
}
for (i = 0; i < len; i++) {
snprintf(out + 2*i, 3, "%02x", buffer[i]);
}
return len * 2;
}
/** Print data as base64, not as VALUE
*
*/
static ssize_t xlat_base64(UNUSED void *instance, REQUEST *request,
char const *fmt, char *out, size_t outlen)
{
VALUE_PAIR *vp;
uint8_t buffer[MAX_STRING_LEN];
ssize_t ret;
while (isspace((int) *fmt)) fmt++;
if ((radius_get_vp(request, fmt, &vp) < 0) || !vp) {
*out = '\0';
return 0;
}
ret = rad_vp2data(vp, buffer, sizeof(buffer));
if (ret < 0) {
*out = '\0';
return ret;
}
return fr_base64_encode(buffer, (size_t) ret, out, outlen);
}
/** Prints the current module processing the request
*
*/
static ssize_t xlat_module(UNUSED void *instance, REQUEST *request,
UNUSED char const *fmt, char *out, size_t outlen)
{
strlcpy(out, request->module, outlen);
return strlen(out);
}
#ifdef WITH_UNLANG
/** Implements the Foreach-Variable-X
*
* @see modcall()
*/
static ssize_t xlat_foreach(void *instance, REQUEST *request,
UNUSED char const *fmt, char *out, size_t outlen)
{
VALUE_PAIR **pvp;
/*
* See modcall, "FOREACH" for how this works.
*/
pvp = (VALUE_PAIR **) request_data_reference(request, radius_get_vp,
*(int*) instance);
if (!pvp || !*pvp) {
*out = '\0';
return 0;
}
return valuepair2str(out, outlen, (*pvp), (*pvp)->da->type);
}
#endif
/** Print data as string, if possible.
*
* If attribute "Foo" is defined as "octets" it will normally
* be printed as 0x0a0a0a. The xlat "%{string:Foo}" will instead
* expand to "\n\n\n"
*/
static ssize_t xlat_string(UNUSED void *instance, REQUEST *request,
char const *fmt, char *out, size_t outlen)
{
int len;
VALUE_PAIR *vp;
while (isspace((int) *fmt)) fmt++;
if (outlen < 3) {
nothing:
*out = '\0';
return 0;
}
if ((radius_get_vp(request, fmt, &vp) < 0) || !vp) goto nothing;
if (vp->da->type != PW_TYPE_OCTETS) goto nothing;
len = fr_print_string(vp->vp_strvalue, vp->length, out, outlen);
out[len] = '\0';
return len;
}
/** xlat expand string attribute value
*
*/
static ssize_t xlat_xlat(UNUSED void *instance, REQUEST *request,
char const *fmt, char *out, size_t outlen)
{
VALUE_PAIR *vp;
while (isspace((int) *fmt)) fmt++;
if (outlen < 3) {
nothing:
*out = '\0';
return 0;
}
if ((radius_get_vp(request, fmt, &vp) < 0) || !vp) goto nothing;
return radius_xlat(out, outlen, request, vp->vp_strvalue, NULL, NULL);
}
/** Dynamically change the debugging level for the current request
*
* Example %{debug:3}
*/
static ssize_t xlat_debug(UNUSED void *instance, REQUEST *request,
char const *fmt, char *out, size_t outlen)
{
int level = 0;
/*
* Expand to previous (or current) level
*/
snprintf(out, outlen, "%d", request->options & RAD_REQUEST_OPTION_DEBUG4);
/*
* Assume we just want to get the current value and NOT set it to 0
*/
if (!*fmt)
goto done;
level = atoi(fmt);
if (level == 0) {
request->options = RAD_REQUEST_OPTION_NONE;
request->radlog = NULL;
} else {
if (level > 4) level = 4;
request->options = level;
request->radlog = radlog_request;
}
done:
return strlen(out);
}
/*
* Compare two xlat_t structs, based ONLY on the module name.
*/
static int xlat_cmp(void const *one, void const *two)
{
const xlat_t *a = one;
const xlat_t *b = two;
if (a->length != b->length) {
return a->length - b->length;
}
return memcmp(a->name, b->name, a->length);
}
/*
* find the appropriate registered xlat function.
*/
static xlat_t *xlat_find(char const *name)
{
xlat_t my_xlat;
strlcpy(my_xlat.name, name, sizeof(my_xlat.name));
my_xlat.length = strlen(my_xlat.name);
return rbtree_finddata(xlat_root, &my_xlat);
}
/** Register an xlat function.
*
* @param[in] name xlat name.
* @param[in] func xlat function to be called.
* @param[in] escape function to sanitize any sub expansions passed to the xlat function.
* @param[in] instance of module that's registering the xlat function.
* @return 0 on success, -1 on failure
*/
int xlat_register(char const *name, RAD_XLAT_FUNC func, RADIUS_ESCAPE_STRING escape, void *instance)
{
xlat_t *c;
xlat_t my_xlat;
if (!name || !*name) {
DEBUG("xlat_register: Invalid xlat name");
return -1;
}
/*
* First time around, build up the tree...
*
* FIXME: This code should be hoisted out of this function,
* and into a global "initialization". But it isn't critical...
*/
if (!xlat_root) {
int i;
xlat_root = rbtree_create(xlat_cmp, free, 0);
if (!xlat_root) {
DEBUG("xlat_register: Failed to create tree.");
return -1;
}
#ifdef WITH_UNLANG
for (i = 0; xlat_foreach_names[i] != NULL; i++) {
xlat_register(xlat_foreach_names[i],
xlat_foreach, NULL, &xlat_inst[i]);
c = xlat_find(xlat_foreach_names[i]);
rad_assert(c != NULL);
c->internal = true;
}
#endif
#define XLAT_REGISTER(_x) xlat_register(Stringify(_x), xlat_ ## _x, NULL, NULL); \
c = xlat_find(Stringify(_x)); \
rad_assert(c != NULL); \
c->internal = true
XLAT_REGISTER(integer);
XLAT_REGISTER(hex);
XLAT_REGISTER(base64);
XLAT_REGISTER(string);
XLAT_REGISTER(xlat);
XLAT_REGISTER(module);
xlat_register("debug", xlat_debug, NULL, &xlat_inst[0]);
c = xlat_find("debug");
rad_assert(c != NULL);
c->internal = true;
}
/*
* If it already exists, replace the instance.
*/
strlcpy(my_xlat.name, name, sizeof(my_xlat.name));
my_xlat.length = strlen(my_xlat.name);
c = rbtree_finddata(xlat_root, &my_xlat);
if (c) {
if (c->internal) {
DEBUG("xlat_register: Cannot re-define internal xlat");
return -1;
}
c->func = func;
c->escape = escape;
c->instance = instance;
return 0;
}
/*
* Doesn't exist. Create it.
*/
c = rad_malloc(sizeof(*c));
memset(c, 0, sizeof(*c));
c->func = func;
c->escape = escape;
strlcpy(c->name, name, sizeof(c->name));
c->length = strlen(c->name);
c->instance = instance;
rbtree_insert(xlat_root, c);
return 0;
}
/** Unregister an xlat function
*
* We can only have one function to call per name, so the passing of "func"
* here is extraneous.
*
* @param[in] name xlat to unregister.
* @param[in] func unused.
* @param[in] instance data.
*/
void xlat_unregister(char const *name, UNUSED RAD_XLAT_FUNC func, void *instance)
{
xlat_t *c;
xlat_t my_xlat;
if (!name) return;
strlcpy(my_xlat.name, name, sizeof(my_xlat.name));
my_xlat.length = strlen(my_xlat.name);
c = rbtree_finddata(xlat_root, &my_xlat);
if (!c) return;
if (c->instance != instance) return;
rbtree_deletebydata(xlat_root, c);
}
/** De-register all xlat functions, used mainly for debugging.
*
*/
void xlat_free(void)
{
rbtree_free(xlat_root);
}
#if 1
#define XLAT_DEBUG(fmt, ...) printf(fmt, ## __VA_ARGS__);printf("\n")
#endif
#ifndef XLAT_DEBUG
#if 0
#define XLAT_DEBUG DEBUG3
#else
#define XLAT_DEBUG(...)
#endif
#endif
static ssize_t xlat_tokenize_expansion(TALLOC_CTX *ctx, char *fmt, xlat_exp_t **head,
char const **error);
static ssize_t xlat_tokenize_literal(TALLOC_CTX *ctx, char *fmt, xlat_exp_t **head,
int brace, char const **error);
static size_t xlat_process(char **out, REQUEST *request, xlat_exp_t const * const head,
RADIUS_ESCAPE_STRING escape, void *escape_ctx);
static ssize_t xlat_tokenize_alternation(TALLOC_CTX *ctx, char *fmt, xlat_exp_t **head,
char const **error)
{
ssize_t slen;
char *p;
xlat_exp_t *node;
rad_assert(fmt[0] == '%');
rad_assert(fmt[1] == '{');
rad_assert(fmt[2] == '%');
rad_assert(fmt[3] == '{');
XLAT_DEBUG("ALTERNATE: %s", fmt);
node = talloc_zero(ctx, xlat_exp_t);
node->type = XLAT_ALTERNATE;
p = fmt + 2;
slen = xlat_tokenize_expansion(node, p, &node->child, error);
if (slen <= 0) {
talloc_free(node);
return slen - (p - fmt);
}
p += slen;
if (p[0] != ':') {
talloc_free(node);
*error = "Expected ':' after first expansion";
return -(p - fmt);
}
p++;
if (p[0] != '-') {
talloc_free(node);
*error = "Expected '-' after ':'";
return -(p - fmt);
}
p++;
/*
* Allow the RHS to be empty as a special case.
*/
if (*p == '}') {
/*
* Hack up an empty string.
*/
node->alternate = talloc_zero(node, xlat_exp_t);
node->alternate->type = XLAT_LITERAL;
node->alternate->fmt = talloc_strdup(node->alternate, "");
*(p++) = '\0';
} else {
slen = xlat_tokenize_literal(node, p, &node->alternate, true, error);
if (slen <= 0) {
talloc_free(node);
return slen - (p - fmt);
}
if (!node->alternate) {
talloc_free(node);
*error = "Empty expansion is invalid";
return -(p - fmt);
}
p += slen;
}
*head = node;
return p - fmt;
}
static ssize_t xlat_tokenize_expansion(TALLOC_CTX *ctx, char *fmt, xlat_exp_t **head,
char const **error)
{
ssize_t slen;
char *p, *q, *brace;
char const *attrname;
xlat_exp_t *node;
rad_assert(fmt[0] == '%');
rad_assert(fmt[1] == '{');
/*
* %{%{...}:-bar}
*/
if ((fmt[2] == '%') && (fmt[3] == '{')) {
return xlat_tokenize_alternation(ctx, fmt, head, error);
}
XLAT_DEBUG("EXPANSION: %s", fmt);
node = talloc_zero(ctx, xlat_exp_t);
attrname = node->fmt = fmt + 2;
node->len = 0;
#ifdef HAVE_REGEX_H
/*
* Handle regex's specially.
*/
if (isdigit((int) fmt[2]) && (fmt[3] == '}')) {
if (fmt[2] == '9') {
talloc_free(node);
*error = "Invalid regex reference";
return -2;
}
XLAT_DEBUG("REGEX: %s", fmt);
fmt[3] = '\0';
node->num = fmt[2] - '0'; /* ASCII */
node->type = XLAT_REGEX;
*head = node;
return 4;
}
#endif /* HAVE_REGEX_H */
/*
* %{Attr-Name}
* %{Attr-Name[#]}
* %{Tunnel-Password:1}
* %{Tunnel-Password:1[#]}
* %{request:Attr-Name}
* %{request:Tunnel-Password:1}
* %{request:Tunnel-Password:1[#]}
* %{mod:foo}
*/
q = brace = NULL;
for (p = fmt + 2; *p != '\0'; p++) {
if (*p == ':') break;
if (isspace((int) *p)) break;
if (*p == '[') break;
if (*p == '}') break;
}
if (*p != ':') p = NULL;
/*
* Might be a module name reference.
*/
if (p) {
*p = '\0';
/*
* %{mod:foo}
*/
node->xlat = xlat_find(node->fmt);
if (node->xlat) {
node->type = XLAT_MODULE;
XLAT_DEBUG("MOD: %s --> %s", node->fmt, p);
slen = xlat_tokenize_literal(node, p + 1, &node->child, true, error);
if (slen <= 0) {
talloc_free(node);
return slen - (p - fmt);
}
p += slen + 1;
*head = node;
rad_assert(node->next == NULL);
return p - fmt;
}
/*
* Modules can have '}' in their RHS, so we
* didn't check for that until now.
*
* As of now, node->fmt MUST be a reference to an
* attribute, however complicated. So it MUST have a closing brace.
*/
brace = strchr(p + 1, '}');
if (!brace) goto no_brace;
*brace = '\0';
/*
* %{User-Name}
* %{User-Name[1]}
* %{Tunnel-Password:1}
* %{request:Tunnel-Password:1}
*
* <sigh> The syntax is fairly poor.
*/
XLAT_DEBUG("Looking for list in '%s'", attrname);
/*
* Not a module. Has to be an attribute
* reference.
*
* As of v3, we've removed %{request: ..>} as
* internally registered xlats.
*/
*p = ':';
node->ref = radius_request_name(&attrname, REQUEST_CURRENT);
rad_assert(node->ref != REQUEST_UNKNOWN);
node->list = radius_list_name(&attrname, PAIR_LIST_REQUEST);
if (node->list == PAIR_LIST_UNKNOWN) {
talloc_free(node);
*error = "Unknown module";
return -2;
}
/*
* Check for a trailing tag.
*/
p = strchr(attrname, ':');
if (p) *p = '\0';
} else {
brace = strchr(attrname, '}');
if (!brace) {
no_brace:
talloc_free(node);
*error = "No matching closing brace";
return -1; /* second character of format string */
}
*brace = '\0';
node->ref = REQUEST_CURRENT;
node->list = PAIR_LIST_REQUEST;
}
*brace = '\0';
XLAT_DEBUG("Looking for attribute name in %s", attrname);
/*
* Allow for an array reference. They come AFTER the
* tag, if the tag exists. Otherwise, they come after
* the attribute name.
*/
if (p) {
q = strchr(p + 1, '[');
} else {
q = strchr(attrname, '[');
}
if (q) *(q++) = '\0';
if (!*attrname) {
talloc_free(node);
*error = "Empty expression is invalid";
return -(attrname - fmt);
}
/*
* It's either an attribute name, or a Tunnel-Password:TAG
* with the ':' already set to NULL.
*/
node->da = dict_attrbyname(attrname);
if (!node->da) {
/*
* Foreach. Maybe other stuff, too.
*/
node->xlat = xlat_find(attrname);
if (node->xlat) {
node->type = XLAT_VIRTUAL;
node->fmt = attrname;
XLAT_DEBUG("VIRTUAL: %s", node->fmt);
*head = node;
rad_assert(node->next == NULL);
brace++;
return brace - fmt;
}
talloc_free(node);
*error = "Unknown attribute";
return -(attrname - fmt);
}
/*
* Parse the tag.
*/
if (p) {
unsigned long tag;
char *end;
if (!node->da->flags.has_tag) {
talloc_free(node);
*error = "Attribute cannot have a tag";
return - (p - fmt);
}
tag = strtoul(p + 1, &end, 10);
p++;
if (tag == ULONG_MAX) {
talloc_free(node);
*error = "Invalid tag value";
return - (p - fmt);
}
node->tag = tag;
p = end;
if (*p) {
talloc_free(node);
*error = "Unexpected text after tag";
return - (p - fmt);
}
} else {
node->tag = TAG_ANY;
/* leave p alone */
}
/*
* Check for array reference
*/
if (q) {
unsigned long num;
char *end;
p = q;
if (*p== '#') {
node->num = 65536;
p++;
} else if (*p == '*') {
node->num = 65537;
p++;
} else if (isdigit((int) *p)) {
num = strtoul(p, &end, 10);
if ((num == ULONG_MAX) || (num > 65535)) {
talloc_free(node);
*error = "Invalid number";
return - (p - fmt);
}
p = end;
DEBUG("END %s", p);
node->num = num;
} else {
talloc_free(node);
*error = "Invalid array reference";
return - (p - fmt);
}
if (*p != ']') {
talloc_free(node);
*error = "Expected ']'";
return - (p - fmt);
}
p++;
if (*p) {
talloc_free(node);
*error = "Unexpected text after array reference";
return - (p - fmt);
}
}
rad_assert(!p || (p == brace));
node->type = XLAT_ATTRIBUTE;
p = brace + 1;
*head = node;
rad_assert(node->next == NULL);
return p - fmt;
}
static ssize_t xlat_tokenize_literal(TALLOC_CTX *ctx, char *fmt, xlat_exp_t **head,
int brace, char const **error)
{
char *p, *q;
xlat_exp_t *node;
if (!*fmt) return 0;
XLAT_DEBUG("LITERAL: %s", fmt);
node = talloc_zero(ctx, xlat_exp_t);
node->fmt = fmt;
node->len = 0;
node->type = XLAT_LITERAL;
p = fmt;
q = fmt;
while (*p) {
/*
* Convert \n to it's literal representation.
*/
if (p[0] == '\\') switch (p[1]) {
case 't':
*(q++) = '\t';
p += 2;
node->len++;
continue;
case 'n':
*(q++) = '\n';
p += 2;
node->len++;
continue;
case 'x':
p += 2;
if (!p[0] || !p[1]) {
talloc_free(node);
*error = "Hex expansion requires two hex digits";
return -(p - fmt);
}
if (!fr_hex2bin(p, (uint8_t *) q, 2)) {
talloc_free(node);
*error = "Invalid hex characters";
return -(p - fmt);
}
/*
* Don't let people shoot themselves in the foot.
* \x00 is forbidden.
*/
if (!*q) {
talloc_free(node);
*error = "Cannot add zero byte to printable string";
return -(p - fmt);
}
p += 2;
q++;
node->len++;
continue;
default:
*(q++) = *p;
p += 2;
node->len++;
continue;
}
/*
* Process the expansion.
*/
if ((p[0] == '%') && (p[1] == '{')) {
ssize_t slen;
XLAT_DEBUG("LITERAL: %s --> %s", node->fmt, p);
slen = xlat_tokenize_expansion(node, p, &node->next, error);
if (slen <= 0) {
talloc_free(node);
return slen - (p - fmt);
}
*p = '\0'; /* end the literal */
p += slen;
rad_assert(node->next != NULL);