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