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