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