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.
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.
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
21 * @brief String expansion ("translation"). Implements %Attribute -> value
23 * @copyright 2000,2006 The FreeRADIUS server project
24 * @copyright 2000 Alan DeKok <aland@ox.org>
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>
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.
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
52 XLAT_REGEX, //!< regex reference
54 XLAT_ALTERNATE //!< xlat conditional syntax :-
58 char const *fmt; //!< The format string.
59 size_t len; //!< Length of the format string.
61 xlat_state_t type; //!< type of this expansion.
62 xlat_exp_t *next; //!< Next in the list.
64 xlat_exp_t *child; //!< Nested expansion.
65 xlat_exp_t *alternate; //!< Alternative expansion if this one expanded to a zero length string.
67 value_pair_tmpl_t attr; //!< An attribute template.
68 xlat_t const *xlat; //!< The xlat expansion to expand format with.
71 typedef struct xlat_out {
72 char const *out; //!< Output data.
73 size_t len; //!< Length of the output string.
76 static rbtree_t *xlat_root = NULL;
79 static char const * const xlat_foreach_names[] = {"Foreach-Variable-0",
93 static int xlat_inst[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; /* up to 10 for foreach */
95 static char const *radiusd_short_version = RADIUSD_VERSION_STRING;
97 /** Print length of its RHS.
100 static ssize_t xlat_strlen(UNUSED void *instance, UNUSED REQUEST *request,
101 char const *fmt, char *out, size_t outlen)
103 snprintf(out, outlen, "%u", (unsigned int) strlen(fmt));
107 /** Print the size of the attribute in bytes.
110 static ssize_t xlat_length(UNUSED void *instance, REQUEST *request,
111 char const *fmt, char *out, size_t outlen)
114 while (isspace((int) *fmt)) fmt++;
116 if ((radius_get_vp(&vp, request, fmt) < 0) || !vp) {
121 snprintf(out, outlen, "%zu", vp->vp_length);
125 /** Print data as integer, not as VALUE.
128 static ssize_t xlat_integer(UNUSED void *instance, REQUEST *request,
129 char const *fmt, char *out, size_t outlen)
133 uint64_t int64 = 0; /* Needs to be initialised to zero */
134 uint32_t int32 = 0; /* Needs to be initialised to zero */
136 while (isspace((int) *fmt)) fmt++;
138 if ((radius_get_vp(&vp, request, fmt) < 0) || !vp) {
143 switch (vp->da->type) {
146 if (vp->vp_length > 8) {
150 if (vp->vp_length > 4) {
151 memcpy(&int64, vp->vp_octets, vp->vp_length);
152 return snprintf(out, outlen, "%" PRIu64, htonll(int64));
155 memcpy(&int32, vp->vp_octets, vp->vp_length);
156 return snprintf(out, outlen, "%i", htonl(int32));
158 case PW_TYPE_INTEGER64:
159 return snprintf(out, outlen, "%" PRIu64, vp->vp_integer64);
162 * IP addresses are treated specially, as parsing functions assume the value
163 * is bigendian and will convert it for us.
165 case PW_TYPE_IPV4_ADDR:
166 return snprintf(out, outlen, "%u", htonl(vp->vp_ipaddr));
168 case PW_TYPE_IPV4_PREFIX:
169 return snprintf(out, outlen, "%u", htonl((*(uint32_t *)(vp->vp_ipv4prefix + 2))));
171 case PW_TYPE_INTEGER:
173 return snprintf(out, outlen, "%u", vp->vp_integer);
175 return snprintf(out, outlen, "%u", (unsigned int) vp->vp_byte);
177 return snprintf(out, outlen, "%u", (unsigned int) vp->vp_short);
180 * Ethernet is weird... It's network related, so we assume to it should be
183 case PW_TYPE_ETHERNET:
184 memcpy(&int64, vp->vp_ether, vp->vp_length);
185 return snprintf(out, outlen, "%" PRIu64, htonll(int64));
188 return snprintf(out, outlen, "%i", vp->vp_signed);
190 case PW_TYPE_IPV6_ADDR:
191 return fr_prints_uint128(out, outlen, ntohlll(*(uint128_t const *) &vp->vp_ipv6addr));
193 case PW_TYPE_IPV6_PREFIX:
194 return fr_prints_uint128(out, outlen, ntohlll(*(uint128_t const *) &vp->vp_ipv6prefix[2]));
200 REDEBUG("Type '%s' of length %zu cannot be converted to integer",
201 fr_int2str(dict_attr_types, vp->da->type, "???"), vp->vp_length);
207 /** Print data as hex, not as VALUE.
210 static ssize_t xlat_hex(UNUSED void *instance, REQUEST *request,
211 char const *fmt, char *out, size_t outlen)
219 uint8_t const *buff = NULL;
221 while (isspace((int) *fmt)) fmt++;
223 if ((radius_get_vp(&vp, request, fmt) < 0) || !vp) {
232 if (vp->da->type == PW_TYPE_OCTETS) {
236 * Cast the value_data_t of the VP to an octets string and
240 ret = value_data_cast(request, &dst, PW_TYPE_OCTETS, NULL, vp->da->type,
241 NULL, &vp->data, vp->vp_length);
243 REDEBUG("%s", fr_strerror());
247 p = buff = dst.octets;
253 * Don't truncate the data.
255 if (outlen < (len * 2)) {
256 rad_const_free(buff);
260 for (i = 0; i < len; i++) {
261 snprintf(out + 2*i, 3, "%02x", p[i]);
263 rad_const_free(buff);
268 /** Return the tag of an attribute reference
271 static ssize_t xlat_tag(UNUSED void *instance, REQUEST *request,
272 char const *fmt, char *out, size_t outlen)
276 while (isspace((int) *fmt)) fmt++;
278 if ((radius_get_vp(&vp, request, fmt) < 0) || !vp) {
283 if (!vp->da->flags.has_tag || !TAG_VALID(vp->tag)) {
288 return snprintf(out, outlen, "%u", vp->tag);
291 /** Print out attribute info
293 * Prints out all instances of a current attribute, or all attributes in a list.
295 * At higher debugging levels, also prints out alternative decodings of the same
296 * value. This is helpful to determine types for unknown attributes of long
297 * passed vendors, or just crazy/broken NAS.
299 * It's also useful for exposing issues in the packet decoding functions, as in
300 * some cases they get fed random garbage data.
302 * This expands to a zero length string.
304 static ssize_t xlat_debug_attr(UNUSED void *instance, REQUEST *request, char const *fmt,
305 char *out, UNUSED size_t outlen)
310 value_pair_tmpl_t vpt;
312 if (!RDEBUG_ENABLED2) {
317 while (isspace((int) *fmt)) fmt++;
319 if (tmpl_from_attr_str(&vpt, fmt, REQUEST_CURRENT, PAIR_LIST_REQUEST, false) <= 0) {
320 RDEBUG("%s", fr_strerror());
324 RIDEBUG("Attributes matching \"%s\"", fmt);
327 for (vp = tmpl_cursor_init(NULL, &cursor, request, &vpt);
329 vp = tmpl_cursor_next(&cursor, &vpt)) {
330 FR_NAME_NUMBER const *type;
333 value = vp_aprints_value(vp, vp, '\'');
334 if (vp->da->flags.has_tag) {
335 RIDEBUG2("&%s:%s:%i %s %s",
336 fr_int2str(pair_lists, vpt.tmpl_list, "<INVALID>"),
339 fr_int2str(fr_tokens, vp->op, "<INVALID>"),
342 RIDEBUG2("&%s:%s %s %s",
343 fr_int2str(pair_lists, vpt.tmpl_list, "<INVALID>"),
345 fr_int2str(fr_tokens, vp->op, "<INVALID>"),
350 if (!RDEBUG_ENABLED3) continue;
352 if (vp->da->vendor) {
355 dv = dict_vendorbyvalue(vp->da->vendor);
356 RIDEBUG2("Vendor : %i (%s)", vp->da->vendor, dv ? dv->name : "unknown");
358 RIDEBUG2("Type : %s", fr_int2str(dict_attr_types, vp->da->type, "<INVALID>"));
359 RIDEBUG2("Length : %zu", vp->vp_length);
361 if (!RDEBUG_ENABLED4) continue;
363 type = dict_attr_types;
367 value_data_t *dst = NULL;
371 if ((PW_TYPE) type->number == vp->da->type) {
375 switch (type->number) {
376 case PW_TYPE_INVALID: /* Not real type */
377 case PW_TYPE_MAX: /* Not real type */
378 case PW_TYPE_EXTENDED: /* Not safe/appropriate */
379 case PW_TYPE_LONG_EXTENDED: /* Not safe/appropriate */
380 case PW_TYPE_TLV: /* Not safe/appropriate */
381 case PW_TYPE_EVS: /* Not safe/appropriate */
382 case PW_TYPE_VSA: /* @fixme We need special behaviour for these */
383 case PW_TYPE_COMBO_IP_ADDR: /* Covered by IPv4 address IPv6 address */
384 case PW_TYPE_COMBO_IP_PREFIX: /* Covered by IPv4 address IPv6 address */
385 case PW_TYPE_TIMEVAL: /* Not a VALUE_PAIR type */
393 dst = talloc_zero(vp, value_data_t);
394 ret = value_data_cast(dst, dst, type->number, NULL, vp->da->type, vp->da,
395 &vp->data, vp->vp_length);
396 if (ret < 0) goto next_type; /* We expect some to fail */
398 value = vp_data_aprints_value(dst, type->number, NULL, dst, (size_t)ret, '\'');
399 if (!value) goto next_type;
401 if ((pad = (11 - strlen(type->name))) < 0) {
406 RDEBUG2("as %s%*s: %s", type->name, pad, " ", value);
419 /** Prints the current module processing the request
422 static ssize_t xlat_module(UNUSED void *instance, REQUEST *request,
423 UNUSED char const *fmt, char *out, size_t outlen)
425 strlcpy(out, request->module, outlen);
430 #if defined(HAVE_REGEX) && defined(HAVE_PCRE)
431 static ssize_t xlat_regex(UNUSED void *instance, REQUEST *request,
432 char const *fmt, char *out, size_t outlen)
437 if (regex_request_to_sub_named(request, &p, request, fmt) < 0) {
442 len = talloc_array_length(p);
444 RDEBUG("Insufficient buffer space to write subcapture value, needed %zu bytes, have %zu bytes",
448 strlcpy(out, p, outlen);
450 return len - 1; /* - \0 */
455 /** Implements the Foreach-Variable-X
459 static ssize_t xlat_foreach(void *instance, REQUEST *request,
460 UNUSED char const *fmt, char *out, size_t outlen)
466 * See modcall, "FOREACH" for how this works.
468 pvp = (VALUE_PAIR **) request_data_reference(request, (void *)radius_get_vp, *(int*) instance);
474 len = vp_prints_value(out, outlen, *pvp, 0);
475 if (is_truncated(len, outlen)) {
476 RDEBUG("Insufficient buffer space to write foreach value");
484 /** Print data as string, if possible.
486 * If attribute "Foo" is defined as "octets" it will normally
487 * be printed as 0x0a0a0a. The xlat "%{string:Foo}" will instead
490 static ssize_t xlat_string(UNUSED void *instance, REQUEST *request,
491 char const *fmt, char *out, size_t outlen)
498 while (isspace((int) *fmt)) fmt++;
506 if ((radius_get_vp(&vp, request, fmt) < 0) || !vp) goto nothing;
508 ret = rad_vp2data(&p, vp);
513 switch (vp->da->type) {
515 len = fr_prints(out, outlen, (char const *) p, vp->vp_length, '"');
519 len = strlcpy(out, vp->vp_strvalue, outlen);
523 len = fr_prints(out, outlen, (char const *) p, ret, '\0');
530 /** xlat expand string attribute value
533 static ssize_t xlat_xlat(UNUSED void *instance, REQUEST *request,
534 char const *fmt, char *out, size_t outlen)
538 while (isspace((int) *fmt)) fmt++;
546 if ((radius_get_vp(&vp, request, fmt) < 0) || !vp) goto nothing;
548 return radius_xlat(out, outlen, request, vp->vp_strvalue, NULL, NULL);
551 /** Dynamically change the debugging level for the current request
555 static ssize_t xlat_debug(UNUSED void *instance, REQUEST *request,
556 char const *fmt, char *out, size_t outlen)
561 * Expand to previous (or current) level
563 snprintf(out, outlen, "%d", request->log.lvl & RAD_REQUEST_OPTION_DEBUG4);
566 * Assume we just want to get the current value and NOT set it to 0
573 request->log.lvl = RAD_REQUEST_OPTION_NONE;
574 request->log.func = NULL;
576 if (level > 4) level = 4;
578 request->log.lvl = level;
579 request->log.func = vradlog_request;
587 * Compare two xlat_t structs, based ONLY on the module name.
589 static int xlat_cmp(void const *one, void const *two)
591 xlat_t const *a = one;
592 xlat_t const *b = two;
594 if (a->length != b->length) {
595 return a->length - b->length;
598 return memcmp(a->name, b->name, a->length);
603 * find the appropriate registered xlat function.
605 static xlat_t *xlat_find(char const *name)
609 strlcpy(my_xlat.name, name, sizeof(my_xlat.name));
610 my_xlat.length = strlen(my_xlat.name);
612 return rbtree_finddata(xlat_root, &my_xlat);
616 /** Register an xlat function.
618 * @param[in] name xlat name.
619 * @param[in] func xlat function to be called.
620 * @param[in] escape function to sanitize any sub expansions passed to the xlat function.
621 * @param[in] instance of module that's registering the xlat function.
622 * @return 0 on success, -1 on failure
624 int xlat_register(char const *name, RAD_XLAT_FUNC func, RADIUS_ESCAPE_STRING escape, void *instance)
630 if (!name || !*name) {
631 DEBUG("xlat_register: Invalid xlat name");
636 * First time around, build up the tree...
638 * FIXME: This code should be hoisted out of this function,
639 * and into a global "initialization". But it isn't critical...
646 xlat_root = rbtree_create(NULL, xlat_cmp, NULL, RBTREE_FLAG_REPLACE);
648 DEBUG("xlat_register: Failed to create tree");
653 for (i = 0; xlat_foreach_names[i] != NULL; i++) {
654 xlat_register(xlat_foreach_names[i],
655 xlat_foreach, NULL, &xlat_inst[i]);
656 c = xlat_find(xlat_foreach_names[i]);
657 rad_assert(c != NULL);
662 #define XLAT_REGISTER(_x) xlat_register(STRINGIFY(_x), xlat_ ## _x, NULL, NULL); \
663 c = xlat_find(STRINGIFY(_x)); \
664 rad_assert(c != NULL); \
667 XLAT_REGISTER(integer);
668 XLAT_REGISTER(strlen);
669 XLAT_REGISTER(length);
672 XLAT_REGISTER(string);
674 XLAT_REGISTER(module);
675 XLAT_REGISTER(debug_attr);
676 #if defined(HAVE_REGEX) && defined(HAVE_PCRE)
677 XLAT_REGISTER(regex);
680 xlat_register("debug", xlat_debug, NULL, &xlat_inst[0]);
681 c = xlat_find("debug");
682 rad_assert(c != NULL);
687 * If it already exists, replace the instance.
689 strlcpy(my_xlat.name, name, sizeof(my_xlat.name));
690 my_xlat.length = strlen(my_xlat.name);
691 c = rbtree_finddata(xlat_root, &my_xlat);
694 DEBUG("xlat_register: Cannot re-define internal xlat");
700 c->instance = instance;
705 * Doesn't exist. Create it.
707 c = talloc_zero(xlat_root, xlat_t);
711 strlcpy(c->name, name, sizeof(c->name));
712 c->length = strlen(c->name);
713 c->instance = instance;
715 node = rbtree_insert_node(xlat_root, c);
722 * Ensure that the data is deleted when the node is
725 * @todo: Maybe this should be the other way around...
726 * when a thing IN the tree is deleted, it's automatically
727 * removed from the tree. But for now, this works.
729 (void) talloc_steal(node, c);
733 /** Unregister an xlat function
735 * We can only have one function to call per name, so the passing of "func"
736 * here is extraneous.
738 * @param[in] name xlat to unregister.
739 * @param[in] func unused.
740 * @param[in] instance data.
742 void xlat_unregister(char const *name, UNUSED RAD_XLAT_FUNC func, void *instance)
749 strlcpy(my_xlat.name, name, sizeof(my_xlat.name));
750 my_xlat.length = strlen(my_xlat.name);
752 c = rbtree_finddata(xlat_root, &my_xlat);
755 if (c->instance != instance) return;
757 rbtree_deletebydata(xlat_root, c);
760 static int xlat_unregister_callback(void *instance, void *data)
762 xlat_t *c = (xlat_t *) data;
764 if (c->instance != instance) return 0; /* keep walking */
766 return 2; /* delete it */
769 void xlat_unregister_module(void *instance)
771 rbtree_walk(xlat_root, RBTREE_DELETE_ORDER, xlat_unregister_callback, instance);
775 * Internal redundant handler for xlats
777 typedef enum xlat_redundant_type_t {
781 XLAT_REDUNDANT_LOAD_BALANCE,
782 } xlat_redundant_type_t;
784 typedef struct xlat_redundant_t {
785 xlat_redundant_type_t type;
791 static ssize_t xlat_redundant(void *instance, REQUEST *request,
792 char const *fmt, char *out, size_t outlen)
794 xlat_redundant_t *xr = instance;
799 rad_assert(xr->type == XLAT_REDUNDANT);
802 * Pick the first xlat which succeeds
804 for (ci = cf_item_find_next(xr->cs, NULL);
806 ci = cf_item_find_next(xr->cs, ci)) {
809 if (!cf_item_is_pair(ci)) continue;
811 name = cf_pair_attr(cf_item_to_pair(ci));
812 rad_assert(name != NULL);
814 xlat = xlat_find(name);
817 rcode = xlat->func(xlat->instance, request, fmt, out, outlen);
818 if (rcode <= 0) continue;
823 * Everything failed. Oh well.
830 static ssize_t xlat_load_balance(void *instance, REQUEST *request,
831 char const *fmt, char *out, size_t outlen)
834 xlat_redundant_t *xr = instance;
836 CONF_ITEM *found = NULL;
841 * Choose a child at random.
843 for (ci = cf_item_find_next(xr->cs, NULL);
845 ci = cf_item_find_next(xr->cs, ci)) {
846 if (!cf_item_is_pair(ci)) continue;
850 * Replace the previously found one with a random
853 if ((count * (fr_rand() & 0xffff)) < (uint32_t) 0x10000) {
859 * Plain load balancing: do one child, and only one child.
861 if (xr->type == XLAT_LOAD_BALANCE) {
862 name = cf_pair_attr(cf_item_to_pair(found));
863 rad_assert(name != NULL);
865 xlat = xlat_find(name);
866 if (!xlat) return -1;
868 return xlat->func(xlat->instance, request, fmt, out, outlen);
871 rad_assert(xr->type == XLAT_REDUNDANT_LOAD_BALANCE);
874 * Try the random one we found. If it fails, keep going
875 * through the rest of the children.
879 name = cf_pair_attr(cf_item_to_pair(ci));
880 rad_assert(name != NULL);
882 xlat = xlat_find(name);
886 rcode = xlat->func(xlat->instance, request, fmt, out, outlen);
887 if (rcode > 0) return rcode;
891 * Go to the next one, wrapping around at the end.
893 ci = cf_item_find_next(xr->cs, ci);
894 if (!ci) ci = cf_item_find_next(xr->cs, NULL);
895 } while (ci != found);
901 bool xlat_register_redundant(CONF_SECTION *cs)
903 char const *name1, *name2;
904 xlat_redundant_t *xr;
906 name1 = cf_section_name1(cs);
907 name2 = cf_section_name2(cs);
909 if (!name2) return false;
911 if (xlat_find(name2)) {
912 cf_log_err_cs(cs, "An expansion is already registered for this name");
916 xr = talloc_zero(cs, xlat_redundant_t);
917 if (!xr) return false;
919 if (strcmp(name1, "redundant") == 0) {
920 xr->type = XLAT_REDUNDANT;
922 } else if (strcmp(name1, "redundant-load-balance") == 0) {
923 xr->type = XLAT_REDUNDANT_LOAD_BALANCE;
925 } else if (strcmp(name1, "load-balance") == 0) {
926 xr->type = XLAT_LOAD_BALANCE;
935 * Get the number of children for load balancing.
937 if (xr->type == XLAT_REDUNDANT) {
938 if (xlat_register(name2, xlat_redundant, NULL, xr) < 0) {
946 for (ci = cf_item_find_next(cs, NULL);
948 ci = cf_item_find_next(cs, ci)) {
949 if (!cf_item_is_pair(ci)) continue;
951 if (!xlat_find(cf_pair_attr(cf_item_to_pair(ci)))) {
959 if (xlat_register(name2, xlat_load_balance, NULL, xr) < 0) {
969 /** Crappy temporary function to add attribute ref support to xlats
971 * This needs to die, and hopefully will die, when xlat functions accept
972 * xlat node structures.
974 * Provides either a pointer to a buffer which contains the value of the reference VALUE_PAIR
975 * in an architecture independent format. Or a pointer to the start of the fmt string.
977 * The pointer is only guaranteed to be valid between calls to xlat_fmt_to_ref,
978 * and so long as the source VALUE_PAIR is not freed.
980 * @param out where to write a pointer to the buffer to the data the xlat function needs to work on.
981 * @param request current request.
983 * @returns the length of the data or -1 on error.
985 ssize_t xlat_fmt_to_ref(uint8_t const **out, REQUEST *request, char const *fmt)
989 while (isspace((int) *fmt)) fmt++;
992 if ((radius_get_vp(&vp, request, fmt) < 0) || !vp) {
997 return rad_vp2data(out, vp);
1000 *out = (uint8_t const *)fmt;
1004 /** De-register all xlat functions, used mainly for debugging.
1007 void xlat_free(void)
1009 rbtree_free(xlat_root);
1013 # define XLAT_DEBUG DEBUG3
1015 # define XLAT_DEBUG(...)
1018 static ssize_t xlat_tokenize_expansion(TALLOC_CTX *ctx, char *fmt, xlat_exp_t **head,
1019 char const **error);
1020 static ssize_t xlat_tokenize_literal(TALLOC_CTX *ctx, char *fmt, xlat_exp_t **head,
1021 bool brace, char const **error);
1022 static size_t xlat_process(char **out, REQUEST *request, xlat_exp_t const * const head,
1023 RADIUS_ESCAPE_STRING escape, void *escape_ctx);
1025 static ssize_t xlat_tokenize_alternation(TALLOC_CTX *ctx, char *fmt, xlat_exp_t **head,
1032 rad_assert(fmt[0] == '%');
1033 rad_assert(fmt[1] == '{');
1034 rad_assert(fmt[2] == '%');
1035 rad_assert(fmt[3] == '{');
1037 XLAT_DEBUG("ALTERNATE <-- %s", fmt);
1039 node = talloc_zero(ctx, xlat_exp_t);
1040 node->type = XLAT_ALTERNATE;
1043 slen = xlat_tokenize_expansion(node, p, &node->child, error);
1046 return slen - (p - fmt);
1052 *error = "Expected ':' after first expansion";
1059 *error = "Expected '-' after ':'";
1065 * Allow the RHS to be empty as a special case.
1069 * Hack up an empty string.
1071 node->alternate = talloc_zero(node, xlat_exp_t);
1072 node->alternate->type = XLAT_LITERAL;
1073 node->alternate->fmt = talloc_typed_strdup(node->alternate, "");
1077 slen = xlat_tokenize_literal(node, p, &node->alternate, true, error);
1080 return slen - (p - fmt);
1083 if (!node->alternate) {
1085 *error = "Empty expansion is invalid";
1095 static ssize_t xlat_tokenize_expansion(TALLOC_CTX *ctx, char *fmt, xlat_exp_t **head,
1103 rad_assert(fmt[0] == '%');
1104 rad_assert(fmt[1] == '{');
1109 if ((fmt[2] == '%') && (fmt[3] == '{')) return xlat_tokenize_alternation(ctx, fmt, head, error);
1111 XLAT_DEBUG("EXPANSION <-- %s", fmt);
1112 node = talloc_zero(ctx, xlat_exp_t);
1113 node->fmt = fmt + 2;
1118 * Handle regex's specially.
1121 num = strtol(p, &q, 10);
1122 if (p != q && (*q == '}')) {
1123 XLAT_DEBUG("REGEX <-- %s", fmt);
1126 if ((num > REQUEST_MAX_REGEX) || (num < 0)) {
1128 *error = "Invalid regex reference. Must be in range 0-" STRINGIFY(REQUEST_MAX_REGEX);
1131 node->attr.tmpl_num = num;
1133 node->type = XLAT_REGEX;
1136 return (q - fmt) + 1;
1138 #endif /* HAVE_REGEX */
1143 * %{Tunnel-Password:1}
1144 * %{Tunnel-Password:1[#]}
1145 * %{request:Attr-Name}
1146 * %{request:Tunnel-Password:1}
1147 * %{request:Tunnel-Password:1[#]}
1152 * This is for efficiency, so we don't search for an xlat,
1153 * when what's being referenced is obviously an attribute.
1156 for (q = p; *q != '\0'; q++) {
1157 if (*q == ':') break;
1159 if (isspace((int) *q)) break;
1161 if (*q == '[') continue;
1163 if (*q == '}') break;
1167 * Check for empty expressions %{}
1169 if ((*q == '}') && (q == p)) {
1170 *error = "Empty expression is invalid";
1175 * Might be a module name reference.
1177 * If it's not, it's an attribute or parse error.
1181 node->xlat = xlat_find(node->fmt);
1186 node->type = XLAT_MODULE;
1189 XLAT_DEBUG("MOD <-- %s ... %s", node->fmt, p);
1191 slen = xlat_tokenize_literal(node, p, &node->child, true, error);
1194 return slen - (p - fmt);
1199 rad_assert(node->next == NULL);
1203 *q = ':'; /* Avoids a strdup */
1207 * The first token ends with:
1208 * - '[' - Which is an attribute index, so it must be an attribute.
1209 * - '}' - The end of the expansion, which means it was a bareword.
1211 slen = tmpl_from_attr_substr(&node->attr, p, REQUEST_CURRENT, PAIR_LIST_REQUEST, true);
1214 * If the parse error occurred before the ':'
1215 * then the error is changed to 'Unknown module',
1216 * as it was more likely to be a bad module name,
1217 * than a request qualifier.
1219 if ((*q == ':') && ((p + (slen * -1)) < q)) {
1220 *error = "Unknown module";
1222 *error = fr_strerror();
1224 return slen - (p - fmt);
1228 * Might be a virtual XLAT attribute
1230 if (node->attr.type == TMPL_TYPE_ATTR_UNDEFINED) {
1231 node->xlat = xlat_find(node->attr.tmpl_unknown_name);
1233 node->type = XLAT_VIRTUAL;
1234 node->fmt = node->attr.tmpl_unknown_name;
1236 XLAT_DEBUG("VIRTUAL <-- %s", node->fmt);
1238 rad_assert(node->next == NULL);
1244 *error = "Unknown attribute";
1248 node->type = XLAT_ATTRIBUTE;
1253 *error = "No matching closing brace";
1254 return -1; /* second character of format string */
1258 rad_assert(node->next == NULL);
1264 static ssize_t xlat_tokenize_literal(TALLOC_CTX *ctx, char *fmt, xlat_exp_t **head,
1265 bool brace, char const **error)
1270 if (!*fmt) return 0;
1272 XLAT_DEBUG("LITERAL <-- %s", fmt);
1274 node = talloc_zero(ctx, xlat_exp_t);
1277 node->type = XLAT_LITERAL;
1285 *error = "Invalid escape at end of string";
1293 * Process the expansion.
1295 if ((p[0] == '%') && (p[1] == '{')) {
1298 XLAT_DEBUG("EXPANSION-2 <-- %s", node->fmt);
1300 slen = xlat_tokenize_expansion(node, p, &node->next, error);
1303 return slen - (p - fmt);
1305 *p = '\0'; /* end the literal */
1308 rad_assert(node->next != NULL);
1311 * Short-circuit the recursive call.
1312 * This saves another function call and
1313 * memory allocation.
1318 * "foo %{User-Name} bar"
1320 * EXPANSION User-Name
1323 slen = xlat_tokenize_literal(node->next, p, &(node->next->next), brace, error);
1324 rad_assert(slen != 0);
1327 return slen - (p - fmt);
1330 brace = false; /* it was found above, or else the above code errored out */
1332 break; /* stop processing the string */
1336 * Check for valid single-character expansions.
1342 if (!p[1] || !strchr("%dlmntDGHISTYv", p[1])) {
1344 *error = "Invalid variable expansion";
1349 next = talloc_zero(node, xlat_exp_t);
1353 next->fmt = talloc_typed_strdup(next, "%");
1355 XLAT_DEBUG("LITERAL-PERCENT <-- %s", next->fmt);
1356 next->type = XLAT_LITERAL;
1361 XLAT_DEBUG("PERCENT <-- %c", *next->fmt);
1362 next->type = XLAT_PERCENT;
1374 slen = xlat_tokenize_literal(node->next, p, &(node->next->next), brace, error);
1375 rad_assert(slen != 0);
1378 return slen - (p - fmt);
1381 brace = false; /* it was found above, or else the above code errored out */
1383 break; /* stop processing the string */
1387 * If required, eat the brace.
1389 if (brace && (*p == '}')) {
1401 * We were told to look for a brace, but we ran off of
1402 * the end of the string before we found one.
1405 *error = "Missing closing brace at end of string";
1410 * Squash zero-width literals
1412 if (node->len > 0) {
1416 (void) talloc_steal(ctx, node->next);
1425 static char const xlat_tabs[] = " ";
1427 static void xlat_tokenize_debug(xlat_exp_t const *node, int lvl)
1429 rad_assert(node != NULL);
1431 if (lvl >= (int) sizeof(xlat_tabs)) lvl = sizeof(xlat_tabs);
1434 switch (node->type) {
1436 DEBUG("%.*sliteral --> %s", lvl, xlat_tabs, node->fmt);
1440 DEBUG("%.*spercent --> %c", lvl, xlat_tabs, node->fmt[0]);
1443 case XLAT_ATTRIBUTE:
1444 rad_assert(node->attr.tmpl_da != NULL);
1445 DEBUG("%.*sattribute --> %s", lvl, xlat_tabs, node->attr.tmpl_da->name);
1446 rad_assert(node->child == NULL);
1447 if ((node->attr.tmpl_tag != TAG_ANY) || (node->attr.tmpl_num != NUM_ANY)) {
1448 DEBUG("%.*s{", lvl, xlat_tabs);
1450 DEBUG("%.*sref %d", lvl + 1, xlat_tabs, node->attr.tmpl_request);
1451 DEBUG("%.*slist %d", lvl + 1, xlat_tabs, node->attr.tmpl_list);
1453 if (node->attr.tmpl_tag != TAG_ANY) {
1454 DEBUG("%.*stag %d", lvl + 1, xlat_tabs, node->attr.tmpl_tag);
1456 if (node->attr.tmpl_num != NUM_ANY) {
1457 if (node->attr.tmpl_num == NUM_COUNT) {
1458 DEBUG("%.*s[#]", lvl + 1, xlat_tabs);
1459 } else if (node->attr.tmpl_num == NUM_ALL) {
1460 DEBUG("%.*s[*]", lvl + 1, xlat_tabs);
1462 DEBUG("%.*s[%d]", lvl + 1, xlat_tabs, node->attr.tmpl_num);
1466 DEBUG("%.*s}", lvl, xlat_tabs);
1471 rad_assert(node->fmt != NULL);
1472 DEBUG("%.*svirtual --> %s", lvl, xlat_tabs, node->fmt);
1476 rad_assert(node->xlat != NULL);
1477 DEBUG("%.*sxlat --> %s", lvl, xlat_tabs, node->xlat->name);
1479 DEBUG("%.*s{", lvl, xlat_tabs);
1480 xlat_tokenize_debug(node->child, lvl + 1);
1481 DEBUG("%.*s}", lvl, xlat_tabs);
1487 DEBUG("%.*sregex-var --> %d", lvl, xlat_tabs, node->attr.tmpl_num);
1491 case XLAT_ALTERNATE:
1492 DEBUG("%.*sif {", lvl, xlat_tabs);
1493 xlat_tokenize_debug(node->child, lvl + 1);
1494 DEBUG("%.*s}", lvl, xlat_tabs);
1495 DEBUG("%.*selse {", lvl, xlat_tabs);
1496 xlat_tokenize_debug(node->alternate, lvl + 1);
1497 DEBUG("%.*s}", lvl, xlat_tabs);
1504 size_t xlat_sprint(char *buffer, size_t bufsize, xlat_exp_t const *node)
1515 end = buffer + bufsize;
1518 switch (node->type) {
1520 strlcpy(p, node->fmt, end - p);
1526 p[1] = node->fmt[0];
1530 case XLAT_ATTRIBUTE:
1534 if (node->attr.tmpl_request != REQUEST_CURRENT) {
1535 strlcpy(p, fr_int2str(request_refs, node->attr.tmpl_request, "??"), end - p);
1540 if ((node->attr.tmpl_request != REQUEST_CURRENT) ||
1541 (node->attr.tmpl_list != PAIR_LIST_REQUEST)) {
1542 strlcpy(p, fr_int2str(pair_lists, node->attr.tmpl_list, "??"), end - p);
1547 strlcpy(p, node->attr.tmpl_da->name, end - p);
1550 if (node->attr.tmpl_tag != TAG_ANY) {
1552 snprintf(p, end - p, "%u", node->attr.tmpl_tag);
1556 if (node->attr.tmpl_num != NUM_ANY) {
1558 switch (node->attr.tmpl_num) {
1568 snprintf(p, end - p, "%i", node->attr.tmpl_num);
1577 snprintf(p, end - p, "%%{%i}", node->attr.tmpl_num);
1584 strlcpy(p, node->fmt, end - p);
1592 strlcpy(p, node->xlat->name, end - p);
1595 rad_assert(node->child != NULL);
1596 len = xlat_sprint(p, end - p, node->child);
1601 case XLAT_ALTERNATE:
1605 len = xlat_sprint(p, end - p, node->child);
1611 len = xlat_sprint(p, end - p, node->alternate);
1619 if (p == end) break;
1629 ssize_t xlat_tokenize(TALLOC_CTX *ctx, char *fmt, xlat_exp_t **head,
1632 return xlat_tokenize_literal(ctx, fmt, head, false, error);
1636 /** Tokenize an xlat expansion
1638 * @param[in] request the input request. Memory will be attached here.
1639 * @param[in] fmt the format string to expand
1640 * @param[out] head the head of the xlat list / tree structure.
1642 static ssize_t xlat_tokenize_request(REQUEST *request, char const *fmt, xlat_exp_t **head)
1646 char const *error = NULL;
1651 * Copy the original format string to a buffer so that
1652 * the later functions can mangle it in-place, which is
1655 tokens = talloc_typed_strdup(request, fmt);
1656 if (!tokens) return -1;
1658 slen = xlat_tokenize_literal(request, tokens, head, false, &error);
1661 * Zero length expansion, return a zero length node.
1664 *head = talloc_zero(request, xlat_exp_t);
1668 * Output something like:
1671 * " ^ error was here"
1674 talloc_free(tokens);
1675 rad_assert(error != NULL);
1677 REMARKER(fmt, -slen, error);
1681 if (*head && (debug_flag > 2)) {
1683 DEBUG("Parsed xlat tree:");
1684 xlat_tokenize_debug(*head, 0);
1688 * All of the nodes point to offsets in the "tokens"
1689 * string. Let's ensure that free'ing head will free
1692 (void) talloc_steal(*head, tokens);
1698 static char *xlat_getvp(TALLOC_CTX *ctx, REQUEST *request, value_pair_tmpl_t const *vpt,
1699 bool escape, bool return_null)
1701 VALUE_PAIR *vp = NULL, *virtual = NULL;
1702 RADIUS_PACKET *packet = NULL;
1707 char quote = escape ? '"' : '\0';
1712 * See if we're dealing with an attribute in the request
1714 * This allows users to manipulate virtual attributes as if
1715 * they were real ones.
1717 vp = tmpl_cursor_init(&err, &cursor, request, vpt);
1718 if (vp) goto do_print;
1721 * We didn't find the VP in a list.
1722 * If it's not a virtual one, and we're not meant to
1723 * be counting it, return.
1725 if (!vpt->tmpl_da->flags.virtual) {
1726 if (vpt->tmpl_num == NUM_COUNT) goto do_print;
1731 * Switch out the request to the one specified by the template
1733 if (radius_request(&request, vpt->tmpl_request) < 0) return NULL;
1736 * Some non-packet expansions
1738 switch (vpt->tmpl_da->attr) {
1740 break; /* ignore them */
1742 case PW_CLIENT_SHORTNAME:
1743 if (vpt->tmpl_num == NUM_COUNT) goto count_virtual;
1744 if (request->client && request->client->shortname) {
1745 return talloc_typed_strdup(ctx, request->client->shortname);
1747 return talloc_typed_strdup(ctx, "<UNKNOWN-CLIENT>");
1749 case PW_REQUEST_PROCESSING_STAGE:
1750 if (vpt->tmpl_num == NUM_COUNT) goto count_virtual;
1751 if (request->component) {
1752 return talloc_typed_strdup(ctx, request->component);
1754 return talloc_typed_strdup(ctx, "server_core");
1756 case PW_VIRTUAL_SERVER:
1757 if (vpt->tmpl_num == NUM_COUNT) goto count_virtual;
1758 if (!request->server) return NULL;
1759 return talloc_typed_strdup(ctx, request->server);
1761 case PW_MODULE_RETURN_CODE:
1762 if (vpt->tmpl_num == NUM_COUNT) goto count_virtual;
1763 if (!request->rcode) return NULL;
1764 return talloc_typed_strdup(ctx, fr_int2str(modreturn_table, request->rcode, ""));
1768 * All of the attributes must now refer to a packet.
1769 * If there's no packet, we can't print any attribute
1772 packet = radius_packet(request, vpt->tmpl_list);
1774 if (return_null) return NULL;
1775 return vp_aprints_type(ctx, vpt->tmpl_da->type);
1779 switch (vpt->tmpl_da->attr) {
1783 case PW_PACKET_TYPE:
1784 dv = dict_valbyattr(PW_PACKET_TYPE, 0, packet->code);
1785 if (dv) return talloc_typed_strdup(ctx, dv->name);
1786 return talloc_typed_asprintf(ctx, "%d", packet->code);
1788 case PW_RESPONSE_PACKET_TYPE:
1793 if (request->proxy_reply && (!request->reply || !request->reply->code)) {
1794 code = request->proxy_reply->code;
1797 if (request->reply) {
1798 code = request->reply->code;
1801 return talloc_typed_strdup(ctx, fr_packet_codes[code]);
1805 * Virtual attributes which require a temporary VALUE_PAIR
1806 * to be allocated. We can't use stack allocated memory
1807 * because of the talloc checks sprinkled throughout the
1808 * various VP functions.
1810 case PW_PACKET_AUTHENTICATION_VECTOR:
1811 virtual = pairalloc(ctx, vpt->tmpl_da);
1812 pairmemcpy(virtual, packet->vector, sizeof(packet->vector));
1816 case PW_CLIENT_IP_ADDRESS:
1817 case PW_PACKET_SRC_IP_ADDRESS:
1818 if (packet->src_ipaddr.af == AF_INET) {
1819 virtual = pairalloc(ctx, vpt->tmpl_da);
1820 virtual->vp_ipaddr = packet->src_ipaddr.ipaddr.ip4addr.s_addr;
1825 case PW_PACKET_DST_IP_ADDRESS:
1826 if (packet->dst_ipaddr.af == AF_INET) {
1827 virtual = pairalloc(ctx, vpt->tmpl_da);
1828 virtual->vp_ipaddr = packet->dst_ipaddr.ipaddr.ip4addr.s_addr;
1833 case PW_PACKET_SRC_IPV6_ADDRESS:
1834 if (packet->src_ipaddr.af == AF_INET6) {
1835 virtual = pairalloc(ctx, vpt->tmpl_da);
1836 memcpy(&virtual->vp_ipv6addr,
1837 &packet->src_ipaddr.ipaddr.ip6addr,
1838 sizeof(packet->src_ipaddr.ipaddr.ip6addr));
1843 case PW_PACKET_DST_IPV6_ADDRESS:
1844 if (packet->dst_ipaddr.af == AF_INET6) {
1845 virtual = pairalloc(ctx, vpt->tmpl_da);
1846 memcpy(&virtual->vp_ipv6addr,
1847 &packet->dst_ipaddr.ipaddr.ip6addr,
1848 sizeof(packet->dst_ipaddr.ipaddr.ip6addr));
1853 case PW_PACKET_SRC_PORT:
1854 virtual = pairalloc(ctx, vpt->tmpl_da);
1855 virtual->vp_integer = packet->src_port;
1859 case PW_PACKET_DST_PORT:
1860 virtual = pairalloc(ctx, vpt->tmpl_da);
1861 virtual->vp_integer = packet->dst_port;
1867 * Fake various operations for virtual attributes.
1870 if (vpt->tmpl_num != NUM_ANY) switch (vpt->tmpl_num) {
1872 * [n] is NULL (we only have [0])
1877 * [*] means only one.
1883 * [#] means 1 (as there's only one)
1887 ret = talloc_strdup(ctx, "1");
1891 * [0] is fine (get the first instance)
1900 switch (vpt->tmpl_num) {
1902 * Return a count of the VPs.
1908 fr_cursor_first(&cursor);
1909 while (fr_cursor_next_by_da(&cursor, vpt->tmpl_da, vpt->tmpl_tag)) count++;
1911 return talloc_typed_asprintf(ctx, "%d", count);
1916 * Concatenate all values together,
1917 * separated by commas.
1923 if (!fr_cursor_current(&cursor)) return NULL;
1924 p = vp_aprints_value(ctx, vp, quote);
1925 if (!p) return NULL;
1927 while ((vp = tmpl_cursor_next(&cursor, vpt)) != NULL) {
1928 q = vp_aprints_value(ctx, vp, quote);
1929 if (!q) return NULL;
1930 p = talloc_strdup_append(p, ",");
1931 p = talloc_strdup_append(p, q);
1939 * The cursor was set to the correct
1940 * position above by tmpl_cursor_init.
1942 vp = fr_cursor_current(&cursor);
1947 if (return_null) return NULL;
1948 return vp_aprints_type(ctx, vpt->tmpl_da->type);
1952 ret = vp_aprints_value(ctx, vp, quote);
1955 talloc_free(virtual);
1960 static const char xlat_spaces[] = " ";
1963 static char *xlat_aprint(TALLOC_CTX *ctx, REQUEST *request, xlat_exp_t const * const node,
1964 RADIUS_ESCAPE_STRING escape, void *escape_ctx, int lvl)
1967 char *str = NULL, *child;
1970 XLAT_DEBUG("%.*sxlat aprint %d", lvl, xlat_spaces, node->type);
1972 switch (node->type) {
1974 * Don't escape this.
1977 XLAT_DEBUG("xlat_aprint LITERAL");
1978 return talloc_typed_strdup(ctx, node->fmt);
1981 * Do a one-character expansion.
1986 size_t freespace = 256;
1990 XLAT_DEBUG("xlat_aprint PERCENT");
1992 str = talloc_array(ctx, char, freespace); /* @todo do better allocation */
1995 when = request->timestamp;
1996 if (request->packet) {
1997 when = request->packet->timestamp.tv_sec;
2006 case 'd': /* request day */
2007 if (!localtime_r(&when, &ts)) goto error;
2008 strftime(str, freespace, "%d", &ts);
2011 case 'l': /* request timestamp */
2012 snprintf(str, freespace, "%lu",
2013 (unsigned long) when);
2016 case 'm': /* request month */
2017 if (!localtime_r(&when, &ts)) goto error;
2018 strftime(str, freespace, "%m", &ts);
2021 case 'n': /* Request Number*/
2022 snprintf(str, freespace, "%u", request->number);
2025 case 't': /* request timestamp */
2026 CTIME_R(&when, str, freespace);
2027 nl = strchr(str, '\n');
2031 case 'D': /* request date */
2032 if (!localtime_r(&when, &ts)) goto error;
2033 strftime(str, freespace, "%Y%m%d", &ts);
2036 case 'G': /* request minute */
2037 if (!localtime_r(&when, &ts)) goto error;
2038 strftime(str, freespace, "%M", &ts);
2041 case 'H': /* request hour */
2042 if (!localtime_r(&when, &ts)) goto error;
2043 strftime(str, freespace, "%H", &ts);
2046 case 'I': /* Request ID */
2047 if (request->packet) {
2048 snprintf(str, freespace, "%i", request->packet->id);
2052 case 'S': /* request timestamp in SQL format*/
2053 if (!localtime_r(&when, &ts)) goto error;
2054 strftime(str, freespace, "%Y-%m-%d %H:%M:%S", &ts);
2057 case 'T': /* request timestamp */
2058 if (!localtime_r(&when, &ts)) goto error;
2059 strftime(str, freespace, "%Y-%m-%d-%H.%M.%S.000000", &ts);
2062 case 'Y': /* request year */
2063 if (!localtime_r(&when, &ts)) {
2065 REDEBUG("Failed converting packet timestamp to localtime: %s", fr_syserror(errno));
2069 strftime(str, freespace, "%Y", &ts);
2072 case 'v': /* Version of code */
2073 snprintf(str, freespace, "%s", radiusd_short_version);
2083 case XLAT_ATTRIBUTE:
2084 XLAT_DEBUG("xlat_aprint ATTRIBUTE");
2087 * Some attributes are virtual <sigh>
2089 str = xlat_getvp(ctx, request, &node->attr, escape ? false : true, true);
2091 XLAT_DEBUG("EXPAND attr %s", node->attr.tmpl_da->name);
2092 XLAT_DEBUG(" ---> %s", str);
2097 XLAT_DEBUG("xlat_aprint VIRTUAL");
2098 str = talloc_array(ctx, char, 2048); /* FIXME: have the module call talloc_typed_asprintf */
2099 rcode = node->xlat->func(node->xlat->instance, request, NULL, str, 2048);
2104 RDEBUG2("EXPAND %s", node->xlat->name);
2105 RDEBUG2(" --> %s", str);
2109 XLAT_DEBUG("xlat_aprint MODULE");
2110 if (xlat_process(&child, request, node->child, node->xlat->escape, node->xlat->instance) == 0) {
2114 XLAT_DEBUG("%.*sEXPAND mod %s %s", lvl, xlat_spaces, node->fmt, node->child->fmt);
2115 XLAT_DEBUG("%.*s ---> %s", lvl, xlat_spaces, child);
2120 * The OUTPUT of xlat is a printable string. The INPUT might not be...
2122 * This is really the reverse of fr_prints().
2124 if (cf_new_escape && *child) {
2129 type = PW_TYPE_STRING;
2130 slen = value_data_from_str(request, &data, &type, NULL, child, talloc_array_length(child) - 1, '"');
2131 rad_assert(slen > 0);
2141 if (*p == '\\') switch (p[1]) {
2163 str = talloc_array(ctx, char, 2048); /* FIXME: have the module call talloc_typed_asprintf */
2164 *str = '\0'; /* Be sure the string is NULL terminated, we now only free on error */
2166 rcode = node->xlat->func(node->xlat->instance, request, child, str, 2048);
2176 XLAT_DEBUG("xlat_aprint REGEX");
2177 if (regex_request_to_sub(ctx, &str, request, node->attr.tmpl_num) < 0) return NULL;
2182 case XLAT_ALTERNATE:
2183 XLAT_DEBUG("xlat_aprint ALTERNATE");
2184 rad_assert(node->child != NULL);
2185 rad_assert(node->alternate != NULL);
2187 str = xlat_aprint(ctx, request, node->child, escape, escape_ctx, lvl);
2190 str = xlat_aprint(ctx, request, node->alternate, escape, escape_ctx, lvl);
2196 * Escape the non-literals we found above.
2198 if (str && escape) {
2201 escaped = talloc_array(ctx, char, 2048); /* FIXME: do something intelligent */
2202 escape(request, escaped, 2038, str, escape_ctx);
2211 static size_t xlat_process(char **out, REQUEST *request, xlat_exp_t const * const head,
2212 RADIUS_ESCAPE_STRING escape, void *escape_ctx)
2216 char **array, *answer;
2217 xlat_exp_t const *node;
2222 * There are no nodes to process, so the result is a zero
2226 *out = talloc_zero_array(request, char, 1);
2231 * Hack for speed. If it's one expansion, just allocate
2232 * that and return, instead of allocating an intermediary
2237 * Pass the MAIN escape function. Recursive
2238 * calls will call node-specific escape
2241 answer = xlat_aprint(request, request, head, escape, escape_ctx, 0);
2243 *out = talloc_zero_array(request, char, 1);
2247 return strlen(answer);
2250 list = 0; /* FIXME: calculate this once */
2251 for (node = head; node != NULL; node = node->next) {
2255 array = talloc_array(request, char *, list);
2256 if (!array) return -1;
2258 for (node = head, i = 0; node != NULL; node = node->next, i++) {
2259 array[i] = xlat_aprint(array, request, node, escape, escape_ctx, 0); /* may be NULL */
2263 for (i = 0; i < list; i++) {
2264 if (array[i]) total += strlen(array[i]); /* FIXME: calculate strlen once */
2269 *out = talloc_zero_array(request, char, 1);
2273 answer = talloc_array(request, char, total + 1);
2276 for (i = 0; i < list; i++) {
2280 len = strlen(array[i]);
2281 memcpy(answer + total, array[i], len);
2285 answer[total] = '\0';
2286 talloc_free(array); /* and child entries */
2293 /** Replace %whatever in a string.
2295 * See 'doc/variables.txt' for more information.
2297 * @param[out] out Where to write pointer to output buffer.
2298 * @param[in] outlen Size of out.
2299 * @param[in] request current request.
2300 * @param[in] node the xlat structure to expand
2301 * @param[in] escape function to escape final value e.g. SQL quoting.
2302 * @param[in] escape_ctx pointer to pass to escape function.
2303 * @return length of string written @bug should really have -1 for failure
2305 static ssize_t xlat_expand_struct(char **out, size_t outlen, REQUEST *request, xlat_exp_t const *node,
2306 RADIUS_ESCAPE_STRING escape, void *escape_ctx)
2311 rad_assert(node != NULL);
2313 len = xlat_process(&buff, request, node, escape, escape_ctx);
2314 if ((len < 0) || !buff) {
2315 rad_assert(buff == NULL);
2316 if (*out) *out[0] = '\0';
2323 strlcpy(*out, buff, outlen);
2327 return strlen(*out);
2330 static ssize_t xlat_expand(char **out, size_t outlen, REQUEST *request, char const *fmt,
2331 RADIUS_ESCAPE_STRING escape, void *escape_ctx) CC_HINT(nonnull (1, 3, 4));
2333 /** Replace %whatever in a string.
2335 * See 'doc/variables.txt' for more information.
2337 * @param[out] out Where to write pointer to output buffer.
2338 * @param[in] outlen Size of out.
2339 * @param[in] request current request.
2340 * @param[in] fmt string to expand.
2341 * @param[in] escape function to escape final value e.g. SQL quoting.
2342 * @param[in] escape_ctx pointer to pass to escape function.
2343 * @return length of string written @bug should really have -1 for failure
2345 static ssize_t xlat_expand(char **out, size_t outlen, REQUEST *request, char const *fmt,
2346 RADIUS_ESCAPE_STRING escape, void *escape_ctx)
2352 * Give better errors than the old code.
2354 len = xlat_tokenize_request(request, fmt, &node);
2359 *out = talloc_zero_array(request, char, 1);
2365 if (*out) *out[0] = '\0';
2369 len = xlat_expand_struct(out, outlen, request, node, escape, escape_ctx);
2372 RDEBUG2("EXPAND %s", fmt);
2373 RDEBUG2(" --> %s", *out);
2378 /** Try to convert an xlat to a tmpl for efficiency
2380 * @param ctx to allocate new value_pair_tmpl_t in.
2381 * @param node to convert.
2382 * @return NULL if unable to convert (not necessarily error), or a new value_pair_tmpl_t.
2384 value_pair_tmpl_t *xlat_to_tmpl_attr(TALLOC_CTX *ctx, xlat_exp_t *node)
2386 value_pair_tmpl_t *vpt;
2388 if (node->next || (node->type != XLAT_ATTRIBUTE)) return NULL;
2391 * Concat means something completely different as an attribute reference
2392 * Count isn't implemented.
2394 if ((node->attr.tmpl_num == NUM_COUNT) || (node->attr.tmpl_num == NUM_ALL)) return NULL;
2396 vpt = tmpl_alloc(ctx, TMPL_TYPE_ATTR, node->fmt, -1);
2397 if (!vpt) return NULL;
2398 memcpy(&vpt->data, &node->attr.data, sizeof(vpt->data));
2405 /** Try to convert attr tmpl to an xlat for &attr[*] and artificially constructing expansions
2407 * @param ctx to allocate new xlat_expt_t in.
2408 * @param vpt to convert.
2409 * @return NULL if unable to convert (not necessarily error), or a new value_pair_tmpl_t.
2411 xlat_exp_t *xlat_from_tmpl_attr(TALLOC_CTX *ctx, value_pair_tmpl_t *vpt)
2415 if (vpt->type != TMPL_TYPE_ATTR) return NULL;
2417 node = talloc_zero(ctx, xlat_exp_t);
2418 node->fmt = talloc_memdup(node, vpt->name, vpt->len);
2419 tmpl_init(&node->attr, TMPL_TYPE_ATTR, node->fmt, talloc_array_length(node->fmt) - 1);
2420 memcpy(&node->attr.data, &vpt->data, sizeof(vpt->data));
2425 ssize_t radius_xlat(char *out, size_t outlen, REQUEST *request, char const *fmt, RADIUS_ESCAPE_STRING escape, void *ctx)
2427 return xlat_expand(&out, outlen, request, fmt, escape, ctx);
2430 ssize_t radius_axlat(char **out, REQUEST *request, char const *fmt, RADIUS_ESCAPE_STRING escape, void *ctx)
2432 return xlat_expand(out, 0, request, fmt, escape, ctx);
2435 ssize_t radius_axlat_struct(char **out, REQUEST *request, xlat_exp_t const *xlat, RADIUS_ESCAPE_STRING escape, void *ctx)
2437 return xlat_expand_struct(out, 0, request, xlat, escape, ctx);