MDL-40179 auth_cas: change is_proxybypass parameter to use server url
[moodle.git] / auth / cas / auth.php
CommitLineData
b9ddb2d5 1<?php
a2f10958
PS
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
df884cd8 16
b9ddb2d5 17/**
b9ddb2d5 18 * Authentication Plugin: CAS Authentication
19 *
20 * Authentication using CAS (Central Authentication Server).
21 *
a2f10958
PS
22 * @author Martin Dougiamas
23 * @author Jerome GUTIERREZ
24 * @author Iñaki Arenaza
25 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
26 * @package auth_cas
b9ddb2d5 27 */
fcf46da1 28
60d7078a 29defined('MOODLE_INTERNAL') || die();
fcf46da1
I
30
31require_once($CFG->dirroot.'/auth/ldap/auth.php');
c8afa50b 32require_once($CFG->dirroot.'/auth/cas/CAS/CAS.php');
fcf46da1 33
b9ddb2d5 34/**
35 * CAS authentication plugin.
36 */
fcf46da1
I
37class auth_plugin_cas extends auth_plugin_ldap {
38
b9ddb2d5 39 /**
40 * Constructor.
41 */
42 function auth_plugin_cas() {
6bc1e5d5 43 $this->authtype = 'cas';
fcf46da1
I
44 $this->roleauth = 'auth_cas';
45 $this->errorlogtag = '[AUTH CAS] ';
46 $this->init_plugin($this->authtype);
b9ddb2d5 47 }
edb5da83
PS
48
49 function prevent_local_passwords() {
50 return true;
51 }
52
b9ddb2d5 53 /**
fcf46da1 54 * Authenticates user against CAS
b9ddb2d5 55 * Returns true if the username and password work and false if they are
56 * wrong or don't exist.
57 *
fcf46da1
I
58 * @param string $username The username (with system magic quotes)
59 * @param string $password The password (with system magic quotes)
139ebfdb 60 * @return bool Authentication success or failure.
b9ddb2d5 61 */
62 function user_login ($username, $password) {
4454447d 63 $this->connectCAS();
2f1e464a 64 return phpCAS::isAuthenticated() && (trim(core_text::strtolower(phpCAS::getUser())) == $username);
b9ddb2d5 65 }
fcf46da1 66
b9ddb2d5 67 /**
68 * Returns true if this authentication plugin is 'internal'.
69 *
139ebfdb 70 * @return bool
b9ddb2d5 71 */
72 function is_internal() {
73 return false;
74 }
fcf46da1 75
b9ddb2d5 76 /**
77 * Returns true if this authentication plugin can change the user's
78 * password.
79 *
139ebfdb 80 * @return bool
b9ddb2d5 81 */
82 function can_change_password() {
0c121d99 83 return false;
b9ddb2d5 84 }
fcf46da1 85
0c121d99 86 /**
fcf46da1
I
87 * Authentication choice (CAS or other)
88 * Redirection to the CAS form or to login/index.php
0c121d99 89 * for other authentication
90 */
f5fd4347 91 function loginpage_hook() {
fcf46da1
I
92 global $frm;
93 global $CFG;
94 global $SESSION, $OUTPUT, $PAGE;
6dfddff7 95
fcf46da1
I
96 $site = get_site();
97 $CASform = get_string('CASform', 'auth_cas');
98 $username = optional_param('username', '', PARAM_RAW);
adbf94c9 99 $courseid = optional_param('courseid', 0, PARAM_INT);
6dfddff7 100
fcf46da1
I
101 if (!empty($username)) {
102 if (isset($SESSION->wantsurl) && (strstr($SESSION->wantsurl, 'ticket') ||
103 strstr($SESSION->wantsurl, 'NOCAS'))) {
104 unset($SESSION->wantsurl);
105 }
106 return;
6bc1e5d5 107 }
6dfddff7 108
fcf46da1
I
109 // Return if CAS enabled and settings not specified yet
110 if (empty($this->config->hostname)) {
111 return;
112 }
c39e7213 113
886601cf
GPL
114 // If the multi-authentication setting is used, check for the param before connecting to CAS.
115 if ($this->config->multiauth) {
699001c7
GPL
116
117 // If there is an authentication error, stay on the default authentication page.
118 if (!empty($SESSION->loginerrormsg)) {
119 return;
120 }
121
886601cf
GPL
122 $authCAS = optional_param('authCAS', '', PARAM_RAW);
123 if ($authCAS == 'NOCAS') {
124 return;
125 }
126 // Show authentication form for multi-authentication.
127 // Test pgtIou parameter for proxy mode (https connection in background from CAS server to the php server).
128 if ($authCAS != 'CAS' && !isset($_GET['pgtIou'])) {
129 $PAGE->set_url('/login/index.php');
130 $PAGE->navbar->add($CASform);
131 $PAGE->set_title("$site->fullname: $CASform");
132 $PAGE->set_heading($site->fullname);
133 echo $OUTPUT->header();
134 include($CFG->dirroot.'/auth/cas/cas_form.html');
135 echo $OUTPUT->footer();
136 exit();
137 }
138 }
139
fcf46da1
I
140 // Connection to CAS server
141 $this->connectCAS();
4454447d 142
fcf46da1 143 if (phpCAS::checkAuthentication()) {
99f66eb1 144 $frm = new stdClass();
fcf46da1
I
145 $frm->username = phpCAS::getUser();
146 $frm->password = 'passwdCas';
adbf94c9
GPL
147
148 // Redirect to a course if multi-auth is activated, authCAS is set to CAS and the courseid is specified.
149 if ($this->config->multiauth && !empty($courseid)) {
150 redirect(new moodle_url('/course/view.php', array('id'=>$courseid)));
151 }
152
fcf46da1
I
153 return;
154 }
4454447d 155
fcf46da1 156 if (isset($_GET['loginguest']) && ($_GET['loginguest'] == true)) {
99f66eb1 157 $frm = new stdClass();
fcf46da1
I
158 $frm->username = 'guest';
159 $frm->password = 'guest';
160 return;
161 }
389d6f72 162
fcf46da1
I
163 // Force CAS authentication (if needed).
164 if (!phpCAS::isAuthenticated()) {
165 phpCAS::setLang($this->config->language);
166 phpCAS::forceAuthentication();
167 }
168 }
e295df44 169
fcf46da1 170
0c121d99 171 /**
fcf46da1 172 * Connect to the CAS (clientcas connection or proxycas connection)
0c121d99 173 *
174 */
175 function connectCAS() {
02a9e4a7
IA
176 global $CFG;
177 static $connected = false;
4454447d 178
02a9e4a7 179 if (!$connected) {
fcf46da1
I
180 // Make sure phpCAS doesn't try to start a new PHP session when connecting to the CAS server.
181 if ($this->config->proxycas) {
182 phpCAS::proxy($this->config->casversion, $this->config->hostname, (int) $this->config->port, $this->config->baseuri, false);
183 } else {
184 phpCAS::client($this->config->casversion, $this->config->hostname, (int) $this->config->port, $this->config->baseuri, false);
185 }
02a9e4a7 186 $connected = true;
fcf46da1 187 }
4454447d 188
5c8535dc 189 // If Moodle is configured to use a proxy, phpCAS needs some curl options set.
e450eb32 190 if (!empty($CFG->proxyhost) && !is_proxybypass(phpCAS::getServerLoginURL())) {
5c8535dc
BK
191 phpCAS::setExtraCurlOption(CURLOPT_PROXY, $CFG->proxyhost);
192 if (!empty($CFG->proxyport)) {
193 phpCAS::setExtraCurlOption(CURLOPT_PROXYPORT, $CFG->proxyport);
194 }
195 if (!empty($CFG->proxytype)) {
196 // Only set CURLOPT_PROXYTYPE if it's something other than the curl-default http
197 if ($CFG->proxytype == 'SOCKS5') {
198 phpCAS::setExtraCurlOption(CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
199 }
200 }
201 if (!empty($CFG->proxyuser) and !empty($CFG->proxypassword)) {
202 phpCAS::setExtraCurlOption(CURLOPT_PROXYUSERPWD, $CFG->proxyuser.':'.$CFG->proxypassword);
203 if (defined('CURLOPT_PROXYAUTH')) {
204 // any proxy authentication if PHP 5.1
205 phpCAS::setExtraCurlOption(CURLOPT_PROXYAUTH, CURLAUTH_BASIC | CURLAUTH_NTLM);
206 }
207 }
208 }
209
02a9e4a7 210 if ($this->config->certificate_check && $this->config->certificate_path){
387d1dc0 211 phpCAS::setCasServerCACert($this->config->certificate_path);
02a9e4a7 212 } else {
387d1dc0
I
213 // Don't try to validate the server SSL credentials
214 phpCAS::setNoCasServerValidation();
215 }
0c121d99 216 }
fcf46da1 217
b9ddb2d5 218 /**
219 * Prints a form for configuring this authentication plugin.
220 *
221 * This function is called from admin/auth.php, and outputs a full page with
222 * a form for configuring this plugin.
223 *
224 * @param array $page An object containing all the data for this page.
225 */
139ebfdb 226 function config_form($config, $err, $user_fields) {
fcf46da1 227 global $CFG, $OUTPUT;
95cb3955 228
840fcf0c
I
229 if (!function_exists('ldap_connect')) { // Is php-ldap really there?
230 echo $OUTPUT->notification(get_string('auth_ldap_noextension', 'auth_ldap'));
231
232 // Don't return here, like we do in auth/ldap. We cas use CAS without LDAP.
233 // So just warn the user (done above) and define the LDAP constants we use
234 // in config.html, to silence the warnings.
235 if (!defined('LDAP_DEREF_NEVER')) {
236 define ('LDAP_DEREF_NEVER', 0);
237 }
238 if (!defined('LDAP_DEREF_ALWAYS')) {
02a9e4a7 239 define ('LDAP_DEREF_ALWAYS', 3);
840fcf0c
I
240 }
241 }
242
fcf46da1 243 include($CFG->dirroot.'/auth/cas/config.html');
b9ddb2d5 244 }
fcf46da1 245
40293947
I
246 /**
247 * A chance to validate form data, and last chance to
248 * do stuff before it is inserted in config_plugin
249 * @param object object with submitted configuration settings (without system magic quotes)
250 * @param array $err array of error messages
251 */
e2bb3c92 252 function validate_form($form, &$err) {
40293947
I
253 $certificate_path = trim($form->certificate_path);
254 if ($form->certificate_check && empty($certificate_path)) {
255 $err['certificate_path'] = get_string('auth_cas_certificate_path_empty', 'auth_cas');
256 }
257 }
258
b9ddb2d5 259 /**
430759a5 260 * Returns the URL for changing the user's pw, or empty if the default can
b9ddb2d5 261 * be used.
262 *
99f9f85f 263 * @return moodle_url
b9ddb2d5 264 */
265 function change_password_url() {
99f9f85f 266 return null;
b9ddb2d5 267 }
fcf46da1 268
b9ddb2d5 269 /**
270 * Processes and stores configuration data for this authentication plugin.
271 */
272 function process_config($config) {
fcf46da1 273
0c121d99 274 // CAS settings
fcf46da1 275 if (!isset($config->hostname)) {
b9ddb2d5 276 $config->hostname = '';
fcf46da1
I
277 }
278 if (!isset($config->port)) {
b9ddb2d5 279 $config->port = '';
fcf46da1
I
280 }
281 if (!isset($config->casversion)) {
b9ddb2d5 282 $config->casversion = '';
fcf46da1
I
283 }
284 if (!isset($config->baseuri)) {
b9ddb2d5 285 $config->baseuri = '';
fcf46da1
I
286 }
287 if (!isset($config->language)) {
b9ddb2d5 288 $config->language = '';
fcf46da1
I
289 }
290 if (!isset($config->proxycas)) {
0c121d99 291 $config->proxycas = '';
0c121d99 292 }
fcf46da1
I
293 if (!isset($config->logoutcas)) {
294 $config->logoutcas = '';
0c121d99 295 }
fcf46da1
I
296 if (!isset($config->multiauth)) {
297 $config->multiauth = '';
0c121d99 298 }
387d1dc0
I
299 if (!isset($config->certificate_check)) {
300 $config->certificate_check = '';
301 }
302 if (!isset($config->certificate_path)) {
303 $config->certificate_path = '';
304 }
7d697431 305 if (!isset($config->logout_return_url)) {
7d697431
JPG
306 $config->logout_return_url = '';
307 }
74c9f514 308
fcf46da1
I
309 // LDAP settings
310 if (!isset($config->host_url)) {
311 $config->host_url = '';
0c121d99 312 }
326929d5
IA
313 if (!isset($config->start_tls)) {
314 $config->start_tls = false;
315 }
fcf46da1
I
316 if (empty($config->ldapencoding)) {
317 $config->ldapencoding = 'utf-8';
0c121d99 318 }
c090d7c9
IA
319 if (!isset($config->pagesize)) {
320 $config->pagesize = LDAP_DEFAULT_PAGESIZE;
321 }
fcf46da1
I
322 if (!isset($config->contexts)) {
323 $config->contexts = '';
0c121d99 324 }
fcf46da1
I
325 if (!isset($config->user_type)) {
326 $config->user_type = 'default';
eee34307 327 }
fcf46da1
I
328 if (!isset($config->user_attribute)) {
329 $config->user_attribute = '';
0c121d99 330 }
fcf46da1
I
331 if (!isset($config->search_sub)) {
332 $config->search_sub = '';
0c121d99 333 }
fcf46da1
I
334 if (!isset($config->opt_deref)) {
335 $config->opt_deref = LDAP_DEREF_NEVER;
0c121d99 336 }
fcf46da1
I
337 if (!isset($config->bind_dn)) {
338 $config->bind_dn = '';
0c121d99 339 }
fcf46da1
I
340 if (!isset($config->bind_pw)) {
341 $config->bind_pw = '';
0c121d99 342 }
1298e78b
I
343 if (!isset($config->ldap_version)) {
344 $config->ldap_version = '3';
0c121d99 345 }
fcf46da1
I
346 if (!isset($config->objectclass)) {
347 $config->objectclass = '';
0c121d99 348 }
fcf46da1
I
349 if (!isset($config->memberattribute)) {
350 $config->memberattribute = '';
0c121d99 351 }
df884cd8 352
fcf46da1
I
353 if (!isset($config->memberattribute_isdn)) {
354 $config->memberattribute_isdn = '';
0c121d99 355 }
fcf46da1
I
356 if (!isset($config->attrcreators)) {
357 $config->attrcreators = '';
0c121d99 358 }
fcf46da1
I
359 if (!isset($config->groupecreators)) {
360 $config->groupecreators = '';
0c121d99 361 }
fcf46da1
I
362 if (!isset($config->removeuser)) {
363 $config->removeuser = AUTH_REMOVEUSER_KEEP;
0c121d99 364 }
df884cd8 365
fcf46da1
I
366 // save CAS settings
367 set_config('hostname', trim($config->hostname), $this->pluginconfig);
368 set_config('port', trim($config->port), $this->pluginconfig);
369 set_config('casversion', $config->casversion, $this->pluginconfig);
370 set_config('baseuri', trim($config->baseuri), $this->pluginconfig);
371 set_config('language', $config->language, $this->pluginconfig);
372 set_config('proxycas', $config->proxycas, $this->pluginconfig);
373 set_config('logoutcas', $config->logoutcas, $this->pluginconfig);
374 set_config('multiauth', $config->multiauth, $this->pluginconfig);
387d1dc0
I
375 set_config('certificate_check', $config->certificate_check, $this->pluginconfig);
376 set_config('certificate_path', $config->certificate_path, $this->pluginconfig);
7d697431 377 set_config('logout_return_url', $config->logout_return_url, $this->pluginconfig);
df884cd8 378
fcf46da1
I
379 // save LDAP settings
380 set_config('host_url', trim($config->host_url), $this->pluginconfig);
326929d5 381 set_config('start_tls', $config->start_tls, $this->pluginconfig);
fcf46da1 382 set_config('ldapencoding', trim($config->ldapencoding), $this->pluginconfig);
c090d7c9 383 set_config('pagesize', (int)trim($config->pagesize), $this->pluginconfig);
fcf46da1 384 set_config('contexts', trim($config->contexts), $this->pluginconfig);
2f1e464a
PS
385 set_config('user_type', core_text::strtolower(trim($config->user_type)), $this->pluginconfig);
386 set_config('user_attribute', core_text::strtolower(trim($config->user_attribute)), $this->pluginconfig);
fcf46da1
I
387 set_config('search_sub', $config->search_sub, $this->pluginconfig);
388 set_config('opt_deref', $config->opt_deref, $this->pluginconfig);
389 set_config('bind_dn', trim($config->bind_dn), $this->pluginconfig);
390 set_config('bind_pw', $config->bind_pw, $this->pluginconfig);
1298e78b 391 set_config('ldap_version', $config->ldap_version, $this->pluginconfig);
fcf46da1 392 set_config('objectclass', trim($config->objectclass), $this->pluginconfig);
2f1e464a 393 set_config('memberattribute', core_text::strtolower(trim($config->memberattribute)), $this->pluginconfig);
fcf46da1
I
394 set_config('memberattribute_isdn', $config->memberattribute_isdn, $this->pluginconfig);
395 set_config('attrcreators', trim($config->attrcreators), $this->pluginconfig);
396 set_config('groupecreators', trim($config->groupecreators), $this->pluginconfig);
397 set_config('removeuser', $config->removeuser, $this->pluginconfig);
df884cd8 398
0c121d99 399 return true;
400 }
df884cd8 401
0c121d99 402 /**
403 * Returns true if user should be coursecreator.
404 *
405 * @param mixed $username username (without system magic quotes)
406 * @return boolean result
407 */
408 function iscreator($username) {
ab8b1d15 409 if (empty($this->config->host_url) or (empty($this->config->attrcreators) && empty($this->config->groupecreators)) or empty($this->config->memberattribute)) {
fcf46da1 410 return false;
0c121d99 411 }
fcf46da1 412
2f1e464a 413 $extusername = core_text::convert($username, 'utf-8', $this->config->ldapencoding);
74c9f514 414
fcf46da1
I
415 // Test for group creator
416 if (!empty($this->config->groupecreators)) {
c28694dc 417 $ldapconnection = $this->ldap_connect();
fcf46da1
I
418 if ($this->config->memberattribute_isdn) {
419 if(!($userid = $this->ldap_find_userdn($ldapconnection, $extusername))) {
420 return false;
0c121d99 421 }
fcf46da1
I
422 } else {
423 $userid = $extusername;
0c121d99 424 }
fcf46da1
I
425
426 $group_dns = explode(';', $this->config->groupecreators);
427 if (ldap_isgroupmember($ldapconnection, $userid, $group_dns, $this->config->memberattribute)) {
428 return true;
0c121d99 429 }
430 }
74c9f514 431
fcf46da1
I
432 // Build filter for attrcreator
433 if (!empty($this->config->attrcreators)) {
434 $attrs = explode(';', $this->config->attrcreators);
435 $filter = '(& ('.$this->config->user_attribute."=$username)(|";
436 foreach ($attrs as $attr){
437 if(strpos($attr, '=')) {
438 $filter .= "($attr)";
439 } else {
440 $filter .= '('.$this->config->memberattribute."=$attr)";
0c121d99 441 }
442 }
fcf46da1 443 $filter .= '))';
74c9f514 444
fcf46da1
I
445 // Search
446 $result = $this->ldap_get_userlist($filter);
447 if (count($result) != 0) {
448 return true;
0c121d99 449 }
450 }
fcf46da1
I
451
452 return false;
0c121d99 453 }
ab8b1d15
IA
454
455 /**
456 * Reads user information from LDAP and returns it as array()
457 *
458 * If no LDAP servers are configured, user information has to be
459 * provided via other methods (CSV file, manually, etc.). Return
460 * an empty array so existing user info is not lost. Otherwise,
461 * calls parent class method to get user info.
462 *
463 * @param string $username username
464 * @return mixed array with no magic quotes or false on error
465 */
466 function get_userinfo($username) {
467 if (empty($this->config->host_url)) {
468 return array();
469 }
470 return parent::get_userinfo($username);
471 }
472
473 /**
474 * Syncronizes users from LDAP server to moodle user table.
475 *
476 * If no LDAP servers are configured, simply return. Otherwise,
477 * call parent class method to do the work.
478 *
479 * @param bool $do_updates will do pull in data updates from LDAP if relevant
480 * @return nothing
481 */
482 function sync_users($do_updates=true) {
483 if (empty($this->config->host_url)) {
484 error_log('[AUTH CAS] '.get_string('noldapserver', 'auth_cas'));
485 return;
486 }
487 parent::sync_users($do_updates);
488 }
7d697431
JPG
489
490 /**
491 * Hook for logout page
492 */
493 function logoutpage_hook() {
494 global $USER, $redirect;
02a9e4a7 495
7d697431
JPG
496 // Only do this if the user is actually logged in via CAS
497 if ($USER->auth === $this->authtype) {
498 // Check if there is an alternative logout return url defined
499 if (isset($this->config->logout_return_url) && !empty($this->config->logout_return_url)) {
500 // Set redirect to alternative return url
501 $redirect = $this->config->logout_return_url;
502 }
503 }
504 }
f9f9d187
SL
505
506 /**
507 * Post logout hook.
508 *
509 * Note: this method replace the prelogout_hook method to avoid redirect to CAS logout
510 * before the event userlogout being triggered.
511 *
512 * @param stdClass $user clone of USER object object before the user session was terminated
513 */
514 public function postlogout_hook($user) {
515 global $CFG;
516 // Only redirect to CAS logout if the user is logged as a CAS user.
517 if (!empty($this->config->logoutcas) && $user->auth == $this->authtype) {
71ec01af 518 $backurl = !empty($this->config->logout_return_url) ? $this->config->logout_return_url : $CFG->wwwroot;
f9f9d187
SL
519 $this->connectCAS();
520 phpCAS::logoutWithRedirectService($backurl);
521 }
522 }
b9ddb2d5 523}