blob: 1633eb931cdbf728f9e39babb9db80455313c4f1 [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;
matheusfillipef43dd962021-05-13 23:27:01 -030030 $this->user_hash = "{crypt}" . crypt($password, '$6$' . generateSalt(10) . '$');
matheusfillipeabd513e2021-05-11 03:29:11 -030031 $this->password = $this->user_hash;
32 }
33}
34
matheusfillipef43dd962021-05-13 23:27:01 -030035function ldap_search_query($query, $filter = "cn")
36{
matheusfillipeabd513e2021-05-11 03:29:11 -030037 include 'config.php';
38 $ldap_host = $HOST;
39 $ldap_port = $PORT;
40 $ldaptree = explode("{},", $BASE_DN)[1];
41
matheusfillipef43dd962021-05-13 23:27:01 -030042 $ldap_user = "cn=" . $USER . "," . join(",", array_slice(explode(",", $ldaptree), 1));
matheusfillipeabd513e2021-05-11 03:29:11 -030043 $ldap_pass = $PASSWORD;
44
45 //First: Connect to LDAP Server
matheusfillipef43dd962021-05-13 23:27:01 -030046 $connect = ldap_connect($ldap_host, $ldap_port)
47 or debug(">>Could not connect to LDAP server to add user<<");
matheusfillipeabd513e2021-05-11 03:29:11 -030048 ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3);
49 ldap_set_option($connect, LDAP_OPT_REFERRALS, 0);
50
51 //Login to LDAP
52 ldap_bind($connect, $ldap_user, $ldap_pass)
matheusfillipef43dd962021-05-13 23:27:01 -030053 or debug(">>Could not bind to $ldap_host to add user<<");
matheusfillipeabd513e2021-05-11 03:29:11 -030054
matheusfillipef43dd962021-05-13 23:27:01 -030055
56 $result = ldap_search($connect, $ldaptree, "(" . $filter . "=" . $query . ")") or die("Error in search query: " . ldap_error($connect));
matheusfillipeabd513e2021-05-11 03:29:11 -030057 $data = ldap_get_entries($connect, $result);
58 return $data;
59}
60
matheusfillipef43dd962021-05-13 23:27:01 -030061function ldap_add_user($user)
matheusfillipeabd513e2021-05-11 03:29:11 -030062{
63 include 'config.php';
64 $ldap_host = $HOST;
65 $ldap_port = $PORT;
66 $base_dn = str_replace('{}', $user->user_name, $BASE_DN);
67 $ldaptree = explode("{},", $BASE_DN)[1];
68
69
matheusfillipef43dd962021-05-13 23:27:01 -030070 $info["givenName"] = $user->first_name;
71 $info["sn"] = $user->last_name;
72 $info["uid"] = $user->user_name;
matheusfillipeabd513e2021-05-11 03:29:11 -030073 #$info["homeDirectory"]="/home/";
matheusfillipef43dd962021-05-13 23:27:01 -030074 $info["mail"] = $user->email;
75 $info["displayName"] = $user->first_name . " " . $user->last_name;
matheusfillipeabd513e2021-05-11 03:29:11 -030076 #$info["departmentNumber"]=$user->id;
matheusfillipef43dd962021-05-13 23:27:01 -030077 $info["cn"] = $user->user_name;
78 $info["userPassword"] = $user->user_hash;
matheusfillipeabd513e2021-05-11 03:29:11 -030079 $info["objectclass"][0] = "top";
80 $info["objectclass"][1] = "person";
81 $info["objectclass"][2] = "inetOrgPerson";
82 $info["objectclass"][3] = "organizationalPerson";
83
84
85
matheusfillipef43dd962021-05-13 23:27:01 -030086 $ldap_user = "cn=" . $USER . "," . join(",", array_slice(explode(",", $ldaptree), 1));
matheusfillipeabd513e2021-05-11 03:29:11 -030087 $ldap_pass = $PASSWORD;
88
89 //First: Connect to LDAP Server
matheusfillipef43dd962021-05-13 23:27:01 -030090 $connect = ldap_connect($ldap_host, $ldap_port)
91 or debug(">>Could not connect to LDAP server to add user<<");
matheusfillipeabd513e2021-05-11 03:29:11 -030092 ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3);
93 ldap_set_option($connect, LDAP_OPT_REFERRALS, 0);
94
95 //Login to LDAP
96 ldap_bind($connect, $ldap_user, $ldap_pass)
matheusfillipef43dd962021-05-13 23:27:01 -030097 or debug(">>Could not bind to $ldap_host to add user<<");
matheusfillipeabd513e2021-05-11 03:29:11 -030098
99 // Adding new user
100
matheusfillipef43dd962021-05-13 23:27:01 -0300101 $add = ldap_add($connect, $base_dn, $info)
102 or debug(">>Not able to load user <<");
matheusfillipeabd513e2021-05-11 03:29:11 -0300103
104 // Close connection
matheusfillipef43dd962021-05-13 23:27:01 -0300105 ldap_close($connect);
matheusfillipeabd513e2021-05-11 03:29:11 -0300106
matheusfillipef43dd962021-05-13 23:27:01 -0300107 // Return value of operation
matheusfillipeabd513e2021-05-11 03:29:11 -0300108
109 return $add;
110}
matheusfillipef43dd962021-05-13 23:27:01 -0300111function ldap_user_count($user)
112{
matheusfillipeabd513e2021-05-11 03:29:11 -0300113 return ldap_search_query($user)["count"];
114}
matheusfillipef43dd962021-05-13 23:27:01 -0300115function ldap_mail_count($email)
116{
matheusfillipeabd513e2021-05-11 03:29:11 -0300117 return ldap_search_query($email, "mail")["count"];
118}
matheusfillipef43dd962021-05-13 23:27:01 -0300119
120function change_password($email, $new_password)
121{
122 include 'config.php';
123 $ldap_host = $HOST;
124 $ldap_port = $PORT;
125 $ldaptree = explode("{},", $BASE_DN)[1];
126
127 $ldap_user = "cn=" . $USER . "," . join(",", array_slice(explode(",", $ldaptree), 1));
128 $ldap_pass = $PASSWORD;
129
130 //First: Connect to LDAP Server
131 $connect = ldap_connect($ldap_host, $ldap_port)
132 or debug(">>Could not connect to LDAP server to add user<<");
133 ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3);
134 ldap_set_option($connect, LDAP_OPT_REFERRALS, 0);
135
136 //Login to LDAP
137 ldap_bind($connect, $ldap_user, $ldap_pass)
138 or debug(">>Could not bind to $ldap_host to add user<<");
139
140
141 $result = ldap_search($connect, $ldaptree, "(mail=" . $email . ")") or die("Error in search query: " . ldap_error($connect));
142 $data = ldap_get_entries($connect, $result);
143 if (!$data['count'] || !isset($data[0]["dn"]) || empty($data[0]["dn"])) {
144 return false;
145 }
146 $dn = $data[0]["dn"];
147
148 $newEntry = ['userPassword' => "{crypt}" . crypt($new_password, '$6$' . generateSalt(10) . '$')];
149
150 if (ldap_mod_replace($connect, $dn, $newEntry))
151 return true;
152 else
153 return false;
154}