3 // the global lib sets alot of global variables, its fairly unexciting
4 $BASE_DIR = realpath(dirname(__FILE__)."/../../");
7 // the tcp port number we use for comms
8 $TCP_PORT_NUMBER = 21336;
9 global $TCP_PORT_NUMBER;
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);
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
43 function adTestLogin($domain, $user, $password)
45 $servers = dns_get_record("_gc._tcp.$domain");
46 if(count($servers)<1) {
47 echo "AD servers cant be found for $domain, fail!\n";
50 echo count($servers)." AD servers returned, using ".$servers[0]["target"]."\n";
52 // we should check all servers, but lets just go with 0 for now
53 $cnt = ldap_connect($servers[0]["target"], $servers[0]["port"]);
55 $bind = ldap_bind($cnt, "$user@$domain", "$password");
57 echo "login has succeeded\n";
60 echo "login has failed\n";
65 function getADGroups($domain, $user, $password)
67 $servers = dns_get_record("_gc._tcp.$domain");
68 if(count($servers)<1) {
69 echo "AD servers cant be found for $domain, fail!\n";
72 echo count($servers)." AD servers returned, using ".$servers[0]["target"]."\n";
74 // we should check all servers, but lets just go with 0 for now
75 $cnt = ldap_connect($servers[0]["target"], $servers[0]["port"]);
77 $bind = ldap_bind($cnt, "$user@$domain", "$password");
79 echo "login has failed\n";
83 $ars = explode(".", $addom);
86 foreach($ars as $val) {
90 $basecn = preg_replace("/,$/", "", $tcn);
92 $sr = ldap_search($cnt, "$basecn", "(objectclass=group)");
93 $info = ldap_get_entries($cnt, $sr);
95 if($info["count"] < 1) {
96 echo "Couldn't find a matching group\n";
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";
107 function userInGroup($user, $domain, $adlogin, $adpass, $group)
110 $usertocheck = $user;
112 $servers = dns_get_record("_gc._tcp.$addom");
113 if(count($servers)<1) {
114 echo "AD servers cant be found, fail!\n";
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");
123 echo "Bind Failed\n";
127 $ars = explode(".", $addom);
130 foreach($ars as $val) {
134 $basecn = preg_replace("/,$/", "", $tcn);
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);
140 $usercn=$info[0]["dn"];
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;
158 function getUsersInGroup($domain, $adlogin, $adpass, $group)
162 $servers = dns_get_record("_gc._tcp.$addom");
163 if(count($servers)<1) {
164 echo "AD servers cant be found, fail!\n";
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");
173 echo "Bind Failed\n";
177 $ars = explode(".", $addom);
180 foreach($ars as $val) {
184 $basecn = preg_replace("/,$/", "", $tcn);
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);
190 $groupcn=$info[0]["dn"];
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);
201 foreach($info as $kpot => $lpot) {
202 if(isset($lpot["samaccountname"])) {
203 $arbi[$lpot["samaccountname"][0]] = $lpot["name"][0];
210 function generateRandomString($len)
213 $strpos = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
215 for($i=0; $i<$len; $i++) {
216 $str .= $strpos[rand(0, strlen($strpos)-1)];
222 function generateHexString($len)
225 $strpos = "0123456789ABCDEF";
227 for($i=0; $i<$len; $i++) {
228 $str .= $strpos[rand(0, strlen($strpos)-1)];