Initial TPQ Server code
authorMargaret Wasserman <mrw@painless-security.com>
Tue, 4 Dec 2012 19:08:50 +0000 (14:08 -0500)
committerMargaret Wasserman <mrw@painless-security.com>
Tue, 4 Dec 2012 19:08:50 +0000 (14:08 -0500)
include/tpq.h
tpq/example/tpqc_main.c [moved from tpq/example_tpqc/tpqc_main.c with 82% similarity]
tpq/example/tpqs_main.c [new file with mode: 0644]
tpq/tpq_common.c [new file with mode: 0644]
tpq/tpqc.c
tpq/tpqs.c

index aa8cb45..0f8d847 100644 (file)
@@ -35,7 +35,7 @@
 #ifndef TPQ_H
 #define TPQ_H
 
-#define TRUST_PATH_QUERY_PORT  12309
+#define TPQ_PORT       12309
 
 typedef struct tpq_name {
   char *buf;
@@ -44,9 +44,9 @@ typedef struct tpq_name {
 
 typedef struct tpq_req {
   struct tpq_req *next_req;
+  int conn;
   TPQ_NAME realm;
   TPQ_NAME coi;
-  int conn;
   void *resp_func;
   void *cookie;
 } TPQ_REQ;
@@ -56,21 +56,27 @@ typedef struct tpq_resp {
   TPQ_NAME coi;
   /* Address of AAA Server */
   /* Credentials */
+  /* Trust Path Used */
 } TPQ_RESP;
 
 typedef struct tpqc_instance {
   TPQ_REQ *req_list;
 } TPQC_INSTANCE;
 
-typedef void (*TPQC_RESP_FUNC)(TPQC_INSTANCE *, TPQ_REQ *, TPQ_RESP *, void *);
+typedef struct tpqs_instance {
+  int req_count;
+} TPQS_INSTANCE;
+
+typedef void (*TPQC_RESP_FUNC)(TPQC_INSTANCE *, TPQ_RESP *, void *);
+typedef int (*TPQS_REQ_FUNC)(TPQS_INSTANCE *, TPQ_REQ *, TPQ_RESP *, void *);
+
+TPQ_NAME *tpq_dup_name (TPQ_NAME *from);
 
 TPQC_INSTANCE *tpqc_create (void);
 int tpqc_open_connection (TPQC_INSTANCE *tpqc, char *server);
-int tpqc_send_request (TPQC_INSTANCE *tpqc, int conn, char *realm, char *coi, TPQC_RESP_FUNC *resp_handler);
+int tpqc_send_request (TPQC_INSTANCE *tpqc, int conn, char *realm, char *coi, TPQC_RESP_FUNC *resp_handler, void *cookie);
 void tpqc_destroy (TPQC_INSTANCE *tpqc);
 
-typedef void TPQS_INSTANCE;
-
 TPQS_INSTANCE *tpqs_create ();
 int tpqs_start (TPQS_INSTANCE *tpqs);
 void tpqs_destroy (TPQS_INSTANCE *tpqs);
similarity index 82%
rename from tpq/example_tpqc/tpqc_main.c
rename to tpq/example/tpqc_main.c
index ec52202..cef89c3 100644 (file)
@@ -43,11 +43,11 @@ void tpqc_print_usage (char *name)
 }
 
 void tpqc_resp_handler (TPQC_INSTANCE * tpqc, 
-                       TPQC_REQ *treq, 
-                       TPQC_RESP *tresp, 
+                       TPQ_RESP *resp, 
                        void *cookie) 
 {
-  printf ("Response received!");
+  printf ("Response received! Realm = %s, COI = %s.\n", resp->realm->buf, 
+         resp->coi->buf);
   tpqc_response_received = 1;
 }
 
@@ -55,7 +55,7 @@ int main (int argc,
          const char *argv[]) 
 {
   TPQC_INSTANCE *tpqc;
-  TPQC_REQ *treq;
+  TPQ_REQ *treq;
   char *server = NULL;
   char *realm = NULL;
   char *coi = NULL;
@@ -75,23 +75,23 @@ int main (int argc,
   tpqc = tpqc_create();
 
   /* Set-up TPQ connection */
-  if (0 == (conn = tpqc_open_connection(tpqc, server))) {
+  if (-1 == (conn = tpqc_open_connection(tpqc, server))) {
     /* Handle error */
+    print("Error in tpqc_open_connection.\n");
+    return 1;
   };
 
-  /* Build and send a TPQ request */
-  if (NULL == (treq = tpqc_build_request(tpqc, conn, realm, coi))) {
-    /* Handle error */
-  }
-
-  if (tpqc_send_request(tpqc, tpqc_resp_handler)) {
+  /* Send a TPQ request */
+  if (rc = tpqc_send_request(tpqc, conn, realm, coi, tpqc_resp_handler, NULL)) {
     /* Handle error */
+    printf("Error in tpqc_send_request, rc = &d.\n", rc);
+    return 1;
   }
     
-  /* Wait for any response */
+  /* Wait for a response */
   while (!tpqc_response_received);
 
-  /* Clean-up the TPQ client instance */
+  /* Clean-up the TPQ client instance, and exit */
   tpqc_release(tpqc);
 
   return 0;
diff --git a/tpq/example/tpqs_main.c b/tpq/example/tpqs_main.c
new file mode 100644 (file)
index 0000000..70784c3
--- /dev/null
@@ -0,0 +1,76 @@
+/*
+ * Copyright (c) 2012, JANET(UK)
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * 3. Neither the name of JANET(UK) nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include <stdio,h>
+#include <tpq.h>
+
+int tpqc_req_handler (TPQS_INSTANCE * tpqs,
+                     TPQ_REQ *req, 
+                     TPQ_RESP *resp,
+                     void *cookie);
+{
+  printf("Request received! Realm = %s, COI = %s\n", req->realm->buf, req->coi->buf);
+  if (tpqs)
+    tpqs->req_count++;
+
+  if ((NULL == (resp->realm = tpq_dup_name(req->realm))) ||
+      (NULL == (resp->coi = tpq_dup_name(req->coi)))) {
+    printf ("Error in tpq_dup_name, not responding.\n");
+    return 1;
+  }
+
+  return 0;
+}
+
+
+int main (int argc, 
+         const char *argv[]) 
+{
+  static TPQS_INSTANCE *tpqs;
+
+  /* Parse command-line arguments */ 
+  if (argc != 1)
+    printf("Unexpected arguments, ignored.\n");
+
+  /* Create a TPQ server instance */
+  tpqc = tpqc_create();
+
+  /* Start-up the server, won't return unless there is an error. */
+
+  /* Clean-up the TPQ server instance */
+  tpqs_release(tpqs);
+
+  return 1;
+}
+
diff --git a/tpq/tpq_common.c b/tpq/tpq_common.c
new file mode 100644 (file)
index 0000000..50fb0e4
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * Copyright (c) 2012, JANET(UK)
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * 3. Neither the name of JANET(UK) nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include <tpq.h>
+
+TPQ_NAME *tpq_dup_name (TPQ_NAME *from) 
+{
+  TPQ_NAME to;
+
+  to.len = from->len;
+  to.buf = malloc(to.len+1);
+  strncpy(from->buf, to.buf, to.len);
+  to.buf[to.len] = 0;          /* NULL terminate for debugging printf()s */
+
+  return &to;
+}
index 085b3d6..a9be6bb 100644 (file)
@@ -47,25 +47,41 @@ TPQC_INSTANCE *tpqc_create ()
   return tpqc;
 }
 
-void tpqc_destroy (TPQC_INSTANCE *tpqc)
+int tpqc_open_connection (TPQC_INSTANCE *tpqc, 
+                         char *server)
 {
-  if (tpqc)
-    free(tpqc);
-}
+  int err = 0;
+  int conn = -1;
+  gss_ctx_id_t gssContext = GSS_C_NO_CONTEXT;
 
-int tpqc_open_connection (TPQC_INSTANCE *tpqc, char *server)
-{
 
+  err = gsscon_connect(server, TPQ_PORT, &conn);
+  if (!err)
+    err = fsscon_active_authenticate(conn, NULL, "trustquery", &gssContext);
+
+  if (!err)
+    return conn;
+  else
+    return -1;
 }
 
 int tpqc_send_request (TPQC_INSTANCE *tpqc, 
                       int conn, 
                       char *realm, 
                       char *coi,
-                      TPQC_RESP_FUNC *resp_handler)
+                      TPQC_RESP_FUNC *resp_handler,
+                      void *cookie)
 
 {
 
 }
 
+void tpqc_destroy (TPQC_INSTANCE *tpqc)
+{
+  if (tpqc)
+    free(tpqc);
+}
+
+
+
 
index 6051d73..1671b35 100644 (file)
  */
 
 #include <stdlib.h>
+#include <gsscon.h>
 
 #include <tpq.h>
 
 TPQS_INSTANCE *tpqs_create ()
 {
   TPQS_INSTANCE *tpqs = 0;
-  tpqs = malloc(sizeof(TPQS_INSTANCE));
+  if (tpqs = malloc(sizeof(TPQS_INSTANCE)))
+    memset(tpqs, 0, sizeof(TPQS_INSTANCE));
   return tpqs;
 }