Change the input/output protocol to expect and emit binary lengths before messages.
authorMark Donnelly <mark@painless-security.com>
Fri, 26 Sep 2014 02:13:20 +0000 (22:13 -0400)
committerMark Donnelly <mark@painless-security.com>
Fri, 26 Sep 2014 02:13:20 +0000 (22:13 -0400)
json_gssapi/json_gssapi_logger.sh [new file with mode: 0644]
json_gssapi/json_protocol.txt
json_gssapi/main.cpp

diff --git a/json_gssapi/json_gssapi_logger.sh b/json_gssapi/json_gssapi_logger.sh
new file mode 100644 (file)
index 0000000..589016f
--- /dev/null
@@ -0,0 +1,5 @@
+#!/bin/bash
+
+mydir=`/usr/bin/dirname $0`
+
+/usr/bin/tee /tmp/json_gssapi_input.txt | $mydir/json_gssapi.exe | /usr/bin/tee /tmp/json_gssapi_outout.txt
index a69fb7f..9a6c30a 100644 (file)
Binary files a/json_gssapi/json_protocol.txt and b/json_gssapi/json_protocol.txt differ
index ec3cb53..eb6beae 100644 (file)
@@ -5,8 +5,10 @@
 #include <exception>
 #include <iostream>
 #include <string>
+#include <unistd.h>
 #include <util_json.h>
 
+
 using std::cin;
 using std::cout;
 using std::endl;
@@ -15,12 +17,16 @@ using std::string;
 
 int main(int argc, char **argv) {
   /* Variables */
-  string input, method;
+  string method, output;
   const char* c_str;
+  char *input;
   JSONObject json;
   JSONObject *result;
   json_error_t jsonErr;
   GSSCommand *cmd;
+  int len;
+  ssize_t readTotal, readThisRound, readRemaining;
+  std::streamsize threeTwoBits = 32 / sizeof(string::size_type);
   
   /* Error checking */
   
@@ -31,11 +37,26 @@ int main(int argc, char **argv) {
   {
     try 
     {
-      cout << "Give me some JSON: ";
-      getline(cin, input);
+      len = 0;
+      readThisRound = 0;
+      while(0 == readThisRound)
+        readThisRound = fread(&len, 4, 1, stdin);
       
-      c_str = input.c_str();
-      JSONObject json = JSONObject::load(c_str, 0, &jsonErr);
+      input = new char[len + 1];
+      readTotal = readThisRound = 0;
+      while (readTotal < len)
+      {
+        readRemaining = len - readTotal;
+        readThisRound = fread( &(input[readTotal]), 1, readRemaining, stdin );
+        if (-1 == readThisRound)
+          readTotal = len;
+        else
+          readTotal += readThisRound;
+      }
+      input[len] = '\0';
+      
+      JSONObject json = JSONObject::load(input, 0, &jsonErr);
+      delete[] input;
       
       // Oh, how I wish I could simply use: switch(json.get("method"))
       c_str = json.get("method").string();
@@ -57,7 +78,10 @@ int main(int argc, char **argv) {
         JSONObject response;
         response.set("method", "unknown");
         response.set("error_message", "Did not find a valid method to execute.");
-        cout << response.dump() << endl;
+       output = response.dump();
+       len = output.length();
+       cout.write((char *)&len, threeTwoBits);
+        cout << output << endl;
       
         continue;
       }
@@ -66,22 +90,31 @@ int main(int argc, char **argv) {
       result = cmd->toJSON();
       delete cmd;
       
-      cout << result->dump( JSON_INDENT(4) ) << endl;
+      output = result->dump();
+      len = output.length();
+      
+      cout.write((char *)&len, threeTwoBits);
+      cout << output;
+      cout.flush();
 
     }
-    catch ( std::bad_alloc )
+    catch ( std::bad_alloc& e )
     {
       JSONObject response;
       JSONObject error;
       response.set("method", "unknown");
       response.set("error_message", "Could not parse the input JSON");
+      response.set("original message", input);
       error.set("text", jsonErr.text);
       error.set("source", jsonErr.source);
       error.set("line", jsonErr.line);
       error.set("column", jsonErr.column);
       error.set("position", jsonErr.position);
       response.set("error", error);
-      cout << response.dump() << endl;
+      output = response.dump();
+      len = output.length();
+      cout.write((char *)&len, threeTwoBits);
+      cout << output << endl;
     }
   } while(1);