PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/www/class/centreonAuth.SSO.class.php

https://gitlab.com/florianocomercial/centreon
PHP | 112 lines | 58 code | 10 blank | 44 comment | 23 complexity | 2f515ab6ed896d3bbde578e1ca7ef420 MD5 | raw file
  1. <?php
  2. /*
  3. * Copyright 2005-2015 Centreon
  4. * Centreon is developped by : Julien Mathis and Romain Le Merlus under
  5. * GPL Licence 2.0.
  6. *
  7. * This program is free software; you can redistribute it and/or modify it under
  8. * the terms of the GNU General Public License as published by the Free Software
  9. * Foundation ; either version 2 of the License.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT ANY
  12. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  13. * PARTICULAR PURPOSE. See the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, see <http://www.gnu.org/licenses>.
  17. *
  18. * Linking this program statically or dynamically with other modules is making a
  19. * combined work based on this program. Thus, the terms and conditions of the GNU
  20. * General Public License cover the whole combination.
  21. *
  22. * As a special exception, the copyright holders of this program give Centreon
  23. * permission to link this program with independent modules to produce an executable,
  24. * regardless of the license terms of these independent modules, and to copy and
  25. * distribute the resulting executable under terms of Centreon choice, provided that
  26. * Centreon also meet, for each linked independent module, the terms and conditions
  27. * of the license of that module. An independent module is a module which is not
  28. * derived from this program. If you modify this program, you may extend this
  29. * exception to your version of the program, but you are not obliged to do so. If you
  30. * do not wish to do so, delete this exception statement from your version.
  31. *
  32. * For more information : contact@centreon.com
  33. *
  34. */
  35. include_once (_CENTREON_PATH_."/www/class/centreonAuth.class.php");
  36. class CentreonAuthSSO extends CentreonAuth {
  37. protected $options_sso = array();
  38. protected $sso_mandatory = 0;
  39. public function __construct($username, $password, $autologin, $pearDB, $CentreonLog, $encryptType = 1, $token = "", $generalOptions) {
  40. $this->options_sso = $generalOptions;
  41. # var
  42. #$this->options_sso['sso_enable'] = 1;
  43. #$this->options_sso['sso_mode'] = 1;
  44. #$this->options_sso['sso_trusted_clients'] = '10.30.3.53';
  45. #$this->options_sso['sso_header_username'] = 'HTTP_AUTH_USER';
  46. if (isset($this->options_sso['sso_enable']) && $this->options_sso['sso_enable'] == 1 &&
  47. isset($this->options_sso['sso_header_username']) && $this->options_sso['sso_header_username'] != '') {
  48. $this->sso_username = $_SERVER[$this->options_sso['sso_header_username']];
  49. if ($this->check_sso_client()) {
  50. $this->sso_mandatory = 1;
  51. $username = $this->sso_username;
  52. if (isset($this->options_sso['sso_username_pattern']) && $this->options_sso['sso_username_pattern'] != '') {
  53. $username = preg_replace($this->options_sso['sso_username_pattern'], $this->options_sso['sso_username_replace'], $username);
  54. }
  55. }
  56. }
  57. parent::__construct($username, $password, $autologin, $pearDB, $CentreonLog, $encryptType, $token);
  58. if ($this->error != '' && $this->sso_mandatory == 1) {
  59. global $msg_error;
  60. $msg_error = "Invalid User. SSO Protection (user=" . $this->sso_username . ")";
  61. }
  62. }
  63. protected function check_sso_client() {
  64. if (isset($this->options_sso['sso_mode']) && $this->options_sso['sso_mode'] == 1) {
  65. # Mixed
  66. $blacklist = explode(',', $this->options_sso['sso_blacklist_clients']);
  67. foreach ($blacklist as $value) {
  68. $value = trim($value);
  69. if (preg_match('/' . $value . '/', $_SERVER['REMOTE_ADDR'])) {
  70. return 0;
  71. }
  72. }
  73. $whitelist = explode(',', $this->options_sso['sso_trusted_clients']);
  74. foreach ($whitelist as $value) {
  75. $value = trim($value);
  76. if (preg_match('/' . $value . '/', $_SERVER['REMOTE_ADDR'])) {
  77. return 1;
  78. }
  79. }
  80. return 0;
  81. } else {
  82. # Only SSO (no login from local users)
  83. return 1;
  84. }
  85. }
  86. protected function checkPassword($password, $token, $autoimport = false) {
  87. if ($this->sso_mandatory == 1) {
  88. # Mode LDAP autoimport. Need to call it
  89. if ($autoimport) {
  90. # Password is only because it needs one...
  91. parent::checkPassword('test', $token, $autoimport);
  92. }
  93. # We delete old sessions with same SID
  94. global $pearDB;
  95. $pearDB->query("DELETE FROM session WHERE session_id = '".session_id()."'");
  96. $this->passwdOk = 1;
  97. } else {
  98. # local connect (when sso not enabled and 'sso_mode' == 1
  99. return parent::checkPassword($password, $token);
  100. }
  101. }
  102. }