renamed identity-provider subsystem to reclaim
[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 static void
138 do_cleanup(void *cls)
139 {
140   if (NULL != timeout)
141     GNUNET_SCHEDULER_cancel (timeout);
142   if (NULL != reclaim_op)
143     GNUNET_RECLAIM_cancel (reclaim_op);
144   if (NULL != attr_iterator)
145     GNUNET_RECLAIM_get_attributes_stop (attr_iterator);
146   if (NULL != reclaim_handle)
147     GNUNET_RECLAIM_disconnect (reclaim_handle);
148   if (NULL != identity_handle)
149     GNUNET_IDENTITY_disconnect (identity_handle);
150   if (NULL != abe_key)
151     GNUNET_free (abe_key);
152   if (NULL != attr_list)
153     GNUNET_free (attr_list);
154 }
155
156 static void
157 ticket_issue_cb (void* cls,
158                  const struct GNUNET_RECLAIM_Ticket *ticket)
159 {
160   char* ticket_str;
161   reclaim_op = NULL;
162   if (NULL != ticket) {
163     ticket_str = GNUNET_STRINGS_data_to_string_alloc (ticket,
164                                                       sizeof (struct GNUNET_RECLAIM_Ticket));
165     printf("%s\n",
166            ticket_str);
167     GNUNET_free (ticket_str);
168   }
169   GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
170 }
171
172 static void
173 store_attr_cont (void *cls,
174                  int32_t success,
175                  const char*emsg)
176 {
177   reclaim_op = NULL;
178   if (GNUNET_SYSERR == success) {
179     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
180                 "%s\n", emsg);
181   }
182   GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
183 }
184
185 static void
186 process_attrs (void *cls,
187          const struct GNUNET_CRYPTO_EcdsaPublicKey *identity,
188          const struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr)
189 {
190   char *value_str;
191   if (NULL == identity)
192   {
193     reclaim_op = NULL;
194     GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
195     return;
196   }
197   if (NULL == attr)
198   {
199     ret = 1;
200     return;
201   }
202   value_str = GNUNET_RECLAIM_ATTRIBUTE_value_to_string (attr->type,
203                                                         attr->data,
204                                                         attr->data_size);
205   GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
206               "%s: %s\n", attr->name, value_str);
207 }
208
209
210 static void
211 iter_error (void *cls)
212 {
213   attr_iterator = NULL;
214   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
215               "Failed to iterate over attributes\n");
216   GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
217 }
218
219 static void
220 timeout_task (void *cls)
221 {
222   timeout = NULL;
223   ret = 1;
224   GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
225               "Timeout\n");
226   GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
227 }
228
229 static void
230 process_rvk (void *cls, int success, const char* msg)
231 {
232   reclaim_op = NULL;
233   if (GNUNET_OK != success)
234   {
235     GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
236                 "Revocation failed.\n");
237     ret = 1;
238   }
239   GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
240 }
241
242 static void
243 iter_finished (void *cls)
244 {
245   struct GNUNET_RECLAIM_ATTRIBUTE_Claim *claim;
246   char *data;
247   size_t data_size;
248   int type;
249
250   attr_iterator = NULL;
251   if (list)
252   {
253     GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
254     return;
255   }
256
257   if (issue_attrs)
258   {
259     reclaim_op = GNUNET_RECLAIM_ticket_issue (reclaim_handle,
260                                               pkey,
261                                               &rp_key,
262                                               attr_list,
263                                               &ticket_issue_cb,
264                                               NULL);
265     return;
266   }
267   if (consume_ticket)
268   {
269     reclaim_op = GNUNET_RECLAIM_ticket_consume (reclaim_handle,
270                                                 pkey,
271                                                 &ticket,
272                                                 &process_attrs,
273                                                 NULL);
274     timeout = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 10),
275                                             &timeout_task,
276                                             NULL);
277     return;
278   }
279   if (revoke_ticket)
280   {
281     reclaim_op = GNUNET_RECLAIM_ticket_revoke (reclaim_handle,
282                                                pkey,
283                                                &ticket,
284                                                &process_rvk,
285                                                NULL);
286     return;
287   }
288   if (attr_name)
289   {
290     if (NULL == type_str)
291       type = GNUNET_RECLAIM_ATTRIBUTE_TYPE_STRING;
292     else
293       type = GNUNET_RECLAIM_ATTRIBUTE_typename_to_number (type_str);
294
295     GNUNET_assert (GNUNET_SYSERR != GNUNET_RECLAIM_ATTRIBUTE_string_to_value (type,
296                                                                               attr_value,
297                                                                               (void**)&data,
298                                                                               &data_size));
299     claim = GNUNET_RECLAIM_ATTRIBUTE_claim_new (attr_name,
300                                                  type,
301                                                  data,
302                                                  data_size);
303     reclaim_op = GNUNET_RECLAIM_attribute_store (reclaim_handle,
304                                                  pkey,
305                                                  claim,
306                                                  &exp_interval,
307                                                  &store_attr_cont,
308                                                  NULL);
309     return;
310   }
311   GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
312 }
313
314 static void
315 iter_cb (void *cls,
316          const struct GNUNET_CRYPTO_EcdsaPublicKey *identity,
317          const struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr)
318 {
319   struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le;
320   char *attrs_tmp;
321   char *attr_str;
322
323   if (issue_attrs)
324   {
325     attrs_tmp = GNUNET_strdup (issue_attrs);
326     attr_str = strtok (attrs_tmp, ",");
327     while (NULL != attr_str) {
328       if (0 != strcmp (attr_str, attr->name)) {
329         attr_str = strtok (NULL, ",");
330         continue;
331       }
332       le = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry);
333       le->claim = GNUNET_RECLAIM_ATTRIBUTE_claim_new (attr->name,
334                                                       attr->type,
335                                                       attr->data,
336                                                       attr->data_size);
337       GNUNET_CONTAINER_DLL_insert (attr_list->list_head,
338                                    attr_list->list_tail,
339                                    le);
340       break;
341     }
342     GNUNET_free (attrs_tmp);
343   } else if (list) {
344     GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
345                 "%s: %s\n", attr->name, (char*)attr->data);
346   }
347   GNUNET_RECLAIM_get_attributes_next (attr_iterator);
348 }
349
350 static void
351 ego_iter_finished (void *cls)
352 {
353   if (NULL == pkey)
354   {
355     GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
356                 "Ego %s not found\n", ego_name);
357     return;
358   }
359
360   if (NULL != rp)
361     GNUNET_CRYPTO_ecdsa_public_key_from_string (rp,
362                                                 strlen (rp),
363                                                 &rp_key);
364   if (NULL != consume_ticket)
365     GNUNET_STRINGS_string_to_data (consume_ticket,
366                                    strlen (consume_ticket),
367                                    &ticket,
368                                    sizeof (struct GNUNET_RECLAIM_Ticket));
369   if (NULL != revoke_ticket)
370     GNUNET_STRINGS_string_to_data (revoke_ticket,
371                                    strlen (revoke_ticket),
372                                    &ticket,
373                                    sizeof (struct GNUNET_RECLAIM_Ticket));
374
375
376   attr_list = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList);
377
378   attr_iterator = GNUNET_RECLAIM_get_attributes_start (reclaim_handle,
379                                                        pkey,
380                                                        &iter_error,
381                                                        NULL,
382                                                        &iter_cb,
383                                                        NULL,
384                                                        &iter_finished,
385                                                        NULL);
386
387
388 }
389
390 static int init = GNUNET_YES;
391
392 static void
393 ego_cb (void *cls,
394         struct GNUNET_IDENTITY_Ego *ego,
395         void **ctx,
396         const char *name)
397 {
398   if (NULL == name) {
399     if (GNUNET_YES == init) {
400       init = GNUNET_NO;
401       GNUNET_SCHEDULER_add_now (&ego_iter_finished, NULL);
402     }
403     return;
404   }
405   if (0 != strcmp (name, ego_name))
406     return;
407   pkey = GNUNET_IDENTITY_ego_get_private_key (ego);
408 }
409
410
411 static void
412 run (void *cls,
413      char *const *args,
414      const char *cfgfile,
415      const struct GNUNET_CONFIGURATION_Handle *c)
416 {
417   ret = 0;
418   if (NULL == ego_name)
419   {
420     ret = 1;
421     GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
422                 _("Ego is required\n"));
423     return;
424   }
425
426   if ( (NULL == attr_value) && (NULL != attr_name) )
427   {
428     ret = 1;
429     GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
430                 _("Attribute value missing!\n"));
431     return;
432   }
433
434   if ( (NULL == rp) && (NULL != issue_attrs) )
435   {
436     ret = 1;
437     GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
438                 _("Requesting party key is required!\n"));
439     return;
440   }
441
442   reclaim_handle = GNUNET_RECLAIM_connect (c);
443   //Get Ego
444   identity_handle = GNUNET_IDENTITY_connect (c,
445                                              &ego_cb,
446                                              NULL);
447
448
449 }
450
451
452 int
453 main(int argc, char *const argv[])
454 {
455   exp_interval = GNUNET_TIME_UNIT_HOURS;
456   struct GNUNET_GETOPT_CommandLineOption options[] = {
457
458     GNUNET_GETOPT_option_string ('a',
459                                  "add",
460                                  NULL,
461                                  gettext_noop ("Add attribute"),
462                                  &attr_name),
463
464     GNUNET_GETOPT_option_string ('V',
465                                  "value",
466                                  NULL,
467                                  gettext_noop ("Attribute value"),
468                                  &attr_value),
469     GNUNET_GETOPT_option_string ('e',
470                                  "ego",
471                                  NULL,
472                                  gettext_noop ("Ego"),
473                                  &ego_name),
474     GNUNET_GETOPT_option_string ('r',
475                                  "rp",
476                                  NULL,
477                                  gettext_noop ("Audience (relying party)"),
478                                  &rp),
479     GNUNET_GETOPT_option_flag ('D',
480                                "dump",
481                                gettext_noop ("List attributes for Ego"),
482                                &list),
483     GNUNET_GETOPT_option_string ('i',
484                                  "issue",
485                                  NULL,
486                                  gettext_noop ("Issue a ticket"),
487                                  &issue_attrs),
488     GNUNET_GETOPT_option_string ('C',
489                                  "consume",
490                                  NULL,
491                                  gettext_noop ("Consume a ticket"),
492                                  &consume_ticket),
493     GNUNET_GETOPT_option_string ('R',
494                                  "revoke",
495                                  NULL,
496                                  gettext_noop ("Revoke a ticket"),
497                                  &revoke_ticket),
498     GNUNET_GETOPT_option_string ('t',
499                                  "type",
500                                  NULL,
501                                  gettext_noop ("Type of attribute"),
502                                  &type_str),
503     GNUNET_GETOPT_option_relative_time ('E',
504                                         "expiration",
505                                         NULL,
506                                         gettext_noop ("Expiration interval of the attribute"),
507                                         &exp_interval),
508
509     GNUNET_GETOPT_OPTION_END
510   };
511   if (GNUNET_OK != GNUNET_PROGRAM_run (argc, argv, "ct",
512                                        "ct", options,
513                                        &run, NULL))
514     return 1;
515   else
516     return ret;
517 }