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