51854875d88990d149828a766bb3c3e2e72d3f69
[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 = 21335;
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
21
22 // the gaasd call's $MESSAGE[<MSG>]_server() for the server side
23 // and $MESSAGE[<msg>]_client() for the client side 
24 $MESSAGES[MSG_STATUS] = "gaasStatus";
25 $MESSAGES[MSG_INIT_SERVER] = "gaasInitServer";
26 $MESSAGES[MSG_SET_AD_LOGIN] = "gaasSetADLogin";
27 $MESSAGES[MSG_SET_CLIENT_GROUP] = "gaasSetClientGroup";
28 $MESSAGES[MSG_SET_ADMIN_GROUP] = "gaasSetAdminGroup";
29 global $MESSAGES;
30
31
32
33
34
35
36
37 function adTestLogin($domain, $user, $password)
38 {
39         $servers = dns_get_record("_gc._tcp.$domain");
40         if(count($servers)<1) {
41                 echo "AD servers cant be found for $domain, fail!\n";
42         }
43         
44         echo count($servers)." AD servers returned, using ".$servers[0]["target"]."\n";
45         
46         // we should check all servers, but lets just go with 0 for now
47         $cnt = ldap_connect($servers[0]["target"], $servers[0]["port"]);
48         echo "Connected\n";
49         $bind = ldap_bind($cnt, "$user@$domain", "$password");
50         if($bind) {
51                 echo "login has succeeded\n";
52                 return true;
53         } else {
54                 echo "login has failed\n";
55                 return false;
56         }       
57 }
58
59 function getADGroups($domain, $user, $password)
60 {
61         $servers = dns_get_record("_gc._tcp.$domain");
62         if(count($servers)<1) {
63                 echo "AD servers cant be found for $domain, fail!\n";
64         }
65         
66         echo count($servers)." AD servers returned, using ".$servers[0]["target"]."\n";
67         
68         // we should check all servers, but lets just go with 0 for now
69         $cnt = ldap_connect($servers[0]["target"], $servers[0]["port"]);
70         echo "Connected\n";
71         $bind = ldap_bind($cnt, "$user@$domain", "$password");
72         if(!$bind) {
73                 echo "login has failed\n";
74                 return false;
75         }       
76
77         $ars = explode(".", $addom);
78         
79         $tcn = "";
80         foreach($ars as $val) {
81                 $tcn .= "DC=$val,";
82         }
83         
84         $basecn = preg_replace("/,$/", "", $tcn);
85         
86         $sr = ldap_search($cnt, "$basecn", "(objectclass=group)");
87         $info = ldap_get_entries($cnt, $sr);
88         
89         if($info["count"] < 1) {
90                 echo "Couldn't find a matching group\n";
91                 return 0;
92         } else {
93                 echo "Found a group, ".$info[0]["cn"][0]."\n";
94                 echo "With a description of, ".$info[0]["description"][0]."\n";
95                 echo "and a dn of, ".$info[0]["dn"]."\n";
96         }
97         
98         return $info;
99 }
100
101 function generateRandomString($len)
102 {
103         $str = "";
104         $strpos = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
105         
106         for($i=0; $i<$len; $i++) {
107                 $str .= $strpos[rand(0, strlen($strpos)-1)];
108         }
109         
110         return $str;
111 }
112
113 function generateHexString($len)
114 {
115         $str = "";
116         $strpos = "0123456789ABCDEF";
117         
118         for($i=0; $i<$len; $i++) {
119                 $str .= $strpos[rand(0, strlen($strpos)-1)];
120         }
121         
122         return $str;
123 }
124
125
126 ?>