uncrustify as demanded.
[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 = "72QC35CO20UJN1E91KPJFNT9TG4CLKAPB4VK9S3Q758S9MLBRKOG";
71
72 /**
73  * Set to GNUNET_YES if private record was found;
74  */
75 static int found_private_rec = GNUNET_NO;
76
77 /**
78  * Set to GNUNET_YES if pin record was found;
79  */
80 static int found_pin_rec = GNUNET_NO;
81
82 /**
83  * Exit code.
84  */
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,
93                      enum GNUNET_OS_ProcessStatusType *st,
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
136   if (GNUNET_OK != GNUNET_OS_process_wait(p))
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 #ifdef WINDOWS
163   if (GNUNET_OS_PROCESS_EXITED != *st || 0 != *code)
164     {
165       ret = 7;
166       fprintf(stderr, "`%s' did not end correctly (%d, %d)\n", args, *st, *code);
167       return 1;
168     }
169 #endif
170   return 0;
171 }
172
173 static void
174 check_pkey(unsigned int rd_len, const struct GNUNET_GNSRECORD_Data *rd,
175            char *pk, int *found_rec)
176 {
177   int i;
178
179   for (i = 0; i < rd_len; i++)
180     {
181       char *s;
182       if (GNUNET_GNSRECORD_TYPE_PKEY != rd[i].record_type ||
183           rd[i].data_size != sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey))
184         continue;
185       s = GNUNET_GNSRECORD_value_to_string(rd[i].record_type,
186                                            rd[i].data,
187                                            rd[i].data_size);
188       if (NULL == s)
189         continue;
190       if (0 == strcmp(s, pk))
191         *found_rec = GNUNET_YES;
192       GNUNET_free(s);
193     }
194 }
195
196 /**
197  * Process a record that was stored in the namestore.
198  *
199  * @param cls closure
200  * @param zone_key private key of the zone
201  * @param rname name that is being mapped (at most 255 characters long)
202  * @param rd_len number of entries in @a rd array
203  * @param rd array of records with data to store
204  */
205 static void
206 zone_iterator(void *cls,
207               const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
208               const char *rname, unsigned int rd_len,
209               const struct GNUNET_GNSRECORD_Data *rd)
210 {
211   if (NULL != rname)
212     {
213       if (0 == strcmp(rname, "private"))
214         check_pkey(rd_len, rd, private_zone_pkey, &found_private_rec);
215       else if (0 == strcmp(rname, "pin"))
216         check_pkey(rd_len, rd, pin_zone_pkey, &found_pin_rec);
217     }
218   GNUNET_NAMESTORE_zone_iterator_next(list_it);
219 }
220
221 static void
222 zone_iteration_error(void *cls)
223 {
224   enum GNUNET_OS_ProcessStatusType st;
225   unsigned long code;
226
227   if (!found_private_rec)
228     {
229       if (0 != run_process_and_wait(GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, &st, &code,
230                                     "gnunet-namestore",
231                                     "gnunet-namestore", "-z", "master-zone", "-a", "-e", "never", "-n", "private", "-p", "-t", "PKEY", "-V", 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, NULL, NULL, &st, &code,
240                                     "gnunet-namestore",
241                                     "gnunet-namestore", "-z", "master-zone", "-a", "-e", "never", "-n", "pin", "-p", "-t", "PKEY", "-V", pin_zone_pkey, NULL))
242         {
243           ret = 10;
244           return;
245         }
246     }
247   list_it = NULL;
248   GNUNET_SCHEDULER_shutdown();
249 }
250
251
252 static void
253 zone_iteration_finished(void *cls)
254 {
255 }
256
257
258 /**
259  * Get master-zone and private-zone keys.
260  *
261  * This function is initially called for all egos and then again
262  * whenever a ego's identifier changes or if it is deleted.  At the
263  * end of the initial pass over all egos, the function is once called
264  * with 'NULL' for 'ego'. That does NOT mean that the callback won't
265  * be invoked in the future or that there was an error.
266  *
267  * When used with 'GNUNET_IDENTITY_create' or 'GNUNET_IDENTITY_get',
268  * this function is only called ONCE, and 'NULL' being passed in
269  * 'ego' does indicate an error (i.e. name is taken or no default
270  * value is known).  If 'ego' is non-NULL and if '*ctx'
271  * is set in those callbacks, the value WILL be passed to a subsequent
272  * call to the identity callback of 'GNUNET_IDENTITY_connect' (if
273  * that one was not NULL).
274  *
275  * When an identity is renamed, this function is called with the
276  * (known) ego but the NEW identifier.
277  *
278  * When an identity is deleted, this function is called with the
279  * (known) ego and "NULL" for the 'identifier'.  In this case,
280  * the 'ego' is henceforth invalid (and the 'ctx' should also be
281  * cleaned up).
282  *
283  * @param cls closure
284  * @param ego ego handle
285  * @param ctx context for application to store data for this ego
286  *                 (during the lifetime of this process, initially NULL)
287  * @param identifier identifier assigned by the user for this ego,
288  *                   NULL if the user just deleted the ego and it
289  *                   must thus no longer be used
290  */
291 static void
292 get_ego(void *cls,
293         struct GNUNET_IDENTITY_Ego *ego,
294         void **ctx,
295         const char *identifier)
296 {
297   static struct GNUNET_CRYPTO_EcdsaPublicKey pk;
298
299   if (NULL == ego)
300     {
301       if (NULL == master_zone_pkey ||
302           NULL == private_zone_pkey)
303         {
304           ret = 11;
305           GNUNET_SCHEDULER_shutdown();
306           return;
307         }
308       list_it = GNUNET_NAMESTORE_zone_iteration_start(ns,
309                                                       &master_pk, &zone_iteration_error, NULL, &zone_iterator, NULL, &zone_iteration_finished, NULL);
310       if (NULL == list_it)
311         {
312           ret = 12;
313           GNUNET_SCHEDULER_shutdown();
314         }
315       return;
316     }
317   GNUNET_IDENTITY_ego_get_public_key(ego, &pk);
318   if (NULL != identifier)
319     {
320       if (NULL == master_zone_pkey && 0 == strcmp("master-zone", identifier))
321         {
322           master_zone_pkey = GNUNET_CRYPTO_ecdsa_public_key_to_string(&pk);
323           master_pk = *GNUNET_IDENTITY_ego_get_private_key(ego);
324         }
325       else if (NULL == private_zone_pkey && 0 == strcmp("private-zone", identifier))
326         private_zone_pkey = GNUNET_CRYPTO_ecdsa_public_key_to_string(&pk);
327     }
328 }
329
330 /**
331  * Task run on shutdown.
332  *
333  * @param cls NULL
334  */
335 static void
336 shutdown_task(void *cls)
337 {
338   GNUNET_free_non_null(master_zone_pkey);
339   master_zone_pkey = NULL;
340   GNUNET_free_non_null(private_zone_pkey);
341   private_zone_pkey = NULL;
342   if (NULL != list_it)
343     {
344       GNUNET_NAMESTORE_zone_iteration_stop(list_it);
345       list_it = NULL;
346     }
347   if (NULL != ns)
348     {
349       GNUNET_NAMESTORE_disconnect(ns);
350       ns = NULL;
351     }
352   if (NULL != sh)
353     {
354       GNUNET_IDENTITY_disconnect(sh);
355       sh = NULL;
356     }
357 }
358
359 /**
360  * Main function that will be run.
361  *
362  * @param cls closure
363  * @param args remaining command-line arguments
364  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
365  * @param c configuration
366  */
367 static void
368 run(void *cls, char *const *args, const char *cfgfile,
369     const struct GNUNET_CONFIGURATION_Handle *c)
370 {
371   enum GNUNET_OS_ProcessStatusType st;
372   unsigned long code;
373
374   cfg = c;
375
376   if (0 != run_process_and_wait(GNUNET_NO, 0, NULL, NULL, &st, &code,
377                                 "gnunet-arm",
378                                 "gnunet-arm", "-I", NULL))
379     {
380       if (7 == ret)
381         fprintf(stderr, "GNUnet is not running, please start GNUnet before running import\n");
382       return;
383     }
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", "master-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", "-C", "private-zone", 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", "-C", "sks-zone", 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-master", 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", "namestore", 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", "master-zone", "-s", "gns-proxy", 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", "master-zone", "-s", "gns-intercept", NULL))
418     return;
419
420   if (0 != run_process_and_wait(GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, &st, &code,
421                                 "gnunet-identity",
422                                 "gnunet-identity", "-e", "private-zone", "-s", "gns-private", NULL))
423     return;
424
425   if (0 != run_process_and_wait(GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, &st, &code,
426                                 "gnunet-identity",
427                                 "gnunet-identity", "-e", "sks-zone", "-s", "fs-sks", NULL))
428     return;
429
430   ns = GNUNET_NAMESTORE_connect(cfg);
431   sh = GNUNET_IDENTITY_connect(cfg, &get_ego, NULL);
432   GNUNET_SCHEDULER_add_shutdown(&shutdown_task, NULL);
433 }
434
435
436 /**
437  * The main function for gnunet-gns.
438  *
439  * @param argc number of arguments from the command line
440  * @param argv command line arguments
441  * @return 0 ok, 1 on error
442  */
443 int
444 main(int argc, char *const *argv)
445 {
446   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
447     GNUNET_GETOPT_OPTION_END
448   };
449   int r;
450
451   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args(argc, argv, &argc, &argv))
452     return 2;
453
454   GNUNET_log_setup("gnunet-gns-import", "WARNING", NULL);
455   ret = 0;
456   r = GNUNET_PROGRAM_run(argc, argv, "gnunet-gns-import",
457                          _("This program will import some GNS authorities into your GNS namestore."),
458                          options,
459                          &run, NULL);
460   GNUNET_free((void*)argv);
461   return GNUNET_OK == r ? ret : 1;
462 }
463
464 /* end of gnunet-gns-import.c */