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