2 This file is part of GNUnet.
3 (C) 2012-2013 Christian Grothoff (and other contributing authors)
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.
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.
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @brief binary version of gnunet-gns-import.sh
23 * (for OSes that have no POSIX shell).
27 #include <gnunet_util_lib.h>
28 #include <gnunet_gnsrecord_lib.h>
29 #include <gnunet_identity_service.h>
30 #include <gnunet_namestore_service.h>
33 * Configuration we are using.
35 static const struct GNUNET_CONFIGURATION_Handle *cfg;
38 * Handle to IDENTITY service.
40 static struct GNUNET_IDENTITY_Handle *sh;
43 * Zone iterator for master zone
45 struct GNUNET_NAMESTORE_ZoneIterator *list_it;
48 * Handle to the namestore.
50 static struct GNUNET_NAMESTORE_Handle *ns;
53 * String version of PKEY for master-zone.
55 static char *master_zone_pkey;
58 * Binary version of PKEY for master-zone.
60 static struct GNUNET_CRYPTO_EcdsaPrivateKey master_pk;
63 * String version of PKEY for short-zone.
65 static char *short_zone_pkey;
68 * String version of PKEY for private-zone.
70 static char *private_zone_pkey;
73 * String version of PKEY for pin-zone.
75 static char *pin_zone_pkey = "72QC35CO20UJN1E91KPJFNT9TG4CLKAPB4VK9S3Q758S9MLBRKOG";
78 * Set to GNUNET_YES if private record was found;
80 static int found_private_rec = GNUNET_NO;
83 * Set to GNUNET_YES if short record was found;
85 static int found_short_rec = GNUNET_NO;
88 * Set to GNUNET_YES if pin record was found;
90 static int found_pin_rec = GNUNET_NO;
99 run_process_and_wait (int pipe_control,
100 enum GNUNET_OS_InheritStdioFlags std_inheritance,
101 struct GNUNET_DISK_PipeHandle *pipe_stdin,
102 struct GNUNET_DISK_PipeHandle *pipe_stdout,
103 enum GNUNET_OS_ProcessStatusType *st,
105 const char *filename, ...)
107 static struct GNUNET_OS_Process *p;
112 va_list ap, apc1, apc2;
114 va_start (ap, filename);
118 while (NULL != (arg = va_arg (apc1, char *)))
119 arglen += strlen (arg) + 1;
121 args = argp = GNUNET_malloc (arglen);
122 while (NULL != (arg = va_arg (apc2, char *)))
125 argp += strlen (arg);
132 p = GNUNET_OS_start_process_va (pipe_control, std_inheritance,
141 fprintf (stderr, "Failed to run `%s'\n", args);
146 if (GNUNET_OK != GNUNET_OS_process_wait (p))
149 fprintf (stderr, "Failed to wait for `%s'\n", args);
154 switch (GNUNET_OS_process_status (p, st, code))
160 fprintf (stderr, "`%s' is still running\n", args);
166 fprintf (stderr, "Failed to check the status of `%s'\n", args);
171 if (GNUNET_OS_PROCESS_EXITED != *st || 0 != *code)
174 fprintf (stderr, "`%s' did not end correctly (%d, %d)\n", args, *st, *code);
182 check_pkey (unsigned int rd_len, const struct GNUNET_GNSRECORD_Data *rd,
183 char *pk, int *found_rec)
186 for (i = 0; i < rd_len; i++)
189 if (GNUNET_GNSRECORD_TYPE_PKEY != rd[i].record_type ||
190 rd[i].data_size != sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey))
192 s = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
197 if (0 == strcmp (s, pk))
198 *found_rec = GNUNET_YES;
204 * Process a record that was stored in the namestore.
207 * @param zone_key private key of the zone
208 * @param rname name that is being mapped (at most 255 characters long)
209 * @param rd_len number of entries in @a rd array
210 * @param rd array of records with data to store
213 zone_iterator (void *cls,
214 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
215 const char *rname, unsigned int rd_len,
216 const struct GNUNET_GNSRECORD_Data *rd)
220 if (0 == strcmp (rname, "private"))
221 check_pkey (rd_len, rd, private_zone_pkey, &found_private_rec);
222 else if (0 == strcmp (rname, "short"))
223 check_pkey (rd_len, rd, short_zone_pkey, &found_short_rec);
224 else if (0 == strcmp (rname, "pin"))
225 check_pkey (rd_len, rd, pin_zone_pkey, &found_pin_rec);
227 if (NULL == rname && 0 == rd_len && NULL == rd)
229 enum GNUNET_OS_ProcessStatusType st;
231 if (!found_private_rec)
233 if (0 != run_process_and_wait (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, &st, &code,
235 "gnunet-namestore", "-z", "master-zone", "-a", "-e", "never", "-n", "private", "-p", "-t", "PKEY", "-V", private_zone_pkey, NULL))
241 if (!found_short_rec)
243 if (0 != run_process_and_wait (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, &st, &code,
245 "gnunet-namestore", "-z", "master-zone", "-a", "-e", "never", "-n", "short", "-p", "-t", "PKEY", "-V", short_zone_pkey, NULL))
253 if (0 != run_process_and_wait (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, &st, &code,
255 "gnunet-namestore", "-z", "master-zone", "-a", "-e", "never", "-n", "pin", "-p", "-t", "PKEY", "-V", pin_zone_pkey, NULL))
262 GNUNET_SCHEDULER_shutdown ();
265 GNUNET_NAMESTORE_zone_iterator_next (list_it);
269 * Get master-zone, short-zone and private-zone keys.
271 * This function is initially called for all egos and then again
272 * whenever a ego's identifier changes or if it is deleted. At the
273 * end of the initial pass over all egos, the function is once called
274 * with 'NULL' for 'ego'. That does NOT mean that the callback won't
275 * be invoked in the future or that there was an error.
277 * When used with 'GNUNET_IDENTITY_create' or 'GNUNET_IDENTITY_get',
278 * this function is only called ONCE, and 'NULL' being passed in
279 * 'ego' does indicate an error (i.e. name is taken or no default
280 * value is known). If 'ego' is non-NULL and if '*ctx'
281 * is set in those callbacks, the value WILL be passed to a subsequent
282 * call to the identity callback of 'GNUNET_IDENTITY_connect' (if
283 * that one was not NULL).
285 * When an identity is renamed, this function is called with the
286 * (known) ego but the NEW identifier.
288 * When an identity is deleted, this function is called with the
289 * (known) ego and "NULL" for the 'identifier'. In this case,
290 * the 'ego' is henceforth invalid (and the 'ctx' should also be
294 * @param ego ego handle
295 * @param ctx context for application to store data for this ego
296 * (during the lifetime of this process, initially NULL)
297 * @param identifier identifier assigned by the user for this ego,
298 * NULL if the user just deleted the ego and it
299 * must thus no longer be used
303 struct GNUNET_IDENTITY_Ego *ego,
305 const char *identifier)
307 static struct GNUNET_CRYPTO_EcdsaPublicKey pk;
310 if (NULL == master_zone_pkey ||
311 NULL == short_zone_pkey ||
312 NULL == private_zone_pkey)
315 GNUNET_SCHEDULER_shutdown ();
318 list_it = GNUNET_NAMESTORE_zone_iteration_start (ns,
319 &master_pk, &zone_iterator, NULL);
323 GNUNET_SCHEDULER_shutdown ();
327 GNUNET_IDENTITY_ego_get_public_key (ego, &pk);
328 if (NULL != identifier)
330 if (NULL == master_zone_pkey && 0 == strcmp ("master-zone", identifier))
332 master_zone_pkey = GNUNET_CRYPTO_ecdsa_public_key_to_string (&pk);
333 master_pk = *GNUNET_IDENTITY_ego_get_private_key (ego);
335 else if (NULL == short_zone_pkey && 0 == strcmp ("short-zone", identifier))
336 short_zone_pkey = GNUNET_CRYPTO_ecdsa_public_key_to_string (&pk);
337 else if (NULL == private_zone_pkey && 0 == strcmp ("private-zone", identifier))
338 private_zone_pkey = GNUNET_CRYPTO_ecdsa_public_key_to_string (&pk);
343 * Task run on shutdown.
349 shutdown_task (void *cls,
350 const struct GNUNET_SCHEDULER_TaskContext *tc)
352 GNUNET_free_non_null (master_zone_pkey);
353 master_zone_pkey = NULL;
354 GNUNET_free_non_null (short_zone_pkey);
355 short_zone_pkey = NULL;
356 GNUNET_free_non_null (private_zone_pkey);
357 private_zone_pkey = NULL;
360 GNUNET_NAMESTORE_zone_iteration_stop (list_it);
365 GNUNET_NAMESTORE_disconnect (ns);
370 GNUNET_IDENTITY_disconnect (sh);
376 * Main function that will be run.
379 * @param args remaining command-line arguments
380 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
381 * @param c configuration
384 run (void *cls, char *const *args, const char *cfgfile,
385 const struct GNUNET_CONFIGURATION_Handle *c)
387 enum GNUNET_OS_ProcessStatusType st;
392 if (0 != run_process_and_wait (GNUNET_NO, 0, NULL, NULL, &st, &code,
394 "gnunet-arm", "-I", NULL))
397 fprintf (stderr, "GNUnet is not running, please start GNUnet before running import\n");
401 if (0 != run_process_and_wait (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, &st, &code,
403 "gnunet-identity", "-C", "master-zone", NULL))
406 if (0 != run_process_and_wait (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, &st, &code,
408 "gnunet-identity", "-C", "short-zone", NULL))
411 if (0 != run_process_and_wait (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, &st, &code,
413 "gnunet-identity", "-C", "private-zone", NULL))
416 if (0 != run_process_and_wait (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, &st, &code,
418 "gnunet-identity", "-C", "sks-zone", NULL))
421 if (0 != run_process_and_wait (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, &st, &code,
423 "gnunet-identity", "-e", "short-zone", "-s", "gns-short", NULL))
426 if (0 != run_process_and_wait (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, &st, &code,
428 "gnunet-identity", "-e", "master-zone", "-s", "gns-master", NULL))
431 if (0 != run_process_and_wait (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, &st, &code,
433 "gnunet-identity", "-e", "master-zone", "-s", "namestore", NULL))
436 if (0 != run_process_and_wait (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, &st, &code,
438 "gnunet-identity", "-e", "short-zone", "-s", "gns-proxy", NULL))
441 if (0 != run_process_and_wait (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, &st, &code,
443 "gnunet-identity", "-e", "private-zone", "-s", "gns-private", NULL))
446 if (0 != run_process_and_wait (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, &st, &code,
448 "gnunet-identity", "-e", "sks-zone", "-s", "fs-sks", NULL))
451 ns = GNUNET_NAMESTORE_connect (cfg);
452 sh = GNUNET_IDENTITY_connect (cfg, &get_ego, NULL);
453 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
454 &shutdown_task, NULL);
459 * The main function for gnunet-gns.
461 * @param argc number of arguments from the command line
462 * @param argv command line arguments
463 * @return 0 ok, 1 on error
466 main (int argc, char *const *argv)
468 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
469 GNUNET_GETOPT_OPTION_END
473 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
476 GNUNET_log_setup ("gnunet-gns-import", "WARNING", NULL);
478 r = GNUNET_PROGRAM_run (argc, argv, "gnunet-gns-import",
479 _("This program will import some GNS authorities into your GNS namestore."),
482 GNUNET_free ((void*) argv);
483 return GNUNET_OK == r ? ret : 1;
486 /* end of gnunet-gns-import.c */