Define a macro for max regex matches, so we don't have typos.
[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                  *      FIXME: Add SRC/DST IP address!
234                  */
235                 if (packet) {
236                         switch (da->attr) {
237                         case PW_PACKET_TYPE:
238                         {
239                                 DICT_VALUE *dval;
240                                 
241                                 dval = dict_valbyattr(da->attr, packet->code);
242                                 if (dval) {
243                                         snprintf(out, outlen, "%s", dval->name);
244                                 } else {
245                                         snprintf(out, outlen, "%d", packet->code);
246                                 }
247                                 return strlen(out);
248                         }
249                         break;
250                         
251                         default:
252                                 break;
253                         }
254                 }
255
256                 /*
257                  *      Not found, die.
258                  */
259                 return 0;
260         }
261
262         if (!vps) return 0;     /* silently fail */
263
264         /*
265          *      Convert the VP to a string, and return it.
266          */
267         return valuepair2str(out, outlen, vp, da->type, func);
268 }
269
270 #ifdef HAVE_REGEX_H
271 /*
272  *      Pull %{0} to %{8} out of the packet.
273  */
274 static int xlat_regex(void *instance, REQUEST *request,
275                       char *fmt, char *out, size_t outlen,
276                       RADIUS_ESCAPE_STRING func)
277 {
278         char *regex;
279
280         /*
281          *      We cheat: fmt is "0" to "8", but those numbers
282          *      are already in the "instance".
283          */
284         fmt = fmt;              /* -Wunused */
285         func = func;            /* -Wunused FIXME: do escaping? */
286         
287         regex = request_data_get(request, request,
288                                  REQUEST_DATA_REGEX | *(int *)instance);
289         if (!regex) return 0;
290
291         /*
292          *      Copy UP TO "freespace" bytes, including
293          *      a zero byte.
294          */
295         strNcpy(out, regex, outlen);
296         free(regex); /* was strdup'd */
297         return strlen(out);
298 }
299 #endif                          /* HAVE_REGEX_H */
300
301 /*
302  *      Compare two xlat_t structs, based ONLY on the module name.
303  */
304 static int xlat_cmp(const void *a, const void *b)
305 {
306         if (((const xlat_t *)a)->length != ((const xlat_t *)b)->length) {
307                 return ((const xlat_t *)a)->length - ((const xlat_t *)b)->length;
308         }
309
310         return memcmp(((const xlat_t *)a)->module,
311                       ((const xlat_t *)b)->module,
312                       ((const xlat_t *)a)->length);
313 }
314
315
316 /*
317  *      find the appropriate registered xlat function.
318  */
319 static xlat_t *xlat_find(const char *module)
320 {
321         char *p;
322         xlat_t my_xlat;
323
324         strNcpy(my_xlat.module, module, sizeof(my_xlat.module));
325
326         /*
327          *      We get passed the WHOLE string, and all we want here
328          *      is the first piece.
329          */
330         p = strchr(my_xlat.module, ':');
331         if (p) *p = '\0';
332
333         my_xlat.length = strlen(my_xlat.module);
334
335         return rbtree_finddata(xlat_root, &my_xlat);
336 }
337
338
339 /*
340  *      Register an xlat function.
341  */
342 int xlat_register(const char *module, RAD_XLAT_FUNC func, void *instance)
343 {
344         xlat_t  *c;
345         xlat_t  my_xlat;
346
347         if ((module == NULL) || (strlen(module) == 0)) {
348                 DEBUG("xlat_register: Invalid module name");
349                 return -1;
350         }
351
352         /*
353          *      First time around, build up the tree...
354          *
355          *      FIXME: This code should be hoisted out of this function,
356          *      and into a global "initialization".  But it isn't critical...
357          */
358         if (!xlat_root) {
359                 int i;
360 #ifdef HAVE_REGEX_H
361                 char buffer[2];
362 #endif
363
364                 xlat_root = rbtree_create(xlat_cmp, free, 0);
365                 if (!xlat_root) {
366                         DEBUG("xlat_register: Failed to create tree.");
367                         return -1;
368                 }
369
370                 /*
371                  *      Register the internal packet xlat's.
372                  */
373                 for (i = 0; internal_xlat[i] != NULL; i++) {
374                         xlat_register(internal_xlat[i], xlat_packet, &xlat_inst[i]);
375                         c = xlat_find(internal_xlat[i]);
376                         rad_assert(c != NULL);
377                         c->internal = TRUE;
378                 }
379
380 #ifdef HAVE_REGEX_H
381                 /*
382                  *      Register xlat's for regexes.
383                  */
384                 buffer[1] = '\0';
385                 for (i = 0; i <= REQUEST_MAX_REGEX; i++) {
386                         buffer[0] = '0' + i;
387                         xlat_register(buffer, xlat_regex, &xlat_inst[i]);
388                         c = xlat_find(buffer);
389                         rad_assert(c != NULL);
390                         c->internal = TRUE;
391                 }
392 #endif /* HAVE_REGEX_H */
393         }
394
395         /*
396          *      If it already exists, replace the instance.
397          */
398         strNcpy(my_xlat.module, module, sizeof(my_xlat.module));
399         my_xlat.length = strlen(my_xlat.module);
400         c = rbtree_finddata(xlat_root, &my_xlat);
401         if (c) {
402                 if (c->internal) {
403                         DEBUG("xlat_register: Cannot re-define internal xlat");
404                         return -1;
405                 }
406
407                 c->do_xlat = func;
408                 c->instance = instance;
409                 return 0;
410         }
411
412         /*
413          *      Doesn't exist.  Create it.
414          */
415         c = rad_malloc(sizeof(xlat_t));
416         memset(c, 0, sizeof(*c));
417
418         c->do_xlat = func;
419         strNcpy(c->module, module, sizeof(c->module));
420         c->length = strlen(c->module);
421         c->instance = instance;
422
423         rbtree_insert(xlat_root, c);
424
425         return 0;
426 }
427
428 /*
429  *      Unregister an xlat function.
430  *
431  *      We can only have one function to call per name, so the
432  *      passing of "func" here is extraneous.
433  */
434 void xlat_unregister(const char *module, RAD_XLAT_FUNC func)
435 {
436         rbnode_t        *node;
437         xlat_t          my_xlat;
438
439         func = func;            /* -Wunused */
440
441         strNcpy(my_xlat.module, module, sizeof(my_xlat.module));
442         my_xlat.length = strlen(my_xlat.module);
443
444         node = rbtree_find(xlat_root, &my_xlat);
445         if (!node) return;
446
447         rbtree_delete(xlat_root, node);
448 }
449
450 /*
451  *      De-register all xlat functions,
452  *      used mainly for debugging.
453  */
454 void xlat_free(void)
455 {
456         rbtree_free(xlat_root);
457 }
458
459
460 /*
461  *      Decode an attribute name into a string.
462  */
463 static void decode_attribute(const char **from, char **to, int freespace,
464                              int *open, REQUEST *request,
465                              RADIUS_ESCAPE_STRING func)
466 {
467         int     do_length = 0;
468         char attrname[256];
469         const char *p;
470         char *q, *pa;
471         int stop=0, found=0, retlen=0;
472         int openbraces = *open;
473         xlat_t *c;
474
475         p = *from;
476         q = *to;
477         pa = &attrname[0];
478
479         *q = '\0';
480
481         /*
482          * Skip the '{' at the front of 'p'
483          * Increment open braces
484          */
485         p++;
486         openbraces++;
487
488         if (*p == '#') {
489                 p++;
490                 do_length = 1;
491         }
492
493         /*
494          *  Copy over the rest of the string.
495          */
496         while ((*p) && (!stop)) {
497                 switch(*p) {
498                         /*
499                          *  Allow braces inside things, too.
500                          */
501                         case '\\':
502                                 p++; /* skip it */
503                                 *pa++ = *p++;
504                                 break;
505
506                         case '{':
507                                 openbraces++;
508                                 *pa++ = *p++;
509                                 break;
510
511                         case '}':
512                                 openbraces--;
513                                 if (openbraces == *open) {
514                                         p++;
515                                         stop=1;
516                                 } else {
517                                         *pa++ = *p++;
518                                 }
519                                 break;
520
521                                 /*
522                                  *  Attr-Name1:-Attr-Name2
523                                  *
524                                  *  Use Attr-Name1, and if not found,
525                                  *  use Attr-Name2.
526                                  */
527                         case ':':
528                                 if (p[1] == '-') {
529                                         p += 2;
530                                         stop = 1;
531                                         break;
532                                 }
533                                 /* else FALL-THROUGH */
534
535                         default:
536                                 *pa++ = *p++;
537                                 break;
538                 }
539         }
540         *pa = '\0';
541
542         /*
543          *      Look up almost everything in the new tree of xlat
544          *      functions.  this makes it a little quicker...
545          */
546         if ((c = xlat_find(attrname)) != NULL) {
547                 if (!c->internal) DEBUG("radius_xlat: Running registered xlat function of module %s for string \'%s\'",
548                                         c->module, attrname+ c->length + 1);
549                 retlen = c->do_xlat(c->instance, request, attrname+(c->length+1), q, freespace, func);
550                 /* If retlen is 0, treat it as not found */
551                 if (retlen > 0) found = 1;
552
553                 /*
554                  *      Not in the default xlat database.  Must be
555                  *      a bare attribute number.
556                  */
557         } else if ((retlen = xlat_packet(&xlat_inst[1], request, attrname,
558                                          q, freespace, func)) > 0) {
559                 found = 1;
560
561                 /*
562                  *      Look up the name, in order to get the correct
563                  *      debug message.
564                  */
565 #ifndef NDEBUG
566         } else if (dict_attrbyname(attrname) == NULL) {
567                 /*
568                  *      No attribute by that name, return an error.
569                  */
570                 DEBUG2("WARNING: Attempt to use unknown xlat function, or non-existent attribute in string %%{%s}", attrname);
571 #endif
572         } /* else the attribute is known, but not in the request */
573
574         /*
575          * Skip to last '}' if attr is found
576          * The rest of the stuff within the braces is
577          * useless if we found what we need
578          */
579         if (found) {
580                 if (do_length) {
581                         snprintf(q, freespace, "%d", retlen);
582                         retlen = strlen(q);
583                 }
584
585                 q += retlen;
586
587                 while((*p != '\0') && (openbraces > 0)) {
588                         /*
589                          *      Handle escapes outside of the loop.
590                          */
591                         if (*p == '\\') {
592                                 p++;
593                                 if (!*p) break;
594                                 p++; /* get & ignore next character */
595                                 continue;
596                         }
597
598                         switch (*p) {
599                         default:
600                                 break;
601
602                                 /*
603                                  *  Bare brace
604                                  */
605                         case '{':
606                                 openbraces++;
607                                 break;
608
609                         case '}':
610                                 openbraces--;
611                                 break;
612                         }
613                         p++;    /* skip the character */
614                 }
615         }
616
617         *open = openbraces;
618         *from = p;
619         *to = q;
620 }
621
622 /*
623  *  If the caller doesn't pass xlat an escape function, then
624  *  we use this one.  It simplifies the coding, as the check for
625  *  func == NULL only happens once.
626  */
627 static int xlat_copy(char *out, int outlen, const char *in)
628 {
629         int len = 0;
630
631         while (*in) {
632                 /*
633                  *  Truncate, if too much.
634                  */
635                 if (len >= outlen) {
636                         break;
637                 }
638
639                 /*
640                  *  Copy data.
641                  *
642                  *  FIXME: Do escaping of bad stuff!
643                  */
644                 *out = *in;
645
646                 out++;
647                 in++;
648                 len++;
649         }
650
651         *out = '\0';
652         return len;
653 }
654
655 /*
656  *      Replace %<whatever> in a string.
657  *
658  *      See 'doc/variables.txt' for more information.
659  */
660 int radius_xlat(char *out, int outlen, const char *fmt,
661                 REQUEST *request, RADIUS_ESCAPE_STRING func)
662 {
663         int i, c,freespace;
664         const char *p;
665         char *q;
666         VALUE_PAIR *tmp;
667         struct tm *TM, s_TM;
668         char tmpdt[40]; /* For temporary storing of dates */
669         int openbraces=0;
670
671         /*
672          *      Catch bad modules.
673          */
674         if (!fmt || !out || !request) return 0;
675
676         /*
677          *  Ensure that we always have an escaping function.
678          */
679         if (func == NULL) {
680                 func = xlat_copy;
681         }
682
683         q = out;
684         p = fmt;
685         while (*p) {
686                 /* Calculate freespace in output */
687                 freespace = outlen - (q - out);
688                 if (freespace <= 1)
689                         break;
690                 c = *p;
691
692                 if ((c != '%') && (c != '$') && (c != '\\')) {
693                         /*
694                          * We check if we're inside an open brace.  If we are
695                          * then we assume this brace is NOT literal, but is
696                          * a closing brace and apply it
697                          */
698                         if ((c == '}') && openbraces) {
699                                 openbraces--;
700                                 p++; /* skip it */
701                                 continue;
702                         }
703                         *q++ = *p++;
704                         continue;
705                 }
706
707                 /*
708                  *      There's nothing after this character, copy
709                  *      the last '%' or "$' or '\\' over to the output
710                  *      buffer, and exit.
711                  */
712                 if (*++p == '\0') {
713                         *q++ = c;
714                         break;
715                 }
716
717                 if (c == '\\') {
718                         switch(*p) {
719                         case '\\':
720                                 *q++ = *p;
721                                 break;
722                         case 't':
723                                 *q++ = '\t';
724                                 break;
725                         case 'n':
726                                 *q++ = '\n';
727                                 break;
728                         default:
729                                 *q++ = c;
730                                 *q++ = *p;
731                                 break;
732                         }
733                         p++;
734
735                         /*
736                          *      Hmmm... ${User-Name} is a synonym for
737                          *      %{User-Name}.
738                          *
739                          *      Why, exactly?
740                          */
741                 } else if (c == '$') switch(*p) {
742                         case '{': /* Attribute by Name */
743                                 decode_attribute(&p, &q, freespace, &openbraces, request, func);
744                                 break;
745                         default:
746                                 *q++ = c;
747                                 *q++ = *p++;
748                                 break;
749
750                 } else if (c == '%') switch(*p) {
751                         case '{':
752                                 decode_attribute(&p, &q, freespace, &openbraces, request, func);
753                                 break;
754
755                         case '%':
756                                 *q++ = *p++;
757                                 break;
758                         case 'a': /* Protocol: */
759                                 q += valuepair2str(q,freespace,pairfind(request->reply->vps,PW_FRAMED_PROTOCOL),PW_TYPE_INTEGER, func);
760                                 p++;
761                                 break;
762                         case 'c': /* Callback-Number */
763                                 q += valuepair2str(q,freespace,pairfind(request->reply->vps,PW_CALLBACK_NUMBER),PW_TYPE_STRING, func);
764                                 p++;
765                                 break;
766                         case 'd': /* request day */
767                                 TM = localtime_r(&request->timestamp, &s_TM);
768                                 strftime(tmpdt,sizeof(tmpdt),"%d",TM);
769                                 strNcpy(q,tmpdt,freespace);
770                                 q += strlen(q);
771                                 p++;
772                                 break;
773                         case 'f': /* Framed IP address */
774                                 q += valuepair2str(q,freespace,pairfind(request->reply->vps,PW_FRAMED_IP_ADDRESS),PW_TYPE_IPADDR, func);
775                                 p++;
776                                 break;
777                         case 'i': /* Calling station ID */
778                                 q += valuepair2str(q,freespace,pairfind(request->packet->vps,PW_CALLING_STATION_ID),PW_TYPE_STRING, func);
779                                 p++;
780                                 break;
781                         case 'l': /* request timestamp */
782                                 snprintf(tmpdt, sizeof(tmpdt), "%lu",
783                                          (unsigned long) request->timestamp);
784                                 strNcpy(q,tmpdt,freespace);
785                                 q += strlen(q);
786                                 p++;
787                                 break;
788                         case 'm': /* request month */
789                                 TM = localtime_r(&request->timestamp, &s_TM);
790                                 strftime(tmpdt,sizeof(tmpdt),"%m",TM);
791                                 strNcpy(q,tmpdt,freespace);
792                                 q += strlen(q);
793                                 p++;
794                                 break;
795                         case 'n': /* NAS IP address */
796                                 q += valuepair2str(q,freespace,pairfind(request->packet->vps,PW_NAS_IP_ADDRESS),PW_TYPE_IPADDR, func);
797                                 p++;
798                                 break;
799                         case 'p': /* Port number */
800                                 q += valuepair2str(q,freespace,pairfind(request->packet->vps,PW_NAS_PORT),PW_TYPE_INTEGER, func);
801                                 p++;
802                                 break;
803                         case 's': /* Speed */
804                                 q += valuepair2str(q,freespace,pairfind(request->packet->vps,PW_CONNECT_INFO),PW_TYPE_STRING, func);
805                                 p++;
806                                 break;
807                         case 't': /* request timestamp */
808                                 CTIME_R(&request->timestamp, q, freespace);
809                                 q += strlen(q);
810                                 p++;
811                                 break;
812                         case 'u': /* User name */
813                                 q += valuepair2str(q,freespace,pairfind(request->packet->vps,PW_USER_NAME),PW_TYPE_STRING, func);
814                                 p++;
815                                 break;
816                         case 'A': /* radacct_dir */
817                                 strNcpy(q,radacct_dir,freespace-1);
818                                 q += strlen(q);
819                                 p++;
820                                 break;
821                         case 'C': /* ClientName */
822                                 strNcpy(q,client_name(request->packet->src_ipaddr),freespace-1);
823                                 q += strlen(q);
824                                 p++;
825                                 break;
826                         case 'D': /* request date */
827                                 TM = localtime_r(&request->timestamp, &s_TM);
828                                 strftime(tmpdt,sizeof(tmpdt),"%Y%m%d",TM);
829                                 strNcpy(q,tmpdt,freespace);
830                                 q += strlen(q);
831                                 p++;
832                                 break;
833                         case 'H': /* request hour */
834                                 TM = localtime_r(&request->timestamp, &s_TM);
835                                 strftime(tmpdt,sizeof(tmpdt),"%H",TM);
836                                 strNcpy(q,tmpdt,freespace);
837                                 q += strlen(q);
838                                 p++;
839                                 break;
840                         case 'L': /* radlog_dir */
841                                 strNcpy(q,radlog_dir,freespace-1);
842                                 q += strlen(q);
843                                 p++;
844                                 break;
845                         case 'M': /* MTU */
846                                 q += valuepair2str(q,freespace,pairfind(request->reply->vps,PW_FRAMED_MTU),PW_TYPE_INTEGER, func);
847                                 p++;
848                                 break;
849                         case 'R': /* radius_dir */
850                                 strNcpy(q,radius_dir,freespace-1);
851                                 q += strlen(q);
852                                 p++;
853                                 break;
854                         case 'S': /* request timestamp in SQL format*/
855                                 TM = localtime_r(&request->timestamp, &s_TM);
856                                 strftime(tmpdt,sizeof(tmpdt),"%Y-%m-%d %H:%M:%S",TM);
857                                 strNcpy(q,tmpdt,freespace);
858                                 q += strlen(q);
859                                 p++;
860                                 break;
861                         case 'T': /* request timestamp */
862                                 TM = localtime_r(&request->timestamp, &s_TM);
863                                 strftime(tmpdt,sizeof(tmpdt),"%Y-%m-%d-%H.%M.%S.000000",TM);
864                                 strNcpy(q,tmpdt,freespace);
865                                 q += strlen(q);
866                                 p++;
867                                 break;
868                         case 'U': /* Stripped User name */
869                                 q += valuepair2str(q,freespace,pairfind(request->packet->vps,PW_STRIPPED_USER_NAME),PW_TYPE_STRING, func);
870                                 p++;
871                                 break;
872                         case 'V': /* Request-Authenticator */
873                                 if (request->packet->verified)
874                                         strNcpy(q,"Verified",freespace-1);
875                                 else
876                                         strNcpy(q,"None",freespace-1);
877                                 q += strlen(q);
878                                 p++;
879                                 break;
880                         case 'Y': /* request year */
881                                 TM = localtime_r(&request->timestamp, &s_TM);
882                                 strftime(tmpdt,sizeof(tmpdt),"%Y",TM);
883                                 strNcpy(q,tmpdt,freespace);
884                                 q += strlen(q);
885                                 p++;
886                                 break;
887                         case 'Z': /* Full request pairs except password */
888                                 tmp = request->packet->vps;
889                                 while (tmp && (freespace > 3)) {
890                                         if (tmp->attribute != PW_PASSWORD) {
891                                                 *q++ = '\t';
892                                                 i = vp_prints(q,freespace-2,tmp);
893                                                 q += i;
894                                                 freespace -= (i+2);
895                                                 *q++ = '\n';
896                                         }
897                                         tmp = tmp->next;
898                                 }
899                                 p++;
900                                 break;
901                         default:
902                                 DEBUG2("WARNING: Unknown variable '%%%c': See 'doc/variables.txt'", *p);
903                                 if (freespace > 2) {
904                                         *q++ = '%';
905                                         *q++ = *p++;
906                                 }
907                                 break;
908                 }
909         }
910         *q = '\0';
911
912         DEBUG2("radius_xlat:  '%s'", out);
913
914         return strlen(out);
915 }