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

acct.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: acct.c,v 1.16 1999/06/23 23:40:37 cdr Exp $  Copyright 1994-1999 Lucent Technologies, Inc.";

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

#include    <stdio.h>
#include    <netdb.h>
#include    <pwd.h>
#include    <time.h>
#include    <ctype.h>
#include    <unistd.h>
#include    <signal.h>
#include    <errno.h>
#include    <sys/wait.h>

#include    "radius.h"

#define SIGN_NOCLIENT   -1
#define SIGN_MATCH      0
#define SIGN_ZERO 1     /* only returned if accept_zero is set */
#define SIGN_NOMATCH    2

extern char       recv_buffer[4096];
extern char       send_buffer[4096];
extern char       *progname;
extern int        debug_flag;
extern int        accept_zero;
#ifdef VPORTS
extern int              vports_flag;
#endif
extern char       *radacct_dir;
extern char       *radius_dir;
extern UINT4            expiration_seconds;
extern UINT4            warning_seconds;
extern int        errno;
extern struct sockaddr_in     rad_saremote;

void
rad_acctreq(fd)
int fd;
{
      AUTH_REQ                *authreq;
      AUTH_REQ                *radrecv();
      UINT4                   addr;
      char              *ip_hostname();
      char              *ipaddr2strp();
      char              secret[20];
      int               calc_acctreq();
      int               handle_proxy();
      int               logok;
      int               proxied;
      int               rad_accounting();
      int               recfrom();
      int               retsig;
      int                     result;
      size_t                  salen;
      struct  sockaddr_in     *sin;
      u_short                 port;
      time_t                  time();
      void              hexdump();
      void              reqfree();
      void              send_acct_reply();
      void              vp_update_cid();

      salen = sizeof(rad_saremote);
        sin = (struct sockaddr_in *) & rad_saremote;
        result = recvfrom (fd, (char *) recv_buffer,
                (int) sizeof(recv_buffer),
                (int) 0, (struct sockaddr *)&rad_saremote, &salen);

        if (result < AUTH_HDR_LEN) {
                log_err("accounting: runt packet of %d bytes\n",result);
                return;
        }

        addr = ntohl(sin->sin_addr.s_addr);
        port = ntohs(sin->sin_port);
      
      /* Verify the client -- returns shared secret in secret */
      retsig=calc_acctreq(addr,secret,recv_buffer,result);

      /* To be strictly compliant with the RADIUS Accounting RFC
         we only accept packets that returned SIGN_MATCH to
         indicate the Request-Authenticator is valid.
         This requires ComOS 3.3.1 or later.

         If accept_flag is set then we also accept all-zero
         Request-Authenticator.
         */

      if (retsig == SIGN_NOCLIENT) {
            /* We do not respond when this fails */
            log_err("accounting: unknown client %s/%d ignored\n", ipaddr2strp(addr),port);
            return;
      } else if (retsig == SIGN_NOMATCH) {
            log_err("accounting: client %s/%d sent accounting-request with invalid request authenticator\n",ipaddr2strp(addr),port);
            if (debug_flag > 2) {
                  hexdump(recv_buffer, result);
            }
            return;
      } 

      authreq = radrecv( addr, port, secret, recv_buffer, result );

      if (authreq == (AUTH_REQ *)NULL) {
            return;
      }

      /* log it locally */
      logok = rad_accounting(authreq,retsig);

      /* handle_proxy returns 
            1 if it forwarded the packet,
            0 if it did not forward the packet,
         and -1 if there was an error 
       */
      proxied = handle_proxy(authreq);
      if (authreq->flags & REQ_ERR) {
            reqfree(authreq,"rad_acctreq");
            return;
      }

      /* If we did not forward this to a remote server, we now
         let NAS know it is OK to delete from buffer
         If we did forward it to a proxy server, we will notify
         NAS to delete when we get the ack back from the remote
         server.  This means it may wind up in our own logs 
         multiple times, but that is useful to know
       */
      if (proxied == 0) {
            if (logok == 1) {
                  send_acct_reply(authreq, (VALUE_PAIR *)NULL, fd);
            }
            reqfree(authreq,"rad_acctreq");
      }
      if (proxied == 1 && (authreq->flags & REQ_FREE)) {
            reqfree(authreq,"rad_acctreq");
      }
      return;
}

int
rad_accounting(authreq,sign)
AUTH_REQ    *authreq;
int         sign;
{
      FILE        *outfd;
      char        buffer[512];
      char        *client_hostname();
      char        clientname[MAX_HOST_SIZE];
      char        *ctime();
      VALUE_PAIR  *pair;
      time_t            curtime;
      time_t            time();
      int         fclose();
      int         fputs();
      void        fprint_attr_val();
#ifdef VPORTS
      void        vp_update_cid();
#endif

      if (authreq == (AUTH_REQ *)NULL) {
            return 0;
      }

#ifdef VPORTS
        if (vports_flag == 1) {
                vp_update_cid(authreq);
      }
#endif /* VPORTS */


      strncpy(clientname, client_hostname(authreq->ipaddr), MAX_HOST_SIZE);

      /*
       * Create a directory for this client.
       */
      sprintf(buffer, "%s/%s", radacct_dir, clientname);
      mkdir(buffer, (mode_t) 0755);

      /*
       * Write Detail file.
       */
      sprintf(buffer, "%s/%s/detail", radacct_dir, clientname);
      if((outfd = fopen(buffer, "a")) == (FILE *)NULL) {
            log_err("accounting: could not append to file %s\n", buffer);
            /* do not respond if we cannot save record */
            return 0;
      }

      /* Post a timestamp */
      curtime = time(0);
      fputs(ctime(&curtime), outfd);

      /* Write each attribute/value to the log file */
      pair = authreq->request;
      while(pair != (VALUE_PAIR *)NULL) {
            if (pair->attribute != PW_PROXY) {
                  fputs("\t", outfd);
                  fprint_attr_val(outfd, pair);
                  fputs("\n", outfd);
            }
            pair = pair->next;
      }
      /* print the seconds since epoch for easier processing */
      sprintf(buffer,"\tTimestamp = %ld\n",(long)curtime);
      fputs(buffer,outfd);
      if (sign == SIGN_ZERO) {
            fputs("\tRequest-Authenticator = None\n",outfd);
      }
      fputs("\n", outfd);
      fclose(outfd);

      return 1;
}

/*************************************************************************
 *
 *    Function: send_acct_reply
 *
 *    Purpose: Reply to the request with an ACKNOWLEDGE.  Also attach
 *           reply attribute value pairs (not that there should be any)
 *
 *************************************************************************/

void
send_acct_reply(authreq, reply, activefd)
AUTH_REQ    *authreq;
VALUE_PAIR  *reply;
int         activefd;
{
      extern int        report[];
      char              *req2strp();
      int               total_length;
      int               build_packet();
      void              send_packet();

      DEBUG("sending acct-response to %s\n", req2strp(authreq));

      total_length = build_packet(authreq,reply,(char *)NULL,PW_ACCOUNTING_RESPONSE,FW_REPLY,send_buffer);

      send_packet(activefd,authreq->ipaddr,authreq->udp_port,
                send_buffer,total_length);

      report[RR_ACCOUNT]++;

}

/*************************************************************************
 *
 *    Function: calc_acctreq
 *
 *    Purpose: Validates the requesting client NAS.  Calculates 
 *           the accounting-request signature based on the 
 *           client's private key.
 *    Returns: -1 Client not found
 *            0 signature matched expected value
 *            1 signature was all-zero and accept_zero flag is true
 *            2 signature was non-zero and did not match
 *
 *************************************************************************/

int
calc_acctreq(client,secret,buffer,buflen)
UINT4 client;
char  *secret;
u_char      *buffer;
int   buflen;
{
      extern int  accept_zero;
      AUTH_HDR *auth;
      u_char      digest[AUTH_VECTOR_LEN];
      u_char      savedigest[AUTH_VECTOR_LEN];
      char  hostnm[256];
      int   secretlen;
      int   len;
      int   find_client();
      void  md5_calc();

      /*
       * Validate the requesting IP address -
       * Not secure, but worth the check for accidental requests
       * find_client() logs an error message if needed
       */
      if(find_client(client, secret, hostnm) != 0) {
            return(SIGN_NOCLIENT);
      }

      /*
       * The NAS and RADIUS accounting server share a secret.
       * The Request Authenticator field in Accounting-Request packets
       * contains a one-way MD5 hash calculated over a stream of octets
       * consisting of the Code + Identifier + Length + 16 zero octets +
       * request attributes + shared secret (where + indicates
       * concatenation).  The 16 octet MD5 hash value is stored in the
       * Authenticator field of the Accounting-Request packet.
       */

      auth = (AUTH_HDR *)buffer;
      memset(savedigest, 0, AUTH_VECTOR_LEN);
      if (accept_zero && memcmp(savedigest,auth->vector,AUTH_VECTOR_LEN) == 0) {
            return(SIGN_ZERO);
      }
      len = ntohs(auth->length);
      memcpy(savedigest,auth->vector,AUTH_VECTOR_LEN);
      memset(auth->vector, 0, AUTH_VECTOR_LEN);
      secretlen = strlen((char *)secret);
      memcpy(buffer+len,secret,secretlen);
      md5_calc(digest, buffer, len+secretlen);
      memcpy(auth->vector,savedigest,AUTH_VECTOR_LEN);
      memset(buffer+len,0,secretlen);
      if (memcmp(digest,savedigest,AUTH_VECTOR_LEN) == 0) {
            return(SIGN_MATCH);
      } else {
            return(SIGN_NOMATCH);
      }
      /* secret is returned as a side-effect */ 
}




Generated by  Doxygen 1.6.0   Back to index