2 This file is part of GNUnet
3 (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file fs/fs_namespace.c
23 * @brief create and destroy namespaces
24 * @author Christian Grothoff
27 #include "gnunet_constants.h"
28 #include "gnunet_signatures.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_fs_service.h"
34 * Return the name of the directory in which we store
35 * our local namespaces (or rather, their public keys).
37 * @param h global fs handle
38 * @return NULL on error, otherwise the name of the directory
41 get_namespace_directory (struct GNUNET_FS_Handle *h)
46 GNUNET_CONFIGURATION_get_value_filename (h->cfg,
51 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
52 _("Configuration fails to specify `%s' in section `%s'\n"),
62 * Context for advertising a namespace.
64 struct AdvertisementContext
67 * Function to call with the result.
69 GNUNET_FS_PublishContinuation cont;
79 struct GNUNET_DATASTORE_Handle *dsh;
84 struct GNUNET_SCHEDULER_Handle *sched;
89 struct GNUNET_FS_Uri *ksk_uri;
97 * NBlock to sign and store.
104 struct GNUNET_FS_Namespace *ns;
109 struct GNUNET_TIME_Absolute expiration;
112 * Number of bytes of plaintext.
127 * Current keyword offset.
134 * Disconnect from the datastore.
136 * @param cls datastore handle
137 * @param tc scheduler context
140 do_disconnect (void *cls,
141 const struct GNUNET_SCHEDULER_TaskContext *tc)
143 struct GNUNET_DATASTORE_Handle *dsh = cls;
145 GNUNET_DATASTORE_disconnect (dsh,
151 * Continuation called to notify client about result of the
154 * @param cls closure (our struct AdvertismentContext)
155 * @param success GNUNET_SYSERR on failure
156 * @param msg NULL on success, otherwise an error message
159 advertisement_cont (void *cls,
163 struct AdvertisementContext *ac = cls;
166 GNUNET_HashCode query;
167 struct GNUNET_CRYPTO_AesSessionKey skey;
168 struct GNUNET_CRYPTO_AesInitializationVector iv;
169 struct GNUNET_CRYPTO_RsaPrivateKey *pk;
171 if (GNUNET_OK != success)
174 GNUNET_SCHEDULER_add_continuation (ac->sched,
177 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
178 ac->cont (ac->cont_cls, NULL, msg);
179 GNUNET_FS_uri_destroy (ac->ksk_uri);
180 GNUNET_free (ac->pt);
181 GNUNET_free (ac->nb);
182 GNUNET_FS_namespace_delete (ac->ns, GNUNET_NO);
186 if (ac->pos == ac->ksk_uri->data.ksk.keywordCount)
189 GNUNET_SCHEDULER_add_continuation (ac->sched,
192 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
193 ac->cont (ac->cont_cls, ac->ksk_uri, NULL);
194 GNUNET_FS_uri_destroy (ac->ksk_uri);
195 GNUNET_free (ac->pt);
196 GNUNET_free (ac->nb);
197 GNUNET_FS_namespace_delete (ac->ns, GNUNET_NO);
201 keyword = ac->ksk_uri->data.ksk.keywords[ac->pos++];
202 /* first character of keyword indicates if it is
203 mandatory or not -- ignore for hashing */
204 GNUNET_CRYPTO_hash (&keyword[1], strlen (&keyword[1]), &key);
205 GNUNET_CRYPTO_hash_to_aes_key (&key, &skey, &iv);
206 GNUNET_CRYPTO_aes_encrypt (ac->pt,
211 GNUNET_break (GNUNET_OK ==
212 GNUNET_CRYPTO_rsa_sign (ac->ns->key,
214 &ac->nb->ns_signature));
215 pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&key);
216 GNUNET_CRYPTO_rsa_key_get_public (pk, &ac->nb->keyspace);
217 GNUNET_CRYPTO_hash (&ac->nb->keyspace,
218 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
220 GNUNET_break (GNUNET_OK ==
221 GNUNET_CRYPTO_rsa_sign (pk,
222 &ac->nb->ksk_purpose,
223 &ac->nb->ksk_signature));
224 GNUNET_CRYPTO_rsa_key_free (pk);
225 GNUNET_DATASTORE_put (ac->dsh,
226 0 /* no reservation */,
228 ac->pt_size + sizeof (struct NBlock),
230 GNUNET_BLOCK_TYPE_NBLOCK,
235 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
242 * Publish an advertismement for a namespace.
244 * @param h handle to the file sharing subsystem
245 * @param ksk_uri keywords to use for advertisment
246 * @param namespace handle for the namespace that should be advertised
247 * @param meta meta-data for the namespace advertisement
248 * @param anonymity for the namespace advertismement
249 * @param priority for the namespace advertisement
250 * @param expiration for the namespace advertisement
251 * @param rootEntry name of the root of the namespace
252 * @param cont continuation
253 * @param cont_cls closure for cont
256 GNUNET_FS_namespace_advertise (struct GNUNET_FS_Handle *h,
257 struct GNUNET_FS_Uri *ksk_uri,
258 struct GNUNET_FS_Namespace *namespace,
259 const struct GNUNET_CONTAINER_MetaData *meta,
262 struct GNUNET_TIME_Absolute expiration,
263 const char *rootEntry,
264 GNUNET_FS_PublishContinuation cont,
272 struct GNUNET_DATASTORE_Handle *dsh;
273 struct AdvertisementContext *ctx;
276 /* create advertisements */
277 mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
280 cont (cont_cls, NULL, _("Failed to serialize meta data"));
283 reslen = strlen (rootEntry) + 1;
284 size = mdsize + sizeof (struct NBlock) + reslen;
285 if (size > MAX_NBLOCK_SIZE)
287 size = MAX_NBLOCK_SIZE;
288 mdsize = size - sizeof (struct NBlock) - reslen;
291 pt = GNUNET_malloc (mdsize + reslen);
292 memcpy (pt, rootEntry, reslen);
294 mdsize = GNUNET_CONTAINER_meta_data_serialize (meta,
297 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
302 cont (cont_cls, NULL, _("Failed to serialize meta data"));
305 size = mdsize + sizeof (struct NBlock) + reslen;
306 nb = GNUNET_malloc (size);
307 GNUNET_CRYPTO_rsa_key_get_public (namespace->key,
309 nb->ns_purpose.size = htonl (mdsize + reslen +
310 sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
311 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
312 nb->ns_purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK);
313 nb->ksk_purpose.size = htonl (size - sizeof (struct GNUNET_CRYPTO_RsaSignature));
314 nb->ksk_purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK_KSIG);
315 dsh = GNUNET_DATASTORE_connect (h->cfg, h->sched);
320 cont (cont_cls, NULL, _("Failed to connect to datastore service"));
323 ctx = GNUNET_malloc (sizeof (struct AdvertisementContext));
325 ctx->cont_cls = cont_cls;
327 ctx->sched = h->sched;
328 ctx->ksk_uri = GNUNET_FS_uri_dup (ksk_uri);
331 ctx->pt_size = mdsize + reslen;
334 ctx->anonymity = anonymity;
335 ctx->priority = priority;
336 ctx->expiration = expiration;
337 advertisement_cont (ctx, GNUNET_OK, NULL);
342 * Create a namespace with the given name; if one already
343 * exists, return a handle to the existing namespace.
345 * @param h handle to the file sharing subsystem
346 * @param name name to use for the namespace
347 * @return handle to the namespace, NULL on error
349 struct GNUNET_FS_Namespace *
350 GNUNET_FS_namespace_create (struct GNUNET_FS_Handle *h,
355 struct GNUNET_FS_Namespace *ret;
357 dn = get_namespace_directory (h);
358 GNUNET_asprintf (&fn,
364 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Namespace));
366 ret->key = GNUNET_CRYPTO_rsa_key_create_from_file (fn);
367 if (ret->key == NULL)
369 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
370 _("Failed to create or read private key for namespace `%s'\n"),
376 ret->name = GNUNET_strdup (name);
383 * Delete a namespace handle. Can be used for a clean shutdown (free
384 * memory) or also to freeze the namespace to prevent further
385 * insertions by anyone.
387 * @param namespace handle to the namespace that should be deleted / freed
388 * @param freeze prevents future insertions; creating a namespace
389 * with the same name again will create a fresh namespace instead
391 * @return GNUNET_OK on success, GNUNET_SYSERR on error
394 GNUNET_FS_namespace_delete (struct GNUNET_FS_Namespace *namespace,
400 if (0 != UNLINK (namespace->filename))
401 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
403 namespace->filename);
405 if (0 == namespace->rc)
407 GNUNET_CRYPTO_rsa_key_free (namespace->key);
408 GNUNET_free (namespace->filename);
409 GNUNET_free (namespace->name);
410 GNUNET_free (namespace);
417 * Context for the 'process_namespace' callback.
418 * Specifies a function to call on each namespace.
420 struct ProcessNamespaceContext
425 GNUNET_FS_NamespaceInfoProcessor cb;
435 * Function called with a filename of a namespace. Reads the key and
436 * calls the callback.
438 * @param cls closure (struct ProcessNamespaceContext)
439 * @param filename complete filename (absolute path)
440 * @return GNUNET_OK to continue to iterate,
441 * GNUNET_SYSERR to abort iteration with error!
444 process_namespace (void *cls,
445 const char *filename)
447 struct ProcessNamespaceContext *pnc = cls;
448 struct GNUNET_CRYPTO_RsaPrivateKey *key;
449 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
454 key = GNUNET_CRYPTO_rsa_key_create_from_file (filename);
457 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
458 _("Failed to read namespace private key file `%s', deleting it!\n"),
460 if (0 != UNLINK (filename))
461 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
466 GNUNET_CRYPTO_rsa_key_get_public (key, &pk);
467 GNUNET_CRYPTO_rsa_key_free (key);
468 GNUNET_CRYPTO_hash (&pk, sizeof(pk), &id);
470 while (NULL != (t = strstr (name, DIR_SEPARATOR_STR)))
472 pnc->cb (pnc->cb_cls,
480 * Build a list of all available local (!) namespaces The returned
481 * names are only the nicknames since we only iterate over the local
484 * @param h handle to the file sharing subsystem
485 * @param cb function to call on each known namespace
486 * @param cb_cls closure for cb
489 GNUNET_FS_namespace_list (struct GNUNET_FS_Handle *h,
490 GNUNET_FS_NamespaceInfoProcessor cb,
494 struct ProcessNamespaceContext ctx;
496 dn = get_namespace_directory (h);
501 GNUNET_DISK_directory_scan (dn,
507 /* end of fs_namespace.c */