-LRN: initialize rd to avoid having garbage in flags
[oweals/gnunet.git] / src / namestore / gnunet-namestore.c
index f48d1c23dc96e77e13db0f0210e7cfa42c4386e8..5cac2e1336ab27e92a3b8213c183c829c43c2366 100644 (file)
  * @author Christian Grothoff
  *
  * TODO:
- * - printing records
  * - allow users to set record options (not just 'RF_AUTHORITY')
  * - test
- * - parsing SOA, PTR and MX value specifications (and define format!)
  * - add options to list/lookup individual records
  */
 #include "platform.h"
@@ -34,6 +32,7 @@
 #include <gnunet_dnsparser_lib.h>
 #include <gnunet_namestore_service.h>
 
+
 /**
  * Handle to the namestore.
  */
@@ -42,7 +41,7 @@ static struct GNUNET_NAMESTORE_Handle *ns;
 /**
  * Hash of the public key of our zone.
  */
-static GNUNET_HashCode zone;
+static struct GNUNET_CRYPTO_ShortHashCode zone;
 
 /**
  * Private key for the our zone.
@@ -64,6 +63,11 @@ static int add;
  */
 static struct GNUNET_NAMESTORE_QueueEntry *add_qe;
 
+/**
+ * Queue entry for the 'add-uri' operation.
+ */
+static struct GNUNET_NAMESTORE_QueueEntry *add_qe_uri;
+
 /**
  * Desired action is to list records.
  */
@@ -79,6 +83,16 @@ static struct GNUNET_NAMESTORE_ZoneIterator *list_it;
  */
 static int del;
 
+/**
+ * Is record public
+ */
+static int public;
+
+/**
+ * Is record authority
+ */
+static int nonauthority;
+
 /**
  * Queue entry for the 'del' operation.
  */
@@ -94,6 +108,11 @@ static char *name;
  */
 static char *value;
 
+/**
+ * URI to import.
+ */
+static char *uri;
+
 /**
  * Type of the record to add/remove, NULL to remove all.
  */
@@ -103,7 +122,12 @@ static char *typestring;
  * Desired expiration time.
  */
 static char *expirationstring;
-               
+
+/**
+ * Global return value
+ */
+static int ret;
+
 
 /**
  * Task run on shutdown.  Cleans up everything.
@@ -115,9 +139,29 @@ static void
 do_shutdown (void *cls,
             const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
+  if (NULL != list_it)
+  {
+    GNUNET_NAMESTORE_zone_iteration_stop (list_it);
+    list_it = NULL;
+  }
+  if (NULL != add_qe)
+  {
+    GNUNET_NAMESTORE_cancel (add_qe);
+    add_qe = NULL;
+  }
+  if (NULL != add_qe_uri)
+  {
+    GNUNET_NAMESTORE_cancel (add_qe_uri);
+    add_qe_uri = NULL;
+  }
+  if (NULL != del_qe)
+  {
+    GNUNET_NAMESTORE_cancel (del_qe);
+    del_qe = NULL;
+  }
   if (NULL != ns)
   {
-    GNUNET_NAMESTORE_disconnect (ns, GNUNET_NO);
+    GNUNET_NAMESTORE_disconnect (ns);
     ns = NULL;
   }
   if (NULL != zone_pkey)
@@ -125,6 +169,11 @@ do_shutdown (void *cls,
     GNUNET_CRYPTO_rsa_key_free (zone_pkey);
     zone_pkey = NULL;
   }
+  if (NULL != uri)
+  {
+    GNUNET_free (uri);
+    uri = NULL;
+  }
 }
 
 
@@ -132,7 +181,7 @@ do_shutdown (void *cls,
  * Continuation called to notify client about result of the
  * operation.
  *
- * @param cls closure, unused
+ * @param cls closure, location of the QueueEntry pointer to NULL out
  * @param success GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
  *                GNUNET_NO if content was already there
  *                GNUNET_YES (or other positive value) on success
@@ -143,12 +192,20 @@ add_continuation (void *cls,
                  int32_t success,
                  const char *emsg)
 {
-  add_qe = NULL;
-  if (success != GNUNET_YES)
+  struct GNUNET_NAMESTORE_QueueEntry **qe = cls;
+
+  *qe = NULL;
+  if (GNUNET_YES != success)
+  {
     fprintf (stderr,
             _("Adding record failed: %s\n"),
-            (success == GNUNET_NO) ? "record exists" : emsg);
-  if ( (NULL == del_qe) &&
+            (GNUNET_NO == success) ? "record exists" : emsg);
+    if (GNUNET_NO != success)
+      ret = 1;
+  }
+  if ( (NULL == add_qe) &&
+       (NULL == add_qe_uri) &&
+       (NULL == del_qe) &&
        (NULL == list_it) )
     GNUNET_SCHEDULER_shutdown ();
 }
@@ -175,6 +232,7 @@ del_continuation (void *cls,
             _("Deleting record failed: %s\n"),
             emsg);
   if ( (NULL == add_qe) &&
+       (NULL == add_qe_uri) &&
        (NULL == list_it) )
     GNUNET_SCHEDULER_shutdown ();
 }
@@ -208,11 +266,15 @@ display_record (void *cls,
   const char *typestring;
   char *s;
   unsigned int i;
+  char *etime;
+  struct GNUNET_TIME_Absolute aex;
+  struct GNUNET_TIME_Relative rex;
 
   if (NULL == name)
   {
     list_it = NULL;
     if ( (NULL == del_qe) &&
+        (NULL == add_qe_uri) &&
         (NULL == add_qe) )
       GNUNET_SCHEDULER_shutdown ();
     return;
@@ -232,7 +294,22 @@ display_record (void *cls,
               (unsigned int) rd[i].record_type);
       continue;
     }
-    FPRINTF (stdout, "\t%s: %s\n", typestring, s);
+    if (0 != (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION))
+    {
+      rex.rel_value = rd[i].expiration_time;
+      etime = GNUNET_STRINGS_relative_time_to_string (rex);
+    }
+    else
+    {
+      aex.abs_value = rd[i].expiration_time;
+      etime = GNUNET_STRINGS_absolute_time_to_string (aex);
+    }
+    FPRINTF (stdout, "\t%s: %s (%s %s)\n", typestring, s, 
+            (0 != (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION)) 
+            ? _(/* what follows is relative expiration */ "for at least")
+            : _(/* what follows is absolute expiration */ "until"),
+            etime);
+    GNUNET_free (etime);
     GNUNET_free (s);    
   }
   FPRINTF (stdout, "%s", "\n");
@@ -256,22 +333,35 @@ run (void *cls, char *const *args, const char *cfgfile,
   uint32_t type;
   void *data = NULL;
   size_t data_size = 0;
-  struct GNUNET_TIME_Relative etime;
+  struct GNUNET_TIME_Relative etime_rel;
+  struct GNUNET_TIME_Absolute etime_abs;
+  int etime_is_rel = GNUNET_SYSERR;
   struct GNUNET_NAMESTORE_RecordData rd;
 
+  if ( (NULL != args[0]) && (NULL == uri) )
+    uri = GNUNET_strdup (args[0]);
   if (NULL == keyfile)
   {
+    if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
+                                                             "ZONEKEY", &keyfile))
+    {
+      fprintf (stderr,
+              _("Option `%s' not given, but I need a zone key file!\n"),
+              "z");
+      return;
+    }
     fprintf (stderr,
-            _("Option `%s' not given, but I need a zone key file!\n"),
-            "z");
-    return;
+            _("Using default zone file `%s'\n"),
+            keyfile);
   }
   zone_pkey = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
   GNUNET_free (keyfile);
   keyfile = NULL;
-  if (! (add|del|list))
+  if (! (add|del|list|(NULL != uri)))
   {
     /* nothing more to be done */  
+    fprintf (stderr,
+             _("No options given\n"));
     GNUNET_CRYPTO_rsa_key_free (zone_pkey);
     zone_pkey = NULL;
     return; 
@@ -284,7 +374,7 @@ run (void *cls, char *const *args, const char *cfgfile,
   }
   GNUNET_CRYPTO_rsa_key_get_public (zone_pkey,
                                    &pub);
-  GNUNET_CRYPTO_hash (&pub, sizeof (pub), &zone);
+  GNUNET_CRYPTO_short_hash (&pub, sizeof (pub), &zone);
 
   ns = GNUNET_NAMESTORE_connect (cfg);
   if (NULL == ns)
@@ -303,13 +393,16 @@ run (void *cls, char *const *args, const char *cfgfile,
   {
     fprintf (stderr, _("Unsupported type `%s'\n"), typestring);
     GNUNET_SCHEDULER_shutdown ();
+    ret = 1;
     return;
-  } else if (add | del)
+  }
+  if ((NULL == typestring) && (add | del))
   {
     fprintf (stderr,
             _("Missing option `%s' for operation `%s'\n"),
             "-t", _("add/del"));
     GNUNET_SCHEDULER_shutdown ();
+    ret = 1;
     return;     
   }
   if (NULL != value)
@@ -324,6 +417,7 @@ run (void *cls, char *const *args, const char *cfgfile,
                 value,
                 typestring);
        GNUNET_SCHEDULER_shutdown ();
+       ret = 1;
        return;
       }
   } else if (add | del)
@@ -331,29 +425,49 @@ run (void *cls, char *const *args, const char *cfgfile,
     fprintf (stderr,
             _("Missing option `%s' for operation `%s'\n"),
             "-V", _("add/del"));
+    ret = 1;   
     GNUNET_SCHEDULER_shutdown ();
     return;     
   }
   if (NULL != expirationstring)
   {
-    if (GNUNET_OK !=
-       GNUNET_STRINGS_fancy_time_to_relative (expirationstring,
-                                              &etime))
+    if (0 == strcmp (expirationstring, "never"))
+    {
+      etime_abs = GNUNET_TIME_UNIT_FOREVER_ABS;
+      etime_is_rel = GNUNET_NO;
+    }
+    else if (GNUNET_OK ==
+            GNUNET_STRINGS_fancy_time_to_relative (expirationstring,
+                                                   &etime_rel))
+    {
+      etime_is_rel = GNUNET_YES;
+    }
+    else if (GNUNET_OK == 
+            GNUNET_STRINGS_fancy_time_to_absolute (expirationstring,
+                                                   &etime_abs))
+    {
+      etime_is_rel = GNUNET_NO;
+    }
+    else
     {
       fprintf (stderr,
               _("Invalid time format `%s'\n"),
               expirationstring);
       GNUNET_SCHEDULER_shutdown ();
+      ret = 1;
       return;     
     }
-  } else if (add | del)
+  } 
+  else if (add)
   {
     fprintf (stderr,
             _("Missing option `%s' for operation `%s'\n"),
-            "-e", _("add/del"));
+            "-e", _("add"));
     GNUNET_SCHEDULER_shutdown ();
+    ret = 1;    
     return;     
   }
+  memset (&rd, 0, sizeof (rd));
   if (add)
   {
     if (NULL == name)
@@ -362,19 +476,38 @@ run (void *cls, char *const *args, const char *cfgfile,
               _("Missing option `%s' for operation `%s'\n"),
               "-n", _("add"));
       GNUNET_SCHEDULER_shutdown ();
+      ret = 1;    
       return;     
     }
     rd.data = data;
     rd.data_size = data_size;
     rd.record_type = type;
-    rd.expiration = GNUNET_TIME_relative_to_absolute (etime);
-    rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY; // FIXME: not always...
+    if (GNUNET_YES == etime_is_rel)
+    {
+      rd.expiration_time = etime_rel.rel_value;
+      rd.flags |= GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION;
+    }
+    else if (GNUNET_NO == etime_is_rel)
+      rd.expiration_time = etime_abs.abs_value;
+    else
+    {
+      fprintf (stderr,
+              _("No valid expiration time for operation `%s'\n"),
+              _("add"));
+      GNUNET_SCHEDULER_shutdown ();
+      ret = 1;
+      return;
+    }
+    if (1 != nonauthority)
+      rd.flags |= GNUNET_NAMESTORE_RF_AUTHORITY;
+    if (1 != public)
+      rd.flags |= GNUNET_NAMESTORE_RF_PRIVATE;
     add_qe = GNUNET_NAMESTORE_record_create (ns,
                                             zone_pkey,
                                             name,
                                             &rd,
                                             &add_continuation,
-                                            NULL);
+                                            &add_qe);
   }
   if (del)
   {
@@ -384,14 +517,15 @@ run (void *cls, char *const *args, const char *cfgfile,
               _("Missing option `%s' for operation `%s'\n"),
               "-n", _("del"));
       GNUNET_SCHEDULER_shutdown ();
+      ret = 1;
       return;     
     }
     rd.data = data;
     rd.data_size = data_size;
     rd.record_type = type;
-    rd.expiration = GNUNET_TIME_relative_to_absolute (etime);
-    rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY; // FIXME: not always...
-    del_qe = GNUNET_NAMESTORE_record_create (ns,
+    rd.expiration_time = 0;
+    rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
+    del_qe = GNUNET_NAMESTORE_record_remove (ns,
                                             zone_pkey,
                                             name,
                                             &rd,
@@ -400,12 +534,63 @@ run (void *cls, char *const *args, const char *cfgfile,
   }
   if (list)
   {
+    uint32_t must_not_flags = 0;
+
+    if (1 == nonauthority) /* List non-authority records */
+      must_not_flags |= GNUNET_NAMESTORE_RF_AUTHORITY;
+
+    if (1 == public)
+      must_not_flags |= GNUNET_NAMESTORE_RF_PRIVATE;
+
     list_it = GNUNET_NAMESTORE_zone_iteration_start (ns,
                                                     &zone,
-                                                    0, 0,
+                                                    GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION,
+                                                    must_not_flags,
                                                     &display_record,
                                                     NULL);
   }
+  if (NULL != uri)
+  {
+    char sh[53];
+    char name[64];
+    struct GNUNET_CRYPTO_ShortHashCode sc;
+
+    if ( (2 != (sscanf (uri,
+                       "gnunet://gns/%52s/%63s",
+                       sh,
+                       name)) ) ||
+        (GNUNET_OK !=
+         GNUNET_CRYPTO_short_hash_from_string (sh, &sc)) )
+    {
+      fprintf (stderr, 
+              _("Invalid URI `%s'\n"),
+              uri);
+      GNUNET_SCHEDULER_shutdown ();
+      ret = 1;
+      return;
+    }
+    rd.data = &sc;
+    rd.data_size = sizeof (struct GNUNET_CRYPTO_ShortHashCode);
+    rd.record_type = GNUNET_NAMESTORE_TYPE_PKEY;
+    if (GNUNET_YES == etime_is_rel)
+    {
+      rd.expiration_time = etime_rel.rel_value;
+      rd.flags |= GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION;
+    }
+    else if (GNUNET_NO == etime_is_rel)
+      rd.expiration_time = etime_abs.abs_value;
+    else    
+      rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value;
+    if (1 != nonauthority)
+      rd.flags |= GNUNET_NAMESTORE_RF_AUTHORITY;
+
+    add_qe_uri = GNUNET_NAMESTORE_record_create (ns,
+                                                zone_pkey,
+                                                name,
+                                                &rd,
+                                                &add_continuation,
+                                                &add_qe_uri);
+  }
   GNUNET_free_non_null (data);
 }
 
@@ -420,10 +605,13 @@ run (void *cls, char *const *args, const char *cfgfile,
 int
 main (int argc, char *const *argv)
 {
+  nonauthority = -1;
+  public = -1;
+
   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
     {'a', "add", NULL,
      gettext_noop ("add record"), 0,
-     &GNUNET_GETOPT_set_one, &add},   
+     &GNUNET_GETOPT_set_one, &add},
     {'d', "delete", NULL,
      gettext_noop ("delete record"), 0,
      &GNUNET_GETOPT_set_one, &del},   
@@ -431,7 +619,7 @@ main (int argc, char *const *argv)
      gettext_noop ("display records"), 0,
      &GNUNET_GETOPT_set_one, &list},   
     {'e', "expiration", "TIME",
-     gettext_noop ("expiration time to use (for adding only)"), 1,
+     gettext_noop ("expiration time for record to use (for adding only), \"never\" is possible"), 1,
      &GNUNET_GETOPT_set_string, &expirationstring},   
     {'n', "name", "NAME",
      gettext_noop ("name of the record to add/delete/display"), 1,
@@ -439,24 +627,34 @@ main (int argc, char *const *argv)
     {'t', "type", "TYPE",
      gettext_noop ("type of the record to add/delete/display"), 1,
      &GNUNET_GETOPT_set_string, &typestring},   
+    {'u', "uri", "URI",
+     gettext_noop ("URI to import into our zone"), 1,
+     &GNUNET_GETOPT_set_string, &uri},   
     {'V', "value", "VALUE",
      gettext_noop ("value of the record to add/delete"), 1,
      &GNUNET_GETOPT_set_string, &value},   
+    {'p', "public", NULL,
+     gettext_noop ("create or list public record"), 0,
+     &GNUNET_GETOPT_set_one, &public},
+    {'N', "non-authority", NULL,
+     gettext_noop ("create or list non-authority record"), 0,
+     &GNUNET_GETOPT_set_one, &nonauthority},
     {'z', "zonekey", "FILENAME",
      gettext_noop ("filename with the zone key"), 1,
      &GNUNET_GETOPT_set_string, &keyfile},   
     GNUNET_GETOPT_OPTION_END
   };
 
-  int ret;
+  if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
+    return 2;
 
   GNUNET_log_setup ("gnunet-namestore", "WARNING", NULL);
-  ret =
-      (GNUNET_OK ==
-       GNUNET_PROGRAM_run (argc, argv, "gnunet-namestore",
-                           _("GNUnet zone manipulation tool"), 
-                          options,
-                           &run, NULL)) ? 0 : 1;
+  if (GNUNET_OK !=
+      GNUNET_PROGRAM_run (argc, argv, "gnunet-namestore",
+                         _("GNUnet zone manipulation tool"), 
+                         options,
+                         &run, NULL))
+    return 1;
 
   return ret;
 }