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