changed default token type to totp
[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_DELETE_USER_TOKEN:
35                                 $username = $msg["username"];
36                                 
37                                 $sql = "select users_otk from users where users_username='$username'";
38                                 $dbo = getDatabase();
39                                 $res = $dbo->query($sql);
40                                 $otkid = "";
41                                 foreach($res as $row) {
42                                         $otkid = $row["users_otk"];
43                                 }
44                                 if($otkid!="") {
45                                         global $BASE_DIR;
46                                         unlink("$BASE_DIR/authserver/authd/otks/$otkid.png");
47                                 }
48                                 
49                                 $sql = "update users set users_tokendata='',users_otk='' where users_username='$username'";
50                                 $dbo = getDatabase();
51                                 $res = $dbo->query($sql);
52                                 
53                                 msg_send($cl_queue, MSG_DELETE_USER_TOKEN, true);
54                                 break;
55                         case MSG_AUTH_USER_TOKEN:
56                                 echo "Call to auth user token\n";
57                                 // minimal checking, we leav it up to authenticateUser to do the real
58                                 // checking
59                                 if(!isset($msg["username"])) $msg["username"] = "";
60                                 if(!isset($msg["passcode"])) $msg["passcode"] = "";
61                                 $username = $msg["username"];
62                                 $passcode = $msg["passcode"];
63                                 global $myga;
64                                 $authval = $myga->authenticateUser($username, $passcode);
65                                 msg_send($cl_queue, MSG_AUTH_USER_TOKEN, $authval);
66                                 break;
67                         case MSG_GET_OTK_ID:
68                                 if(!isset($msg["username"])) {
69                                         msg_send($cl_queue, MSG_GET_OTK_ID, false);
70                                 } else {
71                                         $username = $msg["username"];
72                                         $sql = "select users_otk from users where users_username='$username'";
73                                         $dbo = getDatabase();
74                                         $res = $dbo->query($sql);
75                                         $otkid = "";
76                                         foreach($res as $row) {
77                                                 $otkid = $row["users_otk"];
78                                         }
79                                         
80                                         if($otkid == "") {
81                                                 msg_send($cl_queue, MSG_GET_OTK_ID, false);
82                                         } else {
83                                                 msg_send($cl_queue, MSG_GET_OTK_ID, $otkid);
84                                         }
85                                 }
86                                 break;
87                         case MSG_GET_OTK_PNG:
88                                 if(!isset($msg["otk"])) {
89                                         msg_send($cl_queue, MSG_GET_OTK_PNG, false);
90                                 } else {
91                                         $otk = $msg["otk"];
92                                         $sql = "select users_username from users where users_otk='$otk'";
93                                         $dbo = getDatabase();
94                                         $res = $dbo->query($sql);
95                                         $username = "";
96                                         foreach($res as $row) {
97                                                 $username = $row["users_username"];
98                                         }
99                                         
100                                         if($username == "") {
101                                                 msg_send($cl_queue, MSG_GET_OTK_PNG, false);
102                                         } else if($username != $msg["username"]) {
103                                                 msg_send($cl_queue, MSG_GET_OTK_PNG, false);
104                                         } else {
105                                                 global $BASE_DIR;
106                                                 $hand = fopen("$BASE_DIR/authserver/authd/otks/$otk.png", "rb");
107                                                 $data = fread($hand, filesize("$BASE_DIR/authserver/authd/otks/$otk.png"));
108                                                 fclose($hand);
109                                                 unlink("$BASE_DIR/authserver/authd/otks/$otk.png");
110                                                 $sql = "update users set users_otk='' where users_username='$username'";
111                                                 $dbo->query($sql);
112                                                 error_log("senting otk, fsize: ".filesize("$BASE_DIR/authserver/authd/otks/$otk.png")." $otk ");
113                                                 msg_send($cl_queue, MSG_GET_OTK_PNG, $data);
114                                         }
115                                 }
116                                 
117                                 break;
118                         case MSG_ADD_USER_TOKEN:
119                                 echo "Call to add user token\n";
120                                 if(!isset($msg["username"])) {
121                                         msg_send($cl_queue, MSG_ADD_USER_TOKEN, false); 
122                                 } else {
123                                         global $BASE_DIR;
124                                         $username = $msg["username"];
125                                         $tokentype="TOTP";
126                                         if(isset($msg["tokentype"])) {
127                                                 $tokentype=$msg["tokentype"];
128                                         }
129                                         $hexkey = "";
130                                         if(isset($msg["hexkey"])) {
131                                                 $hexkey = $msg["hexkey"];
132                                         }
133                                         global $myga;
134                                         $myga->setUser($username, $tokentype, "", $hexkey);
135                                         
136                                         $url = $myga->createUrl($username);
137                                         if(!file_exists("$BASE_DIR/authserver/authd/otks")) mkdir("$BASE_DIR/authserver/authd/otks");
138                                         $otk = generateRandomString();
139                                         system("qrencode -o $BASE_DIR/authserver/authd/otks/$otk.png $url");
140                                         
141                                         $sql = "update users set users_otk='$otk' where users_username='$username'";
142                                         $dbo = getDatabase();
143                                         $res = $dbo->query($sql);
144                                         
145                                         msg_send($cl_queue, MSG_ADD_USER_TOKEN, true);
146                                 }
147                                 break;
148                         case MSG_DELETE_USER:
149                                 echo "Call to del user\n";
150                                 if(!isset($msg["username"])) {
151                                         msg_send($cl_queue, MSG_DELETE_USER, false);    
152                                 } else {
153                                         $username = $msg["username"];                           
154                                         global $myga;
155
156                                         $sql = "select users_otk from users where users_username='$username'";
157                                         $dbo = getDatabase();
158                                         $res = $dbo->query($sql);
159                                         $otkid = "";
160                                         foreach($res as $row) {
161                                                 $otkid = $row["users_otk"];
162                                         }
163                                         if($otkid!="") {
164                                                 unlink("otks/$otkid.png");
165                                         }
166                                         
167
168                                         $sql = "delete from users where users_username='$username'";
169                                         $dbo = getDatabase();
170                                         $dbo->query($sql);
171
172                                         msg_send($cl_queue, MSG_DELETE_USER, true);
173                                 }
174                                 break;
175                         case MSG_AUTH_USER_PASSWORD:
176                                 // TODO
177                                 echo "Call to auth user pass\n";
178                                 if(!isset($msg["username"])) {
179                                         msg_send($cl_queue, MSG_AUTH_USER_PASSWORD, false);
180                                         break;
181                                 }
182                                 if(!isset($msg["password"])) {
183                                         msg_send($cl_queue, MSG_AUTH_USER_PASSWORD, false);
184                                         break;
185                                 }
186                                 
187                                 $username = $msg["username"];
188                                 $password = $msg["password"];
189                                 $sql = "select users_password from users where users_username='$username'";
190                                 $dbo = getDatabase();
191                                 $res = $dbo->query($sql);
192                                 $pass = "";
193                                 foreach($res as $row) {
194                                         $pass = $row["users_password"];
195                                 }
196                                 
197                                 // TODO now do auth
198                                 $ourpass = hash('sha512', $password);
199                                 echo "ourpass: $ourpass\nourhash: $pass\n";
200                                 if($ourpass == $pass) {
201                                         msg_send($cl_queue, MSG_AUTH_USER_PASSWORD, true);
202                                         
203                                 } else {
204                                         msg_send($cl_queue, MSG_AUTH_USER_PASSWORD, false);
205                                         
206                                 }
207                                 
208                                 break;
209                         case MSG_SET_USER_PASSWORD:
210                                 echo "how on earth is that happening Call to set user pass, wtf?\n";
211                                 // TODO
212                                 print_r($msg);
213                                 if(!isset($msg["username"])) {
214                                         msg_send($cl_queue, MSG_SET_USER_PASSWORD, false);
215                                         echo "in break 1\n";
216                                         break;
217                                 }
218                                 if(!isset($msg["password"])) {
219                                         msg_send($cl_queue, MSG_SET_USER_PASSWORD, false);
220                                         echo "in break 1\n";
221                                         break;
222                                 }
223                                 
224                                 $username = $msg["username"];
225                                 $password = $msg["password"];
226                                 
227                                 echo "would set pass for $username, to $password\n";
228                                 if($password == "") $pass = "";
229                                 else $pass = hash('sha512', $password);
230                                 
231                                 $dbo = getDatabase();
232                                 echo "in set user pass for $username, $pass\n";
233                                 $sql = "update users set users_password='$pass' where users_username='$username'";
234                                 
235                                 $dbo->query($sql);
236
237                                 msg_send($cl_queue, MSG_SET_USER_REALNAME, true);
238                                 
239                                 
240                                 // these are irrelavent yet
241                                 // TODO now set pass
242                                 break;
243                         case MSG_SET_USER_REALNAME:
244                                 echo "Call to set user realname\n";
245                                 // TODO
246                                 if(!isset($msg["username"])) {
247                                         msg_send($cl_queue, MSG_SET_USER_REALNAME, false);
248                                         break;
249                                 }
250                                 if(!isset($msg["realname"])) {
251                                         msg_send($cl_queue, MSG_SET_USER_REALNAME, false);
252                                         break;
253                                 }
254                                 
255                                 $username = $msg["username"];
256                                 $realname = $msg["realname"];
257                                 $sql = "update users set users_realname='$realname' where users_username='$username'";
258                                 $dbo = getDatabase();
259                                 
260                                 $dbo->query($sql);
261
262                                 msg_send($cl_queue, MSG_SET_USER_REALNAME, true);
263                                 
264                                 // TODO now set real name
265                                 break;
266                         case MSG_SET_USER_TOKEN:
267                                 // TODO
268                                 echo "Call to set user token\n";
269                                 if(!isset($msg["username"])) {
270                                         msg_send($cl_queue, MSG_SET_USER_TOKEN, false);
271                                         break;
272                                 }
273                                 if(!isset($msg["tokenstring"])) {
274                                         msg_send($cl_queue, MSG_SET_USER_TOKEN, false);
275                                         break;
276                                 }
277                                 
278                                 global $myga;
279                                 $username = $msg["username"];
280                                 $token = $msg["tokenstring"];
281                                 $return = $myga->setUserKey($username, $token);
282                                 msg_send($cl_queue, MSG_SET_USER_TOKEN, $return);
283                                 
284                                 // TODO now set token 
285                                 break;                  
286                         case MSG_SET_USER_TOKEN_TYPE:
287                                 // TODO
288                                 echo "Call to set user token type\n";
289                                 if(!isset($msg["username"])) {
290                                         msg_send($cl_queue, MSG_SET_USER_TOKEN_TYPE, false);
291                                         break;
292                                 }
293                                 if(!isset($msg["tokentype"])) {
294                                         msg_send($cl_queue, MSG_SET_USER_TOKEN_TYPE, false);
295                                         break;
296                                 }
297                                 
298                                 $username = $msg["username"];
299                                 $tokentype = $msg["tokentype"];
300                                 global $myga;
301                                 msg_send($cl_queue, MSG_SET_USER_TOKEN_TYPE, $myga->setTokenType($username, $tokentype));
302                                 
303                                 // TODO now set token 
304                                 break;
305                         case MSG_GET_USERS:
306                                 // TODO this needs to be better
307                                 $sql = "select * from users";
308                                 
309                                 $dbo = getDatabase();
310                                 $res = $dbo->query($sql);
311                                 
312                                 $users = "";
313                                 $i = 0;
314                                 foreach($res as $row) {
315                                         $users[$i]["username"] = $row["users_username"];
316                                         $users[$i]["realname"] = $row["users_realname"];
317                                         if($row["users_password"]!="") {
318                                                 $users[$i]["haspass"] = true;
319                                         } else {
320                                                 $users[$i]["haspass"] = false;
321                                         }
322                                         echo "user: ".$users[$i]["username"]." has tdata: \"".$row["users_tokendata"]."\"\n";
323                                         if($row["users_tokendata"]!="") {
324                                                 $users[$i]["hastoken"] = true;
325                                         } else {
326                                                 $users[$i]["hastoken"] = false;
327                                         }
328                                         
329                                         if($row["users_otk"]!="") {
330                                                 $users[$i]["otk"] = $row["users_otk"];
331                                         } else {
332                                                 $users[$i]["otk"] = "";
333                                         }
334                                         $i++; 
335                                 }
336                                 msg_send($cl_queue, MSG_GET_USERS, $users);
337                                 
338                                 // TODO now set token 
339                                 break;
340                                 
341                 }               
342         }       
343 }
344
345 ?>