anastasis

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

anastasis-db_get_recovery_meta_data.c (4668B)


      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/anastasis-db_get_recovery_meta_data.c
     18  * @brief Anastasis database: get recovery meta data
     19  * @author Christian Grothoff
     20  */
     21 #include "platform.h"
     22 #include "anastasis-db_pg.h"
     23 #include "anastasis/anastasis-database/get_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 cls closure
    109  * @param account_pub public key of the user's account
    110  * @param max_version the maximum version number the user requests
    111  * @param cb function to call on each result
    112  * @param cb_cls closure for @a cb
    113  * @return transaction status
    114  */
    115 enum GNUNET_DB_QueryStatus
    116 ANASTASIS_DB_get_recovery_meta_data (
    117   const struct ANASTASIS_CRYPTO_AccountPublicKeyP *account_pub,
    118   uint32_t max_version,
    119   ANASTASIS_DB_RecoveryMetaCallback cb,
    120   void *cb_cls)
    121 {
    122   struct MetaIteratorContext ctx = {
    123     .cb = cb,
    124     .cb_cls = cb_cls
    125   };
    126   struct GNUNET_PQ_QueryParam params[] = {
    127     GNUNET_PQ_query_param_auto_from_type (account_pub),
    128     GNUNET_PQ_query_param_uint32 (&max_version),
    129     GNUNET_PQ_query_param_end
    130   };
    131   enum GNUNET_DB_QueryStatus qs;
    132 
    133   PREPARE ("recoverydocument_select_meta",
    134            "SELECT "
    135            " version"
    136            ",creation_date"
    137            ",recovery_meta_data"
    138            " FROM anastasis_recoverydocument"
    139            " WHERE user_id=$1"
    140            "   AND version < $2"
    141            " ORDER BY version DESC"
    142            " LIMIT 1000;");
    143   qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    144                                              "recoverydocument_select_meta",
    145                                              params,
    146                                              &meta_iterator,
    147                                              &ctx);
    148   if (qs < 0)
    149     return qs;
    150   if (ctx.db_failure)
    151     return GNUNET_DB_STATUS_HARD_ERROR;
    152   return qs;
    153 }
    154 
    155 
    156 /* end of anastasis-db_get_recovery_meta_data.c */