First steps toward actually handling monitoring requests
authorJennifer Richards <jennifer@painless-security.com>
Tue, 17 Apr 2018 16:58:44 +0000 (12:58 -0400)
committerJennifer Richards <jennifer@painless-security.com>
Tue, 17 Apr 2018 16:58:44 +0000 (12:58 -0400)
CMakeLists.txt
Makefile.am
mon/mons.c
mon/mons_handlers.c [new file with mode: 0644]
mon/mons_handlers.h [new file with mode: 0644]

index 863d847..abc00c6 100644 (file)
@@ -91,7 +91,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/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 mon/mons_handlers.h)
 
 # Does not actually build!
 add_executable(trust_router ${SOURCE_FILES})
index 3ef94dd..c73c623 100644 (file)
@@ -40,6 +40,7 @@ common/tr_config.c \
 common/tr_config_internal.c \
 common/tr_mq.c
 
+# general monitoring message sources
 mon_srcs =                   \
     mon/mon_common.c         \
     mon/mon_req.c            \
@@ -49,6 +50,11 @@ mon_srcs =                   \
     mon/mon_resp_decode.c    \
     mon/mon_resp_encode.c
 
+# monitoring server sources
+mons_srcs = \
+    mon/mons.c \
+    mon/mons_handlers.c
+
 check_PROGRAMS = common/t_constraint
 TESTS = common/t_constraint
 TEST_CFLAGS = -Wno-missing-prototypes
@@ -87,7 +93,7 @@ common/tr_gss_client.c \
 $(tid_srcs) \
 $(trp_srcs) \
 $(common_srcs) \
-mon/mons.c
+$(mons_srcs)
 
 tr_trust_router_LDFLAGS = $(AM_LDFLAGS) -levent_pthreads -pthread
 tr_trust_router_LDADD = gsscon/libgsscon.la $(GLIB_LIBS)
index d2a5c8a..2216f5f 100644 (file)
@@ -83,8 +83,9 @@ MONS_INSTANCE *mons_new(TALLOC_CTX *mem_ctx)
 static TR_MSG *mons_req_cb(TALLOC_CTX *mem_ctx, TR_MSG *req_msg, void *data)
 {
   TALLOC_CTX *tmp_ctx = talloc_new(NULL);
-  //MONS_INSTANCE *mons = talloc_get_type_abort(data, MONS_INSTANCE);
+  MONS_INSTANCE *mons = talloc_get_type_abort(data, MONS_INSTANCE);
   MON_REQ *req = NULL;
+  MON_RESP *resp = NULL;
   TR_MSG *resp_msg = NULL; /* This is the response value */
 
   /* Validate inputs */
@@ -101,6 +102,28 @@ static TR_MSG *mons_req_cb(TALLOC_CTX *mem_ctx, TR_MSG *req_msg, void *data)
     goto cleanup;
   }
 
+  /* Allocate a response message */
+  resp_msg = talloc(tmp_ctx, TR_MSG);
+  if (resp_msg == NULL) {
+    /* can't return a message, just emit an error */
+    tr_crit("mons_req_cb: Error allocating response message.");
+    goto cleanup;
+  }
+
+  /* Handle the request */
+  resp = mons_handle_request(resp_msg, mons, req);
+  if (resp == NULL) {
+    /* error processing the request */
+    /* TODO send back an error */
+    goto cleanup;
+  }
+
+  /* Set the response message payload */
+  tr_msg_set_mon_resp(resp_msg, resp);
+
+  /* Put the response message in the caller's context so it does not get freed when we exit */
+  talloc_steal(mem_ctx, resp_msg);
+
 cleanup:
   talloc_free(tmp_ctx);
   return resp_msg;
diff --git a/mon/mons_handlers.c b/mon/mons_handlers.c
new file mode 100644 (file)
index 0000000..5bee3e5
--- /dev/null
@@ -0,0 +1,90 @@
+/*
+ * 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.
+ *
+ */
+
+/* Handlers for monitoring requests */
+
+#include <mon_internal.h>
+
+#include "mons_handlers.h"
+
+typedef MON_RESP *(MONS_HANDLER_FUNC)(TALLOC_CTX *, MONS_INSTANCE *, MON_REQ *);
+
+/* Prototypes for the dispatch table */
+static MON_RESP *mons_handle_show(TALLOC_CTX *mem_ctx, MONS_INSTANCE *mons, MON_REQ *req);
+static MON_RESP *mons_handle_reconfigure(TALLOC_CTX *mem_ctx, MONS_INSTANCE *mons, MON_REQ *req);
+
+
+struct dispatch_table_entry {
+  MON_CMD command;
+  MONS_HANDLER_FUNC *handler;
+};
+
+static struct dispatch_table_entry dispatch_table[] = {
+    {MON_CMD_SHOW, mons_handle_show},
+    {MON_CMD_RECONFIGURE, mons_handle_reconfigure},
+    {MON_CMD_UNKNOWN} /* Must be the last entry in the table */
+};
+
+/**
+ * Call the appropriate handler for a request
+ *
+ * @return a MON_RESP structure or null if there was a processing error
+ */
+MON_RESP *mons_handle_request(TALLOC_CTX *mem_ctx, MONS_INSTANCE *mons, MON_REQ *req)
+{
+  struct dispatch_table_entry *entry = dispatch_table;
+
+  /* Find the handler */
+  while ((entry->command != req->command) && (entry->command != MON_CMD_UNKNOWN)) {
+    entry++;
+  }
+
+  /* See if we found a handler */
+  if (entry->command == MON_CMD_UNKNOWN)
+    return NULL;
+
+  /* Call the handler */
+  return entry->handler(mem_ctx, mons, req);
+}
+
+static MON_RESP *mons_handle_show(TALLOC_CTX *mem_ctx, MONS_INSTANCE *mons, MON_REQ *req)
+{
+  MON_RESP *resp = mon_resp_new(mem_ctx, MON_RESP_SUCCESS, "success", NULL);
+  return resp;
+}
+
+static MON_RESP *mons_handle_reconfigure(TALLOC_CTX *mem_ctx, MONS_INSTANCE *mons, MON_REQ *req)
+{
+  return NULL;
+}
diff --git a/mon/mons_handlers.h b/mon/mons_handlers.h
new file mode 100644 (file)
index 0000000..2f13ff9
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * 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.
+ *
+ */
+
+
+#ifndef TRUST_ROUTER_MONS_HANDLERS_H
+#define TRUST_ROUTER_MONS_HANDLERS_H
+
+MON_RESP *mons_handle_request(TALLOC_CTX *mem_ctx, MONS_INSTANCE *mons, MON_REQ *req);
+
+#endif //TRUST_ROUTER_MONS_HANDLERS_H