renaming PSEU to NICK
[oweals/gnunet.git] / src / namestore / gnunet-namestore.c
1
2 /*
3      This file is part of GNUnet.
4      (C) 2012, 2013 Christian Grothoff (and other contributing authors)
5
6      GNUnet is free software; you can redistribute it and/or modify
7      it under the terms of the GNU General Public License as published
8      by the Free Software Foundation; either version 3, or (at your
9      option) any later version.
10
11      GNUnet is distributed in the hope that it will be useful, but
12      WITHOUT ANY WARRANTY; without even the implied warranty of
13      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14      General Public License for more details.
15
16      You should have received a copy of the GNU General Public License
17      along with GNUnet; see the file COPYING.  If not, write to the
18      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19      Boston, MA 02111-1307, USA.
20 */
21 /**
22  * @file gnunet-namestore.c
23  * @brief command line tool to manipulate the local zone
24  * @author Christian Grothoff
25  *
26  * TODO:
27  * - test
28  */
29 #include "platform.h"
30 #include <gnunet_util_lib.h>
31 #include <gnunet_dnsparser_lib.h>
32 #include <gnunet_identity_service.h>
33 #include <gnunet_gnsrecord_lib.h>
34 #include <gnunet_namestore_service.h>
35
36
37 /**
38  * Handle to the namestore.
39  */
40 static struct GNUNET_NAMESTORE_Handle *ns;
41
42 /**
43  * Private key for the our zone.
44  */
45 static struct GNUNET_CRYPTO_EcdsaPrivateKey zone_pkey;
46
47 /**
48  * Handle to identity lookup.
49  */
50 static struct GNUNET_IDENTITY_EgoLookup *el;
51
52 /**
53  * Name of the ego controlling the zone.
54  */
55 static char *ego_name;
56
57 /**
58  * Desired action is to add a record.
59  */
60 static int add;
61
62 /**
63  * Queue entry for the 'add-uri' operation.
64  */
65 static struct GNUNET_NAMESTORE_QueueEntry *add_qe_uri;
66
67 /**
68  * Queue entry for the 'add' operation.
69  */
70 static struct GNUNET_NAMESTORE_QueueEntry *add_qe;
71
72 /**
73  * Queue entry for the 'reverse lookup' operation (in combination with a name).
74  */
75 static struct GNUNET_NAMESTORE_QueueEntry *reverse_qe;
76
77 /**
78  * Desired action is to list records.
79  */
80 static int list;
81
82 /**
83  * List iterator for the 'list' operation.
84  */
85 static struct GNUNET_NAMESTORE_ZoneIterator *list_it;
86
87 /**
88  * Desired action is to remove a record.
89  */
90 static int del;
91
92 /**
93  * Is record public (opposite of #GNUNET_GNSRECORD_RF_PRIVATE)
94  */
95 static int public;
96
97 /**
98  * Is record a shadow record (#GNUNET_GNSRECORD_RF_SHADOW_RECORD)
99  */
100 static int shadow;
101
102 /**
103  * Queue entry for the 'del' operation.
104  */
105 static struct GNUNET_NAMESTORE_QueueEntry *del_qe;
106
107 /**
108  * Name of the records to add/list/remove.
109  */
110 static char *name;
111
112 /**
113  * Value of the record to add/remove.
114  */
115 static char *value;
116
117 /**
118  * URI to import.
119  */
120 static char *uri;
121
122 /**
123  * Reverse lookup to perform.
124  */
125 static char *reverse_pkey;
126
127 /**
128  * Type of the record to add/remove, NULL to remove all.
129  */
130 static char *typestring;
131
132 /**
133  * Desired expiration time.
134  */
135 static char *expirationstring;
136
137 /**
138  * Desired nick name.
139  */
140 static char *nickstring;
141
142 /**
143  * Global return value
144  */
145 static int ret;
146
147 /**
148  * Type string converted to DNS type value.
149  */
150 static uint32_t type;
151
152 /**
153  * Value in binary format.
154  */
155 static void *data;
156
157 /**
158  * Number of bytes in 'data'.
159  */
160 static size_t data_size;
161
162 /**
163  * Expirationstring converted to relative time.
164  */
165 static struct GNUNET_TIME_Relative etime_rel;
166
167 /**
168  * Expirationstring converted to absolute time.
169  */
170 static struct GNUNET_TIME_Absolute etime_abs;
171
172 /**
173  * Is expiration time relative or absolute time?
174  */
175 static int etime_is_rel = GNUNET_SYSERR;
176
177 /**
178  * Monitor handle.
179  */
180 static struct GNUNET_NAMESTORE_ZoneMonitor *zm;
181
182 /**
183  * Enables monitor mode.
184  */
185 static int monitor;
186
187
188 /**
189  * Task run on shutdown.  Cleans up everything.
190  *
191  * @param cls unused
192  * @param tc scheduler context
193  */
194 static void
195 do_shutdown (void *cls,
196              const struct GNUNET_SCHEDULER_TaskContext *tc)
197 {
198   if (NULL != el)
199   {
200     GNUNET_IDENTITY_ego_lookup_cancel (el);
201     el = NULL;
202   }
203   if (NULL != list_it)
204   {
205     GNUNET_NAMESTORE_zone_iteration_stop (list_it);
206     list_it = NULL;
207   }
208   if (NULL != add_qe)
209   {
210     GNUNET_NAMESTORE_cancel (add_qe);
211     add_qe = NULL;
212   }
213   if (NULL != add_qe_uri)
214   {
215     GNUNET_NAMESTORE_cancel (add_qe_uri);
216     add_qe_uri = NULL;
217   }
218   if (NULL != del_qe)
219   {
220     GNUNET_NAMESTORE_cancel (del_qe);
221     del_qe = NULL;
222   }
223   if (NULL != ns)
224   {
225     GNUNET_NAMESTORE_disconnect (ns);
226     ns = NULL;
227   }
228   memset (&zone_pkey, 0, sizeof (zone_pkey));
229   if (NULL != uri)
230   {
231     GNUNET_free (uri);
232     uri = NULL;
233   }
234   if (NULL != zm)
235   {
236     GNUNET_NAMESTORE_zone_monitor_stop (zm);
237     zm = NULL;
238   }
239   if (NULL != data)
240   {
241     GNUNET_free (data);
242     data = NULL;
243   }
244 }
245
246
247 /**
248  * Check if we are finished, and if so, perform shutdown.
249  */
250 static void
251 test_finished ()
252 {
253   if ( (NULL == add_qe) &&
254        (NULL == add_qe_uri) &&
255        (NULL == del_qe) &&
256        (NULL == reverse_qe) &&
257        (NULL == list_it) )
258     GNUNET_SCHEDULER_shutdown ();
259 }
260
261
262 /**
263  * Continuation called to notify client about result of the
264  * operation.
265  *
266  * @param cls closure, location of the QueueEntry pointer to NULL out
267  * @param success #GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
268  *                #GNUNET_NO if content was already there
269  *                #GNUNET_YES (or other positive value) on success
270  * @param emsg NULL on success, otherwise an error message
271  */
272 static void
273 add_continuation (void *cls,
274                   int32_t success,
275                   const char *emsg)
276 {
277   struct GNUNET_NAMESTORE_QueueEntry **qe = cls;
278
279   *qe = NULL;
280   if (GNUNET_YES != success)
281   {
282     fprintf (stderr,
283              _("Adding record failed: %s\n"),
284              (GNUNET_NO == success) ? "record exists" : emsg);
285     if (GNUNET_NO != success)
286       ret = 1;
287   }
288   test_finished ();
289 }
290
291
292 /**
293  * Continuation called to notify client about result of the
294  * operation.
295  *
296  * @param cls closure, unused
297  * @param success #GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
298  *                #GNUNET_NO if content was already there
299  *                #GNUNET_YES (or other positive value) on success
300  * @param emsg NULL on success, otherwise an error message
301  */
302 static void
303 del_continuation (void *cls,
304                   int32_t success,
305                   const char *emsg)
306 {
307   del_qe = NULL;
308   if (GNUNET_NO == success)
309   {
310     fprintf (stderr,
311              _("Deleting record failed, record does not exist%s%s\n"),
312              (NULL != emsg) ? ": " : "",
313              (NULL != emsg) ? emsg : "");
314   }
315   if (GNUNET_SYSERR == success)
316   {
317     fprintf (stderr,
318              _("Deleting record failed%s%s\n"),
319              (NULL != emsg) ? ": " : "",
320              (NULL != emsg) ? emsg : "");
321   }
322   test_finished ();
323 }
324
325
326 /**
327  * Process a record that was stored in the namestore.
328  *
329  * @param cls closure
330  * @param zone_key private key of the zone
331  * @param rname name that is being mapped (at most 255 characters long)
332  * @param rd_len number of entries in @a rd array
333  * @param rd array of records with data to store
334  */
335 static void
336 display_record (void *cls,
337                 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
338                 const char *rname,
339                 unsigned int rd_len,
340                 const struct GNUNET_GNSRECORD_Data *rd)
341 {
342   const char *typestring;
343   char *s;
344   unsigned int i;
345   const char *ets;
346   struct GNUNET_TIME_Absolute at;
347   struct GNUNET_TIME_Relative rt;
348
349   if (NULL == rname)
350   {
351     list_it = NULL;
352     test_finished ();
353     return;
354   }
355   if ( (NULL != name) &&
356        (0 != strcmp (name, rname)) )
357   {
358     GNUNET_NAMESTORE_zone_iterator_next (list_it);
359     return;
360   }
361   FPRINTF (stdout,
362            "%s:\n",
363            rname);
364   for (i=0;i<rd_len;i++)
365   {
366     typestring = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type);
367     s = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
368                                           rd[i].data,
369                                           rd[i].data_size);
370     if (NULL == s)
371     {
372       FPRINTF (stdout, _("\tCorrupt or unsupported record of type %u\n"),
373                (unsigned int) rd[i].record_type);
374       continue;
375     }
376     if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
377     {
378       rt.rel_value_us = rd[i].expiration_time;
379       ets = GNUNET_STRINGS_relative_time_to_string (rt, GNUNET_YES);
380     }
381     else
382     {
383       at.abs_value_us = rd[i].expiration_time;
384       ets = GNUNET_STRINGS_absolute_time_to_string (at);
385     }
386     FPRINTF (stdout,
387              "\t%s: %s (%s)\t%s\t%s\t%s\n",
388              typestring,
389              s,
390              ets,
391              (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_PRIVATE)) ? "PRIVATE" : "PUBLIC",
392              (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_SHADOW_RECORD)) ? "SHADOW" : "",
393              (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_PENDING)) ? "PENDING" : "");
394     GNUNET_free (s);
395   }
396   FPRINTF (stdout, "%s", "\n");
397   GNUNET_NAMESTORE_zone_iterator_next (list_it);
398 }
399
400
401 /**
402  * Function called once we are in sync in monitor mode.
403  *
404  * @param cls NULL
405  */
406 static void
407 sync_cb (void *cls)
408 {
409   FPRINTF (stdout, "%s", "Monitor is now in sync.\n");
410 }
411
412
413 /**
414  * We're storing a record; this function is given the existing record
415  * so that we can merge the information.
416  *
417  * @param cls closure, unused
418  * @param zone_key private key of the zone
419  * @param rec_name name that is being mapped (at most 255 characters long)
420  * @param rd_count number of entries in @a rd array
421  * @param rd array of records with data to store
422  */
423 static void
424 get_existing_record (void *cls,
425                      const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
426                      const char *rec_name,
427                      unsigned int rd_count,
428                      const struct GNUNET_GNSRECORD_Data *rd)
429 {
430   struct GNUNET_GNSRECORD_Data rdn[rd_count + 1];
431   struct GNUNET_GNSRECORD_Data *rde;
432
433   add_qe = NULL;
434   if ( (NULL != zone_key) &&
435        (0 != strcmp (rec_name, name)) )
436   {
437     GNUNET_break (0);
438     return;
439   }
440
441   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received %u records for name `%s'\n",
442       rd_count, rec_name);
443
444   memset (rdn, 0, sizeof (struct GNUNET_GNSRECORD_Data));
445   memcpy (&rdn[1], rd, rd_count * sizeof (struct GNUNET_GNSRECORD_Data));
446   /* FIXME: should add some logic to overwrite records if there
447      can only be one record of a particular type, and to check
448      if the combination of records is valid to begin with... */
449   rde = &rdn[0];
450   rde->data = data;
451   rde->data_size = data_size;
452   rde->record_type = type;
453   if (1 == shadow)
454     rde->flags |= GNUNET_GNSRECORD_RF_SHADOW_RECORD;
455   if (1 != public)
456     rde->flags |= GNUNET_GNSRECORD_RF_PRIVATE;
457   if (GNUNET_YES == etime_is_rel)
458   {
459     rde->expiration_time = etime_rel.rel_value_us;
460     rde->flags |= GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
461   }
462   else if (GNUNET_NO == etime_is_rel)
463     rde->expiration_time = etime_abs.abs_value_us;
464   else
465     rde->expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
466   GNUNET_assert (NULL != name);
467   add_qe = GNUNET_NAMESTORE_records_store (ns,
468                                            &zone_pkey,
469                                            name,
470                                            rd_count + 1,
471                                            rde,
472                                            &add_continuation,
473                                            &add_qe);
474 }
475
476
477 /**
478  * Function called with the result of our attempt to obtain a name for a given
479  * public key.
480  *
481  * @param cls NULL
482  * @param zone private key of the zone; NULL on disconnect
483  * @param label label of the records; NULL on disconnect
484  * @param rd_count number of entries in @a rd array, 0 if label was deleted
485  * @param rd array of records with data to store
486  */
487 static void
488 handle_reverse_lookup (void *cls,
489                        const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
490                        const char *label,
491                        unsigned int rd_count,
492                        const struct GNUNET_GNSRECORD_Data *rd)
493 {
494   reverse_qe = NULL;
495   if (NULL == label)
496     FPRINTF (stdout,
497              "%s.zkey\n",
498              reverse_pkey);
499   else
500     FPRINTF (stdout,
501              "%s.gnu\n",
502              label);
503   test_finished ();
504 }
505
506
507 /**
508  * Function called with the result from the check if the namestore
509  * service is actually running.  If it is, we start the actual
510  * operation.
511  *
512  * @param cls closure with our configuration
513  * @param result #GNUNET_YES if the namestore service is running
514  */
515 static void
516 testservice_task (void *cls,
517                   int result)
518 {
519   const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
520   struct GNUNET_CRYPTO_EcdsaPublicKey pub;
521   struct GNUNET_GNSRECORD_Data rd;
522
523   if (GNUNET_YES != result)
524   {
525     FPRINTF (stderr, _("Service `%s' is not running\n"),
526              "namestore");
527     return;
528   }
529   if (! (add|del|list|(NULL != nickstring)|(NULL != uri)|(NULL != reverse_pkey)) )
530   {
531     /* nothing more to be done */
532     fprintf (stderr,
533              _("No options given\n"));
534     GNUNET_SCHEDULER_shutdown ();
535     return;
536   }
537   GNUNET_CRYPTO_ecdsa_key_get_public (&zone_pkey,
538                                     &pub);
539
540   ns = GNUNET_NAMESTORE_connect (cfg);
541   if (NULL == ns)
542   {
543     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
544                 _("Failed to connect to namestore\n"));
545     return;
546   }
547   if (add)
548   {
549     if (NULL == name)
550     {
551       fprintf (stderr,
552                _("Missing option `%s' for operation `%s'\n"),
553                "-n", _("add"));
554       GNUNET_SCHEDULER_shutdown ();
555       ret = 1;
556       return;
557     }
558     if (NULL == typestring)
559     {
560       fprintf (stderr,
561                _("Missing option `%s' for operation `%s'\n"),
562                "-t", _("add"));
563       GNUNET_SCHEDULER_shutdown ();
564       ret = 1;
565       return;
566     }
567     type = GNUNET_GNSRECORD_typename_to_number (typestring);
568     if (UINT32_MAX == type)
569     {
570       fprintf (stderr, _("Unsupported type `%s'\n"), typestring);
571       GNUNET_SCHEDULER_shutdown ();
572       ret = 1;
573       return;
574     }
575     if (NULL == value)
576     {
577       fprintf (stderr,
578                _("Missing option `%s' for operation `%s'\n"),
579                "-V", _("add"));
580       ret = 1;
581       GNUNET_SCHEDULER_shutdown ();
582       return;
583     }
584     if (GNUNET_OK !=
585         GNUNET_GNSRECORD_string_to_value (type,
586                                           value,
587                                           &data,
588                                           &data_size))
589     {
590       fprintf (stderr, _("Value `%s' invalid for record type `%s'\n"),
591                value,
592                typestring);
593       GNUNET_SCHEDULER_shutdown ();
594       ret = 1;
595       return;
596     }
597     if (NULL == expirationstring)
598     {
599       fprintf (stderr,
600                _("Missing option `%s' for operation `%s'\n"),
601                "-e", _("add"));
602       GNUNET_SCHEDULER_shutdown ();
603       ret = 1;
604       return;
605     }
606     if (0 == strcmp (expirationstring, "never"))
607     {
608       etime_abs = GNUNET_TIME_UNIT_FOREVER_ABS;
609       etime_is_rel = GNUNET_NO;
610     }
611     else if (GNUNET_OK ==
612              GNUNET_STRINGS_fancy_time_to_relative (expirationstring,
613                                                     &etime_rel))
614     {
615       etime_is_rel = GNUNET_YES;
616     }
617     else if (GNUNET_OK ==
618              GNUNET_STRINGS_fancy_time_to_absolute (expirationstring,
619                                                     &etime_abs))
620     {
621       etime_is_rel = GNUNET_NO;
622     }
623     else
624     {
625       fprintf (stderr,
626                _("Invalid time format `%s'\n"),
627                expirationstring);
628       GNUNET_SCHEDULER_shutdown ();
629       ret = 1;
630       return;
631     }
632     add_qe = GNUNET_NAMESTORE_records_lookup (ns, &zone_pkey, name,
633         &get_existing_record, NULL );
634   }
635   if (del)
636   {
637     if (NULL == name)
638     {
639       fprintf (stderr,
640                _("Missing option `%s' for operation `%s'\n"),
641                "-n", _("del"));
642       GNUNET_SCHEDULER_shutdown ();
643       ret = 1;
644       return;
645     }
646     del_qe = GNUNET_NAMESTORE_records_store (ns,
647                                              &zone_pkey,
648                                              name,
649                                              0, NULL,
650                                              &del_continuation,
651                                              NULL);
652   }
653   if (list)
654   {
655     list_it = GNUNET_NAMESTORE_zone_iteration_start (ns,
656                                                      &zone_pkey,
657                                                      &display_record,
658                                                      NULL);
659   }
660   if (NULL != reverse_pkey)
661   {
662     struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
663
664     if (GNUNET_OK !=
665         GNUNET_CRYPTO_ecdsa_public_key_from_string (reverse_pkey,
666                                                        strlen (reverse_pkey),
667                                                        &pubkey))
668     {
669       fprintf (stderr,
670                _("Invalid public key for reverse lookup `%s'\n"),
671                reverse_pkey);
672       GNUNET_SCHEDULER_shutdown ();
673     }
674     reverse_qe = GNUNET_NAMESTORE_zone_to_name (ns,
675                                                 &zone_pkey,
676                                                 &pubkey,
677                                                 &handle_reverse_lookup,
678                                                 NULL);
679   }
680   if (NULL != uri)
681   {
682     char sh[105];
683     char sname[64];
684     struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
685
686     if ( (2 != (sscanf (uri,
687                         "gnunet://gns/%104s/%63s",
688                         sh,
689                         sname)) ) ||
690          (GNUNET_OK !=
691           GNUNET_CRYPTO_ecdsa_public_key_from_string (sh, strlen (sh), &pkey)) )
692     {
693       fprintf (stderr,
694                _("Invalid URI `%s'\n"),
695                uri);
696       GNUNET_SCHEDULER_shutdown ();
697       ret = 1;
698       return;
699     }
700     memset (&rd, 0, sizeof (rd));
701     rd.data = &pkey;
702     rd.data_size = sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
703     rd.record_type = GNUNET_GNSRECORD_TYPE_PKEY;
704     if (GNUNET_YES == etime_is_rel)
705     {
706       rd.expiration_time = etime_rel.rel_value_us;
707       rd.flags |= GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
708     }
709     else if (GNUNET_NO == etime_is_rel)
710       rd.expiration_time = etime_abs.abs_value_us;
711     else
712       rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
713
714     if (1 != shadow)
715       rd.flags |= GNUNET_GNSRECORD_RF_SHADOW_RECORD;
716     add_qe_uri = GNUNET_NAMESTORE_records_store (ns,
717                                                  &zone_pkey,
718                                                  sname,
719                                                  1,
720                                                  &rd,
721                                                  &add_continuation,
722                                                  &add_qe_uri);
723   }
724   if (NULL != nickstring)
725   {
726     if (0 == strlen(nickstring))
727     {
728       fprintf (stderr,
729                _("Invalid nick `%s'\n"),
730                nickstring);
731       GNUNET_SCHEDULER_shutdown ();
732       ret = 1;
733       return;
734     }
735     memset (&rd, 0, sizeof (rd));
736     rd.data = nickstring;
737     rd.data_size = strlen(nickstring);
738     rd.record_type = GNUNET_GNSRECORD_TYPE_NICK;
739     rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
740     rd.flags |= GNUNET_GNSRECORD_RF_PRIVATE;
741     add_qe_uri = GNUNET_NAMESTORE_records_store (ns,
742                                                  &zone_pkey,
743                                                  "+",
744                                                  1,
745                                                  &rd,
746                                                  &add_continuation,
747                                                  &add_qe_uri);
748   }
749   if (monitor)
750   {
751     zm = GNUNET_NAMESTORE_zone_monitor_start (cfg,
752                                               &zone_pkey,
753                                               GNUNET_YES,
754                                               &display_record,
755                                               &sync_cb,
756                                               NULL);
757   }
758 }
759
760
761 /**
762  * Callback invoked from identity service with ego information.
763  * An @a ego of NULL means the ego was not found.
764  *
765  * @param cls closure with the configuration
766  * @param ego an ego known to identity service, or NULL
767  */
768 static void
769 identity_cb (void *cls,
770              const struct GNUNET_IDENTITY_Ego *ego)
771 {
772   const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
773
774   el = NULL;
775   if (NULL == ego)
776   {
777     fprintf (stderr,
778              _("Ego `%s' not known to identity service\n"),
779              ego_name);
780     GNUNET_SCHEDULER_shutdown ();
781     return;
782   }
783   zone_pkey = *GNUNET_IDENTITY_ego_get_private_key (ego);
784   GNUNET_free (ego_name);
785   ego_name = NULL;
786   GNUNET_CLIENT_service_test ("namestore", cfg,
787                               GNUNET_TIME_UNIT_SECONDS,
788                               &testservice_task,
789                               (void *) cfg);
790 }
791
792
793 /**
794  * Main function that will be run.
795  *
796  * @param cls closure
797  * @param args remaining command-line arguments
798  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
799  * @param cfg configuration
800  */
801 static void
802 run (void *cls, char *const *args, const char *cfgfile,
803      const struct GNUNET_CONFIGURATION_Handle *cfg)
804 {
805   if (NULL == ego_name)
806   {
807     fprintf (stderr,
808              _("You must specify which zone should be accessed\n"));
809     return;
810   }
811
812   if ( (NULL != args[0]) && (NULL == uri) )
813     uri = GNUNET_strdup (args[0]);
814   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
815                                 &do_shutdown, NULL);
816   el = GNUNET_IDENTITY_ego_lookup (cfg,
817                                    ego_name,
818                                    &identity_cb,
819                                    (void *) cfg);
820 }
821
822
823 /**
824  * The main function for gnunet-namestore.
825  *
826  * @param argc number of arguments from the command line
827  * @param argv command line arguments
828  * @return 0 ok, 1 on error
829  */
830 int
831 main (int argc, char *const *argv)
832 {
833   public = -1;
834   shadow = -1;
835
836   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
837     {'a', "add", NULL,
838      gettext_noop ("add record"), 0,
839      &GNUNET_GETOPT_set_one, &add},
840     {'d', "delete", NULL,
841      gettext_noop ("delete record"), 0,
842      &GNUNET_GETOPT_set_one, &del},
843     {'D', "display", NULL,
844      gettext_noop ("display records"), 0,
845      &GNUNET_GETOPT_set_one, &list},
846     {'e', "expiration", "TIME",
847      gettext_noop ("expiration time for record to use (for adding only), \"never\" is possible"), 1,
848      &GNUNET_GETOPT_set_string, &expirationstring},
849     {'i', "nick", "NICKNAME",
850      gettext_noop ("set the desired nick name for the zone"), 1,
851      &GNUNET_GETOPT_set_string, &nickstring},
852     {'m', "monitor", NULL,
853      gettext_noop ("monitor changes in the namestore"), 0,
854      &GNUNET_GETOPT_set_one, &monitor},
855     {'n', "name", "NAME",
856      gettext_noop ("name of the record to add/delete/display"), 1,
857      &GNUNET_GETOPT_set_string, &name},
858     {'r', "reverse", "PKEY",
859      gettext_noop ("determine our name for the given PKEY"), 1,
860      &GNUNET_GETOPT_set_string, &reverse_pkey},
861     {'t', "type", "TYPE",
862      gettext_noop ("type of the record to add/delete/display"), 1,
863      &GNUNET_GETOPT_set_string, &typestring},
864     {'u', "uri", "URI",
865      gettext_noop ("URI to import into our zone"), 1,
866      &GNUNET_GETOPT_set_string, &uri},
867     {'V', "value", "VALUE",
868      gettext_noop ("value of the record to add/delete"), 1,
869      &GNUNET_GETOPT_set_string, &value},
870     {'p', "public", NULL,
871      gettext_noop ("create or list public record"), 0,
872      &GNUNET_GETOPT_set_one, &public},
873     {'s', "shadow", NULL,
874      gettext_noop ("create shadow record (only valid if all other records of the same type have expired"), 0,
875      &GNUNET_GETOPT_set_one, &shadow},
876     {'z', "zone", "EGO",
877      gettext_noop ("name of the ego controlling the zone"), 1,
878      &GNUNET_GETOPT_set_string, &ego_name},
879     GNUNET_GETOPT_OPTION_END
880   };
881
882   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
883     return 2;
884
885   GNUNET_log_setup ("gnunet-namestore", "WARNING", NULL);
886   if (GNUNET_OK !=
887       GNUNET_PROGRAM_run (argc, argv, "gnunet-namestore",
888                           _("GNUnet zone manipulation tool"),
889                           options,
890                           &run, NULL))
891   {
892     GNUNET_free ((void*) argv);
893     GNUNET_CRYPTO_ecdsa_key_clear (&zone_pkey);
894     return 1;
895   }
896   GNUNET_free ((void*) argv);
897   GNUNET_CRYPTO_ecdsa_key_clear (&zone_pkey);
898   return ret;
899 }
900
901 /* end of gnunet-namestore.c */