challenge_set_address_and_pin.c (5629B)
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/challenge_set_address_and_pin.c 18 * @brief Implementation of the challenge_set_address_and_pin 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 "challenge_set_address_and_pin.h" 26 #include "pg_helper.h" 27 28 29 enum GNUNET_DB_QueryStatus 30 CHALLENGERDB_challenge_set_address_and_pin (struct 31 CHALLENGERDB_PostgresContext *ctx, 32 const struct 33 CHALLENGER_ValidationNonceP *nonce, 34 const json_t *address, 35 struct GNUNET_TIME_Relative 36 retransmission_frequency, 37 uint32_t *tan, 38 char **state, 39 struct GNUNET_TIME_Absolute * 40 last_tx_time, 41 uint32_t *auth_attempts_left, 42 bool *pin_transmit, 43 char **client_redirect_uri, 44 bool *address_refused, 45 bool *solved) 46 { 47 struct GNUNET_TIME_Absolute now 48 = GNUNET_TIME_absolute_get (); 49 /* We must gate retransmission on 50 last_tx_time + retransmission_frequency <= now 51 but 'last_tx_time' is only read (under FOR UPDATE) inside the stored 52 procedure, so we cannot form that sum here. We therefore pass the 53 equivalent, inverted form: the *newest* 'last_tx_time' for which a 54 retransmission is still due. The SQL then merely checks 55 last_tx_time <= retransmit_cutoff. 56 Note that this is deliberately a subtraction from 'now', not 57 'now + retransmission_frequency': the value is compared against a 58 timestamp in the *past*. GNUNET_TIME_absolute_subtract() saturates at 59 zero rather than underflowing, so a FOREVER frequency degrades to 60 "never *re*transmit" (the initial PIN, sent when last_tx_time is still 0, 61 is unaffected). */ 62 struct GNUNET_TIME_Absolute retransmit_cutoff 63 = GNUNET_TIME_absolute_subtract (now, 64 retransmission_frequency); 65 struct GNUNET_PQ_QueryParam params[] = { 66 GNUNET_PQ_query_param_auto_from_type (nonce), 67 TALER_PQ_query_param_json (address), 68 GNUNET_PQ_query_param_absolute_time (&retransmit_cutoff), 69 GNUNET_PQ_query_param_absolute_time (&now), 70 GNUNET_PQ_query_param_uint32 (tan), 71 GNUNET_PQ_query_param_end 72 }; 73 bool not_found; 74 struct GNUNET_PQ_ResultSpec rs[] = { 75 GNUNET_PQ_result_spec_bool ("not_found", 76 ¬_found), 77 GNUNET_PQ_result_spec_absolute_time ("last_tx_time", 78 last_tx_time), 79 GNUNET_PQ_result_spec_uint32 ("last_pin", 80 tan), 81 GNUNET_PQ_result_spec_bool ("pin_transmit", 82 pin_transmit), 83 GNUNET_PQ_result_spec_uint32 ("auth_attempts_left", 84 auth_attempts_left), 85 GNUNET_PQ_result_spec_allow_null ( 86 GNUNET_PQ_result_spec_string ("client_redirect_uri", 87 client_redirect_uri), 88 NULL), 89 GNUNET_PQ_result_spec_allow_null ( 90 GNUNET_PQ_result_spec_string ("state", 91 state), 92 NULL), 93 GNUNET_PQ_result_spec_bool ("address_refused", 94 address_refused), 95 GNUNET_PQ_result_spec_bool ("solved", 96 solved), 97 GNUNET_PQ_result_spec_end 98 }; 99 enum GNUNET_DB_QueryStatus qs; 100 101 *client_redirect_uri = NULL; 102 PREPARE (ctx, 103 "do_challenge_set_address_and_pin", 104 "SELECT " 105 " out_not_found AS not_found" 106 ",out_last_tx_time AS last_tx_time" 107 ",out_pin_transmit AS pin_transmit" 108 ",out_last_pin AS last_pin" 109 ",out_state AS state" 110 ",out_auth_attempts_left AS auth_attempts_left" 111 ",out_client_redirect_uri AS client_redirect_uri" 112 ",out_address_refused AS address_refused" 113 ",out_solved AS solved" 114 " FROM challenger_do_challenge_set_address_and_pin" 115 " ($1,$2,$3,$4,$5);"); 116 qs = GNUNET_PQ_eval_prepared_singleton_select (ctx->conn, 117 "do_challenge_set_address_and_pin", 118 params, 119 rs); 120 if (qs <= 0) 121 return qs; 122 if (not_found) 123 return GNUNET_DB_STATUS_SUCCESS_NO_RESULTS; 124 return qs; 125 }