Freeradius users script added
[ga4php.git] / authserver / authd / authd.php
index bd10267..e0f7296 100644 (file)
@@ -1,5 +1,7 @@
 <?php
 
+// TODO: SO MUCH ERROR CHECKING ITS NOT FUNNY
+
 if(file_exists("config.php")) {
        require_once("config.php");
 } else {
@@ -31,12 +33,29 @@ if($pid == -1) {
        global $myga;
        
        
-       print_r($myga);
-       
        while(true) {
                msg_receive($sr_queue, 0, $msg_type, 16384, $msg);
-               print_r($msg);
                switch($msg_type) {
+                       case MSG_DELETE_USER_TOKEN:
+                               $username = $msg["username"];
+                               
+                               $sql = "select users_otk from users where users_username='$username'";
+                               $dbo = getDatabase();
+                               $res = $dbo->query($sql);
+                               $otkid = "";
+                               foreach($res as $row) {
+                                       $otkid = $row["users_otk"];
+                               }
+                               if($otkid!="") {
+                                       unlink("otks/$otkid.png");
+                               }
+                               
+                               $sql = "update users set users_tokendata='',users_otk='' where users_username='$username'";
+                               $dbo = getDatabase();
+                               $res = $dbo->query($sql);
+                               
+                               msg_send($cl_queue, MSG_DELETE_USER_TOKEN, true);
+                               break;
                        case MSG_AUTH_USER_TOKEN:
                                echo "Call to auth user token\n";
                                // minimal checking, we leav it up to authenticateUser to do the real
@@ -49,30 +68,52 @@ if($pid == -1) {
                                $authval = $myga->authenticateUser($username, $passcode);
                                msg_send($cl_queue, MSG_AUTH_USER_TOKEN, $authval);
                                break;
-                               
-                       case MSG_GET_OTK_PNG:
+                       case MSG_GET_OTK_ID:
                                if(!isset($msg["username"])) {
-                                       msg_send($cl_queue, MSG_GET_OTK_PNG, false);
+                                       msg_send($cl_queue, MSG_GET_OTK_ID, false);
                                } else {
                                        $username = $msg["username"];
                                        $sql = "select users_otk from users where users_username='$username'";
                                        $dbo = getDatabase();
                                        $res = $dbo->query($sql);
-                                       $otk = "";
+                                       $otkid = "";
+                                       foreach($res as $row) {
+                                               $otkid = $row["users_otk"];
+                                       }
+                                       
+                                       if($otkid == "") {
+                                               msg_send($cl_queue, MSG_GET_OTK_ID, false);
+                                       } else {
+                                               msg_send($cl_queue, MSG_GET_OTK_ID, $otkid);
+                                       }
+                               }
+                               break;
+                       case MSG_GET_OTK_PNG:
+                               if(!isset($msg["otk"])) {
+                                       msg_send($cl_queue, MSG_GET_OTK_PNG, false);
+                               } else {
+                                       $otk = $msg["otk"];
+                                       $sql = "select users_username from users where users_otk='$otk'";
+                                       $dbo = getDatabase();
+                                       $res = $dbo->query($sql);
+                                       $username = "";
                                        foreach($res as $row) {
-                                               $otk = $row["users_otk"];
+                                               $username = $row["users_username"];
                                        }
                                        
-                                       if($otk == "") {
+                                       if($username == "") {
+                                               msg_send($cl_queue, MSG_GET_OTK_PNG, false);
+                                       } else if($username != $msg["username"]) {
                                                msg_send($cl_queue, MSG_GET_OTK_PNG, false);
                                        } else {
                                                $hand = fopen("otks/$otk.png", "rb");
                                                $data = fread($hand, filesize("otks/$otk.png"));
                                                fclose($hand);
-                                               msg_send($cl_queue, MSG_GET_OTK_PNG, $data);
                                                unlink("otks/$otk.png");
                                                $sql = "update users set users_otk='' where users_username='$username'";
                                                $dbo->query($sql);
+                                               error_log("senting otk, fsize: ".filesize("otks/$otk.png")." $otk ");
+                                               msg_send($cl_queue, MSG_GET_OTK_PNG, $data);
                                        }
                                }
                                
@@ -85,7 +126,7 @@ if($pid == -1) {
                                        $username = $msg["username"];
                                        $tokentype="HOTP";
                                        if(isset($msg["tokentype"])) {
-                                               $tokentype="HOTP";
+                                               $tokentype=$msg["tokentype"];
                                        }
                                        $hexkey = "";
                                        if(isset($msg["hexkey"])) {
@@ -113,6 +154,19 @@ if($pid == -1) {
                                } else {
                                        $username = $msg["username"];                           
                                        global $myga;
+
+                                       $sql = "select users_otk from users where users_username='$username'";
+                                       $dbo = getDatabase();
+                                       $res = $dbo->query($sql);
+                                       $otkid = "";
+                                       foreach($res as $row) {
+                                               $otkid = $row["users_otk"];
+                                       }
+                                       if($otkid!="") {
+                                               unlink("otks/$otkid.png");
+                                       }
+                                       
+
                                        $sql = "delete from users where users_username='$username'";
                                        $dbo = getDatabase();
                                        $dbo->query($sql);
@@ -155,23 +209,29 @@ if($pid == -1) {
                                
                                break;
                        case MSG_SET_USER_PASSWORD:
-                               echo "Call to set user pass\n";
+                               echo "how on earth is that happening Call to set user pass, wtf?\n";
                                // TODO
+                               print_r($msg);
                                if(!isset($msg["username"])) {
                                        msg_send($cl_queue, MSG_SET_USER_PASSWORD, false);
+                                       echo "in break 1\n";
                                        break;
                                }
                                if(!isset($msg["password"])) {
                                        msg_send($cl_queue, MSG_SET_USER_PASSWORD, false);
+                                       echo "in break 1\n";
                                        break;
                                }
                                
                                $username = $msg["username"];
                                $password = $msg["password"];
                                
-                               $pass = hash('sha512', $password);
+                               echo "would set pass for $username, to $password\n";
+                               if($password == "") $pass = "";
+                               else $pass = hash('sha512', $password);
                                
                                $dbo = getDatabase();
+                               echo "in set user pass for $username, $pass\n";
                                $sql = "update users set users_password='$pass' where users_username='$username'";
                                
                                $dbo->query($sql);
@@ -267,6 +327,12 @@ if($pid == -1) {
                                        } else {
                                                $users[$i]["hastoken"] = false;
                                        }
+                                       
+                                       if($row["users_otk"]!="") {
+                                               $users[$i]["otk"] = $row["users_otk"];
+                                       } else {
+                                               $users[$i]["otk"] = "";
+                                       }
                                        $i++; 
                                }
                                msg_send($cl_queue, MSG_GET_USERS, $users);