Work with new hostname parsing and improve error reports
authorJennifer Richards <jennifer@painless-security.com>
Wed, 30 May 2018 05:00:21 +0000 (01:00 -0400)
committerJennifer Richards <jennifer@painless-security.com>
Wed, 30 May 2018 05:00:21 +0000 (01:00 -0400)
  * Use the new tr_parse_host() function
  * Output more useful errors when parsing aaa servers
  * Update Makefile.am

Makefile.am
common/tr_aaa_server.c
common/tr_config_orgs.c
common/tr_config_realms.c
common/tr_filter.c
common/tr_msg.c
trp/trp_route_encoders.c

index 4a05c9f..c08e9df 100644 (file)
@@ -15,6 +15,7 @@ common_srcs = common/tr_name.c \
        common/tr_dh.c \
     common/tr_debug.c \
        common/tr_util.c \
+       common/tr_inet_util.c \
        common/tr_apc.c \
        common/tr_comm.c \
        common/tr_comm_encoders.c \
@@ -162,6 +163,7 @@ common/tr_name.c \
 common/tr_gss_names.c \
 common/tr_debug.c \
 common/tr_util.c \
+common/tr_inet_util.c \
 common/tr_list.c \
 trp/trp_route.c \
 trp/trp_route_encoders.c \
@@ -295,7 +297,7 @@ noinst_HEADERS = include/gsscon.h \
        include/trp_route.h include/trp_rtable.h \
        include/tr_list.h \
        include/tr_name_internal.h \
-       include/tr_util.h include/tr_json_util.h \
+       include/tr_util.h include/tr_json_util.h include/tr_inet_util.h\
        include/tr_rand_id.h include/tr_socket.h \
        include/tr_constraint_internal.h
 
index 193f018..dedf054 100644 (file)
@@ -39,6 +39,7 @@
 #include <tr_aaa_server.h>
 #include <trust_router/tid.h>
 #include <tr_util.h>
+#include <tr_inet_util.h>
 
 static int tr_aaa_server_destructor(void *obj)
 {
@@ -120,8 +121,7 @@ void tr_aaa_server_set_port(TR_AAA_SERVER *aaa, int port)
 /**
  * Allocate a AAA server record and fill it in by parsing a hostname:port string
  *
- * Does not validate hostname or port values. The port will be -1 if the port
- * could not be parsed properly.
+ * If hostname or port are invalid, hostname will be empty and port will be -1.
  *
  * @return newly allocated TR_AAA_SERVER in the mem_ctx context, or NULL on error
  */
@@ -129,15 +129,23 @@ TR_AAA_SERVER *tr_aaa_server_from_string(TALLOC_CTX *mem_ctx, const char *s)
 {
   TALLOC_CTX *tmp_ctx = talloc_new(NULL);
   TR_AAA_SERVER *aaa = tr_aaa_server_new(tmp_ctx);
+  char *hostname;
+  int port;
 
   if (aaa == NULL)
     goto failed;
 
-  tr_aaa_server_set_hostname(aaa, tr_parse_hostname(s));
+  hostname = tr_parse_host(tmp_ctx, s, &port);
+  if (NULL == hostname) {
+    hostname = "";
+    port = -1;
+  }
+
+  tr_aaa_server_set_hostname(aaa, tr_new_name(hostname));
   if (tr_aaa_server_get_hostname(aaa) == NULL)
     goto failed;
 
-  tr_aaa_server_set_port(aaa, tr_parse_port(s));
+  tr_aaa_server_set_port(aaa, port); /* port = 0 uses default TID port */
   talloc_steal(mem_ctx, aaa); /*put this in the caller's context */
   goto succeeded;
 
index 60674c5..e9b2c3c 100644 (file)
@@ -49,6 +49,7 @@
 #include <tr.h>
 #include <trust_router/trp.h>
 #include <tr_util.h>
+#include <tr_inet_util.h>
 
 #if JANSSON_VERSION_HEX < 0x020500
 #include "jansson_iterators.h"
@@ -171,8 +172,7 @@ static TR_CFG_RC tr_cfg_parse_one_peer_org(TR_CFG *trc, json_t *jporg)
   TR_GSS_NAMES *names=NULL;
   TR_FILTER_SET *filt_set=NULL;
   TR_CFG_RC rc=TR_CFG_ERROR;
-  TR_NAME *hostname=NULL;
-  char *s_hostname=NULL;
+  char *hostname=NULL;
   int port;
 
   jhost=json_object_get(jporg, "hostname");
@@ -204,13 +204,15 @@ static TR_CFG_RC tr_cfg_parse_one_peer_org(TR_CFG *trc, json_t *jporg)
     goto cleanup;
   }
 
-  if (0 != tr_parse_hostname_and_port(json_string_value(jhost), &hostname, &port)) {
+  /* parse / validate the hostname and port */
+  hostname = tr_parse_host(tmp_ctx, json_string_value(jhost), &port);
+  if (NULL == hostname) {
     tr_err("tr_cfg_parse_one_peer_org: error parsing hostname (%s)", json_string_value(jhost));
     rc=TR_CFG_NOPARSE;
     goto cleanup;
   }
 
-  if ((port < 0) || (port > 65535)) {
+  if (port < 0) {
     tr_err("tr_cfg_parse_one_peer_org: invalid port (%s)", json_string_value(jhost));
     rc=TR_CFG_NOPARSE;
     goto cleanup;
@@ -220,21 +222,7 @@ static TR_CFG_RC tr_cfg_parse_one_peer_org(TR_CFG *trc, json_t *jporg)
     port = TRP_PORT;
   trp_peer_set_port(new_peer, port);
 
-
-  if (hostname->len == 0) {
-    tr_err("tr_cfg_parse_one_peer_org: no hostname specified (%s)", json_string_value(jhost));
-    rc=TR_CFG_NOPARSE;
-    goto cleanup;
-  }
-
-  s_hostname = tr_name_strdup(hostname);
-  if (s_hostname == NULL) {
-    tr_err("tr_cfg_parse_one_peer_org: could not allocate hostname string.");
-    rc = TR_CFG_NOMEM;
-    goto cleanup;
-  }
-
-  trp_peer_set_server(new_peer, s_hostname); /* string is strdup'ed in _set_server() */
+  trp_peer_set_server(new_peer, hostname); /* string is strdup'ed in _set_server() */
   if (trp_peer_get_server(new_peer) == NULL) {
     tr_err("tr_cfg_parse_one_peer: could not set server hostname for new peer");
     rc = TR_CFG_NOMEM;
@@ -264,12 +252,6 @@ static TR_CFG_RC tr_cfg_parse_one_peer_org(TR_CFG *trc, json_t *jporg)
   rc=TR_CFG_SUCCESS;
 
 cleanup:
-  if (hostname)
-    tr_free_name(hostname);
-
-  if (s_hostname)
-    free(s_hostname);
-
   talloc_free(tmp_ctx);
   return rc;
 }
index cd8f660..60bae00 100644 (file)
@@ -72,14 +72,16 @@ TR_AAA_SERVER *tr_cfg_parse_one_aaa_server(TALLOC_CTX *mem_ctx, json_t *jaddr, T
   }
 
   if (tr_aaa_server_get_hostname(aaa)->len == 0) {
-    tr_debug("tr_cfg_parse_one_aaa_server: Empty hostname for AAA server not allowed");
+    tr_debug("tr_cfg_parse_one_aaa_server: Invalid hostname for AAA server (%s)",
+             json_string_value(jaddr));
     *rc = TR_CFG_NOPARSE;
     goto cleanup;
   }
 
   if ((tr_aaa_server_get_port(aaa) <= 0)
       || (tr_aaa_server_get_port(aaa) > 65535)) {
-    tr_debug("tr_cfg_parse_one_aaa_server: Invalid AAA server port");
+    tr_debug("tr_cfg_parse_one_aaa_server: Invalid AAA server port (%s)",
+             json_string_value(jaddr));
     *rc = TR_CFG_NOPARSE;
     goto cleanup;
   }
index 0d7c5c4..1df9aa4 100644 (file)
@@ -41,8 +41,8 @@
 #include <tr_filter.h>
 #include <trp_internal.h>
 #include <tid_internal.h>
+#include <tr_inet_util.h>
 #include <tr_debug.h>
-#include <tr_util.h>
 
 /* Function types for handling filter fields generally. All target values
  * are represented as strings in a TR_NAME.
index 350fc5b..886b535 100644 (file)
@@ -51,6 +51,7 @@
 #include <trust_router/tr_constraint.h>
 #include <trust_router/tr_dh.h>
 #include <tr_debug.h>
+#include <tr_inet_util.h>
 
 /* JSON helpers */
 /* Read attribute attr from msg as an integer. */
@@ -916,6 +917,7 @@ static TRP_RC tr_msg_decode_trp_inforec_route(json_t *jrecord, TRP_INFOREC *rec)
   TRP_RC rc=TRP_ERROR;
   char *s=NULL;
   TR_NAME *name;
+  char *hostname;
   int port;
   int num=0;
 
@@ -924,11 +926,15 @@ static TRP_RC tr_msg_decode_trp_inforec_route(json_t *jrecord, TRP_INFOREC *rec)
   if (rc != TRP_SUCCESS)
     goto cleanup;
 
-  if (0 != tr_parse_hostname_and_port(s, &name, &port)) {
+  hostname = tr_parse_host(tmp_ctx, s, &port);
+  if ((NULL == hostname)
+      || (NULL == (name = tr_new_name(hostname)))
+      || (port < 0)) {
     rc = TRP_ERROR;
     goto cleanup;
   }
   talloc_free(s); s=NULL;
+  talloc_free(hostname);
 
   if (port == 0)
     port = TRP_PORT;
@@ -943,7 +949,10 @@ static TRP_RC tr_msg_decode_trp_inforec_route(json_t *jrecord, TRP_INFOREC *rec)
   switch(tr_msg_get_json_string(jrecord, "next_hop", &s, tmp_ctx)) {
     case TRP_SUCCESS:
       /* we got a next_hop field */
-      if (0 != tr_parse_hostname_and_port(s, &name, &port)) {
+      hostname = tr_parse_host(tmp_ctx, s, &port);
+      if ((hostname == NULL)
+          || (NULL == (name = tr_new_name(hostname)))
+          || (port < 0)) {
         rc = TRP_ERROR;
         goto cleanup;
       }
index b7adf88..ad913da 100644 (file)
@@ -45,6 +45,7 @@
 #include <trust_router/trp.h>
 #include <tr_util.h>
 #include <tr_json_util.h>
+#include <tr_inet_util.h>
 
 /* Pretty print a route table entry to a newly allocated string. If sep is NULL,
  * returns comma+space separated string. */