Initial coding of the gaasd new auth server.
authorpaulr <me@pjr.cc>
Mon, 7 Feb 2011 15:24:26 +0000 (02:24 +1100)
committerpaulr <me@pjr.cc>
Mon, 7 Feb 2011 15:24:26 +0000 (02:24 +1100)
doco/pseudo.txt [new file with mode: 0644]
gaas/gaasd/gaasd.php [new file with mode: 0644]
gaas/html/admin.php [new file with mode: 0644]
gaas/html/index.php [new file with mode: 0644]
gaas/html/setup.php [new file with mode: 0644]
gaas/lib/gaasdClient.php [new file with mode: 0644]
gaas/lib/gaasdLib.php [new file with mode: 0644]
gaas/lib/htmlLib.php [new file with mode: 0644]
unittests/gaasdlibconf.php [new file with mode: 0644]

diff --git a/doco/pseudo.txt b/doco/pseudo.txt
new file mode 100644 (file)
index 0000000..7aa375f
--- /dev/null
@@ -0,0 +1,7 @@
+I am the pseudo code for how gaasd will work...
+
+start:
+       am I inited?
+               yes: load from datastore (AD, database, etc)
+               no: tell any request returns "uninited"
+       figure out my datastore.
\ No newline at end of file
diff --git a/gaas/gaasd/gaasd.php b/gaas/gaasd/gaasd.php
new file mode 100644 (file)
index 0000000..cb716a7
--- /dev/null
@@ -0,0 +1,50 @@
+<?php 
+
+// get out master library for gaasd daemon
+require_once("../lib/lib.php");
+
+// first we want to fork into the background like all good daemons should
+//$pid = pcntl_fork();
+
+
+// uncomment this bit and comment the fork above to stop it going into the background
+$pid = 0;
+
+if($pid == -1) {
+       // we failed to fork, oh woe is me
+} else if($pid) {
+       // i am the parent, i shall leave
+       //echo "i am a parent, i leave\n";
+       exit(0);
+} else {
+       // here is where i need to swithc to TCP network protocol stuff
+       // i must bind 127.0.0.1 though.
+       // what i want to happen is this:
+       // 1) server receives connection
+       // 2) server forks off process to process connection
+       // 3) main server continues.
+       // a forked process thingy should be fully self contained and capable of dealing
+       // with "problems", i.e. the parent doesnt want to have to clean up children
+       
+       // Here goes the tcp equivalent
+       global $TCP_PORT_NUMBER;
+       $res = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
+       socket_bind($res, "127.0.0.1", $TCP_PORT_NUMBER);
+       socket_listen($res);
+
+       while(true) {
+               $data_socket = socket_accept($res);
+               // now i fork
+               $forked = pcntl_fork();
+               
+               // TODO: DEAL WITH THIS PROPERLY
+               if($forked == -1) {
+                       echo "Failed to fork\n";
+               } else if(!$forked) {
+                       // I am the child, i process the request
+                       // all the shit down below goes in here
+               }
+       }
+}
+
+?>
\ No newline at end of file
diff --git a/gaas/html/admin.php b/gaas/html/admin.php
new file mode 100644 (file)
index 0000000..cd0ec3c
--- /dev/null
@@ -0,0 +1,9 @@
+<?php
+
+require_once("../lib/htmlLib.php");
+
+if(getInitStatus()) {
+       header("Location: setup.php");
+}
+
+?>
\ No newline at end of file
diff --git a/gaas/html/index.php b/gaas/html/index.php
new file mode 100644 (file)
index 0000000..cd0ec3c
--- /dev/null
@@ -0,0 +1,9 @@
+<?php
+
+require_once("../lib/htmlLib.php");
+
+if(getInitStatus()) {
+       header("Location: setup.php");
+}
+
+?>
\ No newline at end of file
diff --git a/gaas/html/setup.php b/gaas/html/setup.php
new file mode 100644 (file)
index 0000000..62d1a91
--- /dev/null
@@ -0,0 +1,6 @@
+<?php
+
+require_once("../lib/htmlLib.php");
+
+// we are going to use a cookie session for storing configuration info as we "do" it
+?>
\ No newline at end of file
diff --git a/gaas/lib/gaasdClient.php b/gaas/lib/gaasdClient.php
new file mode 100644 (file)
index 0000000..36da285
--- /dev/null
@@ -0,0 +1,5 @@
+<?php 
+
+// I am the gaasd client.. i know all, i see all... I am the "only" way to interact with the gaasd server.
+
+?>
\ No newline at end of file
diff --git a/gaas/lib/gaasdLib.php b/gaas/lib/gaasdLib.php
new file mode 100644 (file)
index 0000000..5cfe52f
--- /dev/null
@@ -0,0 +1,152 @@
+<?php 
+
+// first include the ga4php.php file itself
+$BASE_DIR = realpath(dirname(__FILE__)."/../../");
+global $BASE_DIR;
+
+// messy
+require_once(dirname(__FILE__)."/../../lib/ga4php.php");
+
+
+
+// first we check if our db exists, if not, we're not inited
+$initState = false;
+$backEnd = "";
+global $initState, $backEnd;
+if(file_exists($BASE_DIR."/gaas/gaasd/gaasd.sqlite")) {
+       // then we check if the config vars we need exist in the db
+       $backEndType = confGetVar("backend");
+       
+       if($backEndType == "AD") {
+               $backEnd = "AD";
+               
+               // TODO: we should now check all vars are set, but for now this will surfice
+               $initState = true;
+       }
+
+       if($backEndType == "internal") {
+               $backEnd = "IN";
+               $initState = true;
+       }
+}
+
+// have a gloval db handle so we dont have to keep opening the db all the time
+// this may go away when we consider the implications for a parallel gaasd
+$DB_HANDLE = false;
+global $DB_HANDLE;
+
+
+// a function to create our db
+// TODO: error checking
+function createDB()
+{
+       $dbobject = false;
+       global $BASE_DIR, $initState, $backEnd;
+       try {
+               $dbobject = new PDO("sqlite:$BASE_DIR/gaas/gaasd/gaasd.sqlite");
+       } catch(PDOException $exep) {
+               error_log("execpt on db open");
+               return false;
+       }
+       
+       if($backEnd == "IN") {
+               $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 "config" ("conf_id" INTEGER PRIMARY KEY AUTOINCREMENT,"conf_name" TEXT, "conf_value" 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);
+       $sql = 'CREATE TABLE "hardwaretokens" ("tok_id" INTEGER PRIMARY KEY AUTOINCREMENT,"tok_name" TEXT, "tok_key" TEXT, "tok_type" TEXT);';
+       $dbobject->query($sql);
+       
+       return true;
+}
+
+// a function to get the database
+function getDB()
+{
+       $dbobject = false;
+       global $BASE_DIR, $DB_HANDLE;
+       if($DB_HANDLE != false) return $DB_HANDLE;
+       if(file_exists("$BASE_DIR/gaas/gaasd/gaasd.sqlite")) {
+               try {
+                       $dbobject = new PDO("sqlite:$BASE_DIR/gaas/gaasd/gaasd.sqlite");
+               } catch(PDOException $exep) {
+                       error_log("execpt on db open");
+                       return false;
+               }
+       } else {
+               return false;
+       }
+       
+       $DB_HANDLE = $dbobject;
+       return $dbobject;
+}
+
+
+function confDelVar($varname)
+{
+       $db = getDB();
+       
+       $sql = "delete from config where conf_name='$varname'";
+       $db->query($sql);
+       
+       return true;
+}
+
+// a funciton to deal with Config Vars
+function confGetVar($varname)
+{
+       $db = getDB();
+       
+       $sql = "select conf_value from config where conf_name='$varname'";
+       
+       $result = $db->query($sql);
+       
+       if(!$result) return false;
+       
+       $val = "";
+       foreach($result as $row) {
+               $val = $row["conf_value"];
+       }
+
+       // TOTALLY GUNNA WORK!
+       return $val;
+}
+
+// and a function to put vars
+function confPutVar($varname, $value)
+{
+       $db = getDB();
+       
+       $sql = "delete from config where conf_name='$varname'";
+       $db->query($sql);
+       
+       $sql = "insert into config values (NULL, '$varname','$value')";
+       $db->query($sql);
+       
+       // TODO: do all this better
+       return true;
+}
+
+// now we define our extended class
+class gaasGA extends GoogleAuthenticator
+{
+       
+       function getData($username)
+       {
+       }
+       
+       
+       function putData($username, $data)
+       {
+       }
+       
+       
+       function getUsers()
+       {
+       }
+}
+?>
\ No newline at end of file
diff --git a/gaas/lib/htmlLib.php b/gaas/lib/htmlLib.php
new file mode 100644 (file)
index 0000000..7b75e9a
--- /dev/null
@@ -0,0 +1,5 @@
+<?php
+
+require_once("gaasdClient.php");
+
+?>
\ No newline at end of file
diff --git a/unittests/gaasdlibconf.php b/unittests/gaasdlibconf.php
new file mode 100644 (file)
index 0000000..a86cc0b
--- /dev/null
@@ -0,0 +1,30 @@
+<?php
+
+require_once("../gaas/lib/gaasdLib.php");
+
+$backEnd = "IN";
+createDB();
+
+function grs()
+{
+       $str = "";
+       $strpos = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
+       
+       for($i=0; $i<10; $i++) {
+               $str .= $strpos[rand(0, strlen($strpos)-1)];
+       }
+       
+       return $str;
+}
+
+for($i = 0; $i < 20; $i++) {
+       $grs = grs();
+       confPutVar("val$i", $grs);
+       echo "set $i to $grs\n";
+}
+
+for($i = 0; $i < 20; $i++) {
+       $value = confGetVar("val$i");
+       echo "Value for $i is $value\n";
+}
+?>
\ No newline at end of file