blob: 62323646f0c4c930f7bece7e2e6da313cb6dd36c [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)
37 or debug(">>Could not connect to LDAP server to add 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
42 ldap_bind($connect, $ldap_user, $ldap_pass)
matheusfillipef43dd962021-05-13 23:27:01 -030043 or debug(">>Could not bind to $ldap_host to add user<<");
matheusfillipeabd513e2021-05-11 03:29:11 -030044
matheusfillipef43dd962021-05-13 23:27:01 -030045
46 $result = ldap_search($connect, $ldaptree, "(" . $filter . "=" . $query . ")") or die("Error in search query: " . ldap_error($connect));
matheusfillipeabd513e2021-05-11 03:29:11 -030047 $data = ldap_get_entries($connect, $result);
48 return $data;
49}
50
Marc Kupietz0215a442023-03-05 18:34:16 +010051function ldap_add_user(User $user)
matheusfillipeabd513e2021-05-11 03:29:11 -030052{
53 include 'config.php';
54 $ldap_host = $HOST;
55 $ldap_port = $PORT;
Marc Kupietzade9e3c2023-03-06 21:39:23 +010056 $base_dn = str_replace('{}', $user->username, $BASE_DN);
matheusfillipeabd513e2021-05-11 03:29:11 -030057 $ldaptree = explode("{},", $BASE_DN)[1];
58
59
matheusfillipef43dd962021-05-13 23:27:01 -030060 $info["givenName"] = $user->first_name;
61 $info["sn"] = $user->last_name;
Marc Kupietzade9e3c2023-03-06 21:39:23 +010062 $info["uid"] = $user->username;
matheusfillipeabd513e2021-05-11 03:29:11 -030063 #$info["homeDirectory"]="/home/";
matheusfillipef43dd962021-05-13 23:27:01 -030064 $info["mail"] = $user->email;
Marc Kupietza19f3072023-02-25 14:16:40 +010065 $info["o"] = $user->organization;
matheusfillipef43dd962021-05-13 23:27:01 -030066 $info["displayName"] = $user->first_name . " " . $user->last_name;
matheusfillipeabd513e2021-05-11 03:29:11 -030067 #$info["departmentNumber"]=$user->id;
Marc Kupietzade9e3c2023-03-06 21:39:23 +010068 $info["cn"] = $user->username;
matheusfillipef43dd962021-05-13 23:27:01 -030069 $info["userPassword"] = $user->user_hash;
Marc Kupietzade9e3c2023-03-06 21:39:23 +010070 $info["l"] = $user->city;
71 $info["street"] = $user->street;
72 $info["postalcode"] = $user->zip;
73 $info["l"] = $user->city;
74 $info["co"] = $user->country;
Marc Kupietz03146622023-03-07 12:03:21 +010075 $info["telephoneNumber"] = $user->phone;
Marc Kupietzade9e3c2023-03-06 21:39:23 +010076
matheusfillipeabd513e2021-05-11 03:29:11 -030077 $info["objectclass"][0] = "top";
78 $info["objectclass"][1] = "person";
79 $info["objectclass"][2] = "inetOrgPerson";
80 $info["objectclass"][3] = "organizationalPerson";
Marc Kupietz00e13e82023-04-20 11:16:24 +020081 $info["objectclass"][4] = "extensibleObject";
matheusfillipeabd513e2021-05-11 03:29:11 -030082
matheusfillipef43dd962021-05-13 23:27:01 -030083 $ldap_user = "cn=" . $USER . "," . join(",", array_slice(explode(",", $ldaptree), 1));
matheusfillipeabd513e2021-05-11 03:29:11 -030084 $ldap_pass = $PASSWORD;
85
86 //First: Connect to LDAP Server
matheusfillipef43dd962021-05-13 23:27:01 -030087 $connect = ldap_connect($ldap_host, $ldap_port)
88 or debug(">>Could not connect to LDAP server to add user<<");
matheusfillipeabd513e2021-05-11 03:29:11 -030089 ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3);
90 ldap_set_option($connect, LDAP_OPT_REFERRALS, 0);
91
92 //Login to LDAP
93 ldap_bind($connect, $ldap_user, $ldap_pass)
matheusfillipef43dd962021-05-13 23:27:01 -030094 or debug(">>Could not bind to $ldap_host to add user<<");
matheusfillipeabd513e2021-05-11 03:29:11 -030095
96 // Adding new user
97
matheusfillipef43dd962021-05-13 23:27:01 -030098 $add = ldap_add($connect, $base_dn, $info)
99 or debug(">>Not able to load user <<");
matheusfillipeabd513e2021-05-11 03:29:11 -0300100
101 // Close connection
matheusfillipef43dd962021-05-13 23:27:01 -0300102 ldap_close($connect);
matheusfillipeabd513e2021-05-11 03:29:11 -0300103
matheusfillipef43dd962021-05-13 23:27:01 -0300104 // Return value of operation
matheusfillipeabd513e2021-05-11 03:29:11 -0300105
106 return $add;
107}
Marc Kupietz0215a442023-03-05 18:34:16 +0100108function ldap_user_count(string $user)
matheusfillipef43dd962021-05-13 23:27:01 -0300109{
matheusfillipeabd513e2021-05-11 03:29:11 -0300110 return ldap_search_query($user)["count"];
111}
matheusfillipef43dd962021-05-13 23:27:01 -0300112function ldap_mail_count($email)
113{
matheusfillipeabd513e2021-05-11 03:29:11 -0300114 return ldap_search_query($email, "mail")["count"];
115}
matheusfillipef43dd962021-05-13 23:27:01 -0300116
117function change_password($email, $new_password)
118{
119 include 'config.php';
120 $ldap_host = $HOST;
121 $ldap_port = $PORT;
122 $ldaptree = explode("{},", $BASE_DN)[1];
123
124 $ldap_user = "cn=" . $USER . "," . join(",", array_slice(explode(",", $ldaptree), 1));
125 $ldap_pass = $PASSWORD;
126
127 //First: Connect to LDAP Server
128 $connect = ldap_connect($ldap_host, $ldap_port)
129 or debug(">>Could not connect to LDAP server to add user<<");
130 ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3);
131 ldap_set_option($connect, LDAP_OPT_REFERRALS, 0);
132
133 //Login to LDAP
134 ldap_bind($connect, $ldap_user, $ldap_pass)
135 or debug(">>Could not bind to $ldap_host to add user<<");
136
137
138 $result = ldap_search($connect, $ldaptree, "(mail=" . $email . ")") or die("Error in search query: " . ldap_error($connect));
139 $data = ldap_get_entries($connect, $result);
140 if (!$data['count'] || !isset($data[0]["dn"]) || empty($data[0]["dn"])) {
141 return false;
142 }
143 $dn = $data[0]["dn"];
144
Marc Kupietz92e48662023-02-23 10:04:21 +0100145 if ($ENCRYPT_PASSWORDS) {
146 $newEntry = ['userPassword' => "{crypt}" . crypt($new_password, '$6$' . generateSalt(10) . '$')];
147 # $newEntry = ['userPassword' => "{SHA}" . base64_encode(sha1($new_password, true))];
148 } else {
149 $newEntry = ['userPassword' => "{CLEAR}" . $new_password];
150 }
matheusfillipef43dd962021-05-13 23:27:01 -0300151 if (ldap_mod_replace($connect, $dn, $newEntry))
152 return true;
153 else
154 return false;
155}