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