aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2021-09-01 00:59:34 +0200
committerChristian Grothoff <christian@grothoff.org>2021-09-01 00:59:34 +0200
commitc3a1a4ecbea0c8df729b7a29da5d31c1f9dab9c9 (patch)
tree9fee7ded48451ba9e25b06b355c76078e291cef5
parent79de1f2faddd1032fa0f915365be6d85ea333fb3 (diff)
downloadanastasis-c3a1a4ecbea0c8df729b7a29da5d31c1f9dab9c9.tar.gz
anastasis-c3a1a4ecbea0c8df729b7a29da5d31c1f9dab9c9.zip
separate DB creation from plugin loading; fix misc. Debian package issues
-rw-r--r--debian/anastasis-httpd.postinst6
-rwxr-xr-xdebian/db/install/pgsql2
-rw-r--r--debian/etc/anastasis/anastasis.conf21
-rwxr-xr-xdebian/rules2
-rw-r--r--doc/sphinx/manpages/anastasis.conf.5.rst3
-rw-r--r--src/authorization/anastasis-helper-authorization-iban.c10
-rw-r--r--src/backend/anastasis-httpd.c8
-rw-r--r--src/include/anastasis_database_plugin.h22
-rw-r--r--src/stasis/anastasis-dbinit.c8
-rw-r--r--src/stasis/plugin_anastasis_postgres.c788
-rw-r--r--src/stasis/test_anastasis_db.c10
11 files changed, 495 insertions, 385 deletions
diff --git a/debian/anastasis-httpd.postinst b/debian/anastasis-httpd.postinst
index fe89acb..994b06b 100644
--- a/debian/anastasis-httpd.postinst
+++ b/debian/anastasis-httpd.postinst
@@ -20,6 +20,12 @@ configure)
20 adduser --quiet --system --ingroup ${_GROUPNAME} --no-create-home --home ${TALER_HOME} ${_USERNAME} 20 adduser --quiet --system --ingroup ${_GROUPNAME} --no-create-home --home ${TALER_HOME} ${_USERNAME}
21 fi 21 fi
22 22
23 if ! dpkg-statoverride --list /etc/anastasis/secrets/anastasis-db.secret.conf >/dev/null 2>&1; then
24 dpkg-statoverride --add --update \
25 anastasis-httpd root 460 \
26 /etc/anastasis/secrets/anastasis-db.secret.conf
27 fi
28
23 # Setup postgres database (needs dbconfig-pgsql package) 29 # Setup postgres database (needs dbconfig-pgsql package)
24 if [ -f /usr/share/dbconfig-common/dpkg/postinst.pgsql ]; then 30 if [ -f /usr/share/dbconfig-common/dpkg/postinst.pgsql ]; then
25 . /usr/share/dbconfig-common/dpkg/postinst.pgsql 31 . /usr/share/dbconfig-common/dpkg/postinst.pgsql
diff --git a/debian/db/install/pgsql b/debian/db/install/pgsql
index dab5d1e..f695fed 100755
--- a/debian/db/install/pgsql
+++ b/debian/db/install/pgsql
@@ -1,4 +1,4 @@
1#!/bin/sh 1#!/bin/bash
2 2
3set -eu 3set -eu
4 4
diff --git a/debian/etc/anastasis/anastasis.conf b/debian/etc/anastasis/anastasis.conf
index e662a43..4d0deda 100644
--- a/debian/etc/anastasis/anastasis.conf
+++ b/debian/etc/anastasis/anastasis.conf
@@ -1,5 +1,26 @@
1[anastasis] 1[anastasis]
2DATABASE = postgres 2DATABASE = postgres
3SERVE = unix
4
5# You must set each of the following options
6# before starting anastasis-httpd!
7#
8# Storage fee for policies (per year)
9#ANNUAL_FEE = KUDOS:0
10#
11# Storage fee for truth
12#TRUTH_UPLOAD_FEE = KUDOS:0
13
14# Name of your business
15#BUSINESS_NAME = ""
16
17# Random server salt. Use output of 'uuidgen'
18#SERVER_SALT = ""
19
20# How high is the per key share insurance offered
21# by your business?
22#INSURANCE = KUDOS:0
23
3 24
4[taler] 25[taler]
5# Currency accepted by anastasis via GNU Taler payments. 26# Currency accepted by anastasis via GNU Taler payments.
diff --git a/debian/rules b/debian/rules
index e3c7db5..cfd43c7 100755
--- a/debian/rules
+++ b/debian/rules
@@ -34,7 +34,7 @@ override_dh_install:
34 dh_install 34 dh_install
35# Done manually for debhelper-compat<13 35# Done manually for debhelper-compat<13
36 dh_installtmpfiles 36 dh_installtmpfiles
37# Remove files already present in libtalerexchange from main taler-exchange package 37# Remove files already present in libanastasis-dev/cli from anastasis-httpd/libanastasis packages
38 cd debian/libanastasis-dev; find . -type f,l -exec rm -f ../anastasis-httpd/{} \; 38 cd debian/libanastasis-dev; find . -type f,l -exec rm -f ../anastasis-httpd/{} \;
39 cd debian/anastasis-cli; find . -type f -exec rm -f ../anastasis-httpd/{} \; 39 cd debian/anastasis-cli; find . -type f -exec rm -f ../anastasis-httpd/{} \;
40 cd debian/libanastasis-dev; find . -type f,l -exec rm -f ../libanastasis/{} \; 40 cd debian/libanastasis-dev; find . -type f,l -exec rm -f ../libanastasis/{} \;
diff --git a/doc/sphinx/manpages/anastasis.conf.5.rst b/doc/sphinx/manpages/anastasis.conf.5.rst
index 1f6b49e..000d8f0 100644
--- a/doc/sphinx/manpages/anastasis.conf.5.rst
+++ b/doc/sphinx/manpages/anastasis.conf.5.rst
@@ -67,6 +67,9 @@ ANNUAL_FEE
67TRUTH_UPLOAD_FEE 67TRUTH_UPLOAD_FEE
68 Annual fee to be paid for truth uploads, i.e. "EUR:1.5". 68 Annual fee to be paid for truth uploads, i.e. "EUR:1.5".
69 69
70INSURANCE
71 Amount up to which key shares are warranted, i.e. "EUR:1000000".
72
70DB 73DB
71 Database backend to use, only ``postgres`` is supported right now. 74 Database backend to use, only ``postgres`` is supported right now.
72 75
diff --git a/src/authorization/anastasis-helper-authorization-iban.c b/src/authorization/anastasis-helper-authorization-iban.c
index 04dfa03..c6e5335 100644
--- a/src/authorization/anastasis-helper-authorization-iban.c
+++ b/src/authorization/anastasis-helper-authorization-iban.c
@@ -390,6 +390,16 @@ run (void *cls,
390 return; 390 return;
391 } 391 }
392 if (GNUNET_OK != 392 if (GNUNET_OK !=
393 db_plugin->connect (db_plugin->cls))
394 {
395 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
396 "Database not set up. Did you run anastasis-dbinit?\n");
397 global_ret = EXIT_NOTCONFIGURED;
398 ANASTASIS_DB_plugin_unload (db_plugin);
399 db_plugin = NULL;
400 return;
401 }
402 if (GNUNET_OK !=
393 GNUNET_CONFIGURATION_get_value_string (cfg, 403 GNUNET_CONFIGURATION_get_value_string (cfg,
394 "authorization-iban", 404 "authorization-iban",
395 "CREDIT_IBAN", 405 "CREDIT_IBAN",
diff --git a/src/backend/anastasis-httpd.c b/src/backend/anastasis-httpd.c
index fdf17ff..9f5c87b 100644
--- a/src/backend/anastasis-httpd.c
+++ b/src/backend/anastasis-httpd.c
@@ -888,6 +888,14 @@ run (void *cls,
888 GNUNET_SCHEDULER_shutdown (); 888 GNUNET_SCHEDULER_shutdown ();
889 return; 889 return;
890 } 890 }
891 if (GNUNET_OK !=
892 db->connect (db->cls))
893 {
894 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
895 "Database not setup. Did you run anastasis-dbinit?\n");
896 GNUNET_SCHEDULER_shutdown ();
897 return;
898 }
891 899
892 fh = TALER_MHD_bind (config, 900 fh = TALER_MHD_bind (config,
893 "anastasis", 901 "anastasis",
diff --git a/src/include/anastasis_database_plugin.h b/src/include/anastasis_database_plugin.h
index 565ad69..bc4b0e6 100644
--- a/src/include/anastasis_database_plugin.h
+++ b/src/include/anastasis_database_plugin.h
@@ -187,8 +187,26 @@ struct ANASTASIS_DatabasePlugin
187 * @param cls closure 187 * @param cls closure
188 * @return #GNUNET_OK upon success; #GNUNET_SYSERR upon failure 188 * @return #GNUNET_OK upon success; #GNUNET_SYSERR upon failure
189 */ 189 */
190 int 190 enum GNUNET_GenericReturnValue
191 (*drop_tables) (void *cls); 191 (*drop_tables)(void *cls);
192
193 /**
194 * Connect to the database.
195 *
196 * @param cls closure
197 * @return #GNUNET_OK upon success; #GNUNET_SYSERR upon failure
198 */
199 enum GNUNET_GenericReturnValue
200 (*connect)(void *cls);
201
202 /**
203 * Initialize merchant tables
204 *
205 * @param cls closure
206 * @return #GNUNET_OK upon success; #GNUNET_SYSERR upon failure
207 */
208 enum GNUNET_GenericReturnValue
209 (*create_tables)(void *cls);
192 210
193 /** 211 /**
194 * Function called to perform "garbage collection" on the 212 * Function called to perform "garbage collection" on the
diff --git a/src/stasis/anastasis-dbinit.c b/src/stasis/anastasis-dbinit.c
index 038fb7c..17b3c56 100644
--- a/src/stasis/anastasis-dbinit.c
+++ b/src/stasis/anastasis-dbinit.c
@@ -54,7 +54,7 @@ run (void *cls,
54 { 54 {
55 fprintf (stderr, 55 fprintf (stderr,
56 "Failed to initialize database plugin.\n"); 56 "Failed to initialize database plugin.\n");
57 global_ret = 1; 57 global_ret = EXIT_FAILURE;
58 return; 58 return;
59 } 59 }
60 if (reset_db) 60 if (reset_db)
@@ -63,6 +63,12 @@ run (void *cls,
63 ANASTASIS_DB_plugin_unload (plugin); 63 ANASTASIS_DB_plugin_unload (plugin);
64 plugin = ANASTASIS_DB_plugin_load (cfg); 64 plugin = ANASTASIS_DB_plugin_load (cfg);
65 } 65 }
66 if (GNUNET_OK !=
67 plugin->create_tables (plugin->cls))
68 {
69 global_ret = EXIT_FAILURE;
70 return;
71 }
66 ANASTASIS_DB_plugin_unload (plugin); 72 ANASTASIS_DB_plugin_unload (plugin);
67} 73}
68 74
diff --git a/src/stasis/plugin_anastasis_postgres.c b/src/stasis/plugin_anastasis_postgres.c
index 8ee16ad..b78dbdb 100644
--- a/src/stasis/plugin_anastasis_postgres.c
+++ b/src/stasis/plugin_anastasis_postgres.c
@@ -81,7 +81,7 @@ struct PostgresClosure
81 * @param cls closure our `struct Plugin` 81 * @param cls closure our `struct Plugin`
82 * @return #GNUNET_OK upon success; #GNUNET_SYSERR upon failure 82 * @return #GNUNET_OK upon success; #GNUNET_SYSERR upon failure
83 */ 83 */
84static int 84static enum GNUNET_GenericReturnValue
85postgres_drop_tables (void *cls) 85postgres_drop_tables (void *cls)
86{ 86{
87 struct PostgresClosure *pg = cls; 87 struct PostgresClosure *pg = cls;
@@ -100,6 +100,416 @@ postgres_drop_tables (void *cls)
100 100
101 101
102/** 102/**
103 * Initialize tables.
104 *
105 * @param cls the `struct PostgresClosure` with the plugin-specific state
106 * @return #GNUNET_OK upon success; #GNUNET_SYSERR upon failure
107 */
108static enum GNUNET_GenericReturnValue
109postgres_create_tables (void *cls)
110{
111 struct PostgresClosure *pc = cls;
112 struct GNUNET_PQ_Context *conn;
113
114 conn = GNUNET_PQ_connect_with_cfg (pc->cfg,
115 "stasis-postgres",
116 "stasis-",
117 NULL,
118 NULL);
119 if (NULL == conn)
120 return GNUNET_SYSERR;
121 GNUNET_PQ_disconnect (conn);
122 return GNUNET_OK;
123}
124
125
126/**
127 * Establish connection to the database.
128 *
129 * @param cls plugin context
130 * @return #GNUNET_OK upon success; #GNUNET_SYSERR upon failure
131 */
132static enum GNUNET_GenericReturnValue
133postgres_connect (void *cls)
134{
135 struct PostgresClosure *pg = cls;
136 struct GNUNET_PQ_PreparedStatement ps[] = {
137 GNUNET_PQ_make_prepare ("user_insert",
138 "INSERT INTO anastasis_user "
139 "(user_id"
140 ",expiration_date"
141 ") VALUES "
142 "($1, $2);",
143 2),
144 GNUNET_PQ_make_prepare ("do_commit",
145 "COMMIT",
146 0),
147 GNUNET_PQ_make_prepare ("user_select",
148 "SELECT"
149 " expiration_date "
150 "FROM anastasis_user"
151 " WHERE user_id=$1"
152 " FOR UPDATE;",
153 1),
154 GNUNET_PQ_make_prepare ("user_update",
155 "UPDATE anastasis_user"
156 " SET "
157 " expiration_date=$1"
158 " WHERE user_id=$2;",
159 2),
160 GNUNET_PQ_make_prepare ("recdoc_payment_insert",
161 "INSERT INTO anastasis_recdoc_payment "
162 "(user_id"
163 ",post_counter"
164 ",amount_val"
165 ",amount_frac"
166 ",payment_identifier"
167 ",creation_date"
168 ") VALUES "
169 "($1, $2, $3, $4, $5, $6);",
170 6),
171 GNUNET_PQ_make_prepare ("challenge_payment_insert",
172 "INSERT INTO anastasis_challenge_payment "
173 "(truth_uuid"
174 ",amount_val"
175 ",amount_frac"
176 ",payment_identifier"
177 ",creation_date"
178 ") VALUES "
179 "($1, $2, $3, $4, $5);",
180 5),
181 GNUNET_PQ_make_prepare ("truth_payment_insert",
182 "INSERT INTO anastasis_truth_payment "
183 "(truth_uuid"
184 ",amount_val"
185 ",amount_frac"
186 ",expiration"
187 ") VALUES "
188 "($1, $2, $3, $4);",
189 4),
190 GNUNET_PQ_make_prepare ("recdoc_payment_done",
191 "UPDATE anastasis_recdoc_payment "
192 "SET"
193 " paid=TRUE "
194 "WHERE"
195 " payment_identifier=$1"
196 " AND"
197 " user_id=$2"
198 " AND"
199 " paid=FALSE;",
200 2),
201 GNUNET_PQ_make_prepare ("challenge_refund_update",
202 "UPDATE anastasis_challenge_payment "
203 "SET"
204 " refunded=TRUE "
205 "WHERE"
206 " payment_identifier=$1"
207 " AND"
208 " paid=TRUE"
209 " AND"
210 " truth_uuid=$2;",
211 2),
212 GNUNET_PQ_make_prepare ("challenge_payment_done",
213 "UPDATE anastasis_challenge_payment "
214 "SET"
215 " paid=TRUE "
216 "WHERE"
217 " payment_identifier=$1"
218 " AND"
219 " refunded=FALSE"
220 " AND"
221 " truth_uuid=$2"
222 " AND"
223 " paid=FALSE;",
224 2),
225 GNUNET_PQ_make_prepare ("recdoc_payment_select",
226 "SELECT"
227 " creation_date"
228 ",post_counter"
229 ",amount_val"
230 ",amount_frac"
231 ",paid"
232 " FROM anastasis_recdoc_payment"
233 " WHERE payment_identifier=$1;",
234 1),
235 GNUNET_PQ_make_prepare ("truth_payment_select",
236 "SELECT"
237 " expiration"
238 " FROM anastasis_truth_payment"
239 " WHERE truth_uuid=$1"
240 " AND expiration>$2;",
241 2),
242 GNUNET_PQ_make_prepare ("challenge_payment_select",
243 "SELECT"
244 " creation_date"
245 ",amount_val"
246 ",amount_frac"
247 ",paid"
248 " FROM anastasis_challenge_payment"
249 " WHERE payment_identifier=$1"
250 " AND truth_uuid=$2"
251 " AND refunded=FALSE"
252 " AND counter>0;",
253 1),
254 GNUNET_PQ_make_prepare ("challenge_pending_payment_select",
255 "SELECT"
256 " creation_date"
257 ",payment_identifier"
258 ",amount_val"
259 ",amount_frac"
260 " FROM anastasis_challenge_payment"
261 " WHERE"
262 " paid=FALSE"
263 " AND"
264 " refunded=FALSE"
265 " AND"
266 " truth_uuid=$1"
267 " AND"
268 " creation_date > $2;",
269 1),
270 GNUNET_PQ_make_prepare ("recdoc_payments_select",
271 "SELECT"
272 " user_id"
273 ",payment_identifier"
274 ",amount_val"
275 ",amount_frac"
276 " FROM anastasis_recdoc_payment"
277 " WHERE paid=FALSE;",
278 0),
279 GNUNET_PQ_make_prepare ("gc_accounts",
280 "DELETE FROM anastasis_user "
281 "WHERE"
282 " expiration_date < $1;",
283 1),
284 GNUNET_PQ_make_prepare ("gc_recdoc_pending_payments",
285 "DELETE FROM anastasis_recdoc_payment "
286 "WHERE"
287 " paid=FALSE"
288 " AND"
289 " creation_date < $1;",
290 1),
291 GNUNET_PQ_make_prepare ("gc_challenge_pending_payments",
292 "DELETE FROM anastasis_challenge_payment "
293 "WHERE"
294 " (paid=FALSE"
295 " OR"
296 " refunded=TRUE)"
297 " AND"
298 " creation_date < $1;",
299 1),
300 GNUNET_PQ_make_prepare ("truth_insert",
301 "INSERT INTO anastasis_truth "
302 "(truth_uuid"
303 ",key_share_data"
304 ",method_name"
305 ",encrypted_truth"
306 ",truth_mime"
307 ",expiration"
308 ") VALUES "
309 "($1, $2, $3, $4, $5, $6);",
310 6),
311
312 GNUNET_PQ_make_prepare ("test_auth_iban_payment",
313 "SELECT"
314 " credit_val"
315 ",credit_frac"
316 ",wire_subject"
317 " FROM anastasis_auth_iban_in"
318 " WHERE debit_account_details=$1"
319 " AND execution_date>=$2;",
320 2),
321 GNUNET_PQ_make_prepare ("store_auth_iban_payment_details",
322 "INSERT INTO anastasis_auth_iban_in "
323 "(wire_reference"
324 ",wire_subject"
325 ",credit_val"
326 ",credit_frac"
327 ",debit_account_details"
328 ",credit_account_details"
329 ",execution_date"
330 ") VALUES "
331 "($1, $2, $3, $4, $5, $6, $7);",
332 7),
333
334
335 GNUNET_PQ_make_prepare ("recovery_document_insert",
336 "INSERT INTO anastasis_recoverydocument "
337 "(user_id"
338 ",version"
339 ",account_sig"
340 ",recovery_data_hash"
341 ",recovery_data"
342 ") VALUES "
343 "($1, $2, $3, $4, $5);",
344 5),
345 GNUNET_PQ_make_prepare ("truth_select",
346 "SELECT "
347 " method_name"
348 ",encrypted_truth"
349 ",truth_mime"
350 " FROM anastasis_truth"
351 " WHERE truth_uuid =$1;",
352 1),
353 GNUNET_PQ_make_prepare ("latest_recoverydocument_select",
354 "SELECT "
355 " version"
356 ",account_sig"
357 ",recovery_data_hash"
358 ",recovery_data"
359 " FROM anastasis_recoverydocument"
360 " WHERE user_id =$1 "
361 " ORDER BY version DESC"
362 " LIMIT 1;",
363 1),
364 GNUNET_PQ_make_prepare ("latest_recovery_version_select",
365 "SELECT"
366 " version"
367 ",recovery_data_hash"
368 ",expiration_date"
369 " FROM anastasis_recoverydocument"
370 " JOIN anastasis_user USING (user_id)"
371 " WHERE user_id=$1"
372 " ORDER BY version DESC"
373 " LIMIT 1;",
374 1),
375 GNUNET_PQ_make_prepare ("recoverydocument_select",
376 "SELECT "
377 " account_sig"
378 ",recovery_data_hash"
379 ",recovery_data"
380 " FROM anastasis_recoverydocument"
381 " WHERE user_id=$1"
382 " AND version=$2;",
383 2),
384 GNUNET_PQ_make_prepare ("postcounter_select",
385 "SELECT"
386 " post_counter"
387 " FROM anastasis_recdoc_payment"
388 " WHERE user_id=$1"
389 " AND payment_identifier=$2;",
390 2),
391 GNUNET_PQ_make_prepare ("postcounter_update",
392 "UPDATE "
393 "anastasis_recdoc_payment "
394 "SET "
395 "post_counter=$1 "
396 "WHERE user_id =$2 "
397 "AND payment_identifier=$3;",
398 3),
399 GNUNET_PQ_make_prepare ("key_share_select",
400 "SELECT "
401 "key_share_data "
402 "FROM "
403 "anastasis_truth "
404 "WHERE truth_uuid =$1;",
405 1),
406 GNUNET_PQ_make_prepare ("challengecode_insert",
407 "INSERT INTO anastasis_challengecode "
408 "(truth_uuid"
409 ",code"
410 ",creation_date"
411 ",expiration_date"
412 ",retry_counter"
413 ") VALUES "
414 "($1, $2, $3, $4, $5);",
415 5),
416 GNUNET_PQ_make_prepare ("challengecode_select",
417 "SELECT "
418 " code"
419 ",satisfied"
420 " FROM anastasis_challengecode"
421 " WHERE truth_uuid=$1"
422 " AND expiration_date > $2"
423 " AND retry_counter != 0;",
424 2),
425 GNUNET_PQ_make_prepare ("challengecode_set_satisfied",
426 "UPDATE anastasis_challengecode"
427 " SET satisfied=TRUE"
428 " WHERE truth_uuid=$1"
429 " AND code=$2"
430 " AND creation_date IN"
431 " (SELECT creation_date"
432 " FROM anastasis_challengecode"
433 " WHERE truth_uuid=$1"
434 " AND code=$2"
435 " ORDER BY creation_date DESC"
436 " LIMIT 1);",
437 2),
438 GNUNET_PQ_make_prepare ("challengecode_test_satisfied",
439 "SELECT 1 FROM anastasis_challengecode"
440 " WHERE truth_uuid=$1"
441 " AND satisfied=TRUE"
442 " AND code=$2"
443 " AND creation_date >= $3"
444 " LIMIT 1;",
445 3),
446 GNUNET_PQ_make_prepare ("challengecode_select_meta",
447 "SELECT "
448 " code"
449 ",retry_counter"
450 ",retransmission_date"
451 " FROM anastasis_challengecode"
452 " WHERE truth_uuid=$1"
453 " AND expiration_date > $2"
454 " AND creation_date > $3"
455 " ORDER BY creation_date DESC"
456 " LIMIT 1;",
457 2),
458 GNUNET_PQ_make_prepare ("challengecode_update_retry",
459 "UPDATE anastasis_challengecode"
460 " SET retry_counter=retry_counter - 1"
461 " WHERE truth_uuid=$1"
462 " AND code=$2"
463 " AND retry_counter != 0;",
464 1),
465 GNUNET_PQ_make_prepare ("challengepayment_dec_counter",
466 "UPDATE anastasis_challenge_payment"
467 " SET counter=counter - 1"
468 " WHERE truth_uuid=$1"
469 " AND payment_identifier=$2"
470 " AND counter > 0;",
471 2),
472 GNUNET_PQ_make_prepare ("challengecode_mark_sent",
473 "UPDATE anastasis_challengecode"
474 " SET retransmission_date=$3"
475 " WHERE truth_uuid=$1"
476 " AND code=$2"
477 " AND creation_date IN"
478 " (SELECT creation_date"
479 " FROM anastasis_challengecode"
480 " WHERE truth_uuid=$1"
481 " AND code=$2"
482 " ORDER BY creation_date DESC"
483 " LIMIT 1);",
484 3),
485 GNUNET_PQ_make_prepare ("get_last_auth_iban_payment",
486 "SELECT "
487 " wire_reference"
488 " FROM anastasis_auth_iban_in"
489 " WHERE credit_account_details=$1"
490 " ORDER BY wire_reference DESC"
491 " LIMIT 1;",
492 1),
493 GNUNET_PQ_make_prepare ("gc_challengecodes",
494 "DELETE FROM anastasis_challengecode "
495 "WHERE "
496 "expiration_date < $1;",
497 1),
498 GNUNET_PQ_PREPARED_STATEMENT_END
499 };
500
501 pg->conn = GNUNET_PQ_connect_with_cfg (pg->cfg,
502 "stasis-postgres",
503 NULL,
504 NULL,
505 ps);
506 if (NULL == pg->conn)
507 return GNUNET_SYSERR;
508 return GNUNET_OK;
509}
510
511
512/**
103 * Check that the database connection is still up. 513 * Check that the database connection is still up.
104 * 514 *
105 * @param cls a `struct PostgresClosure` with connection to check 515 * @param cls a `struct PostgresClosure` with connection to check
@@ -2281,383 +2691,9 @@ libanastasis_plugin_db_postgres_init (void *cls)
2281 struct GNUNET_CONFIGURATION_Handle *cfg = cls; 2691 struct GNUNET_CONFIGURATION_Handle *cfg = cls;
2282 struct PostgresClosure *pg; 2692 struct PostgresClosure *pg;
2283 struct ANASTASIS_DatabasePlugin *plugin; 2693 struct ANASTASIS_DatabasePlugin *plugin;
2284 struct GNUNET_PQ_PreparedStatement ps[] = {
2285 GNUNET_PQ_make_prepare ("user_insert",
2286 "INSERT INTO anastasis_user "
2287 "(user_id"
2288 ",expiration_date"
2289 ") VALUES "
2290 "($1, $2);",
2291 2),
2292 GNUNET_PQ_make_prepare ("do_commit",
2293 "COMMIT",
2294 0),
2295 GNUNET_PQ_make_prepare ("user_select",
2296 "SELECT"
2297 " expiration_date "
2298 "FROM anastasis_user"
2299 " WHERE user_id=$1"
2300 " FOR UPDATE;",
2301 1),
2302 GNUNET_PQ_make_prepare ("user_update",
2303 "UPDATE anastasis_user"
2304 " SET "
2305 " expiration_date=$1"
2306 " WHERE user_id=$2;",
2307 2),
2308 GNUNET_PQ_make_prepare ("recdoc_payment_insert",
2309 "INSERT INTO anastasis_recdoc_payment "
2310 "(user_id"
2311 ",post_counter"
2312 ",amount_val"
2313 ",amount_frac"
2314 ",payment_identifier"
2315 ",creation_date"
2316 ") VALUES "
2317 "($1, $2, $3, $4, $5, $6);",
2318 6),
2319 GNUNET_PQ_make_prepare ("challenge_payment_insert",
2320 "INSERT INTO anastasis_challenge_payment "
2321 "(truth_uuid"
2322 ",amount_val"
2323 ",amount_frac"
2324 ",payment_identifier"
2325 ",creation_date"
2326 ") VALUES "
2327 "($1, $2, $3, $4, $5);",
2328 5),
2329 GNUNET_PQ_make_prepare ("truth_payment_insert",
2330 "INSERT INTO anastasis_truth_payment "
2331 "(truth_uuid"
2332 ",amount_val"
2333 ",amount_frac"
2334 ",expiration"
2335 ") VALUES "
2336 "($1, $2, $3, $4);",
2337 4),
2338 GNUNET_PQ_make_prepare ("recdoc_payment_done",
2339 "UPDATE anastasis_recdoc_payment "
2340 "SET"
2341 " paid=TRUE "
2342 "WHERE"
2343 " payment_identifier=$1"
2344 " AND"
2345 " user_id=$2"
2346 " AND"
2347 " paid=FALSE;",
2348 2),
2349 GNUNET_PQ_make_prepare ("challenge_refund_update",
2350 "UPDATE anastasis_challenge_payment "
2351 "SET"
2352 " refunded=TRUE "
2353 "WHERE"
2354 " payment_identifier=$1"
2355 " AND"
2356 " paid=TRUE"
2357 " AND"
2358 " truth_uuid=$2;",
2359 2),
2360 GNUNET_PQ_make_prepare ("challenge_payment_done",
2361 "UPDATE anastasis_challenge_payment "
2362 "SET"
2363 " paid=TRUE "
2364 "WHERE"
2365 " payment_identifier=$1"
2366 " AND"
2367 " refunded=FALSE"
2368 " AND"
2369 " truth_uuid=$2"
2370 " AND"
2371 " paid=FALSE;",
2372 2),
2373 GNUNET_PQ_make_prepare ("recdoc_payment_select",
2374 "SELECT"
2375 " creation_date"
2376 ",post_counter"
2377 ",amount_val"
2378 ",amount_frac"
2379 ",paid"
2380 " FROM anastasis_recdoc_payment"
2381 " WHERE payment_identifier=$1;",
2382 1),
2383 GNUNET_PQ_make_prepare ("truth_payment_select",
2384 "SELECT"
2385 " expiration"
2386 " FROM anastasis_truth_payment"
2387 " WHERE truth_uuid=$1"
2388 " AND expiration>$2;",
2389 2),
2390 GNUNET_PQ_make_prepare ("challenge_payment_select",
2391 "SELECT"
2392 " creation_date"
2393 ",amount_val"
2394 ",amount_frac"
2395 ",paid"
2396 " FROM anastasis_challenge_payment"
2397 " WHERE payment_identifier=$1"
2398 " AND truth_uuid=$2"
2399 " AND refunded=FALSE"
2400 " AND counter>0;",
2401 1),
2402 GNUNET_PQ_make_prepare ("challenge_pending_payment_select",
2403 "SELECT"
2404 " creation_date"
2405 ",payment_identifier"
2406 ",amount_val"
2407 ",amount_frac"
2408 " FROM anastasis_challenge_payment"
2409 " WHERE"
2410 " paid=FALSE"
2411 " AND"
2412 " refunded=FALSE"
2413 " AND"
2414 " truth_uuid=$1"
2415 " AND"
2416 " creation_date > $2;",
2417 1),
2418 GNUNET_PQ_make_prepare ("recdoc_payments_select",
2419 "SELECT"
2420 " user_id"
2421 ",payment_identifier"
2422 ",amount_val"
2423 ",amount_frac"
2424 " FROM anastasis_recdoc_payment"
2425 " WHERE paid=FALSE;",
2426 0),
2427 GNUNET_PQ_make_prepare ("gc_accounts",
2428 "DELETE FROM anastasis_user "
2429 "WHERE"
2430 " expiration_date < $1;",
2431 1),
2432 GNUNET_PQ_make_prepare ("gc_recdoc_pending_payments",
2433 "DELETE FROM anastasis_recdoc_payment "
2434 "WHERE"
2435 " paid=FALSE"
2436 " AND"
2437 " creation_date < $1;",
2438 1),
2439 GNUNET_PQ_make_prepare ("gc_challenge_pending_payments",
2440 "DELETE FROM anastasis_challenge_payment "
2441 "WHERE"
2442 " (paid=FALSE"
2443 " OR"
2444 " refunded=TRUE)"
2445 " AND"
2446 " creation_date < $1;",
2447 1),
2448 GNUNET_PQ_make_prepare ("truth_insert",
2449 "INSERT INTO anastasis_truth "
2450 "(truth_uuid"
2451 ",key_share_data"
2452 ",method_name"
2453 ",encrypted_truth"
2454 ",truth_mime"
2455 ",expiration"
2456 ") VALUES "
2457 "($1, $2, $3, $4, $5, $6);",
2458 6),
2459
2460 GNUNET_PQ_make_prepare ("test_auth_iban_payment",
2461 "SELECT"
2462 " credit_val"
2463 ",credit_frac"
2464 ",wire_subject"
2465 " FROM anastasis_auth_iban_in"
2466 " WHERE debit_account_details=$1"
2467 " AND execution_date>=$2;",
2468 2),
2469 GNUNET_PQ_make_prepare ("store_auth_iban_payment_details",
2470 "INSERT INTO anastasis_auth_iban_in "
2471 "(wire_reference"
2472 ",wire_subject"
2473 ",credit_val"
2474 ",credit_frac"
2475 ",debit_account_details"
2476 ",credit_account_details"
2477 ",execution_date"
2478 ") VALUES "
2479 "($1, $2, $3, $4, $5, $6, $7);",
2480 7),
2481
2482
2483 GNUNET_PQ_make_prepare ("recovery_document_insert",
2484 "INSERT INTO anastasis_recoverydocument "
2485 "(user_id"
2486 ",version"
2487 ",account_sig"
2488 ",recovery_data_hash"
2489 ",recovery_data"
2490 ") VALUES "
2491 "($1, $2, $3, $4, $5);",
2492 5),
2493 GNUNET_PQ_make_prepare ("truth_select",
2494 "SELECT "
2495 " method_name"
2496 ",encrypted_truth"
2497 ",truth_mime"
2498 " FROM anastasis_truth"
2499 " WHERE truth_uuid =$1;",
2500 1),
2501 GNUNET_PQ_make_prepare ("latest_recoverydocument_select",
2502 "SELECT "
2503 " version"
2504 ",account_sig"
2505 ",recovery_data_hash"
2506 ",recovery_data"
2507 " FROM anastasis_recoverydocument"
2508 " WHERE user_id =$1 "
2509 " ORDER BY version DESC"
2510 " LIMIT 1;",
2511 1),
2512 GNUNET_PQ_make_prepare ("latest_recovery_version_select",
2513 "SELECT"
2514 " version"
2515 ",recovery_data_hash"
2516 ",expiration_date"
2517 " FROM anastasis_recoverydocument"
2518 " JOIN anastasis_user USING (user_id)"
2519 " WHERE user_id=$1"
2520 " ORDER BY version DESC"
2521 " LIMIT 1;",
2522 1),
2523 GNUNET_PQ_make_prepare ("recoverydocument_select",
2524 "SELECT "
2525 " account_sig"
2526 ",recovery_data_hash"
2527 ",recovery_data"
2528 " FROM anastasis_recoverydocument"
2529 " WHERE user_id=$1"
2530 " AND version=$2;",
2531 2),
2532 GNUNET_PQ_make_prepare ("postcounter_select",
2533 "SELECT"
2534 " post_counter"
2535 " FROM anastasis_recdoc_payment"
2536 " WHERE user_id=$1"
2537 " AND payment_identifier=$2;",
2538 2),
2539 GNUNET_PQ_make_prepare ("postcounter_update",
2540 "UPDATE "
2541 "anastasis_recdoc_payment "
2542 "SET "
2543 "post_counter=$1 "
2544 "WHERE user_id =$2 "
2545 "AND payment_identifier=$3;",
2546 3),
2547 GNUNET_PQ_make_prepare ("key_share_select",
2548 "SELECT "
2549 "key_share_data "
2550 "FROM "
2551 "anastasis_truth "
2552 "WHERE truth_uuid =$1;",
2553 1),
2554 GNUNET_PQ_make_prepare ("challengecode_insert",
2555 "INSERT INTO anastasis_challengecode "
2556 "(truth_uuid"
2557 ",code"
2558 ",creation_date"
2559 ",expiration_date"
2560 ",retry_counter"
2561 ") VALUES "
2562 "($1, $2, $3, $4, $5);",
2563 5),
2564 GNUNET_PQ_make_prepare ("challengecode_select",
2565 "SELECT "
2566 " code"
2567 ",satisfied"
2568 " FROM anastasis_challengecode"
2569 " WHERE truth_uuid=$1"
2570 " AND expiration_date > $2"
2571 " AND retry_counter != 0;",
2572 2),
2573 GNUNET_PQ_make_prepare ("challengecode_set_satisfied",
2574 "UPDATE anastasis_challengecode"
2575 " SET satisfied=TRUE"
2576 " WHERE truth_uuid=$1"
2577 " AND code=$2"
2578 " AND creation_date IN"
2579 " (SELECT creation_date"
2580 " FROM anastasis_challengecode"
2581 " WHERE truth_uuid=$1"
2582 " AND code=$2"
2583 " ORDER BY creation_date DESC"
2584 " LIMIT 1);",
2585 2),
2586 GNUNET_PQ_make_prepare ("challengecode_test_satisfied",
2587 "SELECT 1 FROM anastasis_challengecode"
2588 " WHERE truth_uuid=$1"
2589 " AND satisfied=TRUE"
2590 " AND code=$2"
2591 " AND creation_date >= $3"
2592 " LIMIT 1;",
2593 3),
2594 GNUNET_PQ_make_prepare ("challengecode_select_meta",
2595 "SELECT "
2596 " code"
2597 ",retry_counter"
2598 ",retransmission_date"
2599 " FROM anastasis_challengecode"
2600 " WHERE truth_uuid=$1"
2601 " AND expiration_date > $2"
2602 " AND creation_date > $3"
2603 " ORDER BY creation_date DESC"
2604 " LIMIT 1;",
2605 2),
2606 GNUNET_PQ_make_prepare ("challengecode_update_retry",
2607 "UPDATE anastasis_challengecode"
2608 " SET retry_counter=retry_counter - 1"
2609 " WHERE truth_uuid=$1"
2610 " AND code=$2"
2611 " AND retry_counter != 0;",
2612 1),
2613 GNUNET_PQ_make_prepare ("challengepayment_dec_counter",
2614 "UPDATE anastasis_challenge_payment"
2615 " SET counter=counter - 1"
2616 " WHERE truth_uuid=$1"
2617 " AND payment_identifier=$2"
2618 " AND counter > 0;",
2619 2),
2620 GNUNET_PQ_make_prepare ("challengecode_mark_sent",
2621 "UPDATE anastasis_challengecode"
2622 " SET retransmission_date=$3"
2623 " WHERE truth_uuid=$1"
2624 " AND code=$2"
2625 " AND creation_date IN"
2626 " (SELECT creation_date"
2627 " FROM anastasis_challengecode"
2628 " WHERE truth_uuid=$1"
2629 " AND code=$2"
2630 " ORDER BY creation_date DESC"
2631 " LIMIT 1);",
2632 3),
2633 GNUNET_PQ_make_prepare ("get_last_auth_iban_payment",
2634 "SELECT "
2635 " wire_reference"
2636 " FROM anastasis_auth_iban_in"
2637 " WHERE credit_account_details=$1"
2638 " ORDER BY wire_reference DESC"
2639 " LIMIT 1;",
2640 1),
2641 GNUNET_PQ_make_prepare ("gc_challengecodes",
2642 "DELETE FROM anastasis_challengecode "
2643 "WHERE "
2644 "expiration_date < $1;",
2645 1),
2646 GNUNET_PQ_PREPARED_STATEMENT_END
2647 };
2648 2694
2649 pg = GNUNET_new (struct PostgresClosure); 2695 pg = GNUNET_new (struct PostgresClosure);
2650 pg->cfg = cfg; 2696 pg->cfg = cfg;
2651 pg->conn = GNUNET_PQ_connect_with_cfg (cfg,
2652 "stasis-postgres",
2653 "stasis-",
2654 NULL,
2655 ps);
2656 if (NULL == pg->conn)
2657 {
2658 GNUNET_free (pg);
2659 return NULL;
2660 }
2661 if (GNUNET_OK != 2697 if (GNUNET_OK !=
2662 GNUNET_CONFIGURATION_get_value_string (cfg, 2698 GNUNET_CONFIGURATION_get_value_string (cfg,
2663 "taler", 2699 "taler",
@@ -2673,6 +2709,8 @@ libanastasis_plugin_db_postgres_init (void *cls)
2673 } 2709 }
2674 plugin = GNUNET_new (struct ANASTASIS_DatabasePlugin); 2710 plugin = GNUNET_new (struct ANASTASIS_DatabasePlugin);
2675 plugin->cls = pg; 2711 plugin->cls = pg;
2712 plugin->connect = &postgres_connect;
2713 plugin->create_tables = &postgres_create_tables;
2676 plugin->drop_tables = &postgres_drop_tables; 2714 plugin->drop_tables = &postgres_drop_tables;
2677 plugin->gc = &postgres_gc; 2715 plugin->gc = &postgres_gc;
2678 plugin->preflight = &postgres_preflight; 2716 plugin->preflight = &postgres_preflight;
diff --git a/src/stasis/test_anastasis_db.c b/src/stasis/test_anastasis_db.c
index 204307a..1ec9770 100644
--- a/src/stasis/test_anastasis_db.c
+++ b/src/stasis/test_anastasis_db.c
@@ -92,15 +92,15 @@ run (void *cls)
92 result = 77; 92 result = 77;
93 return; 93 return;
94 } 94 }
95 if (GNUNET_OK != plugin->drop_tables (plugin->cls)) 95 (void) plugin->drop_tables (plugin->cls);
96 if (GNUNET_OK !=
97 plugin->create_tables (plugin->cls))
96 { 98 {
97 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
98 "Dropping tables failed\n");
99 result = 77; 99 result = 77;
100 return; 100 return;
101 } 101 }
102 ANASTASIS_DB_plugin_unload (plugin); 102 if (GNUNET_OK !=
103 if (NULL == (plugin = ANASTASIS_DB_plugin_load (cfg))) 103 plugin->connect (plugin->cls))
104 { 104 {
105 result = 77; 105 result = 77;
106 return; 106 return;