anastasis

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

iterate_recovery_meta_data.c (4636B)


      1 /*
      2   This file is part of Anastasis
      3   Copyright (C) 2020-2022 Anastasis SARL
      4 
      5   Anastasis is free software; you can redistribute it and/or modify it under the
      6   terms of the GNU Affero General Public License as published by the Free Software
      7   Foundation; either version 3, or (at your option) any later version.
      8 
      9   Anastasis 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 Affero General Public License for more details.
     12 
     13   You should have received a copy of the GNU Affero General Public License along with
     14   Anastasis; see the file COPYING.GPL.  If not, see <http://www.gnu.org/licenses/>
     15 */
     16 /**
     17  * @file stasis/iterate_recovery_meta_data.c
     18  * @brief Anastasis database: iterate recovery meta data
     19  * @author Christian Grothoff
     20  */
     21 #include "platform.h"
     22 #include "anastasis-db_pg.h"
     23 #include "anastasis/anastasis-database/iterate_recovery_meta_data.h"
     24 #include "anastasis/anastasis-database/transaction.h"
     25 #include "anastasis/anastasis-database/preflight.h"
     26 #include <taler/taler_pq_lib.h>
     27 
     28 
     29 struct MetaIteratorContext
     30 {
     31   /**
     32    * Function to call on each result.
     33    */
     34   ANASTASIS_DB_RecoveryMetaCallback cb;
     35 
     36   /**
     37    * Closure for @e cb.
     38    */
     39   void *cb_cls;
     40 
     41   /**
     42    * Set to true on database failure.
     43    */
     44   bool db_failure;
     45 };
     46 
     47 
     48 /**
     49  * Helper function for #postgres_get_recovery_meta_data().
     50  * To be called with the results of a SELECT statement
     51  * that has returned @a num_results results.
     52  *
     53  * @param cls closure of type `struct MetaIteratorContext *`
     54  * @param result the postgres result
     55  * @param num_results the number of results in @a result
     56  */
     57 static void
     58 meta_iterator (void *cls,
     59                PGresult *result,
     60                unsigned int num_results)
     61 {
     62   struct MetaIteratorContext *ctx = cls;
     63 
     64   for (unsigned int i = 0; i<num_results; i++)
     65   {
     66     uint32_t version;
     67     void *meta_data;
     68     size_t meta_data_size;
     69     struct GNUNET_TIME_Timestamp ts;
     70     struct GNUNET_PQ_ResultSpec rs[] = {
     71       GNUNET_PQ_result_spec_uint32 ("version",
     72                                     &version),
     73       GNUNET_PQ_result_spec_timestamp ("creation_date",
     74                                        &ts),
     75       GNUNET_PQ_result_spec_variable_size ("recovery_meta_data",
     76                                            &meta_data,
     77                                            &meta_data_size),
     78       GNUNET_PQ_result_spec_end
     79     };
     80     enum GNUNET_GenericReturnValue ret;
     81 
     82     if (GNUNET_OK !=
     83         GNUNET_PQ_extract_result (result,
     84                                   rs,
     85                                   i))
     86     {
     87       GNUNET_break (0);
     88       ctx->db_failure = true;
     89       return;
     90     }
     91     ret = ctx->cb (ctx->cb_cls,
     92                    version,
     93                    ts,
     94                    meta_data,
     95                    meta_data_size);
     96     GNUNET_PQ_cleanup_result (rs);
     97     if (GNUNET_OK != ret)
     98       break;
     99   }
    100 }
    101 
    102 
    103 /**
    104  * Fetch recovery document meta data for user. Returns
    105  * meta data in descending order from @a max_version.
    106  * The size of the result set may be limited.
    107  *
    108  * @param account_pub public key of the user's account
    109  * @param max_version the maximum version number the user requests
    110  * @param cb function to call on each result
    111  * @param cb_cls closure for @a cb
    112  * @return transaction status
    113  */
    114 enum GNUNET_DB_QueryStatus
    115 ANASTASIS_DB_iterate_recovery_meta_data (
    116   const struct ANASTASIS_CRYPTO_AccountPublicKeyP *account_pub,
    117   uint32_t max_version,
    118   ANASTASIS_DB_RecoveryMetaCallback cb,
    119   void *cb_cls)
    120 {
    121   struct MetaIteratorContext ctx = {
    122     .cb = cb,
    123     .cb_cls = cb_cls
    124   };
    125   struct GNUNET_PQ_QueryParam params[] = {
    126     GNUNET_PQ_query_param_auto_from_type (account_pub),
    127     GNUNET_PQ_query_param_uint32 (&max_version),
    128     GNUNET_PQ_query_param_end
    129   };
    130   enum GNUNET_DB_QueryStatus qs;
    131 
    132   PREPARE ("iterate_recovery_meta_data",
    133            "SELECT "
    134            " version"
    135            ",creation_date"
    136            ",recovery_meta_data"
    137            " FROM anastasis_recoverydocument"
    138            " WHERE user_id=$1"
    139            "   AND version < $2"
    140            " ORDER BY version DESC"
    141            " LIMIT 1000;");
    142   qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    143                                              "iterate_recovery_meta_data",
    144                                              params,
    145                                              &meta_iterator,
    146                                              &ctx);
    147   if (qs < 0)
    148     return qs;
    149   if (ctx.db_failure)
    150     return GNUNET_DB_STATUS_HARD_ERROR;
    151   return qs;
    152 }
    153 
    154 
    155 /* end of iterate_recovery_meta_data.c */