2 This file is part of GNUnet.
3 Copyright (C) 2012, 2013, 2014, 2019 GNUnet e.V.
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.
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.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @file gnunet-namestore.c
22 * @brief command line tool to manipulate the local zone
23 * @author Christian Grothoff
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>
38 * Entry in record set for bulk processing.
43 * Kept in a linked list.
45 struct RecordSetEntry *next;
48 * The record to add/remove.
50 struct GNUNET_GNSRECORD_Data record;
55 * Handle to the namestore.
57 static struct GNUNET_NAMESTORE_Handle *ns;
60 * Private key for the our zone.
62 static struct GNUNET_CRYPTO_EcdsaPrivateKey zone_pkey;
65 * Handle to identity lookup.
67 static struct GNUNET_IDENTITY_EgoLookup *el;
70 * Identity service handle
72 static struct GNUNET_IDENTITY_Handle *idh;
77 struct GNUNET_IDENTITY_Operation *get_default;
80 * Name of the ego controlling the zone.
82 static char *ego_name;
85 * Desired action is to add a record.
90 * Queue entry for the 'add-uri' operation.
92 static struct GNUNET_NAMESTORE_QueueEntry *add_qe_uri;
95 * Queue entry for the 'add' operation.
97 static struct GNUNET_NAMESTORE_QueueEntry *add_qe;
100 * Queue entry for the 'lookup' operation.
102 static struct GNUNET_NAMESTORE_QueueEntry *get_qe;
105 * Queue entry for the 'reverse lookup' operation (in combination with a name).
107 static struct GNUNET_NAMESTORE_QueueEntry *reverse_qe;
110 * Desired action is to list records.
115 * List iterator for the 'list' operation.
117 static struct GNUNET_NAMESTORE_ZoneIterator *list_it;
120 * Desired action is to remove a record.
125 * Is record public (opposite of #GNUNET_GNSRECORD_RF_PRIVATE)
127 static int is_public;
130 * Is record a shadow record (#GNUNET_GNSRECORD_RF_SHADOW_RECORD)
132 static int is_shadow;
135 * Queue entry for the 'del' operation.
137 static struct GNUNET_NAMESTORE_QueueEntry *del_qe;
140 * Queue entry for the 'set/replace' operation.
142 static struct GNUNET_NAMESTORE_QueueEntry *set_qe;
145 * Name of the records to add/list/remove.
150 * Value of the record to add/remove.
160 * Reverse lookup to perform.
162 static char *reverse_pkey;
165 * Type of the record to add/remove, NULL to remove all.
167 static char *typestring;
170 * Desired expiration time.
172 static char *expirationstring;
177 static char *nickstring;
180 * Global return value
185 * Type string converted to DNS type value.
187 static uint32_t type;
190 * Value in binary format.
195 * Number of bytes in #data.
197 static size_t data_size;
200 * Expiration string converted to numeric value.
202 static uint64_t etime;
205 * Is expiration time relative or absolute time?
207 static int etime_is_rel = GNUNET_SYSERR;
212 static struct GNUNET_NAMESTORE_ZoneMonitor *zm;
215 * Enables monitor mode.
220 * Entry in record set for processing records in bulk.
222 static struct RecordSetEntry *recordset;
226 * Task run on shutdown. Cleans up everything.
231 do_shutdown (void *cls)
234 if (NULL != get_default)
236 GNUNET_IDENTITY_cancel (get_default);
241 GNUNET_IDENTITY_disconnect (idh);
246 GNUNET_IDENTITY_ego_lookup_cancel (el);
251 GNUNET_NAMESTORE_zone_iteration_stop (list_it);
256 GNUNET_NAMESTORE_cancel (add_qe);
261 GNUNET_NAMESTORE_cancel (set_qe);
264 if (NULL != add_qe_uri)
266 GNUNET_NAMESTORE_cancel (add_qe_uri);
271 GNUNET_NAMESTORE_cancel (get_qe);
276 GNUNET_NAMESTORE_cancel (del_qe);
281 GNUNET_NAMESTORE_disconnect (ns);
284 memset (&zone_pkey, 0, sizeof (zone_pkey));
292 GNUNET_NAMESTORE_zone_monitor_stop (zm);
304 * Check if we are finished, and if so, perform shutdown.
309 if ((NULL == add_qe) && (NULL == add_qe_uri) && (NULL == get_qe) &&
310 (NULL == del_qe) && (NULL == reverse_qe) && (NULL == list_it))
311 GNUNET_SCHEDULER_shutdown ();
316 * Continuation called to notify client about result of the
319 * @param cls closure, location of the QueueEntry pointer to NULL out
320 * @param success #GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
321 * #GNUNET_NO if content was already there
322 * #GNUNET_YES (or other positive value) on success
323 * @param emsg NULL on success, otherwise an error message
326 add_continuation (void *cls, int32_t success, const char *emsg)
328 struct GNUNET_NAMESTORE_QueueEntry **qe = cls;
331 if (GNUNET_YES != success)
334 _ ("Adding record failed: %s\n"),
335 (GNUNET_NO == success) ? "record exists" : emsg);
336 if (GNUNET_NO != success)
345 * Continuation called to notify client about result of the
348 * @param cls closure, unused
349 * @param success #GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
350 * #GNUNET_NO if content was already there
351 * #GNUNET_YES (or other positive value) on success
352 * @param emsg NULL on success, otherwise an error message
355 del_continuation (void *cls, int32_t success, const char *emsg)
359 if (GNUNET_NO == success)
362 _ ("Deleting record failed, record does not exist%s%s\n"),
363 (NULL != emsg) ? ": " : "",
364 (NULL != emsg) ? emsg : "");
366 if (GNUNET_SYSERR == success)
369 _ ("Deleting record failed%s%s\n"),
370 (NULL != emsg) ? ": " : "",
371 (NULL != emsg) ? emsg : "");
378 * Function called when we are done with a zone iteration.
381 zone_iteration_finished (void *cls)
390 * Function called when we encountered an error in a zone iteration.
393 zone_iteration_error_cb (void *cls)
397 fprintf (stderr, "Error iterating over zone\n");
404 * Process a record that was stored in the namestore.
406 * @param rname name that is being mapped (at most 255 characters long)
407 * @param rd_len number of entries in @a rd array
408 * @param rd array of records with data to store
411 display_record (const char *rname,
413 const struct GNUNET_GNSRECORD_Data *rd)
418 struct GNUNET_TIME_Absolute at;
419 struct GNUNET_TIME_Relative rt;
422 if ((NULL != name) && (0 != strcmp (name, rname)))
424 GNUNET_NAMESTORE_zone_iterator_next (list_it, 1);
427 have_record = GNUNET_NO;
428 for (unsigned int i = 0; i < rd_len; i++)
430 if ((GNUNET_GNSRECORD_TYPE_NICK == rd[i].record_type) &&
431 (0 != strcmp (rname, GNUNET_GNS_EMPTY_LABEL_AT)))
433 if ((type != rd[i].record_type) && (GNUNET_GNSRECORD_TYPE_ANY != type))
435 have_record = GNUNET_YES;
438 if (GNUNET_NO == have_record)
440 FPRINTF (stdout, "%s:\n", rname);
441 if (NULL != typestring)
442 type = GNUNET_GNSRECORD_typename_to_number (typestring);
444 type = GNUNET_GNSRECORD_TYPE_ANY;
445 for (unsigned int i = 0; i < rd_len; i++)
447 if ((GNUNET_GNSRECORD_TYPE_NICK == rd[i].record_type) &&
448 (0 != strcmp (rname, GNUNET_GNS_EMPTY_LABEL_AT)))
450 if ((type != rd[i].record_type) && (GNUNET_GNSRECORD_TYPE_ANY != type))
452 typestr = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type);
453 s = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
459 _ ("\tCorrupt or unsupported record of type %u\n"),
460 (unsigned int) rd[i].record_type);
463 if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
465 rt.rel_value_us = rd[i].expiration_time;
466 ets = GNUNET_STRINGS_relative_time_to_string (rt, GNUNET_YES);
470 at.abs_value_us = rd[i].expiration_time;
471 ets = GNUNET_STRINGS_absolute_time_to_string (at);
474 "\t%s: %s (%s)\t%s\t%s\n",
478 (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_PRIVATE)) ? "PRIVATE"
480 (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_SHADOW_RECORD)) ? "SHADOW"
484 FPRINTF (stdout, "%s", "\n");
489 * Process a record that was stored in the namestore.
492 * @param zone_key private key of the zone
493 * @param rname name that is being mapped (at most 255 characters long)
494 * @param rd_len number of entries in @a rd array
495 * @param rd array of records with data to store
498 display_record_iterator (void *cls,
499 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
502 const struct GNUNET_GNSRECORD_Data *rd)
506 display_record (rname, rd_len, rd);
507 GNUNET_NAMESTORE_zone_iterator_next (list_it, 1);
512 * Process a record that was stored in the namestore.
515 * @param zone_key private key of the zone
516 * @param rname name that is being mapped (at most 255 characters long)
517 * @param rd_len number of entries in @a rd array
518 * @param rd array of records with data to store
521 display_record_monitor (void *cls,
522 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
525 const struct GNUNET_GNSRECORD_Data *rd)
529 display_record (rname, rd_len, rd);
530 GNUNET_NAMESTORE_zone_monitor_next (zm, 1);
535 * Process a record that was stored in the namestore.
538 * @param zone_key private key of the zone
539 * @param rname name that is being mapped (at most 255 characters long)
540 * @param rd_len number of entries in @a rd array
541 * @param rd array of records with data to store
544 display_record_lookup (void *cls,
545 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
548 const struct GNUNET_GNSRECORD_Data *rd)
553 display_record (rname, rd_len, rd);
559 * Function called once we are in sync in monitor mode.
567 FPRINTF (stdout, "%s", "Monitor is now in sync.\n");
572 * Function called on errors while monitoring.
577 monitor_error_cb (void *cls)
580 FPRINTF (stderr, "%s", "Monitor disconnected and out of sync.\n");
585 * Function called on errors while monitoring.
590 lookup_error_cb (void *cls)
594 FPRINTF (stderr, "%s", "Failed to lookup record.\n");
600 * Function called if lookup fails.
603 add_error_cb (void *cls)
614 * We're storing a record; this function is given the existing record
615 * so that we can merge the information.
617 * @param cls closure, unused
618 * @param zone_key private key of the zone
619 * @param rec_name name that is being mapped (at most 255 characters long)
620 * @param rd_count number of entries in @a rd array
621 * @param rd array of records with data to store
624 get_existing_record (void *cls,
625 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
626 const char *rec_name,
627 unsigned int rd_count,
628 const struct GNUNET_GNSRECORD_Data *rd)
630 struct GNUNET_GNSRECORD_Data rdn[rd_count + 1];
631 struct GNUNET_GNSRECORD_Data *rde;
636 if (0 != strcmp (rec_name, name))
644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
645 "Received %u records for name `%s'\n",
648 for (unsigned int i = 0; i < rd_count; i++)
650 switch (rd[i].record_type)
652 case GNUNET_DNSPARSER_TYPE_CNAME:
656 "A %s record exists already under `%s', no other records can be added.\n"),
662 case GNUNET_GNSRECORD_TYPE_PKEY:
666 "A %s record exists already under `%s', no other records can be added.\n"),
672 case GNUNET_DNSPARSER_TYPE_SOA:
673 if (GNUNET_DNSPARSER_TYPE_SOA == type)
678 "A SOA record exists already under `%s', cannot add a second SOA to the same zone.\n"),
689 case GNUNET_DNSPARSER_TYPE_CNAME:
694 "Records already exist under `%s', cannot add `%s' record.\n"),
702 case GNUNET_GNSRECORD_TYPE_PKEY:
707 "Records already exist under `%s', cannot add `%s' record.\n"),
715 case GNUNET_GNSRECORD_TYPE_GNS2DNS:
716 for (unsigned int i = 0; i < rd_count; i++)
717 if (GNUNET_GNSRECORD_TYPE_GNS2DNS != rd[i].record_type)
722 "Non-GNS2DNS records already exist under `%s', cannot add GNS2DNS record.\n"),
730 memset (rdn, 0, sizeof (struct GNUNET_GNSRECORD_Data));
731 GNUNET_memcpy (&rdn[1], rd, rd_count * sizeof (struct GNUNET_GNSRECORD_Data));
734 rde->data_size = data_size;
735 rde->record_type = type;
737 rde->flags |= GNUNET_GNSRECORD_RF_SHADOW_RECORD;
739 rde->flags |= GNUNET_GNSRECORD_RF_PRIVATE;
740 rde->expiration_time = etime;
741 if (GNUNET_YES == etime_is_rel)
742 rde->flags |= GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
743 else if (GNUNET_NO != etime_is_rel)
744 rde->expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
745 GNUNET_assert (NULL != name);
746 add_qe = GNUNET_NAMESTORE_records_store (ns,
757 * Function called if we encountered an error in zone-to-name.
760 reverse_error_cb (void *cls)
764 FPRINTF (stdout, "%s.zkey\n", reverse_pkey);
769 * Function called with the result of our attempt to obtain a name for a given
773 * @param zone private key of the zone; NULL on disconnect
774 * @param label label of the records; NULL on disconnect
775 * @param rd_count number of entries in @a rd array, 0 if label was deleted
776 * @param rd array of records with data to store
779 handle_reverse_lookup (void *cls,
780 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
782 unsigned int rd_count,
783 const struct GNUNET_GNSRECORD_Data *rd)
791 FPRINTF (stdout, "%s\n", reverse_pkey);
793 FPRINTF (stdout, "%s.%s\n", label, ego_name);
799 * Function called if lookup for deletion fails.
802 del_lookup_error_cb (void *cls)
813 * We were asked to delete something; this function is called with
814 * the existing records. Now we should determine what should be
815 * deleted and then issue the deletion operation.
818 * @param zone private key of the zone we are deleting from
819 * @param label name of the records we are editing
820 * @param rd_count size of the @a rd array
821 * @param rd existing records
824 del_monitor (void *cls,
825 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
827 unsigned int rd_count,
828 const struct GNUNET_GNSRECORD_Data *rd)
830 struct GNUNET_GNSRECORD_Data rdx[rd_count];
831 unsigned int rd_left;
842 "There are no records under label `%s' that could be deleted.\n"),
848 if ((NULL == value) && (NULL == typestring))
850 /* delete everything */
851 del_qe = GNUNET_NAMESTORE_records_store (ns,
861 if (NULL != typestring)
862 type = GNUNET_GNSRECORD_typename_to_number (typestring);
864 type = GNUNET_GNSRECORD_TYPE_ANY;
865 for (unsigned int i = 0; i < rd_count; i++)
868 if (! (((GNUNET_GNSRECORD_TYPE_ANY == type) ||
869 (rd[i].record_type == type)) &&
872 (vs = (GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
874 rd[i].data_size)))) ||
875 (0 == strcmp (vs, value)))))
876 rdx[rd_left++] = rd[i];
877 GNUNET_free_non_null (vs);
879 if (rd_count == rd_left)
881 /* nothing got deleted */
885 "There are no records under label `%s' that match the request for deletion.\n"),
890 /* delete everything but what we copied to 'rdx' */
891 del_qe = GNUNET_NAMESTORE_records_store (ns,
902 * Parse expiration time.
904 * @param expirationstring text to parse
905 * @param etime_is_rel[out] set to #GNUNET_YES if time is relative
906 * @param etime[out] set to expiration time (abs or rel)
907 * @return #GNUNET_OK on success
910 parse_expiration (const char *expirationstring,
914 struct GNUNET_TIME_Relative etime_rel;
915 struct GNUNET_TIME_Absolute etime_abs;
917 if (0 == strcmp (expirationstring, "never"))
919 *etime = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
920 *etime_is_rel = GNUNET_NO;
924 GNUNET_STRINGS_fancy_time_to_relative (expirationstring, &etime_rel))
926 *etime_is_rel = GNUNET_YES;
927 *etime = etime_rel.rel_value_us;
928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
929 "Storing record with relative expiration time of %s\n",
930 GNUNET_STRINGS_relative_time_to_string (etime_rel, GNUNET_NO));
934 GNUNET_STRINGS_fancy_time_to_absolute (expirationstring, &etime_abs))
936 *etime_is_rel = GNUNET_NO;
937 *etime = etime_abs.abs_value_us;
938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
939 "Storing record with absolute expiration time of %s\n",
940 GNUNET_STRINGS_absolute_time_to_string (etime_abs));
943 return GNUNET_SYSERR;
948 * Function called when namestore is done with the replace
952 * @param success #GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
953 * #GNUNET_NO if content was already there or not found
954 * #GNUNET_YES (or other positive value) on success
955 * @param emsg NULL on success, otherwise an error message
958 replace_cont (void *cls, int success, const char *emsg)
963 if (GNUNET_OK != success)
965 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
966 _ ("Failed to replace records: %s\n"),
968 ret = 1; /* fail from 'main' */
970 GNUNET_SCHEDULER_shutdown ();
975 * Callback invoked from identity service with ego information.
976 * An @a ego of NULL means the ego was not found.
978 * @param cls closure with the configuration
979 * @param ego an ego known to identity service, or NULL
982 identity_cb (void *cls, const struct GNUNET_IDENTITY_Ego *ego)
984 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
985 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
986 struct GNUNET_GNSRECORD_Data rd;
989 if ((NULL != name) && (0 != strchr (name, '.')))
992 _ ("Label `%s' contains `.' which is not allowed\n"),
994 GNUNET_SCHEDULER_shutdown ();
1001 if (NULL != ego_name)
1004 _ ("Ego `%s' not known to identity service\n"),
1007 GNUNET_SCHEDULER_shutdown ();
1011 zone_pkey = *GNUNET_IDENTITY_ego_get_private_key (ego);
1012 GNUNET_free_non_null (ego_name);
1015 if (! (add | del | list | (NULL != nickstring) | (NULL != uri) |
1016 (NULL != reverse_pkey) | (NULL != recordset)))
1018 /* nothing more to be done */
1019 fprintf (stderr, _ ("No options given\n"));
1020 GNUNET_SCHEDULER_shutdown ();
1023 GNUNET_CRYPTO_ecdsa_key_get_public (&zone_pkey, &pub);
1024 ns = GNUNET_NAMESTORE_connect (cfg);
1027 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1028 _ ("Failed to connect to namestore\n"));
1032 if (NULL != recordset)
1034 /* replace entire record set */
1035 unsigned int rd_count;
1036 struct GNUNET_GNSRECORD_Data *rd;
1041 _ ("Missing option `%s' for operation `%s'\n"),
1044 GNUNET_SCHEDULER_shutdown ();
1049 for (struct RecordSetEntry *e = recordset; NULL != e; e = e->next)
1051 rd = GNUNET_new_array (rd_count, struct GNUNET_GNSRECORD_Data);
1053 for (struct RecordSetEntry *e = recordset; NULL != e; e = e->next)
1055 rd[rd_count] = e->record;
1058 set_qe = GNUNET_NAMESTORE_records_store (ns,
1074 _ ("Missing option `%s' for operation `%s'\n"),
1077 GNUNET_SCHEDULER_shutdown ();
1081 if (NULL == typestring)
1084 _ ("Missing option `%s' for operation `%s'\n"),
1087 GNUNET_SCHEDULER_shutdown ();
1091 type = GNUNET_GNSRECORD_typename_to_number (typestring);
1092 if (UINT32_MAX == type)
1094 fprintf (stderr, _ ("Unsupported type `%s'\n"), typestring);
1095 GNUNET_SCHEDULER_shutdown ();
1102 _ ("Missing option `%s' for operation `%s'\n"),
1106 GNUNET_SCHEDULER_shutdown ();
1110 GNUNET_GNSRECORD_string_to_value (type, value, &data, &data_size))
1113 _ ("Value `%s' invalid for record type `%s'\n"),
1116 GNUNET_SCHEDULER_shutdown ();
1120 if (NULL == expirationstring)
1123 _ ("Missing option `%s' for operation `%s'\n"),
1126 GNUNET_SCHEDULER_shutdown ();
1130 if (GNUNET_OK != parse_expiration (expirationstring, &etime_is_rel, &etime))
1132 fprintf (stderr, _ ("Invalid time format `%s'\n"), expirationstring);
1133 GNUNET_SCHEDULER_shutdown ();
1137 add_qe = GNUNET_NAMESTORE_records_lookup (ns,
1142 &get_existing_record,
1150 _ ("Missing option `%s' for operation `%s'\n"),
1153 GNUNET_SCHEDULER_shutdown ();
1157 del_qe = GNUNET_NAMESTORE_records_lookup (ns,
1160 &del_lookup_error_cb,
1168 get_qe = GNUNET_NAMESTORE_records_lookup (ns,
1173 &display_record_lookup,
1176 list_it = GNUNET_NAMESTORE_zone_iteration_start (ns,
1178 &zone_iteration_error_cb,
1180 &display_record_iterator,
1182 &zone_iteration_finished,
1185 if (NULL != reverse_pkey)
1187 struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
1190 GNUNET_CRYPTO_ecdsa_public_key_from_string (reverse_pkey,
1191 strlen (reverse_pkey),
1195 _ ("Invalid public key for reverse lookup `%s'\n"),
1197 GNUNET_SCHEDULER_shutdown ();
1199 reverse_qe = GNUNET_NAMESTORE_zone_to_name (ns,
1204 &handle_reverse_lookup,
1211 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
1213 GNUNET_STRINGS_utf8_tolower (uri, uri);
1214 if ((2 != (sscanf (uri, "gnunet://gns/%52s/%63s", sh, sname))) ||
1216 GNUNET_CRYPTO_ecdsa_public_key_from_string (sh, strlen (sh), &pkey)))
1218 fprintf (stderr, _ ("Invalid URI `%s'\n"), uri);
1219 GNUNET_SCHEDULER_shutdown ();
1223 memset (&rd, 0, sizeof (rd));
1225 rd.data_size = sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
1226 rd.record_type = GNUNET_GNSRECORD_TYPE_PKEY;
1227 rd.expiration_time = etime;
1228 if (GNUNET_YES == etime_is_rel)
1229 rd.flags |= GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
1231 rd.flags |= GNUNET_GNSRECORD_RF_SHADOW_RECORD;
1232 add_qe_uri = GNUNET_NAMESTORE_records_store (ns,
1240 if (NULL != nickstring)
1242 if (0 == strlen (nickstring))
1244 fprintf (stderr, _ ("Invalid nick `%s'\n"), nickstring);
1245 GNUNET_SCHEDULER_shutdown ();
1249 add_qe_uri = GNUNET_NAMESTORE_set_nick (ns,
1257 zm = GNUNET_NAMESTORE_zone_monitor_start (cfg,
1262 &display_record_monitor,
1271 default_ego_cb (void *cls,
1272 struct GNUNET_IDENTITY_Ego *ego,
1282 fprintf (stderr, _ ("No default ego configured in identity service\n"));
1283 GNUNET_SCHEDULER_shutdown ();
1289 identity_cb (cls, ego);
1295 id_connect_cb (void *cls,
1296 struct GNUNET_IDENTITY_Ego *ego,
1300 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
1308 GNUNET_IDENTITY_get (idh, "namestore", &default_ego_cb, (void *) cfg);
1314 * Main function that will be run.
1316 * @param cls closure
1317 * @param args remaining command-line arguments
1318 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1319 * @param cfg configuration
1324 const char *cfgfile,
1325 const struct GNUNET_CONFIGURATION_Handle *cfg)
1330 if (NULL != args[0])
1332 GNUNET_ERROR_TYPE_WARNING,
1333 _ ("Superfluous command line arguments (starting with `%s') ignored\n"),
1335 if ((NULL != args[0]) && (NULL == uri))
1336 uri = GNUNET_strdup (args[0]);
1338 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, (void *) cfg);
1340 if (NULL == ego_name)
1342 idh = GNUNET_IDENTITY_connect (cfg, &id_connect_cb, (void *) cfg);
1344 fprintf (stderr, _ ("Cannot connect to identity service\n"));
1348 el = GNUNET_IDENTITY_ego_lookup (cfg, ego_name, &identity_cb, (void *) cfg);
1353 * Command-line option parser function that allows the user to specify
1354 * a complete record as one argument for adding/removing. A pointer
1355 * to the head of the list of record sets must be passed as the "scls"
1358 * @param ctx command line processor context
1359 * @param scls must be of type "struct GNUNET_FS_Uri **"
1360 * @param option name of the option (typically 'R')
1361 * @param value command line argument given; format is
1362 * "TTL TYPE FLAGS VALUE" where TTL is an expiration time (rel or abs),
1363 * always given in seconds (without the unit),
1364 * TYPE is a DNS/GNS record type, FLAGS is either "n" for no flags or
1365 * a combination of 's' (shadow) and 'p' (public) and VALUE is the
1366 * value (in human-readable format)
1367 * @return #GNUNET_OK on success
1370 multirecord_process (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
1375 struct RecordSetEntry **head = scls;
1376 struct RecordSetEntry *r;
1377 struct GNUNET_GNSRECORD_Data record;
1386 cp = GNUNET_strdup (value);
1387 tok = strtok_r (cp, " ", &saveptr);
1390 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1391 _ ("Empty record line argument is not allowed.\n"));
1393 return GNUNET_SYSERR;
1398 GNUNET_asprintf (&etime_in_s, "%s s", tok);
1400 parse_expiration (etime_in_s, &etime_is_rel, &record.expiration_time))
1402 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1403 _ ("Invalid expiration time `%s' (must be without unit)\n"),
1406 GNUNET_free (etime_in_s);
1407 return GNUNET_SYSERR;
1409 GNUNET_free (etime_in_s);
1411 tok = strtok_r (NULL, " ", &saveptr);
1414 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1415 _ ("Missing entries in record line `%s'.\n"),
1418 return GNUNET_SYSERR;
1420 record.record_type = GNUNET_GNSRECORD_typename_to_number (tok);
1421 if (UINT32_MAX == record.record_type)
1423 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _ ("Unknown record type `%s'\n"), tok);
1425 return GNUNET_SYSERR;
1427 tok = strtok_r (NULL, " ", &saveptr);
1430 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1431 _ ("Missing entries in record line `%s'.\n"),
1434 return GNUNET_SYSERR;
1436 record.flags = GNUNET_GNSRECORD_RF_NONE;
1438 record.flags |= GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
1439 if (NULL == strchr (tok, (unsigned char) 'p')) /* p = public */
1440 record.flags |= GNUNET_GNSRECORD_RF_PRIVATE;
1441 if (NULL != strchr (tok, (unsigned char) 's'))
1442 record.flags |= GNUNET_GNSRECORD_RF_SHADOW_RECORD;
1443 /* find beginning of record value */
1444 tok = strchr (&value[tok - cp], (unsigned char) ' ');
1447 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1448 _ ("Missing entries in record line `%s'.\n"),
1451 return GNUNET_SYSERR;
1454 tok++; /* skip space */
1455 if (GNUNET_OK != GNUNET_GNSRECORD_string_to_value (record.record_type,
1460 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1461 _ ("Invalid record data for type %s: `%s'.\n"),
1462 GNUNET_GNSRECORD_number_to_typename (record.record_type),
1464 return GNUNET_SYSERR;
1467 r = GNUNET_malloc (sizeof (struct RecordSetEntry) + record.data_size);
1469 record.data = &r[1];
1470 memcpy (&r[1], raw_data, record.data_size);
1471 GNUNET_free (raw_data);
1479 * Allow user to specify keywords.
1481 * @param shortName short name of the option
1482 * @param name long name of the option
1483 * @param argumentHelp help text for the option argument
1484 * @param description long help text for the option
1485 * @param[out] topKeywords set to the desired value
1487 struct GNUNET_GETOPT_CommandLineOption
1488 multirecord_option (char shortName,
1490 const char *argumentHelp,
1491 const char *description,
1492 struct RecordSetEntry **rs)
1494 struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = shortName,
1496 .argumentHelp = argumentHelp,
1497 .description = description,
1498 .require_argument = 1,
1500 &multirecord_process,
1501 .scls = (void *) rs};
1508 * The main function for gnunet-namestore.
1510 * @param argc number of arguments from the command line
1511 * @param argv command line arguments
1512 * @return 0 ok, 1 on error
1515 main (int argc, char *const *argv)
1517 struct GNUNET_GETOPT_CommandLineOption options[] =
1518 {GNUNET_GETOPT_option_flag ('a', "add", gettext_noop ("add record"), &add),
1519 GNUNET_GETOPT_option_flag ('d',
1521 gettext_noop ("delete record"),
1523 GNUNET_GETOPT_option_flag ('D',
1525 gettext_noop ("display records"),
1527 GNUNET_GETOPT_option_string (
1532 "expiration time for record to use (for adding only), \"never\" is possible"),
1534 GNUNET_GETOPT_option_string ('i',
1538 "set the desired nick name for the zone"),
1540 GNUNET_GETOPT_option_flag ('m',
1543 "monitor changes in the namestore"),
1545 GNUNET_GETOPT_option_string ('n',
1549 "name of the record to add/delete/display"),
1551 GNUNET_GETOPT_option_string ('r',
1555 "determine our name for the given PKEY"),
1557 multirecord_option (
1562 "set record set to values given by (possibly multiple) RECORDLINES; can be specified multiple times"),
1564 GNUNET_GETOPT_option_string ('t',
1568 "type of the record to add/delete/display"),
1570 GNUNET_GETOPT_option_string ('u',
1573 gettext_noop ("URI to import into our zone"),
1575 GNUNET_GETOPT_option_string ('V',
1579 "value of the record to add/delete"),
1581 GNUNET_GETOPT_option_flag ('p',
1583 gettext_noop ("create or list public record"),
1585 GNUNET_GETOPT_option_flag (
1589 "create shadow record (only valid if all other records of the same type have expired"),
1591 GNUNET_GETOPT_option_string ('z',
1595 "name of the ego controlling the zone"),
1597 GNUNET_GETOPT_OPTION_END};
1599 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1604 GNUNET_log_setup ("gnunet-namestore", "WARNING", NULL);
1605 if (GNUNET_OK != GNUNET_PROGRAM_run (argc,
1608 _ ("GNUnet zone manipulation tool"),
1613 GNUNET_free ((void *) argv);
1614 GNUNET_CRYPTO_ecdsa_key_clear (&zone_pkey);
1617 GNUNET_free ((void *) argv);
1618 GNUNET_CRYPTO_ecdsa_key_clear (&zone_pkey);
1622 /* end of gnunet-namestore.c */