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