Fork of FusionPBX but with LDAP kinda working
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
5.5 KiB

2 years ago
2 years ago
  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. //use ldap to validate the user credentials
  23. if (isset($_SESSION["ldap"]["certpath"])) {
  24. $s = "LDAPTLS_CERT=" . $_SESSION["ldap"]["certpath"]["text"];
  25. putenv($s);
  26. }
  27. if (isset($_SESSION["ldap"]["certkey"])) {
  28. $s = "LDAPTLS_KEY=" . $_SESSION["ldap"]["certkey"]["text"];
  29. putenv($s);
  30. }
  31. $host = $_SESSION["ldap"]["server_host"]["text"];
  32. $port = $_SESSION["ldap"]["server_port"]["numeric"];
  33. $connect = ldap_connect($host, $port)
  34. or die("Could not connect to the LDAP server.");
  35. //ldap_set_option($connect, LDAP_OPT_NETWORK_TIMEOUT, 10);
  36. ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3);
  37. //ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, 7);
  38. //set the default status
  39. $user_authorized = false;
  40. //provide backwards compatability
  41. if (strlen($_SESSION["ldap"]["user_dn"]["text"]) > 0) {
  42. $_SESSION["ldap"]["user_dn"][] = $_SESSION["ldap"]["user_dn"]["text"];
  43. }
  44. //check all user_dn in the array
  45. foreach ($_SESSION["ldap"]["user_dn"] as $user_dn) {
  46. $bind_dn = $_SESSION["ldap"]["user_attribute"]["text"]."=".$this->username.",".$user_dn;
  47. $bind_pw = $this->password;
  48. //Note: As of 4/16, the call below will fail randomly. PHP debug reports ldap_bind
  49. //called below with all arguments '*uninitialized*'. However, the debugger
  50. //single-stepping just before the failing call correctly displays all the values.
  51. if (strlen($bind_pw) > 0) {
  52. $bind = ldap_bind($connect, $bind_dn, $bind_pw);
  53. if ($bind) {
  54. //connected and authorized
  55. $user_authorized = true;
  56. break;
  57. }
  58. }
  59. }
  60. //check to see if the user exists
  61. if ($user_authorized) {
  62. $sql = "select * from v_users ";
  63. $sql .= "where username = :username ";
  64. if ($_SESSION["users"]["unique"]["text"] != "global") {
  65. //unique username per domain (not globally unique across system - example: email address)
  66. $sql .= "and domain_uuid = :domain_uuid ";
  67. $parameters['domain_uuid'] = $this->domain_uuid;
  68. }
  69. $parameters['username'] = $this->username;
  70. $database = new database;
  71. $row = $database->select($sql, $parameters, 'row');
  72. if (is_array($row) && @sizeof($row) != 0) {
  73. if ($_SESSION["users"]["unique"]["text"] == "global" && $row["domain_uuid"] != $this->domain_uuid) {
  74. //get the domain uuid
  75. $this->domain_uuid = $row["domain_uuid"];
  76. $this->domain_name = $_SESSION['domains'][$this->domain_uuid]['domain_name'];
  77. //set the domain session variables
  78. $_SESSION["domain_uuid"] = $this->domain_uuid;
  79. $_SESSION["domain_name"] = $this->domain_name;
  80. //set the setting arrays
  81. $domain = new domains();
  82. $domain->set();
  83. }
  84. $this->user_uuid = $row["user_uuid"];
  85. $this->contact_uuid = $row["contact_uuid"];
  86. }
  87. else {
  88. //salt used with the password to create a one way hash
  89. $salt = generate_password('32', '4');
  90. $password = generate_password('32', '4');
  91. //prepare the uuids
  92. $this->user_uuid = uuid();
  93. $this->contact_uuid = uuid();
  94. //build user insert array
  95. $array['users'][0]['user_uuid'] = $this->user_uuid;
  96. $array['users'][0]['domain_uuid'] = $this->domain_uuid;
  97. $array['users'][0]['contact_uuid'] = $this->contact_uuid;
  98. $array['users'][0]['username'] = strtolower($this->username);
  99. $array['users'][0]['password'] = md5($salt.$password);
  100. $array['users'][0]['salt'] = $salt;
  101. $array['users'][0]['add_date'] = now();
  102. $array['users'][0]['add_user'] = strtolower($this->username);
  103. $array['users'][0]['user_enabled'] = 'true';
  104. //build user group insert array
  105. $array['user_groups'][0]['user_group_uuid'] = uuid();
  106. $array['user_groups'][0]['group_uuid'] = (!is_null($_SESSION["ldap"]["assign_group_uuid"]) && strlen($_SESSION["ldap"]["assign_group_uuid"]) > 0) ? $_SESSION["ldap"]["assign_group_uuid"] : uuid();
  107. //To set default group add default setting : Categ ldap Subcateg: assign_group_uuid Type: uuid Value : group UUID
  108. $array['user_groups'][0]['domain_uuid'] = $this->domain_uuid;
  109. $array['user_groups'][0]['group_name'] = 'user';
  110. $array['user_groups'][0]['user_uuid'] = $this->user_uuid;
  111. //grant temporary permissions
  112. $p = new permissions;
  113. $p->add('user_add', 'temp');
  114. $p->add('user_group_add', 'temp');
  115. //execute insert
  116. $database = new database;
  117. $database->app_name = 'authentication';
  118. $database->app_uuid = 'a8a12918-69a4-4ece-a1ae-3932be0e41f1';
  119. $database->save($array);
  120. unset($array);
  121. //revoke temporary permissions
  122. $p->delete('user_add', 'temp');
  123. $p->delete('user_group_add', 'temp');
  124. }
  125. unset($sql, $parameters, $row);
  126. }
  127. //result array
  128. $result["plugin"] = "ldap";
  129. $result["domain_name"] = $this->domain_name;
  130. $result["username"] = $this->username;
  131. if ($this->debug) {
  132. $result["password"] = $this->password;
  133. }
  134. $result["user_uuid"] = $this->user_uuid;
  135. $result["domain_uuid"] = $this->domain_uuid;
  136. $result["authorized"] = $user_authorized ? 'true' : 'false';
  137. return $result;
  138. }
  139. }
  140. ?>