blob: c3233245619538e1cccb321c152ba13877ee7471 [file] [log] [blame]
matheusfillipeabd513e2021-05-11 03:29:11 -03001<?php
2
Marc Kupietz0215a442023-03-05 18:34:16 +01003include_once "User.php";
4use \User as User;
5
matheusfillipef43dd962021-05-13 23:27:01 -03006function debug($msg)
7{
matheusfillipeabd513e2021-05-11 03:29:11 -03008 include 'config.php';
9 if ($DEBUG)
matheusfillipef43dd962021-05-13 23:27:01 -030010 echo $msg . "\n";
matheusfillipeabd513e2021-05-11 03:29:11 -030011}
matheusfillipef43dd962021-05-13 23:27:01 -030012function generateSalt($length = 10)
13{
14 $chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
matheusfillipeabd513e2021-05-11 03:29:11 -030015
matheusfillipef43dd962021-05-13 23:27:01 -030016 $string = "";
17 for ($i = 0; $i < $length; $i++) {
18 $string .= substr($chars, rand(0, strlen($chars) - 1), 1);
19 }
matheusfillipeabd513e2021-05-11 03:29:11 -030020
matheusfillipef43dd962021-05-13 23:27:01 -030021 return $string;
matheusfillipeabd513e2021-05-11 03:29:11 -030022}
23
matheusfillipeabd513e2021-05-11 03:29:11 -030024
matheusfillipef43dd962021-05-13 23:27:01 -030025function ldap_search_query($query, $filter = "cn")
26{
matheusfillipeabd513e2021-05-11 03:29:11 -030027 include 'config.php';
28 $ldap_host = $HOST;
29 $ldap_port = $PORT;
30 $ldaptree = explode("{},", $BASE_DN)[1];
31
matheusfillipef43dd962021-05-13 23:27:01 -030032 $ldap_user = "cn=" . $USER . "," . join(",", array_slice(explode(",", $ldaptree), 1));
matheusfillipeabd513e2021-05-11 03:29:11 -030033 $ldap_pass = $PASSWORD;
34
35 //First: Connect to LDAP Server
matheusfillipef43dd962021-05-13 23:27:01 -030036 $connect = ldap_connect($ldap_host, $ldap_port)
Marc Kupietz5f066092023-04-20 11:16:25 +020037 or debug(">>Could not connect to LDAP server to add user $ldap_user<<");
matheusfillipeabd513e2021-05-11 03:29:11 -030038 ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3);
39 ldap_set_option($connect, LDAP_OPT_REFERRALS, 0);
40
41 //Login to LDAP
Marc Kupietz5f066092023-04-20 11:16:25 +020042 $bind = ldap_bind($connect, $ldap_user, $ldap_pass);
43 if (!$bind) {
44 debug(">>Could not bind to $ldap_host to add user $ldap_user<<");
45 if (ldap_get_option($connect, LDAP_OPT_DIAGNOSTIC_MESSAGE, $extended_error)) {
46 debug("Error Binding to LDAP: $extended_error");
47 } else {
48 debug("Error Binding to LDAP: No additional information is available.");
49 }
50 }
matheusfillipef43dd962021-05-13 23:27:01 -030051
52 $result = ldap_search($connect, $ldaptree, "(" . $filter . "=" . $query . ")") or die("Error in search query: " . ldap_error($connect));
matheusfillipeabd513e2021-05-11 03:29:11 -030053 $data = ldap_get_entries($connect, $result);
54 return $data;
55}
56
Marc Kupietz0215a442023-03-05 18:34:16 +010057function ldap_add_user(User $user)
matheusfillipeabd513e2021-05-11 03:29:11 -030058{
59 include 'config.php';
60 $ldap_host = $HOST;
61 $ldap_port = $PORT;
Marc Kupietzade9e3c2023-03-06 21:39:23 +010062 $base_dn = str_replace('{}', $user->username, $BASE_DN);
matheusfillipeabd513e2021-05-11 03:29:11 -030063 $ldaptree = explode("{},", $BASE_DN)[1];
64
65
matheusfillipef43dd962021-05-13 23:27:01 -030066 $info["givenName"] = $user->first_name;
67 $info["sn"] = $user->last_name;
Marc Kupietzade9e3c2023-03-06 21:39:23 +010068 $info["uid"] = $user->username;
matheusfillipeabd513e2021-05-11 03:29:11 -030069 #$info["homeDirectory"]="/home/";
matheusfillipef43dd962021-05-13 23:27:01 -030070 $info["mail"] = $user->email;
Marc Kupietza19f3072023-02-25 14:16:40 +010071 $info["o"] = $user->organization;
matheusfillipef43dd962021-05-13 23:27:01 -030072 $info["displayName"] = $user->first_name . " " . $user->last_name;
matheusfillipeabd513e2021-05-11 03:29:11 -030073 #$info["departmentNumber"]=$user->id;
Marc Kupietzade9e3c2023-03-06 21:39:23 +010074 $info["cn"] = $user->username;
matheusfillipef43dd962021-05-13 23:27:01 -030075 $info["userPassword"] = $user->user_hash;
Marc Kupietzade9e3c2023-03-06 21:39:23 +010076 $info["l"] = $user->city;
77 $info["street"] = $user->street;
78 $info["postalcode"] = $user->zip;
79 $info["l"] = $user->city;
80 $info["co"] = $user->country;
Marc Kupietz03146622023-03-07 12:03:21 +010081 $info["telephoneNumber"] = $user->phone;
Marc Kupietzade9e3c2023-03-06 21:39:23 +010082
matheusfillipeabd513e2021-05-11 03:29:11 -030083 $info["objectclass"][0] = "top";
84 $info["objectclass"][1] = "person";
85 $info["objectclass"][2] = "inetOrgPerson";
86 $info["objectclass"][3] = "organizationalPerson";
Marc Kupietz00e13e82023-04-20 11:16:24 +020087 $info["objectclass"][4] = "extensibleObject";
matheusfillipeabd513e2021-05-11 03:29:11 -030088
matheusfillipef43dd962021-05-13 23:27:01 -030089 $ldap_user = "cn=" . $USER . "," . join(",", array_slice(explode(",", $ldaptree), 1));
matheusfillipeabd513e2021-05-11 03:29:11 -030090 $ldap_pass = $PASSWORD;
91
92 //First: Connect to LDAP Server
matheusfillipef43dd962021-05-13 23:27:01 -030093 $connect = ldap_connect($ldap_host, $ldap_port)
94 or debug(">>Could not connect to LDAP server to add user<<");
matheusfillipeabd513e2021-05-11 03:29:11 -030095 ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3);
96 ldap_set_option($connect, LDAP_OPT_REFERRALS, 0);
97
98 //Login to LDAP
99 ldap_bind($connect, $ldap_user, $ldap_pass)
matheusfillipef43dd962021-05-13 23:27:01 -0300100 or debug(">>Could not bind to $ldap_host to add user<<");
matheusfillipeabd513e2021-05-11 03:29:11 -0300101
102 // Adding new user
103
Marc Kupietz5f066092023-04-20 11:16:25 +0200104 $add = ldap_add($connect, $base_dn, $info);
105 if (!$add) {
106 debug(">>Not able to add user $info<<");
107 debug(">>Could not bind to $ldap_host to add user $ldap_user<<");
108 if (ldap_get_option($connect, LDAP_OPT_DIAGNOSTIC_MESSAGE, $extended_error)) {
109 debug("Error Binding to LDAP: $extended_error");
110 } else {
111 debug("Error Binding to LDAP: No additional information is available.");
112 }
113 }
matheusfillipeabd513e2021-05-11 03:29:11 -0300114
115 // Close connection
matheusfillipef43dd962021-05-13 23:27:01 -0300116 ldap_close($connect);
matheusfillipeabd513e2021-05-11 03:29:11 -0300117
matheusfillipef43dd962021-05-13 23:27:01 -0300118 // Return value of operation
matheusfillipeabd513e2021-05-11 03:29:11 -0300119
120 return $add;
121}
Marc Kupietz0215a442023-03-05 18:34:16 +0100122function ldap_user_count(string $user)
matheusfillipef43dd962021-05-13 23:27:01 -0300123{
matheusfillipeabd513e2021-05-11 03:29:11 -0300124 return ldap_search_query($user)["count"];
125}
matheusfillipef43dd962021-05-13 23:27:01 -0300126function ldap_mail_count($email)
127{
matheusfillipeabd513e2021-05-11 03:29:11 -0300128 return ldap_search_query($email, "mail")["count"];
129}
matheusfillipef43dd962021-05-13 23:27:01 -0300130
131function change_password($email, $new_password)
132{
133 include 'config.php';
134 $ldap_host = $HOST;
135 $ldap_port = $PORT;
136 $ldaptree = explode("{},", $BASE_DN)[1];
137
138 $ldap_user = "cn=" . $USER . "," . join(",", array_slice(explode(",", $ldaptree), 1));
139 $ldap_pass = $PASSWORD;
140
141 //First: Connect to LDAP Server
142 $connect = ldap_connect($ldap_host, $ldap_port)
143 or debug(">>Could not connect to LDAP server to add user<<");
144 ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3);
145 ldap_set_option($connect, LDAP_OPT_REFERRALS, 0);
146
147 //Login to LDAP
148 ldap_bind($connect, $ldap_user, $ldap_pass)
149 or debug(">>Could not bind to $ldap_host to add user<<");
150
151
152 $result = ldap_search($connect, $ldaptree, "(mail=" . $email . ")") or die("Error in search query: " . ldap_error($connect));
153 $data = ldap_get_entries($connect, $result);
154 if (!$data['count'] || !isset($data[0]["dn"]) || empty($data[0]["dn"])) {
155 return false;
156 }
157 $dn = $data[0]["dn"];
158
Marc Kupietz92e48662023-02-23 10:04:21 +0100159 if ($ENCRYPT_PASSWORDS) {
Marc Kupietz2f7136a2023-04-24 14:42:05 +0200160 # $newEntry = ['userPassword' => "{crypt}" . crypt($new_password, '$6$' . generateSalt(10) . '$')];
Marc Kupietz421c13a2023-04-26 22:10:48 +0200161 # $newEntry = ['userPassword' => "{SHA}" . base64_encode(sha1($new_password, true))];
162 $salt = generateSalt(10);
163 $newEntry = ['userPassword' => "{SSHA}" . base64_encode( sha1( $new_password . $salt, true) . $salt )];
Marc Kupietz92e48662023-02-23 10:04:21 +0100164 } else {
165 $newEntry = ['userPassword' => "{CLEAR}" . $new_password];
166 }
matheusfillipef43dd962021-05-13 23:27:01 -0300167 if (ldap_mod_replace($connect, $dn, $newEntry))
168 return true;
169 else
170 return false;
171}