stub
[oweals/gnunet.git] / src / fs / fs_namespace.c
1 /*
2      This file is part of GNUnet
3      (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 Christian Grothoff (and other contributing authors)
4
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 3, or (at your
8      option) any later version.
9
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.
14
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.
19 */
20
21 /**
22  * @file fs/fs_namespace.c
23  * @brief create and destroy namespaces
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_constants.h"
28 #include "gnunet_signatures.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_fs_service.h"
31 #include "fs.h"
32
33 /**
34  * Return the name of the directory in which we store
35  * our local namespaces (or rather, their public keys).
36  *
37  * @param h global fs handle 
38  * @return NULL on error, otherwise the name of the directory
39  */
40 static char *
41 get_namespace_directory (struct GNUNET_FS_Handle *h)
42 {
43   char *dn;
44
45   if (GNUNET_OK !=
46       GNUNET_CONFIGURATION_get_value_filename (h->cfg,
47                                                "FS",
48                                                "IDENTITY_DIR",
49                                                &dn))
50     {
51       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
52                   _("Configuration fails to specify `%s' in section `%s'\n"),
53                   "IDENTITY_DIR",
54                   "fs");
55       return NULL;
56     }
57   return dn;
58 }
59
60
61 /**
62  * Context for advertising a namespace.
63  */
64 struct AdvertisementContext
65 {
66   /**
67    * Function to call with the result.
68    */
69   GNUNET_FS_PublishContinuation cont;
70
71   /**
72    * Closure for cont.
73    */
74   void *cont_cls;
75
76   /**
77    * Datastore handle.
78    */
79   struct GNUNET_DATASTORE_Handle *dsh;
80
81   /**
82    * Our scheduler.
83    */
84   struct GNUNET_SCHEDULER_Handle *sched;
85
86   /**
87    * Our KSK URI.
88    */ 
89   struct GNUNET_FS_Uri *ksk_uri;
90
91   /**
92    * Plaintext.
93    */
94   char *pt;
95
96   /**
97    * NBlock to sign and store.
98    */
99   struct NBlock *nb;
100
101   /**
102    * The namespace.
103    */
104   struct GNUNET_FS_Namespace *ns;
105
106   /**
107    * Expiration time.
108    */
109   struct GNUNET_TIME_Absolute expiration;
110
111   /**
112    * Number of bytes of plaintext.
113    */ 
114   size_t pt_size;
115
116   /**
117    * Anonymity level.
118    */
119   uint32_t anonymity;
120
121   /**
122    * Content priority.
123    */
124   uint32_t priority;
125
126   /**
127    * Current keyword offset.
128    */
129   unsigned int pos;
130 };
131
132
133 /**
134  * Disconnect from the datastore.
135  * 
136  * @param cls datastore handle
137  * @param tc scheduler context
138  */
139 static void
140 do_disconnect (void *cls,
141                const struct GNUNET_SCHEDULER_TaskContext *tc)
142 {
143   struct GNUNET_DATASTORE_Handle *dsh = cls;
144
145   GNUNET_DATASTORE_disconnect (dsh, 
146                                GNUNET_NO);
147 }
148
149
150 /**
151  * Continuation called to notify client about result of the
152  * operation.
153  *
154  * @param cls closure (our struct AdvertismentContext)
155  * @param success GNUNET_SYSERR on failure
156  * @param msg NULL on success, otherwise an error message
157  */
158 static void
159 advertisement_cont (void *cls,
160                     int success,
161                     const char *msg)
162 {
163   struct AdvertisementContext *ac = cls;
164   const char *keyword;
165   GNUNET_HashCode key;
166   GNUNET_HashCode query;
167   struct GNUNET_CRYPTO_AesSessionKey skey;
168   struct GNUNET_CRYPTO_AesInitializationVector iv;
169   struct GNUNET_CRYPTO_RsaPrivateKey *pk;
170   
171   if (GNUNET_OK != success)
172     {
173       /* error! */
174       GNUNET_SCHEDULER_add_continuation (ac->sched,
175                                          &do_disconnect,
176                                          ac->dsh,
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);
183       GNUNET_free (ac);
184       return;
185     }
186   if (ac->pos == ac->ksk_uri->data.ksk.keywordCount)
187     {
188       /* done! */
189       GNUNET_SCHEDULER_add_continuation (ac->sched,
190                                          &do_disconnect,
191                                          ac->dsh,
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);
198       GNUNET_free (ac);
199       return;
200     }
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,
207                              ac->pt_size,
208                              &skey,
209                              &iv,
210                              &ac->nb[1]);
211   GNUNET_break (GNUNET_OK == 
212                 GNUNET_CRYPTO_rsa_sign (ac->ns->key,
213                                         &ac->nb->ns_purpose,
214                                         &ac->nb->ns_signature));
215   pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&key);
216   GNUNET_assert (pk != NULL);
217   GNUNET_CRYPTO_rsa_key_get_public (pk, &ac->nb->keyspace);
218   GNUNET_CRYPTO_hash (&ac->nb->keyspace,
219                       sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
220                       &query);
221   GNUNET_break (GNUNET_OK == 
222                 GNUNET_CRYPTO_rsa_sign (pk,
223                                         &ac->nb->ksk_purpose,
224                                         &ac->nb->ksk_signature));
225   GNUNET_CRYPTO_rsa_key_free (pk);
226   GNUNET_DATASTORE_put (ac->dsh,
227                         0 /* no reservation */, 
228                         &query,
229                         ac->pt_size + sizeof (struct NBlock),
230                         ac->nb,
231                         GNUNET_BLOCK_TYPE_NBLOCK,
232                         ac->priority,
233                         ac->anonymity,
234                         ac->expiration,
235                         -2, 1,
236                         GNUNET_CONSTANTS_SERVICE_TIMEOUT, 
237                         &advertisement_cont,
238                         ac);
239 }
240
241
242 /**
243  * Publish an advertismement for a namespace.  
244  *
245  * @param h handle to the file sharing subsystem
246  * @param ksk_uri keywords to use for advertisment
247  * @param namespace handle for the namespace that should be advertised
248  * @param meta meta-data for the namespace advertisement
249  * @param anonymity for the namespace advertismement
250  * @param priority for the namespace advertisement
251  * @param expiration for the namespace advertisement
252  * @param rootEntry name of the root of the namespace
253  * @param cont continuation
254  * @param cont_cls closure for cont
255  */
256 void
257 GNUNET_FS_namespace_advertise (struct GNUNET_FS_Handle *h,
258                                struct GNUNET_FS_Uri *ksk_uri,
259                                struct GNUNET_FS_Namespace *namespace,
260                                const struct GNUNET_CONTAINER_MetaData *meta,
261                                uint32_t anonymity,
262                                uint32_t priority,
263                                struct GNUNET_TIME_Absolute expiration,
264                                const char *rootEntry,
265                                GNUNET_FS_PublishContinuation cont,
266                                void *cont_cls)
267 {
268   size_t reslen;
269   size_t size;
270   ssize_t mdsize;
271   struct NBlock *nb;
272   char *mdst;
273   struct GNUNET_DATASTORE_Handle *dsh;
274   struct AdvertisementContext *ctx;
275   char *pt;
276
277   /* create advertisements */
278   mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
279   if (-1 == mdsize)
280     {
281       cont (cont_cls, NULL, _("Failed to serialize meta data"));
282       return;
283     }
284   reslen = strlen (rootEntry) + 1;
285   size = mdsize + sizeof (struct NBlock) + reslen;
286   if (size > MAX_NBLOCK_SIZE)
287     {
288       size = MAX_NBLOCK_SIZE;
289       mdsize = size - sizeof (struct NBlock) - reslen;
290     }
291
292   pt = GNUNET_malloc (mdsize + reslen);
293   memcpy (pt, rootEntry, reslen);
294   mdst = &pt[reslen];
295   mdsize = GNUNET_CONTAINER_meta_data_serialize (meta,
296                                                  &mdst,
297                                                  mdsize,
298                                                  GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
299   if (mdsize == -1)
300     {
301       GNUNET_break (0);
302       GNUNET_free (pt);
303       cont (cont_cls, NULL, _("Failed to serialize meta data"));
304       return;
305     }
306   size = mdsize + sizeof (struct NBlock) + reslen;  
307   nb = GNUNET_malloc (size);
308   GNUNET_CRYPTO_rsa_key_get_public (namespace->key, 
309                                     &nb->subspace);
310   nb->ns_purpose.size = htonl (mdsize + reslen + 
311                             sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
312                             sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
313   nb->ns_purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK);
314   nb->ksk_purpose.size = htonl (size - sizeof (struct GNUNET_CRYPTO_RsaSignature));
315   nb->ksk_purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK_KSIG);
316   dsh = GNUNET_DATASTORE_connect (h->cfg, h->sched);
317   if (NULL == dsh)
318     {
319       GNUNET_free (nb);
320       GNUNET_free (pt);
321       cont (cont_cls, NULL, _("Failed to connect to datastore service"));
322       return;
323     }  
324   ctx = GNUNET_malloc (sizeof (struct AdvertisementContext));
325   ctx->cont = cont;
326   ctx->cont_cls = cont_cls;
327   ctx->dsh = dsh;
328   ctx->sched = h->sched;
329   ctx->ksk_uri = GNUNET_FS_uri_dup (ksk_uri);
330   ctx->nb = nb;
331   ctx->pt = pt;
332   ctx->pt_size = mdsize + reslen;
333   ctx->ns = namespace;
334   ctx->ns->rc++;
335   ctx->anonymity = anonymity;
336   ctx->priority = priority;
337   ctx->expiration = expiration;
338   advertisement_cont (ctx, GNUNET_OK, NULL);
339 }
340
341
342 /**
343  * Create a namespace with the given name; if one already
344  * exists, return a handle to the existing namespace.
345  *
346  * @param h handle to the file sharing subsystem
347  * @param name name to use for the namespace
348  * @return handle to the namespace, NULL on error
349  */
350 struct GNUNET_FS_Namespace *
351 GNUNET_FS_namespace_create (struct GNUNET_FS_Handle *h,
352                             const char *name)
353 {
354   char *dn;
355   char *fn;
356   struct GNUNET_FS_Namespace *ret;
357
358   dn = get_namespace_directory (h);
359   GNUNET_asprintf (&fn,
360                    "%s%s%s",
361                    dn,
362                    DIR_SEPARATOR_STR,
363                    name);
364   GNUNET_free (dn);
365   ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Namespace));
366   ret->rc = 1;
367   ret->key = GNUNET_CRYPTO_rsa_key_create_from_file (fn);
368   if (ret->key == NULL)
369     {
370       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
371                   _("Failed to create or read private key for namespace `%s'\n"),
372                   name);
373       GNUNET_free (ret);
374       GNUNET_free (fn);
375       return NULL;
376     }
377   ret->name = GNUNET_strdup (name);
378   ret->filename = fn;
379   return ret;
380 }
381
382
383 /**
384  * Delete a namespace handle.  Can be used for a clean shutdown (free
385  * memory) or also to freeze the namespace to prevent further
386  * insertions by anyone.
387  *
388  * @param namespace handle to the namespace that should be deleted / freed
389  * @param freeze prevents future insertions; creating a namespace
390  *        with the same name again will create a fresh namespace instead
391  *
392  * @return GNUNET_OK on success, GNUNET_SYSERR on error
393  */
394 int 
395 GNUNET_FS_namespace_delete (struct GNUNET_FS_Namespace *namespace,
396                             int freeze)
397 {
398   namespace->rc--;
399   if (freeze)
400     {
401       if (0 != UNLINK (namespace->filename))
402         GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
403                                   "unlink",
404                                   namespace->filename);      
405     }
406   if (0 == namespace->rc)
407     {
408       GNUNET_CRYPTO_rsa_key_free (namespace->key);
409       GNUNET_free (namespace->filename);
410       GNUNET_free (namespace->name);
411       GNUNET_free (namespace);
412     }
413   return GNUNET_OK;
414 }
415
416
417 /**
418  * Context for the 'process_namespace' callback.
419  * Specifies a function to call on each namespace.
420  */
421 struct ProcessNamespaceContext
422 {
423   /**
424    * Function to call.
425    */
426   GNUNET_FS_NamespaceInfoProcessor cb;
427
428   /**
429    * Closure for 'cb'.
430    */
431   void *cb_cls;
432 };
433
434
435 /**
436  * Function called with a filename of a namespace. Reads the key and
437  * calls the callback.
438  *
439  * @param cls closure (struct ProcessNamespaceContext)
440  * @param filename complete filename (absolute path)
441  * @return GNUNET_OK to continue to iterate,
442  *  GNUNET_SYSERR to abort iteration with error!
443  */
444 static int
445 process_namespace (void *cls, 
446                    const char *filename)
447 {
448   struct ProcessNamespaceContext *pnc = cls;
449   struct GNUNET_CRYPTO_RsaPrivateKey *key;
450   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
451   GNUNET_HashCode id;
452   const char *name;
453   const char *t;
454
455   key = GNUNET_CRYPTO_rsa_key_create_from_file (filename);
456   if (key == NULL)
457     {
458       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
459                   _("Failed to read namespace private key file `%s', deleting it!\n"),
460                   filename);
461       if (0 != UNLINK (filename))
462         GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
463                                   "unlink",
464                                   filename);
465       return GNUNET_OK;
466     }
467   GNUNET_CRYPTO_rsa_key_get_public (key, &pk);
468   GNUNET_CRYPTO_rsa_key_free (key);
469   GNUNET_CRYPTO_hash (&pk, sizeof(pk), &id); 
470   name = filename;
471   while (NULL != (t = strstr (name, DIR_SEPARATOR_STR)))
472     name = t + 1;
473   pnc->cb (pnc->cb_cls,
474            name,
475            &id);
476   return GNUNET_OK;
477 }
478
479
480 /**
481  * Build a list of all available local (!) namespaces The returned
482  * names are only the nicknames since we only iterate over the local
483  * namespaces.
484  *
485  * @param h handle to the file sharing subsystem
486  * @param cb function to call on each known namespace
487  * @param cb_cls closure for cb
488  */
489 void 
490 GNUNET_FS_namespace_list (struct GNUNET_FS_Handle *h,
491                           GNUNET_FS_NamespaceInfoProcessor cb,
492                           void *cb_cls)
493 {
494   char *dn;
495   struct ProcessNamespaceContext ctx;
496   
497   dn = get_namespace_directory (h);
498   if (dn == NULL)
499     return;
500   ctx.cb = cb;
501   ctx.cb_cls = cb_cls;
502   GNUNET_DISK_directory_scan (dn,
503                               &process_namespace,
504                               &ctx);
505   GNUNET_free (dn);
506 }
507
508
509
510 /**
511  * List all of the identifiers in the namespace for 
512  * which we could produce an update.
513  *
514  * @param namespace namespace to inspect for updateable content
515  * @param ip function to call on each updateable identifier
516  * @param ip_cls closure for ip
517  */
518 void
519 GNUNET_FS_namespace_list_updateable (struct GNUNET_FS_Namespace *namespace,
520                                      GNUNET_FS_IdentifierProcessor ip, 
521                                      void *ip_cls)
522 {
523   GNUNET_break (0);
524 }
525
526
527
528 /* end of fs_namespace.c */
529