plugin_namecache_postgres.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. * This file is part of GNUnet
  3. * Copyright (C) 2009-2013, 2016, 2017 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. SPDX-License-Identifier: AGPL3.0-or-later
  18. */
  19. /**
  20. * @file namecache/plugin_namecache_postgres.c
  21. * @brief postgres-based namecache backend
  22. * @author Christian Grothoff
  23. */
  24. #include "platform.h"
  25. #include "gnunet_namecache_plugin.h"
  26. #include "gnunet_namecache_service.h"
  27. #include "gnunet_gnsrecord_lib.h"
  28. #include "gnunet_pq_lib.h"
  29. #include "namecache.h"
  30. #define LOG(kind, ...) GNUNET_log_from (kind, "namecache-postgres", __VA_ARGS__)
  31. /**
  32. * Context for all functions in this plugin.
  33. */
  34. struct Plugin
  35. {
  36. const struct GNUNET_CONFIGURATION_Handle *cfg;
  37. /**
  38. * Postgres database handle.
  39. */
  40. struct GNUNET_PQ_Context *dbh;
  41. };
  42. /**
  43. * Initialize the database connections and associated
  44. * data structures (create tables and indices
  45. * as needed as well).
  46. *
  47. * @param plugin the plugin context (state for this module)
  48. * @return #GNUNET_OK on success
  49. */
  50. static int
  51. database_setup (struct Plugin *plugin)
  52. {
  53. struct GNUNET_PQ_ExecuteStatement es_temporary =
  54. GNUNET_PQ_make_execute ("CREATE TEMPORARY TABLE IF NOT EXISTS ns096blocks ("
  55. " query BYTEA NOT NULL DEFAULT '',"
  56. " block BYTEA NOT NULL DEFAULT '',"
  57. " expiration_time BIGINT NOT NULL DEFAULT 0"
  58. ")");
  59. struct GNUNET_PQ_ExecuteStatement es_default =
  60. GNUNET_PQ_make_execute ("CREATE TABLE IF NOT EXISTS ns096blocks ("
  61. " query BYTEA NOT NULL DEFAULT '',"
  62. " block BYTEA NOT NULL DEFAULT '',"
  63. " expiration_time BIGINT NOT NULL DEFAULT 0"
  64. ")");
  65. const struct GNUNET_PQ_ExecuteStatement *cr;
  66. if (GNUNET_YES ==
  67. GNUNET_CONFIGURATION_get_value_yesno (plugin->cfg,
  68. "namecache-postgres",
  69. "TEMPORARY_TABLE"))
  70. {
  71. cr = &es_temporary;
  72. }
  73. else
  74. {
  75. cr = &es_default;
  76. }
  77. {
  78. struct GNUNET_PQ_ExecuteStatement es[] = {
  79. *cr,
  80. GNUNET_PQ_make_try_execute (
  81. "CREATE INDEX ir_query_hash ON ns096blocks (query,expiration_time)"),
  82. GNUNET_PQ_make_try_execute (
  83. "CREATE INDEX ir_block_expiration ON ns096blocks (expiration_time)"),
  84. GNUNET_PQ_EXECUTE_STATEMENT_END
  85. };
  86. struct GNUNET_PQ_PreparedStatement ps[] = {
  87. GNUNET_PQ_make_prepare ("cache_block",
  88. "INSERT INTO ns096blocks (query, block, expiration_time) VALUES "
  89. "($1, $2, $3)", 3),
  90. GNUNET_PQ_make_prepare ("expire_blocks",
  91. "DELETE FROM ns096blocks WHERE expiration_time<$1",
  92. 1),
  93. GNUNET_PQ_make_prepare ("delete_block",
  94. "DELETE FROM ns096blocks WHERE query=$1 AND expiration_time<=$2",
  95. 2),
  96. GNUNET_PQ_make_prepare ("lookup_block",
  97. "SELECT block FROM ns096blocks WHERE query=$1"
  98. " ORDER BY expiration_time DESC LIMIT 1", 1),
  99. GNUNET_PQ_PREPARED_STATEMENT_END
  100. };
  101. plugin->dbh = GNUNET_PQ_connect_with_cfg (plugin->cfg,
  102. "namecache-postgres",
  103. NULL,
  104. es,
  105. ps);
  106. }
  107. if (NULL == plugin->dbh)
  108. return GNUNET_SYSERR;
  109. return GNUNET_OK;
  110. }
  111. /**
  112. * Removes any expired block.
  113. *
  114. * @param plugin the plugin
  115. */
  116. static void
  117. namecache_postgres_expire_blocks (struct Plugin *plugin)
  118. {
  119. struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
  120. struct GNUNET_PQ_QueryParam params[] = {
  121. GNUNET_PQ_query_param_absolute_time (&now),
  122. GNUNET_PQ_query_param_end
  123. };
  124. enum GNUNET_DB_QueryStatus res;
  125. res = GNUNET_PQ_eval_prepared_non_select (plugin->dbh,
  126. "expire_blocks",
  127. params);
  128. GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR != res);
  129. }
  130. /**
  131. * Delete older block in the datastore.
  132. *
  133. * @param plugin the plugin
  134. * @param query query for the block
  135. * @param expiration_time how old does the block have to be for deletion
  136. */
  137. static void
  138. delete_old_block (struct Plugin *plugin,
  139. const struct GNUNET_HashCode *query,
  140. struct GNUNET_TIME_AbsoluteNBO expiration_time)
  141. {
  142. struct GNUNET_PQ_QueryParam params[] = {
  143. GNUNET_PQ_query_param_auto_from_type (query),
  144. GNUNET_PQ_query_param_absolute_time_nbo (&expiration_time),
  145. GNUNET_PQ_query_param_end
  146. };
  147. enum GNUNET_DB_QueryStatus res;
  148. res = GNUNET_PQ_eval_prepared_non_select (plugin->dbh,
  149. "delete_block",
  150. params);
  151. GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR != res);
  152. }
  153. /**
  154. * Cache a block in the datastore.
  155. *
  156. * @param cls closure (internal context for the plugin)
  157. * @param block block to cache
  158. * @return #GNUNET_OK on success, else #GNUNET_SYSERR
  159. */
  160. static int
  161. namecache_postgres_cache_block (void *cls,
  162. const struct GNUNET_GNSRECORD_Block *block)
  163. {
  164. struct Plugin *plugin = cls;
  165. struct GNUNET_HashCode query;
  166. size_t block_size = ntohl (block->purpose.size)
  167. + sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey)
  168. + sizeof(struct GNUNET_CRYPTO_EcdsaSignature);
  169. struct GNUNET_PQ_QueryParam params[] = {
  170. GNUNET_PQ_query_param_auto_from_type (&query),
  171. GNUNET_PQ_query_param_fixed_size (block, block_size),
  172. GNUNET_PQ_query_param_absolute_time_nbo (&block->expiration_time),
  173. GNUNET_PQ_query_param_end
  174. };
  175. enum GNUNET_DB_QueryStatus res;
  176. namecache_postgres_expire_blocks (plugin);
  177. GNUNET_CRYPTO_hash (&block->derived_key,
  178. sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey),
  179. &query);
  180. if (block_size > 64 * 65536)
  181. {
  182. GNUNET_break (0);
  183. return GNUNET_SYSERR;
  184. }
  185. delete_old_block (plugin,
  186. &query,
  187. block->expiration_time);
  188. res = GNUNET_PQ_eval_prepared_non_select (plugin->dbh,
  189. "cache_block",
  190. params);
  191. if (0 > res)
  192. return GNUNET_SYSERR;
  193. return GNUNET_OK;
  194. }
  195. /**
  196. * Get the block for a particular zone and label in the
  197. * datastore. Will return at most one result to the iterator.
  198. *
  199. * @param cls closure (internal context for the plugin)
  200. * @param query hash of public key derived from the zone and the label
  201. * @param iter function to call with the result
  202. * @param iter_cls closure for @a iter
  203. * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
  204. */
  205. static int
  206. namecache_postgres_lookup_block (void *cls,
  207. const struct GNUNET_HashCode *query,
  208. GNUNET_NAMECACHE_BlockCallback iter,
  209. void *iter_cls)
  210. {
  211. struct Plugin *plugin = cls;
  212. size_t bsize;
  213. struct GNUNET_GNSRECORD_Block *block;
  214. struct GNUNET_PQ_QueryParam params[] = {
  215. GNUNET_PQ_query_param_auto_from_type (query),
  216. GNUNET_PQ_query_param_end
  217. };
  218. struct GNUNET_PQ_ResultSpec rs[] = {
  219. GNUNET_PQ_result_spec_variable_size ("block",
  220. (void **) &block,
  221. &bsize),
  222. GNUNET_PQ_result_spec_end
  223. };
  224. enum GNUNET_DB_QueryStatus res;
  225. res = GNUNET_PQ_eval_prepared_singleton_select (plugin->dbh,
  226. "lookup_block",
  227. params,
  228. rs);
  229. if (0 > res)
  230. {
  231. LOG (GNUNET_ERROR_TYPE_WARNING,
  232. "Failing lookup block in namecache (postgres error)\n");
  233. return GNUNET_SYSERR;
  234. }
  235. if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == res)
  236. {
  237. /* no result */
  238. LOG (GNUNET_ERROR_TYPE_DEBUG,
  239. "Ending iteration (no more results)\n");
  240. return GNUNET_NO;
  241. }
  242. if ((bsize < sizeof(*block)) ||
  243. (bsize != ntohl (block->purpose.size)
  244. + sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey)
  245. + sizeof(struct GNUNET_CRYPTO_EcdsaSignature)))
  246. {
  247. GNUNET_break (0);
  248. LOG (GNUNET_ERROR_TYPE_DEBUG,
  249. "Failing lookup (corrupt block)\n");
  250. GNUNET_PQ_cleanup_result (rs);
  251. return GNUNET_SYSERR;
  252. }
  253. iter (iter_cls,
  254. block);
  255. GNUNET_PQ_cleanup_result (rs);
  256. return GNUNET_OK;
  257. }
  258. /**
  259. * Shutdown database connection and associate data
  260. * structures.
  261. *
  262. * @param plugin the plugin context (state for this module)
  263. */
  264. static void
  265. database_shutdown (struct Plugin *plugin)
  266. {
  267. GNUNET_PQ_disconnect (plugin->dbh);
  268. plugin->dbh = NULL;
  269. }
  270. /**
  271. * Entry point for the plugin.
  272. *
  273. * @param cls the `struct GNUNET_NAMECACHE_PluginEnvironment *`
  274. * @return NULL on error, otherwise the plugin context
  275. */
  276. void *
  277. libgnunet_plugin_namecache_postgres_init (void *cls)
  278. {
  279. static struct Plugin plugin;
  280. const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
  281. struct GNUNET_NAMECACHE_PluginFunctions *api;
  282. if (NULL != plugin.cfg)
  283. return NULL; /* can only initialize once! */
  284. memset (&plugin, 0, sizeof(struct Plugin));
  285. plugin.cfg = cfg;
  286. if (GNUNET_OK != database_setup (&plugin))
  287. {
  288. database_shutdown (&plugin);
  289. return NULL;
  290. }
  291. api = GNUNET_new (struct GNUNET_NAMECACHE_PluginFunctions);
  292. api->cls = &plugin;
  293. api->cache_block = &namecache_postgres_cache_block;
  294. api->lookup_block = &namecache_postgres_lookup_block;
  295. LOG (GNUNET_ERROR_TYPE_INFO,
  296. "Postgres namecache plugin running\n");
  297. return api;
  298. }
  299. /**
  300. * Exit point from the plugin.
  301. *
  302. * @param cls the plugin context (as returned by "init")
  303. * @return always NULL
  304. */
  305. void *
  306. libgnunet_plugin_namecache_postgres_done (void *cls)
  307. {
  308. struct GNUNET_NAMECACHE_PluginFunctions *api = cls;
  309. struct Plugin *plugin = api->cls;
  310. database_shutdown (plugin);
  311. plugin->cfg = NULL;
  312. GNUNET_free (api);
  313. LOG (GNUNET_ERROR_TYPE_DEBUG,
  314. "Postgres namecache plugin is finished\n");
  315. return NULL;
  316. }
  317. /* end of plugin_namecache_postgres.c */