2 * xlat.c Translate strings. This is the first version of xlat
3 * incorporated to RADIUS
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * Copyright 2000 The FreeRADIUS server project
22 * Copyright 2000 Alan DeKok <aland@ox.org>
25 static const char rcsid[] =
29 #include "libradius.h"
38 #include "rad_assert.h"
40 typedef struct xlat_t {
41 char module[MAX_STRING_LEN];
44 RAD_XLAT_FUNC do_xlat;
45 int internal; /* not allowed to re-define these */
48 static rbtree_t *xlat_root = NULL;
51 * Define all xlat's in the structure.
53 static const char *internal_xlat[] = {"check",
60 #if REQUEST_MAX_REGEX > 8
61 #error Please fix the following line
63 static const int xlat_inst[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 }; /* up to 8 for regex */
67 * Convert the value on a VALUE_PAIR to string
69 static int valuepair2str(char * out,int outlen,VALUE_PAIR * pair,
70 int type, RADIUS_ESCAPE_STRING func)
72 char buffer[MAX_STRING_LEN * 4];
75 vp_prints_value(buffer, sizeof(buffer), pair, -1);
76 return func(out, outlen, buffer);
81 strNcpy(out,"_",outlen);
83 case PW_TYPE_INTEGER :
84 strNcpy(out,"0",outlen);
87 strNcpy(out,"?.?.?.?",outlen);
90 strNcpy(out,"0",outlen);
93 strNcpy(out,"unknown_type",outlen);
100 * Dynamically translate for check:, request:, reply:, etc.
102 static int xlat_packet(void *instance, REQUEST *request,
103 char *fmt, char *out, size_t outlen,
104 RADIUS_ESCAPE_STRING func)
108 VALUE_PAIR *vps = NULL;
109 RADIUS_PACKET *packet = NULL;
111 switch (*(int*) instance) {
113 vps = request->config_items;
117 vps = request->packet->vps;
118 packet = request->packet;
122 vps = request->reply->vps;
123 packet = request->reply;
127 if (request->proxy) vps = request->proxy->vps;
128 packet = request->proxy;
132 if (request->proxy_reply) vps = request->proxy_reply->vps;
133 packet = request->proxy_reply;
141 * The "format" string is the attribute name.
143 da = dict_attrbyname(fmt);
146 const char *p = strchr(fmt, '[');
150 if (strlen(fmt) > sizeof(buffer)) return 0;
152 strNcpy(buffer, fmt, p - fmt + 1);
154 da = dict_attrbyname(buffer);
158 * %{Attribute-Name[#]} returns the count of
159 * attributes of that name in the list.
161 if ((p[1] == '#') && (p[2] == ']')) {
164 for (vp = pairfind(vps, da->attr);
166 vp = pairfind(vp->next, da->attr)) {
169 snprintf(out, outlen, "%d", index);
174 * %{Attribute-Name[*]} returns ALL of the
175 * the attributes, separated by a newline.
177 if ((p[1] == '*') && (p[2] == ']')) {
180 for (vp = pairfind(vps, da->attr);
182 vp = pairfind(vp->next, da->attr)) {
183 index = valuepair2str(out, outlen - 1, vp, da->type, func);
184 rad_assert(index <= outlen);
186 outlen -= (index + 1);
191 if (outlen == 0) break;
202 p += 1 + strspn(p + 1, "0123456789");
204 DEBUG2("xlat: Invalid array reference in string at %s %s",
210 * Find the N'th value.
212 for (vp = pairfind(vps, da->attr);
214 vp = pairfind(vp->next, da->attr)) {
215 if (index == 0) break;
220 * Non-existent array reference.
224 return valuepair2str(out, outlen, vp, da->type, func);
227 vp = pairfind(vps, da->attr);
230 * Some "magic" handlers, which are never in VP's, but
231 * which are in the packet.
236 localvp.strvalue[0] = 0;
243 dval = dict_valbyattr(da->attr, packet->code);
245 snprintf(out, outlen, "%s", dval->name);
247 snprintf(out, outlen, "%d", packet->code);
253 case PW_PACKET_SRC_IP_ADDRESS:
254 localvp.attribute = da->attr;
255 localvp.lvalue = packet->src_ipaddr;
258 case PW_PACKET_DST_IP_ADDRESS:
259 localvp.attribute = da->attr;
260 localvp.lvalue = packet->dst_ipaddr;
263 case PW_PACKET_SRC_PORT:
264 localvp.attribute = da->attr;
265 localvp.lvalue = packet->src_port;
268 case PW_PACKET_DST_PORT:
269 localvp.attribute = da->attr;
270 localvp.lvalue = packet->dst_port;
273 case PW_PACKET_AUTHENTICATION_VECTOR:
274 localvp.attribute = da->attr;
275 memcpy(localvp.strvalue, packet->vector,
276 sizeof(packet->vector));
277 localvp.length = sizeof(packet->vector);
281 * Authorization, accounting, etc.
283 case PW_REQUEST_PROCESSING_STAGE:
284 if (request->component) {
285 strNcpy(out, request->component, outlen);
287 strNcpy(out, "server_core", outlen);
292 return 0; /* not found */
296 localvp.type = da->type;
297 return valuepair2str(out, outlen, &localvp,
307 if (!vps) return 0; /* silently fail */
310 * Convert the VP to a string, and return it.
312 return valuepair2str(out, outlen, vp, da->type, func);
317 * Pull %{0} to %{8} out of the packet.
319 static int xlat_regex(void *instance, REQUEST *request,
320 char *fmt, char *out, size_t outlen,
321 RADIUS_ESCAPE_STRING func)
326 * We cheat: fmt is "0" to "8", but those numbers
327 * are already in the "instance".
329 fmt = fmt; /* -Wunused */
330 func = func; /* -Wunused FIXME: do escaping? */
332 regex = request_data_get(request, request,
333 REQUEST_DATA_REGEX | *(int *)instance);
334 if (!regex) return 0;
337 * Copy UP TO "freespace" bytes, including
340 strNcpy(out, regex, outlen);
341 free(regex); /* was strdup'd */
344 #endif /* HAVE_REGEX_H */
347 * Compare two xlat_t structs, based ONLY on the module name.
349 static int xlat_cmp(const void *a, const void *b)
351 if (((const xlat_t *)a)->length != ((const xlat_t *)b)->length) {
352 return ((const xlat_t *)a)->length - ((const xlat_t *)b)->length;
355 return memcmp(((const xlat_t *)a)->module,
356 ((const xlat_t *)b)->module,
357 ((const xlat_t *)a)->length);
362 * find the appropriate registered xlat function.
364 static const xlat_t *xlat_find(const char *module)
369 * Look for dictionary attributes first.
371 if (dict_attrbyname(module) != NULL) {
372 static const xlat_t dict_xlat = {
383 strNcpy(my_xlat.module, module, sizeof(my_xlat.module));
384 my_xlat.length = strlen(my_xlat.module);
386 return rbtree_finddata(xlat_root, &my_xlat);
391 * Register an xlat function.
393 int xlat_register(const char *module, RAD_XLAT_FUNC func, void *instance)
398 if ((module == NULL) || (strlen(module) == 0)) {
399 DEBUG("xlat_register: Invalid module name");
404 * First time around, build up the tree...
406 * FIXME: This code should be hoisted out of this function,
407 * and into a global "initialization". But it isn't critical...
415 xlat_root = rbtree_create(xlat_cmp, free, 0);
417 DEBUG("xlat_register: Failed to create tree.");
422 * Register the internal packet xlat's.
424 for (i = 0; internal_xlat[i] != NULL; i++) {
425 xlat_register(internal_xlat[i], xlat_packet, &xlat_inst[i]);
426 c = xlat_find(internal_xlat[i]);
427 rad_assert(c != NULL);
433 * Register xlat's for regexes.
436 for (i = 0; i <= REQUEST_MAX_REGEX; i++) {
438 xlat_register(buffer, xlat_regex, &xlat_inst[i]);
439 c = xlat_find(buffer);
440 rad_assert(c != NULL);
443 #endif /* HAVE_REGEX_H */
447 * If it already exists, replace the instance.
449 strNcpy(my_xlat.module, module, sizeof(my_xlat.module));
450 my_xlat.length = strlen(my_xlat.module);
451 c = rbtree_finddata(xlat_root, &my_xlat);
454 DEBUG("xlat_register: Cannot re-define internal xlat");
459 c->instance = instance;
464 * Doesn't exist. Create it.
466 c = rad_malloc(sizeof(xlat_t));
467 memset(c, 0, sizeof(*c));
470 strNcpy(c->module, module, sizeof(c->module));
471 c->length = strlen(c->module);
472 c->instance = instance;
474 rbtree_insert(xlat_root, c);
480 * Unregister an xlat function.
482 * We can only have one function to call per name, so the
483 * passing of "func" here is extraneous.
485 void xlat_unregister(const char *module, RAD_XLAT_FUNC func)
490 func = func; /* -Wunused */
492 strNcpy(my_xlat.module, module, sizeof(my_xlat.module));
493 my_xlat.length = strlen(my_xlat.module);
495 node = rbtree_find(xlat_root, &my_xlat);
498 rbtree_delete(xlat_root, node);
502 * De-register all xlat functions,
503 * used mainly for debugging.
507 rbtree_free(xlat_root);
512 * Decode an attribute name into a string.
514 static void decode_attribute(const char **from, char **to, int freespace,
515 int *open, REQUEST *request,
516 RADIUS_ESCAPE_STRING func)
520 char *xlat_string = NULL; /* can be large */
521 int free_xlat_string = FALSE;
524 int found=0, retlen=0;
525 int openbraces = *open;
535 * Skip the '{' at the front of 'p'
536 * Increment open braces
547 * First, copy the xlat key name to one buffer
549 while (*p && (*p != '}') && (*p != ':')) {
552 if (pa >= (xlat_name + sizeof(xlat_name) - 1)) {
554 * Skip to the end of the input
557 DEBUG("xlat: Module name is too long in string %%%s",
565 DEBUG("xlat: Invalid syntax in %s", *from);
568 * %{name} is a simple attribute reference,
569 * or regex reference.
571 } else if (*p == '}') {
573 rad_assert(openbraces == *open);
576 xlat_string = xlat_name;
579 } else if (p[1] == '-') { /* handle ':- */
581 xlat_string = xlat_name;
584 } else { /* module name, followed by per-module string */
586 int delimitbrace = *open;
588 rad_assert(*p == ':');
589 p++; /* skip the ':' */
592 * If there's a brace immediately following the colon,
593 * then we've chosen to delimite the per-module string,
594 * so keep track of that.
597 delimitbrace = openbraces;
602 xlat_string = rad_malloc(strlen(p) + 1); /* always returns */
603 free_xlat_string = TRUE;
607 * Copy over the rest of the string, which is per-module
610 while (*p && !stop) {
613 * What the heck is this supposed
622 * This is pretty hokey... we
623 * should use the functions in
633 if (openbraces == delimitbrace) {
650 * Now check to see if we're at the end of the string
651 * we were sent. If we're not, check for :-
653 if (openbraces == delimitbrace) {
654 if (p[0] == ':' && p[1] == '-') {
660 * Look up almost everything in the new tree of xlat
661 * functions. This makes it a little quicker...
664 if ((c = xlat_find(xlat_name)) != NULL) {
665 if (!c->internal) DEBUG("radius_xlat: Running registered xlat function of module %s for string \'%s\'",
666 c->module, xlat_string);
667 retlen = c->do_xlat(c->instance, request, xlat_string,
669 /* If retlen is 0, treat it as not found */
670 if (retlen > 0) found = 1;
674 * No attribute by that name, return an error.
676 DEBUG2("WARNING: Unknown module \"%s\" in string expansion \"%%%s\"", xlat_name, *from);
682 * Skip to last '}' if attr is found
683 * The rest of the stuff within the braces is
684 * useless if we found what we need
688 snprintf(q, freespace, "%d", retlen);
694 while((*p != '\0') && (openbraces > 0)) {
696 * Handle escapes outside of the loop.
701 p++; /* get & ignore next character */
720 p++; /* skip the character */
725 if (free_xlat_string) free(xlat_string);
733 * If the caller doesn't pass xlat an escape function, then
734 * we use this one. It simplifies the coding, as the check for
735 * func == NULL only happens once.
737 static int xlat_copy(char *out, int outlen, const char *in)
743 * Truncate, if too much.
752 * FIXME: Do escaping of bad stuff!
766 * Replace %<whatever> in a string.
768 * See 'doc/variables.txt' for more information.
770 int radius_xlat(char *out, int outlen, const char *fmt,
771 REQUEST *request, RADIUS_ESCAPE_STRING func)
778 char tmpdt[40]; /* For temporary storing of dates */
784 if (!fmt || !out || !request) return 0;
787 * Ensure that we always have an escaping function.
796 /* Calculate freespace in output */
797 freespace = outlen - (q - out);
802 if ((c != '%') && (c != '$') && (c != '\\')) {
804 * We check if we're inside an open brace. If we are
805 * then we assume this brace is NOT literal, but is
806 * a closing brace and apply it
808 if ((c == '}') && openbraces) {
818 * There's nothing after this character, copy
819 * the last '%' or "$' or '\\' over to the output
846 * Hmmm... ${User-Name} is a synonym for
851 } else if (c == '$') switch(*p) {
852 case '{': /* Attribute by Name */
853 decode_attribute(&p, &q, freespace, &openbraces, request, func);
860 } else if (c == '%') switch(*p) {
862 decode_attribute(&p, &q, freespace, &openbraces, request, func);
868 case 'a': /* Protocol: */
869 q += valuepair2str(q,freespace,pairfind(request->reply->vps,PW_FRAMED_PROTOCOL),PW_TYPE_INTEGER, func);
872 case 'c': /* Callback-Number */
873 q += valuepair2str(q,freespace,pairfind(request->reply->vps,PW_CALLBACK_NUMBER),PW_TYPE_STRING, func);
876 case 'd': /* request day */
877 TM = localtime_r(&request->timestamp, &s_TM);
878 strftime(tmpdt,sizeof(tmpdt),"%d",TM);
879 strNcpy(q,tmpdt,freespace);
883 case 'f': /* Framed IP address */
884 q += valuepair2str(q,freespace,pairfind(request->reply->vps,PW_FRAMED_IP_ADDRESS),PW_TYPE_IPADDR, func);
887 case 'i': /* Calling station ID */
888 q += valuepair2str(q,freespace,pairfind(request->packet->vps,PW_CALLING_STATION_ID),PW_TYPE_STRING, func);
891 case 'l': /* request timestamp */
892 snprintf(tmpdt, sizeof(tmpdt), "%lu",
893 (unsigned long) request->timestamp);
894 strNcpy(q,tmpdt,freespace);
898 case 'm': /* request month */
899 TM = localtime_r(&request->timestamp, &s_TM);
900 strftime(tmpdt,sizeof(tmpdt),"%m",TM);
901 strNcpy(q,tmpdt,freespace);
905 case 'n': /* NAS IP address */
906 q += valuepair2str(q,freespace,pairfind(request->packet->vps,PW_NAS_IP_ADDRESS),PW_TYPE_IPADDR, func);
909 case 'p': /* Port number */
910 q += valuepair2str(q,freespace,pairfind(request->packet->vps,PW_NAS_PORT),PW_TYPE_INTEGER, func);
913 case 's': /* Speed */
914 q += valuepair2str(q,freespace,pairfind(request->packet->vps,PW_CONNECT_INFO),PW_TYPE_STRING, func);
917 case 't': /* request timestamp */
918 CTIME_R(&request->timestamp, q, freespace);
927 case 'u': /* User name */
928 q += valuepair2str(q,freespace,pairfind(request->packet->vps,PW_USER_NAME),PW_TYPE_STRING, func);
931 case 'A': /* radacct_dir */
932 strNcpy(q,radacct_dir,freespace-1);
936 case 'C': /* ClientName */
937 strNcpy(q,client_name(request->packet->src_ipaddr),freespace-1);
941 case 'D': /* request date */
942 TM = localtime_r(&request->timestamp, &s_TM);
943 strftime(tmpdt,sizeof(tmpdt),"%Y%m%d",TM);
944 strNcpy(q,tmpdt,freespace);
948 case 'H': /* request hour */
949 TM = localtime_r(&request->timestamp, &s_TM);
950 strftime(tmpdt,sizeof(tmpdt),"%H",TM);
951 strNcpy(q,tmpdt,freespace);
955 case 'L': /* radlog_dir */
956 strNcpy(q,radlog_dir,freespace-1);
961 q += valuepair2str(q,freespace,pairfind(request->reply->vps,PW_FRAMED_MTU),PW_TYPE_INTEGER, func);
964 case 'R': /* radius_dir */
965 strNcpy(q,radius_dir,freespace-1);
969 case 'S': /* request timestamp in SQL format*/
970 TM = localtime_r(&request->timestamp, &s_TM);
971 strftime(tmpdt,sizeof(tmpdt),"%Y-%m-%d %H:%M:%S",TM);
972 strNcpy(q,tmpdt,freespace);
976 case 'T': /* request timestamp */
977 TM = localtime_r(&request->timestamp, &s_TM);
978 strftime(tmpdt,sizeof(tmpdt),"%Y-%m-%d-%H.%M.%S.000000",TM);
979 strNcpy(q,tmpdt,freespace);
983 case 'U': /* Stripped User name */
984 q += valuepair2str(q,freespace,pairfind(request->packet->vps,PW_STRIPPED_USER_NAME),PW_TYPE_STRING, func);
987 case 'V': /* Request-Authenticator */
988 if (request->packet->verified)
989 strNcpy(q,"Verified",freespace-1);
991 strNcpy(q,"None",freespace-1);
995 case 'Y': /* request year */
996 TM = localtime_r(&request->timestamp, &s_TM);
997 strftime(tmpdt,sizeof(tmpdt),"%Y",TM);
998 strNcpy(q,tmpdt,freespace);
1002 case 'Z': /* Full request pairs except password */
1003 tmp = request->packet->vps;
1004 while (tmp && (freespace > 3)) {
1005 if (tmp->attribute != PW_PASSWORD) {
1007 i = vp_prints(q,freespace-2,tmp);
1017 DEBUG2("WARNING: Unknown variable '%%%c': See 'doc/variables.txt'", *p);
1018 if (freespace > 2) {
1027 DEBUG2("radius_xlat: '%s'", out);