added the tcp code in, but its not running yet
[ga4php.git] / authserver / lib / lib.php
index efc6d95..bbdc186 100644 (file)
@@ -1,32 +1,95 @@
 <?php
 
-if(!isset($MSG_QUEUE_KEY_ID_SERVER)) $MSG_QUEUE_KEY_ID_SERVER = "189751072";
-if(!isset($MSG_QUEUE_KEY_ID_CLIENT)) $MSG_QUEUE_KEY_ID_CLIENT = "189751073";
+if(!isset($MSG_QUEUE_KEY_ID_SERVER)) $MSG_QUEUE_KEY_ID_SERVER = "189751072"; // i would use ftok, but its crap
+if(!isset($MSG_QUEUE_KEY_ID_CLIENT)) $MSG_QUEUE_KEY_ID_CLIENT = "189751073"; // ftok is not ok!
 global $MSG_QUEUE_KEY_ID_SERVER, $MSG_QUEUE_KEY_ID_CLIENT;
 
-define("MSG_AUTH_USER", 1);
-define("MSG_ADD_USER", 2);
-define("MSG_DELETE_USER", 2);
+if(!isset($TCP_PORT_NUMBER)) $TCP_PORT_NUMBER = 21416;
+global $TCP_PORT_NUMBER;
 
+define("MSG_AUTH_USER_TOKEN", 1);
+define("MSG_ADD_USER_TOKEN", 2);
+define("MSG_DELETE_USER", 3);
+define("MSG_AUTH_USER_PASSWORD", 4);
+define("MSG_SET_USER_PASSWORD", 5);
+define("MSG_SET_USER_REALNAME", 6);
+define("MSG_SET_USER_TOKEN", 7);
+define("MSG_SET_USER_TOKEN_TYPE", 8);
+define("MSG_GET_USERS", 9);
+define("MSG_GET_OTK_PNG", 10);
+define("MSG_GET_OTK_ID", 11);
+define("MSG_DELETE_USER_TOKEN", 12);
+define("MSG_SYNC_TOKEN", 13);
+define("MSG_GET_TOKEN_TYPE", 14);
+define("MSG_GET_RADIUS_CLIENTS", 15);
+define("MSG_REMOVE_RADIUS_CLIENT", 16);
+define("MSG_ADD_RADIUS_CLIENT", 17);
 
-if(file_exists("../../lib/ga4php.php")) require_once("../../lib/ga4php.php");
-if(file_exists("../lib/ga4php.php")) require_once("../lib/ga4php.php");
+// BASE_DIR = 
+// messy
+$BASE_DIR = realpath(dirname(__FILE__)."/../../");
+global $BASE_DIR;
 
-function getDatabase() {
+// messy
+require_once(dirname(__FILE__)."/../../lib/ga4php.php");
+
+function generateRandomString()
+{
+       $str = "";
+       $strpos = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
+       
+       for($i=0; $i<128; $i++) {
+               $str .= $strpos[rand(0, strlen($strpos)-1)];
+       }
+       
+       return $str;
+}
+
+function updateRadius()
+{
+       // this is hardcoded for now.
+       //$clientfile = "/etc/freeradius/clients.conf";
+       $clientfile = "/tmp/clients.conf";
+       $reloadinit = "/etc/init.d/freeradius restart";
+       
+       $db = getDatabase();
+       
+       echo "in updateradius\n";
+       $hand = fopen($clientfile, "w");
+       $sql = "select * from radclients";
+       $res = $db->query($sql);
+       foreach($res as $row) {
+               $cname = $row["rad_name"];
+               $cip = $row["rad_ip"];
+               $csec = $row["rad_secret"];
+               $lines = "client $cname {\nipaddr = $cip\nsecret = $csec\nrequire_message_authenticator = no\n}\n\n";
+               fwrite($hand, $lines);
+       }
+       fclose($hand);
+       // not yet
+       //system($reloadinit);
+}
+
+
+function getDatabase()
+{
        $dbobject = false;
-       if(file_exists("/tmp/gadata.sqlite")) {
+       global $BASE_DIR;
+       if(file_exists("$BASE_DIR/authserver/authd/gaasdata.sqlite")) {
                try {
-                       $dbobject = new PDO("sqlite:/tmp/gadata.sqlite");
+                       $dbobject = new PDO("sqlite:$BASE_DIR/authserver/authd/gaasdata.sqlite");
                } catch(PDOException $exep) {
                        error_log("execpt on db open");
                }
        } else {
                try {
-                       $dbobject = new PDO("sqlite:/tmp/gadata.sqlite");
+                       $dbobject = new PDO("sqlite:$BASE_DIR/authserver/authd/gaasdata.sqlite");
                } catch(PDOException $exep) {
                        error_log("execpt on db open");
                }
-               $sql = 'CREATE TABLE "users" ("users_id" INTEGER PRIMARY KEY AUTOINCREMENT,"users_username" TEXT,"users_tokendata" TEXT);';
+               $sql = 'CREATE TABLE "users" ("users_id" INTEGER PRIMARY KEY AUTOINCREMENT,"users_username" TEXT, "users_realname" TEXT, "users_password" TEXT, "users_tokendata" TEXT, "users_otk" TEXT);';
+               $dbobject->query($sql);
+               $sql = 'CREATE TABLE "radclients" ("rad_id" INTEGER PRIMARY KEY AUTOINCREMENT,"rad_name" TEXT, "rad_ip" TEXT, "rad_secret" TEXT, "rad_desc" TEXT);';
                $dbobject->query($sql);
        }
        
@@ -39,6 +102,7 @@ function closeDatabase($db) {
 
 class gaasGA extends GoogleAuthenticator {
        function getData($username) {
+               echo "called into getdata\n";
                
                // get our database connection
                $dbObject = getDatabase();
@@ -50,14 +114,17 @@ class gaasGA extends GoogleAuthenticator {
                $result = $dbObject->query($sql);
                
                // check the result
+               echo "next1\n";
                if(!$result) return false;
                
                // now just retreieve all the data (there should only be one, but whatever)
+               echo "next2\n";
                $tokendata = false;
                foreach($result as $row) {
                        $tokendata = $row["users_tokendata"];
                }
-               
+
+               echo "next3, $username, $tokendata\n";
                // now we have our data, we just return it. If we got no data
                // we'll just return false by default
                return $tokendata;
@@ -66,33 +133,29 @@ class gaasGA extends GoogleAuthenticator {
        }
        
        
-       // now we need a function for putting the data back into our user table.
-       // in this example, we wont check anything, we'll just overwrite it.
        function putData($username, $data) {
                // get our database connection
                $dbObject = getDatabase();
                
-               // set the sql for updating the data
-               // token data is stored as a base64 encoded string, it should
-               // not need to be escaped in any way prior to storing in a database
-               // but feel free to call your databases "addslashes" (or whatever)
-               // function on $data prior to doing the SQL.
-               $sql = "delete from users where users_username='$username'";
-               $dbObject->query($sql);
-               
-               $sql = "insert into users values (NULL, '$username', '$data')";
-               
+               // we need to check if the user exists, and if so put the data, if not create the data
+               $sql = "select * from users where users_username='$username'";
+               $res = $dbObject->query($sql);
+               if($res->fetchColumn() > 0) {
+                       // do update
+                       error_log("doing userdata update");
+                       $sql = "update users set users_tokendata='$data' where users_username='$username'";
+               } else {
+                       // do insert
+                       error_log("doing user data create");
+                       $sql = "insert into users values (NULL, '$username', '', '', '$data', '')";
+               }
                
-               // now execute the sql and return straight away - you should probably
-               // clean up after yourselves, but im going to assume pdo does this
-               // for us anyway in this exmaple
                if($dbObject->query($sql)) {
                        return true;
                } else {
                        return false;
                }
-               
-               // even simpler!
+
        }
        
        function getUsers() {
@@ -119,4 +182,4 @@ class gaasGA extends GoogleAuthenticator {
        }       
 }
 
-?>
\ No newline at end of file
+?>