aboutsummaryrefslogtreecommitdiff
path: root/src/gns/plugin_gnsrecord_gns.c
diff options
context:
space:
mode:
authorMartin Schanzenbach <schanzen@gnunet.org>2023-10-19 11:33:18 +0200
committerMartin Schanzenbach <schanzen@gnunet.org>2023-10-19 11:33:18 +0200
commit7c7d819e8e03dadb91935d5ae91aa921cc7b86c7 (patch)
tree9327ae110e5e64c99901cd853d3d36e23f39aaee /src/gns/plugin_gnsrecord_gns.c
parentdf59c19d712a4339f7c75c76942c1a4f86bf2e5b (diff)
downloadgnunet-7c7d819e8e03dadb91935d5ae91aa921cc7b86c7.tar.gz
gnunet-7c7d819e8e03dadb91935d5ae91aa921cc7b86c7.zip
BUILD: Move gns/zonemaster to service
Diffstat (limited to 'src/gns/plugin_gnsrecord_gns.c')
-rw-r--r--src/gns/plugin_gnsrecord_gns.c431
1 files changed, 0 insertions, 431 deletions
diff --git a/src/gns/plugin_gnsrecord_gns.c b/src/gns/plugin_gnsrecord_gns.c
deleted file mode 100644
index 65587172d..000000000
--- a/src/gns/plugin_gnsrecord_gns.c
+++ /dev/null
@@ -1,431 +0,0 @@
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_gnsrecord_plugin.h"
33#include <inttypes.h>
34
35
36/**
37 * Convert the 'value' of a record to a string.
38 *
39 * @param cls closure, unused
40 * @param type type of the record
41 * @param data value in binary encoding
42 * @param data_size number of bytes in @a data
43 * @return NULL on error, otherwise human-readable representation of the value
44 */
45static char *
46gns_value_to_string (void *cls,
47 uint32_t type,
48 const void *data,
49 size_t data_size)
50{
51 const char *cdata;
52 struct GNUNET_CRYPTO_PublicKey pk;
53
54 switch (type)
55 {
56 case GNUNET_GNSRECORD_TYPE_PKEY:
57 case GNUNET_GNSRECORD_TYPE_EDKEY:
58 if (GNUNET_OK !=
59 GNUNET_GNSRECORD_identity_from_data (data,
60 data_size,
61 type,
62 &pk))
63 return NULL;
64 return GNUNET_CRYPTO_public_key_to_string (&pk);
65
66 case GNUNET_GNSRECORD_TYPE_NICK:
67 case GNUNET_GNSRECORD_TYPE_REDIRECT:
68 case GNUNET_GNSRECORD_TYPE_LEHO:
69 return GNUNET_strndup (data, data_size);
70
71 case GNUNET_GNSRECORD_TYPE_GNS2DNS: {
72 char *ns;
73 char *ip;
74 size_t off;
75 char *nstr;
76
77 off = 0;
78 ns = GNUNET_DNSPARSER_parse_name (data, data_size, &off);
79 if (NULL == ns)
80 {
81 GNUNET_break_op (0);
82 GNUNET_free (ns);
83 return NULL;
84 }
85 /* DNS server IP/name must be UTF-8 */
86 ip = GNUNET_strdup (&((const char*) data)[off]);
87 GNUNET_asprintf (&nstr, "%s@%s", ns, ip);
88 GNUNET_free (ns);
89 GNUNET_free (ip);
90 return nstr;
91 }
92
93 case GNUNET_GNSRECORD_TYPE_VPN: {
94 struct GNUNET_TUN_GnsVpnRecord vpn;
95 char *vpn_str;
96
97 cdata = data;
98 if ((data_size <= sizeof(vpn)) || ('\0' != cdata[data_size - 1]))
99 return NULL; /* malformed */
100 /* need to memcpy for alignment */
101 GNUNET_memcpy (&vpn, data, sizeof(vpn));
102 GNUNET_asprintf (&vpn_str,
103 "%u %s %s",
104 (unsigned int) ntohs (vpn.proto),
105 (const char *) GNUNET_i2s_full (&vpn.peer),
106 (const char *) &cdata[sizeof(vpn)]);
107 return vpn_str;
108 }
109
110 case GNUNET_GNSRECORD_TYPE_BOX: {
111 struct GNUNET_GNSRECORD_BoxRecord box;
112 uint32_t rt;
113 char *box_str;
114 char *ival;
115
116 cdata = data;
117 if (data_size < sizeof(struct GNUNET_GNSRECORD_BoxRecord))
118 return NULL; /* malformed */
119 GNUNET_memcpy (&box, data, sizeof(box));
120 rt = ntohl (box.record_type);
121 ival = GNUNET_GNSRECORD_value_to_string (rt,
122 &cdata[sizeof(box)],
123 data_size - sizeof(box));
124 if (NULL == ival)
125 return NULL; /* malformed */
126 GNUNET_asprintf (&box_str,
127 "%u %u %u %s",
128 (unsigned int) ntohs (box.protocol),
129 (unsigned int) ntohs (box.service),
130 (unsigned int) rt,
131 ival);
132 GNUNET_free (ival);
133 return box_str;
134 }
135 case GNUNET_GNSRECORD_TYPE_TOMBSTONE: {
136 return GNUNET_strdup (_ (
137 "This is a memento of an older block for internal maintenance."));
138 }
139 default:
140 return NULL;
141 }
142}
143
144
145/**
146 * Convert human-readable version of a 'value' of a record to the binary
147 * representation.
148 *
149 * @param cls closure, unused
150 * @param type type of the record
151 * @param s human-readable string
152 * @param data set to value in binary encoding (will be allocated)
153 * @param data_size set to number of bytes in @a data
154 * @return #GNUNET_OK on success
155 */
156static int
157gns_string_to_value (void *cls,
158 uint32_t type,
159 const char *s,
160 void **data,
161 size_t *data_size)
162{
163 struct GNUNET_CRYPTO_PublicKey pk;
164 uint32_t record_type;
165
166 if (NULL == s)
167 return GNUNET_SYSERR;
168 switch (type)
169 {
170 case GNUNET_GNSRECORD_TYPE_PKEY:
171 case GNUNET_GNSRECORD_TYPE_EDKEY:
172 if (GNUNET_OK !=
173 GNUNET_CRYPTO_public_key_from_string (s, &pk))
174 {
175 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
176 _ ("Unable to parse zone key record `%s'\n"),
177 s);
178 return GNUNET_SYSERR;
179 }
180 *data_size = GNUNET_CRYPTO_public_key_get_length (&pk);
181 if (GNUNET_OK !=
182 GNUNET_GNSRECORD_data_from_identity (&pk,
183 (char **) data,
184 data_size,
185 &record_type))
186 return GNUNET_SYSERR;
187 if (record_type != type)
188 {
189 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
190 _ ("Record type does not match parsed record type\n"));
191 return GNUNET_SYSERR;
192 }
193 return GNUNET_OK;
194
195 case GNUNET_GNSRECORD_TYPE_NICK:
196 case GNUNET_GNSRECORD_TYPE_REDIRECT:
197 case GNUNET_GNSRECORD_TYPE_LEHO:
198 *data = GNUNET_strdup (s);
199 *data_size = strlen (s);
200 return GNUNET_OK;
201
202 case GNUNET_GNSRECORD_TYPE_GNS2DNS: {
203 char nsbuf[514];
204 char *cpy;
205 char *at;
206 size_t off;
207
208 cpy = GNUNET_strdup (s);
209 at = strchr (cpy, '@');
210 if (NULL == at)
211 {
212 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
213 _ ("Unable to parse GNS2DNS record `%s'\n"),
214 s);
215 GNUNET_free (cpy);
216 return GNUNET_SYSERR;
217 }
218 *at = '\0';
219 at++;
220
221 off = 0;
222 if (GNUNET_OK !=
223 GNUNET_DNSPARSER_builder_add_name (nsbuf,
224 sizeof(nsbuf),
225 &off,
226 cpy))
227 {
228 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
229 _ (
230 "Failed to serialize GNS2DNS record with value `%s': Not a DNS name.\n"),
231 s);
232 GNUNET_free (cpy);
233 return GNUNET_SYSERR;
234 }
235 /* The DNS server location/name is in UTF-8 */
236 GNUNET_memcpy (&nsbuf[off], at, strlen (at) + 1);
237 off += strlen (at) + 1;
238 GNUNET_free (cpy);
239 *data_size = off;
240 *data = GNUNET_malloc (off);
241 GNUNET_memcpy (*data, nsbuf, off);
242 return GNUNET_OK;
243 }
244
245 case GNUNET_GNSRECORD_TYPE_VPN: {
246 struct GNUNET_TUN_GnsVpnRecord *vpn;
247 char s_peer[103 + 1];
248 char s_serv[253 + 1];
249 unsigned int proto;
250
251 if (3 != sscanf (s, "%u %103s %253s", &proto, s_peer, s_serv))
252 {
253 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
254 _ ("Unable to parse VPN record string `%s'\n"),
255 s);
256 return GNUNET_SYSERR;
257 }
258 *data_size = sizeof(struct GNUNET_TUN_GnsVpnRecord) + strlen (s_serv) + 1;
259 *data = vpn = GNUNET_malloc (*data_size);
260 if (GNUNET_OK !=
261 GNUNET_CRYPTO_eddsa_public_key_from_string ((char *) s_peer,
262 strlen (s_peer),
263 &vpn->peer.public_key))
264 {
265 GNUNET_free (vpn);
266 *data_size = 0;
267 return GNUNET_SYSERR;
268 }
269 vpn->proto = htons ((uint16_t) proto);
270 strcpy ((char *) &vpn[1], s_serv);
271 return GNUNET_OK;
272 }
273
274 case GNUNET_GNSRECORD_TYPE_BOX: {
275 struct GNUNET_GNSRECORD_BoxRecord *box;
276 size_t rest;
277 unsigned int protocol;
278 unsigned int service;
279 unsigned int record_type;
280 void *bval;
281 size_t bval_size;
282
283 if (3 != sscanf (s, "%u %u %u ", &protocol, &service, &record_type))
284 {
285 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
286 _ ("Unable to parse BOX record string `%s'\n"),
287 s);
288 return GNUNET_SYSERR;
289 }
290 rest = snprintf (NULL, 0, "%u %u %u ", protocol, service, record_type);
291 if (GNUNET_OK != GNUNET_GNSRECORD_string_to_value (record_type,
292 &s[rest],
293 &bval,
294 &bval_size))
295 return GNUNET_SYSERR;
296 *data_size = sizeof(struct GNUNET_GNSRECORD_BoxRecord) + bval_size;
297 *data = box = GNUNET_malloc (*data_size);
298 box->protocol = htons (protocol);
299 box->service = htons (service);
300 box->record_type = htonl (record_type);
301 GNUNET_memcpy (&box[1], bval, bval_size);
302 GNUNET_free (bval);
303 return GNUNET_OK;
304 }
305 case GNUNET_GNSRECORD_TYPE_TOMBSTONE: {
306 *data_size = 0;
307 *data = NULL;
308 return GNUNET_OK;
309 }
310
311 default:
312 return GNUNET_SYSERR;
313 }
314}
315
316
317/**
318 * Mapping of record type numbers to human-readable
319 * record type names.
320 */
321static struct
322{
323 const char *name;
324 uint32_t number;
325} gns_name_map[] = {
326 { "PKEY", GNUNET_GNSRECORD_TYPE_PKEY },
327 { "EDKEY", GNUNET_GNSRECORD_TYPE_EDKEY },
328 { "NICK", GNUNET_GNSRECORD_TYPE_NICK },
329 { "LEHO", GNUNET_GNSRECORD_TYPE_LEHO },
330 { "VPN", GNUNET_GNSRECORD_TYPE_VPN },
331 { "GNS2DNS", GNUNET_GNSRECORD_TYPE_GNS2DNS },
332 { "BOX", GNUNET_GNSRECORD_TYPE_BOX },
333 { "REDIRECT", GNUNET_GNSRECORD_TYPE_REDIRECT },
334 /* Tombstones should never be added manually
335 * so this makes sense, kind of */
336 { "\u271E", GNUNET_GNSRECORD_TYPE_TOMBSTONE },
337 { NULL, UINT32_MAX }
338};
339
340
341/**
342 * Convert a type name (e.g. "AAAA") to the corresponding number.
343 *
344 * @param cls closure, unused
345 * @param gns_typename name to convert
346 * @return corresponding number, UINT32_MAX on error
347 */
348static uint32_t
349gns_typename_to_number (void *cls,
350 const char *gns_typename)
351{
352 unsigned int i;
353
354 i = 0;
355 while ((NULL != gns_name_map[i].name) &&
356 (0 != strcasecmp (gns_typename, gns_name_map[i].name)))
357 i++;
358 return gns_name_map[i].number;
359}
360
361
362/**
363 * Convert a type number to the corresponding type string (e.g. 1 to "A")
364 *
365 * @param cls closure, unused
366 * @param type number of a type to convert
367 * @return corresponding typestring, NULL on error
368 */
369static const char *
370gns_number_to_typename (void *cls,
371 uint32_t type)
372{
373 unsigned int i;
374
375 i = 0;
376 while ( (NULL != gns_name_map[i].name) &&
377 (type != gns_name_map[i].number) )
378 i++;
379 return gns_name_map[i].name;
380}
381
382
383static enum GNUNET_GenericReturnValue
384gns_is_critical (void *cls, uint32_t type)
385{
386 return ((type == GNUNET_GNSRECORD_TYPE_PKEY) ||
387 (type == GNUNET_GNSRECORD_TYPE_EDKEY) ||
388 (type == GNUNET_GNSRECORD_TYPE_GNS2DNS) ||
389 (type == GNUNET_GNSRECORD_TYPE_REDIRECT) ?
390 GNUNET_YES : GNUNET_NO);
391}
392
393
394/**
395 * Entry point for the plugin.
396 *
397 * @param cls NULL
398 * @return the exported block API
399 */
400void *
401libgnunet_plugin_gnsrecord_gns_init (void *cls)
402{
403 struct GNUNET_GNSRECORD_PluginFunctions *api;
404
405 api = GNUNET_new (struct GNUNET_GNSRECORD_PluginFunctions);
406 api->value_to_string = &gns_value_to_string;
407 api->string_to_value = &gns_string_to_value;
408 api->typename_to_number = &gns_typename_to_number;
409 api->number_to_typename = &gns_number_to_typename;
410 api->is_critical = &gns_is_critical;
411 return api;
412}
413
414
415/**
416 * Exit point from the plugin.
417 *
418 * @param cls the return value from #libgnunet_plugin_block_test_init()
419 * @return NULL
420 */
421void *
422libgnunet_plugin_gnsrecord_gns_done (void *cls)
423{
424 struct GNUNET_GNSRECORD_PluginFunctions *api = cls;
425
426 GNUNET_free (api);
427 return NULL;
428}
429
430
431/* end of plugin_gnsrecord_gns.c */