Refactor tidc/monc to better share code
[trust_router.git] / tid / tidc.c
index 648107a..94cd98d 100644 (file)
 #include <jansson.h>
 #include <talloc.h>
 
+#include <gsscon.h>
 #include <trust_router/tr_dh.h>
 #include <tid_internal.h>
 #include <tr_msg.h>
-#include <gsscon.h>
 #include <tr_debug.h>
 
 
 int tmp_len = 32;
 
-static int tidc_destructor(void *obj)
-{
-  TIDC_INSTANCE *tidc=talloc_get_type_abort(obj, TIDC_INSTANCE);
-  if (NULL!=tidc) {
-    if (NULL!=tidc->client_dh)
-      tr_destroy_dh_params(tidc->client_dh);
-  }
-  return 0;
-}
-
 /* creates struct in talloc null context */
 TIDC_INSTANCE *tidc_create(void)
 {
   TIDC_INSTANCE *tidc=talloc(NULL, TIDC_INSTANCE);
   if (tidc!=NULL) {
-    tidc->client_dh=NULL;
-    talloc_set_destructor((void *)tidc, tidc_destructor);
+    tidc->gssc = tr_gssc_instance_new(tidc);
+    if (tidc->gssc == NULL) {
+      talloc_free(tidc);
+      return NULL;
+    }
+
+    tidc->gssc->service_name = "trustidentity";
   }
   return tidc;
 }
@@ -72,43 +67,55 @@ void tidc_destroy(TIDC_INSTANCE *tidc)
 }
 
 int tidc_open_connection (TIDC_INSTANCE *tidc, 
-                         const char *server,
-                         unsigned int port,
-                         gss_ctx_id_t *gssctx)
+                          const char *server,
+                          unsigned int port,
+                          gss_ctx_id_t *gssctx)
 {
-  int err = 0;
-  int conn = -1;
   unsigned int use_port = 0;
+  tidc->gssc->gss_ctx = gssctx;
 
   if (0 == port)
     use_port = TID_PORT;
   else
     use_port = port;
 
-  tr_debug("tidc_open_connection: opening tidc connection to %s:%d", server, port);
-  err = gsscon_connect(server, use_port, "trustidentity", &conn, gssctx);
-
-  if (!err)
-    return conn;
+  tr_debug("tidc_open_connection: opening tidc connection to %s:%d", server, use_port);
+  if (0 == tr_gssc_open_connection(tidc->gssc, server, use_port))
+    return tidc->gssc->conn;
   else
     return -1;
 }
 
 int tidc_send_request (TIDC_INSTANCE *tidc,
-                      int conn,
-                      gss_ctx_id_t gssctx,
-                      const char *rp_realm,
-                      const char *realm, 
-                      const char *comm,
-                      TIDC_RESP_FUNC *resp_handler,
-                      void *cookie)
+                       int conn,
+                       gss_ctx_id_t gssctx,
+                       const char *rp_realm,
+                       const char *realm,
+                       const char *comm,
+                       TIDC_RESP_FUNC *resp_handler,
+                       void *cookie)
 {
   TID_REQ *tid_req = NULL;
   int rc;
+  int orig_conn = 0;
+  gss_ctx_id_t *orig_gss_ctx = NULL;
+
+  /* For ABI compatibility, replace the generic GSS client parameters
+   * with the arguments we were passed. */
+  orig_conn = tidc->gssc->conn; /* save to restore later */
+  if (conn != tidc->gssc->conn) {
+    tr_warning("tidc_send_request: WARNING: socket connection FD does not match FD opened by tidc_open_connection()");
+    tidc->gssc->conn = conn;
+  }
+  orig_gss_ctx = tidc->gssc->gss_ctx; /* save to restore later */
+  if (gssctx != *(tidc->gssc->gss_ctx)) {
+    tr_warning("tidc_send_request: WARNING: sending request with different GSS context than used for tidc_open_connection()");
+    *tidc->gssc->gss_ctx = gssctx;
+  }
 
   /* Create and populate a TID req structure */
   if (!(tid_req = tid_req_new()))
-    return -1;
+    goto error;
 
   tid_req->conn = conn;
   tid_req->gssctx = gssctx;
@@ -120,75 +127,45 @@ int tidc_send_request (TIDC_INSTANCE *tidc,
     goto error;
   }
 
-  tid_req->tidc_dh = tr_dh_dup(tidc->client_dh);
+  tid_req->tidc_dh = tr_dh_dup(tidc->gssc->client_dh);
 
   rc = tidc_fwd_request(tidc, tid_req, resp_handler, cookie);
   goto cleanup;
  error:
   rc = -1;
  cleanup:
-  tid_req_free(tid_req);
+  if (tid_req)
+    tid_req_free(tid_req);
+
+  tidc->gssc->conn = orig_conn;
+  tidc->gssc->gss_ctx = orig_gss_ctx;
   return rc;
 }
 
 int tidc_fwd_request(TIDC_INSTANCE *tidc,
                      TID_REQ *tid_req,
-                    TIDC_RESP_FUNC *resp_handler,
+                     TIDC_RESP_FUNC *resp_handler,
                      void *cookie)
 {
-  char *req_buf = NULL;
-  char *resp_buf = NULL;
-  size_t resp_buflen = 0;
+  TALLOC_CTX *tmp_ctx = talloc_new(NULL);
   TR_MSG *msg = NULL;
   TR_MSG *resp_msg = NULL;
-  int err;
   int rc = 0;
 
   /* Create and populate a TID msg structure */
-  if (!(msg = talloc_zero(tid_req, TR_MSG)))
+  if (!(msg = talloc_zero(tmp_ctx, TR_MSG)))
     goto error;
 
   msg->msg_type = TID_REQUEST;
   tr_msg_set_req(msg, tid_req);
 
-  /* store the response function and cookie */
-  // tid_req->resp_func = resp_handler;
-  // tid_req->cookie = cookie;
 
-
-  /* Encode the request into a json string */
-  if (!(req_buf = tr_msg_encode(NULL, msg))) {
-    tr_err("tidc_fwd_request: Error encoding TID request.\n");
-    goto error;
-  }
-
-  tr_debug( "tidc_fwd_request: Sending TID request:\n");
-  tr_debug( "%s\n", req_buf);
+  tr_debug( "tidc_fwd_request: Sending TID request\n");
 
   /* Send the request over the connection */
-  if (err = gsscon_write_encrypted_token (tid_req->conn, tid_req->gssctx, req_buf,
-                                         strlen(req_buf))) {
-    tr_err( "tidc_fwd_request: Error sending request over connection.\n");
+  resp_msg = tr_gssc_exchange_msgs(tmp_ctx, tidc->gssc, msg);
+  if (resp_msg == NULL)
     goto error;
-  }
-
-  /* TBD -- queue request on instance, read resps in separate thread */
-
-  /* Read the response from the connection */
-  /* TBD -- timeout? */
-  if (err = gsscon_read_encrypted_token(tid_req->conn, tid_req->gssctx, &resp_buf, &resp_buflen)) {
-    if (resp_buf)
-      free(resp_buf);
-    goto error;
-  }
-
-  tr_debug( "tidc_fwd_request: Response Received (%u bytes).\n", (unsigned) resp_buflen);
-  tr_debug( "%s\n", resp_buf);
-
-  if (NULL == (resp_msg = tr_msg_decode(resp_buf, resp_buflen))) {
-    tr_err( "tidc_fwd_request: Error decoding response.\n");
-    goto error;
-  }
 
   /* TBD -- Check if this is actually a valid response */
   if (TID_RESPONSE != tr_msg_get_msg_type(resp_msg)) {
@@ -207,25 +184,17 @@ int tidc_fwd_request(TIDC_INSTANCE *tidc,
  error:
   rc = -1;
  cleanup:
-  if (msg)
-    talloc_free(msg);
-  if (req_buf)
-    free(req_buf);
-  if (resp_buf)
-    free(resp_buf);
-  if (resp_msg)
-    tr_msg_free_decoded(resp_msg);
+  talloc_free(tmp_ctx);
   return rc;
 }
 
 
 DH * tidc_get_dh(TIDC_INSTANCE *inst)
 {
-  return inst->client_dh;
+  return tr_gssc_get_dh(inst->gssc);
 }
 
 DH *tidc_set_dh(TIDC_INSTANCE *inst, DH *dh)
 {
-  inst->client_dh = dh;
-  return dh;
+  return tr_gssc_set_dh(inst->gssc, dh);
 }