2 * This file is part of GNUnet
3 * Copyright (C) 2009-2013, 2016, 2017 GNUnet e.V.
5 * GNUnet is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published
7 * by the Free Software Foundation; either version 3, or (at your
8 * option) any later version.
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 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with GNUnet; see the file COPYING. If not, write to the
17 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
22 * @file namestore/plugin_namestore_postgres.c
23 * @brief postgres-based namestore backend
24 * @author Christian Grothoff
27 #include "gnunet_namestore_plugin.h"
28 #include "gnunet_namestore_service.h"
29 #include "gnunet_gnsrecord_lib.h"
30 #include "gnunet_pq_lib.h"
31 #include "namestore.h"
34 #define LOG(kind,...) GNUNET_log_from (kind, "namestore-postgres", __VA_ARGS__)
38 * Context for all functions in this plugin.
46 const struct GNUNET_CONFIGURATION_Handle *cfg;
49 * Native Postgres database handle.
57 * Initialize the database connections and associated
58 * data structures (create tables and indices
61 * @param plugin the plugin context (state for this module)
62 * @return #GNUNET_OK on success
65 database_setup (struct Plugin *plugin)
67 struct GNUNET_PQ_ExecuteStatement es_temporary =
68 GNUNET_PQ_make_execute ("CREATE TEMPORARY TABLE IF NOT EXISTS ns097records ("
69 " zone_private_key BYTEA NOT NULL DEFAULT '',"
70 " pkey BYTEA DEFAULT '',"
71 " rvalue BYTEA NOT NULL DEFAULT '',"
72 " record_count INTEGER NOT NULL DEFAULT 0,"
73 " record_data BYTEA NOT NULL DEFAULT '',"
74 " label TEXT NOT NULL DEFAULT ''"
77 struct GNUNET_PQ_ExecuteStatement es_default =
78 GNUNET_PQ_make_execute ("CREATE TABLE IF NOT EXISTS ns097records ("
79 " zone_private_key BYTEA NOT NULL DEFAULT '',"
80 " pkey BYTEA DEFAULT '',"
81 " rvalue BYTEA NOT NULL DEFAULT '',"
82 " record_count INTEGER NOT NULL DEFAULT 0,"
83 " record_data BYTEA NOT NULL DEFAULT '',"
84 " label TEXT NOT NULL DEFAULT ''"
87 const struct GNUNET_PQ_ExecuteStatement *cr;
89 plugin->dbh = GNUNET_PQ_connect_with_cfg (plugin->cfg,
90 "namestore-postgres");
91 if (NULL == plugin->dbh)
94 GNUNET_CONFIGURATION_get_value_yesno (plugin->cfg,
106 struct GNUNET_PQ_ExecuteStatement es[] = {
108 GNUNET_PQ_make_try_execute ("CREATE INDEX IF NOT EXISTS ir_pkey_reverse "
109 "ON ns097records (zone_private_key,pkey)"),
110 GNUNET_PQ_make_try_execute ("CREATE INDEX IF NOT EXISTS ir_pkey_iter "
111 "ON ns097records (zone_private_key,rvalue)"),
112 GNUNET_PQ_make_try_execute ("CREATE INDEX IF NOT EXISTS it_iter "
113 "ON ns097records (rvalue)"),
114 GNUNET_PQ_make_try_execute ("CREATE INDEX IF NOT EXISTS ir_label "
115 "ON ns097records (label)"),
116 GNUNET_PQ_EXECUTE_STATEMENT_END
120 GNUNET_PQ_exec_statements (plugin->dbh,
123 PQfinish (plugin->dbh);
125 return GNUNET_SYSERR;
130 struct GNUNET_PQ_PreparedStatement ps[] = {
131 GNUNET_PQ_make_prepare ("store_records",
132 "INSERT INTO ns097records (zone_private_key, pkey, rvalue, record_count, record_data, label) VALUES "
133 "($1, $2, $3, $4, $5, $6)", 6),
134 GNUNET_PQ_make_prepare ("delete_records",
135 "DELETE FROM ns097records "
136 "WHERE zone_private_key=$1 AND label=$2", 2),
137 GNUNET_PQ_make_prepare ("zone_to_name",
138 "SELECT record_count,record_data,label FROM ns097records"
139 " WHERE zone_private_key=$1 AND pkey=$2", 2),
140 GNUNET_PQ_make_prepare ("iterate_zone",
141 "SELECT record_count,record_data,label FROM ns097records "
142 "WHERE zone_private_key=$1 ORDER BY rvalue LIMIT 1 OFFSET $2", 2),
143 GNUNET_PQ_make_prepare ("iterate_all_zones",
144 "SELECT record_count,record_data,label,zone_private_key"
145 " FROM ns097records ORDER BY rvalue LIMIT 1 OFFSET $1", 1),
146 GNUNET_PQ_make_prepare ("lookup_label",
147 "SELECT record_count,record_data,label "
148 "FROM ns097records WHERE zone_private_key=$1 AND label=$2", 2),
149 GNUNET_PQ_PREPARED_STATEMENT_END
153 GNUNET_PQ_prepare_statements (plugin->dbh,
156 PQfinish (plugin->dbh);
158 return GNUNET_SYSERR;
167 * Store a record in the datastore. Removes any existing record in the
168 * same zone with the same name.
170 * @param cls closure (internal context for the plugin)
171 * @param zone_key private key of the zone
172 * @param label name that is being mapped (at most 255 characters long)
173 * @param rd_count number of entries in @a rd array
174 * @param rd array of records with data to store
175 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
178 namestore_postgres_store_records (void *cls,
179 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
181 unsigned int rd_count,
182 const struct GNUNET_GNSRECORD_Data *rd)
184 struct Plugin *plugin = cls;
185 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
187 uint32_t rd_count32 = (uint32_t) rd_count;
190 memset (&pkey, 0, sizeof (pkey));
191 for (unsigned int i=0;i<rd_count;i++)
192 if (GNUNET_GNSRECORD_TYPE_PKEY == rd[i].record_type)
194 GNUNET_break (sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) == rd[i].data_size);
195 GNUNET_memcpy (&pkey,
200 rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
202 data_size = GNUNET_GNSRECORD_records_get_size (rd_count, rd);
203 if (data_size > 64 * 65536)
206 return GNUNET_SYSERR;
209 char data[data_size];
210 struct GNUNET_PQ_QueryParam params[] = {
211 GNUNET_PQ_query_param_auto_from_type (zone_key),
212 GNUNET_PQ_query_param_auto_from_type (&pkey),
213 GNUNET_PQ_query_param_uint64 (&rvalue),
214 GNUNET_PQ_query_param_uint32 (&rd_count32),
215 GNUNET_PQ_query_param_fixed_size (data, data_size),
216 GNUNET_PQ_query_param_string (label),
217 GNUNET_PQ_query_param_end
219 enum GNUNET_DB_QueryStatus res;
222 GNUNET_GNSRECORD_records_serialize (rd_count, rd,
226 return GNUNET_SYSERR;
229 res = GNUNET_PQ_eval_prepared_non_select (plugin->dbh,
232 if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS != res)
233 return GNUNET_SYSERR;
240 * Closure for #parse_result_call_iterator.
245 * Function to call for each result.
247 GNUNET_NAMESTORE_RecordIterator iter;
250 * Closure for @e iter.
255 * Zone key, NULL if part of record.
257 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key;
262 * A statement has been run. We should evaluate the result, and if possible
263 * call the @a iter in @a cls with the result.
265 * @param cls closure of type `struct ParserContext *`
266 * @param result the postgres result
267 * @param num_result the number of results in @a result
270 parse_result_call_iterator (void *cls,
272 unsigned int num_results)
274 struct ParserContext *pc = cls;
276 for (unsigned int i=0;i<num_results;i++)
280 uint32_t record_count;
282 struct GNUNET_CRYPTO_EcdsaPrivateKey zk;
283 struct GNUNET_PQ_ResultSpec rs_with_zone[] = {
284 GNUNET_PQ_result_spec_uint32 ("record_count", &record_count),
285 GNUNET_PQ_result_spec_variable_size ("record_data", &data, &data_size),
286 GNUNET_PQ_result_spec_string ("label", &label),
287 GNUNET_PQ_result_spec_auto_from_type ("zone_private_key", &zk),
288 GNUNET_PQ_result_spec_end
290 struct GNUNET_PQ_ResultSpec rs_without_zone[] = {
291 GNUNET_PQ_result_spec_uint32 ("record_count", &record_count),
292 GNUNET_PQ_result_spec_variable_size ("record_data", &data, &data_size),
293 GNUNET_PQ_result_spec_string ("label", &label),
294 GNUNET_PQ_result_spec_end
296 struct GNUNET_PQ_ResultSpec *rs;
298 rs = (NULL == pc->zone_key) ? rs_with_zone : rs_without_zone;
300 GNUNET_PQ_extract_result (res,
308 if (record_count > 64 * 1024)
310 /* sanity check, don't stack allocate far too much just
311 because database might contain a large value here */
313 GNUNET_PQ_cleanup_result (rs);
318 struct GNUNET_GNSRECORD_Data rd[record_count];
321 GNUNET_GNSRECORD_records_deserialize (data_size,
327 GNUNET_PQ_cleanup_result (rs);
330 pc->iter (pc->iter_cls,
331 (NULL == pc->zone_key) ? &zk : pc->zone_key,
336 GNUNET_PQ_cleanup_result (rs);
342 * Lookup records in the datastore for which we are the authority.
344 * @param cls closure (internal context for the plugin)
345 * @param zone private key of the zone
346 * @param label name of the record in the zone
347 * @param iter function to call with the result
348 * @param iter_cls closure for @a iter
349 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
352 namestore_postgres_lookup_records (void *cls,
353 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
355 GNUNET_NAMESTORE_RecordIterator iter,
358 struct Plugin *plugin = cls;
359 struct GNUNET_PQ_QueryParam params[] = {
360 GNUNET_PQ_query_param_auto_from_type (zone),
361 GNUNET_PQ_query_param_string (label),
362 GNUNET_PQ_query_param_end
364 struct ParserContext pc;
365 enum GNUNET_DB_QueryStatus res;
368 pc.iter_cls = iter_cls;
370 res = GNUNET_PQ_eval_prepared_multi_select (plugin->dbh,
373 &parse_result_call_iterator,
376 return GNUNET_SYSERR;
382 * Iterate over the results for a particular key and zone in the
383 * datastore. Will return at most one result to the iterator.
385 * @param cls closure (internal context for the plugin)
386 * @param zone hash of public key of the zone, NULL to iterate over all zones
387 * @param offset offset in the list of all matching records
388 * @param iter function to call with the result
389 * @param iter_cls closure for @a iter
390 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
393 namestore_postgres_iterate_records (void *cls,
394 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
396 GNUNET_NAMESTORE_RecordIterator iter,
399 struct Plugin *plugin = cls;
400 enum GNUNET_DB_QueryStatus res;
401 struct ParserContext pc;
404 pc.iter_cls = iter_cls;
408 struct GNUNET_PQ_QueryParam params_without_zone[] = {
409 GNUNET_PQ_query_param_uint64 (&offset),
410 GNUNET_PQ_query_param_end
413 res = GNUNET_PQ_eval_prepared_multi_select (plugin->dbh,
416 &parse_result_call_iterator,
421 struct GNUNET_PQ_QueryParam params_with_zone[] = {
422 GNUNET_PQ_query_param_auto_from_type (zone),
423 GNUNET_PQ_query_param_uint64 (&offset),
424 GNUNET_PQ_query_param_end
427 res = GNUNET_PQ_eval_prepared_multi_select (plugin->dbh,
430 &parse_result_call_iterator,
434 return GNUNET_SYSERR;
436 if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == res)
444 * Look for an existing PKEY delegation record for a given public key.
445 * Returns at most one result to the iterator.
447 * @param cls closure (internal context for the plugin)
448 * @param zone private key of the zone to look up in, never NULL
449 * @param value_zone public key of the target zone (value), never NULL
450 * @param iter function to call with the result
451 * @param iter_cls closure for @a iter
452 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
455 namestore_postgres_zone_to_name (void *cls,
456 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
457 const struct GNUNET_CRYPTO_EcdsaPublicKey *value_zone,
458 GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
460 struct Plugin *plugin = cls;
461 struct GNUNET_PQ_QueryParam params[] = {
462 GNUNET_PQ_query_param_auto_from_type (zone),
463 GNUNET_PQ_query_param_auto_from_type (value_zone),
464 GNUNET_PQ_query_param_end
466 enum GNUNET_DB_QueryStatus res;
467 struct ParserContext pc;
470 pc.iter_cls = iter_cls;
473 res = GNUNET_PQ_eval_prepared_multi_select (plugin->dbh,
476 &parse_result_call_iterator,
479 return GNUNET_SYSERR;
485 * Shutdown database connection and associate data
488 * @param plugin the plugin context (state for this module)
491 database_shutdown (struct Plugin *plugin)
493 PQfinish (plugin->dbh);
499 * Entry point for the plugin.
501 * @param cls the `struct GNUNET_NAMESTORE_PluginEnvironment*`
502 * @return NULL on error, othrewise the plugin context
505 libgnunet_plugin_namestore_postgres_init (void *cls)
507 static struct Plugin plugin;
508 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
509 struct GNUNET_NAMESTORE_PluginFunctions *api;
511 if (NULL != plugin.cfg)
512 return NULL; /* can only initialize once! */
513 memset (&plugin, 0, sizeof (struct Plugin));
515 if (GNUNET_OK != database_setup (&plugin))
517 database_shutdown (&plugin);
520 api = GNUNET_new (struct GNUNET_NAMESTORE_PluginFunctions);
522 api->store_records = &namestore_postgres_store_records;
523 api->iterate_records = &namestore_postgres_iterate_records;
524 api->zone_to_name = &namestore_postgres_zone_to_name;
525 api->lookup_records = &namestore_postgres_lookup_records;
526 LOG (GNUNET_ERROR_TYPE_INFO,
527 "Postgres namestore plugin running\n");
533 * Exit point from the plugin.
535 * @param cls the plugin context (as returned by "init")
536 * @return always NULL
539 libgnunet_plugin_namestore_postgres_done (void *cls)
541 struct GNUNET_NAMESTORE_PluginFunctions *api = cls;
542 struct Plugin *plugin = api->cls;
544 database_shutdown (plugin);
547 LOG (GNUNET_ERROR_TYPE_DEBUG,
548 "Postgres namestore plugin is finished\n");
552 /* end of plugin_namestore_postgres.c */