eliminate use of OFFSET in namestore DB queries
[oweals/gnunet.git] / src / namestore / gnunet-namestore.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2012, 2013, 2014 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, 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  */
203 static void
204 do_shutdown (void *cls)
205 {
206   (void) cls;
207   if (NULL != get_default)
208   {
209     GNUNET_IDENTITY_cancel (get_default);
210     get_default = NULL;
211   }
212   if (NULL != idh)
213   {
214     GNUNET_IDENTITY_disconnect (idh);
215     idh = NULL;
216   }
217   if (NULL != el)
218   {
219     GNUNET_IDENTITY_ego_lookup_cancel (el);
220     el = NULL;
221   }
222   if (NULL != list_it)
223   {
224     GNUNET_NAMESTORE_zone_iteration_stop (list_it);
225     list_it = NULL;
226   }
227   if (NULL != add_qe)
228   {
229     GNUNET_NAMESTORE_cancel (add_qe);
230     add_qe = NULL;
231   }
232   if (NULL != add_qe_uri)
233   {
234     GNUNET_NAMESTORE_cancel (add_qe_uri);
235     add_qe_uri = NULL;
236   }
237   if (NULL != del_qe)
238   {
239     GNUNET_NAMESTORE_cancel (del_qe);
240     del_qe = NULL;
241   }
242   if (NULL != ns)
243   {
244     GNUNET_NAMESTORE_disconnect (ns);
245     ns = NULL;
246   }
247   memset (&zone_pkey, 0, sizeof (zone_pkey));
248   if (NULL != uri)
249   {
250     GNUNET_free (uri);
251     uri = NULL;
252   }
253   if (NULL != zm)
254   {
255     GNUNET_NAMESTORE_zone_monitor_stop (zm);
256     zm = NULL;
257   }
258   if (NULL != data)
259   {
260     GNUNET_free (data);
261     data = NULL;
262   }
263 }
264
265
266 /**
267  * Check if we are finished, and if so, perform shutdown.
268  */
269 static void
270 test_finished ()
271 {
272   if ( (NULL == add_qe) &&
273        (NULL == add_qe_uri) &&
274        (NULL == del_qe) &&
275        (NULL == reverse_qe) &&
276        (NULL == list_it) )
277     GNUNET_SCHEDULER_shutdown ();
278 }
279
280
281 /**
282  * Continuation called to notify client about result of the
283  * operation.
284  *
285  * @param cls closure, location of the QueueEntry pointer to NULL out
286  * @param success #GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
287  *                #GNUNET_NO if content was already there
288  *                #GNUNET_YES (or other positive value) on success
289  * @param emsg NULL on success, otherwise an error message
290  */
291 static void
292 add_continuation (void *cls,
293                   int32_t success,
294                   const char *emsg)
295 {
296   struct GNUNET_NAMESTORE_QueueEntry **qe = cls;
297
298   *qe = NULL;
299   if (GNUNET_YES != success)
300   {
301     fprintf (stderr,
302              _("Adding record failed: %s\n"),
303              (GNUNET_NO == success) ? "record exists" : emsg);
304     if (GNUNET_NO != success)
305       ret = 1;
306   }
307   ret = 0;
308   test_finished ();
309 }
310
311
312 /**
313  * Continuation called to notify client about result of the
314  * operation.
315  *
316  * @param cls closure, unused
317  * @param success #GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
318  *                #GNUNET_NO if content was already there
319  *                #GNUNET_YES (or other positive value) on success
320  * @param emsg NULL on success, otherwise an error message
321  */
322 static void
323 del_continuation (void *cls,
324                   int32_t success,
325                   const char *emsg)
326 {
327   (void) cls;
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  * Function called when we are done with a zone iteration.
349  */
350 static void
351 zone_iteration_finished (void *cls)
352 {
353   (void) cls;
354   list_it = NULL;
355   test_finished ();
356 }
357
358
359 /**
360  * Function called when we encountered an error in a zone iteration.
361  */
362 static void
363 zone_iteration_error_cb (void *cls)
364 {
365   (void) cls;
366   list_it = NULL;
367   fprintf (stderr,
368            "Error iterating over zone\n");
369   ret = 1;
370   test_finished ();
371 }
372
373
374 /**
375  * Process a record that was stored in the namestore.
376  *
377  * @param cls closure
378  * @param zone_key private key of the zone
379  * @param rname name that is being mapped (at most 255 characters long)
380  * @param rd_len number of entries in @a rd array
381  * @param rd array of records with data to store
382  */
383 static void
384 display_record (void *cls,
385                 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
386                 const char *rname,
387                 unsigned int rd_len,
388                 const struct GNUNET_GNSRECORD_Data *rd)
389 {
390   const char *typestring;
391   char *s;
392   const char *ets;
393   struct GNUNET_TIME_Absolute at;
394   struct GNUNET_TIME_Relative rt;
395
396   (void) cls;
397   (void) zone_key;
398   if ( (NULL != name) &&
399        (0 != strcmp (name, rname)) )
400   {
401     GNUNET_NAMESTORE_zone_iterator_next (list_it,
402                                          1);
403     return;
404   }
405   FPRINTF (stdout,
406            "%s:\n",
407            rname);
408   for (unsigned int i=0;i<rd_len;i++)
409   {
410     if ( (GNUNET_GNSRECORD_TYPE_NICK == rd[i].record_type) &&
411          (0 != strcmp (rname,
412                        GNUNET_GNS_EMPTY_LABEL_AT)) )
413       continue;
414     typestring = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type);
415     s = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
416                                           rd[i].data,
417                                           rd[i].data_size);
418     if (NULL == s)
419     {
420       FPRINTF (stdout,
421                _("\tCorrupt or unsupported record of type %u\n"),
422                (unsigned int) rd[i].record_type);
423       continue;
424     }
425     if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
426     {
427       rt.rel_value_us = rd[i].expiration_time;
428       ets = GNUNET_STRINGS_relative_time_to_string (rt, GNUNET_YES);
429     }
430     else
431     {
432       at.abs_value_us = rd[i].expiration_time;
433       ets = GNUNET_STRINGS_absolute_time_to_string (at);
434     }
435     FPRINTF (stdout,
436              "\t%s: %s (%s)\t%s\t%s\n",
437              typestring,
438              s,
439              ets,
440              (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_PRIVATE)) ? "PRIVATE" : "PUBLIC",
441              (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_SHADOW_RECORD)) ? "SHADOW" : "");
442     GNUNET_free (s);
443   }
444   FPRINTF (stdout, "%s", "\n");
445   GNUNET_NAMESTORE_zone_iterator_next (list_it,
446                                        1);
447 }
448
449
450 /**
451  * Function called once we are in sync in monitor mode.
452  *
453  * @param cls NULL
454  */
455 static void
456 sync_cb (void *cls)
457 {
458   (void) cls;
459   FPRINTF (stdout,
460            "%s",
461            "Monitor is now in sync.\n");
462 }
463
464
465 /**
466  * Function called on errors while monitoring.
467  *
468  * @param cls NULL
469  */
470 static void
471 monitor_error_cb (void *cls)
472 {
473   (void) cls;
474   FPRINTF (stderr,
475            "%s",
476            "Monitor disconnected and out of sync.\n");
477 }
478
479
480 /**
481  * Function called if lookup fails.
482  */
483 static void
484 lookup_error_cb (void *cls)
485 {
486   (void) cls;
487   add_qe = NULL;
488   GNUNET_break (0);
489   ret = 1;
490   test_finished ();
491 }
492
493
494 /**
495  * We're storing a record; this function is given the existing record
496  * so that we can merge the information.
497  *
498  * @param cls closure, unused
499  * @param zone_key private key of the zone
500  * @param rec_name name that is being mapped (at most 255 characters long)
501  * @param rd_count number of entries in @a rd array
502  * @param rd array of records with data to store
503  */
504 static void
505 get_existing_record (void *cls,
506                      const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
507                      const char *rec_name,
508                      unsigned int rd_count,
509                      const struct GNUNET_GNSRECORD_Data *rd)
510 {
511   struct GNUNET_GNSRECORD_Data rdn[rd_count + 1];
512   struct GNUNET_GNSRECORD_Data *rde;
513
514   (void) cls;
515   (void) zone_key;
516   add_qe = NULL;
517   if (0 != strcmp (rec_name, name))
518   {
519     GNUNET_break (0);
520     ret = 1;
521     test_finished ();
522     return;
523   }
524
525   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
526               "Received %u records for name `%s'\n",
527               rd_count, rec_name);
528   for (unsigned int i=0;i<rd_count;i++)
529   {
530     switch (rd[i].record_type)
531     {
532     case GNUNET_DNSPARSER_TYPE_CNAME:
533       fprintf (stderr,
534                _("A %s record exists already under `%s', no other records can be added.\n"),
535                "CNAME",
536                rec_name);
537       ret = 1;
538       test_finished ();
539       return;
540     case GNUNET_GNSRECORD_TYPE_PKEY:
541       fprintf (stderr,
542                _("A %s record exists already under `%s', no other records can be added.\n"),
543                "PKEY",
544                rec_name);
545       ret = 1;
546       test_finished ();
547       return;
548     case GNUNET_GNSRECORD_TYPE_GNS2DNS:
549       fprintf (stderr,
550                _("A %s record exists already under `%s', no other records can be added.\n"),
551                "GNS2DNS",
552                rec_name);
553       ret = 1;
554       test_finished ();
555       return;
556     }
557   }
558   switch (type)
559   {
560   case GNUNET_DNSPARSER_TYPE_CNAME:
561     if (0 != rd_count)
562     {
563       fprintf (stderr,
564                _("Records already exist under `%s', cannot add `%s' record.\n"),
565                rec_name,
566                "CNAME");
567       ret = 1;
568       test_finished ();
569       return;
570     }
571     break;
572   case GNUNET_GNSRECORD_TYPE_PKEY:
573     if (0 != rd_count)
574     {
575       fprintf (stderr,
576                _("Records already exist under `%s', cannot add `%s' record.\n"),
577                rec_name,
578                "PKEY");
579       ret = 1;
580       test_finished ();
581       return;
582     }
583     break;
584   case GNUNET_GNSRECORD_TYPE_GNS2DNS:
585     if (0 != rd_count)
586     {
587       fprintf (stderr,
588                _("Records already exist under `%s', cannot add `%s' record.\n"),
589                rec_name,
590                "GNS2DNS");
591       ret = 1;
592       test_finished ();
593       return;
594     }
595     break;
596   }
597   memset (rdn,
598           0,
599           sizeof (struct GNUNET_GNSRECORD_Data));
600   GNUNET_memcpy (&rdn[1],
601                  rd,
602                  rd_count * sizeof (struct GNUNET_GNSRECORD_Data));
603   rde = &rdn[0];
604   rde->data = data;
605   rde->data_size = data_size;
606   rde->record_type = type;
607   if (1 == is_shadow)
608     rde->flags |= GNUNET_GNSRECORD_RF_SHADOW_RECORD;
609   if (1 != is_public)
610     rde->flags |= GNUNET_GNSRECORD_RF_PRIVATE;
611   if (GNUNET_YES == etime_is_rel)
612   {
613     rde->expiration_time = etime_rel.rel_value_us;
614     rde->flags |= GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
615   }
616   else if (GNUNET_NO == etime_is_rel)
617     rde->expiration_time = etime_abs.abs_value_us;
618   else
619     rde->expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
620   GNUNET_assert (NULL != name);
621   add_qe = GNUNET_NAMESTORE_records_store (ns,
622                                            &zone_pkey,
623                                            name,
624                                            rd_count + 1,
625                                            rde,
626                                            &add_continuation,
627                                            &add_qe);
628 }
629
630
631 /**
632  * Function called if we encountered an error in zone-to-name.
633  */
634 static void
635 reverse_error_cb (void *cls)
636 {
637   (void) cls;
638   reverse_qe = NULL;
639   FPRINTF (stdout,
640            "%s.zkey\n",
641            reverse_pkey);
642 }
643
644
645 /**
646  * Function called with the result of our attempt to obtain a name for a given
647  * public key.
648  *
649  * @param cls NULL
650  * @param zone private key of the zone; NULL on disconnect
651  * @param label label of the records; NULL on disconnect
652  * @param rd_count number of entries in @a rd array, 0 if label was deleted
653  * @param rd array of records with data to store
654  */
655 static void
656 handle_reverse_lookup (void *cls,
657                        const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
658                        const char *label,
659                        unsigned int rd_count,
660                        const struct GNUNET_GNSRECORD_Data *rd)
661 {
662   (void) cls;
663   (void) zone;
664   (void) rd_count;
665   (void) rd;
666   reverse_qe = NULL;
667   if (NULL == label)
668     FPRINTF (stdout,
669              "%s.zkey\n",
670              reverse_pkey);
671   else
672     FPRINTF (stdout,
673              "%s.gnu\n",
674              label);
675   test_finished ();
676 }
677
678
679 /**
680  * Function called if lookup for deletion fails.
681  */
682 static void
683 del_lookup_error_cb (void *cls)
684 {
685   (void) cls;
686   del_qe = NULL;
687   GNUNET_break (0);
688   ret = 1;
689   test_finished ();
690 }
691
692
693 /**
694  * We were asked to delete something; this function is called with
695  * the existing records. Now we should determine what should be
696  * deleted and then issue the deletion operation.
697  *
698  * @param cls NULL
699  * @param zone private key of the zone we are deleting from
700  * @param label name of the records we are editing
701  * @param rd_count size of the @a rd array
702  * @param rd existing records
703  */
704 static void
705 del_monitor (void *cls,
706              const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
707              const char *label,
708              unsigned int rd_count,
709              const struct GNUNET_GNSRECORD_Data *rd)
710 {
711   struct GNUNET_GNSRECORD_Data rdx[rd_count];
712   unsigned int rd_left;
713   uint32_t type;
714   char *vs;
715
716   (void) cls;
717   (void) zone;
718   del_qe = NULL;
719   if (0 == rd_count)
720   {
721     FPRINTF (stderr,
722              _("There are no records under label `%s' that could be deleted.\n"),
723              label);
724     ret = 1;
725     test_finished ();
726     return;
727   }
728   if ( (NULL == value) &&
729        (NULL == typestring) )
730   {
731     /* delete everything */
732     del_qe = GNUNET_NAMESTORE_records_store (ns,
733                                              &zone_pkey,
734                                              name,
735                                              0,
736                                              NULL,
737                                              &del_continuation,
738                                              NULL);
739     return;
740   }
741   rd_left = 0;
742   if (NULL != typestring)
743     type = GNUNET_GNSRECORD_typename_to_number (typestring);
744   else
745     type = GNUNET_GNSRECORD_TYPE_ANY;
746   for (unsigned int i=0;i<rd_count;i++)
747   {
748     vs = NULL;
749     if (! ( ( (GNUNET_GNSRECORD_TYPE_ANY == type) ||
750               (rd[i].record_type == type) ) &&
751             ( (NULL == value) ||
752               (NULL == (vs = (GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
753                                                                 rd[i].data,
754                                                                 rd[i].data_size)))) ||
755               (0 == strcmp (vs, value)) ) ) )
756       rdx[rd_left++] = rd[i];
757     GNUNET_free_non_null (vs);
758   }
759   if (rd_count == rd_left)
760   {
761     /* nothing got deleted */
762     FPRINTF (stderr,
763              _("There are no records under label `%s' that match the request for deletion.\n"),
764              label);
765     test_finished ();
766     return;
767   }
768   /* delete everything but what we copied to 'rdx' */
769   del_qe = GNUNET_NAMESTORE_records_store (ns,
770                                            &zone_pkey,
771                                            name,
772                                            rd_left,
773                                            rdx,
774                                            &del_continuation,
775                                            NULL);
776 }
777
778
779 /**
780  * Callback invoked from identity service with ego information.
781  * An @a ego of NULL means the ego was not found.
782  *
783  * @param cls closure with the configuration
784  * @param ego an ego known to identity service, or NULL
785  */
786 static void
787 identity_cb (void *cls,
788              const struct GNUNET_IDENTITY_Ego *ego)
789 {
790   const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
791   struct GNUNET_CRYPTO_EcdsaPublicKey pub;
792   struct GNUNET_GNSRECORD_Data rd;
793
794   el = NULL;
795   if (NULL == ego)
796   {
797     if (NULL != ego_name)
798     {
799       fprintf (stderr,
800                _("Ego `%s' not known to identity service\n"),
801                ego_name);
802     }
803     GNUNET_SCHEDULER_shutdown ();
804     ret = -1;
805     return;
806   }
807   zone_pkey = *GNUNET_IDENTITY_ego_get_private_key (ego);
808   GNUNET_free_non_null (ego_name);
809   ego_name = NULL;
810
811   if (! (add|del|list|(NULL != nickstring)|(NULL != uri)|(NULL != reverse_pkey)) )
812   {
813     /* nothing more to be done */
814     fprintf (stderr,
815              _("No options given\n"));
816     GNUNET_SCHEDULER_shutdown ();
817     return;
818   }
819   GNUNET_CRYPTO_ecdsa_key_get_public (&zone_pkey,
820                                     &pub);
821
822   ns = GNUNET_NAMESTORE_connect (cfg);
823   if (NULL == ns)
824   {
825     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
826                 _("Failed to connect to namestore\n"));
827     return;
828   }
829   if (add)
830   {
831     if (NULL == name)
832     {
833       fprintf (stderr,
834                _("Missing option `%s' for operation `%s'\n"),
835                "-n", _("add"));
836       GNUNET_SCHEDULER_shutdown ();
837       ret = 1;
838       return;
839     }
840     if (NULL == typestring)
841     {
842       fprintf (stderr,
843                _("Missing option `%s' for operation `%s'\n"),
844                "-t", _("add"));
845       GNUNET_SCHEDULER_shutdown ();
846       ret = 1;
847       return;
848     }
849     type = GNUNET_GNSRECORD_typename_to_number (typestring);
850     if (UINT32_MAX == type)
851     {
852       fprintf (stderr,
853                _("Unsupported type `%s'\n"),
854                typestring);
855       GNUNET_SCHEDULER_shutdown ();
856       ret = 1;
857       return;
858     }
859     if (NULL == value)
860     {
861       fprintf (stderr,
862                _("Missing option `%s' for operation `%s'\n"),
863                "-V", _("add"));
864       ret = 1;
865       GNUNET_SCHEDULER_shutdown ();
866       return;
867     }
868     if (GNUNET_OK !=
869         GNUNET_GNSRECORD_string_to_value (type,
870                                           value,
871                                           &data,
872                                           &data_size))
873     {
874       fprintf (stderr,
875                _("Value `%s' invalid for record type `%s'\n"),
876                value,
877                typestring);
878       GNUNET_SCHEDULER_shutdown ();
879       ret = 1;
880       return;
881     }
882     if (NULL == expirationstring)
883     {
884       fprintf (stderr,
885                _("Missing option `%s' for operation `%s'\n"),
886                "-e",
887                _("add"));
888       GNUNET_SCHEDULER_shutdown ();
889       ret = 1;
890       return;
891     }
892     if (0 == strcmp (expirationstring,
893                      "never"))
894     {
895       etime_abs = GNUNET_TIME_UNIT_FOREVER_ABS;
896       etime_is_rel = GNUNET_NO;
897     }
898     else if (GNUNET_OK ==
899              GNUNET_STRINGS_fancy_time_to_relative (expirationstring,
900                                                     &etime_rel))
901     {
902       etime_is_rel = GNUNET_YES;
903       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
904                   "Storing record with relative expiration time of %s\n",
905                   GNUNET_STRINGS_relative_time_to_string (etime_rel,
906                                                           GNUNET_NO));
907     }
908     else if (GNUNET_OK ==
909              GNUNET_STRINGS_fancy_time_to_absolute (expirationstring,
910                                                     &etime_abs))
911     {
912       etime_is_rel = GNUNET_NO;
913       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
914                   "Storing record with absolute expiration time of %s\n",
915                   GNUNET_STRINGS_absolute_time_to_string (etime_abs));
916     }
917     else
918     {
919       fprintf (stderr,
920                _("Invalid time format `%s'\n"),
921                expirationstring);
922       GNUNET_SCHEDULER_shutdown ();
923       ret = 1;
924       return;
925     }
926     add_qe = GNUNET_NAMESTORE_records_lookup (ns,
927                                               &zone_pkey,
928                                               name,
929                                               &lookup_error_cb,
930                                               NULL,
931                                               &get_existing_record,
932                                               NULL);
933   }
934   if (del)
935   {
936     if (NULL == name)
937     {
938       fprintf (stderr,
939                _("Missing option `%s' for operation `%s'\n"),
940                "-n", _("del"));
941       GNUNET_SCHEDULER_shutdown ();
942       ret = 1;
943       return;
944     }
945     del_qe = GNUNET_NAMESTORE_records_lookup (ns,
946                                               &zone_pkey,
947                                               name,
948                                               &del_lookup_error_cb,
949                                               NULL,
950                                               &del_monitor,
951                                               NULL);
952   }
953   if (list)
954   {
955     list_it = GNUNET_NAMESTORE_zone_iteration_start (ns,
956                                                      &zone_pkey,
957                                                      &zone_iteration_error_cb,
958                                                      NULL,
959                                                      &display_record,
960                                                      NULL,
961                                                      &zone_iteration_finished,
962                                                      NULL);
963   }
964   if (NULL != reverse_pkey)
965   {
966     struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
967
968     if (GNUNET_OK !=
969         GNUNET_CRYPTO_ecdsa_public_key_from_string (reverse_pkey,
970                                                     strlen (reverse_pkey),
971                                                     &pubkey))
972     {
973       fprintf (stderr,
974                _("Invalid public key for reverse lookup `%s'\n"),
975                reverse_pkey);
976       GNUNET_SCHEDULER_shutdown ();
977     }
978     reverse_qe = GNUNET_NAMESTORE_zone_to_name (ns,
979                                                 &zone_pkey,
980                                                 &pubkey,
981                                                 &reverse_error_cb,
982                                                 NULL,
983                                                 &handle_reverse_lookup,
984                                                 NULL);
985   }
986   if (NULL != uri)
987   {
988     char sh[105];
989     char sname[64];
990     struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
991
992     GNUNET_STRINGS_utf8_tolower (uri, uri);
993     if ( (2 != (sscanf (uri,
994                         "gnunet://gns/%52s/%63s",
995                         sh,
996                         sname)) ) ||
997          (GNUNET_OK !=
998           GNUNET_CRYPTO_ecdsa_public_key_from_string (sh,
999                                                       strlen (sh),
1000                                                       &pkey)) )
1001     {
1002       fprintf (stderr,
1003                _("Invalid URI `%s'\n"),
1004                uri);
1005       GNUNET_SCHEDULER_shutdown ();
1006       ret = 1;
1007       return;
1008     }
1009     memset (&rd, 0, sizeof (rd));
1010     rd.data = &pkey;
1011     rd.data_size = sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
1012     rd.record_type = GNUNET_GNSRECORD_TYPE_PKEY;
1013     if (GNUNET_YES == etime_is_rel)
1014     {
1015       rd.expiration_time = etime_rel.rel_value_us;
1016       rd.flags |= GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
1017     }
1018     else if (GNUNET_NO == etime_is_rel)
1019       rd.expiration_time = etime_abs.abs_value_us;
1020     else
1021       rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
1022
1023     if (1 == is_shadow)
1024       rd.flags |= GNUNET_GNSRECORD_RF_SHADOW_RECORD;
1025     add_qe_uri = GNUNET_NAMESTORE_records_store (ns,
1026                                                  &zone_pkey,
1027                                                  sname,
1028                                                  1,
1029                                                  &rd,
1030                                                  &add_continuation,
1031                                                  &add_qe_uri);
1032   }
1033   if (NULL != nickstring)
1034   {
1035     if (0 == strlen(nickstring))
1036     {
1037       fprintf (stderr,
1038                _("Invalid nick `%s'\n"),
1039                nickstring);
1040       GNUNET_SCHEDULER_shutdown ();
1041       ret = 1;
1042       return;
1043     }
1044     add_qe_uri = GNUNET_NAMESTORE_set_nick (ns,
1045                                             &zone_pkey,
1046                                             nickstring,
1047                                             &add_continuation,
1048                                             &add_qe_uri);
1049   }
1050   if (monitor)
1051   {
1052     zm = GNUNET_NAMESTORE_zone_monitor_start (cfg,
1053                                               &zone_pkey,
1054                                               GNUNET_YES,
1055                                               &monitor_error_cb,
1056                                               NULL,
1057                                               &display_record,
1058                                               NULL,
1059                                               &sync_cb,
1060                                               NULL);
1061   }
1062 }
1063
1064
1065 static void
1066 default_ego_cb (void *cls,
1067                 struct GNUNET_IDENTITY_Ego *ego,
1068                 void **ctx,
1069                 const char *name)
1070 {
1071   (void) cls;
1072   (void) ctx;
1073   (void) name;
1074   get_default = NULL;
1075   if (NULL == ego)
1076   {
1077     fprintf (stderr,
1078              _("No default ego configured in identity service\n"));
1079     GNUNET_SCHEDULER_shutdown ();
1080     ret = -1;
1081     return;
1082   }
1083   else
1084   {
1085     identity_cb (cls, ego);
1086   }
1087 }
1088
1089
1090 static void
1091 id_connect_cb (void *cls,
1092                struct GNUNET_IDENTITY_Ego *ego,
1093                void **ctx,
1094                const char *name)
1095 {
1096   const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
1097
1098   (void) cls;
1099   (void) ctx;
1100   (void) name;
1101   if (NULL == ego)
1102   {
1103     get_default = GNUNET_IDENTITY_get (idh,
1104                                        "namestore",
1105                                        &default_ego_cb,
1106                                        (void *) cfg);
1107   }
1108 }
1109
1110
1111 /**
1112  * Main function that will be run.
1113  *
1114  * @param cls closure
1115  * @param args remaining command-line arguments
1116  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1117  * @param cfg configuration
1118  */
1119 static void
1120 run (void *cls,
1121      char *const *args,
1122      const char *cfgfile,
1123      const struct GNUNET_CONFIGURATION_Handle *cfg)
1124 {
1125   (void) cls;
1126   (void) args;
1127   (void) cfgfile;
1128   if ( (NULL != args[0]) &&
1129        (NULL == uri) )
1130     uri = GNUNET_strdup (args[0]);
1131
1132   GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
1133                                  (void *) cfg);
1134
1135   if (NULL == ego_name)
1136   {
1137     idh = GNUNET_IDENTITY_connect (cfg,
1138                                    &id_connect_cb,
1139                                    (void *) cfg);
1140     if (NULL == idh)
1141       fprintf (stderr,
1142                _("Cannot connect to identity service\n"));
1143     ret = -1;
1144     return;
1145   }
1146   el = GNUNET_IDENTITY_ego_lookup (cfg,
1147                                    ego_name,
1148                                    &identity_cb,
1149                                    (void *) cfg);
1150 }
1151
1152
1153 /**
1154  * The main function for gnunet-namestore.
1155  *
1156  * @param argc number of arguments from the command line
1157  * @param argv command line arguments
1158  * @return 0 ok, 1 on error
1159  */
1160 int
1161 main (int argc,
1162       char *const *argv)
1163 {
1164   struct GNUNET_GETOPT_CommandLineOption options[] = {
1165     GNUNET_GETOPT_option_flag ('a',
1166                                "add",
1167                                gettext_noop ("add record"),
1168                                &add),
1169     GNUNET_GETOPT_option_flag ('d',
1170                                "delete",
1171                                gettext_noop ("delete record"),
1172                                &del),
1173     GNUNET_GETOPT_option_flag ('D',
1174                                "display",
1175                                gettext_noop ("display records"),
1176                                &list),
1177     GNUNET_GETOPT_option_string ('e',
1178                                  "expiration",
1179                                  "TIME",
1180                                  gettext_noop ("expiration time for record to use (for adding only), \"never\" is possible"),
1181                                  &expirationstring),
1182     GNUNET_GETOPT_option_string ('i',
1183                                  "nick",
1184                                  "NICKNAME",
1185                                  gettext_noop ("set the desired nick name for the zone"),
1186                                  &nickstring),
1187     GNUNET_GETOPT_option_flag ('m',
1188                                "monitor",
1189                                gettext_noop ("monitor changes in the namestore"),
1190                                &monitor),
1191     GNUNET_GETOPT_option_string ('n',
1192                                  "name",
1193                                  "NAME",
1194                                  gettext_noop ("name of the record to add/delete/display"),
1195                                  &name),
1196     GNUNET_GETOPT_option_string ('r',
1197                                  "reverse",
1198                                  "PKEY",
1199                                  gettext_noop ("determine our name for the given PKEY"),
1200                                  &reverse_pkey),
1201     GNUNET_GETOPT_option_string ('t',
1202                                  "type",
1203                                  "TYPE",
1204                                  gettext_noop ("type of the record to add/delete/display"),
1205                                  &typestring),
1206     GNUNET_GETOPT_option_string ('u',
1207                                  "uri",
1208                                  "URI",
1209                                  gettext_noop ("URI to import into our zone"),
1210                                  &uri),
1211     GNUNET_GETOPT_option_string ('V',
1212                                  "value",
1213                                  "VALUE",
1214                                  gettext_noop ("value of the record to add/delete"),
1215                                  &value),
1216     GNUNET_GETOPT_option_flag ('p',
1217                                "public",
1218                                gettext_noop ("create or list public record"),
1219                                &is_public),
1220     GNUNET_GETOPT_option_flag ('s',
1221                                "shadow",
1222                                gettext_noop ("create shadow record (only valid if all other records of the same type have expired"),
1223                                &is_shadow),
1224     GNUNET_GETOPT_option_string ('z',
1225                                  "zone",
1226                                  "EGO",
1227                                  gettext_noop ("name of the ego controlling the zone"),
1228                                  &ego_name),
1229     GNUNET_GETOPT_OPTION_END
1230   };
1231
1232   if (GNUNET_OK !=
1233       GNUNET_STRINGS_get_utf8_args (argc, argv,
1234                                     &argc, &argv))
1235     return 2;
1236
1237   is_public = -1;
1238   is_shadow = -1;
1239   GNUNET_log_setup ("gnunet-namestore",
1240                     "WARNING",
1241                     NULL);
1242   if (GNUNET_OK !=
1243       GNUNET_PROGRAM_run (argc,
1244                           argv,
1245                           "gnunet-namestore",
1246                           _("GNUnet zone manipulation tool"),
1247                           options,
1248                           &run, NULL))
1249   {
1250     GNUNET_free ((void*) argv);
1251     GNUNET_CRYPTO_ecdsa_key_clear (&zone_pkey);
1252     return 1;
1253   }
1254   GNUNET_free ((void*) argv);
1255   GNUNET_CRYPTO_ecdsa_key_clear (&zone_pkey);
1256   return ret;
1257 }
1258
1259 /* end of gnunet-namestore.c */