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