fixes
[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 2, 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    * URI that was created.
83    */ 
84   struct GNUNET_FS_Uri *uri;
85 };
86
87
88 /**
89  * Continuation called to notify client about result of the
90  * operation.
91  *
92  * @param cls closure (our struct AdvertismentContext)
93  * @param success GNUNET_SYSERR on failure
94  * @param msg NULL on success, otherwise an error message
95  */
96 static void
97 advertisement_cont (void *cls,
98                     int success,
99                     const char *msg)
100 {
101   struct AdvertisementContext *ac = cls;
102   
103   if (GNUNET_OK != success)
104     ac->cont (ac->cont_cls, NULL, msg);
105   else
106     ac->cont (ac->cont_cls, ac->uri, NULL);
107   GNUNET_DATASTORE_disconnect (ac->dsh, GNUNET_NO);
108   GNUNET_FS_uri_destroy (ac->uri);
109   GNUNET_free (ac);
110 }
111
112
113 /**
114  * Publish an advertismement for a namespace.  
115  *
116  * @param h handle to the file sharing subsystem
117  * @param namespace handle for the namespace that should be advertised
118  * @param meta meta-data for the namespace advertisement
119  * @param anonymity for the namespace advertismement
120  * @param priority for the namespace advertisement
121  * @param expiration for the namespace advertisement
122  * @param rootEntry name of the root of the namespace
123  * @param cont continuation
124  * @param cont_cls closure for cont
125  */
126 void
127 GNUNET_FS_namespace_advertise (struct GNUNET_FS_Handle *h,
128                                struct GNUNET_FS_Namespace *namespace,
129                                const struct GNUNET_CONTAINER_MetaData *meta,
130                                uint32_t anonymity,
131                                uint32_t priority,
132                                struct GNUNET_TIME_Absolute expiration,
133                                const char *rootEntry,
134                                GNUNET_FS_PublishContinuation cont,
135                                void *cont_cls)
136 {
137   size_t reslen;
138   size_t size;
139   ssize_t mdsize;
140   struct NBlock *nb;
141   char *rtgt;
142   char *mdst;
143   struct GNUNET_DATASTORE_Handle *dsh;
144   struct AdvertisementContext *ctx;
145
146   /* create advertisements */
147   mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
148   if (-1 == mdsize)
149     {
150       cont (cont_cls, NULL, _("Failed to serialize meta data"));
151       return;
152     }
153   reslen = strlen (rootEntry) + 1;
154   size = mdsize + sizeof (struct NBlock) + reslen;
155   if (size > MAX_NBLOCK_SIZE)
156     {
157       size = MAX_NBLOCK_SIZE;
158       mdsize = size - sizeof (struct NBlock) - reslen;
159     }
160   nb = GNUNET_malloc (size);
161   GNUNET_CRYPTO_rsa_key_get_public (namespace->key, &nb->subspace);
162   rtgt = (char *) &nb[1];
163   memcpy (rtgt, rootEntry, reslen);
164   mdst = &rtgt[reslen];
165   mdsize = GNUNET_CONTAINER_meta_data_serialize (meta,
166                                                  &mdst,
167                                                  mdsize,
168                                                  GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
169   if (mdsize == -1)
170     {
171       GNUNET_break (0);
172       GNUNET_free (nb);
173       cont (cont_cls, NULL, _("Failed to serialize meta data"));
174       return;
175     }
176   size = mdsize + sizeof (struct NBlock) + reslen;
177   nb->purpose.size = htonl (size - sizeof (struct GNUNET_CRYPTO_RsaSignature));
178   nb->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK);
179   GNUNET_break (GNUNET_OK == 
180                 GNUNET_CRYPTO_rsa_sign (namespace->key,
181                                         &nb->purpose,
182                                         &nb->signature));
183   dsh = GNUNET_DATASTORE_connect (h->cfg, h->sched);
184   if (NULL == dsh)
185     {
186       GNUNET_free (nb);
187       cont (cont_cls, NULL, _("Failed to connect to datastore service"));
188       return;
189     }
190   ctx = GNUNET_malloc (sizeof (struct AdvertisementContext));
191   ctx->cont = cont;
192   ctx->cont_cls = cont_cls;
193   ctx->dsh = dsh;
194   ctx->uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
195   ctx->uri->type = sks;
196   ctx->uri->data.sks.identifier = GNUNET_strdup ("");
197   GNUNET_CRYPTO_hash (&nb->subspace,
198                       sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
199                       &ctx->uri->data.sks.namespace); 
200   GNUNET_DATASTORE_put (dsh,
201                         0, 
202                         &ctx->uri->data.sks.namespace,
203                         size,
204                         nb,
205                         GNUNET_DATASTORE_BLOCKTYPE_NBLOCK,
206                         priority,
207                         anonymity,
208                         expiration,
209                         GNUNET_CONSTANTS_SERVICE_TIMEOUT, 
210                         &advertisement_cont,
211                         ctx);
212 }
213
214
215 /**
216  * Create a namespace with the given name; if one already
217  * exists, return a handle to the existing namespace.
218  *
219  * @param h handle to the file sharing subsystem
220  * @param name name to use for the namespace
221  * @return handle to the namespace, NULL on error
222  */
223 struct GNUNET_FS_Namespace *
224 GNUNET_FS_namespace_create (struct GNUNET_FS_Handle *h,
225                             const char *name)
226 {
227   char *dn;
228   char *fn;
229   struct GNUNET_FS_Namespace *ret;
230
231   dn = get_namespace_directory (h);
232   GNUNET_asprintf (&fn,
233                    "%s%s%s",
234                    dn,
235                    DIR_SEPARATOR_STR,
236                    name);
237   GNUNET_free (dn);
238   ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Namespace));
239   ret->rc = 1;
240   ret->key = GNUNET_CRYPTO_rsa_key_create_from_file (fn);
241   if (ret->key == NULL)
242     {
243       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
244                   _("Failed to create or read private key for namespace `%s'\n"),
245                   name);
246       GNUNET_free (ret);
247       GNUNET_free (fn);
248       return NULL;
249     }
250   ret->filename = fn;
251   return ret;
252 }
253
254
255 /**
256  * Delete a namespace handle.  Can be used for a clean shutdown (free
257  * memory) or also to freeze the namespace to prevent further
258  * insertions by anyone.
259  *
260  * @param namespace handle to the namespace that should be deleted / freed
261  * @param freeze prevents future insertions; creating a namespace
262  *        with the same name again will create a fresh namespace instead
263  *
264  * @return GNUNET_OK on success, GNUNET_SYSERR on error
265  */
266 int 
267 GNUNET_FS_namespace_delete (struct GNUNET_FS_Namespace *namespace,
268                             int freeze)
269 {
270   namespace->rc--;
271   if (freeze)
272     {
273       if (0 != UNLINK (namespace->filename))
274         GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
275                                   "unlink",
276                                   namespace->filename);      
277     }
278   if (0 == namespace->rc)
279     {
280       GNUNET_CRYPTO_rsa_key_free (namespace->key);
281       GNUNET_free (namespace->filename);
282       GNUNET_free (namespace);
283     }
284   return GNUNET_OK;
285 }
286
287
288 /**
289  * Context for the 'process_namespace' callback.
290  * Specifies a function to call on each namespace.
291  */
292 struct ProcessNamespaceContext
293 {
294   /**
295    * Function to call.
296    */
297   GNUNET_FS_NamespaceInfoProcessor cb;
298
299   /**
300    * Closure for 'cb'.
301    */
302   void *cb_cls;
303 };
304
305
306 /**
307  * Function called with a filename of a namespace. Reads the key and
308  * calls the callback.
309  *
310  * @param cls closure (struct ProcessNamespaceContext)
311  * @param filename complete filename (absolute path)
312  * @return GNUNET_OK to continue to iterate,
313  *  GNUNET_SYSERR to abort iteration with error!
314  */
315 static int
316 process_namespace (void *cls, 
317                    const char *filename)
318 {
319   struct ProcessNamespaceContext *pnc = cls;
320   struct GNUNET_CRYPTO_RsaPrivateKey *key;
321   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
322   GNUNET_HashCode id;
323   const char *name;
324   const char *t;
325
326   key = GNUNET_CRYPTO_rsa_key_create_from_file (filename);
327   if (key == NULL)
328     {
329       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
330                   _("Failed to read namespace private key file `%s', deleting it!\n"),
331                   filename);
332       if (0 != UNLINK (filename))
333         GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
334                                   "unlink",
335                                   filename);
336       return GNUNET_OK;
337     }
338   GNUNET_CRYPTO_rsa_key_get_public (key, &pk);
339   GNUNET_CRYPTO_rsa_key_free (key);
340   GNUNET_CRYPTO_hash (&pk, sizeof(pk), &id); 
341   name = filename;
342   while (NULL != (t = strstr (name, DIR_SEPARATOR_STR)))
343     name = t + 1;
344   pnc->cb (pnc->cb_cls,
345            name,
346            &id);
347   return GNUNET_OK;
348 }
349
350
351 /**
352  * Build a list of all available local (!) namespaces The returned
353  * names are only the nicknames since we only iterate over the local
354  * namespaces.
355  *
356  * @param h handle to the file sharing subsystem
357  * @param cb function to call on each known namespace
358  * @param cb_cls closure for cb
359  */
360 void 
361 GNUNET_FS_namespace_list (struct GNUNET_FS_Handle *h,
362                           GNUNET_FS_NamespaceInfoProcessor cb,
363                           void *cb_cls)
364 {
365   char *dn;
366   struct ProcessNamespaceContext ctx;
367   
368   dn = get_namespace_directory (h);
369   if (dn == NULL)
370     return;
371   ctx.cb = cb;
372   ctx.cb_cls = cb_cls;
373   GNUNET_DISK_directory_scan (dn,
374                               &process_namespace,
375                               &ctx);
376   GNUNET_free (dn);
377 }
378
379 /* end of fs_namespace.c */
380