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