511575910ebcaefe9b1980867e01664d92c45843
[ga4php.git] / gaas / lib / gaasdMessages.php
1 <?php
2
3 // this file defines all the messages used by gaaasd
4
5 // there are only really two status messages at this point - "init" meaning we have no been defined yet
6 // and "running" meaning we have been defined
7 function gaasStatus_server($messages)
8 {
9         global $initState, $backEnd;
10
11         $return = "init";
12         if($initState != false && $backEnd != "") {
13                 $return = "running";
14                 $be = confGetVal("backend");
15                 if($be == "AD") {
16                         $dom = confGetVal("ad.domain");
17                         $user = confGetVal("ad.user");
18                         $client = confGetVal("ad.clientdef");
19                         $admin = confGetVal("ad.admindef");
20                         $return .= " - AD integrated to $dom, GAASD Username: $user, Clients Group: $client, Admins Group: $admin";             
21                 } else {
22                         $return .= " - internal database";
23                 }
24                 
25         }
26         
27         
28         
29         return $return;
30 }
31
32
33 function gaasInitServer_server($msg)
34 {
35         global $initState, $backEnd;
36         
37         error_log("Init server called\n");
38         // here we "init" the server, if we're ad, we attempt to connect to AD and if it all works
39         // we then create the db
40         // $m["backend"] = "AD|IN";
41         // AD expects:
42         // $m["domain"] = "somedomain.com";
43         // $m["user"] = "someuser";
44         // $m["pass"] = "somepassword";
45         // $m["userdef"] = "user definition paramaters";
46         // IN expects
47         // $m["user"] = "someuser";
48         // $m["pass"] = "somepass";
49         echo "initstate is $initState";
50         if($initState) {
51                 echo "true\n";
52         } else {
53                 echo "false\n";
54         }
55         if($initState) {
56                 error_log("init server called when server already init'd\n");
57                 return false;
58         }
59         
60         if($msg["backend"] == "AD") {
61                 $backEnd = "AD";
62                 // attempt connect to AD, verify creds
63                 $addom = $msg["domain"];
64                 $adlogin = $msg["user"];
65                 $adpass = $msg["pass"];
66                 $adclientdef = $msg["clientdef"];
67                 $adadmindef = $msg["admindef"];
68                 
69                 // now wee test our logins...
70                 // first look up the domain name stuff
71                 $servers = dns_get_record("_gc._tcp.$addom");
72                 if(count($servers)<1) {
73                         echo "AD servers cant be found, fail!\n";
74                 }
75                 
76                 // we should check all servers, but lets just go with 0 for now
77                 $res =  adTestLogin($addom, $adlogin, $adpass);
78                 if(!$res) {
79                         return false;
80                 }
81                 
82                 
83                 // then
84                 createDB();
85                 confSetVal("ad.domain", $addom);
86                 confSetVal("ad.user", $adlogin);
87                 confSetVal("ad.pass", $adpass);
88                 confSetVal("ad.encryptionkey", generateHexString(32));
89                 confSetVal("ad.clientdef", $adclientdef);
90                 confSetVal("ad.admindef", $adadmindef);
91                 confSetVal("backend", "AD");
92                 
93                 $initState = true;
94                 $backEnd = "AD";
95                 
96                 // and that should be it... i think cept im in a forked erg.. lets assume it works, need pain i do not.
97                 return true;
98         } else if($msg["backend"] == "IN") {
99                 // this ones simpler
100                 $backEnd = "IN";
101                 createDB();
102                 
103                 // create the user in the db
104                 $username = $msg["user"];
105                 $password = $msg["pass"];
106                 
107                 $myga = new gaasdGA();
108                 $myga->setUser($username);
109                 
110                 if($password == "") $pass = "";
111                 else $pass = hash('sha512', $password);
112                 
113                 $db = getDB();
114                 $db->query($sql = "update users set users_password='$pass' where users_username='$username'");
115                 
116                 $initState = "running";
117                 return true;
118         } else {
119                 return false;
120         }
121 }
122
123
124 function gaasSetADLogin_server($msg)
125 {
126         global $initState, $backEnd;
127         
128         if($initState != "running") {
129                 return "not in running init state";
130         }
131         
132         if($backEnd != "AD") {
133                 return "not setup as AD client";
134         }
135         
136         $addom = $msg["domain"];
137         $adlogin = $msg["user"];
138         $adpass = $msg["pass"];
139         
140         $res = adTestLogin($addmo, $adlogin, $adpass);
141         if($res != 0) {
142                 return "not able to connect to AD with given cred's";
143         }
144         
145         confSetVal("ad.domain", $addom);
146         confSetVal("ad.user", $adlogin);
147         confSetVal("ad.pass", $adpass);
148         
149         return true;
150         
151 }
152
153 function gaasSetAdminGroup_server($msg)
154 {
155         if(confGetVal("backend") == "AD") {
156                 confSetVal("ad.admindef", $msg["admingroup"]);
157         } else return false;
158         
159         return true;
160 }
161
162 function gaasSetClientGroup_server($msg)
163 {
164         if(confGetVal("backend") == "AD") {
165                 confSetVal("ad.clientdef", $msg["clientgroup"]);
166         } else return false;
167         
168         return true;
169 }
170
171 function gaasProvisionUser_server($msg)
172 {
173         
174         // function userInGroup($user, $domain, $adlogin, $adpass, $group)
175         if(confGetVal("backend") == "AD") {
176                 userInGroup($msg["username"], confGetVal("ad.domain"), confGetVal("ad.user", $adlogin), confGetVal("ad.pass"), confGetVal("ad.clientdef"));
177         } else {
178                 // internal db
179         }
180         
181         
182         return true;
183 }
184
185 function gaasGetUsers_server($msg)
186 {
187         $haveTokens = $msg["havetokens"];
188         $userPatter = $msg["userpattern"];
189         $group = $msg["group"];
190         
191         if(confGetval("backend") == "AD") {
192                 $adgroup = "";
193                 if($group == "admin") {
194                         $adgroup = confGetVal("ad.admindef");
195                 } else {
196                         $adgroup = confGetVal("ad.clientdef");
197                 }
198                 $addom = confGetVal("ad.domain");
199                 $aduser = confGetVal("ad.user");
200                 $adpass = confGetVal("ad.pass");
201                 //echo "using group $adgroup for $group\n";
202                 
203                 $users = getUsersInGroup($addom, $aduser, $adpass, $adgroup);
204                 foreach($users as $user => $real) {
205                         hasToken($user);
206                 }
207         } else {
208                 // internal db
209         }
210         return $users;
211 }
212
213 function gaasDeleteUser_server($msg)
214 {
215         $username = $msg["username"];
216         $db = getDB();
217         $db->query($sql = "delete from users where users_username='$username'");
218         
219 }
220 ?>