ldap.php 8.1 KB

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