remove 'illegal' (non-reentrant) log logic from signal handler
[oweals/gnunet.git] / src / gns / gnunet-gns-import.c
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  */
20 /**
21  * @file gnunet-gns.c
22  * @brief binary version of gnunet-gns-import.sh
23  *        (for OSes that have no POSIX shell).
24  * @author LRN
25  */
26 #include "platform.h"
27 #include <gnunet_util_lib.h>
28 #include <gnunet_gnsrecord_lib.h>
29 #include <gnunet_identity_service.h>
30 #include <gnunet_namestore_service.h>
31
32 /**
33  * Configuration we are using.
34  */
35 static const struct GNUNET_CONFIGURATION_Handle *cfg;
36
37 /**
38  * Handle to IDENTITY service.
39  */
40 static struct GNUNET_IDENTITY_Handle *sh;
41
42 /**
43  * Zone iterator for master zone
44  */
45 struct GNUNET_NAMESTORE_ZoneIterator *list_it;
46
47 /**
48  * Handle to the namestore.
49  */
50 static struct GNUNET_NAMESTORE_Handle *ns;
51
52 /**
53  * String version of PKEY for master-zone.
54  */
55 static char *master_zone_pkey;
56
57 /**
58  * Binary version of PKEY for master-zone.
59  */
60 static struct GNUNET_CRYPTO_EcdsaPrivateKey master_pk;
61
62 /**
63  * String version of PKEY for private-zone.
64  */
65 static char *private_zone_pkey;
66
67 /**
68  * String version of PKEY for pin-zone.
69  */
70 static char *pin_zone_pkey =
71   "72QC35CO20UJN1E91KPJFNT9TG4CLKAPB4VK9S3Q758S9MLBRKOG";
72
73 /**
74  * Set to GNUNET_YES if private record was found;
75  */
76 static int found_private_rec = GNUNET_NO;
77
78 /**
79  * Set to GNUNET_YES if pin record was found;
80  */
81 static int found_pin_rec = GNUNET_NO;
82
83 /**
84  * Exit code.
85  */
86 static int ret;
87
88
89 static int
90 run_process_and_wait (int pipe_control,
91                       enum GNUNET_OS_InheritStdioFlags std_inheritance,
92                       struct GNUNET_DISK_PipeHandle *pipe_stdin,
93                       struct GNUNET_DISK_PipeHandle *pipe_stdout,
94                       enum GNUNET_OS_ProcessStatusType *st,
95                       unsigned long *code,
96                       const char *filename, ...)
97 {
98   static struct GNUNET_OS_Process *p;
99   int arglen;
100   char *arg;
101   char *args;
102   char *argp;
103   va_list ap, apc1, apc2;
104
105   va_start (ap, filename);
106   va_copy (apc1, ap);
107   va_copy (apc2, ap);
108   arglen = 0;
109   while (NULL != (arg = va_arg (apc1, char *)))
110     arglen += strlen (arg) + 1;
111   va_end (apc1);
112   args = argp = GNUNET_malloc (arglen);
113   while (NULL != (arg = va_arg (apc2, char *)))
114   {
115     strcpy (argp, arg);
116     argp += strlen (arg);
117     *argp = ' ';
118     argp += 1;
119   }
120   va_end (apc2);
121   if (arglen > 0)
122     argp[-1] = '\0';
123   p = GNUNET_OS_start_process_va (pipe_control, std_inheritance,
124                                   pipe_stdin,
125                                   pipe_stdout,
126                                   NULL,
127                                   filename, ap);
128   va_end (ap);
129   if (NULL == p)
130   {
131     ret = 3;
132     fprintf (stderr, "Failed to run `%s'\n", args);
133     GNUNET_free (args);
134     return 1;
135   }
136
137   if (GNUNET_OK != GNUNET_OS_process_wait (p))
138   {
139     ret = 4;
140     fprintf (stderr, "Failed to wait for `%s'\n", args);
141     GNUNET_free (args);
142     return 1;
143   }
144
145   switch (GNUNET_OS_process_status (p, st, code))
146   {
147   case GNUNET_OK:
148     break;
149
150   case GNUNET_NO:
151     ret = 5;
152     fprintf (stderr, "`%s' is still running\n", args);
153     GNUNET_free (args);
154     return 1;
155
156   default:
157   case GNUNET_SYSERR:
158     ret = 6;
159     fprintf (stderr, "Failed to check the status of `%s'\n", args);
160     GNUNET_free (args);
161     return 1;
162   }
163   return 0;
164 }
165
166
167 static void
168 check_pkey (unsigned int rd_len, const struct GNUNET_GNSRECORD_Data *rd,
169             char *pk, int *found_rec)
170 {
171   int i;
172
173   for (i = 0; i < rd_len; i++)
174   {
175     char *s;
176     if ((GNUNET_GNSRECORD_TYPE_PKEY != rd[i].record_type) ||
177         (rd[i].data_size != sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey)) )
178       continue;
179     s = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
180                                           rd[i].data,
181                                           rd[i].data_size);
182     if (NULL == s)
183       continue;
184     if (0 == strcmp (s, pk))
185       *found_rec = GNUNET_YES;
186     GNUNET_free (s);
187   }
188 }
189
190
191 /**
192  * Process a record that was stored in the namestore.
193  *
194  * @param cls closure
195  * @param zone_key private key of the zone
196  * @param rname name that is being mapped (at most 255 characters long)
197  * @param rd_len number of entries in @a rd array
198  * @param rd array of records with data to store
199  */
200 static void
201 zone_iterator (void *cls,
202                const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
203                const char *rname, unsigned int rd_len,
204                const struct GNUNET_GNSRECORD_Data *rd)
205 {
206   if (NULL != rname)
207   {
208     if (0 == strcmp (rname, "private"))
209       check_pkey (rd_len, rd, private_zone_pkey, &found_private_rec);
210     else if (0 == strcmp (rname, "pin"))
211       check_pkey (rd_len, rd, pin_zone_pkey, &found_pin_rec);
212   }
213   GNUNET_NAMESTORE_zone_iterator_next (list_it);
214 }
215
216
217 static void
218 zone_iteration_error (void *cls)
219 {
220   enum GNUNET_OS_ProcessStatusType st;
221   unsigned long code;
222
223   if (! found_private_rec)
224   {
225     if (0 != run_process_and_wait (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
226                                    NULL, NULL, &st, &code,
227                                    "gnunet-namestore",
228                                    "gnunet-namestore", "-z", "master-zone",
229                                    "-a", "-e", "never", "-n", "private", "-p",
230                                    "-t", "PKEY", "-V",
231                                    private_zone_pkey, NULL))
232     {
233       ret = 8;
234       return;
235     }
236   }
237   if (! found_pin_rec)
238   {
239     if (0 != run_process_and_wait (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
240                                    NULL, NULL, &st, &code,
241                                    "gnunet-namestore",
242                                    "gnunet-namestore", "-z", "master-zone",
243                                    "-a", "-e", "never", "-n", "pin", "-p", "-t",
244                                    "PKEY", "-V", pin_zone_pkey,
245                                    NULL))
246     {
247       ret = 10;
248       return;
249     }
250   }
251   list_it = NULL;
252   GNUNET_SCHEDULER_shutdown ();
253 }
254
255
256 static void
257 zone_iteration_finished (void *cls)
258 {
259 }
260
261
262 /**
263  * Get master-zone and private-zone keys.
264  *
265  * This function is initially called for all egos and then again
266  * whenever a ego's identifier changes or if it is deleted.  At the
267  * end of the initial pass over all egos, the function is once called
268  * with 'NULL' for 'ego'. That does NOT mean that the callback won't
269  * be invoked in the future or that there was an error.
270  *
271  * When used with 'GNUNET_IDENTITY_create' or 'GNUNET_IDENTITY_get',
272  * this function is only called ONCE, and 'NULL' being passed in
273  * 'ego' does indicate an error (i.e. name is taken or no default
274  * value is known).  If 'ego' is non-NULL and if '*ctx'
275  * is set in those callbacks, the value WILL be passed to a subsequent
276  * call to the identity callback of 'GNUNET_IDENTITY_connect' (if
277  * that one was not NULL).
278  *
279  * When an identity is renamed, this function is called with the
280  * (known) ego but the NEW identifier.
281  *
282  * When an identity is deleted, this function is called with the
283  * (known) ego and "NULL" for the 'identifier'.  In this case,
284  * the 'ego' is henceforth invalid (and the 'ctx' should also be
285  * cleaned up).
286  *
287  * @param cls closure
288  * @param ego ego handle
289  * @param ctx context for application to store data for this ego
290  *                 (during the lifetime of this process, initially NULL)
291  * @param identifier identifier assigned by the user for this ego,
292  *                   NULL if the user just deleted the ego and it
293  *                   must thus no longer be used
294  */
295 static void
296 get_ego (void *cls,
297          struct GNUNET_IDENTITY_Ego *ego,
298          void **ctx,
299          const char *identifier)
300 {
301   static struct GNUNET_CRYPTO_EcdsaPublicKey pk;
302
303   if (NULL == ego)
304   {
305     if ((NULL == master_zone_pkey) ||
306         (NULL == private_zone_pkey) )
307     {
308       ret = 11;
309       GNUNET_SCHEDULER_shutdown ();
310       return;
311     }
312     list_it = GNUNET_NAMESTORE_zone_iteration_start (ns,
313                                                      &master_pk,
314                                                      &zone_iteration_error,
315                                                      NULL, &zone_iterator, NULL,
316                                                      &zone_iteration_finished,
317                                                      NULL);
318     if (NULL == list_it)
319     {
320       ret = 12;
321       GNUNET_SCHEDULER_shutdown ();
322     }
323     return;
324   }
325   GNUNET_IDENTITY_ego_get_public_key (ego, &pk);
326   if (NULL != identifier)
327   {
328     if ((NULL == master_zone_pkey) && (0 == strcmp ("master-zone",
329                                                     identifier)) )
330     {
331       master_zone_pkey = GNUNET_CRYPTO_ecdsa_public_key_to_string (&pk);
332       master_pk = *GNUNET_IDENTITY_ego_get_private_key (ego);
333     }
334     else if ((NULL == private_zone_pkey) && (0 == strcmp ("private-zone",
335                                                           identifier)) )
336       private_zone_pkey = GNUNET_CRYPTO_ecdsa_public_key_to_string (&pk);
337   }
338 }
339
340
341 /**
342  * Task run on shutdown.
343  *
344  * @param cls NULL
345  */
346 static void
347 shutdown_task (void *cls)
348 {
349   GNUNET_free_non_null (master_zone_pkey);
350   master_zone_pkey = NULL;
351   GNUNET_free_non_null (private_zone_pkey);
352   private_zone_pkey = NULL;
353   if (NULL != list_it)
354   {
355     GNUNET_NAMESTORE_zone_iteration_stop (list_it);
356     list_it = NULL;
357   }
358   if (NULL != ns)
359   {
360     GNUNET_NAMESTORE_disconnect (ns);
361     ns = NULL;
362   }
363   if (NULL != sh)
364   {
365     GNUNET_IDENTITY_disconnect (sh);
366     sh = NULL;
367   }
368 }
369
370
371 /**
372  * Main function that will be run.
373  *
374  * @param cls closure
375  * @param args remaining command-line arguments
376  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
377  * @param c configuration
378  */
379 static void
380 run (void *cls, char *const *args, const char *cfgfile,
381      const struct GNUNET_CONFIGURATION_Handle *c)
382 {
383   enum GNUNET_OS_ProcessStatusType st;
384   unsigned long code;
385
386   cfg = c;
387
388   if (0 != run_process_and_wait (GNUNET_NO, 0, NULL, NULL, &st, &code,
389                                  "gnunet-arm",
390                                  "gnunet-arm", "-I", NULL))
391   {
392     if (7 == ret)
393       fprintf (stderr,
394                "GNUnet is not running, please start GNUnet before running import\n");
395     return;
396   }
397
398   if (0 != run_process_and_wait (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
399                                  NULL, NULL, &st, &code,
400                                  "gnunet-identity",
401                                  "gnunet-identity", "-C", "master-zone", NULL))
402     return;
403
404   if (0 != run_process_and_wait (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
405                                  NULL, NULL, &st, &code,
406                                  "gnunet-identity",
407                                  "gnunet-identity", "-C", "private-zone", NULL))
408     return;
409
410   if (0 != run_process_and_wait (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
411                                  NULL, NULL, &st, &code,
412                                  "gnunet-identity",
413                                  "gnunet-identity", "-C", "sks-zone", NULL))
414     return;
415
416   if (0 != run_process_and_wait (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
417                                  NULL, NULL, &st, &code,
418                                  "gnunet-identity",
419                                  "gnunet-identity", "-e", "master-zone", "-s",
420                                  "gns-master", NULL))
421     return;
422
423   if (0 != run_process_and_wait (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
424                                  NULL, NULL, &st, &code,
425                                  "gnunet-identity",
426                                  "gnunet-identity", "-e", "master-zone", "-s",
427                                  "namestore", NULL))
428     return;
429
430   if (0 != run_process_and_wait (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
431                                  NULL, NULL, &st, &code,
432                                  "gnunet-identity",
433                                  "gnunet-identity", "-e", "master-zone", "-s",
434                                  "gns-proxy", NULL))
435     return;
436
437   if (0 != run_process_and_wait (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
438                                  NULL, NULL, &st, &code,
439                                  "gnunet-identity",
440                                  "gnunet-identity", "-e", "master-zone", "-s",
441                                  "gns-intercept", NULL))
442     return;
443
444   if (0 != run_process_and_wait (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
445                                  NULL, NULL, &st, &code,
446                                  "gnunet-identity",
447                                  "gnunet-identity", "-e", "private-zone", "-s",
448                                  "gns-private", NULL))
449     return;
450
451   if (0 != run_process_and_wait (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
452                                  NULL, NULL, &st, &code,
453                                  "gnunet-identity",
454                                  "gnunet-identity", "-e", "sks-zone", "-s",
455                                  "fs-sks", NULL))
456     return;
457
458   ns = GNUNET_NAMESTORE_connect (cfg);
459   sh = GNUNET_IDENTITY_connect (cfg, &get_ego, NULL);
460   GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
461 }
462
463
464 /**
465  * The main function for gnunet-gns.
466  *
467  * @param argc number of arguments from the command line
468  * @param argv command line arguments
469  * @return 0 ok, 1 on error
470  */
471 int
472 main (int argc, char *const *argv)
473 {
474   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
475     GNUNET_GETOPT_OPTION_END
476   };
477   int r;
478
479   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
480     return 2;
481
482   GNUNET_log_setup ("gnunet-gns-import", "WARNING", NULL);
483   ret = 0;
484   r = GNUNET_PROGRAM_run (argc, argv, "gnunet-gns-import",
485                           _ (
486                             "This program will import some GNS authorities into your GNS namestore."),
487                           options,
488                           &run, NULL);
489   GNUNET_free ((void *) argv);
490   return GNUNET_OK == r ? ret : 1;
491 }
492
493
494 /* end of gnunet-gns-import.c */