Remove unused attributes
[freeradius.git] / src / main / xlat.c
1 /*
2  *   This program is free software; you can redistribute it and/or modify
3  *   it under the terms of the GNU General Public License as published by
4  *   the Free Software Foundation; either version 2 of the License, or
5  *   (at your option) any later version.
6  *
7  *   This program is distributed in the hope that it will be useful,
8  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *   GNU General Public License for more details.
11  *
12  *   You should have received a copy of the GNU General Public License
13  *   along with this program; if not, write to the Free Software
14  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15  */
16
17 /**
18  * $Id$
19  *
20  * @file xlat.c
21  * @brief String expansion ("translation"). Implements %Attribute -> value
22  *
23  * @copyright 2000,2006  The FreeRADIUS server project
24  * @copyright 2000  Alan DeKok <aland@ox.org>
25  */
26
27 RCSID("$Id$")
28
29 #include <freeradius-devel/radiusd.h>
30 #include <freeradius-devel/parser.h>
31 #include <freeradius-devel/rad_assert.h>
32 #include <freeradius-devel/base64.h>
33
34 #include <ctype.h>
35
36 typedef struct xlat_t {
37         char                    name[MAX_STRING_LEN];   //!< Name of the xlat expansion.
38         int                     length;                 //!< Length of name.
39         void                    *instance;              //!< Module instance passed to xlat and escape functions.
40         RAD_XLAT_FUNC           func;                   //!< xlat function.
41         RADIUS_ESCAPE_STRING    escape;                 //!< Escape function to apply to dynamic input to func.
42         bool                    internal;               //!< If true, cannot be redefined.
43 } xlat_t;
44
45 typedef enum {
46         XLAT_LITERAL,           //!< Literal string
47         XLAT_PERCENT,           //!< Literal string with %v
48         XLAT_MODULE,            //!< xlat module
49         XLAT_VIRTUAL,           //!< virtual attribute
50         XLAT_ATTRIBUTE,         //!< xlat attribute
51 #ifdef HAVE_REGEX
52         XLAT_REGEX,             //!< regex reference
53 #endif
54         XLAT_ALTERNATE          //!< xlat conditional syntax :-
55 } xlat_state_t;
56
57 struct xlat_exp {
58         char const *fmt;        //!< The format string.
59         size_t len;             //!< Length of the format string.
60
61         xlat_state_t type;      //!< type of this expansion.
62         xlat_exp_t *next;       //!< Next in the list.
63
64         xlat_exp_t *child;      //!< Nested expansion.
65         xlat_exp_t *alternate;  //!< Alternative expansion if this one expanded to a zero length string.
66
67         value_pair_tmpl_t attr; //!< An attribute template.
68         xlat_t const *xlat;     //!< The xlat expansion to expand format with.
69 };
70
71 typedef struct xlat_out {
72         char const *out;        //!< Output data.
73         size_t len;             //!< Length of the output string.
74 } xlat_out_t;
75
76 static rbtree_t *xlat_root = NULL;
77
78 #ifdef WITH_UNLANG
79 static char const * const xlat_foreach_names[] = {"Foreach-Variable-0",
80                                                   "Foreach-Variable-1",
81                                                   "Foreach-Variable-2",
82                                                   "Foreach-Variable-3",
83                                                   "Foreach-Variable-4",
84                                                   "Foreach-Variable-5",
85                                                   "Foreach-Variable-6",
86                                                   "Foreach-Variable-7",
87                                                   "Foreach-Variable-8",
88                                                   "Foreach-Variable-9",
89                                                   NULL};
90 #endif
91
92 #if REQUEST_MAX_REGEX > 8
93 #  error Please fix the following line
94 #endif
95 static int xlat_inst[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 }; /* up to 8 for regex */
96
97 char const *radiusd_short_version = RADIUSD_VERSION_STRING;
98
99 /** Print length of its RHS.
100  *
101  */
102 static ssize_t xlat_strlen(UNUSED void *instance, UNUSED REQUEST *request,
103                            char const *fmt, char *out, size_t outlen)
104 {
105         snprintf(out, outlen, "%u", (unsigned int) strlen(fmt));
106         return strlen(out);
107 }
108
109 /** Print the size of the attribute in bytes.
110  *
111  */
112 static ssize_t xlat_length(UNUSED void *instance, UNUSED REQUEST *request,
113                            char const *fmt, char *out, size_t outlen)
114 {
115         VALUE_PAIR *vp;
116         while (isspace((int) *fmt)) fmt++;
117
118         if ((radius_get_vp(&vp, request, fmt) < 0) || !vp) {
119                 *out = '\0';
120                 return 0;
121         }
122
123         snprintf(out, outlen, "%zu", vp->length);
124         return strlen(out);
125 }
126
127 /** Print data as integer, not as VALUE.
128  *
129  */
130 static ssize_t xlat_integer(UNUSED void *instance, REQUEST *request,
131                             char const *fmt, char *out, size_t outlen)
132 {
133         VALUE_PAIR      *vp;
134
135         uint64_t        int64 = 0;      /* Needs to be initialised to zero */
136         uint32_t        int32 = 0;      /* Needs to be initialised to zero */
137
138         while (isspace((int) *fmt)) fmt++;
139
140         if ((radius_get_vp(&vp, request, fmt) < 0) || !vp) {
141                 *out = '\0';
142                 return 0;
143         }
144
145         switch (vp->da->type) {
146         case PW_TYPE_OCTETS:
147         case PW_TYPE_STRING:
148                 if (vp->length > 8) {
149                         break;
150                 }
151
152                 if (vp->length > 4) {
153                         memcpy(&int64, vp->vp_octets, vp->length);
154                         return snprintf(out, outlen, "%" PRIu64, htonll(int64));
155                 }
156
157                 memcpy(&int32, vp->vp_octets, vp->length);
158                 return snprintf(out, outlen, "%i", htonl(int32));
159
160         case PW_TYPE_INTEGER64:
161                 return snprintf(out, outlen, "%" PRIu64, vp->vp_integer64);
162
163         /*
164          *      IP addresses are treated specially, as parsing functions assume the value
165          *      is bigendian and will convert it for us.
166          */
167         case PW_TYPE_IPV4_ADDR:
168                 return snprintf(out, outlen, "%u", htonl(vp->vp_ipaddr));
169
170         case PW_TYPE_IPV4_PREFIX:
171                 return snprintf(out, outlen, "%u", htonl((*(uint32_t *)(vp->vp_ipv4prefix + 2))));
172
173         case PW_TYPE_INTEGER:
174         case PW_TYPE_DATE:
175                 return snprintf(out, outlen, "%u", vp->vp_integer);
176         case PW_TYPE_BYTE:
177                 return snprintf(out, outlen, "%u", (unsigned int) vp->vp_byte);
178         case PW_TYPE_SHORT:
179                 return snprintf(out, outlen, "%u", (unsigned int) vp->vp_short);
180
181         /*
182          *      Ethernet is weird... It's network related, so we assume to it should be
183          *      bigendian.
184          */
185         case PW_TYPE_ETHERNET:
186                 memcpy(&int64, &vp->vp_ether, vp->length);
187                 return snprintf(out, outlen, "%" PRIu64, htonll(int64));
188
189         case PW_TYPE_SIGNED:
190                 return snprintf(out, outlen, "%i", vp->vp_signed);
191
192         case PW_TYPE_IPV6_ADDR:
193                 return fr_prints_uint128(out, outlen, ntohlll(*(uint128_t const *) &vp->vp_ipv6addr));
194
195         case PW_TYPE_IPV6_PREFIX:
196                 return fr_prints_uint128(out, outlen, ntohlll(*(uint128_t const *) &(vp->vp_ipv6prefix[2])));
197
198         default:
199                 break;
200         }
201
202         REDEBUG("Type '%s' of length %zu cannot be converted to integer",
203                 fr_int2str(dict_attr_types, vp->da->type, "???"), vp->length);
204         *out = '\0';
205
206         return -1;
207 }
208
209 /** Print data as hex, not as VALUE.
210  *
211  */
212 static ssize_t xlat_hex(UNUSED void *instance, REQUEST *request,
213                         char const *fmt, char *out, size_t outlen)
214 {
215         size_t i;
216         VALUE_PAIR *vp;
217         uint8_t const *p;
218         ssize_t ret;
219         size_t  len;
220         value_data_t dst;
221         uint8_t const *buff = NULL;
222
223         while (isspace((int) *fmt)) fmt++;
224
225         if ((radius_get_vp(&vp, request, fmt) < 0) || !vp) {
226         error:
227                 *out = '\0';
228                 return -1;
229         }
230
231         /*
232          *      The easy case.
233          */
234         if (vp->da->type == PW_TYPE_OCTETS) {
235                 p = vp->vp_octets;
236                 len = vp->length;
237         /*
238          *      Cast the value_data_t of the VP to an octets string and
239          *      print that as hex.
240          */
241         } else {
242                 ret = value_data_cast(request, &dst, PW_TYPE_OCTETS, NULL, vp->da->type,
243                                       NULL, &vp->data, vp->length);
244                 if (ret < 0) {
245                         REDEBUG("%s", fr_strerror());
246                         goto error;
247                 }
248                 len = (size_t) ret;
249                 p = buff = dst.octets;
250         }
251
252         rad_assert(p);
253
254         /*
255          *      Don't truncate the data.
256          */
257         if (outlen < (len * 2)) {
258                 rad_const_free(buff);
259                 goto error;
260         }
261
262         for (i = 0; i < len; i++) {
263                 snprintf(out + 2*i, 3, "%02x", p[i]);
264         }
265         rad_const_free(buff);
266
267         return len * 2;
268 }
269
270 /** Return the tag of an attribute reference
271  *
272  */
273 static ssize_t xlat_tag(UNUSED void *instance, REQUEST *request,
274                         char const *fmt, char *out, size_t outlen)
275 {
276         VALUE_PAIR *vp;
277
278         while (isspace((int) *fmt)) fmt++;
279
280         if ((radius_get_vp(&vp, request, fmt) < 0) || !vp) {
281                 *out = '\0';
282                 return 0;
283         }
284
285         if (!vp->da->flags.has_tag || !TAG_VALID(vp->tag)) {
286                 *out = '\0';
287                 return 0;
288         }
289
290         return snprintf(out, outlen, "%u", vp->tag);
291 }
292
293 /** Print out attribute info
294  *
295  * Prints out all instances of a current attribute, or all attributes in a list.
296  *
297  * At higher debugging levels, also prints out alternative decodings of the same
298  * value. This is helpful to determine types for unknown attributes of long
299  * passed vendors, or just crazy/broken NAS.
300  *
301  * It's also useful for exposing issues in the packet decoding functions, as in
302  * some cases they get fed random garbage data.
303  *
304  * This expands to a zero length string.
305  */
306 static ssize_t xlat_debug_attr(UNUSED void *instance, REQUEST *request, char const *fmt,
307                                char *out, UNUSED size_t outlen)
308 {
309         VALUE_PAIR *vp;
310         vp_cursor_t cursor;
311
312         value_pair_tmpl_t vpt;
313
314         if (!RDEBUG_ENABLED2) {
315                 *out = '\0';
316                 return -1;
317         }
318
319         while (isspace((int) *fmt)) fmt++;
320
321         if (tmpl_from_attr_str(&vpt, fmt, REQUEST_CURRENT, PAIR_LIST_REQUEST, false) <= 0) {
322                 RDEBUG("%s", fr_strerror());
323                 return -1;
324         }
325
326         RIDEBUG("Attributes matching \"%s\"", fmt);
327
328         RINDENT();
329         for (vp = tmpl_cursor_init(NULL, &cursor, request, &vpt);
330              vp;
331              vp = tmpl_cursor_next(&cursor, &vpt)) {
332                 FR_NAME_NUMBER const *type;
333                 char *value;
334
335                 value = vp_aprints_value(vp, vp, '\'');
336                 if (vp->da->flags.has_tag) {
337                         RIDEBUG2("&%s:%s:%i %s %s",
338                                 fr_int2str(pair_lists, vpt.tmpl_list, "<INVALID>"),
339                                 vp->da->name,
340                                 vp->tag,
341                                 fr_int2str(fr_tokens, vp->op, "<INVALID>"),
342                                 value);
343                 } else {
344                         RIDEBUG2("&%s:%s %s %s",
345                                 fr_int2str(pair_lists, vpt.tmpl_list, "<INVALID>"),
346                                 vp->da->name,
347                                 fr_int2str(fr_tokens, vp->op, "<INVALID>"),
348                                 value);
349                 }
350                 talloc_free(value);
351
352                 if (!RDEBUG_ENABLED3) continue;
353
354                 if (vp->da->vendor) {
355                         DICT_VENDOR *dv;
356
357                         dv = dict_vendorbyvalue(vp->da->vendor);
358                         RIDEBUG2("Vendor : %i (%s)", vp->da->vendor, dv ? dv->name : "unknown");
359                 }
360                 RIDEBUG2("Type   : %s", fr_int2str(dict_attr_types, vp->da->type, "<INVALID>"));
361                 RIDEBUG2("Length : %zu", vp->length);
362
363                 if (!RDEBUG_ENABLED4) continue;
364
365                 type = dict_attr_types;
366                 while (type->name) {
367                         int pad;
368
369                         value_data_t *dst = NULL;
370
371                         ssize_t ret;
372
373                         if ((PW_TYPE) type->number == vp->da->type) {
374                                 goto next_type;
375                         }
376
377                         switch (type->number) {
378                         case PW_TYPE_INVALID:           /* Not real type */
379                         case PW_TYPE_MAX:               /* Not real type */
380                         case PW_TYPE_EXTENDED:          /* Not safe/appropriate */
381                         case PW_TYPE_LONG_EXTENDED:     /* Not safe/appropriate */
382                         case PW_TYPE_TLV:               /* Not safe/appropriate */
383                         case PW_TYPE_EVS:               /* Not safe/appropriate */
384                         case PW_TYPE_VSA:               /* @fixme We need special behaviour for these */
385                         case PW_TYPE_COMBO_IP_ADDR:     /* Covered by IPv4 address IPv6 address */
386                         case PW_TYPE_COMBO_IP_PREFIX:   /* Covered by IPv4 address IPv6 address */
387                         case PW_TYPE_TIMEVAL:           /* Not a VALUE_PAIR type */
388
389                                 goto next_type;
390
391                         default:
392                                 break;
393                         }
394
395                         dst = talloc_zero(vp, value_data_t);
396                         ret = value_data_cast(dst, dst, type->number, NULL, vp->da->type, vp->da,
397                                               &vp->data, vp->length);
398                         if (ret < 0) goto next_type;    /* We expect some to fail */
399
400                         value = vp_data_aprints_value(dst, type->number, NULL, dst, (size_t)ret, '\'');
401                         if (!value) goto next_type;
402
403                         if ((pad = (11 - strlen(type->name))) < 0) {
404                                 pad = 0;
405                         }
406
407                         RINDENT();
408                         RDEBUG2("as %s%*s: %s", type->name, pad, " ", value);
409                         REXDENT();
410
411                 next_type:
412                         talloc_free(dst);
413                         type++;
414                 }
415         }
416
417         *out = '\0';
418         return 0;
419 }
420
421 /** Prints the current module processing the request
422  *
423  */
424 static ssize_t xlat_module(UNUSED void *instance, REQUEST *request,
425                            UNUSED char const *fmt, char *out, size_t outlen)
426 {
427         strlcpy(out, request->module, outlen);
428
429         return strlen(out);
430 }
431
432 #ifdef WITH_UNLANG
433 /** Implements the Foreach-Variable-X
434  *
435  * @see modcall()
436  */
437 static ssize_t xlat_foreach(void *instance, REQUEST *request,
438                             UNUSED char const *fmt, char *out, size_t outlen)
439 {
440         VALUE_PAIR      **pvp;
441         size_t          len;
442
443         /*
444          *      See modcall, "FOREACH" for how this works.
445          */
446         pvp = (VALUE_PAIR **) request_data_reference(request, radius_get_vp, *(int*) instance);
447         if (!pvp || !*pvp) {
448                 *out = '\0';
449                 return 0;
450         }
451
452         len = vp_prints_value(out, outlen, *pvp, 0);
453         if (is_truncated(len, outlen)) {
454                 RDEBUG("Insufficient buffer space to write foreach value");
455                 return -1;
456         }
457
458         return len;
459 }
460 #endif
461
462 /** Print data as string, if possible.
463  *
464  * If attribute "Foo" is defined as "octets" it will normally
465  * be printed as 0x0a0a0a. The xlat "%{string:Foo}" will instead
466  * expand to "\n\n\n"
467  */
468 static ssize_t xlat_string(UNUSED void *instance, REQUEST *request,
469                            char const *fmt, char *out, size_t outlen)
470 {
471         size_t len;
472         ssize_t ret;
473         VALUE_PAIR *vp;
474         uint8_t const *p;
475
476         while (isspace((int) *fmt)) fmt++;
477
478         if (outlen < 3) {
479         nothing:
480                 *out = '\0';
481                 return 0;
482         }
483
484         if ((radius_get_vp(&vp, request, fmt) < 0) || !vp) goto nothing;
485
486         ret = rad_vp2data(&p, vp);
487         if (ret < 0) {
488                 return ret;
489         }
490
491         switch (vp->da->type) {
492         case PW_TYPE_OCTETS:
493                 len = fr_print_string((char const *) p, vp->length, out, outlen, '\0');
494                 break;
495
496         case PW_TYPE_STRING:
497                 len = strlcpy(out, vp->vp_strvalue, outlen);
498                 break;
499
500         default:
501                 len = fr_print_string((char const *) p, ret, out, outlen, '\0');
502                 break;
503         }
504
505         return len;
506 }
507
508 /** xlat expand string attribute value
509  *
510  */
511 static ssize_t xlat_xlat(UNUSED void *instance, REQUEST *request,
512                         char const *fmt, char *out, size_t outlen)
513 {
514         VALUE_PAIR *vp;
515
516         while (isspace((int) *fmt)) fmt++;
517
518         if (outlen < 3) {
519         nothing:
520                 *out = '\0';
521                 return 0;
522         }
523
524         if ((radius_get_vp(&vp, request, fmt) < 0) || !vp) goto nothing;
525
526         return radius_xlat(out, outlen, request, vp->vp_strvalue, NULL, NULL);
527 }
528
529 /** Dynamically change the debugging level for the current request
530  *
531  * Example %{debug:3}
532  */
533 static ssize_t xlat_debug(UNUSED void *instance, REQUEST *request,
534                           char const *fmt, char *out, size_t outlen)
535 {
536         int level = 0;
537
538         /*
539          *  Expand to previous (or current) level
540          */
541         snprintf(out, outlen, "%d", request->log.lvl & RAD_REQUEST_OPTION_DEBUG4);
542
543         /*
544          *  Assume we just want to get the current value and NOT set it to 0
545          */
546         if (!*fmt)
547                 goto done;
548
549         level = atoi(fmt);
550         if (level == 0) {
551                 request->log.lvl = RAD_REQUEST_OPTION_NONE;
552                 request->log.func = NULL;
553         } else {
554                 if (level > 4) level = 4;
555
556                 request->log.lvl = level;
557                 request->log.func = vradlog_request;
558         }
559
560         done:
561         return strlen(out);
562 }
563
564 /*
565  *      Compare two xlat_t structs, based ONLY on the module name.
566  */
567 static int xlat_cmp(void const *one, void const *two)
568 {
569         xlat_t const *a = one;
570         xlat_t const *b = two;
571
572         if (a->length != b->length) {
573                 return a->length - b->length;
574         }
575
576         return memcmp(a->name, b->name, a->length);
577 }
578
579
580 /*
581  *      find the appropriate registered xlat function.
582  */
583 static xlat_t *xlat_find(char const *name)
584 {
585         xlat_t my_xlat;
586
587         strlcpy(my_xlat.name, name, sizeof(my_xlat.name));
588         my_xlat.length = strlen(my_xlat.name);
589
590         return rbtree_finddata(xlat_root, &my_xlat);
591 }
592
593
594 /** Register an xlat function.
595  *
596  * @param[in] name xlat name.
597  * @param[in] func xlat function to be called.
598  * @param[in] escape function to sanitize any sub expansions passed to the xlat function.
599  * @param[in] instance of module that's registering the xlat function.
600  * @return 0 on success, -1 on failure
601  */
602 int xlat_register(char const *name, RAD_XLAT_FUNC func, RADIUS_ESCAPE_STRING escape, void *instance)
603 {
604         xlat_t  *c;
605         xlat_t  my_xlat;
606         rbnode_t *node;
607
608         if (!name || !*name) {
609                 DEBUG("xlat_register: Invalid xlat name");
610                 return -1;
611         }
612
613         /*
614          *      First time around, build up the tree...
615          *
616          *      FIXME: This code should be hoisted out of this function,
617          *      and into a global "initialization".  But it isn't critical...
618          */
619         if (!xlat_root) {
620 #ifdef WITH_UNLANG
621                 int i;
622 #endif
623
624                 xlat_root = rbtree_create(NULL, xlat_cmp, NULL, RBTREE_FLAG_REPLACE);
625                 if (!xlat_root) {
626                         DEBUG("xlat_register: Failed to create tree");
627                         return -1;
628                 }
629
630 #ifdef WITH_UNLANG
631                 for (i = 0; xlat_foreach_names[i] != NULL; i++) {
632                         xlat_register(xlat_foreach_names[i],
633                                       xlat_foreach, NULL, &xlat_inst[i]);
634                         c = xlat_find(xlat_foreach_names[i]);
635                         rad_assert(c != NULL);
636                         c->internal = true;
637                 }
638 #endif
639
640 #define XLAT_REGISTER(_x) xlat_register(STRINGIFY(_x), xlat_ ## _x, NULL, NULL); \
641                 c = xlat_find(STRINGIFY(_x)); \
642                 rad_assert(c != NULL); \
643                 c->internal = true
644
645                 XLAT_REGISTER(integer);
646                 XLAT_REGISTER(strlen);
647                 XLAT_REGISTER(length);
648                 XLAT_REGISTER(hex);
649                 XLAT_REGISTER(tag);
650                 XLAT_REGISTER(string);
651                 XLAT_REGISTER(xlat);
652                 XLAT_REGISTER(module);
653                 XLAT_REGISTER(debug_attr);
654
655                 xlat_register("debug", xlat_debug, NULL, &xlat_inst[0]);
656                 c = xlat_find("debug");
657                 rad_assert(c != NULL);
658                 c->internal = true;
659         }
660
661         /*
662          *      If it already exists, replace the instance.
663          */
664         strlcpy(my_xlat.name, name, sizeof(my_xlat.name));
665         my_xlat.length = strlen(my_xlat.name);
666         c = rbtree_finddata(xlat_root, &my_xlat);
667         if (c) {
668                 if (c->internal) {
669                         DEBUG("xlat_register: Cannot re-define internal xlat");
670                         return -1;
671                 }
672
673                 c->func = func;
674                 c->escape = escape;
675                 c->instance = instance;
676                 return 0;
677         }
678
679         /*
680          *      Doesn't exist.  Create it.
681          */
682         c = talloc_zero(xlat_root, xlat_t);
683
684         c->func = func;
685         c->escape = escape;
686         strlcpy(c->name, name, sizeof(c->name));
687         c->length = strlen(c->name);
688         c->instance = instance;
689
690         node = rbtree_insert_node(xlat_root, c);
691         if (!node) {
692                 talloc_free(c);
693                 return -1;
694         }
695
696         /*
697          *      Ensure that the data is deleted when the node is
698          *      deleted.
699          *
700          *      @todo: Maybe this should be the other way around...
701          *      when a thing IN the tree is deleted, it's automatically
702          *      removed from the tree.  But for now, this works.
703          */
704         (void) talloc_steal(node, c);
705         return 0;
706 }
707
708 /** Unregister an xlat function
709  *
710  * We can only have one function to call per name, so the passing of "func"
711  * here is extraneous.
712  *
713  * @param[in] name xlat to unregister.
714  * @param[in] func unused.
715  * @param[in] instance data.
716  */
717 void xlat_unregister(char const *name, UNUSED RAD_XLAT_FUNC func, void *instance)
718 {
719         xlat_t  *c;
720         xlat_t          my_xlat;
721
722         if (!name) return;
723
724         strlcpy(my_xlat.name, name, sizeof(my_xlat.name));
725         my_xlat.length = strlen(my_xlat.name);
726
727         c = rbtree_finddata(xlat_root, &my_xlat);
728         if (!c) return;
729
730         if (c->instance != instance) return;
731
732         rbtree_deletebydata(xlat_root, c);
733 }
734
735 static int xlat_unregister_callback(void *instance, void *data)
736 {
737         xlat_t *c = (xlat_t *) data;
738
739         if (c->instance != instance) return 0; /* keep walking */
740
741         return 2;               /* delete it */
742 }
743
744 void xlat_unregister_module(void *instance)
745 {
746         rbtree_walk(xlat_root, RBTREE_DELETE_ORDER, xlat_unregister_callback, instance);
747 }
748
749
750 /** Crappy temporary function to add attribute ref support to xlats
751  *
752  * This needs to die, and hopefully will die, when xlat functions accept
753  * xlat node structures.
754  *
755  * Provides either a pointer to a buffer which contains the value of the reference VALUE_PAIR
756  * in an architecture independent format. Or a pointer to the start of the fmt string.
757  *
758  * The pointer is only guaranteed to be valid between calls to xlat_fmt_to_ref,
759  * and so long as the source VALUE_PAIR is not freed.
760  *
761  * @param out where to write a pointer to the buffer to the data the xlat function needs to work on.
762  * @param request current request.
763  * @param fmt string.
764  * @returns the length of the data or -1 on error.
765  */
766 ssize_t xlat_fmt_to_ref(uint8_t const **out, REQUEST *request, char const *fmt)
767 {
768         VALUE_PAIR *vp;
769
770         while (isspace((int) *fmt)) fmt++;
771
772         if (fmt[0] == '&') {
773                 if ((radius_get_vp(&vp, request, fmt) < 0) || !vp) {
774                         *out = NULL;
775                         return -1;
776                 }
777
778                 return rad_vp2data(out, vp);
779         }
780
781         *out = (uint8_t const *)fmt;
782         return strlen(fmt);
783 }
784
785 /** De-register all xlat functions, used mainly for debugging.
786  *
787  */
788 void xlat_free(void)
789 {
790         rbtree_free(xlat_root);
791 }
792
793
794 #ifdef DEBUG_XLAT
795 #  define XLAT_DEBUG DEBUG3
796 #else
797 #  define XLAT_DEBUG(...)
798 #endif
799
800 static ssize_t xlat_tokenize_expansion(TALLOC_CTX *ctx, char *fmt, xlat_exp_t **head,
801                                        char const **error);
802 static ssize_t xlat_tokenize_literal(TALLOC_CTX *ctx, char *fmt, xlat_exp_t **head,
803                                      bool brace, char const **error);
804 static size_t xlat_process(char **out, REQUEST *request, xlat_exp_t const * const head,
805                            RADIUS_ESCAPE_STRING escape, void *escape_ctx);
806
807 static ssize_t xlat_tokenize_alternation(TALLOC_CTX *ctx, char *fmt, xlat_exp_t **head,
808                                          char const **error)
809 {
810         ssize_t slen;
811         char *p;
812         xlat_exp_t *node;
813
814         rad_assert(fmt[0] == '%');
815         rad_assert(fmt[1] == '{');
816         rad_assert(fmt[2] == '%');
817         rad_assert(fmt[3] == '{');
818
819         XLAT_DEBUG("ALTERNATE <-- %s", fmt);
820
821         node = talloc_zero(ctx, xlat_exp_t);
822         node->type = XLAT_ALTERNATE;
823
824         p = fmt + 2;
825         slen = xlat_tokenize_expansion(node, p, &node->child, error);
826         if (slen <= 0) {
827                 talloc_free(node);
828                 return slen - (p - fmt);
829         }
830         p += slen;
831
832         if (p[0] != ':') {
833                 talloc_free(node);
834                 *error = "Expected ':' after first expansion";
835                 return -(p - fmt);
836         }
837         p++;
838
839         if (p[0] != '-') {
840                 talloc_free(node);
841                 *error = "Expected '-' after ':'";
842                 return -(p - fmt);
843         }
844         p++;
845
846         /*
847          *      Allow the RHS to be empty as a special case.
848          */
849         if (*p == '}') {
850                 /*
851                  *      Hack up an empty string.
852                  */
853                 node->alternate = talloc_zero(node, xlat_exp_t);
854                 node->alternate->type = XLAT_LITERAL;
855                 node->alternate->fmt = talloc_typed_strdup(node->alternate, "");
856                 *(p++) = '\0';
857
858         } else {
859                 slen = xlat_tokenize_literal(node, p,  &node->alternate, true, error);
860                 if (slen <= 0) {
861                         talloc_free(node);
862                         return slen - (p - fmt);
863                 }
864
865                 if (!node->alternate) {
866                         talloc_free(node);
867                         *error = "Empty expansion is invalid";
868                         return -(p - fmt);
869                 }
870                 p += slen;
871         }
872
873         *head = node;
874         return p - fmt;
875 }
876
877 static ssize_t xlat_tokenize_expansion(TALLOC_CTX *ctx, char *fmt, xlat_exp_t **head,
878                                        char const **error)
879 {
880         ssize_t slen;
881         char *p, *q;
882         xlat_exp_t *node;
883
884         rad_assert(fmt[0] == '%');
885         rad_assert(fmt[1] == '{');
886
887         /*
888          *      %{%{...}:-bar}
889          */
890         if ((fmt[2] == '%') && (fmt[3] == '{')) return xlat_tokenize_alternation(ctx, fmt, head, error);
891
892         XLAT_DEBUG("EXPANSION <-- %s", fmt);
893         node = talloc_zero(ctx, xlat_exp_t);
894         node->fmt = fmt + 2;
895         node->len = 0;
896
897 #ifdef HAVE_REGEX
898         /*
899          *      Handle regex's specially.
900          */
901         if (isdigit((int) fmt[2]) && (fmt[3] == '}')) {
902                 if (fmt[2] == '9') {
903                         talloc_free(node);
904                         *error = "Invalid regex reference";
905                         return -2;
906                 }
907
908                 XLAT_DEBUG("REGEX <-- %s", fmt);
909                 fmt[3] = '\0';
910                 node->attr.tmpl_num = fmt[2] - '0'; /* ASCII */
911
912                 node->type = XLAT_REGEX;
913                 *head = node;
914                 return 4;
915         }
916 #endif /* HAVE_REGEX */
917
918         /*
919          *      %{Attr-Name}
920          *      %{Attr-Name[#]}
921          *      %{Tunnel-Password:1}
922          *      %{Tunnel-Password:1[#]}
923          *      %{request:Attr-Name}
924          *      %{request:Tunnel-Password:1}
925          *      %{request:Tunnel-Password:1[#]}
926          *      %{mod:foo}
927          */
928         brace = NULL;
929
930         /*
931          *      This is for efficiency, so we don't search for an xlat,
932          *      when what's being referenced is obviously an attribute.
933          */
934         p = fmt + 2;
935         for (q = p; *q != '\0'; q++) {
936                 if (*q == ':') break;
937
938                 if (isspace((int) *q)) break;
939
940                 if (*q == '[') {
941                         is_attr = true;
942                         continue;
943                 }
944
945                 if (*q == '}') break;
946         }
947
948         /*
949          *      Check for empty expressions %{}
950          */
951         if ((*q == '}') && (q == p)) {
952                 *error = "Empty expression is invalid";
953                 return -(q - fmt);
954         }
955
956         /*
957          *      Might be a module name reference.
958          *
959          *      If it's not, it's an attribute or parse error.
960          */
961         if (*q == ':') {
962                 *q = '\0';
963                 node->xlat = xlat_find(node->fmt);
964                 if (node->xlat) {
965                         /*
966                          *      %{mod:foo}
967                          */
968                         node->type = XLAT_MODULE;
969
970                         p = q + 1;
971                         XLAT_DEBUG("MOD <-- %s ... %s", node->fmt, p);
972
973                         slen = xlat_tokenize_literal(node, p, &node->child, true, error);
974                         if (slen <= 0) {
975                                 talloc_free(node);
976                                 return slen - (p - fmt);
977                         }
978                         p += slen;
979
980                         *head = node;
981                         rad_assert(node->next == NULL);
982
983                         return p - fmt;
984                 }
985                 *q = ':';       /* Avoids a strdup */
986         }
987
988         /*
989          *      The first token ends with:
990          *      - '[' - Which is an attribute index, so it must be an attribute.
991          *      - '}' - The end of the expansion, which means it was a bareword.
992          */
993         slen = tmpl_from_attr_substr(&node->attr, p, REQUEST_CURRENT, PAIR_LIST_REQUEST, true);
994         if (slen <= 0) {
995                 /*
996                  *      If the parse error occurred before the ':'
997                  *      then the error is changed to 'Unknown module',
998                  *      as it was more likely to be a bad module name,
999                  *      than a request qualifier.
1000                  */
1001                 if ((*q == ':') && ((p + (slen * -1)) < q)) {
1002                         *error = "Unknown module";
1003                 } else {
1004                         *error = fr_strerror();
1005                 }
1006                 return slen - (p - fmt);
1007         }
1008
1009         /*
1010          *      Might be a virtual XLAT attribute
1011          */
1012         if (node->attr.type == TMPL_TYPE_ATTR_UNKNOWN) {
1013                 node->xlat = xlat_find(node->attr.tmpl_unknown_name);
1014                 if (node->xlat) {
1015                         node->type = XLAT_VIRTUAL;
1016                         node->fmt = node->attr.tmpl_unknown_name;
1017
1018                         XLAT_DEBUG("VIRTUAL <-- %s", node->fmt);
1019                         *head = node;
1020                         rad_assert(node->next == NULL);
1021                         q++;
1022                         return q - fmt;
1023                 }
1024
1025                 talloc_free(node);
1026                 *error = "Unknown attribute";
1027                 return -(node->fmt - fmt);
1028         }
1029
1030         node->type = XLAT_ATTRIBUTE;
1031         p += slen;
1032         if (*p != '}') {
1033                 talloc_free(node);
1034                 *error = "No matching closing brace";
1035                 return -1;      /* second character of format string */
1036         }
1037         p++;
1038         *head = node;
1039         rad_assert(node->next == NULL);
1040
1041         return p - fmt;
1042 }
1043
1044
1045 static ssize_t xlat_tokenize_literal(TALLOC_CTX *ctx, char *fmt, xlat_exp_t **head,
1046                                      bool brace, char const **error)
1047 {
1048         char *p;
1049         xlat_exp_t *node;
1050
1051         if (!*fmt) return 0;
1052
1053         XLAT_DEBUG("LITERAL <-- %s", fmt);
1054
1055         node = talloc_zero(ctx, xlat_exp_t);
1056         node->fmt = fmt;
1057         node->len = 0;
1058         node->type = XLAT_LITERAL;
1059
1060         p = fmt;
1061
1062         while (*p) {
1063                 if (*p == '\\') {
1064                         if (!p[1]) {
1065                                 talloc_free(node);
1066                                 *error = "Invalid escape at end of string";
1067                                 return -(p - fmt);
1068                         }
1069                         p += 2;
1070                         continue;
1071                 }
1072
1073                 /*
1074                  *      Process the expansion.
1075                  */
1076                 if ((p[0] == '%') && (p[1] == '{')) {
1077                         ssize_t slen;
1078
1079                         XLAT_DEBUG("LITERAL <-- %s", node->fmt);
1080
1081                         slen = xlat_tokenize_expansion(node, p, &node->next, error);
1082                         if (slen <= 0) {
1083                                 talloc_free(node);
1084                                 return slen - (p - fmt);
1085                         }
1086                         *p = '\0'; /* end the literal */
1087                         p += slen;
1088
1089                         rad_assert(node->next != NULL);
1090
1091                         /*
1092                          *      Short-circuit the recursive call.
1093                          *      This saves another function call and
1094                          *      memory allocation.
1095                          */
1096                         if (!*p) break;
1097
1098                         /*
1099                          *      "foo %{User-Name} bar"
1100                          *      LITERAL         "foo "
1101                          *      EXPANSION       User-Name
1102                          *      LITERAL         " bar"
1103                          */
1104                         slen = xlat_tokenize_literal(node->next, p, &(node->next->next), brace, error);
1105                         rad_assert(slen != 0);
1106                         if (slen < 0) {
1107                                 talloc_free(node);
1108                                 return slen - (p - fmt);
1109                         }
1110
1111                         p += slen;
1112                         break;  /* stop processing the string */
1113                 }
1114
1115                 /*
1116                  *      Check for valid single-character expansions.
1117                  */
1118                 if (p[0] == '%') {
1119                         ssize_t slen;
1120                         xlat_exp_t *next;
1121
1122                         if (!p[1] || !strchr("%dlmtDGHISTYv", p[1])) {
1123                                         talloc_free(node);
1124                                         *error = "Invalid variable expansion";
1125                                         p++;
1126                                         return - (p - fmt);
1127                         }
1128
1129                         next = talloc_zero(node, xlat_exp_t);
1130                         next->len = 1;
1131
1132                         if (p[1] == '%') {
1133                                 next->fmt = talloc_typed_strdup(next, "%");
1134
1135                                 XLAT_DEBUG("LITERAL <-- %s", next->fmt);
1136                                 next->type = XLAT_LITERAL;
1137
1138                         } else {
1139                                 next->fmt = p + 1;
1140
1141                                 XLAT_DEBUG("PERCENT <-- %c", *next->fmt);
1142                                 next->type = XLAT_PERCENT;
1143                         }
1144
1145                         node->next = next;
1146                         *p = '\0';
1147                         p += 2;
1148
1149                         if (!*p) break;
1150
1151                         /*
1152                          *      And recurse.
1153                          */
1154                         slen = xlat_tokenize_literal(node->next, p, &(node->next->next), brace, error);
1155                         rad_assert(slen != 0);
1156                         if (slen < 0) {
1157                                 talloc_free(node);
1158                                 return slen - (p - fmt);
1159                         }
1160
1161                         p += slen;
1162                         break;  /* stop processing the string */
1163                 }
1164
1165                 /*
1166                  *      If required, eat the brace.
1167                  */
1168                 if (brace && (*p == '}')) {
1169                         *p = '\0';
1170                         p++;
1171                         break;
1172                 }
1173
1174                 p++;
1175                 node->len++;
1176         }
1177
1178         /*
1179          *      Squash zero-width literals
1180          */
1181         if (node->len > 0) {
1182                 *head = node;
1183
1184         } else {
1185                 (void) talloc_steal(ctx, node->next);
1186                 *head = node->next;
1187                 talloc_free(node);
1188         }
1189
1190         return p - fmt;
1191 }
1192
1193
1194 static char const xlat_tabs[] = "                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               ";
1195
1196 static void xlat_tokenize_debug(xlat_exp_t const *node, int lvl)
1197 {
1198         rad_assert(node != NULL);
1199
1200         if (lvl >= (int) sizeof(xlat_tabs)) lvl = sizeof(xlat_tabs);
1201
1202         while (node) {
1203                 switch (node->type) {
1204                 case XLAT_LITERAL:
1205                         DEBUG("%.*sliteral --> %s", lvl, xlat_tabs, node->fmt);
1206                         break;
1207
1208                 case XLAT_PERCENT:
1209                         DEBUG("%.*spercent --> %c", lvl, xlat_tabs, node->fmt[0]);
1210                         break;
1211
1212                 case XLAT_ATTRIBUTE:
1213                         rad_assert(node->attr.tmpl_da != NULL);
1214                         DEBUG("%.*sattribute --> %s", lvl, xlat_tabs, node->attr.tmpl_da->name);
1215                         rad_assert(node->child == NULL);
1216                         if ((node->attr.tmpl_tag != TAG_ANY) || (node->attr.tmpl_num != NUM_ANY)) {
1217                                 DEBUG("%.*s{", lvl, xlat_tabs);
1218
1219                                 DEBUG("%.*sref  %d", lvl + 1, xlat_tabs, node->attr.tmpl_request);
1220                                 DEBUG("%.*slist %d", lvl + 1, xlat_tabs, node->attr.tmpl_list);
1221
1222                                 if (node->attr.tmpl_tag != TAG_ANY) {
1223                                         DEBUG("%.*stag %d", lvl + 1, xlat_tabs, node->attr.tmpl_tag);
1224                                 }
1225                                 if (node->attr.tmpl_num != NUM_ANY) {
1226                                         if (node->attr.tmpl_num == NUM_COUNT) {
1227                                                 DEBUG("%.*s[#]", lvl + 1, xlat_tabs);
1228                                         } else if (node->attr.tmpl_num == NUM_ALL) {
1229                                                 DEBUG("%.*s[*]", lvl + 1, xlat_tabs);
1230                                         } else {
1231                                                 DEBUG("%.*s[%d]", lvl + 1, xlat_tabs, node->attr.tmpl_num);
1232                                         }
1233                                 }
1234
1235                                 DEBUG("%.*s}", lvl, xlat_tabs);
1236                         }
1237                         break;
1238
1239                 case XLAT_VIRTUAL:
1240                         rad_assert(node->fmt != NULL);
1241                         DEBUG("%.*svirtual --> %s", lvl, xlat_tabs, node->fmt);
1242                         break;
1243
1244                 case XLAT_MODULE:
1245                         rad_assert(node->xlat != NULL);
1246                         DEBUG("%.*sxlat --> %s", lvl, xlat_tabs, node->xlat->name);
1247                         if (node->child) {
1248                                 DEBUG("%.*s{", lvl, xlat_tabs);
1249                                 xlat_tokenize_debug(node->child, lvl + 1);
1250                                 DEBUG("%.*s}", lvl, xlat_tabs);
1251                         }
1252                         break;
1253
1254 #ifdef HAVE_REGEX
1255                 case XLAT_REGEX:
1256                         DEBUG("%.*sregex-var --> %d", lvl, xlat_tabs, node->attr.tmpl_num);
1257                         break;
1258 #endif
1259
1260                 case XLAT_ALTERNATE:
1261                         DEBUG("%.*sif {", lvl, xlat_tabs);
1262                         xlat_tokenize_debug(node->child, lvl + 1);
1263                         DEBUG("%.*s}", lvl, xlat_tabs);
1264                         DEBUG("%.*selse {", lvl, xlat_tabs);
1265                         xlat_tokenize_debug(node->alternate, lvl + 1);
1266                         DEBUG("%.*s}", lvl, xlat_tabs);
1267                         break;
1268                 }
1269                 node = node->next;
1270         }
1271 }
1272
1273 size_t xlat_sprint(char *buffer, size_t bufsize, xlat_exp_t const *node)
1274 {
1275         size_t len;
1276         char *p, *end;
1277
1278         if (!node) {
1279                 *buffer = '\0';
1280                 return 0;
1281         }
1282
1283         p = buffer;
1284         end = buffer + bufsize;
1285
1286         while (node) {
1287                 switch (node->type) {
1288                 case XLAT_LITERAL:
1289                         strlcpy(p, node->fmt, end - p);
1290                         p += strlen(p);
1291                         break;
1292
1293                 case XLAT_PERCENT:
1294                         p[0] = '%';
1295                         p[1] = node->fmt[0];
1296                         p += 2;
1297                         break;
1298
1299                 case XLAT_ATTRIBUTE:
1300                         *(p++) = '%';
1301                         *(p++) = '{';
1302
1303                         if (node->attr.tmpl_request != REQUEST_CURRENT) {
1304                                 strlcpy(p, fr_int2str(request_refs, node->attr.tmpl_request, "??"), end - p);
1305                                 p += strlen(p);
1306                                 *(p++) = '.';
1307                         }
1308
1309                         if ((node->attr.tmpl_request != REQUEST_CURRENT) ||
1310                             (node->attr.tmpl_list != PAIR_LIST_REQUEST)) {
1311                                 strlcpy(p, fr_int2str(pair_lists, node->attr.tmpl_list, "??"), end - p);
1312                                 p += strlen(p);
1313                                 *(p++) = ':';
1314                         }
1315
1316                         strlcpy(p, node->attr.tmpl_da->name, end - p);
1317                         p += strlen(p);
1318
1319                         if (node->attr.tmpl_tag != TAG_ANY) {
1320                                 *(p++) = ':';
1321                                 snprintf(p, end - p, "%u", node->attr.tmpl_tag);
1322                                 p += strlen(p);
1323                         }
1324
1325                         if (node->attr.tmpl_num != NUM_ANY) {
1326                                 *(p++) = '[';
1327                                 switch (node->attr.tmpl_num) {
1328                                 case NUM_COUNT:
1329                                         *(p++) = '#';
1330                                         break;
1331
1332                                 case NUM_ALL:
1333                                         *(p++) = '*';
1334                                         break;
1335
1336                                 default:
1337                                         snprintf(p, end - p, "%i", node->attr.tmpl_num);
1338                                         p += strlen(p);
1339                                 }
1340                                 *(p++) = ']';
1341                         }
1342                         *(p++) = '}';
1343                         break;
1344 #ifdef HAVE_REGEX
1345                 case XLAT_REGEX:
1346                         snprintf(p, end - p, "%%{%i}", node->attr.tmpl_num);
1347                         p += strlen(p);
1348                         break;
1349 #endif
1350                 case XLAT_VIRTUAL:
1351                         *(p++) = '%';
1352                         *(p++) = '{';
1353                         strlcpy(p, node->fmt, end - p);
1354                         p += strlen(p);
1355                         *(p++) = '}';
1356                         break;
1357
1358                 case XLAT_MODULE:
1359                         *(p++) = '%';
1360                         *(p++) = '{';
1361                         strlcpy(p, node->xlat->name, end - p);
1362                         p += strlen(p);
1363                         *(p++) = ':';
1364                         rad_assert(node->child != NULL);
1365                         len = xlat_sprint(p, end - p, node->child);
1366                         p += len;
1367                         *(p++) = '}';
1368                         break;
1369
1370                 case XLAT_ALTERNATE:
1371                         *(p++) = '%';
1372                         *(p++) = '{';
1373
1374                         len = xlat_sprint(p, end - p, node->child);
1375                         p += len;
1376
1377                         *(p++) = ':';
1378                         *(p++) = '-';
1379
1380                         len = xlat_sprint(p, end - p, node->alternate);
1381                         p += len;
1382
1383                         *(p++) = '}';
1384                         break;
1385                 }
1386
1387
1388                 if (p == end) break;
1389
1390                 node = node->next;
1391         }
1392
1393         *p = '\0';
1394
1395         return p - buffer;
1396 }
1397
1398 ssize_t xlat_tokenize(TALLOC_CTX *ctx, char *fmt, xlat_exp_t **head,
1399                       char const **error)
1400 {
1401         return xlat_tokenize_literal(ctx, fmt, head, false, error);
1402 }
1403
1404
1405 /** Tokenize an xlat expansion
1406  *
1407  * @param[in] request the input request.  Memory will be attached here.
1408  * @param[in] fmt the format string to expand
1409  * @param[out] head the head of the xlat list / tree structure.
1410  */
1411 static ssize_t xlat_tokenize_request(REQUEST *request, char const *fmt, xlat_exp_t **head)
1412 {
1413         ssize_t slen;
1414         char *tokens;
1415         char const *error = NULL;
1416
1417         *head = NULL;
1418
1419         /*
1420          *      Copy the original format string to a buffer so that
1421          *      the later functions can mangle it in-place, which is
1422          *      much faster.
1423          */
1424         tokens = talloc_typed_strdup(request, fmt);
1425         if (!tokens) return -1;
1426
1427         slen = xlat_tokenize_literal(request, tokens, head, false, &error);
1428
1429         /*
1430          *      Zero length expansion, return a zero length node.
1431          */
1432         if (slen == 0) {
1433                 *head = talloc_zero(request, xlat_exp_t);
1434         }
1435
1436         /*
1437          *      Output something like:
1438          *
1439          *      "format string"
1440          *      "       ^ error was here"
1441          */
1442         if (slen < 0) {
1443                 talloc_free(tokens);
1444                 rad_assert(error != NULL);
1445
1446                 REMARKER(fmt, -slen, error);
1447                 return slen;
1448         }
1449
1450         if (*head && (debug_flag > 2)) {
1451                 DEBUG("%s", fmt);
1452                 DEBUG("Parsed xlat tree:");
1453                 xlat_tokenize_debug(*head, 0);
1454         }
1455
1456         /*
1457          *      All of the nodes point to offsets in the "tokens"
1458          *      string.  Let's ensure that free'ing head will free
1459          *      "tokens", too.
1460          */
1461         (void) talloc_steal(*head, tokens);
1462
1463         return slen;
1464 }
1465
1466
1467 static char *xlat_getvp(TALLOC_CTX *ctx, REQUEST *request, pair_lists_t list, DICT_ATTR const *da,
1468                         int8_t tag, int num, bool return_null)
1469 {
1470         VALUE_PAIR *vp = NULL, *vps = NULL, *myvp = NULL;
1471         RADIUS_PACKET *packet = NULL;
1472         DICT_VALUE *dv;
1473         char *ret = NULL;
1474
1475         /*
1476          *      Arg.  Too much abstraction is annoying.
1477          */
1478         switch (list) {
1479         default:
1480                 if (return_null) return NULL;
1481                 return vp_aprints_type(ctx, da->type);
1482
1483         case PAIR_LIST_CONTROL:
1484                 vps = request->config_items;
1485                 break;
1486
1487         case PAIR_LIST_REQUEST:
1488                 packet = request->packet;
1489                 if (packet) vps = packet->vps;
1490                 break;
1491
1492         case PAIR_LIST_REPLY:
1493                 packet = request->reply;
1494                 if (packet) vps = packet->vps;
1495                 break;
1496
1497 #ifdef WITH_PROXY
1498         case PAIR_LIST_PROXY_REQUEST:
1499                 packet = request->proxy;
1500                 if (packet) vps = packet->vps;
1501                 break;
1502
1503         case PAIR_LIST_PROXY_REPLY:
1504                 packet = request->proxy_reply;
1505                 if (packet) vps = packet->vps;
1506                 break;
1507 #endif
1508
1509 #ifdef WITH_COA
1510         case PAIR_LIST_COA:
1511         case PAIR_LIST_DM:
1512                 if (request->coa) packet = request->coa->packet;
1513                 if (packet) vps = packet->vps;
1514                 break;
1515
1516         case PAIR_LIST_COA_REPLY:
1517         case PAIR_LIST_DM_REPLY:
1518                 if (request->coa) packet = request->coa->reply;
1519                 if (packet) vps = packet->vps;
1520                 break;
1521
1522 #endif
1523         }
1524
1525         /*
1526          *      Counting attributes doesn't require us to search for them
1527          */
1528         if (!da->flags.virtual && (num == NUM_COUNT)) goto do_print;
1529
1530         /*
1531          *      Now we have the list, check to see if we have an attribute in
1532          *      the request, if we do, it takes precedence over the virtual
1533          *      attributes.
1534          *
1535          *      This allows users to manipulate virtual attributes as if they
1536          *      were real ones.
1537          */
1538         vp = pair_find_by_da(vps, da, tag);
1539         if (vp) goto do_print;
1540
1541         /*
1542          *      We didn't find the VP in a list.  It MIGHT be a
1543          *      virtual one, in which case we do lots more checks
1544          *      below.  However, if we're looking for a normal
1545          *      attribute, it must exist, and therefore not finding it
1546          *      means we return NULL.
1547          */
1548         if (!da->flags.virtual) return NULL;
1549
1550         /*
1551          *      Some non-packet expansions
1552          */
1553         switch (da->attr) {
1554         default:
1555                 break;          /* ignore them */
1556
1557         case PW_CLIENT_SHORTNAME:
1558                 if (num == NUM_COUNT) goto count;
1559                 if (request->client && request->client->shortname) {
1560                         return talloc_typed_strdup(ctx, request->client->shortname);
1561                 }
1562                 return talloc_typed_strdup(ctx, "<UNKNOWN-CLIENT>");
1563
1564         case PW_REQUEST_PROCESSING_STAGE:
1565                 if (num == NUM_COUNT) goto count;
1566                 if (request->component) {
1567                         return talloc_typed_strdup(ctx, request->component);
1568                 }
1569                 return talloc_typed_strdup(ctx, "server_core");
1570
1571         case PW_VIRTUAL_SERVER:
1572                 if (num == NUM_COUNT) goto count;
1573                 if (!request->server) return NULL;
1574                 return talloc_typed_strdup(ctx, request->server);
1575
1576         case PW_MODULE_RETURN_CODE:
1577                 if (num == NUM_COUNT) goto count;
1578                 if (!request->rcode) return NULL;
1579                 return talloc_typed_strdup(ctx, fr_int2str(modreturn_table, request->rcode, ""));
1580         }
1581
1582         /*
1583          *      All of the attributes must now refer to a packet.
1584          *      If there's no packet, we can't print any attribute
1585          *      referencing it.
1586          */
1587         if (!packet) {
1588                 if (return_null) return NULL;
1589                 return vp_aprints_type(ctx, da->type);
1590         }
1591
1592         vp = NULL;
1593         switch (da->attr) {
1594         default:
1595                 break;
1596
1597         case PW_PACKET_TYPE:
1598                 dv = dict_valbyattr(PW_PACKET_TYPE, 0, packet->code);
1599                 if (dv) return talloc_typed_strdup(ctx, dv->name);
1600                 return talloc_typed_asprintf(ctx, "%d", packet->code);
1601
1602         case PW_RESPONSE_PACKET_TYPE:
1603         {
1604                 int code = 0;
1605
1606 #ifdef WITH_PROXY
1607                 if (request->proxy_reply && (!request->reply || !request->reply->code)) {
1608                         code = request->proxy_reply->code;
1609                 } else
1610 #endif
1611                         if (request->reply) {
1612                                 code = request->reply->code;
1613                         }
1614
1615                 return talloc_typed_strdup(ctx, fr_packet_codes[code]);
1616         }
1617
1618         /*
1619          *      Virtual attributes which require a temporary VALUE_PAIR
1620          *      to be allocated. We can't use stack allocated memory
1621          *      because of the talloc checks sprinkled throughout the
1622          *      various VP functions.
1623          */
1624         case PW_PACKET_AUTHENTICATION_VECTOR:
1625                 myvp = pairalloc(ctx, da);
1626                 pairmemcpy(myvp, packet->vector, sizeof(packet->vector));
1627                 vp = myvp;
1628                 break;
1629
1630         case PW_CLIENT_IP_ADDRESS:
1631         case PW_PACKET_SRC_IP_ADDRESS:
1632                 if (packet->src_ipaddr.af == AF_INET) {
1633                         myvp = pairalloc(ctx, da);
1634                         myvp->vp_ipaddr = packet->src_ipaddr.ipaddr.ip4addr.s_addr;
1635                         vp = myvp;
1636                 }
1637                 break;
1638
1639         case PW_PACKET_DST_IP_ADDRESS:
1640                 if (packet->dst_ipaddr.af == AF_INET) {
1641                         myvp = pairalloc(ctx, da);
1642                         myvp->vp_ipaddr = packet->dst_ipaddr.ipaddr.ip4addr.s_addr;
1643                         vp = myvp;
1644                 }
1645                 break;
1646
1647         case PW_PACKET_SRC_IPV6_ADDRESS:
1648                 if (packet->src_ipaddr.af == AF_INET6) {
1649                         myvp = pairalloc(ctx, da);
1650                         memcpy(&myvp->vp_ipv6addr,
1651                                &packet->src_ipaddr.ipaddr.ip6addr,
1652                                sizeof(packet->src_ipaddr.ipaddr.ip6addr));
1653                         vp = myvp;
1654                 }
1655                 break;
1656
1657         case PW_PACKET_DST_IPV6_ADDRESS:
1658                 if (packet->dst_ipaddr.af == AF_INET6) {
1659                         myvp = pairalloc(ctx, da);
1660                         memcpy(&myvp->vp_ipv6addr,
1661                                &packet->dst_ipaddr.ipaddr.ip6addr,
1662                                sizeof(packet->dst_ipaddr.ipaddr.ip6addr));
1663                         vp = myvp;
1664                 }
1665                 break;
1666
1667         case PW_PACKET_SRC_PORT:
1668                 myvp = pairalloc(ctx, da);
1669                 myvp->vp_integer = packet->src_port;
1670                 vp = myvp;
1671                 break;
1672
1673         case PW_PACKET_DST_PORT:
1674                 myvp = pairalloc(ctx, da);
1675                 myvp->vp_integer = packet->dst_port;
1676                 vp = myvp;
1677                 break;
1678         }
1679
1680         /*
1681          *      Fake various operations for virtual attributes.
1682          */
1683         if (myvp) {
1684                 if (num != NUM_ANY) switch (num) {
1685                 /*
1686                  *      [n] is NULL (we only have [0])
1687                  */
1688                 default:
1689                         goto finish;
1690                 /*
1691                  *      [*] means only one.
1692                  */
1693                 case NUM_ALL:
1694                         break;
1695
1696                 /*
1697                  *      [#] means 1 (as there's only one)
1698                  */
1699                 case NUM_COUNT:
1700                 count:
1701                         ret = talloc_strdup(ctx, "1");
1702                         goto finish;
1703
1704                 /*
1705                  *      [0] is fine (get the first instance)
1706                  */
1707                 case 0:
1708                         break;
1709                 }
1710                 goto print;
1711         }
1712
1713 do_print:
1714         /*
1715          *      We want the N'th VP.
1716          */
1717         if (num != NUM_ANY) {
1718                 int count = 0;
1719                 vp_cursor_t cursor;
1720
1721                 switch (num) {
1722                 /*
1723                  *      Return a count of the VPs.
1724                  */
1725                 case NUM_COUNT:
1726                         fr_cursor_init(&cursor, &vps);
1727                         while (fr_cursor_next_by_da(&cursor, da, tag) != NULL) count++;
1728
1729                         return talloc_typed_asprintf(ctx, "%d", count);
1730
1731                 /*
1732                  *      Ugly, but working.
1733                  */
1734                 case NUM_ALL:
1735                 {
1736                         char *p, *q;
1737
1738                         (void) fr_cursor_init(&cursor, &vps);
1739                         vp = fr_cursor_next_by_da(&cursor, da, tag);
1740                         if (!vp) return NULL;
1741
1742                         p = vp_aprints_value(ctx, vp, '"');
1743                         if (!p) return NULL;
1744                         while ((vp = fr_cursor_next_by_da(&cursor, da, tag)) != NULL) {
1745                                 q = vp_aprints_value(ctx, vp, '"');
1746                                 if (!q) return NULL;
1747                                 p = talloc_strdup_append(p, ",");
1748                                 p = talloc_strdup_append(p, q);
1749                         }
1750
1751                         return p;
1752                 }
1753
1754                 default:
1755                         fr_cursor_init(&cursor, &vps);
1756                         while ((vp = fr_cursor_next_by_da(&cursor, da, tag)) != NULL) {
1757                                 if (count++ == num) break;
1758                         }
1759                         break;
1760                 }
1761         }
1762
1763         if (!vp) {
1764                 if (return_null) return NULL;
1765                 return vp_aprints_type(ctx, da->type);
1766         }
1767
1768 print:
1769         ret = vp_aprints_value(ctx, vp, '"');
1770
1771 finish:
1772         talloc_free(myvp);
1773         return ret;
1774 }
1775
1776 #ifdef DEBUG_XLAT
1777 static const char xlat_spaces[] = "                                                                                                                                                                                                                                                                ";
1778 #endif
1779
1780 static char *xlat_aprint(TALLOC_CTX *ctx, REQUEST *request, xlat_exp_t const * const node,
1781                          RADIUS_ESCAPE_STRING escape, void *escape_ctx, int lvl)
1782 {
1783         ssize_t rcode;
1784         char *str = NULL, *child;
1785         char *q;
1786         char const *p;
1787         REQUEST *ref;
1788
1789         XLAT_DEBUG("%.*sxlat aprint %d", lvl, xlat_spaces, node->type);
1790
1791         switch (node->type) {
1792                 /*
1793                  *      Don't escape this.
1794                  */
1795         case XLAT_LITERAL:
1796                 XLAT_DEBUG("xlat_aprint LITERAL");
1797                 return talloc_typed_strdup(ctx, node->fmt);
1798
1799                 /*
1800                  *      Do a one-character expansion.
1801                  */
1802         case XLAT_PERCENT:
1803         {
1804                 char *nl;
1805                 size_t freespace = 256;
1806                 struct tm ts;
1807                 time_t when;
1808
1809                 XLAT_DEBUG("xlat_aprint PERCENT");
1810
1811                 str = talloc_array(ctx, char, freespace); /* @todo do better allocation */
1812                 p = node->fmt;
1813
1814                 when = request->timestamp;
1815                 if (request->packet) {
1816                         when = request->packet->timestamp.tv_sec;
1817                 }
1818
1819                 switch (*p) {
1820                 case '%':
1821                         str[0] = '%';
1822                         str[1] = '\0';
1823                         break;
1824
1825                 case 'd': /* request day */
1826                         if (!localtime_r(&when, &ts)) goto error;
1827                         strftime(str, freespace, "%d", &ts);
1828                         break;
1829
1830                 case 'l': /* request timestamp */
1831                         snprintf(str, freespace, "%lu",
1832                                  (unsigned long) when);
1833                         break;
1834
1835                 case 'm': /* request month */
1836                         if (!localtime_r(&when, &ts)) goto error;
1837                         strftime(str, freespace, "%m", &ts);
1838                         break;
1839
1840                 case 'n': /* Request Number*/
1841                         snprintf(str, freespace, "%u", request->number);
1842                         break;
1843
1844                 case 't': /* request timestamp */
1845                         CTIME_R(&when, str, freespace);
1846                         nl = strchr(str, '\n');
1847                         if (nl) *nl = '\0';
1848                         break;
1849
1850                 case 'D': /* request date */
1851                         if (!localtime_r(&when, &ts)) goto error;
1852                         strftime(str, freespace, "%Y%m%d", &ts);
1853                         break;
1854
1855                 case 'G': /* request minute */
1856                         if (!localtime_r(&when, &ts)) goto error;
1857                         strftime(str, freespace, "%M", &ts);
1858                         break;
1859
1860                 case 'H': /* request hour */
1861                         if (!localtime_r(&when, &ts)) goto error;
1862                         strftime(str, freespace, "%H", &ts);
1863                         break;
1864
1865                 case 'I': /* Request ID */
1866                         if (request->packet) {
1867                                 snprintf(str, freespace, "%i", request->packet->id);
1868                         }
1869                         break;
1870
1871                 case 'S': /* request timestamp in SQL format*/
1872                         if (!localtime_r(&when, &ts)) goto error;
1873                         strftime(str, freespace, "%Y-%m-%d %H:%M:%S", &ts);
1874                         break;
1875
1876                 case 'T': /* request timestamp */
1877                         if (!localtime_r(&when, &ts)) goto error;
1878                         strftime(str, freespace, "%Y-%m-%d-%H.%M.%S.000000", &ts);
1879                         break;
1880
1881                 case 'Y': /* request year */
1882                         if (!localtime_r(&when, &ts)) {
1883                                 error:
1884                                 REDEBUG("Failed converting packet timestamp to localtime: %s", fr_syserror(errno));
1885                                 talloc_free(str);
1886                                 return NULL;
1887                         }
1888                         strftime(str, freespace, "%Y", &ts);
1889                         break;
1890
1891                 case 'v': /* Version of code */
1892                         snprintf(str, freespace, "%s", radiusd_short_version);
1893                         break;
1894
1895                 default:
1896                         rad_assert(0 == 1);
1897                         break;
1898                 }
1899         }
1900                 break;
1901
1902         case XLAT_ATTRIBUTE:
1903                 XLAT_DEBUG("xlat_aprint ATTRIBUTE");
1904                 ref = request;
1905                 if (radius_request(&ref, node->attr.tmpl_request) < 0) {
1906                         return NULL;
1907                 }
1908
1909                 /*
1910                  *      Some attributes are virtual <sigh>
1911                  */
1912                 str = xlat_getvp(ctx, ref, node->attr.tmpl_list, node->attr.tmpl_da, node->attr.tmpl_tag, node->attr.tmpl_num, true);
1913                 if (str) {
1914                         XLAT_DEBUG("EXPAND attr %s", node->attr.tmpl_da->name);
1915                         XLAT_DEBUG("       ---> %s", str);
1916                 }
1917                 break;
1918
1919         case XLAT_VIRTUAL:
1920                 XLAT_DEBUG("xlat_aprint VIRTUAL");
1921                 str = talloc_array(ctx, char, 2048); /* FIXME: have the module call talloc_typed_asprintf */
1922                 rcode = node->xlat->func(node->xlat->instance, request, NULL, str, 2048);
1923                 if (rcode < 0) {
1924                         talloc_free(str);
1925                         return NULL;
1926                 }
1927                 break;
1928
1929         case XLAT_MODULE:
1930                 XLAT_DEBUG("xlat_aprint MODULE");
1931                 if (xlat_process(&child, request, node->child, node->xlat->escape, node->xlat->instance) == 0) {
1932                         return NULL;
1933                 }
1934
1935                 XLAT_DEBUG("%.*sEXPAND mod %s %s", lvl, xlat_spaces, node->fmt, node->child->fmt);
1936                 XLAT_DEBUG("%.*s      ---> %s", lvl, xlat_spaces, child);
1937
1938                 /*
1939                  *      Smash \n --> CR.
1940                  *
1941                  *      The OUTPUT of xlat is a printable string.  The INPUT might not be...
1942                  *
1943                  *      This is really the reverse of fr_print_string().
1944                  */
1945                 p = q = child;
1946                 while (*p) {
1947                         if (*p == '\\') switch (p[1]) {
1948                                 default:
1949                                         *(q++) = p[1];
1950                                         p += 2;
1951                                         continue;
1952
1953                                 case 'n':
1954                                         *(q++) = '\n';
1955                                         p += 2;
1956                                         continue;
1957
1958                                 case 't':
1959                                         *(q++) = '\t';
1960                                         p += 2;
1961                                         continue;
1962                         }
1963
1964                         *(q++) = *(p++);
1965                 }
1966                 *q = '\0';
1967
1968                 str = talloc_array(ctx, char, 2048); /* FIXME: have the module call talloc_typed_asprintf */
1969                 *str = '\0';    /* Be sure the string is NULL terminated, we now only free on error */
1970
1971                 rcode = node->xlat->func(node->xlat->instance, request, child, str, 2048);
1972                 talloc_free(child);
1973                 if (rcode < 0) {
1974                         talloc_free(str);
1975                         return NULL;
1976                 }
1977                 break;
1978
1979 #ifdef HAVE_REGEX
1980         case XLAT_REGEX:
1981                 XLAT_DEBUG("xlat_aprint REGEX");
1982                 child = request_data_reference(request, request,
1983                                                REQUEST_DATA_REGEX | node->attr.tmpl_num);
1984                 if (!child) return NULL;
1985
1986                 str = talloc_typed_strdup(ctx, child);
1987                 break;
1988 #endif
1989
1990         case XLAT_ALTERNATE:
1991                 XLAT_DEBUG("xlat_aprint ALTERNATE");
1992                 rad_assert(node->child != NULL);
1993                 rad_assert(node->alternate != NULL);
1994
1995                 str = xlat_aprint(ctx, request, node->child, escape, escape_ctx, lvl);
1996                 if (str) break;
1997
1998                 str = xlat_aprint(ctx, request, node->alternate, escape, escape_ctx, lvl);
1999                 break;
2000
2001         }
2002
2003         /*
2004          *      Escape the non-literals we found above.
2005          */
2006         if (str && escape) {
2007                 char *escaped;
2008
2009                 escaped = talloc_array(ctx, char, 2048); /* FIXME: do something intelligent */
2010                 escape(request, escaped, 2038, str, escape_ctx);
2011                 talloc_free(str);
2012                 str = escaped;
2013         }
2014
2015         return str;
2016 }
2017
2018
2019 static size_t xlat_process(char **out, REQUEST *request, xlat_exp_t const * const head,
2020                            RADIUS_ESCAPE_STRING escape, void *escape_ctx)
2021 {
2022         int i, list;
2023         size_t total;
2024         char **array, *answer;
2025         xlat_exp_t const *node;
2026
2027         *out = NULL;
2028
2029         /*
2030          *      There are no nodes to process, so the result is a zero
2031          *      length string.
2032          */
2033         if (!head) {
2034                 *out = talloc_zero_array(request, char, 1);
2035                 return 0;
2036         }
2037
2038         /*
2039          *      Hack for speed.  If it's one expansion, just allocate
2040          *      that and return, instead of allocating an intermediary
2041          *      array.
2042          */
2043         if (!head->next) {
2044                 /*
2045                  *      Pass the MAIN escape function.  Recursive
2046                  *      calls will call node-specific escape
2047                  *      functions.
2048                  */
2049                 answer = xlat_aprint(request, request, head, escape, escape_ctx, 0);
2050                 if (!answer) {
2051                         *out = talloc_zero_array(request, char, 1);
2052                         return 0;
2053                 }
2054                 *out = answer;
2055                 return strlen(answer);
2056         }
2057
2058         list = 0;               /* FIXME: calculate this once */
2059         for (node = head; node != NULL; node = node->next) {
2060                 list++;
2061         }
2062
2063         array = talloc_array(request, char *, list);
2064         if (!array) return -1;
2065
2066         for (node = head, i = 0; node != NULL; node = node->next, i++) {
2067                 array[i] = xlat_aprint(array, request, node, escape, escape_ctx, 0); /* may be NULL */
2068         }
2069
2070         total = 0;
2071         for (i = 0; i < list; i++) {
2072                 if (array[i]) total += strlen(array[i]); /* FIXME: calculate strlen once */
2073         }
2074
2075         if (!total) {
2076                 talloc_free(array);
2077                 *out = talloc_zero_array(request, char, 1);
2078                 return 0;
2079         }
2080
2081         answer = talloc_array(request, char, total + 1);
2082
2083         total = 0;
2084         for (i = 0; i < list; i++) {
2085                 size_t len;
2086
2087                 if (array[i]) {
2088                         len = strlen(array[i]);
2089                         memcpy(answer + total, array[i], len);
2090                         total += len;
2091                 }
2092         }
2093         answer[total] = '\0';
2094         talloc_free(array);     /* and child entries */
2095
2096         *out = answer;
2097         return total;
2098 }
2099
2100
2101 /** Replace %whatever in a string.
2102  *
2103  * See 'doc/variables.txt' for more information.
2104  *
2105  * @param[out] out Where to write pointer to output buffer.
2106  * @param[in] outlen Size of out.
2107  * @param[in] request current request.
2108  * @param[in] node the xlat structure to expand
2109  * @param[in] escape function to escape final value e.g. SQL quoting.
2110  * @param[in] escape_ctx pointer to pass to escape function.
2111  * @return length of string written @bug should really have -1 for failure
2112  */
2113 static ssize_t xlat_expand_struct(char **out, size_t outlen, REQUEST *request, xlat_exp_t const *node,
2114                                   RADIUS_ESCAPE_STRING escape, void *escape_ctx)
2115 {
2116         char *buff;
2117         ssize_t len;
2118
2119         rad_assert(node != NULL);
2120
2121         len = xlat_process(&buff, request, node, escape, escape_ctx);
2122         if ((len < 0) || !buff) {
2123                 rad_assert(buff == NULL);
2124                 if (*out) *out[0] = '\0';
2125                 return len;
2126         }
2127
2128         if (!*out) {
2129                 *out = buff;
2130         } else {
2131                 strlcpy(*out, buff, outlen);
2132                 talloc_free(buff);
2133         }
2134
2135         return strlen(*out);
2136 }
2137
2138 static ssize_t xlat_expand(char **out, size_t outlen, REQUEST *request, char const *fmt,
2139                            RADIUS_ESCAPE_STRING escape, void *escape_ctx) CC_HINT(nonnull (1, 3, 4));
2140
2141 /** Replace %whatever in a string.
2142  *
2143  * See 'doc/variables.txt' for more information.
2144  *
2145  * @param[out] out Where to write pointer to output buffer.
2146  * @param[in] outlen Size of out.
2147  * @param[in] request current request.
2148  * @param[in] fmt string to expand.
2149  * @param[in] escape function to escape final value e.g. SQL quoting.
2150  * @param[in] escape_ctx pointer to pass to escape function.
2151  * @return length of string written @bug should really have -1 for failure
2152  */
2153 static ssize_t xlat_expand(char **out, size_t outlen, REQUEST *request, char const *fmt,
2154                            RADIUS_ESCAPE_STRING escape, void *escape_ctx)
2155 {
2156         ssize_t len;
2157         xlat_exp_t *node;
2158
2159         /*
2160          *      Give better errors than the old code.
2161          */
2162         len = xlat_tokenize_request(request, fmt, &node);
2163         if (len == 0) {
2164                 if (*out) {
2165                         *out[0] = '\0';
2166                 } else {
2167                         *out = talloc_zero_array(request, char, 1);
2168                 }
2169                 return 0;
2170         }
2171
2172         if (len < 0) {
2173                 if (*out) *out[0] = '\0';
2174                 return -1;
2175         }
2176
2177         len = xlat_expand_struct(out, outlen, request, node, escape, escape_ctx);
2178         talloc_free(node);
2179
2180         RDEBUG2("EXPAND %s", fmt);
2181         RDEBUG2("   --> %s", *out);
2182
2183         return len;
2184 }
2185
2186 /*
2187  *      Try to convert an xlat to a tmpl for efficiency
2188  */
2189 value_pair_tmpl_t *radius_xlat2tmpl(TALLOC_CTX *ctx, xlat_exp_t *node)
2190 {
2191         value_pair_tmpl_t *vpt;
2192
2193         if (node->next || (node->type != XLAT_ATTRIBUTE)) return NULL;
2194
2195         /*
2196          * @todo it should be possible to emulate the concat and count operations in the
2197          * map code.
2198          */
2199         if ((node->attr.tmpl_num == NUM_COUNT) || (node->attr.tmpl_num == NUM_ALL)) return NULL;
2200
2201         vpt = tmpl_alloc(ctx, TMPL_TYPE_ATTR, node->fmt, -1);
2202         if (!vpt) return NULL;
2203         vpt->tmpl_request = node->attr.tmpl_request;
2204         vpt->tmpl_list = node->attr.tmpl_list;
2205         vpt->tmpl_da = node->attr.tmpl_da;
2206         vpt->tmpl_num = node->attr.tmpl_num;
2207         vpt->tmpl_tag = node->attr.tmpl_tag;
2208
2209         VERIFY_TMPL(vpt);
2210
2211         return vpt;
2212 }
2213
2214 ssize_t radius_xlat(char *out, size_t outlen, REQUEST *request, char const *fmt, RADIUS_ESCAPE_STRING escape, void *ctx)
2215 {
2216         return xlat_expand(&out, outlen, request, fmt, escape, ctx);
2217 }
2218
2219 ssize_t radius_axlat(char **out, REQUEST *request, char const *fmt, RADIUS_ESCAPE_STRING escape, void *ctx)
2220 {
2221         return xlat_expand(out, 0, request, fmt, escape, ctx);
2222 }
2223
2224 ssize_t radius_axlat_struct(char **out, REQUEST *request, xlat_exp_t const *xlat, RADIUS_ESCAPE_STRING escape, void *ctx)
2225 {
2226         return xlat_expand_struct(out, 0, request, xlat, escape, ctx);
2227 }