plugin_namecache_postgres.c 11 KB

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