anastasis

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

anastasis-db_lookup_account.c (4755B)


      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_lookup_account.c
     18  * @brief Anastasis database: lookup account
     19  * @author Christian Grothoff
     20  */
     21 #include "platform.h"
     22 #include "anastasis-db_pg.h"
     23 #include "anastasis/anastasis-database/lookup_account.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 /**
     30  * Check if an account exists, and if so, return the
     31  * current @a recovery_document_hash.
     32  *
     33  * @param account_pub account identifier
     34  * @param[out] paid_until until when is the account paid up?
     35  * @param[out] recovery_data_hash set to hash of @a recovery document
     36  * @param[out] version set to the recovery policy version
     37  * @return transaction status
     38  */
     39 enum ANASTASIS_DB_AccountStatus
     40 ANASTASIS_DB_lookup_account (
     41   const struct ANASTASIS_CRYPTO_AccountPublicKeyP *account_pub,
     42   struct GNUNET_TIME_Timestamp *paid_until,
     43   struct GNUNET_HashCode *recovery_data_hash,
     44   uint32_t *version)
     45 {
     46   struct GNUNET_PQ_QueryParam params[] = {
     47     GNUNET_PQ_query_param_auto_from_type (account_pub),
     48     GNUNET_PQ_query_param_end
     49   };
     50   enum GNUNET_DB_QueryStatus qs;
     51 
     52   PREPARE ("user_select2",
     53            "SELECT"
     54            " expiration_date "
     55            "FROM anastasis_user"
     56            " WHERE user_id=$1"
     57            " FOR UPDATE;");
     58   PREPARE ("latest_recovery_version_select",
     59            "SELECT"
     60            " version"
     61            ",recovery_data_hash"
     62            ",expiration_date"
     63            " FROM anastasis_recoverydocument"
     64            " JOIN anastasis_user USING (user_id)"
     65            " WHERE user_id=$1"
     66            " ORDER BY version DESC"
     67            " LIMIT 1;");
     68   GNUNET_break (GNUNET_OK ==
     69                 ANASTASIS_DB_preflight ());
     70   {
     71     struct GNUNET_PQ_ResultSpec rs[] = {
     72       GNUNET_PQ_result_spec_timestamp ("expiration_date",
     73                                        paid_until),
     74       GNUNET_PQ_result_spec_auto_from_type ("recovery_data_hash",
     75                                             recovery_data_hash),
     76       GNUNET_PQ_result_spec_uint32 ("version",
     77                                     version),
     78       GNUNET_PQ_result_spec_end
     79     };
     80 
     81     qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
     82                                                    "latest_recovery_version_select",
     83                                                    params,
     84                                                    rs);
     85   }
     86   switch (qs)
     87   {
     88   case GNUNET_DB_STATUS_HARD_ERROR:
     89     return ANASTASIS_DB_ACCOUNT_STATUS_HARD_ERROR;
     90   case GNUNET_DB_STATUS_SOFT_ERROR:
     91     GNUNET_break (0);
     92     return ANASTASIS_DB_ACCOUNT_STATUS_HARD_ERROR;
     93   case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
     94     break; /* handle interesting case below */
     95   case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
     96     return ANASTASIS_DB_ACCOUNT_STATUS_VALID_HASH_RETURNED;
     97   }
     98 
     99   /* check if account exists */
    100   {
    101     struct GNUNET_PQ_ResultSpec rs[] = {
    102       GNUNET_PQ_result_spec_timestamp ("expiration_date",
    103                                        paid_until),
    104       GNUNET_PQ_result_spec_end
    105     };
    106 
    107     qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
    108                                                    "user_select2",
    109                                                    params,
    110                                                    rs);
    111   }
    112   switch (qs)
    113   {
    114   case GNUNET_DB_STATUS_HARD_ERROR:
    115     return ANASTASIS_DB_ACCOUNT_STATUS_HARD_ERROR;
    116   case GNUNET_DB_STATUS_SOFT_ERROR:
    117     GNUNET_break (0);
    118     return ANASTASIS_DB_ACCOUNT_STATUS_HARD_ERROR;
    119   case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
    120     /* indicates: no account */
    121     return ANASTASIS_DB_ACCOUNT_STATUS_PAYMENT_REQUIRED;
    122   case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
    123     /* indicates: no backup */
    124     *version = UINT32_MAX;
    125     memset (recovery_data_hash,
    126             0,
    127             sizeof (*recovery_data_hash));
    128     return ANASTASIS_DB_ACCOUNT_STATUS_NO_RESULTS;
    129   default:
    130     GNUNET_break (0);
    131     return ANASTASIS_DB_ACCOUNT_STATUS_HARD_ERROR;
    132   }
    133 }
    134 
    135 
    136 /* end of anastasis-db_lookup_account.c */