b6289560fda78ac5c2309833aeb907c4d3e6a836
[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 define("MSG_PROVISION_USER",23);
21
22
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";
28 $MESSAGES[MSG_SET_CLIENT_GROUP] = "gaasSetClientGroup";
29 $MESSAGES[MSG_SET_ADMIN_GROUP] = "gaasSetAdminGroup";
30 $MESSAGES[MSG_PROVISION_USER] = "gaasProvisionUser";
31 global $MESSAGES;
32
33
34
35
36
37
38
39 function adTestLogin($domain, $user, $password)
40 {
41         $servers = dns_get_record("_gc._tcp.$domain");
42         if(count($servers)<1) {
43                 echo "AD servers cant be found for $domain, fail!\n";
44         }
45         
46         echo count($servers)." AD servers returned, using ".$servers[0]["target"]."\n";
47         
48         // we should check all servers, but lets just go with 0 for now
49         $cnt = ldap_connect($servers[0]["target"], $servers[0]["port"]);
50         echo "Connected\n";
51         $bind = ldap_bind($cnt, "$user@$domain", "$password");
52         if($bind) {
53                 echo "login has succeeded\n";
54                 return true;
55         } else {
56                 echo "login has failed\n";
57                 return false;
58         }       
59 }
60
61 function getADGroups($domain, $user, $password)
62 {
63         $servers = dns_get_record("_gc._tcp.$domain");
64         if(count($servers)<1) {
65                 echo "AD servers cant be found for $domain, fail!\n";
66         }
67         
68         echo count($servers)." AD servers returned, using ".$servers[0]["target"]."\n";
69         
70         // we should check all servers, but lets just go with 0 for now
71         $cnt = ldap_connect($servers[0]["target"], $servers[0]["port"]);
72         echo "Connected\n";
73         $bind = ldap_bind($cnt, "$user@$domain", "$password");
74         if(!$bind) {
75                 echo "login has failed\n";
76                 return false;
77         }       
78
79         $ars = explode(".", $addom);
80         
81         $tcn = "";
82         foreach($ars as $val) {
83                 $tcn .= "DC=$val,";
84         }
85         
86         $basecn = preg_replace("/,$/", "", $tcn);
87         
88         $sr = ldap_search($cnt, "$basecn", "(objectclass=group)");
89         $info = ldap_get_entries($cnt, $sr);
90         
91         if($info["count"] < 1) {
92                 echo "Couldn't find a matching group\n";
93                 return 0;
94         } else {
95                 echo "Found a group, ".$info[0]["cn"][0]."\n";
96                 echo "With a description of, ".$info[0]["description"][0]."\n";
97                 echo "and a dn of, ".$info[0]["dn"]."\n";
98         }
99         
100         return $info;
101 }
102
103 function userInGroup($user, $domain, $adlogin, $adpass, $group)
104 {
105         $addom = $domain;
106         $usertocheck = $user;
107         
108         $servers = dns_get_record("_gc._tcp.$addom");
109         if(count($servers)<1) {
110                 echo "AD servers cant be found, fail!\n";
111         }
112         
113         
114         // we should check all servers, but lets just go with 0 for now
115         $cnt = ldap_connect($servers[0]["target"], $servers[0]["port"]);
116         $bind = ldap_bind($cnt, "$adlogin@$addom", "$adpass");
117         if($bind) {
118         } else {
119                 echo "Bind Failed\n";
120                 return false;
121         }
122         
123         $ars = explode(".", $addom);
124         
125         $tcn = "";
126         foreach($ars as $val) {
127                 $tcn .= "DC=$val,";
128         }
129         
130         $basecn = preg_replace("/,$/", "", $tcn);
131         
132         // first, find the dn for our user
133         $sr = ldap_search($cnt, "$basecn", "(&(objectclass=user)(samaccountname=$usertocheck))");
134         $info = ldap_get_entries($cnt, $sr);
135         //print_r($info);
136         $usercn=$info[0]["dn"];
137         
138         
139         //exit(0);
140         
141         $basecn = preg_replace("/,$/", "", $tcn);
142         $sr = ldap_search($cnt, "$basecn", "(&(objectCategory=group)(member:1.2.840.113556.1.4.1941:=$usercn))");
143         $fil = "(&(objectCategory=group)(member:1.2.840.113556.1.4.1941:=$usercn))";
144         $info = ldap_get_entries($cnt, $sr);
145         foreach($info as $kpot => $lpot) {
146                 if(isset($lpot["samaccountname"])) {
147                         if($lpot["cn"][0] == $group) return true;
148                 }
149         }
150         return false;
151 }
152
153 function generateRandomString($len)
154 {
155         $str = "";
156         $strpos = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
157         
158         for($i=0; $i<$len; $i++) {
159                 $str .= $strpos[rand(0, strlen($strpos)-1)];
160         }
161         
162         return $str;
163 }
164
165 function generateHexString($len)
166 {
167         $str = "";
168         $strpos = "0123456789ABCDEF";
169         
170         for($i=0; $i<$len; $i++) {
171                 $str .= $strpos[rand(0, strlen($strpos)-1)];
172         }
173         
174         return $str;
175 }
176
177
178 ?>