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