Fix typo, reorder methods in tr_aaa_server.c
[trust_router.git] / tid / tidc.c
index 94cd98d..1cff6f0 100644 (file)
 #include <tid_internal.h>
 #include <tr_msg.h>
 #include <tr_debug.h>
+#include <tr_rand_id.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)
 {
@@ -55,8 +67,9 @@ TIDC_INSTANCE *tidc_create(void)
       talloc_free(tidc);
       return NULL;
     }
-
     tidc->gssc->service_name = "trustidentity";
+    tidc->client_dh = NULL;
+    talloc_set_destructor((void *)tidc, tidc_destructor);
   }
   return tidc;
 }
@@ -96,6 +109,7 @@ int tidc_send_request (TIDC_INSTANCE *tidc,
                        void *cookie)
 {
   TID_REQ *tid_req = NULL;
+  char *request_id = NULL;
   int rc;
   int orig_conn = 0;
   gss_ctx_id_t *orig_gss_ctx = NULL;
@@ -127,7 +141,18 @@ int tidc_send_request (TIDC_INSTANCE *tidc,
     goto error;
   }
 
-  tid_req->tidc_dh = tr_dh_dup(tidc->gssc->client_dh);
+  tid_req->tidc_dh = tr_dh_dup(tidc_get_dh(tidc));
+
+  /* generate an ID */
+  request_id = tr_random_id(NULL);
+  if (request_id) {
+    if (tid_req->request_id = tr_new_name(request_id))
+      tr_debug("tidc_send_request: Created TID request ID: %s", request_id);
+    else
+      tr_debug("tidc_send_request: Unable to set request ID, proceeding without one");
+    talloc_free(request_id);
+  } else
+    tr_debug("tidc_send_request: Failed to generate a TID request ID, proceeding without one");
 
   rc = tidc_fwd_request(tidc, tid_req, resp_handler, cookie);
   goto cleanup;
@@ -150,6 +175,7 @@ int tidc_fwd_request(TIDC_INSTANCE *tidc,
   TALLOC_CTX *tmp_ctx = talloc_new(NULL);
   TR_MSG *msg = NULL;
   TR_MSG *resp_msg = NULL;
+  TID_RESP *tid_resp = NULL;
   int rc = 0;
 
   /* Create and populate a TID msg structure */
@@ -168,11 +194,27 @@ int tidc_fwd_request(TIDC_INSTANCE *tidc,
     goto error;
 
   /* TBD -- Check if this is actually a valid response */
-  if (TID_RESPONSE != tr_msg_get_msg_type(resp_msg)) {
+  tid_resp = tr_msg_get_resp(resp_msg);
+  if (tid_resp == NULL) {
     tr_err( "tidc_fwd_request: Error, no response in the response!\n");
     goto error;
   }
 
+  /* Check whether the request IDs matched and warn if not. Do nothing if we don't get
+   * an ID on the return - it is not mandatory to preserve that field. */
+  if (tid_req->request_id) {
+    if ((tid_resp->request_id)
+        && (tr_name_cmp(tid_resp->request_id, tid_req->request_id) != 0)) {
+      /* Requests present but do not match */
+      tr_warning("tidc_fwd_request: Sent request ID %.*s, received response for %.*s",
+                 tid_req->request_id->len, tid_req->request_id->buf,
+                 tid_resp->request_id->len, tid_resp->request_id->buf);
+    }
+  } else if (tid_resp->request_id) {
+    tr_warning("tidc_fwd_request: Sent request without ID, received response for %.*s",
+               tid_resp->request_id->len, tid_resp->request_id->buf);
+  }
+
   if (resp_handler) {
     /* Call the caller's response function. It must copy any data it needs before returning. */
     tr_debug("tidc_fwd_request: calling response callback function.");
@@ -189,12 +231,13 @@ int tidc_fwd_request(TIDC_INSTANCE *tidc,
 }
 
 
-DH * tidc_get_dh(TIDC_INSTANCE *inst)
+DH *tidc_get_dh(TIDC_INSTANCE *inst)
 {
-  return tr_gssc_get_dh(inst->gssc);
+  return inst->client_dh;
 }
 
 DH *tidc_set_dh(TIDC_INSTANCE *inst, DH *dh)
 {
-  return tr_gssc_set_dh(inst->gssc, dh);
+  inst->client_dh = dh;
+  return dh;
 }