psyc/social: local join flag; social service: leave place, save _file
[oweals/gnunet.git] / src / pt / test_gns_vpn.c
index 49a0954756a62091b21e436d591f76c391ad55a8..f1ddcddc23afb620beab12f88e805b4fda8c2cc3 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet
-     Copyright (C) 2007, 2009, 2011, 2012 Christian Grothoff
+     Copyright (C) 2007, 2009, 2011, 2012, 2015 Christian Grothoff
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -14,8 +14,8 @@
 
      You should have received a copy of the GNU General Public License
      along with GNUnet; see the file COPYING.  If not, write to the
-     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-     Boston, MA 02111-1307, USA.
+     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+     Boston, MA 02110-1301, USA.
 */
 
 /**
  * @author Martin Schanzenbach
  */
 #include "platform.h"
+#if HAVE_CURL_CURL_H
 #include <curl/curl.h>
+#elif HAVE_GNURL_CURL_H
+#include <gnurl/curl.h>
+#endif
 #include <microhttpd.h>
+#include "gnunet_identity_service.h"
 #include "gnunet_namestore_service.h"
 #include "gnunet_gnsrecord_lib.h"
 #include "gnunet_gns_service.h"
@@ -37,7 +42,7 @@
 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
 
 /**
- * Return value for 'main'.
+ * Return value for #main().
  */
 static int global_ret;
 
@@ -45,9 +50,13 @@ static struct GNUNET_NAMESTORE_Handle *namestore;
 
 static struct MHD_Daemon *mhd;
 
-static struct GNUNET_SCHEDULER_Task * mhd_task_id;
+static struct GNUNET_SCHEDULER_Task *mhd_task_id;
 
-static struct GNUNET_SCHEDULER_Task * curl_task_id;
+static struct GNUNET_SCHEDULER_Task *curl_task_id;
+
+static struct GNUNET_IDENTITY_Handle *identity;
+
+static struct GNUNET_NAMESTORE_QueueEntry *qe;
 
 static CURL *curl;
 
@@ -55,6 +64,8 @@ static CURLM *multi;
 
 static char *url;
 
+static struct GNUNET_PeerIdentity id;
+
 /**
  * IP address of the ultimate destination.
  */
@@ -129,7 +140,8 @@ mhd_ahc (void *cls,
 
 
 static void
-do_shutdown ()
+do_shutdown (void *cls,
+             const struct GNUNET_SCHEDULER_TaskContext *c)
 {
   if (mhd_task_id != NULL)
   {
@@ -146,6 +158,16 @@ do_shutdown ()
     MHD_stop_daemon (mhd);
     mhd = NULL;
   }
+  if (NULL != identity)
+  {
+    GNUNET_IDENTITY_disconnect (identity);
+    identity = NULL;
+  }
+  if (NULL != qe)
+  {
+    GNUNET_NAMESTORE_cancel (qe);
+    qe = NULL;
+  }
   GNUNET_free_non_null (url);
   url = NULL;
 }
@@ -217,7 +239,7 @@ curl_main ()
       global_ret = 3;
     }
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download complete, shutting down!\n");
-    do_shutdown ();
+    GNUNET_SCHEDULER_shutdown ();
     return;
   }
   GNUNET_assert (CURLM_OK == curl_multi_fdset (multi, &rs, &ws, &es, &max));
@@ -253,13 +275,15 @@ start_curl (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   curl_easy_setopt (curl, CURLOPT_WRITEDATA, &cbc);
   curl_easy_setopt (curl, CURLOPT_FAILONERROR, 1);
   curl_easy_setopt (curl, CURLOPT_TIMEOUT, 150L);
-  curl_easy_setopt (curl, CURLOPT_CONNECTTIMEOUT, 15L);
+  curl_easy_setopt (curl, CURLOPT_CONNECTTIMEOUT, 150L);
   curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1);
 
   multi = curl_multi_init ();
   GNUNET_assert (multi != NULL);
   GNUNET_assert (CURLM_OK == curl_multi_add_handle (multi, curl));
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Beginning HTTP download from `%s'\n", url);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Beginning HTTP download from `%s'\n",
+              url);
   curl_main ();
 }
 
@@ -285,18 +309,26 @@ disco_ns (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  *                specified target peer; NULL on error
  */
 static void
-commence_testing (void *cls, int32_t success, const char *emsg)
+commence_testing (void *cls,
+                  int32_t success,
+                  const char *emsg)
 {
+  qe = NULL;
   GNUNET_SCHEDULER_add_now (&disco_ns, NULL);
 
   if ((emsg != NULL) && (GNUNET_YES != success))
   {
     fprintf (stderr,
-            "NS failed to create record %s\n", emsg);
+            "NS failed to create record %s\n",
+             emsg);
     GNUNET_SCHEDULER_shutdown ();
     return;
   }
-  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10), &start_curl, NULL);
+  /* wait a little bit before downloading, as we just created the record */
+  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
+                                (GNUNET_TIME_UNIT_SECONDS, 1),
+                                &start_curl,
+                                NULL);
 }
 
 
@@ -356,70 +388,6 @@ mhd_main ()
 }
 
 
-static void
-run (void *cls,
-     const struct GNUNET_CONFIGURATION_Handle *cfg,
-     struct GNUNET_TESTING_Peer *peer)
-{
-  enum MHD_FLAG flags;
-  struct GNUNET_PeerIdentity id;
-  char *peername;
-  struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key;
-  struct GNUNET_GNSRECORD_Data rd;
-  char *rd_string;
-  char *zone_keyfile;
-
-  GNUNET_TESTING_peer_get_identity (peer, &id);
-  peername = GNUNET_strdup (GNUNET_i2s_full (&id));
-
-  namestore = GNUNET_NAMESTORE_connect (cfg);
-  GNUNET_assert (NULL != namestore);
-  flags = MHD_USE_DEBUG;
-  //if (GNUNET_YES == use_v6)
-  //  flags |= MHD_USE_IPv6;
-  mhd = MHD_start_daemon (flags,
-                         PORT,
-                         NULL, NULL,
-                         &mhd_ahc, NULL,
-                         MHD_OPTION_END);
-  GNUNET_assert (NULL != mhd);
-  mhd_main ();
-
-  if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
-                                                            "ZONEKEY",
-                                                            &zone_keyfile))
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                "Failed to get key from cfg\n");
-    GNUNET_free (peername);
-    return;
-  }
-
-  zone_key = GNUNET_CRYPTO_ecdsa_key_create_from_file (zone_keyfile);
-  rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
-  GNUNET_asprintf (&rd_string,
-                   "6 %s %s",
-                   peername,
-                   "www.gnu.");
-  GNUNET_free (peername);
-  GNUNET_assert (GNUNET_OK ==
-                 GNUNET_GNSRECORD_string_to_value (GNUNET_GNSRECORD_TYPE_VPN,
-                                                   rd_string,
-                                                   (void**) &rd.data,
-                                                   &rd.data_size));
-  rd.record_type = GNUNET_GNSRECORD_TYPE_VPN;
-
-  GNUNET_NAMESTORE_records_store (namestore,
-                                 zone_key,
-                                 "www",
-                                 1, &rd,
-                                 &commence_testing,
-                                 NULL);
-  GNUNET_free ((void**)rd.data);
-  GNUNET_free (rd_string);
-  GNUNET_free (zone_keyfile);
-  GNUNET_free (zone_key);
-}
 
 
 /**
@@ -505,6 +473,237 @@ fork_and_exec (const char *file,
   return 0;
 }
 
+
+
+/**
+ * Method called to inform about the egos of this peer.
+ *
+ * When used with #GNUNET_IDENTITY_connect, this function is
+ * initially called for all egos and then again whenever a
+ * ego's name changes or if it is deleted.  At the end of
+ * the initial pass over all egos, the function is once called
+ * with 'NULL' for @a ego. That does NOT mean that the callback won't
+ * be invoked in the future or that there was an error.
+ *
+ * When used with #GNUNET_IDENTITY_create or #GNUNET_IDENTITY_get,
+ * this function is only called ONCE, and 'NULL' being passed in
+ * @a ego does indicate an error (i.e. name is taken or no default
+ * value is known).  If @a ego is non-NULL and if '*ctx'
+ * is set in those callbacks, the value WILL be passed to a subsequent
+ * call to the identity callback of #GNUNET_IDENTITY_connect (if
+ * that one was not NULL).
+ *
+ * When an identity is renamed, this function is called with the
+ * (known) @a ego but the NEW @a name.
+ *
+ * When an identity is deleted, this function is called with the
+ * (known) ego and "NULL" for the @a name.  In this case,
+ * the @a ego is henceforth invalid (and the @a ctx should also be
+ * cleaned up).
+ *
+ * @param cls closure
+ * @param ego ego handle
+ * @param ctx context for application to store data for this ego
+ *                 (during the lifetime of this process, initially NULL)
+ * @param name name assigned by the user for this ego,
+ *                   NULL if the user just deleted the ego and it
+ *                   must thus no longer be used
+ */
+static void
+identity_cb (void *cls,
+             struct GNUNET_IDENTITY_Ego *ego,
+             void **ctx,
+             const char *name)
+{
+  const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key;
+  struct GNUNET_GNSRECORD_Data rd;
+  char *rd_string;
+  char *peername;
+
+  if (NULL == name)
+    return;
+  if (NULL == ego)
+  {
+    if (NULL == qe)
+    {
+      fprintf (stderr,
+               "Failed to find master-zone ego\n");
+      GNUNET_SCHEDULER_shutdown ();
+      return;
+    }
+    GNUNET_IDENTITY_disconnect (identity);
+    identity = NULL;
+    return;
+  }
+  GNUNET_assert (NULL != name);
+  if (0 != strcmp (name,
+                   "master-zone"))
+  {
+    fprintf (stderr,
+             "Unexpected name %s\n",
+             name);
+    return;
+  }
+  zone_key = GNUNET_IDENTITY_ego_get_private_key (ego);
+  rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
+  peername = GNUNET_strdup (GNUNET_i2s_full (&id));
+  GNUNET_asprintf (&rd_string,
+                   "6 %s %s",
+                   peername,
+                   "www");
+  GNUNET_free (peername);
+  GNUNET_assert (GNUNET_OK ==
+                 GNUNET_GNSRECORD_string_to_value (GNUNET_GNSRECORD_TYPE_VPN,
+                                                   rd_string,
+                                                   (void**) &rd.data,
+                                                   &rd.data_size));
+  rd.record_type = GNUNET_GNSRECORD_TYPE_VPN;
+
+  qe = GNUNET_NAMESTORE_records_store (namestore,
+                                       zone_key,
+                                       "www",
+                                       1, &rd,
+                                       &commence_testing,
+                                       NULL);
+  GNUNET_free ((void**)rd.data);
+  GNUNET_free (rd_string);
+}
+
+
+static void
+run (void *cls,
+     const struct GNUNET_CONFIGURATION_Handle *cfg,
+     struct GNUNET_TESTING_Peer *peer)
+{
+  enum MHD_FLAG flags;
+
+  char *bin;
+  char *bin_identity;
+  char *bin_gns;
+  char *config;
+
+  if (GNUNET_OK !=
+      GNUNET_CONFIGURATION_get_value_string (cfg,
+                                             "arm",
+                                             "CONFIG",
+                                             &config))
+  {
+    fprintf (stderr,
+             "Failed to locate configuration file. Skipping test.\n");
+    GNUNET_SCHEDULER_shutdown ();
+    return;
+  }
+
+  char *const identity_args[] =
+  {
+    "gnunet-identity",
+    "-C", "master-zone",
+    "-c", config,
+    NULL
+  };
+  char *const identity2_args[] =
+  {
+    "gnunet-identity",
+    "-e", "master-zone",
+    "-s", "gns-master",
+    "-c", config,
+    NULL
+  };
+  char *const identity3_args[] =
+  {
+    "gnunet-identity",
+    "-e", "master-zone",
+    "-s", "gns-intercept",
+    "-c", config,
+    NULL
+  };
+  char *const gns_args[] =
+  {
+    "gnunet-gns",
+    "-u", "www.gns",
+    "-c", config,
+    NULL
+  };
+  GNUNET_TESTING_peer_get_identity (peer, &id);
+  GNUNET_SCHEDULER_add_delayed (TIMEOUT,
+                                &do_shutdown,
+                                NULL);
+  bin = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_BINDIR);
+  GNUNET_asprintf (&bin_identity,
+                   "%s/%s",
+                   bin,
+                   "gnunet-identity");
+  if (0 != fork_and_exec (bin_identity, identity_args))
+  {
+    fprintf (stderr,
+             "Failed to run `gnunet-identity -C. Skipping test.\n");
+    GNUNET_SCHEDULER_shutdown ();
+    GNUNET_free (bin_identity);
+    GNUNET_free (config);
+    GNUNET_free (bin);
+    return;
+  }
+  if (0 != fork_and_exec (bin_identity, identity2_args))
+  {
+    fprintf (stderr,
+             "Failed to run `gnunet-identity -e. Skipping test.\n");
+    GNUNET_SCHEDULER_shutdown ();
+    GNUNET_free (bin_identity);
+    GNUNET_free (config);
+    GNUNET_free (bin);
+    return;
+  }
+  if (0 != fork_and_exec (bin_identity, identity3_args))
+  {
+    fprintf (stderr,
+             "Failed to run `gnunet-identity -e. Skipping test.\n");
+    GNUNET_SCHEDULER_shutdown ();
+    GNUNET_free (bin_identity);
+    GNUNET_free (config);
+    GNUNET_free (bin);
+    return;
+  }
+  GNUNET_free (bin_identity);
+
+  /* do lookup just to launch GNS service */
+  GNUNET_asprintf (&bin_gns,
+                   "%s/%s",
+                   bin,
+                   "gnunet-gns");
+  if (0 != fork_and_exec (bin_gns, gns_args))
+  {
+    fprintf (stderr,
+             "Failed to run `gnunet-gns -u. Skipping test.\n");
+    GNUNET_SCHEDULER_shutdown ();
+    GNUNET_free (bin_gns);
+    GNUNET_free (config);
+    GNUNET_free (bin);
+    return;
+  }
+  GNUNET_free (bin_gns);
+  GNUNET_free (config);
+  GNUNET_free (bin);
+
+
+  namestore = GNUNET_NAMESTORE_connect (cfg);
+  GNUNET_assert (NULL != namestore);
+  flags = MHD_USE_DEBUG;
+  if (GNUNET_YES == use_v6)
+    flags |= MHD_USE_DUAL_STACK;
+  mhd = MHD_start_daemon (flags,
+                         PORT,
+                         NULL, NULL,
+                         &mhd_ahc, NULL,
+                         MHD_OPTION_END);
+  GNUNET_assert (NULL != mhd);
+  mhd_main ();
+
+  identity = GNUNET_IDENTITY_connect (cfg,
+                                      &identity_cb,
+                                      NULL);
+}
+
+
 int
 main (int argc, char *const *argv)
 {
@@ -512,6 +711,9 @@ main (int argc, char *const *argv)
   char *bin_vpn;
   char *bin_exit;
   char *bin_dns;
+  char *srv_dns;
+  struct stat s;
+  gid_t my_gid;
   char *const iptables_args[] =
   {
     "iptables", "-t", "mangle", "-L", "-v", NULL
@@ -549,6 +751,7 @@ main (int argc, char *const *argv)
   bin_vpn = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-vpn");
   bin_exit = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-exit");
   bin_dns = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-dns");
+  srv_dns = GNUNET_OS_get_libexec_binary_path ("gnunet-service-dns");
   if ( (0 != geteuid ()) &&
        ( (GNUNET_YES !=
          GNUNET_OS_check_helper_binary (bin_vpn, GNUNET_YES, "-d gnunet-vpn - - 169.1.3.3.7 255.255.255.0")) || //ipv4 only please!
@@ -564,11 +767,42 @@ main (int argc, char *const *argv)
     GNUNET_free (bin_vpn);
     GNUNET_free (bin_exit);
     GNUNET_free (bin_dns);
+    GNUNET_free (srv_dns);
     return 0;
   }
   GNUNET_free (bin_vpn);
   GNUNET_free (bin_exit);
+  my_gid = getgid ();
+  if ( (0 != stat (bin_dns, &s)) ||
+       (my_gid == s.st_gid) ||
+       ( (0 == (S_ISUID & s.st_mode)) && (0 != getuid()) ) )
+  {
+    fprintf (stderr,
+            "WARNING: %s has wrong permissions (%d, %d, %d), refusing to run test (as it would have to fail).\n",
+             bin_dns,
+             (0 != stat (bin_dns, &s)),
+             (my_gid == s.st_gid),
+             (0 == (S_ISUID & s.st_mode)) || (0 != getuid()) );
+    GNUNET_free (bin_dns);
+    GNUNET_free (srv_dns);
+    return 0;
+  }
+  if ( (0 != stat (srv_dns, &s)) ||
+       (my_gid == s.st_gid) ||
+       (0 == (S_ISGID & s.st_mode)) )
+  {
+    fprintf (stderr,
+            "WARNING: %s has wrong permissions (%d, %d, %d), refusing to run test (as it would have to fail).\n",
+             srv_dns,
+             (0 != stat (bin_dns, &s)),
+             (my_gid == s.st_gid),
+             (0 == (S_ISGID & s.st_mode)) );
+    GNUNET_free (bin_dns);
+    GNUNET_free (srv_dns);
+    return 0;
+  }
   GNUNET_free (bin_dns);
+  GNUNET_free (srv_dns);
 
   dest_ip = "169.254.86.1";
   dest_af = AF_INET;
@@ -591,6 +825,8 @@ main (int argc, char *const *argv)
     fprintf (stderr, "failed to initialize curl\n");
     return 2;
   }
+
+
   if (0 != GNUNET_TESTING_peer_run ("test-gnunet-vpn",
                                    "test_gns_vpn.conf",
                                    &run, NULL))