Project

General

Profile

Download (7.51 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 212ab80f Ermal
/**
45
 * Get the NAS-IP-Address based on the current wan address
46
 *
47
 * Use functions in interfaces.inc to find this out
48
 *
49
 */
50
if (!function_exists('getNasIP')) {
51
        function getNasIP()
52
        {
53
                global $config, $cpzone;
54
55
                if (empty($config['captiveportal'][$cpzone]['radiussrcip_attribute'])) {
56
                                $nasIp = get_interface_ip();
57
                } else {
58
                        if (is_ipaddr($config['captiveportal'][$cpzone]['radiussrcip_attribute']))
59
                                $nasIp = $config['captiveportal'][$cpzone]['radiussrcip_attribute'];
60
                        else
61
                                $nasIp = get_interface_ip($config['captiveportal'][$cpzone]['radiussrcip_attribute']);
62
                }
63
64
                if(!is_ipaddr($nasIp))
65
                        $nasIp = "0.0.0.0";
66
67
                return $nasIp;
68
        }
69
}
70
71 856e58a6 Scott Ullrich
/*
72
RADIUS AUTHENTICATION
73
---------------------
74
*/
75
76 b3765f4c Roberto Nunnari
require_once("CHAP.inc");
77
78 856e58a6 Scott Ullrich
function RADIUS_AUTHENTICATION($username,$password,$radiusservers,$clientip,$clientmac,$ruleno) {
79
80 b4792bf8 Ermal
    global $config, $cpzone;
81 856e58a6 Scott Ullrich
82
    $retvalue = array();
83 54df925b bcyrill
    $clientmac = mac_format($clientmac, $cpzone);
84 89a31c7e Ermal Lu?i
    $nas_port = $ruleno;
85 b4792bf8 Ermal
    $radiusvendor = $config['captiveportal'][$cpzone]['radiusvendor'] ? $config['captiveportal'][$cpzone]['radiusvendor'] : null;
86 b3765f4c Roberto Nunnari
    $radius_protocol = $config['captiveportal'][$cpzone]['radius_protocol'];
87 856e58a6 Scott Ullrich
    // Do we even need to set it to NULL?
88
    $retvalue['error'] = $retvalue['reply_message'] = $retvalue['url_redirection'] = $retvalue['session_timeout'] = $retvalue['idle_timeout'] = $retvalue['session_terminate_time'] = null;
89
90
    switch($radiusvendor) {
91
92
        case 'cisco':
93 34e9ca60 Ermal
        	$calledstationid = $clientmac;
94
        	$callingstationid = $clientip;
95
        	break;
96 856e58a6 Scott Ullrich
        default:
97 212ab80f Ermal
        	$calledstationid = getNasIP();
98 34e9ca60 Ermal
        	$callingstationid = $clientmac;
99
		break;
100 856e58a6 Scott Ullrich
    }
101
102
    // Create our instance
103 b3765f4c Roberto Nunnari
    $classname = 'Auth_RADIUS_' . $radius_protocol;
104
    $rauth = new $classname($username, $password);
105 856e58a6 Scott Ullrich
106
    /*
107 27588018 Ermal
     * Add support for more then one radiusserver. 
108
     * At most 10 servers may be specified. 
109
     * When multiple servers are given, they are tried in round-robin fashion until a valid response is received 
110
     */
111 856e58a6 Scott Ullrich
    foreach ($radiusservers as $radsrv) {
112
        // Add a new server to our instance
113
        $rauth->addServer($radsrv['ipaddr'], $radsrv['port'], $radsrv['key']);
114
115
    }
116
117
    // Construct data package
118
    $rauth->username = $username;
119 b3765f4c Roberto Nunnari
    switch ($radius_protocol) {
120
        case 'CHAP_MD5':
121
        case 'MSCHAPv1':
122
            $classname = $radius_protocol == 'MSCHAPv1' ? 'Crypt_CHAP_MSv1' : 'Crypt_CHAP_MD5';
123
            $crpt = new $classname;
124
            $crpt->username = $username;
125
            $crpt->password = $password;
126
            $rauth->challenge = $crpt->challenge;
127
            $rauth->chapid = $crpt->chapid;
128
            $rauth->response = $crpt->challengeResponse();
129
            $rauth->flags = 1;
130
            // If you must use deprecated and weak LAN-Manager-Responses use this:
131
            //$rauth->lmResponse = $crpt->lmChallengeResponse();
132
            //$rauth->flags = 0;
133
            break;
134
135
        case 'MSCHAPv2':
136
            // Construct data package
137
            $crpt = new Crypt_CHAP_MSv2;
138
            $crpt->username = $username;
139
            $crpt->password = $password;
140
            $rauth->challenge = $crpt->authChallenge;
141
            $rauth->peerChallenge = $crpt->peerChallenge;
142
            $rauth->chapid = $crpt->chapid;
143
            $rauth->response = $crpt->challengeResponse();
144
            break;
145
146
        default:
147
            $rauth->password = $password;
148
            break;
149
    }
150 856e58a6 Scott Ullrich
151
    if (PEAR::isError($rauth->start())) {
152
        $retvalue['auth_val'] = 1;
153 ef345a70 Scott Ullrich
        $retvalue['error'] = $rauth->getError(); 
154 5918fbaf Scott Ullrich
155 856e58a6 Scott Ullrich
        // If we encounter an error immediately stop this function and go back
156
        $rauth->close();
157
        return $retvalue;
158
    }
159
160
    // Default attributes
161
    $rauth->putAttribute(RADIUS_SERVICE_TYPE, RADIUS_LOGIN);
162
    $rauth->putAttribute(RADIUS_NAS_PORT_TYPE, RADIUS_ETHERNET);
163 d0d70b03 Ermal
    $rauth->putAttribute(RADIUS_NAS_PORT, $nas_port, 'integer');
164 856e58a6 Scott Ullrich
165
    // Extra data to identify the client and nas
166
    $rauth->putAttribute(RADIUS_FRAMED_IP_ADDRESS, $clientip, addr);
167
    $rauth->putAttribute(RADIUS_CALLED_STATION_ID, $calledstationid);
168
    $rauth->putAttribute(RADIUS_CALLING_STATION_ID, $callingstationid);
169
170
    // Send request
171
    $result = $rauth->send();
172
173
    // Evaluation of the response
174
    // 1 -> Access-Request => We will use this value as an error indicator since we can't get a 1 back from the radius
175
    // 2 -> Access-Accept
176
    // 3 -> Access-Reject
177
    // See RFC2865 for this.
178
    if (PEAR::isError($result)) {
179
        $retvalue['auth_val'] = 1;
180
        $retvalue['error'] = $result->getMessage();
181 637345f3 Scott Ullrich
182 856e58a6 Scott Ullrich
    } else if ($result === true) {
183
        $retvalue['auth_val'] = 2;
184 637345f3 Scott Ullrich
185 856e58a6 Scott Ullrich
    } else {
186
        $retvalue['auth_val'] = 3;
187 637345f3 Scott Ullrich
188 856e58a6 Scott Ullrich
    }
189
190
    // Get attributes, even if auth failed.
191
    // We will push the results in the retvalue array
192
    if (!$rauth->getAttributes()) {
193
        $retvalue['error'] = $rauth->getError();
194 637345f3 Scott Ullrich
195 856e58a6 Scott Ullrich
    } else {
196
        $retvalue = array_merge($retvalue,$rauth->listAttributes());
197 637345f3 Scott Ullrich
198 856e58a6 Scott Ullrich
        // We convert the session_terminate_time to unixtimestamp if its set before returning the whole array to our caller
199
        if (!empty($retvalue['session_terminate_time'])) {
200
        $stt = &$retvalue['session_terminate_time'];
201
        $stt = strtotime(preg_replace("/\+(\d+):(\d+)$/", " +\${1}\${2}", preg_replace("/(\d+)T(\d+)/", "\${1} \${2}",$stt)));
202
         }
203
    }
204
205
    // close OO RADIUS_AUTHENTICATION
206
    $rauth->close();
207
208
    return $retvalue;
209
210 5b237745 Scott Ullrich
}
211 409d2492 Scott Ullrich
212 d0d70b03 Ermal
?>