Send TPQ resoonse from server, receive on client.
authorMargaret Wasserman <mrw@painless-security.com>
Wed, 5 Dec 2012 13:24:55 +0000 (08:24 -0500)
committerMargaret Wasserman <mrw@painless-security.com>
Wed, 5 Dec 2012 13:24:55 +0000 (08:24 -0500)
include/tpq.h
tpq/example/tpqc_main.c
tpq/tpqc.c
tpq/tpqs.c

index 941fd6b..c51958d 100644 (file)
@@ -76,6 +76,11 @@ typedef int (TPQS_REQ_FUNC)(TPQS_INSTANCE *, TPQ_REQ *, TPQ_RESP *, void *);
 TPQ_NAME *tpq_new_name (char *name);
 TPQ_NAME *tpq_dup_name (TPQ_NAME *from);
 
+char *tpq_req_encode(TPQ_REQ *req);
+TPQ_REQ *tpq_req_decode(char *jreq);
+char *tpq_resp_encode(TPQ_REQ *resp);
+TPQ_RESP *tpq_resp_decode(char *jresp);
+
 TPQC_INSTANCE *tpqc_create (void);
 int tpqc_open_connection (TPQC_INSTANCE *tpqc, char *server, gss_ctx_id_t *gssctx);
 int tpqc_send_request (TPQC_INSTANCE *tpqc, int conn, gss_ctx_id_t gssctx, char *realm, char *coi, TPQC_RESP_FUNC *resp_handler, void *cookie);
index 7732144..8eb6ab3 100644 (file)
@@ -49,9 +49,11 @@ void tpqc_resp_handler (TPQC_INSTANCE * tpqc,
                        TPQ_RESP *resp, 
                        void *cookie) 
 {
-  printf ("Response received! Realm = %s, COI = %s.\n", resp->realm->buf, 
-         resp->coi->buf);
+  //  printf ("Response received! Realm = %s, COI = %s.\n", resp->realm->buf, 
+  //     resp->coi->buf);
+  printf ("Response received at handler!\n");
   tpqc_response_received = 1;
+  return;
 }
 
 int main (int argc, 
@@ -89,8 +91,8 @@ int main (int argc,
   };
 
   /* Send a TPQ request */
-  if (rc = tpqc_send_request(tpqc, conn, gssctx, realm, coi, 
-                            &tpqc_resp_handler, NULL)) {
+  if (0 > (rc = tpqc_send_request(tpqc, conn, gssctx, realm, coi, 
+                                 &tpqc_resp_handler, NULL))) {
     /* Handle error */
     printf("Error in tpqc_send_request, rc = %d.\n", rc);
     return 1;
index b31d78f..16cc81e 100644 (file)
@@ -33,6 +33,7 @@
  */
 
 #include <stdlib.h>
+#include <jansson.h>
 
 #include <gsscon.h>
 #include <tpq.h>
@@ -48,16 +49,16 @@ TPQC_INSTANCE *tpqc_create ()
 }
 
 int tpqc_open_connection (TPQC_INSTANCE *tpqc, 
-                         char *server)
+                         char *server,
+                         gss_ctx_id_t *gssctx)
 {
   int err = 0;
   int conn = -1;
-  gss_ctx_id_t gssContext = GSS_C_NO_CONTEXT;
-
 
   err = gsscon_connect(server, TPQ_PORT, &conn);
+
   if (!err)
-    err = gsscon_active_authenticate(conn, NULL, "trustquery", &gssContext);
+    err = gsscon_active_authenticate(conn, NULL, "trustquery", gssctx);
 
   if (!err)
     return conn;
@@ -67,13 +68,79 @@ int tpqc_open_connection (TPQC_INSTANCE *tpqc,
 
 int tpqc_send_request (TPQC_INSTANCE *tpqc, 
                       int conn, 
+                      gss_ctx_id_t gssctx,
                       char *realm, 
                       char *coi,
                       TPQC_RESP_FUNC *resp_handler,
                       void *cookie)
 
 {
+  json_t *jreq;
+  int err;
+  char *req_buf;
+  char *resp_buf;
+  size_t resp_buflen = 0;
+
+  /* Create a json TPQ request */
+  if (NULL == (jreq = json_object())) {
+    fprintf(stderr,"Error creating json object.\n");
+    return -1;
+  }
+
+  if (0 > (err = json_object_set_new(jreq, "type", json_string("tpq_request")))) {
+    fprintf(stderr, "Error adding type to request.\n");
+    return -1;
+  }
+
+  /* Insert realm and coi into the json request */
+  if (0 > (err = json_object_set_new(jreq, "realm", json_string(realm)))) {
+    fprintf(stderr, "Error adding realm to request.\n");
+    return -1;
+  }
+  if (0 > (err = json_object_set_new(jreq, "coi", json_string(coi)))) {
+    fprintf(stderr, "Error adding coi to request.\n");
+    return -1;
+  }
+
+  /* Generate half of a D-H exchange -- TBD */
+  /* Insert D-H information into the request -- TBD */
+
+  /* Encode the json request */
+  if (NULL == (req_buf = json_dumps(jreq, 0))) {
+    fprintf(stderr, "Error encoding json request.\n");
+    return -1;
+  }
+  
+  printf("Encoded request:\n%s\n", req_buf);
+  
+  /* Send the request over the connection */
+  if (err = gsscon_write_encrypted_token (conn, gssctx, req_buf, 
+                                         strlen(req_buf) + 1)) {
+    fprintf(stderr, "Error sending request over connection.\n");
+    return -1;
+  }
+
+  free(req_buf);
+
+  /* read the response from the connection */
+
+  if (err = gsscon_read_encrypted_token(conn, gssctx, &resp_buf, &resp_buflen)) {
+    if (resp_buf)
+      free(resp_buf);
+    return -1;
+  }
+
+  fprintf(stdout, "Response Received, %d bytes.\n", resp_buflen);
+
+  /* Parse response -- TBD */
+
+  /* Call the caller's response function */
+  (*resp_handler)(tpqc, NULL, cookie);
+
+  if (resp_buf)
+    free(resp_buf);
 
+  return 0;
 }
 
 void tpqc_destroy (TPQC_INSTANCE *tpqc)
index d819dfe..f915379 100644 (file)
@@ -93,17 +93,75 @@ static int tpqs_auth_connection (int conn, gss_ctx_id_t *gssctx)
 
 static int tpqs_read_request (int conn, gss_ctx_id_t *gssctx, TPQ_REQ *req)
 {
-  return -1;
+  int err;
+  char *buf;
+  size_t buflen = 0;
+
+  if (err = gsscon_read_encrypted_token(conn, *gssctx, &buf, &buflen)) {
+    if (buf)
+      free(buf);
+    return -1;
+  }
+
+  fprintf(stdout, "Request Received, %d bytes.\n", buflen);
+
+  /* Parse request -- TBD */
+
+  if (buf)
+    free(buf);
+
+  return buflen;
 }
 
 static int tpqs_handle_request (TPQ_REQ *req, TPQ_RESP *resp) 
 {
-  return -1;
+  return 0;
 }
 
 static int tpqs_send_response (int conn, gss_ctx_id_t *gssctx, TPQ_RESP *resp)
 {
-  return -1;
+  json_t *jreq;
+  int err;
+  char *resp_buf;
+
+  /* Create a json TPQ response */
+  if (NULL == (jreq = json_object())) {
+    fprintf(stderr,"Error creating json object.\n");
+    return -1;
+  }
+
+  if (0 > (err = json_object_set_new(jreq, "type", json_string("tpq_response")))) {
+    fprintf(stderr, "Error adding type to response.\n");
+    return -1;
+  }
+  if (0 > (err = json_object_set_new(jreq, "result", json_string("error")))) {
+    fprintf(stderr, "Error adding result to response.\n");
+    return -1;
+  }
+  if (0 > (err = json_object_set_new(jreq, "msg", json_string("No path to realm")))) {
+    fprintf(stderr, "Error adding msg to response.\n");
+    return -1;
+  }
+
+  /* Encode the json response */
+  if (NULL == (resp_buf = json_dumps(jreq, 0))) {
+    fprintf(stderr, "Error encoding json response.\n");
+    return -1;
+  }
+  
+  printf("Encoded response:\n%s\n", resp_buf);
+  
+  /* Send the request over the connection */
+  if (err = gsscon_write_encrypted_token (conn, *gssctx, resp_buf, 
+                                         strlen(resp_buf) + 1)) {
+    fprintf(stderr, "Error sending request over connection.\n");
+    return -1;
+  }
+
+  free(resp_buf);
+
+  return 0;
+
 }
 
 static void tpqs_handle_connection (int conn)
@@ -178,6 +236,7 @@ int tpqs_start (TPQS_INSTANCE *tpqs,
     if (pid == 0) {
       close(listen);
       tpqs_handle_connection(conn);
+      close(conn);
       exit(0);
     } else {
       close(conn);