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 const char *p = strchr(fmt, '[');
150 if (strlen(fmt) > sizeof(buffer)) return 0;
152 strNcpy(buffer, fmt, p - fmt + 1);
157 * Check the format of the index before looking
158 * the attribute up in the dictionary, because
159 * it's a cheap check.
161 p += 1 + strspn(p + 1, "0123456789");
163 DEBUG2("xlat: Invalid array reference in string at %s %s",
168 da = dict_attrbyname(buffer);
173 * Find the N'th value.
175 for (vp = pairfind(vps, da->attr);
177 vp = pairfind(vp->next, da->attr)) {
178 if (count == 0) break;
183 * Non-existent array reference.
187 return valuepair2str(out, outlen, vp, da->type, func);
190 vp = pairfind(vps, da->attr);
193 * Some "magic" handlers, which are never in VP's, but
194 * which are in the packet.
196 * FIXME: Add SRC/DST IP address!
204 dval = dict_valbyattr(da->attr, packet->code);
206 snprintf(out, outlen, "%s", dval->name);
208 snprintf(out, outlen, "%d", packet->code);
225 if (!vps) return 0; /* silently fail */
228 * Convert the VP to a string, and return it.
230 return valuepair2str(out, outlen, vp, da->type, func);
235 * Pull %{0} to %{8} out of the packet.
237 static int xlat_regex(void *instance, REQUEST *request,
238 char *fmt, char *out, size_t outlen,
239 RADIUS_ESCAPE_STRING func)
244 * We cheat: fmt is "0" to "8", but those numbers
245 * are already in the "instance".
247 fmt = fmt; /* -Wunused */
248 func = func; /* -Wunused FIXME: do escaping? */
250 regex = request_data_get(request, request,
251 REQUEST_DATA_REGEX | *(int *)instance);
252 if (!regex) return 0;
255 * Copy UP TO "freespace" bytes, including
258 strNcpy(out, regex, outlen);
259 free(regex); /* was strdup'd */
262 #endif /* HAVE_REGEX_H */
265 * Compare two xlat_t structs, based ONLY on the module name.
267 static int xlat_cmp(const void *a, const void *b)
269 if (((const xlat_t *)a)->length != ((const xlat_t *)b)->length) {
270 return ((const xlat_t *)a)->length - ((const xlat_t *)b)->length;
273 return memcmp(((const xlat_t *)a)->module,
274 ((const xlat_t *)b)->module,
275 ((const xlat_t *)a)->length);
280 * find the appropriate registered xlat function.
282 static xlat_t *xlat_find(const char *module)
287 strNcpy(my_xlat.module, module, sizeof(my_xlat.module));
290 * We get passed the WHOLE string, and all we want here
291 * is the first piece.
293 p = strchr(my_xlat.module, ':');
296 my_xlat.length = strlen(my_xlat.module);
298 return rbtree_finddata(xlat_root, &my_xlat);
303 * Register an xlat function.
305 int xlat_register(const char *module, RAD_XLAT_FUNC func, void *instance)
310 if ((module == NULL) || (strlen(module) == 0)) {
311 DEBUG("xlat_register: Invalid module name");
316 * First time around, build up the tree...
318 * FIXME: This code should be hoisted out of this function,
319 * and into a global "initialization". But it isn't critical...
327 xlat_root = rbtree_create(xlat_cmp, free, 0);
329 DEBUG("xlat_register: Failed to create tree.");
334 * Register the internal packet xlat's.
336 for (i = 0; internal_xlat[i] != NULL; i++) {
337 xlat_register(internal_xlat[i], xlat_packet, &xlat_inst[i]);
338 c = xlat_find(internal_xlat[i]);
339 rad_assert(c != NULL);
345 * Register xlat's for regexes.
348 for (i = 0; i <= REQUEST_MAX_REGEX; i++) {
350 xlat_register(buffer, xlat_regex, &xlat_inst[i]);
351 c = xlat_find(buffer);
352 rad_assert(c != NULL);
355 #endif /* HAVE_REGEX_H */
359 * If it already exists, replace the instance.
361 strNcpy(my_xlat.module, module, sizeof(my_xlat.module));
362 my_xlat.length = strlen(my_xlat.module);
363 c = rbtree_finddata(xlat_root, &my_xlat);
366 DEBUG("xlat_register: Cannot re-define internal xlat");
371 c->instance = instance;
376 * Doesn't exist. Create it.
378 c = rad_malloc(sizeof(xlat_t));
379 memset(c, 0, sizeof(*c));
382 strNcpy(c->module, module, sizeof(c->module));
383 c->length = strlen(c->module);
384 c->instance = instance;
386 rbtree_insert(xlat_root, c);
392 * Unregister an xlat function.
394 * We can only have one function to call per name, so the
395 * passing of "func" here is extraneous.
397 void xlat_unregister(const char *module, RAD_XLAT_FUNC func)
402 func = func; /* -Wunused */
404 strNcpy(my_xlat.module, module, sizeof(my_xlat.module));
405 my_xlat.length = strlen(my_xlat.module);
407 node = rbtree_find(xlat_root, &my_xlat);
410 rbtree_delete(xlat_root, node);
415 * Decode an attribute name into a string.
417 static void decode_attribute(const char **from, char **to, int freespace,
418 int *open, REQUEST *request,
419 RADIUS_ESCAPE_STRING func)
424 int stop=0, found=0, retlen=0;
425 int openbraces = *open;
427 size_t namelen = sizeof(attrname);
436 * Skip the '{' at the front of 'p'
437 * Increment open braces
443 * Copy over the rest of the string.
445 while ((*p) && (!stop) && (namelen > 1)) {
448 * Allow braces inside things, too.
462 if (openbraces == *open) {
471 * Attr-Name1:-Attr-Name2
473 * Use Attr-Name1, and if not found,
482 /* else FALL-THROUGH */
493 * Look up almost everything in the new tree of xlat
494 * functions. this makes it a little quicker...
496 if ((c = xlat_find(attrname)) != NULL) {
497 if (!c->internal) DEBUG("radius_xlat: Running registered xlat function of module %s for string \'%s\'",
498 c->module, attrname+ c->length + 1);
499 retlen = c->do_xlat(c->instance, request, attrname+(c->length+1), q, freespace, func);
500 /* If retlen is 0, treat it as not found */
509 * Not in the default xlat database. Must be
510 * a bare attribute number.
512 } else if ((retlen = xlat_packet(&xlat_inst[1], request, attrname,
513 q, freespace, func)) > 0) {
518 * Look up the name, in order to get the correct
522 } else if (dict_attrbyname(attrname) == NULL) {
524 * No attribute by that name, return an error.
526 DEBUG2("WARNING: Attempt to use unknown xlat function, or non-existent attribute in string %%{%s}", attrname);
528 } /* else the attribute is known, but not in the request */
531 * Skip to last '}' if attr is found
532 * The rest of the stuff within the braces is
533 * useless if we found what we need
536 while((*p != '\0') && (openbraces > *open)) {
538 * Handle escapes outside of the loop.
543 p++; /* get & ignore next character */
562 p++; /* skip the character */
572 * If the caller doesn't pass xlat an escape function, then
573 * we use this one. It simplifies the coding, as the check for
574 * func == NULL only happens once.
576 static int xlat_copy(char *out, int outlen, const char *in)
578 int freespace = outlen;
580 rad_assert(outlen > 0);
582 while ((*in) && (freespace > 1)) {
586 * FIXME: Do escaping of bad stuff!
594 return (outlen - freespace); /* count does not include NUL */
598 * Replace %<whatever> in a string.
600 * See 'doc/variables.txt' for more information.
602 int radius_xlat(char *out, int outlen, const char *fmt,
603 REQUEST *request, RADIUS_ESCAPE_STRING func)
605 int c, len, freespace;
611 char tmpdt[40]; /* For temporary storing of dates */
617 if (!fmt || !out || !request) return 0;
620 * Ensure that we always have an escaping function.
629 /* Calculate freespace in output */
630 freespace = outlen - (q - out);
635 if ((c != '%') && (c != '$') && (c != '\\')) {
637 * We check if we're inside an open brace. If we are
638 * then we assume this brace is NOT literal, but is
639 * a closing brace and apply it
641 if ((c == '}') && openbraces) {
651 * There's nothing after this character, copy
652 * the last '%' or "$' or '\\' over to the output
679 * Hmmm... ${User-Name} is a synonym for
684 } else if (c == '$') switch(*p) {
685 case '{': /* Attribute by Name */
686 decode_attribute(&p, &q, freespace, &openbraces, request, func);
693 } else if (c == '%') switch(*p) {
695 decode_attribute(&p, &q, freespace, &openbraces, request, func);
701 case 'a': /* Protocol: */
702 q += valuepair2str(q,freespace,pairfind(request->reply->vps,PW_FRAMED_PROTOCOL),PW_TYPE_INTEGER, func);
705 case 'c': /* Callback-Number */
706 q += valuepair2str(q,freespace,pairfind(request->reply->vps,PW_CALLBACK_NUMBER),PW_TYPE_STRING, func);
709 case 'd': /* request day */
710 TM = localtime_r(&request->timestamp, &s_TM);
711 len = strftime(tmpdt, sizeof(tmpdt), "%d", TM);
713 strNcpy(q, tmpdt, freespace);
718 case 'f': /* Framed IP address */
719 q += valuepair2str(q,freespace,pairfind(request->reply->vps,PW_FRAMED_IP_ADDRESS),PW_TYPE_IPADDR, func);
722 case 'i': /* Calling station ID */
723 q += valuepair2str(q,freespace,pairfind(request->packet->vps,PW_CALLING_STATION_ID),PW_TYPE_STRING, func);
726 case 'l': /* request timestamp */
727 snprintf(tmpdt, sizeof(tmpdt), "%lu",
728 (unsigned long) request->timestamp);
729 strNcpy(q,tmpdt,freespace);
733 case 'm': /* request month */
734 TM = localtime_r(&request->timestamp, &s_TM);
735 len = strftime(tmpdt, sizeof(tmpdt), "%m", TM);
737 strNcpy(q, tmpdt, freespace);
742 case 'n': /* NAS IP address */
743 q += valuepair2str(q,freespace,pairfind(request->packet->vps,PW_NAS_IP_ADDRESS),PW_TYPE_IPADDR, func);
746 case 'p': /* Port number */
747 q += valuepair2str(q,freespace,pairfind(request->packet->vps,PW_NAS_PORT),PW_TYPE_INTEGER, func);
750 case 's': /* Speed */
751 q += valuepair2str(q,freespace,pairfind(request->packet->vps,PW_CONNECT_INFO),PW_TYPE_STRING, func);
754 case 't': /* request timestamp */
755 CTIME_R(&request->timestamp, tmpdt, sizeof(tmpdt));
756 nl = strchr(tmpdt, '\n');
758 strNcpy(q, tmpdt, freespace);
762 case 'u': /* User name */
763 q += valuepair2str(q,freespace,pairfind(request->packet->vps,PW_USER_NAME),PW_TYPE_STRING, func);
766 case 'A': /* radacct_dir */
767 strNcpy(q,radacct_dir,freespace-1);
771 case 'C': /* ClientName */
772 strNcpy(q,client_name(request->packet->src_ipaddr),freespace-1);
776 case 'D': /* request date */
777 TM = localtime_r(&request->timestamp, &s_TM);
778 len = strftime(tmpdt, sizeof(tmpdt), "%Y%m%d", TM);
780 strNcpy(q, tmpdt, freespace);
785 case 'H': /* request hour */
786 TM = localtime_r(&request->timestamp, &s_TM);
787 len = strftime(tmpdt, sizeof(tmpdt), "%H", TM);
789 strNcpy(q, tmpdt, freespace);
794 case 'L': /* radlog_dir */
795 strNcpy(q,radlog_dir,freespace-1);
800 q += valuepair2str(q,freespace,pairfind(request->reply->vps,PW_FRAMED_MTU),PW_TYPE_INTEGER, func);
803 case 'R': /* radius_dir */
804 strNcpy(q,radius_dir,freespace-1);
808 case 'S': /* request timestamp in SQL format*/
809 TM = localtime_r(&request->timestamp, &s_TM);
810 len = strftime(tmpdt, sizeof(tmpdt), "%Y-%m-%d %H:%M:%S", TM);
812 strNcpy(q, tmpdt, freespace);
817 case 'T': /* request timestamp */
818 TM = localtime_r(&request->timestamp, &s_TM);
819 len = strftime(tmpdt, sizeof(tmpdt), "%Y-%m-%d-%H.%M.%S.000000", TM);
821 strNcpy(q, tmpdt, freespace);
826 case 'U': /* Stripped User name */
827 q += valuepair2str(q,freespace,pairfind(request->packet->vps,PW_STRIPPED_USER_NAME),PW_TYPE_STRING, func);
830 case 'V': /* Request-Authenticator */
831 if (request->packet->verified)
832 strNcpy(q,"Verified",freespace-1);
834 strNcpy(q,"None",freespace-1);
838 case 'Y': /* request year */
839 TM = localtime_r(&request->timestamp, &s_TM);
840 len = strftime(tmpdt, sizeof(tmpdt), "%Y", TM);
842 strNcpy(q, tmpdt, freespace);
847 case 'Z': /* Full request pairs except password */
848 tmp = request->packet->vps;
849 while (tmp && (freespace > 3)) {
850 if (tmp->attribute != PW_PASSWORD) {
852 len = vp_prints(q, freespace - 2, tmp);
854 freespace -= (len + 2);
862 DEBUG2("WARNING: Unknown variable '%%%c': See 'doc/variables.txt'", *p);
872 DEBUG2("radius_xlat: '%s'", out);