- use tunnel encryption state to select decryption key
[oweals/gnunet.git] / src / testbed / testbed_api_hosts.c
index b7c824bc200691f2e8ac7cc3922472394a7d318a..8af8e23d00829190cb93a2ab390efc0857f958b7 100644 (file)
@@ -37,6 +37,7 @@
 #include "testbed_api_operations.h"
 
 #include <zlib.h>
+#include <regex.h>
 
 /**
  * Generic logging shorthand
@@ -317,7 +318,7 @@ GNUNET_TESTBED_host_replace_cfg_ (struct GNUNET_TESTBED_Host *host,
  */
 struct GNUNET_TESTBED_Host *
 GNUNET_TESTBED_host_create_with_id (uint32_t id, const char *hostname,
-                                    const char *username, 
+                                    const char *username,
                                     const struct GNUNET_CONFIGURATION_Handle
                                     *cfg,
                                     uint16_t port)
@@ -337,7 +338,7 @@ GNUNET_TESTBED_host_create_with_id (uint32_t id, const char *hostname,
   host->port = (0 == port) ? 22 : port;
   host->cfg = GNUNET_CONFIGURATION_dup (cfg);
   host->opq_parallel_overlay_connect_operations =
-      GNUNET_TESTBED_operation_queue_create_ (OPERATION_QUEUE_TYPE_ADAPTIVE, 
+      GNUNET_TESTBED_operation_queue_create_ (OPERATION_QUEUE_TYPE_ADAPTIVE,
                                               UINT_MAX);
   new_size = host_list_size;
   while (id >= new_size)
@@ -370,7 +371,7 @@ GNUNET_TESTBED_host_create (const char *hostname, const char *username,
   static uint32_t uid_generator;
 
   if (NULL == hostname)
-    return GNUNET_TESTBED_host_create_with_id (0, hostname, username, 
+    return GNUNET_TESTBED_host_create_with_id (0, hostname, username,
                                                cfg, port);
   return GNUNET_TESTBED_host_create_with_id (++uid_generator, hostname,
                                              username, cfg, port);
@@ -398,11 +399,12 @@ GNUNET_TESTBED_hosts_load_from_file (const char *filename,
   struct GNUNET_TESTBED_Host *starting_host;
   char *data;
   char *buf;
-  char username[256];
-  char hostname[256];
+  char *username;
+  char *hostname;
+  regex_t rex;
+  regmatch_t pmatch[6];
   uint64_t fs;
   short int port;
-  int ret;
   unsigned int offset;
   unsigned int count;
 
@@ -433,37 +435,67 @@ GNUNET_TESTBED_hosts_load_from_file (const char *filename,
   offset = 0;
   starting_host = NULL;
   count = 0;
+  /* refer RFC 952 and RFC 1123 for valid hostnames */
+  GNUNET_assert (0 == regcomp (&rex,
+                               "^(([[:alnum:]]+)@)?" /* username */
+                               "([[:alnum:]]+[-[:alnum:]_\\.]+)" /* hostname */
+                               "(:([[:digit:]]{1,5}))?", /* port */
+                               REG_EXTENDED | REG_ICASE));
   while (offset < (fs - 1))
   {
     offset++;
     if (((data[offset] == '\n')) && (buf != &data[offset]))
     {
+      unsigned int size;
+
       data[offset] = '\0';
-      ret =
-          SSCANF (buf, "%255[a-zA-Z0-9_]@%255[.a-zA-Z0-9-]:%5hd", username,
-                  hostname, &port);
-      if (3 == ret)
+      username = NULL;
+      hostname = NULL;
+      port = 0;
+      if ((REG_NOMATCH == regexec (&rex, buf, 6, pmatch, 0))
+          || (-1 == pmatch[3].rm_so))
       {
-        LOG (GNUNET_ERROR_TYPE_DEBUG,
-             "Successfully read host %s, port %d and user %s from file\n",
-             hostname, port, username);
-        /* We store hosts in a static list; hence we only require the starting
-         * host pointer in that list to access the newly created list of hosts */
-        if (NULL == starting_host)
-          starting_host = GNUNET_TESTBED_host_create (hostname, username, cfg,
-                                                      port);
-        else
-          (void) GNUNET_TESTBED_host_create (hostname, username, cfg, port);
-        count++;
-      }
-      else
         GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                     "Error reading line `%s' in hostfile\n", buf);
+        buf = &data[offset + 1];
+        continue;
+      }
+      if (-1 != pmatch[2].rm_so)
+      {
+        size = pmatch[2].rm_eo - pmatch[2].rm_so;
+        username = GNUNET_malloc (size + 1);
+        username[size] = '\0';
+        GNUNET_assert (NULL != strncpy (username, buf + pmatch[2].rm_so, size));
+      }
+      if (-1 != pmatch[5].rm_so)
+      {
+        (void) SSCANF (buf + pmatch[5].rm_so, "%5hd", &port);
+      }
+      size = pmatch[3].rm_eo - pmatch[3].rm_so;
+      hostname = GNUNET_malloc (size + 1);
+      hostname[size] = '\0';
+      GNUNET_assert (NULL != strncpy (hostname, buf + pmatch[3].rm_so, size));
+      LOG (GNUNET_ERROR_TYPE_DEBUG,
+           "Successfully read host %s, port %d and user %s from file\n",
+           (NULL == hostname) ? "NULL" : hostname,
+           port,
+           (NULL == username) ? "NULL" : username);
+      /* We store hosts in a static list; hence we only require the starting
+       * host pointer in that list to access the newly created list of hosts */
+      if (NULL == starting_host)
+        starting_host = GNUNET_TESTBED_host_create (hostname, username, cfg,
+                                                    port);
+      else
+        (void) GNUNET_TESTBED_host_create (hostname, username, cfg, port);
+      count++;
+      GNUNET_free_non_null (username);
+      GNUNET_free (hostname);
       buf = &data[offset + 1];
     }
     else if ((data[offset] == '\n') || (data[offset] == '\0'))
       buf = &data[offset + 1];
   }
+  regfree (&rex);
   GNUNET_free (data);
   if (NULL == starting_host)
     return 0;
@@ -484,7 +516,7 @@ const char *
 simple_resolve (const char *host)
 {
   struct addrinfo *res;
-  const struct sockaddr_in *in_addr; 
+  const struct sockaddr_in *in_addr;
   char *hostip;
   struct addrinfo hint;
   unsigned int rc;
@@ -515,17 +547,6 @@ simple_resolve (const char *host)
   return hostip;
 }
 
-#if ENABLE_LL
-static int
-cmpstringp(const void *p1, const void *p2)
-{
-  /* The actual arguments to this function are "pointers to
-     pointers to char", but strcmp(3) arguments are "pointers
-     to char", hence the following cast plus dereference */
-  
-  return strcmp(* (char * const *) p1, * (char * const *) p2);
-}
-#endif
 
 /**
  * Loads the set of host allocated by the LoadLeveler Job Scheduler.  This
@@ -544,127 +565,20 @@ GNUNET_TESTBED_hosts_load_from_loadleveler (const struct
                                             GNUNET_CONFIGURATION_Handle *cfg,
                                             struct GNUNET_TESTBED_Host ***hosts)
 {
-#if !ENABLE_LL
-  LOG (GNUNET_ERROR_TYPE_ERROR, 
+#if !ENABLE_SUPERMUC
+  LOG (GNUNET_ERROR_TYPE_ERROR,
        _("The function %s is only available when compiled with (--with-ll)\n"),
        __func__);
   GNUNET_assert (0);
 #else
   const char *hostfile;
-  char *buf;
-  char *hostname;
-  char **hostnames;
-  struct GNUNET_TESTBED_Host **host_list;
-  ssize_t rsize;
-  uint64_t size;
-  uint64_t offset;
-  enum {
-    SCAN,
-    SKIP,
-    TRIM,
-    READHOST
-  } pstep;
-  unsigned int host;
-  unsigned int nhosts;
-  
+
   if (NULL == (hostfile = getenv ("MP_SAVEHOSTFILE")))
   {
     GNUNET_break (0);
     return 0;
   }
-  if (GNUNET_SYSERR == GNUNET_DISK_file_size (hostfile, &size, GNUNET_YES,
-                                              GNUNET_YES))
-  {
-    GNUNET_break (0);
-    return 0;
-  }
-  if (0 == size)
-  {
-    GNUNET_break (0);
-    return 0;
-  }
-  buf = GNUNET_malloc (size + 1);
-  rsize = GNUNET_DISK_fn_read (hostfile, buf, (size_t) size);
-  if ( (GNUNET_SYSERR == rsize) || ((ssize_t) size != rsize) )
-  {
-    GNUNET_free (buf);
-    GNUNET_break (0);
-    return 0;
-  }
-  size++;
-  offset = 0;
-  pstep = SCAN;
-  hostname = NULL;
-  hostnames = NULL;
-  nhosts = 0;
-  while (offset < size)
-  {
-    switch (pstep)
-    {
-    case SCAN:
-      if ('!' == buf[offset])
-        pstep = SKIP;
-      else 
-        pstep = TRIM;
-      break;
-    case SKIP:
-      if ('\n' == buf[offset])
-        pstep = SCAN;
-      break;
-    case TRIM:
-      if ('!' == buf[offset])
-      {
-        pstep = SKIP;
-        break;
-      }
-      if ( (' ' == buf[offset]) 
-           || ('\t' == buf[offset])
-           || ('\r' == buf[offset]) )
-        pstep = TRIM;
-      else
-      {
-        pstep = READHOST;
-        hostname = &buf[offset];        
-      }
-      break;
-    case READHOST:
-      if (isspace (buf[offset]))
-      {
-        buf[offset] = '\0';
-        for (host = 0; host < nhosts; host++)
-          if (0 == strcmp (hostnames[host], hostname))
-            break;
-        if (host == nhosts)
-        {
-          LOG_DEBUG ("Adding host [%s]\n", hostname);
-          hostname = GNUNET_strdup (hostname);
-          GNUNET_array_append (hostnames, nhosts, hostname);
-        }
-        else
-          LOG_DEBUG ("Not adding host [%s] as it is already included\n", hostname);
-        hostname = NULL;
-        pstep = SCAN;
-      }
-      break;
-    }
-    offset++;
-  }
-  GNUNET_free_non_null (buf);
-  if (NULL == hostnames)
-    return 0;
-  if (NULL == hosts)
-    goto cleanup;
-  qsort (hostnames, nhosts, sizeof (hostnames[0]), cmpstringp);
-  host_list = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Host *) * nhosts);
-  for (host = 0; host < nhosts; host++)
-    host_list[host] = GNUNET_TESTBED_host_create (hostnames[host], NULL, cfg, 0);
-  *hosts = host_list;
-
- cleanup:
-  for (host = 0; host < nhosts; host++)
-    GNUNET_free (hostnames[host]);
-  GNUNET_free(hostnames);
-  return nhosts;
+  return GNUNET_TESTBED_hosts_load_from_file (hostfile, cfg, hosts);
 #endif
 }
 
@@ -913,11 +827,12 @@ free_argv (char **argv)
  * NoHostAuthenticationForLocalhost=yes'
  *
  * @param port the destination port number
- * @param dst the destination address
+ * @param hostname the hostname of the target host
+ * @param username the username to use while connecting to target host
  * @return NULL terminated list of arguments
  */
 static char **
-gen_rsh_args (const char *port, const char *dst)
+gen_rsh_args (const char *port, const char *hostname, const char *username)
 {
   static const char *default_ssh_args[] = {
     "ssh",
@@ -925,12 +840,15 @@ gen_rsh_args (const char *port, const char *dst)
     "BatchMode=yes",
     "-o",
     "NoHostAuthenticationForLocalhost=yes",
+    "%h",
     NULL
   };
   char **ssh_args;
   char *ssh_cmd;
   char *ssh_cmd_cp;
   char *arg;
+  const char *new_arg;
+  unsigned int size;
   unsigned int cnt;
 
   ssh_args = NULL;
@@ -938,20 +856,44 @@ gen_rsh_args (const char *port, const char *dst)
   {
     ssh_cmd = GNUNET_strdup (ssh_cmd);
     ssh_cmd_cp = ssh_cmd;
-    for (cnt = 0; NULL != (arg = strtok (ssh_cmd, " ")); ssh_cmd = NULL)
-      GNUNET_array_append (ssh_args, cnt, GNUNET_strdup (arg));
+    for (size = 0; NULL != (arg = strtok (ssh_cmd, " ")); ssh_cmd = NULL)
+      GNUNET_array_append (ssh_args, size, GNUNET_strdup (arg));
     GNUNET_free (ssh_cmd_cp);
   }
   else
   {
     ssh_args = copy_argv (default_ssh_args);
-    cnt = (sizeof (default_ssh_args)) / (sizeof (const char *));
-    GNUNET_array_grow (ssh_args, cnt, cnt - 1);
+    size = (sizeof (default_ssh_args)) / (sizeof (const char *));
+    GNUNET_array_grow (ssh_args, size, size - 1);
   }
-  GNUNET_array_append (ssh_args, cnt, GNUNET_strdup ("-p"));
-  GNUNET_array_append (ssh_args, cnt, GNUNET_strdup (port));
-  GNUNET_array_append (ssh_args, cnt, GNUNET_strdup (dst));
-  GNUNET_array_append (ssh_args, cnt, NULL);
+  for (cnt = 0; cnt < size; cnt++)
+  {
+    arg = ssh_args[cnt];
+    if ('%' != arg[0])
+      continue;
+    switch (arg[1])
+    {
+    case 'p':
+      new_arg = port;
+      break;
+
+    case 'u':
+      new_arg = username;
+      break;
+
+    case 'h':
+      new_arg = hostname;
+      break;
+
+    default:
+      continue;
+    }
+    if (NULL == new_arg)
+      continue;
+    GNUNET_free (arg);
+    ssh_args[cnt] = GNUNET_strdup (new_arg);
+  }
+  GNUNET_array_append (ssh_args, size, NULL);
   return ssh_args;
 }
 
@@ -988,7 +930,7 @@ gen_rsh_suffix_args (const char * const *append_args)
   }
   if (NULL != append_args)
   {
-    for (append_cnt = 0; NULL != append_args[append_cnt]; append_cnt++)      
+    for (append_cnt = 0; NULL != append_args[append_cnt]; append_cnt++)
       GNUNET_array_append (rshell_args, cnt, GNUNET_strdup (append_args[append_cnt]));
   }
   GNUNET_array_append (rshell_args, cnt, NULL);
@@ -1066,6 +1008,7 @@ clear_msg (void *cls, int result)
   GNUNET_assert (NULL != cp->shandle);
   cp->shandle = NULL;
   GNUNET_free (cp->msg);
+  cp->msg = NULL;
 }
 
 
@@ -1127,7 +1070,7 @@ GNUNET_TESTBED_controller_start (const char *trusted_ip,
   static char *const binary_argv[] = {
     HELPER_TESTBED_BINARY, NULL
   };
-  
+
   GNUNET_assert (NULL != host);
   GNUNET_assert (NULL != (cfg = GNUNET_TESTBED_host_get_cfg_ (host)));
   hostname = NULL;
@@ -1150,32 +1093,37 @@ GNUNET_TESTBED_controller_start (const char *trusted_ip,
     char **rsh_suffix_args;
     const char *username;
     char *port;
-    char *dst;
-
-    username = GNUNET_TESTBED_host_get_username_ (host);
-    hostname = GNUNET_TESTBED_host_get_hostname (host);
-    GNUNET_asprintf (&port, "%u", GNUNET_TESTBED_host_get_ssh_port_ (host));
-    if (NULL == username)
-      GNUNET_asprintf (&dst, "%s", hostname);
-    else
-      GNUNET_asprintf (&dst, "%s@%s", username, hostname);
-    LOG_DEBUG ("Starting SSH to destination %s\n", dst);
-
+    char *argstr;
+    char *aux;
+    unsigned int cnt;
+
+    username = host->username;
+    hostname = host->hostname;
+    GNUNET_asprintf (&port, "%u", host->port);
+    LOG_DEBUG ("Starting remote connection to destination %s\n", hostname);
     if (GNUNET_OK !=
-        GNUNET_CONFIGURATION_get_value_string (cfg, "testbed",
+        GNUNET_CONFIGURATION_get_value_filename (cfg, "testbed",
                                                "HELPER_BINARY_PATH",
                                                &helper_binary_path_args[0]))
       helper_binary_path_args[0] =
           GNUNET_OS_get_libexec_binary_path (HELPER_TESTBED_BINARY);
     helper_binary_path_args[1] = NULL;
-    rsh_args = gen_rsh_args (port, dst);
+    rsh_args = gen_rsh_args (port, hostname, username);
     rsh_suffix_args = gen_rsh_suffix_args ((const char **) helper_binary_path_args);
     cp->helper_argv =
         join_argv ((const char **) rsh_args, (const char **) rsh_suffix_args);
     free_argv (rsh_args);
     free_argv (rsh_suffix_args);
     GNUNET_free (port);
-    GNUNET_free (dst);
+    argstr = GNUNET_strdup ("");
+    for (cnt = 0; NULL != cp->helper_argv[cnt]; cnt++)
+    {
+      aux = argstr;
+      GNUNET_assert (0 < GNUNET_asprintf (&argstr, "%s %s", aux, cp->helper_argv[cnt]));
+      GNUNET_free (aux);
+    }
+    LOG_DEBUG ("Helper cmd str: %s\n", argstr);
+    GNUNET_free (argstr);
     cp->helper =
         GNUNET_HELPER_start (GNUNET_NO, cp->helper_argv[0], cp->helper_argv, &helper_mst,
                              &helper_exp_cb, cp);
@@ -1237,6 +1185,7 @@ GNUNET_TESTBED_controller_destroy_ (struct GNUNET_TESTBED_ControllerProc *cproc)
     free_argv (cproc->helper_argv);
   cproc->host->controller_started = GNUNET_NO;
   cproc->host->locked = GNUNET_NO;
+  GNUNET_free_non_null (cproc->msg);
   GNUNET_free (cproc);
 }
 
@@ -1374,29 +1323,22 @@ GNUNET_TESTBED_is_host_habitable (const struct GNUNET_TESTBED_Host *host,
   char *stat_args[3];
   const char *hostname;
   char *port;
-  char *dst;
 
   h = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_HostHabitableCheckHandle));
   h->cb = cb;
   h->cb_cls = cb_cls;
   h->host = host;
   hostname = (NULL == host->hostname) ? "127.0.0.1" : host->hostname;
-  if (NULL == host->username)
-    dst = GNUNET_strdup (hostname);
-  else
-    GNUNET_asprintf (&dst, "%s@%s", host->username, hostname);
   if (GNUNET_OK !=
-      GNUNET_CONFIGURATION_get_value_string (config, "testbed",
+      GNUNET_CONFIGURATION_get_value_filename (config, "testbed",
                                              "HELPER_BINARY_PATH",
                                              &stat_args[1]))
     stat_args[1] =
-        GNUNET_OS_get_libexec_binary_path (HELPER_TESTBED_BINARY);  
+        GNUNET_OS_get_libexec_binary_path (HELPER_TESTBED_BINARY);
   GNUNET_asprintf (&port, "%u", host->port);
-  rsh_args = gen_rsh_args (port, dst);
+  rsh_args = gen_rsh_args (port, hostname, host->username);
   GNUNET_free (port);
-  GNUNET_free (dst);
   port = NULL;
-  dst = NULL;
   stat_args[0] = "stat";
   stat_args[2] = NULL;
   rsh_suffix_args = gen_rsh_suffix_args ((const char **) stat_args);
@@ -1432,7 +1374,7 @@ GNUNET_TESTBED_is_host_habitable_cancel (struct
                                          *handle)
 {
   GNUNET_SCHEDULER_cancel (handle->habitability_check_task);
-  (void) GNUNET_OS_process_kill (handle->auxp, SIGTERM);
+  (void) GNUNET_OS_process_kill (handle->auxp, GNUNET_TERM_SIG);
   (void) GNUNET_OS_process_wait (handle->auxp);
   GNUNET_OS_process_destroy (handle->auxp);
   free_argv (handle->helper_argv);
@@ -1581,12 +1523,12 @@ GNUNET_TESTBED_cancel_registration (struct GNUNET_TESTBED_HostRegistrationHandle
  *
  * @param h the host handle
  * @param op the operation to queue in the given host's parally overlay connect
- *          queue 
+ *          queue
  */
 void
-GNUNET_TESTBED_host_queue_oc_ (struct GNUNET_TESTBED_Host *h, 
+GNUNET_TESTBED_host_queue_oc_ (struct GNUNET_TESTBED_Host *h,
                                struct GNUNET_TESTBED_Operation *op)
-{  
+{
   GNUNET_TESTBED_operation_queue_insert_
       (h->opq_parallel_overlay_connect_operations, op);
 }