error handling
[oweals/gnunet.git] / src / gns / plugin_gnsrecord_gns.c
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2013, 2014, 2016 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      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20
21 /**
22  * @file gns/plugin_gnsrecord_gns.c
23  * @brief gnsrecord plugin to provide the API for fundamental GNS records
24  *                  This includes the VPN record because GNS resolution
25  *                  is expected to understand VPN records and (if needed)
26  *                  map the result to A/AAAA.
27  * @author Christian Grothoff
28  */
29 #include "platform.h"
30 #include "gnunet_util_lib.h"
31 #include "gnunet_gnsrecord_lib.h"
32 #include "gnunet_dnsparser_lib.h"
33 #include "gnunet_gnsrecord_plugin.h"
34 #include <inttypes.h>
35
36
37 /**
38  * Convert the 'value' of a record to a string.
39  *
40  * @param cls closure, unused
41  * @param type type of the record
42  * @param data value in binary encoding
43  * @param data_size number of bytes in @a data
44  * @return NULL on error, otherwise human-readable representation of the value
45  */
46 static char *
47 gns_value_to_string (void *cls,
48                      uint32_t type,
49                      const void *data,
50                      size_t data_size)
51 {
52   const char *cdata;
53
54   switch (type)
55   {
56   case GNUNET_GNSRECORD_TYPE_PKEY:
57     if (data_size != sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey))
58       return NULL;
59     return GNUNET_CRYPTO_ecdsa_public_key_to_string (data);
60
61   case GNUNET_GNSRECORD_TYPE_NICK:
62     return GNUNET_strndup (data, data_size);
63
64   case GNUNET_GNSRECORD_TYPE_LEHO:
65     return GNUNET_strndup (data, data_size);
66
67   case GNUNET_GNSRECORD_TYPE_GNS2DNS: {
68       char *ns;
69       char *ip;
70       size_t off;
71       char *nstr;
72
73       off = 0;
74       ns = GNUNET_DNSPARSER_parse_name (data, data_size, &off);
75       if (NULL == ns)
76       {
77         GNUNET_break_op (0);
78         GNUNET_free_non_null (ns);
79         return NULL;
80       }
81       /* DNS server IP/name must be UTF-8 */
82       ip = GNUNET_strdup (&((const char*) data)[off]);
83       GNUNET_asprintf (&nstr, "%s@%s", ns, ip);
84       GNUNET_free_non_null (ns);
85       GNUNET_free_non_null (ip);
86       return nstr;
87     }
88
89   case GNUNET_GNSRECORD_TYPE_VPN: {
90       struct GNUNET_TUN_GnsVpnRecord vpn;
91       char *vpn_str;
92
93       cdata = data;
94       if ((data_size <= sizeof(vpn)) || ('\0' != cdata[data_size - 1]))
95         return NULL; /* malformed */
96       /* need to memcpy for alignment */
97       GNUNET_memcpy (&vpn, data, sizeof(vpn));
98       GNUNET_asprintf (&vpn_str,
99                        "%u %s %s",
100                        (unsigned int) ntohs (vpn.proto),
101                        (const char *) GNUNET_i2s_full (&vpn.peer),
102                        (const char *) &cdata[sizeof(vpn)]);
103       return vpn_str;
104     }
105
106   case GNUNET_GNSRECORD_TYPE_BOX: {
107       struct GNUNET_GNSRECORD_BoxRecord box;
108       uint32_t rt;
109       char *box_str;
110       char *ival;
111
112       cdata = data;
113       if (data_size < sizeof(struct GNUNET_GNSRECORD_BoxRecord))
114         return NULL; /* malformed */
115       GNUNET_memcpy (&box, data, sizeof(box));
116       rt = ntohl (box.record_type);
117       ival = GNUNET_GNSRECORD_value_to_string (rt,
118                                                &cdata[sizeof(box)],
119                                                data_size - sizeof(box));
120       if (NULL == ival)
121         return NULL; /* malformed */
122       GNUNET_asprintf (&box_str,
123                        "%u %u %u %s",
124                        (unsigned int) ntohs (box.protocol),
125                        (unsigned int) ntohs (box.service),
126                        (unsigned int) rt,
127                        ival);
128       GNUNET_free (ival);
129       return box_str;
130     }
131
132   default:
133     return NULL;
134   }
135 }
136
137
138 /**
139  * Convert human-readable version of a 'value' of a record to the binary
140  * representation.
141  *
142  * @param cls closure, unused
143  * @param type type of the record
144  * @param s human-readable string
145  * @param data set to value in binary encoding (will be allocated)
146  * @param data_size set to number of bytes in @a data
147  * @return #GNUNET_OK on success
148  */
149 static int
150 gns_string_to_value (void *cls,
151                      uint32_t type,
152                      const char *s,
153                      void **data,
154                      size_t *data_size)
155 {
156   struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
157
158   if (NULL == s)
159     return GNUNET_SYSERR;
160   switch (type)
161   {
162   case GNUNET_GNSRECORD_TYPE_PKEY:
163     if (GNUNET_OK !=
164         GNUNET_CRYPTO_ecdsa_public_key_from_string (s, strlen (s), &pkey))
165     {
166       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
167                   _ ("Unable to parse PKEY record `%s'\n"),
168                   s);
169       return GNUNET_SYSERR;
170     }
171     *data = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPublicKey);
172     GNUNET_memcpy (*data, &pkey, sizeof(pkey));
173     *data_size = sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey);
174     return GNUNET_OK;
175
176   case GNUNET_GNSRECORD_TYPE_NICK:
177     *data = GNUNET_strdup (s);
178     *data_size = strlen (s);
179     return GNUNET_OK;
180
181   case GNUNET_GNSRECORD_TYPE_LEHO:
182     *data = GNUNET_strdup (s);
183     *data_size = strlen (s);
184     return GNUNET_OK;
185
186   case GNUNET_GNSRECORD_TYPE_GNS2DNS: {
187       char nsbuf[514];
188       char *cpy;
189       char *at;
190       size_t off;
191
192       cpy = GNUNET_strdup (s);
193       at = strchr (cpy, '@');
194       if (NULL == at)
195       {
196         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
197                     _ ("Unable to parse GNS2DNS record `%s'\n"),
198                     s);
199         GNUNET_free (cpy);
200         return GNUNET_SYSERR;
201       }
202       *at = '\0';
203       at++;
204
205       off = 0;
206       if (GNUNET_OK != GNUNET_DNSPARSER_builder_add_name (nsbuf,
207                                                           sizeof(nsbuf),
208                                                           &off,
209                                                           cpy))
210       {
211         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
212                     _ (
213                       "Failed to serialize GNS2DNS record with value `%s': Not a DNS name.\n"),
214                     s);
215         GNUNET_free (cpy);
216         return GNUNET_SYSERR;
217       }
218       /* The DNS server location/name is in UTF-8 */
219       GNUNET_memcpy (&nsbuf[off], at, strlen (at) + 1);
220       off += strlen (at) + 1;
221       GNUNET_free (cpy);
222       *data_size = off;
223       *data = GNUNET_malloc (off);
224       GNUNET_memcpy (*data, nsbuf, off);
225       return GNUNET_OK;
226     }
227
228   case GNUNET_GNSRECORD_TYPE_VPN: {
229       struct GNUNET_TUN_GnsVpnRecord *vpn;
230       char s_peer[103 + 1];
231       char s_serv[253 + 1];
232       unsigned int proto;
233
234       if (3 != sscanf (s, "%u %103s %253s", &proto, s_peer, s_serv))
235       {
236         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
237                     _ ("Unable to parse VPN record string `%s'\n"),
238                     s);
239         return GNUNET_SYSERR;
240       }
241       *data_size = sizeof(struct GNUNET_TUN_GnsVpnRecord) + strlen (s_serv) + 1;
242       *data = vpn = GNUNET_malloc (*data_size);
243       if (GNUNET_OK !=
244           GNUNET_CRYPTO_eddsa_public_key_from_string ((char *) s_peer,
245                                                       strlen (s_peer),
246                                                       &vpn->peer.public_key))
247       {
248         GNUNET_free (vpn);
249         *data_size = 0;
250         return GNUNET_SYSERR;
251       }
252       vpn->proto = htons ((uint16_t) proto);
253       strcpy ((char *) &vpn[1], s_serv);
254       return GNUNET_OK;
255     }
256
257   case GNUNET_GNSRECORD_TYPE_BOX: {
258       struct GNUNET_GNSRECORD_BoxRecord *box;
259       size_t rest;
260       unsigned int protocol;
261       unsigned int service;
262       unsigned int record_type;
263       void *bval;
264       size_t bval_size;
265
266       if (3 != sscanf (s, "%u %u %u ", &protocol, &service, &record_type))
267       {
268         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
269                     _ ("Unable to parse BOX record string `%s'\n"),
270                     s);
271         return GNUNET_SYSERR;
272       }
273       rest = snprintf (NULL, 0, "%u %u %u ", protocol, service, record_type);
274       if (GNUNET_OK != GNUNET_GNSRECORD_string_to_value (record_type,
275                                                          &s[rest],
276                                                          &bval,
277                                                          &bval_size))
278         return GNUNET_SYSERR;
279       *data_size = sizeof(struct GNUNET_GNSRECORD_BoxRecord) + bval_size;
280       *data = box = GNUNET_malloc (*data_size);
281       box->protocol = htons (protocol);
282       box->service = htons (service);
283       box->record_type = htonl (record_type);
284       GNUNET_memcpy (&box[1], bval, bval_size);
285       GNUNET_free (bval);
286       return GNUNET_OK;
287     }
288
289   default:
290     return GNUNET_SYSERR;
291   }
292 }
293
294
295 /**
296  * Mapping of record type numbers to human-readable
297  * record type names.
298  */
299 static struct
300 {
301   const char *name;
302   uint32_t number;
303 } gns_name_map[] = { { "PKEY", GNUNET_GNSRECORD_TYPE_PKEY },
304                      { "NICK", GNUNET_GNSRECORD_TYPE_NICK },
305                      { "LEHO", GNUNET_GNSRECORD_TYPE_LEHO },
306                      { "VPN", GNUNET_GNSRECORD_TYPE_VPN },
307                      { "GNS2DNS", GNUNET_GNSRECORD_TYPE_GNS2DNS },
308                      { "BOX", GNUNET_GNSRECORD_TYPE_BOX },
309                      { NULL, UINT32_MAX } };
310
311
312 /**
313  * Convert a type name (i.e. "AAAA") to the corresponding number.
314  *
315  * @param cls closure, unused
316  * @param gns_typename name to convert
317  * @return corresponding number, UINT32_MAX on error
318  */
319 static uint32_t
320 gns_typename_to_number (void *cls, const char *gns_typename)
321 {
322   unsigned int i;
323
324   i = 0;
325   while ((NULL != gns_name_map[i].name) &&
326          (0 != strcasecmp (gns_typename, gns_name_map[i].name)))
327     i++;
328   return gns_name_map[i].number;
329 }
330
331
332 /**
333  * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
334  *
335  * @param cls closure, unused
336  * @param type number of a type to convert
337  * @return corresponding typestring, NULL on error
338  */
339 static const char *
340 gns_number_to_typename (void *cls, uint32_t type)
341 {
342   unsigned int i;
343
344   i = 0;
345   while ((NULL != gns_name_map[i].name) && (type != gns_name_map[i].number))
346     i++;
347   return gns_name_map[i].name;
348 }
349
350
351 /**
352  * Entry point for the plugin.
353  *
354  * @param cls NULL
355  * @return the exported block API
356  */
357 void *
358 libgnunet_plugin_gnsrecord_gns_init (void *cls)
359 {
360   struct GNUNET_GNSRECORD_PluginFunctions *api;
361
362   api = GNUNET_new (struct GNUNET_GNSRECORD_PluginFunctions);
363   api->value_to_string = &gns_value_to_string;
364   api->string_to_value = &gns_string_to_value;
365   api->typename_to_number = &gns_typename_to_number;
366   api->number_to_typename = &gns_number_to_typename;
367   return api;
368 }
369
370
371 /**
372  * Exit point from the plugin.
373  *
374  * @param cls the return value from #libgnunet_plugin_block_test_init()
375  * @return NULL
376  */
377 void *
378 libgnunet_plugin_gnsrecord_gns_done (void *cls)
379 {
380   struct GNUNET_GNSRECORD_PluginFunctions *api = cls;
381
382   GNUNET_free (api);
383   return NULL;
384 }
385
386
387 /* end of plugin_gnsrecord_gns.c */