2 This file is part of GNUnet.
3 Copyright (C) 2012-2013 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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 private-zone.
65 static char *private_zone_pkey;
68 * String version of PKEY for pin-zone.
70 static char *pin_zone_pkey = "72QC35CO20UJN1E91KPJFNT9TG4CLKAPB4VK9S3Q758S9MLBRKOG";
73 * Set to GNUNET_YES if private record was found;
75 static int found_private_rec = GNUNET_NO;
78 * Set to GNUNET_YES if pin record was found;
80 static int found_pin_rec = GNUNET_NO;
89 run_process_and_wait (int pipe_control,
90 enum GNUNET_OS_InheritStdioFlags std_inheritance,
91 struct GNUNET_DISK_PipeHandle *pipe_stdin,
92 struct GNUNET_DISK_PipeHandle *pipe_stdout,
93 enum GNUNET_OS_ProcessStatusType *st,
95 const char *filename, ...)
97 static struct GNUNET_OS_Process *p;
102 va_list ap, apc1, apc2;
104 va_start (ap, filename);
108 while (NULL != (arg = va_arg (apc1, char *)))
109 arglen += strlen (arg) + 1;
111 args = argp = GNUNET_malloc (arglen);
112 while (NULL != (arg = va_arg (apc2, char *)))
115 argp += strlen (arg);
122 p = GNUNET_OS_start_process_va (pipe_control, std_inheritance,
131 fprintf (stderr, "Failed to run `%s'\n", args);
136 if (GNUNET_OK != GNUNET_OS_process_wait (p))
139 fprintf (stderr, "Failed to wait for `%s'\n", args);
144 switch (GNUNET_OS_process_status (p, st, code))
150 fprintf (stderr, "`%s' is still running\n", args);
156 fprintf (stderr, "Failed to check the status of `%s'\n", args);
161 if (GNUNET_OS_PROCESS_EXITED != *st || 0 != *code)
164 fprintf (stderr, "`%s' did not end correctly (%d, %d)\n", args, *st, *code);
172 check_pkey (unsigned int rd_len, const struct GNUNET_GNSRECORD_Data *rd,
173 char *pk, int *found_rec)
176 for (i = 0; i < rd_len; i++)
179 if (GNUNET_GNSRECORD_TYPE_PKEY != rd[i].record_type ||
180 rd[i].data_size != sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey))
182 s = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
187 if (0 == strcmp (s, pk))
188 *found_rec = GNUNET_YES;
194 * Process a record that was stored in the namestore.
197 * @param zone_key private key of the zone
198 * @param rname name that is being mapped (at most 255 characters long)
199 * @param rd_len number of entries in @a rd array
200 * @param rd array of records with data to store
203 zone_iterator (void *cls,
204 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
205 const char *rname, unsigned int rd_len,
206 const struct GNUNET_GNSRECORD_Data *rd)
210 if (0 == strcmp (rname, "private"))
211 check_pkey (rd_len, rd, private_zone_pkey, &found_private_rec);
212 else if (0 == strcmp (rname, "pin"))
213 check_pkey (rd_len, rd, pin_zone_pkey, &found_pin_rec);
215 GNUNET_NAMESTORE_zone_iterator_next (list_it);
219 zone_iteration_error (void *cls)
221 enum GNUNET_OS_ProcessStatusType st;
223 if (!found_private_rec)
225 if (0 != run_process_and_wait (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, &st, &code,
227 "gnunet-namestore", "-z", "master-zone", "-a", "-e", "never", "-n", "private", "-p", "-t", "PKEY", "-V", private_zone_pkey, NULL))
235 if (0 != run_process_and_wait (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, &st, &code,
237 "gnunet-namestore", "-z", "master-zone", "-a", "-e", "never", "-n", "pin", "-p", "-t", "PKEY", "-V", pin_zone_pkey, NULL))
244 GNUNET_SCHEDULER_shutdown ();
249 zone_iteration_finished (void *cls)
255 * Get master-zone and private-zone keys.
257 * This function is initially called for all egos and then again
258 * whenever a ego's identifier changes or if it is deleted. At the
259 * end of the initial pass over all egos, the function is once called
260 * with 'NULL' for 'ego'. That does NOT mean that the callback won't
261 * be invoked in the future or that there was an error.
263 * When used with 'GNUNET_IDENTITY_create' or 'GNUNET_IDENTITY_get',
264 * this function is only called ONCE, and 'NULL' being passed in
265 * 'ego' does indicate an error (i.e. name is taken or no default
266 * value is known). If 'ego' is non-NULL and if '*ctx'
267 * is set in those callbacks, the value WILL be passed to a subsequent
268 * call to the identity callback of 'GNUNET_IDENTITY_connect' (if
269 * that one was not NULL).
271 * When an identity is renamed, this function is called with the
272 * (known) ego but the NEW identifier.
274 * When an identity is deleted, this function is called with the
275 * (known) ego and "NULL" for the 'identifier'. In this case,
276 * the 'ego' is henceforth invalid (and the 'ctx' should also be
280 * @param ego ego handle
281 * @param ctx context for application to store data for this ego
282 * (during the lifetime of this process, initially NULL)
283 * @param identifier identifier assigned by the user for this ego,
284 * NULL if the user just deleted the ego and it
285 * must thus no longer be used
289 struct GNUNET_IDENTITY_Ego *ego,
291 const char *identifier)
293 static struct GNUNET_CRYPTO_EcdsaPublicKey pk;
296 if (NULL == master_zone_pkey ||
297 NULL == private_zone_pkey)
300 GNUNET_SCHEDULER_shutdown ();
303 list_it = GNUNET_NAMESTORE_zone_iteration_start (ns,
304 &master_pk, &zone_iteration_error, NULL, &zone_iterator, NULL, &zone_iteration_finished, NULL);
308 GNUNET_SCHEDULER_shutdown ();
312 GNUNET_IDENTITY_ego_get_public_key (ego, &pk);
313 if (NULL != identifier)
315 if (NULL == master_zone_pkey && 0 == strcmp ("master-zone", identifier))
317 master_zone_pkey = GNUNET_CRYPTO_ecdsa_public_key_to_string (&pk);
318 master_pk = *GNUNET_IDENTITY_ego_get_private_key (ego);
320 else if (NULL == private_zone_pkey && 0 == strcmp ("private-zone", identifier))
321 private_zone_pkey = GNUNET_CRYPTO_ecdsa_public_key_to_string (&pk);
326 * Task run on shutdown.
331 shutdown_task (void *cls)
333 GNUNET_free_non_null (master_zone_pkey);
334 master_zone_pkey = NULL;
335 GNUNET_free_non_null (private_zone_pkey);
336 private_zone_pkey = NULL;
339 GNUNET_NAMESTORE_zone_iteration_stop (list_it);
344 GNUNET_NAMESTORE_disconnect (ns);
349 GNUNET_IDENTITY_disconnect (sh);
355 * Main function that will be run.
358 * @param args remaining command-line arguments
359 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
360 * @param c configuration
363 run (void *cls, char *const *args, const char *cfgfile,
364 const struct GNUNET_CONFIGURATION_Handle *c)
366 enum GNUNET_OS_ProcessStatusType st;
371 if (0 != run_process_and_wait (GNUNET_NO, 0, NULL, NULL, &st, &code,
373 "gnunet-arm", "-I", NULL))
376 fprintf (stderr, "GNUnet is not running, please start GNUnet before running import\n");
380 if (0 != run_process_and_wait (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, &st, &code,
382 "gnunet-identity", "-C", "master-zone", NULL))
385 if (0 != run_process_and_wait (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, &st, &code,
387 "gnunet-identity", "-C", "private-zone", NULL))
390 if (0 != run_process_and_wait (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, &st, &code,
392 "gnunet-identity", "-C", "sks-zone", NULL))
395 if (0 != run_process_and_wait (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, &st, &code,
397 "gnunet-identity", "-e", "master-zone", "-s", "gns-master", NULL))
400 if (0 != run_process_and_wait (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, &st, &code,
402 "gnunet-identity", "-e", "master-zone", "-s", "namestore", NULL))
405 if (0 != run_process_and_wait (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, &st, &code,
407 "gnunet-identity", "-e", "master-zone", "-s", "gns-proxy", NULL))
410 if (0 != run_process_and_wait (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, &st, &code,
412 "gnunet-identity", "-e", "master-zone", "-s", "gns-intercept", NULL))
415 if (0 != run_process_and_wait (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, &st, &code,
417 "gnunet-identity", "-e", "private-zone", "-s", "gns-private", NULL))
420 if (0 != run_process_and_wait (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, &st, &code,
422 "gnunet-identity", "-e", "sks-zone", "-s", "fs-sks", NULL))
425 ns = GNUNET_NAMESTORE_connect (cfg);
426 sh = GNUNET_IDENTITY_connect (cfg, &get_ego, NULL);
427 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
432 * The main function for gnunet-gns.
434 * @param argc number of arguments from the command line
435 * @param argv command line arguments
436 * @return 0 ok, 1 on error
439 main (int argc, char *const *argv)
441 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
442 GNUNET_GETOPT_OPTION_END
446 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
449 GNUNET_log_setup ("gnunet-gns-import", "WARNING", NULL);
451 r = GNUNET_PROGRAM_run (argc, argv, "gnunet-gns-import",
452 _("This program will import some GNS authorities into your GNS namestore."),
455 GNUNET_free ((void*) argv);
456 return GNUNET_OK == r ? ret : 1;
459 /* end of gnunet-gns-import.c */