glitch in the license text detected by hyazinthe, thank you!
[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 it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14 */
15 /**
16  * @file gnunet-namestore.c
17  * @brief command line tool to manipulate the local zone
18  * @author Christian Grothoff
19  *
20  * TODO:
21  * - test
22  */
23 #include "platform.h"
24 #include <gnunet_util_lib.h>
25 #include <gnunet_dnsparser_lib.h>
26 #include <gnunet_identity_service.h>
27 #include <gnunet_gnsrecord_lib.h>
28 #include <gnunet_gns_service.h>
29 #include <gnunet_namestore_service.h>
30
31
32 /**
33  * Handle to the namestore.
34  */
35 static struct GNUNET_NAMESTORE_Handle *ns;
36
37 /**
38  * Private key for the our zone.
39  */
40 static struct GNUNET_CRYPTO_EcdsaPrivateKey zone_pkey;
41
42 /**
43  * Handle to identity lookup.
44  */
45 static struct GNUNET_IDENTITY_EgoLookup *el;
46
47 /**
48  * Identity service handle
49  */
50 static struct GNUNET_IDENTITY_Handle *idh;
51
52 /**
53  * Obtain default ego
54  */
55 struct GNUNET_IDENTITY_Operation *get_default;
56
57 /**
58  * Name of the ego controlling the zone.
59  */
60 static char *ego_name;
61
62 /**
63  * Desired action is to add a record.
64  */
65 static int add;
66
67 /**
68  * Queue entry for the 'add-uri' operation.
69  */
70 static struct GNUNET_NAMESTORE_QueueEntry *add_qe_uri;
71
72 /**
73  * Queue entry for the 'add' operation.
74  */
75 static struct GNUNET_NAMESTORE_QueueEntry *add_qe;
76
77 /**
78  * Queue entry for the 'lookup' operation.
79  */
80 static struct GNUNET_NAMESTORE_QueueEntry *get_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 != get_qe)
238   {
239     GNUNET_NAMESTORE_cancel (get_qe);
240     get_qe = NULL;
241   }
242   if (NULL != del_qe)
243   {
244     GNUNET_NAMESTORE_cancel (del_qe);
245     del_qe = NULL;
246   }
247   if (NULL != ns)
248   {
249     GNUNET_NAMESTORE_disconnect (ns);
250     ns = NULL;
251   }
252   memset (&zone_pkey, 0, sizeof (zone_pkey));
253   if (NULL != uri)
254   {
255     GNUNET_free (uri);
256     uri = NULL;
257   }
258   if (NULL != zm)
259   {
260     GNUNET_NAMESTORE_zone_monitor_stop (zm);
261     zm = NULL;
262   }
263   if (NULL != data)
264   {
265     GNUNET_free (data);
266     data = NULL;
267   }
268 }
269
270
271 /**
272  * Check if we are finished, and if so, perform shutdown.
273  */
274 static void
275 test_finished ()
276 {
277   if ( (NULL == add_qe) &&
278        (NULL == add_qe_uri) &&
279        (NULL == get_qe) &&
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   (void) cls;
334   del_qe = NULL;
335   if (GNUNET_NO == success)
336   {
337     fprintf (stderr,
338              _("Deleting record failed, record does not exist%s%s\n"),
339              (NULL != emsg) ? ": " : "",
340              (NULL != emsg) ? emsg : "");
341   }
342   if (GNUNET_SYSERR == success)
343   {
344     fprintf (stderr,
345              _("Deleting record failed%s%s\n"),
346              (NULL != emsg) ? ": " : "",
347              (NULL != emsg) ? emsg : "");
348   }
349   test_finished ();
350 }
351
352
353 /**
354  * Function called when we are done with a zone iteration.
355  */
356 static void
357 zone_iteration_finished (void *cls)
358 {
359   (void) cls;
360   list_it = NULL;
361   test_finished ();
362 }
363
364
365 /**
366  * Function called when we encountered an error in a zone iteration.
367  */
368 static void
369 zone_iteration_error_cb (void *cls)
370 {
371   (void) cls;
372   list_it = NULL;
373   fprintf (stderr,
374            "Error iterating over zone\n");
375   ret = 1;
376   test_finished ();
377 }
378
379
380 /**
381  * Process a record that was stored in the namestore.
382  *
383  * @param rname name that is being mapped (at most 255 characters long)
384  * @param rd_len number of entries in @a rd array
385  * @param rd array of records with data to store
386  */
387 static void
388 display_record (const char *rname,
389                 unsigned int rd_len,
390                 const struct GNUNET_GNSRECORD_Data *rd)
391 {
392   const char *typestring;
393   char *s;
394   const char *ets;
395   struct GNUNET_TIME_Absolute at;
396   struct GNUNET_TIME_Relative rt;
397
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 }
446
447
448 /**
449  * Process a record that was stored in the namestore.
450  *
451  * @param cls closure
452  * @param zone_key private key of the zone
453  * @param rname name that is being mapped (at most 255 characters long)
454  * @param rd_len number of entries in @a rd array
455  * @param rd array of records with data to store
456  */
457 static void
458 display_record_iterator (void *cls,
459                          const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
460                          const char *rname,
461                          unsigned int rd_len,
462                          const struct GNUNET_GNSRECORD_Data *rd)
463 {
464   (void) cls;
465   (void) zone_key;
466   display_record (rname,
467                   rd_len,
468                   rd);
469   GNUNET_NAMESTORE_zone_iterator_next (list_it,
470                                        1);
471 }
472
473
474 /**
475  * Process a record that was stored in the namestore.
476  *
477  * @param cls closure
478  * @param zone_key private key of the zone
479  * @param rname name that is being mapped (at most 255 characters long)
480  * @param rd_len number of entries in @a rd array
481  * @param rd array of records with data to store
482  */
483 static void
484 display_record_monitor (void *cls,
485                         const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
486                         const char *rname,
487                         unsigned int rd_len,
488                         const struct GNUNET_GNSRECORD_Data *rd)
489 {
490   (void) cls;
491   (void) zone_key;
492   display_record (rname,
493                   rd_len,
494                   rd);
495   GNUNET_NAMESTORE_zone_monitor_next (zm,
496                                       1);
497 }
498
499
500 /**
501  * Process a record that was stored in the namestore.
502  *
503  * @param cls closure
504  * @param zone_key private key of the zone
505  * @param rname name that is being mapped (at most 255 characters long)
506  * @param rd_len number of entries in @a rd array
507  * @param rd array of records with data to store
508  */
509 static void
510 display_record_lookup (void *cls,
511                        const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
512                        const char *rname,
513                        unsigned int rd_len,
514                        const struct GNUNET_GNSRECORD_Data *rd)
515 {
516   get_qe = NULL;
517   display_record (rname,
518                   rd_len,
519                   rd);
520   test_finished ();
521 }
522
523
524 /**
525  * Function called once we are in sync in monitor mode.
526  *
527  * @param cls NULL
528  */
529 static void
530 sync_cb (void *cls)
531 {
532   (void) cls;
533   FPRINTF (stdout,
534            "%s",
535            "Monitor is now in sync.\n");
536 }
537
538
539 /**
540  * Function called on errors while monitoring.
541  *
542  * @param cls NULL
543  */
544 static void
545 monitor_error_cb (void *cls)
546 {
547   (void) cls;
548   FPRINTF (stderr,
549            "%s",
550            "Monitor disconnected and out of sync.\n");
551 }
552
553
554 /**
555  * Function called on errors while monitoring.
556  *
557  * @param cls NULL
558  */
559 static void
560 lookup_error_cb (void *cls)
561 {
562   (void) cls;
563   get_qe = NULL;
564   FPRINTF (stderr,
565            "%s",
566            "Failed to lookup record.\n");
567   test_finished ();
568 }
569
570
571 /**
572  * Function called if lookup fails.
573  */
574 static void
575 add_error_cb (void *cls)
576 {
577   (void) cls;
578   add_qe = NULL;
579   GNUNET_break (0);
580   ret = 1;
581   test_finished ();
582 }
583
584
585 /**
586  * We're storing a record; this function is given the existing record
587  * so that we can merge the information.
588  *
589  * @param cls closure, unused
590  * @param zone_key private key of the zone
591  * @param rec_name name that is being mapped (at most 255 characters long)
592  * @param rd_count number of entries in @a rd array
593  * @param rd array of records with data to store
594  */
595 static void
596 get_existing_record (void *cls,
597                      const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
598                      const char *rec_name,
599                      unsigned int rd_count,
600                      const struct GNUNET_GNSRECORD_Data *rd)
601 {
602   struct GNUNET_GNSRECORD_Data rdn[rd_count + 1];
603   struct GNUNET_GNSRECORD_Data *rde;
604
605   (void) cls;
606   (void) zone_key;
607   add_qe = NULL;
608   if (0 != strcmp (rec_name, name))
609   {
610     GNUNET_break (0);
611     ret = 1;
612     test_finished ();
613     return;
614   }
615
616   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
617               "Received %u records for name `%s'\n",
618               rd_count, rec_name);
619   for (unsigned int i=0;i<rd_count;i++)
620   {
621     switch (rd[i].record_type)
622     {
623     case GNUNET_DNSPARSER_TYPE_CNAME:
624       fprintf (stderr,
625                _("A %s record exists already under `%s', no other records can be added.\n"),
626                "CNAME",
627                rec_name);
628       ret = 1;
629       test_finished ();
630       return;
631     case GNUNET_GNSRECORD_TYPE_PKEY:
632       fprintf (stderr,
633                _("A %s record exists already under `%s', no other records can be added.\n"),
634                "PKEY",
635                rec_name);
636       ret = 1;
637       test_finished ();
638       return;
639     }
640   }
641   switch (type)
642   {
643   case GNUNET_DNSPARSER_TYPE_CNAME:
644     if (0 != rd_count)
645     {
646       fprintf (stderr,
647                _("Records already exist under `%s', cannot add `%s' record.\n"),
648                rec_name,
649                "CNAME");
650       ret = 1;
651       test_finished ();
652       return;
653     }
654     break;
655   case GNUNET_GNSRECORD_TYPE_PKEY:
656     if (0 != rd_count)
657     {
658       fprintf (stderr,
659                _("Records already exist under `%s', cannot add `%s' record.\n"),
660                rec_name,
661                "PKEY");
662       ret = 1;
663       test_finished ();
664       return;
665     }
666     break;
667   case GNUNET_GNSRECORD_TYPE_GNS2DNS:
668     for (unsigned int i=0;i<rd_count;i++)
669       if (GNUNET_GNSRECORD_TYPE_GNS2DNS != rd[i].record_type)
670       {
671         fprintf (stderr,
672                  _("Non-GNS2DNS records already exist under `%s', cannot add GNS2DNS record.\n"),
673                  rec_name);
674         ret = 1;
675         test_finished ();
676         return;
677       }
678     break;
679   }
680   memset (rdn,
681           0,
682           sizeof (struct GNUNET_GNSRECORD_Data));
683   GNUNET_memcpy (&rdn[1],
684                  rd,
685                  rd_count * sizeof (struct GNUNET_GNSRECORD_Data));
686   rde = &rdn[0];
687   rde->data = data;
688   rde->data_size = data_size;
689   rde->record_type = type;
690   if (1 == is_shadow)
691     rde->flags |= GNUNET_GNSRECORD_RF_SHADOW_RECORD;
692   if (1 != is_public)
693     rde->flags |= GNUNET_GNSRECORD_RF_PRIVATE;
694   if (GNUNET_YES == etime_is_rel)
695   {
696     rde->expiration_time = etime_rel.rel_value_us;
697     rde->flags |= GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
698   }
699   else if (GNUNET_NO == etime_is_rel)
700     rde->expiration_time = etime_abs.abs_value_us;
701   else
702     rde->expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
703   GNUNET_assert (NULL != name);
704   add_qe = GNUNET_NAMESTORE_records_store (ns,
705                                            &zone_pkey,
706                                            name,
707                                            rd_count + 1,
708                                            rde,
709                                            &add_continuation,
710                                            &add_qe);
711 }
712
713
714 /**
715  * Function called if we encountered an error in zone-to-name.
716  */
717 static void
718 reverse_error_cb (void *cls)
719 {
720   (void) cls;
721   reverse_qe = NULL;
722   FPRINTF (stdout,
723            "%s.zkey\n",
724            reverse_pkey);
725 }
726
727
728 /**
729  * Function called with the result of our attempt to obtain a name for a given
730  * public key.
731  *
732  * @param cls NULL
733  * @param zone private key of the zone; NULL on disconnect
734  * @param label label of the records; NULL on disconnect
735  * @param rd_count number of entries in @a rd array, 0 if label was deleted
736  * @param rd array of records with data to store
737  */
738 static void
739 handle_reverse_lookup (void *cls,
740                        const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
741                        const char *label,
742                        unsigned int rd_count,
743                        const struct GNUNET_GNSRECORD_Data *rd)
744 {
745   (void) cls;
746   (void) zone;
747   (void) rd_count;
748   (void) rd;
749   reverse_qe = NULL;
750   if (NULL == label)
751     FPRINTF (stdout,
752              "%s.zkey\n",
753              reverse_pkey);
754   else
755     FPRINTF (stdout,
756              "%s.gnu\n",
757              label);
758   test_finished ();
759 }
760
761
762 /**
763  * Function called if lookup for deletion fails.
764  */
765 static void
766 del_lookup_error_cb (void *cls)
767 {
768   (void) cls;
769   del_qe = NULL;
770   GNUNET_break (0);
771   ret = 1;
772   test_finished ();
773 }
774
775
776 /**
777  * We were asked to delete something; this function is called with
778  * the existing records. Now we should determine what should be
779  * deleted and then issue the deletion operation.
780  *
781  * @param cls NULL
782  * @param zone private key of the zone we are deleting from
783  * @param label name of the records we are editing
784  * @param rd_count size of the @a rd array
785  * @param rd existing records
786  */
787 static void
788 del_monitor (void *cls,
789              const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
790              const char *label,
791              unsigned int rd_count,
792              const struct GNUNET_GNSRECORD_Data *rd)
793 {
794   struct GNUNET_GNSRECORD_Data rdx[rd_count];
795   unsigned int rd_left;
796   uint32_t type;
797   char *vs;
798
799   (void) cls;
800   (void) zone;
801   del_qe = NULL;
802   if (0 == rd_count)
803   {
804     FPRINTF (stderr,
805              _("There are no records under label `%s' that could be deleted.\n"),
806              label);
807     ret = 1;
808     test_finished ();
809     return;
810   }
811   if ( (NULL == value) &&
812        (NULL == typestring) )
813   {
814     /* delete everything */
815     del_qe = GNUNET_NAMESTORE_records_store (ns,
816                                              &zone_pkey,
817                                              name,
818                                              0,
819                                              NULL,
820                                              &del_continuation,
821                                              NULL);
822     return;
823   }
824   rd_left = 0;
825   if (NULL != typestring)
826     type = GNUNET_GNSRECORD_typename_to_number (typestring);
827   else
828     type = GNUNET_GNSRECORD_TYPE_ANY;
829   for (unsigned int i=0;i<rd_count;i++)
830   {
831     vs = NULL;
832     if (! ( ( (GNUNET_GNSRECORD_TYPE_ANY == type) ||
833               (rd[i].record_type == type) ) &&
834             ( (NULL == value) ||
835               (NULL == (vs = (GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
836                                                                 rd[i].data,
837                                                                 rd[i].data_size)))) ||
838               (0 == strcmp (vs, value)) ) ) )
839       rdx[rd_left++] = rd[i];
840     GNUNET_free_non_null (vs);
841   }
842   if (rd_count == rd_left)
843   {
844     /* nothing got deleted */
845     FPRINTF (stderr,
846              _("There are no records under label `%s' that match the request for deletion.\n"),
847              label);
848     test_finished ();
849     return;
850   }
851   /* delete everything but what we copied to 'rdx' */
852   del_qe = GNUNET_NAMESTORE_records_store (ns,
853                                            &zone_pkey,
854                                            name,
855                                            rd_left,
856                                            rdx,
857                                            &del_continuation,
858                                            NULL);
859 }
860
861
862 /**
863  * Callback invoked from identity service with ego information.
864  * An @a ego of NULL means the ego was not found.
865  *
866  * @param cls closure with the configuration
867  * @param ego an ego known to identity service, or NULL
868  */
869 static void
870 identity_cb (void *cls,
871              const struct GNUNET_IDENTITY_Ego *ego)
872 {
873   const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
874   struct GNUNET_CRYPTO_EcdsaPublicKey pub;
875   struct GNUNET_GNSRECORD_Data rd;
876
877   el = NULL;
878   if (NULL == ego)
879   {
880     if (NULL != ego_name)
881     {
882       fprintf (stderr,
883                _("Ego `%s' not known to identity service\n"),
884                ego_name);
885     }
886     GNUNET_SCHEDULER_shutdown ();
887     ret = -1;
888     return;
889   }
890   zone_pkey = *GNUNET_IDENTITY_ego_get_private_key (ego);
891   GNUNET_free_non_null (ego_name);
892   ego_name = NULL;
893
894   if (! (add|del|list|(NULL != nickstring)|(NULL != uri)|(NULL != reverse_pkey)) )
895   {
896     /* nothing more to be done */
897     fprintf (stderr,
898              _("No options given\n"));
899     GNUNET_SCHEDULER_shutdown ();
900     return;
901   }
902   GNUNET_CRYPTO_ecdsa_key_get_public (&zone_pkey,
903                                     &pub);
904
905   ns = GNUNET_NAMESTORE_connect (cfg);
906   if (NULL == ns)
907   {
908     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
909                 _("Failed to connect to namestore\n"));
910     return;
911   }
912   if (add)
913   {
914     if (NULL == name)
915     {
916       fprintf (stderr,
917                _("Missing option `%s' for operation `%s'\n"),
918                "-n", _("add"));
919       GNUNET_SCHEDULER_shutdown ();
920       ret = 1;
921       return;
922     }
923     if (NULL == typestring)
924     {
925       fprintf (stderr,
926                _("Missing option `%s' for operation `%s'\n"),
927                "-t", _("add"));
928       GNUNET_SCHEDULER_shutdown ();
929       ret = 1;
930       return;
931     }
932     type = GNUNET_GNSRECORD_typename_to_number (typestring);
933     if (UINT32_MAX == type)
934     {
935       fprintf (stderr,
936                _("Unsupported type `%s'\n"),
937                typestring);
938       GNUNET_SCHEDULER_shutdown ();
939       ret = 1;
940       return;
941     }
942     if (NULL == value)
943     {
944       fprintf (stderr,
945                _("Missing option `%s' for operation `%s'\n"),
946                "-V", _("add"));
947       ret = 1;
948       GNUNET_SCHEDULER_shutdown ();
949       return;
950     }
951     if (GNUNET_OK !=
952         GNUNET_GNSRECORD_string_to_value (type,
953                                           value,
954                                           &data,
955                                           &data_size))
956     {
957       fprintf (stderr,
958                _("Value `%s' invalid for record type `%s'\n"),
959                value,
960                typestring);
961       GNUNET_SCHEDULER_shutdown ();
962       ret = 1;
963       return;
964     }
965     if (NULL == expirationstring)
966     {
967       fprintf (stderr,
968                _("Missing option `%s' for operation `%s'\n"),
969                "-e",
970                _("add"));
971       GNUNET_SCHEDULER_shutdown ();
972       ret = 1;
973       return;
974     }
975     if (0 == strcmp (expirationstring,
976                      "never"))
977     {
978       etime_abs = GNUNET_TIME_UNIT_FOREVER_ABS;
979       etime_is_rel = GNUNET_NO;
980     }
981     else if (GNUNET_OK ==
982              GNUNET_STRINGS_fancy_time_to_relative (expirationstring,
983                                                     &etime_rel))
984     {
985       etime_is_rel = GNUNET_YES;
986       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
987                   "Storing record with relative expiration time of %s\n",
988                   GNUNET_STRINGS_relative_time_to_string (etime_rel,
989                                                           GNUNET_NO));
990     }
991     else if (GNUNET_OK ==
992              GNUNET_STRINGS_fancy_time_to_absolute (expirationstring,
993                                                     &etime_abs))
994     {
995       etime_is_rel = GNUNET_NO;
996       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
997                   "Storing record with absolute expiration time of %s\n",
998                   GNUNET_STRINGS_absolute_time_to_string (etime_abs));
999     }
1000     else
1001     {
1002       fprintf (stderr,
1003                _("Invalid time format `%s'\n"),
1004                expirationstring);
1005       GNUNET_SCHEDULER_shutdown ();
1006       ret = 1;
1007       return;
1008     }
1009     add_qe = GNUNET_NAMESTORE_records_lookup (ns,
1010                                               &zone_pkey,
1011                                               name,
1012                                               &add_error_cb,
1013                                               NULL,
1014                                               &get_existing_record,
1015                                               NULL);
1016   }
1017   if (del)
1018   {
1019     if (NULL == name)
1020     {
1021       fprintf (stderr,
1022                _("Missing option `%s' for operation `%s'\n"),
1023                "-n", _("del"));
1024       GNUNET_SCHEDULER_shutdown ();
1025       ret = 1;
1026       return;
1027     }
1028     del_qe = GNUNET_NAMESTORE_records_lookup (ns,
1029                                               &zone_pkey,
1030                                               name,
1031                                               &del_lookup_error_cb,
1032                                               NULL,
1033                                               &del_monitor,
1034                                               NULL);
1035   }
1036   if (list)
1037   {
1038     if (NULL != name)
1039       get_qe = GNUNET_NAMESTORE_records_lookup (ns,
1040                                                 &zone_pkey,
1041                                                 name,
1042                                                 &lookup_error_cb,
1043                                                 NULL,
1044                                                 &display_record_lookup,
1045                                                 NULL);
1046     else
1047       list_it = GNUNET_NAMESTORE_zone_iteration_start (ns,
1048                                                        &zone_pkey,
1049                                                        &zone_iteration_error_cb,
1050                                                        NULL,
1051                                                        &display_record_iterator,
1052                                                        NULL,
1053                                                        &zone_iteration_finished,
1054                                                        NULL);
1055   }
1056   if (NULL != reverse_pkey)
1057   {
1058     struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
1059
1060     if (GNUNET_OK !=
1061         GNUNET_CRYPTO_ecdsa_public_key_from_string (reverse_pkey,
1062                                                     strlen (reverse_pkey),
1063                                                     &pubkey))
1064     {
1065       fprintf (stderr,
1066                _("Invalid public key for reverse lookup `%s'\n"),
1067                reverse_pkey);
1068       GNUNET_SCHEDULER_shutdown ();
1069     }
1070     reverse_qe = GNUNET_NAMESTORE_zone_to_name (ns,
1071                                                 &zone_pkey,
1072                                                 &pubkey,
1073                                                 &reverse_error_cb,
1074                                                 NULL,
1075                                                 &handle_reverse_lookup,
1076                                                 NULL);
1077   }
1078   if (NULL != uri)
1079   {
1080     char sh[105];
1081     char sname[64];
1082     struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
1083
1084     GNUNET_STRINGS_utf8_tolower (uri, uri);
1085     if ( (2 != (sscanf (uri,
1086                         "gnunet://gns/%52s/%63s",
1087                         sh,
1088                         sname)) ) ||
1089          (GNUNET_OK !=
1090           GNUNET_CRYPTO_ecdsa_public_key_from_string (sh,
1091                                                       strlen (sh),
1092                                                       &pkey)) )
1093     {
1094       fprintf (stderr,
1095                _("Invalid URI `%s'\n"),
1096                uri);
1097       GNUNET_SCHEDULER_shutdown ();
1098       ret = 1;
1099       return;
1100     }
1101     memset (&rd, 0, sizeof (rd));
1102     rd.data = &pkey;
1103     rd.data_size = sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
1104     rd.record_type = GNUNET_GNSRECORD_TYPE_PKEY;
1105     if (GNUNET_YES == etime_is_rel)
1106     {
1107       rd.expiration_time = etime_rel.rel_value_us;
1108       rd.flags |= GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
1109     }
1110     else if (GNUNET_NO == etime_is_rel)
1111       rd.expiration_time = etime_abs.abs_value_us;
1112     else
1113       rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
1114
1115     if (1 == is_shadow)
1116       rd.flags |= GNUNET_GNSRECORD_RF_SHADOW_RECORD;
1117     add_qe_uri = GNUNET_NAMESTORE_records_store (ns,
1118                                                  &zone_pkey,
1119                                                  sname,
1120                                                  1,
1121                                                  &rd,
1122                                                  &add_continuation,
1123                                                  &add_qe_uri);
1124   }
1125   if (NULL != nickstring)
1126   {
1127     if (0 == strlen(nickstring))
1128     {
1129       fprintf (stderr,
1130                _("Invalid nick `%s'\n"),
1131                nickstring);
1132       GNUNET_SCHEDULER_shutdown ();
1133       ret = 1;
1134       return;
1135     }
1136     add_qe_uri = GNUNET_NAMESTORE_set_nick (ns,
1137                                             &zone_pkey,
1138                                             nickstring,
1139                                             &add_continuation,
1140                                             &add_qe_uri);
1141   }
1142   if (monitor)
1143   {
1144     zm = GNUNET_NAMESTORE_zone_monitor_start (cfg,
1145                                               &zone_pkey,
1146                                               GNUNET_YES,
1147                                               &monitor_error_cb,
1148                                               NULL,
1149                                               &display_record_monitor,
1150                                               NULL,
1151                                               &sync_cb,
1152                                               NULL);
1153   }
1154 }
1155
1156
1157 static void
1158 default_ego_cb (void *cls,
1159                 struct GNUNET_IDENTITY_Ego *ego,
1160                 void **ctx,
1161                 const char *name)
1162 {
1163   (void) cls;
1164   (void) ctx;
1165   (void) name;
1166   get_default = NULL;
1167   if (NULL == ego)
1168   {
1169     fprintf (stderr,
1170              _("No default ego configured in identity service\n"));
1171     GNUNET_SCHEDULER_shutdown ();
1172     ret = -1;
1173     return;
1174   }
1175   else
1176   {
1177     identity_cb (cls, ego);
1178   }
1179 }
1180
1181
1182 static void
1183 id_connect_cb (void *cls,
1184                struct GNUNET_IDENTITY_Ego *ego,
1185                void **ctx,
1186                const char *name)
1187 {
1188   const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
1189
1190   (void) cls;
1191   (void) ctx;
1192   (void) name;
1193   if (NULL == ego)
1194   {
1195     get_default = GNUNET_IDENTITY_get (idh,
1196                                        "namestore",
1197                                        &default_ego_cb,
1198                                        (void *) cfg);
1199   }
1200 }
1201
1202
1203 /**
1204  * Main function that will be run.
1205  *
1206  * @param cls closure
1207  * @param args remaining command-line arguments
1208  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1209  * @param cfg configuration
1210  */
1211 static void
1212 run (void *cls,
1213      char *const *args,
1214      const char *cfgfile,
1215      const struct GNUNET_CONFIGURATION_Handle *cfg)
1216 {
1217   (void) cls;
1218   (void) args;
1219   (void) cfgfile;
1220   if ( (NULL != args[0]) &&
1221        (NULL == uri) )
1222     uri = GNUNET_strdup (args[0]);
1223
1224   GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
1225                                  (void *) cfg);
1226
1227   if (NULL == ego_name)
1228   {
1229     idh = GNUNET_IDENTITY_connect (cfg,
1230                                    &id_connect_cb,
1231                                    (void *) cfg);
1232     if (NULL == idh)
1233       fprintf (stderr,
1234                _("Cannot connect to identity service\n"));
1235     ret = -1;
1236     return;
1237   }
1238   el = GNUNET_IDENTITY_ego_lookup (cfg,
1239                                    ego_name,
1240                                    &identity_cb,
1241                                    (void *) cfg);
1242 }
1243
1244
1245 /**
1246  * The main function for gnunet-namestore.
1247  *
1248  * @param argc number of arguments from the command line
1249  * @param argv command line arguments
1250  * @return 0 ok, 1 on error
1251  */
1252 int
1253 main (int argc,
1254       char *const *argv)
1255 {
1256   struct GNUNET_GETOPT_CommandLineOption options[] = {
1257     GNUNET_GETOPT_option_flag ('a',
1258                                "add",
1259                                gettext_noop ("add record"),
1260                                &add),
1261     GNUNET_GETOPT_option_flag ('d',
1262                                "delete",
1263                                gettext_noop ("delete record"),
1264                                &del),
1265     GNUNET_GETOPT_option_flag ('D',
1266                                "display",
1267                                gettext_noop ("display records"),
1268                                &list),
1269     GNUNET_GETOPT_option_string ('e',
1270                                  "expiration",
1271                                  "TIME",
1272                                  gettext_noop ("expiration time for record to use (for adding only), \"never\" is possible"),
1273                                  &expirationstring),
1274     GNUNET_GETOPT_option_string ('i',
1275                                  "nick",
1276                                  "NICKNAME",
1277                                  gettext_noop ("set the desired nick name for the zone"),
1278                                  &nickstring),
1279     GNUNET_GETOPT_option_flag ('m',
1280                                "monitor",
1281                                gettext_noop ("monitor changes in the namestore"),
1282                                &monitor),
1283     GNUNET_GETOPT_option_string ('n',
1284                                  "name",
1285                                  "NAME",
1286                                  gettext_noop ("name of the record to add/delete/display"),
1287                                  &name),
1288     GNUNET_GETOPT_option_string ('r',
1289                                  "reverse",
1290                                  "PKEY",
1291                                  gettext_noop ("determine our name for the given PKEY"),
1292                                  &reverse_pkey),
1293     GNUNET_GETOPT_option_string ('t',
1294                                  "type",
1295                                  "TYPE",
1296                                  gettext_noop ("type of the record to add/delete/display"),
1297                                  &typestring),
1298     GNUNET_GETOPT_option_string ('u',
1299                                  "uri",
1300                                  "URI",
1301                                  gettext_noop ("URI to import into our zone"),
1302                                  &uri),
1303     GNUNET_GETOPT_option_string ('V',
1304                                  "value",
1305                                  "VALUE",
1306                                  gettext_noop ("value of the record to add/delete"),
1307                                  &value),
1308     GNUNET_GETOPT_option_flag ('p',
1309                                "public",
1310                                gettext_noop ("create or list public record"),
1311                                &is_public),
1312     GNUNET_GETOPT_option_flag ('s',
1313                                "shadow",
1314                                gettext_noop ("create shadow record (only valid if all other records of the same type have expired"),
1315                                &is_shadow),
1316     GNUNET_GETOPT_option_string ('z',
1317                                  "zone",
1318                                  "EGO",
1319                                  gettext_noop ("name of the ego controlling the zone"),
1320                                  &ego_name),
1321     GNUNET_GETOPT_OPTION_END
1322   };
1323
1324   if (GNUNET_OK !=
1325       GNUNET_STRINGS_get_utf8_args (argc, argv,
1326                                     &argc, &argv))
1327     return 2;
1328
1329   is_public = -1;
1330   is_shadow = -1;
1331   GNUNET_log_setup ("gnunet-namestore",
1332                     "WARNING",
1333                     NULL);
1334   if (GNUNET_OK !=
1335       GNUNET_PROGRAM_run (argc,
1336                           argv,
1337                           "gnunet-namestore",
1338                           _("GNUnet zone manipulation tool"),
1339                           options,
1340                           &run, NULL))
1341   {
1342     GNUNET_free ((void*) argv);
1343     GNUNET_CRYPTO_ecdsa_key_clear (&zone_pkey);
1344     return 1;
1345   }
1346   GNUNET_free ((void*) argv);
1347   GNUNET_CRYPTO_ecdsa_key_clear (&zone_pkey);
1348   return ret;
1349 }
1350
1351 /* end of gnunet-namestore.c */