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);
53 if(!isset($msg["otk"])) {
54 msg_send($cl_queue, MSG_GET_OTK_PNG, false);
57 $sql = "select users_username from users where users_otk='$otk'";
59 $res = $dbo->query($sql);
61 foreach($res as $row) {
62 $username = $row["users_username"];
66 msg_send($cl_queue, MSG_GET_OTK_PNG, false);
68 $hand = fopen("otks/$otk.png", "rb");
69 $data = fread($hand, filesize("otks/$otk.png"));
71 //unlink("otks/$otk.png");
72 //$sql = "update users set users_otk='' where users_username='$username'";
74 error_log("senting otk, fsize: ".filesize("otks/$otk.png")." $otk ");
75 msg_send($cl_queue, MSG_GET_OTK_PNG, $data);
80 case MSG_ADD_USER_TOKEN:
81 echo "Call to add user token\n";
82 if(!isset($msg["username"])) {
83 msg_send($cl_queue, MSG_ADD_USER_TOKEN, false);
85 $username = $msg["username"];
87 if(isset($msg["tokentype"])) {
91 if(isset($msg["hexkey"])) {
92 $hexkey = $msg["hexkey"];
95 $myga->setUser($username, $tokentype, "", $hexkey);
97 $url = $myga->createUrl($username);
99 $otk = generateRandomString();
100 system("qrencode -o otks/$otk.png $url");
102 $sql = "update users set users_otk='$otk' where users_username='$username'";
103 $dbo = getDatabase();
104 $res = $dbo->query($sql);
106 msg_send($cl_queue, MSG_ADD_USER_TOKEN, true);
109 case MSG_DELETE_USER:
110 echo "Call to del user\n";
111 if(!isset($msg["username"])) {
112 msg_send($cl_queue, MSG_DELETE_USER, false);
114 $username = $msg["username"];
116 $sql = "delete from users where users_username='$username'";
117 $dbo = getDatabase();
120 msg_send($cl_queue, MSG_DELETE_USER, true);
123 case MSG_AUTH_USER_PASSWORD:
125 echo "Call to auth user pass\n";
126 if(!isset($msg["username"])) {
127 msg_send($cl_queue, MSG_AUTH_USER_PASSWORD, false);
130 if(!isset($msg["password"])) {
131 msg_send($cl_queue, MSG_AUTH_USER_PASSWORD, false);
135 $username = $msg["username"];
136 $password = $msg["password"];
137 $sql = "select users_password from users where users_username='$username'";
138 $dbo = getDatabase();
139 $res = $dbo->query($sql);
141 foreach($res as $row) {
142 $pass = $row["users_password"];
146 $ourpass = hash('sha512', $password);
147 echo "ourpass: $ourpass\nourhash: $pass\n";
148 if($ourpass == $pass) {
149 msg_send($cl_queue, MSG_AUTH_USER_PASSWORD, true);
152 msg_send($cl_queue, MSG_AUTH_USER_PASSWORD, false);
157 case MSG_SET_USER_PASSWORD:
158 echo "how on earth is that happening Call to set user pass, wtf?\n";
161 if(!isset($msg["username"])) {
162 msg_send($cl_queue, MSG_SET_USER_PASSWORD, false);
166 if(!isset($msg["password"])) {
167 msg_send($cl_queue, MSG_SET_USER_PASSWORD, false);
172 $username = $msg["username"];
173 $password = $msg["password"];
175 echo "would set pass for $username, to $password\n";
176 if($password == "") $pass = "";
177 else $pass = hash('sha512', $password);
179 $dbo = getDatabase();
180 echo "in set user pass for $username, $pass\n";
181 $sql = "update users set users_password='$pass' where users_username='$username'";
185 msg_send($cl_queue, MSG_SET_USER_REALNAME, true);
188 // these are irrelavent yet
191 case MSG_SET_USER_REALNAME:
192 echo "Call to set user realname\n";
194 if(!isset($msg["username"])) {
195 msg_send($cl_queue, MSG_SET_USER_REALNAME, false);
198 if(!isset($msg["realname"])) {
199 msg_send($cl_queue, MSG_SET_USER_REALNAME, false);
203 $username = $msg["username"];
204 $realname = $msg["realname"];
205 $sql = "update users set users_realname='$realname' where users_username='$username'";
206 $dbo = getDatabase();
210 msg_send($cl_queue, MSG_SET_USER_REALNAME, true);
212 // TODO now set real name
214 case MSG_SET_USER_TOKEN:
216 echo "Call to set user token\n";
217 if(!isset($msg["username"])) {
218 msg_send($cl_queue, MSG_SET_USER_TOKEN, false);
221 if(!isset($msg["tokenstring"])) {
222 msg_send($cl_queue, MSG_SET_USER_TOKEN, false);
227 $username = $msg["username"];
228 $token = $msg["tokenstring"];
229 $return = $myga->setUserKey($username, $token);
230 msg_send($cl_queue, MSG_SET_USER_TOKEN, $return);
232 // TODO now set token
234 case MSG_SET_USER_TOKEN_TYPE:
236 echo "Call to set user token type\n";
237 if(!isset($msg["username"])) {
238 msg_send($cl_queue, MSG_SET_USER_TOKEN_TYPE, false);
241 if(!isset($msg["tokentype"])) {
242 msg_send($cl_queue, MSG_SET_USER_TOKEN_TYPE, false);
246 $username = $msg["username"];
247 $tokentype = $msg["tokentype"];
249 msg_send($cl_queue, MSG_SET_USER_TOKEN_TYPE, $myga->setTokenType($username, $tokentype));
251 // TODO now set token
254 // TODO this needs to be better
255 $sql = "select * from users";
257 $dbo = getDatabase();
258 $res = $dbo->query($sql);
262 foreach($res as $row) {
263 $users[$i]["username"] = $row["users_username"];
264 $users[$i]["realname"] = $row["users_realname"];
265 if($row["users_password"]!="") {
266 $users[$i]["haspass"] = true;
268 $users[$i]["haspass"] = false;
270 echo "user: ".$users[$i]["username"]." has tdata: \"".$row["users_tokendata"]."\"\n";
271 if($row["users_tokendata"]!="") {
272 $users[$i]["hastoken"] = true;
274 $users[$i]["hastoken"] = false;
277 if($row["users_otk"]!="") {
278 $users[$i]["otk"] = $row["users_otk"];
280 $users[$i]["otk"] = "";
284 msg_send($cl_queue, MSG_GET_USERS, $users);
286 // TODO now set token