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