added some code to deal with username case (made it all lower)
[ga4php.git] / gaas / lib / globalLib.php
1 <?php
2
3 // the global lib sets alot of global variables, its fairly unexciting
4 $BASE_DIR = realpath(dirname(__FILE__)."/../../");
5 global $BASE_DIR;
6
7 // the tcp port number we use for comms
8 $TCP_PORT_NUMBER = 21256;
9 global $TCP_PORT_NUMBER;
10
11
12 /* tasks we need to do (- prefix means done or working)
13  * - status
14  * - init
15  * - set ad login
16  * - set ad client group
17  * - set ad admin group
18  * - provision user
19  * - get users
20  * - delete user
21  * - create hardware token
22  * - list hardware tokens
23  * - assign hardware token
24  * disable user
25  * set user password
26  * enable admin for user
27  * disable admin for user
28  * resync token
29  * get qrcode
30  * re-create user token
31  * set user pin
32  * authenticate user by token
33  * authenticate user by password
34  * 
35  */
36
37
38 // the messages structure, used to extend gaas if needed
39 define("MSG_STATUS", 18);
40 define("MSG_INIT_SERVER", 19);
41 define("MSG_SET_AD_LOGIN", 20);
42 define("MSG_SET_CLIENT_GROUP", 21);
43 define("MSG_SET_ADMIN_GROUP", 22);
44 define("MSG_PROVISION_USER",23);
45 define("MSG_GET_USERS", 24);
46 define("MSG_DELETE_USER", 25);
47 define("MSG_ASSIGN_TOKEN",26);
48 define("MSG_ADD_HARDWARE",27);
49 define("MSG_GET_HARDWARE",28);
50
51 // the gaasd call's $MESSAGE[<MSG>]_server() for the server side
52 // and $MESSAGE[<msg>]_client() for the client side 
53 $MESSAGES[MSG_STATUS] = "gaasStatus"; //
54 $MESSAGES[MSG_INIT_SERVER] = "gaasInitServer"; // AD: "AD", "user", "pass", "domain", "client def", "admin def" - IN: "IN", "user", "pass"
55 $MESSAGES[MSG_SET_AD_LOGIN] = "gaasSetADLogin"; // domain, user, password
56 $MESSAGES[MSG_SET_CLIENT_GROUP] = "gaasSetClientGroup"; // groupname
57 $MESSAGES[MSG_SET_ADMIN_GROUP] = "gaasSetAdminGroup"; // groupname
58 $MESSAGES[MSG_PROVISION_USER] = "gaasProvisionUser"; // username, tokentype, tokenkey, hardware|software
59 $MESSAGES[MSG_GET_USERS] = "gaasGetUsers"; // [admin|client], [name pattern], [only with tokens]
60 $MESSAGES[MSG_DELETE_USER] = "gaasDeleteUser"; // username
61 $MESSAGES[MSG_ASSIGN_TOKEN] = "gaasAssignToken"; // username, tokenid
62 $MESSAGES[MSG_ADD_HARDWARE] = "gaasAddHardwareToken"; // username, tokenid
63 $MESSAGES[MSG_GET_HARDWARE] = "gaasGetHardwareTokens"; //
64
65 global $MESSAGES;
66
67
68
69
70
71
72
73 function adTestLogin($domain, $user, $password)
74 {
75         $servers = dns_get_record("_gc._tcp.$domain");
76         if(count($servers)<1) {
77                 echo "AD servers cant be found for $domain, fail!\n";
78         }
79         
80         echo count($servers)." AD servers returned, using ".$servers[0]["target"]."\n";
81         
82         // we should check all servers, but lets just go with 0 for now
83         $cnt = ldap_connect($servers[0]["target"], $servers[0]["port"]);
84         echo "Connected\n";
85         $bind = ldap_bind($cnt, "$user@$domain", "$password");
86         if($bind) {
87                 echo "login has succeeded\n";
88                 return true;
89         } else {
90                 echo "login has failed\n";
91                 return false;
92         }       
93 }
94
95 function getADGroups($domain, $user, $password)
96 {
97         $servers = dns_get_record("_gc._tcp.$domain");
98         if(count($servers)<1) {
99                 echo "AD servers cant be found for $domain, fail!\n";
100         }
101         
102         echo count($servers)." AD servers returned, using ".$servers[0]["target"]."\n";
103         
104         // we should check all servers, but lets just go with 0 for now
105         $cnt = ldap_connect($servers[0]["target"], $servers[0]["port"]);
106         echo "Connected\n";
107         $bind = ldap_bind($cnt, "$user@$domain", "$password");
108         if(!$bind) {
109                 echo "login has failed\n";
110                 return false;
111         }       
112
113         $ars = explode(".", $addom);
114         
115         $tcn = "";
116         foreach($ars as $val) {
117                 $tcn .= "DC=$val,";
118         }
119         
120         $basecn = preg_replace("/,$/", "", $tcn);
121         
122         $sr = ldap_search($cnt, "$basecn", "(objectclass=group)");
123         $info = ldap_get_entries($cnt, $sr);
124         
125         if($info["count"] < 1) {
126                 echo "Couldn't find a matching group\n";
127                 return 0;
128         } else {
129                 echo "Found a group, ".$info[0]["cn"][0]."\n";
130                 echo "With a description of, ".$info[0]["description"][0]."\n";
131                 echo "and a dn of, ".$info[0]["dn"]."\n";
132         }
133         
134         return $info;
135 }
136
137 function userInGroup($user, $domain, $adlogin, $adpass, $group)
138 {
139         $addom = $domain;
140         $usertocheck = $user;
141         
142         $servers = dns_get_record("_gc._tcp.$addom");
143         if(count($servers)<1) {
144                 echo "AD servers cant be found, fail!\n";
145         }
146         
147         
148         // we should check all servers, but lets just go with 0 for now
149         $cnt = ldap_connect($servers[0]["target"], $servers[0]["port"]);
150         $bind = ldap_bind($cnt, "$adlogin@$addom", "$adpass");
151         if($bind) {
152         } else {
153                 echo "Bind Failed\n";
154                 return false;
155         }
156         
157         $ars = explode(".", $addom);
158         
159         $tcn = "";
160         foreach($ars as $val) {
161                 $tcn .= "DC=$val,";
162         }
163         
164         $basecn = preg_replace("/,$/", "", $tcn);
165         
166         // first, find the dn for our user
167         $sr = ldap_search($cnt, "$basecn", "(&(objectclass=user)(samaccountname=$usertocheck))");
168         $info = ldap_get_entries($cnt, $sr);
169         //print_r($info);
170         $usercn=$info[0]["dn"];
171         
172         
173         //exit(0);
174         
175         //echo "usercn: $usercn\n";
176         $basecn = preg_replace("/,$/", "", $tcn);
177         $sr = ldap_search($cnt, "$basecn", "(&(objectCategory=group)(member:1.2.840.113556.1.4.1941:=$usercn))");
178         $fil = "(&(objectCategory=group)(member:1.2.840.113556.1.4.1941:=$usercn))";
179         $info = ldap_get_entries($cnt, $sr);
180         foreach($info as $kpot => $lpot) {
181                 if(isset($lpot["samaccountname"])) {
182                         //echo "checking: ".$lpot["cn"][0]."\n";
183                         if(strtolower($lpot["cn"][0]) == strtolower($group)) return true;
184                 }
185         }
186         return false;
187 }
188
189
190 function getUsersInGroup($domain, $adlogin, $adpass, $group)
191 {
192         $addom = $domain;
193         
194         $servers = dns_get_record("_gc._tcp.$addom");
195         if(count($servers)<1) {
196                 echo "AD servers cant be found, fail!\n";
197         }
198         
199         
200         // we should check all servers, but lets just go with 0 for now
201         $cnt = ldap_connect($servers[0]["target"], $servers[0]["port"]);
202         $bind = ldap_bind($cnt, "$adlogin@$addom", "$adpass");
203         if($bind) {
204         } else {
205                 echo "Bind Failed\n";
206                 return false;
207         }
208         
209         $ars = explode(".", $addom);
210         
211         $tcn = "";
212         foreach($ars as $val) {
213                 $tcn .= "DC=$val,";
214         }
215         
216         $basecn = preg_replace("/,$/", "", $tcn);
217         
218         // first, find the dn for our user
219         $sr = ldap_search($cnt, "$basecn", "(&(objectCategory=group)(cn=$group))");
220         $info = ldap_get_entries($cnt, $sr);
221         //print_r($info);
222         $groupcn=$info[0]["dn"];
223         //exit(0);
224         
225         $basecn = preg_replace("/,$/", "", $tcn);
226         $sr = ldap_search($cnt, "$basecn", "(&(objectCategory=user)(memberof:1.2.840.113556.1.4.1941:=$groupcn))");
227         //$fil = "(&(objectCategory=group)(member:1.2.840.113556.1.4.1941:=$usercn))";
228         $info = ldap_get_entries($cnt, $sr);
229         //print_r($info);
230         $arbi = "";
231         //exit(0);
232         $i = 0;
233         foreach($info as $kpot => $lpot) {
234                 if(isset($lpot["samaccountname"])) {
235                         $arbi[$i]["realname"] =  $lpot["name"][0];
236                         $arbi[$i]["username"] = strtolower($lpot["samaccountname"][0]);
237                         $i++;
238                 }
239         }
240         
241         return $arbi;
242 }
243
244 function generateRandomString($len)
245 {
246         $str = "";
247         $strpos = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
248         
249         for($i=0; $i<$len; $i++) {
250                 $str .= $strpos[rand(0, strlen($strpos)-1)];
251         }
252         
253         return $str;
254 }
255
256 function generateHexString($len)
257 {
258         $str = "";
259         $strpos = "0123456789ABCDEF";
260         
261         for($i=0; $i<$len; $i++) {
262                 $str .= $strpos[rand(0, strlen($strpos)-1)];
263         }
264         
265         return $str;
266 }
267
268
269 ?>