Project

General

Profile

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