anastasis

Credential backup and recovery protocol and service
Log | Files | Refs | Submodules | README | LICENSE

commit aa9e1d0e40684eda203846981f2b24e17043f003
parent 376cf70c1a73f9597979d6d9b9fe0440a9691880
Author: Christian Grothoff <christian@grothoff.org>
Date:   Wed, 22 Jul 2026 03:07:05 +0200

stasis: improve naming conventions of DB functions

Diffstat:
Acontrib/check-db-naming.py | 165+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mmeson.build | 2+-
Msrc/authorization/anastasis-helper-authorization-iban.c | 4++--
Msrc/authorization/anastasis_authorization_plugin_email.c | 2+-
Msrc/authorization/anastasis_authorization_plugin_file.c | 2+-
Msrc/authorization/anastasis_authorization_plugin_iban.c | 8++++----
Msrc/authorization/anastasis_authorization_plugin_post.c | 2+-
Msrc/authorization/anastasis_authorization_plugin_sms.c | 2+-
Msrc/backend/anastasis-httpd_policy-meta.c | 6+++---
Msrc/backend/anastasis-httpd_policy-upload.c | 24+++++++++++++-----------
Msrc/backend/anastasis-httpd_policy.c | 4++--
Msrc/backend/anastasis-httpd_truth-challenge.c | 28++++++++++++++--------------
Msrc/backend/anastasis-httpd_truth-solve.c | 32++++++++++++++++----------------
Msrc/backend/anastasis-httpd_truth-upload.c | 12++++++------
Msrc/cli/setup.sh | 2+-
Msrc/cli/test_anastasis_reducer_enter_secret.sh | 2+-
Msrc/cli/test_anastasis_reducer_recovery_enter_user_attributes.sh | 2+-
Dsrc/include/anastasis/anastasis-database/challenge_gc.h | 37-------------------------------------
Dsrc/include/anastasis/anastasis-database/check_challenge_payment.h | 44--------------------------------------------
Dsrc/include/anastasis/anastasis-database/check_payment_identifier.h | 44--------------------------------------------
Dsrc/include/anastasis/anastasis-database/check_truth_upload_paid.h | 41-----------------------------------------
Msrc/include/anastasis/anastasis-database/common.h | 18+-----------------
Dsrc/include/anastasis/anastasis-database/create_challenge_code.h | 53-----------------------------------------------------
Asrc/include/anastasis/anastasis-database/do_insert_recovery_document.h | 54++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/include/anastasis/anastasis-database/do_update_account_lifetime.h | 62++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/include/anastasis/anastasis-database/do_verify_challenge_code.h | 47+++++++++++++++++++++++++++++++++++++++++++++++
Asrc/include/anastasis/anastasis-database/gc_challenge_codes.h | 37+++++++++++++++++++++++++++++++++++++
Asrc/include/anastasis/anastasis-database/get_account.h | 46++++++++++++++++++++++++++++++++++++++++++++++
Asrc/include/anastasis/anastasis-database/get_challenge_payment.h | 44++++++++++++++++++++++++++++++++++++++++++++
Dsrc/include/anastasis/anastasis-database/get_escrow_challenge.h | 47-----------------------------------------------
Asrc/include/anastasis/anastasis-database/get_exists_challenge_code_satisfied.h | 45+++++++++++++++++++++++++++++++++++++++++++++
Dsrc/include/anastasis/anastasis-database/get_key_share.h | 41-----------------------------------------
Asrc/include/anastasis/anastasis-database/get_last_auth_iban_in_row.h | 45+++++++++++++++++++++++++++++++++++++++++++++
Dsrc/include/anastasis/anastasis-database/get_last_auth_iban_payment_row.h | 45---------------------------------------------
Asrc/include/anastasis/anastasis-database/get_pending_challenge_payment.h | 41+++++++++++++++++++++++++++++++++++++++++
Asrc/include/anastasis/anastasis-database/get_recdoc_payment.h | 44++++++++++++++++++++++++++++++++++++++++++++
Dsrc/include/anastasis/anastasis-database/get_recovery_meta_data.h | 39---------------------------------------
Asrc/include/anastasis/anastasis-database/get_truth.h | 47+++++++++++++++++++++++++++++++++++++++++++++++
Asrc/include/anastasis/anastasis-database/get_truth_key_share.h | 41+++++++++++++++++++++++++++++++++++++++++
Asrc/include/anastasis/anastasis-database/get_truth_payment.h | 41+++++++++++++++++++++++++++++++++++++++++
Dsrc/include/anastasis/anastasis-database/increment_lifetime.h | 46----------------------------------------------
Asrc/include/anastasis/anastasis-database/insert_auth_iban_in.h | 49+++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/include/anastasis/anastasis-database/insert_challenge_code.h | 53+++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/include/anastasis/anastasis-database/insert_challenge_payment.h | 43+++++++++++++++++++++++++++++++++++++++++++
Asrc/include/anastasis/anastasis-database/insert_recdoc_payment.h | 47+++++++++++++++++++++++++++++++++++++++++++++++
Asrc/include/anastasis/anastasis-database/insert_truth.h | 51+++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/include/anastasis/anastasis-database/insert_truth_payment.h | 43+++++++++++++++++++++++++++++++++++++++++++
Asrc/include/anastasis/anastasis-database/iterate_auth_iban_transfers.h | 50++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/include/anastasis/anastasis-database/iterate_recovery_meta_data.h | 47+++++++++++++++++++++++++++++++++++++++++++++++
Dsrc/include/anastasis/anastasis-database/lookup_account.h | 46----------------------------------------------
Dsrc/include/anastasis/anastasis-database/lookup_challenge_payment.h | 41-----------------------------------------
Dsrc/include/anastasis/anastasis-database/mark_challenge_code_satisfied.h | 42------------------------------------------
Dsrc/include/anastasis/anastasis-database/mark_challenge_sent.h | 42------------------------------------------
Dsrc/include/anastasis/anastasis-database/record_auth_iban_payment.h | 49-------------------------------------------------
Dsrc/include/anastasis/anastasis-database/record_challenge_payment.h | 43-------------------------------------------
Dsrc/include/anastasis/anastasis-database/record_challenge_refund.h | 41-----------------------------------------
Dsrc/include/anastasis/anastasis-database/record_recdoc_payment.h | 47-----------------------------------------------
Dsrc/include/anastasis/anastasis-database/record_truth_upload_payment.h | 43-------------------------------------------
Dsrc/include/anastasis/anastasis-database/store_recovery_document.h | 54------------------------------------------------------
Dsrc/include/anastasis/anastasis-database/store_truth.h | 51---------------------------------------------------
Dsrc/include/anastasis/anastasis-database/test_auth_iban_payment.h | 39---------------------------------------
Dsrc/include/anastasis/anastasis-database/test_challenge_code_satisfied.h | 45---------------------------------------------
Dsrc/include/anastasis/anastasis-database/update_challenge_payment.h | 41-----------------------------------------
Dsrc/include/anastasis/anastasis-database/update_lifetime.h | 44--------------------------------------------
Asrc/include/anastasis/anastasis-database/update_to_challenge_code_satisfied.h | 42++++++++++++++++++++++++++++++++++++++++++
Asrc/include/anastasis/anastasis-database/update_to_challenge_code_sent.h | 42++++++++++++++++++++++++++++++++++++++++++
Asrc/include/anastasis/anastasis-database/update_to_challenge_payment_paid.h | 41+++++++++++++++++++++++++++++++++++++++++
Asrc/include/anastasis/anastasis-database/update_to_challenge_payment_refunded.h | 41+++++++++++++++++++++++++++++++++++++++++
Dsrc/include/anastasis/anastasis-database/verify_challenge_code.h | 39---------------------------------------
Msrc/include/anastasis_database_lib.h | 51+++++++++++++++++++++++++--------------------------
Msrc/include/meson.build | 51+++++++++++++++++++++++++--------------------------
Asrc/stasis/amalgamate-sql.sh | 46++++++++++++++++++++++++++++++++++++++++++++++
Dsrc/stasis/anastasis-db_challenge_gc.c | 55-------------------------------------------------------
Dsrc/stasis/anastasis-db_check_challenge_payment.c | 71-----------------------------------------------------------------------
Dsrc/stasis/anastasis-db_check_payment_identifier.c | 71-----------------------------------------------------------------------
Dsrc/stasis/anastasis-db_check_truth_upload_paid.c | 66------------------------------------------------------------------
Dsrc/stasis/anastasis-db_create_challenge_code.c | 195-------------------------------------------------------------------------------
Dsrc/stasis/anastasis-db_create_tables.c | 55-------------------------------------------------------
Dsrc/stasis/anastasis-db_drop_tables.c | 34----------------------------------
Dsrc/stasis/anastasis-db_gc.c | 76----------------------------------------------------------------------------
Dsrc/stasis/anastasis-db_get_escrow_challenge.c | 76----------------------------------------------------------------------------
Dsrc/stasis/anastasis-db_get_key_share.c | 64----------------------------------------------------------------
Dsrc/stasis/anastasis-db_get_last_auth_iban_payment_row.c | 69---------------------------------------------------------------------
Dsrc/stasis/anastasis-db_get_latest_recovery_document.c | 85-------------------------------------------------------------------------------
Dsrc/stasis/anastasis-db_get_recovery_document.c | 80-------------------------------------------------------------------------------
Dsrc/stasis/anastasis-db_get_recovery_meta_data.c | 156-------------------------------------------------------------------------------
Dsrc/stasis/anastasis-db_increment_lifetime.c | 245-------------------------------------------------------------------------------
Dsrc/stasis/anastasis-db_lookup_account.c | 136-------------------------------------------------------------------------------
Dsrc/stasis/anastasis-db_lookup_challenge_payment.c | 78------------------------------------------------------------------------------
Dsrc/stasis/anastasis-db_mark_challenge_code_satisfied.c | 66------------------------------------------------------------------
Dsrc/stasis/anastasis-db_mark_challenge_sent.c | 98-------------------------------------------------------------------------------
Dsrc/stasis/anastasis-db_preflight.c | 58----------------------------------------------------------
Dsrc/stasis/anastasis-db_record_auth_iban_payment.c | 76----------------------------------------------------------------------------
Dsrc/stasis/anastasis-db_record_challenge_payment.c | 67-------------------------------------------------------------------
Dsrc/stasis/anastasis-db_record_challenge_refund.c | 63---------------------------------------------------------------
Dsrc/stasis/anastasis-db_record_recdoc_payment.c | 155-------------------------------------------------------------------------------
Dsrc/stasis/anastasis-db_record_truth_upload_payment.c | 66------------------------------------------------------------------
Dsrc/stasis/anastasis-db_store_recovery_document.c | 312-------------------------------------------------------------------------------
Dsrc/stasis/anastasis-db_store_truth.c | 80-------------------------------------------------------------------------------
Dsrc/stasis/anastasis-db_test_auth_iban_payment.c | 156-------------------------------------------------------------------------------
Dsrc/stasis/anastasis-db_test_challenge_code_satisfied.c | 69---------------------------------------------------------------------
Dsrc/stasis/anastasis-db_update_challenge_payment.c | 65-----------------------------------------------------------------
Dsrc/stasis/anastasis-db_update_lifetime.c | 201-------------------------------------------------------------------------------
Dsrc/stasis/anastasis-db_verify_challenge_code.c | 211-------------------------------------------------------------------------------
Asrc/stasis/commit.sql | 17+++++++++++++++++
Asrc/stasis/create_tables.c | 53+++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/stasis/do_insert_recovery_document.c | 312+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/stasis/do_update_account_lifetime.c | 99+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/stasis/do_update_account_lifetime.sql | 79+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/stasis/do_verify_challenge_code.c | 210+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/stasis/drop_tables.c | 34++++++++++++++++++++++++++++++++++
Asrc/stasis/gc.c | 76++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/stasis/gc_challenge_codes.c | 55+++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/stasis/get_account.c | 136+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/stasis/get_challenge_payment.c | 71+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/stasis/get_exists_challenge_code_satisfied.c | 69+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/stasis/get_last_auth_iban_in_row.c | 69+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/stasis/get_latest_recovery_document.c | 85+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/stasis/get_pending_challenge_payment.c | 78++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/stasis/get_recdoc_payment.c | 71+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/stasis/get_recovery_document.c | 80+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/stasis/get_truth.c | 76++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/stasis/get_truth_key_share.c | 64++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/stasis/get_truth_payment.c | 66++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/stasis/insert_auth_iban_in.c | 76++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/stasis/insert_challenge_code.c | 195+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/stasis/insert_challenge_payment.c | 67+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/stasis/insert_recdoc_payment.c | 155+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/stasis/insert_truth.c | 80+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/stasis/insert_truth_payment.c | 66++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/stasis/iterate_auth_iban_transfers.c | 149+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/stasis/iterate_recovery_meta_data.c | 155+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/stasis/meson.build | 96+++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------
Asrc/stasis/preflight.c | 58++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/stasis/procedures-prelude.sql | 19+++++++++++++++++++
Msrc/stasis/test_anastasis_db.c | 55+++++++++++++++++++++++++++++++++++++++----------------
Asrc/stasis/update_to_challenge_code_satisfied.c | 66++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/stasis/update_to_challenge_code_sent.c | 98+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/stasis/update_to_challenge_payment_paid.c | 65+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/stasis/update_to_challenge_payment_refunded.c | 63+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
140 files changed, 4688 insertions(+), 4682 deletions(-)

diff --git a/contrib/check-db-naming.py b/contrib/check-db-naming.py @@ -0,0 +1,165 @@ +#!/usr/bin/env python3 +"""Enforce the database layer naming convention. + +Rules checked: + + 1. every public function's name starts with the name of the file it lives in + 2. the file's own name uses a prefix from the closed set, or is exempt + 3. iterate_ iff the function takes a caller-supplied callback; get_ iff not + 4. a .h exists beside every .c, and every .sql that is not schema or + boilerplate is named after the .c that runs it + 5. every prepared statement is named after the function preparing it + 6. no prepared statement name is used in two files + 7. no numeric suffixes and no repeated library prefix + 8. no orphaned header: every .h in the API directory has an implementation + +Run from the top of the source tree; exits non-zero on any violation. +""" +import re +import sys +import pathlib + +# ---------------------------------------------------------------- CONFIG -- +# (impl dir, header dir, symbol prefix, .c basename prefix) +LAYERS = [ + ("src/stasis", "src/include/anastasis/anastasis-database", + "ANASTASIS_DB_", ""), +] + +# Prefixes that describe what the caller gets. +PREFIXES = ("get_", "get_count_", "get_exists_", "iterate_", + "insert_", "update_", "update_to_", "delete_", "do_") + +# Not CRUD: transaction control, DDL, events, memory, session plumbing, +# cancellable handle APIs, and multi-function helper modules. Keep this list +# short and justified -- every entry is a rule the checker stops enforcing. +EXEMPT = { + # transaction control (declared in transaction.h, implemented in + # anastasis-db_pg.c) + "start", "commit", "rollback", "check_connection", "preflight", + # DDL and maintenance + "create_tables", "drop_tables", "gc", "gc_challenge_codes", + # events (declared in event.h, implemented in anastasis-db_pg.c) + "event_listen", "event_listen_cancel", "event_notify", + # subsystem lifecycle + "init", "init_admin", "fini", +} + +# Headers that hold shared declarations rather than one public function. +HDR_EXEMPT = {"common", "event", "transaction"} + +# Files that are not part of the API surface at all. Listed by name on +# purpose: a "skip anything called test_*" rule would silently exempt any +# future API function whose name starts with test_, which is exactly how +# ANASTASIS_DB_test_auth_iban_payment() and +# ANASTASIS_DB_test_challenge_code_satisfied() escaped notice. +SKIP = {"anastasis-db_pg", "anastasis-dbinit", "test_anastasis_db"} + +# SQL that belongs to the schema or to the amalgamation boilerplate rather +# than to one API function. +SQL_NOT_A_FUNCTION = {"versioning", "drop", "commit", "procedures-prelude"} +SQL_SCHEMA_RE = re.compile(r"^stasis-\d+$") +# ------------------------------------------------------------ END CONFIG -- + +PREP = re.compile(r'PREPARE\s*\(\s*"([A-Za-z0-9_]+)"', re.S) + +errors = [] + + +def check_layer(impl_dir, hdr_dir, prefix, fpfx): + impl, hdr = pathlib.Path(impl_dir), pathlib.Path(hdr_dir) + if not impl.is_dir(): + return + decl = re.compile(r"^" + re.escape(prefix) + r"(\w+)\s*\((.*?)\)\s*;", + re.S | re.M) + stmt_owner = {} + stems = set() + + for c in sorted(impl.glob("*.c")): + if c.stem in SKIP: + continue + if fpfx and not c.stem.startswith(fpfx): + continue + stem = c.stem[len(fpfx):] if fpfx else c.stem + stems.add(stem) + + # 2. prefix from the closed set, or exempt + if stem not in EXEMPT and not stem.startswith(PREFIXES): + errors.append(f"{c}: '{stem}' uses no approved prefix") + + # 7. numeric suffix + if re.search(r"\d$", stem): + errors.append(f"{c}: '{stem}' ends in a digit") + + # 4. matching header + h = hdr / (stem + ".h") + if stem not in EXEMPT and not h.exists(): + errors.append(f"{c}: no matching header {h}") + + text = c.read_text(errors="replace") + + # 5. prepared statements named after the function + for s in dict.fromkeys(PREP.findall(text)): + if s != stem and not s.startswith(stem + "_"): + errors.append( + f"{c}: prepared statement '{s}' is not named after " + f"'{stem}'") + # 6. cross-file uniqueness + if s in stmt_owner and stmt_owner[s] != str(c): + errors.append( + f"{c}: prepared statement '{s}' also prepared in " + f"{stmt_owner[s]}") + stmt_owner[s] = str(c) + + if not h.exists(): + continue + htext = re.sub(r"/\*.*?\*/", "", h.read_text(errors="replace"), + flags=re.S) + for name, params in decl.findall(htext): + # 1. declared names belong to their file. Exempt entries are the + # documented multi-function modules, so this does not apply there. + if (stem not in EXEMPT + and name != stem and not name.startswith(stem + "_")): + errors.append( + f"{h}: declares '{name}', which does not belong to " + f"'{stem}'") + # 3. iterate_ iff callback + has_cb = bool(re.search(r"\w*(Callback|Iterator)\s+\w+", params)) + if name.startswith("iterate_") and not has_cb: + errors.append( + f"{h}: '{name}' is iterate_ but takes no callback") + if name.startswith("get_") and has_cb: + errors.append(f"{h}: '{name}' takes a callback; use iterate_") + + # 4. an amalgamated .sql is named after the .c that runs it, so that a + # renamed function cannot leave its stored procedure behind. + for s in sorted(impl.glob("*.sql")): + if s.stem in SQL_NOT_A_FUNCTION or SQL_SCHEMA_RE.match(s.stem): + continue + if s.stem not in stems: + errors.append(f"{s}: no {impl}/{s.stem}.c runs this SQL") + + # 8. every header belongs to an implementation + for h in sorted(hdr.glob("*.h")): + if h.stem in HDR_EXEMPT: + continue + if not (impl / (fpfx + h.stem + ".c")).exists(): + errors.append(f"{h}: no implementation {impl / (fpfx + h.stem)}.c") + + # 7. repeated library prefix anywhere in the tree + head = prefix.split("_")[0] + for f in list(impl.glob("*.[ch]")) + list(hdr.glob("*.h")): + if "unc-backup" in f.name: + continue + if re.search(r"\b" + re.escape(head) + r"_" + re.escape(prefix), + f.read_text(errors="replace")): + errors.append(f"{f}: repeated '{prefix}' prefix") + + +for a in LAYERS: + check_layer(*a) + +for e in errors: + print("ERROR:", e) +print(f"\ndb-naming: {len(errors)} violation(s)") +sys.exit(1 if errors else 0) diff --git a/meson.build b/meson.build @@ -314,7 +314,7 @@ if not get_option('only-doc') ['libanastasisutil', '0:0:0'], ['libanastasisauthorization', '0:0:0'], ['libanastasiseufin', '0:0:0'], - ['libanastasisdb', '2:0:0'], + ['libanastasisdb', '3:0:0'], ['libanastasisrest', '0:0:0'], ['libanastasistesting', '0:0:0'], ['libanastasisredux', '0:0:0'], diff --git a/src/authorization/anastasis-helper-authorization-iban.c b/src/authorization/anastasis-helper-authorization-iban.c @@ -280,7 +280,7 @@ history_cb (void *cls, char *dcanon = payto_get_iban (details->debit_account_uri); char *ccanon = payto_get_iban (details->credit_account_uri); - qs = ANASTASIS_DB_record_auth_iban_payment ( + qs = ANASTASIS_DB_insert_auth_iban_in ( serial_id, details->wire_subject, &details->amount, @@ -412,7 +412,7 @@ run (void *cls, { enum GNUNET_DB_QueryStatus qs; - qs = ANASTASIS_DB_get_last_auth_iban_payment_row ( + qs = ANASTASIS_DB_get_last_auth_iban_in_row ( authorization_iban, &latest_row_off); if (qs < 0) diff --git a/src/authorization/anastasis_authorization_plugin_email.c b/src/authorization/anastasis_authorization_plugin_email.c @@ -262,7 +262,7 @@ email_start (void *cls, plugin is already happy (no additional requirements), so mark this challenge as already satisfied from the start. */ - qs = ANASTASIS_DB_mark_challenge_code_satisfied ( + qs = ANASTASIS_DB_update_to_challenge_code_satisfied ( truth_uuid, code); if (qs <= 0) diff --git a/src/authorization/anastasis_authorization_plugin_file.c b/src/authorization/anastasis_authorization_plugin_file.c @@ -138,7 +138,7 @@ file_start (void *cls, plugin is already happy (no additional requirements), so mark this challenge as already satisfied from the start. */ - qs = ANASTASIS_DB_mark_challenge_code_satisfied ( + qs = ANASTASIS_DB_update_to_challenge_code_satisfied ( truth_uuid, code); if (qs <= 0) diff --git a/src/authorization/anastasis_authorization_plugin_iban.c b/src/authorization/anastasis_authorization_plugin_iban.c @@ -375,7 +375,7 @@ test_wire_transfers (struct ANASTASIS_AUTHORIZATION_State *as) limit = GNUNET_TIME_absolute_to_timestamp ( GNUNET_TIME_absolute_subtract (now, CODE_VALIDITY_PERIOD)); - qs = ANASTASIS_DB_test_auth_iban_payment ( + qs = ANASTASIS_DB_iterate_auth_iban_transfers ( as->iban_number, limit, &check_payment_ok, @@ -396,7 +396,7 @@ test_wire_transfers (struct ANASTASIS_AUTHORIZATION_State *as) case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: break; } - qs = ANASTASIS_DB_mark_challenge_code_satisfied ( + qs = ANASTASIS_DB_update_to_challenge_code_satisfied ( &as->truth_uuid, as->code); GNUNET_break (qs > 0); @@ -544,7 +544,7 @@ iban_solve (struct ANASTASIS_AUTHORIZATION_State *as, after = GNUNET_TIME_absolute_to_timestamp ( GNUNET_TIME_absolute_subtract (now, CODE_VALIDITY_PERIOD)); - qs = ANASTASIS_DB_test_challenge_code_satisfied ( + qs = ANASTASIS_DB_get_exists_challenge_code_satisfied ( &as->truth_uuid, as->code, after); @@ -555,7 +555,7 @@ iban_solve (struct ANASTASIS_AUTHORIZATION_State *as, mres = TALER_MHD_reply_with_error (connection, MHD_HTTP_INTERNAL_SERVER_ERROR, TALER_EC_GENERIC_DB_FETCH_FAILED, - "test challenge code satisfied"); + "get exists challenge code satisfied"); if (MHD_YES != mres) return ANASTASIS_AUTHORIZATION_SRES_FAILED_REPLY_FAILED; return ANASTASIS_AUTHORIZATION_SRES_FAILED; diff --git a/src/authorization/anastasis_authorization_plugin_post.c b/src/authorization/anastasis_authorization_plugin_post.c @@ -292,7 +292,7 @@ post_start (void *cls, plugin is already happy (no additional requirements), so mark this challenge as already satisfied from the start. */ - qs = ANASTASIS_DB_mark_challenge_code_satisfied ( + qs = ANASTASIS_DB_update_to_challenge_code_satisfied ( truth_uuid, code); if (qs <= 0) diff --git a/src/authorization/anastasis_authorization_plugin_sms.c b/src/authorization/anastasis_authorization_plugin_sms.c @@ -261,7 +261,7 @@ sms_start (void *cls, plugin is already happy (no additional requirements), so mark this challenge as already satisfied from the start. */ - qs = ANASTASIS_DB_mark_challenge_code_satisfied ( + qs = ANASTASIS_DB_update_to_challenge_code_satisfied ( truth_uuid, code); if (qs <= 0) diff --git a/src/backend/anastasis-httpd_policy-meta.c b/src/backend/anastasis-httpd_policy-meta.c @@ -114,7 +114,7 @@ return_policy_meta ( } result = json_object (); GNUNET_assert (NULL != result); - qs = ANASTASIS_DB_get_recovery_meta_data ( + qs = ANASTASIS_DB_iterate_recovery_meta_data ( account_pub, max_version, &build_meta_result, @@ -160,7 +160,7 @@ AH_policy_meta_get ( uint32_t version; struct GNUNET_TIME_Timestamp expiration; - as = ANASTASIS_DB_lookup_account ( + as = ANASTASIS_DB_get_account ( account_pub, &expiration, &recovery_data_hash, @@ -177,7 +177,7 @@ AH_policy_meta_get ( return TALER_MHD_reply_with_error (connection, MHD_HTTP_INTERNAL_SERVER_ERROR, TALER_EC_GENERIC_DB_FETCH_FAILED, - "lookup account"); + "get account"); case ANASTASIS_DB_ACCOUNT_STATUS_NO_RESULTS: return TALER_MHD_reply_with_error (connection, MHD_HTTP_NOT_FOUND, diff --git a/src/backend/anastasis-httpd_policy-upload.c b/src/backend/anastasis-httpd_policy-upload.c @@ -364,7 +364,7 @@ proposal_cb (struct PolicyUploadContext *puc, "Storing payment request for order `%s'\n", por->details.ok.order_id); - qs = ANASTASIS_DB_record_recdoc_payment ( + qs = ANASTASIS_DB_insert_recdoc_payment ( &puc->account, (uint32_t) AH_post_counter, &puc->payment_identifier, @@ -374,7 +374,7 @@ proposal_cb (struct PolicyUploadContext *puc, GNUNET_break (0); puc->resp = TALER_MHD_make_error ( TALER_EC_GENERIC_DB_STORE_FAILED, - "record recdoc payment"); + "insert recdoc payment"); puc->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR; return; } @@ -474,9 +474,10 @@ check_payment_cb (struct PolicyUploadContext *puc, paid_until = GNUNET_TIME_relative_add (paid_until, GNUNET_TIME_UNIT_WEEKS); - qs = ANASTASIS_DB_increment_lifetime ( + qs = ANASTASIS_DB_do_update_account_lifetime ( &puc->account, &puc->payment_identifier, + GNUNET_TIME_timestamp_get (), paid_until, &puc->paid_until); if (0 <= qs) @@ -484,7 +485,7 @@ check_payment_cb (struct PolicyUploadContext *puc, GNUNET_break (0); puc->resp = TALER_MHD_make_error ( TALER_EC_GENERIC_DB_FETCH_FAILED, - "increment lifetime"); + "do update account lifetime"); puc->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR; return; /* continue as planned */ } @@ -870,7 +871,7 @@ AH_handler_policy_post ( bool valid_counter; enum GNUNET_DB_QueryStatus qs; - qs = ANASTASIS_DB_check_payment_identifier ( + qs = ANASTASIS_DB_get_recdoc_payment ( &puc->payment_identifier, &paid, &valid_counter); @@ -917,7 +918,7 @@ AH_handler_policy_post ( /* Cost is zero, fake "zero" payment having happened */ GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Policy upload is free, allowing upload without payment\n"); - qs = ANASTASIS_DB_record_recdoc_payment ( + qs = ANASTASIS_DB_insert_recdoc_payment ( account_pub, AH_post_counter, &puc->payment_identifier, @@ -935,11 +936,12 @@ AH_handler_policy_post ( GNUNET_TIME_relative2s (rel, true), ANASTASIS_MAX_YEARS_STORAGE); - puc->paid_until = GNUNET_TIME_relative_to_timestamp (rel); - qs = ANASTASIS_DB_update_lifetime ( + qs = ANASTASIS_DB_do_update_account_lifetime ( account_pub, &puc->payment_identifier, - puc->paid_until); + GNUNET_TIME_relative_to_timestamp (rel), + GNUNET_TIME_UNIT_ZERO, + &puc->paid_until); if (qs <= 0) { GNUNET_break (0); @@ -959,7 +961,7 @@ AH_handler_policy_post ( struct GNUNET_TIME_Timestamp now; struct GNUNET_TIME_Relative rem; - as = ANASTASIS_DB_lookup_account ( + as = ANASTASIS_DB_get_account ( account_pub, &puc->paid_until, &hash, @@ -1104,7 +1106,7 @@ AH_handler_policy_post ( GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Uploading recovery document\n"); - ss = ANASTASIS_DB_store_recovery_document ( + ss = ANASTASIS_DB_do_insert_recovery_document ( &puc->account, &puc->account_sig, &puc->new_policy_upload_hash, diff --git a/src/backend/anastasis-httpd_policy.c b/src/backend/anastasis-httpd_policy.c @@ -170,7 +170,7 @@ AH_policy_get (struct MHD_Connection *connection, uint32_t version; struct GNUNET_TIME_Timestamp expiration; - as = ANASTASIS_DB_lookup_account ( + as = ANASTASIS_DB_get_account ( account_pub, &expiration, &recovery_data_hash, @@ -187,7 +187,7 @@ AH_policy_get (struct MHD_Connection *connection, return TALER_MHD_reply_with_error (connection, MHD_HTTP_INTERNAL_SERVER_ERROR, TALER_EC_GENERIC_DB_FETCH_FAILED, - "lookup account"); + "get account"); case ANASTASIS_DB_ACCOUNT_STATUS_NO_RESULTS: { struct MHD_Response *resp; diff --git a/src/backend/anastasis-httpd_truth-challenge.c b/src/backend/anastasis-httpd_truth-challenge.c @@ -388,7 +388,7 @@ refund_cb ( GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Refund `%s' succeeded\n", re->order_id); - qs = ANASTASIS_DB_record_challenge_refund ( + qs = ANASTASIS_DB_update_to_challenge_payment_refunded ( &re->truth_uuid, &re->payment_identifier); switch (qs) @@ -640,7 +640,7 @@ proposal_cb (struct ChallengeContext *gc, gc->response_code = MHD_HTTP_BAD_GATEWAY; return; } - qs = ANASTASIS_DB_record_challenge_payment ( + qs = ANASTASIS_DB_insert_challenge_payment ( &gc->truth_uuid, &gc->payment_identifier, &gc->challenge_cost); @@ -648,7 +648,7 @@ proposal_cb (struct ChallengeContext *gc, { GNUNET_break (0); gc->resp = TALER_MHD_make_error (TALER_EC_GENERIC_DB_STORE_FAILED, - "record challenge payment"); + "insert challenge payment"); gc->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR; return; } @@ -730,7 +730,7 @@ check_payment_cb (struct ChallengeContext *gc, { enum GNUNET_DB_QueryStatus qs; - qs = ANASTASIS_DB_update_challenge_payment ( + qs = ANASTASIS_DB_update_to_challenge_payment_paid ( &gc->truth_uuid, &gc->payment_identifier); if (0 <= qs) @@ -742,7 +742,7 @@ check_payment_cb (struct ChallengeContext *gc, } GNUNET_break (0); gc->resp = TALER_MHD_make_error (TALER_EC_GENERIC_DB_STORE_FAILED, - "update challenge payment"); + "update to challenge payment paid"); gc->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR; return; /* continue as planned */ } @@ -773,7 +773,7 @@ begin_payment (struct ChallengeContext *gc) enum GNUNET_DB_QueryStatus qs; char *order_id; - qs = ANASTASIS_DB_lookup_challenge_payment ( + qs = ANASTASIS_DB_get_pending_challenge_payment ( &gc->truth_uuid, &gc->payment_identifier); if (qs < 0) @@ -782,7 +782,7 @@ begin_payment (struct ChallengeContext *gc) return TALER_MHD_reply_with_error (gc->connection, MHD_HTTP_INTERNAL_SERVER_ERROR, TALER_EC_GENERIC_DB_FETCH_FAILED, - "lookup challenge payment"); + "get pending challenge payment"); } GNUNET_assert (! gc->in_list); gc->in_list = true; @@ -937,7 +937,7 @@ run_authorization_process (struct MHD_Connection *connection, "Authorization request %llu for %s sent successfully\n", (unsigned long long) gc->code, TALER_B2S (&gc->truth_uuid)); - qs = ANASTASIS_DB_mark_challenge_sent ( + qs = ANASTASIS_DB_update_to_challenge_code_sent ( &gc->payment_identifier, &gc->truth_uuid, gc->code); @@ -959,7 +959,7 @@ run_authorization_process (struct MHD_Connection *connection, return MHD_YES; case ANASTASIS_AUTHORIZATION_CRES_SUCCESS_REPLY_FAILED: /* Challenge sent successfully */ - qs = ANASTASIS_DB_mark_challenge_sent ( + qs = ANASTASIS_DB_update_to_challenge_code_sent ( &gc->payment_identifier, &gc->truth_uuid, gc->code); @@ -1098,7 +1098,7 @@ AH_handler_truth_challenge ( enum GNUNET_DB_QueryStatus qs; char *method; - qs = ANASTASIS_DB_get_escrow_challenge ( + qs = ANASTASIS_DB_get_truth ( &gc->truth_uuid, &encrypted_truth, &encrypted_truth_size, @@ -1187,7 +1187,7 @@ AH_handler_truth_challenge ( "Beginning payment, client did not provide payment identifier\n"); return begin_payment (gc); } - qs = ANASTASIS_DB_check_challenge_payment ( + qs = ANASTASIS_DB_get_challenge_payment ( &gc->payment_identifier, &gc->truth_uuid, &paid); @@ -1201,7 +1201,7 @@ AH_handler_truth_challenge ( return TALER_MHD_reply_with_error (gc->connection, MHD_HTTP_INTERNAL_SERVER_ERROR, TALER_EC_GENERIC_DB_FETCH_FAILED, - "check challenge payment"); + "get challenge payment"); case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: /* Create fresh payment identifier (cannot trust client) */ GNUNET_log (GNUNET_ERROR_TYPE_INFO, @@ -1283,7 +1283,7 @@ AH_handler_truth_challenge ( struct GNUNET_TIME_Timestamp transmission_date; enum GNUNET_DB_QueryStatus qs; - qs = ANASTASIS_DB_create_challenge_code ( + qs = ANASTASIS_DB_insert_challenge_code ( &gc->truth_uuid, gc->authorization->code_rotation_period, gc->authorization->code_validity_period, @@ -1299,7 +1299,7 @@ AH_handler_truth_challenge ( return TALER_MHD_reply_with_error (gc->connection, MHD_HTTP_INTERNAL_SERVER_ERROR, TALER_EC_GENERIC_DB_FETCH_FAILED, - "create_challenge_code"); + "insert_challenge_code"); case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: /* 0 == retry_counter of existing challenge => rate limit exceeded */ GNUNET_free (decrypted_truth); diff --git a/src/backend/anastasis-httpd_truth-solve.c b/src/backend/anastasis-httpd_truth-solve.c @@ -475,7 +475,7 @@ proposal_cb (struct SolveContext *gc, gc->response_code = MHD_HTTP_BAD_GATEWAY; return; } - qs = ANASTASIS_DB_record_challenge_payment ( + qs = ANASTASIS_DB_insert_challenge_payment ( &gc->truth_uuid, &gc->payment_identifier, &gc->challenge_cost); @@ -483,7 +483,7 @@ proposal_cb (struct SolveContext *gc, { GNUNET_break (0); gc->resp = TALER_MHD_make_error (TALER_EC_GENERIC_DB_STORE_FAILED, - "record challenge payment"); + "insert challenge payment"); gc->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR; return; } @@ -565,7 +565,7 @@ check_payment_cb (struct SolveContext *gc, { enum GNUNET_DB_QueryStatus qs; - qs = ANASTASIS_DB_update_challenge_payment ( + qs = ANASTASIS_DB_update_to_challenge_payment_paid ( &gc->truth_uuid, &gc->payment_identifier); if (0 <= qs) @@ -577,7 +577,7 @@ check_payment_cb (struct SolveContext *gc, } GNUNET_break (0); gc->resp = TALER_MHD_make_error (TALER_EC_GENERIC_DB_STORE_FAILED, - "update challenge payment"); + "update to challenge payment paid"); gc->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR; return; /* continue as planned */ } @@ -608,7 +608,7 @@ begin_payment (struct SolveContext *gc) enum GNUNET_DB_QueryStatus qs; char *order_id; - qs = ANASTASIS_DB_lookup_challenge_payment ( + qs = ANASTASIS_DB_get_pending_challenge_payment ( &gc->truth_uuid, &gc->payment_identifier); if (qs < 0) @@ -617,7 +617,7 @@ begin_payment (struct SolveContext *gc) return TALER_MHD_reply_with_error (gc->connection, MHD_HTTP_INTERNAL_SERVER_ERROR, TALER_EC_GENERIC_DB_FETCH_FAILED, - "lookup challenge payment"); + "get pending challenge payment"); } GNUNET_assert (! gc->in_list); gc->in_list = true; @@ -725,7 +725,7 @@ return_key_share ( { enum GNUNET_DB_QueryStatus qs; - qs = ANASTASIS_DB_get_key_share ( + qs = ANASTASIS_DB_get_truth_key_share ( truth_uuid, &encrypted_keyshare); switch (qs) @@ -887,7 +887,7 @@ rate_limit (struct SolveContext *gc) uint64_t dummy; rt = GNUNET_TIME_UNIT_FOREVER_TS; - qs = ANASTASIS_DB_create_challenge_code ( + qs = ANASTASIS_DB_insert_challenge_code ( &gc->truth_uuid, MAX_QUESTION_FREQ, GNUNET_TIME_UNIT_HOURS, @@ -901,7 +901,7 @@ rate_limit (struct SolveContext *gc) TALER_MHD_reply_with_error (gc->connection, MHD_HTTP_INTERNAL_SERVER_ERROR, TALER_EC_GENERIC_DB_FETCH_FAILED, - "create_challenge_code (for rate limiting)")) + "insert_challenge_code (for rate limiting)")) ? GNUNET_NO : GNUNET_SYSERR; } @@ -917,7 +917,7 @@ rate_limit (struct SolveContext *gc) /* decrement trial counter */ ANASTASIS_hash_answer (code + 1, /* always use wrong answer */ &hc); - cs = ANASTASIS_DB_verify_challenge_code ( + cs = ANASTASIS_DB_do_verify_challenge_code ( &gc->truth_uuid, &hc, &dummy, @@ -934,7 +934,7 @@ rate_limit (struct SolveContext *gc) TALER_MHD_reply_with_error (gc->connection, MHD_HTTP_INTERNAL_SERVER_ERROR, TALER_EC_GENERIC_DB_FETCH_FAILED, - "verify_challenge_code")) + "do_verify_challenge_code")) ? GNUNET_NO : GNUNET_SYSERR; case ANASTASIS_DB_CODE_STATUS_NO_RESULTS: @@ -1257,7 +1257,7 @@ AH_handler_truth_solve ( enum GNUNET_DB_QueryStatus qs; char *method; - qs = ANASTASIS_DB_get_escrow_challenge ( + qs = ANASTASIS_DB_get_truth ( &gc->truth_uuid, &encrypted_truth, &encrypted_truth_size, @@ -1328,7 +1328,7 @@ AH_handler_truth_solve ( "Beginning payment, client did not provide payment identifier\n"); return begin_payment (gc); } - qs = ANASTASIS_DB_check_challenge_payment ( + qs = ANASTASIS_DB_get_challenge_payment ( &gc->payment_identifier, &gc->truth_uuid, &paid); @@ -1342,7 +1342,7 @@ AH_handler_truth_solve ( return TALER_MHD_reply_with_error (gc->connection, MHD_HTTP_INTERNAL_SERVER_ERROR, TALER_EC_GENERIC_DB_FETCH_FAILED, - "check challenge payment"); + "get challenge payment"); case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: /* Create fresh payment identifier (cannot trust client) */ GNUNET_log (GNUNET_ERROR_TYPE_INFO, @@ -1433,7 +1433,7 @@ AH_handler_truth_solve ( } /* random code, check against database */ - cs = ANASTASIS_DB_verify_challenge_code ( + cs = ANASTASIS_DB_do_verify_challenge_code ( &gc->truth_uuid, &gc->challenge_response, &code, @@ -1455,7 +1455,7 @@ AH_handler_truth_solve ( return TALER_MHD_reply_with_error (gc->connection, MHD_HTTP_INTERNAL_SERVER_ERROR, TALER_EC_GENERIC_DB_FETCH_FAILED, - "verify_challenge_code"); + "do_verify_challenge_code"); case ANASTASIS_DB_CODE_STATUS_NO_RESULTS: GNUNET_free (decrypted_truth); GNUNET_log (GNUNET_ERROR_TYPE_INFO, diff --git a/src/backend/anastasis-httpd_truth-upload.c b/src/backend/anastasis-httpd_truth-upload.c @@ -357,7 +357,7 @@ check_payment_cb (struct TruthUploadContext *tuc, with 365 days. */ paid_until = GNUNET_TIME_relative_add (paid_until, GNUNET_TIME_UNIT_WEEKS); - qs = ANASTASIS_DB_record_truth_upload_payment ( + qs = ANASTASIS_DB_insert_truth_payment ( &tuc->truth_uuid, &osr->details.ok.details.paid.deposit_total, paid_until); @@ -367,7 +367,7 @@ check_payment_cb (struct TruthUploadContext *tuc, tuc->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR; tuc->resp = TALER_MHD_make_error ( TALER_EC_GENERIC_DB_STORE_FAILED, - "record_truth_upload_payment"); + "insert_truth_payment"); break; } } @@ -651,7 +651,7 @@ AH_handler_truth_post ( = GNUNET_TIME_relative_to_timestamp ( GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_YEARS, storage_years)); - qs = ANASTASIS_DB_check_truth_upload_paid ( + qs = ANASTASIS_DB_get_truth_payment ( truth_uuid, &paid_until); if (qs < 0) @@ -717,7 +717,7 @@ AH_handler_truth_post ( { enum GNUNET_DB_QueryStatus qs; - qs = ANASTASIS_DB_store_truth ( + qs = ANASTASIS_DB_insert_truth ( truth_uuid, &key_share_data, (NULL == truth_mime) @@ -737,7 +737,7 @@ AH_handler_truth_post ( return TALER_MHD_reply_with_error (connection, MHD_HTTP_INTERNAL_SERVER_ERROR, TALER_EC_GENERIC_DB_INVARIANT_FAILURE, - "store_truth"); + "insert_truth"); case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: { void *xtruth; @@ -747,7 +747,7 @@ AH_handler_truth_post ( bool ok = false; if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == - ANASTASIS_DB_get_escrow_challenge ( + ANASTASIS_DB_get_truth ( truth_uuid, &xtruth, &xtruth_size, diff --git a/src/cli/setup.sh b/src/cli/setup.sh @@ -39,7 +39,7 @@ function setup() SETUP_PID=$! # Close FD3 exec 3>&- - sed -u '/<<READY>>/ q' <&4 + sed -u '/READY:/ q' <&4 # Close FD4 exec 4>&- echo "Test system ready" >&2 diff --git a/src/cli/test_anastasis_reducer_enter_secret.sh b/src/cli/test_anastasis_reducer_enter_secret.sh @@ -79,7 +79,7 @@ CONF_4="test_anastasis_reducer_4.conf" CONF="test_reducer.conf.edited" TMP_DIR=$(mktemp -p "${TMPDIR:-/tmp}" -d keys-tmp-XXXXXX) -WALLET_DB=$(mktemp -p "${TMPDIR:-/tmp}" test_reducer_walletXXXXXX.json) +WALLET_DB=$(mktemp -p "${TMPDIR:-/tmp}" test_reducer_wallet.json-XXXXXX) TFILE=$(mktemp -p "${TMPDIR:-/tmp}" test_reducer_statePPXXXXXX) UFILE=$(mktemp -p "${TMPDIR:-/tmp}" test_reducer_stateBFXXXXXX) diff --git a/src/cli/test_anastasis_reducer_recovery_enter_user_attributes.sh b/src/cli/test_anastasis_reducer_recovery_enter_user_attributes.sh @@ -109,7 +109,7 @@ CONF="$(mktemp -p "${TMPDIR:-/tmp}" test_reducerXXXXXX.conf)" cp test_reducer.conf "$CONF" TMP_DIR=$(mktemp -p "${TMPDIR:-/tmp}" -d keys-tmp-XXXXXX) -WALLET_DB=$(mktemp -p "${TMPDIR:-/tmp}" test_reducer_walletXXXXXX.json) +WALLET_DB=$(mktemp -p "${TMPDIR:-/tmp}" test_reducer_wallet.json-XXXXXX) B1FILE=$(mktemp -p "${TMPDIR:-/tmp}" test_reducer_stateB1XXXXXX) B2FILE=$(mktemp -p "${TMPDIR:-/tmp}" test_reducer_stateB2XXXXXX) R1FILE=$(mktemp -p "${TMPDIR:-/tmp}" test_reducer_stateR1XXXXXX) diff --git a/src/include/anastasis/anastasis-database/challenge_gc.h b/src/include/anastasis/anastasis-database/challenge_gc.h @@ -1,37 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file include/anastasis/anastasis-database/challenge_gc.h - * @brief Anastasis database: challenge gc - * @author Christian Grothoff - */ -#ifndef ANASTASIS_DATABASE_CHALLENGE_GC_H -#define ANASTASIS_DATABASE_CHALLENGE_GC_H - -#include <gnunet/gnunet_util_lib.h> -#include <gnunet/gnunet_db_lib.h> -#include "anastasis_service.h" -#include "anastasis/anastasis-database/common.h" - -/** - * Function called to remove all expired codes from the database. - * - * @return transaction status - */ -enum GNUNET_DB_QueryStatus -ANASTASIS_DB_challenge_gc (void); - -#endif diff --git a/src/include/anastasis/anastasis-database/check_challenge_payment.h b/src/include/anastasis/anastasis-database/check_challenge_payment.h @@ -1,44 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file include/anastasis/anastasis-database/check_challenge_payment.h - * @brief Anastasis database: check challenge payment - * @author Christian Grothoff - */ -#ifndef ANASTASIS_DATABASE_CHECK_CHALLENGE_PAYMENT_H -#define ANASTASIS_DATABASE_CHECK_CHALLENGE_PAYMENT_H - -#include <gnunet/gnunet_util_lib.h> -#include <gnunet/gnunet_db_lib.h> -#include "anastasis_service.h" -#include "anastasis/anastasis-database/common.h" - -/** - * Check payment identifier. Used to check if a payment identifier given by - * the user is valid (existing and paid). - * - * @param payment_secret payment secret which the user must provide with every upload - * @param truth_uuid which truth should we check the payment status of - * @param[out] paid bool value to show if payment is paid - * @return transaction status - */ -enum GNUNET_DB_QueryStatus -ANASTASIS_DB_check_challenge_payment ( - const struct ANASTASIS_PaymentSecretP *payment_secret, - const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid, - bool *paid); - -#endif diff --git a/src/include/anastasis/anastasis-database/check_payment_identifier.h b/src/include/anastasis/anastasis-database/check_payment_identifier.h @@ -1,44 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file include/anastasis/anastasis-database/check_payment_identifier.h - * @brief Anastasis database: check payment identifier - * @author Christian Grothoff - */ -#ifndef ANASTASIS_DATABASE_CHECK_PAYMENT_IDENTIFIER_H -#define ANASTASIS_DATABASE_CHECK_PAYMENT_IDENTIFIER_H - -#include <gnunet/gnunet_util_lib.h> -#include <gnunet/gnunet_db_lib.h> -#include "anastasis_service.h" -#include "anastasis/anastasis-database/common.h" - -/** - * Check payment identifier. Used to check if a payment identifier given by - * the user is valid (existing and paid). - * - * @param payment_secret payment secret which the user must provide with every upload - * @param[out] paid bool value to show if payment is paid - * @param[out] valid_counter bool value to show if post_counter is > 0 - * @return transaction status - */ -enum GNUNET_DB_QueryStatus -ANASTASIS_DB_check_payment_identifier ( - const struct ANASTASIS_PaymentSecretP *payment_secret, - bool *paid, - bool *valid_counter); - -#endif diff --git a/src/include/anastasis/anastasis-database/check_truth_upload_paid.h b/src/include/anastasis/anastasis-database/check_truth_upload_paid.h @@ -1,41 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file include/anastasis/anastasis-database/check_truth_upload_paid.h - * @brief Anastasis database: check truth upload paid - * @author Christian Grothoff - */ -#ifndef ANASTASIS_DATABASE_CHECK_TRUTH_UPLOAD_PAID_H -#define ANASTASIS_DATABASE_CHECK_TRUTH_UPLOAD_PAID_H - -#include <gnunet/gnunet_util_lib.h> -#include <gnunet/gnunet_db_lib.h> -#include "anastasis_service.h" -#include "anastasis/anastasis-database/common.h" - -/** - * Inquire whether truth upload payment was made. - * - * @param uuid the truth's UUID - * @param[out] paid_until set for how long this truth is paid for - * @return transaction status - */ -enum GNUNET_DB_QueryStatus -ANASTASIS_DB_check_truth_upload_paid ( - const struct ANASTASIS_CRYPTO_TruthUUIDP *uuid, - struct GNUNET_TIME_Timestamp *paid_until); - -#endif diff --git a/src/include/anastasis/anastasis-database/common.h b/src/include/anastasis/anastasis-database/common.h @@ -126,22 +126,6 @@ enum ANASTASIS_DB_StoreStatus /** - * Function called on all pending payments for an account or challenge. - * - * @param cls closure - * @param timestamp for how long have we been waiting - * @param payment_secret payment secret / order id in the backend - * @param amount how much is the order for - */ -typedef void -(*ANASTASIS_DB_PaymentPendingIterator)( - void *cls, - struct GNUNET_TIME_Timestamp timestamp, - const struct ANASTASIS_PaymentSecretP *payment_secret, - const struct TALER_Amount *amount); - - -/** * Function called to test if a given wire transfer * satisfied the authentication requirement of the * IBAN plugin. @@ -152,7 +136,7 @@ typedef void * @return true if this wire transfer satisfied the authentication check */ typedef bool -(*ANASTASIS_DB_AuthIbanTransfercheck)( +(*ANASTASIS_DB_AuthIbanTransferCallback)( void *cls, const struct TALER_Amount *credit, const char *wire_subject); diff --git a/src/include/anastasis/anastasis-database/create_challenge_code.h b/src/include/anastasis/anastasis-database/create_challenge_code.h @@ -1,53 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file include/anastasis/anastasis-database/create_challenge_code.h - * @brief Anastasis database: create challenge code - * @author Christian Grothoff - */ -#ifndef ANASTASIS_DATABASE_CREATE_CHALLENGE_CODE_H -#define ANASTASIS_DATABASE_CREATE_CHALLENGE_CODE_H - -#include <gnunet/gnunet_util_lib.h> -#include <gnunet/gnunet_db_lib.h> -#include "anastasis_service.h" -#include "anastasis/anastasis-database/common.h" - -/** - * Create a new challenge code for a given challenge identified by the challenge - * public key. The function will first check if there is already a valid code - * for this challenge present and won't insert a new one in this case. - * - * @param truth_uuid the identifier for the challenge - * @param rotation_period for how long is the code available - * @param validity_period for how long is the code available - * @param retry_counter amount of retries allowed - * @param[out] retransmission_date when to next retransmit - * @param[out] code set to the code which will be checked for later - * @return transaction status, - * #GNUNET_DB_STATUS_SUCCESS_NO_RESULTS if we are out of valid tries, - * #GNUNET_DB_STATUS_SUCCESS_ONE_RESULT if @a code is now in the DB - */ -enum GNUNET_DB_QueryStatus -ANASTASIS_DB_create_challenge_code ( - const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid, - struct GNUNET_TIME_Relative rotation_period, - struct GNUNET_TIME_Relative validity_period, - uint32_t retry_counter, - struct GNUNET_TIME_Timestamp *retransmission_date, - uint64_t *code); - -#endif diff --git a/src/include/anastasis/anastasis-database/do_insert_recovery_document.h b/src/include/anastasis/anastasis-database/do_insert_recovery_document.h @@ -0,0 +1,54 @@ +/* + This file is part of Anastasis + Copyright (C) 2020-2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file include/anastasis/anastasis-database/do_insert_recovery_document.h + * @brief Anastasis database: do insert recovery document + * @author Christian Grothoff + */ +#ifndef ANASTASIS_DATABASE_DO_INSERT_RECOVERY_DOCUMENT_H +#define ANASTASIS_DATABASE_DO_INSERT_RECOVERY_DOCUMENT_H + +#include <gnunet/gnunet_util_lib.h> +#include <gnunet/gnunet_db_lib.h> +#include "anastasis_service.h" +#include "anastasis/anastasis-database/common.h" + +/** + * Store encrypted recovery document. + * + * @param account_pub public key of the user's account + * @param account_sig signature affirming storage request + * @param recovery_data_hash hash of @a data + * @param recovery_data contains encrypted_recovery_document + * @param recovery_data_size size of data blob + * @param payment_secret identifier for the payment, used to later charge on uploads + * @param[out] version set to the version assigned to the document by the database + * @return transaction status, 0 if upload could not be finished because @a payment_secret + * did not have enough upload left; HARD error if @a payment_secret is unknown, ... + */ +enum ANASTASIS_DB_StoreStatus +ANASTASIS_DB_do_insert_recovery_document ( + const struct ANASTASIS_CRYPTO_AccountPublicKeyP *account_pub, + const struct ANASTASIS_AccountSignatureP *account_sig, + const struct GNUNET_HashCode *recovery_data_hash, + const void *recovery_data, + size_t recovery_data_size, + const void *recovery_meta_data, + size_t recovery_meta_data_size, + const struct ANASTASIS_PaymentSecretP *payment_secret, + uint32_t *version); + +#endif diff --git a/src/include/anastasis/anastasis-database/do_update_account_lifetime.h b/src/include/anastasis/anastasis-database/do_update_account_lifetime.h @@ -0,0 +1,62 @@ +/* + This file is part of Anastasis + Copyright (C) 2020-2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file include/anastasis/anastasis-database/do_update_account_lifetime.h + * @brief Anastasis database: do update account lifetime + * @author Christian Grothoff + */ +#ifndef ANASTASIS_DATABASE_DO_UPDATE_ACCOUNT_LIFETIME_H +#define ANASTASIS_DATABASE_DO_UPDATE_ACCOUNT_LIFETIME_H + +#include <gnunet/gnunet_util_lib.h> +#include <gnunet/gnunet_db_lib.h> +#include "anastasis_service.h" +#include "anastasis/anastasis-database/common.h" + +/** + * Credit a recovery document payment and extend the account's lifetime + * accordingly. Does nothing if the payment is not new. + * + * The account is set to expire at + * + * MAX(current expiration, @a min_expiration) + @a lifetime + * + * (saturating at "forever"), and is created if it does not exist yet. + * Callers that bought a duration pass the current time as @a min_expiration + * and the duration as @a lifetime; callers that bought a fixed end date pass + * that date as @a min_expiration and #GNUNET_TIME_UNIT_ZERO as @a lifetime. + * + * @param account_pub which account received a payment + * @param payment_identifier proof of payment, must be unique and match pending payment + * @param min_expiration lower bound for the resulting expiration + * @param lifetime by how much to extend beyond @a min_expiration + * @param[out] paid_until set to the lifetime of the account after the + * operation; on a payment that was not new, to the lifetime it + * already had (zero if there is no such account) + * @return #GNUNET_DB_STATUS_SUCCESS_ONE_RESULT if the payment was credited + * and the lifetime extended, + * #GNUNET_DB_STATUS_SUCCESS_NO_RESULTS if the payment was unknown or + * had been credited before, in which case the lifetime is unchanged + */ +enum GNUNET_DB_QueryStatus +ANASTASIS_DB_do_update_account_lifetime ( + const struct ANASTASIS_CRYPTO_AccountPublicKeyP *account_pub, + const struct ANASTASIS_PaymentSecretP *payment_identifier, + struct GNUNET_TIME_Timestamp min_expiration, + struct GNUNET_TIME_Relative lifetime, + struct GNUNET_TIME_Timestamp *paid_until); + +#endif diff --git a/src/include/anastasis/anastasis-database/do_verify_challenge_code.h b/src/include/anastasis/anastasis-database/do_verify_challenge_code.h @@ -0,0 +1,47 @@ +/* + This file is part of Anastasis + Copyright (C) 2020-2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file include/anastasis/anastasis-database/do_do_verify_challenge_code.h + * @brief Anastasis database: do verify challenge code + * @author Christian Grothoff + */ +#ifndef ANASTASIS_DATABASE_DO_VERIFY_CHALLENGE_CODE_H +#define ANASTASIS_DATABASE_DO_VERIFY_CHALLENGE_CODE_H + +#include <gnunet/gnunet_util_lib.h> +#include <gnunet/gnunet_db_lib.h> +#include "anastasis_service.h" +#include "anastasis/anastasis-database/common.h" + +/** + * Verify the provided code with the code on the server. + * If the code matches the function will return with success, if the code + * does not match, the retry counter will be decreased by one. + * + * @param truth_uuid identification of the challenge which the code corresponds to + * @param hashed_code code which the user provided and wants to verify + * @param[out] code set to the original numeric code + * @param[out] satisfied set to true if the challenge is set to satisfied + * @return code validity status + */ +enum ANASTASIS_DB_CodeStatus +ANASTASIS_DB_do_verify_challenge_code ( + const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid, + const struct GNUNET_HashCode *hashed_code, + uint64_t *code, + bool *satisfied); + +#endif diff --git a/src/include/anastasis/anastasis-database/gc_challenge_codes.h b/src/include/anastasis/anastasis-database/gc_challenge_codes.h @@ -0,0 +1,37 @@ +/* + This file is part of Anastasis + Copyright (C) 2020-2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file include/anastasis/anastasis-database/gc_challenge_codes.h + * @brief Anastasis database: gc challenge codes + * @author Christian Grothoff + */ +#ifndef ANASTASIS_DATABASE_GC_CHALLENGE_CODES_H +#define ANASTASIS_DATABASE_GC_CHALLENGE_CODES_H + +#include <gnunet/gnunet_util_lib.h> +#include <gnunet/gnunet_db_lib.h> +#include "anastasis_service.h" +#include "anastasis/anastasis-database/common.h" + +/** + * Function called to remove all expired codes from the database. + * + * @return transaction status + */ +enum GNUNET_DB_QueryStatus +ANASTASIS_DB_gc_challenge_codes (void); + +#endif diff --git a/src/include/anastasis/anastasis-database/get_account.h b/src/include/anastasis/anastasis-database/get_account.h @@ -0,0 +1,46 @@ +/* + This file is part of Anastasis + Copyright (C) 2020-2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file include/anastasis/anastasis-database/get_account.h + * @brief Anastasis database: get account + * @author Christian Grothoff + */ +#ifndef ANASTASIS_DATABASE_GET_ACCOUNT_H +#define ANASTASIS_DATABASE_GET_ACCOUNT_H + +#include <gnunet/gnunet_util_lib.h> +#include <gnunet/gnunet_db_lib.h> +#include "anastasis_service.h" +#include "anastasis/anastasis-database/common.h" + +/** + * Check if an account exists, and if so, return the + * current @a recovery_document_hash. + * + * @param account_pub account identifier + * @param[out] paid_until until when is the account paid up? + * @param[out] recovery_data_hash set to hash of @a recovery document + * @param[out] version set to the recovery policy version + * @return transaction status + */ +enum ANASTASIS_DB_AccountStatus +ANASTASIS_DB_get_account ( + const struct ANASTASIS_CRYPTO_AccountPublicKeyP *account_pub, + struct GNUNET_TIME_Timestamp *paid_until, + struct GNUNET_HashCode *recovery_data_hash, + uint32_t *version); + +#endif diff --git a/src/include/anastasis/anastasis-database/get_challenge_payment.h b/src/include/anastasis/anastasis-database/get_challenge_payment.h @@ -0,0 +1,44 @@ +/* + This file is part of Anastasis + Copyright (C) 2020-2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file include/anastasis/anastasis-database/get_challenge_payment.h + * @brief Anastasis database: get challenge payment + * @author Christian Grothoff + */ +#ifndef ANASTASIS_DATABASE_GET_CHALLENGE_PAYMENT_H +#define ANASTASIS_DATABASE_GET_CHALLENGE_PAYMENT_H + +#include <gnunet/gnunet_util_lib.h> +#include <gnunet/gnunet_db_lib.h> +#include "anastasis_service.h" +#include "anastasis/anastasis-database/common.h" + +/** + * Check payment identifier. Used to check if a payment identifier given by + * the user is valid (existing and paid). + * + * @param payment_secret payment secret which the user must provide with every upload + * @param truth_uuid which truth should we check the payment status of + * @param[out] paid bool value to show if payment is paid + * @return transaction status + */ +enum GNUNET_DB_QueryStatus +ANASTASIS_DB_get_challenge_payment ( + const struct ANASTASIS_PaymentSecretP *payment_secret, + const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid, + bool *paid); + +#endif diff --git a/src/include/anastasis/anastasis-database/get_escrow_challenge.h b/src/include/anastasis/anastasis-database/get_escrow_challenge.h @@ -1,47 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file include/anastasis/anastasis-database/get_escrow_challenge.h - * @brief Anastasis database: get escrow challenge - * @author Christian Grothoff - */ -#ifndef ANASTASIS_DATABASE_GET_ESCROW_CHALLENGE_H -#define ANASTASIS_DATABASE_GET_ESCROW_CHALLENGE_H - -#include <gnunet/gnunet_util_lib.h> -#include <gnunet/gnunet_db_lib.h> -#include "anastasis_service.h" -#include "anastasis/anastasis-database/common.h" - -/** - * Get the encrypted truth to validate the challenge response - * - * @param truth_uuid the identifier for the Truth - * @param[out] truth contains the encrypted truth - * @param[out] truth_size size of the encrypted truth - * @param[out] truth_mime mime type of truth - * @param[out] method type of the challenge - * @return transaction status - */ -enum GNUNET_DB_QueryStatus -ANASTASIS_DB_get_escrow_challenge ( - const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid, - void **truth, - size_t *truth_size, - char **truth_mime, - char **method); - -#endif diff --git a/src/include/anastasis/anastasis-database/get_exists_challenge_code_satisfied.h b/src/include/anastasis/anastasis-database/get_exists_challenge_code_satisfied.h @@ -0,0 +1,45 @@ +/* + This file is part of Anastasis + Copyright (C) 2020-2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file include/anastasis/anastasis-database/get_exists_challenge_code_satisfied.h + * @brief Anastasis database: get exists challenge code satisfied + * @author Christian Grothoff + */ +#ifndef ANASTASIS_DATABASE_GET_EXISTS_CHALLENGE_CODE_SATISFIED_H +#define ANASTASIS_DATABASE_GET_EXISTS_CHALLENGE_CODE_SATISFIED_H + +#include <gnunet/gnunet_util_lib.h> +#include <gnunet/gnunet_db_lib.h> +#include "anastasis_service.h" +#include "anastasis/anastasis-database/common.h" + +/** + * Check if the 'satisfied' bit for the given challenge and code is + * 'true' and the challenge code is not yet expired. + * + * @param truth_uuid identification of the challenge which the code corresponds to + * @param code code which is now satisfied + * @return transaction status, + * #GNUNET_DB_STATUS_SUCCESS_NO_RESULTS if the challenge code is not satisfied or expired + * #GNUNET_DB_STATUS_SUCCESS_ONE_RESULT if the challenge code has been marked as satisfied + */ +enum GNUNET_DB_QueryStatus +ANASTASIS_DB_get_exists_challenge_code_satisfied ( + const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid, + const uint64_t code, + struct GNUNET_TIME_Timestamp after); + +#endif diff --git a/src/include/anastasis/anastasis-database/get_key_share.h b/src/include/anastasis/anastasis-database/get_key_share.h @@ -1,41 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file include/anastasis/anastasis-database/get_key_share.h - * @brief Anastasis database: get key share - * @author Christian Grothoff - */ -#ifndef ANASTASIS_DATABASE_GET_KEY_SHARE_H -#define ANASTASIS_DATABASE_GET_KEY_SHARE_H - -#include <gnunet/gnunet_util_lib.h> -#include <gnunet/gnunet_db_lib.h> -#include "anastasis_service.h" -#include "anastasis/anastasis-database/common.h" - -/** - * Lookup (encrypted) key share by @a truth_uuid. - * - * @param truth_uuid the identifier for the Truth - * @param[out] key_share contains the encrypted Keyshare - * @return transaction status - */ -enum GNUNET_DB_QueryStatus -ANASTASIS_DB_get_key_share ( - const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid, - struct ANASTASIS_CRYPTO_EncryptedKeyShareP *key_share); - -#endif diff --git a/src/include/anastasis/anastasis-database/get_last_auth_iban_in_row.h b/src/include/anastasis/anastasis-database/get_last_auth_iban_in_row.h @@ -0,0 +1,45 @@ +/* + This file is part of Anastasis + Copyright (C) 2020-2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file include/anastasis/anastasis-database/get_last_auth_iban_in_row.h + * @brief Anastasis database: get last auth iban in row + * @author Christian Grothoff + */ +#ifndef ANASTASIS_DATABASE_GET_LAST_AUTH_IBAN_IN_ROW_H +#define ANASTASIS_DATABASE_GET_LAST_AUTH_IBAN_IN_ROW_H + +#include <gnunet/gnunet_util_lib.h> +#include <gnunet/gnunet_db_lib.h> +#include "anastasis_service.h" +#include "anastasis/anastasis-database/common.h" + +/** + * Function to check the last known IBAN payment. + * + * @param credit_account which credit account to check + * @param[out] last_row set to the last known row + * @return transaction status, + * #GNUNET_DB_STATUS_SUCCESS_ONE_RESULT if @a cb + * returned 'true' once + * #GNUNET_DB_STATUS_SUCCESS_NO_RESULTS if no + * wire transfers existed for which @a cb returned true + */ +enum GNUNET_DB_QueryStatus +ANASTASIS_DB_get_last_auth_iban_in_row ( + const char *credit_account, + uint64_t *last_row); + +#endif diff --git a/src/include/anastasis/anastasis-database/get_last_auth_iban_payment_row.h b/src/include/anastasis/anastasis-database/get_last_auth_iban_payment_row.h @@ -1,45 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file include/anastasis/anastasis-database/get_last_auth_iban_payment_row.h - * @brief Anastasis database: get last auth iban payment row - * @author Christian Grothoff - */ -#ifndef ANASTASIS_DATABASE_GET_LAST_AUTH_IBAN_PAYMENT_ROW_H -#define ANASTASIS_DATABASE_GET_LAST_AUTH_IBAN_PAYMENT_ROW_H - -#include <gnunet/gnunet_util_lib.h> -#include <gnunet/gnunet_db_lib.h> -#include "anastasis_service.h" -#include "anastasis/anastasis-database/common.h" - -/** - * Function to check the last known IBAN payment. - * - * @param credit_account which credit account to check - * @param[out] last_row set to the last known row - * @return transaction status, - * #GNUNET_DB_STATUS_SUCCESS_ONE_RESULT if @a cb - * returned 'true' once - * #GNUNET_DB_STATUS_SUCCESS_NO_RESULTS if no - * wire transfers existed for which @a cb returned true - */ -enum GNUNET_DB_QueryStatus -ANASTASIS_DB_get_last_auth_iban_payment_row ( - const char *credit_account, - uint64_t *last_row); - -#endif diff --git a/src/include/anastasis/anastasis-database/get_pending_challenge_payment.h b/src/include/anastasis/anastasis-database/get_pending_challenge_payment.h @@ -0,0 +1,41 @@ +/* + This file is part of Anastasis + Copyright (C) 2020-2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file include/anastasis/anastasis-database/get_pending_challenge_payment.h + * @brief Anastasis database: get pending challenge payment + * @author Christian Grothoff + */ +#ifndef ANASTASIS_DATABASE_GET_PENDING_CHALLENGE_PAYMENT_H +#define ANASTASIS_DATABASE_GET_PENDING_CHALLENGE_PAYMENT_H + +#include <gnunet/gnunet_util_lib.h> +#include <gnunet/gnunet_db_lib.h> +#include "anastasis_service.h" +#include "anastasis/anastasis-database/common.h" + +/** + * Lookup pending payment for a certain challenge. + * + * @param truth_uuid identification of the challenge + * @param[out] payment_secret set to the challenge payment secret + * @return transaction status + */ +enum GNUNET_DB_QueryStatus +ANASTASIS_DB_get_pending_challenge_payment ( + const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid, + struct ANASTASIS_PaymentSecretP *payment_secret); + +#endif diff --git a/src/include/anastasis/anastasis-database/get_recdoc_payment.h b/src/include/anastasis/anastasis-database/get_recdoc_payment.h @@ -0,0 +1,44 @@ +/* + This file is part of Anastasis + Copyright (C) 2020-2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file include/anastasis/anastasis-database/get_recdoc_payment.h + * @brief Anastasis database: get recdoc payment + * @author Christian Grothoff + */ +#ifndef ANASTASIS_DATABASE_GET_RECDOC_PAYMENT_H +#define ANASTASIS_DATABASE_GET_RECDOC_PAYMENT_H + +#include <gnunet/gnunet_util_lib.h> +#include <gnunet/gnunet_db_lib.h> +#include "anastasis_service.h" +#include "anastasis/anastasis-database/common.h" + +/** + * Check payment identifier. Used to check if a payment identifier given by + * the user is valid (existing and paid). + * + * @param payment_secret payment secret which the user must provide with every upload + * @param[out] paid bool value to show if payment is paid + * @param[out] valid_counter bool value to show if post_counter is > 0 + * @return transaction status + */ +enum GNUNET_DB_QueryStatus +ANASTASIS_DB_get_recdoc_payment ( + const struct ANASTASIS_PaymentSecretP *payment_secret, + bool *paid, + bool *valid_counter); + +#endif diff --git a/src/include/anastasis/anastasis-database/get_recovery_meta_data.h b/src/include/anastasis/anastasis-database/get_recovery_meta_data.h @@ -1,39 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file include/anastasis/anastasis-database/get_recovery_meta_data.h - * @brief Anastasis database: get recovery meta data - * @author Christian Grothoff - */ -#ifndef ANASTASIS_DATABASE_GET_RECOVERY_META_DATA_H -#define ANASTASIS_DATABASE_GET_RECOVERY_META_DATA_H - -#include <gnunet/gnunet_util_lib.h> -#include <gnunet/gnunet_db_lib.h> -#include "anastasis_service.h" -#include "anastasis/anastasis-database/common.h" - -/** - * Closure for meta_iterator(). - */ -enum GNUNET_DB_QueryStatus -ANASTASIS_DB_get_recovery_meta_data ( - const struct ANASTASIS_CRYPTO_AccountPublicKeyP *account_pub, - uint32_t max_version, - ANASTASIS_DB_RecoveryMetaCallback cb, - void *cb_cls); - -#endif diff --git a/src/include/anastasis/anastasis-database/get_truth.h b/src/include/anastasis/anastasis-database/get_truth.h @@ -0,0 +1,47 @@ +/* + This file is part of Anastasis + Copyright (C) 2020-2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file include/anastasis/anastasis-database/get_truth.h + * @brief Anastasis database: get truth + * @author Christian Grothoff + */ +#ifndef ANASTASIS_DATABASE_GET_TRUTH_H +#define ANASTASIS_DATABASE_GET_TRUTH_H + +#include <gnunet/gnunet_util_lib.h> +#include <gnunet/gnunet_db_lib.h> +#include "anastasis_service.h" +#include "anastasis/anastasis-database/common.h" + +/** + * Get the encrypted truth to validate the challenge response + * + * @param truth_uuid the identifier for the Truth + * @param[out] truth contains the encrypted truth + * @param[out] truth_size size of the encrypted truth + * @param[out] truth_mime mime type of truth + * @param[out] method type of the challenge + * @return transaction status + */ +enum GNUNET_DB_QueryStatus +ANASTASIS_DB_get_truth ( + const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid, + void **truth, + size_t *truth_size, + char **truth_mime, + char **method); + +#endif diff --git a/src/include/anastasis/anastasis-database/get_truth_key_share.h b/src/include/anastasis/anastasis-database/get_truth_key_share.h @@ -0,0 +1,41 @@ +/* + This file is part of Anastasis + Copyright (C) 2020-2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file include/anastasis/anastasis-database/get_truth_key_share.h + * @brief Anastasis database: get truth key share + * @author Christian Grothoff + */ +#ifndef ANASTASIS_DATABASE_GET_TRUTH_KEY_SHARE_H +#define ANASTASIS_DATABASE_GET_TRUTH_KEY_SHARE_H + +#include <gnunet/gnunet_util_lib.h> +#include <gnunet/gnunet_db_lib.h> +#include "anastasis_service.h" +#include "anastasis/anastasis-database/common.h" + +/** + * Lookup (encrypted) key share by @a truth_uuid. + * + * @param truth_uuid the identifier for the Truth + * @param[out] key_share contains the encrypted Keyshare + * @return transaction status + */ +enum GNUNET_DB_QueryStatus +ANASTASIS_DB_get_truth_key_share ( + const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid, + struct ANASTASIS_CRYPTO_EncryptedKeyShareP *key_share); + +#endif diff --git a/src/include/anastasis/anastasis-database/get_truth_payment.h b/src/include/anastasis/anastasis-database/get_truth_payment.h @@ -0,0 +1,41 @@ +/* + This file is part of Anastasis + Copyright (C) 2020-2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file include/anastasis/anastasis-database/get_truth_payment.h + * @brief Anastasis database: get truth payment + * @author Christian Grothoff + */ +#ifndef ANASTASIS_DATABASE_GET_TRUTH_PAYMENT_H +#define ANASTASIS_DATABASE_GET_TRUTH_PAYMENT_H + +#include <gnunet/gnunet_util_lib.h> +#include <gnunet/gnunet_db_lib.h> +#include "anastasis_service.h" +#include "anastasis/anastasis-database/common.h" + +/** + * Inquire whether truth upload payment was made. + * + * @param uuid the truth's UUID + * @param[out] paid_until set for how long this truth is paid for + * @return transaction status + */ +enum GNUNET_DB_QueryStatus +ANASTASIS_DB_get_truth_payment ( + const struct ANASTASIS_CRYPTO_TruthUUIDP *uuid, + struct GNUNET_TIME_Timestamp *paid_until); + +#endif diff --git a/src/include/anastasis/anastasis-database/increment_lifetime.h b/src/include/anastasis/anastasis-database/increment_lifetime.h @@ -1,46 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file include/anastasis/anastasis-database/increment_lifetime.h - * @brief Anastasis database: increment lifetime - * @author Christian Grothoff - */ -#ifndef ANASTASIS_DATABASE_INCREMENT_LIFETIME_H -#define ANASTASIS_DATABASE_INCREMENT_LIFETIME_H - -#include <gnunet/gnunet_util_lib.h> -#include <gnunet/gnunet_db_lib.h> -#include "anastasis_service.h" -#include "anastasis/anastasis-database/common.h" - -/** - * Increment account lifetime based on payment having been received. - * Does nothing if the payment is not new. - * - * @param account_pub which account received a payment - * @param payment_identifier proof of payment, must be unique and match pending payment - * @param lifetime for how long is the account now paid (increment) - * @param[out] paid_until set to the end of the lifetime after the operation - * @return transaction status - */ -enum GNUNET_DB_QueryStatus -ANASTASIS_DB_increment_lifetime ( - const struct ANASTASIS_CRYPTO_AccountPublicKeyP *account_pub, - const struct ANASTASIS_PaymentSecretP *payment_identifier, - struct GNUNET_TIME_Relative lifetime, - struct GNUNET_TIME_Timestamp *paid_until); - -#endif diff --git a/src/include/anastasis/anastasis-database/insert_auth_iban_in.h b/src/include/anastasis/anastasis-database/insert_auth_iban_in.h @@ -0,0 +1,49 @@ +/* + This file is part of Anastasis + Copyright (C) 2020-2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file include/anastasis/anastasis-database/insert_auth_iban_in.h + * @brief Anastasis database: insert auth iban in + * @author Christian Grothoff + */ +#ifndef ANASTASIS_DATABASE_INSERT_AUTH_IBAN_IN_H +#define ANASTASIS_DATABASE_INSERT_AUTH_IBAN_IN_H + +#include <gnunet/gnunet_util_lib.h> +#include <gnunet/gnunet_db_lib.h> +#include "anastasis_service.h" +#include "anastasis/anastasis-database/common.h" + +/** + * Store inbound IBAN payment made for authentication. + * + * @param wire_reference unique identifier inside LibEuFin/Nexus + * @param wire_subject subject of the wire transfer + * @param amount how much was transferred + * @param debit_account account that was debited + * @param credit_account Anastasis operator account credited + * @param execution_date when was the transfer made + * @return transaction status + */ +enum GNUNET_DB_QueryStatus +ANASTASIS_DB_insert_auth_iban_in ( + uint64_t wire_reference, + const char *wire_subject, + const struct TALER_Amount *amount, + const char *debit_account, + const char *credit_account, + struct GNUNET_TIME_Timestamp execution_date); + +#endif diff --git a/src/include/anastasis/anastasis-database/insert_challenge_code.h b/src/include/anastasis/anastasis-database/insert_challenge_code.h @@ -0,0 +1,53 @@ +/* + This file is part of Anastasis + Copyright (C) 2020-2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file include/anastasis/anastasis-database/insert_challenge_code.h + * @brief Anastasis database: insert challenge code + * @author Christian Grothoff + */ +#ifndef ANASTASIS_DATABASE_INSERT_CHALLENGE_CODE_H +#define ANASTASIS_DATABASE_INSERT_CHALLENGE_CODE_H + +#include <gnunet/gnunet_util_lib.h> +#include <gnunet/gnunet_db_lib.h> +#include "anastasis_service.h" +#include "anastasis/anastasis-database/common.h" + +/** + * Create a new challenge code for a given challenge identified by the challenge + * public key. The function will first check if there is already a valid code + * for this challenge present and won't insert a new one in this case. + * + * @param truth_uuid the identifier for the challenge + * @param rotation_period for how long is the code available + * @param validity_period for how long is the code available + * @param retry_counter amount of retries allowed + * @param[out] retransmission_date when to next retransmit + * @param[out] code set to the code which will be checked for later + * @return transaction status, + * #GNUNET_DB_STATUS_SUCCESS_NO_RESULTS if we are out of valid tries, + * #GNUNET_DB_STATUS_SUCCESS_ONE_RESULT if @a code is now in the DB + */ +enum GNUNET_DB_QueryStatus +ANASTASIS_DB_insert_challenge_code ( + const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid, + struct GNUNET_TIME_Relative rotation_period, + struct GNUNET_TIME_Relative validity_period, + uint32_t retry_counter, + struct GNUNET_TIME_Timestamp *retransmission_date, + uint64_t *code); + +#endif diff --git a/src/include/anastasis/anastasis-database/insert_challenge_payment.h b/src/include/anastasis/anastasis-database/insert_challenge_payment.h @@ -0,0 +1,43 @@ +/* + This file is part of Anastasis + Copyright (C) 2020-2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file include/anastasis/anastasis-database/insert_challenge_payment.h + * @brief Anastasis database: insert challenge payment + * @author Christian Grothoff + */ +#ifndef ANASTASIS_DATABASE_INSERT_CHALLENGE_PAYMENT_H +#define ANASTASIS_DATABASE_INSERT_CHALLENGE_PAYMENT_H + +#include <gnunet/gnunet_util_lib.h> +#include <gnunet/gnunet_db_lib.h> +#include "anastasis_service.h" +#include "anastasis/anastasis-database/common.h" + +/** + * Store payment for challenge. + * + * @param truth_uuid identifier of the challenge to pay + * @param payment_secret payment secret which the user must provide with every upload + * @param amount how much we asked for + * @return transaction status + */ +enum GNUNET_DB_QueryStatus +ANASTASIS_DB_insert_challenge_payment ( + const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid, + const struct ANASTASIS_PaymentSecretP *payment_secret, + const struct TALER_Amount *amount); + +#endif diff --git a/src/include/anastasis/anastasis-database/insert_recdoc_payment.h b/src/include/anastasis/anastasis-database/insert_recdoc_payment.h @@ -0,0 +1,47 @@ +/* + This file is part of Anastasis + Copyright (C) 2020-2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file include/anastasis/anastasis-database/insert_recdoc_payment.h + * @brief Anastasis database: insert recdoc payment + * @author Christian Grothoff + */ +#ifndef ANASTASIS_DATABASE_INSERT_RECDOC_PAYMENT_H +#define ANASTASIS_DATABASE_INSERT_RECDOC_PAYMENT_H + +#include <gnunet/gnunet_util_lib.h> +#include <gnunet/gnunet_db_lib.h> +#include "anastasis_service.h" +#include "anastasis/anastasis-database/common.h" + +/** + * Store payment. Used to begin a payment, not indicative + * that the payment actually was made. (That is done + * when we increment the account's lifetime.) + * + * @param account_pub anastasis's public key + * @param post_counter how many uploads does @a amount pay for + * @param payment_secret payment secret which the user must provide with every upload + * @param amount how much we asked for + * @return transaction status + */ +enum GNUNET_DB_QueryStatus +ANASTASIS_DB_insert_recdoc_payment ( + const struct ANASTASIS_CRYPTO_AccountPublicKeyP *account_pub, + uint32_t post_counter, + const struct ANASTASIS_PaymentSecretP *payment_secret, + const struct TALER_Amount *amount); + +#endif diff --git a/src/include/anastasis/anastasis-database/insert_truth.h b/src/include/anastasis/anastasis-database/insert_truth.h @@ -0,0 +1,51 @@ +/* + This file is part of Anastasis + Copyright (C) 2020-2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file include/anastasis/anastasis-database/insert_truth.h + * @brief Anastasis database: insert truth + * @author Christian Grothoff + */ +#ifndef ANASTASIS_DATABASE_INSERT_TRUTH_H +#define ANASTASIS_DATABASE_INSERT_TRUTH_H + +#include <gnunet/gnunet_util_lib.h> +#include <gnunet/gnunet_db_lib.h> +#include "anastasis_service.h" +#include "anastasis/anastasis-database/common.h" + +/** + * Upload Truth, which contains the Truth and the KeyShare. + * + * @param truth_uuid the identifier for the Truth + * @param key_share_data contains information of an EncryptedKeyShare + * @param mime_type presumed mime type of data in @a encrypted_truth + * @param encrypted_truth contains the encrypted Truth which includes the ground truth i.e. H(challenge answer), phonenumber, SMS + * @param encrypted_truth_size the size of the Truth + * @param method name of method + * @param truth_expiration time till the according data will be stored + * @return transaction status + */ +enum GNUNET_DB_QueryStatus +ANASTASIS_DB_insert_truth ( + const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid, + const struct ANASTASIS_CRYPTO_EncryptedKeyShareP *key_share_data, + const char *mime_type, + const void *encrypted_truth, + size_t encrypted_truth_size, + const char *method, + struct GNUNET_TIME_Relative truth_expiration); + +#endif diff --git a/src/include/anastasis/anastasis-database/insert_truth_payment.h b/src/include/anastasis/anastasis-database/insert_truth_payment.h @@ -0,0 +1,43 @@ +/* + This file is part of Anastasis + Copyright (C) 2020-2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file include/anastasis/anastasis-database/insert_truth_payment.h + * @brief Anastasis database: insert truth payment + * @author Christian Grothoff + */ +#ifndef ANASTASIS_DATABASE_INSERT_TRUTH_PAYMENT_H +#define ANASTASIS_DATABASE_INSERT_TRUTH_PAYMENT_H + +#include <gnunet/gnunet_util_lib.h> +#include <gnunet/gnunet_db_lib.h> +#include "anastasis_service.h" +#include "anastasis/anastasis-database/common.h" + +/** + * Record truth upload payment was made. + * + * @param uuid the truth's UUID + * @param amount the amount that was paid + * @param duration how long is the truth paid for + * @return transaction status + */ +enum GNUNET_DB_QueryStatus +ANASTASIS_DB_insert_truth_payment ( + const struct ANASTASIS_CRYPTO_TruthUUIDP *uuid, + const struct TALER_Amount *amount, + struct GNUNET_TIME_Relative duration); + +#endif diff --git a/src/include/anastasis/anastasis-database/iterate_auth_iban_transfers.h b/src/include/anastasis/anastasis-database/iterate_auth_iban_transfers.h @@ -0,0 +1,50 @@ +/* + This file is part of Anastasis + Copyright (C) 2020-2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file include/anastasis/anastasis-database/iterate_auth_iban_transfers.h + * @brief Anastasis database: iterate auth iban transfers + * @author Christian Grothoff + */ +#ifndef ANASTASIS_DATABASE_ITERATE_AUTH_IBAN_TRANSFERS_H +#define ANASTASIS_DATABASE_ITERATE_AUTH_IBAN_TRANSFERS_H + +#include <gnunet/gnunet_util_lib.h> +#include <gnunet/gnunet_db_lib.h> +#include "anastasis_service.h" +#include "anastasis/anastasis-database/common.h" + +/** + * Function to check if we are aware of a wire transfer + * that satisfies the IBAN plugin's authentication check. + * + * @param debit_account which debit account to check + * @param earliest_date earliest date to check + * @param cb function to call on all entries found + * @param cb_cls closure for @a cb + * @return transaction status, + * #GNUNET_DB_STATUS_SUCCESS_ONE_RESULT if @a cb + * returned 'true' once + * #GNUNET_DB_STATUS_SUCCESS_NO_RESULTS if no + * wire transfers existed for which @a cb returned true + */ +enum GNUNET_DB_QueryStatus +ANASTASIS_DB_iterate_auth_iban_transfers ( + const char *debit_account, + struct GNUNET_TIME_Timestamp earliest_date, + ANASTASIS_DB_AuthIbanTransferCallback cb, + void *cb_cls); + +#endif diff --git a/src/include/anastasis/anastasis-database/iterate_recovery_meta_data.h b/src/include/anastasis/anastasis-database/iterate_recovery_meta_data.h @@ -0,0 +1,47 @@ +/* + This file is part of Anastasis + Copyright (C) 2020-2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file include/anastasis/anastasis-database/iterate_recovery_meta_data.h + * @brief Anastasis database: iterate recovery meta data + * @author Christian Grothoff + */ +#ifndef ANASTASIS_DATABASE_ITERATE_RECOVERY_META_DATA_H +#define ANASTASIS_DATABASE_ITERATE_RECOVERY_META_DATA_H + +#include <gnunet/gnunet_util_lib.h> +#include <gnunet/gnunet_db_lib.h> +#include "anastasis_service.h" +#include "anastasis/anastasis-database/common.h" + +/** + * Fetch recovery document meta data for user. Returns + * meta data in descending order from @a max_version. + * The size of the result set may be limited. + * + * @param account_pub public key of the user's account + * @param max_version the maximum version number the user requests + * @param cb function to call on each result + * @param cb_cls closure for @a cb + * @return transaction status + */ +enum GNUNET_DB_QueryStatus +ANASTASIS_DB_iterate_recovery_meta_data ( + const struct ANASTASIS_CRYPTO_AccountPublicKeyP *account_pub, + uint32_t max_version, + ANASTASIS_DB_RecoveryMetaCallback cb, + void *cb_cls); + +#endif diff --git a/src/include/anastasis/anastasis-database/lookup_account.h b/src/include/anastasis/anastasis-database/lookup_account.h @@ -1,46 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file include/anastasis/anastasis-database/lookup_account.h - * @brief Anastasis database: lookup account - * @author Christian Grothoff - */ -#ifndef ANASTASIS_DATABASE_LOOKUP_ACCOUNT_H -#define ANASTASIS_DATABASE_LOOKUP_ACCOUNT_H - -#include <gnunet/gnunet_util_lib.h> -#include <gnunet/gnunet_db_lib.h> -#include "anastasis_service.h" -#include "anastasis/anastasis-database/common.h" - -/** - * Check if an account exists, and if so, return the - * current @a recovery_document_hash. - * - * @param account_pub account identifier - * @param[out] paid_until until when is the account paid up? - * @param[out] recovery_data_hash set to hash of @a recovery document - * @param[out] version set to the recovery policy version - * @return transaction status - */ -enum ANASTASIS_DB_AccountStatus -ANASTASIS_DB_lookup_account ( - const struct ANASTASIS_CRYPTO_AccountPublicKeyP *account_pub, - struct GNUNET_TIME_Timestamp *paid_until, - struct GNUNET_HashCode *recovery_data_hash, - uint32_t *version); - -#endif diff --git a/src/include/anastasis/anastasis-database/lookup_challenge_payment.h b/src/include/anastasis/anastasis-database/lookup_challenge_payment.h @@ -1,41 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file include/anastasis/anastasis-database/lookup_challenge_payment.h - * @brief Anastasis database: lookup challenge payment - * @author Christian Grothoff - */ -#ifndef ANASTASIS_DATABASE_LOOKUP_CHALLENGE_PAYMENT_H -#define ANASTASIS_DATABASE_LOOKUP_CHALLENGE_PAYMENT_H - -#include <gnunet/gnunet_util_lib.h> -#include <gnunet/gnunet_db_lib.h> -#include "anastasis_service.h" -#include "anastasis/anastasis-database/common.h" - -/** - * Lookup pending payment for a certain challenge. - * - * @param truth_uuid identification of the challenge - * @param[out] payment_secret set to the challenge payment secret - * @return transaction status - */ -enum GNUNET_DB_QueryStatus -ANASTASIS_DB_lookup_challenge_payment ( - const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid, - struct ANASTASIS_PaymentSecretP *payment_secret); - -#endif diff --git a/src/include/anastasis/anastasis-database/mark_challenge_code_satisfied.h b/src/include/anastasis/anastasis-database/mark_challenge_code_satisfied.h @@ -1,42 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file include/anastasis/anastasis-database/mark_challenge_code_satisfied.h - * @brief Anastasis database: mark challenge code satisfied - * @author Christian Grothoff - */ -#ifndef ANASTASIS_DATABASE_MARK_CHALLENGE_CODE_SATISFIED_H -#define ANASTASIS_DATABASE_MARK_CHALLENGE_CODE_SATISFIED_H - -#include <gnunet/gnunet_util_lib.h> -#include <gnunet/gnunet_db_lib.h> -#include "anastasis_service.h" -#include "anastasis/anastasis-database/common.h" - -/** - * Set the 'satisfied' bit for the given challenge and code to - * 'true'. - * - * @param truth_uuid identification of the challenge which the code corresponds to - * @param code code which is now satisfied - * @return transaction status - */ -enum GNUNET_DB_QueryStatus -ANASTASIS_DB_mark_challenge_code_satisfied ( - const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid, - const uint64_t code); - -#endif diff --git a/src/include/anastasis/anastasis-database/mark_challenge_sent.h b/src/include/anastasis/anastasis-database/mark_challenge_sent.h @@ -1,42 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file include/anastasis/anastasis-database/mark_challenge_sent.h - * @brief Anastasis database: mark challenge sent - * @author Christian Grothoff - */ -#ifndef ANASTASIS_DATABASE_MARK_CHALLENGE_SENT_H -#define ANASTASIS_DATABASE_MARK_CHALLENGE_SENT_H - -#include <gnunet/gnunet_util_lib.h> -#include <gnunet/gnunet_db_lib.h> -#include "anastasis_service.h" -#include "anastasis/anastasis-database/common.h" - -/** - * Remember in the database that we successfully sent a challenge. - * - * @param payment_secret payment secret which the user must provide with every upload - * @param truth_uuid the identifier for the challenge - * @param code the challenge that was sent - */ -enum GNUNET_DB_QueryStatus -ANASTASIS_DB_mark_challenge_sent ( - const struct ANASTASIS_PaymentSecretP *payment_secret, - const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid, - uint64_t code); - -#endif diff --git a/src/include/anastasis/anastasis-database/record_auth_iban_payment.h b/src/include/anastasis/anastasis-database/record_auth_iban_payment.h @@ -1,49 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file include/anastasis/anastasis-database/record_auth_iban_payment.h - * @brief Anastasis database: record auth iban payment - * @author Christian Grothoff - */ -#ifndef ANASTASIS_DATABASE_RECORD_AUTH_IBAN_PAYMENT_H -#define ANASTASIS_DATABASE_RECORD_AUTH_IBAN_PAYMENT_H - -#include <gnunet/gnunet_util_lib.h> -#include <gnunet/gnunet_db_lib.h> -#include "anastasis_service.h" -#include "anastasis/anastasis-database/common.h" - -/** - * Store inbound IBAN payment made for authentication. - * - * @param wire_reference unique identifier inside LibEuFin/Nexus - * @param wire_subject subject of the wire transfer - * @param amount how much was transferred - * @param debit_account account that was debited - * @param credit_account Anastasis operator account credited - * @param execution_date when was the transfer made - * @return transaction status - */ -enum GNUNET_DB_QueryStatus -ANASTASIS_DB_record_auth_iban_payment ( - uint64_t wire_reference, - const char *wire_subject, - const struct TALER_Amount *amount, - const char *debit_account, - const char *credit_account, - struct GNUNET_TIME_Timestamp execution_date); - -#endif diff --git a/src/include/anastasis/anastasis-database/record_challenge_payment.h b/src/include/anastasis/anastasis-database/record_challenge_payment.h @@ -1,43 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file include/anastasis/anastasis-database/record_challenge_payment.h - * @brief Anastasis database: record challenge payment - * @author Christian Grothoff - */ -#ifndef ANASTASIS_DATABASE_RECORD_CHALLENGE_PAYMENT_H -#define ANASTASIS_DATABASE_RECORD_CHALLENGE_PAYMENT_H - -#include <gnunet/gnunet_util_lib.h> -#include <gnunet/gnunet_db_lib.h> -#include "anastasis_service.h" -#include "anastasis/anastasis-database/common.h" - -/** - * Store payment for challenge. - * - * @param truth_uuid identifier of the challenge to pay - * @param payment_secret payment secret which the user must provide with every upload - * @param amount how much we asked for - * @return transaction status - */ -enum GNUNET_DB_QueryStatus -ANASTASIS_DB_record_challenge_payment ( - const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid, - const struct ANASTASIS_PaymentSecretP *payment_secret, - const struct TALER_Amount *amount); - -#endif diff --git a/src/include/anastasis/anastasis-database/record_challenge_refund.h b/src/include/anastasis/anastasis-database/record_challenge_refund.h @@ -1,41 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file include/anastasis/anastasis-database/record_challenge_refund.h - * @brief Anastasis database: record challenge refund - * @author Christian Grothoff - */ -#ifndef ANASTASIS_DATABASE_RECORD_CHALLENGE_REFUND_H -#define ANASTASIS_DATABASE_RECORD_CHALLENGE_REFUND_H - -#include <gnunet/gnunet_util_lib.h> -#include <gnunet/gnunet_db_lib.h> -#include "anastasis_service.h" -#include "anastasis/anastasis-database/common.h" - -/** - * Store refund granted for challenge. - * - * @param truth_uuid identifier of the challenge to refund - * @param payment_secret payment secret which the user must provide with every upload - * @return transaction status - */ -enum GNUNET_DB_QueryStatus -ANASTASIS_DB_record_challenge_refund ( - const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid, - const struct ANASTASIS_PaymentSecretP *payment_secret); - -#endif diff --git a/src/include/anastasis/anastasis-database/record_recdoc_payment.h b/src/include/anastasis/anastasis-database/record_recdoc_payment.h @@ -1,47 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file include/anastasis/anastasis-database/record_recdoc_payment.h - * @brief Anastasis database: record recdoc payment - * @author Christian Grothoff - */ -#ifndef ANASTASIS_DATABASE_RECORD_RECDOC_PAYMENT_H -#define ANASTASIS_DATABASE_RECORD_RECDOC_PAYMENT_H - -#include <gnunet/gnunet_util_lib.h> -#include <gnunet/gnunet_db_lib.h> -#include "anastasis_service.h" -#include "anastasis/anastasis-database/common.h" - -/** - * Store payment. Used to begin a payment, not indicative - * that the payment actually was made. (That is done - * when we increment the account's lifetime.) - * - * @param account_pub anastasis's public key - * @param post_counter how many uploads does @a amount pay for - * @param payment_secret payment secret which the user must provide with every upload - * @param amount how much we asked for - * @return transaction status - */ -enum GNUNET_DB_QueryStatus -ANASTASIS_DB_record_recdoc_payment ( - const struct ANASTASIS_CRYPTO_AccountPublicKeyP *account_pub, - uint32_t post_counter, - const struct ANASTASIS_PaymentSecretP *payment_secret, - const struct TALER_Amount *amount); - -#endif diff --git a/src/include/anastasis/anastasis-database/record_truth_upload_payment.h b/src/include/anastasis/anastasis-database/record_truth_upload_payment.h @@ -1,43 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file include/anastasis/anastasis-database/record_truth_upload_payment.h - * @brief Anastasis database: record truth upload payment - * @author Christian Grothoff - */ -#ifndef ANASTASIS_DATABASE_RECORD_TRUTH_UPLOAD_PAYMENT_H -#define ANASTASIS_DATABASE_RECORD_TRUTH_UPLOAD_PAYMENT_H - -#include <gnunet/gnunet_util_lib.h> -#include <gnunet/gnunet_db_lib.h> -#include "anastasis_service.h" -#include "anastasis/anastasis-database/common.h" - -/** - * Record truth upload payment was made. - * - * @param uuid the truth's UUID - * @param amount the amount that was paid - * @param duration how long is the truth paid for - * @return transaction status - */ -enum GNUNET_DB_QueryStatus -ANASTASIS_DB_record_truth_upload_payment ( - const struct ANASTASIS_CRYPTO_TruthUUIDP *uuid, - const struct TALER_Amount *amount, - struct GNUNET_TIME_Relative duration); - -#endif diff --git a/src/include/anastasis/anastasis-database/store_recovery_document.h b/src/include/anastasis/anastasis-database/store_recovery_document.h @@ -1,54 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file include/anastasis/anastasis-database/store_recovery_document.h - * @brief Anastasis database: store recovery document - * @author Christian Grothoff - */ -#ifndef ANASTASIS_DATABASE_STORE_RECOVERY_DOCUMENT_H -#define ANASTASIS_DATABASE_STORE_RECOVERY_DOCUMENT_H - -#include <gnunet/gnunet_util_lib.h> -#include <gnunet/gnunet_db_lib.h> -#include "anastasis_service.h" -#include "anastasis/anastasis-database/common.h" - -/** - * Store encrypted recovery document. - * - * @param account_pub public key of the user's account - * @param account_sig signature affirming storage request - * @param recovery_data_hash hash of @a data - * @param recovery_data contains encrypted_recovery_document - * @param recovery_data_size size of data blob - * @param payment_secret identifier for the payment, used to later charge on uploads - * @param[out] version set to the version assigned to the document by the database - * @return transaction status, 0 if upload could not be finished because @a payment_secret - * did not have enough upload left; HARD error if @a payment_secret is unknown, ... - */ -enum ANASTASIS_DB_StoreStatus -ANASTASIS_DB_store_recovery_document ( - const struct ANASTASIS_CRYPTO_AccountPublicKeyP *account_pub, - const struct ANASTASIS_AccountSignatureP *account_sig, - const struct GNUNET_HashCode *recovery_data_hash, - const void *recovery_data, - size_t recovery_data_size, - const void *recovery_meta_data, - size_t recovery_meta_data_size, - const struct ANASTASIS_PaymentSecretP *payment_secret, - uint32_t *version); - -#endif diff --git a/src/include/anastasis/anastasis-database/store_truth.h b/src/include/anastasis/anastasis-database/store_truth.h @@ -1,51 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file include/anastasis/anastasis-database/store_truth.h - * @brief Anastasis database: store truth - * @author Christian Grothoff - */ -#ifndef ANASTASIS_DATABASE_STORE_TRUTH_H -#define ANASTASIS_DATABASE_STORE_TRUTH_H - -#include <gnunet/gnunet_util_lib.h> -#include <gnunet/gnunet_db_lib.h> -#include "anastasis_service.h" -#include "anastasis/anastasis-database/common.h" - -/** - * Upload Truth, which contains the Truth and the KeyShare. - * - * @param truth_uuid the identifier for the Truth - * @param key_share_data contains information of an EncryptedKeyShare - * @param mime_type presumed mime type of data in @a encrypted_truth - * @param encrypted_truth contains the encrypted Truth which includes the ground truth i.e. H(challenge answer), phonenumber, SMS - * @param encrypted_truth_size the size of the Truth - * @param method name of method - * @param truth_expiration time till the according data will be stored - * @return transaction status - */ -enum GNUNET_DB_QueryStatus -ANASTASIS_DB_store_truth ( - const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid, - const struct ANASTASIS_CRYPTO_EncryptedKeyShareP *key_share_data, - const char *mime_type, - const void *encrypted_truth, - size_t encrypted_truth_size, - const char *method, - struct GNUNET_TIME_Relative truth_expiration); - -#endif diff --git a/src/include/anastasis/anastasis-database/test_auth_iban_payment.h b/src/include/anastasis/anastasis-database/test_auth_iban_payment.h @@ -1,39 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file include/anastasis/anastasis-database/test_auth_iban_payment.h - * @brief Anastasis database: test auth iban payment - * @author Christian Grothoff - */ -#ifndef ANASTASIS_DATABASE_TEST_AUTH_IBAN_PAYMENT_H -#define ANASTASIS_DATABASE_TEST_AUTH_IBAN_PAYMENT_H - -#include <gnunet/gnunet_util_lib.h> -#include <gnunet/gnunet_db_lib.h> -#include "anastasis_service.h" -#include "anastasis/anastasis-database/common.h" - -/** - * Closure for #test_auth_cb. - */ -enum GNUNET_DB_QueryStatus -ANASTASIS_DB_test_auth_iban_payment ( - const char *debit_account, - struct GNUNET_TIME_Timestamp earliest_date, - ANASTASIS_DB_AuthIbanTransfercheck cb, - void *cb_cls); - -#endif diff --git a/src/include/anastasis/anastasis-database/test_challenge_code_satisfied.h b/src/include/anastasis/anastasis-database/test_challenge_code_satisfied.h @@ -1,45 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file include/anastasis/anastasis-database/test_challenge_code_satisfied.h - * @brief Anastasis database: test challenge code satisfied - * @author Christian Grothoff - */ -#ifndef ANASTASIS_DATABASE_TEST_CHALLENGE_CODE_SATISFIED_H -#define ANASTASIS_DATABASE_TEST_CHALLENGE_CODE_SATISFIED_H - -#include <gnunet/gnunet_util_lib.h> -#include <gnunet/gnunet_db_lib.h> -#include "anastasis_service.h" -#include "anastasis/anastasis-database/common.h" - -/** - * Check if the 'satisfied' bit for the given challenge and code is - * 'true' and the challenge code is not yet expired. - * - * @param truth_uuid identification of the challenge which the code corresponds to - * @param code code which is now satisfied - * @return transaction status, - * #GNUNET_DB_STATUS_SUCCESS_NO_RESULTS if the challenge code is not satisfied or expired - * #GNUNET_DB_STATUS_SUCCESS_ONE_RESULT if the challenge code has been marked as satisfied - */ -enum GNUNET_DB_QueryStatus -ANASTASIS_DB_test_challenge_code_satisfied ( - const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid, - const uint64_t code, - struct GNUNET_TIME_Timestamp after); - -#endif diff --git a/src/include/anastasis/anastasis-database/update_challenge_payment.h b/src/include/anastasis/anastasis-database/update_challenge_payment.h @@ -1,41 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file include/anastasis/anastasis-database/update_challenge_payment.h - * @brief Anastasis database: update challenge payment - * @author Christian Grothoff - */ -#ifndef ANASTASIS_DATABASE_UPDATE_CHALLENGE_PAYMENT_H -#define ANASTASIS_DATABASE_UPDATE_CHALLENGE_PAYMENT_H - -#include <gnunet/gnunet_util_lib.h> -#include <gnunet/gnunet_db_lib.h> -#include "anastasis_service.h" -#include "anastasis/anastasis-database/common.h" - -/** - * Update payment status of challenge - * - * @param truth_uuid which challenge received a payment - * @param payment_identifier proof of payment, must be unique and match pending payment - * @return transaction status - */ -enum GNUNET_DB_QueryStatus -ANASTASIS_DB_update_challenge_payment ( - const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid, - const struct ANASTASIS_PaymentSecretP *payment_identifier); - -#endif diff --git a/src/include/anastasis/anastasis-database/update_lifetime.h b/src/include/anastasis/anastasis-database/update_lifetime.h @@ -1,44 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file include/anastasis/anastasis-database/update_lifetime.h - * @brief Anastasis database: update lifetime - * @author Christian Grothoff - */ -#ifndef ANASTASIS_DATABASE_UPDATE_LIFETIME_H -#define ANASTASIS_DATABASE_UPDATE_LIFETIME_H - -#include <gnunet/gnunet_util_lib.h> -#include <gnunet/gnunet_db_lib.h> -#include "anastasis_service.h" -#include "anastasis/anastasis-database/common.h" - -/** - * Update account lifetime to the maximum of the current - * value and @a eol. - * - * @param account_pub which account received a payment - * @param payment_identifier proof of payment, must be unique and match pending payment - * @param eol for how long is the account now paid (absolute) - * @return transaction status - */ -enum GNUNET_DB_QueryStatus -ANASTASIS_DB_update_lifetime ( - const struct ANASTASIS_CRYPTO_AccountPublicKeyP *account_pub, - const struct ANASTASIS_PaymentSecretP *payment_identifier, - struct GNUNET_TIME_Timestamp eol); - -#endif diff --git a/src/include/anastasis/anastasis-database/update_to_challenge_code_satisfied.h b/src/include/anastasis/anastasis-database/update_to_challenge_code_satisfied.h @@ -0,0 +1,42 @@ +/* + This file is part of Anastasis + Copyright (C) 2020-2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file include/anastasis/anastasis-database/update_to_challenge_code_satisfied.h + * @brief Anastasis database: update to challenge code satisfied + * @author Christian Grothoff + */ +#ifndef ANASTASIS_DATABASE_UPDATE_TO_CHALLENGE_CODE_SATISFIED_H +#define ANASTASIS_DATABASE_UPDATE_TO_CHALLENGE_CODE_SATISFIED_H + +#include <gnunet/gnunet_util_lib.h> +#include <gnunet/gnunet_db_lib.h> +#include "anastasis_service.h" +#include "anastasis/anastasis-database/common.h" + +/** + * Set the 'satisfied' bit for the given challenge and code to + * 'true'. + * + * @param truth_uuid identification of the challenge which the code corresponds to + * @param code code which is now satisfied + * @return transaction status + */ +enum GNUNET_DB_QueryStatus +ANASTASIS_DB_update_to_challenge_code_satisfied ( + const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid, + const uint64_t code); + +#endif diff --git a/src/include/anastasis/anastasis-database/update_to_challenge_code_sent.h b/src/include/anastasis/anastasis-database/update_to_challenge_code_sent.h @@ -0,0 +1,42 @@ +/* + This file is part of Anastasis + Copyright (C) 2020-2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file include/anastasis/anastasis-database/update_to_challenge_code_sent.h + * @brief Anastasis database: update to challenge code sent + * @author Christian Grothoff + */ +#ifndef ANASTASIS_DATABASE_UPDATE_TO_CHALLENGE_CODE_SENT_H +#define ANASTASIS_DATABASE_UPDATE_TO_CHALLENGE_CODE_SENT_H + +#include <gnunet/gnunet_util_lib.h> +#include <gnunet/gnunet_db_lib.h> +#include "anastasis_service.h" +#include "anastasis/anastasis-database/common.h" + +/** + * Remember in the database that we successfully sent a challenge. + * + * @param payment_secret payment secret which the user must provide with every upload + * @param truth_uuid the identifier for the challenge + * @param code the challenge that was sent + */ +enum GNUNET_DB_QueryStatus +ANASTASIS_DB_update_to_challenge_code_sent ( + const struct ANASTASIS_PaymentSecretP *payment_secret, + const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid, + uint64_t code); + +#endif diff --git a/src/include/anastasis/anastasis-database/update_to_challenge_payment_paid.h b/src/include/anastasis/anastasis-database/update_to_challenge_payment_paid.h @@ -0,0 +1,41 @@ +/* + This file is part of Anastasis + Copyright (C) 2020-2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file include/anastasis/anastasis-database/update_to_challenge_payment_paid.h + * @brief Anastasis database: update to challenge payment paid + * @author Christian Grothoff + */ +#ifndef ANASTASIS_DATABASE_UPDATE_TO_CHALLENGE_PAYMENT_PAID_H +#define ANASTASIS_DATABASE_UPDATE_TO_CHALLENGE_PAYMENT_PAID_H + +#include <gnunet/gnunet_util_lib.h> +#include <gnunet/gnunet_db_lib.h> +#include "anastasis_service.h" +#include "anastasis/anastasis-database/common.h" + +/** + * Update payment status of challenge + * + * @param truth_uuid which challenge received a payment + * @param payment_identifier proof of payment, must be unique and match pending payment + * @return transaction status + */ +enum GNUNET_DB_QueryStatus +ANASTASIS_DB_update_to_challenge_payment_paid ( + const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid, + const struct ANASTASIS_PaymentSecretP *payment_identifier); + +#endif diff --git a/src/include/anastasis/anastasis-database/update_to_challenge_payment_refunded.h b/src/include/anastasis/anastasis-database/update_to_challenge_payment_refunded.h @@ -0,0 +1,41 @@ +/* + This file is part of Anastasis + Copyright (C) 2020-2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file include/anastasis/anastasis-database/update_to_challenge_payment_refunded.h + * @brief Anastasis database: update to challenge payment refunded + * @author Christian Grothoff + */ +#ifndef ANASTASIS_DATABASE_UPDATE_TO_CHALLENGE_PAYMENT_REFUNDED_H +#define ANASTASIS_DATABASE_UPDATE_TO_CHALLENGE_PAYMENT_REFUNDED_H + +#include <gnunet/gnunet_util_lib.h> +#include <gnunet/gnunet_db_lib.h> +#include "anastasis_service.h" +#include "anastasis/anastasis-database/common.h" + +/** + * Store refund granted for challenge. + * + * @param truth_uuid identifier of the challenge to refund + * @param payment_secret payment secret which the user must provide with every upload + * @return transaction status + */ +enum GNUNET_DB_QueryStatus +ANASTASIS_DB_update_to_challenge_payment_refunded ( + const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid, + const struct ANASTASIS_PaymentSecretP *payment_secret); + +#endif diff --git a/src/include/anastasis/anastasis-database/verify_challenge_code.h b/src/include/anastasis/anastasis-database/verify_challenge_code.h @@ -1,39 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file include/anastasis/anastasis-database/verify_challenge_code.h - * @brief Anastasis database: verify challenge code - * @author Christian Grothoff - */ -#ifndef ANASTASIS_DATABASE_VERIFY_CHALLENGE_CODE_H -#define ANASTASIS_DATABASE_VERIFY_CHALLENGE_CODE_H - -#include <gnunet/gnunet_util_lib.h> -#include <gnunet/gnunet_db_lib.h> -#include "anastasis_service.h" -#include "anastasis/anastasis-database/common.h" - -/** - * Closure for check_valid_code(). - */ -enum ANASTASIS_DB_CodeStatus -ANASTASIS_DB_verify_challenge_code ( - const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid, - const struct GNUNET_HashCode *hashed_code, - uint64_t *code, - bool *satisfied); - -#endif diff --git a/src/include/anastasis_database_lib.h b/src/include/anastasis_database_lib.h @@ -32,34 +32,33 @@ #include "anastasis/anastasis-database/create_tables.h" #include "anastasis/anastasis-database/drop_tables.h" #include "anastasis/anastasis-database/gc.h" -#include "anastasis/anastasis-database/store_recovery_document.h" -#include "anastasis/anastasis-database/get_recovery_meta_data.h" +#include "anastasis/anastasis-database/do_insert_recovery_document.h" +#include "anastasis/anastasis-database/iterate_recovery_meta_data.h" #include "anastasis/anastasis-database/get_recovery_document.h" #include "anastasis/anastasis-database/get_latest_recovery_document.h" -#include "anastasis/anastasis-database/store_truth.h" -#include "anastasis/anastasis-database/get_escrow_challenge.h" -#include "anastasis/anastasis-database/get_key_share.h" -#include "anastasis/anastasis-database/lookup_account.h" -#include "anastasis/anastasis-database/check_payment_identifier.h" -#include "anastasis/anastasis-database/check_challenge_payment.h" -#include "anastasis/anastasis-database/increment_lifetime.h" -#include "anastasis/anastasis-database/update_lifetime.h" -#include "anastasis/anastasis-database/record_recdoc_payment.h" -#include "anastasis/anastasis-database/record_truth_upload_payment.h" -#include "anastasis/anastasis-database/check_truth_upload_paid.h" -#include "anastasis/anastasis-database/verify_challenge_code.h" -#include "anastasis/anastasis-database/mark_challenge_code_satisfied.h" -#include "anastasis/anastasis-database/test_challenge_code_satisfied.h" -#include "anastasis/anastasis-database/create_challenge_code.h" -#include "anastasis/anastasis-database/mark_challenge_sent.h" -#include "anastasis/anastasis-database/challenge_gc.h" -#include "anastasis/anastasis-database/record_challenge_payment.h" -#include "anastasis/anastasis-database/record_challenge_refund.h" -#include "anastasis/anastasis-database/lookup_challenge_payment.h" -#include "anastasis/anastasis-database/update_challenge_payment.h" -#include "anastasis/anastasis-database/record_auth_iban_payment.h" -#include "anastasis/anastasis-database/test_auth_iban_payment.h" -#include "anastasis/anastasis-database/get_last_auth_iban_payment_row.h" +#include "anastasis/anastasis-database/insert_truth.h" +#include "anastasis/anastasis-database/get_truth.h" +#include "anastasis/anastasis-database/get_truth_key_share.h" +#include "anastasis/anastasis-database/get_account.h" +#include "anastasis/anastasis-database/get_recdoc_payment.h" +#include "anastasis/anastasis-database/get_challenge_payment.h" +#include "anastasis/anastasis-database/do_update_account_lifetime.h" +#include "anastasis/anastasis-database/insert_recdoc_payment.h" +#include "anastasis/anastasis-database/insert_truth_payment.h" +#include "anastasis/anastasis-database/get_truth_payment.h" +#include "anastasis/anastasis-database/do_verify_challenge_code.h" +#include "anastasis/anastasis-database/update_to_challenge_code_satisfied.h" +#include "anastasis/anastasis-database/get_exists_challenge_code_satisfied.h" +#include "anastasis/anastasis-database/insert_challenge_code.h" +#include "anastasis/anastasis-database/update_to_challenge_code_sent.h" +#include "anastasis/anastasis-database/gc_challenge_codes.h" +#include "anastasis/anastasis-database/insert_challenge_payment.h" +#include "anastasis/anastasis-database/update_to_challenge_payment_refunded.h" +#include "anastasis/anastasis-database/get_pending_challenge_payment.h" +#include "anastasis/anastasis-database/update_to_challenge_payment_paid.h" +#include "anastasis/anastasis-database/insert_auth_iban_in.h" +#include "anastasis/anastasis-database/iterate_auth_iban_transfers.h" +#include "anastasis/anastasis-database/get_last_auth_iban_in_row.h" /** diff --git a/src/include/meson.build b/src/include/meson.build @@ -26,33 +26,32 @@ install_data( 'anastasis/anastasis-database/create_tables.h', 'anastasis/anastasis-database/drop_tables.h', 'anastasis/anastasis-database/gc.h', - 'anastasis/anastasis-database/store_recovery_document.h', - 'anastasis/anastasis-database/get_recovery_meta_data.h', + 'anastasis/anastasis-database/do_insert_recovery_document.h', + 'anastasis/anastasis-database/iterate_recovery_meta_data.h', 'anastasis/anastasis-database/get_recovery_document.h', 'anastasis/anastasis-database/get_latest_recovery_document.h', - 'anastasis/anastasis-database/store_truth.h', - 'anastasis/anastasis-database/get_escrow_challenge.h', - 'anastasis/anastasis-database/get_key_share.h', - 'anastasis/anastasis-database/lookup_account.h', - 'anastasis/anastasis-database/check_payment_identifier.h', - 'anastasis/anastasis-database/check_challenge_payment.h', - 'anastasis/anastasis-database/increment_lifetime.h', - 'anastasis/anastasis-database/update_lifetime.h', - 'anastasis/anastasis-database/record_recdoc_payment.h', - 'anastasis/anastasis-database/record_truth_upload_payment.h', - 'anastasis/anastasis-database/check_truth_upload_paid.h', - 'anastasis/anastasis-database/verify_challenge_code.h', - 'anastasis/anastasis-database/mark_challenge_code_satisfied.h', - 'anastasis/anastasis-database/test_challenge_code_satisfied.h', - 'anastasis/anastasis-database/create_challenge_code.h', - 'anastasis/anastasis-database/mark_challenge_sent.h', - 'anastasis/anastasis-database/challenge_gc.h', - 'anastasis/anastasis-database/record_challenge_payment.h', - 'anastasis/anastasis-database/record_challenge_refund.h', - 'anastasis/anastasis-database/lookup_challenge_payment.h', - 'anastasis/anastasis-database/update_challenge_payment.h', - 'anastasis/anastasis-database/record_auth_iban_payment.h', - 'anastasis/anastasis-database/test_auth_iban_payment.h', - 'anastasis/anastasis-database/get_last_auth_iban_payment_row.h', + 'anastasis/anastasis-database/insert_truth.h', + 'anastasis/anastasis-database/get_truth.h', + 'anastasis/anastasis-database/get_truth_key_share.h', + 'anastasis/anastasis-database/get_account.h', + 'anastasis/anastasis-database/get_recdoc_payment.h', + 'anastasis/anastasis-database/get_challenge_payment.h', + 'anastasis/anastasis-database/do_update_account_lifetime.h', + 'anastasis/anastasis-database/insert_recdoc_payment.h', + 'anastasis/anastasis-database/insert_truth_payment.h', + 'anastasis/anastasis-database/get_truth_payment.h', + 'anastasis/anastasis-database/do_verify_challenge_code.h', + 'anastasis/anastasis-database/update_to_challenge_code_satisfied.h', + 'anastasis/anastasis-database/get_exists_challenge_code_satisfied.h', + 'anastasis/anastasis-database/insert_challenge_code.h', + 'anastasis/anastasis-database/update_to_challenge_code_sent.h', + 'anastasis/anastasis-database/gc_challenge_codes.h', + 'anastasis/anastasis-database/insert_challenge_payment.h', + 'anastasis/anastasis-database/update_to_challenge_payment_refunded.h', + 'anastasis/anastasis-database/get_pending_challenge_payment.h', + 'anastasis/anastasis-database/update_to_challenge_payment_paid.h', + 'anastasis/anastasis-database/insert_auth_iban_in.h', + 'anastasis/anastasis-database/iterate_auth_iban_transfers.h', + 'anastasis/anastasis-database/get_last_auth_iban_in_row.h', install_dir: anastasisincludedir / 'anastasis-database', ) diff --git a/src/stasis/amalgamate-sql.sh b/src/stasis/amalgamate-sql.sh @@ -0,0 +1,46 @@ +#!/bin/sh + +# This file is part of Anastasis +# Copyright (C) 2026 Anastasis SARL +# +# ANASTASIS is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 3, or (at your option) any later version. +# +# ANASTASIS is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along with +# ANASTASIS; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + +set -eu + +if [ $# -lt 1 ]; then + echo "Usage: $0 SQLFILES..." >&2 + exit 1 +fi + +cat <<'EOF' +-- +-- This file is part of Anastasis +-- Copyright (C) 2020--2026 Anastasis SARL +-- +-- ANASTASIS is free software; you can redistribute it and/or modify it under the +-- terms of the GNU General Public License as published by the Free Software +-- Foundation; either version 3, or (at your option) any later version. +-- +-- ANASTASIS is distributed in the hope that it will be useful, but WITHOUT ANY +-- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +-- A PARTICULAR PURPOSE. See the GNU General Public License for more details. +-- +-- You should have received a copy of the GNU General Public License along with +-- ANASTASIS; see the file COPYING. If not, see <http://www.gnu.org/licenses/> +-- +EOF + +for x in $@; do + echo -- generated from $x + # Remove SQL comments and empty lines + cat $x | sed -e "s/--.*//" | awk 'NF' +done diff --git a/src/stasis/anastasis-db_challenge_gc.c b/src/stasis/anastasis-db_challenge_gc.c @@ -1,55 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file stasis/anastasis-db_challenge_gc.c - * @brief Anastasis database: challenge gc - * @author Christian Grothoff - */ -#include "platform.h" -#include "anastasis-db_pg.h" -#include "anastasis/anastasis-database/challenge_gc.h" -#include "anastasis/anastasis-database/transaction.h" -#include "anastasis/anastasis-database/preflight.h" -#include <taler/taler_pq_lib.h> - - -/** - * Function called to remove all expired codes from the database. - * - * @return transaction status - */ -enum GNUNET_DB_QueryStatus -ANASTASIS_DB_challenge_gc (void) -{ - struct GNUNET_TIME_Timestamp time_now = GNUNET_TIME_timestamp_get (); - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_timestamp (&time_now), - GNUNET_PQ_query_param_end - }; - - GNUNET_break (GNUNET_OK == - ANASTASIS_DB_preflight ()); - PREPARE ("gc_challengecodes", - "DELETE FROM anastasis_challengecode " - "WHERE " - "expiration_date < $1;"); - return GNUNET_PQ_eval_prepared_non_select (pg->conn, - "gc_challengecodes", - params); -} - - -/* end of anastasis-db_challenge_gc.c */ diff --git a/src/stasis/anastasis-db_check_challenge_payment.c b/src/stasis/anastasis-db_check_challenge_payment.c @@ -1,71 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file stasis/anastasis-db_check_challenge_payment.c - * @brief Anastasis database: check challenge payment - * @author Christian Grothoff - */ -#include "platform.h" -#include "anastasis-db_pg.h" -#include "anastasis/anastasis-database/check_challenge_payment.h" -#include "anastasis/anastasis-database/transaction.h" -#include "anastasis/anastasis-database/preflight.h" -#include <taler/taler_pq_lib.h> - - -/** - * Check payment identifier. Used to check if a payment identifier given by - * the user is valid (existing and paid). - * - * @param payment_secret payment secret which the user must provide with every upload - * @param truth_uuid which truth should we check the payment status of - * @param[out] paid bool value to show if payment is paid - * @return transaction status - */ -enum GNUNET_DB_QueryStatus -ANASTASIS_DB_check_challenge_payment ( - const struct ANASTASIS_PaymentSecretP *payment_secret, - const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid, - bool *paid) -{ - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_auto_from_type (payment_secret), - GNUNET_PQ_query_param_auto_from_type (truth_uuid), - GNUNET_PQ_query_param_end - }; - struct GNUNET_PQ_ResultSpec rs[] = { - GNUNET_PQ_result_spec_bool ("paid", - paid), - GNUNET_PQ_result_spec_end - }; - - PREPARE ("challenge_payment_select", - "SELECT" - " paid" - " FROM anastasis_challenge_payment" - " WHERE payment_identifier=$1" - " AND truth_uuid=$2" - " AND refunded=FALSE" - " AND counter>0;"); - return GNUNET_PQ_eval_prepared_singleton_select ( - pg->conn, - "challenge_payment_select", - params, - rs); -} - - -/* end of anastasis-db_check_challenge_payment.c */ diff --git a/src/stasis/anastasis-db_check_payment_identifier.c b/src/stasis/anastasis-db_check_payment_identifier.c @@ -1,71 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file stasis/anastasis-db_check_payment_identifier.c - * @brief Anastasis database: check payment identifier - * @author Christian Grothoff - */ -#include "platform.h" -#include "anastasis-db_pg.h" -#include "anastasis/anastasis-database/check_payment_identifier.h" -#include "anastasis/anastasis-database/transaction.h" -#include "anastasis/anastasis-database/preflight.h" -#include <taler/taler_pq_lib.h> - - -/** - * Check payment identifier. Used to check if a payment identifier given by - * the user is valid (existing and paid). - * - * @param payment_secret payment secret which the user must provide with every upload - * @param[out] paid bool value to show if payment is paid - * @param[out] valid_counter bool value to show if post_counter is > 0 - * @return transaction status - */ -enum GNUNET_DB_QueryStatus -ANASTASIS_DB_check_payment_identifier ( - const struct ANASTASIS_PaymentSecretP *payment_secret, - bool *paid, - bool *valid_counter) -{ - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_auto_from_type (payment_secret), - GNUNET_PQ_query_param_end - }; - struct GNUNET_PQ_ResultSpec rs[] = { - GNUNET_PQ_result_spec_bool ("paid", - paid), - GNUNET_PQ_result_spec_bool ("valid_counter", - valid_counter), - GNUNET_PQ_result_spec_end - }; - - PREPARE ("recdoc_payment_select", - "SELECT" - " creation_date" - ",post_counter > 0 AS valid_counter" - ",amount" - ",paid" - " FROM anastasis_recdoc_payment" - " WHERE payment_identifier=$1;"); - return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, - "recdoc_payment_select", - params, - rs); -} - - -/* end of anastasis-db_check_payment_identifier.c */ diff --git a/src/stasis/anastasis-db_check_truth_upload_paid.c b/src/stasis/anastasis-db_check_truth_upload_paid.c @@ -1,66 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file stasis/anastasis-db_check_truth_upload_paid.c - * @brief Anastasis database: check truth upload paid - * @author Christian Grothoff - */ -#include "platform.h" -#include "anastasis-db_pg.h" -#include "anastasis/anastasis-database/check_truth_upload_paid.h" -#include "anastasis/anastasis-database/transaction.h" -#include "anastasis/anastasis-database/preflight.h" -#include <taler/taler_pq_lib.h> - - -/** - * Inquire whether truth upload payment was made. - * - * @param uuid the truth's UUID - * @param[out] paid_until set for how long this truth is paid for - * @return transaction status - */ -enum GNUNET_DB_QueryStatus -ANASTASIS_DB_check_truth_upload_paid ( - const struct ANASTASIS_CRYPTO_TruthUUIDP *uuid, - struct GNUNET_TIME_Timestamp *paid_until) -{ - struct GNUNET_TIME_Timestamp now = GNUNET_TIME_timestamp_get (); - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_auto_from_type (uuid), - GNUNET_PQ_query_param_timestamp (&now), - GNUNET_PQ_query_param_end - }; - struct GNUNET_PQ_ResultSpec rs[] = { - GNUNET_PQ_result_spec_timestamp ("expiration", - paid_until), - GNUNET_PQ_result_spec_end - }; - - PREPARE ("truth_payment_select", - "SELECT" - " expiration" - " FROM anastasis_truth_payment" - " WHERE truth_uuid=$1" - " AND expiration>$2;"); - return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, - "truth_payment_select", - params, - rs); -} - - -/* end of anastasis-db_check_truth_upload_paid.c */ diff --git a/src/stasis/anastasis-db_create_challenge_code.c b/src/stasis/anastasis-db_create_challenge_code.c @@ -1,195 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file stasis/anastasis-db_create_challenge_code.c - * @brief Anastasis database: create challenge code - * @author Christian Grothoff - */ -#include "platform.h" -#include "anastasis-db_pg.h" -#include "anastasis/anastasis-database/create_challenge_code.h" -#include "anastasis/anastasis-database/transaction.h" -#include "anastasis/anastasis-database/preflight.h" -#include <taler/taler_pq_lib.h> - - -/** - * Create a new challenge code for a given challenge identified by the challenge - * public key. The function will first check if there is already a valid code - * for this challenge present and won't insert a new one in this case. - * - * @param truth_uuid the identifier for the challenge - * @param rotation_period for how long is the code available - * @param validity_period for how long is the code available - * @param retry_counter amount of retries allowed - * @param[out] retransmission_date when to next retransmit - * @param[out] code set to the code which will be checked for later - * @return transaction status, - * #GNUNET_DB_STATUS_SUCCESS_NO_RESULTS if we are out of valid tries, - * #GNUNET_DB_STATUS_SUCCESS_ONE_RESULT if @a code is now in the DB - */ -enum GNUNET_DB_QueryStatus -ANASTASIS_DB_create_challenge_code ( - const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid, - struct GNUNET_TIME_Relative rotation_period, - struct GNUNET_TIME_Relative validity_period, - uint32_t retry_counter, - struct GNUNET_TIME_Timestamp *retransmission_date, - uint64_t *code) -{ - struct GNUNET_TIME_Timestamp now = GNUNET_TIME_timestamp_get (); - struct GNUNET_TIME_Timestamp expiration_date; - struct GNUNET_TIME_Absolute ex_rot; - - PREPARE ("challengecode_select_meta", - "SELECT " - " code" - ",retry_counter" - ",retransmission_date" - " FROM anastasis_challengecode" - " WHERE truth_uuid=$1" - " AND expiration_date > $2" - " AND creation_date > $3" - " ORDER BY creation_date DESC" - " LIMIT 1;"); - PREPARE ("challengecode_insert", - "INSERT INTO anastasis_challengecode " - "(truth_uuid" - ",code" - ",creation_date" - ",expiration_date" - ",retry_counter" - ") VALUES " - "($1, $2, $3, $4, $5);"); - expiration_date = GNUNET_TIME_relative_to_timestamp (validity_period); - ex_rot = GNUNET_TIME_absolute_subtract (now.abs_time, - rotation_period); - for (unsigned int retries = 0; retries<MAX_RETRIES; retries++) - { - if (GNUNET_OK != - ANASTASIS_DB_start ( - "create_challenge_code")) - { - GNUNET_break (0); - return GNUNET_DB_STATUS_HARD_ERROR; - } - - { - uint32_t old_retry_counter; - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_auto_from_type (truth_uuid), - GNUNET_PQ_query_param_timestamp (&now), - GNUNET_PQ_query_param_absolute_time (&ex_rot), - GNUNET_PQ_query_param_end - }; - struct GNUNET_PQ_ResultSpec rs[] = { - GNUNET_PQ_result_spec_uint64 ("code", - code), - GNUNET_PQ_result_spec_uint32 ("retry_counter", - &old_retry_counter), - GNUNET_PQ_result_spec_timestamp ("retransmission_date", - retransmission_date), - GNUNET_PQ_result_spec_end - }; - enum GNUNET_DB_QueryStatus qs; - - qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn, - "challengecode_select_meta", - params, - rs); - switch (qs) - { - case GNUNET_DB_STATUS_HARD_ERROR: - GNUNET_break (0); - ANASTASIS_DB_rollback (); - return qs; - case GNUNET_DB_STATUS_SOFT_ERROR: - goto retry; - case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "No active challenge found, creating a fresh one\n"); - break; - case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: - if (0 == old_retry_counter) - { - ANASTASIS_DB_rollback (); - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "Active challenge %llu has zero tries left, refusing to create another one\n", - (unsigned long long) *code); - return GNUNET_DB_STATUS_SUCCESS_NO_RESULTS; - } - ANASTASIS_DB_rollback (); - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "Active challenge has %u tries left, returning old challenge %llu\n", - (unsigned int) old_retry_counter, - (unsigned long long) *code); - return qs; - } - } - - *code = GNUNET_CRYPTO_random_u64 (ANASTASIS_PIN_MAX_VALUE); - *retransmission_date = GNUNET_TIME_UNIT_ZERO_TS; - { - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_auto_from_type (truth_uuid), - GNUNET_PQ_query_param_uint64 (code), - GNUNET_PQ_query_param_timestamp (&now), - GNUNET_PQ_query_param_timestamp (&expiration_date), - GNUNET_PQ_query_param_uint32 (&retry_counter), - GNUNET_PQ_query_param_end - }; - enum GNUNET_DB_QueryStatus qs; - - qs = GNUNET_PQ_eval_prepared_non_select (pg->conn, - "challengecode_insert", - params); - switch (qs) - { - case GNUNET_DB_STATUS_HARD_ERROR: - ANASTASIS_DB_rollback (); - return GNUNET_DB_STATUS_HARD_ERROR; - case GNUNET_DB_STATUS_SOFT_ERROR: - goto retry; - case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: - GNUNET_break (0); - ANASTASIS_DB_rollback (); - return GNUNET_DB_STATUS_HARD_ERROR; - case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "Created fresh challenge with %u tries left\n", - (unsigned int) retry_counter); - break; - } - } - - { - enum GNUNET_DB_QueryStatus qs; - - qs = ANASTASIS_DB_commit (); - if (GNUNET_DB_STATUS_SOFT_ERROR == qs) - goto retry; - if (qs < 0) - return qs; - } - return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT; -retry: - ANASTASIS_DB_rollback (); - } - return GNUNET_DB_STATUS_SOFT_ERROR; -} - - -/* end of anastasis-db_create_challenge_code.c */ diff --git a/src/stasis/anastasis-db_create_tables.c b/src/stasis/anastasis-db_create_tables.c @@ -1,55 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020, 2021, 2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file stasis/anastasis-db_create_tables.c - * @brief create the Anastasis database tables - * @author Christian Grothoff - */ -#include "platform.h" -#include "anastasis-db_pg.h" -#include "anastasis/anastasis-database/create_tables.h" - - -enum GNUNET_GenericReturnValue -ANASTASIS_DB_create_tables (void) -{ - if (GNUNET_SYSERR == - GNUNET_PQ_load_versioning (pg->conn)) - { - GNUNET_break (0); - return GNUNET_SYSERR; - } - if (GNUNET_OK != - GNUNET_PQ_run_sql (pg->conn, - "stasis-")) - { - GNUNET_break (0); - return GNUNET_SYSERR; - } -#if 0 - if (GNUNET_OK != - GNUNET_PQ_exec_sql (pg->conn, - "procedures")) - { - GNUNET_break (0); - return GNUNET_SYSERR; - } -#endif - return GNUNET_OK; -} - - -/* end of anastasis-db_create_tables.c */ diff --git a/src/stasis/anastasis-db_drop_tables.c b/src/stasis/anastasis-db_drop_tables.c @@ -1,34 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020, 2021, 2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file stasis/anastasis-db_drop_tables.c - * @brief drop the Anastasis database tables - * @author Christian Grothoff - */ -#include "platform.h" -#include "anastasis-db_pg.h" -#include "anastasis/anastasis-database/drop_tables.h" - - -enum GNUNET_GenericReturnValue -ANASTASIS_DB_drop_tables (void) -{ - return GNUNET_PQ_exec_sql (pg->conn, - "drop"); -} - - -/* end of anastasis-db_drop_tables.c */ diff --git a/src/stasis/anastasis-db_gc.c b/src/stasis/anastasis-db_gc.c @@ -1,76 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020, 2021, 2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file stasis/anastasis-db_gc.c - * @brief garbage collection for the Anastasis database - * @author Christian Grothoff - */ -#include "platform.h" -#include "anastasis-db_pg.h" -#include "anastasis/anastasis-database/gc.h" -#include "anastasis/anastasis-database/preflight.h" -#include "anastasis/anastasis-database/transaction.h" - - -enum GNUNET_DB_QueryStatus -ANASTASIS_DB_gc (struct GNUNET_TIME_Absolute expire_backups, - struct GNUNET_TIME_Absolute expire_pending_payments) -{ - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_absolute_time (&expire_backups), - GNUNET_PQ_query_param_end - }; - struct GNUNET_PQ_QueryParam params2[] = { - GNUNET_PQ_query_param_absolute_time (&expire_pending_payments), - GNUNET_PQ_query_param_end - }; - enum GNUNET_DB_QueryStatus qs; - - GNUNET_break (GNUNET_OK == - ANASTASIS_DB_preflight ()); -#if 0 - /* FIXME: should we do this? */ - PREPARE ("gc_challenge_pending_payments", - "DELETE FROM anastasis_challenge_payment " - "WHERE" - " (paid=FALSE" - " OR" - " refunded)" - " AND" - " creation_date < $1;"), -#endif - PREPARE ("gc_accounts", - "DELETE FROM anastasis_user " - "WHERE" - " expiration_date < $1;"); - qs = GNUNET_PQ_eval_prepared_non_select (pg->conn, - "gc_accounts", - params); - if (qs < 0) - return qs; - PREPARE ("gc_recdoc_pending_payments", - "DELETE FROM anastasis_recdoc_payment " - "WHERE" - " paid=FALSE" - " AND" - " creation_date < $1;"); - return GNUNET_PQ_eval_prepared_non_select (pg->conn, - "gc_recdoc_pending_payments", - params2); -} - - -/* end of anastasis-db_gc.c */ diff --git a/src/stasis/anastasis-db_get_escrow_challenge.c b/src/stasis/anastasis-db_get_escrow_challenge.c @@ -1,76 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file stasis/anastasis-db_get_escrow_challenge.c - * @brief Anastasis database: get escrow challenge - * @author Christian Grothoff - */ -#include "platform.h" -#include "anastasis-db_pg.h" -#include "anastasis/anastasis-database/get_escrow_challenge.h" -#include "anastasis/anastasis-database/transaction.h" -#include "anastasis/anastasis-database/preflight.h" -#include <taler/taler_pq_lib.h> - - -/** - * Get the encrypted truth to validate the challenge response - * - * @param truth_uuid the identifier for the Truth - * @param[out] truth contains the encrypted truth - * @param[out] truth_size size of the encrypted truth - * @param[out] truth_mime mime type of truth - * @param[out] method type of the challenge - * @return transaction status - */ -enum GNUNET_DB_QueryStatus -ANASTASIS_DB_get_escrow_challenge ( - const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid, - void **truth, - size_t *truth_size, - char **truth_mime, - char **method) -{ - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_auto_from_type (truth_uuid), - GNUNET_PQ_query_param_end - }; - struct GNUNET_PQ_ResultSpec rs[] = { - GNUNET_PQ_result_spec_variable_size ("encrypted_truth", - truth, - truth_size), - GNUNET_PQ_result_spec_string ("truth_mime", - truth_mime), - GNUNET_PQ_result_spec_string ("method_name", - method), - GNUNET_PQ_result_spec_end - }; - - PREPARE ("truth_select", - "SELECT " - " method_name" - ",encrypted_truth" - ",truth_mime" - " FROM anastasis_truth" - " WHERE truth_uuid=$1;"); - return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, - "truth_select", - params, - rs); -} - - -/* end of anastasis-db_get_escrow_challenge.c */ diff --git a/src/stasis/anastasis-db_get_key_share.c b/src/stasis/anastasis-db_get_key_share.c @@ -1,64 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file stasis/anastasis-db_get_key_share.c - * @brief Anastasis database: get key share - * @author Christian Grothoff - */ -#include "platform.h" -#include "anastasis-db_pg.h" -#include "anastasis/anastasis-database/get_key_share.h" -#include "anastasis/anastasis-database/transaction.h" -#include "anastasis/anastasis-database/preflight.h" -#include <taler/taler_pq_lib.h> - - -/** - * Lookup (encrypted) key share by @a truth_uuid. - * - * @param truth_uuid the identifier for the Truth - * @param[out] key_share contains the encrypted Keyshare - * @return transaction status - */ -enum GNUNET_DB_QueryStatus -ANASTASIS_DB_get_key_share ( - const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid, - struct ANASTASIS_CRYPTO_EncryptedKeyShareP *key_share) -{ - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_auto_from_type (truth_uuid), - GNUNET_PQ_query_param_end - }; - struct GNUNET_PQ_ResultSpec rs[] = { - GNUNET_PQ_result_spec_auto_from_type ("key_share_data", - key_share), - GNUNET_PQ_result_spec_end - }; - - PREPARE ("key_share_select", - "SELECT " - "key_share_data " - "FROM " - "anastasis_truth " - "WHERE truth_uuid =$1;"); - return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, - "key_share_select", - params, - rs); -} - - -/* end of anastasis-db_get_key_share.c */ diff --git a/src/stasis/anastasis-db_get_last_auth_iban_payment_row.c b/src/stasis/anastasis-db_get_last_auth_iban_payment_row.c @@ -1,69 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file stasis/anastasis-db_get_last_auth_iban_payment_row.c - * @brief Anastasis database: get last auth iban payment row - * @author Christian Grothoff - */ -#include "platform.h" -#include "anastasis-db_pg.h" -#include "anastasis/anastasis-database/get_last_auth_iban_payment_row.h" -#include "anastasis/anastasis-database/transaction.h" -#include "anastasis/anastasis-database/preflight.h" -#include <taler/taler_pq_lib.h> - - -/** - * Function to check the last known IBAN payment. - * - * @param credit_account which credit account to check - * @param[out] last_row set to the last known row - * @return transaction status, - * #GNUNET_DB_STATUS_SUCCESS_ONE_RESULT if @a cb - * returned 'true' once - * #GNUNET_DB_STATUS_SUCCESS_NO_RESULTS if no - * wire transfers existed for which @a cb returned true - */ -enum GNUNET_DB_QueryStatus -ANASTASIS_DB_get_last_auth_iban_payment_row ( - const char *credit_account, - uint64_t *last_row) -{ - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_string (credit_account), - GNUNET_PQ_query_param_end - }; - struct GNUNET_PQ_ResultSpec rs[] = { - GNUNET_PQ_result_spec_uint64 ("wire_reference", - last_row), - GNUNET_PQ_result_spec_end - }; - - PREPARE ("get_last_auth_iban_payment", - "SELECT " - " wire_reference" - " FROM anastasis_auth_iban_in" - " WHERE credit_account_details=$1" - " ORDER BY wire_reference DESC" - " LIMIT 1;"); - return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, - "get_last_auth_iban_payment", - params, - rs); -} - - -/* end of anastasis-db_get_last_auth_iban_payment_row.c */ diff --git a/src/stasis/anastasis-db_get_latest_recovery_document.c b/src/stasis/anastasis-db_get_latest_recovery_document.c @@ -1,85 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file stasis/anastasis-db_get_latest_recovery_document.c - * @brief Anastasis database: get latest recovery document - * @author Christian Grothoff - */ -#include "platform.h" -#include "anastasis-db_pg.h" -#include "anastasis/anastasis-database/get_latest_recovery_document.h" -#include "anastasis/anastasis-database/transaction.h" -#include "anastasis/anastasis-database/preflight.h" -#include <taler/taler_pq_lib.h> - - -/** - * Fetch latest recovery document for user. - * - * @param account_pub public key of the user's account - * @param account_sig signature - * @param recovery_data_hash hash of the current recovery data - * @param data_size size of data blob - * @param data blob which contains the recovery document - * @param[out] version set to the version number of the policy being returned - * @return transaction status - */ -enum GNUNET_DB_QueryStatus -ANASTASIS_DB_get_latest_recovery_document ( - const struct ANASTASIS_CRYPTO_AccountPublicKeyP *account_pub, - struct ANASTASIS_AccountSignatureP *account_sig, - struct GNUNET_HashCode *recovery_data_hash, - size_t *data_size, - void **data, - uint32_t *version) -{ - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_auto_from_type (account_pub), - GNUNET_PQ_query_param_end - }; - struct GNUNET_PQ_ResultSpec rs[] = { - GNUNET_PQ_result_spec_uint32 ("version", - version), - GNUNET_PQ_result_spec_auto_from_type ("account_sig", - account_sig), - GNUNET_PQ_result_spec_auto_from_type ("recovery_data_hash", - recovery_data_hash), - GNUNET_PQ_result_spec_variable_size ("recovery_data", - data, - data_size), - GNUNET_PQ_result_spec_end - }; - - GNUNET_break (GNUNET_OK == - ANASTASIS_DB_preflight ()); - PREPARE ("latest_recoverydocument_select", - "SELECT " - " version" - ",account_sig" - ",recovery_data_hash" - ",recovery_data" - " FROM anastasis_recoverydocument" - " WHERE user_id=$1" - " ORDER BY version DESC" - " LIMIT 1;"); - return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, - "latest_recoverydocument_select", - params, - rs); -} - - -/* end of anastasis-db_get_latest_recovery_document.c */ diff --git a/src/stasis/anastasis-db_get_recovery_document.c b/src/stasis/anastasis-db_get_recovery_document.c @@ -1,80 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file stasis/anastasis-db_get_recovery_document.c - * @brief Anastasis database: get recovery document - * @author Christian Grothoff - */ -#include "platform.h" -#include "anastasis-db_pg.h" -#include "anastasis/anastasis-database/get_recovery_document.h" -#include "anastasis/anastasis-database/transaction.h" -#include "anastasis/anastasis-database/preflight.h" -#include <taler/taler_pq_lib.h> - - -/** - * Fetch recovery document for user according given version. - * - * @param account_pub public key of the user's account - * @param version the version number of the policy the user requests - * @param[out] account_sig signature - * @param[out] recovery_data_hash hash of the current recovery data - * @param[out] data_size size of data blob - * @param[out] data blob which contains the recovery document - * @return transaction status - */ -enum GNUNET_DB_QueryStatus -ANASTASIS_DB_get_recovery_document ( - const struct ANASTASIS_CRYPTO_AccountPublicKeyP *account_pub, - uint32_t version, - struct ANASTASIS_AccountSignatureP *account_sig, - struct GNUNET_HashCode *recovery_data_hash, - size_t *data_size, - void **data) -{ - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_auto_from_type (account_pub), - GNUNET_PQ_query_param_uint32 (&version), - GNUNET_PQ_query_param_end - }; - struct GNUNET_PQ_ResultSpec rs[] = { - GNUNET_PQ_result_spec_auto_from_type ("account_sig", - account_sig), - GNUNET_PQ_result_spec_auto_from_type ("recovery_data_hash", - recovery_data_hash), - GNUNET_PQ_result_spec_variable_size ("recovery_data", - data, - data_size), - GNUNET_PQ_result_spec_end - }; - - PREPARE ("recoverydocument_select", - "SELECT " - " account_sig" - ",recovery_data_hash" - ",recovery_data" - " FROM anastasis_recoverydocument" - " WHERE user_id=$1" - " AND version=$2;"); - return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, - "recoverydocument_select", - params, - rs); -} - - -/* end of anastasis-db_get_recovery_document.c */ diff --git a/src/stasis/anastasis-db_get_recovery_meta_data.c b/src/stasis/anastasis-db_get_recovery_meta_data.c @@ -1,156 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file stasis/anastasis-db_get_recovery_meta_data.c - * @brief Anastasis database: get recovery meta data - * @author Christian Grothoff - */ -#include "platform.h" -#include "anastasis-db_pg.h" -#include "anastasis/anastasis-database/get_recovery_meta_data.h" -#include "anastasis/anastasis-database/transaction.h" -#include "anastasis/anastasis-database/preflight.h" -#include <taler/taler_pq_lib.h> - - -struct MetaIteratorContext -{ - /** - * Function to call on each result. - */ - ANASTASIS_DB_RecoveryMetaCallback cb; - - /** - * Closure for @e cb. - */ - void *cb_cls; - - /** - * Set to true on database failure. - */ - bool db_failure; -}; - - -/** - * Helper function for #postgres_get_recovery_meta_data(). - * To be called with the results of a SELECT statement - * that has returned @a num_results results. - * - * @param cls closure of type `struct MetaIteratorContext *` - * @param result the postgres result - * @param num_results the number of results in @a result - */ -static void -meta_iterator (void *cls, - PGresult *result, - unsigned int num_results) -{ - struct MetaIteratorContext *ctx = cls; - - for (unsigned int i = 0; i<num_results; i++) - { - uint32_t version; - void *meta_data; - size_t meta_data_size; - struct GNUNET_TIME_Timestamp ts; - struct GNUNET_PQ_ResultSpec rs[] = { - GNUNET_PQ_result_spec_uint32 ("version", - &version), - GNUNET_PQ_result_spec_timestamp ("creation_date", - &ts), - GNUNET_PQ_result_spec_variable_size ("recovery_meta_data", - &meta_data, - &meta_data_size), - GNUNET_PQ_result_spec_end - }; - enum GNUNET_GenericReturnValue ret; - - if (GNUNET_OK != - GNUNET_PQ_extract_result (result, - rs, - i)) - { - GNUNET_break (0); - ctx->db_failure = true; - return; - } - ret = ctx->cb (ctx->cb_cls, - version, - ts, - meta_data, - meta_data_size); - GNUNET_PQ_cleanup_result (rs); - if (GNUNET_OK != ret) - break; - } -} - - -/** - * Fetch recovery document meta data for user. Returns - * meta data in descending order from @a max_version. - * The size of the result set may be limited. - * - * @param cls closure - * @param account_pub public key of the user's account - * @param max_version the maximum version number the user requests - * @param cb function to call on each result - * @param cb_cls closure for @a cb - * @return transaction status - */ -enum GNUNET_DB_QueryStatus -ANASTASIS_DB_get_recovery_meta_data ( - const struct ANASTASIS_CRYPTO_AccountPublicKeyP *account_pub, - uint32_t max_version, - ANASTASIS_DB_RecoveryMetaCallback cb, - void *cb_cls) -{ - struct MetaIteratorContext ctx = { - .cb = cb, - .cb_cls = cb_cls - }; - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_auto_from_type (account_pub), - GNUNET_PQ_query_param_uint32 (&max_version), - GNUNET_PQ_query_param_end - }; - enum GNUNET_DB_QueryStatus qs; - - PREPARE ("recoverydocument_select_meta", - "SELECT " - " version" - ",creation_date" - ",recovery_meta_data" - " FROM anastasis_recoverydocument" - " WHERE user_id=$1" - " AND version < $2" - " ORDER BY version DESC" - " LIMIT 1000;"); - qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn, - "recoverydocument_select_meta", - params, - &meta_iterator, - &ctx); - if (qs < 0) - return qs; - if (ctx.db_failure) - return GNUNET_DB_STATUS_HARD_ERROR; - return qs; -} - - -/* end of anastasis-db_get_recovery_meta_data.c */ diff --git a/src/stasis/anastasis-db_increment_lifetime.c b/src/stasis/anastasis-db_increment_lifetime.c @@ -1,245 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file stasis/anastasis-db_increment_lifetime.c - * @brief Anastasis database: increment lifetime - * @author Christian Grothoff - */ -#include "platform.h" -#include "anastasis-db_pg.h" -#include "anastasis/anastasis-database/increment_lifetime.h" -#include "anastasis/anastasis-database/transaction.h" -#include "anastasis/anastasis-database/preflight.h" -#include <taler/taler_pq_lib.h> - - -/** - * Increment account lifetime based on payment having been received. - * Does nothing if the payment is not new. - * - * @param account_pub which account received a payment - * @param payment_identifier proof of payment, must be unique and match pending payment - * @param lifetime for how long is the account now paid (increment) - * @param[out] paid_until set to the end of the lifetime after the operation - * @return transaction status - */ -enum GNUNET_DB_QueryStatus -ANASTASIS_DB_increment_lifetime ( - const struct ANASTASIS_CRYPTO_AccountPublicKeyP *account_pub, - const struct ANASTASIS_PaymentSecretP *payment_identifier, - struct GNUNET_TIME_Relative lifetime, - struct GNUNET_TIME_Timestamp *paid_until) -{ - enum GNUNET_DB_QueryStatus qs; - - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "Incrementing lifetime of account %s based on payment by %s\n", - TALER_B2S (account_pub), - GNUNET_TIME_relative2s (lifetime, - true)); - PREPARE ("user_insert", - "INSERT INTO anastasis_user " - "(user_id" - ",expiration_date" - ") VALUES " - "($1, $2);"); - PREPARE ("user_select", - "SELECT" - " expiration_date " - "FROM anastasis_user" - " WHERE user_id=$1" - " FOR UPDATE;"); - PREPARE ("user_update", - "UPDATE anastasis_user" - " SET " - " expiration_date=$1" - " WHERE user_id=$2;"); - PREPARE ("recdoc_payment_done", - "UPDATE anastasis_recdoc_payment " - "SET" - " paid=TRUE " - "WHERE" - " payment_identifier=$1" - " AND" - " user_id=$2" - " AND" - " paid=FALSE;"); - for (unsigned int retries = 0; retries<MAX_RETRIES; retries++) - { - if (GNUNET_OK != - ANASTASIS_DB_start ( - "increment lifetime")) - { - GNUNET_break (0); - return GNUNET_DB_STATUS_HARD_ERROR; - } - - { - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_auto_from_type (payment_identifier), - GNUNET_PQ_query_param_auto_from_type (account_pub), - GNUNET_PQ_query_param_end - }; - qs = GNUNET_PQ_eval_prepared_non_select (pg->conn, - "recdoc_payment_done", - params); - switch (qs) - { - case GNUNET_DB_STATUS_HARD_ERROR: - ANASTASIS_DB_rollback (); - *paid_until = GNUNET_TIME_UNIT_ZERO_TS; - return qs; - case GNUNET_DB_STATUS_SOFT_ERROR: - goto retry; - case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: - /* Payment not new or payment request unknown. */ - /* continued below */ - break; - case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: - /* Payment just now marked as 'paid' */ - /* continued below */ - break; - } - } - - { - enum GNUNET_DB_QueryStatus qs2; - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_auto_from_type (account_pub), - GNUNET_PQ_query_param_end - }; - struct GNUNET_TIME_Timestamp expiration; - struct GNUNET_PQ_ResultSpec rs[] = { - GNUNET_PQ_result_spec_timestamp ("expiration_date", - &expiration), - GNUNET_PQ_result_spec_end - }; - - qs2 = GNUNET_PQ_eval_prepared_singleton_select (pg->conn, - "user_select", - params, - rs); - switch (qs2) - { - case GNUNET_DB_STATUS_HARD_ERROR: - ANASTASIS_DB_rollback (); - return qs2; - case GNUNET_DB_STATUS_SOFT_ERROR: - goto retry; - case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: - if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs) - { - /* inconsistent, cannot have recdoc payment but no user!? */ - GNUNET_break (0); - ANASTASIS_DB_rollback (); - return GNUNET_DB_STATUS_HARD_ERROR; - } - else - { - /* user does not exist, create new one */ - struct GNUNET_PQ_QueryParam iparams[] = { - GNUNET_PQ_query_param_auto_from_type (account_pub), - GNUNET_PQ_query_param_timestamp (&expiration), - GNUNET_PQ_query_param_end - }; - - expiration = GNUNET_TIME_relative_to_timestamp (lifetime); - GNUNET_break (! GNUNET_TIME_absolute_is_never (expiration.abs_time)); - *paid_until = expiration; - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "Creating new account %s with initial lifetime of %s\n", - TALER_B2S (account_pub), - GNUNET_TIME_relative2s (lifetime, - true)); - qs = GNUNET_PQ_eval_prepared_non_select (pg->conn, - "user_insert", - iparams); - } - break; - case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: - if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs) - { - /* existing rec doc payment (payment replay), return - existing expiration */ - *paid_until = expiration; - ANASTASIS_DB_rollback (); - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "Payment existed, lifetime of account %s unchanged at %s\n", - TALER_B2S (account_pub), - GNUNET_TIME_timestamp2s (*paid_until)); - return GNUNET_DB_STATUS_SUCCESS_NO_RESULTS; - } - else - { - /* user exists, payment is new, update expiration_date */ - struct GNUNET_PQ_QueryParam iparams[] = { - GNUNET_PQ_query_param_timestamp (&expiration), - GNUNET_PQ_query_param_auto_from_type (account_pub), - GNUNET_PQ_query_param_end - }; - - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "Incrementing lifetime of account %s by %s\n", - TALER_B2S (account_pub), - GNUNET_TIME_relative2s (lifetime, - true)); - expiration - = GNUNET_TIME_absolute_to_timestamp ( - GNUNET_TIME_absolute_add (expiration.abs_time, - lifetime)); - GNUNET_break (! GNUNET_TIME_absolute_is_never ( - expiration.abs_time)); - *paid_until = expiration; - qs = GNUNET_PQ_eval_prepared_non_select (pg->conn, - "user_update", - iparams); - } - break; - } - } - - switch (qs) - { - case GNUNET_DB_STATUS_HARD_ERROR: - ANASTASIS_DB_rollback (); - return qs; - case GNUNET_DB_STATUS_SOFT_ERROR: - goto retry; - case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: - GNUNET_break (0); - ANASTASIS_DB_rollback (); - return GNUNET_DB_STATUS_HARD_ERROR; - case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: - break; - } - qs = ANASTASIS_DB_commit (); - if (GNUNET_DB_STATUS_SOFT_ERROR == qs) - goto retry; - if (qs < 0) - return GNUNET_DB_STATUS_HARD_ERROR; - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "Incremented lifetime of account %s to %s\n", - TALER_B2S (account_pub), - GNUNET_TIME_timestamp2s (*paid_until)); - return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT; -retry: - ANASTASIS_DB_rollback (); - } - return GNUNET_DB_STATUS_SOFT_ERROR; -} - - -/* end of anastasis-db_increment_lifetime.c */ diff --git a/src/stasis/anastasis-db_lookup_account.c b/src/stasis/anastasis-db_lookup_account.c @@ -1,136 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file stasis/anastasis-db_lookup_account.c - * @brief Anastasis database: lookup account - * @author Christian Grothoff - */ -#include "platform.h" -#include "anastasis-db_pg.h" -#include "anastasis/anastasis-database/lookup_account.h" -#include "anastasis/anastasis-database/transaction.h" -#include "anastasis/anastasis-database/preflight.h" -#include <taler/taler_pq_lib.h> - - -/** - * Check if an account exists, and if so, return the - * current @a recovery_document_hash. - * - * @param account_pub account identifier - * @param[out] paid_until until when is the account paid up? - * @param[out] recovery_data_hash set to hash of @a recovery document - * @param[out] version set to the recovery policy version - * @return transaction status - */ -enum ANASTASIS_DB_AccountStatus -ANASTASIS_DB_lookup_account ( - const struct ANASTASIS_CRYPTO_AccountPublicKeyP *account_pub, - struct GNUNET_TIME_Timestamp *paid_until, - struct GNUNET_HashCode *recovery_data_hash, - uint32_t *version) -{ - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_auto_from_type (account_pub), - GNUNET_PQ_query_param_end - }; - enum GNUNET_DB_QueryStatus qs; - - PREPARE ("user_select2", - "SELECT" - " expiration_date " - "FROM anastasis_user" - " WHERE user_id=$1" - " FOR UPDATE;"); - PREPARE ("latest_recovery_version_select", - "SELECT" - " version" - ",recovery_data_hash" - ",expiration_date" - " FROM anastasis_recoverydocument" - " JOIN anastasis_user USING (user_id)" - " WHERE user_id=$1" - " ORDER BY version DESC" - " LIMIT 1;"); - GNUNET_break (GNUNET_OK == - ANASTASIS_DB_preflight ()); - { - struct GNUNET_PQ_ResultSpec rs[] = { - GNUNET_PQ_result_spec_timestamp ("expiration_date", - paid_until), - GNUNET_PQ_result_spec_auto_from_type ("recovery_data_hash", - recovery_data_hash), - GNUNET_PQ_result_spec_uint32 ("version", - version), - GNUNET_PQ_result_spec_end - }; - - qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn, - "latest_recovery_version_select", - params, - rs); - } - switch (qs) - { - case GNUNET_DB_STATUS_HARD_ERROR: - return ANASTASIS_DB_ACCOUNT_STATUS_HARD_ERROR; - case GNUNET_DB_STATUS_SOFT_ERROR: - GNUNET_break (0); - return ANASTASIS_DB_ACCOUNT_STATUS_HARD_ERROR; - case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: - break; /* handle interesting case below */ - case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: - return ANASTASIS_DB_ACCOUNT_STATUS_VALID_HASH_RETURNED; - } - - /* check if account exists */ - { - struct GNUNET_PQ_ResultSpec rs[] = { - GNUNET_PQ_result_spec_timestamp ("expiration_date", - paid_until), - GNUNET_PQ_result_spec_end - }; - - qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn, - "user_select2", - params, - rs); - } - switch (qs) - { - case GNUNET_DB_STATUS_HARD_ERROR: - return ANASTASIS_DB_ACCOUNT_STATUS_HARD_ERROR; - case GNUNET_DB_STATUS_SOFT_ERROR: - GNUNET_break (0); - return ANASTASIS_DB_ACCOUNT_STATUS_HARD_ERROR; - case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: - /* indicates: no account */ - return ANASTASIS_DB_ACCOUNT_STATUS_PAYMENT_REQUIRED; - case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: - /* indicates: no backup */ - *version = UINT32_MAX; - memset (recovery_data_hash, - 0, - sizeof (*recovery_data_hash)); - return ANASTASIS_DB_ACCOUNT_STATUS_NO_RESULTS; - default: - GNUNET_break (0); - return ANASTASIS_DB_ACCOUNT_STATUS_HARD_ERROR; - } -} - - -/* end of anastasis-db_lookup_account.c */ diff --git a/src/stasis/anastasis-db_lookup_challenge_payment.c b/src/stasis/anastasis-db_lookup_challenge_payment.c @@ -1,78 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file stasis/anastasis-db_lookup_challenge_payment.c - * @brief Anastasis database: lookup challenge payment - * @author Christian Grothoff - */ -#include "platform.h" -#include "anastasis-db_pg.h" -#include "anastasis/anastasis-database/lookup_challenge_payment.h" -#include "anastasis/anastasis-database/transaction.h" -#include "anastasis/anastasis-database/preflight.h" -#include <taler/taler_pq_lib.h> - - -/** - * Lookup pending payment for a certain challenge. - * - * @param truth_uuid identification of the challenge - * @param[out] payment_secret set to the challenge payment secret - * @return transaction status - */ -enum GNUNET_DB_QueryStatus -ANASTASIS_DB_lookup_challenge_payment ( - const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid, - struct ANASTASIS_PaymentSecretP *payment_secret) -{ - struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get (); - struct GNUNET_TIME_Timestamp recent - = GNUNET_TIME_absolute_to_timestamp ( - GNUNET_TIME_absolute_subtract (now, - ANASTASIS_CHALLENGE_OFFER_LIFETIME)); - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_auto_from_type (truth_uuid), - GNUNET_PQ_query_param_timestamp (&recent), - GNUNET_PQ_query_param_end - }; - struct GNUNET_PQ_ResultSpec rs[] = { - GNUNET_PQ_result_spec_auto_from_type ("payment_identifier", - payment_secret), - GNUNET_PQ_result_spec_end - }; - - PREPARE ("challenge_pending_payment_select", - "SELECT" - " creation_date" - ",payment_identifier" - ",amount" - " FROM anastasis_challenge_payment" - " WHERE" - " paid=FALSE" - " AND" - " refunded=FALSE" - " AND" - " truth_uuid=$1" - " AND" - " creation_date > $2;"); - return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, - "challenge_pending_payment_select", - params, - rs); -} - - -/* end of anastasis-db_lookup_challenge_payment.c */ diff --git a/src/stasis/anastasis-db_mark_challenge_code_satisfied.c b/src/stasis/anastasis-db_mark_challenge_code_satisfied.c @@ -1,66 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file stasis/anastasis-db_mark_challenge_code_satisfied.c - * @brief Anastasis database: mark challenge code satisfied - * @author Christian Grothoff - */ -#include "platform.h" -#include "anastasis-db_pg.h" -#include "anastasis/anastasis-database/mark_challenge_code_satisfied.h" -#include "anastasis/anastasis-database/transaction.h" -#include "anastasis/anastasis-database/preflight.h" -#include <taler/taler_pq_lib.h> - - -/** - * Set the 'satisfied' bit for the given challenge and code to - * 'true'. - * - * @param truth_uuid identification of the challenge which the code corresponds to - * @param code code which is now satisfied - * @return transaction status - */ -enum GNUNET_DB_QueryStatus -ANASTASIS_DB_mark_challenge_code_satisfied ( - const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid, - const uint64_t code) -{ - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_auto_from_type (truth_uuid), - GNUNET_PQ_query_param_uint64 (&code), - GNUNET_PQ_query_param_end - }; - - PREPARE ("challengecode_set_satisfied", - "UPDATE anastasis_challengecode" - " SET satisfied=TRUE" - " WHERE truth_uuid=$1" - " AND code=$2" - " AND creation_date IN" - " (SELECT creation_date" - " FROM anastasis_challengecode" - " WHERE truth_uuid=$1" - " AND code=$2" - " ORDER BY creation_date DESC" - " LIMIT 1);"); - return GNUNET_PQ_eval_prepared_non_select (pg->conn, - "challengecode_set_satisfied", - params); -} - - -/* end of anastasis-db_mark_challenge_code_satisfied.c */ diff --git a/src/stasis/anastasis-db_mark_challenge_sent.c b/src/stasis/anastasis-db_mark_challenge_sent.c @@ -1,98 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file stasis/anastasis-db_mark_challenge_sent.c - * @brief Anastasis database: mark challenge sent - * @author Christian Grothoff - */ -#include "platform.h" -#include "anastasis-db_pg.h" -#include "anastasis/anastasis-database/mark_challenge_sent.h" -#include "anastasis/anastasis-database/transaction.h" -#include "anastasis/anastasis-database/preflight.h" -#include <taler/taler_pq_lib.h> - - -/** - * Remember in the database that we successfully sent a challenge. - * - * @param payment_secret payment secret which the user must provide with every upload - * @param truth_uuid the identifier for the challenge - * @param code the challenge that was sent - */ -enum GNUNET_DB_QueryStatus -ANASTASIS_DB_mark_challenge_sent ( - const struct ANASTASIS_PaymentSecretP *payment_secret, - const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid, - uint64_t code) -{ - enum GNUNET_DB_QueryStatus qs; - - PREPARE ("challengecode_mark_sent", - "UPDATE anastasis_challengecode" - " SET retransmission_date=$3" - " WHERE truth_uuid=$1" - " AND code=$2" - " AND creation_date IN" - " (SELECT creation_date" - " FROM anastasis_challengecode" - " WHERE truth_uuid=$1" - " AND code=$2" - " ORDER BY creation_date DESC" - " LIMIT 1);"); - PREPARE ("challengepayment_dec_counter", - "UPDATE anastasis_challenge_payment" - " SET counter=counter - 1" - " WHERE truth_uuid=$1" - " AND payment_identifier=$2" - " AND counter > 0;"); - { - struct GNUNET_TIME_Timestamp now; - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_auto_from_type (truth_uuid), - GNUNET_PQ_query_param_uint64 (&code), - GNUNET_PQ_query_param_timestamp (&now), - GNUNET_PQ_query_param_end - }; - - now = GNUNET_TIME_timestamp_get (); - qs = GNUNET_PQ_eval_prepared_non_select (pg->conn, - "challengecode_mark_sent", - params); - if (qs <= 0) - return qs; - } - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "Marking challenge %llu as issued\n", - (unsigned long long) code); - { - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_auto_from_type (truth_uuid), - GNUNET_PQ_query_param_auto_from_type (payment_secret), - GNUNET_PQ_query_param_end - }; - - qs = GNUNET_PQ_eval_prepared_non_select (pg->conn, - "challengepayment_dec_counter", - params); - if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs) - return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT; /* probably was free */ - return qs; - } -} - - -/* end of anastasis-db_mark_challenge_sent.c */ diff --git a/src/stasis/anastasis-db_preflight.c b/src/stasis/anastasis-db_preflight.c @@ -1,58 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020, 2021, 2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file stasis/anastasis-db_preflight.c - * @brief preflight check for the Anastasis database - * @author Christian Grothoff - */ -#include "platform.h" -#include "anastasis-db_pg.h" -#include "anastasis/anastasis-database/preflight.h" - - -enum GNUNET_GenericReturnValue -ANASTASIS_DB_preflight (void) -{ - struct GNUNET_PQ_ExecuteStatement es[] = { - GNUNET_PQ_make_execute ("ROLLBACK"), - GNUNET_PQ_EXECUTE_STATEMENT_END - }; - - if (NULL == pg->transaction_name) - { - GNUNET_PQ_reconnect_if_down (pg->conn); - return GNUNET_OK; /* all good */ - } - if (GNUNET_OK == - GNUNET_PQ_exec_statements (pg->conn, - es)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "BUG: Preflight check rolled back transaction `%s'!\n", - pg->transaction_name); - } - else - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "BUG: Preflight check failed to rollback transaction `%s'!\n", - pg->transaction_name); - } - pg->transaction_name = NULL; - return GNUNET_NO; -} - - -/* end of anastasis-db_preflight.c */ diff --git a/src/stasis/anastasis-db_record_auth_iban_payment.c b/src/stasis/anastasis-db_record_auth_iban_payment.c @@ -1,76 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file stasis/anastasis-db_record_auth_iban_payment.c - * @brief Anastasis database: record auth iban payment - * @author Christian Grothoff - */ -#include "platform.h" -#include "anastasis-db_pg.h" -#include "anastasis/anastasis-database/record_auth_iban_payment.h" -#include "anastasis/anastasis-database/transaction.h" -#include "anastasis/anastasis-database/preflight.h" -#include <taler/taler_pq_lib.h> - - -/** - * Store inbound IBAN payment made for authentication. - * - * @param wire_reference unique identifier inside LibEuFin/Nexus - * @param wire_subject subject of the wire transfer - * @param amount how much was transferred - * @param debit_account account that was debited - * @param credit_account Anastasis operator account credited - * @param execution_date when was the transfer made - * @return transaction status - */ -enum GNUNET_DB_QueryStatus -ANASTASIS_DB_record_auth_iban_payment ( - uint64_t wire_reference, - const char *wire_subject, - const struct TALER_Amount *amount, - const char *debit_account, - const char *credit_account, - struct GNUNET_TIME_Timestamp execution_date) -{ - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_uint64 (&wire_reference), - GNUNET_PQ_query_param_string (wire_subject), - TALER_PQ_query_param_amount (pg->conn, - amount), - GNUNET_PQ_query_param_string (debit_account), - GNUNET_PQ_query_param_string (credit_account), - GNUNET_PQ_query_param_timestamp (&execution_date), - GNUNET_PQ_query_param_end - }; - - PREPARE ("store_auth_iban_payment_details", - "INSERT INTO anastasis_auth_iban_in " - "(wire_reference" - ",wire_subject" - ",credit" - ",debit_account_details" - ",credit_account_details" - ",execution_date" - ") VALUES " - "($1, $2, $3, $4, $5, $6);"); - return GNUNET_PQ_eval_prepared_non_select (pg->conn, - "store_auth_iban_payment_details", - params); -} - - -/* end of anastasis-db_record_auth_iban_payment.c */ diff --git a/src/stasis/anastasis-db_record_challenge_payment.c b/src/stasis/anastasis-db_record_challenge_payment.c @@ -1,67 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file stasis/anastasis-db_record_challenge_payment.c - * @brief Anastasis database: record challenge payment - * @author Christian Grothoff - */ -#include "platform.h" -#include "anastasis-db_pg.h" -#include "anastasis/anastasis-database/record_challenge_payment.h" -#include "anastasis/anastasis-database/transaction.h" -#include "anastasis/anastasis-database/preflight.h" -#include <taler/taler_pq_lib.h> - - -/** - * Store payment for challenge. - * - * @param truth_uuid identifier of the challenge to pay - * @param payment_secret payment secret which the user must provide with every upload - * @param amount how much we asked for - * @return transaction status - */ -enum GNUNET_DB_QueryStatus -ANASTASIS_DB_record_challenge_payment ( - const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid, - const struct ANASTASIS_PaymentSecretP *payment_secret, - const struct TALER_Amount *amount) -{ - struct GNUNET_TIME_Timestamp now = GNUNET_TIME_timestamp_get (); - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_auto_from_type (truth_uuid), - TALER_PQ_query_param_amount (pg->conn, - amount), - GNUNET_PQ_query_param_auto_from_type (payment_secret), - GNUNET_PQ_query_param_timestamp (&now), - GNUNET_PQ_query_param_end - }; - - PREPARE ("challenge_payment_insert", - "INSERT INTO anastasis_challenge_payment " - "(truth_uuid" - ",amount" - ",payment_identifier" - ",creation_date" - ") VALUES " - "($1, $2, $3, $4);"); - return GNUNET_PQ_eval_prepared_non_select (pg->conn, - "challenge_payment_insert", - params); -} - - -/* end of anastasis-db_record_challenge_payment.c */ diff --git a/src/stasis/anastasis-db_record_challenge_refund.c b/src/stasis/anastasis-db_record_challenge_refund.c @@ -1,63 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file stasis/anastasis-db_record_challenge_refund.c - * @brief Anastasis database: record challenge refund - * @author Christian Grothoff - */ -#include "platform.h" -#include "anastasis-db_pg.h" -#include "anastasis/anastasis-database/record_challenge_refund.h" -#include "anastasis/anastasis-database/transaction.h" -#include "anastasis/anastasis-database/preflight.h" -#include <taler/taler_pq_lib.h> - - -/** - * Store refund granted for challenge. - * - * @param truth_uuid identifier of the challenge to refund - * @param payment_secret payment secret which the user must provide with every upload - * @return transaction status - */ -enum GNUNET_DB_QueryStatus -ANASTASIS_DB_record_challenge_refund ( - const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid, - const struct ANASTASIS_PaymentSecretP *payment_secret) -{ - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_auto_from_type (payment_secret), - GNUNET_PQ_query_param_auto_from_type (truth_uuid), - GNUNET_PQ_query_param_end - }; - - PREPARE ("challenge_refund_update", - "UPDATE anastasis_challenge_payment " - "SET" - " refunded=TRUE " - "WHERE" - " payment_identifier=$1" - " AND" - " paid=TRUE" - " AND" - " truth_uuid=$2;"); - return GNUNET_PQ_eval_prepared_non_select (pg->conn, - "challenge_refund_update", - params); -} - - -/* end of anastasis-db_record_challenge_refund.c */ diff --git a/src/stasis/anastasis-db_record_recdoc_payment.c b/src/stasis/anastasis-db_record_recdoc_payment.c @@ -1,155 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file stasis/anastasis-db_record_recdoc_payment.c - * @brief Anastasis database: record recdoc payment - * @author Christian Grothoff - */ -#include "platform.h" -#include "anastasis-db_pg.h" -#include "anastasis/anastasis-database/record_recdoc_payment.h" -#include "anastasis/anastasis-database/transaction.h" -#include "anastasis/anastasis-database/preflight.h" -#include <taler/taler_pq_lib.h> - - -/** - * Store payment. Used to begin a payment, not indicative - * that the payment actually was made. (That is done - * when we increment the account's lifetime.) - * - * @param account_pub anastasis's public key - * @param post_counter how many uploads does @a amount pay for - * @param payment_secret payment secret which the user must provide with every upload - * @param amount how much we asked for - * @return transaction status - */ -enum GNUNET_DB_QueryStatus -ANASTASIS_DB_record_recdoc_payment ( - const struct ANASTASIS_CRYPTO_AccountPublicKeyP *account_pub, - uint32_t post_counter, - const struct ANASTASIS_PaymentSecretP *payment_secret, - const struct TALER_Amount *amount) -{ - struct GNUNET_TIME_Timestamp now = GNUNET_TIME_timestamp_get (); - struct GNUNET_TIME_Timestamp expiration; - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_auto_from_type (account_pub), - GNUNET_PQ_query_param_uint32 (&post_counter), - TALER_PQ_query_param_amount (pg->conn, - amount), - GNUNET_PQ_query_param_auto_from_type (payment_secret), - GNUNET_PQ_query_param_timestamp (&now), - GNUNET_PQ_query_param_end - }; - enum GNUNET_DB_QueryStatus qs; - - GNUNET_break (GNUNET_OK == - ANASTASIS_DB_preflight ()); - PREPARE ("user_select3", - "SELECT" - " expiration_date " - "FROM anastasis_user" - " WHERE user_id=$1" - " FOR UPDATE;"); - PREPARE ("user_insert3", - "INSERT INTO anastasis_user " - "(user_id" - ",expiration_date" - ") VALUES " - "($1, $2);"); - PREPARE ("recdoc_payment_insert", - "INSERT INTO anastasis_recdoc_payment " - "(user_id" - ",post_counter" - ",amount" - ",payment_identifier" - ",creation_date" - ") VALUES " - "($1, $2, $3, $4, $5);"); - - /* because of constraint at user_id, first we have to verify - if user exists, and if not, create one */ - { - struct GNUNET_PQ_QueryParam iparams[] = { - GNUNET_PQ_query_param_auto_from_type (account_pub), - GNUNET_PQ_query_param_end - }; - struct GNUNET_PQ_ResultSpec rs[] = { - GNUNET_PQ_result_spec_timestamp ("expiration_date", - &expiration), - GNUNET_PQ_result_spec_end - }; - - qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn, - "user_select3", - iparams, - rs); - } - switch (qs) - { - case GNUNET_DB_STATUS_HARD_ERROR: - return qs; - case GNUNET_DB_STATUS_SOFT_ERROR: - GNUNET_break (0); - return GNUNET_DB_STATUS_HARD_ERROR; - case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: - { - /* create new user with short lifetime */ - struct GNUNET_TIME_Timestamp exp - = GNUNET_TIME_relative_to_timestamp (TRANSIENT_LIFETIME); - struct GNUNET_PQ_QueryParam iparams[] = { - GNUNET_PQ_query_param_auto_from_type (account_pub), - GNUNET_PQ_query_param_timestamp (&exp), - GNUNET_PQ_query_param_end - }; - - qs = GNUNET_PQ_eval_prepared_non_select (pg->conn, - "user_insert3", - iparams); - switch (qs) - { - case GNUNET_DB_STATUS_HARD_ERROR: - return GNUNET_DB_STATUS_HARD_ERROR; - case GNUNET_DB_STATUS_SOFT_ERROR: - GNUNET_break (0); - return GNUNET_DB_STATUS_HARD_ERROR; - case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: - GNUNET_break (0); - return GNUNET_DB_STATUS_HARD_ERROR; - case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: - /* successful, continue below */ - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "Created new account %s with transient life until %s\n", - TALER_B2S (account_pub), - GNUNET_TIME_timestamp2s (exp)); - break; - } - } - /* continue below */ - break; - case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: - /* handle case below */ - break; - } - - return GNUNET_PQ_eval_prepared_non_select (pg->conn, - "recdoc_payment_insert", - params); -} - - -/* end of anastasis-db_record_recdoc_payment.c */ diff --git a/src/stasis/anastasis-db_record_truth_upload_payment.c b/src/stasis/anastasis-db_record_truth_upload_payment.c @@ -1,66 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file stasis/anastasis-db_record_truth_upload_payment.c - * @brief Anastasis database: record truth upload payment - * @author Christian Grothoff - */ -#include "platform.h" -#include "anastasis-db_pg.h" -#include "anastasis/anastasis-database/record_truth_upload_payment.h" -#include "anastasis/anastasis-database/transaction.h" -#include "anastasis/anastasis-database/preflight.h" -#include <taler/taler_pq_lib.h> - - -/** - * Record truth upload payment was made. - * - * @param uuid the truth's UUID - * @param amount the amount that was paid - * @param duration how long is the truth paid for - * @return transaction status - */ -enum GNUNET_DB_QueryStatus -ANASTASIS_DB_record_truth_upload_payment ( - const struct ANASTASIS_CRYPTO_TruthUUIDP *uuid, - const struct TALER_Amount *amount, - struct GNUNET_TIME_Relative duration) -{ - struct GNUNET_TIME_Timestamp exp = GNUNET_TIME_relative_to_timestamp ( - duration); - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_auto_from_type (uuid), - TALER_PQ_query_param_amount (pg->conn, - amount), - GNUNET_PQ_query_param_timestamp (&exp), - GNUNET_PQ_query_param_end - }; - - PREPARE ("truth_payment_insert", - "INSERT INTO anastasis_truth_payment " - "(truth_uuid" - ",amount" - ",expiration" - ") VALUES " - "($1, $2, $3);"); - return GNUNET_PQ_eval_prepared_non_select (pg->conn, - "truth_payment_insert", - params); -} - - -/* end of anastasis-db_record_truth_upload_payment.c */ diff --git a/src/stasis/anastasis-db_store_recovery_document.c b/src/stasis/anastasis-db_store_recovery_document.c @@ -1,312 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file stasis/anastasis-db_store_recovery_document.c - * @brief Anastasis database: store recovery document - * @author Christian Grothoff - */ -#include "platform.h" -#include "anastasis-db_pg.h" -#include "anastasis/anastasis-database/store_recovery_document.h" -#include "anastasis/anastasis-database/transaction.h" -#include "anastasis/anastasis-database/preflight.h" -#include <taler/taler_pq_lib.h> - - -/** - * Store encrypted recovery document. - * - * @param account_pub public key of the user's account - * @param account_sig signature affirming storage request - * @param recovery_data_hash hash of @a data - * @param recovery_data contains encrypted_recovery_document - * @param recovery_data_size size of data blob - * @param payment_secret identifier for the payment, used to later charge on uploads - * @param[out] version set to the version assigned to the document by the database - * @return transaction status, 0 if upload could not be finished because @a payment_secret - * did not have enough upload left; HARD error if @a payment_secret is unknown, ... - */ -enum ANASTASIS_DB_StoreStatus -ANASTASIS_DB_store_recovery_document ( - const struct ANASTASIS_CRYPTO_AccountPublicKeyP *account_pub, - const struct ANASTASIS_AccountSignatureP *account_sig, - const struct GNUNET_HashCode *recovery_data_hash, - const void *recovery_data, - size_t recovery_data_size, - const void *recovery_meta_data, - size_t recovery_meta_data_size, - const struct ANASTASIS_PaymentSecretP *payment_secret, - uint32_t *version) -{ - enum GNUNET_DB_QueryStatus qs; - - GNUNET_break (GNUNET_OK == - ANASTASIS_DB_preflight ()); - - PREPARE ("user_select5", - "SELECT" - " expiration_date " - "FROM anastasis_user" - " WHERE user_id=$1" - " FOR UPDATE;"); - PREPARE ("latest_recovery_version_select2", - "SELECT" - " version" - ",recovery_data_hash" - ",expiration_date" - " FROM anastasis_recoverydocument" - " JOIN anastasis_user USING (user_id)" - " WHERE user_id=$1" - " ORDER BY version DESC" - " LIMIT 1;"); - PREPARE ("postcounter_select", - "SELECT" - " post_counter" - " FROM anastasis_recdoc_payment" - " WHERE user_id=$1" - " AND payment_identifier=$2;"); - PREPARE ("postcounter_update", - "UPDATE" - " anastasis_recdoc_payment" - " SET" - " post_counter=$1" - " WHERE user_id =$2" - " AND payment_identifier=$3;"); - PREPARE ("recovery_document_insert", - "INSERT INTO anastasis_recoverydocument " - "(user_id" - ",version" - ",account_sig" - ",recovery_data_hash" - ",recovery_data" - ",recovery_meta_data" - ",creation_date" - ") VALUES " - "($1, $2, $3, $4, $5, $6, $7);"); - for (unsigned int retry = 0; retry<MAX_RETRIES; retry++) - { - if (GNUNET_OK != - ANASTASIS_DB_start ( - "store_recovery_document")) - { - GNUNET_break (0); - return ANASTASIS_DB_STORE_STATUS_HARD_ERROR; - } - /* get the current version and hash of the latest recovery document - for this account */ - { - struct GNUNET_HashCode dh; - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_auto_from_type (account_pub), - GNUNET_PQ_query_param_end - }; - struct GNUNET_PQ_ResultSpec rs[] = { - GNUNET_PQ_result_spec_uint32 ("version", - version), - GNUNET_PQ_result_spec_auto_from_type ("recovery_data_hash", - &dh), - GNUNET_PQ_result_spec_end - }; - - qs = GNUNET_PQ_eval_prepared_singleton_select ( - pg->conn, - "latest_recovery_version_select2", - params, - rs); - switch (qs) - { - case GNUNET_DB_STATUS_HARD_ERROR: - GNUNET_break (0); - ANASTASIS_DB_rollback (); - return ANASTASIS_DB_STORE_STATUS_HARD_ERROR; - case GNUNET_DB_STATUS_SOFT_ERROR: - goto retry; - case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: - *version = 1; - break; - case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: - /* had an existing recovery_data, is it identical? */ - if (0 == GNUNET_memcmp (&dh, - recovery_data_hash)) - { - /* Yes. Previous identical recovery data exists */ - ANASTASIS_DB_rollback (); - return ANASTASIS_DB_STORE_STATUS_NO_RESULTS; - } - (*version)++; - break; - default: - GNUNET_break (0); - ANASTASIS_DB_rollback (); - return ANASTASIS_DB_STORE_STATUS_HARD_ERROR; - } - } - - /* First, check if account exists */ - { - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_auto_from_type (account_pub), - GNUNET_PQ_query_param_end - }; - struct GNUNET_PQ_ResultSpec rs[] = { - GNUNET_PQ_result_spec_end - }; - - qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn, - "user_select5", - params, - rs); - } - switch (qs) - { - case GNUNET_DB_STATUS_HARD_ERROR: - ANASTASIS_DB_rollback (); - return ANASTASIS_DB_STORE_STATUS_HARD_ERROR; - case GNUNET_DB_STATUS_SOFT_ERROR: - goto retry; - case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: - ANASTASIS_DB_rollback (); - return ANASTASIS_DB_STORE_STATUS_PAYMENT_REQUIRED; - case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: - /* handle interesting case below */ - break; - } - - { - uint32_t postcounter; - - /* lookup if the user has enough uploads left and decrement */ - { - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_auto_from_type (account_pub), - GNUNET_PQ_query_param_auto_from_type (payment_secret), - GNUNET_PQ_query_param_end - }; - struct GNUNET_PQ_ResultSpec rs[] = { - GNUNET_PQ_result_spec_uint32 ("post_counter", - &postcounter), - GNUNET_PQ_result_spec_end - }; - - qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn, - "postcounter_select", - params, - rs); - switch (qs) - { - case GNUNET_DB_STATUS_HARD_ERROR: - ANASTASIS_DB_rollback (); - return ANASTASIS_DB_STORE_STATUS_HARD_ERROR; - case GNUNET_DB_STATUS_SOFT_ERROR: - goto retry; - case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: - ANASTASIS_DB_rollback (); - return ANASTASIS_DB_STORE_STATUS_HARD_ERROR; - case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: - break; - } - } - - if (0 == postcounter) - { - ANASTASIS_DB_rollback (); - return ANASTASIS_DB_STORE_STATUS_STORE_LIMIT_EXCEEDED; - } - /* Decrement the postcounter by one */ - postcounter--; - - /* Update the postcounter in the Database */ - { - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_uint32 (&postcounter), - GNUNET_PQ_query_param_auto_from_type (account_pub), - GNUNET_PQ_query_param_auto_from_type (payment_secret), - GNUNET_PQ_query_param_end - }; - - qs = GNUNET_PQ_eval_prepared_non_select (pg->conn, - "postcounter_update", - params); - switch (qs) - { - case GNUNET_DB_STATUS_HARD_ERROR: - GNUNET_break (0); - ANASTASIS_DB_rollback (); - return ANASTASIS_DB_STORE_STATUS_HARD_ERROR; - case GNUNET_DB_STATUS_SOFT_ERROR: - goto retry; - case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: - GNUNET_break (0); - ANASTASIS_DB_rollback (); - return ANASTASIS_DB_STORE_STATUS_HARD_ERROR; - case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: - break; - default: - GNUNET_break (0); - ANASTASIS_DB_rollback (); - return ANASTASIS_DB_STORE_STATUS_HARD_ERROR; - } - } - } - - /* finally, actually insert the recovery document */ - { - struct GNUNET_TIME_Timestamp now - = GNUNET_TIME_timestamp_get (); - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_auto_from_type (account_pub), - GNUNET_PQ_query_param_uint32 (version), - GNUNET_PQ_query_param_auto_from_type (account_sig), - GNUNET_PQ_query_param_auto_from_type (recovery_data_hash), - GNUNET_PQ_query_param_fixed_size (recovery_data, - recovery_data_size), - GNUNET_PQ_query_param_fixed_size (recovery_meta_data, - recovery_meta_data_size), - GNUNET_PQ_query_param_timestamp (&now), - GNUNET_PQ_query_param_end - }; - - qs = GNUNET_PQ_eval_prepared_non_select (pg->conn, - "recovery_document_insert", - params); - switch (qs) - { - case GNUNET_DB_STATUS_HARD_ERROR: - ANASTASIS_DB_rollback (); - return ANASTASIS_DB_STORE_STATUS_HARD_ERROR; - case GNUNET_DB_STATUS_SOFT_ERROR: - goto retry; - case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: - GNUNET_break (0); - ANASTASIS_DB_rollback (); - return ANASTASIS_DB_STORE_STATUS_HARD_ERROR; - case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: - qs = ANASTASIS_DB_commit (); - if (GNUNET_DB_STATUS_SOFT_ERROR == qs) - goto retry; - if (qs < 0) - return ANASTASIS_DB_STORE_STATUS_HARD_ERROR; - return ANASTASIS_DB_STORE_STATUS_SUCCESS; - } - } -retry: - ANASTASIS_DB_rollback (); - } - return ANASTASIS_DB_STORE_STATUS_SOFT_ERROR; -} - - -/* end of anastasis-db_store_recovery_document.c */ diff --git a/src/stasis/anastasis-db_store_truth.c b/src/stasis/anastasis-db_store_truth.c @@ -1,80 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file stasis/anastasis-db_store_truth.c - * @brief Anastasis database: store truth - * @author Christian Grothoff - */ -#include "platform.h" -#include "anastasis-db_pg.h" -#include "anastasis/anastasis-database/store_truth.h" -#include "anastasis/anastasis-database/transaction.h" -#include "anastasis/anastasis-database/preflight.h" -#include <taler/taler_pq_lib.h> - - -/** - * Upload Truth, which contains the Truth and the KeyShare. - * - * @param truth_uuid the identifier for the Truth - * @param key_share_data contains information of an EncryptedKeyShare - * @param mime_type presumed mime type of data in @a encrypted_truth - * @param encrypted_truth contains the encrypted Truth which includes the ground truth i.e. H(challenge answer), phonenumber, SMS - * @param encrypted_truth_size the size of the Truth - * @param method name of method - * @param truth_expiration time till the according data will be stored - * @return transaction status - */ -enum GNUNET_DB_QueryStatus -ANASTASIS_DB_store_truth ( - const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid, - const struct ANASTASIS_CRYPTO_EncryptedKeyShareP *key_share_data, - const char *mime_type, - const void *encrypted_truth, - size_t encrypted_truth_size, - const char *method, - struct GNUNET_TIME_Relative truth_expiration) -{ - struct GNUNET_TIME_Timestamp expiration; - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_auto_from_type (truth_uuid), - GNUNET_PQ_query_param_auto_from_type (key_share_data), - GNUNET_PQ_query_param_string (method), - GNUNET_PQ_query_param_fixed_size (encrypted_truth, - encrypted_truth_size), - GNUNET_PQ_query_param_string (mime_type), - GNUNET_PQ_query_param_timestamp (&expiration), - GNUNET_PQ_query_param_end - }; - - expiration = GNUNET_TIME_relative_to_timestamp (truth_expiration); - PREPARE ("truth_insert", - "INSERT INTO anastasis_truth " - "(truth_uuid" - ",key_share_data" - ",method_name" - ",encrypted_truth" - ",truth_mime" - ",expiration" - ") VALUES " - "($1, $2, $3, $4, $5, $6);"); - return GNUNET_PQ_eval_prepared_non_select (pg->conn, - "truth_insert", - params); -} - - -/* end of anastasis-db_store_truth.c */ diff --git a/src/stasis/anastasis-db_test_auth_iban_payment.c b/src/stasis/anastasis-db_test_auth_iban_payment.c @@ -1,156 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file stasis/anastasis-db_test_auth_iban_payment.c - * @brief Anastasis database: test auth iban payment - * @author Christian Grothoff - */ -#include "platform.h" -#include "anastasis-db_pg.h" -#include "anastasis/anastasis-database/test_auth_iban_payment.h" -#include "anastasis/anastasis-database/transaction.h" -#include "anastasis/anastasis-database/preflight.h" -#include <taler/taler_pq_lib.h> - - -struct TestIbanContext -{ - - /** - * Function to call on each wire transfer found. - */ - ANASTASIS_DB_AuthIbanTransfercheck cb; - - /** - * Closure for @a cb. - */ - void *cb_cls; - - /** - * Value to return. - */ - enum GNUNET_DB_QueryStatus qs; -}; - - -/** - * Helper function for #postgres_test_auth_iban_payment(). - * To be called with the results of a SELECT statement - * that has returned @a num_results results. - * - * @param cls closure of type `struct TestIbanContext *` - * @param result the postgres result - * @param num_results the number of results in @a result - */ -static void -test_auth_cb (void *cls, - PGresult *result, - unsigned int num_results) -{ - struct TestIbanContext *tic = cls; - - for (unsigned int i = 0; i<num_results; i++) - { - struct TALER_Amount credit; - char *wire_subject; - struct GNUNET_PQ_ResultSpec rs[] = { - TALER_PQ_result_spec_amount ("credit", - pg->currency, - &credit), - GNUNET_PQ_result_spec_string ("wire_subject", - &wire_subject), - GNUNET_PQ_result_spec_end - }; - - if (GNUNET_OK != - GNUNET_PQ_extract_result (result, - rs, - i)) - { - GNUNET_break (0); - tic->qs = GNUNET_DB_STATUS_HARD_ERROR; - return; - } - if (tic->cb (tic->cb_cls, - &credit, - wire_subject)) - { - GNUNET_free (wire_subject); - tic->qs = GNUNET_DB_STATUS_SUCCESS_ONE_RESULT; - return; - } - GNUNET_free (wire_subject); - } -} - - -/** - * Function to check if we are aware of a wire transfer - * that satisfies the IBAN plugin's authentication check. - * - * @param cls closure - * @param debit_account which debit account to check - * @param earliest_date earliest date to check - * @param cb function to call on all entries found - * @param cb_cls closure for @a cb - * @return transaction status, - * #GNUNET_DB_STATUS_SUCCESS_ONE_RESULT if @a cb - * returned 'true' once - * #GNUNET_DB_STATUS_SUCCESS_NO_RESULTS if no - * wire transfers existed for which @a cb returned true - */ - - -/** - * Closure for #test_auth_cb. - */ -enum GNUNET_DB_QueryStatus -ANASTASIS_DB_test_auth_iban_payment ( - const char *debit_account, - struct GNUNET_TIME_Timestamp earliest_date, - ANASTASIS_DB_AuthIbanTransfercheck cb, - void *cb_cls) -{ - struct TestIbanContext tic = { - .cb = cb, - .cb_cls = cb_cls - }; - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_string (debit_account), - GNUNET_PQ_query_param_timestamp (&earliest_date), - GNUNET_PQ_query_param_end - }; - enum GNUNET_DB_QueryStatus qs; - - PREPARE ("test_auth_iban_payment", - "SELECT" - " credit" - ",wire_subject" - " FROM anastasis_auth_iban_in" - " WHERE debit_account_details=$1" - " AND execution_date>=$2;"); - qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn, - "test_auth_iban_payment", - params, - &test_auth_cb, - &tic); - if (qs < 0) - return qs; - return tic.qs; -} - - -/* end of anastasis-db_test_auth_iban_payment.c */ diff --git a/src/stasis/anastasis-db_test_challenge_code_satisfied.c b/src/stasis/anastasis-db_test_challenge_code_satisfied.c @@ -1,69 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file stasis/anastasis-db_test_challenge_code_satisfied.c - * @brief Anastasis database: test challenge code satisfied - * @author Christian Grothoff - */ -#include "platform.h" -#include "anastasis-db_pg.h" -#include "anastasis/anastasis-database/test_challenge_code_satisfied.h" -#include "anastasis/anastasis-database/transaction.h" -#include "anastasis/anastasis-database/preflight.h" -#include <taler/taler_pq_lib.h> - - -/** - * Check if the 'satisfied' bit for the given challenge and code is - * 'true' and the challenge code is not yet expired. - * - * @param truth_uuid identification of the challenge which the code corresponds to - * @param code code which is now satisfied - * @return transaction status, - * #GNUNET_DB_STATUS_SUCCESS_NO_RESULTS if the challenge code is not satisfied or expired - * #GNUNET_DB_STATUS_SUCCESS_ONE_RESULT if the challenge code has been marked as satisfied - */ -enum GNUNET_DB_QueryStatus -ANASTASIS_DB_test_challenge_code_satisfied ( - const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid, - const uint64_t code, - struct GNUNET_TIME_Timestamp after) -{ - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_auto_from_type (truth_uuid), - GNUNET_PQ_query_param_uint64 (&code), - GNUNET_PQ_query_param_timestamp (&after), - GNUNET_PQ_query_param_end - }; - struct GNUNET_PQ_ResultSpec rs[] = { - GNUNET_PQ_result_spec_end - }; - - PREPARE ("challengecode_test_satisfied", - "SELECT 1 FROM anastasis_challengecode" - " WHERE truth_uuid=$1" - " AND satisfied=TRUE" - " AND code=$2" - " AND creation_date >= $3" - " LIMIT 1;"); - return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, - "challengecode_test_satisfied", - params, - rs); -} - - -/* end of anastasis-db_test_challenge_code_satisfied.c */ diff --git a/src/stasis/anastasis-db_update_challenge_payment.c b/src/stasis/anastasis-db_update_challenge_payment.c @@ -1,65 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file stasis/anastasis-db_update_challenge_payment.c - * @brief Anastasis database: update challenge payment - * @author Christian Grothoff - */ -#include "platform.h" -#include "anastasis-db_pg.h" -#include "anastasis/anastasis-database/update_challenge_payment.h" -#include "anastasis/anastasis-database/transaction.h" -#include "anastasis/anastasis-database/preflight.h" -#include <taler/taler_pq_lib.h> - - -/** - * Update payment status of challenge - * - * @param truth_uuid which challenge received a payment - * @param payment_identifier proof of payment, must be unique and match pending payment - * @return transaction status - */ -enum GNUNET_DB_QueryStatus -ANASTASIS_DB_update_challenge_payment ( - const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid, - const struct ANASTASIS_PaymentSecretP *payment_identifier) -{ - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_auto_from_type (payment_identifier), - GNUNET_PQ_query_param_auto_from_type (truth_uuid), - GNUNET_PQ_query_param_end - }; - - PREPARE ("challenge_payment_done", - "UPDATE anastasis_challenge_payment " - "SET" - " paid=TRUE " - "WHERE" - " payment_identifier=$1" - " AND" - " refunded=FALSE" - " AND" - " truth_uuid=$2" - " AND" - " paid=FALSE;"); - return GNUNET_PQ_eval_prepared_non_select (pg->conn, - "challenge_payment_done", - params); -} - - -/* end of anastasis-db_update_challenge_payment.c */ diff --git a/src/stasis/anastasis-db_update_lifetime.c b/src/stasis/anastasis-db_update_lifetime.c @@ -1,201 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file stasis/anastasis-db_update_lifetime.c - * @brief Anastasis database: update lifetime - * @author Christian Grothoff - */ -#include "platform.h" -#include "anastasis-db_pg.h" -#include "anastasis/anastasis-database/update_lifetime.h" -#include "anastasis/anastasis-database/transaction.h" -#include "anastasis/anastasis-database/preflight.h" -#include <taler/taler_pq_lib.h> - - -/** - * Update account lifetime to the maximum of the current - * value and @a eol. - * - * @param account_pub which account received a payment - * @param payment_identifier proof of payment, must be unique and match pending payment - * @param eol for how long is the account now paid (absolute) - * @return transaction status - */ -// FIXME: what is the significant delta to increment_lifetime? -// Should we combine these? -enum GNUNET_DB_QueryStatus -ANASTASIS_DB_update_lifetime ( - const struct ANASTASIS_CRYPTO_AccountPublicKeyP *account_pub, - const struct ANASTASIS_PaymentSecretP *payment_identifier, - struct GNUNET_TIME_Timestamp eol) -{ - enum GNUNET_DB_QueryStatus qs; - - PREPARE ("user_select4", - "SELECT" - " expiration_date " - "FROM anastasis_user" - " WHERE user_id=$1" - " FOR UPDATE;"); - PREPARE ("user_insert4", - "INSERT INTO anastasis_user " - "(user_id" - ",expiration_date" - ") VALUES " - "($1, $2);"); - PREPARE ("user_update4", - "UPDATE anastasis_user" - " SET " - " expiration_date=$1" - " WHERE user_id=$2;"); - PREPARE ("recdoc_payment_done2", - "UPDATE anastasis_recdoc_payment " - "SET" - " paid=TRUE " - "WHERE" - " payment_identifier=$1" - " AND" - " user_id=$2" - " AND" - " paid=FALSE;"); - for (unsigned int retries = 0; retries<MAX_RETRIES; retries++) - { - if (GNUNET_OK != - ANASTASIS_DB_start ( - "update lifetime")) - { - GNUNET_break (0); - return GNUNET_DB_STATUS_HARD_ERROR; - } - - { - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_auto_from_type (payment_identifier), - GNUNET_PQ_query_param_auto_from_type (account_pub), - GNUNET_PQ_query_param_end - }; - qs = GNUNET_PQ_eval_prepared_non_select (pg->conn, - "recdoc_payment_done2", - params); - if (GNUNET_DB_STATUS_SOFT_ERROR == qs) - goto retry; - if (0 >= qs) - { - /* same payment made before, or unknown, or error - => no further action! */ - ANASTASIS_DB_rollback (); - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "Payment existed, lifetime of account %s unchanged\n", - TALER_B2S (account_pub)); - return qs; - } - } - - { - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_auto_from_type (account_pub), - GNUNET_PQ_query_param_end - }; - struct GNUNET_TIME_Timestamp expiration; - struct GNUNET_PQ_ResultSpec rs[] = { - GNUNET_PQ_result_spec_timestamp ("expiration_date", - &expiration), - GNUNET_PQ_result_spec_end - }; - - qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn, - "user_select4", - params, - rs); - switch (qs) - { - case GNUNET_DB_STATUS_HARD_ERROR: - ANASTASIS_DB_rollback (); - return qs; - case GNUNET_DB_STATUS_SOFT_ERROR: - goto retry; - case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: - { - /* user does not exist, create new one */ - struct GNUNET_PQ_QueryParam iparams[] = { - GNUNET_PQ_query_param_auto_from_type (account_pub), - GNUNET_PQ_query_param_timestamp (&eol), - GNUNET_PQ_query_param_end - }; - - GNUNET_break (! GNUNET_TIME_absolute_is_never (eol.abs_time)); - qs = GNUNET_PQ_eval_prepared_non_select (pg->conn, - "user_insert4", - iparams); - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "Created new account %s with expiration %s\n", - TALER_B2S (account_pub), - GNUNET_TIME_timestamp2s (eol)); - } - break; - case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: - { - /* user exists, update expiration_date */ - struct GNUNET_PQ_QueryParam iparams[] = { - GNUNET_PQ_query_param_timestamp (&expiration), - GNUNET_PQ_query_param_auto_from_type (account_pub), - GNUNET_PQ_query_param_end - }; - - expiration = GNUNET_TIME_timestamp_max (expiration, - eol); - GNUNET_break (! GNUNET_TIME_absolute_is_never (expiration.abs_time)); - qs = GNUNET_PQ_eval_prepared_non_select (pg->conn, - "user_update4", - iparams); - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "Updated account %s to new expiration %s\n", - TALER_B2S (account_pub), - GNUNET_TIME_timestamp2s (expiration)); - } - break; - } - } - - switch (qs) - { - case GNUNET_DB_STATUS_HARD_ERROR: - ANASTASIS_DB_rollback (); - return qs; - case GNUNET_DB_STATUS_SOFT_ERROR: - goto retry; - case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: - GNUNET_break (0); - ANASTASIS_DB_rollback (); - return GNUNET_DB_STATUS_HARD_ERROR; - case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: - break; - } - qs = ANASTASIS_DB_commit (); - if (GNUNET_DB_STATUS_SOFT_ERROR == qs) - goto retry; - if (qs < 0) - return GNUNET_DB_STATUS_HARD_ERROR; - return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT; -retry: - ANASTASIS_DB_rollback (); - } - return GNUNET_DB_STATUS_SOFT_ERROR; -} - - -/* end of anastasis-db_update_lifetime.c */ diff --git a/src/stasis/anastasis-db_verify_challenge_code.c b/src/stasis/anastasis-db_verify_challenge_code.c @@ -1,211 +0,0 @@ -/* - This file is part of Anastasis - Copyright (C) 2020-2022 Anastasis SARL - - Anastasis is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file stasis/anastasis-db_verify_challenge_code.c - * @brief Anastasis database: verify challenge code - * @author Christian Grothoff - */ -#include "platform.h" -#include "anastasis-db_pg.h" -#include "anastasis/anastasis-database/verify_challenge_code.h" -#include "anastasis/anastasis-database/transaction.h" -#include "anastasis/anastasis-database/preflight.h" -#include <taler/taler_pq_lib.h> - - -struct CheckValidityContext -{ - /** - * Code to check for. - */ - const struct GNUNET_HashCode *hashed_code; - - /** - * Truth we are processing. - */ - const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid; - - /** - * Set to the matching challenge code (if @e valid). - */ - uint64_t code; - - /** - * Set to true if a code matching @e hashed_code was found. - */ - bool valid; - - /** - * Set to true if a code matching @e hashed_code was set to 'satisfied' by the plugin. - */ - bool satisfied; - - /** - * Set to true if we had a database failure. - */ - bool db_failure; - -}; - - -/** - * Helper function for #postgres_verify_challenge_code(). - * To be called with the results of a SELECT statement - * that has returned @a num_results results. - * - * @param cls closure of type `struct CheckValidityContext *` - * @param result the postgres result - * @param num_results the number of results in @a result - */ -static void -check_valid_code (void *cls, - PGresult *result, - unsigned int num_results) -{ - struct CheckValidityContext *cvc = cls; - - for (unsigned int i = 0; i < num_results; i++) - { - uint64_t server_code; - uint8_t sat; - struct GNUNET_PQ_ResultSpec rs[] = { - GNUNET_PQ_result_spec_uint64 ("code", - &server_code), - GNUNET_PQ_result_spec_auto_from_type ("satisfied", - &sat), - GNUNET_PQ_result_spec_end - }; - - if (GNUNET_OK != - GNUNET_PQ_extract_result (result, - rs, - i)) - { - GNUNET_break (0); - cvc->db_failure = true; - return; - } - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "Found issued challenge %llu (client: %s)\n", - (unsigned long long) server_code, - GNUNET_h2s (cvc->hashed_code)); - { - struct GNUNET_HashCode shashed_code; - - ANASTASIS_hash_answer (server_code, - &shashed_code); - if (0 == - GNUNET_memcmp (&shashed_code, - cvc->hashed_code)) - { - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "Challenge is valid challenge (%s)\n", - (0 != sat) ? "satisfied" : "not satisfied"); - cvc->valid = true; - cvc->code = server_code; - cvc->satisfied = (0 != sat); - } - else - { - /* count failures to prevent brute-force attacks */ - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_auto_from_type (cvc->truth_uuid), - GNUNET_PQ_query_param_uint64 (&server_code), - GNUNET_PQ_query_param_end - }; - enum GNUNET_DB_QueryStatus qs; - - qs = GNUNET_PQ_eval_prepared_non_select (pg->conn, - "challengecode_update_retry", - params); - if (qs <= 0) - { - GNUNET_break (0); - cvc->db_failure = true; - } - } - } - } -} - - -/** - * Verify the provided code with the code on the server. - * If the code matches the function will return with success, if the code - * does not match, the retry counter will be decreased by one. - * - * @param cls closure - * @param truth_uuid identification of the challenge which the code corresponds to - * @param hashed_code code which the user provided and wants to verify - * @param[out] code set to the original numeric code - * @param[out] satisfied set to true if the challenge is set to satisfied - * @return code validity status - */ -enum ANASTASIS_DB_CodeStatus -ANASTASIS_DB_verify_challenge_code ( - const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid, - const struct GNUNET_HashCode *hashed_code, - uint64_t *code, - bool *satisfied) -{ - struct CheckValidityContext cvc = { - .truth_uuid = truth_uuid, - .hashed_code = hashed_code, - }; - struct GNUNET_TIME_Timestamp now = GNUNET_TIME_timestamp_get (); - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_auto_from_type (truth_uuid), - GNUNET_PQ_query_param_timestamp (&now), - GNUNET_PQ_query_param_end - }; - enum GNUNET_DB_QueryStatus qs; - - *satisfied = false; - PREPARE ("challengecode_update_retry", - "UPDATE anastasis_challengecode" - " SET retry_counter=retry_counter - 1" - " WHERE truth_uuid=$1" - " AND code=$2" - " AND retry_counter != 0;"); - PREPARE ("challengecode_select", - "SELECT " - " code" - ",satisfied" - " FROM anastasis_challengecode" - " WHERE truth_uuid=$1" - " AND expiration_date > $2" - " AND retry_counter != 0;"); - qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn, - "challengecode_select", - params, - &check_valid_code, - &cvc); - if ( (qs < 0) || - (cvc.db_failure) ) - return ANASTASIS_DB_CODE_STATUS_HARD_ERROR; - *code = cvc.code; - if (cvc.valid) - { - *satisfied = cvc.satisfied; - return ANASTASIS_DB_CODE_STATUS_VALID_CODE_STORED; - } - if (0 == qs) - return ANASTASIS_DB_CODE_STATUS_NO_RESULTS; - return ANASTASIS_DB_CODE_STATUS_CHALLENGE_CODE_MISMATCH; -} - - -/* end of anastasis-db_verify_challenge_code.c */ diff --git a/src/stasis/commit.sql b/src/stasis/commit.sql @@ -0,0 +1,17 @@ +-- +-- This file is part of Anastasis +-- Copyright (C) 2026 Anastasis SARL +-- +-- ANASTASIS is free software; you can redistribute it and/or modify it under the +-- terms of the GNU General Public License as published by the Free Software +-- Foundation; either version 3, or (at your option) any later version. +-- +-- ANASTASIS is distributed in the hope that it will be useful, but WITHOUT ANY +-- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +-- A PARTICULAR PURPOSE. See the GNU General Public License for more details. +-- +-- You should have received a copy of the GNU General Public License along with +-- ANASTASIS; see the file COPYING. If not, see <http://www.gnu.org/licenses/> +-- + +COMMIT; diff --git a/src/stasis/create_tables.c b/src/stasis/create_tables.c @@ -0,0 +1,53 @@ +/* + This file is part of Anastasis + Copyright (C) 2020, 2021, 2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file stasis/create_tables.c + * @brief create the Anastasis database tables + * @author Christian Grothoff + */ +#include "platform.h" +#include "anastasis-db_pg.h" +#include "anastasis/anastasis-database/create_tables.h" + + +enum GNUNET_GenericReturnValue +ANASTASIS_DB_create_tables (void) +{ + if (GNUNET_SYSERR == + GNUNET_PQ_load_versioning (pg->conn)) + { + GNUNET_break (0); + return GNUNET_SYSERR; + } + if (GNUNET_OK != + GNUNET_PQ_run_sql (pg->conn, + "stasis-")) + { + GNUNET_break (0); + return GNUNET_SYSERR; + } + if (GNUNET_OK != + GNUNET_PQ_exec_sql (pg->conn, + "procedures")) + { + GNUNET_break (0); + return GNUNET_SYSERR; + } + return GNUNET_OK; +} + + +/* end of create_tables.c */ diff --git a/src/stasis/do_insert_recovery_document.c b/src/stasis/do_insert_recovery_document.c @@ -0,0 +1,312 @@ +/* + This file is part of Anastasis + Copyright (C) 2020-2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file stasis/do_insert_recovery_document.c + * @brief Anastasis database: do insert recovery document + * @author Christian Grothoff + */ +#include "platform.h" +#include "anastasis-db_pg.h" +#include "anastasis/anastasis-database/do_insert_recovery_document.h" +#include "anastasis/anastasis-database/transaction.h" +#include "anastasis/anastasis-database/preflight.h" +#include <taler/taler_pq_lib.h> + + +/** + * Store encrypted recovery document. + * + * @param account_pub public key of the user's account + * @param account_sig signature affirming storage request + * @param recovery_data_hash hash of @a data + * @param recovery_data contains encrypted_recovery_document + * @param recovery_data_size size of data blob + * @param payment_secret identifier for the payment, used to later charge on uploads + * @param[out] version set to the version assigned to the document by the database + * @return transaction status, 0 if upload could not be finished because @a payment_secret + * did not have enough upload left; HARD error if @a payment_secret is unknown, ... + */ +enum ANASTASIS_DB_StoreStatus +ANASTASIS_DB_do_insert_recovery_document ( + const struct ANASTASIS_CRYPTO_AccountPublicKeyP *account_pub, + const struct ANASTASIS_AccountSignatureP *account_sig, + const struct GNUNET_HashCode *recovery_data_hash, + const void *recovery_data, + size_t recovery_data_size, + const void *recovery_meta_data, + size_t recovery_meta_data_size, + const struct ANASTASIS_PaymentSecretP *payment_secret, + uint32_t *version) +{ + enum GNUNET_DB_QueryStatus qs; + + GNUNET_break (GNUNET_OK == + ANASTASIS_DB_preflight ()); + + PREPARE ("do_insert_recovery_document_select_account", + "SELECT" + " expiration_date " + "FROM anastasis_user" + " WHERE user_id=$1" + " FOR UPDATE;"); + PREPARE ("do_insert_recovery_document_select_latest_version", + "SELECT" + " version" + ",recovery_data_hash" + ",expiration_date" + " FROM anastasis_recoverydocument" + " JOIN anastasis_user USING (user_id)" + " WHERE user_id=$1" + " ORDER BY version DESC" + " LIMIT 1;"); + PREPARE ("do_insert_recovery_document_select_post_counter", + "SELECT" + " post_counter" + " FROM anastasis_recdoc_payment" + " WHERE user_id=$1" + " AND payment_identifier=$2;"); + PREPARE ("do_insert_recovery_document_update_post_counter", + "UPDATE" + " anastasis_recdoc_payment" + " SET" + " post_counter=$1" + " WHERE user_id =$2" + " AND payment_identifier=$3;"); + PREPARE ("do_insert_recovery_document_insert", + "INSERT INTO anastasis_recoverydocument " + "(user_id" + ",version" + ",account_sig" + ",recovery_data_hash" + ",recovery_data" + ",recovery_meta_data" + ",creation_date" + ") VALUES " + "($1, $2, $3, $4, $5, $6, $7);"); + for (unsigned int retry = 0; retry<MAX_RETRIES; retry++) + { + if (GNUNET_OK != + ANASTASIS_DB_start ( + "do_insert_recovery_document")) + { + GNUNET_break (0); + return ANASTASIS_DB_STORE_STATUS_HARD_ERROR; + } + /* get the current version and hash of the latest recovery document + for this account */ + { + struct GNUNET_HashCode dh; + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_auto_from_type (account_pub), + GNUNET_PQ_query_param_end + }; + struct GNUNET_PQ_ResultSpec rs[] = { + GNUNET_PQ_result_spec_uint32 ("version", + version), + GNUNET_PQ_result_spec_auto_from_type ("recovery_data_hash", + &dh), + GNUNET_PQ_result_spec_end + }; + + qs = GNUNET_PQ_eval_prepared_singleton_select ( + pg->conn, + "do_insert_recovery_document_select_latest_version", + params, + rs); + switch (qs) + { + case GNUNET_DB_STATUS_HARD_ERROR: + GNUNET_break (0); + ANASTASIS_DB_rollback (); + return ANASTASIS_DB_STORE_STATUS_HARD_ERROR; + case GNUNET_DB_STATUS_SOFT_ERROR: + goto retry; + case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: + *version = 1; + break; + case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: + /* had an existing recovery_data, is it identical? */ + if (0 == GNUNET_memcmp (&dh, + recovery_data_hash)) + { + /* Yes. Previous identical recovery data exists */ + ANASTASIS_DB_rollback (); + return ANASTASIS_DB_STORE_STATUS_NO_RESULTS; + } + (*version)++; + break; + default: + GNUNET_break (0); + ANASTASIS_DB_rollback (); + return ANASTASIS_DB_STORE_STATUS_HARD_ERROR; + } + } + + /* First, check if account exists */ + { + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_auto_from_type (account_pub), + GNUNET_PQ_query_param_end + }; + struct GNUNET_PQ_ResultSpec rs[] = { + GNUNET_PQ_result_spec_end + }; + + qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn, + "do_insert_recovery_document_select_account", + params, + rs); + } + switch (qs) + { + case GNUNET_DB_STATUS_HARD_ERROR: + ANASTASIS_DB_rollback (); + return ANASTASIS_DB_STORE_STATUS_HARD_ERROR; + case GNUNET_DB_STATUS_SOFT_ERROR: + goto retry; + case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: + ANASTASIS_DB_rollback (); + return ANASTASIS_DB_STORE_STATUS_PAYMENT_REQUIRED; + case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: + /* handle interesting case below */ + break; + } + + { + uint32_t postcounter; + + /* lookup if the user has enough uploads left and decrement */ + { + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_auto_from_type (account_pub), + GNUNET_PQ_query_param_auto_from_type (payment_secret), + GNUNET_PQ_query_param_end + }; + struct GNUNET_PQ_ResultSpec rs[] = { + GNUNET_PQ_result_spec_uint32 ("post_counter", + &postcounter), + GNUNET_PQ_result_spec_end + }; + + qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn, + "do_insert_recovery_document_select_post_counter", + params, + rs); + switch (qs) + { + case GNUNET_DB_STATUS_HARD_ERROR: + ANASTASIS_DB_rollback (); + return ANASTASIS_DB_STORE_STATUS_HARD_ERROR; + case GNUNET_DB_STATUS_SOFT_ERROR: + goto retry; + case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: + ANASTASIS_DB_rollback (); + return ANASTASIS_DB_STORE_STATUS_HARD_ERROR; + case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: + break; + } + } + + if (0 == postcounter) + { + ANASTASIS_DB_rollback (); + return ANASTASIS_DB_STORE_STATUS_STORE_LIMIT_EXCEEDED; + } + /* Decrement the postcounter by one */ + postcounter--; + + /* Update the postcounter in the Database */ + { + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_uint32 (&postcounter), + GNUNET_PQ_query_param_auto_from_type (account_pub), + GNUNET_PQ_query_param_auto_from_type (payment_secret), + GNUNET_PQ_query_param_end + }; + + qs = GNUNET_PQ_eval_prepared_non_select (pg->conn, + "do_insert_recovery_document_update_post_counter", + params); + switch (qs) + { + case GNUNET_DB_STATUS_HARD_ERROR: + GNUNET_break (0); + ANASTASIS_DB_rollback (); + return ANASTASIS_DB_STORE_STATUS_HARD_ERROR; + case GNUNET_DB_STATUS_SOFT_ERROR: + goto retry; + case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: + GNUNET_break (0); + ANASTASIS_DB_rollback (); + return ANASTASIS_DB_STORE_STATUS_HARD_ERROR; + case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: + break; + default: + GNUNET_break (0); + ANASTASIS_DB_rollback (); + return ANASTASIS_DB_STORE_STATUS_HARD_ERROR; + } + } + } + + /* finally, actually insert the recovery document */ + { + struct GNUNET_TIME_Timestamp now + = GNUNET_TIME_timestamp_get (); + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_auto_from_type (account_pub), + GNUNET_PQ_query_param_uint32 (version), + GNUNET_PQ_query_param_auto_from_type (account_sig), + GNUNET_PQ_query_param_auto_from_type (recovery_data_hash), + GNUNET_PQ_query_param_fixed_size (recovery_data, + recovery_data_size), + GNUNET_PQ_query_param_fixed_size (recovery_meta_data, + recovery_meta_data_size), + GNUNET_PQ_query_param_timestamp (&now), + GNUNET_PQ_query_param_end + }; + + qs = GNUNET_PQ_eval_prepared_non_select (pg->conn, + "do_insert_recovery_document_insert", + params); + switch (qs) + { + case GNUNET_DB_STATUS_HARD_ERROR: + ANASTASIS_DB_rollback (); + return ANASTASIS_DB_STORE_STATUS_HARD_ERROR; + case GNUNET_DB_STATUS_SOFT_ERROR: + goto retry; + case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: + GNUNET_break (0); + ANASTASIS_DB_rollback (); + return ANASTASIS_DB_STORE_STATUS_HARD_ERROR; + case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: + qs = ANASTASIS_DB_commit (); + if (GNUNET_DB_STATUS_SOFT_ERROR == qs) + goto retry; + if (qs < 0) + return ANASTASIS_DB_STORE_STATUS_HARD_ERROR; + return ANASTASIS_DB_STORE_STATUS_SUCCESS; + } + } +retry: + ANASTASIS_DB_rollback (); + } + return ANASTASIS_DB_STORE_STATUS_SOFT_ERROR; +} + + +/* end of do_insert_recovery_document.c */ diff --git a/src/stasis/do_update_account_lifetime.c b/src/stasis/do_update_account_lifetime.c @@ -0,0 +1,99 @@ +/* + This file is part of Anastasis + Copyright (C) 2020-2026 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file stasis/do_update_account_lifetime.c + * @brief Anastasis database: do update account lifetime + * @author Christian Grothoff + */ +#include "platform.h" +#include "anastasis-db_pg.h" +#include "anastasis/anastasis-database/do_update_account_lifetime.h" +#include "anastasis/anastasis-database/preflight.h" +#include <taler/taler_pq_lib.h> + + +enum GNUNET_DB_QueryStatus +ANASTASIS_DB_do_update_account_lifetime ( + const struct ANASTASIS_CRYPTO_AccountPublicKeyP *account_pub, + const struct ANASTASIS_PaymentSecretP *payment_identifier, + struct GNUNET_TIME_Timestamp min_expiration, + struct GNUNET_TIME_Relative lifetime, + struct GNUNET_TIME_Timestamp *paid_until) +{ + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_auto_from_type (account_pub), + GNUNET_PQ_query_param_auto_from_type (payment_identifier), + GNUNET_PQ_query_param_timestamp (&min_expiration), + GNUNET_PQ_query_param_relative_time (&lifetime), + GNUNET_PQ_query_param_end + }; + bool payment_credited; + struct GNUNET_PQ_ResultSpec rs[] = { + GNUNET_PQ_result_spec_bool ("payment_credited", + &payment_credited), + GNUNET_PQ_result_spec_timestamp ("paid_until", + paid_until), + GNUNET_PQ_result_spec_end + }; + enum GNUNET_DB_QueryStatus qs; + + /* Expiration dates are whole seconds; make sure adding the increment + cannot break that (GNUNET_PQ_result_spec_timestamp() insists on it). */ + if (! GNUNET_TIME_relative_is_forever (lifetime)) + lifetime.rel_value_us + -= lifetime.rel_value_us % GNUNET_TIME_UNIT_SECONDS.rel_value_us; + *paid_until = GNUNET_TIME_UNIT_ZERO_TS; + GNUNET_break (GNUNET_OK == + ANASTASIS_DB_preflight ()); + PREPARE ("do_update_account_lifetime", + "SELECT" + " out_payment_credited AS payment_credited" + ",out_paid_until AS paid_until" + " FROM anastasis_do_update_account_lifetime" + " ($1, $2, $3, $4);"); + qs = GNUNET_PQ_eval_prepared_singleton_select ( + pg->conn, + "do_update_account_lifetime", + params, + rs); + if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != qs) + { + /* The stored procedure always returns exactly one row, so anything + else is a real database failure. */ + GNUNET_break (qs < 0); + *paid_until = GNUNET_TIME_UNIT_ZERO_TS; + return (qs < 0) + ? qs + : GNUNET_DB_STATUS_HARD_ERROR; + } + if (! payment_credited) + { + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "Payment existed, lifetime of account %s unchanged at %s\n", + TALER_B2S (account_pub), + GNUNET_TIME_timestamp2s (*paid_until)); + return GNUNET_DB_STATUS_SUCCESS_NO_RESULTS; + } + GNUNET_break (! GNUNET_TIME_absolute_is_never (paid_until->abs_time)); + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "Updated lifetime of account %s to %s\n", + TALER_B2S (account_pub), + GNUNET_TIME_timestamp2s (*paid_until)); + return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT; +} + + +/* end of do_update_account_lifetime.c */ diff --git a/src/stasis/do_update_account_lifetime.sql b/src/stasis/do_update_account_lifetime.sql @@ -0,0 +1,79 @@ +-- +-- This file is part of Anastasis +-- Copyright (C) 2026 Anastasis SARL +-- +-- ANASTASIS is free software; you can redistribute it and/or modify it under the +-- terms of the GNU General Public License as published by the Free Software +-- Foundation; either version 3, or (at your option) any later version. +-- +-- ANASTASIS is distributed in the hope that it will be useful, but WITHOUT ANY +-- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +-- A PARTICULAR PURPOSE. See the GNU General Public License for more details. +-- +-- You should have received a copy of the GNU General Public License along with +-- ANASTASIS; see the file COPYING. If not, see <http://www.gnu.org/licenses/> +-- + +DROP FUNCTION IF EXISTS anastasis_do_update_account_lifetime; +CREATE FUNCTION anastasis_do_update_account_lifetime ( + IN in_user_id BYTEA, + IN in_payment_identifier BYTEA, + IN in_absolute_lifetime INT8, -- lower bound for the new expiration + IN in_relative_lifetime INT8, -- added on top of that lower bound + OUT out_payment_credited BOOLEAN, -- FALSE if the payment was not new + OUT out_paid_until INT8) +LANGUAGE plpgsql +AS $$ +BEGIN + +-- Credit the payment. This is the gate: a payment that is unknown or was +-- already credited must not extend the lifetime a second time. +UPDATE anastasis_recdoc_payment + SET paid=TRUE + WHERE payment_identifier=in_payment_identifier + AND user_id=in_user_id + AND paid=FALSE; + +IF NOT FOUND +THEN + -- Payment unknown or credited before: report the lifetime the account has + -- right now and leave it alone. + out_payment_credited=FALSE; + SELECT expiration_date + INTO out_paid_until + FROM anastasis_user + WHERE user_id=in_user_id; + IF NOT FOUND + THEN + out_paid_until=0; + END IF; + RETURN; +END IF; + +out_payment_credited=TRUE; + +-- New expiration is MAX(what the account has, the caller's lower bound) +-- plus the caller's increment. A caller that paid for a duration passes +-- the current time as the lower bound and the duration as the increment; a +-- caller that paid up to a date passes that date and a zero increment. The +-- LEAST() saturates at "forever" instead of overflowing INT8. +INSERT INTO anastasis_user + (user_id + ,expiration_date) + VALUES + (in_user_id + ,LEAST(in_absolute_lifetime, + 9223372036854775807 - in_relative_lifetime) + + in_relative_lifetime) +ON CONFLICT (user_id) DO UPDATE + SET expiration_date= + LEAST(GREATEST(anastasis_user.expiration_date, + in_absolute_lifetime), + 9223372036854775807 - in_relative_lifetime) + + in_relative_lifetime +RETURNING expiration_date INTO out_paid_until; + +END $$; + +COMMENT ON FUNCTION anastasis_do_update_account_lifetime + IS 'Credits a recovery-document payment and extends the account lifetime to MAX(current,in_absolute_lifetime)+in_relative_lifetime, creating the account if needed. Does nothing but report the current lifetime if the payment is not new.'; diff --git a/src/stasis/do_verify_challenge_code.c b/src/stasis/do_verify_challenge_code.c @@ -0,0 +1,210 @@ +/* + This file is part of Anastasis + Copyright (C) 2020-2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file stasis/do_verify_challenge_code.c + * @brief Anastasis database: do verify challenge code + * @author Christian Grothoff + */ +#include "platform.h" +#include "anastasis-db_pg.h" +#include "anastasis/anastasis-database/do_verify_challenge_code.h" +#include "anastasis/anastasis-database/transaction.h" +#include "anastasis/anastasis-database/preflight.h" +#include <taler/taler_pq_lib.h> + + +struct CheckValidityContext +{ + /** + * Code to check for. + */ + const struct GNUNET_HashCode *hashed_code; + + /** + * Truth we are processing. + */ + const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid; + + /** + * Set to the matching challenge code (if @e valid). + */ + uint64_t code; + + /** + * Set to true if a code matching @e hashed_code was found. + */ + bool valid; + + /** + * Set to true if a code matching @e hashed_code was set to 'satisfied' by the plugin. + */ + bool satisfied; + + /** + * Set to true if we had a database failure. + */ + bool db_failure; + +}; + + +/** + * Helper function for #postgres_verify_challenge_code(). + * To be called with the results of a SELECT statement + * that has returned @a num_results results. + * + * @param cls closure of type `struct CheckValidityContext *` + * @param result the postgres result + * @param num_results the number of results in @a result + */ +static void +check_valid_code (void *cls, + PGresult *result, + unsigned int num_results) +{ + struct CheckValidityContext *cvc = cls; + + for (unsigned int i = 0; i < num_results; i++) + { + uint64_t server_code; + uint8_t sat; + struct GNUNET_PQ_ResultSpec rs[] = { + GNUNET_PQ_result_spec_uint64 ("code", + &server_code), + GNUNET_PQ_result_spec_auto_from_type ("satisfied", + &sat), + GNUNET_PQ_result_spec_end + }; + + if (GNUNET_OK != + GNUNET_PQ_extract_result (result, + rs, + i)) + { + GNUNET_break (0); + cvc->db_failure = true; + return; + } + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "Found issued challenge %llu (client: %s)\n", + (unsigned long long) server_code, + GNUNET_h2s (cvc->hashed_code)); + { + struct GNUNET_HashCode shashed_code; + + ANASTASIS_hash_answer (server_code, + &shashed_code); + if (0 == + GNUNET_memcmp (&shashed_code, + cvc->hashed_code)) + { + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "Challenge is valid challenge (%s)\n", + (0 != sat) ? "satisfied" : "not satisfied"); + cvc->valid = true; + cvc->code = server_code; + cvc->satisfied = (0 != sat); + } + else + { + /* count failures to prevent brute-force attacks */ + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_auto_from_type (cvc->truth_uuid), + GNUNET_PQ_query_param_uint64 (&server_code), + GNUNET_PQ_query_param_end + }; + enum GNUNET_DB_QueryStatus qs; + + qs = GNUNET_PQ_eval_prepared_non_select (pg->conn, + "do_verify_challenge_code_update_retry_counter", + params); + if (qs <= 0) + { + GNUNET_break (0); + cvc->db_failure = true; + } + } + } + } +} + + +/** + * Verify the provided code with the code on the server. + * If the code matches the function will return with success, if the code + * does not match, the retry counter will be decreased by one. + * + * @param truth_uuid identification of the challenge which the code corresponds to + * @param hashed_code code which the user provided and wants to verify + * @param[out] code set to the original numeric code + * @param[out] satisfied set to true if the challenge is set to satisfied + * @return code validity status + */ +enum ANASTASIS_DB_CodeStatus +ANASTASIS_DB_do_verify_challenge_code ( + const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid, + const struct GNUNET_HashCode *hashed_code, + uint64_t *code, + bool *satisfied) +{ + struct CheckValidityContext cvc = { + .truth_uuid = truth_uuid, + .hashed_code = hashed_code, + }; + struct GNUNET_TIME_Timestamp now = GNUNET_TIME_timestamp_get (); + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_auto_from_type (truth_uuid), + GNUNET_PQ_query_param_timestamp (&now), + GNUNET_PQ_query_param_end + }; + enum GNUNET_DB_QueryStatus qs; + + *satisfied = false; + PREPARE ("do_verify_challenge_code_update_retry_counter", + "UPDATE anastasis_challengecode" + " SET retry_counter=retry_counter - 1" + " WHERE truth_uuid=$1" + " AND code=$2" + " AND retry_counter != 0;"); + PREPARE ("do_verify_challenge_code_select", + "SELECT " + " code" + ",satisfied" + " FROM anastasis_challengecode" + " WHERE truth_uuid=$1" + " AND expiration_date > $2" + " AND retry_counter != 0;"); + qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn, + "do_verify_challenge_code_select", + params, + &check_valid_code, + &cvc); + if ( (qs < 0) || + (cvc.db_failure) ) + return ANASTASIS_DB_CODE_STATUS_HARD_ERROR; + *code = cvc.code; + if (cvc.valid) + { + *satisfied = cvc.satisfied; + return ANASTASIS_DB_CODE_STATUS_VALID_CODE_STORED; + } + if (0 == qs) + return ANASTASIS_DB_CODE_STATUS_NO_RESULTS; + return ANASTASIS_DB_CODE_STATUS_CHALLENGE_CODE_MISMATCH; +} + + +/* end of do_verify_challenge_code.c */ diff --git a/src/stasis/drop_tables.c b/src/stasis/drop_tables.c @@ -0,0 +1,34 @@ +/* + This file is part of Anastasis + Copyright (C) 2020, 2021, 2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file stasis/drop_tables.c + * @brief drop the Anastasis database tables + * @author Christian Grothoff + */ +#include "platform.h" +#include "anastasis-db_pg.h" +#include "anastasis/anastasis-database/drop_tables.h" + + +enum GNUNET_GenericReturnValue +ANASTASIS_DB_drop_tables (void) +{ + return GNUNET_PQ_exec_sql (pg->conn, + "drop"); +} + + +/* end of drop_tables.c */ diff --git a/src/stasis/gc.c b/src/stasis/gc.c @@ -0,0 +1,76 @@ +/* + This file is part of Anastasis + Copyright (C) 2020, 2021, 2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file stasis/gc.c + * @brief garbage collection for the Anastasis database + * @author Christian Grothoff + */ +#include "platform.h" +#include "anastasis-db_pg.h" +#include "anastasis/anastasis-database/gc.h" +#include "anastasis/anastasis-database/preflight.h" +#include "anastasis/anastasis-database/transaction.h" + + +enum GNUNET_DB_QueryStatus +ANASTASIS_DB_gc (struct GNUNET_TIME_Absolute expire_backups, + struct GNUNET_TIME_Absolute expire_pending_payments) +{ + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_absolute_time (&expire_backups), + GNUNET_PQ_query_param_end + }; + struct GNUNET_PQ_QueryParam params2[] = { + GNUNET_PQ_query_param_absolute_time (&expire_pending_payments), + GNUNET_PQ_query_param_end + }; + enum GNUNET_DB_QueryStatus qs; + + GNUNET_break (GNUNET_OK == + ANASTASIS_DB_preflight ()); +#if 0 + /* FIXME: should we do this? */ + PREPARE ("gc_challenge_pending_payments", + "DELETE FROM anastasis_challenge_payment " + "WHERE" + " (paid=FALSE" + " OR" + " refunded)" + " AND" + " creation_date < $1;"), +#endif + PREPARE ("gc_accounts", + "DELETE FROM anastasis_user " + "WHERE" + " expiration_date < $1;"); + qs = GNUNET_PQ_eval_prepared_non_select (pg->conn, + "gc_accounts", + params); + if (qs < 0) + return qs; + PREPARE ("gc_recdoc_pending_payments", + "DELETE FROM anastasis_recdoc_payment " + "WHERE" + " paid=FALSE" + " AND" + " creation_date < $1;"); + return GNUNET_PQ_eval_prepared_non_select (pg->conn, + "gc_recdoc_pending_payments", + params2); +} + + +/* end of gc.c */ diff --git a/src/stasis/gc_challenge_codes.c b/src/stasis/gc_challenge_codes.c @@ -0,0 +1,55 @@ +/* + This file is part of Anastasis + Copyright (C) 2020-2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file stasis/gc_challenge_codes.c + * @brief Anastasis database: gc challenge codes + * @author Christian Grothoff + */ +#include "platform.h" +#include "anastasis-db_pg.h" +#include "anastasis/anastasis-database/gc_challenge_codes.h" +#include "anastasis/anastasis-database/transaction.h" +#include "anastasis/anastasis-database/preflight.h" +#include <taler/taler_pq_lib.h> + + +/** + * Function called to remove all expired codes from the database. + * + * @return transaction status + */ +enum GNUNET_DB_QueryStatus +ANASTASIS_DB_gc_challenge_codes (void) +{ + struct GNUNET_TIME_Timestamp time_now = GNUNET_TIME_timestamp_get (); + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_timestamp (&time_now), + GNUNET_PQ_query_param_end + }; + + GNUNET_break (GNUNET_OK == + ANASTASIS_DB_preflight ()); + PREPARE ("gc_challenge_codes", + "DELETE FROM anastasis_challengecode " + "WHERE " + "expiration_date < $1;"); + return GNUNET_PQ_eval_prepared_non_select (pg->conn, + "gc_challenge_codes", + params); +} + + +/* end of gc_challenge_codes.c */ diff --git a/src/stasis/get_account.c b/src/stasis/get_account.c @@ -0,0 +1,136 @@ +/* + This file is part of Anastasis + Copyright (C) 2020-2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file stasis/get_account.c + * @brief Anastasis database: get account + * @author Christian Grothoff + */ +#include "platform.h" +#include "anastasis-db_pg.h" +#include "anastasis/anastasis-database/get_account.h" +#include "anastasis/anastasis-database/transaction.h" +#include "anastasis/anastasis-database/preflight.h" +#include <taler/taler_pq_lib.h> + + +/** + * Check if an account exists, and if so, return the + * current @a recovery_document_hash. + * + * @param account_pub account identifier + * @param[out] paid_until until when is the account paid up? + * @param[out] recovery_data_hash set to hash of @a recovery document + * @param[out] version set to the recovery policy version + * @return transaction status + */ +enum ANASTASIS_DB_AccountStatus +ANASTASIS_DB_get_account ( + const struct ANASTASIS_CRYPTO_AccountPublicKeyP *account_pub, + struct GNUNET_TIME_Timestamp *paid_until, + struct GNUNET_HashCode *recovery_data_hash, + uint32_t *version) +{ + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_auto_from_type (account_pub), + GNUNET_PQ_query_param_end + }; + enum GNUNET_DB_QueryStatus qs; + + PREPARE ("get_account_select_account", + "SELECT" + " expiration_date " + "FROM anastasis_user" + " WHERE user_id=$1" + " FOR UPDATE;"); + PREPARE ("get_account_select_latest_version", + "SELECT" + " version" + ",recovery_data_hash" + ",expiration_date" + " FROM anastasis_recoverydocument" + " JOIN anastasis_user USING (user_id)" + " WHERE user_id=$1" + " ORDER BY version DESC" + " LIMIT 1;"); + GNUNET_break (GNUNET_OK == + ANASTASIS_DB_preflight ()); + { + struct GNUNET_PQ_ResultSpec rs[] = { + GNUNET_PQ_result_spec_timestamp ("expiration_date", + paid_until), + GNUNET_PQ_result_spec_auto_from_type ("recovery_data_hash", + recovery_data_hash), + GNUNET_PQ_result_spec_uint32 ("version", + version), + GNUNET_PQ_result_spec_end + }; + + qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn, + "get_account_select_latest_version", + params, + rs); + } + switch (qs) + { + case GNUNET_DB_STATUS_HARD_ERROR: + return ANASTASIS_DB_ACCOUNT_STATUS_HARD_ERROR; + case GNUNET_DB_STATUS_SOFT_ERROR: + GNUNET_break (0); + return ANASTASIS_DB_ACCOUNT_STATUS_HARD_ERROR; + case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: + break; /* handle interesting case below */ + case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: + return ANASTASIS_DB_ACCOUNT_STATUS_VALID_HASH_RETURNED; + } + + /* check if account exists */ + { + struct GNUNET_PQ_ResultSpec rs[] = { + GNUNET_PQ_result_spec_timestamp ("expiration_date", + paid_until), + GNUNET_PQ_result_spec_end + }; + + qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn, + "get_account_select_account", + params, + rs); + } + switch (qs) + { + case GNUNET_DB_STATUS_HARD_ERROR: + return ANASTASIS_DB_ACCOUNT_STATUS_HARD_ERROR; + case GNUNET_DB_STATUS_SOFT_ERROR: + GNUNET_break (0); + return ANASTASIS_DB_ACCOUNT_STATUS_HARD_ERROR; + case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: + /* indicates: no account */ + return ANASTASIS_DB_ACCOUNT_STATUS_PAYMENT_REQUIRED; + case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: + /* indicates: no backup */ + *version = UINT32_MAX; + memset (recovery_data_hash, + 0, + sizeof (*recovery_data_hash)); + return ANASTASIS_DB_ACCOUNT_STATUS_NO_RESULTS; + default: + GNUNET_break (0); + return ANASTASIS_DB_ACCOUNT_STATUS_HARD_ERROR; + } +} + + +/* end of get_account.c */ diff --git a/src/stasis/get_challenge_payment.c b/src/stasis/get_challenge_payment.c @@ -0,0 +1,71 @@ +/* + This file is part of Anastasis + Copyright (C) 2020-2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file stasis/get_challenge_payment.c + * @brief Anastasis database: get challenge payment + * @author Christian Grothoff + */ +#include "platform.h" +#include "anastasis-db_pg.h" +#include "anastasis/anastasis-database/get_challenge_payment.h" +#include "anastasis/anastasis-database/transaction.h" +#include "anastasis/anastasis-database/preflight.h" +#include <taler/taler_pq_lib.h> + + +/** + * Check payment identifier. Used to check if a payment identifier given by + * the user is valid (existing and paid). + * + * @param payment_secret payment secret which the user must provide with every upload + * @param truth_uuid which truth should we check the payment status of + * @param[out] paid bool value to show if payment is paid + * @return transaction status + */ +enum GNUNET_DB_QueryStatus +ANASTASIS_DB_get_challenge_payment ( + const struct ANASTASIS_PaymentSecretP *payment_secret, + const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid, + bool *paid) +{ + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_auto_from_type (payment_secret), + GNUNET_PQ_query_param_auto_from_type (truth_uuid), + GNUNET_PQ_query_param_end + }; + struct GNUNET_PQ_ResultSpec rs[] = { + GNUNET_PQ_result_spec_bool ("paid", + paid), + GNUNET_PQ_result_spec_end + }; + + PREPARE ("get_challenge_payment", + "SELECT" + " paid" + " FROM anastasis_challenge_payment" + " WHERE payment_identifier=$1" + " AND truth_uuid=$2" + " AND refunded=FALSE" + " AND counter>0;"); + return GNUNET_PQ_eval_prepared_singleton_select ( + pg->conn, + "get_challenge_payment", + params, + rs); +} + + +/* end of get_challenge_payment.c */ diff --git a/src/stasis/get_exists_challenge_code_satisfied.c b/src/stasis/get_exists_challenge_code_satisfied.c @@ -0,0 +1,69 @@ +/* + This file is part of Anastasis + Copyright (C) 2020-2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file stasis/get_exists_challenge_code_satisfied.c + * @brief Anastasis database: get exists challenge code satisfied + * @author Christian Grothoff + */ +#include "platform.h" +#include "anastasis-db_pg.h" +#include "anastasis/anastasis-database/get_exists_challenge_code_satisfied.h" +#include "anastasis/anastasis-database/transaction.h" +#include "anastasis/anastasis-database/preflight.h" +#include <taler/taler_pq_lib.h> + + +/** + * Check if the 'satisfied' bit for the given challenge and code is + * 'true' and the challenge code is not yet expired. + * + * @param truth_uuid identification of the challenge which the code corresponds to + * @param code code which is now satisfied + * @return transaction status, + * #GNUNET_DB_STATUS_SUCCESS_NO_RESULTS if the challenge code is not satisfied or expired + * #GNUNET_DB_STATUS_SUCCESS_ONE_RESULT if the challenge code has been marked as satisfied + */ +enum GNUNET_DB_QueryStatus +ANASTASIS_DB_get_exists_challenge_code_satisfied ( + const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid, + const uint64_t code, + struct GNUNET_TIME_Timestamp after) +{ + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_auto_from_type (truth_uuid), + GNUNET_PQ_query_param_uint64 (&code), + GNUNET_PQ_query_param_timestamp (&after), + GNUNET_PQ_query_param_end + }; + struct GNUNET_PQ_ResultSpec rs[] = { + GNUNET_PQ_result_spec_end + }; + + PREPARE ("get_exists_challenge_code_satisfied", + "SELECT 1 FROM anastasis_challengecode" + " WHERE truth_uuid=$1" + " AND satisfied=TRUE" + " AND code=$2" + " AND creation_date >= $3" + " LIMIT 1;"); + return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, + "get_exists_challenge_code_satisfied", + params, + rs); +} + + +/* end of get_exists_challenge_code_satisfied.c */ diff --git a/src/stasis/get_last_auth_iban_in_row.c b/src/stasis/get_last_auth_iban_in_row.c @@ -0,0 +1,69 @@ +/* + This file is part of Anastasis + Copyright (C) 2020-2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file stasis/get_last_auth_iban_in_row.c + * @brief Anastasis database: get last auth iban in row + * @author Christian Grothoff + */ +#include "platform.h" +#include "anastasis-db_pg.h" +#include "anastasis/anastasis-database/get_last_auth_iban_in_row.h" +#include "anastasis/anastasis-database/transaction.h" +#include "anastasis/anastasis-database/preflight.h" +#include <taler/taler_pq_lib.h> + + +/** + * Function to check the last known IBAN payment. + * + * @param credit_account which credit account to check + * @param[out] last_row set to the last known row + * @return transaction status, + * #GNUNET_DB_STATUS_SUCCESS_ONE_RESULT if @a cb + * returned 'true' once + * #GNUNET_DB_STATUS_SUCCESS_NO_RESULTS if no + * wire transfers existed for which @a cb returned true + */ +enum GNUNET_DB_QueryStatus +ANASTASIS_DB_get_last_auth_iban_in_row ( + const char *credit_account, + uint64_t *last_row) +{ + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_string (credit_account), + GNUNET_PQ_query_param_end + }; + struct GNUNET_PQ_ResultSpec rs[] = { + GNUNET_PQ_result_spec_uint64 ("wire_reference", + last_row), + GNUNET_PQ_result_spec_end + }; + + PREPARE ("get_last_auth_iban_in_row", + "SELECT " + " wire_reference" + " FROM anastasis_auth_iban_in" + " WHERE credit_account_details=$1" + " ORDER BY wire_reference DESC" + " LIMIT 1;"); + return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, + "get_last_auth_iban_in_row", + params, + rs); +} + + +/* end of get_last_auth_iban_in_row.c */ diff --git a/src/stasis/get_latest_recovery_document.c b/src/stasis/get_latest_recovery_document.c @@ -0,0 +1,85 @@ +/* + This file is part of Anastasis + Copyright (C) 2020-2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file stasis/get_latest_recovery_document.c + * @brief Anastasis database: get latest recovery document + * @author Christian Grothoff + */ +#include "platform.h" +#include "anastasis-db_pg.h" +#include "anastasis/anastasis-database/get_latest_recovery_document.h" +#include "anastasis/anastasis-database/transaction.h" +#include "anastasis/anastasis-database/preflight.h" +#include <taler/taler_pq_lib.h> + + +/** + * Fetch latest recovery document for user. + * + * @param account_pub public key of the user's account + * @param account_sig signature + * @param recovery_data_hash hash of the current recovery data + * @param data_size size of data blob + * @param data blob which contains the recovery document + * @param[out] version set to the version number of the policy being returned + * @return transaction status + */ +enum GNUNET_DB_QueryStatus +ANASTASIS_DB_get_latest_recovery_document ( + const struct ANASTASIS_CRYPTO_AccountPublicKeyP *account_pub, + struct ANASTASIS_AccountSignatureP *account_sig, + struct GNUNET_HashCode *recovery_data_hash, + size_t *data_size, + void **data, + uint32_t *version) +{ + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_auto_from_type (account_pub), + GNUNET_PQ_query_param_end + }; + struct GNUNET_PQ_ResultSpec rs[] = { + GNUNET_PQ_result_spec_uint32 ("version", + version), + GNUNET_PQ_result_spec_auto_from_type ("account_sig", + account_sig), + GNUNET_PQ_result_spec_auto_from_type ("recovery_data_hash", + recovery_data_hash), + GNUNET_PQ_result_spec_variable_size ("recovery_data", + data, + data_size), + GNUNET_PQ_result_spec_end + }; + + GNUNET_break (GNUNET_OK == + ANASTASIS_DB_preflight ()); + PREPARE ("get_latest_recovery_document", + "SELECT " + " version" + ",account_sig" + ",recovery_data_hash" + ",recovery_data" + " FROM anastasis_recoverydocument" + " WHERE user_id=$1" + " ORDER BY version DESC" + " LIMIT 1;"); + return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, + "get_latest_recovery_document", + params, + rs); +} + + +/* end of get_latest_recovery_document.c */ diff --git a/src/stasis/get_pending_challenge_payment.c b/src/stasis/get_pending_challenge_payment.c @@ -0,0 +1,78 @@ +/* + This file is part of Anastasis + Copyright (C) 2020-2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file stasis/get_pending_challenge_payment.c + * @brief Anastasis database: get pending challenge payment + * @author Christian Grothoff + */ +#include "platform.h" +#include "anastasis-db_pg.h" +#include "anastasis/anastasis-database/get_pending_challenge_payment.h" +#include "anastasis/anastasis-database/transaction.h" +#include "anastasis/anastasis-database/preflight.h" +#include <taler/taler_pq_lib.h> + + +/** + * Lookup pending payment for a certain challenge. + * + * @param truth_uuid identification of the challenge + * @param[out] payment_secret set to the challenge payment secret + * @return transaction status + */ +enum GNUNET_DB_QueryStatus +ANASTASIS_DB_get_pending_challenge_payment ( + const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid, + struct ANASTASIS_PaymentSecretP *payment_secret) +{ + struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get (); + struct GNUNET_TIME_Timestamp recent + = GNUNET_TIME_absolute_to_timestamp ( + GNUNET_TIME_absolute_subtract (now, + ANASTASIS_CHALLENGE_OFFER_LIFETIME)); + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_auto_from_type (truth_uuid), + GNUNET_PQ_query_param_timestamp (&recent), + GNUNET_PQ_query_param_end + }; + struct GNUNET_PQ_ResultSpec rs[] = { + GNUNET_PQ_result_spec_auto_from_type ("payment_identifier", + payment_secret), + GNUNET_PQ_result_spec_end + }; + + PREPARE ("get_pending_challenge_payment", + "SELECT" + " creation_date" + ",payment_identifier" + ",amount" + " FROM anastasis_challenge_payment" + " WHERE" + " paid=FALSE" + " AND" + " refunded=FALSE" + " AND" + " truth_uuid=$1" + " AND" + " creation_date > $2;"); + return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, + "get_pending_challenge_payment", + params, + rs); +} + + +/* end of get_pending_challenge_payment.c */ diff --git a/src/stasis/get_recdoc_payment.c b/src/stasis/get_recdoc_payment.c @@ -0,0 +1,71 @@ +/* + This file is part of Anastasis + Copyright (C) 2020-2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file stasis/get_recdoc_payment.c + * @brief Anastasis database: get recdoc payment + * @author Christian Grothoff + */ +#include "platform.h" +#include "anastasis-db_pg.h" +#include "anastasis/anastasis-database/get_recdoc_payment.h" +#include "anastasis/anastasis-database/transaction.h" +#include "anastasis/anastasis-database/preflight.h" +#include <taler/taler_pq_lib.h> + + +/** + * Check payment identifier. Used to check if a payment identifier given by + * the user is valid (existing and paid). + * + * @param payment_secret payment secret which the user must provide with every upload + * @param[out] paid bool value to show if payment is paid + * @param[out] valid_counter bool value to show if post_counter is > 0 + * @return transaction status + */ +enum GNUNET_DB_QueryStatus +ANASTASIS_DB_get_recdoc_payment ( + const struct ANASTASIS_PaymentSecretP *payment_secret, + bool *paid, + bool *valid_counter) +{ + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_auto_from_type (payment_secret), + GNUNET_PQ_query_param_end + }; + struct GNUNET_PQ_ResultSpec rs[] = { + GNUNET_PQ_result_spec_bool ("paid", + paid), + GNUNET_PQ_result_spec_bool ("valid_counter", + valid_counter), + GNUNET_PQ_result_spec_end + }; + + PREPARE ("get_recdoc_payment", + "SELECT" + " creation_date" + ",post_counter > 0 AS valid_counter" + ",amount" + ",paid" + " FROM anastasis_recdoc_payment" + " WHERE payment_identifier=$1;"); + return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, + "get_recdoc_payment", + params, + rs); +} + + +/* end of get_recdoc_payment.c */ diff --git a/src/stasis/get_recovery_document.c b/src/stasis/get_recovery_document.c @@ -0,0 +1,80 @@ +/* + This file is part of Anastasis + Copyright (C) 2020-2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file stasis/get_recovery_document.c + * @brief Anastasis database: get recovery document + * @author Christian Grothoff + */ +#include "platform.h" +#include "anastasis-db_pg.h" +#include "anastasis/anastasis-database/get_recovery_document.h" +#include "anastasis/anastasis-database/transaction.h" +#include "anastasis/anastasis-database/preflight.h" +#include <taler/taler_pq_lib.h> + + +/** + * Fetch recovery document for user according given version. + * + * @param account_pub public key of the user's account + * @param version the version number of the policy the user requests + * @param[out] account_sig signature + * @param[out] recovery_data_hash hash of the current recovery data + * @param[out] data_size size of data blob + * @param[out] data blob which contains the recovery document + * @return transaction status + */ +enum GNUNET_DB_QueryStatus +ANASTASIS_DB_get_recovery_document ( + const struct ANASTASIS_CRYPTO_AccountPublicKeyP *account_pub, + uint32_t version, + struct ANASTASIS_AccountSignatureP *account_sig, + struct GNUNET_HashCode *recovery_data_hash, + size_t *data_size, + void **data) +{ + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_auto_from_type (account_pub), + GNUNET_PQ_query_param_uint32 (&version), + GNUNET_PQ_query_param_end + }; + struct GNUNET_PQ_ResultSpec rs[] = { + GNUNET_PQ_result_spec_auto_from_type ("account_sig", + account_sig), + GNUNET_PQ_result_spec_auto_from_type ("recovery_data_hash", + recovery_data_hash), + GNUNET_PQ_result_spec_variable_size ("recovery_data", + data, + data_size), + GNUNET_PQ_result_spec_end + }; + + PREPARE ("get_recovery_document", + "SELECT " + " account_sig" + ",recovery_data_hash" + ",recovery_data" + " FROM anastasis_recoverydocument" + " WHERE user_id=$1" + " AND version=$2;"); + return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, + "get_recovery_document", + params, + rs); +} + + +/* end of get_recovery_document.c */ diff --git a/src/stasis/get_truth.c b/src/stasis/get_truth.c @@ -0,0 +1,76 @@ +/* + This file is part of Anastasis + Copyright (C) 2020-2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file stasis/get_truth.c + * @brief Anastasis database: get truth + * @author Christian Grothoff + */ +#include "platform.h" +#include "anastasis-db_pg.h" +#include "anastasis/anastasis-database/get_truth.h" +#include "anastasis/anastasis-database/transaction.h" +#include "anastasis/anastasis-database/preflight.h" +#include <taler/taler_pq_lib.h> + + +/** + * Get the encrypted truth to validate the challenge response + * + * @param truth_uuid the identifier for the Truth + * @param[out] truth contains the encrypted truth + * @param[out] truth_size size of the encrypted truth + * @param[out] truth_mime mime type of truth + * @param[out] method type of the challenge + * @return transaction status + */ +enum GNUNET_DB_QueryStatus +ANASTASIS_DB_get_truth ( + const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid, + void **truth, + size_t *truth_size, + char **truth_mime, + char **method) +{ + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_auto_from_type (truth_uuid), + GNUNET_PQ_query_param_end + }; + struct GNUNET_PQ_ResultSpec rs[] = { + GNUNET_PQ_result_spec_variable_size ("encrypted_truth", + truth, + truth_size), + GNUNET_PQ_result_spec_string ("truth_mime", + truth_mime), + GNUNET_PQ_result_spec_string ("method_name", + method), + GNUNET_PQ_result_spec_end + }; + + PREPARE ("get_truth", + "SELECT " + " method_name" + ",encrypted_truth" + ",truth_mime" + " FROM anastasis_truth" + " WHERE truth_uuid=$1;"); + return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, + "get_truth", + params, + rs); +} + + +/* end of get_truth.c */ diff --git a/src/stasis/get_truth_key_share.c b/src/stasis/get_truth_key_share.c @@ -0,0 +1,64 @@ +/* + This file is part of Anastasis + Copyright (C) 2020-2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file stasis/get_truth_key_share.c + * @brief Anastasis database: get truth key share + * @author Christian Grothoff + */ +#include "platform.h" +#include "anastasis-db_pg.h" +#include "anastasis/anastasis-database/get_truth_key_share.h" +#include "anastasis/anastasis-database/transaction.h" +#include "anastasis/anastasis-database/preflight.h" +#include <taler/taler_pq_lib.h> + + +/** + * Lookup (encrypted) key share by @a truth_uuid. + * + * @param truth_uuid the identifier for the Truth + * @param[out] key_share contains the encrypted Keyshare + * @return transaction status + */ +enum GNUNET_DB_QueryStatus +ANASTASIS_DB_get_truth_key_share ( + const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid, + struct ANASTASIS_CRYPTO_EncryptedKeyShareP *key_share) +{ + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_auto_from_type (truth_uuid), + GNUNET_PQ_query_param_end + }; + struct GNUNET_PQ_ResultSpec rs[] = { + GNUNET_PQ_result_spec_auto_from_type ("key_share_data", + key_share), + GNUNET_PQ_result_spec_end + }; + + PREPARE ("get_truth_key_share", + "SELECT " + "key_share_data " + "FROM " + "anastasis_truth " + "WHERE truth_uuid =$1;"); + return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, + "get_truth_key_share", + params, + rs); +} + + +/* end of get_truth_key_share.c */ diff --git a/src/stasis/get_truth_payment.c b/src/stasis/get_truth_payment.c @@ -0,0 +1,66 @@ +/* + This file is part of Anastasis + Copyright (C) 2020-2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file stasis/get_truth_payment.c + * @brief Anastasis database: get truth payment + * @author Christian Grothoff + */ +#include "platform.h" +#include "anastasis-db_pg.h" +#include "anastasis/anastasis-database/get_truth_payment.h" +#include "anastasis/anastasis-database/transaction.h" +#include "anastasis/anastasis-database/preflight.h" +#include <taler/taler_pq_lib.h> + + +/** + * Inquire whether truth upload payment was made. + * + * @param uuid the truth's UUID + * @param[out] paid_until set for how long this truth is paid for + * @return transaction status + */ +enum GNUNET_DB_QueryStatus +ANASTASIS_DB_get_truth_payment ( + const struct ANASTASIS_CRYPTO_TruthUUIDP *uuid, + struct GNUNET_TIME_Timestamp *paid_until) +{ + struct GNUNET_TIME_Timestamp now = GNUNET_TIME_timestamp_get (); + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_auto_from_type (uuid), + GNUNET_PQ_query_param_timestamp (&now), + GNUNET_PQ_query_param_end + }; + struct GNUNET_PQ_ResultSpec rs[] = { + GNUNET_PQ_result_spec_timestamp ("expiration", + paid_until), + GNUNET_PQ_result_spec_end + }; + + PREPARE ("get_truth_payment", + "SELECT" + " expiration" + " FROM anastasis_truth_payment" + " WHERE truth_uuid=$1" + " AND expiration>$2;"); + return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, + "get_truth_payment", + params, + rs); +} + + +/* end of get_truth_payment.c */ diff --git a/src/stasis/insert_auth_iban_in.c b/src/stasis/insert_auth_iban_in.c @@ -0,0 +1,76 @@ +/* + This file is part of Anastasis + Copyright (C) 2020-2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file stasis/insert_auth_iban_in.c + * @brief Anastasis database: insert auth iban in + * @author Christian Grothoff + */ +#include "platform.h" +#include "anastasis-db_pg.h" +#include "anastasis/anastasis-database/insert_auth_iban_in.h" +#include "anastasis/anastasis-database/transaction.h" +#include "anastasis/anastasis-database/preflight.h" +#include <taler/taler_pq_lib.h> + + +/** + * Store inbound IBAN payment made for authentication. + * + * @param wire_reference unique identifier inside LibEuFin/Nexus + * @param wire_subject subject of the wire transfer + * @param amount how much was transferred + * @param debit_account account that was debited + * @param credit_account Anastasis operator account credited + * @param execution_date when was the transfer made + * @return transaction status + */ +enum GNUNET_DB_QueryStatus +ANASTASIS_DB_insert_auth_iban_in ( + uint64_t wire_reference, + const char *wire_subject, + const struct TALER_Amount *amount, + const char *debit_account, + const char *credit_account, + struct GNUNET_TIME_Timestamp execution_date) +{ + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_uint64 (&wire_reference), + GNUNET_PQ_query_param_string (wire_subject), + TALER_PQ_query_param_amount (pg->conn, + amount), + GNUNET_PQ_query_param_string (debit_account), + GNUNET_PQ_query_param_string (credit_account), + GNUNET_PQ_query_param_timestamp (&execution_date), + GNUNET_PQ_query_param_end + }; + + PREPARE ("insert_auth_iban_in", + "INSERT INTO anastasis_auth_iban_in " + "(wire_reference" + ",wire_subject" + ",credit" + ",debit_account_details" + ",credit_account_details" + ",execution_date" + ") VALUES " + "($1, $2, $3, $4, $5, $6);"); + return GNUNET_PQ_eval_prepared_non_select (pg->conn, + "insert_auth_iban_in", + params); +} + + +/* end of insert_auth_iban_in.c */ diff --git a/src/stasis/insert_challenge_code.c b/src/stasis/insert_challenge_code.c @@ -0,0 +1,195 @@ +/* + This file is part of Anastasis + Copyright (C) 2020-2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file stasis/insert_challenge_code.c + * @brief Anastasis database: insert challenge code + * @author Christian Grothoff + */ +#include "platform.h" +#include "anastasis-db_pg.h" +#include "anastasis/anastasis-database/insert_challenge_code.h" +#include "anastasis/anastasis-database/transaction.h" +#include "anastasis/anastasis-database/preflight.h" +#include <taler/taler_pq_lib.h> + + +/** + * Create a new challenge code for a given challenge identified by the challenge + * public key. The function will first check if there is already a valid code + * for this challenge present and won't insert a new one in this case. + * + * @param truth_uuid the identifier for the challenge + * @param rotation_period for how long is the code available + * @param validity_period for how long is the code available + * @param retry_counter amount of retries allowed + * @param[out] retransmission_date when to next retransmit + * @param[out] code set to the code which will be checked for later + * @return transaction status, + * #GNUNET_DB_STATUS_SUCCESS_NO_RESULTS if we are out of valid tries, + * #GNUNET_DB_STATUS_SUCCESS_ONE_RESULT if @a code is now in the DB + */ +enum GNUNET_DB_QueryStatus +ANASTASIS_DB_insert_challenge_code ( + const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid, + struct GNUNET_TIME_Relative rotation_period, + struct GNUNET_TIME_Relative validity_period, + uint32_t retry_counter, + struct GNUNET_TIME_Timestamp *retransmission_date, + uint64_t *code) +{ + struct GNUNET_TIME_Timestamp now = GNUNET_TIME_timestamp_get (); + struct GNUNET_TIME_Timestamp expiration_date; + struct GNUNET_TIME_Absolute ex_rot; + + PREPARE ("insert_challenge_code_select_active", + "SELECT " + " code" + ",retry_counter" + ",retransmission_date" + " FROM anastasis_challengecode" + " WHERE truth_uuid=$1" + " AND expiration_date > $2" + " AND creation_date > $3" + " ORDER BY creation_date DESC" + " LIMIT 1;"); + PREPARE ("insert_challenge_code_insert", + "INSERT INTO anastasis_challengecode " + "(truth_uuid" + ",code" + ",creation_date" + ",expiration_date" + ",retry_counter" + ") VALUES " + "($1, $2, $3, $4, $5);"); + expiration_date = GNUNET_TIME_relative_to_timestamp (validity_period); + ex_rot = GNUNET_TIME_absolute_subtract (now.abs_time, + rotation_period); + for (unsigned int retries = 0; retries<MAX_RETRIES; retries++) + { + if (GNUNET_OK != + ANASTASIS_DB_start ( + "insert_challenge_code")) + { + GNUNET_break (0); + return GNUNET_DB_STATUS_HARD_ERROR; + } + + { + uint32_t old_retry_counter; + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_auto_from_type (truth_uuid), + GNUNET_PQ_query_param_timestamp (&now), + GNUNET_PQ_query_param_absolute_time (&ex_rot), + GNUNET_PQ_query_param_end + }; + struct GNUNET_PQ_ResultSpec rs[] = { + GNUNET_PQ_result_spec_uint64 ("code", + code), + GNUNET_PQ_result_spec_uint32 ("retry_counter", + &old_retry_counter), + GNUNET_PQ_result_spec_timestamp ("retransmission_date", + retransmission_date), + GNUNET_PQ_result_spec_end + }; + enum GNUNET_DB_QueryStatus qs; + + qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn, + "insert_challenge_code_select_active", + params, + rs); + switch (qs) + { + case GNUNET_DB_STATUS_HARD_ERROR: + GNUNET_break (0); + ANASTASIS_DB_rollback (); + return qs; + case GNUNET_DB_STATUS_SOFT_ERROR: + goto retry; + case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "No active challenge found, creating a fresh one\n"); + break; + case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: + if (0 == old_retry_counter) + { + ANASTASIS_DB_rollback (); + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "Active challenge %llu has zero tries left, refusing to create another one\n", + (unsigned long long) *code); + return GNUNET_DB_STATUS_SUCCESS_NO_RESULTS; + } + ANASTASIS_DB_rollback (); + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "Active challenge has %u tries left, returning old challenge %llu\n", + (unsigned int) old_retry_counter, + (unsigned long long) *code); + return qs; + } + } + + *code = GNUNET_CRYPTO_random_u64 (ANASTASIS_PIN_MAX_VALUE); + *retransmission_date = GNUNET_TIME_UNIT_ZERO_TS; + { + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_auto_from_type (truth_uuid), + GNUNET_PQ_query_param_uint64 (code), + GNUNET_PQ_query_param_timestamp (&now), + GNUNET_PQ_query_param_timestamp (&expiration_date), + GNUNET_PQ_query_param_uint32 (&retry_counter), + GNUNET_PQ_query_param_end + }; + enum GNUNET_DB_QueryStatus qs; + + qs = GNUNET_PQ_eval_prepared_non_select (pg->conn, + "insert_challenge_code_insert", + params); + switch (qs) + { + case GNUNET_DB_STATUS_HARD_ERROR: + ANASTASIS_DB_rollback (); + return GNUNET_DB_STATUS_HARD_ERROR; + case GNUNET_DB_STATUS_SOFT_ERROR: + goto retry; + case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: + GNUNET_break (0); + ANASTASIS_DB_rollback (); + return GNUNET_DB_STATUS_HARD_ERROR; + case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "Created fresh challenge with %u tries left\n", + (unsigned int) retry_counter); + break; + } + } + + { + enum GNUNET_DB_QueryStatus qs; + + qs = ANASTASIS_DB_commit (); + if (GNUNET_DB_STATUS_SOFT_ERROR == qs) + goto retry; + if (qs < 0) + return qs; + } + return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT; +retry: + ANASTASIS_DB_rollback (); + } + return GNUNET_DB_STATUS_SOFT_ERROR; +} + + +/* end of insert_challenge_code.c */ diff --git a/src/stasis/insert_challenge_payment.c b/src/stasis/insert_challenge_payment.c @@ -0,0 +1,67 @@ +/* + This file is part of Anastasis + Copyright (C) 2020-2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file stasis/insert_challenge_payment.c + * @brief Anastasis database: insert challenge payment + * @author Christian Grothoff + */ +#include "platform.h" +#include "anastasis-db_pg.h" +#include "anastasis/anastasis-database/insert_challenge_payment.h" +#include "anastasis/anastasis-database/transaction.h" +#include "anastasis/anastasis-database/preflight.h" +#include <taler/taler_pq_lib.h> + + +/** + * Store payment for challenge. + * + * @param truth_uuid identifier of the challenge to pay + * @param payment_secret payment secret which the user must provide with every upload + * @param amount how much we asked for + * @return transaction status + */ +enum GNUNET_DB_QueryStatus +ANASTASIS_DB_insert_challenge_payment ( + const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid, + const struct ANASTASIS_PaymentSecretP *payment_secret, + const struct TALER_Amount *amount) +{ + struct GNUNET_TIME_Timestamp now = GNUNET_TIME_timestamp_get (); + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_auto_from_type (truth_uuid), + TALER_PQ_query_param_amount (pg->conn, + amount), + GNUNET_PQ_query_param_auto_from_type (payment_secret), + GNUNET_PQ_query_param_timestamp (&now), + GNUNET_PQ_query_param_end + }; + + PREPARE ("insert_challenge_payment", + "INSERT INTO anastasis_challenge_payment " + "(truth_uuid" + ",amount" + ",payment_identifier" + ",creation_date" + ") VALUES " + "($1, $2, $3, $4);"); + return GNUNET_PQ_eval_prepared_non_select (pg->conn, + "insert_challenge_payment", + params); +} + + +/* end of insert_challenge_payment.c */ diff --git a/src/stasis/insert_recdoc_payment.c b/src/stasis/insert_recdoc_payment.c @@ -0,0 +1,155 @@ +/* + This file is part of Anastasis + Copyright (C) 2020-2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file stasis/insert_recdoc_payment.c + * @brief Anastasis database: insert recdoc payment + * @author Christian Grothoff + */ +#include "platform.h" +#include "anastasis-db_pg.h" +#include "anastasis/anastasis-database/insert_recdoc_payment.h" +#include "anastasis/anastasis-database/transaction.h" +#include "anastasis/anastasis-database/preflight.h" +#include <taler/taler_pq_lib.h> + + +/** + * Store payment. Used to begin a payment, not indicative + * that the payment actually was made. (That is done + * when we increment the account's lifetime.) + * + * @param account_pub anastasis's public key + * @param post_counter how many uploads does @a amount pay for + * @param payment_secret payment secret which the user must provide with every upload + * @param amount how much we asked for + * @return transaction status + */ +enum GNUNET_DB_QueryStatus +ANASTASIS_DB_insert_recdoc_payment ( + const struct ANASTASIS_CRYPTO_AccountPublicKeyP *account_pub, + uint32_t post_counter, + const struct ANASTASIS_PaymentSecretP *payment_secret, + const struct TALER_Amount *amount) +{ + struct GNUNET_TIME_Timestamp now = GNUNET_TIME_timestamp_get (); + struct GNUNET_TIME_Timestamp expiration; + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_auto_from_type (account_pub), + GNUNET_PQ_query_param_uint32 (&post_counter), + TALER_PQ_query_param_amount (pg->conn, + amount), + GNUNET_PQ_query_param_auto_from_type (payment_secret), + GNUNET_PQ_query_param_timestamp (&now), + GNUNET_PQ_query_param_end + }; + enum GNUNET_DB_QueryStatus qs; + + GNUNET_break (GNUNET_OK == + ANASTASIS_DB_preflight ()); + PREPARE ("insert_recdoc_payment_select_account", + "SELECT" + " expiration_date " + "FROM anastasis_user" + " WHERE user_id=$1" + " FOR UPDATE;"); + PREPARE ("insert_recdoc_payment_insert_account", + "INSERT INTO anastasis_user " + "(user_id" + ",expiration_date" + ") VALUES " + "($1, $2);"); + PREPARE ("insert_recdoc_payment_insert", + "INSERT INTO anastasis_recdoc_payment " + "(user_id" + ",post_counter" + ",amount" + ",payment_identifier" + ",creation_date" + ") VALUES " + "($1, $2, $3, $4, $5);"); + + /* because of constraint at user_id, first we have to verify + if user exists, and if not, create one */ + { + struct GNUNET_PQ_QueryParam iparams[] = { + GNUNET_PQ_query_param_auto_from_type (account_pub), + GNUNET_PQ_query_param_end + }; + struct GNUNET_PQ_ResultSpec rs[] = { + GNUNET_PQ_result_spec_timestamp ("expiration_date", + &expiration), + GNUNET_PQ_result_spec_end + }; + + qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn, + "insert_recdoc_payment_select_account", + iparams, + rs); + } + switch (qs) + { + case GNUNET_DB_STATUS_HARD_ERROR: + return qs; + case GNUNET_DB_STATUS_SOFT_ERROR: + GNUNET_break (0); + return GNUNET_DB_STATUS_HARD_ERROR; + case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: + { + /* create new user with short lifetime */ + struct GNUNET_TIME_Timestamp exp + = GNUNET_TIME_relative_to_timestamp (TRANSIENT_LIFETIME); + struct GNUNET_PQ_QueryParam iparams[] = { + GNUNET_PQ_query_param_auto_from_type (account_pub), + GNUNET_PQ_query_param_timestamp (&exp), + GNUNET_PQ_query_param_end + }; + + qs = GNUNET_PQ_eval_prepared_non_select (pg->conn, + "insert_recdoc_payment_insert_account", + iparams); + switch (qs) + { + case GNUNET_DB_STATUS_HARD_ERROR: + return GNUNET_DB_STATUS_HARD_ERROR; + case GNUNET_DB_STATUS_SOFT_ERROR: + GNUNET_break (0); + return GNUNET_DB_STATUS_HARD_ERROR; + case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: + GNUNET_break (0); + return GNUNET_DB_STATUS_HARD_ERROR; + case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: + /* successful, continue below */ + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "Created new account %s with transient life until %s\n", + TALER_B2S (account_pub), + GNUNET_TIME_timestamp2s (exp)); + break; + } + } + /* continue below */ + break; + case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: + /* handle case below */ + break; + } + + return GNUNET_PQ_eval_prepared_non_select (pg->conn, + "insert_recdoc_payment_insert", + params); +} + + +/* end of insert_recdoc_payment.c */ diff --git a/src/stasis/insert_truth.c b/src/stasis/insert_truth.c @@ -0,0 +1,80 @@ +/* + This file is part of Anastasis + Copyright (C) 2020-2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file stasis/insert_truth.c + * @brief Anastasis database: insert truth + * @author Christian Grothoff + */ +#include "platform.h" +#include "anastasis-db_pg.h" +#include "anastasis/anastasis-database/insert_truth.h" +#include "anastasis/anastasis-database/transaction.h" +#include "anastasis/anastasis-database/preflight.h" +#include <taler/taler_pq_lib.h> + + +/** + * Upload Truth, which contains the Truth and the KeyShare. + * + * @param truth_uuid the identifier for the Truth + * @param key_share_data contains information of an EncryptedKeyShare + * @param mime_type presumed mime type of data in @a encrypted_truth + * @param encrypted_truth contains the encrypted Truth which includes the ground truth i.e. H(challenge answer), phonenumber, SMS + * @param encrypted_truth_size the size of the Truth + * @param method name of method + * @param truth_expiration time till the according data will be stored + * @return transaction status + */ +enum GNUNET_DB_QueryStatus +ANASTASIS_DB_insert_truth ( + const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid, + const struct ANASTASIS_CRYPTO_EncryptedKeyShareP *key_share_data, + const char *mime_type, + const void *encrypted_truth, + size_t encrypted_truth_size, + const char *method, + struct GNUNET_TIME_Relative truth_expiration) +{ + struct GNUNET_TIME_Timestamp expiration; + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_auto_from_type (truth_uuid), + GNUNET_PQ_query_param_auto_from_type (key_share_data), + GNUNET_PQ_query_param_string (method), + GNUNET_PQ_query_param_fixed_size (encrypted_truth, + encrypted_truth_size), + GNUNET_PQ_query_param_string (mime_type), + GNUNET_PQ_query_param_timestamp (&expiration), + GNUNET_PQ_query_param_end + }; + + expiration = GNUNET_TIME_relative_to_timestamp (truth_expiration); + PREPARE ("insert_truth", + "INSERT INTO anastasis_truth " + "(truth_uuid" + ",key_share_data" + ",method_name" + ",encrypted_truth" + ",truth_mime" + ",expiration" + ") VALUES " + "($1, $2, $3, $4, $5, $6);"); + return GNUNET_PQ_eval_prepared_non_select (pg->conn, + "insert_truth", + params); +} + + +/* end of insert_truth.c */ diff --git a/src/stasis/insert_truth_payment.c b/src/stasis/insert_truth_payment.c @@ -0,0 +1,66 @@ +/* + This file is part of Anastasis + Copyright (C) 2020-2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file stasis/insert_truth_payment.c + * @brief Anastasis database: insert truth payment + * @author Christian Grothoff + */ +#include "platform.h" +#include "anastasis-db_pg.h" +#include "anastasis/anastasis-database/insert_truth_payment.h" +#include "anastasis/anastasis-database/transaction.h" +#include "anastasis/anastasis-database/preflight.h" +#include <taler/taler_pq_lib.h> + + +/** + * Record truth upload payment was made. + * + * @param uuid the truth's UUID + * @param amount the amount that was paid + * @param duration how long is the truth paid for + * @return transaction status + */ +enum GNUNET_DB_QueryStatus +ANASTASIS_DB_insert_truth_payment ( + const struct ANASTASIS_CRYPTO_TruthUUIDP *uuid, + const struct TALER_Amount *amount, + struct GNUNET_TIME_Relative duration) +{ + struct GNUNET_TIME_Timestamp exp = GNUNET_TIME_relative_to_timestamp ( + duration); + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_auto_from_type (uuid), + TALER_PQ_query_param_amount (pg->conn, + amount), + GNUNET_PQ_query_param_timestamp (&exp), + GNUNET_PQ_query_param_end + }; + + PREPARE ("insert_truth_payment", + "INSERT INTO anastasis_truth_payment " + "(truth_uuid" + ",amount" + ",expiration" + ") VALUES " + "($1, $2, $3);"); + return GNUNET_PQ_eval_prepared_non_select (pg->conn, + "insert_truth_payment", + params); +} + + +/* end of insert_truth_payment.c */ diff --git a/src/stasis/iterate_auth_iban_transfers.c b/src/stasis/iterate_auth_iban_transfers.c @@ -0,0 +1,149 @@ +/* + This file is part of Anastasis + Copyright (C) 2020-2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file stasis/iterate_auth_iban_transfers.c + * @brief Anastasis database: iterate auth iban transfers + * @author Christian Grothoff + */ +#include "platform.h" +#include "anastasis-db_pg.h" +#include "anastasis/anastasis-database/iterate_auth_iban_transfers.h" +#include "anastasis/anastasis-database/transaction.h" +#include "anastasis/anastasis-database/preflight.h" +#include <taler/taler_pq_lib.h> + + +struct TestIbanContext +{ + + /** + * Function to call on each wire transfer found. + */ + ANASTASIS_DB_AuthIbanTransferCallback cb; + + /** + * Closure for @a cb. + */ + void *cb_cls; + + /** + * Value to return. + */ + enum GNUNET_DB_QueryStatus qs; +}; + + +/** + * Helper function for #postgres_test_auth_iban_payment(). + * To be called with the results of a SELECT statement + * that has returned @a num_results results. + * + * @param cls closure of type `struct TestIbanContext *` + * @param result the postgres result + * @param num_results the number of results in @a result + */ +static void +test_auth_cb (void *cls, + PGresult *result, + unsigned int num_results) +{ + struct TestIbanContext *tic = cls; + + for (unsigned int i = 0; i<num_results; i++) + { + struct TALER_Amount credit; + char *wire_subject; + struct GNUNET_PQ_ResultSpec rs[] = { + TALER_PQ_result_spec_amount ("credit", + pg->currency, + &credit), + GNUNET_PQ_result_spec_string ("wire_subject", + &wire_subject), + GNUNET_PQ_result_spec_end + }; + + if (GNUNET_OK != + GNUNET_PQ_extract_result (result, + rs, + i)) + { + GNUNET_break (0); + tic->qs = GNUNET_DB_STATUS_HARD_ERROR; + return; + } + if (tic->cb (tic->cb_cls, + &credit, + wire_subject)) + { + GNUNET_free (wire_subject); + tic->qs = GNUNET_DB_STATUS_SUCCESS_ONE_RESULT; + return; + } + GNUNET_free (wire_subject); + } +} + + +/** + * Function to check if we are aware of a wire transfer + * that satisfies the IBAN plugin's authentication check. + * + * @param debit_account which debit account to check + * @param earliest_date earliest date to check + * @param cb function to call on all entries found + * @param cb_cls closure for @a cb + * @return transaction status, + * #GNUNET_DB_STATUS_SUCCESS_ONE_RESULT if @a cb + * returned 'true' once + * #GNUNET_DB_STATUS_SUCCESS_NO_RESULTS if no + * wire transfers existed for which @a cb returned true + */enum GNUNET_DB_QueryStatus +ANASTASIS_DB_iterate_auth_iban_transfers ( + const char *debit_account, + struct GNUNET_TIME_Timestamp earliest_date, + ANASTASIS_DB_AuthIbanTransferCallback cb, + void *cb_cls) +{ + struct TestIbanContext tic = { + .cb = cb, + .cb_cls = cb_cls + }; + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_string (debit_account), + GNUNET_PQ_query_param_timestamp (&earliest_date), + GNUNET_PQ_query_param_end + }; + enum GNUNET_DB_QueryStatus qs; + + PREPARE ("iterate_auth_iban_transfers", + "SELECT" + " credit" + ",wire_subject" + " FROM anastasis_auth_iban_in" + " WHERE debit_account_details=$1" + " AND execution_date>=$2;"); + qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn, + "iterate_auth_iban_transfers", + params, + &test_auth_cb, + &tic); + if (qs < 0) + return qs; + return tic.qs; +} + + +/* end of iterate_auth_iban_transfers.c */ diff --git a/src/stasis/iterate_recovery_meta_data.c b/src/stasis/iterate_recovery_meta_data.c @@ -0,0 +1,155 @@ +/* + This file is part of Anastasis + Copyright (C) 2020-2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file stasis/iterate_recovery_meta_data.c + * @brief Anastasis database: iterate recovery meta data + * @author Christian Grothoff + */ +#include "platform.h" +#include "anastasis-db_pg.h" +#include "anastasis/anastasis-database/iterate_recovery_meta_data.h" +#include "anastasis/anastasis-database/transaction.h" +#include "anastasis/anastasis-database/preflight.h" +#include <taler/taler_pq_lib.h> + + +struct MetaIteratorContext +{ + /** + * Function to call on each result. + */ + ANASTASIS_DB_RecoveryMetaCallback cb; + + /** + * Closure for @e cb. + */ + void *cb_cls; + + /** + * Set to true on database failure. + */ + bool db_failure; +}; + + +/** + * Helper function for #postgres_get_recovery_meta_data(). + * To be called with the results of a SELECT statement + * that has returned @a num_results results. + * + * @param cls closure of type `struct MetaIteratorContext *` + * @param result the postgres result + * @param num_results the number of results in @a result + */ +static void +meta_iterator (void *cls, + PGresult *result, + unsigned int num_results) +{ + struct MetaIteratorContext *ctx = cls; + + for (unsigned int i = 0; i<num_results; i++) + { + uint32_t version; + void *meta_data; + size_t meta_data_size; + struct GNUNET_TIME_Timestamp ts; + struct GNUNET_PQ_ResultSpec rs[] = { + GNUNET_PQ_result_spec_uint32 ("version", + &version), + GNUNET_PQ_result_spec_timestamp ("creation_date", + &ts), + GNUNET_PQ_result_spec_variable_size ("recovery_meta_data", + &meta_data, + &meta_data_size), + GNUNET_PQ_result_spec_end + }; + enum GNUNET_GenericReturnValue ret; + + if (GNUNET_OK != + GNUNET_PQ_extract_result (result, + rs, + i)) + { + GNUNET_break (0); + ctx->db_failure = true; + return; + } + ret = ctx->cb (ctx->cb_cls, + version, + ts, + meta_data, + meta_data_size); + GNUNET_PQ_cleanup_result (rs); + if (GNUNET_OK != ret) + break; + } +} + + +/** + * Fetch recovery document meta data for user. Returns + * meta data in descending order from @a max_version. + * The size of the result set may be limited. + * + * @param account_pub public key of the user's account + * @param max_version the maximum version number the user requests + * @param cb function to call on each result + * @param cb_cls closure for @a cb + * @return transaction status + */ +enum GNUNET_DB_QueryStatus +ANASTASIS_DB_iterate_recovery_meta_data ( + const struct ANASTASIS_CRYPTO_AccountPublicKeyP *account_pub, + uint32_t max_version, + ANASTASIS_DB_RecoveryMetaCallback cb, + void *cb_cls) +{ + struct MetaIteratorContext ctx = { + .cb = cb, + .cb_cls = cb_cls + }; + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_auto_from_type (account_pub), + GNUNET_PQ_query_param_uint32 (&max_version), + GNUNET_PQ_query_param_end + }; + enum GNUNET_DB_QueryStatus qs; + + PREPARE ("iterate_recovery_meta_data", + "SELECT " + " version" + ",creation_date" + ",recovery_meta_data" + " FROM anastasis_recoverydocument" + " WHERE user_id=$1" + " AND version < $2" + " ORDER BY version DESC" + " LIMIT 1000;"); + qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn, + "iterate_recovery_meta_data", + params, + &meta_iterator, + &ctx); + if (qs < 0) + return qs; + if (ctx.db_failure) + return GNUNET_DB_STATUS_HARD_ERROR; + return qs; +} + + +/* end of iterate_recovery_meta_data.c */ diff --git a/src/stasis/meson.build b/src/stasis/meson.build @@ -10,42 +10,60 @@ install_data( install_dir: sqldir, ) +# Stored procedures are not schema: they are dropped and recreated on every +# anastasis-dbinit, so fixing one does not need a new migration. Keep the +# list in the same order as libanastasisdb_SOURCES. +procedures_sql = [ + 'procedures-prelude.sql', + 'do_update_account_lifetime.sql', + 'commit.sql', +] + +custom_target( + 'gen-stasis-procedures.sql', + input: procedures_sql, + output: 'procedures.sql', + capture: true, + command: ['./amalgamate-sql.sh', '@INPUT@'], + install: true, + install_dir: sqldir, +) + install_data('stasis-postgres.conf', install_dir: pkgcfgdir) libanastasisdb_SOURCES = [ 'anastasis-db_pg.c', - 'anastasis-db_preflight.c', - 'anastasis-db_create_tables.c', - 'anastasis-db_drop_tables.c', - 'anastasis-db_gc.c', - 'anastasis-db_challenge_gc.c', - 'anastasis-db_check_challenge_payment.c', - 'anastasis-db_check_payment_identifier.c', - 'anastasis-db_check_truth_upload_paid.c', - 'anastasis-db_create_challenge_code.c', - 'anastasis-db_get_escrow_challenge.c', - 'anastasis-db_get_key_share.c', - 'anastasis-db_get_last_auth_iban_payment_row.c', - 'anastasis-db_get_latest_recovery_document.c', - 'anastasis-db_get_recovery_document.c', - 'anastasis-db_get_recovery_meta_data.c', - 'anastasis-db_increment_lifetime.c', - 'anastasis-db_lookup_account.c', - 'anastasis-db_lookup_challenge_payment.c', - 'anastasis-db_mark_challenge_code_satisfied.c', - 'anastasis-db_mark_challenge_sent.c', - 'anastasis-db_record_auth_iban_payment.c', - 'anastasis-db_record_challenge_payment.c', - 'anastasis-db_record_challenge_refund.c', - 'anastasis-db_record_recdoc_payment.c', - 'anastasis-db_record_truth_upload_payment.c', - 'anastasis-db_store_recovery_document.c', - 'anastasis-db_store_truth.c', - 'anastasis-db_test_auth_iban_payment.c', - 'anastasis-db_test_challenge_code_satisfied.c', - 'anastasis-db_update_challenge_payment.c', - 'anastasis-db_update_lifetime.c', - 'anastasis-db_verify_challenge_code.c', + 'preflight.c', + 'create_tables.c', + 'drop_tables.c', + 'gc.c', + 'gc_challenge_codes.c', + 'get_challenge_payment.c', + 'get_recdoc_payment.c', + 'get_truth_payment.c', + 'insert_challenge_code.c', + 'get_truth.c', + 'get_truth_key_share.c', + 'get_last_auth_iban_in_row.c', + 'get_latest_recovery_document.c', + 'get_recovery_document.c', + 'iterate_recovery_meta_data.c', + 'get_account.c', + 'get_pending_challenge_payment.c', + 'update_to_challenge_code_satisfied.c', + 'update_to_challenge_code_sent.c', + 'insert_auth_iban_in.c', + 'insert_challenge_payment.c', + 'update_to_challenge_payment_refunded.c', + 'insert_recdoc_payment.c', + 'insert_truth_payment.c', + 'do_insert_recovery_document.c', + 'insert_truth.c', + 'iterate_auth_iban_transfers.c', + 'get_exists_challenge_code_satisfied.c', + 'update_to_challenge_payment_paid.c', + 'do_update_account_lifetime.c', + 'do_verify_challenge_code.c', ] libanastasisdb = library( @@ -120,3 +138,17 @@ configure_file( output: 'test_anastasis_db_postgres.conf', copy: true, ) + + +# Enforce the database layer naming convention (see contrib/check-db-naming.py +# and the rules it implements). Needs no database, only the sources. +python3_bin = find_program('python3', required: false) +if python3_bin.found() + test( + 'check_db_naming', + python3_bin, + args: [meson.project_source_root() / 'contrib' / 'check-db-naming.py'], + workdir: meson.project_source_root(), + suite: ['stasis'], + ) +endif diff --git a/src/stasis/preflight.c b/src/stasis/preflight.c @@ -0,0 +1,58 @@ +/* + This file is part of Anastasis + Copyright (C) 2020, 2021, 2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file stasis/preflight.c + * @brief preflight check for the Anastasis database + * @author Christian Grothoff + */ +#include "platform.h" +#include "anastasis-db_pg.h" +#include "anastasis/anastasis-database/preflight.h" + + +enum GNUNET_GenericReturnValue +ANASTASIS_DB_preflight (void) +{ + struct GNUNET_PQ_ExecuteStatement es[] = { + GNUNET_PQ_make_execute ("ROLLBACK"), + GNUNET_PQ_EXECUTE_STATEMENT_END + }; + + if (NULL == pg->transaction_name) + { + GNUNET_PQ_reconnect_if_down (pg->conn); + return GNUNET_OK; /* all good */ + } + if (GNUNET_OK == + GNUNET_PQ_exec_statements (pg->conn, + es)) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "BUG: Preflight check rolled back transaction `%s'!\n", + pg->transaction_name); + } + else + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "BUG: Preflight check failed to rollback transaction `%s'!\n", + pg->transaction_name); + } + pg->transaction_name = NULL; + return GNUNET_NO; +} + + +/* end of preflight.c */ diff --git a/src/stasis/procedures-prelude.sql b/src/stasis/procedures-prelude.sql @@ -0,0 +1,19 @@ +-- +-- This file is part of Anastasis +-- Copyright (C) 2026 Anastasis SARL +-- +-- ANASTASIS is free software; you can redistribute it and/or modify it under the +-- terms of the GNU General Public License as published by the Free Software +-- Foundation; either version 3, or (at your option) any later version. +-- +-- ANASTASIS is distributed in the hope that it will be useful, but WITHOUT ANY +-- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +-- A PARTICULAR PURPOSE. See the GNU General Public License for more details. +-- +-- You should have received a copy of the GNU General Public License along with +-- ANASTASIS; see the file COPYING. If not, see <http://www.gnu.org/licenses/> +-- + +BEGIN; + +SET search_path TO anastasis; diff --git a/src/stasis/test_anastasis_db.c b/src/stasis/test_anastasis_db.c @@ -122,7 +122,7 @@ run (void *cls) memset (&key_share, 1, sizeof (key_share)); FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != - ANASTASIS_DB_store_truth ( + ANASTASIS_DB_insert_truth ( &truth_uuid, &key_share, mime_type, @@ -132,7 +132,7 @@ run (void *cls) rel_time)); FAILIF (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS != - ANASTASIS_DB_check_payment_identifier ( + ANASTASIS_DB_get_recdoc_payment ( &paymentSecretP, &paid, &valid_counter)); @@ -140,48 +140,71 @@ run (void *cls) memset (&accountPubP, 2, sizeof (accountPubP)); memset (&accountSig, 3, sizeof (accountSig)); FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != - ANASTASIS_DB_record_recdoc_payment ( + ANASTASIS_DB_insert_recdoc_payment ( &accountPubP, post_counter, &paymentSecretP, &amount)); { + struct GNUNET_TIME_Timestamp now = GNUNET_TIME_timestamp_get (); struct GNUNET_TIME_Timestamp res_time; + struct GNUNET_TIME_Timestamp replay_time; FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != - ANASTASIS_DB_increment_lifetime ( + ANASTASIS_DB_do_update_account_lifetime ( &accountPubP, &paymentSecretP, + now, rel_time, &res_time)); + /* Lifetime is MAX(what the account had, now) + rel_time; the account + already carries the transient lifetime insert_recdoc_payment() gave + it, so all we can pin down is the lower bound. */ + FAILIF (GNUNET_TIME_timestamp_cmp ( + res_time, + <, + GNUNET_TIME_absolute_to_timestamp ( + GNUNET_TIME_absolute_add (now.abs_time, + rel_time)))); + /* Replaying the same payment must not extend the lifetime again. */ + FAILIF (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS != + ANASTASIS_DB_do_update_account_lifetime ( + &accountPubP, + &paymentSecretP, + now, + rel_time, + &replay_time)); + FAILIF (GNUNET_TIME_timestamp_cmp (replay_time, + !=, + res_time)); } FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != - ANASTASIS_DB_check_payment_identifier ( + ANASTASIS_DB_get_recdoc_payment ( &paymentSecretP, &paid, &valid_counter)); FAILIF (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS != - ANASTASIS_DB_check_challenge_payment ( + ANASTASIS_DB_get_challenge_payment ( &paymentSecretP, &truth_uuid, &paid)); FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != - ANASTASIS_DB_record_challenge_payment ( + ANASTASIS_DB_insert_challenge_payment ( &truth_uuid, &paymentSecretP, &amount)); FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != - ANASTASIS_DB_update_challenge_payment ( + ANASTASIS_DB_update_to_challenge_payment_paid ( &truth_uuid, &paymentSecretP)); FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != - ANASTASIS_DB_check_challenge_payment ( + ANASTASIS_DB_get_challenge_payment ( &paymentSecretP, &truth_uuid, &paid)); FAILIF (! paid); FAILIF (ANASTASIS_DB_STORE_STATUS_SUCCESS != - ANASTASIS_DB_store_recovery_document ( + ANASTASIS_DB_do_insert_recovery_document ( &accountPubP, &accountSig, &recoveryDataHash, @@ -196,14 +219,14 @@ run (void *cls) struct GNUNET_TIME_Timestamp exp; FAILIF (ANASTASIS_DB_ACCOUNT_STATUS_VALID_HASH_RETURNED != - ANASTASIS_DB_lookup_account ( + ANASTASIS_DB_get_account ( &accountPubP, &exp, &r, &vrs)); } FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != - ANASTASIS_DB_get_key_share ( + ANASTASIS_DB_get_truth_key_share ( &truth_uuid, &res_key_share)); FAILIF (0 != @@ -240,7 +263,7 @@ run (void *cls) struct GNUNET_TIME_Timestamp rt; FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != - ANASTASIS_DB_create_challenge_code ( + ANASTASIS_DB_insert_challenge_code ( &truth_uuid, GNUNET_TIME_UNIT_HOURS, GNUNET_TIME_UNIT_DAYS, @@ -254,7 +277,7 @@ run (void *cls) uint64_t c2; FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != - ANASTASIS_DB_create_challenge_code ( + ANASTASIS_DB_insert_challenge_code ( &truth_uuid, GNUNET_TIME_UNIT_HOURS, GNUNET_TIME_UNIT_DAYS, @@ -270,7 +293,7 @@ run (void *cls) uint64_t r_code; FAILIF (ANASTASIS_DB_CODE_STATUS_CHALLENGE_CODE_MISMATCH != - ANASTASIS_DB_verify_challenge_code ( + ANASTASIS_DB_do_verify_challenge_code ( &truth_uuid, &c_hash, &r_code, @@ -279,7 +302,7 @@ run (void *cls) ANASTASIS_hash_answer (challenge_code, &c_hash); FAILIF (ANASTASIS_DB_CODE_STATUS_VALID_CODE_STORED != - ANASTASIS_DB_verify_challenge_code ( + ANASTASIS_DB_do_verify_challenge_code ( &truth_uuid, &c_hash, &r_code, diff --git a/src/stasis/update_to_challenge_code_satisfied.c b/src/stasis/update_to_challenge_code_satisfied.c @@ -0,0 +1,66 @@ +/* + This file is part of Anastasis + Copyright (C) 2020-2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file stasis/update_to_challenge_code_satisfied.c + * @brief Anastasis database: update to challenge code satisfied + * @author Christian Grothoff + */ +#include "platform.h" +#include "anastasis-db_pg.h" +#include "anastasis/anastasis-database/update_to_challenge_code_satisfied.h" +#include "anastasis/anastasis-database/transaction.h" +#include "anastasis/anastasis-database/preflight.h" +#include <taler/taler_pq_lib.h> + + +/** + * Set the 'satisfied' bit for the given challenge and code to + * 'true'. + * + * @param truth_uuid identification of the challenge which the code corresponds to + * @param code code which is now satisfied + * @return transaction status + */ +enum GNUNET_DB_QueryStatus +ANASTASIS_DB_update_to_challenge_code_satisfied ( + const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid, + const uint64_t code) +{ + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_auto_from_type (truth_uuid), + GNUNET_PQ_query_param_uint64 (&code), + GNUNET_PQ_query_param_end + }; + + PREPARE ("update_to_challenge_code_satisfied", + "UPDATE anastasis_challengecode" + " SET satisfied=TRUE" + " WHERE truth_uuid=$1" + " AND code=$2" + " AND creation_date IN" + " (SELECT creation_date" + " FROM anastasis_challengecode" + " WHERE truth_uuid=$1" + " AND code=$2" + " ORDER BY creation_date DESC" + " LIMIT 1);"); + return GNUNET_PQ_eval_prepared_non_select (pg->conn, + "update_to_challenge_code_satisfied", + params); +} + + +/* end of update_to_challenge_code_satisfied.c */ diff --git a/src/stasis/update_to_challenge_code_sent.c b/src/stasis/update_to_challenge_code_sent.c @@ -0,0 +1,98 @@ +/* + This file is part of Anastasis + Copyright (C) 2020-2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file stasis/update_to_challenge_code_sent.c + * @brief Anastasis database: update to challenge code sent + * @author Christian Grothoff + */ +#include "platform.h" +#include "anastasis-db_pg.h" +#include "anastasis/anastasis-database/update_to_challenge_code_sent.h" +#include "anastasis/anastasis-database/transaction.h" +#include "anastasis/anastasis-database/preflight.h" +#include <taler/taler_pq_lib.h> + + +/** + * Remember in the database that we successfully sent a challenge. + * + * @param payment_secret payment secret which the user must provide with every upload + * @param truth_uuid the identifier for the challenge + * @param code the challenge that was sent + */ +enum GNUNET_DB_QueryStatus +ANASTASIS_DB_update_to_challenge_code_sent ( + const struct ANASTASIS_PaymentSecretP *payment_secret, + const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid, + uint64_t code) +{ + enum GNUNET_DB_QueryStatus qs; + + PREPARE ("update_to_challenge_code_sent_update_code", + "UPDATE anastasis_challengecode" + " SET retransmission_date=$3" + " WHERE truth_uuid=$1" + " AND code=$2" + " AND creation_date IN" + " (SELECT creation_date" + " FROM anastasis_challengecode" + " WHERE truth_uuid=$1" + " AND code=$2" + " ORDER BY creation_date DESC" + " LIMIT 1);"); + PREPARE ("update_to_challenge_code_sent_dec_payment_counter", + "UPDATE anastasis_challenge_payment" + " SET counter=counter - 1" + " WHERE truth_uuid=$1" + " AND payment_identifier=$2" + " AND counter > 0;"); + { + struct GNUNET_TIME_Timestamp now; + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_auto_from_type (truth_uuid), + GNUNET_PQ_query_param_uint64 (&code), + GNUNET_PQ_query_param_timestamp (&now), + GNUNET_PQ_query_param_end + }; + + now = GNUNET_TIME_timestamp_get (); + qs = GNUNET_PQ_eval_prepared_non_select (pg->conn, + "update_to_challenge_code_sent_update_code", + params); + if (qs <= 0) + return qs; + } + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "Marking challenge %llu as issued\n", + (unsigned long long) code); + { + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_auto_from_type (truth_uuid), + GNUNET_PQ_query_param_auto_from_type (payment_secret), + GNUNET_PQ_query_param_end + }; + + qs = GNUNET_PQ_eval_prepared_non_select (pg->conn, + "update_to_challenge_code_sent_dec_payment_counter", + params); + if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs) + return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT; /* probably was free */ + return qs; + } +} + + +/* end of update_to_challenge_code_sent.c */ diff --git a/src/stasis/update_to_challenge_payment_paid.c b/src/stasis/update_to_challenge_payment_paid.c @@ -0,0 +1,65 @@ +/* + This file is part of Anastasis + Copyright (C) 2020-2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file stasis/update_to_challenge_payment_paid.c + * @brief Anastasis database: update to challenge payment paid + * @author Christian Grothoff + */ +#include "platform.h" +#include "anastasis-db_pg.h" +#include "anastasis/anastasis-database/update_to_challenge_payment_paid.h" +#include "anastasis/anastasis-database/transaction.h" +#include "anastasis/anastasis-database/preflight.h" +#include <taler/taler_pq_lib.h> + + +/** + * Update payment status of challenge + * + * @param truth_uuid which challenge received a payment + * @param payment_identifier proof of payment, must be unique and match pending payment + * @return transaction status + */ +enum GNUNET_DB_QueryStatus +ANASTASIS_DB_update_to_challenge_payment_paid ( + const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid, + const struct ANASTASIS_PaymentSecretP *payment_identifier) +{ + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_auto_from_type (payment_identifier), + GNUNET_PQ_query_param_auto_from_type (truth_uuid), + GNUNET_PQ_query_param_end + }; + + PREPARE ("update_to_challenge_payment_paid", + "UPDATE anastasis_challenge_payment " + "SET" + " paid=TRUE " + "WHERE" + " payment_identifier=$1" + " AND" + " refunded=FALSE" + " AND" + " truth_uuid=$2" + " AND" + " paid=FALSE;"); + return GNUNET_PQ_eval_prepared_non_select (pg->conn, + "update_to_challenge_payment_paid", + params); +} + + +/* end of update_to_challenge_payment_paid.c */ diff --git a/src/stasis/update_to_challenge_payment_refunded.c b/src/stasis/update_to_challenge_payment_refunded.c @@ -0,0 +1,63 @@ +/* + This file is part of Anastasis + Copyright (C) 2020-2022 Anastasis SARL + + Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file stasis/update_to_challenge_payment_refunded.c + * @brief Anastasis database: update to challenge payment refunded + * @author Christian Grothoff + */ +#include "platform.h" +#include "anastasis-db_pg.h" +#include "anastasis/anastasis-database/update_to_challenge_payment_refunded.h" +#include "anastasis/anastasis-database/transaction.h" +#include "anastasis/anastasis-database/preflight.h" +#include <taler/taler_pq_lib.h> + + +/** + * Store refund granted for challenge. + * + * @param truth_uuid identifier of the challenge to refund + * @param payment_secret payment secret which the user must provide with every upload + * @return transaction status + */ +enum GNUNET_DB_QueryStatus +ANASTASIS_DB_update_to_challenge_payment_refunded ( + const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid, + const struct ANASTASIS_PaymentSecretP *payment_secret) +{ + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_auto_from_type (payment_secret), + GNUNET_PQ_query_param_auto_from_type (truth_uuid), + GNUNET_PQ_query_param_end + }; + + PREPARE ("update_to_challenge_payment_refunded", + "UPDATE anastasis_challenge_payment " + "SET" + " refunded=TRUE " + "WHERE" + " payment_identifier=$1" + " AND" + " paid=TRUE" + " AND" + " truth_uuid=$2;"); + return GNUNET_PQ_eval_prepared_non_select (pg->conn, + "update_to_challenge_payment_refunded", + params); +} + + +/* end of update_to_challenge_payment_refunded.c */