summaryrefslogtreecommitdiff
path: root/src/fs/gnunet-pseudonym.c
blob: f585cf6ccd63a0fecc88a5bd819a96dad4b6dfd1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
     This file is part of GNUnet.
     (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009, 2010 Christian Grothoff (and other contributing authors)

     GNUnet is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 2, or (at your
     option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
*/
/**
 * @file fs/gnunet-pseudonym.c
 * @brief manage GNUnet namespaces / pseudonyms
 * @author Christian Grothoff
 */
#include "platform.h"
#include "gnunet_fs_service.h"

/**
 * -a optiton.
 */
static unsigned int anonymity;

/**
 * -A option.
 */
static int start_automate;

/**
 * -e option
 */
static int stop_automate;

/**
 * -C option
 */
static char *create_ns;

/**
 * -D option
 */
static char *delete_ns;

/**
 * -k option
 */
static struct GNUNET_FS_Uri *ksk_uri;

/**
 * -l option.
 */
static int print_local_only;

/**
 * -m option.
 */
static struct GNUNET_CONTAINER_MetaData *adv_metadata;

/**
 * -p option.
 */
static unsigned int priority = 365;

/**
 * -q option given.
 */
static int no_remote_printing; 

/**
 * -r option.
 */
static char *root_identifier;

/**
 * -s option.
 */
static char *rating_change;

/**
 * Handle to fs service.
 */
static struct GNUNET_FS_Handle *h;

/**
 * Namespace we are looking at.
 */
static struct GNUNET_FS_Namespace *ns;

/**
 * Our configuration.
 */
static const struct GNUNET_CONFIGURATION_Handle *cfg;

static int ret;

static void* 
progress_cb (void *cls,
	     const struct GNUNET_FS_ProgressInfo *info)
{
  return NULL;
}


static void
ns_printer (void *cls,
	    const char *name,
	    const GNUNET_HashCode *id)
{
  struct GNUNET_CRYPTO_HashAsciiEncoded enc;

  GNUNET_CRYPTO_hash_to_enc (id, &enc);
  fprintf (stdout, 
	   "%s (%s)\n",
	   name,
	   (const char*) &enc);
}


static int
pseudo_printer (void *cls,
		const GNUNET_HashCode *
		pseudonym,
		const struct
		GNUNET_CONTAINER_MetaData * md,
		int rating)
{
  char *id;

  id = GNUNET_PSEUDONYM_id_to_name (cfg,
				    pseudonym);
  if (id == NULL)
    {
      GNUNET_break (0);
      return GNUNET_OK;
    }
  fprintf (stdout, 
	   "%s (%d):\n",
	   id,
	   rating);
  GNUNET_CONTAINER_meta_data_iterate (md,
				      &EXTRACTOR_meta_data_print, 
				      stdout);
  fprintf (stdout, "\n");
  GNUNET_free (id);
  return GNUNET_OK;
}


static void
post_advertising (void *cls,
		  const struct GNUNET_FS_Uri *uri,
		  const char *emsg)
{
  GNUNET_HashCode nsid;
  char *set;
  int delta;

  if (emsg != NULL)
    {
      fprintf (stderr, "%s", emsg);
      ret = 1;
    }
  if (ns != NULL)
    {
      if (GNUNET_OK !=
	  GNUNET_FS_namespace_delete (ns,
				      GNUNET_NO))
	ret = 1;
    }
  if (0 != stop_automate)
    {
      GNUNET_break (0); // FIXME: not implemented
    }
  if (0 != start_automate)
    {
      GNUNET_break (0); // FIXME: not implemented
    }
  if (NULL != rating_change)
    {
      set = rating_change;
      while ((*set != '\0') && (*set != ':'))
        set++;
      if (*set != ':')
	{
	  GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
		      _("Invalid argument `%s'\n"),
		      rating_change);
	}
      else
	{
	  *set = '\0';
	  delta = strtol (&set[1], NULL, /* no error handling yet */
                          10);
	  if (GNUNET_OK ==
	      GNUNET_PSEUDONYM_name_to_id (cfg,
					   rating_change,
					   &nsid))
	    {
	      (void) GNUNET_PSEUDONYM_rank (cfg,
					    &nsid,
					    delta);	      
	    }
	  else
	    {
	      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
			  _("Namespace `%s' unknown.\n"),
			  rating_change);
	    }
	}
      GNUNET_free (rating_change);
      rating_change = NULL;
    }
  if (0 != print_local_only)
    {
      GNUNET_FS_namespace_list (h,
				&ns_printer, 
				NULL);
    }  
  else if (0 == no_remote_printing)
    {
      GNUNET_PSEUDONYM_list_all (cfg,
				 &pseudo_printer,
				 NULL);
    }
  GNUNET_FS_stop (h);
}


/**
 * Main function that will be run by the scheduler.
 *
 * @param cls closure
 * @param sched the scheduler to use
 * @param args remaining command-line arguments
 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
 * @param c configuration
 */
static void
run (void *cls,
     struct GNUNET_SCHEDULER_Handle *sched,
     char *const *args,
     const char *cfgfile,
     const struct GNUNET_CONFIGURATION_Handle *c)
{
  struct GNUNET_TIME_Absolute expiration;
  char *emsg;

  cfg = c;
  h = GNUNET_FS_start (sched,
		       cfg,
		       "gnunet-pseudonym",
		       &progress_cb,
		       NULL,
		       GNUNET_FS_FLAGS_NONE);
  if (NULL != delete_ns)
    {
      ns = GNUNET_FS_namespace_create (h, delete_ns);
      if (ns == NULL)
	{
	  ret = 1;
	}
      else
	{
	  if (GNUNET_OK !=
	      GNUNET_FS_namespace_delete (ns,
					  GNUNET_YES))
	    ret = 1;
	  ns = NULL;
	}
    }
  if (NULL != create_ns)
    {
      ns = GNUNET_FS_namespace_create (h, create_ns);
      if (ns == NULL)
	{
	  ret = 1;
	}
      else
	{
	  if (NULL != root_identifier)
	    {
	      expiration = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_YEARS);
	      if (ksk_uri == NULL)
		{
		  emsg = NULL;
		  ksk_uri = GNUNET_FS_uri_parse ("gnunet://fs/ksk/namespace", &emsg);
		  GNUNET_assert (NULL == emsg);
		}
	      GNUNET_FS_namespace_advertise (h,
					     ksk_uri,
					     ns,
					     adv_metadata,
					     anonymity,
					     priority,					     
					     expiration,
					     root_identifier,
					     &post_advertising,
					     NULL);
	      return;
	    }
	  else
	    {
	      if (ksk_uri != NULL)
		fprintf (stderr, _("Option `%s' ignored\n"), "-k");   
	    }
	}
    }
  else
    {
      if (root_identifier != NULL) 
	fprintf (stderr, _("Option `%s' ignored\n"), "-r");
      if (ksk_uri != NULL)
	fprintf (stderr, _("Option `%s' ignored\n"), "-k");   
    }    
    
  post_advertising (NULL, NULL, NULL);
}

/**
 * gnunet-pseudonym command line options
 */
static struct GNUNET_GETOPT_CommandLineOption options[] = {
  {'a', "anonymity", "LEVEL",
   gettext_noop ("set the desired LEVEL of sender-anonymity"),
   1, &GNUNET_GETOPT_set_uint, &anonymity},
  {'A', "automate", NULL,
   gettext_noop ("start a collection"),
   0, &GNUNET_GETOPT_set_one, &start_automate},
  {'C', "create", "NAME",
   gettext_noop
   ("create or advertise namespace NAME"),
   1, &GNUNET_GETOPT_set_string, &create_ns},
  {'D', "delete", "NAME",
   gettext_noop
   ("delete namespace NAME "),
   1, &GNUNET_GETOPT_set_string, &delete_ns},
  {'e', "end", NULL,
   gettext_noop ("end current collection"),
   0, &GNUNET_GETOPT_set_one, &stop_automate},
  {'k', "keyword", "VALUE",
  gettext_noop
   ("add an additional keyword for the advertisment"
    " (this option can be specified multiple times)"),
   1, &GNUNET_FS_getopt_set_keywords, &ksk_uri},
  {'m', "meta", "TYPE:VALUE",
   gettext_noop ("set the meta-data for the given TYPE to the given VALUE"),
   1, &GNUNET_FS_getopt_set_metadata, &adv_metadata},
  {'o', "only-local", NULL,
   gettext_noop ("print names of local namespaces"),
   0, &GNUNET_GETOPT_set_one, &print_local_only},
  {'p', "priority", "PRIORITY",
   gettext_noop ("use the given PRIORITY for the advertisments"),
   1, &GNUNET_GETOPT_set_uint, &priority},
  {'q', "quiet", NULL,
   gettext_noop ("do not print names of remote namespaces"),
   0, &GNUNET_GETOPT_set_one, &no_remote_printing},
  {'r', "root", "ID",
   gettext_noop
   ("specify ID of the root of the namespace"),
   1, &GNUNET_GETOPT_set_string, &root_identifier},
  {'s', "set-rating", "ID:VALUE",
   gettext_noop
   ("change rating of namespace ID by VALUE"),
   1, &GNUNET_GETOPT_set_string, &rating_change},
  GNUNET_GETOPT_OPTION_END
};


/**
 * The main function to inspect GNUnet directories.
 *
 * @param argc number of arguments from the command line
 * @param argv command line arguments
 * @return 0 ok, 1 on error
 */
int
main (int argc, char *const *argv)
{
  return (GNUNET_OK ==
          GNUNET_PROGRAM_run (argc,
                              argv,
                              "gnunet-pseudonym",
                              gettext_noop
                              ("Manage GNUnet pseudonyms."),
                              options, &run, NULL)) ? ret : 1;
}

/* end of gnunet-pseudonym.c */