2 * This file is part of GNUnet
3 * Copyright (C) 2009-2013, 2016-2018 GNUnet e.V.
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.
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.
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/>.
20 * @file namestore/plugin_namestore_postgres.c
21 * @brief postgres-based namestore backend
22 * @author Christian Grothoff
25 #include "gnunet_namestore_plugin.h"
26 #include "gnunet_namestore_service.h"
27 #include "gnunet_gnsrecord_lib.h"
28 #include "gnunet_pq_lib.h"
29 #include "namestore.h"
32 #define LOG(kind,...) GNUNET_log_from (kind, "namestore-postgres", __VA_ARGS__)
36 * Context for all functions in this plugin.
44 const struct GNUNET_CONFIGURATION_Handle *cfg;
47 * Native Postgres database handle.
55 * Initialize the database connections and associated
56 * data structures (create tables and indices
59 * @param plugin the plugin context (state for this module)
60 * @return #GNUNET_OK on success
63 database_setup (struct Plugin *plugin)
65 struct GNUNET_PQ_ExecuteStatement es_temporary =
66 GNUNET_PQ_make_execute ("CREATE TEMPORARY TABLE IF NOT EXISTS ns098records ("
67 " seq BIGSERIAL PRIMARY KEY,"
68 " zone_private_key BYTEA NOT NULL DEFAULT '',"
69 " pkey BYTEA DEFAULT '',"
70 " rvalue BYTEA NOT NULL DEFAULT '',"
71 " record_count INTEGER NOT NULL DEFAULT 0,"
72 " record_data BYTEA NOT NULL DEFAULT '',"
73 " label TEXT NOT NULL DEFAULT '',"
74 " CONSTRAINT zl UNIQUE (zone_private_key,label)"
77 struct GNUNET_PQ_ExecuteStatement es_default =
78 GNUNET_PQ_make_execute ("CREATE TABLE IF NOT EXISTS ns098records ("
79 " seq BIGSERIAL PRIMARY KEY,"
80 " zone_private_key BYTEA NOT NULL DEFAULT '',"
81 " pkey BYTEA DEFAULT '',"
82 " rvalue BYTEA NOT NULL DEFAULT '',"
83 " record_count INTEGER NOT NULL DEFAULT 0,"
84 " record_data BYTEA NOT NULL DEFAULT '',"
85 " label TEXT NOT NULL DEFAULT '',"
86 " CONSTRAINT zl UNIQUE (zone_private_key,label)"
89 const struct GNUNET_PQ_ExecuteStatement *cr;
91 plugin->dbh = GNUNET_PQ_connect_with_cfg (plugin->cfg,
92 "namestore-postgres");
93 if (NULL == plugin->dbh)
96 GNUNET_CONFIGURATION_get_value_yesno (plugin->cfg,
100 struct GNUNET_PQ_ExecuteStatement es[] = {
101 GNUNET_PQ_make_try_execute ("SET synchronous_commit TO off"),
102 GNUNET_PQ_EXECUTE_STATEMENT_END
106 GNUNET_PQ_exec_statements (plugin->dbh,
109 PQfinish (plugin->dbh);
111 return GNUNET_SYSERR;
115 GNUNET_CONFIGURATION_get_value_yesno (plugin->cfg,
116 "namestore-postgres",
127 struct GNUNET_PQ_ExecuteStatement es[] = {
129 GNUNET_PQ_make_try_execute ("CREATE INDEX IF NOT EXISTS ir_pkey_reverse "
130 "ON ns098records (zone_private_key,pkey)"),
131 GNUNET_PQ_make_try_execute ("CREATE INDEX IF NOT EXISTS ir_pkey_iter "
132 "ON ns098records (zone_private_key,seq)"),
133 GNUNET_PQ_make_try_execute ("CREATE INDEX IF NOT EXISTS ir_label "
134 "ON ns098records (label)"),
135 GNUNET_PQ_make_try_execute ("CREATE INDEX IF NOT EXISTS zone_label "
136 "ON ns098records (zone_private_key,label)"),
137 GNUNET_PQ_EXECUTE_STATEMENT_END
141 GNUNET_PQ_exec_statements (plugin->dbh,
144 PQfinish (plugin->dbh);
146 return GNUNET_SYSERR;
151 struct GNUNET_PQ_PreparedStatement ps[] = {
152 GNUNET_PQ_make_prepare ("store_records",
153 "INSERT INTO ns098records"
154 " (zone_private_key, pkey, rvalue, record_count, record_data, label)"
155 " VALUES ($1, $2, $3, $4, $5, $6)"
156 " ON CONFLICT ON CONSTRAINT zl"
158 " SET pkey=$2,rvalue=$3,record_count=$4,record_data=$5"
159 " WHERE ns098records.zone_private_key = $1"
160 " AND ns098records.label = $6",
162 GNUNET_PQ_make_prepare ("delete_records",
163 "DELETE FROM ns098records "
164 "WHERE zone_private_key=$1 AND label=$2",
166 GNUNET_PQ_make_prepare ("zone_to_name",
167 "SELECT seq,record_count,record_data,label FROM ns098records"
168 " WHERE zone_private_key=$1 AND pkey=$2",
170 GNUNET_PQ_make_prepare ("iterate_zone",
171 "SELECT seq,record_count,record_data,label FROM ns098records "
172 "WHERE zone_private_key=$1 AND seq > $2 ORDER BY seq ASC LIMIT $3",
174 GNUNET_PQ_make_prepare ("iterate_all_zones",
175 "SELECT seq,record_count,record_data,label,zone_private_key"
176 " FROM ns098records WHERE seq > $1 ORDER BY seq ASC LIMIT $2",
178 GNUNET_PQ_make_prepare ("lookup_label",
179 "SELECT seq,record_count,record_data,label "
180 "FROM ns098records WHERE zone_private_key=$1 AND label=$2",
182 GNUNET_PQ_PREPARED_STATEMENT_END
186 GNUNET_PQ_prepare_statements (plugin->dbh,
189 PQfinish (plugin->dbh);
191 return GNUNET_SYSERR;
200 * Store a record in the datastore. Removes any existing record in the
201 * same zone with the same name.
203 * @param cls closure (internal context for the plugin)
204 * @param zone_key private key of the zone
205 * @param label name that is being mapped (at most 255 characters long)
206 * @param rd_count number of entries in @a rd array
207 * @param rd array of records with data to store
208 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
211 namestore_postgres_store_records (void *cls,
212 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
214 unsigned int rd_count,
215 const struct GNUNET_GNSRECORD_Data *rd)
217 struct Plugin *plugin = cls;
218 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
220 uint32_t rd_count32 = (uint32_t) rd_count;
226 for (unsigned int i=0;i<rd_count;i++)
227 if (GNUNET_GNSRECORD_TYPE_PKEY == rd[i].record_type)
229 GNUNET_break (sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) == rd[i].data_size);
230 GNUNET_memcpy (&pkey,
235 rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
237 data_size = GNUNET_GNSRECORD_records_get_size (rd_count,
242 return GNUNET_SYSERR;
244 if (data_size >= UINT16_MAX)
247 return GNUNET_SYSERR;
249 /* if record set is empty, delete existing records */
252 struct GNUNET_PQ_QueryParam params[] = {
253 GNUNET_PQ_query_param_auto_from_type (zone_key),
254 GNUNET_PQ_query_param_string (label),
255 GNUNET_PQ_query_param_end
257 enum GNUNET_DB_QueryStatus res;
259 res = GNUNET_PQ_eval_prepared_non_select (plugin->dbh,
262 if ( (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != res) &&
263 (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS != res) )
266 return GNUNET_SYSERR;
268 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
273 /* otherwise, UPSERT (i.e. UPDATE if exists, otherwise INSERT) */
275 char data[data_size];
276 struct GNUNET_PQ_QueryParam params[] = {
277 GNUNET_PQ_query_param_auto_from_type (zone_key),
278 GNUNET_PQ_query_param_auto_from_type (&pkey),
279 GNUNET_PQ_query_param_uint64 (&rvalue),
280 GNUNET_PQ_query_param_uint32 (&rd_count32),
281 GNUNET_PQ_query_param_fixed_size (data, data_size),
282 GNUNET_PQ_query_param_string (label),
283 GNUNET_PQ_query_param_end
285 enum GNUNET_DB_QueryStatus res;
288 ret = GNUNET_GNSRECORD_records_serialize (rd_count,
296 return GNUNET_SYSERR;
299 res = GNUNET_PQ_eval_prepared_non_select (plugin->dbh,
302 if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != res)
303 return GNUNET_SYSERR;
310 * Closure for #parse_result_call_iterator.
315 * Function to call for each result.
317 GNUNET_NAMESTORE_RecordIterator iter;
320 * Closure for @e iter.
325 * Zone key, NULL if part of record.
327 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key;
330 * Number of results still to return (counted down by
331 * number of results given to iterator).
338 * A statement has been run. We should evaluate the result, and if possible
339 * call the @a iter in @a cls with the result.
341 * @param cls closure of type `struct ParserContext *`
342 * @param result the postgres result
343 * @param num_result the number of results in @a result
346 parse_result_call_iterator (void *cls,
348 unsigned int num_results)
350 struct ParserContext *pc = cls;
352 if (NULL == pc->iter)
353 return; /* no need to do more work */
354 for (unsigned int i=0;i<num_results;i++)
359 uint32_t record_count;
361 struct GNUNET_CRYPTO_EcdsaPrivateKey zk;
362 struct GNUNET_PQ_ResultSpec rs_with_zone[] = {
363 GNUNET_PQ_result_spec_uint64 ("seq", &serial),
364 GNUNET_PQ_result_spec_uint32 ("record_count", &record_count),
365 GNUNET_PQ_result_spec_variable_size ("record_data", &data, &data_size),
366 GNUNET_PQ_result_spec_string ("label", &label),
367 GNUNET_PQ_result_spec_auto_from_type ("zone_private_key", &zk),
368 GNUNET_PQ_result_spec_end
370 struct GNUNET_PQ_ResultSpec rs_without_zone[] = {
371 GNUNET_PQ_result_spec_uint64 ("seq", &serial),
372 GNUNET_PQ_result_spec_uint32 ("record_count", &record_count),
373 GNUNET_PQ_result_spec_variable_size ("record_data", &data, &data_size),
374 GNUNET_PQ_result_spec_string ("label", &label),
375 GNUNET_PQ_result_spec_end
377 struct GNUNET_PQ_ResultSpec *rs;
379 rs = (NULL == pc->zone_key) ? rs_with_zone : rs_without_zone;
381 GNUNET_PQ_extract_result (res,
389 if (record_count > 64 * 1024)
391 /* sanity check, don't stack allocate far too much just
392 because database might contain a large value here */
394 GNUNET_PQ_cleanup_result (rs);
399 struct GNUNET_GNSRECORD_Data rd[GNUNET_NZL(record_count)];
402 GNUNET_GNSRECORD_records_deserialize (data_size,
408 GNUNET_PQ_cleanup_result (rs);
411 pc->iter (pc->iter_cls,
413 (NULL == pc->zone_key) ? &zk : pc->zone_key,
418 GNUNET_PQ_cleanup_result (rs);
420 pc->limit -= num_results;
425 * Lookup records in the datastore for which we are the authority.
427 * @param cls closure (internal context for the plugin)
428 * @param zone private key of the zone
429 * @param label name of the record in the zone
430 * @param iter function to call with the result
431 * @param iter_cls closure for @a iter
432 * @return #GNUNET_OK on success, #GNUNET_NO for no results, else #GNUNET_SYSERR
435 namestore_postgres_lookup_records (void *cls,
436 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
438 GNUNET_NAMESTORE_RecordIterator iter,
441 struct Plugin *plugin = cls;
442 struct GNUNET_PQ_QueryParam params[] = {
443 GNUNET_PQ_query_param_auto_from_type (zone),
444 GNUNET_PQ_query_param_string (label),
445 GNUNET_PQ_query_param_end
447 struct ParserContext pc;
448 enum GNUNET_DB_QueryStatus res;
453 return GNUNET_SYSERR;
456 pc.iter_cls = iter_cls;
458 res = GNUNET_PQ_eval_prepared_multi_select (plugin->dbh,
461 &parse_result_call_iterator,
464 return GNUNET_SYSERR;
465 if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == res)
472 * Iterate over the results for a particular key and zone in the
473 * datastore. Will return at most one result to the iterator.
475 * @param cls closure (internal context for the plugin)
476 * @param zone hash of public key of the zone, NULL to iterate over all zones
477 * @param serial serial number to exclude in the list of all matching records
478 * @param limit maximum number of results to fetch
479 * @param iter function to call with the result
480 * @param iter_cls closure for @a iter
481 * @return #GNUNET_OK on success, #GNUNET_NO if there were no more results, #GNUNET_SYSERR on error
484 namestore_postgres_iterate_records (void *cls,
485 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
488 GNUNET_NAMESTORE_RecordIterator iter,
491 struct Plugin *plugin = cls;
492 enum GNUNET_DB_QueryStatus res;
493 struct ParserContext pc;
496 pc.iter_cls = iter_cls;
501 struct GNUNET_PQ_QueryParam params_without_zone[] = {
502 GNUNET_PQ_query_param_uint64 (&serial),
503 GNUNET_PQ_query_param_uint64 (&limit),
504 GNUNET_PQ_query_param_end
507 res = GNUNET_PQ_eval_prepared_multi_select (plugin->dbh,
510 &parse_result_call_iterator,
515 struct GNUNET_PQ_QueryParam params_with_zone[] = {
516 GNUNET_PQ_query_param_auto_from_type (zone),
517 GNUNET_PQ_query_param_uint64 (&serial),
518 GNUNET_PQ_query_param_uint64 (&limit),
519 GNUNET_PQ_query_param_end
522 res = GNUNET_PQ_eval_prepared_multi_select (plugin->dbh,
525 &parse_result_call_iterator,
529 return GNUNET_SYSERR;
531 if ( (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == res) ||
539 * Look for an existing PKEY delegation record for a given public key.
540 * Returns at most one result to the iterator.
542 * @param cls closure (internal context for the plugin)
543 * @param zone private key of the zone to look up in, never NULL
544 * @param value_zone public key of the target zone (value), never NULL
545 * @param iter function to call with the result
546 * @param iter_cls closure for @a iter
547 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
550 namestore_postgres_zone_to_name (void *cls,
551 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
552 const struct GNUNET_CRYPTO_EcdsaPublicKey *value_zone,
553 GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
555 struct Plugin *plugin = cls;
556 struct GNUNET_PQ_QueryParam params[] = {
557 GNUNET_PQ_query_param_auto_from_type (zone),
558 GNUNET_PQ_query_param_auto_from_type (value_zone),
559 GNUNET_PQ_query_param_end
561 enum GNUNET_DB_QueryStatus res;
562 struct ParserContext pc;
565 pc.iter_cls = iter_cls;
567 res = GNUNET_PQ_eval_prepared_multi_select (plugin->dbh,
570 &parse_result_call_iterator,
573 return GNUNET_SYSERR;
579 * Shutdown database connection and associate data
582 * @param plugin the plugin context (state for this module)
585 database_shutdown (struct Plugin *plugin)
587 PQfinish (plugin->dbh);
593 * Entry point for the plugin.
595 * @param cls the `struct GNUNET_NAMESTORE_PluginEnvironment*`
596 * @return NULL on error, othrewise the plugin context
599 libgnunet_plugin_namestore_postgres_init (void *cls)
601 static struct Plugin plugin;
602 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
603 struct GNUNET_NAMESTORE_PluginFunctions *api;
605 if (NULL != plugin.cfg)
606 return NULL; /* can only initialize once! */
607 memset (&plugin, 0, sizeof (struct Plugin));
609 if (GNUNET_OK != database_setup (&plugin))
611 database_shutdown (&plugin);
614 api = GNUNET_new (struct GNUNET_NAMESTORE_PluginFunctions);
616 api->store_records = &namestore_postgres_store_records;
617 api->iterate_records = &namestore_postgres_iterate_records;
618 api->zone_to_name = &namestore_postgres_zone_to_name;
619 api->lookup_records = &namestore_postgres_lookup_records;
620 LOG (GNUNET_ERROR_TYPE_INFO,
621 "Postgres namestore plugin running\n");
627 * Exit point from the plugin.
629 * @param cls the plugin context (as returned by "init")
630 * @return always NULL
633 libgnunet_plugin_namestore_postgres_done (void *cls)
635 struct GNUNET_NAMESTORE_PluginFunctions *api = cls;
636 struct Plugin *plugin = api->cls;
638 database_shutdown (plugin);
641 LOG (GNUNET_ERROR_TYPE_DEBUG,
642 "Postgres namestore plugin is finished\n");
646 /* end of plugin_namestore_postgres.c */