authentication of ciphertexts (+ seed)
[oweals/gnunet.git] / src / fs / gnunet-pseudonym.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2004, 2005, 2006, 2007, 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  * @file fs/gnunet-pseudonym.c
22  * @brief manage GNUnet namespaces / pseudonyms
23  * @author Christian Grothoff
24  */
25 #include "platform.h"
26 #include "gnunet_fs_service.h"
27
28 /**
29  * -a optiton.
30  */
31 static unsigned int anonymity;
32
33 /**
34  * -C option
35  */
36 static char *create_ns;
37
38 /**
39  * -D option
40  */
41 static char *delete_ns;
42
43 /**
44  * -k option
45  */
46 static struct GNUNET_FS_Uri *ksk_uri;
47
48 /**
49  * -l option.
50  */
51 static int print_local_only;
52
53 /**
54  * -m option.
55  */
56 static struct GNUNET_CONTAINER_MetaData *adv_metadata;
57
58 /**
59  * -p option.
60  */
61 static unsigned int priority = 365;
62
63 /**
64  * -q option given.
65  */
66 static int no_remote_printing; 
67
68 /**
69  * -r option.
70  */
71 static char *root_identifier;
72
73 /**
74  * -s option.
75  */
76 static char *rating_change;
77
78 /**
79  * Handle to fs service.
80  */
81 static struct GNUNET_FS_Handle *h;
82
83 /**
84  * Namespace we are looking at.
85  */
86 static struct GNUNET_FS_Namespace *ns;
87
88 /**
89  * Our configuration.
90  */
91 static const struct GNUNET_CONFIGURATION_Handle *cfg;
92
93 static int ret;
94
95 static void* 
96 progress_cb (void *cls,
97              const struct GNUNET_FS_ProgressInfo *info)
98 {
99   return NULL;
100 }
101
102
103 static void
104 ns_printer (void *cls,
105             const char *name,
106             const GNUNET_HashCode *id)
107 {
108   struct GNUNET_CRYPTO_HashAsciiEncoded enc;
109
110   GNUNET_CRYPTO_hash_to_enc (id, &enc);
111   fprintf (stdout, 
112            "%s (%s)\n",
113            name,
114            (const char*) &enc);
115 }
116
117
118 static int
119 pseudo_printer (void *cls,
120                 const GNUNET_HashCode *
121                 pseudonym,
122                 const struct
123                 GNUNET_CONTAINER_MetaData * md,
124                 int rating)
125 {
126   char *id;
127
128   id = GNUNET_PSEUDONYM_id_to_name (cfg,
129                                     pseudonym);
130   if (id == NULL)
131     {
132       GNUNET_break (0);
133       return GNUNET_OK;
134     }
135   fprintf (stdout, 
136            "%s (%d):\n",
137            id,
138            rating);
139   GNUNET_CONTAINER_meta_data_iterate (md,
140                                       &EXTRACTOR_meta_data_print, 
141                                       stdout);
142   fprintf (stdout, "\n");
143   GNUNET_free (id);
144   return GNUNET_OK;
145 }
146
147
148 static void
149 post_advertising (void *cls,
150                   const struct GNUNET_FS_Uri *uri,
151                   const char *emsg)
152 {
153   GNUNET_HashCode nsid;
154   char *set;
155   int delta;
156
157   if (emsg != NULL)
158     {
159       fprintf (stderr, "%s", emsg);
160       ret = 1;
161     }
162   if (ns != NULL)
163     {
164       if (GNUNET_OK !=
165           GNUNET_FS_namespace_delete (ns,
166                                       GNUNET_NO))
167         ret = 1;
168     }
169   if (NULL != rating_change)
170     {
171       set = rating_change;
172       while ((*set != '\0') && (*set != ':'))
173         set++;
174       if (*set != ':')
175         {
176           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
177                       _("Invalid argument `%s'\n"),
178                       rating_change);
179         }
180       else
181         {
182           *set = '\0';
183           delta = strtol (&set[1], NULL, /* no error handling yet */
184                           10);
185           if (GNUNET_OK ==
186               GNUNET_PSEUDONYM_name_to_id (cfg,
187                                            rating_change,
188                                            &nsid))
189             {
190               (void) GNUNET_PSEUDONYM_rank (cfg,
191                                             &nsid,
192                                             delta);           
193             }
194           else
195             {
196               GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
197                           _("Namespace `%s' unknown.\n"),
198                           rating_change);
199             }
200         }
201       GNUNET_free (rating_change);
202       rating_change = NULL;
203     }
204   if (0 != print_local_only)
205     {
206       GNUNET_FS_namespace_list (h,
207                                 &ns_printer, 
208                                 NULL);
209     }  
210   else if (0 == no_remote_printing)
211     {
212       GNUNET_PSEUDONYM_list_all (cfg,
213                                  &pseudo_printer,
214                                  NULL);
215     }
216   GNUNET_FS_stop (h);
217 }
218
219
220 /**
221  * Main function that will be run by the scheduler.
222  *
223  * @param cls closure
224  * @param sched the scheduler to use
225  * @param args remaining command-line arguments
226  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
227  * @param c configuration
228  */
229 static void
230 run (void *cls,
231      struct GNUNET_SCHEDULER_Handle *sched,
232      char *const *args,
233      const char *cfgfile,
234      const struct GNUNET_CONFIGURATION_Handle *c)
235 {
236   struct GNUNET_TIME_Absolute expiration;
237   char *emsg;
238
239   cfg = c;
240   h = GNUNET_FS_start (sched,
241                        cfg,
242                        "gnunet-pseudonym",
243                        &progress_cb,
244                        NULL,
245                        GNUNET_FS_FLAGS_NONE,
246                        GNUNET_FS_OPTIONS_END);
247   if (NULL != delete_ns)
248     {
249       ns = GNUNET_FS_namespace_create (h, delete_ns);
250       if (ns == NULL)
251         {
252           ret = 1;
253         }
254       else
255         {
256           if (GNUNET_OK !=
257               GNUNET_FS_namespace_delete (ns,
258                                           GNUNET_YES))
259             ret = 1;
260           ns = NULL;
261         }
262     }
263   if (NULL != create_ns)
264     {
265       ns = GNUNET_FS_namespace_create (h, create_ns);
266       if (ns == NULL)
267         {
268           ret = 1;
269         }
270       else
271         {
272           if (NULL != root_identifier)
273             {
274               expiration = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_YEARS);
275               if (ksk_uri == NULL)
276                 {
277                   emsg = NULL;
278                   ksk_uri = GNUNET_FS_uri_parse ("gnunet://fs/ksk/namespace", &emsg);
279                   GNUNET_assert (NULL == emsg);
280                 }
281               GNUNET_FS_namespace_advertise (h,
282                                              ksk_uri,
283                                              ns,
284                                              adv_metadata,
285                                              anonymity,
286                                              priority,                                       
287                                              expiration,
288                                              root_identifier,
289                                              &post_advertising,
290                                              NULL);
291               return;
292             }
293           else
294             {
295               if (ksk_uri != NULL)
296                 fprintf (stderr, _("Option `%s' ignored\n"), "-k");   
297             }
298         }
299     }
300   else
301     {
302       if (root_identifier != NULL) 
303         fprintf (stderr, _("Option `%s' ignored\n"), "-r");
304       if (ksk_uri != NULL)
305         fprintf (stderr, _("Option `%s' ignored\n"), "-k");   
306     }    
307     
308   post_advertising (NULL, NULL, NULL);
309 }
310
311
312 /**
313  * The main function to inspect GNUnet directories.
314  *
315  * @param argc number of arguments from the command line
316  * @param argv command line arguments
317  * @return 0 ok, 1 on error
318  */
319 int
320 main (int argc, char *const *argv)
321 {
322   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
323     {'a', "anonymity", "LEVEL",
324      gettext_noop ("set the desired LEVEL of sender-anonymity"),
325      1, &GNUNET_GETOPT_set_uint, &anonymity},
326     {'C', "create", "NAME",
327      gettext_noop
328      ("create or advertise namespace NAME"),
329      1, &GNUNET_GETOPT_set_string, &create_ns},
330     {'D', "delete", "NAME",
331      gettext_noop
332      ("delete namespace NAME "),
333      1, &GNUNET_GETOPT_set_string, &delete_ns},
334     {'k', "keyword", "VALUE",
335      gettext_noop
336      ("add an additional keyword for the advertisment"
337       " (this option can be specified multiple times)"),
338      1, &GNUNET_FS_getopt_set_keywords, &ksk_uri},
339     {'m', "meta", "TYPE:VALUE",
340      gettext_noop ("set the meta-data for the given TYPE to the given VALUE"),
341      1, &GNUNET_FS_getopt_set_metadata, &adv_metadata},
342     {'o', "only-local", NULL,
343      gettext_noop ("print names of local namespaces"),
344      0, &GNUNET_GETOPT_set_one, &print_local_only},
345     {'p', "priority", "PRIORITY",
346      gettext_noop ("use the given PRIORITY for the advertisments"),
347      1, &GNUNET_GETOPT_set_uint, &priority},
348     {'q', "quiet", NULL,
349      gettext_noop ("do not print names of remote namespaces"),
350      0, &GNUNET_GETOPT_set_one, &no_remote_printing},
351     {'r', "root", "ID",
352      gettext_noop
353      ("specify ID of the root of the namespace"),
354      1, &GNUNET_GETOPT_set_string, &root_identifier},
355     {'s', "set-rating", "ID:VALUE",
356      gettext_noop
357      ("change rating of namespace ID by VALUE"),
358      1, &GNUNET_GETOPT_set_string, &rating_change},
359     GNUNET_GETOPT_OPTION_END
360   };
361   return (GNUNET_OK ==
362           GNUNET_PROGRAM_run (argc,
363                               argv,
364                               "gnunet-pseudonym",
365                               gettext_noop
366                               ("Manage GNUnet pseudonyms."),
367                               options, &run, NULL)) ? ret : 1;
368 }
369
370 /* end of gnunet-pseudonym.c */