ldap.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. /**
  3. * plugin_ldap
  4. *
  5. * @method ldap checks a local or remote ldap database to authenticate the user
  6. */
  7. class plugin_ldap {
  8. /**
  9. * Define variables and their scope
  10. */
  11. public $debug;
  12. public $domain_name;
  13. public $username;
  14. public $password;
  15. public $user_uuid;
  16. public $contact_uuid;
  17. /**
  18. * ldap checks a local or remote ldap database to authenticate the user
  19. * @return array [authorized] => true or false
  20. */
  21. function ldap() {
  22. //show the authentication code view
  23. if ($_REQUEST["username"]) {
  24. //pre-process some settings
  25. $settings['theme']['favicon'] = !empty($_SESSION['theme']['favicon']['text']) ? $_SESSION['theme']['favicon']['text'] : PROJECT_PATH.'/themes/default/favicon.ico';
  26. $settings['login']['destination'] = !empty($_SESSION['login']['destination']['text']) ? $_SESSION['login']['destination']['text'] : '';
  27. $settings['users']['unique'] = !empty($_SESSION['users']['unique']['text']) ? $_SESSION['users']['unique']['text'] : '';
  28. $settings['theme']['logo'] = !empty($_SESSION['theme']['logo']['text']) ? $_SESSION['theme']['logo']['text'] : PROJECT_PATH.'/themes/default/images/logo_login.png';
  29. $settings['theme']['login_logo_width'] = !empty($_SESSION['theme']['login_logo_width']['text']) ? $_SESSION['theme']['login_logo_width']['text'] : 'auto; max-width: 300px';
  30. $settings['theme']['login_logo_height'] = !empty($_SESSION['theme']['login_logo_height']['text']) ? $_SESSION['theme']['login_logo_height']['text'] : 'auto; max-height: 300px';
  31. $settings['theme']['background_video'] = isset($_SESSION['theme']['background_video'][0]) ? $_SESSION['theme']['background_video'][0] : null;
  32. //get the domain
  33. $domain_array = explode(":", $_SERVER["HTTP_HOST"]);
  34. $domain_name = $domain_array[0];
  35. //create token
  36. //$object = new token;
  37. //$token = $object->create('login');
  38. //add multi-lingual support
  39. $language = new text;
  40. $text = $language->get(null, '/core/authentication');
  41. //initialize a template object
  42. $view = new template();
  43. $view->engine = 'smarty';
  44. $view->template_dir = $_SERVER["DOCUMENT_ROOT"].PROJECT_PATH.'/core/authentication/resources/views/';
  45. $view->cache_dir = sys_get_temp_dir();
  46. $view->init();
  47. //add translations
  48. $view->assign("login_title", $text['button-login']);
  49. $view->assign("label_username", $text['label-username']);
  50. $view->assign("label_password", $text['label-password']);
  51. $view->assign("button_login", $text['button-login']);
  52. //assign default values to the template
  53. $view->assign("project_path", PROJECT_PATH);
  54. $view->assign("login_destination_url", $settings['login']['destination']);
  55. $view->assign("favicon", $settings['theme']['favicon']);
  56. $view->assign("login_logo_width", $settings['theme']['login_logo_width']);
  57. $view->assign("login_logo_height", $settings['theme']['login_logo_height']);
  58. $view->assign("login_logo_source", $settings['theme']['logo']);
  59. $view->assign("background_video", $settings['theme']['background_video']);
  60. //add the token name and hash to the view
  61. //$view->assign("token_name", $token['name']);
  62. //$view->assign("token_hash", $token['hash']);
  63. //show the views
  64. $content = $view->render('login.htm');
  65. echo $content;
  66. exit;
  67. }
  68. //use ldap to validate the user credentials
  69. if (isset($_SESSION["ldap"]["certpath"])) {
  70. $s = "LDAPTLS_CERT=" . $_SESSION["ldap"]["certpath"]["text"];
  71. putenv($s);
  72. }
  73. if (isset($_SESSION["ldap"]["certkey"])) {
  74. $s = "LDAPTLS_KEY=" . $_SESSION["ldap"]["certkey"]["text"];
  75. putenv($s);
  76. }
  77. $host = $_SESSION["ldap"]["server_host"]["text"];
  78. $port = $_SESSION["ldap"]["server_port"]["numeric"];
  79. $connect = ldap_connect($host, $port)
  80. or die("Could not connect to the LDAP server.");
  81. //ldap_set_option($connect, LDAP_OPT_NETWORK_TIMEOUT, 10);
  82. ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3);
  83. //ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, 7);
  84. //set the default status
  85. $user_authorized = false;
  86. //provide backwards compatability
  87. if (!empty($_SESSION["ldap"]["user_dn"]["text"])) {
  88. $_SESSION["ldap"]["user_dn"][] = $_SESSION["ldap"]["user_dn"]["text"];
  89. }
  90. //check all user_dn in the array
  91. foreach ($_SESSION["ldap"]["user_dn"] as $user_dn) {
  92. $bind_dn = $_SESSION["ldap"]["user_attribute"]["text"]."=".$this->username.",".$user_dn;
  93. $bind_pw = $this->password;
  94. //Note: As of 4/16, the call below will fail randomly. PHP debug reports ldap_bind
  95. //called below with all arguments '*uninitialized*'. However, the debugger
  96. //single-stepping just before the failing call correctly displays all the values.
  97. if (!empty($bind_pw)) {
  98. $bind = ldap_bind($connect, $bind_dn, $bind_pw);
  99. if ($bind) {
  100. //connected and authorized
  101. $user_authorized = true;
  102. break;
  103. }
  104. }
  105. }
  106. //check to see if the user exists
  107. if ($user_authorized) {
  108. $sql = "select * from v_users ";
  109. $sql .= "where username = :username ";
  110. if ($settings['users']['unique'] != "global") {
  111. //unique username per domain (not globally unique across system - example: email address)
  112. $sql .= "and domain_uuid = :domain_uuid ";
  113. $parameters['domain_uuid'] = $this->domain_uuid;
  114. }
  115. $sql .= "and (user_type = 'default' or user_type is null) ";
  116. $parameters['username'] = $this->username;
  117. $database = new database;
  118. $row = $database->select($sql, $parameters, 'row');
  119. if (is_array($row) && @sizeof($row) != 0) {
  120. if ($settings['users']['unique'] == "global" && $row["domain_uuid"] != $this->domain_uuid) {
  121. //get the domain uuid
  122. $this->domain_uuid = $row["domain_uuid"];
  123. $this->domain_name = $_SESSION['domains'][$this->domain_uuid]['domain_name'];
  124. //set the domain session variables
  125. $_SESSION["domain_uuid"] = $this->domain_uuid;
  126. $_SESSION["domain_name"] = $this->domain_name;
  127. //set the setting arrays
  128. $domain = new domains();
  129. $domain->set();
  130. }
  131. $this->user_uuid = $row["user_uuid"];
  132. $this->contact_uuid = $row["contact_uuid"];
  133. }
  134. else {
  135. //salt used with the password to create a one way hash
  136. $salt = generate_password('32', '4');
  137. $password = generate_password('32', '4');
  138. //prepare the uuids
  139. $this->user_uuid = uuid();
  140. $this->contact_uuid = uuid();
  141. //build user insert array
  142. $array['users'][0]['user_uuid'] = $this->user_uuid;
  143. $array['users'][0]['domain_uuid'] = $this->domain_uuid;
  144. $array['users'][0]['contact_uuid'] = $this->contact_uuid;
  145. $array['users'][0]['username'] = strtolower($this->username);
  146. $array['users'][0]['password'] = md5($salt.$password);
  147. $array['users'][0]['salt'] = $salt;
  148. $array['users'][0]['add_date'] = now();
  149. $array['users'][0]['add_user'] = strtolower($this->username);
  150. $array['users'][0]['user_enabled'] = 'true';
  151. //build user group insert array
  152. $array['user_groups'][0]['user_group_uuid'] = uuid();
  153. $array['user_groups'][0]['domain_uuid'] = $this->domain_uuid;
  154. $array['user_groups'][0]['group_name'] = 'user';
  155. $array['user_groups'][0]['user_uuid'] = $this->user_uuid;
  156. //grant temporary permissions
  157. $p = permissions::new();
  158. $p->add('user_add', 'temp');
  159. $p->add('user_group_add', 'temp');
  160. //execute insert
  161. $database = new database;
  162. $database->app_name = 'authentication';
  163. $database->app_uuid = 'a8a12918-69a4-4ece-a1ae-3932be0e41f1';
  164. $database->save($array);
  165. unset($array);
  166. //revoke temporary permissions
  167. $p->delete('user_add', 'temp');
  168. $p->delete('user_group_add', 'temp');
  169. }
  170. unset($sql, $parameters, $row);
  171. }
  172. //result array
  173. $result["ldap"]["plugin"] = "ldap";
  174. $result["ldap"]["domain_name"] = $this->domain_name;
  175. $result["ldap"]["username"] = $this->username;
  176. if ($this->debug) {
  177. $result["ldap"]["password"] = $this->password;
  178. }
  179. $result["ldap"]["user_uuid"] = $this->user_uuid;
  180. $result["ldap"]["domain_uuid"] = $this->domain_uuid;
  181. $result["ldap"]["authorized"] = $user_authorized ? true : false;
  182. return $result;
  183. }
  184. }
  185. ?>