Logo Search packages:      
Sourcecode: radiusd-livingston version File versions  Download package

util.c

/***********************************************************************

RADIUS
Remote Authentication Dial In User Service

Lucent Technologies Remote Access
4464 Willow Road
Pleasanton, CA   94588

Copyright 1992-1999 Lucent Technologies Inc.  All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

   * Redistributions of source code must retain the above copyright
     notice, this list of conditions and the following disclaimer.

   * Redistributions in binary form must reproduce the above
     copyright notice, this list of conditions and the following
     disclaimer in the documentation and/or other materials provided
     with the distribution.

   * All advertising materials mentioning features or use of this
     software must display the following acknowledgement:

        This product includes software developed by Lucent
        Technologies and its contributors.

   * Neither the name of the copyright holder nor the names of its
     contributors may be used to endorse or promote products derived
     from this software without specific prior written permission.

This software is provided by the copyright holders and contributors
``as is'' and any express or implied warranties, including, but not
limited to, the implied warranties of merchantability and fitness for a
particular purpose are disclaimed. In no event shall the copyright
holder or contributors be liable for any direct, indirect, incidental,
special, exemplary, or consequential damages (including, but not
limited to, procurement of substitute goods or services; loss of use,
data, or profits; or business interruption) however caused and on any
theory of liability, whether in contract, strict liability, or tort
(including negligence or otherwise) arising in any way out of the use
of this software, even if advised of the possibility of such damage.

************************************************************************/


static char sccsid[] =
"$Id: util.c,v 1.12 1999/06/23 23:40:45 cdr Exp $ Copyright 1992-1999 Lucent Technologies Inc";

#include    <sys/types.h>
#include    <sys/socket.h>
#include    <sys/time.h>
#include    <netinet/in.h>

#include    <stdio.h>
#include    <netdb.h>
#include    <pwd.h>
#include    <time.h>
#include    <ctype.h>
#include    <string.h>

#include    "radius.h"

/* Memory structures allocated, for debugging purposes */
int hmembuf = 0;  /* highwater marks */
int hmempair = 0;
int hmempeer = 0;
int hmemreq = 0;

int nmembuf = 0;  /* number allocated but not freed */
int nmempair = 0;
int nmempeer = 0;
int nmemreq = 0;

int tmembuf = 0;  /* total number allocated */
int tmempair = 0;
int tmempeer = 0;
int tmemreq = 0;

extern int debug_mem;

/*************************************************************************
 *
 *    Function: ip_hostname
 *
 *    Purpose: Return a printable host name (or IP address in dot notation)
 *           for the supplied IP address.
 *
 *************************************************************************/

char  *
ip_hostname(ipaddr)
UINT4 ipaddr;
{
      struct      hostent *hp;
      static char hstname[128];
      UINT4 n_ipaddr;
      void  ipaddr2str();

      n_ipaddr = htonl(ipaddr);
      hp = gethostbyaddr((char *)&n_ipaddr, sizeof (struct in_addr), AF_INET);
      if (hp == 0) {
            ipaddr2str(hstname, ipaddr);
            return(hstname);
      }
      return(hp->h_name);
}

/*************************************************************************
 *
 *    Function: get_ipaddr
 *
 *    Purpose: Return an IP address in host long notation from a host
 *           name or address in dot notation.
 *
 *************************************************************************/

UINT4
get_ipaddr(host)
char  *host;
{
      struct hostent *hp;
      UINT4       ipstr2long();
      int         good_ipaddr();

      if(good_ipaddr(host) == 0) {
            return(ipstr2long(host));
      }
      else if((hp = gethostbyname(host)) == (struct hostent *)NULL) {
            return((UINT4)0);
      }
      return(ntohl(*(UINT4 *)hp->h_addr));
}

/*************************************************************************
 *
 *    Function: good_ipaddr
 *
 *    Purpose: Check for valid IP address in standard dot notation.
 *
 *************************************************************************/

int
good_ipaddr(addr)
char  *addr;
{
      int   dot_count;
      int   digit_count;

      dot_count = 0;
      digit_count = 0;
      while(*addr != '\0' && *addr != ' ') {
            if(*addr == '.') {
                  dot_count++;
                  digit_count = 0;
            }
            else if(!isdigit(*addr)) {
                  dot_count = 5;
            }
            else {
                  digit_count++;
                  if(digit_count > 3) {
                        dot_count = 5;
                  }
            }
            addr++;
      }
      if(dot_count != 3) {
            return(-1);
      }
      else {
            return(0);
      }
}

/*************************************************************************
 *
 *    Function: ipaddr2str
 *
 *    Purpose: Return an IP address in standard dot notation for the
 *           provided address in host long notation.
 *
 *************************************************************************/

void
ipaddr2str(buffer, ipaddr)
char  *buffer;
UINT4 ipaddr;
{
      int   addr_byte[4];
      int   i;
      UINT4 xbyte;

      for(i = 0;i < 4;i++) {
            xbyte = ipaddr >> (i*8);
            xbyte = xbyte & (UINT4)0x000000FF;
            addr_byte[i] = xbyte;
      }
      sprintf(buffer, "%u.%u.%u.%u", addr_byte[3], addr_byte[2],
            addr_byte[1], addr_byte[0]);
}

/*************************************************************************
 *
 *    Function: ipaddr2strp
 *
 *    Purpose: Return an IP address in standard dot notation for the
 *           provided address in host long notation.
 *
 *************************************************************************/

char  *
ipaddr2strp(ipaddr)
UINT4 ipaddr;
{
      int   addr_byte[4];
      int   i;
      UINT4 xbyte;
      static char buffer[32];

      for(i = 0;i < 4;i++) {
            xbyte = ipaddr >> (i*8);
            xbyte = xbyte & (UINT4)0x000000FF;
            addr_byte[i] = xbyte;
      }
      sprintf(buffer, "%u.%u.%u.%u", addr_byte[3], addr_byte[2],
            addr_byte[1], addr_byte[0]);
      return(buffer);
}


/*************************************************************************
 *
 *    Function: ipstr2long
 *
 *    Purpose: Return an IP address in host long notation from
 *           one supplied in standard dot notation.
 *
 *************************************************************************/

UINT4
ipstr2long(ip_str)
char  *ip_str;
{
      char  buf[6];
      char  *ptr;
      int   i;
      int   count;
      UINT4 ipaddr;
      int   cur_byte;
      int   atoi();

      ipaddr = (UINT4)0;
      for(i = 0;i < 4;i++) {
            ptr = buf;
            count = 0;
            *ptr = '\0';
            while(*ip_str != '.' && *ip_str != '\0' && count < 4) {
                  if(!isdigit(*ip_str)) {
                        return((UINT4)0);
                  }
                  *ptr++ = *ip_str++;
                  count++;
            }
            if(count >= 4 || count == 0) {
                  return((UINT4)0);
            }
            *ptr = '\0';
            cur_byte = atoi(buf);
            if(cur_byte < 0 || cur_byte > 255) {
                  return((UINT4)0);
            }
            ip_str++;
            ipaddr = ipaddr << 8 | (UINT4)cur_byte;
      }
      return(ipaddr);
}


/*************************************************************************
 *
 *    Function: req2str
 *            req2strp
 *
 *    Purpose: Return an IP address in standard dot notation,
 *           followed by / port . id, for the provided authreq
 *
 *************************************************************************/

char  *
req2str(buffer,authreq)
char   *buffer;
AUTH_REQ *authreq;
{
      int   addr_byte[4];
      int   i;
      UINT4 xbyte;
      UINT4 ipaddr;

      if (buffer == (char *)NULL) {
            return buffer;
      }

      if (authreq == (AUTH_REQ *)NULL) {
            memcpy(buffer,"(unidentified)",15);
            return (buffer);
      }

      ipaddr = authreq->ipaddr;

      for(i = 0;i < 4;i++) {
            xbyte = ipaddr >> (i*8);
            xbyte = xbyte & (UINT4)0x000000FF;
            addr_byte[i] = xbyte;
      }
      sprintf(buffer, "%u.%u.%u.%u/%u.%u", addr_byte[3], addr_byte[2],
            addr_byte[1], addr_byte[0], authreq->udp_port, authreq->id);

      return(buffer);
}


char  *
req2strp(authreq)
AUTH_REQ *authreq;
{
      static char buffer[32];

      return req2str(buffer,authreq);
}


/*************************************************************************
 *
 *      Function: bufalloc
 *
 *      Purpose: Allocate memory for use by a buffer
 *
 *************************************************************************/

char *
bufalloc(size,where)
int size;
char *where;
{
      extern int  hmembuf;
      extern int  nmembuf;
      extern int  tmembuf;
      extern int  debug_mem;
      char        *buf;
      void        rad_exit();

      if((buf = (char *)malloc((unsigned)size)) == (char *)NULL) {
                log_err("%s: fatal system error: out of memory, exiting\n",where);
                rad_exit(-1);
        }

      /* no need to zero buffer since we'll be copying into it */
      nmembuf++;
      tmembuf++;
      if (nmembuf > hmembuf) {
            hmembuf = nmembuf;
      }
      if (debug_mem) {
            log_err("called bufalloc(%d,%s) = %p %d\n",size,where,buf,nmembuf);
      }

      return buf;

}



/*************************************************************************
 *
 *      Function: buffree
 *
 *      Purpose: Release the memory allocated by bufalloc();
 *
 *************************************************************************/

void
buffree(buf,where)
char        *buf;
char        *where;
{
      extern int nmembuf;
      extern int debug_mem;

      if (debug_mem) {
            log_err("called buffree(%p,%s) = %d\n",buf,where,nmembuf);
      }
        if (buf == (char *)NULL) {
            log_err("%s called buffree with NULL pointer\n",where);
        } else {
                free(buf);
            nmembuf--;
      }
      /* calling routine took care to zero buffer if it had anything sensitive */ 
}


/*************************************************************************
 *
 *      Function: pairalloc
 *
 *      Purpose: Allocate memory for VALUE_PAIR
 *
 *************************************************************************/

VALUE_PAIR *
pairalloc(where)
char *where;
{
      extern int  hmempair;
      extern int  nmempair;
      extern int  tmempair;
      VALUE_PAIR  *pair;
      void        rad_exit();

      if((pair = (VALUE_PAIR *)malloc(sizeof(VALUE_PAIR))) == (VALUE_PAIR *)NULL) {
                log_err("%s: fatal system error: out of memory, exiting\n",where);
                rad_exit(-1);
        }

        memset(pair,0,sizeof(VALUE_PAIR));
      pair->next = (VALUE_PAIR *)NULL;

      nmempair++;
      tmempair++;
      if (nmempair > hmempair) {
            hmempair = nmempair;
      }

      return pair;

}


/*************************************************************************
 *
 *    Function: pairfree
 *
 *    Purpose: Release the memory used by a list of attribute-value
 *           pairs.
 *
 *************************************************************************/

void
pairfree(pair,where)
VALUE_PAIR  *pair;
char        *where;
{
      extern int  nmempair;
      VALUE_PAIR  *next;

      while(pair != (VALUE_PAIR *)NULL) {
            next = pair->next;
            memset(pair,0,sizeof(VALUE_PAIR));
            free(pair);
            pair = next;
            nmempair--;
      }
}



/*************************************************************************
 *
 *      Function: peeralloc
 *
 *      Purpose: Allocate memory for PEER
 *
 *************************************************************************/

PEER *
peeralloc(where)
char *where;
{
      extern int  hmempeer;
      extern int  nmempeer;
      extern int  tmempeer;
      PEER        *peer;
      void        rad_exit();

      if((peer = (PEER *)malloc(sizeof(PEER))) == (PEER *)NULL) {
                log_err("%s: fatal system error: out of memory, exiting\n",
                  where);
                rad_exit(-1);
        }

        memset(peer,0,sizeof(PEER));
      peer->next = (PEER *)NULL;

      nmempeer++;
      tmempeer++;
      if (nmempeer > hmempeer) {
            hmempeer = nmempeer;
      }

      return peer;

}


/*************************************************************************
 *
 *    Function: peerfree
 *
 *    Purpose: Release the memory used by a PEER structure
 *
 *************************************************************************/

void
peerfree(peer,where)
PEER  *peer;
char        *where;
{
      extern int  nmempeer;
      void        reqfree();

      if (peer == (PEER *)NULL) {
            log_err("%s called peerfree with NULL pointer\n",where);
      } else {
            if (peer->next != (PEER *)NULL) {
                  DEBUG("%s called peerfree with live next pointer\n",
                        where);
            }
            memset(peer,0,sizeof(PEER));
            free(peer);
            nmempeer--;
      }
}




/*************************************************************************
 *
 *      Function: reqalloc
 *
 *      Purpose: Allocate memory for use by an AUTH_REQ structure
 *
 *************************************************************************/

AUTH_REQ *
reqalloc(where)
char *where;
{
      extern int  hmemreq;
      extern int  nmemreq;
      extern int  tmemreq;
      extern int  debug_mem;
      AUTH_REQ    *authreq;
      void        rad_exit();

      if((authreq = (AUTH_REQ *)malloc(sizeof(AUTH_REQ))) == (AUTH_REQ *)NULL) {
                log_err("%s: fatal system error: out of memory, exiting\n",where);
                rad_exit(-1);
        }

        memset(authreq,0,sizeof(AUTH_REQ));
      authreq->request = (VALUE_PAIR *)NULL;
      authreq->next = (AUTH_REQ *)NULL;
      authreq->packet = (char *)NULL;

      nmemreq++;
      tmemreq++;
      if (nmemreq > hmemreq) {
            hmemreq = nmemreq;
      }
      if (debug_mem) {
            log_err("called reqalloc(%s) = %p %d\n",where,authreq,nmemreq);
      }

      return authreq;

}



/*************************************************************************
 *
 *      Function: reqfree
 *
 *      Purpose: Release the memory used by an AUTH_REQ structure
 *
 *************************************************************************/

void
reqfree(authreq,where)
AUTH_REQ        *authreq;
char        *where;
{
      extern int nmemreq;
      extern int debug_mem;

      if (debug_mem) {
            log_err("called reqfree(%p,%s) = %d\n",authreq,where,nmemreq);
      }
        if (authreq == (AUTH_REQ *)NULL) {
            log_err("%s called reqfree with NULL pointer\n",where);
        } else {
                pairfree(authreq->request,where);
            if (authreq->packet != (char *) NULL) {
                  buffree(authreq->packet,where);
            }
                memset(authreq, 0, sizeof(AUTH_REQ));
                free(authreq);
            nmemreq--;
      }
}

void
memreport()
{

      extern int nmembuf,hmembuf,tmembuf, nmempair,hmempair,tmempair, 
            nmempeer,hmempeer,tmempeer, hmemreq,nmemreq,tmemreq;

      log_err("memory usage = pair %d/%d/%d  peer %d/%d/%d  req %d/%d/%d  buf %d/%d/%d\n",
      nmempair,hmempair,tmempair, nmempeer,hmempeer,tmempeer, 
      nmemreq,hmemreq,tmemreq, nmembuf,hmembuf,tmembuf );
     
}


/*************************************************************************
 *
 *    Function: fprint_attr_val
 *
 *    Purpose: Write a printable version of the attribute-value
 *           pair to the supplied File.
 *
 *************************************************************************/

void
fprint_attr_val(fd, pair)
FILE        *fd;
VALUE_PAIR  *pair;
{
      DICT_VALUE  *dict_valget();
      DICT_VALUE  *dval;
      char        buffer[32];
      char        prtbuf[1060];
      int         len;
      u_char            *ptr;
      void        fprint_attr_putc();
      void        ipaddr2str();

      switch(pair->type) {

      case PW_TYPE_STRING:
            sprintf(prtbuf, "%s = \"", pair->name);
            ptr = (u_char *)pair->strvalue;
            len = pair->lvalue;
            while(len-- > 0) {
                  if(!(isprint(*ptr))) {
                        sprintf(buffer, "\\%03o", *ptr);
                        strcat(prtbuf, buffer);
                  }
                  else {
                        fprint_attr_putc(*ptr, prtbuf);
                  }
                  ptr++;
            }
            fprint_attr_putc('"', prtbuf);
            break;
                  
      case PW_TYPE_INTEGER:
            dval = dict_valget(pair->lvalue, pair->name);
            if(dval != (DICT_VALUE *)NULL) {
                  sprintf(prtbuf, "%s = %s", pair->name, dval->name);
            }
            else {
                  sprintf(prtbuf, "%s = %ld", pair->name, pair->lvalue);
            }
            break;

      case PW_TYPE_IPADDR:
            ipaddr2str(buffer, pair->lvalue);
            sprintf(prtbuf, "%s = %s", pair->name, buffer);
            break;

      case PW_TYPE_DATE:
            strftime(buffer, sizeof(buffer), "%b %e %Y",
                              gmtime((time_t *)&pair->lvalue));
            sprintf(prtbuf, "%s = \"%s\"", pair->name, buffer);
            break;

      default:
            sprintf(prtbuf, "Unknown type %d", pair->type);
            break;
      }
      if (fd == (FILE *)-1) {
            /*
             * send to debug log
             */
            log_debug("%s\n", prtbuf);
      } else {
            fputs(prtbuf, fd);
      }
}

void
fprint_attr_putc(cc, buf)
u_char cc;
u_char * buf;
{
      int len;

      len = strlen((const char *)buf);
      buf[len] = cc;
      buf[len+1] = (u_char)0;
}


/*************************************************************************
 *
 *      Function: hexdump
 *
 *      Purpose: log buffer in hex
 *
 *************************************************************************/

void
hexdump(buf,n)
u_char *buf;
int n;
{
      int i;
      int j = 0;
      char s[64];

      if (n > 200) {
            n = 200;
      }
      s[48] = '\n';
      s[48] = '\0';
      for (i = 0; i < n; i++) {     
            j = i & 0x0f;
            sprintf(&s[3*j],"%02x ",(int)buf[i]);
            if (j == 15) {
                  log_debug("%s\n",s);
            }
      }
      if (n > 0 && j < 15) {
            s[3*j+3] = '\n';
            s[3*j+3] = '\0';
            log_debug("%s\n",s);
      }

}

Generated by  Doxygen 1.6.0   Back to index