Merge pull request #57 from painless-security/jennifer/show_rp_clients
authormrw42 <margaret@painless-security.com>
Thu, 3 May 2018 20:11:35 +0000 (16:11 -0400)
committerGitHub <noreply@github.com>
Thu, 3 May 2018 20:11:35 +0000 (16:11 -0400)
Add show rp_clients command (pull request 9)

15 files changed:
CMakeLists.txt
Makefile.am
common/tr_filter_encoders.c [new file with mode: 0644]
common/tr_gss_names.c
common/tr_rp.c
common/tr_rp_client.c [new file with mode: 0644]
common/tr_rp_client_encoders.c [new file with mode: 0644]
include/tr_config.h
include/tr_filter.h
include/tr_gss_names.h
include/tr_rp.h
include/tr_rp_client.h [new file with mode: 0644]
tr/tr_main.c
tr/tr_tid.c
trp/trp_peer_encoders.c

index 0985721..c031f68 100644 (file)
@@ -96,7 +96,7 @@ set(SOURCE_FILES
     trp/trp_upd.c
     trp/trpc.c
     trp/trps.c include/tr_name_internal.h mon/mon_req.c mon/mon_req_encode.c mon/mon_req_decode.c
-        mon/mon_resp.c mon/mon_common.c mon/mon_resp_encode.c mon/mon_resp_decode.c tr/tr_mon.c mon/mons.c include/tr_socket.h common/tr_gss.c include/tr_gss.h common/tr_config_internal.c mon/mons_handlers.c include/mons_handlers.h tr/tr_tid_mons.c tr/tr_tid_mons.c trp/trp_route.c include/trp_route.h trp/trp_rtable_encoders.c trp/trp_route_encoders.c trp/trp_peer.c include/trp_peer.h trp/trp_peer_encoders.c trp/trp_ptable_encoders.c common/tr_idp_encoders.c common/tr_comm_encoders.c)
+        mon/mon_resp.c mon/mon_common.c mon/mon_resp_encode.c mon/mon_resp_decode.c tr/tr_mon.c mon/mons.c include/tr_socket.h common/tr_gss.c include/tr_gss.h common/tr_config_internal.c mon/mons_handlers.c include/mons_handlers.h tr/tr_tid_mons.c tr/tr_tid_mons.c trp/trp_route.c include/trp_route.h trp/trp_rtable_encoders.c trp/trp_route_encoders.c trp/trp_peer.c include/trp_peer.h trp/trp_peer_encoders.c trp/trp_ptable_encoders.c common/tr_idp_encoders.c common/tr_comm_encoders.c common/tr_rp_client.c include/tr_rp_client.h common/tr_rp_client_encoders.c common/tr_filter_encoders.c)
 
 # Does not actually build!
 add_executable(trust_router ${SOURCE_FILES})
index d88ca77..5ef3761 100644 (file)
@@ -19,9 +19,12 @@ common_srcs = common/tr_name.c \
        common/tr_comm.c \
        common/tr_comm_encoders.c \
        common/tr_rp.c \
+       common/tr_rp_client.c \
+       common/tr_rp_client_encoders.c \
        common/tr_idp.c \
        common/tr_idp_encoders.c \
        common/tr_filter.c \
+       common/tr_filter_encoders.c \
        common/tr_gss_names.c \
        common/tr_socket.c \
        $(mon_srcs)
diff --git a/common/tr_filter_encoders.c b/common/tr_filter_encoders.c
new file mode 100644 (file)
index 0000000..cc22b02
--- /dev/null
@@ -0,0 +1,184 @@
+/*
+ * Copyright (c) 2018 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 <talloc.h>
+#include <jansson.h>
+
+#include <tr_filter.h>
+
+/* helper for below */
+#define OBJECT_SET_OR_FAIL(jobj, key, val)     \
+do {                                           \
+  if (val)                                     \
+    json_object_set_new((jobj),(key),(val));   \
+  else                                         \
+    goto cleanup;                              \
+} while (0)
+
+#define ARRAY_APPEND_OR_FAIL(jary, val)        \
+do {                                           \
+  if (val)                                     \
+    json_array_append_new((jary),(val));       \
+  else                                         \
+    goto cleanup;                              \
+} while (0)
+
+
+typedef json_t *(ITEM_ENCODER_FUNC)(void *);
+
+static json_t *items_to_json_array(void *items[], ITEM_ENCODER_FUNC *item_encoder, size_t max_items)
+{
+  size_t ii;
+  json_t *jarray = json_array();
+  json_t *retval = NULL;
+
+  if (jarray == NULL)
+    goto cleanup;
+
+  for (ii=0; ii<max_items; ii++) {
+    if (items[ii] != NULL)
+      ARRAY_APPEND_OR_FAIL(jarray, item_encoder(items[ii]));
+  }
+  /* success */
+  retval = jarray;
+  json_incref(retval);
+
+cleanup:
+  if (jarray)
+    json_decref(jarray);
+
+  return retval;
+}
+
+static json_t *tr_fspec_to_json(TR_FSPEC *fspec)
+{
+  json_t *fspec_json = NULL;
+  json_t *retval = NULL;
+
+  fspec_json = json_object();
+  if (fspec_json == NULL)
+    goto cleanup;
+
+  OBJECT_SET_OR_FAIL(fspec_json, "field",
+                     tr_name_to_json_string(fspec->field));
+  OBJECT_SET_OR_FAIL(fspec_json, "matches",
+                     items_to_json_array((void **)fspec->match,
+                                         (ITEM_ENCODER_FUNC *) tr_name_to_json_string,
+                                         TR_MAX_FILTER_SPEC_MATCHES));
+
+  /* succeeded - set the return value and increment the reference count */
+  retval = fspec_json;
+  json_incref(retval);
+
+cleanup:
+  if (fspec_json)
+    json_decref(fspec_json);
+  return retval;
+}
+
+static json_t *tr_fline_to_json(TR_FLINE *fline)
+{
+  json_t *fline_json = NULL;
+  json_t *retval = NULL;
+
+  fline_json = json_object();
+  if (fline_json == NULL)
+    goto cleanup;
+
+  OBJECT_SET_OR_FAIL(fline_json, "action",
+                     json_string( (fline->action == TR_FILTER_ACTION_ACCEPT) ? "accept" : "reject"));
+  OBJECT_SET_OR_FAIL(fline_json, "specs",
+                     items_to_json_array((void **)fline->specs,
+                                         (ITEM_ENCODER_FUNC *) tr_fspec_to_json,
+                                         TR_MAX_FILTER_SPECS));
+  if (fline->realm_cons) {
+    OBJECT_SET_OR_FAIL(fline_json, "realm_constraints",
+                       items_to_json_array((void **) fline->realm_cons->matches,
+                                           (ITEM_ENCODER_FUNC *) tr_name_to_json_string,
+                                           TR_MAX_CONST_MATCHES));
+  }
+  if (fline->domain_cons) {
+    OBJECT_SET_OR_FAIL(fline_json, "domain_constraints",
+                       items_to_json_array((void **) fline->domain_cons->matches,
+                                           (ITEM_ENCODER_FUNC *) tr_name_to_json_string,
+                                           TR_MAX_CONST_MATCHES));
+  }
+
+  /* succeeded - set the return value and increment the reference count */
+  retval = fline_json;
+  json_incref(retval);
+
+cleanup:
+  if (fline_json)
+    json_decref(fline_json);
+  return retval;
+}
+
+json_t *tr_filter_set_to_json(TR_FILTER_SET *filter_set)
+{
+  json_t *fset_json = NULL;
+  json_t *retval = NULL;
+  TR_FILTER *filt = NULL;
+  TR_FILTER_TYPE *filt_type = NULL;
+  TR_FILTER_TYPE types[] = {
+      TR_FILTER_TYPE_TID_INBOUND,
+      TR_FILTER_TYPE_TRP_INBOUND,
+      TR_FILTER_TYPE_TRP_OUTBOUND,
+      TR_FILTER_TYPE_UNKNOWN /* list terminator */
+  };
+
+  fset_json = json_object();
+  if (fset_json == NULL)
+    goto cleanup;
+
+  for (filt_type = types; *filt_type != TR_FILTER_TYPE_UNKNOWN; filt_type++) {
+    filt = tr_filter_set_get(filter_set, *filt_type);
+    if (filt) {
+      OBJECT_SET_OR_FAIL(fset_json, tr_filter_type_to_string(*filt_type),
+                         items_to_json_array((void **)filt->lines,
+                                             (ITEM_ENCODER_FUNC *) tr_fline_to_json,
+                                             TR_MAX_FILTER_LINES));
+    }
+  }
+
+  /* succeeded - set the return value and increment the reference count */
+  retval = fset_json;
+  json_incref(retval);
+
+cleanup:
+  if (fset_json)
+    json_decref(fset_json);
+  return retval;
+}
+
index ef66d31..970efdb 100644 (file)
@@ -131,7 +131,7 @@ void tr_gss_names_iter_free(TR_GSS_NAMES_ITER *iter)
   talloc_free(iter);
 }
 
-json_t *gss_names_to_json_array(TR_GSS_NAMES *gss_names)
+json_t *tr_gss_names_to_json_array(TR_GSS_NAMES *gss_names)
 {
   TR_GSS_NAMES_ITER *iter = tr_gss_names_iter_new(NULL);
   json_t *jarray = json_array();
index a8988f6..0b7d08e 100644 (file)
 #include <tr_rp.h>
 #include <tr_debug.h>
 
-static int tr_rp_client_destructor(void *obj)
-{
-  return 0;
-}
-
-TR_RP_CLIENT *tr_rp_client_new(TALLOC_CTX *mem_ctx)
-{
-  TR_RP_CLIENT *client=talloc(mem_ctx, TR_RP_CLIENT);
-
-  if (client!=NULL) {
-    client->next=NULL;
-    client->comm_next=NULL;
-    client->gss_names=NULL;
-    client->filters=NULL;
-    talloc_set_destructor((void *)client, tr_rp_client_destructor);
-  }
-  return client;
-}
-
-void tr_rp_client_free(TR_RP_CLIENT *client)
-{
-  talloc_free(client);
-}
-
-static TR_RP_CLIENT *tr_rp_client_tail(TR_RP_CLIENT *client)
-{
-  if (client==NULL)
-    return NULL;
-
-  while (client->next!=NULL)
-    client=client->next;
-  return client;
-}
-
-/* do not call directly, use the tr_rp_client_add() macro */
-TR_RP_CLIENT *tr_rp_client_add_func(TR_RP_CLIENT *clients, TR_RP_CLIENT *new)
-{
-  if (clients==NULL)
-    clients=new;
-  else {
-    tr_rp_client_tail(clients)->next=new;
-    while (new!=NULL) {
-      talloc_steal(clients, new); /* put it in the right context */
-      new=new->next;
-    }
-  }
-  return clients;
-}
-
-
-int tr_rp_client_add_gss_name(TR_RP_CLIENT *rp_client, TR_NAME *gss_name)
-{
-  return tr_gss_names_add(rp_client->gss_names, gss_name);
-}
-
-int tr_rp_client_set_filters(TR_RP_CLIENT *client, TR_FILTER_SET *filts)
-{
-  if (client->filters!=NULL)
-    tr_filter_set_free(client->filters);
-  client->filters=filts;
-  talloc_steal(client, filts);
-  return 0; /* success */
-}
-
-TR_RP_CLIENT_ITER *tr_rp_client_iter_new(TALLOC_CTX *memctx)
-{
-  return talloc(memctx, TR_RP_CLIENT_ITER);
-}
-
-void tr_rp_client_iter_free(TR_RP_CLIENT_ITER *iter)
-{
-  talloc_free(iter);
-}
-
-TR_RP_CLIENT *tr_rp_client_iter_first(TR_RP_CLIENT_ITER *iter, TR_RP_CLIENT *rp_clients)
-{
-  if (!iter) {
-    tr_err("tr_rp_client_iter_first: Iterator is null, failing.");
-    return NULL;
-  }
-  *iter=rp_clients;
-  return *iter;
-}
-
-TR_RP_CLIENT *tr_rp_client_iter_next(TR_RP_CLIENT_ITER *iter)
-{
-  if (*iter)
-    *iter=(*iter)->next;
-  return *iter;
-}
-
-/**
- * Find a client associated with a GSS name. It's possible there are other clients that match as well.
- *
- * @param rp_clients List of RP clients to search
- * @param gss_name GSS name to search for
- * @return Borrowed reference to an RP client linked to the GSS name
- */
-TR_RP_CLIENT *tr_rp_client_lookup(TR_RP_CLIENT *rp_clients, TR_NAME *gss_name)
-{
-  TR_RP_CLIENT_ITER *iter=tr_rp_client_iter_new(NULL);
-  TR_RP_CLIENT *client=NULL;
-
-  if (iter==NULL) {
-    tr_err("tr_rp_client_lookup: Unable to allocate iterator");
-    return NULL;
-  }
-  for (client=tr_rp_client_iter_first(iter, rp_clients); client != NULL; client=tr_rp_client_iter_next(iter)) {
-    if (tr_gss_names_matches(client->gss_names, gss_name))
-      break;
-  }
-  tr_rp_client_iter_free(iter);
-  return client;
-}
-
 TR_RP_REALM *tr_rp_realm_lookup(TR_RP_REALM *rp_realms, TR_NAME *rp_name)
 {
   TR_RP_REALM *rp = NULL;
@@ -331,7 +216,3 @@ char *tr_rp_realm_to_str(TALLOC_CTX *mem_ctx, TR_RP_REALM *rp)
                          rp->realm_id->len, rp->realm_id->buf);
 }
 
-json_t *tr_rp_realm_to_json(TR_RP_REALM *rp)
-{
-  return tr_name_to_json_string(tr_rp_realm_get_id(rp));
-}
\ No newline at end of file
diff --git a/common/tr_rp_client.c b/common/tr_rp_client.c
new file mode 100644 (file)
index 0000000..b5e657e
--- /dev/null
@@ -0,0 +1,153 @@
+/*
+ * Copyright (c) 2012-2018, 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 <talloc.h>
+#include <tr_rp_client.h>
+#include <tr_debug.h>
+
+static int tr_rp_client_destructor(void *obj)
+{
+  return 0;
+}
+
+TR_RP_CLIENT *tr_rp_client_new(TALLOC_CTX *mem_ctx)
+{
+  TR_RP_CLIENT *client=talloc(mem_ctx, TR_RP_CLIENT);
+
+  if (client!=NULL) {
+    client->next=NULL;
+    client->comm_next=NULL;
+    client->gss_names=NULL;
+    client->filters=NULL;
+    talloc_set_destructor((void *)client, tr_rp_client_destructor);
+  }
+  return client;
+}
+
+void tr_rp_client_free(TR_RP_CLIENT *client)
+{
+  talloc_free(client);
+}
+
+static TR_RP_CLIENT *tr_rp_client_tail(TR_RP_CLIENT *client)
+{
+  if (client==NULL)
+    return NULL;
+
+  while (client->next!=NULL)
+    client=client->next;
+  return client;
+}
+
+/* do not call directly, use the tr_rp_client_add() macro */
+TR_RP_CLIENT *tr_rp_client_add_func(TR_RP_CLIENT *clients, TR_RP_CLIENT *new)
+{
+  if (clients==NULL)
+    clients=new;
+  else {
+    tr_rp_client_tail(clients)->next=new;
+    while (new!=NULL) {
+      talloc_steal(clients, new); /* put it in the right context */
+      new=new->next;
+    }
+  }
+  return clients;
+}
+
+
+int tr_rp_client_add_gss_name(TR_RP_CLIENT *rp_client, TR_NAME *gss_name)
+{
+  return tr_gss_names_add(rp_client->gss_names, gss_name);
+}
+
+int tr_rp_client_set_filters(TR_RP_CLIENT *client, TR_FILTER_SET *filts)
+{
+  if (client->filters!=NULL)
+    tr_filter_set_free(client->filters);
+  client->filters=filts;
+  talloc_steal(client, filts);
+  return 0; /* success */
+}
+
+TR_RP_CLIENT_ITER *tr_rp_client_iter_new(TALLOC_CTX *memctx)
+{
+  return talloc(memctx, TR_RP_CLIENT_ITER);
+}
+
+void tr_rp_client_iter_free(TR_RP_CLIENT_ITER *iter)
+{
+  talloc_free(iter);
+}
+
+TR_RP_CLIENT *tr_rp_client_iter_first(TR_RP_CLIENT_ITER *iter, TR_RP_CLIENT *rp_clients)
+{
+  if (!iter) {
+    tr_err("tr_rp_client_iter_first: Iterator is null, failing.");
+    return NULL;
+  }
+  *iter=rp_clients;
+  return *iter;
+}
+
+TR_RP_CLIENT *tr_rp_client_iter_next(TR_RP_CLIENT_ITER *iter)
+{
+  if (*iter)
+    *iter=(*iter)->next;
+  return *iter;
+}
+
+/**
+ * Find a client associated with a GSS name. It's possible there are other clients that match as well.
+ *
+ * @param rp_clients List of RP clients to search
+ * @param gss_name GSS name to search for
+ * @return Borrowed reference to an RP client linked to the GSS name
+ */
+TR_RP_CLIENT *tr_rp_client_lookup(TR_RP_CLIENT *rp_clients, TR_NAME *gss_name)
+{
+  TR_RP_CLIENT_ITER *iter=tr_rp_client_iter_new(NULL);
+  TR_RP_CLIENT *client=NULL;
+
+  if (iter==NULL) {
+    tr_err("tr_rp_client_lookup: Unable to allocate iterator");
+    return NULL;
+  }
+  for (client=tr_rp_client_iter_first(iter, rp_clients); client != NULL; client=tr_rp_client_iter_next(iter)) {
+    if (tr_gss_names_matches(client->gss_names, gss_name))
+      break;
+  }
+  tr_rp_client_iter_free(iter);
+  return client;
+}
+
diff --git a/common/tr_rp_client_encoders.c b/common/tr_rp_client_encoders.c
new file mode 100644 (file)
index 0000000..d0a5cd0
--- /dev/null
@@ -0,0 +1,108 @@
+/*
+ * Copyright (c) 2018, 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 <talloc.h>
+#include <jansson.h>
+
+#include <tr_gss_names.h>
+#include <tr_rp_client.h>
+
+/* helper for below */
+#define OBJECT_SET_OR_FAIL(jobj, key, val)     \
+do {                                           \
+  if (val)                                     \
+    json_object_set_new((jobj),(key),(val));   \
+  else                                         \
+    goto cleanup;                              \
+} while (0)
+
+#define ARRAY_APPEND_OR_FAIL(jary, val)        \
+do {                                           \
+  if (val)                                     \
+    json_array_append_new((jary),(val));       \
+  else                                         \
+    goto cleanup;                              \
+} while (0)
+
+static json_t *tr_rp_client_to_json(TR_RP_CLIENT *rp_client)
+{
+  json_t *client_json = NULL;
+  json_t *retval = NULL;
+
+  client_json = json_object();
+  if (client_json == NULL)
+    goto cleanup;
+
+  OBJECT_SET_OR_FAIL(client_json, "gss_names", tr_gss_names_to_json_array(rp_client->gss_names));
+  OBJECT_SET_OR_FAIL(client_json, "filters", tr_filter_set_to_json(rp_client->filters));
+  
+  /* succeeded - set the return value and increment the reference count */
+  retval = client_json;
+  json_incref(retval);
+
+cleanup:
+  if (client_json)
+    json_decref(client_json);
+  return retval;
+}
+
+json_t *tr_rp_clients_to_json(TR_RP_CLIENT *rp_clients)
+{
+  json_t *jarray = json_array();
+  json_t *retval = NULL;
+  TR_RP_CLIENT_ITER *iter = tr_rp_client_iter_new(NULL);
+  TR_RP_CLIENT *rp_client = NULL;
+
+  if ((jarray == NULL) || (iter == NULL))
+    goto cleanup;
+
+  rp_client = tr_rp_client_iter_first(iter, rp_clients);
+  while (rp_client) {
+    ARRAY_APPEND_OR_FAIL(jarray, tr_rp_client_to_json(rp_client));
+    rp_client = tr_rp_client_iter_next(iter);
+  }
+
+  /* succeeded - set the return value and increment the reference count */
+  retval = jarray;
+  json_incref(retval);
+
+cleanup:
+  if (jarray)
+    json_decref(jarray);
+
+  if (iter)
+    tr_rp_client_iter_free(iter);
+
+  return retval;
+}
index aaf016b..64339e7 100644 (file)
@@ -44,6 +44,7 @@
 
 #include <tr_comm.h>
 #include <tr_rp.h>
+#include <tr_rp_client.h>
 #include <tr_idp.h>
 #include <trp_ptable.h>
 #include <trp_internal.h>
index a7704d7..ece3650 100644 (file)
@@ -144,4 +144,7 @@ int tr_filter_validate_spec_field(TR_FILTER_TYPE ftype, TR_FSPEC *fspec);
 const char *tr_filter_type_to_string(TR_FILTER_TYPE ftype);
 TR_FILTER_TYPE tr_filter_type_from_string(const char *s);
 
+/* tr_filter_encoders.c */
+json_t *tr_filter_set_to_json(TR_FILTER_SET *filter_set);
+
 #endif
index 33590a3..7585798 100644 (file)
@@ -59,6 +59,6 @@ TR_NAME *tr_gss_names_iter_first(TR_GSS_NAMES_ITER *iter, TR_GSS_NAMES *gn);
 TR_NAME *tr_gss_names_iter_next(TR_GSS_NAMES_ITER *iter);
 void tr_gss_names_iter_free(TR_GSS_NAMES_ITER *iter);
 
-json_t *gss_names_to_json_array(TR_GSS_NAMES *gss_names);
+json_t *tr_gss_names_to_json_array(TR_GSS_NAMES *gss_names);
 
 #endif /* __TR_GSS_H__ */
index abf476b..4424c73 100644 (file)
 #define TR_RP_H
 
 #include <talloc.h>
-
-#include <tr_gss_names.h>
-#include <tr_filter.h>
-
-typedef struct tr_rp_client {
-  struct tr_rp_client *next;
-  struct tr_rp_client *comm_next;
-  TR_GSS_NAMES *gss_names;
-  TR_FILTER_SET *filters;
-} TR_RP_CLIENT;
-
-typedef struct tr_rp_client *TR_RP_CLIENT_ITER;
+#include <tr_name_internal.h>
 
 /* Structure to make a linked list of RP realms by name for community config */
 typedef struct tr_rp_realm {
@@ -57,18 +46,6 @@ typedef struct tr_rp_realm {
 } TR_RP_REALM;
 
 /* prototypes */
-TR_RP_CLIENT *tr_rp_client_new(TALLOC_CTX *mem_ctx);
-void tr_rp_client_free(TR_RP_CLIENT *client);
-TR_RP_CLIENT *tr_rp_client_add_func(TR_RP_CLIENT *clients, TR_RP_CLIENT *new);
-#define tr_rp_client_add(clients,new) ((clients)=tr_rp_client_add_func((clients),(new)))
-int tr_rp_client_add_gss_name(TR_RP_CLIENT *client, TR_NAME *name);
-int tr_rp_client_set_filters(TR_RP_CLIENT *client, TR_FILTER_SET *filts);
-TR_RP_CLIENT_ITER *tr_rp_client_iter_new(TALLOC_CTX *memctx);
-void tr_rp_client_iter_free(TR_RP_CLIENT_ITER *iter);
-TR_RP_CLIENT *tr_rp_client_iter_first(TR_RP_CLIENT_ITER *iter, TR_RP_CLIENT *rp_clients);
-TR_RP_CLIENT *tr_rp_client_iter_next(TR_RP_CLIENT_ITER *iter);
-TR_RP_CLIENT *tr_rp_client_lookup(TR_RP_CLIENT *rp_clients, TR_NAME *gss_name);
-
 TR_RP_REALM *tr_rp_realm_new(TALLOC_CTX *mem_ctx);
 void tr_rp_realm_free(TR_RP_REALM *rp);
 TR_NAME *tr_rp_realm_get_id(TR_RP_REALM *rp);
@@ -85,6 +62,5 @@ void tr_rp_realm_incref(TR_RP_REALM *realm);
 void tr_rp_realm_decref(TR_RP_REALM *realm);
 
 char *tr_rp_realm_to_str(TALLOC_CTX *mem_ctx, TR_RP_REALM *rp);
-json_t *tr_rp_realm_to_json(TR_RP_REALM *rp);
 
 #endif
diff --git a/include/tr_rp_client.h b/include/tr_rp_client.h
new file mode 100644 (file)
index 0000000..1ff4b29
--- /dev/null
@@ -0,0 +1,68 @@
+/*
+ * Copyright (c) 2012-2018, 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.
+ *
+ */
+
+#ifndef TRUST_ROUTER_TR_RP_CLIENT_H
+#define TRUST_ROUTER_TR_RP_CLIENT_H
+
+#include <talloc.h>
+
+#include <tr_gss_names.h>
+#include <tr_filter.h>
+
+typedef struct tr_rp_client {
+  struct tr_rp_client *next;
+  struct tr_rp_client *comm_next;
+  TR_GSS_NAMES *gss_names;
+  TR_FILTER_SET *filters;
+} TR_RP_CLIENT;
+
+typedef struct tr_rp_client *TR_RP_CLIENT_ITER;
+
+/* tr_rp_client.c */
+TR_RP_CLIENT *tr_rp_client_new(TALLOC_CTX *mem_ctx);
+void tr_rp_client_free(TR_RP_CLIENT *client);
+TR_RP_CLIENT *tr_rp_client_add_func(TR_RP_CLIENT *clients, TR_RP_CLIENT *new);
+#define tr_rp_client_add(clients,new) ((clients)=tr_rp_client_add_func((clients),(new)))
+int tr_rp_client_add_gss_name(TR_RP_CLIENT *client, TR_NAME *name);
+int tr_rp_client_set_filters(TR_RP_CLIENT *client, TR_FILTER_SET *filts);
+TR_RP_CLIENT_ITER *tr_rp_client_iter_new(TALLOC_CTX *memctx);
+void tr_rp_client_iter_free(TR_RP_CLIENT_ITER *iter);
+TR_RP_CLIENT *tr_rp_client_iter_first(TR_RP_CLIENT_ITER *iter, TR_RP_CLIENT *rp_clients);
+TR_RP_CLIENT *tr_rp_client_iter_next(TR_RP_CLIENT_ITER *iter);
+TR_RP_CLIENT *tr_rp_client_lookup(TR_RP_CLIENT *rp_clients, TR_NAME *gss_name);
+
+/* tr_rp_client_encoders.c */
+json_t *tr_rp_clients_to_json(TR_RP_CLIENT *rp_clients);
+
+#endif //TRUST_ROUTER_TR_RP_CLIENT_H
index 1f18e4e..61f210c 100644 (file)
@@ -151,20 +151,30 @@ static void configure_signals(void)
   pthread_sigmask(SIG_BLOCK, &signals, NULL);
 }
 
-/* TODO move this function */
-static MON_RC tr_mon_handle_version(void *cookie, json_t **result_ptr)
+/* Monitoring handlers */
+static MON_RC tr_handle_version(void *cookie, json_t **result_ptr)
 {
   *result_ptr = json_string(PACKAGE_VERSION);
   return (*result_ptr == NULL) ? MON_NOMEM : MON_SUCCESS;
 }
 
-static MON_RC tr_mon_handle_uptime(void *cookie, json_t **result_ptr)
+static MON_RC tr_handle_uptime(void *cookie, json_t **result_ptr)
 {
   time_t *start_time = cookie;
   *result_ptr = json_integer(time(NULL) - (*start_time));
   return (*result_ptr == NULL) ? MON_NOMEM : MON_SUCCESS;
 }
 
+static MON_RC tr_handle_show_rp_clients(void *cookie, json_t **response_ptr)
+{
+  TR_CFG_MGR *cfg_mgr = talloc_get_type_abort(cookie, TR_CFG_MGR);
+
+  *response_ptr = tr_rp_clients_to_json(cfg_mgr->active->rp_clients);
+  return (*response_ptr == NULL) ? MON_NOMEM : MON_SUCCESS;
+}
+
+
+
 int main(int argc, char *argv[])
 {
   TALLOC_CTX *main_ctx=NULL;
@@ -237,9 +247,10 @@ int main(int argc, char *argv[])
   tr->mons->tids = tr->tids;
   tr->mons->trps = tr->trps;
 
-  /* TODO do this more systematically */
-  mons_register_handler(tr->mons, MON_CMD_SHOW, OPT_TYPE_SHOW_VERSION, tr_mon_handle_version, NULL);
-  mons_register_handler(tr->mons, MON_CMD_SHOW, OPT_TYPE_SHOW_UPTIME, tr_mon_handle_uptime, &start_time);
+  /* Register monitoring handlers */
+  mons_register_handler(tr->mons, MON_CMD_SHOW, OPT_TYPE_SHOW_VERSION, tr_handle_version, NULL);
+  mons_register_handler(tr->mons, MON_CMD_SHOW, OPT_TYPE_SHOW_UPTIME, tr_handle_uptime, &start_time);
+  mons_register_handler(tr->mons, MON_CMD_SHOW, OPT_TYPE_SHOW_RP_CLIENTS, tr_handle_show_rp_clients, tr->cfg_mgr);
   tr_tid_register_mons_handlers(tr->tids, tr->mons);
   tr_trp_register_mons_handlers(tr->trps, tr->mons);
 
index 6c649c0..e10b036 100644 (file)
@@ -40,6 +40,7 @@
 #include <tr_comm.h>
 #include <tr_idp.h>
 #include <tr_rp.h>
+#include <tr_rp_client.h>
 #include <tr_event.h>
 #include <tr_debug.h>
 #include <gsscon.h>
index 6c8a4bf..8aafece 100644 (file)
@@ -111,7 +111,9 @@ json_t *trp_peer_to_json(TRP_PEER *peer)
   OBJECT_SET_OR_FAIL(peer_json, "last_connection_attempt",
                      last_attempt_to_json_string(peer));
   OBJECT_SET_OR_FAIL(peer_json, "allowed_credentials",
-                     gss_names_to_json_array(trp_peer_get_gss_names(peer)));
+                     tr_gss_names_to_json_array(trp_peer_get_gss_names(peer)));
+  OBJECT_SET_OR_FAIL(peer_json, "filters",
+                     tr_filter_set_to_json(peer->filters));
 
   /* succeeded - set the return value and increment the reference count */
   retval = peer_json;