added client get users command
[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 = 21336;
9 global $TCP_PORT_NUMBER;
10
11
12
13
14 // the messages structure, used to extend gaas if needed
15 define("MSG_STATUS", 18);
16 define("MSG_INIT_SERVER", 19);
17 define("MSG_SET_AD_LOGIN", 20);
18 define("MSG_SET_CLIENT_GROUP", 21);
19 define("MSG_SET_ADMIN_GROUP", 22);
20 define("MSG_PROVISION_USER",23);
21 define("MSG_GET_USERS", 24);
22
23 // the gaasd call's $MESSAGE[<MSG>]_server() for the server side
24 // and $MESSAGE[<msg>]_client() for the client side 
25 $MESSAGES[MSG_STATUS] = "gaasStatus";
26 $MESSAGES[MSG_INIT_SERVER] = "gaasInitServer"; 
27 $MESSAGES[MSG_SET_AD_LOGIN] = "gaasSetADLogin"; // domain, user, password
28 $MESSAGES[MSG_SET_CLIENT_GROUP] = "gaasSetClientGroup"; // groupname
29 $MESSAGES[MSG_SET_ADMIN_GROUP] = "gaasSetAdminGroup";
30 $MESSAGES[MSG_PROVISION_USER] = "gaasProvisionUser"; // username
31 $MESSAGES[MSG_GET_USERS] = "gaasGetUsers"; // [admin|client], [name pattern], [only with tokens]
32
33 global $MESSAGES;
34
35
36
37
38
39
40
41 function adTestLogin($domain, $user, $password)
42 {
43         $servers = dns_get_record("_gc._tcp.$domain");
44         if(count($servers)<1) {
45                 echo "AD servers cant be found for $domain, fail!\n";
46         }
47         
48         echo count($servers)." AD servers returned, using ".$servers[0]["target"]."\n";
49         
50         // we should check all servers, but lets just go with 0 for now
51         $cnt = ldap_connect($servers[0]["target"], $servers[0]["port"]);
52         echo "Connected\n";
53         $bind = ldap_bind($cnt, "$user@$domain", "$password");
54         if($bind) {
55                 echo "login has succeeded\n";
56                 return true;
57         } else {
58                 echo "login has failed\n";
59                 return false;
60         }       
61 }
62
63 function getADGroups($domain, $user, $password)
64 {
65         $servers = dns_get_record("_gc._tcp.$domain");
66         if(count($servers)<1) {
67                 echo "AD servers cant be found for $domain, fail!\n";
68         }
69         
70         echo count($servers)." AD servers returned, using ".$servers[0]["target"]."\n";
71         
72         // we should check all servers, but lets just go with 0 for now
73         $cnt = ldap_connect($servers[0]["target"], $servers[0]["port"]);
74         echo "Connected\n";
75         $bind = ldap_bind($cnt, "$user@$domain", "$password");
76         if(!$bind) {
77                 echo "login has failed\n";
78                 return false;
79         }       
80
81         $ars = explode(".", $addom);
82         
83         $tcn = "";
84         foreach($ars as $val) {
85                 $tcn .= "DC=$val,";
86         }
87         
88         $basecn = preg_replace("/,$/", "", $tcn);
89         
90         $sr = ldap_search($cnt, "$basecn", "(objectclass=group)");
91         $info = ldap_get_entries($cnt, $sr);
92         
93         if($info["count"] < 1) {
94                 echo "Couldn't find a matching group\n";
95                 return 0;
96         } else {
97                 echo "Found a group, ".$info[0]["cn"][0]."\n";
98                 echo "With a description of, ".$info[0]["description"][0]."\n";
99                 echo "and a dn of, ".$info[0]["dn"]."\n";
100         }
101         
102         return $info;
103 }
104
105 function userInGroup($user, $domain, $adlogin, $adpass, $group)
106 {
107         $addom = $domain;
108         $usertocheck = $user;
109         
110         $servers = dns_get_record("_gc._tcp.$addom");
111         if(count($servers)<1) {
112                 echo "AD servers cant be found, fail!\n";
113         }
114         
115         
116         // we should check all servers, but lets just go with 0 for now
117         $cnt = ldap_connect($servers[0]["target"], $servers[0]["port"]);
118         $bind = ldap_bind($cnt, "$adlogin@$addom", "$adpass");
119         if($bind) {
120         } else {
121                 echo "Bind Failed\n";
122                 return false;
123         }
124         
125         $ars = explode(".", $addom);
126         
127         $tcn = "";
128         foreach($ars as $val) {
129                 $tcn .= "DC=$val,";
130         }
131         
132         $basecn = preg_replace("/,$/", "", $tcn);
133         
134         // first, find the dn for our user
135         $sr = ldap_search($cnt, "$basecn", "(&(objectclass=user)(samaccountname=$usertocheck))");
136         $info = ldap_get_entries($cnt, $sr);
137         //print_r($info);
138         $usercn=$info[0]["dn"];
139         
140         
141         //exit(0);
142         
143         $basecn = preg_replace("/,$/", "", $tcn);
144         $sr = ldap_search($cnt, "$basecn", "(&(objectCategory=group)(member:1.2.840.113556.1.4.1941:=$usercn))");
145         $fil = "(&(objectCategory=group)(member:1.2.840.113556.1.4.1941:=$usercn))";
146         $info = ldap_get_entries($cnt, $sr);
147         foreach($info as $kpot => $lpot) {
148                 if(isset($lpot["samaccountname"])) {
149                         if($lpot["cn"][0] == $group) return true;
150                 }
151         }
152         return false;
153 }
154
155
156 function getUsersInGroup($domain, $adlogin, $adpass, $group)
157 {
158         $addom = $domain;
159         
160         $servers = dns_get_record("_gc._tcp.$addom");
161         if(count($servers)<1) {
162                 echo "AD servers cant be found, fail!\n";
163         }
164         
165         
166         // we should check all servers, but lets just go with 0 for now
167         $cnt = ldap_connect($servers[0]["target"], $servers[0]["port"]);
168         $bind = ldap_bind($cnt, "$adlogin@$addom", "$adpass");
169         if($bind) {
170         } else {
171                 echo "Bind Failed\n";
172                 return false;
173         }
174         
175         $ars = explode(".", $addom);
176         
177         $tcn = "";
178         foreach($ars as $val) {
179                 $tcn .= "DC=$val,";
180         }
181         
182         $basecn = preg_replace("/,$/", "", $tcn);
183         
184         // first, find the dn for our user
185         $sr = ldap_search($cnt, "$basecn", "(&(objectCategory=group)(cn=$group))");
186         $info = ldap_get_entries($cnt, $sr);
187         //print_r($info);
188         $groupcn=$info[0]["dn"];
189         //exit(0);
190         
191         $basecn = preg_replace("/,$/", "", $tcn);
192         $sr = ldap_search($cnt, "$basecn", "(&(objectCategory=user)(memberof:1.2.840.113556.1.4.1941:=$groupcn))");
193         //$fil = "(&(objectCategory=group)(member:1.2.840.113556.1.4.1941:=$usercn))";
194         $info = ldap_get_entries($cnt, $sr);
195         //print_r($info);
196         $arbi = "";
197         //exit(0);
198         $i = 0;
199         foreach($info as $kpot => $lpot) {
200                 if(isset($lpot["samaccountname"])) {
201                         $arbi[$i]["username"] = $lpot["samaccountname"][0];
202                         $arbi[$i]["realname"] = $lpot["name"][0];
203                         $i++;
204                 }
205         }
206         
207         return $arbi;
208 }
209
210 function generateRandomString($len)
211 {
212         $str = "";
213         $strpos = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
214         
215         for($i=0; $i<$len; $i++) {
216                 $str .= $strpos[rand(0, strlen($strpos)-1)];
217         }
218         
219         return $str;
220 }
221
222 function generateHexString($len)
223 {
224         $str = "";
225         $strpos = "0123456789ABCDEF";
226         
227         for($i=0; $i<$len; $i++) {
228                 $str .= $strpos[rand(0, strlen($strpos)-1)];
229         }
230         
231         return $str;
232 }
233
234
235 ?>