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