~ubuntu-branches/ubuntu/natty/freeradius/natty-updates

« back to all changes in this revision

Viewing changes to src/main/xlat.c

  • Committer: Bazaar Package Importer
  • Author(s): Josip Rodin
  • Date: 2009-11-23 03:57:37 UTC
  • mfrom: (1.2.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 28.
  • Revision ID: james.westby@ubuntu.com-20091123035737-zsgtzhfych8hir68
Tags: 2.1.7+dfsg-1
* Adopting the package, closes: #536623.
* New upstream version, closes: #513484.
  + Fixes the blooper in unlang evaluation logic, closes: #526175.
* Used quilt (and added README.source), and moved upstream file patching
  into debian/patches/. The source is no longer in collab-maint git
  (to make it simpler for me to finally get this out the door), but
  kept the .gitignore should we need that again.
* Dropped the dialup_admin/bin/backup_radacct patch (integrated upstream).
* Dropped the raddb/Makefile patch (problem no longer exists upstream).
* Dropped the lib/packet.c lib/radius.c main/listen.c patches (was from
  upstream 2.0.5 anyway).
* Dropped references to otp.conf, it no longer exists upstream.
  Keep removing the conffile statoverride in prerm.
* Dropped references to snmp.conf, it no longer exists upstream.
  Keep removing the conffile statoverride in prerm.
* Ship /etc/freeradius/modules/* in the freeradius package.
* Stop shipping sites-enabled symlinks in the package and instead create
  them only on initial install, thanks to Matej Vela, closes: #533396.
* Add export PATH="${PATH:+$PATH:}/usr/sbin:/sbin" to the init script
  at the request of John Morrissey, closes: #550143.
* Stop installing /var/run/freeradius in the package to silence Lintian.
  The init script already recreates it at will.
* Remove executable bit from example.pl to silence Lintian.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 * xlat.c       Translate strings.  This is the first version of xlat
3
3
 *              incorporated to RADIUS
4
4
 *
5
 
 * Version:     $Id: xlat.c,v 1.135 2008/03/16 17:59:29 aland Exp $
 
5
 * Version:     $Id$
6
6
 *
7
7
 *   This program is free software; you can redistribute it and/or modify
8
8
 *   it under the terms of the GNU General Public License as published by
23
23
 */
24
24
 
25
25
#include <freeradius-devel/ident.h>
26
 
RCSID("$Id: xlat.c,v 1.135 2008/03/16 17:59:29 aland Exp $")
 
26
RCSID("$Id$")
27
27
 
28
28
#include        <freeradius-devel/radiusd.h>
 
29
#include        <freeradius-devel/md5.h>
29
30
#include        <freeradius-devel/rad_assert.h>
30
31
 
31
32
#include        <ctype.h>
122
123
                break;
123
124
 
124
125
        case 3:
 
126
#ifdef WITH_PROXY
125
127
                if (request->proxy) vps = request->proxy->vps;
126
128
                packet = request->proxy;
 
129
#endif
127
130
                break;
128
131
 
129
132
        case 4:
 
133
#ifdef WITH_PROXY
130
134
                if (request->proxy_reply) vps = request->proxy_reply->vps;
131
135
                packet = request->proxy_reply;
 
136
#endif
132
137
                break;
133
138
 
134
139
        case 5:
178
183
                             vp = pairfind(vp->next, da->attr)) {
179
184
                                count++;
180
185
                        }
181
 
                        snprintf(out, outlen, "%d", count);
 
186
                        snprintf(out, outlen, "%d", (int) count);
182
187
                        return strlen(out);
183
188
                }
184
189
 
213
218
                 */
214
219
                p += 1 + strspn(p + 1, "0123456789");
215
220
                if (*p != ']') {
216
 
                        DEBUG2("xlat: Invalid array reference in string at %s %s",
 
221
                        RDEBUG2("xlat: Invalid array reference in string at %s %s",
217
222
                               fmt, p);
218
223
                        return 0;
219
224
                }
410
415
 
411
416
 
412
417
/*
 
418
 *      Change the debugging level.
 
419
 */
 
420
static size_t xlat_debug(UNUSED void *instance, REQUEST *request,
 
421
                          char *fmt, char *out, size_t outlen,
 
422
                          UNUSED RADIUS_ESCAPE_STRING func)
 
423
{
 
424
        int level = 0;
 
425
 
 
426
        if (*fmt) level = atoi(fmt);
 
427
 
 
428
        if (level == 0) {
 
429
                request->options = RAD_REQUEST_OPTION_NONE;
 
430
                request->radlog = NULL;
 
431
        } else {
 
432
                if (level > 4) level = 4;
 
433
 
 
434
                request->options = level;
 
435
                request->radlog = radlog_request;
 
436
        }
 
437
 
 
438
        snprintf(out, outlen, "%d", level);
 
439
        return strlen(out);
 
440
}
 
441
 
 
442
 
 
443
/*
 
444
 *      Calculate the MD5 hash of a string.
 
445
 */
 
446
static size_t xlat_md5(UNUSED void *instance, REQUEST *request,
 
447
                       char *fmt, char *out, size_t outlen,
 
448
                       UNUSED RADIUS_ESCAPE_STRING func)
 
449
{
 
450
        int i;
 
451
        uint8_t digest[16];
 
452
        FR_MD5_CTX ctx;
 
453
        char buffer[1024];
 
454
 
 
455
        if (!radius_xlat(buffer, sizeof(buffer), fmt, request, func)) {
 
456
                *out = '\0';
 
457
                return 0;
 
458
        }
 
459
 
 
460
        fr_MD5Init(&ctx);
 
461
        fr_MD5Update(&ctx, (void *) buffer, strlen(buffer));
 
462
        fr_MD5Final(digest, &ctx);
 
463
 
 
464
        if (outlen < 33) {
 
465
                snprintf(out, outlen, "md5_overflow");
 
466
                return strlen(out);
 
467
        }
 
468
 
 
469
        for (i = 0; i < 16; i++) {
 
470
                snprintf(out + i * 2, 3, "%02x", digest[i]);
 
471
        }
 
472
 
 
473
        return strlen(out);
 
474
}
 
475
 
 
476
/*
413
477
 *      Compare two xlat_t structs, based ONLY on the module name.
414
478
 */
415
479
static int xlat_cmp(const void *a, const void *b)
508
572
                        c->internal = TRUE;
509
573
                }
510
574
#endif /* HAVE_REGEX_H */
 
575
 
 
576
 
 
577
                xlat_register("debug", xlat_debug, &xlat_inst[0]);
 
578
                c = xlat_find("debug");
 
579
                rad_assert(c != NULL);
 
580
                c->internal = TRUE;
 
581
 
 
582
                xlat_register("md5", xlat_md5, &xlat_inst[0]);
 
583
                c = xlat_find("md5");
 
584
                rad_assert(c != NULL);
 
585
                c->internal = TRUE;
511
586
        }
512
587
 
513
588
        /*
624
699
                 */
625
700
                int len1, len2;
626
701
                size_t mylen = strlen(p);
627
 
                char *first = rad_malloc(mylen);
628
 
                char *second = rad_malloc(mylen);
 
702
                char *first = rad_malloc(mylen + 1);
 
703
                char *second = rad_malloc(mylen + 1);
629
704
                int expand2 = FALSE;
630
705
 
631
706
                len1 = rad_copy_variable(first, p);
632
707
                if (len1 < 0) {
633
 
                        DEBUG2("Badly formatted variable: %s", p);
 
708
                        RDEBUG2("Badly formatted variable: %s", p);
634
709
                        goto free_and_done;
635
710
                }
636
711
 
637
712
                if ((p[len1] != ':') || (p[len1 + 1] != '-')) {
638
 
                        DEBUG2("No trailing :- after variable at %s", p);
 
713
                        RDEBUG2("No trailing :- after variable at %s", p);
639
714
                        goto free_and_done;
640
715
                }
641
716
 
646
721
 
647
722
                        expand2 = TRUE;
648
723
                        if (len2 < 0) {
649
 
                                DEBUG2("Invalid text after :- at %s", p);
 
724
                                RDEBUG2("Invalid text after :- at %s", p);
650
725
                                goto free_and_done;
651
726
                        }
652
727
                        p += len2;
664
739
                }
665
740
 
666
741
                if (*p != '}') {
667
 
                        DEBUG2("Failed to find trailing '}' in string");
 
742
                        RDEBUG2("Failed to find trailing '}' in string");
668
743
                        goto free_and_done;
669
744
                }
670
745
 
707
782
                         *      Skip to the end of the input
708
783
                         */
709
784
                        p += strlen(p);
710
 
                        DEBUG("xlat: Module name is too long in string %%%s",
 
785
                        RDEBUG("xlat: Module name is too long in string %%%s",
711
786
                              *from);
712
787
                        goto done;
713
788
                }
715
790
        *pa = '\0';
716
791
 
717
792
        if (!*p) {
718
 
                DEBUG("xlat: Invalid syntax in %s", *from);
 
793
                RDEBUG("xlat: Invalid syntax in %s", *from);
719
794
 
720
795
                /*
721
796
                 *      %{name} is a simple attribute reference,
730
805
                goto do_xlat;
731
806
 
732
807
        } else if ((p[0] == ':') && (p[1] == '-')) { /* handle ':- */
733
 
                DEBUG2("WARNING: Deprecated conditional expansion \":-\".  See \"man unlang\" for details");
 
808
                RDEBUG2("WARNING: Deprecated conditional expansion \":-\".  See \"man unlang\" for details");
734
809
                p += 2;
735
810
                xlat_string = xlat_name;
736
811
                goto do_xlat;
831
906
                 */
832
907
        do_xlat:
833
908
                if ((c = xlat_find(xlat_name)) != NULL) {
834
 
                        if (!c->internal) DEBUG3("radius_xlat: Running registered xlat function of module %s for string \'%s\'",
 
909
                        if (!c->internal) RDEBUG3("radius_xlat: Running registered xlat function of module %s for string \'%s\'",
835
910
                                                c->module, xlat_string);
836
911
                        retlen = c->do_xlat(c->instance, request, xlat_string,
837
912
                                            q, freespace, func);
838
913
                        /* If retlen is 0, treat it as not found */
839
914
                        if (retlen > 0) found = 1;
840
915
 
841
 
#ifndef NDEBUG
 
916
#ifndef NRDEBUG
842
917
                } else {
843
918
                        /*
844
919
                         *      No attribute by that name, return an error.
845
920
                         */
846
 
                        DEBUG2("WARNING: Unknown module \"%s\" in string expansion \"%%%s\"", xlat_name, *from);
 
921
                        RDEBUG2("WARNING: Unknown module \"%s\" in string expansion \"%%%s\"", xlat_name, *from);
847
922
#endif
848
923
                }
849
924
        }
1041
1116
                                break;
1042
1117
                        case 'l': /* request timestamp */
1043
1118
                                snprintf(tmpdt, sizeof(tmpdt), "%lu",
1044
 
                                         (unsigned long) request->received.tv_sec);
 
1119
                                         (unsigned long) request->timestamp);
1045
1120
                                strlcpy(q,tmpdt,freespace);
1046
1121
                                q += strlen(q);
1047
1122
                                p++;
1172
1247
                                p++;
1173
1248
                                break;
1174
1249
                        default:
1175
 
                                DEBUG2("WARNING: Unknown variable '%%%c': See 'doc/variables.txt'", *p);
 
1250
                                RDEBUG2("WARNING: Unknown variable '%%%c': See 'doc/variables.txt'", *p);
1176
1251
                                if (freespace > 2) {
1177
1252
                                        *q++ = '%';
1178
1253
                                        *q++ = *p++;
1182
1257
        }
1183
1258
        *q = '\0';
1184
1259
 
1185
 
        DEBUG2("\texpand: %s -> %s", fmt, out);
 
1260
        RDEBUG2("\texpand: %s -> %s", fmt, out);
1186
1261
 
1187
1262
        return strlen(out);
1188
1263
}