fixed the provision user method
[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 = 21356;
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         //echo "usercn: $usercn\n";
146         $basecn = preg_replace("/,$/", "", $tcn);
147         $sr = ldap_search($cnt, "$basecn", "(&(objectCategory=group)(member:1.2.840.113556.1.4.1941:=$usercn))");
148         $fil = "(&(objectCategory=group)(member:1.2.840.113556.1.4.1941:=$usercn))";
149         $info = ldap_get_entries($cnt, $sr);
150         foreach($info as $kpot => $lpot) {
151                 if(isset($lpot["samaccountname"])) {
152                         //echo "checking: ".$lpot["cn"][0]."\n";
153                         if(strtolower($lpot["cn"][0]) == strtolower($group)) return true;
154                 }
155         }
156         return false;
157 }
158
159
160 function getUsersInGroup($domain, $adlogin, $adpass, $group)
161 {
162         $addom = $domain;
163         
164         $servers = dns_get_record("_gc._tcp.$addom");
165         if(count($servers)<1) {
166                 echo "AD servers cant be found, fail!\n";
167         }
168         
169         
170         // we should check all servers, but lets just go with 0 for now
171         $cnt = ldap_connect($servers[0]["target"], $servers[0]["port"]);
172         $bind = ldap_bind($cnt, "$adlogin@$addom", "$adpass");
173         if($bind) {
174         } else {
175                 echo "Bind Failed\n";
176                 return false;
177         }
178         
179         $ars = explode(".", $addom);
180         
181         $tcn = "";
182         foreach($ars as $val) {
183                 $tcn .= "DC=$val,";
184         }
185         
186         $basecn = preg_replace("/,$/", "", $tcn);
187         
188         // first, find the dn for our user
189         $sr = ldap_search($cnt, "$basecn", "(&(objectCategory=group)(cn=$group))");
190         $info = ldap_get_entries($cnt, $sr);
191         //print_r($info);
192         $groupcn=$info[0]["dn"];
193         //exit(0);
194         
195         $basecn = preg_replace("/,$/", "", $tcn);
196         $sr = ldap_search($cnt, "$basecn", "(&(objectCategory=user)(memberof:1.2.840.113556.1.4.1941:=$groupcn))");
197         //$fil = "(&(objectCategory=group)(member:1.2.840.113556.1.4.1941:=$usercn))";
198         $info = ldap_get_entries($cnt, $sr);
199         //print_r($info);
200         $arbi = "";
201         //exit(0);
202         $i = 0;
203         foreach($info as $kpot => $lpot) {
204                 if(isset($lpot["samaccountname"])) {
205                         $arbi[$lpot["samaccountname"][0]] =  $lpot["name"][0];
206                 }
207         }
208         
209         return $arbi;
210 }
211
212 function generateRandomString($len)
213 {
214         $str = "";
215         $strpos = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
216         
217         for($i=0; $i<$len; $i++) {
218                 $str .= $strpos[rand(0, strlen($strpos)-1)];
219         }
220         
221         return $str;
222 }
223
224 function generateHexString($len)
225 {
226         $str = "";
227         $strpos = "0123456789ABCDEF";
228         
229         for($i=0; $i<$len; $i++) {
230                 $str .= $strpos[rand(0, strlen($strpos)-1)];
231         }
232         
233         return $str;
234 }
235
236
237 ?>