-never store NICKs anywhere but in '+', do not display nicks in gnunet-namestore
[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       continue;
389     typestring = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type);
390     s = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
391                                           rd[i].data,
392                                           rd[i].data_size);
393     if (NULL == s)
394     {
395       FPRINTF (stdout, _("\tCorrupt or unsupported record of type %u\n"),
396                (unsigned int) rd[i].record_type);
397       continue;
398     }
399     if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
400     {
401       rt.rel_value_us = rd[i].expiration_time;
402       ets = GNUNET_STRINGS_relative_time_to_string (rt, GNUNET_YES);
403     }
404     else
405     {
406       at.abs_value_us = rd[i].expiration_time;
407       ets = GNUNET_STRINGS_absolute_time_to_string (at);
408     }
409     FPRINTF (stdout,
410              "\t%s: %s (%s)\t%s\t%s\t%s\n",
411              typestring,
412              s,
413              ets,
414              (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_PRIVATE)) ? "PRIVATE" : "PUBLIC",
415              (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_SHADOW_RECORD)) ? "SHADOW" : "",
416              (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_PENDING)) ? "PENDING" : "");
417     GNUNET_free (s);
418   }
419   FPRINTF (stdout, "%s", "\n");
420   GNUNET_NAMESTORE_zone_iterator_next (list_it);
421 }
422
423
424 /**
425  * Function called once we are in sync in monitor mode.
426  *
427  * @param cls NULL
428  */
429 static void
430 sync_cb (void *cls)
431 {
432   FPRINTF (stdout, "%s", "Monitor is now in sync.\n");
433 }
434
435
436 /**
437  * We're storing a record; this function is given the existing record
438  * so that we can merge the information.
439  *
440  * @param cls closure, unused
441  * @param zone_key private key of the zone
442  * @param rec_name name that is being mapped (at most 255 characters long)
443  * @param rd_count number of entries in @a rd array
444  * @param rd array of records with data to store
445  */
446 static void
447 get_existing_record (void *cls,
448                      const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
449                      const char *rec_name,
450                      unsigned int rd_count,
451                      const struct GNUNET_GNSRECORD_Data *rd)
452 {
453   struct GNUNET_GNSRECORD_Data rdn[rd_count + 1];
454   struct GNUNET_GNSRECORD_Data *rde;
455
456   add_qe = NULL;
457   if ( (NULL != zone_key) &&
458        (0 != strcmp (rec_name, name)) )
459   {
460     GNUNET_break (0);
461     return;
462   }
463
464   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received %u records for name `%s'\n",
465       rd_count, rec_name);
466
467   memset (rdn, 0, sizeof (struct GNUNET_GNSRECORD_Data));
468   memcpy (&rdn[1], rd, rd_count * sizeof (struct GNUNET_GNSRECORD_Data));
469   /* FIXME: should add some logic to overwrite records if there
470      can only be one record of a particular type, and to check
471      if the combination of records is valid to begin with... */
472   rde = &rdn[0];
473   rde->data = data;
474   rde->data_size = data_size;
475   rde->record_type = type;
476   if (1 == shadow)
477     rde->flags |= GNUNET_GNSRECORD_RF_SHADOW_RECORD;
478   if (1 != public)
479     rde->flags |= GNUNET_GNSRECORD_RF_PRIVATE;
480   if (GNUNET_YES == etime_is_rel)
481   {
482     rde->expiration_time = etime_rel.rel_value_us;
483     rde->flags |= GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
484   }
485   else if (GNUNET_NO == etime_is_rel)
486     rde->expiration_time = etime_abs.abs_value_us;
487   else
488     rde->expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
489   GNUNET_assert (NULL != name);
490   add_qe = GNUNET_NAMESTORE_records_store (ns,
491                                            &zone_pkey,
492                                            name,
493                                            rd_count + 1,
494                                            rde,
495                                            &add_continuation,
496                                            &add_qe);
497 }
498
499
500 /**
501  * Function called with the result of our attempt to obtain a name for a given
502  * public key.
503  *
504  * @param cls NULL
505  * @param zone private key of the zone; NULL on disconnect
506  * @param label label of the records; NULL on disconnect
507  * @param rd_count number of entries in @a rd array, 0 if label was deleted
508  * @param rd array of records with data to store
509  */
510 static void
511 handle_reverse_lookup (void *cls,
512                        const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
513                        const char *label,
514                        unsigned int rd_count,
515                        const struct GNUNET_GNSRECORD_Data *rd)
516 {
517   reverse_qe = NULL;
518   if (NULL == label)
519     FPRINTF (stdout,
520              "%s.zkey\n",
521              reverse_pkey);
522   else
523     FPRINTF (stdout,
524              "%s.gnu\n",
525              label);
526   test_finished ();
527 }
528
529
530 /**
531  * Function called with the result from the check if the namestore
532  * service is actually running.  If it is, we start the actual
533  * operation.
534  *
535  * @param cls closure with our configuration
536  * @param result #GNUNET_YES if the namestore service is running
537  */
538 static void
539 testservice_task (void *cls,
540                   int result)
541 {
542   const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
543   struct GNUNET_CRYPTO_EcdsaPublicKey pub;
544   struct GNUNET_GNSRECORD_Data rd;
545
546   if (GNUNET_YES != result)
547   {
548     FPRINTF (stderr, _("Service `%s' is not running\n"),
549              "namestore");
550     return;
551   }
552   if (! (add|del|list|(NULL != nickstring)|(NULL != uri)|(NULL != reverse_pkey)) )
553   {
554     /* nothing more to be done */
555     fprintf (stderr,
556              _("No options given\n"));
557     GNUNET_SCHEDULER_shutdown ();
558     return;
559   }
560   GNUNET_CRYPTO_ecdsa_key_get_public (&zone_pkey,
561                                     &pub);
562
563   ns = GNUNET_NAMESTORE_connect (cfg);
564   if (NULL == ns)
565   {
566     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
567                 _("Failed to connect to namestore\n"));
568     return;
569   }
570   if (add)
571   {
572     if (NULL == name)
573     {
574       fprintf (stderr,
575                _("Missing option `%s' for operation `%s'\n"),
576                "-n", _("add"));
577       GNUNET_SCHEDULER_shutdown ();
578       ret = 1;
579       return;
580     }
581     if (NULL == typestring)
582     {
583       fprintf (stderr,
584                _("Missing option `%s' for operation `%s'\n"),
585                "-t", _("add"));
586       GNUNET_SCHEDULER_shutdown ();
587       ret = 1;
588       return;
589     }
590     type = GNUNET_GNSRECORD_typename_to_number (typestring);
591     if (UINT32_MAX == type)
592     {
593       fprintf (stderr, _("Unsupported type `%s'\n"), typestring);
594       GNUNET_SCHEDULER_shutdown ();
595       ret = 1;
596       return;
597     }
598     if (NULL == value)
599     {
600       fprintf (stderr,
601                _("Missing option `%s' for operation `%s'\n"),
602                "-V", _("add"));
603       ret = 1;
604       GNUNET_SCHEDULER_shutdown ();
605       return;
606     }
607     if (GNUNET_OK !=
608         GNUNET_GNSRECORD_string_to_value (type,
609                                           value,
610                                           &data,
611                                           &data_size))
612     {
613       fprintf (stderr, _("Value `%s' invalid for record type `%s'\n"),
614                value,
615                typestring);
616       GNUNET_SCHEDULER_shutdown ();
617       ret = 1;
618       return;
619     }
620     if (NULL == expirationstring)
621     {
622       fprintf (stderr,
623                _("Missing option `%s' for operation `%s'\n"),
624                "-e", _("add"));
625       GNUNET_SCHEDULER_shutdown ();
626       ret = 1;
627       return;
628     }
629     if (0 == strcmp (expirationstring, "never"))
630     {
631       etime_abs = GNUNET_TIME_UNIT_FOREVER_ABS;
632       etime_is_rel = GNUNET_NO;
633     }
634     else if (GNUNET_OK ==
635              GNUNET_STRINGS_fancy_time_to_relative (expirationstring,
636                                                     &etime_rel))
637     {
638       etime_is_rel = GNUNET_YES;
639     }
640     else if (GNUNET_OK ==
641              GNUNET_STRINGS_fancy_time_to_absolute (expirationstring,
642                                                     &etime_abs))
643     {
644       etime_is_rel = GNUNET_NO;
645     }
646     else
647     {
648       fprintf (stderr,
649                _("Invalid time format `%s'\n"),
650                expirationstring);
651       GNUNET_SCHEDULER_shutdown ();
652       ret = 1;
653       return;
654     }
655     add_qe = GNUNET_NAMESTORE_records_lookup (ns, &zone_pkey, name,
656         &get_existing_record, NULL );
657   }
658   if (del)
659   {
660     if (NULL == name)
661     {
662       fprintf (stderr,
663                _("Missing option `%s' for operation `%s'\n"),
664                "-n", _("del"));
665       GNUNET_SCHEDULER_shutdown ();
666       ret = 1;
667       return;
668     }
669     del_qe = GNUNET_NAMESTORE_records_store (ns,
670                                              &zone_pkey,
671                                              name,
672                                              0, NULL,
673                                              &del_continuation,
674                                              NULL);
675   }
676   if (list)
677   {
678     list_it = GNUNET_NAMESTORE_zone_iteration_start (ns,
679                                                      &zone_pkey,
680                                                      &display_record,
681                                                      NULL);
682   }
683   if (NULL != reverse_pkey)
684   {
685     struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
686
687     if (GNUNET_OK !=
688         GNUNET_CRYPTO_ecdsa_public_key_from_string (reverse_pkey,
689                                                        strlen (reverse_pkey),
690                                                        &pubkey))
691     {
692       fprintf (stderr,
693                _("Invalid public key for reverse lookup `%s'\n"),
694                reverse_pkey);
695       GNUNET_SCHEDULER_shutdown ();
696     }
697     reverse_qe = GNUNET_NAMESTORE_zone_to_name (ns,
698                                                 &zone_pkey,
699                                                 &pubkey,
700                                                 &handle_reverse_lookup,
701                                                 NULL);
702   }
703   if (NULL != uri)
704   {
705     char sh[105];
706     char sname[64];
707     struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
708
709     if ( (2 != (sscanf (uri, "gnunet://gns/%52s/%63s", sh, sname)) ) ||
710          (GNUNET_OK != GNUNET_CRYPTO_ecdsa_public_key_from_string (sh, strlen (sh), &pkey)) )
711     {
712       fprintf (stderr,
713                _("Invalid URI `%s'\n"),
714                uri);
715       GNUNET_SCHEDULER_shutdown ();
716       ret = 1;
717       return;
718     }
719     memset (&rd, 0, sizeof (rd));
720     rd.data = &pkey;
721     rd.data_size = sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
722     rd.record_type = GNUNET_GNSRECORD_TYPE_PKEY;
723     if (GNUNET_YES == etime_is_rel)
724     {
725       rd.expiration_time = etime_rel.rel_value_us;
726       rd.flags |= GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
727     }
728     else if (GNUNET_NO == etime_is_rel)
729       rd.expiration_time = etime_abs.abs_value_us;
730     else
731       rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
732
733     if (1 == shadow)
734       rd.flags |= GNUNET_GNSRECORD_RF_SHADOW_RECORD;
735     add_qe_uri = GNUNET_NAMESTORE_records_store (ns,
736                                                  &zone_pkey,
737                                                  sname,
738                                                  1,
739                                                  &rd,
740                                                  &add_continuation,
741                                                  &add_qe_uri);
742   }
743   if (NULL != nickstring)
744   {
745     if (0 == strlen(nickstring))
746     {
747       fprintf (stderr,
748                _("Invalid nick `%s'\n"),
749                nickstring);
750       GNUNET_SCHEDULER_shutdown ();
751       ret = 1;
752       return;
753     }
754     add_qe_uri = GNUNET_NAMESTORE_set_nick(ns, &zone_pkey, nickstring,
755         &add_continuation, &add_qe_uri);
756   }
757   if (monitor)
758   {
759     zm = GNUNET_NAMESTORE_zone_monitor_start (cfg,
760                                               &zone_pkey,
761                                               GNUNET_YES,
762                                               &display_record,
763                                               &sync_cb,
764                                               NULL);
765   }
766 }
767
768
769 /**
770  * Callback invoked from identity service with ego information.
771  * An @a ego of NULL means the ego was not found.
772  *
773  * @param cls closure with the configuration
774  * @param ego an ego known to identity service, or NULL
775  */
776 static void
777 identity_cb (void *cls,
778              const struct GNUNET_IDENTITY_Ego *ego)
779 {
780   const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
781
782   el = NULL;
783   if (NULL == ego)
784   {
785     if (NULL != ego_name)
786     {
787       fprintf (stderr,
788                _("Ego `%s' not known to identity service\n"),
789                ego_name);
790     }
791     GNUNET_SCHEDULER_shutdown ();
792     ret = -1;
793     return;
794   }
795   zone_pkey = *GNUNET_IDENTITY_ego_get_private_key (ego);
796   GNUNET_free_non_null (ego_name);
797   ego_name = NULL;
798   GNUNET_CLIENT_service_test ("namestore", cfg,
799                               GNUNET_TIME_UNIT_SECONDS,
800                               &testservice_task,
801                               (void *) cfg);
802 }
803
804
805 static void
806 default_ego_cb (void *cls,
807     struct GNUNET_IDENTITY_Ego *ego,
808     void **ctx,
809     const char *name)
810 {
811   get_default = NULL;
812   if (NULL == ego)
813   {
814     fprintf (stderr,
815              _("No default ego configured in identity service\n"));
816     GNUNET_SCHEDULER_shutdown ();
817     ret = -1;
818     return;
819   }
820   else
821   {
822     identity_cb (cls, ego);
823   }
824 }
825
826 static void
827 id_connect_cb (void *cls,
828     struct GNUNET_IDENTITY_Ego *ego,
829     void **ctx,
830     const char *name)
831 {
832   const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
833   if (NULL == ego)
834   {
835     get_default = GNUNET_IDENTITY_get (idh, "namestore",
836         &default_ego_cb, (void *) cfg);
837   }
838 }
839
840
841 static void
842 testservice_id_task (void *cls, int result)
843 {
844   const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
845   if (result != GNUNET_YES)
846   {
847     fprintf (stderr,
848              _("Identity service is not running\n"));
849     GNUNET_SCHEDULER_shutdown ();
850     ret = -1;
851     return;
852   }
853   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
854                                 &do_shutdown, (void *) cfg);
855
856   if (NULL == ego_name)
857   {
858     idh = GNUNET_IDENTITY_connect (cfg, &id_connect_cb, (void *) cfg);
859     if (NULL == idh)
860       fprintf (stderr, _("Cannot connect to identity service\n"));
861     ret = -1;
862     return;
863   }
864   el = GNUNET_IDENTITY_ego_lookup (cfg,
865                                    ego_name,
866                                    &identity_cb,
867                                    (void *) cfg);
868 }
869
870
871 /**
872  * Main function that will be run.
873  *
874  * @param cls closure
875  * @param args remaining command-line arguments
876  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
877  * @param cfg configuration
878  */
879 static void
880 run (void *cls, char *const *args, const char *cfgfile,
881      const struct GNUNET_CONFIGURATION_Handle *cfg)
882 {
883   if ( (NULL != args[0]) && (NULL == uri) )
884     uri = GNUNET_strdup (args[0]);
885
886   GNUNET_CLIENT_service_test ("identity", cfg,
887                               GNUNET_TIME_UNIT_SECONDS,
888                               &testservice_id_task,
889                               (void *) cfg);
890 }
891
892
893 /**
894  * The main function for gnunet-namestore.
895  *
896  * @param argc number of arguments from the command line
897  * @param argv command line arguments
898  * @return 0 ok, 1 on error
899  */
900 int
901 main (int argc, char *const *argv)
902 {
903   public = -1;
904   shadow = -1;
905
906   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
907     {'a', "add", NULL,
908      gettext_noop ("add record"), 0,
909      &GNUNET_GETOPT_set_one, &add},
910     {'d', "delete", NULL,
911      gettext_noop ("delete record"), 0,
912      &GNUNET_GETOPT_set_one, &del},
913     {'D', "display", NULL,
914      gettext_noop ("display records"), 0,
915      &GNUNET_GETOPT_set_one, &list},
916     {'e', "expiration", "TIME",
917      gettext_noop ("expiration time for record to use (for adding only), \"never\" is possible"), 1,
918      &GNUNET_GETOPT_set_string, &expirationstring},
919     {'i', "nick", "NICKNAME",
920      gettext_noop ("set the desired nick name for the zone"), 1,
921      &GNUNET_GETOPT_set_string, &nickstring},
922     {'m', "monitor", NULL,
923      gettext_noop ("monitor changes in the namestore"), 0,
924      &GNUNET_GETOPT_set_one, &monitor},
925     {'n', "name", "NAME",
926      gettext_noop ("name of the record to add/delete/display"), 1,
927      &GNUNET_GETOPT_set_string, &name},
928     {'r', "reverse", "PKEY",
929      gettext_noop ("determine our name for the given PKEY"), 1,
930      &GNUNET_GETOPT_set_string, &reverse_pkey},
931     {'t', "type", "TYPE",
932      gettext_noop ("type of the record to add/delete/display"), 1,
933      &GNUNET_GETOPT_set_string, &typestring},
934     {'u', "uri", "URI",
935      gettext_noop ("URI to import into our zone"), 1,
936      &GNUNET_GETOPT_set_string, &uri},
937     {'V', "value", "VALUE",
938      gettext_noop ("value of the record to add/delete"), 1,
939      &GNUNET_GETOPT_set_string, &value},
940     {'p', "public", NULL,
941      gettext_noop ("create or list public record"), 0,
942      &GNUNET_GETOPT_set_one, &public},
943     {'s', "shadow", NULL,
944      gettext_noop ("create shadow record (only valid if all other records of the same type have expired"), 0,
945      &GNUNET_GETOPT_set_one, &shadow},
946     {'z', "zone", "EGO",
947      gettext_noop ("name of the ego controlling the zone"), 1,
948      &GNUNET_GETOPT_set_string, &ego_name},
949     GNUNET_GETOPT_OPTION_END
950   };
951
952   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
953     return 2;
954
955   GNUNET_log_setup ("gnunet-namestore", "WARNING", NULL);
956   if (GNUNET_OK !=
957       GNUNET_PROGRAM_run (argc, argv, "gnunet-namestore",
958                           _("GNUnet zone manipulation tool"),
959                           options,
960                           &run, NULL))
961   {
962     GNUNET_free ((void*) argv);
963     GNUNET_CRYPTO_ecdsa_key_clear (&zone_pkey);
964     return 1;
965   }
966   GNUNET_free ((void*) argv);
967   GNUNET_CRYPTO_ecdsa_key_clear (&zone_pkey);
968   return ret;
969 }
970
971 /* end of gnunet-namestore.c */