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