Added xlat support for Packet-Authentication-Vector
[freeradius.git] / src / main / xlat.c
1 /*
2  * xlat.c       Translate strings.  This is the first version of xlat
3  *              incorporated to RADIUS
4  *
5  * Version:     $Id$
6  *
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.
11  *
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.
16  *
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
20  *
21  * Copyright 2000  The FreeRADIUS server project
22  * Copyright 2000  Alan DeKok <aland@ox.org>
23  */
24
25 static const char rcsid[] =
26 "$Id$";
27
28 #include        "autoconf.h"
29 #include        "libradius.h"
30
31 #include        <stdio.h>
32 #include        <stdlib.h>
33 #include        <string.h>
34 #include        <ctype.h>
35
36 #include        "radiusd.h"
37
38 #include        "rad_assert.h"
39
40 typedef struct xlat_t {
41         char            module[MAX_STRING_LEN];
42         int             length;
43         void            *instance;
44         RAD_XLAT_FUNC   do_xlat;
45         int             internal;       /* not allowed to re-define these */
46 } xlat_t;
47
48 static rbtree_t *xlat_root = NULL;
49
50 /*
51  *      Define all xlat's in the structure.
52  */
53 static const char *internal_xlat[] = {"check",
54                                       "request",
55                                       "reply",
56                                       "proxy-request",
57                                       "proxy-reply",
58                                       NULL};
59
60 #if REQUEST_MAX_REGEX > 8
61 #error Please fix the following line
62 #endif
63 static int xlat_inst[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 }; /* up to 8 for regex */
64
65
66 /*
67  *      Convert the value on a VALUE_PAIR to string
68  */
69 static int valuepair2str(char * out,int outlen,VALUE_PAIR * pair,
70                          int type, RADIUS_ESCAPE_STRING func)
71 {
72         char buffer[MAX_STRING_LEN * 4];
73
74         if (pair != NULL) {
75                 vp_prints_value(buffer, sizeof(buffer), pair, -1);
76                 return func(out, outlen, buffer);
77         }
78
79         switch (type) {
80         case PW_TYPE_STRING :
81                 strNcpy(out,"_",outlen);
82                 break;
83         case PW_TYPE_INTEGER :
84                 strNcpy(out,"0",outlen);
85                 break;
86         case PW_TYPE_IPADDR :
87                 strNcpy(out,"?.?.?.?",outlen);
88                 break;
89         case PW_TYPE_DATE :
90                 strNcpy(out,"0",outlen);
91                 break;
92         default :
93                 strNcpy(out,"unknown_type",outlen);
94         }
95         return strlen(out);
96 }
97
98
99 /*
100  *      Dynamically translate for check:, request:, reply:, etc.
101  */
102 static int xlat_packet(void *instance, REQUEST *request,
103                        char *fmt, char *out, size_t outlen,
104                        RADIUS_ESCAPE_STRING func)
105 {
106         DICT_ATTR       *da;
107         VALUE_PAIR      *vp;
108         VALUE_PAIR      *vps = NULL;
109         RADIUS_PACKET   *packet = NULL;
110
111         switch (*(int*) instance) {
112         case 0:
113                 vps = request->config_items;
114                 break;
115
116         case 1:
117                 vps = request->packet->vps;
118                 packet = request->packet;
119                 break;
120
121         case 2:
122                 vps = request->reply->vps;
123                 packet = request->reply;
124                 break;
125
126         case 3:
127                 if (request->proxy) vps = request->proxy->vps;
128                 packet = request->proxy;
129                 break;
130
131         case 4:
132                 if (request->proxy_reply) vps = request->proxy_reply->vps;
133                 packet = request->proxy_reply;
134                 break;
135
136         default:                /* WTF? */
137                 return 0;
138         }
139
140         /*
141          *      The "format" string is the attribute name.
142          */
143         da = dict_attrbyname(fmt);
144         if (!da) {
145                 int index;
146                 const char *p = strchr(fmt, '[');
147                 char buffer[256];
148
149                 if (!p) return 0;
150                 if (strlen(fmt) > sizeof(buffer)) return 0;
151
152                 strNcpy(buffer, fmt, p - fmt + 1);
153
154                 da = dict_attrbyname(buffer);
155                 if (!da) return 0;
156
157                 /*
158                  *      %{Attribute-Name[#]} returns the count of
159                  *      attributes of that name in the list.
160                  */
161                 if ((p[1] == '#') && (p[2] == ']')) {
162                         index = 0;
163
164                         for (vp = pairfind(vps, da->attr);
165                              vp != NULL;
166                              vp = pairfind(vp->next, da->attr)) {
167                                 index++;
168                         }
169                         snprintf(out, outlen, "%d", index);
170                         return strlen(out);
171                 }
172
173                 /*
174                  *      %{Attribute-Name[*]} returns ALL of the
175                  *      the attributes, separated by a newline.
176                  */             
177                 if ((p[1] == '*') && (p[2] == ']')) {
178                         int total = 0;
179
180                         for (vp = pairfind(vps, da->attr);
181                              vp != NULL;
182                              vp = pairfind(vp->next, da->attr)) {
183                                 index = valuepair2str(out, outlen - 1, vp, da->type, func);
184                                 rad_assert(index <= outlen);
185                                 total += index + 1;
186                                 outlen -= (index + 1);
187                                 out += index;
188                                 
189                                 *(out++) = '\n';
190
191                                 if (outlen == 0) break;
192                         }
193
194                         return total;
195                 }
196                 
197                 index = atoi(p + 1);
198
199                 /*
200                  *      Skip the numbers.
201                  */
202                 p += 1 + strspn(p + 1, "0123456789");
203                 if (*p != ']') {
204                         DEBUG2("xlat: Invalid array reference in string at %s %s",
205                                fmt, p);
206                         return 0;
207                 }
208
209                 /*
210                  *      Find the N'th value.
211                  */
212                 for (vp = pairfind(vps, da->attr);
213                      vp != NULL;
214                      vp = pairfind(vp->next, da->attr)) {
215                         if (index == 0) break;
216                         index--;
217                 }
218
219                 /*
220                  *      Non-existent array reference.
221                  */
222                 if (!vp) return 0;
223
224                 return valuepair2str(out, outlen, vp, da->type, func);
225         }
226
227         vp = pairfind(vps, da->attr);
228         if (!vp) {
229                 /*
230                  *      Some "magic" handlers, which are never in VP's, but
231                  *      which are in the packet.
232                  */
233                 if (packet) {
234                         VALUE_PAIR localvp;
235
236                         localvp.strvalue[0] = 0;
237
238                         switch (da->attr) {
239                         case PW_PACKET_TYPE:
240                         {
241                                 DICT_VALUE *dval;
242                                 
243                                 dval = dict_valbyattr(da->attr, packet->code);
244                                 if (dval) {
245                                         snprintf(out, outlen, "%s", dval->name);
246                                 } else {
247                                         snprintf(out, outlen, "%d", packet->code);
248                                 }
249                                 return strlen(out);
250                         }
251                         break;
252
253                         case PW_PACKET_SRC_IP_ADDRESS:
254                                 localvp.attribute = da->attr;
255                                 localvp.lvalue = packet->src_ipaddr;
256                                 break;
257                         
258                         case PW_PACKET_DST_IP_ADDRESS:
259                                 localvp.attribute = da->attr;
260                                 localvp.lvalue = packet->dst_ipaddr;
261                                 break;
262                         
263                         case PW_PACKET_SRC_PORT:
264                                 localvp.attribute = da->attr;
265                                 localvp.lvalue = packet->src_port;
266                                 break;
267                         
268                         case PW_PACKET_DST_PORT:
269                                 localvp.attribute = da->attr;
270                                 localvp.lvalue = packet->dst_port;
271                                 break;
272                         case PW_PACKET_AUTHENTICATION_VECTOR:
273                                 localvp.attribute = da->attr;
274                                 memcpy(localvp.strvalue, packet->vector,
275                                        sizeof(packet->vector));
276                                 localvp.length = sizeof(packet->vector);
277                                 break;
278                         
279                         default:
280                                 return 0; /* not found */
281                                 break;
282                         }
283
284                         localvp.type = da->type;
285                         return valuepair2str(out, outlen, &localvp,
286                                              da->type, func);
287                 }
288
289                 /*
290                  *      Not found, die.
291                  */
292                 return 0;
293         }
294
295         if (!vps) return 0;     /* silently fail */
296
297         /*
298          *      Convert the VP to a string, and return it.
299          */
300         return valuepair2str(out, outlen, vp, da->type, func);
301 }
302
303 #ifdef HAVE_REGEX_H
304 /*
305  *      Pull %{0} to %{8} out of the packet.
306  */
307 static int xlat_regex(void *instance, REQUEST *request,
308                       char *fmt, char *out, size_t outlen,
309                       RADIUS_ESCAPE_STRING func)
310 {
311         char *regex;
312
313         /*
314          *      We cheat: fmt is "0" to "8", but those numbers
315          *      are already in the "instance".
316          */
317         fmt = fmt;              /* -Wunused */
318         func = func;            /* -Wunused FIXME: do escaping? */
319         
320         regex = request_data_get(request, request,
321                                  REQUEST_DATA_REGEX | *(int *)instance);
322         if (!regex) return 0;
323
324         /*
325          *      Copy UP TO "freespace" bytes, including
326          *      a zero byte.
327          */
328         strNcpy(out, regex, outlen);
329         free(regex); /* was strdup'd */
330         return strlen(out);
331 }
332 #endif                          /* HAVE_REGEX_H */
333
334 /*
335  *      Compare two xlat_t structs, based ONLY on the module name.
336  */
337 static int xlat_cmp(const void *a, const void *b)
338 {
339         if (((const xlat_t *)a)->length != ((const xlat_t *)b)->length) {
340                 return ((const xlat_t *)a)->length - ((const xlat_t *)b)->length;
341         }
342
343         return memcmp(((const xlat_t *)a)->module,
344                       ((const xlat_t *)b)->module,
345                       ((const xlat_t *)a)->length);
346 }
347
348
349 /*
350  *      find the appropriate registered xlat function.
351  */
352 static xlat_t *xlat_find(const char *module)
353 {
354         char *p;
355         xlat_t my_xlat;
356
357         strNcpy(my_xlat.module, module, sizeof(my_xlat.module));
358
359         /*
360          *      We get passed the WHOLE string, and all we want here
361          *      is the first piece.
362          */
363         p = strchr(my_xlat.module, ':');
364         if (p) *p = '\0';
365
366         my_xlat.length = strlen(my_xlat.module);
367
368         return rbtree_finddata(xlat_root, &my_xlat);
369 }
370
371
372 /*
373  *      Register an xlat function.
374  */
375 int xlat_register(const char *module, RAD_XLAT_FUNC func, void *instance)
376 {
377         xlat_t  *c;
378         xlat_t  my_xlat;
379
380         if ((module == NULL) || (strlen(module) == 0)) {
381                 DEBUG("xlat_register: Invalid module name");
382                 return -1;
383         }
384
385         /*
386          *      First time around, build up the tree...
387          *
388          *      FIXME: This code should be hoisted out of this function,
389          *      and into a global "initialization".  But it isn't critical...
390          */
391         if (!xlat_root) {
392                 int i;
393 #ifdef HAVE_REGEX_H
394                 char buffer[2];
395 #endif
396
397                 xlat_root = rbtree_create(xlat_cmp, free, 0);
398                 if (!xlat_root) {
399                         DEBUG("xlat_register: Failed to create tree.");
400                         return -1;
401                 }
402
403                 /*
404                  *      Register the internal packet xlat's.
405                  */
406                 for (i = 0; internal_xlat[i] != NULL; i++) {
407                         xlat_register(internal_xlat[i], xlat_packet, &xlat_inst[i]);
408                         c = xlat_find(internal_xlat[i]);
409                         rad_assert(c != NULL);
410                         c->internal = TRUE;
411                 }
412
413 #ifdef HAVE_REGEX_H
414                 /*
415                  *      Register xlat's for regexes.
416                  */
417                 buffer[1] = '\0';
418                 for (i = 0; i <= REQUEST_MAX_REGEX; i++) {
419                         buffer[0] = '0' + i;
420                         xlat_register(buffer, xlat_regex, &xlat_inst[i]);
421                         c = xlat_find(buffer);
422                         rad_assert(c != NULL);
423                         c->internal = TRUE;
424                 }
425 #endif /* HAVE_REGEX_H */
426         }
427
428         /*
429          *      If it already exists, replace the instance.
430          */
431         strNcpy(my_xlat.module, module, sizeof(my_xlat.module));
432         my_xlat.length = strlen(my_xlat.module);
433         c = rbtree_finddata(xlat_root, &my_xlat);
434         if (c) {
435                 if (c->internal) {
436                         DEBUG("xlat_register: Cannot re-define internal xlat");
437                         return -1;
438                 }
439
440                 c->do_xlat = func;
441                 c->instance = instance;
442                 return 0;
443         }
444
445         /*
446          *      Doesn't exist.  Create it.
447          */
448         c = rad_malloc(sizeof(xlat_t));
449         memset(c, 0, sizeof(*c));
450
451         c->do_xlat = func;
452         strNcpy(c->module, module, sizeof(c->module));
453         c->length = strlen(c->module);
454         c->instance = instance;
455
456         rbtree_insert(xlat_root, c);
457
458         return 0;
459 }
460
461 /*
462  *      Unregister an xlat function.
463  *
464  *      We can only have one function to call per name, so the
465  *      passing of "func" here is extraneous.
466  */
467 void xlat_unregister(const char *module, RAD_XLAT_FUNC func)
468 {
469         rbnode_t        *node;
470         xlat_t          my_xlat;
471
472         func = func;            /* -Wunused */
473
474         strNcpy(my_xlat.module, module, sizeof(my_xlat.module));
475         my_xlat.length = strlen(my_xlat.module);
476
477         node = rbtree_find(xlat_root, &my_xlat);
478         if (!node) return;
479
480         rbtree_delete(xlat_root, node);
481 }
482
483 /*
484  *      De-register all xlat functions,
485  *      used mainly for debugging.
486  */
487 void xlat_free(void)
488 {
489         rbtree_free(xlat_root);
490 }
491
492
493 /*
494  *      Decode an attribute name into a string.
495  */
496 static void decode_attribute(const char **from, char **to, int freespace,
497                              int *open, REQUEST *request,
498                              RADIUS_ESCAPE_STRING func)
499 {
500         int     do_length = 0;
501         char attrname[256];
502         const char *p;
503         char *q, *pa;
504         int stop=0, found=0, retlen=0;
505         int openbraces = *open;
506         xlat_t *c;
507
508         p = *from;
509         q = *to;
510         pa = &attrname[0];
511
512         *q = '\0';
513
514         /*
515          * Skip the '{' at the front of 'p'
516          * Increment open braces
517          */
518         p++;
519         openbraces++;
520
521         if (*p == '#') {
522                 p++;
523                 do_length = 1;
524         }
525
526         /*
527          *  Copy over the rest of the string.
528          */
529         while ((*p) && (!stop)) {
530                 switch(*p) {
531                         /*
532                          *  Allow braces inside things, too.
533                          */
534                         case '\\':
535                                 p++; /* skip it */
536                                 *pa++ = *p++;
537                                 break;
538
539                         case '{':
540                                 openbraces++;
541                                 *pa++ = *p++;
542                                 break;
543
544                         case '}':
545                                 openbraces--;
546                                 if (openbraces == *open) {
547                                         p++;
548                                         stop=1;
549                                 } else {
550                                         *pa++ = *p++;
551                                 }
552                                 break;
553
554                                 /*
555                                  *  Attr-Name1:-Attr-Name2
556                                  *
557                                  *  Use Attr-Name1, and if not found,
558                                  *  use Attr-Name2.
559                                  */
560                         case ':':
561                                 if (p[1] == '-') {
562                                         p += 2;
563                                         stop = 1;
564                                         break;
565                                 }
566                                 /* else FALL-THROUGH */
567
568                         default:
569                                 *pa++ = *p++;
570                                 break;
571                 }
572         }
573         *pa = '\0';
574
575         /*
576          *      Look up almost everything in the new tree of xlat
577          *      functions.  this makes it a little quicker...
578          */
579         if ((c = xlat_find(attrname)) != NULL) {
580                 if (!c->internal) DEBUG("radius_xlat: Running registered xlat function of module %s for string \'%s\'",
581                                         c->module, attrname+ c->length + 1);
582                 retlen = c->do_xlat(c->instance, request, attrname+(c->length+1), q, freespace, func);
583                 /* If retlen is 0, treat it as not found */
584                 if (retlen > 0) found = 1;
585
586                 /*
587                  *      Not in the default xlat database.  Must be
588                  *      a bare attribute number.
589                  */
590         } else if ((retlen = xlat_packet(&xlat_inst[1], request, attrname,
591                                          q, freespace, func)) > 0) {
592                 found = 1;
593
594                 /*
595                  *      Look up the name, in order to get the correct
596                  *      debug message.
597                  */
598 #ifndef NDEBUG
599         } else if (dict_attrbyname(attrname) == NULL) {
600                 /*
601                  *      No attribute by that name, return an error.
602                  */
603                 DEBUG2("WARNING: Attempt to use unknown xlat function, or non-existent attribute in string %%{%s}", attrname);
604 #endif
605         } /* else the attribute is known, but not in the request */
606
607         /*
608          * Skip to last '}' if attr is found
609          * The rest of the stuff within the braces is
610          * useless if we found what we need
611          */
612         if (found) {
613                 if (do_length) {
614                         snprintf(q, freespace, "%d", retlen);
615                         retlen = strlen(q);
616                 }
617
618                 q += retlen;
619
620                 while((*p != '\0') && (openbraces > 0)) {
621                         /*
622                          *      Handle escapes outside of the loop.
623                          */
624                         if (*p == '\\') {
625                                 p++;
626                                 if (!*p) break;
627                                 p++; /* get & ignore next character */
628                                 continue;
629                         }
630
631                         switch (*p) {
632                         default:
633                                 break;
634
635                                 /*
636                                  *  Bare brace
637                                  */
638                         case '{':
639                                 openbraces++;
640                                 break;
641
642                         case '}':
643                                 openbraces--;
644                                 break;
645                         }
646                         p++;    /* skip the character */
647                 }
648         }
649
650         *open = openbraces;
651         *from = p;
652         *to = q;
653 }
654
655 /*
656  *  If the caller doesn't pass xlat an escape function, then
657  *  we use this one.  It simplifies the coding, as the check for
658  *  func == NULL only happens once.
659  */
660 static int xlat_copy(char *out, int outlen, const char *in)
661 {
662         int len = 0;
663
664         while (*in) {
665                 /*
666                  *  Truncate, if too much.
667                  */
668                 if (len >= outlen) {
669                         break;
670                 }
671
672                 /*
673                  *  Copy data.
674                  *
675                  *  FIXME: Do escaping of bad stuff!
676                  */
677                 *out = *in;
678
679                 out++;
680                 in++;
681                 len++;
682         }
683
684         *out = '\0';
685         return len;
686 }
687
688 /*
689  *      Replace %<whatever> in a string.
690  *
691  *      See 'doc/variables.txt' for more information.
692  */
693 int radius_xlat(char *out, int outlen, const char *fmt,
694                 REQUEST *request, RADIUS_ESCAPE_STRING func)
695 {
696         int i, c,freespace;
697         const char *p;
698         char *q;
699         VALUE_PAIR *tmp;
700         struct tm *TM, s_TM;
701         char tmpdt[40]; /* For temporary storing of dates */
702         int openbraces=0;
703
704         /*
705          *      Catch bad modules.
706          */
707         if (!fmt || !out || !request) return 0;
708
709         /*
710          *  Ensure that we always have an escaping function.
711          */
712         if (func == NULL) {
713                 func = xlat_copy;
714         }
715
716         q = out;
717         p = fmt;
718         while (*p) {
719                 /* Calculate freespace in output */
720                 freespace = outlen - (q - out);
721                 if (freespace <= 1)
722                         break;
723                 c = *p;
724
725                 if ((c != '%') && (c != '$') && (c != '\\')) {
726                         /*
727                          * We check if we're inside an open brace.  If we are
728                          * then we assume this brace is NOT literal, but is
729                          * a closing brace and apply it
730                          */
731                         if ((c == '}') && openbraces) {
732                                 openbraces--;
733                                 p++; /* skip it */
734                                 continue;
735                         }
736                         *q++ = *p++;
737                         continue;
738                 }
739
740                 /*
741                  *      There's nothing after this character, copy
742                  *      the last '%' or "$' or '\\' over to the output
743                  *      buffer, and exit.
744                  */
745                 if (*++p == '\0') {
746                         *q++ = c;
747                         break;
748                 }
749
750                 if (c == '\\') {
751                         switch(*p) {
752                         case '\\':
753                                 *q++ = *p;
754                                 break;
755                         case 't':
756                                 *q++ = '\t';
757                                 break;
758                         case 'n':
759                                 *q++ = '\n';
760                                 break;
761                         default:
762                                 *q++ = c;
763                                 *q++ = *p;
764                                 break;
765                         }
766                         p++;
767
768                         /*
769                          *      Hmmm... ${User-Name} is a synonym for
770                          *      %{User-Name}.
771                          *
772                          *      Why, exactly?
773                          */
774                 } else if (c == '$') switch(*p) {
775                         case '{': /* Attribute by Name */
776                                 decode_attribute(&p, &q, freespace, &openbraces, request, func);
777                                 break;
778                         default:
779                                 *q++ = c;
780                                 *q++ = *p++;
781                                 break;
782
783                 } else if (c == '%') switch(*p) {
784                         case '{':
785                                 decode_attribute(&p, &q, freespace, &openbraces, request, func);
786                                 break;
787
788                         case '%':
789                                 *q++ = *p++;
790                                 break;
791                         case 'a': /* Protocol: */
792                                 q += valuepair2str(q,freespace,pairfind(request->reply->vps,PW_FRAMED_PROTOCOL),PW_TYPE_INTEGER, func);
793                                 p++;
794                                 break;
795                         case 'c': /* Callback-Number */
796                                 q += valuepair2str(q,freespace,pairfind(request->reply->vps,PW_CALLBACK_NUMBER),PW_TYPE_STRING, func);
797                                 p++;
798                                 break;
799                         case 'd': /* request day */
800                                 TM = localtime_r(&request->timestamp, &s_TM);
801                                 strftime(tmpdt,sizeof(tmpdt),"%d",TM);
802                                 strNcpy(q,tmpdt,freespace);
803                                 q += strlen(q);
804                                 p++;
805                                 break;
806                         case 'f': /* Framed IP address */
807                                 q += valuepair2str(q,freespace,pairfind(request->reply->vps,PW_FRAMED_IP_ADDRESS),PW_TYPE_IPADDR, func);
808                                 p++;
809                                 break;
810                         case 'i': /* Calling station ID */
811                                 q += valuepair2str(q,freespace,pairfind(request->packet->vps,PW_CALLING_STATION_ID),PW_TYPE_STRING, func);
812                                 p++;
813                                 break;
814                         case 'l': /* request timestamp */
815                                 snprintf(tmpdt, sizeof(tmpdt), "%lu",
816                                          (unsigned long) request->timestamp);
817                                 strNcpy(q,tmpdt,freespace);
818                                 q += strlen(q);
819                                 p++;
820                                 break;
821                         case 'm': /* request month */
822                                 TM = localtime_r(&request->timestamp, &s_TM);
823                                 strftime(tmpdt,sizeof(tmpdt),"%m",TM);
824                                 strNcpy(q,tmpdt,freespace);
825                                 q += strlen(q);
826                                 p++;
827                                 break;
828                         case 'n': /* NAS IP address */
829                                 q += valuepair2str(q,freespace,pairfind(request->packet->vps,PW_NAS_IP_ADDRESS),PW_TYPE_IPADDR, func);
830                                 p++;
831                                 break;
832                         case 'p': /* Port number */
833                                 q += valuepair2str(q,freespace,pairfind(request->packet->vps,PW_NAS_PORT),PW_TYPE_INTEGER, func);
834                                 p++;
835                                 break;
836                         case 's': /* Speed */
837                                 q += valuepair2str(q,freespace,pairfind(request->packet->vps,PW_CONNECT_INFO),PW_TYPE_STRING, func);
838                                 p++;
839                                 break;
840                         case 't': /* request timestamp */
841                                 CTIME_R(&request->timestamp, q, freespace);
842                                 q += strlen(q);
843                                 p++;
844                                 break;
845                         case 'u': /* User name */
846                                 q += valuepair2str(q,freespace,pairfind(request->packet->vps,PW_USER_NAME),PW_TYPE_STRING, func);
847                                 p++;
848                                 break;
849                         case 'A': /* radacct_dir */
850                                 strNcpy(q,radacct_dir,freespace-1);
851                                 q += strlen(q);
852                                 p++;
853                                 break;
854                         case 'C': /* ClientName */
855                                 strNcpy(q,client_name(request->packet->src_ipaddr),freespace-1);
856                                 q += strlen(q);
857                                 p++;
858                                 break;
859                         case 'D': /* request date */
860                                 TM = localtime_r(&request->timestamp, &s_TM);
861                                 strftime(tmpdt,sizeof(tmpdt),"%Y%m%d",TM);
862                                 strNcpy(q,tmpdt,freespace);
863                                 q += strlen(q);
864                                 p++;
865                                 break;
866                         case 'H': /* request hour */
867                                 TM = localtime_r(&request->timestamp, &s_TM);
868                                 strftime(tmpdt,sizeof(tmpdt),"%H",TM);
869                                 strNcpy(q,tmpdt,freespace);
870                                 q += strlen(q);
871                                 p++;
872                                 break;
873                         case 'L': /* radlog_dir */
874                                 strNcpy(q,radlog_dir,freespace-1);
875                                 q += strlen(q);
876                                 p++;
877                                 break;
878                         case 'M': /* MTU */
879                                 q += valuepair2str(q,freespace,pairfind(request->reply->vps,PW_FRAMED_MTU),PW_TYPE_INTEGER, func);
880                                 p++;
881                                 break;
882                         case 'R': /* radius_dir */
883                                 strNcpy(q,radius_dir,freespace-1);
884                                 q += strlen(q);
885                                 p++;
886                                 break;
887                         case 'S': /* request timestamp in SQL format*/
888                                 TM = localtime_r(&request->timestamp, &s_TM);
889                                 strftime(tmpdt,sizeof(tmpdt),"%Y-%m-%d %H:%M:%S",TM);
890                                 strNcpy(q,tmpdt,freespace);
891                                 q += strlen(q);
892                                 p++;
893                                 break;
894                         case 'T': /* request timestamp */
895                                 TM = localtime_r(&request->timestamp, &s_TM);
896                                 strftime(tmpdt,sizeof(tmpdt),"%Y-%m-%d-%H.%M.%S.000000",TM);
897                                 strNcpy(q,tmpdt,freespace);
898                                 q += strlen(q);
899                                 p++;
900                                 break;
901                         case 'U': /* Stripped User name */
902                                 q += valuepair2str(q,freespace,pairfind(request->packet->vps,PW_STRIPPED_USER_NAME),PW_TYPE_STRING, func);
903                                 p++;
904                                 break;
905                         case 'V': /* Request-Authenticator */
906                                 if (request->packet->verified)
907                                         strNcpy(q,"Verified",freespace-1);
908                                 else
909                                         strNcpy(q,"None",freespace-1);
910                                 q += strlen(q);
911                                 p++;
912                                 break;
913                         case 'Y': /* request year */
914                                 TM = localtime_r(&request->timestamp, &s_TM);
915                                 strftime(tmpdt,sizeof(tmpdt),"%Y",TM);
916                                 strNcpy(q,tmpdt,freespace);
917                                 q += strlen(q);
918                                 p++;
919                                 break;
920                         case 'Z': /* Full request pairs except password */
921                                 tmp = request->packet->vps;
922                                 while (tmp && (freespace > 3)) {
923                                         if (tmp->attribute != PW_PASSWORD) {
924                                                 *q++ = '\t';
925                                                 i = vp_prints(q,freespace-2,tmp);
926                                                 q += i;
927                                                 freespace -= (i+2);
928                                                 *q++ = '\n';
929                                         }
930                                         tmp = tmp->next;
931                                 }
932                                 p++;
933                                 break;
934                         default:
935                                 DEBUG2("WARNING: Unknown variable '%%%c': See 'doc/variables.txt'", *p);
936                                 if (freespace > 2) {
937                                         *q++ = '%';
938                                         *q++ = *p++;
939                                 }
940                                 break;
941                 }
942         }
943         *q = '\0';
944
945         DEBUG2("radius_xlat:  '%s'", out);
946
947         return strlen(out);
948 }