blob: e24fe8ccaa1f565d69ac33eb834f5baac00ed029 [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{
23 function __construct(string $user_name, string $first_name, string $last_name, string $email, string $password)
24 {
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 Kupietz92e48662023-02-23 10:04:21 +010030 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 }
matheusfillipeabd513e2021-05-11 03:29:11 -030036 $this->password = $this->user_hash;
Marc Kupietz92e48662023-02-23 10:04:21 +010037
matheusfillipeabd513e2021-05-11 03:29:11 -030038 }
39}
40
matheusfillipef43dd962021-05-13 23:27:01 -030041function ldap_search_query($query, $filter = "cn")
42{
matheusfillipeabd513e2021-05-11 03:29:11 -030043 include 'config.php';
44 $ldap_host = $HOST;
45 $ldap_port = $PORT;
46 $ldaptree = explode("{},", $BASE_DN)[1];
47
matheusfillipef43dd962021-05-13 23:27:01 -030048 $ldap_user = "cn=" . $USER . "," . join(",", array_slice(explode(",", $ldaptree), 1));
matheusfillipeabd513e2021-05-11 03:29:11 -030049 $ldap_pass = $PASSWORD;
50
51 //First: Connect to LDAP Server
matheusfillipef43dd962021-05-13 23:27:01 -030052 $connect = ldap_connect($ldap_host, $ldap_port)
53 or debug(">>Could not connect to LDAP server to add user<<");
matheusfillipeabd513e2021-05-11 03:29:11 -030054 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)
matheusfillipef43dd962021-05-13 23:27:01 -030059 or debug(">>Could not bind to $ldap_host to add user<<");
matheusfillipeabd513e2021-05-11 03:29:11 -030060
matheusfillipef43dd962021-05-13 23:27:01 -030061
62 $result = ldap_search($connect, $ldaptree, "(" . $filter . "=" . $query . ")") or die("Error in search query: " . ldap_error($connect));
matheusfillipeabd513e2021-05-11 03:29:11 -030063 $data = ldap_get_entries($connect, $result);
64 return $data;
65}
66
matheusfillipef43dd962021-05-13 23:27:01 -030067function ldap_add_user($user)
matheusfillipeabd513e2021-05-11 03:29:11 -030068{
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
matheusfillipef43dd962021-05-13 23:27:01 -030076 $info["givenName"] = $user->first_name;
77 $info["sn"] = $user->last_name;
78 $info["uid"] = $user->user_name;
matheusfillipeabd513e2021-05-11 03:29:11 -030079 #$info["homeDirectory"]="/home/";
matheusfillipef43dd962021-05-13 23:27:01 -030080 $info["mail"] = $user->email;
81 $info["displayName"] = $user->first_name . " " . $user->last_name;
matheusfillipeabd513e2021-05-11 03:29:11 -030082 #$info["departmentNumber"]=$user->id;
matheusfillipef43dd962021-05-13 23:27:01 -030083 $info["cn"] = $user->user_name;
84 $info["userPassword"] = $user->user_hash;
matheusfillipeabd513e2021-05-11 03:29:11 -030085 $info["objectclass"][0] = "top";
86 $info["objectclass"][1] = "person";
87 $info["objectclass"][2] = "inetOrgPerson";
88 $info["objectclass"][3] = "organizationalPerson";
89
90
91
matheusfillipef43dd962021-05-13 23:27:01 -030092 $ldap_user = "cn=" . $USER . "," . join(",", array_slice(explode(",", $ldaptree), 1));
matheusfillipeabd513e2021-05-11 03:29:11 -030093 $ldap_pass = $PASSWORD;
94
95 //First: Connect to LDAP Server
matheusfillipef43dd962021-05-13 23:27:01 -030096 $connect = ldap_connect($ldap_host, $ldap_port)
97 or debug(">>Could not connect to LDAP server to add user<<");
matheusfillipeabd513e2021-05-11 03:29:11 -030098 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)
matheusfillipef43dd962021-05-13 23:27:01 -0300103 or debug(">>Could not bind to $ldap_host to add user<<");
matheusfillipeabd513e2021-05-11 03:29:11 -0300104
105 // Adding new user
106
matheusfillipef43dd962021-05-13 23:27:01 -0300107 $add = ldap_add($connect, $base_dn, $info)
108 or debug(">>Not able to load user <<");
matheusfillipeabd513e2021-05-11 03:29:11 -0300109
110 // Close connection
matheusfillipef43dd962021-05-13 23:27:01 -0300111 ldap_close($connect);
matheusfillipeabd513e2021-05-11 03:29:11 -0300112
matheusfillipef43dd962021-05-13 23:27:01 -0300113 // Return value of operation
matheusfillipeabd513e2021-05-11 03:29:11 -0300114
115 return $add;
116}
matheusfillipef43dd962021-05-13 23:27:01 -0300117function ldap_user_count($user)
118{
matheusfillipeabd513e2021-05-11 03:29:11 -0300119 return ldap_search_query($user)["count"];
120}
matheusfillipef43dd962021-05-13 23:27:01 -0300121function ldap_mail_count($email)
122{
matheusfillipeabd513e2021-05-11 03:29:11 -0300123 return ldap_search_query($email, "mail")["count"];
124}
matheusfillipef43dd962021-05-13 23:27:01 -0300125
126function 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 Kupietz92e48662023-02-23 10:04:21 +0100154 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 }
matheusfillipef43dd962021-05-13 23:27:01 -0300160 if (ldap_mod_replace($connect, $dn, $newEntry))
161 return true;
162 else
163 return false;
164}