| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 1 | <?php | 
 | 2 |  | 
| Marc Kupietz | 0215a44 | 2023-03-05 18:34:16 +0100 | [diff] [blame] | 3 | include_once "User.php"; | 
 | 4 | use \User as User; | 
 | 5 |  | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 6 | function debug($msg) | 
 | 7 | { | 
| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 8 |         include 'config.php'; | 
 | 9 |         if ($DEBUG) | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 10 |                 echo $msg . "\n"; | 
| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 11 | } | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 12 | function generateSalt($length = 10) | 
 | 13 | { | 
 | 14 |         $chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; | 
| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 15 |  | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 16 |         $string = ""; | 
 | 17 |         for ($i = 0; $i < $length; $i++) { | 
 | 18 |                 $string .= substr($chars, rand(0, strlen($chars) - 1), 1); | 
 | 19 |         } | 
| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 20 |  | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 21 |         return $string; | 
| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 22 | } | 
 | 23 |  | 
| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 24 |  | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 25 | function ldap_search_query($query, $filter = "cn") | 
 | 26 | { | 
| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 27 |         include 'config.php'; | 
 | 28 |         $ldap_host = $HOST; | 
 | 29 |         $ldap_port = $PORT; | 
 | 30 |         $ldaptree = explode("{},", $BASE_DN)[1]; | 
 | 31 |  | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 32 |         $ldap_user = "cn=" . $USER . "," . join(",", array_slice(explode(",", $ldaptree), 1)); | 
| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 33 |         $ldap_pass = $PASSWORD; | 
 | 34 |  | 
 | 35 |         //First: Connect to  LDAP Server | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 36 |         $connect = ldap_connect($ldap_host, $ldap_port) | 
 | 37 |                 or debug(">>Could not connect to LDAP server to add user<<"); | 
| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 38 |         ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3); | 
 | 39 |         ldap_set_option($connect, LDAP_OPT_REFERRALS, 0); | 
 | 40 |  | 
 | 41 |         //Login to LDAP | 
 | 42 |         ldap_bind($connect, $ldap_user, $ldap_pass) | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 43 |                 or debug(">>Could not bind to $ldap_host to add user<<"); | 
| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 44 |  | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 45 |  | 
 | 46 |         $result = ldap_search($connect, $ldaptree, "(" . $filter . "=" . $query . ")") or die("Error in search query: " . ldap_error($connect)); | 
| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 47 |         $data = ldap_get_entries($connect, $result); | 
 | 48 |         return $data; | 
 | 49 | } | 
 | 50 |  | 
| Marc Kupietz | 0215a44 | 2023-03-05 18:34:16 +0100 | [diff] [blame] | 51 | function ldap_add_user(User $user) | 
| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 52 | { | 
 | 53 |         include 'config.php'; | 
 | 54 |         $ldap_host = $HOST; | 
 | 55 |         $ldap_port = $PORT; | 
 | 56 |         $base_dn = str_replace('{}', $user->user_name, $BASE_DN); | 
 | 57 |         $ldaptree = explode("{},", $BASE_DN)[1]; | 
 | 58 |  | 
 | 59 |  | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 60 |         $info["givenName"] = $user->first_name; | 
 | 61 |         $info["sn"] = $user->last_name; | 
 | 62 |         $info["uid"] = $user->user_name; | 
| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 63 |         #$info["homeDirectory"]="/home/"; | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 64 |         $info["mail"] = $user->email; | 
| Marc Kupietz | a19f307 | 2023-02-25 14:16:40 +0100 | [diff] [blame] | 65 |         $info["o"] = $user->organization; | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 66 |         $info["displayName"] = $user->first_name . " " . $user->last_name; | 
| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 67 |         #$info["departmentNumber"]=$user->id; | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 68 |         $info["cn"] = $user->user_name; | 
 | 69 |         $info["userPassword"] = $user->user_hash; | 
| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 70 |         $info["objectclass"][0] = "top"; | 
 | 71 |         $info["objectclass"][1] = "person"; | 
 | 72 |         $info["objectclass"][2] = "inetOrgPerson"; | 
 | 73 |         $info["objectclass"][3] = "organizationalPerson"; | 
 | 74 |  | 
 | 75 |  | 
 | 76 |  | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 77 |         $ldap_user = "cn=" . $USER . "," . join(",", array_slice(explode(",", $ldaptree), 1)); | 
| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 78 |         $ldap_pass = $PASSWORD; | 
 | 79 |  | 
 | 80 |         //First: Connect to  LDAP Server | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 81 |         $connect = ldap_connect($ldap_host, $ldap_port) | 
 | 82 |                 or debug(">>Could not connect to LDAP server to add user<<"); | 
| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 83 |         ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3); | 
 | 84 |         ldap_set_option($connect, LDAP_OPT_REFERRALS, 0); | 
 | 85 |  | 
 | 86 |         //Login to LDAP | 
 | 87 |         ldap_bind($connect, $ldap_user, $ldap_pass) | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 88 |                 or debug(">>Could not bind to $ldap_host to add user<<"); | 
| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 89 |  | 
 | 90 |         // Adding new user | 
 | 91 |  | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 92 |         $add = ldap_add($connect, $base_dn, $info) | 
 | 93 |                 or debug(">>Not able to load user <<"); | 
| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 94 |  | 
 | 95 |         // Close connection | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 96 |         ldap_close($connect); | 
| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 97 |  | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 98 |         // Return value of operation | 
| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 99 |  | 
 | 100 |         return $add; | 
 | 101 | } | 
| Marc Kupietz | 0215a44 | 2023-03-05 18:34:16 +0100 | [diff] [blame] | 102 | function ldap_user_count(string $user) | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 103 | { | 
| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 104 |         return ldap_search_query($user)["count"]; | 
 | 105 | } | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 106 | function ldap_mail_count($email) | 
 | 107 | { | 
| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 108 |         return ldap_search_query($email, "mail")["count"]; | 
 | 109 | } | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 110 |  | 
 | 111 | function change_password($email, $new_password) | 
 | 112 | { | 
 | 113 |         include 'config.php'; | 
 | 114 |         $ldap_host = $HOST; | 
 | 115 |         $ldap_port = $PORT; | 
 | 116 |         $ldaptree = explode("{},", $BASE_DN)[1]; | 
 | 117 |  | 
 | 118 |         $ldap_user = "cn=" . $USER . "," . join(",", array_slice(explode(",", $ldaptree), 1)); | 
 | 119 |         $ldap_pass = $PASSWORD; | 
 | 120 |  | 
 | 121 |         //First: Connect to  LDAP Server | 
 | 122 |         $connect = ldap_connect($ldap_host, $ldap_port) | 
 | 123 |                 or debug(">>Could not connect to LDAP server to add user<<"); | 
 | 124 |         ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3); | 
 | 125 |         ldap_set_option($connect, LDAP_OPT_REFERRALS, 0); | 
 | 126 |  | 
 | 127 |         //Login to LDAP | 
 | 128 |         ldap_bind($connect, $ldap_user, $ldap_pass) | 
 | 129 |                 or debug(">>Could not bind to $ldap_host to add user<<"); | 
 | 130 |  | 
 | 131 |  | 
 | 132 |         $result = ldap_search($connect, $ldaptree, "(mail=" . $email . ")") or die("Error in search query: " . ldap_error($connect)); | 
 | 133 |         $data = ldap_get_entries($connect, $result); | 
 | 134 |         if (!$data['count'] || !isset($data[0]["dn"]) || empty($data[0]["dn"])) { | 
 | 135 |                 return false; | 
 | 136 |         } | 
 | 137 |         $dn = $data[0]["dn"]; | 
 | 138 |  | 
| Marc Kupietz | 92e4866 | 2023-02-23 10:04:21 +0100 | [diff] [blame] | 139 |         if ($ENCRYPT_PASSWORDS) { | 
 | 140 |                 $newEntry = ['userPassword' => "{crypt}" . crypt($new_password, '$6$' . generateSalt(10) . '$')]; | 
 | 141 |                 # $newEntry = ['userPassword' => "{SHA}" .  base64_encode(sha1($new_password, true))]; | 
 | 142 |         } else { | 
 | 143 |                 $newEntry = ['userPassword' => "{CLEAR}" .  $new_password]; | 
 | 144 |         } | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 145 |         if (ldap_mod_replace($connect, $dn, $newEntry)) | 
 | 146 |                 return true; | 
 | 147 |         else | 
 | 148 |                 return false; | 
 | 149 | } |