Project

General

Profile

Download (5.59 KB) Statistics
| Branch: | Tag: | Revision:
1 5b237745 Scott Ullrich
<?php
2 856e58a6 Scott Ullrich
/* vim: set expandtab tabstop=4 shiftwidth=4: */
3 409d2492 Scott Ullrich
/*
4 856e58a6 Scott Ullrich
    $Id$
5
6
    Copyright (c) 2006, Jonathan De Graeve <jonathan.de.graeve@imelda.be>
7
    All rights reserved.
8
9 ef345a70 Scott Ullrich
    Redistribution and use in source and binary forms, with or without 
10
    modification, are permitted provided that the following conditions 
11 856e58a6 Scott Ullrich
    are met:
12
13 ef345a70 Scott Ullrich
    1. Redistributions of source code must retain the above copyright 
14 856e58a6 Scott Ullrich
       notice, this list of conditions and the following disclaimer.
15 ef345a70 Scott Ullrich
    2. Redistributions in binary form must reproduce the above copyright 
16
       notice, this list of conditions and the following disclaimer in the 
17 856e58a6 Scott Ullrich
       documentation and/or other materials provided with the distribution.
18 ef345a70 Scott Ullrich
    3. The names of the authors may not be used to endorse or promote products 
19 856e58a6 Scott Ullrich
       derived from this software without specific prior written permission.
20
21 ef345a70 Scott Ullrich
    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
22
    ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
23
    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
24
    IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
25
    INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
26
    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
27
    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
28
    OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
29
    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
30 856e58a6 Scott Ullrich
    EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32 ef345a70 Scott Ullrich
    This code cannot simply be copied and put under the GNU Public License or 
33 856e58a6 Scott Ullrich
    any other GPL-like (LGPL, GPL2) License.
34
35
        This code is made possible thx to samples made by Michael Bretterklieber <michael@bretterklieber.com>
36
        author of the PHP PECL Radius package
37
38 409d2492 Scott Ullrich
*/
39 856e58a6 Scott Ullrich
40 f5024891 Scott Ullrich
/*
41
	pfSense_MODULE:	captiveportal
42
*/
43
44 856e58a6 Scott Ullrich
/*
45
RADIUS AUTHENTICATION
46
---------------------
47
*/
48
49
function RADIUS_AUTHENTICATION($username,$password,$radiusservers,$clientip,$clientmac,$ruleno) {
50
51 ef345a70 Scott Ullrich
    global $config;
52 856e58a6 Scott Ullrich
53
    $retvalue = array();
54 27588018 Ermal
    $nas_mac = mac_format(get_interface_mac(get_real_interface("wan")));
55 856e58a6 Scott Ullrich
    $clientmac = mac_format($clientmac);
56 89a31c7e Ermal Lu?i
    $nas_port = $ruleno;
57 856e58a6 Scott Ullrich
    $radiusvendor = $config['captiveportal']['radiusvendor'] ? $config['captiveportal']['radiusvendor'] : null;
58
    // Do we even need to set it to NULL?
59
    $retvalue['error'] = $retvalue['reply_message'] = $retvalue['url_redirection'] = $retvalue['session_timeout'] = $retvalue['idle_timeout'] = $retvalue['session_terminate_time'] = null;
60
61
    switch($radiusvendor) {
62
63
        case 'cisco':
64 34e9ca60 Ermal
        	$calledstationid = $clientmac;
65
        	$callingstationid = $clientip;
66
        	break;
67 856e58a6 Scott Ullrich
        default:
68 34e9ca60 Ermal
		if (is_ipaddr($config['captiveportal']['radiussrcip_attribute']))
69
			$calledstationid = $config['captiveportal']['radiussrcip_attribute'];
70
		else
71
        		$calledstationid = get_interface_ip($config['captiveportal']['radiussrcip_attribute']);
72
        	$callingstationid = $clientmac;
73
		break;
74 856e58a6 Scott Ullrich
    }
75
76
    // Create our instance
77
    $rauth = new Auth_RADIUS_PAP($username, $password);
78
79
    /*
80 27588018 Ermal
     * Add support for more then one radiusserver. 
81
     * At most 10 servers may be specified. 
82
     * When multiple servers are given, they are tried in round-robin fashion until a valid response is received 
83
     */
84 856e58a6 Scott Ullrich
    foreach ($radiusservers as $radsrv) {
85
        // Add a new server to our instance
86
        $rauth->addServer($radsrv['ipaddr'], $radsrv['port'], $radsrv['key']);
87
88
    }
89
90
    // Construct data package
91
    $rauth->username = $username;
92
    $rauth->password = $password;
93
94
    if (PEAR::isError($rauth->start())) {
95
        $retvalue['auth_val'] = 1;
96 ef345a70 Scott Ullrich
        $retvalue['error'] = $rauth->getError(); 
97 5918fbaf Scott Ullrich
98 856e58a6 Scott Ullrich
        // If we encounter an error immediately stop this function and go back
99
        $rauth->close();
100
        return $retvalue;
101
    }
102
103
    // Default attributes
104
    $rauth->putAttribute(RADIUS_SERVICE_TYPE, RADIUS_LOGIN);
105
    $rauth->putAttribute(RADIUS_NAS_PORT_TYPE, RADIUS_ETHERNET);
106 d0d70b03 Ermal
    $rauth->putAttribute(RADIUS_NAS_PORT, $nas_port, 'integer');
107 856e58a6 Scott Ullrich
108
    // Extra data to identify the client and nas
109
    $rauth->putAttribute(RADIUS_FRAMED_IP_ADDRESS, $clientip, addr);
110
    $rauth->putAttribute(RADIUS_CALLED_STATION_ID, $calledstationid);
111
    $rauth->putAttribute(RADIUS_CALLING_STATION_ID, $callingstationid);
112
113
    // Send request
114
    $result = $rauth->send();
115
116
    // Evaluation of the response
117
    // 1 -> Access-Request => We will use this value as an error indicator since we can't get a 1 back from the radius
118
    // 2 -> Access-Accept
119
    // 3 -> Access-Reject
120
    // See RFC2865 for this.
121
    if (PEAR::isError($result)) {
122
        $retvalue['auth_val'] = 1;
123
        $retvalue['error'] = $result->getMessage();
124 637345f3 Scott Ullrich
125 856e58a6 Scott Ullrich
    } else if ($result === true) {
126
        $retvalue['auth_val'] = 2;
127 637345f3 Scott Ullrich
128 856e58a6 Scott Ullrich
    } else {
129
        $retvalue['auth_val'] = 3;
130 637345f3 Scott Ullrich
131 856e58a6 Scott Ullrich
    }
132
133
    // Get attributes, even if auth failed.
134
    // We will push the results in the retvalue array
135
    if (!$rauth->getAttributes()) {
136
        $retvalue['error'] = $rauth->getError();
137 637345f3 Scott Ullrich
138 856e58a6 Scott Ullrich
    } else {
139
        $retvalue = array_merge($retvalue,$rauth->listAttributes());
140 637345f3 Scott Ullrich
141 856e58a6 Scott Ullrich
        // We convert the session_terminate_time to unixtimestamp if its set before returning the whole array to our caller
142
        if (!empty($retvalue['session_terminate_time'])) {
143
        $stt = &$retvalue['session_terminate_time'];
144
        $stt = strtotime(preg_replace("/\+(\d+):(\d+)$/", " +\${1}\${2}", preg_replace("/(\d+)T(\d+)/", "\${1} \${2}",$stt)));
145
         }
146
    }
147
148
    // close OO RADIUS_AUTHENTICATION
149
    $rauth->close();
150
151
    return $retvalue;
152
153 5b237745 Scott Ullrich
}
154 409d2492 Scott Ullrich
155 d0d70b03 Ermal
?>