3 // TODO: SO MUCH ERROR CHECKING ITS NOT FUNNY
6 // get out master library for ga4php
7 require_once("../lib/lib.php");
11 // first we want to fork into the background like all good daemons should
12 //$pid = pcntl_fork();
14 // uncomment this bit and comment the fork above to stop it going into the background
20 // i am the parent, i shall leave
21 echo "i am a parent, i leave\n";
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;
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');
42 msg_receive($sr_queue, 0, $msg_type, 16384, $msg);
43 echo "got message of type $msg_type\n";
45 case MSG_GET_RADIUS_CLIENTS:
46 $sql = "select * from radclients";
48 $res = $dbo->query($sql);
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"];
59 msg_send($cl_queue, MSG_GET_RADIUS_CLIENTS, $clients);
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'";
66 $res = $dbo->query($sql);
68 msg_send($cl_queue, MSG_REMOVE_RADIUS_CLIENT, true);
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"];
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");
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");
94 $sql = "insert into radclients values (NULL, '$client', '$clientip', '$clientsecret', '$clientdesc')";
95 $res = $dbo->query($sql);
97 msg_send($cl_queue, MSG_ADD_RADIUS_CLIENT, true);
102 case MSG_DELETE_USER_TOKEN:
103 $username = $msg["username"];
105 $sql = "select users_otk from users where users_username='$username'";
106 $dbo = getDatabase();
107 $res = $dbo->query($sql);
109 foreach($res as $row) {
110 $otkid = $row["users_otk"];
114 unlink("$BASE_DIR/authserver/authd/otks/$otkid.png");
117 $sql = "update users set users_tokendata='',users_otk='' where users_username='$username'";
118 $dbo = getDatabase();
119 $res = $dbo->query($sql);
121 msg_send($cl_queue, MSG_DELETE_USER_TOKEN, true);
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
127 if(!isset($msg["username"])) $msg["username"] = "";
128 if(!isset($msg["passcode"])) $msg["passcode"] = "";
129 $username = $msg["username"];
130 $passcode = $msg["passcode"];
132 $authval = $myga->authenticateUser($username, $passcode);
133 msg_send($cl_queue, MSG_AUTH_USER_TOKEN, $authval);
136 if(!isset($msg["username"])) {
137 msg_send($cl_queue, MSG_GET_OTK_ID, false);
139 $username = $msg["username"];
140 $sql = "select users_otk from users where users_username='$username'";
141 $dbo = getDatabase();
142 $res = $dbo->query($sql);
144 foreach($res as $row) {
145 $otkid = $row["users_otk"];
149 msg_send($cl_queue, MSG_GET_OTK_ID, false);
151 msg_send($cl_queue, MSG_GET_OTK_ID, $otkid);
155 case MSG_GET_OTK_PNG:
156 if(!isset($msg["otk"])) {
157 msg_send($cl_queue, MSG_GET_OTK_PNG, false);
160 $sql = "select users_username from users where users_otk='$otk'";
161 $dbo = getDatabase();
162 $res = $dbo->query($sql);
164 foreach($res as $row) {
165 $username = $row["users_username"];
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);
174 $hand = fopen("$BASE_DIR/authserver/authd/otks/$otk.png", "rb");
175 $data = fread($hand, filesize("$BASE_DIR/authserver/authd/otks/$otk.png"));
177 unlink("$BASE_DIR/authserver/authd/otks/$otk.png");
178 $sql = "update users set users_otk='' where users_username='$username'";
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);
187 if(!isset($msg["username"])) {
188 msg_send($cl_queue, MSG_SYNC_TOKEN, false);
190 $tokenone = $msg["tokenone"];
191 $tokentwo = $msg["tokentwo"];
193 msg_send($cl_queue, MSG_SYNC_TOKEN, $myga->resyncCode($msg["username"], $tokenone, $tokentwo));
197 case MSG_GET_TOKEN_TYPE:
198 if(!isset($msg["username"])) {
199 msg_send($cl_queue, MSG_GET_TOKEN_TYPE, false);
201 msg_send($cl_queue, MSG_GET_TOKEN_TYPE, $myga->getTokenType($msg["username"]));
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);
210 $username = $msg["username"];
212 if(isset($msg["tokentype"])) {
213 $tokentype=$msg["tokentype"];
216 if(isset($msg["hexkey"])) {
217 $hexkey = $msg["hexkey"];
220 $myga->setUser($username, $tokentype, "", $hexkey);
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'");
228 $sql = "update users set users_otk='$otk' where users_username='$username'";
229 $dbo = getDatabase();
230 $res = $dbo->query($sql);
232 msg_send($cl_queue, MSG_ADD_USER_TOKEN, true);
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);
240 $username = $msg["username"];
243 $sql = "select users_otk from users where users_username='$username'";
244 $dbo = getDatabase();
245 $res = $dbo->query($sql);
247 foreach($res as $row) {
248 $otkid = $row["users_otk"];
251 unlink("otks/$otkid.png");
255 $sql = "delete from users where users_username='$username'";
256 $dbo = getDatabase();
259 msg_send($cl_queue, MSG_DELETE_USER, true);
262 case MSG_AUTH_USER_PASSWORD:
264 echo "Call to auth user pass\n";
265 if(!isset($msg["username"])) {
266 msg_send($cl_queue, MSG_AUTH_USER_PASSWORD, false);
269 if(!isset($msg["password"])) {
270 msg_send($cl_queue, MSG_AUTH_USER_PASSWORD, false);
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);
280 foreach($res as $row) {
281 $pass = $row["users_password"];
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);
291 msg_send($cl_queue, MSG_AUTH_USER_PASSWORD, false);
296 case MSG_SET_USER_PASSWORD:
297 echo "how on earth is that happening Call to set user pass, wtf?\n";
300 if(!isset($msg["username"])) {
301 msg_send($cl_queue, MSG_SET_USER_PASSWORD, false);
305 if(!isset($msg["password"])) {
306 msg_send($cl_queue, MSG_SET_USER_PASSWORD, false);
311 $username = $msg["username"];
312 $password = $msg["password"];
314 echo "would set pass for $username, to $password\n";
315 if($password == "") $pass = "";
316 else $pass = hash('sha512', $password);
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'";
324 msg_send($cl_queue, MSG_SET_USER_REALNAME, true);
327 // these are irrelavent yet
330 case MSG_SET_USER_REALNAME:
331 echo "Call to set user realname\n";
333 if(!isset($msg["username"])) {
334 msg_send($cl_queue, MSG_SET_USER_REALNAME, false);
337 if(!isset($msg["realname"])) {
338 msg_send($cl_queue, MSG_SET_USER_REALNAME, false);
342 $username = $msg["username"];
343 $realname = $msg["realname"];
344 $sql = "update users set users_realname='$realname' where users_username='$username'";
345 $dbo = getDatabase();
349 msg_send($cl_queue, MSG_SET_USER_REALNAME, true);
351 // TODO now set real name
353 case MSG_SET_USER_TOKEN:
355 echo "Call to set user token\n";
356 if(!isset($msg["username"])) {
357 msg_send($cl_queue, MSG_SET_USER_TOKEN, false);
360 if(!isset($msg["tokenstring"])) {
361 msg_send($cl_queue, MSG_SET_USER_TOKEN, false);
366 $username = $msg["username"];
367 $token = $msg["tokenstring"];
368 $return = $myga->setUserKey($username, $token);
369 msg_send($cl_queue, MSG_SET_USER_TOKEN, $return);
371 // TODO now set token
373 case MSG_SET_USER_TOKEN_TYPE:
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);
380 if(!isset($msg["tokentype"])) {
381 msg_send($cl_queue, MSG_SET_USER_TOKEN_TYPE, false);
385 $username = $msg["username"];
386 $tokentype = $msg["tokentype"];
388 msg_send($cl_queue, MSG_SET_USER_TOKEN_TYPE, $myga->setTokenType($username, $tokentype));
390 // TODO now set token
393 // TODO this needs to be better
394 $sql = "select * from users order by users_username";
396 $dbo = getDatabase();
397 $res = $dbo->query($sql);
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;
407 $users[$i]["haspass"] = false;
409 echo "user: ".$users[$i]["username"]." has tdata: \"".$row["users_tokendata"]."\"\n";
410 if($row["users_tokendata"]!="") {
411 $users[$i]["hastoken"] = true;
413 $users[$i]["hastoken"] = false;
416 if($row["users_otk"]!="") {
417 $users[$i]["otk"] = $row["users_otk"];
419 $users[$i]["otk"] = "";
423 msg_send($cl_queue, MSG_GET_USERS, $users);
425 // TODO now set token