Auth_RADIUS
[ class tree: Auth_RADIUS ] [ index: Auth_RADIUS ] [ all elements ]

Source for file RADIUS.php

Documentation is available at RADIUS.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. /*
  4. Copyright (c) 2003, Michael Bretterklieber <michael@bretterklieber.com>
  5. All rights reserved.
  6.  
  7. Redistribution and use in source and binary forms, with or without 
  8. modification, are permitted provided that the following conditions 
  9. are met:
  10.  
  11. 1. Redistributions of source code must retain the above copyright 
  12.    notice, this list of conditions and the following disclaimer.
  13. 2. Redistributions in binary form must reproduce the above copyright 
  14.    notice, this list of conditions and the following disclaimer in the 
  15.    documentation and/or other materials provided with the distribution.
  16. 3. The names of the authors may not be used to endorse or promote products 
  17.    derived from this software without specific prior written permission.
  18.  
  19. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
  20. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
  21. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
  22. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
  23. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
  24. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
  25. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
  26. OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
  27. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
  28. EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29.  
  30. This code cannot simply be copied and put under the GNU Public License or 
  31. any other GPL-like (LGPL, GPL2) License.
  32.  
  33.     $Id: RADIUS.php,v 1.5 2004/03/25 15:48:40 mbretter Exp $
  34. */
  35.  
  36. require_once 'PEAR.php';
  37.  
  38. /**
  39. * Client implementation of RADIUS. This are wrapper classes for
  40. * the RADIUS PECL.
  41. * Provides RADIUS Authentication (RFC2865) and RADIUS Accounting (RFC2866).
  42. *
  43. @package Auth_RADIUS
  44. @author  Michael Bretterklieber <michael@bretterklieber.com>
  45. @access  public
  46. @version $Revision: 1.5 $
  47. */
  48.  
  49. PEAR::loadExtension('radius');
  50.  
  51. /**
  52.  * class Auth_RADIUS
  53.  *
  54.  * Abstract base class for RADIUS
  55.  *
  56.  * @package Auth_RADIUS
  57.  */
  58. class Auth_RADIUS extends PEAR {
  59.  
  60.     /**
  61.      * List of RADIUS servers.
  62.      * @var  array 
  63.      * @see  addServer(), putServer()
  64.      */
  65.     var $_servers  = array();
  66.     
  67.     /**
  68.      * Path to the configuration-file.
  69.      * @var  string 
  70.      * @see  setConfigFile()
  71.      */
  72.     var $_configfile = null;
  73.     
  74.     /**
  75.      * Resource.
  76.      * @var  resource 
  77.      * @see  open(), close()
  78.      */
  79.     var $res = null;
  80.     
  81.     /**
  82.      * Username for authentication and accounting requests.
  83.      * @var  string 
  84.      */
  85.     var $username = null;
  86.  
  87.     /**
  88.      * Password for plaintext-authentication (PAP).
  89.      * @var  string 
  90.      */
  91.     var $password = null;
  92.     
  93.     /**
  94.      * List of known attributes.
  95.      * @var  array 
  96.      * @see  dumpAttributes(), getAttributes()
  97.      */
  98.     var $attributes = array();
  99.     
  100.     /**
  101.      * List of raw attributes.
  102.      * @var  array 
  103.      * @see  dumpAttributes(), getAttributes()
  104.      */
  105.     var $rawAttributes = array();
  106.  
  107.     /**
  108.      * List of raw vendor specific attributes.
  109.      * @var  array 
  110.      * @see  dumpAttributes(), getAttributes()
  111.      */
  112.     var $rawVendorAttributes = array();    
  113.     
  114.     /**
  115.      * Constructor
  116.      *
  117.      * Loads the RADIUS PECL/extension
  118.      *
  119.      * @return void 
  120.      */
  121.     function Auth_RADIUS(
  122.     {
  123.         $this->PEAR();
  124.     }
  125.     
  126.     /**
  127.      * Adds a RADIUS server to the list of servers for requests.
  128.      *
  129.      * At most 10 servers may be specified.    When multiple servers
  130.      * are given, they are tried in round-robin fashion until a
  131.      * valid response is received
  132.      *
  133.      * @access public
  134.      * @param  string  $servername   Servername or IP-Address
  135.      * @param  integer $port         Portnumber
  136.      * @param  string  $sharedSecret Shared secret
  137.      * @param  integer $timeout      Timeout for each request
  138.      * @param  integer $maxtries     Max. retries for each request
  139.      * @return void 
  140.      */
  141.     function addServer($servername 'localhost'$port = 0$sharedSecret 'testing123'$timeout = 3$maxtries = 3
  142.     {
  143.         $this->_servers[= array($servername$port$sharedSecret$timeout$maxtries);    
  144.     }
  145.     
  146.     /**
  147.      * Returns an error message, if an error occurred.
  148.      *
  149.      * @access public
  150.      * @return string 
  151.      */
  152.     function getError(
  153.     {
  154.         return radius_strerror($this->res);
  155.     }
  156.     
  157.     /**
  158.      * Sets the configuration-file.
  159.      *
  160.      * @access public
  161.      * @param  string  $file Path to the configuration file
  162.      * @return void 
  163.      */    
  164.     function setConfigfile($file
  165.     {
  166.         $this->_configfile $file;
  167.     }
  168.  
  169.     /**
  170.      * Puts an attribute.
  171.      *
  172.      * @access public
  173.      * @param  integer $attrib       Attribute-number
  174.      * @param  mixed   $port         Attribute-value
  175.      * @param  type    $type         Attribute-type
  176.      * @return bool  true on success, false on error
  177.      */    
  178.     function putAttribute($attrib$value$type = null
  179.     {
  180.         if ($type == null{
  181.             $type gettype($value);
  182.         }
  183.  
  184.         switch ($type{
  185.         case 'integer':
  186.             return radius_put_int($this->res$attrib$value);
  187.         
  188.         case 'addr':
  189.             return radius_put_addr($this->res$attrib$value);
  190.             
  191.         case 'string':
  192.         default:
  193.             return radius_put_attr($this->res$attrib$value);
  194.         }
  195.  
  196.     }
  197.     
  198.     /**
  199.      * Puts a vendor-specific attribute.
  200.      *
  201.      * @access public
  202.      * @param  integer $vendor       Vendor (MSoft, Cisco, ...)
  203.      * @param  integer $attrib       Attribute-number
  204.      * @param  mixed   $port         Attribute-value
  205.      * @param  type    $type         Attribute-type
  206.      * @return bool  true on success, false on error
  207.      */ 
  208.     function putVendorAttribute($vendor$attrib$value$type = null
  209.     {
  210.         
  211.         if ($type == null{
  212.             $type gettype($value);
  213.         }
  214.         
  215.         switch ($type{
  216.         case 'integer':
  217.             return radius_put_vendor_int($this->res$vendor$attrib$value);
  218.         
  219.         case 'addr':
  220.             return radius_put_vendor_addr($this->res$vendor,$attrib$value);
  221.             
  222.         case 'string':
  223.         default:
  224.             return radius_put_vendor_attr($this->res$vendor$attrib$value);
  225.         }
  226.         
  227.     }    
  228.  
  229.     /**
  230.      * Prints known attributes received from the server.
  231.      *
  232.      * @access public
  233.      */     
  234.     function dumpAttributes()
  235.     {
  236.         foreach ($this->attributes as $name => $data{
  237.             echo "$name:$data<br>\n";
  238.         }
  239.     }
  240.     
  241.     /**
  242.      * Overwrite this.
  243.      *
  244.      * @access public
  245.      */         
  246.     function open(
  247.     {
  248.     }
  249.  
  250.     /**
  251.      * Overwrite this.
  252.      *
  253.      * @access public
  254.      */         
  255.     function createRequest()
  256.     {
  257.     }
  258.     
  259.     /**
  260.      * Puts standard attributes.
  261.      *
  262.      * @access public
  263.      */ 
  264.     function putStandardAttributes()
  265.     {
  266.         if (isset($_SERVER)) {
  267.             $var &$_SERVER;
  268.         else {
  269.             $var &$GLOBALS['HTTP_SERVER_VARS'];
  270.         }
  271.                 
  272.         $this->putAttribute(RADIUS_NAS_IDENTIFIERisset($var['HTTP_HOST']$var['HTTP_HOST''localhost');
  273.         $this->putAttribute(RADIUS_NAS_PORT_TYPERADIUS_VIRTUAL);
  274.         $this->putAttribute(RADIUS_SERVICE_TYPERADIUS_FRAMED);
  275.         $this->putAttribute(RADIUS_FRAMED_PROTOCOLRADIUS_PPP);
  276.         $this->putAttribute(RADIUS_CALLING_STATION_IDisset($var['REMOTE_HOST']$var['REMOTE_HOST''127.0.0.1');
  277.     }
  278.     
  279.     /**
  280.      * Puts custom attributes.
  281.      *
  282.      * @access public
  283.      */ 
  284.     function putAuthAttributes()
  285.     {
  286.         if (isset($this->username)) {
  287.             $this->putAttribute(RADIUS_USER_NAME$this->username);        
  288.         }
  289.     }
  290.     
  291.     /**
  292.      * Configures the radius library.
  293.      *
  294.      * @access public
  295.      * @param  string  $servername   Servername or IP-Address
  296.      * @param  integer $port         Portnumber
  297.      * @param  string  $sharedSecret Shared secret
  298.      * @param  integer $timeout      Timeout for each request
  299.      * @param  integer $maxtries     Max. retries for each request
  300.      * @return bool  true on success, false on error
  301.      * @see addServer()
  302.      */      
  303.     function putServer($servername$port = 0$sharedsecret 'testing123'$timeout = 3$maxtries = 3
  304.     {
  305.         if (!radius_add_server($this->res$servername$port$sharedsecret$timeout$maxtries)) {
  306.             return false;
  307.         }
  308.         return true;
  309.     }
  310.     
  311.     /**
  312.      * Configures the radius library via external configurationfile
  313.      *
  314.      * @access public
  315.      * @param  string  $servername   Servername or IP-Address
  316.      * @return bool  true on success, false on error
  317.      */      
  318.     function putConfigfile($file
  319.     {
  320.         if (!radius_config($this->res$file)) {
  321.             return false;
  322.         }
  323.         return true;
  324.     }    
  325.         
  326.     /**
  327.      * Initiates a RADIUS request.
  328.      *
  329.      * @access public
  330.      * @return bool  true on success, false on errors
  331.      */ 
  332.     function start()
  333.     {
  334.         if (!$this->open()) {
  335.             return false;
  336.         }
  337.         
  338.         foreach ($this->_servers as $s{
  339.             // Servername, port, sharedsecret, timeout, retries
  340.             if (!$this->putServer($s[0]$s[1]$s[2]$s[3]$s[4])) {
  341.                 return false;
  342.             }
  343.         }
  344.         
  345.         if (!empty($this->_configfile)) {
  346.             if (!$this->putConfigfile($this->_configfile)) {
  347.                 return false;
  348.             }
  349.         }
  350.         
  351.         $this->createRequest();
  352.         $this->putStandardAttributes();
  353.         $this->putAuthAttributes();
  354.         return true;
  355.     }
  356.     
  357.     /**
  358.      * Sends a prepared RADIUS request and waits for a response
  359.      *
  360.      * @access public
  361.      * @return mixed  true on success, false on reject, PEAR_Error on error
  362.      */ 
  363.     function send()
  364.     {
  365.         $req = radius_send_request($this->res);
  366.         if (!$req{
  367.             return $this->raiseError('Error sending request: ' $this->getError());
  368.         }
  369.  
  370.         switch($req{
  371.         case RADIUS_ACCESS_ACCEPT:
  372.             if (is_subclass_of($this'auth_radius_acct')) {
  373.                 return $this->raiseError('RADIUS_ACCESS_ACCEPT is unexpected for accounting');
  374.             }
  375.             return true;
  376.  
  377.         case RADIUS_ACCESS_REJECT:
  378.             return false;
  379.             
  380.         case RADIUS_ACCOUNTING_RESPONSE:
  381.             if (is_subclass_of($this'auth_radius_pap')) {
  382.                 return $this->raiseError('RADIUS_ACCOUNTING_RESPONSE is unexpected for authentication');
  383.             }
  384.             return true;
  385.  
  386.         default:
  387.             return $this->raiseError("Unexpected return value: $req");
  388.         }    
  389.         
  390.     }
  391.  
  392.     /**
  393.      * Reads all received attributes after sending the request.
  394.      *
  395.      * This methos stores know attributes in the property attributes,
  396.      * all attributes (including known attibutes) are stored in rawAttributes
  397.      * or rawVendorAttributes.
  398.      * NOTE: call this functio also even if the request was rejected, because the
  399.      * Server returns usualy an errormessage
  400.      *
  401.      * @access public
  402.      * @return bool   true on success, false on error
  403.      */     
  404.     function getAttributes()
  405.     {
  406.  
  407.         while ($attrib = radius_get_attr($this->res)) {
  408.  
  409.             if (!is_array($attrib)) {
  410.                 return false;
  411.             }        
  412.  
  413.             $attr $attrib['attr'];
  414.             $data $attrib['data'];
  415.  
  416.             $this->rawAttributes[$attr$data;
  417.  
  418.             switch ($attr{
  419.             case RADIUS_FRAMED_IP_ADDRESS:
  420.                 $this->attributes['framed_ip'= radius_cvt_addr($data);
  421.                 break;
  422.  
  423.             case RADIUS_FRAMED_IP_NETMASK:
  424.                 $this->attributes['framed_mask'= radius_cvt_addr($data);
  425.                 break;
  426.  
  427.             case RADIUS_FRAMED_MTU:
  428.                 $this->attributes['framed_mtu'= radius_cvt_int($data);
  429.                 break;
  430.  
  431.             case RADIUS_FRAMED_COMPRESSION:
  432.                 $this->attributes['framed_compression'= radius_cvt_int($data);
  433.                 break;
  434.  
  435.             case RADIUS_SESSION_TIMEOUT:
  436.                 $this->attributes['session_timeout'= radius_cvt_int($data);
  437.                 break;
  438.  
  439.             case RADIUS_IDLE_TIMEOUT:
  440.                 $this->attributes['idle_timeout'= radius_cvt_int($data);
  441.                 break;
  442.  
  443.             case RADIUS_SERVICE_TYPE:
  444.                 $this->attributes['service_type'= radius_cvt_int($data);
  445.                 break;
  446.  
  447.             case RADIUS_CLASS:
  448.                 $this->attributes['class'= radius_cvt_int($data);
  449.                 break;
  450.  
  451.             case RADIUS_FRAMED_PROTOCOL:
  452.                 $this->attributes['framed_protocol'= radius_cvt_int($data);
  453.                 break;
  454.  
  455.             case RADIUS_FRAMED_ROUTING:
  456.                 $this->attributes['framed_routing'= radius_cvt_int($data);
  457.                 break;
  458.  
  459.             case RADIUS_FILTER_ID:
  460.                 $this->attributes['filter_id'= radius_cvt_string($data);
  461.                 break;
  462.  
  463.             case RADIUS_VENDOR_SPECIFIC:
  464.                 $attribv = radius_get_vendor_attr($data);
  465.                 if (!is_array($attribv)) {
  466.                     return false;
  467.                 }
  468.                     
  469.                 $vendor $attribv['vendor'];
  470.                 $attrv $attribv['attr'];
  471.                 $datav $attribv['data'];
  472.                 
  473.                 $this->rawVendorAttributes[$vendor][$attrv$datav;
  474.  
  475.                 if ($vendor == RADIUS_VENDOR_MICROSOFT{
  476.  
  477.                     switch ($attrv{
  478.                     case RADIUS_MICROSOFT_MS_CHAP2_SUCCESS:
  479.                         $this->attributes['ms_chap2_success'= radius_cvt_string($datav);
  480.                         break;
  481.  
  482.                     case RADIUS_MICROSOFT_MS_CHAP_ERROR:
  483.                         $this->attributes['ms_chap_error'= radius_cvt_string(substr($datav,1));
  484.                         break;
  485.  
  486.                     case RADIUS_MICROSOFT_MS_CHAP_DOMAIN:
  487.                         $this->attributes['ms_chap_domain'= radius_cvt_string($datav);
  488.                         break;
  489.  
  490.                     case RADIUS_MICROSOFT_MS_MPPE_ENCRYPTION_POLICY:
  491.                         $this->attributes['ms_mppe_encryption_policy'= radius_cvt_int($datav);
  492.                         break;
  493.  
  494.                     case RADIUS_MICROSOFT_MS_MPPE_ENCRYPTION_TYPES:
  495.                         $this->attributes['ms_mppe_encryption_types'= radius_cvt_int($datav);
  496.                         break;
  497.  
  498.                     case RADIUS_MICROSOFT_MS_CHAP_MPPE_KEYS:
  499.                         $demangled = radius_demangle($this->res$datav);
  500.                         $this->attributes['ms_chap_mppe_lm_key'substr($demangled08);
  501.                         $this->attributes['ms_chap_mppe_nt_key'substr($demangled8RADIUS_MPPE_KEY_LEN);
  502.                         break;
  503.  
  504.                     case RADIUS_MICROSOFT_MS_MPPE_SEND_KEY:
  505.                         $this->attributes['ms_chap_mppe_send_key'= radius_demangle_mppe_key($this->res$datav);
  506.                         break;
  507.  
  508.                     case RADIUS_MICROSOFT_MS_MPPE_RECV_KEY:
  509.                         $this->attributes['ms_chap_mppe_recv_key'= radius_demangle_mppe_key($this->res$datav);
  510.                         break;
  511.  
  512.                     case RADIUS_MICROSOFT_MS_PRIMARY_DNS_SERVER:
  513.                         $this->attributes['ms_primary_dns_server'= radius_cvt_string($datav);
  514.                         break;
  515.                     }
  516.                 }
  517.                 break;
  518.                 
  519.             }
  520.         }    
  521.  
  522.         return true;
  523.     }
  524.     
  525.     /**
  526.      * Frees resources.
  527.      *
  528.      * Calling this method is always a good idea, because all security relevant
  529.      * attributes are filled with Nullbytes to leave nothing in the mem.
  530.      *
  531.      * @access public
  532.      */   
  533.     function close()
  534.     {
  535.         if ($this->res != null{
  536.             radius_close($this->res);
  537.             $this->res = null;
  538.         }
  539.         $this->username = str_repeat("\0"strlen($this->username));
  540.         $this->password = str_repeat("\0"strlen($this->password));
  541.     }
  542.     
  543. }
  544.  
  545. /**
  546.  * class Auth_RADIUS_PAP
  547.  *
  548.  * Class for authenticating using PAP (Plaintext)
  549.  * 
  550.  * @package Auth_RADIUS
  551.  */
  552. class Auth_RADIUS_PAP extends Auth_RADIUS 
  553. {
  554.  
  555.     /**
  556.      * Constructor
  557.      *
  558.      * @param  string  $username   Username
  559.      * @param  string  $password   Password
  560.      * @return void 
  561.      */
  562.     function Auth_RADIUS_PAP($username = null$password = null)
  563.     {
  564.         $this->Auth_RADIUS();
  565.         $this->username = $username;
  566.         $this->password = $password;
  567.     }
  568.     
  569.     /**
  570.      * Creates a RADIUS resource
  571.      *
  572.      * Creates a RADIUS resource for authentication. This should be the first
  573.      * call before you make any other things with the library.
  574.      *
  575.      * @return bool   true on success, false on error
  576.      */
  577.     function open(
  578.     {
  579.         $this->res = radius_auth_open();
  580.         if (!$this->res{
  581.             return false;
  582.         }
  583.         return true;
  584.     }
  585.     
  586.     /**
  587.      * Creates an authentication request
  588.      *
  589.      * Creates an authentication request.
  590.      * You MUST call this method before you can put any attribute
  591.      *
  592.      * @return bool   true on success, false on error
  593.      */
  594.     function createRequest()
  595.     {
  596.         if (!radius_create_request($this->resRADIUS_ACCESS_REQUEST)) {
  597.             return false;
  598.         }
  599.         return true;
  600.     }
  601.  
  602.     /**
  603.      * Put authentication specific attributes
  604.      *
  605.      * @return void 
  606.      */
  607.     function putAuthAttributes()
  608.     {
  609.         if (isset($this->username)) {
  610.             $this->putAttribute(RADIUS_USER_NAME$this->username);        
  611.         }
  612.         if (isset($this->password)) {
  613.             $this->putAttribute(RADIUS_USER_PASSWORD$this->password);
  614.         }
  615.     }
  616.  
  617. }
  618.  
  619. /**
  620.  * class Auth_RADIUS_CHAP_MD5
  621.  *
  622.  * Class for authenticating using CHAP-MD5 see RFC1994.
  623.  * Instead og the plaintext password the challenge and
  624.  * the response are needed.
  625.  * 
  626.  * @package Auth_RADIUS
  627.  */
  628. {
  629.     /**
  630.      * 8 Bytes binary challenge
  631.      * @var  string 
  632.      */
  633.     var $challenge = null;
  634.  
  635.     /**
  636.      * 16 Bytes MD5 response binary
  637.      * @var  string 
  638.      */
  639.     var $response = null;
  640.     
  641.     /**
  642.      * Id of the authentication request. Should incremented after every request.
  643.      * @var  integer 
  644.      */
  645.     var $chapid = 1;
  646.     
  647.     /**
  648.      * Constructor
  649.      *
  650.      * @param  string  $username   Username
  651.      * @param  string  $challenge  8 Bytes Challenge (binary)
  652.      * @param  integer $chapid     Requestnumber
  653.      * @return void 
  654.      */
  655.     function Auth_RADIUS_CHAP_MD5($username = null$challenge = null$chapid = 1)
  656.     {
  657.         $this->Auth_RADIUS_PAP();
  658.         $this->username = $username;
  659.         $this->challenge = $challenge;
  660.         $this->chapid = $chapid;
  661.     }
  662.     
  663.     /**
  664.      * Put CHAP-MD5 specific attributes
  665.      *
  666.      * For authenticating using CHAP-MD5 via RADIUS you have to put the challenge
  667.      * and the response. The chapid is inserted in the first byte of the response.
  668.      *
  669.      * @return void 
  670.      */
  671.     function putAuthAttributes()
  672.     {
  673.         if (isset($this->username)) {
  674.             $this->putAttribute(RADIUS_USER_NAME$this->username);        
  675.         }
  676.         if (isset($this->response)) {
  677.             $response pack('C'$this->chapid$this->response;
  678.             $this->putAttribute(RADIUS_CHAP_PASSWORD$response);
  679.         }
  680.         if (isset($this->challenge)) {
  681.             $this->putAttribute(RADIUS_CHAP_CHALLENGE$this->challenge);
  682.         }
  683.     }
  684.     
  685.     /**
  686.      * Frees resources.
  687.      *
  688.      * Calling this method is always a good idea, because all security relevant
  689.      * attributes are filled with Nullbytes to leave nothing in the mem.
  690.      *
  691.      * @access public
  692.      */   
  693.     function close()
  694.     {
  695.         Auth_RADIUS_PAP::close();
  696.         $this->challenge =  str_repeat("\0"strlen($this->challenge));
  697.         $this->response =  str_repeat("\0"strlen($this->response));
  698.     }    
  699.     
  700. }
  701.  
  702. /**
  703.  * class Auth_RADIUS_MSCHAPv1
  704.  *
  705.  * Class for authenticating using MS-CHAPv1 see RFC2433
  706.  * 
  707.  * @package Auth_RADIUS
  708.  */
  709. {
  710.     /**
  711.      * LAN-Manager-Response
  712.      * @var  string 
  713.      */
  714.     var $lmResponse = null;
  715.  
  716.     /**
  717.      * Wether using deprecated LM-Responses or not.
  718.      * 0 = use LM-Response, 1 = use NT-Response
  719.      * @var  bool 
  720.      */
  721.     var $flags = 1;
  722.     
  723.     /**
  724.      * Put MS-CHAPv1 specific attributes
  725.      *
  726.      * For authenticating using MS-CHAPv1 via RADIUS you have to put the challenge
  727.      * and the response. The response has this structure:
  728.      * struct rad_mschapvalue {
  729.      *   u_char ident;
  730.      *   u_char flags;
  731.      *   u_char lm_response[24];
  732.      *   u_char response[24];
  733.      * };
  734.      * 
  735.      * @return void 
  736.      */
  737.     function putAuthAttributes()
  738.     {
  739.         if (isset($this->username)) {
  740.             $this->putAttribute(RADIUS_USER_NAME$this->username);        
  741.         }
  742.         if (isset($this->response|| isset($this->lmResponse)) {
  743.             $lmResp = isset($this->lmResponse$this->lmResponse : str_repeat ("\0"24);
  744.             $ntResp = isset($this->response)   $this->response :   str_repeat ("\0"24);
  745.             $resp pack('CC'$this->chapid$this->flags$lmResp $ntResp;
  746.             $this->putVendorAttribute(RADIUS_VENDOR_MICROSOFTRADIUS_MICROSOFT_MS_CHAP_RESPONSE$resp);
  747.         }
  748.         if (isset($this->challenge)) {        
  749.             $this->putVendorAttribute(RADIUS_VENDOR_MICROSOFTRADIUS_MICROSOFT_MS_CHAP_CHALLENGE$this->challenge);
  750.         }
  751.     }    
  752. }
  753.  
  754. /**
  755.  * class Auth_RADIUS_MSCHAPv2
  756.  *
  757.  * Class for authenticating using MS-CHAPv2 see RFC2759
  758.  * 
  759.  * @package Auth_RADIUS
  760.  */
  761. {
  762.     /**
  763.      * 16 Bytes binary challenge
  764.      * @var  string 
  765.      */
  766.     var $challenge = null;
  767.     
  768.     /**
  769.      * 16 Bytes binary Peer Challenge
  770.      * @var  string 
  771.      */
  772.     var $peerChallenge = null;
  773.  
  774.   /**
  775.      * Put MS-CHAPv2 specific attributes
  776.      *
  777.      * For authenticating using MS-CHAPv1 via RADIUS you have to put the challenge
  778.      * and the response. The response has this structure:
  779.      * struct rad_mschapv2value {
  780.      *   u_char ident;
  781.      *   u_char flags;
  782.      *   u_char pchallenge[16];
  783.      *   u_char reserved[8];
  784.      *   u_char response[24];
  785.      * };
  786.      * where pchallenge is the peer challenge. Like for MS-CHAPv1 we set the flags field to 1.
  787.      * @return void 
  788.      */    
  789.     function putAuthAttributes()
  790.     {
  791.         if (isset($this->username)) {
  792.             $this->putAttribute(RADIUS_USER_NAME$this->username);        
  793.         }
  794.         if (isset($this->response&& isset($this->peerChallenge)) {
  795.             // Response: chapid, flags (1 = use NT Response), Peer challenge, reserved, Response        
  796.             $resp pack('CCa16a8a24',$this->chapid 1$this->peerChallengestr_repeat("\0"8)$this->response);        
  797.             $this->putVendorAttribute(RADIUS_VENDOR_MICROSOFTRADIUS_MICROSOFT_MS_CHAP2_RESPONSE$resp);
  798.         }
  799.         if (isset($this->challenge)) {
  800.             $this->putVendorAttribute(RADIUS_VENDOR_MICROSOFTRADIUS_MICROSOFT_MS_CHAP_CHALLENGE$this->challenge);
  801.         }
  802.     }    
  803.     
  804.     /**
  805.      * Frees resources.
  806.      *
  807.      * Calling this method is always a good idea, because all security relevant
  808.      * attributes are filled with Nullbytes to leave nothing in the mem.
  809.      *
  810.      * @access public
  811.      */   
  812.     function close()
  813.     {
  814.         Auth_RADIUS_MSCHAPv1::close();
  815.         $this->peerChallenge = str_repeat("\0"strlen($this->peerChallenge));
  816.     }       
  817. }
  818.  
  819. /**
  820.  * class Auth_RADIUS_Acct
  821.  *
  822.  * Class for RADIUS accounting
  823.  * 
  824.  * @package Auth_RADIUS
  825.  */
  826. class Auth_RADIUS_Acct extends Auth_RADIUS 
  827. {
  828.     /**
  829.      * Defines where the Authentication was made, possible values are:
  830.      * RADIUS_AUTH_RADIUS, RADIUS_AUTH_LOCAL, RADIUS_AUTH_REMOTE
  831.      * @var  integer 
  832.      */
  833.     var $authentic = null;
  834.  
  835.    /**
  836.      * Defines the type of the accounting request, on of:
  837.      * RADIUS_START, RADIUS_STOP, RADIUS_ACCOUNTING_ON, RADIUS_ACCOUNTING_OFF
  838.      * @var  integer 
  839.      */    
  840.     var $status_type = null;
  841.  
  842.    /**
  843.      * The time the user was logged in in seconds
  844.      * @var  integer 
  845.      */    
  846.     var $session_time = null;
  847.  
  848.    /**
  849.      * A uniq identifier for the session of the user, maybe the PHP-Session-Id
  850.      * @var  string 
  851.      */    
  852.     var $session_id = null;
  853.     
  854.     /**
  855.      * Constructor
  856.      *
  857.      * Generates a predefined session_id. We use the Remote-Address, the PID, and the Current user.
  858.      * @return void 
  859.      */
  860.     function Auth_RADIUS_Acct()
  861.     {
  862.         $this->Auth_RADIUS();
  863.         
  864.         if (isset($_SERVER)) {
  865.             $var &$_SERVER;
  866.         else {
  867.             $var &$GLOBALS['HTTP_SERVER_VARS'];
  868.         }
  869.  
  870.         $this->session_id = sprintf("%s:%d-%s"isset($var['REMOTE_ADDR']$var['REMOTE_ADDR''127.0.0.1' getmypid()get_current_user());
  871.     }
  872.  
  873.     /**
  874.      * Creates a RADIUS resource
  875.      *
  876.      * Creates a RADIUS resource for accounting. This should be the first
  877.      * call before you make any other things with the library.
  878.      *
  879.      * @return bool   true on success, false on error
  880.      */
  881.     function open(
  882.     {
  883.         $this->res = radius_acct_open();
  884.         if (!$this->res{
  885.             return false;
  886.         }
  887.         return true;
  888.     }
  889.  
  890.    /**
  891.      * Creates an accounting request
  892.      *
  893.      * Creates an accounting request.
  894.      * You MUST call this method before you can put any attribute.
  895.      *
  896.      * @return bool   true on success, false on error
  897.      */
  898.     function createRequest()
  899.     {
  900.         if (!radius_create_request($this->resRADIUS_ACCOUNTING_REQUEST)) {
  901.             return false;
  902.         }
  903.         return true;
  904.     }   
  905.    
  906.   /**
  907.      * Put attributes for accounting.
  908.      *
  909.      * Here we put some accounting values. There many more attributes for accounting,
  910.      * but for web-applications only certain attributes make sense.
  911.      * @return void 
  912.      */ 
  913.     function putAuthAttributes()
  914.     {
  915.         $this->putAttribute(RADIUS_ACCT_SESSION_ID$this->session_id);
  916.         $this->putAttribute(RADIUS_ACCT_STATUS_TYPE$this->status_type);
  917.         if (isset($this->session_time&& $this->status_type == RADIUS_STOP{
  918.             $this->putAttribute(RADIUS_ACCT_SESSION_TIME$this->session_time);
  919.         }
  920.         if (isset($this->authentic)) {
  921.             $this->putAttribute(RADIUS_ACCT_AUTHENTIC$this->authentic);
  922.         }
  923.         
  924.     }    
  925.     
  926. }
  927.  
  928. /**
  929.  * class Auth_RADIUS_Acct_Start
  930.  *
  931.  * Class for RADIUS accounting. Its usualy used, after the user has logged in.
  932.  * 
  933.  * @package Auth_RADIUS
  934.  */
  935. {
  936.    /**
  937.      * Defines the type of the accounting request.
  938.      * It is set to RADIUS_START by default in this class.
  939.      * @var  integer 
  940.      */    
  941.     var $status_type = RADIUS_START;
  942. }
  943.  
  944. /**
  945.  * class Auth_RADIUS_Acct_Start
  946.  *
  947.  * Class for RADIUS accounting. Its usualy used, after the user has logged out.
  948.  *
  949.  * @package Auth_RADIUS
  950.  */
  951. {
  952.    /**
  953.      * Defines the type of the accounting request.
  954.      * It is set to RADIUS_STOP by default in this class.
  955.      * @var  integer 
  956.      */
  957.     var $status_type = RADIUS_STOP;
  958. }
  959.  
  960. if (!defined('RADIUS_UPDATE'))
  961.     define('RADIUS_UPDATE'3);
  962.  
  963. /**
  964.  * class Auth_RADIUS_Acct_Update
  965.  *
  966.  * Class for interim RADIUS accounting updates.
  967.  *
  968.  * @package Auth_RADIUS
  969.  */
  970. {
  971.    /**
  972.      * Defines the type of the accounting request.
  973.      * It is set to RADIUS_UPDATE by default in this class.
  974.      * @var  integer 
  975.      */
  976.     var $status_type = RADIUS_UPDATE;
  977. }
  978.  
  979. ?>

Documentation generated on Mon, 11 Mar 2019 10:14:10 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.