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[] =
37 #include "rad_assert.h"
39 typedef struct xlat_t {
40 char module[MAX_STRING_LEN];
43 RAD_XLAT_FUNC do_xlat;
44 int internal; /* not allowed to re-define these */
47 static rbtree_t *xlat_root = NULL;
50 * Define all xlat's in the structure.
52 static const char * const internal_xlat[] = {"check",
59 #if REQUEST_MAX_REGEX > 8
60 #error Please fix the following line
62 static const int xlat_inst[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 }; /* up to 8 for regex */
66 * Convert the value on a VALUE_PAIR to string
68 static int valuepair2str(char * out,int outlen,VALUE_PAIR * pair,
69 int type, RADIUS_ESCAPE_STRING func)
71 char buffer[MAX_STRING_LEN * 4];
74 vp_prints_value(buffer, sizeof(buffer), pair, -1);
75 return func(out, outlen, buffer);
80 strNcpy(out,"_",outlen);
82 case PW_TYPE_INTEGER :
83 strNcpy(out,"0",outlen);
86 strNcpy(out,"?.?.?.?",outlen);
88 case PW_TYPE_IPV6ADDR :
89 strNcpy(out,":?:",outlen);
92 strNcpy(out,"0",outlen);
95 strNcpy(out,"unknown_type",outlen);
102 * Dynamically translate for check:, request:, reply:, etc.
104 static int xlat_packet(void *instance, REQUEST *request,
105 char *fmt, char *out, size_t outlen,
106 RADIUS_ESCAPE_STRING func)
110 VALUE_PAIR *vps = NULL;
111 RADIUS_PACKET *packet = NULL;
113 switch (*(int*) instance) {
115 vps = request->config_items;
119 vps = request->packet->vps;
120 packet = request->packet;
124 vps = request->reply->vps;
125 packet = request->reply;
129 if (request->proxy) vps = request->proxy->vps;
130 packet = request->proxy;
134 if (request->proxy_reply) vps = request->proxy_reply->vps;
135 packet = request->proxy_reply;
143 * The "format" string is the attribute name.
145 da = dict_attrbyname(fmt);
148 const char *p = strchr(fmt, '[');
152 if (strlen(fmt) > sizeof(buffer)) return 0;
154 strNcpy(buffer, fmt, p - fmt + 1);
156 da = dict_attrbyname(buffer);
160 * %{Attribute-Name[#]} returns the count of
161 * attributes of that name in the list.
163 if ((p[1] == '#') && (p[2] == ']')) {
166 for (vp = pairfind(vps, da->attr);
168 vp = pairfind(vp->next, da->attr)) {
171 snprintf(out, outlen, "%d", count);
176 * %{Attribute-Name[*]} returns ALL of the
177 * the attributes, separated by a newline.
179 if ((p[1] == '*') && (p[2] == ']')) {
182 for (vp = pairfind(vps, da->attr);
184 vp = pairfind(vp->next, da->attr)) {
185 count = valuepair2str(out, outlen - 1, vp, da->type, func);
186 rad_assert(count <= outlen);
188 outlen -= (count + 1);
193 if (outlen == 0) break;
204 p += 1 + strspn(p + 1, "0123456789");
206 DEBUG2("xlat: Invalid array reference in string at %s %s",
212 * Find the N'th value.
214 for (vp = pairfind(vps, da->attr);
216 vp = pairfind(vp->next, da->attr)) {
217 if (count == 0) break;
222 * Non-existent array reference.
226 return valuepair2str(out, outlen, vp, da->type, func);
229 vp = pairfind(vps, da->attr);
232 * Some "magic" handlers, which are never in VP's, but
233 * which are in the packet.
235 * FIXME: We should really do this in a more
241 localvp.strvalue[0] = 0;
248 dval = dict_valbyattr(da->attr, packet->code);
250 snprintf(out, outlen, "%s", dval->name);
252 snprintf(out, outlen, "%d", packet->code);
258 case PW_CLIENT_IP_ADDRESS: /* the same as below */
259 case PW_PACKET_SRC_IP_ADDRESS:
260 if (packet->src_ipaddr.af != AF_INET) {
263 localvp.attribute = da->attr;
264 localvp.lvalue = packet->src_ipaddr.ipaddr.ip4addr.s_addr;
267 case PW_PACKET_DST_IP_ADDRESS:
268 if (packet->dst_ipaddr.af != AF_INET) {
271 localvp.attribute = da->attr;
272 localvp.lvalue = packet->dst_ipaddr.ipaddr.ip4addr.s_addr;
275 case PW_PACKET_SRC_PORT:
276 localvp.attribute = da->attr;
277 localvp.lvalue = packet->src_port;
280 case PW_PACKET_DST_PORT:
281 localvp.attribute = da->attr;
282 localvp.lvalue = packet->dst_port;
285 case PW_PACKET_AUTHENTICATION_VECTOR:
286 localvp.attribute = da->attr;
287 memcpy(localvp.strvalue, packet->vector,
288 sizeof(packet->vector));
289 localvp.length = sizeof(packet->vector);
293 * Authorization, accounting, etc.
295 case PW_REQUEST_PROCESSING_STAGE:
296 if (request->component) {
297 strNcpy(out, request->component, outlen);
299 strNcpy(out, "server_core", outlen);
303 case PW_PACKET_SRC_IPV6_ADDRESS:
304 if (packet->src_ipaddr.af != AF_INET6) {
307 localvp.attribute = da->attr;
308 memcpy(localvp.strvalue,
309 &packet->src_ipaddr.ipaddr.ip4addr.s_addr,
310 sizeof(packet->src_ipaddr.ipaddr.ip4addr.s_addr));
313 case PW_PACKET_DST_IPV6_ADDRESS:
314 if (packet->dst_ipaddr.af != AF_INET6) {
317 localvp.attribute = da->attr;
318 memcpy(localvp.strvalue,
319 &packet->dst_ipaddr.ipaddr.ip4addr.s_addr,
320 sizeof(packet->dst_ipaddr.ipaddr.ip4addr.s_addr));
323 case PW_SERVER_IDENTITY:
324 if (!request->listener->identity) return 0;
326 snprintf(out, outlen, "%s", request->listener->identity);
331 return 0; /* not found */
335 localvp.type = da->type;
336 return valuepair2str(out, outlen, &localvp,
346 if (!vps) return 0; /* silently fail */
349 * Convert the VP to a string, and return it.
351 return valuepair2str(out, outlen, vp, da->type, func);
356 * Pull %{0} to %{8} out of the packet.
358 static int xlat_regex(void *instance, REQUEST *request,
359 char *fmt, char *out, size_t outlen,
360 RADIUS_ESCAPE_STRING func)
365 * We cheat: fmt is "0" to "8", but those numbers
366 * are already in the "instance".
368 fmt = fmt; /* -Wunused */
369 func = func; /* -Wunused FIXME: do escaping? */
371 regex = request_data_get(request, request,
372 REQUEST_DATA_REGEX | *(int *)instance);
373 if (!regex) return 0;
376 * Copy UP TO "freespace" bytes, including
379 strNcpy(out, regex, outlen);
380 free(regex); /* was strdup'd */
383 #endif /* HAVE_REGEX_H */
386 * Compare two xlat_t structs, based ONLY on the module name.
388 static int xlat_cmp(const void *a, const void *b)
390 if (((const xlat_t *)a)->length != ((const xlat_t *)b)->length) {
391 return ((const xlat_t *)a)->length - ((const xlat_t *)b)->length;
394 return memcmp(((const xlat_t *)a)->module,
395 ((const xlat_t *)b)->module,
396 ((const xlat_t *)a)->length);
401 * find the appropriate registered xlat function.
403 static const xlat_t *xlat_find(const char *module)
408 * Look for dictionary attributes first.
410 if ((dict_attrbyname(module) != NULL) ||
411 (strchr(module, '[') != NULL)) {
412 static const xlat_t dict_xlat = {
423 strNcpy(my_xlat.module, module, sizeof(my_xlat.module));
424 my_xlat.length = strlen(my_xlat.module);
426 return rbtree_finddata(xlat_root, &my_xlat);
431 * Register an xlat function.
433 int xlat_register(const char *module, RAD_XLAT_FUNC func, void *instance)
438 if ((module == NULL) || (strlen(module) == 0)) {
439 DEBUG("xlat_register: Invalid module name");
444 * First time around, build up the tree...
446 * FIXME: This code should be hoisted out of this function,
447 * and into a global "initialization". But it isn't critical...
455 xlat_root = rbtree_create(xlat_cmp, free, 0);
457 DEBUG("xlat_register: Failed to create tree.");
462 * Register the internal packet xlat's.
464 for (i = 0; internal_xlat[i] != NULL; i++) {
465 xlat_register(internal_xlat[i], xlat_packet, &xlat_inst[i]);
466 c = xlat_find(internal_xlat[i]);
467 rad_assert(c != NULL);
473 * Register xlat's for regexes.
476 for (i = 0; i <= REQUEST_MAX_REGEX; i++) {
478 xlat_register(buffer, xlat_regex, &xlat_inst[i]);
479 c = xlat_find(buffer);
480 rad_assert(c != NULL);
483 #endif /* HAVE_REGEX_H */
487 * If it already exists, replace the instance.
489 strNcpy(my_xlat.module, module, sizeof(my_xlat.module));
490 my_xlat.length = strlen(my_xlat.module);
491 c = rbtree_finddata(xlat_root, &my_xlat);
494 DEBUG("xlat_register: Cannot re-define internal xlat");
499 c->instance = instance;
504 * Doesn't exist. Create it.
506 c = rad_malloc(sizeof(xlat_t));
507 memset(c, 0, sizeof(*c));
510 strNcpy(c->module, module, sizeof(c->module));
511 c->length = strlen(c->module);
512 c->instance = instance;
514 rbtree_insert(xlat_root, c);
520 * Unregister an xlat function.
522 * We can only have one function to call per name, so the
523 * passing of "func" here is extraneous.
525 void xlat_unregister(const char *module, RAD_XLAT_FUNC func)
530 func = func; /* -Wunused */
532 strNcpy(my_xlat.module, module, sizeof(my_xlat.module));
533 my_xlat.length = strlen(my_xlat.module);
535 node = rbtree_find(xlat_root, &my_xlat);
538 rbtree_delete(xlat_root, node);
542 * De-register all xlat functions,
543 * used mainly for debugging.
547 rbtree_free(xlat_root);
552 * Decode an attribute name into a string.
554 static void decode_attribute(const char **from, char **to, int freespace,
555 int *open, REQUEST *request,
556 RADIUS_ESCAPE_STRING func)
560 char *xlat_string = NULL; /* can be large */
561 int free_xlat_string = FALSE;
564 int found=0, retlen=0;
565 int openbraces = *open;
575 * Skip the '{' at the front of 'p'
576 * Increment open braces
587 * First, copy the xlat key name to one buffer
589 while (*p && (*p != '}') && (*p != ':')) {
592 if (pa >= (xlat_name + sizeof(xlat_name) - 1)) {
594 * Skip to the end of the input
597 DEBUG("xlat: Module name is too long in string %%%s",
605 DEBUG("xlat: Invalid syntax in %s", *from);
608 * %{name} is a simple attribute reference,
609 * or regex reference.
611 } else if (*p == '}') {
613 rad_assert(openbraces == *open);
616 xlat_string = xlat_name;
619 } else if (p[1] == '-') { /* handle ':- */
621 xlat_string = xlat_name;
624 } else { /* module name, followed by per-module string */
626 int delimitbrace = *open;
628 rad_assert(*p == ':');
629 p++; /* skip the ':' */
632 * If there's a brace immediately following the colon,
633 * then we've chosen to delimite the per-module string,
634 * so keep track of that.
637 delimitbrace = openbraces;
642 xlat_string = rad_malloc(strlen(p) + 1); /* always returns */
643 free_xlat_string = TRUE;
647 * Copy over the rest of the string, which is per-module
650 while (*p && !stop) {
653 * What the heck is this supposed
662 * This is pretty hokey... we
663 * should use the functions in
673 if (openbraces == delimitbrace) {
690 * Now check to see if we're at the end of the string
691 * we were sent. If we're not, check for :-
693 if (openbraces == delimitbrace) {
694 if (p[0] == ':' && p[1] == '-') {
700 * Look up almost everything in the new tree of xlat
701 * functions. This makes it a little quicker...
704 if ((c = xlat_find(xlat_name)) != NULL) {
705 if (!c->internal) DEBUG("radius_xlat: Running registered xlat function of module %s for string \'%s\'",
706 c->module, xlat_string);
707 retlen = c->do_xlat(c->instance, request, xlat_string,
709 /* If retlen is 0, treat it as not found */
710 if (retlen > 0) found = 1;
714 * No attribute by that name, return an error.
716 DEBUG2("WARNING: Unknown module \"%s\" in string expansion \"%%%s\"", xlat_name, *from);
722 * Skip to last '}' if attr is found
723 * The rest of the stuff within the braces is
724 * useless if we found what we need
728 snprintf(q, freespace, "%d", retlen);
734 while((*p != '\0') && (openbraces > 0)) {
736 * Handle escapes outside of the loop.
741 p++; /* get & ignore next character */
760 p++; /* skip the character */
765 if (free_xlat_string) free(xlat_string);
773 * If the caller doesn't pass xlat an escape function, then
774 * we use this one. It simplifies the coding, as the check for
775 * func == NULL only happens once.
777 static int xlat_copy(char *out, int outlen, const char *in)
779 int freespace = outlen;
781 rad_assert(outlen >= 0);
783 while ((*in) && (freespace > 1)) {
787 * FIXME: Do escaping of bad stuff!
795 return (outlen - freespace); /* count does not include NUL */
799 * Replace %<whatever> in a string.
801 * See 'doc/variables.txt' for more information.
803 int radius_xlat(char *out, int outlen, const char *fmt,
804 REQUEST *request, RADIUS_ESCAPE_STRING func)
811 char tmpdt[40]; /* For temporary storing of dates */
817 if (!fmt || !out || !request) return 0;
820 * Ensure that we always have an escaping function.
829 /* Calculate freespace in output */
830 freespace = outlen - (q - out);
835 if ((c != '%') && (c != '$') && (c != '\\')) {
837 * We check if we're inside an open brace. If we are
838 * then we assume this brace is NOT literal, but is
839 * a closing brace and apply it
841 if ((c == '}') && openbraces) {
851 * There's nothing after this character, copy
852 * the last '%' or "$' or '\\' over to the output
879 * Hmmm... ${User-Name} is a synonym for
884 } else if (c == '$') switch(*p) {
885 case '{': /* Attribute by Name */
886 decode_attribute(&p, &q, freespace, &openbraces, request, func);
893 } else if (c == '%') switch(*p) {
895 decode_attribute(&p, &q, freespace, &openbraces, request, func);
901 case 'a': /* Protocol: */
902 q += valuepair2str(q,freespace,pairfind(request->reply->vps,PW_FRAMED_PROTOCOL),PW_TYPE_INTEGER, func);
905 case 'c': /* Callback-Number */
906 q += valuepair2str(q,freespace,pairfind(request->reply->vps,PW_CALLBACK_NUMBER),PW_TYPE_STRING, func);
909 case 'd': /* request day */
910 TM = localtime_r(&request->timestamp, &s_TM);
911 strftime(tmpdt,sizeof(tmpdt),"%d",TM);
912 strNcpy(q,tmpdt,freespace);
916 case 'f': /* Framed IP address */
917 q += valuepair2str(q,freespace,pairfind(request->reply->vps,PW_FRAMED_IP_ADDRESS),PW_TYPE_IPADDR, func);
920 case 'i': /* Calling station ID */
921 q += valuepair2str(q,freespace,pairfind(request->packet->vps,PW_CALLING_STATION_ID),PW_TYPE_STRING, func);
924 case 'l': /* request timestamp */
925 snprintf(tmpdt, sizeof(tmpdt), "%lu",
926 (unsigned long) request->timestamp);
927 strNcpy(q,tmpdt,freespace);
931 case 'm': /* request month */
932 TM = localtime_r(&request->timestamp, &s_TM);
933 strftime(tmpdt,sizeof(tmpdt),"%m",TM);
934 strNcpy(q,tmpdt,freespace);
938 case 'n': /* NAS IP address */
939 q += valuepair2str(q,freespace,pairfind(request->packet->vps,PW_NAS_IP_ADDRESS),PW_TYPE_IPADDR, func);
942 case 'p': /* Port number */
943 q += valuepair2str(q,freespace,pairfind(request->packet->vps,PW_NAS_PORT),PW_TYPE_INTEGER, func);
946 case 's': /* Speed */
947 q += valuepair2str(q,freespace,pairfind(request->packet->vps,PW_CONNECT_INFO),PW_TYPE_STRING, func);
950 case 't': /* request timestamp */
951 CTIME_R(&request->timestamp, q, freespace);
960 case 'u': /* User name */
961 q += valuepair2str(q,freespace,pairfind(request->packet->vps,PW_USER_NAME),PW_TYPE_STRING, func);
964 case 'A': /* radacct_dir */
965 strNcpy(q,radacct_dir,freespace-1);
969 case 'C': /* ClientName */
970 strNcpy(q,client_name_old(&request->packet->src_ipaddr),freespace-1);
974 case 'D': /* request date */
975 TM = localtime_r(&request->timestamp, &s_TM);
976 strftime(tmpdt,sizeof(tmpdt),"%Y%m%d",TM);
977 strNcpy(q,tmpdt,freespace);
981 case 'H': /* request hour */
982 TM = localtime_r(&request->timestamp, &s_TM);
983 strftime(tmpdt,sizeof(tmpdt),"%H",TM);
984 strNcpy(q,tmpdt,freespace);
988 case 'L': /* radlog_dir */
989 strNcpy(q,radlog_dir,freespace-1);
994 q += valuepair2str(q,freespace,pairfind(request->reply->vps,PW_FRAMED_MTU),PW_TYPE_INTEGER, func);
997 case 'R': /* radius_dir */
998 strNcpy(q,radius_dir,freespace-1);
1002 case 'S': /* request timestamp in SQL format*/
1003 TM = localtime_r(&request->timestamp, &s_TM);
1004 strftime(tmpdt,sizeof(tmpdt),"%Y-%m-%d %H:%M:%S",TM);
1005 strNcpy(q,tmpdt,freespace);
1009 case 'T': /* request timestamp */
1010 TM = localtime_r(&request->timestamp, &s_TM);
1011 strftime(tmpdt,sizeof(tmpdt),"%Y-%m-%d-%H.%M.%S.000000",TM);
1012 strNcpy(q,tmpdt,freespace);
1016 case 'U': /* Stripped User name */
1017 q += valuepair2str(q,freespace,pairfind(request->packet->vps,PW_STRIPPED_USER_NAME),PW_TYPE_STRING, func);
1020 case 'V': /* Request-Authenticator */
1021 if (request->packet->verified)
1022 strNcpy(q,"Verified",freespace-1);
1024 strNcpy(q,"None",freespace-1);
1028 case 'Y': /* request year */
1029 TM = localtime_r(&request->timestamp, &s_TM);
1030 strftime(tmpdt,sizeof(tmpdt),"%Y",TM);
1031 strNcpy(q,tmpdt,freespace);
1035 case 'Z': /* Full request pairs except password */
1036 tmp = request->packet->vps;
1037 while (tmp && (freespace > 3)) {
1038 if (tmp->attribute != PW_PASSWORD) {
1040 i = vp_prints(q,freespace-2,tmp);
1050 DEBUG2("WARNING: Unknown variable '%%%c': See 'doc/variables.txt'", *p);
1051 if (freespace > 2) {
1060 DEBUG2("radius_xlat: '%s'", out);