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