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

« back to all changes in this revision

Viewing changes to src/lib/misc.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:
1
1
/*
2
2
 * misc.c       Various miscellaneous functions.
3
3
 *
4
 
 * Version:     $Id: misc.c,v 1.82 2008/01/10 10:13:04 aland Exp $
 
4
 * Version:     $Id$
5
5
 *
6
6
 *   This library is free software; you can redistribute it and/or
7
7
 *   modify it under the terms of the GNU Lesser General Public
21
21
 */
22
22
 
23
23
#include        <freeradius-devel/ident.h>
24
 
RCSID("$Id: misc.c,v 1.82 2008/01/10 10:13:04 aland Exp $")
 
24
RCSID("$Id$")
25
25
 
26
26
#include        <freeradius-devel/libradius.h>
27
27
 
29
29
#include        <sys/file.h>
30
30
#include        <fcntl.h>
31
31
 
32
 
int             librad_dodns = 0;
33
 
int             librad_debug = 0;
 
32
int             fr_dns_lookups = 0;
 
33
int             fr_debug_flag = 0;
34
34
 
35
35
 
36
36
/*
50
50
        return buffer;
51
51
}
52
52
 
53
 
 
 
53
#undef F_LOCK
54
54
 
55
55
/*
56
56
 *      Internal wrapper for locking, to minimize the number of ifdef's
59
59
 */
60
60
int rad_lockfd(int fd, int lock_len)
61
61
{
62
 
#if defined(F_LOCK) && !defined(BSD)
 
62
#if defined(F_LOCK)
63
63
        return lockf(fd, F_LOCK, lock_len);
64
64
#elif defined(LOCK_EX)
65
65
        lock_len = lock_len;    /* -Wunused */
412
412
        hints.ai_family = af;
413
413
 
414
414
        if ((error = getaddrinfo(src, NULL, &hints, &res)) != 0) {
415
 
                librad_log("ip_nton: %s", gai_strerror(error));
 
415
                fr_strerror_printf("ip_hton: %s", gai_strerror(error));
416
416
                return -1;
417
417
        }
418
418
 
422
422
        }
423
423
 
424
424
        if (!ai) {
425
 
                librad_log("ip_hton failed to find requested information for host %.100s", src);
 
425
                fr_strerror_printf("ip_hton failed to find requested information for host %.100s", src);
426
426
                freeaddrinfo(ai);
427
427
                return -1;
428
428
        }
447
447
                /* Flow should never reach here */
448
448
        case AF_UNSPEC :
449
449
        default :
450
 
                librad_log("ip_hton found unusable information for host %.100s", src);
 
450
                fr_strerror_printf("ip_hton found unusable information for host %.100s", src);
451
451
                freeaddrinfo(ai);
452
452
                return -1;
453
453
        }
462
462
const char *ip_ntoh(const fr_ipaddr_t *src, char *dst, size_t cnt)
463
463
{
464
464
        struct sockaddr_storage ss;
465
 
        struct sockaddr_in  *s4;
466
 
        int error, len;
 
465
        int error;
 
466
        socklen_t salen;
467
467
 
468
468
        /*
469
469
         *      No DNS lookups
470
470
         */
471
 
        if (!librad_dodns) {
 
471
        if (!fr_dns_lookups) {
472
472
                return inet_ntop(src->af, &(src->ipaddr), dst, cnt);
473
473
        }
474
474
 
475
 
 
476
 
        memset(&ss, 0, sizeof(ss));
477
 
        switch (src->af) {
478
 
        case AF_INET :
479
 
                s4 = (struct sockaddr_in *)&ss;
480
 
                len    = sizeof(struct sockaddr_in);
481
 
                s4->sin_family = AF_INET;
482
 
                s4->sin_port = 0;
483
 
                memcpy(&s4->sin_addr, &src->ipaddr.ip4addr, 4);
484
 
                break;
485
 
 
486
 
#ifdef HAVE_STRUCT_SOCKADDR_IN6
487
 
        case AF_INET6 :
488
 
                {
489
 
                struct sockaddr_in6 *s6;
490
 
 
491
 
                s6 = (struct sockaddr_in6 *)&ss;
492
 
                len    = sizeof(struct sockaddr_in6);
493
 
                s6->sin6_family = AF_INET6;
494
 
                s6->sin6_flowinfo = 0;
495
 
                s6->sin6_port = 0;
496
 
                memcpy(&s6->sin6_addr, &src->ipaddr.ip6addr, 16);
497
 
                break;
498
 
                }
499
 
#endif
500
 
 
501
 
        default :
502
 
                return NULL;
503
 
        }
504
 
 
505
 
        if ((error = getnameinfo((struct sockaddr *)&ss, len, dst, cnt, NULL, 0,
 
475
        if (!fr_ipaddr2sockaddr(src, 0, &ss, &salen)) {
 
476
                return NULL;
 
477
        }
 
478
 
 
479
        if ((error = getnameinfo((struct sockaddr *)&ss, salen, dst, cnt, NULL, 0,
506
480
                                 NI_NUMERICHOST | NI_NUMERICSERV)) != 0) {
507
 
                librad_log("ip_ntoh: %s", gai_strerror(error));
 
481
                fr_strerror_printf("ip_ntoh: %s", gai_strerror(error));
508
482
                return NULL;
509
483
        }
510
484
        return dst;
611
585
 
612
586
        return -1;
613
587
}
 
588
 
 
589
int fr_ipaddr2sockaddr(const fr_ipaddr_t *ipaddr, int port,
 
590
                       struct sockaddr_storage *sa, socklen_t *salen)
 
591
{
 
592
        if (ipaddr->af == AF_INET) {
 
593
                struct sockaddr_in s4;
 
594
 
 
595
                *salen = sizeof(s4);
 
596
 
 
597
                memset(&s4, 0, sizeof(s4));
 
598
                s4.sin_family = AF_INET;
 
599
                s4.sin_addr = ipaddr->ipaddr.ip4addr;
 
600
                s4.sin_port = htons(port);
 
601
                memset(sa, 0, sizeof(*sa));
 
602
                memcpy(sa, &s4, sizeof(s4));
 
603
 
 
604
#ifdef HAVE_STRUCT_SOCKADDR_IN6
 
605
        } else if (ipaddr->af == AF_INET6) {
 
606
                struct sockaddr_in6 s6;
 
607
 
 
608
                *salen = sizeof(s6);
 
609
 
 
610
                memset(&s6, 0, sizeof(s6));
 
611
                s6.sin6_family = AF_INET6;
 
612
                s6.sin6_addr = ipaddr->ipaddr.ip6addr;
 
613
                s6.sin6_port = htons(port);
 
614
                memset(sa, 0, sizeof(*sa));
 
615
                memcpy(sa, &s6, sizeof(s6));
 
616
#endif
 
617
        } else {
 
618
                return 0;
 
619
        }
 
620
 
 
621
        return 1;
 
622
}
 
623
 
 
624
 
 
625
int fr_sockaddr2ipaddr(const struct sockaddr_storage *sa, socklen_t salen,
 
626
                       fr_ipaddr_t *ipaddr, int * port)
 
627
{
 
628
        /*
 
629
         *      FIXME: Check salen against sizeof socket structures.
 
630
         */
 
631
        salen = salen;          /* -Wunused */
 
632
 
 
633
        if (sa->ss_family == AF_INET) {
 
634
                struct sockaddr_in      s4;
 
635
                
 
636
                memcpy(&s4, sa, sizeof(s4));
 
637
                ipaddr->af = AF_INET;
 
638
                ipaddr->ipaddr.ip4addr = s4.sin_addr;
 
639
                if (port) *port = ntohs(s4.sin_port);
 
640
                
 
641
#ifdef HAVE_STRUCT_SOCKADDR_IN6
 
642
        } else if (sa->ss_family == AF_INET6) {
 
643
                struct sockaddr_in6     s6;
 
644
                
 
645
                memcpy(&s6, sa, sizeof(s6));
 
646
                ipaddr->af = AF_INET6;
 
647
                ipaddr->ipaddr.ip6addr = s6.sin6_addr;
 
648
                if (port) *port = ntohs(s6.sin6_port);
 
649
#endif
 
650
 
 
651
        } else {
 
652
                return 0;
 
653
        }
 
654
 
 
655
        return 1;
 
656
}