lots of coded stuff, the beginnings of the server/client comms bits
authorpaulr <me@pjr.cc>
Tue, 8 Feb 2011 05:43:30 +0000 (16:43 +1100)
committerpaulr <me@pjr.cc>
Tue, 8 Feb 2011 05:43:30 +0000 (16:43 +1100)
doco/pseudo.txt
gaas/gaasd/gaasclient.php [new file with mode: 0644]
gaas/gaasd/gaasd.php
gaas/lib/gaasClientMessages.php [new file with mode: 0644]
gaas/lib/gaasdClient.php
gaas/lib/gaasdLib.php
gaas/lib/gaasdMessages.php [new file with mode: 0644]
gaas/lib/globalLib.php [new file with mode: 0644]

index 7aa375f..4ab05c3 100644 (file)
@@ -4,4 +4,38 @@ 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
+       figure out my datastore.
+
+       
+Setup:
+       gaasd starts up with backend set to none
+       index.php, admin.php redirect to setup.php
+       setup.php asks questions (ad/in)
+       setup.php sends gaasd setup instructions
+       gaasd checks config
+       setup.php redirects to admin.php if setup works
+       
+       
+       
+Plugins, how they work
+so in globalLib you define a message:
+define("MSG_STATUS", 18);
+
+then you define a functional prefix
+$MESSAGES[MSG_STATUS] = "gaasStatus";
+
+then in gaasClientMessages you define gaasStatus_clientsend($params) where $params is an array of parameters in the function call
+this returns a "vals" structure thats sent to "sent" to the server, then gaasStatus_clientrecv(..) which accepts that data back from the server
+
+then in gaasdMessages.php you define gaasStatus_server(...) which defines a method for handling the data at the server side.
+
+In code you then call $class->MSG_STATUS(params);
+
+
+$params in clientsend is the arguments sent via MSG_STATUS([0], [1], ...);
+
+$params is then played with to create a single text variable (such as a serialised data structure) and returned to the gasdclient and sent to the server
+
+server receives what clientsend returns in _server($params) as $params.
+
+server then returns its data via a text variable in the return which is then send back to clientrecv($params) as $params;
\ No newline at end of file
diff --git a/gaas/gaasd/gaasclient.php b/gaas/gaasd/gaasclient.php
new file mode 100644 (file)
index 0000000..8151e17
--- /dev/null
@@ -0,0 +1,9 @@
+<?php
+
+require_once("../lib/gaasdClient.php");
+
+$myga = new GAASClient();
+
+$myga->MSG_STATUS("asdf");
+
+?>
\ No newline at end of file
index cb716a7..9ce58c6 100644 (file)
@@ -1,7 +1,7 @@
 <?php 
 
 // get out master library for gaasd daemon
-require_once("../lib/lib.php");
+require_once("../lib/gaasdLib.php");
 
 // first we want to fork into the background like all good daemons should
 //$pid = pcntl_fork();
@@ -43,8 +43,50 @@ if($pid == -1) {
                } else if(!$forked) {
                        // I am the child, i process the request
                        // all the shit down below goes in here
+                       $recvd = "";
+                       $continue = true;
+                       while($continue) {
+                               $size = socket_recv($data_socket, $recvd_a, 1024, 0);
+                               $recvd .= $recvd_a;
+                               if(preg_match("/.*\:EOD$/", $recvd)) {
+                                       // we have a full string... break out
+                                       $continue = false;
+                                       break;
+                               }
+                       }
+                       
+                       $xps = explode(":", $recvd);
+                       $component =  unserialize(base64_decode($xps[1]));
+                       $msg_type = $component["type"];
+                       $msg = $component["data"];
+
+                       $data_returned = processMessage($msg_type, $msg);
+                       
+                       $d_comp["type"] = $msg_type;
+                       $d_comp["data"] = $data_returned;
+                       
+                       $realdata_returning = "AS:".base64_encode(serialize($d_comp)).":EOD";
+                       
+                       socket_send($data_socket, $realdata_returning, strlen($realdata_returning), 0);
+                       socket_close($data_socket);
                }
        }
 }
 
+function processMessage($msg_type, $msg)
+{
+       global $MESSAGES;
+
+       $function = $MESSAGES[$msg_type]."_server";
+       
+       if(function_exists($function)) {
+               return $function($msg);
+       } else {
+               error_log("Call to undefined function! $function\n");
+               return false;
+       }
+       
+}
+
+
 ?>
\ No newline at end of file
diff --git a/gaas/lib/gaasClientMessages.php b/gaas/lib/gaasClientMessages.php
new file mode 100644 (file)
index 0000000..f401e1d
--- /dev/null
@@ -0,0 +1,44 @@
+<?php
+
+// thie file defines the messages sent too and from the gaas client.
+function gaasStatus_clientsend($params)
+{
+       return $params[0];
+}
+
+function gaasStatus_clientrecv($params)
+{
+       print_r($params);
+       echo "Server status is $params\n";
+}
+
+
+// INIT server message
+// params are:
+// AD: "AD", "user", "pass", "domain", "client def", "admin def"
+// IN: "IN", "user", "pass"
+function gaasInitServer_clientsend($params)
+{
+       $msg["backend"] = $params[0];
+       $msg["user"] = $params[1];
+       $msg["pass"] = $params[2];
+       if($msg["backend"] == "AD") {
+               $msg["domain"] = $params[3];
+               $msg["clientdef"] = $params[4];
+               $msg["admindef"] = $params[4];
+       } else if($msg["backend"] == "IN") {
+               // we dont do anything
+       } else {
+               // invalid backend type
+               return false;
+       }
+       
+       return $msg;
+}
+
+// pretty simple, it either works or doesnt, we just pass on the result
+function gaasInitServer_clientrecv($params)
+{
+       return $params;
+}
+?>
\ No newline at end of file
index 36da285..4226368 100644 (file)
@@ -1,5 +1,95 @@
-<?php 
+<?php
+
+
+require_once("globalLib.php");
+require_once("gaasClientMessages.php");
 
 // I am the gaasd client.. i know all, i see all... I am the "only" way to interact with the gaasd server.
 
+class GAASClient {
+       
+       // the main send/receive functions. Communicates with gaasd
+       // we always expect one send followed by one receive message
+       function sendReceive($message_type, $message)
+       {
+               // yeah... this is totally gunna work
+               global $TCP_PORT_NUMBER;
+               
+               $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
+               $res = socket_connect($socket, "127.0.0.1", $TCP_PORT_NUMBER);
+               if(!$res) {
+                       socket_close($socket);
+                       return false;
+               }
+               
+               $msg["type"] = $message_type;
+               $msg["data"] = $message;
+               
+               $datacomp = base64_encode(serialize($msg));
+               $tosend = "AC:$datacomp:EOD";
+               
+               socket_send($socket, $tosend, strlen($tosend), 0);
+               
+               // get up to one meg of data - this is bad... i can feel this function
+               // hurting alot
+               // TODO FIX THIS - its garbage code... im not really sure how to handle this really
+               // we need to read back as AS:data:EOD - i think it now does.. i hope, tho we need
+               // timeouts now.
+               $recvd = "";
+               $continue = true;
+               while($continue) {
+                       $size = socket_recv($socket, $recvd_a, 1024, 0);
+                       $recvd .= $recvd_a;
+                       if(preg_match("/.*\:EOD$/", $recvd)) {
+                               // we have a full string... break out
+                               $continue = false;
+                               break;
+                       }
+               }
+               
+               
+               // first check we got something that makes sense
+               if(preg_match("/^AS:.*:EOD/", $recvd) < 1) {
+                       socket_close($socket);
+                       // we have a problem jim
+                       return false;
+               }
+               
+               $xps = explode(":", $recvd);
+               
+               $component =  unserialize(base64_decode($xps[1]));
+               
+               if($component["type"] != $message_type) {
+                       // we have a problem jim
+                       socket_close($socket);
+                       return false;
+               }
+               
+               socket_close($socket);
+               
+               return $component["data"];
+       }
+       
+       // this is one thing i love about php... how truely dynamic it can be in very easy to do ways.
+       // im not entirely sure what im doing with this bit yet
+       function __call($func, $params)
+       {
+               // im a little stuck here.
+               //  want messages to be defineable in terms of plugins
+               // but i dont think this is the way to do it
+               global $MESSAGES;
+               $st_defined = constant($func);
+               //echo "func is $st_defined\n";
+               $function_send = $MESSAGES[$st_defined]."_clientsend";
+               $function_recv = $MESSAGES[$st_defined]."_clientrecv";
+               //echo "real function is $function_send, $function_recv\n";
+               
+               if(function_exists($function_send) && function_exists($function_recv)) {
+                       $function_recv($this->sendReceive($st_defined, $function_send($params)));
+               } else {
+                       error_log("Function, $function does not exist!");
+               }
+       }
+}
+
 ?>
\ No newline at end of file
index 5cfe52f..a9fd81d 100644 (file)
@@ -1,14 +1,11 @@
 <?php 
 
-// first include the ga4php.php file itself
-$BASE_DIR = realpath(dirname(__FILE__)."/../../");
-global $BASE_DIR;
+require_once("globalLib.php");
+require_once("gaasdMessages.php");
 
 // messy
 require_once(dirname(__FILE__)."/../../lib/ga4php.php");
 
-
-
 // first we check if our db exists, if not, we're not inited
 $initState = false;
 $backEnd = "";
diff --git a/gaas/lib/gaasdMessages.php b/gaas/lib/gaasdMessages.php
new file mode 100644 (file)
index 0000000..e518067
--- /dev/null
@@ -0,0 +1,73 @@
+<?php
+
+// this file defines all the messages used by gaaasd
+
+// there are only really two status messages at this point - "init" meaning we have no been defined yet
+// and "running" meaning we have been defined
+function gaasStatus_server($messages)
+{
+       global $initState, $backEnd;
+
+       $return = "init";
+       if($initState != false && $backEnd != "") {
+               $return = "running";
+       }
+       
+       return $return;
+}
+
+
+function gaasInitServer_server($msg)
+{
+       global $initState, $backEnd;
+       
+       // here we "init" the server, if we're ad, we attempt to connect to AD and if it all works
+       // we then create the db
+       // $m["backend"] = "AD|IN";
+       // AD expects:
+       // $m["domain"] = "somedomain.com";
+       // $m["user"] = "someuser";
+       // $m["pass"] = "somepassword";
+       // $m["userdef"] = "user definition paramaters";
+       // IN expects
+       // $m["user"] = "someuser";
+       // $m["pass"] = "somepass";
+       
+       if($msg["backend"] == "AD") {
+               $backEnd = "AD";
+               // attempt connect to AD, verify creds
+               $addom = $msg["domain"];
+               $adlogin = $msg["user"];
+               $adpass = $msg["pass"];
+               $adclientdef = $msg["clientdef"];
+               $adadmindef = $msg["admindef"];
+               // now wee test our logins...
+               
+               
+               // then
+               createDB();
+               confSetVal("ad.domain", $addom);
+               confSetVal("ad.user", $adlogin);
+               confSetVal("ad.pass", $adpass);
+               confSetVal("ad.encryptionkey", generateHexString(32));
+               confSetVal("ad.clientdef", $adclientdef);
+               confSetVal("ad.admindef", $adadmindef);
+               
+               $initState = "running";
+               $backEnd = "AD";
+               
+               // and that should be it... i think cept im in a forked erg.. lets assume it works, need pain i do not.
+               
+               return true;
+       } else if($msg["backend"] == "IN") {
+               // this ones simpler
+               $backEnd = "IN";
+               createDB();
+               $initState = "running";
+               // then we need to "create user";
+               return true;
+       } else {
+               return false;
+       }
+}
+?>
\ No newline at end of file
diff --git a/gaas/lib/globalLib.php b/gaas/lib/globalLib.php
new file mode 100644 (file)
index 0000000..89e24b2
--- /dev/null
@@ -0,0 +1,81 @@
+<?php
+
+// the global lib sets alot of global variables, its fairly unexciting
+$BASE_DIR = realpath(dirname(__FILE__)."/../../");
+global $BASE_DIR;
+
+// the tcp port number we use for comms
+$TCP_PORT_NUMBER = 21335;
+global $TCP_PORT_NUMBER;
+
+// the messages structure, used to extend gaas if needed
+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);
+define("MSG_STATUS", 18);
+define("MSG_INIT_SERVER", 19);
+
+// the gaasd call's $MESSAGE[<MSG>]_server() for the server side
+// and $MESSAGE[<msg>]_client() for the client side 
+
+$MESSAGES[MSG_AUTH_USER_TOKEN] = "gaasAuthUserToken";
+$MESSAGES[MSG_ADD_USER_TOKEN] = "gaasAddUserToken";
+$MESSAGES[MSG_DELETE_USER] = "gaasDeleteUser";
+$MESSAGES[MSG_AUTH_USER_PASSWORD] = "gaasAuthUserPass";
+$MESSAGES[MSG_SET_USER_PASSWORD] = "gaasSetUserPass";
+$MESSAGES[MSG_SET_USER_REALNAME] = "gaasSetUserRealName";
+$MESSAGES[MSG_SET_USER_TOKEN] = "gaasSetUserToken";
+$MESSAGES[MSG_SET_USER_TOKEN_TYPE] = "gaasSetUserTokenType";
+$MESSAGES[MSG_GET_USERS] = "gaasGetUsers";
+$MESSAGES[MSG_GET_OTK_PNG] = "gaasGetOTKPng";
+$MESSAGES[MSG_GET_OTK_ID] = "gaasGetOTKID";
+$MESSAGES[MSG_DELETE_USER_TOKEN] = "gaasDeleteUserToken";
+$MESSAGES[MSG_SYNC_TOKEN] = "gaasSyncToken";
+$MESSAGES[MSG_GET_TOKEN_TYPE] = "gaasGetTokenType";
+$MESSAGES[MSG_GET_RADIUS_CLIENTS] = "gaasGetRadiusClients";
+$MESSAGES[MSG_REMOVE_RADIUS_CLIENT] = "gaasRemoveRadiusClient";
+$MESSAGES[MSG_ADD_RADIUS_CLIENT] = "gaasAddRadiusClient";
+$MESSAGES[MSG_STATUS] = "gaasStatus";
+$MESSAGES[MSG_INIT_SERVER] = "gaasInitServer";
+global $MESSAGES;
+
+function generateRandomString($len)
+{
+       $str = "";
+       $strpos = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
+       
+       for($i=0; $i<$len; $i++) {
+               $str .= $strpos[rand(0, strlen($strpos)-1)];
+       }
+       
+       return $str;
+}
+
+function generateHexString($len)
+{
+       $str = "";
+       $strpos = "0123456789ABCDEF";
+       
+       for($i=0; $i<$len; $i++) {
+               $str .= $strpos[rand(0, strlen($strpos)-1)];
+       }
+       
+       return $str;
+}
+
+
+?>
\ No newline at end of file