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