-only trigger check config if we actually need it
[oweals/gnunet.git] / src / fs / fs_uri.c
index cce420f0dcb3853193b9497b1cb5c980fed0eace..12cae6b64484d592c82575643c934200802604b9 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2003--2014 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2003--2014 GNUnet e.V.
 
      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.
 */
 
 /**
@@ -272,7 +272,6 @@ uri_ksk_parse (const char *s,
   char *dup;
   int saw_quote;
 
-  GNUNET_assert (NULL != s);
   slen = strlen (s);
   pos = strlen (GNUNET_FS_URI_KSK_PREFIX);
   if ((slen <= pos) || (0 != strncmp (s, GNUNET_FS_URI_KSK_PREFIX, pos)))
@@ -316,7 +315,6 @@ uri_ksk_parse (const char *s,
     if ((s[i] == '%') && (&s[i] == strstr (&s[i], "%22")))
     {
       saw_quote = (saw_quote + 1) % 2;
-      i += 3;
       continue;
     }
     if ((dup[i] == '+') && (saw_quote == 0))
@@ -364,7 +362,6 @@ uri_sks_parse (const char *s,
   size_t pos;
   char *end;
 
-  GNUNET_assert (s != NULL);
   pos = strlen (GNUNET_FS_URI_SKS_PREFIX);
   if ((strlen (s) <= pos) || (0 != strncmp (s, GNUNET_FS_URI_SKS_PREFIX, pos)))
     return NULL;                /* not an SKS URI */
@@ -409,9 +406,6 @@ uri_chk_parse (const char *s,
   char h1[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)];
   char h2[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)];
 
-  if (NULL == s)
-    return NULL;
-  GNUNET_assert (s != NULL);
   slen = strlen (s);
   pos = strlen (GNUNET_FS_URI_CHK_PREFIX);
   if ((slen < pos + 2 * sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) + 1) ||
@@ -630,6 +624,13 @@ GNUNET_FS_uri_parse (const char *uri,
   struct GNUNET_FS_Uri *ret;
   char *msg;
 
+  if (NULL == uri)
+  {
+    GNUNET_break (0);
+    if (NULL != emsg)
+      *emsg = GNUNET_strdup (_("invalid argument"));
+    return NULL;
+  }
   if (NULL == emsg)
     emsg = &msg;
   *emsg = NULL;
@@ -838,59 +839,46 @@ GNUNET_FS_uri_loc_get_uri (const struct GNUNET_FS_Uri *uri)
 
 /**
  * Construct a location URI (this peer will be used for the location).
+ * This function should only be called from within gnunet-service-fs,
+ * as it requires the peer's private key which is generally unavailable
+ * to processes directly under the user's control.  However, for
+ * testing and as it logically fits under URIs, it is in this API.
  *
- * @param baseUri content offered by the sender
- * @param cfg configuration information (used to find our hostkey)
+ * @param base_uri content offered by the sender
+ * @param sign_key private key of the peer
  * @param expiration_time how long will the content be offered?
  * @return the location URI, NULL on error
  */
 struct GNUNET_FS_Uri *
-GNUNET_FS_uri_loc_create (const struct GNUNET_FS_Uri *baseUri,
-                          const struct GNUNET_CONFIGURATION_Handle *cfg,
+GNUNET_FS_uri_loc_create (const struct GNUNET_FS_Uri *base_uri,
+                          const struct GNUNET_CRYPTO_EddsaPrivateKey *sign_key,
                           struct GNUNET_TIME_Absolute expiration_time)
 {
   struct GNUNET_FS_Uri *uri;
-  struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key;
   struct GNUNET_CRYPTO_EddsaPublicKey my_public_key;
-  char *keyfile;
   struct LocUriAssembly ass;
   struct GNUNET_TIME_Absolute et;
 
-  if (baseUri->type != GNUNET_FS_URI_CHK)
-    return NULL;
-  if (GNUNET_OK !=
-      GNUNET_CONFIGURATION_get_value_filename (cfg, "PEER", "PRIVATE_KEY",
-                                               &keyfile))
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                _("Lacking key configuration settings.\n"));
+  if (GNUNET_FS_URI_CHK != base_uri->type)
     return NULL;
-  }
-  if (NULL == (my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_file (keyfile)))
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                _("Could not access hostkey file `%s'.\n"), keyfile);
-    GNUNET_free (keyfile);
-    return NULL;
-  }
-  GNUNET_free (keyfile);
   /* we round expiration time to full seconds for SKS URIs */
   et.abs_value_us = (expiration_time.abs_value_us / 1000000LL) * 1000000LL;
-  GNUNET_CRYPTO_eddsa_key_get_public (my_private_key, &my_public_key);
+  GNUNET_CRYPTO_eddsa_key_get_public (sign_key,
+                                      &my_public_key);
   ass.purpose.size = htonl (sizeof (struct LocUriAssembly));
   ass.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_PEER_PLACEMENT);
   ass.exptime = GNUNET_TIME_absolute_hton (et);
-  ass.fi = baseUri->data.chk;
+  ass.fi = base_uri->data.chk;
   ass.peer.public_key = my_public_key;
   uri = GNUNET_new (struct GNUNET_FS_Uri);
   uri->type = GNUNET_FS_URI_LOC;
-  uri->data.loc.fi = baseUri->data.chk;
+  uri->data.loc.fi = base_uri->data.chk;
   uri->data.loc.expirationTime = et;
   uri->data.loc.peer.public_key = my_public_key;
   GNUNET_assert (GNUNET_OK ==
-                 GNUNET_CRYPTO_eddsa_sign (my_private_key, &ass.purpose,
+                 GNUNET_CRYPTO_eddsa_sign (sign_key,
+                                           &ass.purpose,
                                            &uri->data.loc.contentSignature));
-  GNUNET_free (my_private_key);
   return uri;
 }