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