912e12b084f9cf634c0b994ffc8661d1099f057a
[ga4php.git] / authserver / authd / authd.php
1 <?php
2
3 // TODO: SO MUCH ERROR CHECKING ITS NOT FUNNY
4
5
6 // get out master library for ga4php
7 require_once("../lib/lib.php");
8
9         
10 //exit(0);
11 // first we want to fork into the background like all good daemons should
12 //$pid = pcntl_fork();
13
14 // uncomment this bit and comment the fork above to stop it going into the background
15 $pid = 0;
16
17 if($pid == -1) {
18         
19 } else if($pid) {
20         // i am the parent, i shall leave
21         echo "i am a parent, i leave\n";
22         exit(0);
23 } else {
24         // here is where i need to swithc to TCP network protocol stuff
25         // i must bind 127.0.0.1 though.
26         // what i want to happen is this:
27         // 1) server receives connection
28         // 2) server forks off process to process connection
29         // 3) main server continues.
30         // a forked process thingy should be fully self contained and capable of dealing
31         // with "problems", i.e. the parent doesnt want to have to clean up children
32         global $MSG_QUEUE_KEY_ID_SERVER, $MSG_QUEUE_KEY_ID_CLIENT;
33         
34         $cl_queue = msg_get_queue($MSG_QUEUE_KEY_ID_CLIENT, 0666 | 'IPC_CREAT');
35         $sr_queue = msg_get_queue($MSG_QUEUE_KEY_ID_SERVER, 0666 | 'IPC_CREAT');
36
37         $myga = new gaasGA();
38         global $myga;
39         
40         
41         while(true) {
42                 msg_receive($sr_queue, 0, $msg_type, 16384, $msg);
43                 echo "got message of type $msg_type\n";
44                 switch($msg_type) {
45                         case MSG_GET_RADIUS_CLIENTS:
46                                 $sql = "select * from radclients";
47                                 $dbo = getDatabase();
48                                 $res = $dbo->query($sql);
49                                 $clients = "";
50                                 $i=0;
51                                 foreach($res as $row) {
52                                         //              $sql = 'CREATE TABLE "radclients" ("rad_id" INTEGER PRIMARY KEY AUTOINCREMENT,"rad_name" TEXT, "rad_ip" TEXT, "rad_secret" TEXT, "rad_desc" TEXT);';
53                                         $clients[$i]["name"] = $row["rad_name"];
54                                         $clients[$i]["ip"] = $row["rad_ip"];
55                                         $clients[$i]["secret"] = $row["rad_secret"];
56                                         $clients[$i]["desc"] = $row["rad_desc"];
57                                         $i++;
58                                 }
59                                 msg_send($cl_queue, MSG_GET_RADIUS_CLIENTS, $clients);
60                                 break;
61                         case MSG_REMOVE_RADIUS_CLIENT:
62                                 // it should send us a client by rad_name - doesnt work yet
63                                 $client = $msg["clientname"];
64                                 $sql = "delete from radclients where rad_name='$client'";
65                                 $dbo = getDatabase();
66                                 $res = $dbo->query($sql);
67                                 updateRadius();
68                                 msg_send($cl_queue, MSG_REMOVE_RADIUS_CLIENT, true);
69                                 break;
70                         case MSG_ADD_RADIUS_CLIENT:
71                                 echo "in addradclient\n";
72                                 $client = $msg["clientname"];
73                                 $clientsecret = $msg["clientsecret"];
74                                 $clientip = $msg["clientip"];
75                                 $clientdesc = $msg["clientdescription"];
76                                 $dbo = getDatabase();
77                                 
78                                 // check for existing clients with same name
79                                 $sql = "select * from radclients where rad_name='$client'";
80                                 echo "doing select, $sql\n";
81                                 $res = $dbo->query($sql);
82                                 if($res->fetchColumn() > 0) {
83                                         msg_send($cl_queue, MSG_ADD_RADIUS_CLIENT, "name");
84                                                 
85                                 } else {
86                                         // check for existing clients with same ip
87                                         $sql = "select * from radclients where rad_ip='$clientip'";
88                                         $res = $dbo->query($sql);
89                                         echo "doing select, $sql\n";
90                                         if($res->fetchColumn() > 0) {
91                                                 msg_send($cl_queue, MSG_ADD_RADIUS_CLIENT, "ip");
92                                                                 
93                                         } else {
94                                                 $sql = "insert into radclients values (NULL, '$client', '$clientip', '$clientsecret', '$clientdesc')";
95                                                 $res = $dbo->query($sql);
96                                                 updateRadius();
97                                                 msg_send($cl_queue, MSG_ADD_RADIUS_CLIENT, true);
98                                                 break;
99                                         }
100                                 }
101                                 break;
102                         case MSG_DELETE_USER_TOKEN:
103                                 $username = $msg["username"];
104                                 
105                                 $sql = "select users_otk from users where users_username='$username'";
106                                 $dbo = getDatabase();
107                                 $res = $dbo->query($sql);
108                                 $otkid = "";
109                                 foreach($res as $row) {
110                                         $otkid = $row["users_otk"];
111                                 }
112                                 if($otkid!="") {
113                                         global $BASE_DIR;
114                                         unlink("$BASE_DIR/authserver/authd/otks/$otkid.png");
115                                 }
116                                 
117                                 $sql = "update users set users_tokendata='',users_otk='' where users_username='$username'";
118                                 $dbo = getDatabase();
119                                 $res = $dbo->query($sql);
120                                 
121                                 msg_send($cl_queue, MSG_DELETE_USER_TOKEN, true);
122                                 break;
123                         case MSG_AUTH_USER_TOKEN:
124                                 echo "Call to auth user token\n";
125                                 // minimal checking, we leav it up to authenticateUser to do the real
126                                 // checking
127                                 if(!isset($msg["username"])) $msg["username"] = "";
128                                 if(!isset($msg["passcode"])) $msg["passcode"] = "";
129                                 $username = $msg["username"];
130                                 $passcode = $msg["passcode"];
131                                 global $myga;
132                                 $authval = $myga->authenticateUser($username, $passcode);
133                                 msg_send($cl_queue, MSG_AUTH_USER_TOKEN, $authval);
134                                 break;
135                         case MSG_GET_OTK_ID:
136                                 if(!isset($msg["username"])) {
137                                         msg_send($cl_queue, MSG_GET_OTK_ID, false);
138                                 } else {
139                                         $username = $msg["username"];
140                                         $sql = "select users_otk from users where users_username='$username'";
141                                         $dbo = getDatabase();
142                                         $res = $dbo->query($sql);
143                                         $otkid = "";
144                                         foreach($res as $row) {
145                                                 $otkid = $row["users_otk"];
146                                         }
147                                         
148                                         if($otkid == "") {
149                                                 msg_send($cl_queue, MSG_GET_OTK_ID, false);
150                                         } else {
151                                                 msg_send($cl_queue, MSG_GET_OTK_ID, $otkid);
152                                         }
153                                 }
154                                 break;
155                         case MSG_GET_OTK_PNG:
156                                 if(!isset($msg["otk"])) {
157                                         msg_send($cl_queue, MSG_GET_OTK_PNG, false);
158                                 } else {
159                                         $otk = $msg["otk"];
160                                         $sql = "select users_username from users where users_otk='$otk'";
161                                         $dbo = getDatabase();
162                                         $res = $dbo->query($sql);
163                                         $username = "";
164                                         foreach($res as $row) {
165                                                 $username = $row["users_username"];
166                                         }
167                                         
168                                         if($username == "") {
169                                                 msg_send($cl_queue, MSG_GET_OTK_PNG, false);
170                                         } else if($username != $msg["username"]) {
171                                                 msg_send($cl_queue, MSG_GET_OTK_PNG, false);
172                                         } else {
173                                                 global $BASE_DIR;
174                                                 $hand = fopen("$BASE_DIR/authserver/authd/otks/$otk.png", "rb");
175                                                 $data = fread($hand, filesize("$BASE_DIR/authserver/authd/otks/$otk.png"));
176                                                 fclose($hand);
177                                                 unlink("$BASE_DIR/authserver/authd/otks/$otk.png");
178                                                 $sql = "update users set users_otk='' where users_username='$username'";
179                                                 $dbo->query($sql);
180                                                 error_log("senting otk, fsize: ".filesize("$BASE_DIR/authserver/authd/otks/$otk.png")." $otk ");
181                                                 msg_send($cl_queue, MSG_GET_OTK_PNG, $data);
182                                         }
183                                 }
184                                 
185                                 break;
186                         case MSG_SYNC_TOKEN:
187                                 if(!isset($msg["username"])) {
188                                         msg_send($cl_queue, MSG_SYNC_TOKEN, false);
189                                 } else {
190                                         $tokenone = $msg["tokenone"];
191                                         $tokentwo = $msg["tokentwo"];
192                                         
193                                         msg_send($cl_queue, MSG_SYNC_TOKEN, $myga->resyncCode($msg["username"], $tokenone, $tokentwo));
194                                 }
195                                 
196                                 break;
197                         case MSG_GET_TOKEN_TYPE:
198                                 if(!isset($msg["username"])) {
199                                         msg_send($cl_queue, MSG_GET_TOKEN_TYPE, false);
200                                 } else {
201                                         msg_send($cl_queue, MSG_GET_TOKEN_TYPE, $myga->getTokenType($msg["username"]));
202                                 }
203                                 break;
204                         case MSG_ADD_USER_TOKEN:
205                                 echo "Call to add user token\n";
206                                 if(!isset($msg["username"])) {
207                                         msg_send($cl_queue, MSG_ADD_USER_TOKEN, false); 
208                                 } else {
209                                         global $BASE_DIR;
210                                         $username = $msg["username"];
211                                         $tokentype="TOTP";
212                                         if(isset($msg["tokentype"])) {
213                                                 $tokentype=$msg["tokentype"];
214                                         }
215                                         $hexkey = "";
216                                         if(isset($msg["hexkey"])) {
217                                                 $hexkey = $msg["hexkey"];
218                                         }
219                                         global $myga;
220                                         $myga->setUser($username, $tokentype, "", $hexkey);
221                                         
222                                         $url = $myga->createUrl($username);
223                                         echo "Url was: $url\n";
224                                         if(!file_exists("$BASE_DIR/authserver/authd/otks")) mkdir("$BASE_DIR/authserver/authd/otks");
225                                         $otk = generateRandomString();
226                                         system("qrencode -o $BASE_DIR/authserver/authd/otks/$otk.png '$url'");
227                                         
228                                         $sql = "update users set users_otk='$otk' where users_username='$username'";
229                                         $dbo = getDatabase();
230                                         $res = $dbo->query($sql);
231                                         
232                                         msg_send($cl_queue, MSG_ADD_USER_TOKEN, true);
233                                 }
234                                 break;
235                         case MSG_DELETE_USER:
236                                 echo "Call to del user\n";
237                                 if(!isset($msg["username"])) {
238                                         msg_send($cl_queue, MSG_DELETE_USER, false);    
239                                 } else {
240                                         $username = $msg["username"];                           
241                                         global $myga;
242
243                                         $sql = "select users_otk from users where users_username='$username'";
244                                         $dbo = getDatabase();
245                                         $res = $dbo->query($sql);
246                                         $otkid = "";
247                                         foreach($res as $row) {
248                                                 $otkid = $row["users_otk"];
249                                         }
250                                         if($otkid!="") {
251                                                 unlink("otks/$otkid.png");
252                                         }
253                                         
254
255                                         $sql = "delete from users where users_username='$username'";
256                                         $dbo = getDatabase();
257                                         $dbo->query($sql);
258
259                                         msg_send($cl_queue, MSG_DELETE_USER, true);
260                                 }
261                                 break;
262                         case MSG_AUTH_USER_PASSWORD:
263                                 // TODO
264                                 echo "Call to auth user pass\n";
265                                 if(!isset($msg["username"])) {
266                                         msg_send($cl_queue, MSG_AUTH_USER_PASSWORD, false);
267                                         break;
268                                 }
269                                 if(!isset($msg["password"])) {
270                                         msg_send($cl_queue, MSG_AUTH_USER_PASSWORD, false);
271                                         break;
272                                 }
273                                 
274                                 $username = $msg["username"];
275                                 $password = $msg["password"];
276                                 $sql = "select users_password from users where users_username='$username'";
277                                 $dbo = getDatabase();
278                                 $res = $dbo->query($sql);
279                                 $pass = "";
280                                 foreach($res as $row) {
281                                         $pass = $row["users_password"];
282                                 }
283                                 
284                                 // TODO now do auth
285                                 $ourpass = hash('sha512', $password);
286                                 echo "ourpass: $ourpass\nourhash: $pass\n";
287                                 if($ourpass == $pass) {
288                                         msg_send($cl_queue, MSG_AUTH_USER_PASSWORD, true);
289                                         
290                                 } else {
291                                         msg_send($cl_queue, MSG_AUTH_USER_PASSWORD, false);
292                                         
293                                 }
294                                 
295                                 break;
296                         case MSG_SET_USER_PASSWORD:
297                                 echo "how on earth is that happening Call to set user pass, wtf?\n";
298                                 // TODO
299                                 print_r($msg);
300                                 if(!isset($msg["username"])) {
301                                         msg_send($cl_queue, MSG_SET_USER_PASSWORD, false);
302                                         echo "in break 1\n";
303                                         break;
304                                 }
305                                 if(!isset($msg["password"])) {
306                                         msg_send($cl_queue, MSG_SET_USER_PASSWORD, false);
307                                         echo "in break 1\n";
308                                         break;
309                                 }
310                                 
311                                 $username = $msg["username"];
312                                 $password = $msg["password"];
313                                 
314                                 echo "would set pass for $username, to $password\n";
315                                 if($password == "") $pass = "";
316                                 else $pass = hash('sha512', $password);
317                                 
318                                 $dbo = getDatabase();
319                                 echo "in set user pass for $username, $pass\n";
320                                 $sql = "update users set users_password='$pass' where users_username='$username'";
321                                 
322                                 $dbo->query($sql);
323
324                                 msg_send($cl_queue, MSG_SET_USER_REALNAME, true);
325                                 
326                                 
327                                 // these are irrelavent yet
328                                 // TODO now set pass
329                                 break;
330                         case MSG_SET_USER_REALNAME:
331                                 echo "Call to set user realname\n";
332                                 // TODO
333                                 if(!isset($msg["username"])) {
334                                         msg_send($cl_queue, MSG_SET_USER_REALNAME, false);
335                                         break;
336                                 }
337                                 if(!isset($msg["realname"])) {
338                                         msg_send($cl_queue, MSG_SET_USER_REALNAME, false);
339                                         break;
340                                 }
341                                 
342                                 $username = $msg["username"];
343                                 $realname = $msg["realname"];
344                                 $sql = "update users set users_realname='$realname' where users_username='$username'";
345                                 $dbo = getDatabase();
346                                 
347                                 $dbo->query($sql);
348
349                                 msg_send($cl_queue, MSG_SET_USER_REALNAME, true);
350                                 
351                                 // TODO now set real name
352                                 break;
353                         case MSG_SET_USER_TOKEN:
354                                 // TODO
355                                 echo "Call to set user token\n";
356                                 if(!isset($msg["username"])) {
357                                         msg_send($cl_queue, MSG_SET_USER_TOKEN, false);
358                                         break;
359                                 }
360                                 if(!isset($msg["tokenstring"])) {
361                                         msg_send($cl_queue, MSG_SET_USER_TOKEN, false);
362                                         break;
363                                 }
364                                 
365                                 global $myga;
366                                 $username = $msg["username"];
367                                 $token = $msg["tokenstring"];
368                                 $return = $myga->setUserKey($username, $token);
369                                 msg_send($cl_queue, MSG_SET_USER_TOKEN, $return);
370                                 
371                                 // TODO now set token 
372                                 break;                  
373                         case MSG_SET_USER_TOKEN_TYPE:
374                                 // TODO
375                                 echo "Call to set user token type\n";
376                                 if(!isset($msg["username"])) {
377                                         msg_send($cl_queue, MSG_SET_USER_TOKEN_TYPE, false);
378                                         break;
379                                 }
380                                 if(!isset($msg["tokentype"])) {
381                                         msg_send($cl_queue, MSG_SET_USER_TOKEN_TYPE, false);
382                                         break;
383                                 }
384                                 
385                                 $username = $msg["username"];
386                                 $tokentype = $msg["tokentype"];
387                                 global $myga;
388                                 msg_send($cl_queue, MSG_SET_USER_TOKEN_TYPE, $myga->setTokenType($username, $tokentype));
389                                 
390                                 // TODO now set token 
391                                 break;
392                         case MSG_GET_USERS:
393                                 // TODO this needs to be better
394                                 $sql = "select * from users order by users_username";
395                                 
396                                 $dbo = getDatabase();
397                                 $res = $dbo->query($sql);
398                                 
399                                 $users = "";
400                                 $i = 0;
401                                 foreach($res as $row) {
402                                         $users[$i]["username"] = $row["users_username"];
403                                         $users[$i]["realname"] = $row["users_realname"];
404                                         if($row["users_password"]!="") {
405                                                 $users[$i]["haspass"] = true;
406                                         } else {
407                                                 $users[$i]["haspass"] = false;
408                                         }
409                                         echo "user: ".$users[$i]["username"]." has tdata: \"".$row["users_tokendata"]."\"\n";
410                                         if($row["users_tokendata"]!="") {
411                                                 $users[$i]["hastoken"] = true;
412                                         } else {
413                                                 $users[$i]["hastoken"] = false;
414                                         }
415                                         
416                                         if($row["users_otk"]!="") {
417                                                 $users[$i]["otk"] = $row["users_otk"];
418                                         } else {
419                                                 $users[$i]["otk"] = "";
420                                         }
421                                         $i++; 
422                                 }
423                                 msg_send($cl_queue, MSG_GET_USERS, $users);
424                                 
425                                 // TODO now set token 
426                                 break;
427                                 
428                 }               
429         }       
430 }
431
432 ?>