-is pending is dead
[oweals/gnunet.git] / src / namestore / gnunet-namestore.c
1 /*
2      This file is part of GNUnet.
3      (C) 2012, 2013, 2014 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 is_public;
106
107 /**
108  * Is record a shadow record (#GNUNET_GNSRECORD_RF_SHADOW_RECORD)
109  */
110 static int is_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     GNUNET_free (s);
419   }
420   FPRINTF (stdout, "%s", "\n");
421   GNUNET_NAMESTORE_zone_iterator_next (list_it);
422 }
423
424
425 /**
426  * Function called once we are in sync in monitor mode.
427  *
428  * @param cls NULL
429  */
430 static void
431 sync_cb (void *cls)
432 {
433   FPRINTF (stdout, "%s", "Monitor is now in sync.\n");
434 }
435
436
437 /**
438  * We're storing a record; this function is given the existing record
439  * so that we can merge the information.
440  *
441  * @param cls closure, unused
442  * @param zone_key private key of the zone
443  * @param rec_name name that is being mapped (at most 255 characters long)
444  * @param rd_count number of entries in @a rd array
445  * @param rd array of records with data to store
446  */
447 static void
448 get_existing_record (void *cls,
449                      const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
450                      const char *rec_name,
451                      unsigned int rd_count,
452                      const struct GNUNET_GNSRECORD_Data *rd)
453 {
454   struct GNUNET_GNSRECORD_Data rdn[rd_count + 1];
455   struct GNUNET_GNSRECORD_Data *rde;
456   unsigned int i;
457
458   add_qe = NULL;
459   if ( (NULL != zone_key) &&
460        (0 != strcmp (rec_name, name)) )
461   {
462     GNUNET_break (0);
463     ret = 1;
464     test_finished ();
465     return;
466   }
467
468   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
469               "Received %u records for name `%s'\n",
470               rd_count, rec_name);
471   for (i=0;i<rd_count;i++)
472   {
473     switch (rd[i].record_type)
474     {
475     case GNUNET_DNSPARSER_TYPE_CNAME:
476       fprintf (stderr,
477                _("A %s record exists already under `%s', no other records can be added.\n"),
478                "CNAME",
479                rec_name);
480       ret = 1;
481       test_finished ();
482       return;
483     case GNUNET_GNSRECORD_TYPE_PKEY:
484       fprintf (stderr,
485                _("A %s record exists already under `%s', no other records can be added.\n"),
486                "PKEY",
487                rec_name);
488       ret = 1;
489       test_finished ();
490     case GNUNET_GNSRECORD_TYPE_GNS2DNS:
491       fprintf (stderr,
492                _("A %s record exists already under `%s', no other records can be added.\n"),
493                "GNS2DNS",
494                rec_name);
495       ret = 1;
496       test_finished ();
497       return;
498     }
499   }
500   switch (type)
501   {
502   case GNUNET_DNSPARSER_TYPE_CNAME:
503     if (0 != rd_count)
504     {
505       fprintf (stderr,
506                _("Records already exist under `%s', cannot add `%s' record.\n"),
507                rec_name,
508                "CNAME");
509       ret = 1;
510       test_finished ();
511       return;
512     }
513     break;
514   case GNUNET_GNSRECORD_TYPE_PKEY:
515     if (0 != rd_count)
516     {
517       fprintf (stderr,
518                _("Records already exist under `%s', cannot add `%s' record.\n"),
519                rec_name,
520                "PKEY");
521       ret = 1;
522       test_finished ();
523       return;
524     }
525     break;
526   case GNUNET_GNSRECORD_TYPE_GNS2DNS:
527     if (0 != rd_count)
528     {
529       fprintf (stderr,
530                _("Records already exist under `%s', cannot add `%s' record.\n"),
531                rec_name,
532                "GNS2DNS");
533       ret = 1;
534       test_finished ();
535       return;
536     }
537     break;
538   }
539   memset (rdn, 0, sizeof (struct GNUNET_GNSRECORD_Data));
540   memcpy (&rdn[1], rd, rd_count * sizeof (struct GNUNET_GNSRECORD_Data));
541   rde = &rdn[0];
542   rde->data = data;
543   rde->data_size = data_size;
544   rde->record_type = type;
545   if (1 == is_shadow)
546     rde->flags |= GNUNET_GNSRECORD_RF_SHADOW_RECORD;
547   if (1 != is_public)
548     rde->flags |= GNUNET_GNSRECORD_RF_PRIVATE;
549   if (GNUNET_YES == etime_is_rel)
550   {
551     rde->expiration_time = etime_rel.rel_value_us;
552     rde->flags |= GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
553   }
554   else if (GNUNET_NO == etime_is_rel)
555     rde->expiration_time = etime_abs.abs_value_us;
556   else
557     rde->expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
558   GNUNET_assert (NULL != name);
559   add_qe = GNUNET_NAMESTORE_records_store (ns,
560                                            &zone_pkey,
561                                            name,
562                                            rd_count + 1,
563                                            rde,
564                                            &add_continuation,
565                                            &add_qe);
566 }
567
568
569 /**
570  * Function called with the result of our attempt to obtain a name for a given
571  * public key.
572  *
573  * @param cls NULL
574  * @param zone private key of the zone; NULL on disconnect
575  * @param label label of the records; NULL on disconnect
576  * @param rd_count number of entries in @a rd array, 0 if label was deleted
577  * @param rd array of records with data to store
578  */
579 static void
580 handle_reverse_lookup (void *cls,
581                        const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
582                        const char *label,
583                        unsigned int rd_count,
584                        const struct GNUNET_GNSRECORD_Data *rd)
585 {
586   reverse_qe = NULL;
587   if (NULL == label)
588     FPRINTF (stdout,
589              "%s.zkey\n",
590              reverse_pkey);
591   else
592     FPRINTF (stdout,
593              "%s.gnu\n",
594              label);
595   test_finished ();
596 }
597
598
599 /**
600  * We were asked to delete something; this function is called with
601  * the existing records. Now we should determine what should be
602  * deleted and then issue the deletion operation.
603  *
604  * @param cls NULL
605  * @param zone private key of the zone we are deleting from
606  * @param label name of the records we are editing
607  * @param rd_count size of the @a rd array
608  * @param rd existing records
609  */
610 static void
611 del_monitor (void *cls,
612              const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
613              const char *label,
614              unsigned int rd_count,
615              const struct GNUNET_GNSRECORD_Data *rd)
616 {
617   struct GNUNET_GNSRECORD_Data rdx[rd_count];
618   unsigned int rd_left;
619   unsigned int i;
620   uint32_t type;
621   char *vs;
622
623   del_qe = NULL;
624   if (0 == rd_count)
625   {
626     FPRINTF (stderr,
627              _("There are no records under label `%s' that could be deleted.\n"),
628              label);
629     test_finished ();
630     return;
631   }
632   if ( (NULL == value) &&
633        (NULL == typestring) )
634   {
635     /* delete everything */
636     del_qe = GNUNET_NAMESTORE_records_store (ns,
637                                              &zone_pkey,
638                                              name,
639                                              0, NULL,
640                                              &del_continuation,
641                                              NULL);
642     return;
643   }
644   rd_left = 0;
645   if (NULL != typestring)
646     type = GNUNET_GNSRECORD_typename_to_number (typestring);
647   else
648     type = GNUNET_GNSRECORD_TYPE_ANY;
649   for (i=0;i<rd_count;i++)
650   {
651     vs = NULL;
652     if (! ( ( (GNUNET_GNSRECORD_TYPE_ANY == type) ||
653               (rd[i].record_type == type) ) &&
654             ( (NULL == value) ||
655               (NULL == (vs = (GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
656                                                                 rd[i].data,
657                                                                 rd[i].data_size)))) ||
658               (0 == strcmp (vs, value)) ) ) )
659       rdx[rd_left++] = rd[i];
660     GNUNET_free_non_null (vs);
661   }
662   if (rd_count == rd_left)
663   {
664     /* nothing got deleted */
665     FPRINTF (stderr,
666              _("There are no records under label `%s' that match the request for deletion.\n"),
667              label);
668     test_finished ();
669     return;
670   }
671   /* delete everything but what we copied to 'rdx' */
672   del_qe = GNUNET_NAMESTORE_records_store (ns,
673                                            &zone_pkey,
674                                            name,
675                                            rd_left, rdx,
676                                            &del_continuation,
677                                            NULL);
678 }
679
680
681 /**
682  * Function called with the result from the check if the namestore
683  * service is actually running.  If it is, we start the actual
684  * operation.
685  *
686  * @param cls closure with our configuration
687  * @param result #GNUNET_YES if the namestore service is running
688  */
689 static void
690 testservice_task (void *cls,
691                   int result)
692 {
693   const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
694   struct GNUNET_CRYPTO_EcdsaPublicKey pub;
695   struct GNUNET_GNSRECORD_Data rd;
696
697   if (GNUNET_YES != result)
698   {
699     FPRINTF (stderr, _("Service `%s' is not running\n"),
700              "namestore");
701     return;
702   }
703   if (! (add|del|list|(NULL != nickstring)|(NULL != uri)|(NULL != reverse_pkey)) )
704   {
705     /* nothing more to be done */
706     fprintf (stderr,
707              _("No options given\n"));
708     GNUNET_SCHEDULER_shutdown ();
709     return;
710   }
711   GNUNET_CRYPTO_ecdsa_key_get_public (&zone_pkey,
712                                     &pub);
713
714   ns = GNUNET_NAMESTORE_connect (cfg);
715   if (NULL == ns)
716   {
717     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
718                 _("Failed to connect to namestore\n"));
719     return;
720   }
721   if (add)
722   {
723     if (NULL == name)
724     {
725       fprintf (stderr,
726                _("Missing option `%s' for operation `%s'\n"),
727                "-n", _("add"));
728       GNUNET_SCHEDULER_shutdown ();
729       ret = 1;
730       return;
731     }
732     if (NULL == typestring)
733     {
734       fprintf (stderr,
735                _("Missing option `%s' for operation `%s'\n"),
736                "-t", _("add"));
737       GNUNET_SCHEDULER_shutdown ();
738       ret = 1;
739       return;
740     }
741     type = GNUNET_GNSRECORD_typename_to_number (typestring);
742     if (UINT32_MAX == type)
743     {
744       fprintf (stderr, _("Unsupported type `%s'\n"), typestring);
745       GNUNET_SCHEDULER_shutdown ();
746       ret = 1;
747       return;
748     }
749     if (NULL == value)
750     {
751       fprintf (stderr,
752                _("Missing option `%s' for operation `%s'\n"),
753                "-V", _("add"));
754       ret = 1;
755       GNUNET_SCHEDULER_shutdown ();
756       return;
757     }
758     if (GNUNET_OK !=
759         GNUNET_GNSRECORD_string_to_value (type,
760                                           value,
761                                           &data,
762                                           &data_size))
763     {
764       fprintf (stderr, _("Value `%s' invalid for record type `%s'\n"),
765                value,
766                typestring);
767       GNUNET_SCHEDULER_shutdown ();
768       ret = 1;
769       return;
770     }
771     if (NULL == expirationstring)
772     {
773       fprintf (stderr,
774                _("Missing option `%s' for operation `%s'\n"),
775                "-e", _("add"));
776       GNUNET_SCHEDULER_shutdown ();
777       ret = 1;
778       return;
779     }
780     if (0 == strcmp (expirationstring, "never"))
781     {
782       etime_abs = GNUNET_TIME_UNIT_FOREVER_ABS;
783       etime_is_rel = GNUNET_NO;
784     }
785     else if (GNUNET_OK ==
786              GNUNET_STRINGS_fancy_time_to_relative (expirationstring,
787                                                     &etime_rel))
788     {
789       etime_is_rel = GNUNET_YES;
790     }
791     else if (GNUNET_OK ==
792              GNUNET_STRINGS_fancy_time_to_absolute (expirationstring,
793                                                     &etime_abs))
794     {
795       etime_is_rel = GNUNET_NO;
796     }
797     else
798     {
799       fprintf (stderr,
800                _("Invalid time format `%s'\n"),
801                expirationstring);
802       GNUNET_SCHEDULER_shutdown ();
803       ret = 1;
804       return;
805     }
806     add_qe = GNUNET_NAMESTORE_records_lookup (ns, &zone_pkey, name,
807         &get_existing_record, NULL );
808   }
809   if (del)
810   {
811     if (NULL == name)
812     {
813       fprintf (stderr,
814                _("Missing option `%s' for operation `%s'\n"),
815                "-n", _("del"));
816       GNUNET_SCHEDULER_shutdown ();
817       ret = 1;
818       return;
819     }
820     del_qe = GNUNET_NAMESTORE_records_lookup (ns,
821                                               &zone_pkey,
822                                               name,
823                                               &del_monitor,
824                                               NULL);
825   }
826   if (list)
827   {
828     list_it = GNUNET_NAMESTORE_zone_iteration_start (ns,
829                                                      &zone_pkey,
830                                                      &display_record,
831                                                      NULL);
832   }
833   if (NULL != reverse_pkey)
834   {
835     struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
836
837     if (GNUNET_OK !=
838         GNUNET_CRYPTO_ecdsa_public_key_from_string (reverse_pkey,
839                                                        strlen (reverse_pkey),
840                                                        &pubkey))
841     {
842       fprintf (stderr,
843                _("Invalid public key for reverse lookup `%s'\n"),
844                reverse_pkey);
845       GNUNET_SCHEDULER_shutdown ();
846     }
847     reverse_qe = GNUNET_NAMESTORE_zone_to_name (ns,
848                                                 &zone_pkey,
849                                                 &pubkey,
850                                                 &handle_reverse_lookup,
851                                                 NULL);
852   }
853   if (NULL != uri)
854   {
855     char sh[105];
856     char sname[64];
857     struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
858
859     GNUNET_STRINGS_utf8_tolower (uri, uri);
860     if ( (2 != (sscanf (uri,
861                         "gnunet://gns/%52s/%63s",
862                         sh,
863                         sname)) ) ||
864          (GNUNET_OK != GNUNET_CRYPTO_ecdsa_public_key_from_string (sh, strlen (sh), &pkey)) )
865     {
866       fprintf (stderr,
867                _("Invalid URI `%s'\n"),
868                uri);
869       GNUNET_SCHEDULER_shutdown ();
870       ret = 1;
871       return;
872     }
873     memset (&rd, 0, sizeof (rd));
874     rd.data = &pkey;
875     rd.data_size = sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
876     rd.record_type = GNUNET_GNSRECORD_TYPE_PKEY;
877     if (GNUNET_YES == etime_is_rel)
878     {
879       rd.expiration_time = etime_rel.rel_value_us;
880       rd.flags |= GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
881     }
882     else if (GNUNET_NO == etime_is_rel)
883       rd.expiration_time = etime_abs.abs_value_us;
884     else
885       rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
886
887     if (1 == is_shadow)
888       rd.flags |= GNUNET_GNSRECORD_RF_SHADOW_RECORD;
889     add_qe_uri = GNUNET_NAMESTORE_records_store (ns,
890                                                  &zone_pkey,
891                                                  sname,
892                                                  1,
893                                                  &rd,
894                                                  &add_continuation,
895                                                  &add_qe_uri);
896   }
897   if (NULL != nickstring)
898   {
899     if (0 == strlen(nickstring))
900     {
901       fprintf (stderr,
902                _("Invalid nick `%s'\n"),
903                nickstring);
904       GNUNET_SCHEDULER_shutdown ();
905       ret = 1;
906       return;
907     }
908     add_qe_uri = GNUNET_NAMESTORE_set_nick(ns, &zone_pkey, nickstring,
909         &add_continuation, &add_qe_uri);
910   }
911   if (monitor)
912   {
913     zm = GNUNET_NAMESTORE_zone_monitor_start (cfg,
914                                               &zone_pkey,
915                                               GNUNET_YES,
916                                               &display_record,
917                                               &sync_cb,
918                                               NULL);
919   }
920 }
921
922
923 /**
924  * Callback invoked from identity service with ego information.
925  * An @a ego of NULL means the ego was not found.
926  *
927  * @param cls closure with the configuration
928  * @param ego an ego known to identity service, or NULL
929  */
930 static void
931 identity_cb (void *cls,
932              const struct GNUNET_IDENTITY_Ego *ego)
933 {
934   const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
935
936   el = NULL;
937   if (NULL == ego)
938   {
939     if (NULL != ego_name)
940     {
941       fprintf (stderr,
942                _("Ego `%s' not known to identity service\n"),
943                ego_name);
944     }
945     GNUNET_SCHEDULER_shutdown ();
946     ret = -1;
947     return;
948   }
949   zone_pkey = *GNUNET_IDENTITY_ego_get_private_key (ego);
950   GNUNET_free_non_null (ego_name);
951   ego_name = NULL;
952   GNUNET_CLIENT_service_test ("namestore", cfg,
953                               GNUNET_TIME_UNIT_SECONDS,
954                               &testservice_task,
955                               (void *) cfg);
956 }
957
958
959 static void
960 default_ego_cb (void *cls,
961                 struct GNUNET_IDENTITY_Ego *ego,
962                 void **ctx,
963                 const char *name)
964 {
965   get_default = NULL;
966   if (NULL == ego)
967   {
968     fprintf (stderr,
969              _("No default ego configured in identity service\n"));
970     GNUNET_SCHEDULER_shutdown ();
971     ret = -1;
972     return;
973   }
974   else
975   {
976     identity_cb (cls, ego);
977   }
978 }
979
980
981 static void
982 id_connect_cb (void *cls,
983                struct GNUNET_IDENTITY_Ego *ego,
984                void **ctx,
985                const char *name)
986 {
987   const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
988
989   if (NULL == ego)
990   {
991     get_default = GNUNET_IDENTITY_get (idh,
992                                        "namestore",
993                                        &default_ego_cb, (void *) cfg);
994   }
995 }
996
997
998 static void
999 testservice_id_task (void *cls, int result)
1000 {
1001   const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
1002   if (result != GNUNET_YES)
1003   {
1004     fprintf (stderr,
1005              _("Identity service is not running\n"));
1006     GNUNET_SCHEDULER_shutdown ();
1007     ret = -1;
1008     return;
1009   }
1010   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1011                                 &do_shutdown, (void *) cfg);
1012
1013   if (NULL == ego_name)
1014   {
1015     idh = GNUNET_IDENTITY_connect (cfg, &id_connect_cb, (void *) cfg);
1016     if (NULL == idh)
1017       fprintf (stderr, _("Cannot connect to identity service\n"));
1018     ret = -1;
1019     return;
1020   }
1021   el = GNUNET_IDENTITY_ego_lookup (cfg,
1022                                    ego_name,
1023                                    &identity_cb,
1024                                    (void *) cfg);
1025 }
1026
1027
1028 /**
1029  * Main function that will be run.
1030  *
1031  * @param cls closure
1032  * @param args remaining command-line arguments
1033  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1034  * @param cfg configuration
1035  */
1036 static void
1037 run (void *cls, char *const *args, const char *cfgfile,
1038      const struct GNUNET_CONFIGURATION_Handle *cfg)
1039 {
1040   if ( (NULL != args[0]) && (NULL == uri) )
1041     uri = GNUNET_strdup (args[0]);
1042
1043   GNUNET_CLIENT_service_test ("identity", cfg,
1044                               GNUNET_TIME_UNIT_SECONDS,
1045                               &testservice_id_task,
1046                               (void *) cfg);
1047 }
1048
1049
1050 /**
1051  * The main function for gnunet-namestore.
1052  *
1053  * @param argc number of arguments from the command line
1054  * @param argv command line arguments
1055  * @return 0 ok, 1 on error
1056  */
1057 int
1058 main (int argc, char *const *argv)
1059 {
1060   is_public = -1;
1061   is_shadow = -1;
1062
1063   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1064     {'a', "add", NULL,
1065      gettext_noop ("add record"), 0,
1066      &GNUNET_GETOPT_set_one, &add},
1067     {'d', "delete", NULL,
1068      gettext_noop ("delete record"), 0,
1069      &GNUNET_GETOPT_set_one, &del},
1070     {'D', "display", NULL,
1071      gettext_noop ("display records"), 0,
1072      &GNUNET_GETOPT_set_one, &list},
1073     {'e', "expiration", "TIME",
1074      gettext_noop ("expiration time for record to use (for adding only), \"never\" is possible"), 1,
1075      &GNUNET_GETOPT_set_string, &expirationstring},
1076     {'i', "nick", "NICKNAME",
1077      gettext_noop ("set the desired nick name for the zone"), 1,
1078      &GNUNET_GETOPT_set_string, &nickstring},
1079     {'m', "monitor", NULL,
1080      gettext_noop ("monitor changes in the namestore"), 0,
1081      &GNUNET_GETOPT_set_one, &monitor},
1082     {'n', "name", "NAME",
1083      gettext_noop ("name of the record to add/delete/display"), 1,
1084      &GNUNET_GETOPT_set_string, &name},
1085     {'r', "reverse", "PKEY",
1086      gettext_noop ("determine our name for the given PKEY"), 1,
1087      &GNUNET_GETOPT_set_string, &reverse_pkey},
1088     {'t', "type", "TYPE",
1089      gettext_noop ("type of the record to add/delete/display"), 1,
1090      &GNUNET_GETOPT_set_string, &typestring},
1091     {'u', "uri", "URI",
1092      gettext_noop ("URI to import into our zone"), 1,
1093      &GNUNET_GETOPT_set_string, &uri},
1094     {'V', "value", "VALUE",
1095      gettext_noop ("value of the record to add/delete"), 1,
1096      &GNUNET_GETOPT_set_string, &value},
1097     {'p', "public", NULL,
1098      gettext_noop ("create or list public record"), 0,
1099      &GNUNET_GETOPT_set_one, &is_public},
1100     {'s', "shadow", NULL,
1101      gettext_noop ("create shadow record (only valid if all other records of the same type have expired"), 0,
1102      &GNUNET_GETOPT_set_one, &is_shadow},
1103     {'z', "zone", "EGO",
1104      gettext_noop ("name of the ego controlling the zone"), 1,
1105      &GNUNET_GETOPT_set_string, &ego_name},
1106     GNUNET_GETOPT_OPTION_END
1107   };
1108
1109   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1110     return 2;
1111
1112   GNUNET_log_setup ("gnunet-namestore", "WARNING", NULL);
1113   if (GNUNET_OK !=
1114       GNUNET_PROGRAM_run (argc, argv, "gnunet-namestore",
1115                           _("GNUnet zone manipulation tool"),
1116                           options,
1117                           &run, NULL))
1118   {
1119     GNUNET_free ((void*) argv);
1120     GNUNET_CRYPTO_ecdsa_key_clear (&zone_pkey);
1121     return 1;
1122   }
1123   GNUNET_free ((void*) argv);
1124   GNUNET_CRYPTO_ecdsa_key_clear (&zone_pkey);
1125   return ret;
1126 }
1127
1128 /* end of gnunet-namestore.c */