-remove deprecated
[oweals/gnunet.git] / src / identity-provider / gnunet-idp.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
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., 51 Franklin Street, Fifth Floor,
18    Boston, MA 02110-1301, USA.
19    */
20 /**
21  * @author Martin Schanzenbach
22  * @file src/identity-provider/gnunet-idp.c
23  * @brief Identity Provider utility
24  *
25  */
26
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_namestore_service.h"
30 #include "gnunet_identity_provider_service.h"
31 #include "gnunet_identity_service.h"
32 #include "gnunet_signatures.h"
33
34 /**
35  * List attribute flag
36  */
37 static int list;
38
39 /**
40  * Relying party
41  */
42 static char* rp;
43
44 /**
45  * The attribute
46  */
47 static char* attr_name;
48
49 /**
50  * Attribute value
51  */
52 static char* attr_value;
53
54 /**
55  * Attributes to issue
56  */
57 static char* issue_attrs;
58
59 /**
60  * Ticket to consume
61  */
62 static char* consume_ticket;
63
64 /**
65  * Ego name
66  */
67 static char* ego_name;
68
69 /**
70  * Identity handle
71  */
72 static struct GNUNET_IDENTITY_Handle *identity_handle;
73
74 /**
75  * IdP handle
76  */
77 static struct GNUNET_IDENTITY_PROVIDER_Handle *idp_handle;
78
79 /**
80  * IdP operation
81  */
82 static struct GNUNET_IDENTITY_PROVIDER_Operation *idp_op;
83
84 /**
85  * Attribute iterator
86  */
87 static struct GNUNET_IDENTITY_PROVIDER_AttributeIterator *attr_iterator;
88
89 /**
90  * Master ABE key
91  */
92 static struct GNUNET_CRYPTO_AbeMasterKey *abe_key;
93
94 /**
95  * ego private key
96  */
97 static const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey;
98
99 /**
100  * rp public key
101  */
102 static struct GNUNET_CRYPTO_EcdsaPublicKey rp_key;
103
104 /**
105  * Ticket to consume
106  */
107 static struct GNUNET_IDENTITY_PROVIDER_Ticket ticket;
108
109 /**
110  * Attribute list
111  */
112 static struct GNUNET_IDENTITY_PROVIDER_AttributeList *attr_list;
113
114 static void
115 do_cleanup(void *cls)
116 {
117   if (NULL != attr_iterator)
118     GNUNET_IDENTITY_PROVIDER_get_attributes_stop (attr_iterator);
119   if (NULL != idp_handle)
120     GNUNET_IDENTITY_PROVIDER_disconnect (idp_handle);
121   if (NULL != identity_handle)
122     GNUNET_IDENTITY_disconnect (identity_handle);
123   if (NULL != abe_key)
124     GNUNET_free (abe_key);
125   if (NULL != attr_list)
126     GNUNET_free (attr_list);
127 }
128
129 static void
130 ticket_issue_cb (void* cls,
131                  const struct GNUNET_IDENTITY_PROVIDER_Ticket *ticket)
132 {
133   char* ticket_str;
134   if (NULL != ticket) {
135     ticket_str = GNUNET_STRINGS_data_to_string_alloc (ticket,
136                                                       sizeof (struct GNUNET_IDENTITY_PROVIDER_Ticket));
137     printf("%s\n",
138            ticket_str);
139     GNUNET_free (ticket_str);
140   }
141   GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
142 }
143
144 static void
145 store_attr_cont (void *cls,
146                  int32_t success,
147                  const char*emsg)
148 {
149   if (GNUNET_SYSERR == success) {
150     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
151                 "%s\n", emsg);
152   } else {
153     GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
154                 "Successfully added identity attribute %s=%s\n",
155                 attr_name, attr_value);
156   }
157   GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
158 }
159
160 static void
161 process_attrs (void *cls,
162          const struct GNUNET_CRYPTO_EcdsaPublicKey *identity,
163          const struct GNUNET_IDENTITY_PROVIDER_Attribute *attr)
164 {
165   if (NULL == identity)
166   {
167     GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
168     return;
169   }
170   GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
171               "%s: %s\n", attr->name, (char*)attr->data);
172 }
173
174
175 static void
176 iter_error (void *cls)
177 {
178   attr_iterator = NULL;
179   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
180               "Failed to iterate over attributes\n");
181   GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
182 }
183
184 static void
185 iter_finished (void *cls)
186 {
187   struct GNUNET_IDENTITY_PROVIDER_Attribute *attr;
188
189   attr_iterator = NULL;
190   if (list) {
191     GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
192     return;
193   }
194
195   if (issue_attrs) {
196     idp_op = GNUNET_IDENTITY_PROVIDER_idp_ticket_issue (idp_handle,
197                                                         pkey,
198                                                         &rp_key,
199                                                         attr_list,
200                                                         &ticket_issue_cb,
201                                                         NULL);
202     return;
203   }
204   if (consume_ticket) {
205     idp_op = GNUNET_IDENTITY_PROVIDER_rp_ticket_consume (idp_handle,
206                                                          pkey,
207                                                          &ticket,
208                                                          &process_attrs,
209                                                          NULL);
210     return;
211   }
212   attr = GNUNET_IDENTITY_PROVIDER_attribute_new (attr_name,
213                                                  GNUNET_IDENTITY_PROVIDER_AT_STRING,
214                                                  attr_value,
215                                                  strlen (attr_value));
216   idp_op = GNUNET_IDENTITY_PROVIDER_attribute_store (idp_handle,
217                                                      pkey,
218                                                      attr,
219                                                      &store_attr_cont,
220                                                      NULL);
221
222
223 }
224
225 static void
226 iter_cb (void *cls,
227          const struct GNUNET_CRYPTO_EcdsaPublicKey *identity,
228          const struct GNUNET_IDENTITY_PROVIDER_Attribute *attr)
229 {
230   struct GNUNET_IDENTITY_PROVIDER_AttributeListEntry *le;
231   char *attrs_tmp;
232   char *attr_str;
233
234   if (issue_attrs)
235   {
236     attrs_tmp = GNUNET_strdup (issue_attrs);
237     attr_str = strtok (attrs_tmp, ",");
238     while (NULL != attr_str) {
239       if (0 != strcmp (attr_str, attr->name)) {
240         attr_str = strtok (NULL, ",");
241         continue;
242       }
243       le = GNUNET_new (struct GNUNET_IDENTITY_PROVIDER_AttributeListEntry);
244       le->attribute = GNUNET_IDENTITY_PROVIDER_attribute_new (attr->name,
245                                                               attr->attribute_type,
246                                                               attr->data,
247                                                               attr->data_size);
248       GNUNET_CONTAINER_DLL_insert (attr_list->list_head,
249                                    attr_list->list_tail,
250                                    le);
251       break;
252     }
253     GNUNET_free (attrs_tmp);
254   } else if (list) {
255     GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
256                 "%s: %s\n", attr->name, (char*)attr->data);
257   }
258   GNUNET_IDENTITY_PROVIDER_get_attributes_next (attr_iterator);
259 }
260
261 static void
262 ego_cb (void *cls,
263         struct GNUNET_IDENTITY_Ego *ego,
264         void **ctx,
265         const char *name)
266 {
267   if (NULL == name)
268     return;
269   if (0 != strcmp (name, ego_name))
270     return;
271   pkey = GNUNET_IDENTITY_ego_get_private_key (ego);
272
273   if (NULL != rp)
274     GNUNET_CRYPTO_ecdsa_public_key_from_string (rp,
275                                                 strlen (rp),
276                                                 &rp_key);
277   if (NULL != consume_ticket)
278     GNUNET_STRINGS_string_to_data (consume_ticket,
279                                    strlen (consume_ticket),
280                                    &ticket,
281                                    sizeof (struct GNUNET_IDENTITY_PROVIDER_Ticket));
282
283   attr_list = GNUNET_new (struct GNUNET_IDENTITY_PROVIDER_AttributeList);
284
285   attr_iterator = GNUNET_IDENTITY_PROVIDER_get_attributes_start (idp_handle,
286                                                                  pkey,
287                                                                  &iter_error,
288                                                                  NULL,
289                                                                  &iter_cb,
290                                                                  NULL,
291                                                                  &iter_finished,
292                                                                  NULL);
293
294
295 }
296
297 static void
298 run (void *cls,
299      char *const *args,
300      const char *cfgfile,
301      const struct GNUNET_CONFIGURATION_Handle *c)
302 {
303
304   if (NULL == ego_name)
305   {
306     GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
307                 _("Ego is required\n"));
308     return;
309   } 
310
311   idp_handle = GNUNET_IDENTITY_PROVIDER_connect (c);
312   //Get Ego
313   identity_handle = GNUNET_IDENTITY_connect (c,
314                                              &ego_cb,
315                                              NULL);
316
317
318 }
319
320
321 int
322 main(int argc, char *const argv[])
323 {
324   struct GNUNET_GETOPT_CommandLineOption options[] = {
325
326     GNUNET_GETOPT_option_string ('a',
327                                  "add",
328                                  NULL,
329                                  gettext_noop ("Add attribute"),
330                                  &attr_name),
331
332     GNUNET_GETOPT_option_string ('V',
333                                  "value",
334                                  NULL,
335                                  gettext_noop ("Attribute value"),
336                                  &attr_value),
337     GNUNET_GETOPT_option_string ('e',
338                                  "ego",
339                                  NULL,
340                                  gettext_noop ("Ego"),
341                                  &ego_name),
342     GNUNET_GETOPT_option_string ('r',
343                                  "rp",
344                                  NULL,
345                                  gettext_noop ("Audience (relying party)"),
346                                  &rp),
347     GNUNET_GETOPT_option_flag ('D',
348                                "dump",
349                                gettext_noop ("List attributes for Ego"),
350                                &list),
351     GNUNET_GETOPT_option_string ('i',
352                                  "issue",
353                                  NULL,
354                                  gettext_noop ("Issue a ticket"),
355                                  &issue_attrs),
356     GNUNET_GETOPT_option_string ('C',
357                                  "consume",
358                                  NULL,
359                                  gettext_noop ("Consume a ticket"),
360                                  &consume_ticket),
361     GNUNET_GETOPT_OPTION_END
362   };
363   return GNUNET_PROGRAM_run (argc, argv, "ct",
364                              "ct", options,
365                              &run, NULL);
366 }