blob: d73349f9d707cc450213973084f905504eb30a6c [file] [log] [blame]
matheusfillipeabd513e2021-05-11 03:29:11 -03001<?php
2
matheusfillipef43dd962021-05-13 23:27:01 -03003function debug($msg)
4{
matheusfillipeabd513e2021-05-11 03:29:11 -03005 include 'config.php';
6 if ($DEBUG)
matheusfillipef43dd962021-05-13 23:27:01 -03007 echo $msg . "\n";
matheusfillipeabd513e2021-05-11 03:29:11 -03008}
matheusfillipef43dd962021-05-13 23:27:01 -03009function generateSalt($length = 10)
10{
11 $chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
matheusfillipeabd513e2021-05-11 03:29:11 -030012
matheusfillipef43dd962021-05-13 23:27:01 -030013 $string = "";
14 for ($i = 0; $i < $length; $i++) {
15 $string .= substr($chars, rand(0, strlen($chars) - 1), 1);
16 }
matheusfillipeabd513e2021-05-11 03:29:11 -030017
matheusfillipef43dd962021-05-13 23:27:01 -030018 return $string;
matheusfillipeabd513e2021-05-11 03:29:11 -030019}
20
matheusfillipef43dd962021-05-13 23:27:01 -030021class User
22{
Marc Kupietza19f3072023-02-25 14:16:40 +010023 function __construct(string $user_name, string $first_name, string $last_name, string $email, string $password, string $organization)
matheusfillipef43dd962021-05-13 23:27:01 -030024 {
matheusfillipeabd513e2021-05-11 03:29:11 -030025 $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 Kupietza19f3072023-02-25 14:16:40 +010030 $this->organization = $organization;
Marc Kupietz92e48662023-02-23 10:04:21 +010031 if ($ENCRYPT_PASSWORDS) {
32 $this->user_hash = "{crypt}" . crypt($password, '$6$' . generateSalt(10) . '$');
33 # $this->user_hash = "{SHA}" . base64_encode(sha1($password, true));
34 } else {
35 $this->user_hash = "{CLEAR}" . $password;
36 }
matheusfillipeabd513e2021-05-11 03:29:11 -030037 $this->password = $this->user_hash;
Marc Kupietz92e48662023-02-23 10:04:21 +010038
matheusfillipeabd513e2021-05-11 03:29:11 -030039 }
40}
41
matheusfillipef43dd962021-05-13 23:27:01 -030042function ldap_search_query($query, $filter = "cn")
43{
matheusfillipeabd513e2021-05-11 03:29:11 -030044 include 'config.php';
45 $ldap_host = $HOST;
46 $ldap_port = $PORT;
47 $ldaptree = explode("{},", $BASE_DN)[1];
48
matheusfillipef43dd962021-05-13 23:27:01 -030049 $ldap_user = "cn=" . $USER . "," . join(",", array_slice(explode(",", $ldaptree), 1));
matheusfillipeabd513e2021-05-11 03:29:11 -030050 $ldap_pass = $PASSWORD;
51
52 //First: Connect to LDAP Server
matheusfillipef43dd962021-05-13 23:27:01 -030053 $connect = ldap_connect($ldap_host, $ldap_port)
54 or debug(">>Could not connect to LDAP server to add user<<");
matheusfillipeabd513e2021-05-11 03:29:11 -030055 ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3);
56 ldap_set_option($connect, LDAP_OPT_REFERRALS, 0);
57
58 //Login to LDAP
59 ldap_bind($connect, $ldap_user, $ldap_pass)
matheusfillipef43dd962021-05-13 23:27:01 -030060 or debug(">>Could not bind to $ldap_host to add user<<");
matheusfillipeabd513e2021-05-11 03:29:11 -030061
matheusfillipef43dd962021-05-13 23:27:01 -030062
63 $result = ldap_search($connect, $ldaptree, "(" . $filter . "=" . $query . ")") or die("Error in search query: " . ldap_error($connect));
matheusfillipeabd513e2021-05-11 03:29:11 -030064 $data = ldap_get_entries($connect, $result);
65 return $data;
66}
67
matheusfillipef43dd962021-05-13 23:27:01 -030068function ldap_add_user($user)
matheusfillipeabd513e2021-05-11 03:29:11 -030069{
70 include 'config.php';
71 $ldap_host = $HOST;
72 $ldap_port = $PORT;
73 $base_dn = str_replace('{}', $user->user_name, $BASE_DN);
74 $ldaptree = explode("{},", $BASE_DN)[1];
75
76
matheusfillipef43dd962021-05-13 23:27:01 -030077 $info["givenName"] = $user->first_name;
78 $info["sn"] = $user->last_name;
79 $info["uid"] = $user->user_name;
matheusfillipeabd513e2021-05-11 03:29:11 -030080 #$info["homeDirectory"]="/home/";
matheusfillipef43dd962021-05-13 23:27:01 -030081 $info["mail"] = $user->email;
Marc Kupietza19f3072023-02-25 14:16:40 +010082 $info["o"] = $user->organization;
matheusfillipef43dd962021-05-13 23:27:01 -030083 $info["displayName"] = $user->first_name . " " . $user->last_name;
matheusfillipeabd513e2021-05-11 03:29:11 -030084 #$info["departmentNumber"]=$user->id;
matheusfillipef43dd962021-05-13 23:27:01 -030085 $info["cn"] = $user->user_name;
86 $info["userPassword"] = $user->user_hash;
matheusfillipeabd513e2021-05-11 03:29:11 -030087 $info["objectclass"][0] = "top";
88 $info["objectclass"][1] = "person";
89 $info["objectclass"][2] = "inetOrgPerson";
90 $info["objectclass"][3] = "organizationalPerson";
91
92
93
matheusfillipef43dd962021-05-13 23:27:01 -030094 $ldap_user = "cn=" . $USER . "," . join(",", array_slice(explode(",", $ldaptree), 1));
matheusfillipeabd513e2021-05-11 03:29:11 -030095 $ldap_pass = $PASSWORD;
96
97 //First: Connect to LDAP Server
matheusfillipef43dd962021-05-13 23:27:01 -030098 $connect = ldap_connect($ldap_host, $ldap_port)
99 or debug(">>Could not connect to LDAP server to add user<<");
matheusfillipeabd513e2021-05-11 03:29:11 -0300100 ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3);
101 ldap_set_option($connect, LDAP_OPT_REFERRALS, 0);
102
103 //Login to LDAP
104 ldap_bind($connect, $ldap_user, $ldap_pass)
matheusfillipef43dd962021-05-13 23:27:01 -0300105 or debug(">>Could not bind to $ldap_host to add user<<");
matheusfillipeabd513e2021-05-11 03:29:11 -0300106
107 // Adding new user
108
matheusfillipef43dd962021-05-13 23:27:01 -0300109 $add = ldap_add($connect, $base_dn, $info)
110 or debug(">>Not able to load user <<");
matheusfillipeabd513e2021-05-11 03:29:11 -0300111
112 // Close connection
matheusfillipef43dd962021-05-13 23:27:01 -0300113 ldap_close($connect);
matheusfillipeabd513e2021-05-11 03:29:11 -0300114
matheusfillipef43dd962021-05-13 23:27:01 -0300115 // Return value of operation
matheusfillipeabd513e2021-05-11 03:29:11 -0300116
117 return $add;
118}
matheusfillipef43dd962021-05-13 23:27:01 -0300119function ldap_user_count($user)
120{
matheusfillipeabd513e2021-05-11 03:29:11 -0300121 return ldap_search_query($user)["count"];
122}
matheusfillipef43dd962021-05-13 23:27:01 -0300123function ldap_mail_count($email)
124{
matheusfillipeabd513e2021-05-11 03:29:11 -0300125 return ldap_search_query($email, "mail")["count"];
126}
matheusfillipef43dd962021-05-13 23:27:01 -0300127
128function change_password($email, $new_password)
129{
130 include 'config.php';
131 $ldap_host = $HOST;
132 $ldap_port = $PORT;
133 $ldaptree = explode("{},", $BASE_DN)[1];
134
135 $ldap_user = "cn=" . $USER . "," . join(",", array_slice(explode(",", $ldaptree), 1));
136 $ldap_pass = $PASSWORD;
137
138 //First: Connect to LDAP Server
139 $connect = ldap_connect($ldap_host, $ldap_port)
140 or debug(">>Could not connect to LDAP server to add user<<");
141 ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3);
142 ldap_set_option($connect, LDAP_OPT_REFERRALS, 0);
143
144 //Login to LDAP
145 ldap_bind($connect, $ldap_user, $ldap_pass)
146 or debug(">>Could not bind to $ldap_host to add user<<");
147
148
149 $result = ldap_search($connect, $ldaptree, "(mail=" . $email . ")") or die("Error in search query: " . ldap_error($connect));
150 $data = ldap_get_entries($connect, $result);
151 if (!$data['count'] || !isset($data[0]["dn"]) || empty($data[0]["dn"])) {
152 return false;
153 }
154 $dn = $data[0]["dn"];
155
Marc Kupietz92e48662023-02-23 10:04:21 +0100156 if ($ENCRYPT_PASSWORDS) {
157 $newEntry = ['userPassword' => "{crypt}" . crypt($new_password, '$6$' . generateSalt(10) . '$')];
158 # $newEntry = ['userPassword' => "{SHA}" . base64_encode(sha1($new_password, true))];
159 } else {
160 $newEntry = ['userPassword' => "{CLEAR}" . $new_password];
161 }
matheusfillipef43dd962021-05-13 23:27:01 -0300162 if (ldap_mod_replace($connect, $dn, $newEntry))
163 return true;
164 else
165 return false;
166}