radius.inc 35.6 KB
Newer Older
Ad Schellevis's avatar
Ad Schellevis committed
1 2 3 4 5 6 7 8
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
/*
    $Id$

    Copyright (c) 2003, Michael Bretterklieber <michael@bretterklieber.com>
    All rights reserved.

9 10
    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions
Ad Schellevis's avatar
Ad Schellevis committed
11 12
    are met:

13
    1. Redistributions of source code must retain the above copyright
Ad Schellevis's avatar
Ad Schellevis committed
14
       notice, this list of conditions and the following disclaimer.
15 16
    2. Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in the
Ad Schellevis's avatar
Ad Schellevis committed
17
       documentation and/or other materials provided with the distribution.
18
    3. The names of the authors may not be used to endorse or promote products
Ad Schellevis's avatar
Ad Schellevis committed
19 20
       derived from this software without specific prior written permission.

21 22 23 24 25 26 27 28 29
    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 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 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,
Ad Schellevis's avatar
Ad Schellevis committed
30 31
    EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

32
    This code cannot simply be copied and put under the GNU Public License or
Ad Schellevis's avatar
Ad Schellevis committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    any other GPL-like (LGPL, GPL2) License.

        This version of RADIUS.php has been modified by
        Jonathan De Graeve <m0n0wall@esstec.be> to integrate with M0n0wall <http://www.m0n0.ch/wall>

        Changes made include:
        * StandardAttributes for M0n0wall use
        * Removed internal Session-Id creation
        * Adding of ReplyMessage to getAttributes()
        * Adding of listAttributes()
        * Adding of VENDOR Bay Networks (Nortel)
        * Adding of VENDOR Nomadix
        * Adding of VENDOR WISPr (Wi-Fi Alliance)
        * Adding of VENDOR ChilliSpot (bandwidth-attributes only)

*/

/*
    pfSense_MODULE:    auth
*/

require_once("PEAR.inc");
require_once("radius_authentication.inc");
require_once("radius_accounting.inc");

/**
* 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.5 $
*/

PEAR::loadExtension('radius');

/**
 * class Auth_RADIUS
 *
 * Abstract base class for RADIUS
 *
76
 * @package Auth_RADIUS
Ad Schellevis's avatar
Ad Schellevis committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
 */
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();

    /**
     * Constructor
     *
     * Loads the RADIUS PECL/extension
     *
     * @return void
     */
141
    function Auth_RADIUS()
Ad Schellevis's avatar
Ad Schellevis committed
142 143 144 145 146 147 148
    {
        $this->PEAR();
    }

    /**
     * Adds a RADIUS server to the list of servers for requests.
     *
149 150
     * At most 10 servers may be specified.    When multiple servers
     * are given, they are tried in round-robin fashion until a
Ad Schellevis's avatar
Ad Schellevis committed
151 152 153 154 155 156 157 158 159 160
     * 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
     */
161
    function addServer($servername = 'localhost', $port = 0, $sharedSecret = 'testing123', $timeout = 3, $maxtries = 2)
Ad Schellevis's avatar
Ad Schellevis committed
162 163 164 165 166 167 168 169 170 171
    {
        $this->_servers[] = array($servername, $port, $sharedSecret, $timeout, $maxtries);
    }

    /**
     * Returns an error message, if an error occurred.
     *
     * @access public
     * @return string
     */
172
    function getError()
Ad Schellevis's avatar
Ad Schellevis committed
173 174 175 176 177 178 179 180 181 182 183
    {
        return radius_strerror($this->res);
    }

    /**
     * Sets the configuration-file.
     *
     * @access public
     * @param  string  $file Path to the configuration file
     * @return void
     */
184
    function setConfigfile($file)
Ad Schellevis's avatar
Ad Schellevis committed
185 186 187 188 189 190 191 192 193 194 195 196 197
    {
        $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
     */
198
    function putAttribute($attrib, $value, $type = null)
Ad Schellevis's avatar
Ad Schellevis committed
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
    {
        if ($type == null) {
            $type = gettype($value);
        }

        switch ($type) {
        case 'integer':
            // Fix a conversion error so we should be able to handle 4GB values
            return radius_put_int($this->res, $attrib, (float)$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
228 229
     */
    function putVendorAttribute($vendor, $attrib, $value, $type = null)
Ad Schellevis's avatar
Ad Schellevis committed
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
    {

        if ($type == null) {
            $type = gettype($value);
        }

        switch ($type) {
        case 'integer':
            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";
        }
    }

    /**
     * Return our know attributes array received from the server.
     *
     * @access public
     */
    function listAttributes()
    {
        return $this->attributes;
    }

    /**
273
     * Overwrite this.
Ad Schellevis's avatar
Ad Schellevis committed
274 275 276
     *
     * @access public
     */
277
    function open()
Ad Schellevis's avatar
Ad Schellevis committed
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
    {
    }

    /**
     * Overwrite this.
     *
     * @access public
     */
    function createRequest()
    {
    }

    /**
     * Puts standard attributes.
     *
     * These attributes will always be present in a radius request
     *
     * @access public
     */
    function putStandardAttributes()
    {
        global $config, $cpzone;

        if (!function_exists("getNasIp")) {
            $ipaddr = "0.0.0.0";
        } else {
            $ipaddr = getNasIP();
        }
        // Add support for sending NAS-IP-Address, set this explicitly as an ip_addr
        $this->putAttribute(RADIUS_NAS_IP_ADDRESS, $ipaddr, "addr");

        // Add support for sending NAS-Identifier
        if (empty($config["captiveportal"][$cpzone]["radiusnasid"])) {
            $nasId = php_uname("n");
        } else {
            $nasId = $config["captiveportal"][$cpzone]["radiusnasid"];
        }
        $this->putAttribute(RADIUS_NAS_IDENTIFIER, $nasId);
    }

    /**
     * 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()
     */
342
    function putServer($servername, $port = 0, $sharedsecret = 'testing123', $timeout = 3, $maxtries = 3)
Ad Schellevis's avatar
Ad Schellevis committed
343 344 345 346 347 348 349 350 351 352 353 354 355 356
    {
        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
     */
357
    function putConfigfile($file)
Ad Schellevis's avatar
Ad Schellevis committed
358 359 360 361 362 363 364 365
    {
        if (!radius_config($this->res, $file)) {
            return false;
        }
        return true;
    }

    /**
366
     * Initiates a RADIUS request.
Ad Schellevis's avatar
Ad Schellevis committed
367 368
     *
     * @access public
369 370
     * @return bool  true on success, false on errors
     */
Ad Schellevis's avatar
Ad Schellevis committed
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
    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(gettext('Error sending request:') . ' ' . $this->getError());
        }

        switch($req) {
        case RADIUS_ACCESS_ACCEPT:
            if (is_subclass_of($this, 'auth_radius_acct')) {
                return $this->raiseError(gettext('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(gettext('RADIUS_ACCOUNTING_RESPONSE is unexpected for authentication'));
            }
            return true;

        default:
            return $this->raiseError(sprintf(gettext("Unexpected return value: %s"),$req));
        }

    }

    /**
     * Reads all received attributes after sending the request.
     *
     * This methos stores know attributes in the property attributes,
     * all attributes (including known attibutes) are stored in rawAttributes
     * or rawVendorAttributes.
     * NOTE: call this functio 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;
450
            }
Ad Schellevis's avatar
Ad Schellevis committed
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594

            $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_int($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;

                    case RADIUS_MICROSOFT_MS_CHAP_DOMAIN:
                        $this->attributes['ms_chap_domain'] = radius_cvt_string($datav);
                        break;

                    case RADIUS_MICROSOFT_MS_MPPE_ENCRYPTION_POLICY:
                        $this->attributes['ms_mppe_encryption_policy'] = radius_cvt_int($datav);
                        break;

                    case RADIUS_MICROSOFT_MS_MPPE_ENCRYPTION_TYPES:
                        $this->attributes['ms_mppe_encryption_types'] = radius_cvt_int($datav);
                        break;

                    case RADIUS_MICROSOFT_MS_CHAP_MPPE_KEYS:
                        $demangled = radius_demangle($this->res, $datav);
                        $this->attributes['ms_chap_mppe_lm_key'] = substr($demangled, 0, 8);
                        $this->attributes['ms_chap_mppe_nt_key'] = substr($demangled, 8, RADIUS_MPPE_KEY_LEN);
                        break;

                    case RADIUS_MICROSOFT_MS_MPPE_SEND_KEY:
                        $this->attributes['ms_chap_mppe_send_key'] = radius_demangle_mppe_key($this->res, $datav);
                        break;

                    case RADIUS_MICROSOFT_MS_MPPE_RECV_KEY:
                        $this->attributes['ms_chap_mppe_recv_key'] = radius_demangle_mppe_key($this->res, $datav);
                        break;

                    case RADIUS_MICROSOFT_MS_PRIMARY_DNS_SERVER:
                        $this->attributes['ms_primary_dns_server'] = radius_cvt_string($datav);
                        break;
                    }
                }

                elseif ($vendor == 1584) {

                    switch ($attrv) {
                    case 102:
                        $this->attributes['ces_group'] = radius_cvt_string($datav);
                        break;
                    }
                }

                elseif ($vendor == 3309) {    /* RADIUS_VENDOR_NOMADIX */

                    switch ($attrv) {
                    case 1: /* RADIUS_NOMADIX_BW_UP */
                        $this->attributes['bw_up'] = radius_cvt_int($datav);
                        break;
                    case 2: /* RADIUS_NOMADIX_BW_DOWN */
                        $this->attributes['bw_down'] = radius_cvt_int($datav);
                        break;
                    case 3: /* RADIUS_NOMADIX_URL_REDIRECTION */
                        $this->attributes['url_redirection'] = radius_cvt_string($datav);
                        break;
                    case 5: /* RADIUS_NOMADIX_EXPIRATION */
                        $this->attributes['expiration'] = radius_cvt_string($datav);
                        break;
                    case 7: /* RADIUS_NOMADIX_MAXBYTESUP */
                        $this->attributes['maxbytesup'] = radius_cvt_int($datav);
                        break;
                    case 8: /* RADIUS_NOMADIX_MAXBYTESDOWN */
                        $this->attributes['maxbytesdown'] = radius_cvt_int($datav);
                        break;
                    case 10: /* RADIUS_NOMADIX_LOGOFF_URL */
                        $this->attributes['url_logoff'] = radius_cvt_string($datav);
                        break;
                    }
595
                }
Ad Schellevis's avatar
Ad Schellevis committed
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721

                elseif ($vendor == 14122) { /* RADIUS_VENDOR_WISPr Wi-Fi Alliance */

                    switch ($attrv) {
                    case 1: /* WISPr-Location-ID */
                        $this->attributes['location_id'] = radius_cvt_string($datav);
                        break;
                    case 2: /* WISPr-Location-Name */
                        $this->attributes['location_name'] = radius_cvt_string($datav);
                        break;
                    case 3: /* WISPr-Logoff-URL */
                        $this->attributes['url_logoff'] = radius_cvt_string($datav);
                        break;
                    case 4: /* WISPr-Redirection-URL */
                        $this->attributes['url_redirection'] = radius_cvt_string($datav);
                        break;
                    case 5: /* WISPr-Bandwidth-Min-Up */
                        $this->attributes['bw_up_min'] = radius_cvt_int($datav);
                        break;
                    case 6: /* WISPr-Bandwidth-Min-Down */
                        $this->attributes['bw_down_min'] = radius_cvt_int($datav);
                        break;
                    case 7: /* WISPr-Bandwidth-Max-Up */
                        $this->attributes['bw_up'] = radius_cvt_int($datav);
                        break;
                    case 8: /* WISPr-Bandwidth-Max-Down */
                        $this->attributes['bw_down'] = radius_cvt_int($datav);
                        break;
                    case 9: /* WISPr-Session-Terminate-Time */
                        $this->attributes['session_terminate_time'] = radius_cvt_string($datav);
                        break;
                    case 10: /* WISPr-Session-Terminate-End-Of-Day */
                        $this->attributes['session_terminate_endofday'] = radius_cvt_int($datav);
                        break;
                    case 11: /* WISPr-Billing-Class-Of-Service */
                        $this->attributes['billing_class_of_service'] = radius_cvt_string($datav);
                        break;
                    }
                }

                elseif ($vendor == 14559) { /* RADIUS_VENDOR_ChilliSpot */
                    switch ($attrv) {
                    case 4: /* ChilliSpot-Bandwidth-Max-Up */
                        $this->attributes['bw_up'] = radius_cvt_int($datav);
                        break;
                    case 5: /* ChilliSpot-Bandwidth-Max-Down */
                        $this->attributes['bw_down'] = radius_cvt_int($datav);
                        break;
                    }
                }

		elseif ($vendor == 9) { /* RADIUS_VENDOR_CISCO */
			switch ($attrv) {
			case 1: /* Cisco-AVPair */
				if (!is_array($this->attributes['ciscoavpair']))
					$this->attributes['ciscoavpair'] = array();
				$this->attributes['ciscoavpair'][] = radius_cvt_string($datav);
				break;
			}
		}

                elseif ($vendor == 8744) { /* Colubris / HP MSM wireless */
                    //documented at http://bizsupport1.austin.hp.com/bc/docs/support/SupportManual/c02704528/c02704528.pdf pg 15-67
                    if ($attrv == 0) { /* Colubris AV-Pair */
                        $datav = explode('=', $datav);
                        switch ($datav[0]) {
                        case 'max-input-rate':
                            // "Controls the data rate [kbps] at which traffic can be transferred from the user to the [router]."
                            $this->attributes['bw_up'] = radius_cvt_int($datav[1]);
                            break;
                        case 'max-output-rate':
                            //"Controls the data rate [kbps] at which traffic can be transferred from the [router] to the user."
                            $this->attributes['bw_down'] = radius_cvt_int($datav[1]);
                            break;
                        case 'max-input-octets':
                            $this->attributes['maxbytesup'] = radius_cvt_int($datav[1]);
                            break;
                        case 'max-output-octets':
                            $this->attributes['maxbytesdown'] = radius_cvt_int($datav[1]);
                            break;
                        case 'welcome-url':
                            $this->attributes['url_redirection'] = radius_cvt_string($datav[1]);
                            break;
                        case 'goodbye-url':
                            $this->attributes['url_logoff'] = radius_cvt_string($datav[1]);
                            break;
                        }
                    }
                }

                break;

            case 85: /* Acct-Interim-Interval: RFC 2869 */
                $this->attributes['interim_interval'] = radius_cvt_int($data);
                break;
            }
        }

        return true;
    }

    /**
     * Frees resources.
     *
     * Calling this method is always a good idea, because all security relevant
     * attributes are filled with Nullbytes to leave nothing in the mem.
     *
     * @access public
     */
    function close()
    {
        if ($this->res != null) {
            radius_close($this->res);
            $this->res = null;
        }
        $this->username = str_repeat("\0", strlen($this->username));
        $this->password = str_repeat("\0", strlen($this->password));
    }

}

/**
 * class Auth_RADIUS_PAP
 *
 * Class for authenticating using PAP (Plaintext)
 *
722
 * @package Auth_RADIUS
Ad Schellevis's avatar
Ad Schellevis committed
723
 */
724
class Auth_RADIUS_PAP extends Auth_RADIUS
Ad Schellevis's avatar
Ad Schellevis committed
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
{

    /**
     * Constructor
     *
     * @param  string  $username   Username
     * @param  string  $password   Password
     * @return void
     */
    function Auth_RADIUS_PAP($username = null, $password = null)
    {
        $this->Auth_RADIUS();
        $this->username = $username;
        $this->password = $password;
    }

    /**
     * Creates a RADIUS resource
     *
     * Creates a RADIUS resource for authentication. This should be the first
     * call before you make any other things with the library.
     *
     * @return bool   true on success, false on error
     */
749
    function open()
Ad Schellevis's avatar
Ad Schellevis committed
750 751 752 753 754 755 756 757 758
    {
        $this->res = radius_auth_open();
        if (!$this->res) {
            return false;
        }
        return true;
    }

    /**
759
     * Creates an authentication request
Ad Schellevis's avatar
Ad Schellevis committed
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
     *
     * Creates an authentication request.
     * You MUST call this method before you can put any attribute
     *
     * @return bool   true on success, false on error
     */
    function createRequest()
    {
        if (!radius_create_request($this->res, RADIUS_ACCESS_REQUEST)) {
            return false;
        }
        return true;
    }

    /**
775
     * Put authentication specific attributes
Ad Schellevis's avatar
Ad Schellevis committed
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
     *
     * @return void
     */
    function putAuthAttributes()
    {
        if (isset($this->username)) {
            $this->putAttribute(RADIUS_USER_NAME, $this->username);
        }
        if (isset($this->password)) {
            $this->putAttribute(RADIUS_USER_PASSWORD, $this->password);
        }
    }

}

/**
 * class Auth_RADIUS_CHAP_MD5
 *
 * Class for authenticating using CHAP-MD5 see RFC1994.
795
 * Instead og the plaintext password the challenge and
Ad Schellevis's avatar
Ad Schellevis committed
796 797
 * the response are needed.
 *
798
 * @package Auth_RADIUS
Ad Schellevis's avatar
Ad Schellevis committed
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
 */
class Auth_RADIUS_CHAP_MD5 extends Auth_RADIUS_PAP
{
    /**
     * 8 Bytes binary challenge
     * @var  string
     */
    var $challenge = null;

    /**
     * 16 Bytes MD5 response binary
     * @var  string
     */
    var $response = null;

    /**
     * Id of the authentication request. Should incremented after every request.
     * @var  integer
     */
    var $chapid = 1;

    /**
     * Constructor
     *
     * @param  string  $username   Username
     * @param  string  $challenge  8 Bytes Challenge (binary)
     * @param  integer $chapid     Requestnumber
     * @return void
     */
    function Auth_RADIUS_CHAP_MD5($username = null, $challenge = null, $chapid = 1)
    {
        $this->Auth_RADIUS_PAP();
        $this->username = $username;
        $this->challenge = $challenge;
        $this->chapid = $chapid;
    }

    /**
     * Put CHAP-MD5 specific attributes
     *
839
     * For authenticating using CHAP-MD5 via RADIUS you have to put the challenge
Ad Schellevis's avatar
Ad Schellevis committed
840 841 842 843 844 845 846
     * and the response. The chapid is inserted in the first byte of the response.
     *
     * @return void
     */
    function putAuthAttributes()
    {
        if (isset($this->username)) {
847
            $this->putAttribute(RADIUS_USER_NAME, $this->username);
Ad Schellevis's avatar
Ad Schellevis committed
848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
        }
        if (isset($this->response)) {
            $response = pack('C', $this->chapid) . $this->response;
            $this->putAttribute(RADIUS_CHAP_PASSWORD, $response);
        }
        if (isset($this->challenge)) {
            $this->putAttribute(RADIUS_CHAP_CHALLENGE, $this->challenge);
        }
    }

    /**
     * Frees resources.
     *
     * Calling this method is always a good idea, because all security relevant
     * attributes are filled with Nullbytes to leave nothing in the mem.
     *
     * @access public
     */
    function close()
    {
        Auth_RADIUS_PAP::close();
        $this->challenge =  str_repeat("\0", strlen($this->challenge));
        $this->response =  str_repeat("\0", strlen($this->response));
    }

}

/**
 * class Auth_RADIUS_MSCHAPv1
 *
 * Class for authenticating using MS-CHAPv1 see RFC2433
 *
880
 * @package Auth_RADIUS
Ad Schellevis's avatar
Ad Schellevis committed
881
 */
882
class Auth_RADIUS_MSCHAPv1 extends Auth_RADIUS_CHAP_MD5
Ad Schellevis's avatar
Ad Schellevis committed
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897
{
    /**
     * LAN-Manager-Response
     * @var  string
     */
    var $lmResponse = null;

    /**
     * Wether using deprecated LM-Responses or not.
     * 0 = use LM-Response, 1 = use NT-Response
     * @var  bool
     */
    var $flags = 1;

    /**
898
     * Put MS-CHAPv1 specific attributes
Ad Schellevis's avatar
Ad Schellevis committed
899
     *
900
     * For authenticating using MS-CHAPv1 via RADIUS you have to put the challenge
Ad Schellevis's avatar
Ad Schellevis committed
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932
     * and the response. The response has this structure:
     * struct rad_mschapvalue {
     *   u_char ident;
     *   u_char flags;
     *   u_char lm_response[24];
     *   u_char response[24];
     * };
     *
     * @return void
     */
    function putAuthAttributes()
    {
        if (isset($this->username)) {
            $this->putAttribute(RADIUS_USER_NAME, $this->username);
        }
        if (isset($this->response) || isset($this->lmResponse)) {
            $lmResp = isset($this->lmResponse) ? $this->lmResponse : str_repeat ("\0", 24);
            $ntResp = isset($this->response)   ? $this->response :   str_repeat ("\0", 24);
            $resp = pack('CC', $this->chapid, $this->flags) . $lmResp . $ntResp;
            $this->putVendorAttribute(RADIUS_VENDOR_MICROSOFT, RADIUS_MICROSOFT_MS_CHAP_RESPONSE, $resp);
        }
        if (isset($this->challenge)) {
            $this->putVendorAttribute(RADIUS_VENDOR_MICROSOFT, RADIUS_MICROSOFT_MS_CHAP_CHALLENGE, $this->challenge);
        }
    }
}

/**
 * class Auth_RADIUS_MSCHAPv2
 *
 * Class for authenticating using MS-CHAPv2 see RFC2759
 *
933
 * @package Auth_RADIUS
Ad Schellevis's avatar
Ad Schellevis committed
934
 */
935
class Auth_RADIUS_MSCHAPv2 extends Auth_RADIUS_MSCHAPv1
Ad Schellevis's avatar
Ad Schellevis committed
936 937 938 939 940 941 942 943 944 945 946 947 948 949
{
    /**
     * 16 Bytes binary challenge
     * @var  string
     */
    var $challenge = null;

    /**
     * 16 Bytes binary Peer Challenge
     * @var  string
     */
    var $peerChallenge = null;

  /**
950
     * Put MS-CHAPv2 specific attributes
Ad Schellevis's avatar
Ad Schellevis committed
951
     *
952
     * For authenticating using MS-CHAPv1 via RADIUS you have to put the challenge
Ad Schellevis's avatar
Ad Schellevis committed
953 954 955 956 957 958 959 960 961 962 963 964 965 966
     * and the response. The response has this structure:
     * struct rad_mschapv2value {
     *   u_char ident;
     *   u_char flags;
     *   u_char pchallenge[16];
     *   u_char reserved[8];
     *   u_char response[24];
     * };
     * where pchallenge is the peer challenge. Like for MS-CHAPv1 we set the flags field to 1.
     * @return void
     */
    function putAuthAttributes()
    {
        if (isset($this->username)) {
967
            $this->putAttribute(RADIUS_USER_NAME, $this->username);
Ad Schellevis's avatar
Ad Schellevis committed
968 969
        }
        if (isset($this->response) && isset($this->peerChallenge)) {
970 971
            // Response: chapid, flags (1 = use NT Response), Peer challenge, reserved, Response
            $resp = pack('CCa16a8a24',$this->chapid , 1, $this->peerChallenge, str_repeat("\0", 8), $this->response);
Ad Schellevis's avatar
Ad Schellevis committed
972 973 974 975 976 977 978 979 980 981 982 983 984 985
            $this->putVendorAttribute(RADIUS_VENDOR_MICROSOFT, RADIUS_MICROSOFT_MS_CHAP2_RESPONSE, $resp);
        }
        if (isset($this->challenge)) {
            $this->putVendorAttribute(RADIUS_VENDOR_MICROSOFT, RADIUS_MICROSOFT_MS_CHAP_CHALLENGE, $this->challenge);
        }
    }

    /**
     * Frees resources.
     *
     * Calling this method is always a good idea, because all security relevant
     * attributes are filled with Nullbytes to leave nothing in the mem.
     *
     * @access public
986
     */
Ad Schellevis's avatar
Ad Schellevis committed
987 988 989 990 991 992 993 994 995 996 997
    function close()
    {
        Auth_RADIUS_MSCHAPv1::close();
        $this->peerChallenge = str_repeat("\0", strlen($this->peerChallenge));
    }
}

/**
 * class Auth_RADIUS_Acct
 *
 * Class for RADIUS accounting
998 999
 *
 * @package Auth_RADIUS
Ad Schellevis's avatar
Ad Schellevis committed
1000
 */
1001
class Auth_RADIUS_Acct extends Auth_RADIUS
Ad Schellevis's avatar
Ad Schellevis committed
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
{
    /**
     * Defines where the Authentication was made, possible values are:
     * RADIUS_AUTH_RADIUS, RADIUS_AUTH_LOCAL, RADIUS_AUTH_REMOTE
     * @var  integer
     */
    var $authentic = null;

   /**
     * Defines the type of the accounting request, on of:
     * RADIUS_START, RADIUS_STOP, RADIUS_ACCOUNTING_ON, RADIUS_ACCOUNTING_OFF
     * @var  integer
1014
     */
Ad Schellevis's avatar
Ad Schellevis committed
1015 1016 1017 1018 1019
    var $status_type = null;

   /**
     * The time the user was logged in in seconds
     * @var  integer
1020
     */
Ad Schellevis's avatar
Ad Schellevis committed
1021 1022 1023 1024 1025
    var $session_time = null;

   /**
     * A uniq identifier for the session of the user, maybe the PHP-Session-Id
     * @var  string
1026
     */
Ad Schellevis's avatar
Ad Schellevis committed
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
    var $session_id = null;

    /**
     * Constructor
     *
     * This function is disabled for M0n0wall since we use our own session_id
     *
     * Generates a predefined session_id. We use the Remote-Address, the PID, and the Current user.
     * @return void
     *
    function Auth_RADIUS_Acct()
    {
        $this->Auth_RADIUS();

        if (isset($_SERVER)) {
            $var = &$_SERVER;
        } else {
            $var = &$GLOBALS['HTTP_SERVER_VARS'];
        }

        $this->session_id = sprintf("%s:%d-%s", isset($var['REMOTE_ADDR']) ? $var['REMOTE_ADDR'] : '127.0.0.1' , getmypid(), get_current_user());
    }
    */

    /**
     * Constructor
     *
     */

    function Auth_RADIUS_Acct()
    {
        $this->Auth_RADIUS();
    }

    /**
     * Creates a RADIUS resource
     *
     * Creates a RADIUS resource for accounting. This should be the first
     * call before you make any other things with the library.
     *
     * @return bool   true on success, false on error
     */
1069
    function open()
Ad Schellevis's avatar
Ad Schellevis committed
1070 1071 1072 1073 1074 1075 1076 1077 1078
    {
        $this->res = radius_acct_open();
        if (!$this->res) {
            return false;
        }
        return true;
    }

   /**
1079
     * Creates an accounting request
Ad Schellevis's avatar
Ad Schellevis committed
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
     *
     * Creates an accounting request.
     * You MUST call this method before you can put any attribute.
     *
     * @return bool   true on success, false on error
     */
    function createRequest()
    {
        if (!radius_create_request($this->res, RADIUS_ACCOUNTING_REQUEST)) {
            return false;
        }
        return true;
    }

  /**
     * Put attributes for accounting.
     *
1097
     * Here we put some accounting values. There many more attributes for accounting,
Ad Schellevis's avatar
Ad Schellevis committed
1098 1099
     * but for web-applications only certain attributes make sense.
     * @return void
1100
     */
Ad Schellevis's avatar
Ad Schellevis committed
1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
    function putAuthAttributes()
    {
        if (isset($this->username)) {
            $this->putAttribute(RADIUS_USER_NAME, $this->username);
        }
        $this->putAttribute(RADIUS_ACCT_STATUS_TYPE, $this->status_type);
        //if (isset($this->session_time) && $this->status_type == RADIUS_STOP) {
        if (isset($this->session_time)) {
            $this->putAttribute(RADIUS_ACCT_SESSION_TIME, $this->session_time);
        }
        if (isset($this->authentic)) {
            $this->putAttribute(RADIUS_ACCT_AUTHENTIC, $this->authentic);
        }

        $this->putStandardAttributes();
    }

}

/**
 * class Auth_RADIUS_Acct_Start
 *
 * Class for RADIUS accounting. Its usualy used, after the user has logged in.
1124
 *
Ad Schellevis's avatar
Ad Schellevis committed
1125 1126
 * @package Auth_RADIUS
 */
1127
class Auth_RADIUS_Acct_Start extends Auth_RADIUS_Acct
Ad Schellevis's avatar
Ad Schellevis committed
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
{
   /**
     * Defines the type of the accounting request.
     * It is set to RADIUS_START by default in this class.
     * @var  integer
     */
    var $status_type = RADIUS_START;
}

/**
 * class Auth_RADIUS_Acct_Start
 *
 * Class for RADIUS accounting. Its usualy used, after the user has logged out.
 *
 * @package Auth_RADIUS
 */
class Auth_RADIUS_Acct_Stop extends Auth_RADIUS_Acct
{
   /**
     * Defines the type of the accounting request.
     * It is set to RADIUS_STOP by default in this class.
     * @var  integer
     */
    var $status_type = RADIUS_STOP;
}

if (!defined('RADIUS_UPDATE'))
    define('RADIUS_UPDATE', 3);

/**
 * class Auth_RADIUS_Acct_Update
 *
 * Class for interim RADIUS accounting updates.
 *
 * @package Auth_RADIUS
 */
class Auth_RADIUS_Acct_Update extends Auth_RADIUS_Acct
{
   /**
     * Defines the type of the accounting request.
     * It is set to RADIUS_UPDATE by default in this class.
     * @var  integer
     */
    var $status_type = RADIUS_UPDATE;
}

/**
 * class Auth_RADIUS_Acct_On
 *
 * Class for sending Accounting-On updates
 *
 * @package Auth_RADIUS
 */
class Auth_RADIUS_Acct_On extends Auth_RADIUS_Acct
{
    /**
      * Defines the type of the accounting request.
      * It is set to RADIUS_ACCOUNTING_ON by default in this class.
      * @var  integer
      */
    var $status_type = RADIUS_ACCOUNTING_ON;
}

/**
 * class Auth_RADIUS_Acct_Off
 *
 * Class for sending Accounting-Off updates
 *
 * @package Auth_RADIUS
 */
class Auth_RADIUS_Acct_Off extends Auth_RADIUS_Acct
{
    /**
      * Defines the type of the accounting request.
      * It is set to RADIUS_ACCOUNTING_OFF by default in this class.
      * @var  integer
      */
    var $status_type = RADIUS_ACCOUNTING_OFF;
}

?>