📄 radius.php
字号:
<?php/* vim: set expandtab tabstop=4 shiftwidth=4: *//*Copyright (c) 2003, Michael Bretterklieber <michael@bretterklieber.com>All rights reserved.Redistribution and use in source and binary forms, with or withoutmodification, are permitted provided that the following conditionsare met:1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.2. 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.3. The names of the authors may not 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" ANDANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIEDWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER 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 THEORYOF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDINGNEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.This code cannot simply be copied and put under the GNU Public License orany other GPL-like (LGPL, GPL2) License. $Id: RADIUS.php,v 1.1.2.2 2008/12/14 23:18:53 jonathanharker Exp $*/require_once 'PEAR.php';/*** Client implementation of RADIUS. This are wrapper classes for* the RADIUS PECL.* Provides RADIUS Authentication (RFC2865) and RADIUS Accounting (RFC2866).** @package Auth_RADIUS* @author Michael Bretterklieber <michael@bretterklieber.com>* @access public* @version $Revision: 1.1.2.2 $*/PEAR::loadExtension('radius');/** * class Auth_RADIUS * * Abstract base class for RADIUS * * @package Auth_RADIUS */class Auth_RADIUS extends PEAR { /** * List of RADIUS servers. * @var array * @see addServer(), putServer() */ var $_servers = array(); /** * Path to the configuration-file. * @var string * @see setConfigFile() */ var $_configfile = null; /** * Resource. * @var resource * @see open(), close() */ var $res = null; /** * Username for authentication and accounting requests. * @var string */ var $username = null; /** * Password for plaintext-authentication (PAP). * @var string */ var $password = null; /** * List of known attributes. * @var array * @see dumpAttributes(), getAttributes() */ var $attributes = array(); /** * List of raw attributes. * @var array * @see dumpAttributes(), getAttributes() */ var $rawAttributes = array(); /** * List of raw vendor specific attributes. * @var array * @see dumpAttributes(), getAttributes() */ var $rawVendorAttributes = array(); /** * Switch whether we should put standard attributes or not * @var bool * @see putStandardAttributes() */ var $useStandardAttributes = true; /** * Constructor * * Loads the RADIUS PECL/extension * * @return void */ function Auth_RADIUS() { $this->PEAR(); } /** * Adds a RADIUS server to the list of servers for requests. * * At most 10 servers may be specified. When multiple servers * are given, they are tried in round-robin fashion until a * valid response is received * * @access public * @param string $servername Servername or IP-Address * @param integer $port Portnumber * @param string $sharedSecret Shared secret * @param integer $timeout Timeout for each request * @param integer $maxtries Max. retries for each request * @return void */ function addServer($servername = 'localhost', $port = 0, $sharedSecret = 'testing123', $timeout = 3, $maxtries = 3) { $this->_servers[] = array($servername, $port, $sharedSecret, $timeout, $maxtries); } /** * Returns an error message, if an error occurred. * * @access public * @return string */ function getError() { return radius_strerror($this->res); } /** * Sets the configuration-file. * * @access public * @param string $file Path to the configuration file * @return void */ function setConfigfile($file) { $this->_configfile = $file; } /** * Puts an attribute. * * @access public * @param integer $attrib Attribute-number * @param mixed $port Attribute-value * @param type $type Attribute-type * @return bool true on success, false on error */ function putAttribute($attrib, $value, $type = null) { if ($type == null) { $type = gettype($value); } switch ($type) { case 'integer': case 'double': return radius_put_int($this->res, $attrib, $value); case 'addr': return radius_put_addr($this->res, $attrib, $value); case 'string': default: return radius_put_attr($this->res, $attrib, $value); } } /** * Puts a vendor-specific attribute. * * @access public * @param integer $vendor Vendor (MSoft, Cisco, ...) * @param integer $attrib Attribute-number * @param mixed $port Attribute-value * @param type $type Attribute-type * @return bool true on success, false on error */ function putVendorAttribute($vendor, $attrib, $value, $type = null) { if ($type == null) { $type = gettype($value); } switch ($type) { case 'integer': case 'double': return radius_put_vendor_int($this->res, $vendor, $attrib, $value); case 'addr': return radius_put_vendor_addr($this->res, $vendor,$attrib, $value); case 'string': default: return radius_put_vendor_attr($this->res, $vendor, $attrib, $value); } } /** * Prints known attributes received from the server. * * @access public */ function dumpAttributes() { foreach ($this->attributes as $name => $data) { echo "$name:$data<br>\n"; } } /** * Overwrite this. * * @access public */ function open() { } /** * Overwrite this. * * @access public */ function createRequest() { } /** * Puts standard attributes. * * @access public */ function putStandardAttributes() { if (!$this->useStandardAttributes) return; if (isset($_SERVER)) { $var = &$_SERVER; } else { $var = &$GLOBALS['HTTP_SERVER_VARS']; } $this->putAttribute(RADIUS_NAS_IDENTIFIER, isset($var['HTTP_HOST']) ? $var['HTTP_HOST'] : 'localhost'); $this->putAttribute(RADIUS_NAS_PORT_TYPE, RADIUS_VIRTUAL); $this->putAttribute(RADIUS_SERVICE_TYPE, RADIUS_FRAMED); $this->putAttribute(RADIUS_FRAMED_PROTOCOL, RADIUS_PPP); $this->putAttribute(RADIUS_CALLING_STATION_ID, isset($var['REMOTE_HOST']) ? $var['REMOTE_HOST'] : '127.0.0.1'); } /** * Puts custom attributes. * * @access public */ function putAuthAttributes() { if (isset($this->username)) { $this->putAttribute(RADIUS_USER_NAME, $this->username); } } /** * Configures the radius library. * * @access public * @param string $servername Servername or IP-Address * @param integer $port Portnumber * @param string $sharedSecret Shared secret * @param integer $timeout Timeout for each request * @param integer $maxtries Max. retries for each request * @return bool true on success, false on error * @see addServer() */ function putServer($servername, $port = 0, $sharedsecret = 'testing123', $timeout = 3, $maxtries = 3) { if (!radius_add_server($this->res, $servername, $port, $sharedsecret, $timeout, $maxtries)) { return false; } return true; } /** * Configures the radius library via external configurationfile * * @access public * @param string $servername Servername or IP-Address * @return bool true on success, false on error */ function putConfigfile($file) { if (!radius_config($this->res, $file)) { return false; } return true; } /** * Initiates a RADIUS request. * * @access public * @return bool true on success, false on errors */ function start() { if (!$this->open()) { return false; } foreach ($this->_servers as $s) { // Servername, port, sharedsecret, timeout, retries if (!$this->putServer($s[0], $s[1], $s[2], $s[3], $s[4])) { return false; } } if (!empty($this->_configfile)) { if (!$this->putConfigfile($this->_configfile)) { return false; } } $this->createRequest(); $this->putStandardAttributes(); $this->putAuthAttributes(); return true; } /** * Sends a prepared RADIUS request and waits for a response * * @access public * @return mixed true on success, false on reject, PEAR_Error on error */ function send() { $req = radius_send_request($this->res); if (!$req) { return $this->raiseError('Error sending request: ' . $this->getError()); } switch($req) { case RADIUS_ACCESS_ACCEPT: if (is_subclass_of($this, 'auth_radius_acct')) { return $this->raiseError('RADIUS_ACCESS_ACCEPT is unexpected for accounting'); } return true; case RADIUS_ACCESS_REJECT: return false; case RADIUS_ACCOUNTING_RESPONSE: if (is_subclass_of($this, 'auth_radius_pap')) { return $this->raiseError('RADIUS_ACCOUNTING_RESPONSE is unexpected for authentication'); } return true; default: return $this->raiseError("Unexpected return value: $req"); } } /** * Reads all received attributes after sending the request. * * This methods stores known attributes in the property attributes, * all attributes (including known attibutes) are stored in rawAttributes * or rawVendorAttributes. * NOTE: call this function also even if the request was rejected, because the * Server returns usualy an errormessage * * @access public * @return bool true on success, false on error */ function getAttributes() { while ($attrib = radius_get_attr($this->res)) { if (!is_array($attrib)) { return false; } $attr = $attrib['attr']; $data = $attrib['data']; $this->rawAttributes[$attr] = $data; switch ($attr) { case RADIUS_FRAMED_IP_ADDRESS: $this->attributes['framed_ip'] = radius_cvt_addr($data); break; case RADIUS_FRAMED_IP_NETMASK: $this->attributes['framed_mask'] = radius_cvt_addr($data); break; case RADIUS_FRAMED_MTU: $this->attributes['framed_mtu'] = radius_cvt_int($data); break; case RADIUS_FRAMED_COMPRESSION: $this->attributes['framed_compression'] = radius_cvt_int($data); break; case RADIUS_SESSION_TIMEOUT: $this->attributes['session_timeout'] = radius_cvt_int($data); break; case RADIUS_IDLE_TIMEOUT: $this->attributes['idle_timeout'] = radius_cvt_int($data); break; case RADIUS_SERVICE_TYPE: $this->attributes['service_type'] = radius_cvt_int($data); break; case RADIUS_CLASS: $this->attributes['class'] = radius_cvt_string($data); break; case RADIUS_FRAMED_PROTOCOL: $this->attributes['framed_protocol'] = radius_cvt_int($data); break; case RADIUS_FRAMED_ROUTING: $this->attributes['framed_routing'] = radius_cvt_int($data); break; case RADIUS_FILTER_ID: $this->attributes['filter_id'] = radius_cvt_string($data); break; case RADIUS_REPLY_MESSAGE: $this->attributes['reply_message'] = radius_cvt_string($data); break; case RADIUS_VENDOR_SPECIFIC: $attribv = radius_get_vendor_attr($data); if (!is_array($attribv)) { return false; } $vendor = $attribv['vendor']; $attrv = $attribv['attr']; $datav = $attribv['data']; $this->rawVendorAttributes[$vendor][$attrv] = $datav; if ($vendor == RADIUS_VENDOR_MICROSOFT) { switch ($attrv) { case RADIUS_MICROSOFT_MS_CHAP2_SUCCESS: $this->attributes['ms_chap2_success'] = radius_cvt_string($datav); break; case RADIUS_MICROSOFT_MS_CHAP_ERROR: $this->attributes['ms_chap_error'] = radius_cvt_string(substr($datav,1)); break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -