do_challenge_address.c (4933B)
1 /* 2 This file is part of Challenger 3 Copyright (C) 2023 Taler Systems SA 4 5 Challenger is free software; you can redistribute it and/or modify it under the 6 terms of the GNU General Public License as published by the Free Software 7 Foundation; either version 3, or (at your option) any later version. 8 9 Challenger is distributed in the hope that it will be useful, but WITHOUT ANY 10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 A PARTICULAR PURPOSE. See the GNU General Public License for more details. 12 13 You should have received a copy of the GNU General Public License along with 14 Challenger; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 /** 17 * @file src/challengerdb/do_challenge_address.c 18 * @brief Implementation of the do_challenge_address function for Postgres 19 * @author Christian Grothoff 20 */ 21 #include "platform.h" 22 #include <taler/taler_error_codes.h> 23 #include <taler/taler_dbevents.h> 24 #include <taler/taler_pq_lib.h> 25 #include "do_challenge_address.h" 26 #include "pg_helper.h" 27 28 29 enum GNUNET_DB_QueryStatus 30 CHALLENGERDB_do_challenge_address ( 31 struct CHALLENGERDB_PostgresContext *ctx, 32 const struct CHALLENGER_ValidationNonceP *nonce, 33 const json_t *address, 34 struct GNUNET_TIME_Relative retransmission_frequency, 35 uint32_t *tan, 36 char **state, 37 struct GNUNET_TIME_Absolute *last_tx_time, 38 uint32_t *auth_attempts_left, 39 bool *pin_transmit, 40 char **client_redirect_uri, 41 bool *address_refused, 42 bool *solved) 43 { 44 struct GNUNET_TIME_Absolute now 45 = GNUNET_TIME_absolute_get (); 46 /* We must gate retransmission on 47 last_tx_time + retransmission_frequency <= now 48 but 'last_tx_time' is only read (under FOR UPDATE) inside the stored 49 procedure, so we cannot form that sum here. We therefore pass the 50 equivalent, inverted form: the *newest* 'last_tx_time' for which a 51 retransmission is still due. The SQL then merely checks 52 last_tx_time <= retransmit_cutoff. 53 Note that this is deliberately a subtraction from 'now', not 54 'now + retransmission_frequency': the value is compared against a 55 timestamp in the *past*. GNUNET_TIME_absolute_subtract() saturates at 56 zero rather than underflowing, so a FOREVER frequency degrades to 57 "never *re*transmit" (the initial PIN, sent when last_tx_time is still 0, 58 is unaffected). */ 59 struct GNUNET_TIME_Absolute retransmit_cutoff 60 = GNUNET_TIME_absolute_subtract (now, 61 retransmission_frequency); 62 struct GNUNET_PQ_QueryParam params[] = { 63 GNUNET_PQ_query_param_auto_from_type (nonce), 64 TALER_PQ_query_param_json (address), 65 GNUNET_PQ_query_param_absolute_time (&retransmit_cutoff), 66 GNUNET_PQ_query_param_absolute_time (&now), 67 GNUNET_PQ_query_param_uint32 (tan), 68 GNUNET_PQ_query_param_end 69 }; 70 bool not_found; 71 struct GNUNET_PQ_ResultSpec rs[] = { 72 GNUNET_PQ_result_spec_bool ("not_found", 73 ¬_found), 74 GNUNET_PQ_result_spec_absolute_time ("last_tx_time", 75 last_tx_time), 76 GNUNET_PQ_result_spec_uint32 ("last_pin", 77 tan), 78 GNUNET_PQ_result_spec_bool ("pin_transmit", 79 pin_transmit), 80 GNUNET_PQ_result_spec_uint32 ("auth_attempts_left", 81 auth_attempts_left), 82 GNUNET_PQ_result_spec_allow_null ( 83 GNUNET_PQ_result_spec_string ("client_redirect_uri", 84 client_redirect_uri), 85 NULL), 86 GNUNET_PQ_result_spec_allow_null ( 87 GNUNET_PQ_result_spec_string ("state", 88 state), 89 NULL), 90 GNUNET_PQ_result_spec_bool ("address_refused", 91 address_refused), 92 GNUNET_PQ_result_spec_bool ("solved", 93 solved), 94 GNUNET_PQ_result_spec_end 95 }; 96 enum GNUNET_DB_QueryStatus qs; 97 98 *client_redirect_uri = NULL; 99 PREPARE (ctx, 100 "do_challenge_address", 101 "SELECT " 102 " out_not_found AS not_found" 103 ",out_last_tx_time AS last_tx_time" 104 ",out_pin_transmit AS pin_transmit" 105 ",out_last_pin AS last_pin" 106 ",out_state AS state" 107 ",out_auth_attempts_left AS auth_attempts_left" 108 ",out_client_redirect_uri AS client_redirect_uri" 109 ",out_address_refused AS address_refused" 110 ",out_solved AS solved" 111 " FROM challenger_do_challenge_set_address_and_pin" 112 " ($1,$2,$3,$4,$5);"); 113 qs = GNUNET_PQ_eval_prepared_singleton_select (ctx->conn, 114 "do_challenge_address", 115 params, 116 rs); 117 if (qs <= 0) 118 return qs; 119 if (not_found) 120 return GNUNET_DB_STATUS_SUCCESS_NO_RESULTS; 121 return qs; 122 }