radius clients stuff
[ga4php.git] / authserver / authd / authd.php
index 3f723e5..520a39f 100644 (file)
@@ -2,11 +2,6 @@
 
 // TODO: SO MUCH ERROR CHECKING ITS NOT FUNNY
 
-if(file_exists("config.php")) {
-       require_once("config.php");
-} else {
-       // config file doesnt exist, we must abort sensibly
-}
 
 // get out master library for ga4php
 require_once("../lib/lib.php");
@@ -35,7 +30,65 @@ if($pid == -1) {
        
        while(true) {
                msg_receive($sr_queue, 0, $msg_type, 16384, $msg);
+               echo "got message of type $msg_type\n";
                switch($msg_type) {
+                       case MSG_GET_RADIUS_CLIENTS:
+                               $sql = "select * from radclients";
+                               $dbo = getDatabase();
+                               $res = $dbo->query($sql);
+                               $clients = "";
+                               $i=0;
+                               foreach($res as $row) {
+                                       //              $sql = 'CREATE TABLE "radclients" ("rad_id" INTEGER PRIMARY KEY AUTOINCREMENT,"rad_name" TEXT, "rad_ip" TEXT, "rad_secret" TEXT, "rad_desc" TEXT);';
+                                       $clients[$i]["name"] = $row["rad_name"];
+                                       $clients[$i]["ip"] = $row["rad_ip"];
+                                       $clients[$i]["secret"] = $row["rad_secret"];
+                                       $clients[$i]["desc"] = $row["rad_desc"];
+                               }
+                               msg_send($cl_queue, MSG_GET_RADIUS_CLIENTS, $clients);
+                               break;
+                       case MSG_REMOVE_RADIUS_CLIENT:
+                               // it should send us a client by rad_name - doesnt work yet
+                               $client = $msg["clientname"];
+                               $sql = "delete from radclients where rad_name='$client'";
+                               $dbo = getDatabase();
+                               $res = $dbo->query($sql);
+                               updateRadius();
+                               msg_send($cl_queue, MSG_REMOVE_RADIUS_CLIENT, true);
+                               break;
+                       case MSG_ADD_RADIUS_CLIENT:
+                               echo "in addradclient\n";
+                               $client = $msg["clientname"];
+                               $clientsecret = $msg["clientsecret"];
+                               $clientip = $msg["clientip"];
+                               $clientdesc = $msg["clientdescription"];
+                               $sql = "insert into radclients values (NULL, '$client', '$clientip', '$clientsecret', '$clientdesc')";
+                               $dbo = getDatabase();
+                               $res = $dbo->query($sql);
+                               updateRadius();
+                               msg_send($cl_queue, MSG_ADD_RADIUS_CLIENT, true);
+                               break;
+                       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!="") {
+                                       global $BASE_DIR;
+                                       unlink("$BASE_DIR/authserver/authd/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
@@ -48,7 +101,26 @@ if($pid == -1) {
                                $authval = $myga->authenticateUser($username, $passcode);
                                msg_send($cl_queue, MSG_AUTH_USER_TOKEN, $authval);
                                break;
-                               
+                       case MSG_GET_OTK_ID:
+                               if(!isset($msg["username"])) {
+                                       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);
+                                       $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);
@@ -64,28 +136,50 @@ if($pid == -1) {
                                        
                                        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"));
+                                               global $BASE_DIR;
+                                               $hand = fopen("$BASE_DIR/authserver/authd/otks/$otk.png", "rb");
+                                               $data = fread($hand, filesize("$BASE_DIR/authserver/authd/otks/$otk.png"));
                                                fclose($hand);
-                                               //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 ");
+                                               unlink("$BASE_DIR/authserver/authd/otks/$otk.png");
+                                               $sql = "update users set users_otk='' where users_username='$username'";
+                                               $dbo->query($sql);
+                                               error_log("senting otk, fsize: ".filesize("$BASE_DIR/authserver/authd/otks/$otk.png")." $otk ");
                                                msg_send($cl_queue, MSG_GET_OTK_PNG, $data);
                                        }
                                }
                                
                                break;
+                       case MSG_SYNC_TOKEN:
+                               if(!isset($msg["username"])) {
+                                       msg_send($cl_queue, MSG_SYNC_TOKEN, false);
+                               } else {
+                                       $tokenone = $msg["tokenone"];
+                                       $tokentwo = $msg["tokentwo"];
+                                       
+                                       msg_send($cl_queue, MSG_SYNC_TOKEN, $myga->resyncCode($msg["username"], $tokenone, $tokentwo));
+                               }
+                               
+                               break;
+                       case MSG_GET_TOKEN_TYPE:
+                               if(!isset($msg["username"])) {
+                                       msg_send($cl_queue, MSG_GET_TOKEN_TYPE, false);
+                               } else {
+                                       msg_send($cl_queue, MSG_GET_TOKEN_TYPE, $myga->getTokenType($msg["username"]));
+                               }
+                               break;
                        case MSG_ADD_USER_TOKEN:
                                echo "Call to add user token\n";
                                if(!isset($msg["username"])) {
                                        msg_send($cl_queue, MSG_ADD_USER_TOKEN, false); 
                                } else {
+                                       global $BASE_DIR;
                                        $username = $msg["username"];
-                                       $tokentype="HOTP";
+                                       $tokentype="TOTP";
                                        if(isset($msg["tokentype"])) {
-                                               $tokentype="HOTP";
+                                               $tokentype=$msg["tokentype"];
                                        }
                                        $hexkey = "";
                                        if(isset($msg["hexkey"])) {
@@ -95,9 +189,9 @@ if($pid == -1) {
                                        $myga->setUser($username, $tokentype, "", $hexkey);
                                        
                                        $url = $myga->createUrl($username);
-                                       mkdir("otks");
+                                       if(!file_exists("$BASE_DIR/authserver/authd/otks")) mkdir("$BASE_DIR/authserver/authd/otks");
                                        $otk = generateRandomString();
-                                       system("qrencode -o otks/$otk.png $url");
+                                       system("qrencode -o $BASE_DIR/authserver/authd/otks/$otk.png $url");
                                        
                                        $sql = "update users set users_otk='$otk' where users_username='$username'";
                                        $dbo = getDatabase();
@@ -113,6 +207,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);