RECLAIM: forgot conf file for tests
[oweals/gnunet.git] / src / reclaim / plugin_gnsrecord_reclaim.c
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2013, 2014 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 /**
20  * @file reclaim/plugin_gnsrecord_reclaim.c
21  * @brief gnsrecord plugin to provide the API for identity records
22  * @author Martin Schanzenbach
23  */
24 #include "platform.h"
25 #include "gnunet_util_lib.h"
26 #include "gnunet_gnsrecord_lib.h"
27 #include "gnunet_gnsrecord_plugin.h"
28
29
30 /**
31  * Convert the 'value' of a record to a string.
32  *
33  * @param cls closure, unused
34  * @param type type of the record
35  * @param data value in binary encoding
36  * @param data_size number of bytes in @a data
37  * @return NULL on error, otherwise human-readable representation of the value
38  */
39 static char *
40 value_to_string (void *cls,
41                  uint32_t type,
42                  const void *data,
43                  size_t data_size)
44 {
45   const struct GNUNET_CRYPTO_EcdhePrivateKey *ecdhe_privkey;
46   const struct GNUNET_CRYPTO_EcdsaPublicKey *audience_pubkey;
47   const char *scopes;
48   char *ecdhe_str;
49   char *aud_str;
50   char *result;
51
52   switch (type)
53   {
54     case GNUNET_GNSRECORD_TYPE_ID_ATTR:
55       return GNUNET_STRINGS_data_to_string_alloc (data, data_size);
56     case GNUNET_GNSRECORD_TYPE_ID_TOKEN: //DEPRECATED
57     case GNUNET_GNSRECORD_TYPE_RECLAIM_OIDC_REDIRECT:
58     case GNUNET_GNSRECORD_TYPE_RECLAIM_OIDC_CLIENT:
59       return GNUNET_strndup (data, data_size);
60     case GNUNET_GNSRECORD_TYPE_ABE_KEY:
61     case GNUNET_GNSRECORD_TYPE_ABE_MASTER:
62       return GNUNET_STRINGS_data_to_string_alloc (data, data_size); 
63     case GNUNET_GNSRECORD_TYPE_ID_TOKEN_METADATA: //DEPRECATED
64         ecdhe_privkey = data;
65         audience_pubkey = data+sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey);
66         scopes =  (char*) audience_pubkey+(sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
67         ecdhe_str = GNUNET_STRINGS_data_to_string_alloc (ecdhe_privkey,
68                                                         sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey));
69         aud_str = GNUNET_STRINGS_data_to_string_alloc (audience_pubkey,
70                                                        sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
71         GNUNET_asprintf (&result,
72                          "%s;%s;%s",
73                          ecdhe_str, aud_str, scopes);
74         GNUNET_free (aud_str);
75         GNUNET_free (ecdhe_str);
76         return result;
77
78     default:
79       return NULL;
80   }
81 }
82
83
84 /**
85  * Convert human-readable version of a 'value' of a record to the binary
86  * representation.
87  *
88  * @param cls closure, unused
89  * @param type type of the record
90  * @param s human-readable string
91  * @param data set to value in binary encoding (will be allocated)
92  * @param data_size set to number of bytes in @a data
93  * @return #GNUNET_OK on success
94  */
95 static int
96 string_to_value (void *cls,
97                  uint32_t type,
98                  const char *s,
99                  void **data,
100                  size_t *data_size)
101 {
102   char* ecdhe_str;
103   char* aud_keystr;
104   char* write_ptr;
105   char* tmp_tok;
106   char* str;
107
108   if (NULL == s)
109     return GNUNET_SYSERR;
110   switch (type)
111   {
112     case GNUNET_GNSRECORD_TYPE_ID_ATTR:
113       return GNUNET_STRINGS_string_to_data (s,
114                                             strlen (s),
115                                             *data,
116                                             *data_size);
117     case GNUNET_GNSRECORD_TYPE_ID_TOKEN:
118     case GNUNET_GNSRECORD_TYPE_RECLAIM_OIDC_REDIRECT:
119     case GNUNET_GNSRECORD_TYPE_RECLAIM_OIDC_CLIENT:
120       *data = GNUNET_strdup (s);
121       *data_size = strlen (s);
122       return GNUNET_OK;
123     case GNUNET_GNSRECORD_TYPE_ABE_KEY:
124     case GNUNET_GNSRECORD_TYPE_ABE_MASTER:
125       return GNUNET_STRINGS_string_to_data (s,
126                                             strlen (s),
127                                             *data,
128                                             *data_size);
129     case GNUNET_GNSRECORD_TYPE_ID_TOKEN_METADATA:
130       tmp_tok = GNUNET_strdup (s);
131       ecdhe_str = strtok (tmp_tok, ";");
132       if (NULL == ecdhe_str)
133       {
134         GNUNET_free (tmp_tok);
135         return GNUNET_SYSERR;
136       }
137       aud_keystr = strtok (NULL, ";");
138       if (NULL == aud_keystr)
139       {
140         GNUNET_free (tmp_tok);
141         return GNUNET_SYSERR;
142       }
143       str = strtok (NULL, ";");
144       if (NULL == str)
145       {
146         GNUNET_free (tmp_tok);
147         return GNUNET_SYSERR;
148       }
149       *data_size = strlen (str) + 1
150         +sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey)
151         +sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
152       *data = GNUNET_malloc (*data_size);
153
154       write_ptr = *data;
155       GNUNET_STRINGS_string_to_data (ecdhe_str,
156                                      strlen (ecdhe_str),
157                                      write_ptr,
158                                      sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey));
159       write_ptr += sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey);
160       GNUNET_STRINGS_string_to_data (aud_keystr,
161                                      strlen (aud_keystr),
162                                      write_ptr,
163                                      sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
164       write_ptr += sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
165       GNUNET_memcpy (write_ptr, str, strlen (str) + 1); //with 0-Terminator
166       GNUNET_free (tmp_tok);
167       return GNUNET_OK;
168
169     default:
170       return GNUNET_SYSERR;
171   }
172 }
173
174
175 /**
176  * Mapping of record type numbers to human-readable
177  * record type names.
178  */
179 static struct {
180   const char *name;
181   uint32_t number;
182 } name_map[] = {
183   { "ID_ATTR", GNUNET_GNSRECORD_TYPE_ID_ATTR },
184   { "ID_TOKEN", GNUNET_GNSRECORD_TYPE_ID_TOKEN },
185   { "ABE_KEY", GNUNET_GNSRECORD_TYPE_ABE_KEY },
186   { "ABE_MASTER", GNUNET_GNSRECORD_TYPE_ABE_MASTER },
187   { "ID_TOKEN_METADATA", GNUNET_GNSRECORD_TYPE_ID_TOKEN_METADATA },
188   { "RECLAIM_OIDC_CLIENT", GNUNET_GNSRECORD_TYPE_RECLAIM_OIDC_CLIENT },
189   { "RECLAIM_OIDC_REDIRECT", GNUNET_GNSRECORD_TYPE_RECLAIM_OIDC_REDIRECT },
190   { NULL, UINT32_MAX }
191 };
192
193
194 /**
195  * Convert a type name (i.e. "AAAA") to the corresponding number.
196  *
197  * @param cls closure, unused
198  * @param dns_typename name to convert
199  * @return corresponding number, UINT32_MAX on error
200  */
201 static uint32_t
202 typename_to_number (void *cls,
203                     const char *dns_typename)
204 {
205   unsigned int i;
206
207   i=0;
208   while ( (NULL != name_map[i].name) &&
209           (0 != strcasecmp (dns_typename, name_map[i].name)) )
210     i++;
211   return name_map[i].number;
212 }
213
214
215 /**
216  * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
217  *
218  * @param cls closure, unused
219  * @param type number of a type to convert
220  * @return corresponding typestring, NULL on error
221  */
222 static const char *
223 number_to_typename (void *cls,
224                     uint32_t type)
225 {
226   unsigned int i;
227
228   i=0;
229   while ( (NULL != name_map[i].name) &&
230           (type != name_map[i].number) )
231     i++;
232   return name_map[i].name;
233 }
234
235
236 /**
237  * Entry point for the plugin.
238  *
239  * @param cls NULL
240  * @return the exported block API
241  */
242 void *
243 libgnunet_plugin_gnsrecord_reclaim_init (void *cls)
244 {
245   struct GNUNET_GNSRECORD_PluginFunctions *api;
246
247   api = GNUNET_new (struct GNUNET_GNSRECORD_PluginFunctions);
248   api->value_to_string = &value_to_string;
249   api->string_to_value = &string_to_value;
250   api->typename_to_number = &typename_to_number;
251   api->number_to_typename = &number_to_typename;
252   return api;
253 }
254
255
256 /**
257  * Exit point from the plugin.
258  *
259  * @param cls the return value from #libgnunet_plugin_block_test_init
260  * @return NULL
261  */
262 void *
263 libgnunet_plugin_gnsrecord_reclaim_done (void *cls)
264 {
265   struct GNUNET_GNSRECORD_PluginFunctions *api = cls;
266
267   GNUNET_free (api);
268   return NULL;
269 }
270
271 /* end of plugin_gnsrecord_dns.c */