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