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 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, 0);
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 vp = pairfind(vps, da->attr);
149 * Some "magic" handlers, which are never in VP's, but
150 * which are in the packet.
152 * FIXME: Add SRC/DST IP address!
160 dval = dict_valbyattr(da->attr, packet->code);
162 snprintf(out, outlen, "%s", dval->name);
164 snprintf(out, outlen, "%d", packet->code);
181 if (!vps) return 0; /* silently fail */
184 * Convert the VP to a string, and return it.
186 return valuepair2str(out, outlen, vp, da->type, func);
191 * Pull %{0} to %{8} out of the packet.
193 static int xlat_regex(void *instance, REQUEST *request,
194 char *fmt, char *out, size_t outlen,
195 RADIUS_ESCAPE_STRING func)
200 * We cheat: fmt is "0" to "8", but those numbers
201 * are already in the "instance".
203 fmt = fmt; /* -Wunused */
204 func = func; /* -Wunused FIXME: do escaping? */
206 regex = request_data_get(request, request,
207 REQUEST_DATA_REGEX | *(int *)instance);
208 if (!regex) return 0;
211 * Copy UP TO "freespace" bytes, including
214 strNcpy(out, regex, outlen);
215 free(regex); /* was strdup'd */
218 #endif /* HAVE_REGEX_H */
221 * Compare two xlat_t structs, based ONLY on the module name.
223 static int xlat_cmp(const void *a, const void *b)
225 if (((const xlat_t *)a)->length != ((const xlat_t *)b)->length) {
226 return ((const xlat_t *)a)->length - ((const xlat_t *)b)->length;
229 return memcmp(((const xlat_t *)a)->module,
230 ((const xlat_t *)b)->module,
231 ((const xlat_t *)a)->length);
236 * find the appropriate registered xlat function.
238 static xlat_t *xlat_find(const char *module)
243 strNcpy(my_xlat.module, module, sizeof(my_xlat.module));
246 * We get passed the WHOLE string, and all we want here
247 * is the first piece.
249 p = strchr(my_xlat.module, ':');
252 my_xlat.length = strlen(my_xlat.module);
254 return rbtree_finddata(xlat_root, &my_xlat);
259 * Register an xlat function.
261 int xlat_register(const char *module, RAD_XLAT_FUNC func, void *instance)
266 if ((module == NULL) || (strlen(module) == 0)) {
267 DEBUG("xlat_register: Invalid module name");
272 * First time around, build up the tree...
274 * FIXME: This code should be hoisted out of this function,
275 * and into a global "initialization". But it isn't critical...
283 xlat_root = rbtree_create(xlat_cmp, free, 0);
285 DEBUG("xlat_register: Failed to create tree.");
290 * Register the internal packet xlat's.
292 for (i = 0; internal_xlat[i] != NULL; i++) {
293 xlat_register(internal_xlat[i], xlat_packet, &xlat_inst[i]);
294 c = xlat_find(internal_xlat[i]);
295 rad_assert(c != NULL);
301 * Register xlat's for regexes.
304 for (i = 0; i <= REQUEST_MAX_REGEX; i++) {
306 xlat_register(buffer, xlat_regex, &xlat_inst[i]);
307 c = xlat_find(buffer);
308 rad_assert(c != NULL);
311 #endif /* HAVE_REGEX_H */
315 * If it already exists, replace the instance.
317 strNcpy(my_xlat.module, module, sizeof(my_xlat.module));
318 my_xlat.length = strlen(my_xlat.module);
319 c = rbtree_finddata(xlat_root, &my_xlat);
322 DEBUG("xlat_register: Cannot re-define internal xlat");
327 c->instance = instance;
332 * Doesn't exist. Create it.
334 c = rad_malloc(sizeof(xlat_t));
335 memset(c, 0, sizeof(*c));
338 strNcpy(c->module, module, sizeof(c->module));
339 c->length = strlen(c->module);
340 c->instance = instance;
342 rbtree_insert(xlat_root, c);
348 * Unregister an xlat function.
350 * We can only have one function to call per name, so the
351 * passing of "func" here is extraneous.
353 void xlat_unregister(const char *module, RAD_XLAT_FUNC func)
358 func = func; /* -Wunused */
360 strNcpy(my_xlat.module, module, sizeof(my_xlat.module));
361 my_xlat.length = strlen(my_xlat.module);
363 node = rbtree_find(xlat_root, &my_xlat);
366 rbtree_delete(xlat_root, node);
371 * Decode an attribute name into a string.
373 static void decode_attribute(const char **from, char **to, int freespace,
374 int *open, REQUEST *request,
375 RADIUS_ESCAPE_STRING func)
380 int stop=0, found=0, retlen=0;
381 int openbraces = *open;
391 * Skip the '{' at the front of 'p'
392 * Increment open braces
398 * Copy over the rest of the string.
400 while ((*p) && (!stop)) {
403 * Allow braces inside things, too.
417 if (openbraces == *open) {
426 * Attr-Name1:-Attr-Name2
428 * Use Attr-Name1, and if not found,
437 /* else FALL-THROUGH */
447 * Look up almost everything in the new tree of xlat
448 * functions. this makes it a little quicker...
450 if ((c = xlat_find(attrname)) != NULL) {
451 if (!c->internal) DEBUG("radius_xlat: Running registered xlat function of module %s for string \'%s\'",
452 c->module, attrname+ c->length + 1);
453 retlen = c->do_xlat(c->instance, request, attrname+(c->length+1), q, freespace, func);
454 /* If retlen is 0, treat it as not found */
463 * Not in the default xlat database. Must be
464 * a bare attribute number.
466 } else if ((retlen = xlat_packet(&xlat_inst[1], request, attrname,
467 q, freespace, func)) > 0) {
472 * Look up the name, in order to get the correct
476 } else if (dict_attrbyname(attrname) == NULL) {
478 * No attribute by that name, return an error.
480 DEBUG2("WARNING: Attempt to use unknown xlat function, or non-existent attribute in string %%{%s}", attrname);
482 } /* else the attribute is known, but not in the request */
485 * Skip to last '}' if attr is found
486 * The rest of the stuff within the braces is
487 * useless if we found what we need
490 while((*p != '\0') && (openbraces > 0)) {
492 * Handle escapes outside of the loop.
497 p++; /* get & ignore next character */
516 p++; /* skip the character */
526 * If the caller doesn't pass xlat an escape function, then
527 * we use this one. It simplifies the coding, as the check for
528 * func == NULL only happens once.
530 static int xlat_copy(char *out, int outlen, const char *in)
536 * Truncate, if too much.
545 * FIXME: Do escaping of bad stuff!
559 * Replace %<whatever> in a string.
561 * See 'doc/variables.txt' for more information.
563 int radius_xlat(char *out, int outlen, const char *fmt,
564 REQUEST *request, RADIUS_ESCAPE_STRING func)
571 char tmpdt[40]; /* For temporary storing of dates */
577 if (!fmt || !out || !request) return 0;
580 * Ensure that we always have an escaping function.
589 /* Calculate freespace in output */
590 freespace = outlen - (q - out);
595 if ((c != '%') && (c != '$') && (c != '\\')) {
597 * We check if we're inside an open brace. If we are
598 * then we assume this brace is NOT literal, but is
599 * a closing brace and apply it
601 if ((c == '}') && openbraces) {
611 * There's nothing after this character, copy
612 * the last '%' or "$' or '\\' over to the output
639 * Hmmm... ${User-Name} is a synonym for
644 } else if (c == '$') switch(*p) {
645 case '{': /* Attribute by Name */
646 decode_attribute(&p, &q, freespace, &openbraces, request, func);
653 } else if (c == '%') switch(*p) {
655 decode_attribute(&p, &q, freespace, &openbraces, request, func);
661 case 'a': /* Protocol: */
662 q += valuepair2str(q,freespace,pairfind(request->reply->vps,PW_FRAMED_PROTOCOL),PW_TYPE_INTEGER, func);
665 case 'c': /* Callback-Number */
666 q += valuepair2str(q,freespace,pairfind(request->reply->vps,PW_CALLBACK_NUMBER),PW_TYPE_STRING, func);
669 case 'd': /* request day */
670 TM = localtime_r(&request->timestamp, &s_TM);
671 strftime(tmpdt,sizeof(tmpdt),"%d",TM);
672 strNcpy(q,tmpdt,freespace);
676 case 'f': /* Framed IP address */
677 q += valuepair2str(q,freespace,pairfind(request->reply->vps,PW_FRAMED_IP_ADDRESS),PW_TYPE_IPADDR, func);
680 case 'i': /* Calling station ID */
681 q += valuepair2str(q,freespace,pairfind(request->packet->vps,PW_CALLING_STATION_ID),PW_TYPE_STRING, func);
684 case 'l': /* request timestamp */
685 snprintf(tmpdt, sizeof(tmpdt), "%lu",
686 (unsigned long) request->timestamp);
687 strNcpy(q,tmpdt,freespace);
691 case 'm': /* request month */
692 TM = localtime_r(&request->timestamp, &s_TM);
693 strftime(tmpdt,sizeof(tmpdt),"%m",TM);
694 strNcpy(q,tmpdt,freespace);
698 case 'n': /* NAS IP address */
699 q += valuepair2str(q,freespace,pairfind(request->packet->vps,PW_NAS_IP_ADDRESS),PW_TYPE_IPADDR, func);
702 case 'p': /* Port number */
703 q += valuepair2str(q,freespace,pairfind(request->packet->vps,PW_NAS_PORT),PW_TYPE_INTEGER, func);
706 case 's': /* Speed */
707 q += valuepair2str(q,freespace,pairfind(request->packet->vps,PW_CONNECT_INFO),PW_TYPE_STRING, func);
710 case 't': /* request timestamp */
711 CTIME_R(&request->timestamp, q, freespace);
715 case 'u': /* User name */
716 q += valuepair2str(q,freespace,pairfind(request->packet->vps,PW_USER_NAME),PW_TYPE_STRING, func);
719 case 'A': /* radacct_dir */
720 strNcpy(q,radacct_dir,freespace-1);
724 case 'C': /* ClientName */
725 strNcpy(q,client_name(request->packet->src_ipaddr),freespace-1);
729 case 'D': /* request date */
730 TM = localtime_r(&request->timestamp, &s_TM);
731 strftime(tmpdt,sizeof(tmpdt),"%Y%m%d",TM);
732 strNcpy(q,tmpdt,freespace);
736 case 'H': /* request hour */
737 TM = localtime_r(&request->timestamp, &s_TM);
738 strftime(tmpdt,sizeof(tmpdt),"%H",TM);
739 strNcpy(q,tmpdt,freespace);
743 case 'L': /* radlog_dir */
744 strNcpy(q,radlog_dir,freespace-1);
749 q += valuepair2str(q,freespace,pairfind(request->reply->vps,PW_FRAMED_MTU),PW_TYPE_INTEGER, func);
752 case 'R': /* radius_dir */
753 strNcpy(q,radius_dir,freespace-1);
757 case 'S': /* request timestamp in SQL format*/
758 TM = localtime_r(&request->timestamp, &s_TM);
759 strftime(tmpdt,sizeof(tmpdt),"%Y-%m-%d %H:%M:%S",TM);
760 strNcpy(q,tmpdt,freespace);
764 case 'T': /* request timestamp */
765 TM = localtime_r(&request->timestamp, &s_TM);
766 strftime(tmpdt,sizeof(tmpdt),"%Y-%m-%d-%H.%M.%S.000000",TM);
767 strNcpy(q,tmpdt,freespace);
771 case 'U': /* Stripped User name */
772 q += valuepair2str(q,freespace,pairfind(request->packet->vps,PW_STRIPPED_USER_NAME),PW_TYPE_STRING, func);
775 case 'V': /* Request-Authenticator */
776 if (request->packet->verified)
777 strNcpy(q,"Verified",freespace-1);
779 strNcpy(q,"None",freespace-1);
783 case 'Y': /* request year */
784 TM = localtime_r(&request->timestamp, &s_TM);
785 strftime(tmpdt,sizeof(tmpdt),"%Y",TM);
786 strNcpy(q,tmpdt,freespace);
790 case 'Z': /* Full request pairs except password */
791 tmp = request->packet->vps;
792 while (tmp && (freespace > 3)) {
793 if (tmp->attribute != PW_PASSWORD) {
795 i = vp_prints(q,freespace-2,tmp);
805 DEBUG2("WARNING: Unknown variable '%%%c': See 'doc/variables.txt'", *p);
815 DEBUG2("radius_xlat: '%s'", out);