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",
59 static int xlat_inst[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 }; /* up to 8 for regex */
63 * Convert the value on a VALUE_PAIR to string
65 static int valuepair2str(char * out,int outlen,VALUE_PAIR * pair,
66 int type, RADIUS_ESCAPE_STRING func)
68 char buffer[MAX_STRING_LEN * 4];
71 vp_prints_value(buffer, sizeof(buffer), pair, 0);
72 return func(out, outlen, buffer);
77 strNcpy(out,"_",outlen);
79 case PW_TYPE_INTEGER :
80 strNcpy(out,"0",outlen);
83 strNcpy(out,"?.?.?.?",outlen);
86 strNcpy(out,"0",outlen);
89 strNcpy(out,"unknown_type",outlen);
96 * Dynamically translate for check:, request:, reply:, etc.
98 static int xlat_packet(void *instance, REQUEST *request,
99 char *fmt, char *out, size_t outlen,
100 RADIUS_ESCAPE_STRING func)
104 VALUE_PAIR *vps = NULL;
105 RADIUS_PACKET *packet = NULL;
107 switch (*(int*) instance) {
109 vps = request->config_items;
113 vps = request->packet->vps;
114 packet = request->packet;
118 vps = request->reply->vps;
119 packet = request->reply;
123 if (request->proxy) vps = request->proxy->vps;
124 packet = request->proxy;
128 if (request->proxy_reply) vps = request->proxy_reply->vps;
129 packet = request->proxy_reply;
137 * The "format" string is the attribute name.
139 da = dict_attrbyname(fmt);
142 vp = pairfind(vps, da->attr);
145 * Some "magic" handlers, which are never in VP's, but
146 * which are in the packet.
148 * FIXME: Add SRC/DST IP address!
156 dval = dict_valbyattr(da->attr, packet->code);
158 snprintf(out, outlen, "%s", dval->name);
160 snprintf(out, outlen, "%d", packet->code);
177 if (!vps) return 0; /* silently fail */
180 * Convert the VP to a string, and return it.
182 return valuepair2str(out, outlen, vp, da->type, func);
187 * Pull %{0} to %{8} out of the packet.
189 static int xlat_regex(void *instance, REQUEST *request,
190 char *fmt, char *out, size_t outlen,
191 RADIUS_ESCAPE_STRING func)
196 * We cheat: fmt is "0" to "8", but those numbers
197 * are already in the "instance".
199 fmt = fmt; /* -Wunused */
200 func = func; /* -Wunused FIXME: do escaping? */
202 regex = request_data_get(request, request,
203 REQUEST_DATA_REGEX | *(int *)instance);
204 if (!regex) return 0;
207 * Copy UP TO "freespace" bytes, including
210 strNcpy(out, regex, outlen);
211 free(regex); /* was strdup'd */
214 #endif /* HAVE_REGEX_H */
217 * Compare two xlat_t structs, based ONLY on the module name.
219 static int xlat_cmp(const void *a, const void *b)
221 if (((const xlat_t *)a)->length != ((const xlat_t *)b)->length) {
222 return ((const xlat_t *)a)->length - ((const xlat_t *)b)->length;
225 return memcmp(((const xlat_t *)a)->module,
226 ((const xlat_t *)b)->module,
227 ((const xlat_t *)a)->length);
232 * find the appropriate registered xlat function.
234 static xlat_t *xlat_find(const char *module)
239 strNcpy(my_xlat.module, module, sizeof(my_xlat.module));
242 * We get passed the WHOLE string, and all we want here
243 * is the first piece.
245 p = strchr(my_xlat.module, ':');
248 my_xlat.length = strlen(my_xlat.module);
250 return rbtree_finddata(xlat_root, &my_xlat);
255 * Register an xlat function.
257 int xlat_register(const char *module, RAD_XLAT_FUNC func, void *instance)
262 if ((module == NULL) || (strlen(module) == 0)) {
263 DEBUG("xlat_register: Invalid module name");
268 * First time around, build up the tree...
270 * FIXME: This code should be hoisted out of this function,
271 * and into a global "initialization". But it isn't critical...
279 xlat_root = rbtree_create(xlat_cmp, free, 0);
281 DEBUG("xlat_register: Failed to create tree.");
286 * Register the internal packet xlat's.
288 for (i = 0; internal_xlat[i] != NULL; i++) {
289 xlat_register(internal_xlat[i], xlat_packet, &xlat_inst[i]);
290 c = xlat_find(internal_xlat[i]);
291 rad_assert(c != NULL);
297 * Register xlat's for regexes.
300 for (i = 0; i <= 8; i++) {
302 xlat_register(buffer, xlat_regex, &xlat_inst[i]);
303 c = xlat_find(buffer);
304 rad_assert(c != NULL);
307 #endif /* HAVE_REGEX_H */
311 * If it already exists, replace the instance.
313 strNcpy(my_xlat.module, module, sizeof(my_xlat.module));
314 my_xlat.length = strlen(my_xlat.module);
315 c = rbtree_finddata(xlat_root, &my_xlat);
318 DEBUG("xlat_register: Cannot re-define internal xlat");
323 c->instance = instance;
328 * Doesn't exist. Create it.
330 c = rad_malloc(sizeof(xlat_t));
331 memset(c, 0, sizeof(*c));
334 strNcpy(c->module, module, sizeof(c->module));
335 c->length = strlen(c->module);
336 c->instance = instance;
338 rbtree_insert(xlat_root, c);
344 * Unregister an xlat function.
346 * We can only have one function to call per name, so the
347 * passing of "func" here is extraneous.
349 void xlat_unregister(const char *module, RAD_XLAT_FUNC func)
354 func = func; /* -Wunused */
356 strNcpy(my_xlat.module, module, sizeof(my_xlat.module));
357 my_xlat.length = strlen(my_xlat.module);
359 node = rbtree_find(xlat_root, &my_xlat);
362 rbtree_delete(xlat_root, node);
367 * Decode an attribute name into a string.
369 static void decode_attribute(const char **from, char **to, int freespace,
370 int *open, REQUEST *request,
371 RADIUS_ESCAPE_STRING func)
376 int stop=0, found=0, retlen=0;
377 int openbraces = *open;
387 * Skip the '{' at the front of 'p'
388 * Increment open braces
394 * Copy over the rest of the string.
396 while ((*p) && (!stop)) {
399 * Allow braces inside things, too.
413 if (openbraces == *open) {
422 * Attr-Name1:-Attr-Name2
424 * Use Attr-Name1, and if not found,
433 /* else FALL-THROUGH */
443 * Look up almost everything in the new tree of xlat
444 * functions. this makes it a little quicker...
446 if ((c = xlat_find(attrname)) != NULL) {
447 if (!c->internal) DEBUG("radius_xlat: Running registered xlat function of module %s for string \'%s\'",
448 c->module, attrname+ c->length + 1);
449 retlen = c->do_xlat(c->instance, request, attrname+(c->length+1), q, freespace, func);
450 /* If retlen is 0, treat it as not found */
459 * Not in the default xlat database. Must be
460 * a bare attribute number.
462 } else if ((retlen = xlat_packet(&xlat_inst[1], request, attrname,
463 q, freespace, func)) > 0) {
468 * Look up the name, in order to get the correct
472 } else if (dict_attrbyname(attrname) == NULL) {
474 * No attribute by that name, return an error.
476 DEBUG2("WARNING: Attempt to use unknown xlat function, or non-existent attribute in string %%{%s}", attrname);
478 } /* else the attribute is known, but not in the request */
481 * Skip to last '}' if attr is found
482 * The rest of the stuff within the braces is
483 * useless if we found what we need
486 while((*p != '\0') && (openbraces > 0)) {
488 * Handle escapes outside of the loop.
493 p++; /* get & ignore next character */
512 p++; /* skip the character */
522 * If the caller doesn't pass xlat an escape function, then
523 * we use this one. It simplifies the coding, as the check for
524 * func == NULL only happens once.
526 static int xlat_copy(char *out, int outlen, const char *in)
532 * Truncate, if too much.
541 * FIXME: Do escaping of bad stuff!
555 * Replace %<whatever> in a string.
557 * See 'doc/variables.txt' for more information.
559 int radius_xlat(char *out, int outlen, const char *fmt,
560 REQUEST *request, RADIUS_ESCAPE_STRING func)
567 char tmpdt[40]; /* For temporary storing of dates */
573 if (!fmt || !out || !request) return 0;
576 * Ensure that we always have an escaping function.
585 /* Calculate freespace in output */
586 freespace = outlen - (q - out);
591 if ((c != '%') && (c != '$') && (c != '\\')) {
593 * We check if we're inside an open brace. If we are
594 * then we assume this brace is NOT literal, but is
595 * a closing brace and apply it
597 if ((c == '}') && openbraces) {
607 * There's nothing after this character, copy
608 * the last '%' or "$' or '\\' over to the output
635 * Hmmm... ${User-Name} is a synonym for
640 } else if (c == '$') switch(*p) {
641 case '{': /* Attribute by Name */
642 decode_attribute(&p, &q, freespace, &openbraces, request, func);
649 } else if (c == '%') switch(*p) {
651 decode_attribute(&p, &q, freespace, &openbraces, request, func);
657 case 'a': /* Protocol: */
658 q += valuepair2str(q,freespace,pairfind(request->reply->vps,PW_FRAMED_PROTOCOL),PW_TYPE_INTEGER, func);
661 case 'c': /* Callback-Number */
662 q += valuepair2str(q,freespace,pairfind(request->reply->vps,PW_CALLBACK_NUMBER),PW_TYPE_STRING, func);
665 case 'd': /* request day */
666 TM = localtime_r(&request->timestamp, &s_TM);
667 strftime(tmpdt,sizeof(tmpdt),"%d",TM);
668 strNcpy(q,tmpdt,freespace);
672 case 'f': /* Framed IP address */
673 q += valuepair2str(q,freespace,pairfind(request->reply->vps,PW_FRAMED_IP_ADDRESS),PW_TYPE_IPADDR, func);
676 case 'i': /* Calling station ID */
677 q += valuepair2str(q,freespace,pairfind(request->packet->vps,PW_CALLING_STATION_ID),PW_TYPE_STRING, func);
680 case 'l': /* request timestamp */
681 snprintf(tmpdt, sizeof(tmpdt), "%lu",
682 (unsigned long) request->timestamp);
683 strNcpy(q,tmpdt,freespace);
687 case 'm': /* request month */
688 TM = localtime_r(&request->timestamp, &s_TM);
689 strftime(tmpdt,sizeof(tmpdt),"%m",TM);
690 strNcpy(q,tmpdt,freespace);
694 case 'n': /* NAS IP address */
695 q += valuepair2str(q,freespace,pairfind(request->packet->vps,PW_NAS_IP_ADDRESS),PW_TYPE_IPADDR, func);
698 case 'p': /* Port number */
699 q += valuepair2str(q,freespace,pairfind(request->packet->vps,PW_NAS_PORT),PW_TYPE_INTEGER, func);
702 case 's': /* Speed */
703 q += valuepair2str(q,freespace,pairfind(request->packet->vps,PW_CONNECT_INFO),PW_TYPE_STRING, func);
706 case 't': /* request timestamp */
707 CTIME_R(&request->timestamp, q, freespace);
711 case 'u': /* User name */
712 q += valuepair2str(q,freespace,pairfind(request->packet->vps,PW_USER_NAME),PW_TYPE_STRING, func);
715 case 'A': /* radacct_dir */
716 strNcpy(q,radacct_dir,freespace-1);
720 case 'C': /* ClientName */
721 strNcpy(q,client_name(request->packet->src_ipaddr),freespace-1);
725 case 'D': /* request date */
726 TM = localtime_r(&request->timestamp, &s_TM);
727 strftime(tmpdt,sizeof(tmpdt),"%Y%m%d",TM);
728 strNcpy(q,tmpdt,freespace);
732 case 'H': /* request hour */
733 TM = localtime_r(&request->timestamp, &s_TM);
734 strftime(tmpdt,sizeof(tmpdt),"%H",TM);
735 strNcpy(q,tmpdt,freespace);
739 case 'L': /* radlog_dir */
740 strNcpy(q,radlog_dir,freespace-1);
745 q += valuepair2str(q,freespace,pairfind(request->reply->vps,PW_FRAMED_MTU),PW_TYPE_INTEGER, func);
748 case 'R': /* radius_dir */
749 strNcpy(q,radius_dir,freespace-1);
753 case 'S': /* request timestamp in SQL format*/
754 TM = localtime_r(&request->timestamp, &s_TM);
755 strftime(tmpdt,sizeof(tmpdt),"%Y-%m-%d %H:%M:%S",TM);
756 strNcpy(q,tmpdt,freespace);
760 case 'T': /* request timestamp */
761 TM = localtime_r(&request->timestamp, &s_TM);
762 strftime(tmpdt,sizeof(tmpdt),"%Y-%m-%d-%H.%M.%S.000000",TM);
763 strNcpy(q,tmpdt,freespace);
767 case 'U': /* Stripped User name */
768 q += valuepair2str(q,freespace,pairfind(request->packet->vps,PW_STRIPPED_USER_NAME),PW_TYPE_STRING, func);
771 case 'V': /* Request-Authenticator */
772 if (request->packet->verified)
773 strNcpy(q,"Verified",freespace-1);
775 strNcpy(q,"None",freespace-1);
779 case 'Y': /* request year */
780 TM = localtime_r(&request->timestamp, &s_TM);
781 strftime(tmpdt,sizeof(tmpdt),"%Y",TM);
782 strNcpy(q,tmpdt,freespace);
786 case 'Z': /* Full request pairs except password */
787 tmp = request->packet->vps;
788 while (tmp && (freespace > 3)) {
789 if (tmp->attribute != PW_PASSWORD) {
791 i = vp_prints(q,freespace-2,tmp);
801 DEBUG2("WARNING: Unknown variable '%%%c': See 'doc/variables.txt'", *p);
811 DEBUG2("radius_xlat: '%s'", out);