2 This file is part of GNUnet.
3 Copyright (C) 2012-2015 GNUnet e.V.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @author Martin Schanzenbach
22 * @file src/reclaim/gnunet-reclaim.c
23 * @brief Identity Provider utility
29 #include "gnunet_util_lib.h"
31 #include "gnunet_identity_service.h"
32 #include "gnunet_namestore_service.h"
33 #include "gnunet_reclaim_service.h"
34 #include "gnunet_signatures.h"
53 static char *attr_name;
58 static char *attr_value;
63 static char *issue_attrs;
68 static char *consume_ticket;
73 static char *type_str;
78 static char *revoke_ticket;
83 static int list_tickets;
88 static char *ego_name;
93 static struct GNUNET_IDENTITY_Handle *identity_handle;
98 static struct GNUNET_RECLAIM_Handle *reclaim_handle;
103 static struct GNUNET_RECLAIM_Operation *reclaim_op;
108 static struct GNUNET_RECLAIM_AttributeIterator *attr_iterator;
113 static struct GNUNET_RECLAIM_TicketIterator *ticket_iterator;
118 static struct GNUNET_CRYPTO_AbeMasterKey *abe_key;
123 static const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey;
128 static struct GNUNET_CRYPTO_EcdsaPublicKey rp_key;
133 static struct GNUNET_RECLAIM_Ticket ticket;
138 static struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attr_list;
141 * Attribute expiration interval
143 static struct GNUNET_TIME_Relative exp_interval;
148 static struct GNUNET_SCHEDULER_Task *timeout;
153 static struct GNUNET_SCHEDULER_Task *cleanup_task;
158 struct GNUNET_RECLAIM_ATTRIBUTE_Claim *claim;
160 static void do_cleanup (void *cls)
164 GNUNET_SCHEDULER_cancel (timeout);
165 if (NULL != reclaim_op)
166 GNUNET_RECLAIM_cancel (reclaim_op);
167 if (NULL != attr_iterator)
168 GNUNET_RECLAIM_get_attributes_stop (attr_iterator);
169 if (NULL != ticket_iterator)
170 GNUNET_RECLAIM_ticket_iteration_stop (ticket_iterator);
171 if (NULL != reclaim_handle)
172 GNUNET_RECLAIM_disconnect (reclaim_handle);
173 if (NULL != identity_handle)
174 GNUNET_IDENTITY_disconnect (identity_handle);
176 GNUNET_free (abe_key);
177 if (NULL != attr_list)
178 GNUNET_free (attr_list);
181 static void ticket_issue_cb (void *cls,
182 const struct GNUNET_RECLAIM_Ticket *ticket)
186 if (NULL != ticket) {
187 ticket_str = GNUNET_STRINGS_data_to_string_alloc (
188 ticket, sizeof (struct GNUNET_RECLAIM_Ticket));
189 printf ("%s\n", ticket_str);
190 GNUNET_free (ticket_str);
192 cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
195 static void store_attr_cont (void *cls, int32_t success, const char *emsg)
198 if (GNUNET_SYSERR == success) {
199 fprintf (stderr, "%s\n", emsg);
201 cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
204 static void process_attrs (void *cls,
205 const struct GNUNET_CRYPTO_EcdsaPublicKey *identity,
206 const struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr)
209 const char *attr_type;
211 if (NULL == identity) {
213 cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
220 value_str = GNUNET_RECLAIM_ATTRIBUTE_value_to_string (attr->type, attr->data,
222 attr_type = GNUNET_RECLAIM_ATTRIBUTE_number_to_typename (attr->type);
223 fprintf (stdout, "%s: %s [%s,v%u,id=%" PRIu64 "]\n", attr->name, value_str,
224 attr_type, attr->version, attr->id);
227 static void ticket_iter_err (void *cls)
229 ticket_iterator = NULL;
230 fprintf (stderr, "Failed to iterate over tickets\n");
231 cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
234 static void ticket_iter_fin (void *cls)
236 ticket_iterator = NULL;
237 cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
240 static void ticket_iter (void *cls, const struct GNUNET_RECLAIM_Ticket *ticket)
242 fprintf (stdout, "Found ticket\n");
243 GNUNET_RECLAIM_ticket_iteration_next (ticket_iterator);
246 static void iter_error (void *cls)
248 attr_iterator = NULL;
249 fprintf (stderr, "Failed to iterate over attributes\n");
250 cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
253 static void timeout_task (void *cls)
257 fprintf (stderr, "Timeout\n");
258 if (NULL == cleanup_task)
259 cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
262 static void process_rvk (void *cls, int success, const char *msg)
265 if (GNUNET_OK != success) {
266 fprintf (stderr, "Revocation failed.\n");
269 cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
272 static void iter_finished (void *cls)
278 attr_iterator = NULL;
280 cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
285 reclaim_op = GNUNET_RECLAIM_ticket_issue (
286 reclaim_handle, pkey, &rp_key, attr_list, &ticket_issue_cb, NULL);
289 if (consume_ticket) {
290 reclaim_op = GNUNET_RECLAIM_ticket_consume (reclaim_handle, pkey, &ticket,
291 &process_attrs, NULL);
292 timeout = GNUNET_SCHEDULER_add_delayed (
293 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
294 &timeout_task, NULL);
298 reclaim_op = GNUNET_RECLAIM_ticket_revoke (reclaim_handle, pkey, &ticket,
303 if (NULL == type_str)
304 type = GNUNET_RECLAIM_ATTRIBUTE_TYPE_STRING;
306 type = GNUNET_RECLAIM_ATTRIBUTE_typename_to_number (type_str);
308 GNUNET_assert (GNUNET_SYSERR !=
309 GNUNET_RECLAIM_ATTRIBUTE_string_to_value (
310 type, attr_value, (void **)&data, &data_size));
314 claim->data_size = data_size;
317 GNUNET_RECLAIM_ATTRIBUTE_claim_new (attr_name, type, data, data_size);
319 reclaim_op = GNUNET_RECLAIM_attribute_store (
320 reclaim_handle, pkey, claim, &exp_interval, &store_attr_cont, NULL);
325 cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
328 static void iter_cb (void *cls,
329 const struct GNUNET_CRYPTO_EcdsaPublicKey *identity,
330 const struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr)
332 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le;
335 const char *attr_type;
337 if ((NULL != attr_name) && (NULL != claim)) {
338 if (0 == strcasecmp (attr_name, attr->name)) {
339 claim = GNUNET_RECLAIM_ATTRIBUTE_claim_new (attr->name, attr->type,
340 attr->data, attr->data_size);
342 } else if (issue_attrs) {
343 attrs_tmp = GNUNET_strdup (issue_attrs);
344 attr_str = strtok (attrs_tmp, ",");
345 while (NULL != attr_str) {
346 if (0 != strcasecmp (attr_str, attr->name)) {
347 attr_str = strtok (NULL, ",");
350 le = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry);
351 le->claim = GNUNET_RECLAIM_ATTRIBUTE_claim_new (
352 attr->name, attr->type, attr->data, attr->data_size);
353 le->claim->version = attr->version;
354 le->claim->id = attr->id;
355 GNUNET_CONTAINER_DLL_insert (attr_list->list_head, attr_list->list_tail,
359 GNUNET_free (attrs_tmp);
361 attr_str = GNUNET_RECLAIM_ATTRIBUTE_value_to_string (attr->type, attr->data,
363 attr_type = GNUNET_RECLAIM_ATTRIBUTE_number_to_typename (attr->type);
364 fprintf (stdout, "%s: %s [%s,v%u,id=%" PRIu64 "]\n", attr->name, attr_str,
365 attr_type, attr->version, attr->id);
367 GNUNET_RECLAIM_get_attributes_next (attr_iterator);
370 static void start_process ()
373 fprintf (stderr, "Ego %s not found\n", ego_name);
374 cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
379 ticket_iterator = GNUNET_RECLAIM_ticket_iteration_start (
380 reclaim_handle, pkey, &ticket_iter_err, NULL, &ticket_iter, NULL,
381 &ticket_iter_fin, NULL);
386 GNUNET_CRYPTO_ecdsa_public_key_from_string (rp, strlen (rp), &rp_key);
387 if (NULL != consume_ticket)
388 GNUNET_STRINGS_string_to_data (consume_ticket, strlen (consume_ticket),
390 sizeof (struct GNUNET_RECLAIM_Ticket));
391 if (NULL != revoke_ticket)
392 GNUNET_STRINGS_string_to_data (revoke_ticket, strlen (revoke_ticket),
394 sizeof (struct GNUNET_RECLAIM_Ticket));
396 attr_list = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList);
398 attr_iterator = GNUNET_RECLAIM_get_attributes_start (
399 reclaim_handle, pkey, &iter_error, NULL, &iter_cb, NULL, &iter_finished,
403 static int init = GNUNET_YES;
405 static void ego_cb (void *cls, struct GNUNET_IDENTITY_Ego *ego, void **ctx,
409 if (GNUNET_YES == init) {
415 if (0 != strcmp (name, ego_name))
417 pkey = GNUNET_IDENTITY_ego_get_private_key (ego);
421 static void run (void *cls, char *const *args, const char *cfgfile,
422 const struct GNUNET_CONFIGURATION_Handle *c)
425 if (NULL == ego_name) {
427 fprintf (stderr, _ ("Ego is required\n"));
431 if ((NULL == attr_value) && (NULL != attr_name)) {
433 fprintf (stderr, _ ("Attribute value missing!\n"));
437 if ((NULL == rp) && (NULL != issue_attrs)) {
439 fprintf (stderr, _ ("Requesting party key is required!\n"));
443 reclaim_handle = GNUNET_RECLAIM_connect (c);
445 identity_handle = GNUNET_IDENTITY_connect (c, &ego_cb, NULL);
449 int main (int argc, char *const argv[])
451 exp_interval = GNUNET_TIME_UNIT_HOURS;
452 struct GNUNET_GETOPT_CommandLineOption options[] = {
454 GNUNET_GETOPT_option_string ('a', "add", "NAME",
455 gettext_noop ("Add an attribute NAME"),
458 GNUNET_GETOPT_option_string ('V', "value", "VALUE",
459 gettext_noop ("The attribute VALUE"),
461 GNUNET_GETOPT_option_string ('e', "ego", "EGO",
462 gettext_noop ("The EGO to use"), &ego_name),
463 GNUNET_GETOPT_option_string (
464 'r', "rp", "RP", gettext_noop ("Specify the relying party for issue"),
466 GNUNET_GETOPT_option_flag (
467 'D', "dump", gettext_noop ("List attributes for EGO"), &list),
468 GNUNET_GETOPT_option_string (
469 'i', "issue", "A1,A2,...",
471 "Issue a ticket for a set of attributes separated by comma"),
473 GNUNET_GETOPT_option_string ('C', "consume", "TICKET",
474 gettext_noop ("Consume a ticket"),
476 GNUNET_GETOPT_option_string ('R', "revoke", "TICKET",
477 gettext_noop ("Revoke a ticket"),
479 GNUNET_GETOPT_option_string (
480 't', "type", "TYPE", gettext_noop ("Type of attribute"), &type_str),
481 GNUNET_GETOPT_option_flag (
482 'T', "tickets", gettext_noop ("List tickets of ego"), &list_tickets),
483 GNUNET_GETOPT_option_relative_time (
484 'E', "expiration", "INTERVAL",
485 gettext_noop ("Expiration interval of the attribute"), &exp_interval),
487 GNUNET_GETOPT_OPTION_END};
489 GNUNET_PROGRAM_run (argc, argv, "ct", "ct", options, &run, NULL))