plugin_gnsrecord_gns.c 12 KB

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