aboutsummaryrefslogtreecommitdiff
path: root/src/stasis/plugin_anastasis_postgres.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/stasis/plugin_anastasis_postgres.c')
-rw-r--r--src/stasis/plugin_anastasis_postgres.c144
1 files changed, 135 insertions, 9 deletions
diff --git a/src/stasis/plugin_anastasis_postgres.c b/src/stasis/plugin_anastasis_postgres.c
index b2391f8..801552d 100644
--- a/src/stasis/plugin_anastasis_postgres.c
+++ b/src/stasis/plugin_anastasis_postgres.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of Anastasis 2 This file is part of Anastasis
3 Copyright (C) 2020, 2021 Anastasis SARL 3 Copyright (C) 2020, 2021, 2022 Anastasis SARL
4 4
5 Anastasis is free software; you can redistribute it and/or modify it under the 5 Anastasis is free software; you can redistribute it and/or modify it under the
6 terms of the GNU Affero General Public License as published by the Free Software 6 terms of the GNU Affero General Public License as published by the Free Software
@@ -15,8 +15,7 @@
15*/ 15*/
16/** 16/**
17 * @file stasis/plugin_anastasis_postgres.c 17 * @file stasis/plugin_anastasis_postgres.c
18 * @brief database helper functions for postgres used by the anastasis 18 * @brief database helper functions for postgres used by GNU Anastasis
19 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
20 * @author Christian Grothoff 19 * @author Christian Grothoff
21 * @author Marcello Stanisci 20 * @author Marcello Stanisci
22 */ 21 */
@@ -306,7 +305,6 @@ prepare_statements (void *cls)
306 ") VALUES " 305 ") VALUES "
307 "($1, $2, $3, $4, $5, $6);", 306 "($1, $2, $3, $4, $5, $6);",
308 6), 307 6),
309
310 GNUNET_PQ_make_prepare ("test_auth_iban_payment", 308 GNUNET_PQ_make_prepare ("test_auth_iban_payment",
311 "SELECT" 309 "SELECT"
312 " credit_val" 310 " credit_val"
@@ -328,8 +326,6 @@ prepare_statements (void *cls)
328 ") VALUES " 326 ") VALUES "
329 "($1, $2, $3, $4, $5, $6, $7);", 327 "($1, $2, $3, $4, $5, $6, $7);",
330 7), 328 7),
331
332
333 GNUNET_PQ_make_prepare ("recovery_document_insert", 329 GNUNET_PQ_make_prepare ("recovery_document_insert",
334 "INSERT INTO anastasis_recoverydocument " 330 "INSERT INTO anastasis_recoverydocument "
335 "(user_id" 331 "(user_id"
@@ -337,9 +333,10 @@ prepare_statements (void *cls)
337 ",account_sig" 333 ",account_sig"
338 ",recovery_data_hash" 334 ",recovery_data_hash"
339 ",recovery_data" 335 ",recovery_data"
336 ",recovery_meta_data"
340 ") VALUES " 337 ") VALUES "
341 "($1, $2, $3, $4, $5);", 338 "($1, $2, $3, $4, $5, $6);",
342 5), 339 6),
343 GNUNET_PQ_make_prepare ("truth_select", 340 GNUNET_PQ_make_prepare ("truth_select",
344 "SELECT " 341 "SELECT "
345 " method_name" 342 " method_name"
@@ -631,7 +628,7 @@ postgres_preflight (void *cls)
631 * must point to a constant 628 * must point to a constant
632 * @return #GNUNET_OK on success 629 * @return #GNUNET_OK on success
633 */ 630 */
634static int 631static enum GNUNET_GenericReturnValue
635begin_transaction (void *cls, 632begin_transaction (void *cls,
636 const char *name) 633 const char *name)
637{ 634{
@@ -834,6 +831,8 @@ postgres_store_recovery_document (
834 const struct GNUNET_HashCode *recovery_data_hash, 831 const struct GNUNET_HashCode *recovery_data_hash,
835 const void *recovery_data, 832 const void *recovery_data,
836 size_t recovery_data_size, 833 size_t recovery_data_size,
834 const void *recovery_meta_data,
835 size_t recovery_meta_data_size,
837 const struct ANASTASIS_PaymentSecretP *payment_secret, 836 const struct ANASTASIS_PaymentSecretP *payment_secret,
838 uint32_t *version) 837 uint32_t *version)
839{ 838{
@@ -1017,6 +1016,10 @@ postgres_store_recovery_document (
1017 GNUNET_PQ_query_param_auto_from_type (recovery_data_hash), 1016 GNUNET_PQ_query_param_auto_from_type (recovery_data_hash),
1018 GNUNET_PQ_query_param_fixed_size (recovery_data, 1017 GNUNET_PQ_query_param_fixed_size (recovery_data,
1019 recovery_data_size), 1018 recovery_data_size),
1019 (NULL == recovery_meta_data)
1020 ? GNUNET_PQ_query_param_null ()
1021 : GNUNET_PQ_query_param_fixed_size (recovery_meta_data,
1022 recovery_meta_data_size),
1020 GNUNET_PQ_query_param_end 1023 GNUNET_PQ_query_param_end
1021 }; 1024 };
1022 1025
@@ -2193,6 +2196,128 @@ postgres_get_latest_recovery_document (
2193 2196
2194 2197
2195/** 2198/**
2199 * Closure for meta_iterator().
2200 */
2201struct MetaIteratorContext
2202{
2203 /**
2204 * Function to call on each result.
2205 */
2206 ANASTASIS_DB_RecoveryMetaCallback cb;
2207
2208 /**
2209 * Closure for @e cb.
2210 */
2211 void *cb_cls;
2212
2213 /**
2214 * Set to true on database failure.
2215 */
2216 bool db_failure;
2217};
2218
2219
2220/**
2221 * Helper function for #postgres_get_recovery_meta_data().
2222 * To be called with the results of a SELECT statement
2223 * that has returned @a num_results results.
2224 *
2225 * @param cls closure of type `struct MetaIteratorContext *`
2226 * @param result the postgres result
2227 * @param num_results the number of results in @a result
2228 */
2229static void
2230meta_iterator (void *cls,
2231 PGresult *result,
2232 unsigned int num_results)
2233{
2234 struct MetaIteratorContext *ctx = cls;
2235
2236 for (unsigned int i = 0; i<num_results; i++)
2237 {
2238 uint32_t version;
2239 void *meta_data = NULL;
2240 size_t meta_data_size = 0;
2241 bool unused = false;
2242 struct GNUNET_PQ_ResultSpec rs[] = {
2243 GNUNET_PQ_result_spec_uint32 ("version",
2244 &version),
2245 GNUNET_PQ_result_spec_allow_null (
2246 GNUNET_PQ_result_spec_variable_size ("recovery_meta_data",
2247 &meta_data,
2248 &meta_data_size),
2249 &unused),
2250 GNUNET_PQ_result_spec_end
2251 };
2252 enum GNUNET_GenericReturnValue ret;
2253
2254 if (GNUNET_OK !=
2255 GNUNET_PQ_extract_result (result,
2256 rs,
2257 i))
2258 {
2259 GNUNET_break (0);
2260 ctx->db_failure = true;
2261 return;
2262 }
2263 ret = ctx->cb (ctx->cb_cls,
2264 version,
2265 meta_data,
2266 meta_data_size);
2267 GNUNET_PQ_cleanup_result (rs);
2268 if (GNUNET_OK != ret)
2269 break;
2270 }
2271}
2272
2273
2274/**
2275 * Fetch recovery document meta data for user. Returns
2276 * meta data in descending order from @a max_version.
2277 * The size of the result set may be limited.
2278 *
2279 * @param cls closure
2280 * @param account_pub public key of the user's account
2281 * @param max_version the maximum version number the user requests
2282 * @param cb function to call on each result
2283 * @param cb_cls closure for @a cb
2284 * @return transaction status
2285 */
2286static enum GNUNET_DB_QueryStatus
2287postgres_get_recovery_meta_data (
2288 void *cls,
2289 const struct ANASTASIS_CRYPTO_AccountPublicKeyP *account_pub,
2290 uint32_t max_version,
2291 ANASTASIS_DB_RecoveryMetaCallback cb,
2292 void *cb_cls)
2293{
2294 struct PostgresClosure *pg = cls;
2295 struct MetaIteratorContext ctx = {
2296 .cb = cb,
2297 .cb_cls = cb_cls
2298 };
2299 struct GNUNET_PQ_QueryParam params[] = {
2300 GNUNET_PQ_query_param_auto_from_type (account_pub),
2301 GNUNET_PQ_query_param_uint32 (&max_version),
2302 GNUNET_PQ_query_param_end
2303 };
2304 enum GNUNET_DB_QueryStatus qs;
2305
2306 check_connection (pg);
2307 qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
2308 "recoverydocument_select_meta",
2309 params,
2310 &meta_iterator,
2311 &ctx);
2312 if (qs < 0)
2313 return qs;
2314 if (ctx.db_failure)
2315 return GNUNET_DB_STATUS_HARD_ERROR;
2316 return qs;
2317}
2318
2319
2320/**
2196 * Fetch recovery document for user according given version. 2321 * Fetch recovery document for user according given version.
2197 * 2322 *
2198 * @param cls closure 2323 * @param cls closure
@@ -2811,6 +2936,7 @@ libanastasis_plugin_db_postgres_init (void *cls)
2811 plugin->get_escrow_challenge = &postgres_get_escrow_challenge; 2936 plugin->get_escrow_challenge = &postgres_get_escrow_challenge;
2812 plugin->get_key_share = &postgres_get_key_share; 2937 plugin->get_key_share = &postgres_get_key_share;
2813 plugin->get_latest_recovery_document = &postgres_get_latest_recovery_document; 2938 plugin->get_latest_recovery_document = &postgres_get_latest_recovery_document;
2939 plugin->get_recovery_meta_data = &postgres_get_recovery_meta_data;
2814 plugin->get_recovery_document = &postgres_get_recovery_document; 2940 plugin->get_recovery_document = &postgres_get_recovery_document;
2815 plugin->lookup_account = &postgres_lookup_account; 2941 plugin->lookup_account = &postgres_lookup_account;
2816 plugin->check_payment_identifier = &postgres_check_payment_identifier; 2942 plugin->check_payment_identifier = &postgres_check_payment_identifier;