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