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();
18 // i am the parent, i shall leave
19 echo "i am a parent, i leave\n";
22 global $MSG_QUEUE_KEY_ID_SERVER, $MSG_QUEUE_KEY_ID_CLIENT;
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');
32 msg_receive($sr_queue, 0, $msg_type, 16384, $msg);
33 echo "got message of type $msg_type\n";
35 case MSG_GET_RADIUS_CLIENTS:
36 $sql = "select * from radclients";
38 $res = $dbo->query($sql);
41 foreach($res as $row) {
42 // $sql = 'CREATE TABLE "radclients" ("rad_id" INTEGER PRIMARY KEY AUTOINCREMENT,"rad_name" TEXT, "rad_ip" TEXT, "rad_secret" TEXT, "rad_desc" TEXT);';
43 $clients[$i]["name"] = $row["rad_name"];
44 $clients[$i]["ip"] = $row["rad_ip"];
45 $clients[$i]["secret"] = $row["rad_secret"];
46 $clients[$i]["desc"] = $row["rad_desc"];
49 msg_send($cl_queue, MSG_GET_RADIUS_CLIENTS, $clients);
51 case MSG_REMOVE_RADIUS_CLIENT:
52 // it should send us a client by rad_name - doesnt work yet
53 $client = $msg["clientname"];
54 $sql = "delete from radclients where rad_name='$client'";
56 $res = $dbo->query($sql);
58 msg_send($cl_queue, MSG_REMOVE_RADIUS_CLIENT, true);
60 case MSG_ADD_RADIUS_CLIENT:
61 echo "in addradclient\n";
62 $client = $msg["clientname"];
63 $clientsecret = $msg["clientsecret"];
64 $clientip = $msg["clientip"];
65 $clientdesc = $msg["clientdescription"];
68 // check for existing clients with same name
69 $sql = "select * from radclients where rad_name='$client'";
70 echo "doing select, $sql\n";
71 $res = $dbo->query($sql);
72 if($res->fetchColumn() > 0) {
73 msg_send($cl_queue, MSG_ADD_RADIUS_CLIENT, "name");
76 // check for existing clients with same ip
77 $sql = "select * from radclients where rad_ip='$clientip'";
78 $res = $dbo->query($sql);
79 echo "doing select, $sql\n";
80 if($res->fetchColumn() > 0) {
81 msg_send($cl_queue, MSG_ADD_RADIUS_CLIENT, "ip");
84 $sql = "insert into radclients values (NULL, '$client', '$clientip', '$clientsecret', '$clientdesc')";
85 $res = $dbo->query($sql);
87 msg_send($cl_queue, MSG_ADD_RADIUS_CLIENT, true);
92 case MSG_DELETE_USER_TOKEN:
93 $username = $msg["username"];
95 $sql = "select users_otk from users where users_username='$username'";
97 $res = $dbo->query($sql);
99 foreach($res as $row) {
100 $otkid = $row["users_otk"];
104 unlink("$BASE_DIR/authserver/authd/otks/$otkid.png");
107 $sql = "update users set users_tokendata='',users_otk='' where users_username='$username'";
108 $dbo = getDatabase();
109 $res = $dbo->query($sql);
111 msg_send($cl_queue, MSG_DELETE_USER_TOKEN, true);
113 case MSG_AUTH_USER_TOKEN:
114 echo "Call to auth user token\n";
115 // minimal checking, we leav it up to authenticateUser to do the real
117 if(!isset($msg["username"])) $msg["username"] = "";
118 if(!isset($msg["passcode"])) $msg["passcode"] = "";
119 $username = $msg["username"];
120 $passcode = $msg["passcode"];
122 $authval = $myga->authenticateUser($username, $passcode);
123 msg_send($cl_queue, MSG_AUTH_USER_TOKEN, $authval);
126 if(!isset($msg["username"])) {
127 msg_send($cl_queue, MSG_GET_OTK_ID, false);
129 $username = $msg["username"];
130 $sql = "select users_otk from users where users_username='$username'";
131 $dbo = getDatabase();
132 $res = $dbo->query($sql);
134 foreach($res as $row) {
135 $otkid = $row["users_otk"];
139 msg_send($cl_queue, MSG_GET_OTK_ID, false);
141 msg_send($cl_queue, MSG_GET_OTK_ID, $otkid);
145 case MSG_GET_OTK_PNG:
146 if(!isset($msg["otk"])) {
147 msg_send($cl_queue, MSG_GET_OTK_PNG, false);
150 $sql = "select users_username from users where users_otk='$otk'";
151 $dbo = getDatabase();
152 $res = $dbo->query($sql);
154 foreach($res as $row) {
155 $username = $row["users_username"];
158 if($username == "") {
159 msg_send($cl_queue, MSG_GET_OTK_PNG, false);
160 } else if($username != $msg["username"]) {
161 msg_send($cl_queue, MSG_GET_OTK_PNG, false);
164 $hand = fopen("$BASE_DIR/authserver/authd/otks/$otk.png", "rb");
165 $data = fread($hand, filesize("$BASE_DIR/authserver/authd/otks/$otk.png"));
167 unlink("$BASE_DIR/authserver/authd/otks/$otk.png");
168 $sql = "update users set users_otk='' where users_username='$username'";
170 error_log("senting otk, fsize: ".filesize("$BASE_DIR/authserver/authd/otks/$otk.png")." $otk ");
171 msg_send($cl_queue, MSG_GET_OTK_PNG, $data);
177 if(!isset($msg["username"])) {
178 msg_send($cl_queue, MSG_SYNC_TOKEN, false);
180 $tokenone = $msg["tokenone"];
181 $tokentwo = $msg["tokentwo"];
183 msg_send($cl_queue, MSG_SYNC_TOKEN, $myga->resyncCode($msg["username"], $tokenone, $tokentwo));
187 case MSG_GET_TOKEN_TYPE:
188 if(!isset($msg["username"])) {
189 msg_send($cl_queue, MSG_GET_TOKEN_TYPE, false);
191 msg_send($cl_queue, MSG_GET_TOKEN_TYPE, $myga->getTokenType($msg["username"]));
194 case MSG_ADD_USER_TOKEN:
195 echo "Call to add user token\n";
196 if(!isset($msg["username"])) {
197 msg_send($cl_queue, MSG_ADD_USER_TOKEN, false);
200 $username = $msg["username"];
202 if(isset($msg["tokentype"])) {
203 $tokentype=$msg["tokentype"];
206 if(isset($msg["hexkey"])) {
207 $hexkey = $msg["hexkey"];
210 $myga->setUser($username, $tokentype, "", $hexkey);
212 $url = $myga->createUrl($username);
213 if(!file_exists("$BASE_DIR/authserver/authd/otks")) mkdir("$BASE_DIR/authserver/authd/otks");
214 $otk = generateRandomString();
215 system("qrencode -o $BASE_DIR/authserver/authd/otks/$otk.png $url");
217 $sql = "update users set users_otk='$otk' where users_username='$username'";
218 $dbo = getDatabase();
219 $res = $dbo->query($sql);
221 msg_send($cl_queue, MSG_ADD_USER_TOKEN, true);
224 case MSG_DELETE_USER:
225 echo "Call to del user\n";
226 if(!isset($msg["username"])) {
227 msg_send($cl_queue, MSG_DELETE_USER, false);
229 $username = $msg["username"];
232 $sql = "select users_otk from users where users_username='$username'";
233 $dbo = getDatabase();
234 $res = $dbo->query($sql);
236 foreach($res as $row) {
237 $otkid = $row["users_otk"];
240 unlink("otks/$otkid.png");
244 $sql = "delete from users where users_username='$username'";
245 $dbo = getDatabase();
248 msg_send($cl_queue, MSG_DELETE_USER, true);
251 case MSG_AUTH_USER_PASSWORD:
253 echo "Call to auth user pass\n";
254 if(!isset($msg["username"])) {
255 msg_send($cl_queue, MSG_AUTH_USER_PASSWORD, false);
258 if(!isset($msg["password"])) {
259 msg_send($cl_queue, MSG_AUTH_USER_PASSWORD, false);
263 $username = $msg["username"];
264 $password = $msg["password"];
265 $sql = "select users_password from users where users_username='$username'";
266 $dbo = getDatabase();
267 $res = $dbo->query($sql);
269 foreach($res as $row) {
270 $pass = $row["users_password"];
274 $ourpass = hash('sha512', $password);
275 echo "ourpass: $ourpass\nourhash: $pass\n";
276 if($ourpass == $pass) {
277 msg_send($cl_queue, MSG_AUTH_USER_PASSWORD, true);
280 msg_send($cl_queue, MSG_AUTH_USER_PASSWORD, false);
285 case MSG_SET_USER_PASSWORD:
286 echo "how on earth is that happening Call to set user pass, wtf?\n";
289 if(!isset($msg["username"])) {
290 msg_send($cl_queue, MSG_SET_USER_PASSWORD, false);
294 if(!isset($msg["password"])) {
295 msg_send($cl_queue, MSG_SET_USER_PASSWORD, false);
300 $username = $msg["username"];
301 $password = $msg["password"];
303 echo "would set pass for $username, to $password\n";
304 if($password == "") $pass = "";
305 else $pass = hash('sha512', $password);
307 $dbo = getDatabase();
308 echo "in set user pass for $username, $pass\n";
309 $sql = "update users set users_password='$pass' where users_username='$username'";
313 msg_send($cl_queue, MSG_SET_USER_REALNAME, true);
316 // these are irrelavent yet
319 case MSG_SET_USER_REALNAME:
320 echo "Call to set user realname\n";
322 if(!isset($msg["username"])) {
323 msg_send($cl_queue, MSG_SET_USER_REALNAME, false);
326 if(!isset($msg["realname"])) {
327 msg_send($cl_queue, MSG_SET_USER_REALNAME, false);
331 $username = $msg["username"];
332 $realname = $msg["realname"];
333 $sql = "update users set users_realname='$realname' where users_username='$username'";
334 $dbo = getDatabase();
338 msg_send($cl_queue, MSG_SET_USER_REALNAME, true);
340 // TODO now set real name
342 case MSG_SET_USER_TOKEN:
344 echo "Call to set user token\n";
345 if(!isset($msg["username"])) {
346 msg_send($cl_queue, MSG_SET_USER_TOKEN, false);
349 if(!isset($msg["tokenstring"])) {
350 msg_send($cl_queue, MSG_SET_USER_TOKEN, false);
355 $username = $msg["username"];
356 $token = $msg["tokenstring"];
357 $return = $myga->setUserKey($username, $token);
358 msg_send($cl_queue, MSG_SET_USER_TOKEN, $return);
360 // TODO now set token
362 case MSG_SET_USER_TOKEN_TYPE:
364 echo "Call to set user token type\n";
365 if(!isset($msg["username"])) {
366 msg_send($cl_queue, MSG_SET_USER_TOKEN_TYPE, false);
369 if(!isset($msg["tokentype"])) {
370 msg_send($cl_queue, MSG_SET_USER_TOKEN_TYPE, false);
374 $username = $msg["username"];
375 $tokentype = $msg["tokentype"];
377 msg_send($cl_queue, MSG_SET_USER_TOKEN_TYPE, $myga->setTokenType($username, $tokentype));
379 // TODO now set token
382 // TODO this needs to be better
383 $sql = "select * from users";
385 $dbo = getDatabase();
386 $res = $dbo->query($sql);
390 foreach($res as $row) {
391 $users[$i]["username"] = $row["users_username"];
392 $users[$i]["realname"] = $row["users_realname"];
393 if($row["users_password"]!="") {
394 $users[$i]["haspass"] = true;
396 $users[$i]["haspass"] = false;
398 echo "user: ".$users[$i]["username"]." has tdata: \"".$row["users_tokendata"]."\"\n";
399 if($row["users_tokendata"]!="") {
400 $users[$i]["hastoken"] = true;
402 $users[$i]["hastoken"] = false;
405 if($row["users_otk"]!="") {
406 $users[$i]["otk"] = $row["users_otk"];
408 $users[$i]["otk"] = "";
412 msg_send($cl_queue, MSG_GET_USERS, $users);
414 // TODO now set token