diff options
author | LRN <lrn1986@gmail.com> | 2013-12-22 15:51:56 +0000 |
---|---|---|
committer | LRN <lrn1986@gmail.com> | 2013-12-22 15:51:56 +0000 |
commit | 52e5c4137c3950e09f462435a2db4179f62cba9e (patch) | |
tree | bd4cd0b94e1f90839b371f43237a1eed92c78ba9 /src/gns/gnunet-gns-import.c | |
parent | 816907b6881e3f25a41e845ddd34cc01f79b1a2b (diff) | |
download | gnunet-52e5c4137c3950e09f462435a2db4179f62cba9e.tar.gz gnunet-52e5c4137c3950e09f462435a2db4179f62cba9e.zip |
Add compilable gnunet-gns-import
Diffstat (limited to 'src/gns/gnunet-gns-import.c')
-rwxr-xr-x | src/gns/gnunet-gns-import.c | 483 |
1 files changed, 483 insertions, 0 deletions
diff --git a/src/gns/gnunet-gns-import.c b/src/gns/gnunet-gns-import.c new file mode 100755 index 000000000..25c56a975 --- /dev/null +++ b/src/gns/gnunet-gns-import.c | |||
@@ -0,0 +1,483 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2012-2013 Christian Grothoff (and other contributing authors) | ||
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., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, 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, pipe_stdin, | ||
133 | pipe_stdout, filename, ap); | ||
134 | va_end (ap); | ||
135 | if (NULL == p) | ||
136 | { | ||
137 | ret = 3; | ||
138 | fprintf (stderr, "Failed to run `%s'\n", args); | ||
139 | GNUNET_free (args); | ||
140 | return 1; | ||
141 | } | ||
142 | |||
143 | if (GNUNET_OK != GNUNET_OS_process_wait (p)) | ||
144 | { | ||
145 | ret = 4; | ||
146 | fprintf (stderr, "Failed to wait for `%s'\n", args); | ||
147 | GNUNET_free (args); | ||
148 | return 1; | ||
149 | } | ||
150 | |||
151 | switch (GNUNET_OS_process_status (p, st, code)) | ||
152 | { | ||
153 | case GNUNET_OK: | ||
154 | break; | ||
155 | case GNUNET_NO: | ||
156 | ret = 5; | ||
157 | fprintf (stderr, "`%s' is still running\n", args); | ||
158 | GNUNET_free (args); | ||
159 | return 1; | ||
160 | default: | ||
161 | case GNUNET_SYSERR: | ||
162 | ret = 6; | ||
163 | fprintf (stderr, "Failed to check the status of `%s'\n", args); | ||
164 | GNUNET_free (args); | ||
165 | return 1; | ||
166 | } | ||
167 | #ifdef WINDOWS | ||
168 | if (GNUNET_OS_PROCESS_EXITED != *st || 0 != *code) | ||
169 | { | ||
170 | ret = 7; | ||
171 | fprintf (stderr, "`%s' did not end correctly (%d, %d)\n", args, *st, *code); | ||
172 | return 1; | ||
173 | } | ||
174 | #endif | ||
175 | return 0; | ||
176 | } | ||
177 | |||
178 | static void | ||
179 | check_pkey (unsigned int rd_len, const struct GNUNET_GNSRECORD_Data *rd, | ||
180 | char *pk, int *found_rec) | ||
181 | { | ||
182 | int i; | ||
183 | for (i = 0; i < rd_len; i++) | ||
184 | { | ||
185 | char *s; | ||
186 | if (GNUNET_GNSRECORD_TYPE_PKEY != rd[i].record_type || | ||
187 | rd[i].data_size != sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)) | ||
188 | continue; | ||
189 | s = GNUNET_GNSRECORD_value_to_string (rd[i].record_type, | ||
190 | rd[i].data, | ||
191 | rd[i].data_size); | ||
192 | if (NULL == s) | ||
193 | continue; | ||
194 | if (0 == strcmp (s, pk)) | ||
195 | *found_rec = GNUNET_YES; | ||
196 | GNUNET_free (s); | ||
197 | } | ||
198 | } | ||
199 | |||
200 | /** | ||
201 | * Process a record that was stored in the namestore. | ||
202 | * | ||
203 | * @param cls closure | ||
204 | * @param zone_key private key of the zone | ||
205 | * @param rname name that is being mapped (at most 255 characters long) | ||
206 | * @param rd_len number of entries in @a rd array | ||
207 | * @param rd array of records with data to store | ||
208 | */ | ||
209 | static void | ||
210 | zone_iterator (void *cls, | ||
211 | const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key, | ||
212 | const char *rname, unsigned int rd_len, | ||
213 | const struct GNUNET_GNSRECORD_Data *rd) | ||
214 | { | ||
215 | if (NULL != rname) | ||
216 | { | ||
217 | if (0 == strcmp (rname, "private")) | ||
218 | check_pkey (rd_len, rd, private_zone_pkey, &found_private_rec); | ||
219 | else if (0 == strcmp (rname, "short")) | ||
220 | check_pkey (rd_len, rd, short_zone_pkey, &found_short_rec); | ||
221 | else if (0 == strcmp (rname, "pin")) | ||
222 | check_pkey (rd_len, rd, pin_zone_pkey, &found_pin_rec); | ||
223 | } | ||
224 | if (NULL == rname && 0 == rd_len && NULL == rd) | ||
225 | { | ||
226 | enum GNUNET_OS_ProcessStatusType st; | ||
227 | unsigned long code; | ||
228 | if (!found_private_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", "private", "-p", "-t", "PKEY", "-V", private_zone_pkey, NULL)) | ||
233 | { | ||
234 | ret = 8; | ||
235 | return; | ||
236 | } | ||
237 | } | ||
238 | if (!found_short_rec) | ||
239 | { | ||
240 | if (0 != run_process_and_wait (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, &st, &code, | ||
241 | "gnunet-namestore", | ||
242 | "gnunet-namestore", "-z", "master-zone", "-a", "-e", "never", "-n", "short", "-p", "-t", "PKEY", "-V", short_zone_pkey, NULL)) | ||
243 | { | ||
244 | ret = 9; | ||
245 | return; | ||
246 | } | ||
247 | } | ||
248 | if (!found_pin_rec) | ||
249 | { | ||
250 | if (0 != run_process_and_wait (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, &st, &code, | ||
251 | "gnunet-namestore", | ||
252 | "gnunet-namestore", "-z", "master-zone", "-a", "-e", "never", "-n", "pin", "-p", "-t", "PKEY", "-V", "72QC35CO20UJN1E91KPJFNT9TG4CLKAPB4VK9S3Q758S9MLBRKOG", NULL)) | ||
253 | { | ||
254 | ret = 10; | ||
255 | return; | ||
256 | } | ||
257 | } | ||
258 | list_it = NULL; | ||
259 | GNUNET_SCHEDULER_shutdown (); | ||
260 | return; | ||
261 | } | ||
262 | GNUNET_NAMESTORE_zone_iterator_next (list_it); | ||
263 | } | ||
264 | |||
265 | /** | ||
266 | * If listing is enabled, prints information about the egos. | ||
267 | * | ||
268 | * This function is initially called for all egos and then again | ||
269 | * whenever a ego's identifier changes or if it is deleted. At the | ||
270 | * end of the initial pass over all egos, the function is once called | ||
271 | * with 'NULL' for 'ego'. That does NOT mean that the callback won't | ||
272 | * be invoked in the future or that there was an error. | ||
273 | * | ||
274 | * When used with 'GNUNET_IDENTITY_create' or 'GNUNET_IDENTITY_get', | ||
275 | * this function is only called ONCE, and 'NULL' being passed in | ||
276 | * 'ego' does indicate an error (i.e. name is taken or no default | ||
277 | * value is known). If 'ego' is non-NULL and if '*ctx' | ||
278 | * is set in those callbacks, the value WILL be passed to a subsequent | ||
279 | * call to the identity callback of 'GNUNET_IDENTITY_connect' (if | ||
280 | * that one was not NULL). | ||
281 | * | ||
282 | * When an identity is renamed, this function is called with the | ||
283 | * (known) ego but the NEW identifier. | ||
284 | * | ||
285 | * When an identity is deleted, this function is called with the | ||
286 | * (known) ego and "NULL" for the 'identifier'. In this case, | ||
287 | * the 'ego' is henceforth invalid (and the 'ctx' should also be | ||
288 | * cleaned up). | ||
289 | * | ||
290 | * @param cls closure | ||
291 | * @param ego ego handle | ||
292 | * @param ctx context for application to store data for this ego | ||
293 | * (during the lifetime of this process, initially NULL) | ||
294 | * @param identifier identifier assigned by the user for this ego, | ||
295 | * NULL if the user just deleted the ego and it | ||
296 | * must thus no longer be used | ||
297 | */ | ||
298 | static void | ||
299 | get_ego (void *cls, | ||
300 | struct GNUNET_IDENTITY_Ego *ego, | ||
301 | void **ctx, | ||
302 | const char *identifier) | ||
303 | { | ||
304 | static struct GNUNET_CRYPTO_EcdsaPublicKey pk; | ||
305 | if (NULL == ego) | ||
306 | { | ||
307 | if (NULL == master_zone_pkey || | ||
308 | NULL == short_zone_pkey || | ||
309 | NULL == private_zone_pkey) | ||
310 | { | ||
311 | ret = 11; | ||
312 | GNUNET_SCHEDULER_shutdown (); | ||
313 | return; | ||
314 | } | ||
315 | list_it = GNUNET_NAMESTORE_zone_iteration_start (ns, | ||
316 | &master_pk, &zone_iterator, NULL); | ||
317 | if (NULL == list_it) | ||
318 | { | ||
319 | ret = 12; | ||
320 | GNUNET_SCHEDULER_shutdown (); | ||
321 | } | ||
322 | return; | ||
323 | } | ||
324 | GNUNET_IDENTITY_ego_get_public_key (ego, &pk); | ||
325 | if (NULL != identifier) | ||
326 | { | ||
327 | if (NULL == master_zone_pkey && 0 == strcmp ("master-zone", identifier)) | ||
328 | { | ||
329 | master_zone_pkey = GNUNET_CRYPTO_ecdsa_public_key_to_string (&pk); | ||
330 | master_pk = *GNUNET_IDENTITY_ego_get_private_key (ego); | ||
331 | } | ||
332 | else if (NULL == short_zone_pkey && 0 == strcmp ("short-zone", identifier)) | ||
333 | short_zone_pkey = GNUNET_CRYPTO_ecdsa_public_key_to_string (&pk); | ||
334 | else if (NULL == private_zone_pkey && 0 == strcmp ("private-zone", identifier)) | ||
335 | private_zone_pkey = GNUNET_CRYPTO_ecdsa_public_key_to_string (&pk); | ||
336 | } | ||
337 | } | ||
338 | |||
339 | /** | ||
340 | * Task run on shutdown. | ||
341 | * | ||
342 | * @param cls NULL | ||
343 | * @param tc unused | ||
344 | */ | ||
345 | static void | ||
346 | shutdown_task (void *cls, | ||
347 | const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
348 | { | ||
349 | GNUNET_free_non_null (master_zone_pkey); | ||
350 | master_zone_pkey = NULL; | ||
351 | GNUNET_free_non_null (short_zone_pkey); | ||
352 | short_zone_pkey = NULL; | ||
353 | GNUNET_free_non_null (private_zone_pkey); | ||
354 | private_zone_pkey = NULL; | ||
355 | if (NULL != list_it) | ||
356 | { | ||
357 | GNUNET_NAMESTORE_zone_iteration_stop (list_it); | ||
358 | list_it = NULL; | ||
359 | } | ||
360 | if (NULL != ns) | ||
361 | { | ||
362 | GNUNET_NAMESTORE_disconnect (ns); | ||
363 | ns = NULL; | ||
364 | } | ||
365 | if (NULL != sh) | ||
366 | { | ||
367 | GNUNET_IDENTITY_disconnect (sh); | ||
368 | sh = NULL; | ||
369 | } | ||
370 | } | ||
371 | |||
372 | /** | ||
373 | * Main function that will be run. | ||
374 | * | ||
375 | * @param cls closure | ||
376 | * @param args remaining command-line arguments | ||
377 | * @param cfgfile name of the configuration file used (for saving, can be NULL!) | ||
378 | * @param c configuration | ||
379 | */ | ||
380 | static void | ||
381 | run (void *cls, char *const *args, const char *cfgfile, | ||
382 | const struct GNUNET_CONFIGURATION_Handle *c) | ||
383 | { | ||
384 | enum GNUNET_OS_ProcessStatusType st; | ||
385 | unsigned long code; | ||
386 | |||
387 | cfg = c; | ||
388 | |||
389 | if (0 != run_process_and_wait (GNUNET_NO, 0, NULL, NULL, &st, &code, | ||
390 | "gnunet-arm", | ||
391 | "gnunet-arm", "-I", NULL)) | ||
392 | { | ||
393 | if (7 == ret) | ||
394 | fprintf (stderr, "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, NULL, NULL, &st, &code, | ||
399 | "gnunet-identity", | ||
400 | "gnunet-identity", "-C", "master-zone", 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", "-C", "short-zone", 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", "-C", "private-zone", 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", "-C", "sks-zone", 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", "short-zone", "-s", "gns-short", NULL)) | ||
421 | return; | ||
422 | |||
423 | if (0 != run_process_and_wait (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, &st, &code, | ||
424 | "gnunet-identity", | ||
425 | "gnunet-identity", "-e", "master-zone", "-s", "gns-master", NULL)) | ||
426 | return; | ||
427 | |||
428 | if (0 != run_process_and_wait (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, &st, &code, | ||
429 | "gnunet-identity", | ||
430 | "gnunet-identity", "-e", "master-zone", "-s", "namestore", NULL)) | ||
431 | return; | ||
432 | |||
433 | if (0 != run_process_and_wait (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, &st, &code, | ||
434 | "gnunet-identity", | ||
435 | "gnunet-identity", "-e", "short-zone", "-s", "gns-proxy", NULL)) | ||
436 | return; | ||
437 | |||
438 | if (0 != run_process_and_wait (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, &st, &code, | ||
439 | "gnunet-identity", | ||
440 | "gnunet-identity", "-e", "private-zone", "-s", "gns-private", NULL)) | ||
441 | return; | ||
442 | |||
443 | if (0 != run_process_and_wait (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, &st, &code, | ||
444 | "gnunet-identity", | ||
445 | "gnunet-identity", "-e", "sks-zone", "-s", "fs-sks", NULL)) | ||
446 | return; | ||
447 | |||
448 | ns = GNUNET_NAMESTORE_connect (cfg); | ||
449 | sh = GNUNET_IDENTITY_connect (cfg, &get_ego, NULL); | ||
450 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, | ||
451 | &shutdown_task, NULL); | ||
452 | } | ||
453 | |||
454 | |||
455 | /** | ||
456 | * The main function for gnunet-gns. | ||
457 | * | ||
458 | * @param argc number of arguments from the command line | ||
459 | * @param argv command line arguments | ||
460 | * @return 0 ok, 1 on error | ||
461 | */ | ||
462 | int | ||
463 | main (int argc, char *const *argv) | ||
464 | { | ||
465 | static const struct GNUNET_GETOPT_CommandLineOption options[] = { | ||
466 | GNUNET_GETOPT_OPTION_END | ||
467 | }; | ||
468 | int r; | ||
469 | |||
470 | if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) | ||
471 | return 2; | ||
472 | |||
473 | GNUNET_log_setup ("gnunet-gns-import", "WARNING", NULL); | ||
474 | ret = 0; | ||
475 | r = GNUNET_PROGRAM_run (argc, argv, "gnunet-gns-import", | ||
476 | _("This program will import some GNS authorities into your GNS namestore."), | ||
477 | options, | ||
478 | &run, NULL); | ||
479 | GNUNET_free ((void*) argv); | ||
480 | return GNUNET_OK == r ? ret : 1; | ||
481 | } | ||
482 | |||
483 | /* end of gnunet-gns-import.c */ | ||