GNUnet  0.10.x
gnunet-gns-import.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2012-2013 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  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/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19  */
26 #include "platform.h"
27 #include <gnunet_util_lib.h>
28 #include <gnunet_gnsrecord_lib.h>
31 
35 static const struct GNUNET_CONFIGURATION_Handle *cfg;
36 
40 static struct GNUNET_IDENTITY_Handle *sh;
41 
46 
50 static struct GNUNET_NAMESTORE_Handle *ns;
51 
55 static char *master_zone_pkey;
56 
61 
65 static char *private_zone_pkey;
66 
70 static char *pin_zone_pkey = "72QC35CO20UJN1E91KPJFNT9TG4CLKAPB4VK9S3Q758S9MLBRKOG";
71 
76 
80 static int found_pin_rec = GNUNET_NO;
81 
85 static int ret;
86 
87 
88 static int
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,
94  unsigned long *code,
95  const char *filename, ...)
96 {
97  static struct GNUNET_OS_Process *p;
98  int arglen;
99  char *arg;
100  char *args;
101  char *argp;
102  va_list ap, apc1, apc2;
103 
104  va_start(ap, filename);
105  va_copy(apc1, ap);
106  va_copy(apc2, ap);
107  arglen = 0;
108  while (NULL != (arg = va_arg(apc1, char *)))
109  arglen += strlen(arg) + 1;
110  va_end(apc1);
111  args = argp = GNUNET_malloc(arglen);
112  while (NULL != (arg = va_arg(apc2, char *)))
113  {
114  strcpy(argp, arg);
115  argp += strlen(arg);
116  *argp = ' ';
117  argp += 1;
118  }
119  va_end(apc2);
120  if (arglen > 0)
121  argp[-1] = '\0';
122  p = GNUNET_OS_start_process_va(pipe_control, std_inheritance,
123  pipe_stdin,
124  pipe_stdout,
125  NULL,
126  filename, ap);
127  va_end(ap);
128  if (NULL == p)
129  {
130  ret = 3;
131  fprintf(stderr, "Failed to run `%s'\n", args);
132  GNUNET_free(args);
133  return 1;
134  }
135 
137  {
138  ret = 4;
139  fprintf(stderr, "Failed to wait for `%s'\n", args);
140  GNUNET_free(args);
141  return 1;
142  }
143 
144  switch (GNUNET_OS_process_status(p, st, code))
145  {
146  case GNUNET_OK:
147  break;
148 
149  case GNUNET_NO:
150  ret = 5;
151  fprintf(stderr, "`%s' is still running\n", args);
152  GNUNET_free(args);
153  return 1;
154 
155  default:
156  case GNUNET_SYSERR:
157  ret = 6;
158  fprintf(stderr, "Failed to check the status of `%s'\n", args);
159  GNUNET_free(args);
160  return 1;
161  }
162  return 0;
163 }
164 
165 static void
166 check_pkey(unsigned int rd_len, const struct GNUNET_GNSRECORD_Data *rd,
167  char *pk, int *found_rec)
168 {
169  int i;
170 
171  for (i = 0; i < rd_len; i++)
172  {
173  char *s;
174  if (GNUNET_GNSRECORD_TYPE_PKEY != rd[i].record_type ||
175  rd[i].data_size != sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey))
176  continue;
177  s = GNUNET_GNSRECORD_value_to_string(rd[i].record_type,
178  rd[i].data,
179  rd[i].data_size);
180  if (NULL == s)
181  continue;
182  if (0 == strcmp(s, pk))
183  *found_rec = GNUNET_YES;
184  GNUNET_free(s);
185  }
186 }
187 
197 static void
198 zone_iterator(void *cls,
199  const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
200  const char *rname, unsigned int rd_len,
201  const struct GNUNET_GNSRECORD_Data *rd)
202 {
203  if (NULL != rname)
204  {
205  if (0 == strcmp(rname, "private"))
207  else if (0 == strcmp(rname, "pin"))
208  check_pkey(rd_len, rd, pin_zone_pkey, &found_pin_rec);
209  }
211 }
212 
213 static void
215 {
217  unsigned long code;
218 
219  if (!found_private_rec)
220  {
221  if (0 != run_process_and_wait(GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, &st, &code,
222  "gnunet-namestore",
223  "gnunet-namestore", "-z", "master-zone", "-a", "-e", "never", "-n", "private", "-p", "-t", "PKEY", "-V", private_zone_pkey, NULL))
224  {
225  ret = 8;
226  return;
227  }
228  }
229  if (!found_pin_rec)
230  {
231  if (0 != run_process_and_wait(GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, &st, &code,
232  "gnunet-namestore",
233  "gnunet-namestore", "-z", "master-zone", "-a", "-e", "never", "-n", "pin", "-p", "-t", "PKEY", "-V", pin_zone_pkey, NULL))
234  {
235  ret = 10;
236  return;
237  }
238  }
239  list_it = NULL;
241 }
242 
243 
244 static void
246 {
247 }
248 
249 
283 static void
284 get_ego(void *cls,
285  struct GNUNET_IDENTITY_Ego *ego,
286  void **ctx,
287  const char *identifier)
288 {
289  static struct GNUNET_CRYPTO_EcdsaPublicKey pk;
290 
291  if (NULL == ego)
292  {
293  if (NULL == master_zone_pkey ||
294  NULL == private_zone_pkey)
295  {
296  ret = 11;
298  return;
299  }
302  if (NULL == list_it)
303  {
304  ret = 12;
306  }
307  return;
308  }
310  if (NULL != identifier)
311  {
312  if (NULL == master_zone_pkey && 0 == strcmp("master-zone", identifier))
313  {
316  }
317  else if (NULL == private_zone_pkey && 0 == strcmp("private-zone", identifier))
319  }
320 }
321 
327 static void
328 shutdown_task(void *cls)
329 {
331  master_zone_pkey = NULL;
333  private_zone_pkey = NULL;
334  if (NULL != list_it)
335  {
337  list_it = NULL;
338  }
339  if (NULL != ns)
340  {
342  ns = NULL;
343  }
344  if (NULL != sh)
345  {
347  sh = NULL;
348  }
349 }
350 
359 static void
360 run(void *cls, char *const *args, const char *cfgfile,
361  const struct GNUNET_CONFIGURATION_Handle *c)
362 {
364  unsigned long code;
365 
366  cfg = c;
367 
368  if (0 != run_process_and_wait(GNUNET_NO, 0, NULL, NULL, &st, &code,
369  "gnunet-arm",
370  "gnunet-arm", "-I", NULL))
371  {
372  if (7 == ret)
373  fprintf(stderr, "GNUnet is not running, please start GNUnet before running import\n");
374  return;
375  }
376 
377  if (0 != run_process_and_wait(GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, &st, &code,
378  "gnunet-identity",
379  "gnunet-identity", "-C", "master-zone", NULL))
380  return;
381 
382  if (0 != run_process_and_wait(GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, &st, &code,
383  "gnunet-identity",
384  "gnunet-identity", "-C", "private-zone", NULL))
385  return;
386 
387  if (0 != run_process_and_wait(GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, &st, &code,
388  "gnunet-identity",
389  "gnunet-identity", "-C", "sks-zone", NULL))
390  return;
391 
392  if (0 != run_process_and_wait(GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, &st, &code,
393  "gnunet-identity",
394  "gnunet-identity", "-e", "master-zone", "-s", "gns-master", NULL))
395  return;
396 
397  if (0 != run_process_and_wait(GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, &st, &code,
398  "gnunet-identity",
399  "gnunet-identity", "-e", "master-zone", "-s", "namestore", NULL))
400  return;
401 
402  if (0 != run_process_and_wait(GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, &st, &code,
403  "gnunet-identity",
404  "gnunet-identity", "-e", "master-zone", "-s", "gns-proxy", NULL))
405  return;
406 
407  if (0 != run_process_and_wait(GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, &st, &code,
408  "gnunet-identity",
409  "gnunet-identity", "-e", "master-zone", "-s", "gns-intercept", NULL))
410  return;
411 
412  if (0 != run_process_and_wait(GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, &st, &code,
413  "gnunet-identity",
414  "gnunet-identity", "-e", "private-zone", "-s", "gns-private", NULL))
415  return;
416 
417  if (0 != run_process_and_wait(GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, &st, &code,
418  "gnunet-identity",
419  "gnunet-identity", "-e", "sks-zone", "-s", "fs-sks", NULL))
420  return;
421 
422  ns = GNUNET_NAMESTORE_connect(cfg);
423  sh = GNUNET_IDENTITY_connect(cfg, &get_ego, NULL);
425 }
426 
427 
435 int
436 main(int argc, char *const *argv)
437 {
438  static const struct GNUNET_GETOPT_CommandLineOption options[] = {
440  };
441  int r;
442 
443  if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args(argc, argv, &argc, &argv))
444  return 2;
445 
446  GNUNET_log_setup("gnunet-gns-import", "WARNING", NULL);
447  ret = 0;
448  r = GNUNET_PROGRAM_run(argc, argv, "gnunet-gns-import",
449  _("This program will import some GNS authorities into your GNS namestore."),
450  options,
451  &run, NULL);
452  GNUNET_free((void*)argv);
453  return GNUNET_OK == r ? ret : 1;
454 }
455 
456 /* end of gnunet-gns-import.c */
struct GNUNET_IDENTITY_Handle * GNUNET_IDENTITY_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, GNUNET_IDENTITY_Callback cb, void *cb_cls)
Connect to the identity service.
Definition: identity_api.c:525
GNUNET_OS_InheritStdioFlags
Flags that determine which of the standard streams should be inherited by the child process...
Definition: gnunet_os_lib.h:68
static struct GNUNET_CRYPTO_EddsaPrivateKey * pk
Private key of this peer.
static char * pin_zone_pkey
String version of PKEY for pin-zone.
static char * private_zone_pkey
String version of PKEY for private-zone.
int main(int argc, char *const *argv)
The main function for gnunet-gns.
int GNUNET_OS_process_status(struct GNUNET_OS_Process *proc, enum GNUNET_OS_ProcessStatusType *type, unsigned long *code)
Retrieve the status of a process, waiting on it if dead.
Definition: os_priority.c:963
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_shutdown(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run on shutdown, that is when a CTRL-C signal is received, or when GNUNET_SCHEDULER_shutdown() is being invoked.
Definition: scheduler.c:1284
struct GNUNET_NAMESTORE_ZoneIterator * GNUNET_NAMESTORE_zone_iteration_start(struct GNUNET_NAMESTORE_Handle *h, const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, GNUNET_SCHEDULER_TaskCallback error_cb, void *error_cb_cls, GNUNET_NAMESTORE_RecordMonitor proc, void *proc_cls, GNUNET_SCHEDULER_TaskCallback finish_cb, void *finish_cb_cls)
Starts a new zone iteration (used to periodically PUT all of our records into our DHT)...
struct GNUNET_NAMESTORE_ZoneIterator * list_it
Zone iterator for master zone.
int GNUNET_STRINGS_get_utf8_args(int argc, char *const *argv, int *u8argc, char *const **u8argv)
Returns utf-8 encoded arguments.
Definition: strings.c:1439
const struct GNUNET_CRYPTO_EcdsaPrivateKey * GNUNET_IDENTITY_ego_get_private_key(const struct GNUNET_IDENTITY_Ego *ego)
Obtain the ECC key associated with a ego.
Definition: identity_api.c:553
static struct GNUNET_CRYPTO_EcdsaPrivateKey master_pk
Binary version of PKEY for master-zone.
#define GNUNET_NO
Definition: gnunet_common.h:78
static int run_process_and_wait(int pipe_control, enum GNUNET_OS_InheritStdioFlags std_inheritance, struct GNUNET_DISK_PipeHandle *pipe_stdin, struct GNUNET_DISK_PipeHandle *pipe_stdout, enum GNUNET_OS_ProcessStatusType *st, unsigned long *code, const char *filename,...)
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_free_non_null(ptr)
Free the memory pointed to by ptr if ptr is not NULL.
Definition of a command line option.
void GNUNET_NAMESTORE_zone_iterator_next(struct GNUNET_NAMESTORE_ZoneIterator *it, uint64_t limit)
Calls the record processor specified in GNUNET_NAMESTORE_zone_iteration_start for the next record...
Private ECC key encoded for transmission.
struct GNUNET_OS_Process * GNUNET_OS_start_process_va(int pipe_control, enum GNUNET_OS_InheritStdioFlags std_inheritance, struct GNUNET_DISK_PipeHandle *pipe_stdin, struct GNUNET_DISK_PipeHandle *pipe_stdout, struct GNUNET_DISK_PipeHandle *pipe_stderr, const char *filename, va_list va)
Start a process.
Definition: os_priority.c:639
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:517
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
static struct GNUNET_DNSSTUB_Context * ctx
Context for DNS resolution.
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
static void zone_iteration_finished(void *cls)
void GNUNET_NAMESTORE_disconnect(struct GNUNET_NAMESTORE_Handle *h)
Disconnect from the namestore service (and free associated resources).
Handle for a zone iterator operation.
Handle for an ego.
Definition: identity.h:237
static struct GNUNET_OS_Process * p
Helper process we started.
Definition: gnunet-qr.c:59
#define GNUNET_GNSRECORD_TYPE_PKEY
Record type for GNS zone transfer ("PKEY").
Connection to the NAMESTORE service.
Handle for the service.
Definition: identity_api.c:94
GNUNET_OS_ProcessStatusType
Process status types.
static void run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
Main function that will be run.
static char * filename
void GNUNET_IDENTITY_disconnect(struct GNUNET_IDENTITY_Handle *h)
Disconnect from identity service.
Definition: identity_api.c:835
static int found_pin_rec
Set to GNUNET_YES if pin record was found;.
void GNUNET_NAMESTORE_zone_iteration_stop(struct GNUNET_NAMESTORE_ZoneIterator *it)
Stops iteration and releases the namestore handle for further calls.
When these flags are set, the child process will inherit stdout and stderr of the parent...
Definition: gnunet_os_lib.h:96
char * GNUNET_CRYPTO_ecdsa_public_key_to_string(const struct GNUNET_CRYPTO_EcdsaPublicKey *pub)
Convert a public key to a string.
Definition: crypto_ecc.c:334
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
struct GNUNET_NAMESTORE_Handle * GNUNET_NAMESTORE_connect(const struct GNUNET_CONFIGURATION_Handle *cfg)
Connect to the namestore service.
static int found_private_rec
Set to GNUNET_YES if private record was found;.
static int ret
Exit code.
static struct GNUNET_NAMESTORE_Handle * ns
Handle to the namestore.
void GNUNET_IDENTITY_ego_get_public_key(const struct GNUNET_IDENTITY_Ego *ego, struct GNUNET_CRYPTO_EcdsaPublicKey *pk)
Get the identifier (public key) of an ego.
Definition: identity_api.c:566
static void get_ego(void *cls, struct GNUNET_IDENTITY_Ego *ego, void **ctx, const char *identifier)
Get master-zone and private-zone keys.
static char * master_zone_pkey
String version of PKEY for master-zone.
static void shutdown_task(void *cls)
Task run on shutdown.
int GNUNET_OS_process_wait(struct GNUNET_OS_Process *proc)
Wait for a process to terminate.
Definition: os_priority.c:1000
configuration data
Definition: configuration.c:83
static void check_pkey(unsigned int rd_len, const struct GNUNET_GNSRECORD_Data *rd, char *pk, int *found_rec)
static void zone_iterator(void *cls, const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key, const char *rname, unsigned int rd_len, const struct GNUNET_GNSRECORD_Data *rd)
Process a record that was stored in the namestore.
Public ECC key (always for Curve25519) encoded in a format suitable for network transmission and ECDS...
int GNUNET_PROGRAM_run(int argc, char *const *argv, const char *binaryName, const char *binaryHelp, const struct GNUNET_GETOPT_CommandLineOption *options, GNUNET_PROGRAM_Main task, void *task_cls)
Run a standard GNUnet command startup sequence (initialize loggers and configuration, parse options).
Definition: program.c:367
#define GNUNET_YES
Definition: gnunet_common.h:77
static struct GNUNET_IDENTITY_Handle * sh
Handle to IDENTITY service.
int GNUNET_log_setup(const char *comp, const char *loglevel, const char *logfile)
Setup logging.
static struct GNUNET_SCHEDULER_Task * st
The shutdown task.
uint32_t data
The data value.
char * GNUNET_GNSRECORD_value_to_string(uint32_t type, const void *data, size_t data_size)
Convert the &#39;value&#39; of a record to a string.
Definition: gnsrecord.c:142
static size_t data_size
Number of bytes in data.
Handle used to manage a pipe.
Definition: disk.c:68
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
static void zone_iteration_error(void *cls)