4 * @author Martin Dougiamas
5 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
6 * @package moodle multiauth
8 * Authentication Plugin: RADIUS Authentication
10 * Authenticates against a RADIUS server.
11 * Contributed by Clive Gould <clive@ce.bromley.ac.uk>
12 * CHAP support contributed by Stanislav Tsymbalov http://www.tsymbalov.net/
14 * 2006-08-31 File created.
15 * 2008-03-12 CHAP support added by Stanislav Tsymbalov.
18 if (!defined('MOODLE_INTERNAL')) {
19 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
22 require_once($CFG->libdir
.'/authlib.php');
25 * RADIUS authentication plugin.
27 class auth_plugin_radius
extends auth_plugin_base
{
32 function auth_plugin_radius() {
33 $this->authtype
= 'radius';
34 $this->config
= get_config('auth/radius');
38 * Returns true if the username and password work and false if they are
39 * wrong or don't exist.
41 * @param string $username The username
42 * @param string $password The password
43 * @return bool Authentication success or failure.
45 function user_login ($username, $password) {
46 require_once 'Auth/RADIUS.php';
47 require_once 'Crypt/CHAP.php';
49 // Added by Clive on 7th May for test purposes
50 // printf("Username: $username <br/>");
51 // printf("Password: $password <br/>");
52 // printf("host: $this->config->host <br/>");
53 // printf("nasport: $this->config->nasport <br/>");
54 // printf("secret: $this->config->secret <br/>");
56 // Added by Stanislav Tsymbalov on 12th March 2008 only for test purposes
61 $type = $this->config
->radiustype
;
66 $classname = 'Auth_RADIUS_' . $type;
67 $rauth = new $classname($username, $password);
68 $rauth->addServer($this->config
->host
, $this->config
->nasport
, $this->config
->secret
);
70 $rauth->username
= $username;
75 $classname = $type == 'MSCHAPv1' ?
'Crypt_CHAP_MSv1' : 'Crypt_CHAP_MD5';
76 $crpt = new $classname;
77 $crpt->password
= $password;
78 $rauth->challenge
= $crpt->challenge
;
79 $rauth->chapid
= $crpt->chapid
;
80 $rauth->response
= $crpt->challengeResponse();
82 // If you must use deprecated and weak LAN-Manager-Responses use this:
83 // $rauth->lmResponse = $crpt->lmChallengeResponse();
88 $crpt = new Crypt_CHAP_MSv2
;
89 $crpt->username
= $username;
90 $crpt->password
= $password;
91 $rauth->challenge
= $crpt->authChallenge
;
92 $rauth->peerChallenge
= $crpt->peerChallenge
;
93 $rauth->chapid
= $crpt->chapid
;
94 $rauth->response
= $crpt->challengeResponse();
98 $rauth->password
= $password;
102 if (!$rauth->start()) {
103 printf("Radius start: %s<br/>\n", $rauth->getError());
107 $result = $rauth->send();
108 if (PEAR
::isError($result)) {
109 printf("Radius send failed: %s<br/>\n", $result->getMessage());
111 } else if ($result === true) {
112 // printf("Radius Auth succeeded<br/>\n");
115 // printf("Radius Auth rejected<br/>\n");
119 // get attributes, even if auth failed
120 if (!$rauth->getAttributes()) {
121 printf("Radius getAttributes: %s<br/>\n", $rauth->getError());
123 $rauth->dumpAttributes();
129 function prevent_local_passwords() {
134 * Returns true if this authentication plugin is 'internal'.
138 function is_internal() {
143 * Returns true if this authentication plugin can change the user's
148 function can_change_password() {
153 * Prints a form for configuring this authentication plugin.
155 * This function is called from admin/auth.php, and outputs a full page with
156 * a form for configuring this plugin.
158 * @param array $page An object containing all the data for this page.
160 function config_form($config, $err, $user_fields) {
163 include "config.html";
167 * Processes and stores configuration data for this authentication plugin.
169 function process_config($config) {
170 // set to defaults if undefined
171 if (!isset ($config->host
)) {
172 $config->host
= '127.0.0.1';
174 if (!isset ($config->nasport
)) {
175 $config->nasport
= '1812';
177 if (!isset($config->radiustype
)) {
178 $config->radiustype
= 'PAP';
180 if (!isset ($config->secret
)) {
181 $config->secret
= '';
183 if (!isset($config->changepasswordurl
)) {
184 $config->changepasswordurl
= '';
188 set_config('host', $config->host
, 'auth/radius');
189 set_config('nasport', $config->nasport
, 'auth/radius');
190 set_config('secret', $config->secret
, 'auth/radius');
191 set_config('changepasswordurl', $config->changepasswordurl
, 'auth/radius');
192 set_config('radiustype', $config->radiustype
, 'auth/radius');