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_FS_Uri *ksk_uri;
92 * NBlock to sign and store.
99 struct GNUNET_FS_Namespace *ns;
104 struct GNUNET_TIME_Absolute expiration;
107 * Number of bytes of plaintext.
122 * Current keyword offset.
129 * Continuation called to notify client about result of the
132 * @param cls closure (our struct AdvertismentContext)
133 * @param success GNUNET_SYSERR on failure
134 * @param msg NULL on success, otherwise an error message
137 advertisement_cont (void *cls,
141 struct AdvertisementContext *ac = cls;
144 GNUNET_HashCode query;
145 struct GNUNET_CRYPTO_AesSessionKey skey;
146 struct GNUNET_CRYPTO_AesInitializationVector iv;
147 struct GNUNET_CRYPTO_RsaPrivateKey *pk;
149 if (GNUNET_OK != success)
152 GNUNET_DATASTORE_disconnect (ac->dsh, GNUNET_NO);
153 ac->cont (ac->cont_cls, NULL, msg);
154 GNUNET_FS_uri_destroy (ac->ksk_uri);
155 GNUNET_free (ac->pt);
156 GNUNET_free (ac->nb);
157 GNUNET_FS_namespace_delete (ac->ns, GNUNET_NO);
161 if (ac->pos == ac->ksk_uri->data.ksk.keywordCount)
164 GNUNET_DATASTORE_disconnect (ac->dsh, GNUNET_NO);
165 ac->cont (ac->cont_cls, ac->ksk_uri, NULL);
166 GNUNET_FS_uri_destroy (ac->ksk_uri);
167 GNUNET_free (ac->pt);
168 GNUNET_free (ac->nb);
169 GNUNET_FS_namespace_delete (ac->ns, GNUNET_NO);
173 keyword = ac->ksk_uri->data.ksk.keywords[ac->pos++];
174 /* first character of keyword indicates if it is
175 mandatory or not -- ignore for hashing */
176 GNUNET_CRYPTO_hash (&keyword[1], strlen (&keyword[1]), &key);
177 GNUNET_CRYPTO_hash_to_aes_key (&key, &skey, &iv);
178 GNUNET_CRYPTO_aes_encrypt (ac->pt,
183 GNUNET_break (GNUNET_OK ==
184 GNUNET_CRYPTO_rsa_sign (ac->ns->key,
186 &ac->nb->ns_signature));
187 pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&key);
188 GNUNET_CRYPTO_rsa_key_get_public (pk, &ac->nb->keyspace);
189 GNUNET_CRYPTO_hash (&ac->nb->keyspace,
190 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
192 GNUNET_break (GNUNET_OK ==
193 GNUNET_CRYPTO_rsa_sign (pk,
194 &ac->nb->ksk_purpose,
195 &ac->nb->ksk_signature));
196 GNUNET_CRYPTO_rsa_key_free (pk);
197 GNUNET_DATASTORE_put (ac->dsh,
198 0 /* no reservation */,
200 ac->pt_size + sizeof (struct NBlock),
202 GNUNET_DATASTORE_BLOCKTYPE_NBLOCK,
206 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
213 * Publish an advertismement for a namespace.
215 * @param h handle to the file sharing subsystem
216 * @param ksk_uri keywords to use for advertisment
217 * @param namespace handle for the namespace that should be advertised
218 * @param meta meta-data for the namespace advertisement
219 * @param anonymity for the namespace advertismement
220 * @param priority for the namespace advertisement
221 * @param expiration for the namespace advertisement
222 * @param rootEntry name of the root of the namespace
223 * @param cont continuation
224 * @param cont_cls closure for cont
227 GNUNET_FS_namespace_advertise (struct GNUNET_FS_Handle *h,
228 struct GNUNET_FS_Uri *ksk_uri,
229 struct GNUNET_FS_Namespace *namespace,
230 const struct GNUNET_CONTAINER_MetaData *meta,
233 struct GNUNET_TIME_Absolute expiration,
234 const char *rootEntry,
235 GNUNET_FS_PublishContinuation cont,
243 struct GNUNET_DATASTORE_Handle *dsh;
244 struct AdvertisementContext *ctx;
247 /* create advertisements */
248 mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
251 cont (cont_cls, NULL, _("Failed to serialize meta data"));
254 reslen = strlen (rootEntry) + 1;
255 size = mdsize + sizeof (struct NBlock) + reslen;
256 if (size > MAX_NBLOCK_SIZE)
258 size = MAX_NBLOCK_SIZE;
259 mdsize = size - sizeof (struct NBlock) - reslen;
262 pt = GNUNET_malloc (mdsize + reslen);
263 memcpy (pt, rootEntry, reslen);
265 mdsize = GNUNET_CONTAINER_meta_data_serialize (meta,
268 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
273 cont (cont_cls, NULL, _("Failed to serialize meta data"));
276 size = mdsize + sizeof (struct NBlock) + reslen;
277 nb = GNUNET_malloc (size);
278 GNUNET_CRYPTO_rsa_key_get_public (namespace->key,
280 nb->ns_purpose.size = htonl (mdsize + reslen +
281 sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
282 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
283 nb->ns_purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK);
284 nb->ksk_purpose.size = htonl (size - sizeof (struct GNUNET_CRYPTO_RsaSignature));
285 nb->ksk_purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK_KSIG);
286 dsh = GNUNET_DATASTORE_connect (h->cfg, h->sched);
291 cont (cont_cls, NULL, _("Failed to connect to datastore service"));
294 ctx = GNUNET_malloc (sizeof (struct AdvertisementContext));
296 ctx->cont_cls = cont_cls;
298 ctx->ksk_uri = GNUNET_FS_uri_dup (ksk_uri);
301 ctx->pt_size = mdsize + reslen;
304 ctx->anonymity = anonymity;
305 ctx->priority = priority;
306 ctx->expiration = expiration;
307 advertisement_cont (ctx, GNUNET_OK, NULL);
312 * Create a namespace with the given name; if one already
313 * exists, return a handle to the existing namespace.
315 * @param h handle to the file sharing subsystem
316 * @param name name to use for the namespace
317 * @return handle to the namespace, NULL on error
319 struct GNUNET_FS_Namespace *
320 GNUNET_FS_namespace_create (struct GNUNET_FS_Handle *h,
325 struct GNUNET_FS_Namespace *ret;
327 dn = get_namespace_directory (h);
328 GNUNET_asprintf (&fn,
334 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Namespace));
336 ret->key = GNUNET_CRYPTO_rsa_key_create_from_file (fn);
337 if (ret->key == NULL)
339 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
340 _("Failed to create or read private key for namespace `%s'\n"),
352 * Delete a namespace handle. Can be used for a clean shutdown (free
353 * memory) or also to freeze the namespace to prevent further
354 * insertions by anyone.
356 * @param namespace handle to the namespace that should be deleted / freed
357 * @param freeze prevents future insertions; creating a namespace
358 * with the same name again will create a fresh namespace instead
360 * @return GNUNET_OK on success, GNUNET_SYSERR on error
363 GNUNET_FS_namespace_delete (struct GNUNET_FS_Namespace *namespace,
369 if (0 != UNLINK (namespace->filename))
370 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
372 namespace->filename);
374 if (0 == namespace->rc)
376 GNUNET_CRYPTO_rsa_key_free (namespace->key);
377 GNUNET_free (namespace->filename);
378 GNUNET_free (namespace);
385 * Context for the 'process_namespace' callback.
386 * Specifies a function to call on each namespace.
388 struct ProcessNamespaceContext
393 GNUNET_FS_NamespaceInfoProcessor cb;
403 * Function called with a filename of a namespace. Reads the key and
404 * calls the callback.
406 * @param cls closure (struct ProcessNamespaceContext)
407 * @param filename complete filename (absolute path)
408 * @return GNUNET_OK to continue to iterate,
409 * GNUNET_SYSERR to abort iteration with error!
412 process_namespace (void *cls,
413 const char *filename)
415 struct ProcessNamespaceContext *pnc = cls;
416 struct GNUNET_CRYPTO_RsaPrivateKey *key;
417 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
422 key = GNUNET_CRYPTO_rsa_key_create_from_file (filename);
425 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
426 _("Failed to read namespace private key file `%s', deleting it!\n"),
428 if (0 != UNLINK (filename))
429 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
434 GNUNET_CRYPTO_rsa_key_get_public (key, &pk);
435 GNUNET_CRYPTO_rsa_key_free (key);
436 GNUNET_CRYPTO_hash (&pk, sizeof(pk), &id);
438 while (NULL != (t = strstr (name, DIR_SEPARATOR_STR)))
440 pnc->cb (pnc->cb_cls,
448 * Build a list of all available local (!) namespaces The returned
449 * names are only the nicknames since we only iterate over the local
452 * @param h handle to the file sharing subsystem
453 * @param cb function to call on each known namespace
454 * @param cb_cls closure for cb
457 GNUNET_FS_namespace_list (struct GNUNET_FS_Handle *h,
458 GNUNET_FS_NamespaceInfoProcessor cb,
462 struct ProcessNamespaceContext ctx;
464 dn = get_namespace_directory (h);
469 GNUNET_DISK_directory_scan (dn,
475 /* end of fs_namespace.c */