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);
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]
41 function adTestLogin($domain, $user, $password)
43 $servers = dns_get_record("_gc._tcp.$domain");
44 if(count($servers)<1) {
45 echo "AD servers cant be found for $domain, fail!\n";
48 echo count($servers)." AD servers returned, using ".$servers[0]["target"]."\n";
50 // we should check all servers, but lets just go with 0 for now
51 $cnt = ldap_connect($servers[0]["target"], $servers[0]["port"]);
53 $bind = ldap_bind($cnt, "$user@$domain", "$password");
55 echo "login has succeeded\n";
58 echo "login has failed\n";
63 function getADGroups($domain, $user, $password)
65 $servers = dns_get_record("_gc._tcp.$domain");
66 if(count($servers)<1) {
67 echo "AD servers cant be found for $domain, fail!\n";
70 echo count($servers)." AD servers returned, using ".$servers[0]["target"]."\n";
72 // we should check all servers, but lets just go with 0 for now
73 $cnt = ldap_connect($servers[0]["target"], $servers[0]["port"]);
75 $bind = ldap_bind($cnt, "$user@$domain", "$password");
77 echo "login has failed\n";
81 $ars = explode(".", $addom);
84 foreach($ars as $val) {
88 $basecn = preg_replace("/,$/", "", $tcn);
90 $sr = ldap_search($cnt, "$basecn", "(objectclass=group)");
91 $info = ldap_get_entries($cnt, $sr);
93 if($info["count"] < 1) {
94 echo "Couldn't find a matching group\n";
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";
105 function userInGroup($user, $domain, $adlogin, $adpass, $group)
108 $usertocheck = $user;
110 $servers = dns_get_record("_gc._tcp.$addom");
111 if(count($servers)<1) {
112 echo "AD servers cant be found, fail!\n";
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");
121 echo "Bind Failed\n";
125 $ars = explode(".", $addom);
128 foreach($ars as $val) {
132 $basecn = preg_replace("/,$/", "", $tcn);
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);
138 $usercn=$info[0]["dn"];
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;
156 function getUsersInGroup($domain, $adlogin, $adpass, $group)
160 $servers = dns_get_record("_gc._tcp.$addom");
161 if(count($servers)<1) {
162 echo "AD servers cant be found, fail!\n";
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");
171 echo "Bind Failed\n";
175 $ars = explode(".", $addom);
178 foreach($ars as $val) {
182 $basecn = preg_replace("/,$/", "", $tcn);
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);
188 $groupcn=$info[0]["dn"];
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);
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];
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)];