3 // TODO: SO MUCH ERROR CHECKING ITS NOT FUNNY
5 if(file_exists("config.php")) {
6 require_once("config.php");
8 // config file doesnt exist, we must abort sensibly
11 // get out master library for ga4php
12 require_once("../lib/lib.php");
16 // first we want to fork into the background like all good daemons should
17 //$pid = pcntl_fork();
23 // i am the parent, i shall leave
24 echo "i am a parent, i leave\n";
27 global $MSG_QUEUE_KEY_ID_SERVER, $MSG_QUEUE_KEY_ID_CLIENT;
29 $cl_queue = msg_get_queue($MSG_QUEUE_KEY_ID_CLIENT, 0666 | 'IPC_CREAT');
30 $sr_queue = msg_get_queue($MSG_QUEUE_KEY_ID_SERVER, 0666 | 'IPC_CREAT');
37 msg_receive($sr_queue, 0, $msg_type, 16384, $msg);
39 case MSG_AUTH_USER_TOKEN:
40 echo "Call to auth user token\n";
41 // minimal checking, we leav it up to authenticateUser to do the real
43 if(!isset($msg["username"])) $msg["username"] = "";
44 if(!isset($msg["passcode"])) $msg["passcode"] = "";
45 $username = $msg["username"];
46 $passcode = $msg["passcode"];
48 $authval = $myga->authenticateUser($username, $passcode);
49 msg_send($cl_queue, MSG_AUTH_USER_TOKEN, $authval);
52 if(!isset($msg["username"])) {
53 msg_send($cl_queue, MSG_GET_OTK_ID, false);
55 $username = $msg["username"];
56 $sql = "select users_otk from users where users_username='$username'";
58 $res = $dbo->query($sql);
60 foreach($res as $row) {
61 $otkid = $row["users_otk"];
65 msg_send($cl_queue, MSG_GET_OTK_ID, false);
67 msg_send($cl_queue, MSG_GET_OTK_ID, $otkid);
72 if(!isset($msg["otk"])) {
73 msg_send($cl_queue, MSG_GET_OTK_PNG, false);
76 $sql = "select users_username from users where users_otk='$otk'";
78 $res = $dbo->query($sql);
80 foreach($res as $row) {
81 $username = $row["users_username"];
85 msg_send($cl_queue, MSG_GET_OTK_PNG, false);
86 } else if($username != $msg["username"]) {
87 msg_send($cl_queue, MSG_GET_OTK_PNG, false);
89 $hand = fopen("otks/$otk.png", "rb");
90 $data = fread($hand, filesize("otks/$otk.png"));
92 //unlink("otks/$otk.png");
93 //$sql = "update users set users_otk='' where users_username='$username'";
95 error_log("senting otk, fsize: ".filesize("otks/$otk.png")." $otk ");
96 msg_send($cl_queue, MSG_GET_OTK_PNG, $data);
101 case MSG_ADD_USER_TOKEN:
102 echo "Call to add user token\n";
103 if(!isset($msg["username"])) {
104 msg_send($cl_queue, MSG_ADD_USER_TOKEN, false);
106 $username = $msg["username"];
108 if(isset($msg["tokentype"])) {
112 if(isset($msg["hexkey"])) {
113 $hexkey = $msg["hexkey"];
116 $myga->setUser($username, $tokentype, "", $hexkey);
118 $url = $myga->createUrl($username);
120 $otk = generateRandomString();
121 system("qrencode -o otks/$otk.png $url");
123 $sql = "update users set users_otk='$otk' where users_username='$username'";
124 $dbo = getDatabase();
125 $res = $dbo->query($sql);
127 msg_send($cl_queue, MSG_ADD_USER_TOKEN, true);
130 case MSG_DELETE_USER:
131 echo "Call to del user\n";
132 if(!isset($msg["username"])) {
133 msg_send($cl_queue, MSG_DELETE_USER, false);
135 $username = $msg["username"];
137 $sql = "delete from users where users_username='$username'";
138 $dbo = getDatabase();
141 msg_send($cl_queue, MSG_DELETE_USER, true);
144 case MSG_AUTH_USER_PASSWORD:
146 echo "Call to auth user pass\n";
147 if(!isset($msg["username"])) {
148 msg_send($cl_queue, MSG_AUTH_USER_PASSWORD, false);
151 if(!isset($msg["password"])) {
152 msg_send($cl_queue, MSG_AUTH_USER_PASSWORD, false);
156 $username = $msg["username"];
157 $password = $msg["password"];
158 $sql = "select users_password from users where users_username='$username'";
159 $dbo = getDatabase();
160 $res = $dbo->query($sql);
162 foreach($res as $row) {
163 $pass = $row["users_password"];
167 $ourpass = hash('sha512', $password);
168 echo "ourpass: $ourpass\nourhash: $pass\n";
169 if($ourpass == $pass) {
170 msg_send($cl_queue, MSG_AUTH_USER_PASSWORD, true);
173 msg_send($cl_queue, MSG_AUTH_USER_PASSWORD, false);
178 case MSG_SET_USER_PASSWORD:
179 echo "how on earth is that happening Call to set user pass, wtf?\n";
182 if(!isset($msg["username"])) {
183 msg_send($cl_queue, MSG_SET_USER_PASSWORD, false);
187 if(!isset($msg["password"])) {
188 msg_send($cl_queue, MSG_SET_USER_PASSWORD, false);
193 $username = $msg["username"];
194 $password = $msg["password"];
196 echo "would set pass for $username, to $password\n";
197 if($password == "") $pass = "";
198 else $pass = hash('sha512', $password);
200 $dbo = getDatabase();
201 echo "in set user pass for $username, $pass\n";
202 $sql = "update users set users_password='$pass' where users_username='$username'";
206 msg_send($cl_queue, MSG_SET_USER_REALNAME, true);
209 // these are irrelavent yet
212 case MSG_SET_USER_REALNAME:
213 echo "Call to set user realname\n";
215 if(!isset($msg["username"])) {
216 msg_send($cl_queue, MSG_SET_USER_REALNAME, false);
219 if(!isset($msg["realname"])) {
220 msg_send($cl_queue, MSG_SET_USER_REALNAME, false);
224 $username = $msg["username"];
225 $realname = $msg["realname"];
226 $sql = "update users set users_realname='$realname' where users_username='$username'";
227 $dbo = getDatabase();
231 msg_send($cl_queue, MSG_SET_USER_REALNAME, true);
233 // TODO now set real name
235 case MSG_SET_USER_TOKEN:
237 echo "Call to set user token\n";
238 if(!isset($msg["username"])) {
239 msg_send($cl_queue, MSG_SET_USER_TOKEN, false);
242 if(!isset($msg["tokenstring"])) {
243 msg_send($cl_queue, MSG_SET_USER_TOKEN, false);
248 $username = $msg["username"];
249 $token = $msg["tokenstring"];
250 $return = $myga->setUserKey($username, $token);
251 msg_send($cl_queue, MSG_SET_USER_TOKEN, $return);
253 // TODO now set token
255 case MSG_SET_USER_TOKEN_TYPE:
257 echo "Call to set user token type\n";
258 if(!isset($msg["username"])) {
259 msg_send($cl_queue, MSG_SET_USER_TOKEN_TYPE, false);
262 if(!isset($msg["tokentype"])) {
263 msg_send($cl_queue, MSG_SET_USER_TOKEN_TYPE, false);
267 $username = $msg["username"];
268 $tokentype = $msg["tokentype"];
270 msg_send($cl_queue, MSG_SET_USER_TOKEN_TYPE, $myga->setTokenType($username, $tokentype));
272 // TODO now set token
275 // TODO this needs to be better
276 $sql = "select * from users";
278 $dbo = getDatabase();
279 $res = $dbo->query($sql);
283 foreach($res as $row) {
284 $users[$i]["username"] = $row["users_username"];
285 $users[$i]["realname"] = $row["users_realname"];
286 if($row["users_password"]!="") {
287 $users[$i]["haspass"] = true;
289 $users[$i]["haspass"] = false;
291 echo "user: ".$users[$i]["username"]." has tdata: \"".$row["users_tokendata"]."\"\n";
292 if($row["users_tokendata"]!="") {
293 $users[$i]["hastoken"] = true;
295 $users[$i]["hastoken"] = false;
298 if($row["users_otk"]!="") {
299 $users[$i]["otk"] = $row["users_otk"];
301 $users[$i]["otk"] = "";
305 msg_send($cl_queue, MSG_GET_USERS, $users);
307 // TODO now set token