REST/NAMESTORE: rework API
[oweals/gnunet.git] / src / reclaim / gnunet-reclaim.c
1 /*
2    This file is part of GNUnet.
3    Copyright (C) 2012-2015 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  * @author Martin Schanzenbach
22  * @file src/reclaim/gnunet-reclaim.c
23  * @brief Identity Provider utility
24  *
25  */
26 #include "platform.h"
27 #include <inttypes.h>
28
29 #include "gnunet_util_lib.h"
30
31 #include "gnunet_identity_service.h"
32 #include "gnunet_namestore_service.h"
33 #include "gnunet_reclaim_service.h"
34 #include "gnunet_signatures.h"
35 /**
36  * return value
37  */
38 static int ret;
39
40 /**
41  * List attribute flag
42  */
43 static int list;
44
45 /**
46  * Relying party
47  */
48 static char *rp;
49
50 /**
51  * The attribute
52  */
53 static char *attr_name;
54
55 /**
56  * Attribute value
57  */
58 static char *attr_value;
59
60 /**
61  * Attributes to issue
62  */
63 static char *issue_attrs;
64
65 /**
66  * Ticket to consume
67  */
68 static char *consume_ticket;
69
70 /**
71  * Attribute type
72  */
73 static char *type_str;
74
75 /**
76  * Ticket to revoke
77  */
78 static char *revoke_ticket;
79
80 /**
81  * Ticket listing
82  */
83 static int list_tickets;
84
85 /**
86  * Ego name
87  */
88 static char *ego_name;
89
90 /**
91  * Identity handle
92  */
93 static struct GNUNET_IDENTITY_Handle *identity_handle;
94
95 /**
96  * reclaim handle
97  */
98 static struct GNUNET_RECLAIM_Handle *reclaim_handle;
99
100 /**
101  * reclaim operation
102  */
103 static struct GNUNET_RECLAIM_Operation *reclaim_op;
104
105 /**
106  * Attribute iterator
107  */
108 static struct GNUNET_RECLAIM_AttributeIterator *attr_iterator;
109
110 /**
111  * Ticket iterator
112  */
113 static struct GNUNET_RECLAIM_TicketIterator *ticket_iterator;
114
115 /**
116  * Master ABE key
117  */
118 static struct GNUNET_CRYPTO_AbeMasterKey *abe_key;
119
120 /**
121  * ego private key
122  */
123 static const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey;
124
125 /**
126  * rp public key
127  */
128 static struct GNUNET_CRYPTO_EcdsaPublicKey rp_key;
129
130 /**
131  * Ticket to consume
132  */
133 static struct GNUNET_RECLAIM_Ticket ticket;
134
135 /**
136  * Attribute list
137  */
138 static struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attr_list;
139
140 /**
141  * Attribute expiration interval
142  */
143 static struct GNUNET_TIME_Relative exp_interval;
144
145 /**
146  * Timeout task
147  */
148 static struct GNUNET_SCHEDULER_Task *timeout;
149
150 /**
151  * Cleanup task
152  */
153 static struct GNUNET_SCHEDULER_Task *cleanup_task;
154
155 /**
156  * Claim to store
157  */
158 struct GNUNET_RECLAIM_ATTRIBUTE_Claim *claim;
159
160 /**
161  * Claim to delete
162  */
163 static char *attr_delete;
164
165 /**
166  * Claim object to delete
167  */
168 static struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr_to_delete;
169
170 static void
171 do_cleanup (void *cls)
172 {
173   cleanup_task = NULL;
174   if (NULL != timeout)
175     GNUNET_SCHEDULER_cancel (timeout);
176   if (NULL != reclaim_op)
177     GNUNET_RECLAIM_cancel (reclaim_op);
178   if (NULL != attr_iterator)
179     GNUNET_RECLAIM_get_attributes_stop (attr_iterator);
180   if (NULL != ticket_iterator)
181     GNUNET_RECLAIM_ticket_iteration_stop (ticket_iterator);
182   if (NULL != reclaim_handle)
183     GNUNET_RECLAIM_disconnect (reclaim_handle);
184   if (NULL != identity_handle)
185     GNUNET_IDENTITY_disconnect (identity_handle);
186   if (NULL != abe_key)
187     GNUNET_free (abe_key);
188   if (NULL != attr_list)
189     GNUNET_free (attr_list);
190   if (NULL != attr_to_delete)
191     GNUNET_free (attr_to_delete);
192 }
193
194 static void
195 ticket_issue_cb (void *cls, const struct GNUNET_RECLAIM_Ticket *ticket)
196 {
197   char *ticket_str;
198   reclaim_op = NULL;
199   if (NULL != ticket) {
200     ticket_str = GNUNET_STRINGS_data_to_string_alloc (
201         ticket, sizeof (struct GNUNET_RECLAIM_Ticket));
202     printf ("%s\n", ticket_str);
203     GNUNET_free (ticket_str);
204   }
205   cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
206 }
207
208 static void
209 store_attr_cont (void *cls, int32_t success, const char *emsg)
210 {
211   reclaim_op = NULL;
212   if (GNUNET_SYSERR == success) {
213     fprintf (stderr, "%s\n", emsg);
214   }
215   cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
216 }
217
218 static void
219 process_attrs (void *cls, const struct GNUNET_CRYPTO_EcdsaPublicKey *identity,
220                const struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr)
221 {
222   char *value_str;
223   char *id;
224   const char *attr_type;
225
226   if (NULL == identity) {
227     reclaim_op = NULL;
228     cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
229     return;
230   }
231   if (NULL == attr) {
232     ret = 1;
233     return;
234   }
235   value_str = GNUNET_RECLAIM_ATTRIBUTE_value_to_string (attr->type, attr->data,
236                                                         attr->data_size);
237   attr_type = GNUNET_RECLAIM_ATTRIBUTE_number_to_typename (attr->type);
238   id = GNUNET_STRINGS_data_to_string_alloc (&attr->id, sizeof (uint64_t));
239   fprintf (stdout, "Name: %s; Value: %s (%s); Version %u; ID: %s\n", attr->name,
240            value_str, attr_type, attr->version, id);
241   GNUNET_free (id);
242 }
243
244 static void
245 ticket_iter_err (void *cls)
246 {
247   ticket_iterator = NULL;
248   fprintf (stderr, "Failed to iterate over tickets\n");
249   cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
250 }
251
252 static void
253 ticket_iter_fin (void *cls)
254 {
255   ticket_iterator = NULL;
256   cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
257 }
258
259 static void
260 ticket_iter (void *cls, const struct GNUNET_RECLAIM_Ticket *ticket)
261 {
262   char *aud;
263   char *ref;
264
265   aud = GNUNET_STRINGS_data_to_string_alloc (
266       &ticket->audience, sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
267   ref = GNUNET_STRINGS_data_to_string_alloc (&ticket->rnd, sizeof (uint64_t));
268
269   fprintf (stdout, "Ticket ID: %s | Audience: %s\n", ref, aud);
270   GNUNET_RECLAIM_ticket_iteration_next (ticket_iterator);
271 }
272
273 static void
274 iter_error (void *cls)
275 {
276   attr_iterator = NULL;
277   fprintf (stderr, "Failed to iterate over attributes\n");
278   cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
279 }
280
281 static void
282 timeout_task (void *cls)
283 {
284   timeout = NULL;
285   ret = 1;
286   fprintf (stderr, "Timeout\n");
287   if (NULL == cleanup_task)
288     cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
289 }
290
291 static void
292 process_rvk (void *cls, int success, const char *msg)
293 {
294   reclaim_op = NULL;
295   if (GNUNET_OK != success) {
296     fprintf (stderr, "Revocation failed.\n");
297     ret = 1;
298   }
299   cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
300 }
301
302
303 static void
304 process_delete (void *cls, int success, const char *msg)
305 {
306   reclaim_op = NULL;
307   if (GNUNET_OK != success) {
308     fprintf (stderr, "Deletion failed.\n");
309     ret = 1;
310   }
311   cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
312 }
313
314
315 static void
316 iter_finished (void *cls)
317 {
318   char *data;
319   size_t data_size;
320   int type;
321
322   attr_iterator = NULL;
323   if (list) {
324     cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
325     return;
326   }
327
328   if (issue_attrs) {
329     reclaim_op = GNUNET_RECLAIM_ticket_issue (
330         reclaim_handle, pkey, &rp_key, attr_list, &ticket_issue_cb, NULL);
331     return;
332   }
333   if (consume_ticket) {
334     reclaim_op = GNUNET_RECLAIM_ticket_consume (reclaim_handle, pkey, &ticket,
335                                                 &process_attrs, NULL);
336     timeout = GNUNET_SCHEDULER_add_delayed (
337         GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
338         &timeout_task, NULL);
339     return;
340   }
341   if (revoke_ticket) {
342     reclaim_op = GNUNET_RECLAIM_ticket_revoke (reclaim_handle, pkey, &ticket,
343                                                &process_rvk, NULL);
344     return;
345   }
346   if (attr_delete) {
347     if (NULL == attr_to_delete) {
348       fprintf (stdout, "No such attribute ``%s''\n", attr_delete);
349       return;
350     }
351     reclaim_op = GNUNET_RECLAIM_attribute_delete (
352         reclaim_handle, pkey, attr_to_delete, &process_delete, NULL);
353     return;
354   }
355   if (attr_name) {
356     if (NULL == type_str)
357       type = GNUNET_RECLAIM_ATTRIBUTE_TYPE_STRING;
358     else
359       type = GNUNET_RECLAIM_ATTRIBUTE_typename_to_number (type_str);
360
361     GNUNET_assert (GNUNET_SYSERR !=
362                    GNUNET_RECLAIM_ATTRIBUTE_string_to_value (
363                        type, attr_value, (void **)&data, &data_size));
364     if (NULL != claim) {
365       claim->type = type;
366       claim->data = data;
367       claim->data_size = data_size;
368     } else {
369       claim =
370           GNUNET_RECLAIM_ATTRIBUTE_claim_new (attr_name, type, data, data_size);
371     }
372     reclaim_op = GNUNET_RECLAIM_attribute_store (
373         reclaim_handle, pkey, claim, &exp_interval, &store_attr_cont, NULL);
374     GNUNET_free (data);
375     GNUNET_free (claim);
376     return;
377   }
378   cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
379 }
380
381 static void
382 iter_cb (void *cls, const struct GNUNET_CRYPTO_EcdsaPublicKey *identity,
383          const struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr)
384 {
385   struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le;
386   char *attrs_tmp;
387   char *attr_str;
388   char *label;
389   char *id;
390   const char *attr_type;
391
392   if ((NULL != attr_name) && (NULL != claim)) {
393     if (0 == strcasecmp (attr_name, attr->name)) {
394       claim = GNUNET_RECLAIM_ATTRIBUTE_claim_new (attr->name, attr->type,
395                                                   attr->data, attr->data_size);
396     }
397   } else if (issue_attrs) {
398     attrs_tmp = GNUNET_strdup (issue_attrs);
399     attr_str = strtok (attrs_tmp, ",");
400     while (NULL != attr_str) {
401       if (0 != strcasecmp (attr_str, attr->name)) {
402         attr_str = strtok (NULL, ",");
403         continue;
404       }
405       le = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry);
406       le->claim = GNUNET_RECLAIM_ATTRIBUTE_claim_new (
407           attr->name, attr->type, attr->data, attr->data_size);
408       le->claim->version = attr->version;
409       le->claim->id = attr->id;
410       GNUNET_CONTAINER_DLL_insert (attr_list->list_head, attr_list->list_tail,
411                                    le);
412       break;
413     }
414     GNUNET_free (attrs_tmp);
415   } else if (attr_delete && (NULL == attr_to_delete)) {
416     label = GNUNET_STRINGS_data_to_string_alloc (&attr->id, sizeof (uint64_t));
417     if (0 == strcasecmp (attr_delete, label)) {
418       attr_to_delete = GNUNET_RECLAIM_ATTRIBUTE_claim_new (
419           attr->name, attr->type, attr->data, attr->data_size);
420       attr_to_delete->id = attr->id;
421     }
422     GNUNET_free (label);
423   } else if (list) {
424     attr_str = GNUNET_RECLAIM_ATTRIBUTE_value_to_string (attr->type, attr->data,
425                                                          attr->data_size);
426     attr_type = GNUNET_RECLAIM_ATTRIBUTE_number_to_typename (attr->type);
427     id = GNUNET_STRINGS_data_to_string_alloc (&attr->id, sizeof (uint64_t));
428     fprintf (stdout, "Name: %s; Value: %s (%s); Version %u; ID: %s\n",
429              attr->name, attr_str, attr_type, attr->version, id);
430     GNUNET_free (id);
431   }
432   GNUNET_RECLAIM_get_attributes_next (attr_iterator);
433 }
434
435 static void
436 start_process ()
437 {
438   if (NULL == pkey) {
439     fprintf (stderr, "Ego %s not found\n", ego_name);
440     cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
441     return;
442   }
443
444   if (list_tickets) {
445     ticket_iterator = GNUNET_RECLAIM_ticket_iteration_start (
446         reclaim_handle, pkey, &ticket_iter_err, NULL, &ticket_iter, NULL,
447         &ticket_iter_fin, NULL);
448     return;
449   }
450
451   if (NULL != rp)
452     GNUNET_CRYPTO_ecdsa_public_key_from_string (rp, strlen (rp), &rp_key);
453   if (NULL != consume_ticket)
454     GNUNET_STRINGS_string_to_data (consume_ticket, strlen (consume_ticket),
455                                    &ticket,
456                                    sizeof (struct GNUNET_RECLAIM_Ticket));
457   if (NULL != revoke_ticket)
458     GNUNET_STRINGS_string_to_data (revoke_ticket, strlen (revoke_ticket),
459                                    &ticket,
460                                    sizeof (struct GNUNET_RECLAIM_Ticket));
461
462   attr_list = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList);
463   claim = NULL;
464   attr_iterator = GNUNET_RECLAIM_get_attributes_start (
465       reclaim_handle, pkey, &iter_error, NULL, &iter_cb, NULL, &iter_finished,
466       NULL);
467 }
468
469 static int init = GNUNET_YES;
470
471 static void
472 ego_cb (void *cls, struct GNUNET_IDENTITY_Ego *ego, void **ctx,
473         const char *name)
474 {
475   if (NULL == name) {
476     if (GNUNET_YES == init) {
477       init = GNUNET_NO;
478       start_process ();
479     }
480     return;
481   }
482   if (0 != strcmp (name, ego_name))
483     return;
484   pkey = GNUNET_IDENTITY_ego_get_private_key (ego);
485 }
486
487
488 static void
489 run (void *cls, char *const *args, const char *cfgfile,
490      const struct GNUNET_CONFIGURATION_Handle *c)
491 {
492   ret = 0;
493   if (NULL == ego_name) {
494     ret = 1;
495     fprintf (stderr, _ ("Ego is required\n"));
496     return;
497   }
498
499   if ((NULL == attr_value) && (NULL != attr_name)) {
500     ret = 1;
501     fprintf (stderr, _ ("Attribute value missing!\n"));
502     return;
503   }
504
505   if ((NULL == rp) && (NULL != issue_attrs)) {
506     ret = 1;
507     fprintf (stderr, _ ("Requesting party key is required!\n"));
508     return;
509   }
510
511   reclaim_handle = GNUNET_RECLAIM_connect (c);
512   // Get Ego
513   identity_handle = GNUNET_IDENTITY_connect (c, &ego_cb, NULL);
514 }
515
516
517 int
518 main (int argc, char *const argv[])
519 {
520   exp_interval = GNUNET_TIME_UNIT_HOURS;
521   struct GNUNET_GETOPT_CommandLineOption options[] = {
522
523       GNUNET_GETOPT_option_string ('a', "add", "NAME",
524                                    gettext_noop ("Add an attribute NAME"),
525                                    &attr_name),
526       GNUNET_GETOPT_option_string ('d', "delete", "ID",
527                                    gettext_noop ("Add an attribute with ID"),
528                                    &attr_delete),
529       GNUNET_GETOPT_option_string ('V', "value", "VALUE",
530                                    gettext_noop ("The attribute VALUE"),
531                                    &attr_value),
532       GNUNET_GETOPT_option_string ('e', "ego", "EGO",
533                                    gettext_noop ("The EGO to use"), &ego_name),
534       GNUNET_GETOPT_option_string (
535           'r', "rp", "RP", gettext_noop ("Specify the relying party for issue"),
536           &rp),
537       GNUNET_GETOPT_option_flag (
538           'D', "dump", gettext_noop ("List attributes for EGO"), &list),
539       GNUNET_GETOPT_option_string (
540           'i', "issue", "A1,A2,...",
541           gettext_noop (
542               "Issue a ticket for a set of attributes separated by comma"),
543           &issue_attrs),
544       GNUNET_GETOPT_option_string ('C', "consume", "TICKET",
545                                    gettext_noop ("Consume a ticket"),
546                                    &consume_ticket),
547       GNUNET_GETOPT_option_string ('R', "revoke", "TICKET",
548                                    gettext_noop ("Revoke a ticket"),
549                                    &revoke_ticket),
550       GNUNET_GETOPT_option_string (
551           't', "type", "TYPE", gettext_noop ("Type of attribute"), &type_str),
552       GNUNET_GETOPT_option_flag (
553           'T', "tickets", gettext_noop ("List tickets of ego"), &list_tickets),
554       GNUNET_GETOPT_option_relative_time (
555           'E', "expiration", "INTERVAL",
556           gettext_noop ("Expiration interval of the attribute"), &exp_interval),
557
558       GNUNET_GETOPT_OPTION_END};
559   if (GNUNET_OK != GNUNET_PROGRAM_run (argc, argv, "gnunet-reclaim",
560                                        _ ("re:claimID command line tool"),
561                                        options, &run, NULL))
562     return 1;
563   else
564     return ret;
565 }