pg_iterate_active_signing_keys.c (4002B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2024 Taler Systems SA 4 5 TALER 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 TALER 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 TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 /** 17 * @file donaudb/pg_iterate_active_signing_keys.c 18 * @brief Implementation of the iterate_active_signing_keys function for Postgres 19 * @author Christian Grothoff 20 */ 21 #include <donau_config.h> 22 #include <taler/taler_error_codes.h> 23 #include <taler/taler_dbevents.h> 24 #include <taler/taler_pq_lib.h> 25 #include "pg_iterate_active_signing_keys.h" 26 #include "pg_helper.h" 27 28 /** 29 * Closure for #signkeys_cb_helper(). 30 */ 31 struct IterateActiveSigningKeysContext 32 { 33 /** 34 * Function to call per result. 35 */ 36 DONAUDB_IterateActiveSigningKeysCallback cb; 37 38 /** 39 * Closure for @e cb. 40 */ 41 void *cb_cls; 42 43 /** 44 * Flag set to #GNUNET_OK as long as everything is fine. 45 */ 46 enum GNUNET_GenericReturnValue status; 47 48 }; 49 50 /** 51 * Invoke the callback for each result. 52 * 53 * @param cls a `struct MissingWireContext *` 54 * @param result SQL result 55 * @param num_results number of rows in @a result 56 */ 57 static void 58 signkeys_cb_helper (void *cls, 59 PGresult *result, 60 unsigned int num_results) 61 { 62 struct IterateActiveSigningKeysContext *ctx = cls; 63 64 for (unsigned int i = 0; i < num_results; i++) 65 { 66 struct DONAU_DonauPublicKeyP donau_pub; 67 struct DONAUDB_SignkeyMetaData meta; 68 69 struct GNUNET_PQ_ResultSpec rs[] = { 70 GNUNET_PQ_result_spec_auto_from_type ("donau_pub", 71 &donau_pub), 72 GNUNET_PQ_result_spec_timestamp ("valid_from", 73 &meta.valid_from), 74 GNUNET_PQ_result_spec_timestamp ("expire_sign", 75 &meta.expire_sign), 76 GNUNET_PQ_result_spec_timestamp ("expire_legal", 77 &meta.expire_legal), 78 GNUNET_PQ_result_spec_end 79 }; 80 81 if (GNUNET_OK != 82 GNUNET_PQ_extract_result (result, 83 rs, 84 i)) 85 { 86 GNUNET_break (0); 87 ctx->status = GNUNET_SYSERR; 88 return; 89 } 90 ctx->cb (ctx->cb_cls, 91 &donau_pub, 92 &meta); 93 GNUNET_PQ_cleanup_result (rs); 94 } 95 } 96 97 98 enum GNUNET_DB_QueryStatus 99 DH_PG_iterate_active_signing_keys (void *cls, 100 DONAUDB_IterateActiveSigningKeysCallback cb, 101 void *cb_cls) 102 { 103 struct PostgresClosure *pg = cls; 104 struct GNUNET_TIME_Absolute now = {0}; 105 struct IterateActiveSigningKeysContext ctx = { 106 .cb = cb, 107 .cb_cls = cb_cls, 108 .status = GNUNET_OK 109 }; 110 struct GNUNET_PQ_QueryParam params[] = { 111 GNUNET_PQ_query_param_absolute_time (&now), 112 GNUNET_PQ_query_param_end 113 }; 114 enum GNUNET_DB_QueryStatus qs; 115 116 PREPARE (pg, 117 "iterate_active_signing_keys", 118 "SELECT" 119 " donau_pub" 120 ",valid_from" 121 ",expire_sign" 122 ",expire_legal" 123 " FROM donau_sign_keys dsk" 124 " WHERE" 125 " expire_sign > $1"); 126 qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn, 127 "iterate_active_signing_keys", 128 params, 129 &signkeys_cb_helper, 130 &ctx); 131 if (GNUNET_OK != ctx.status) 132 return GNUNET_DB_STATUS_HARD_ERROR; 133 return qs; 134 }