2020-03-20 22:34:17 +01:00
|
|
|
/*
|
|
|
|
This file is part of TALER
|
|
|
|
Copyright (C) 2016-2020 Taler Systems SA
|
|
|
|
|
|
|
|
TALER is free software; you can redistribute it and/or modify it under the
|
|
|
|
terms of the GNU Affero Public License as published by the Free Software
|
|
|
|
Foundation; either version 3, or (at your option) any later version.
|
|
|
|
|
|
|
|
TALER is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
|
|
|
A PARTICULAR PURPOSE. See the GNU Affero Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero Public License along with
|
|
|
|
TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @file auditor/report-lib.c
|
|
|
|
* @brief helper library to facilitate generation of audit reports
|
|
|
|
* @author Christian Grothoff
|
|
|
|
*/
|
|
|
|
#include "platform.h"
|
|
|
|
#include "report-lib.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle to access the exchange's database.
|
|
|
|
*/
|
2020-03-21 11:05:51 +01:00
|
|
|
struct TALER_EXCHANGEDB_Plugin *TALER_ARL_edb;
|
2020-03-20 22:34:17 +01:00
|
|
|
|
|
|
|
/**
|
2020-03-23 21:00:08 +01:00
|
|
|
* Which currency are we doing the audit for?
|
2020-03-20 22:34:17 +01:00
|
|
|
*/
|
2020-03-21 11:05:51 +01:00
|
|
|
char *TALER_ARL_currency;
|
2020-03-20 22:34:17 +01:00
|
|
|
|
|
|
|
/**
|
2020-03-23 21:00:08 +01:00
|
|
|
* How many fractional digits does the currency use?
|
2020-03-20 22:34:17 +01:00
|
|
|
*/
|
2020-03-21 11:05:51 +01:00
|
|
|
struct TALER_Amount TALER_ARL_currency_round_unit;
|
2020-03-20 22:34:17 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Our configuration.
|
|
|
|
*/
|
2020-03-21 11:05:51 +01:00
|
|
|
const struct GNUNET_CONFIGURATION_Handle *TALER_ARL_cfg;
|
2020-03-20 22:34:17 +01:00
|
|
|
|
|
|
|
/**
|
2020-03-21 11:05:51 +01:00
|
|
|
* Our session with the #TALER_ARL_edb.
|
2020-03-20 22:34:17 +01:00
|
|
|
*/
|
2020-03-21 11:05:51 +01:00
|
|
|
struct TALER_EXCHANGEDB_Session *TALER_ARL_esession;
|
2020-03-20 22:34:17 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle to access the auditor's database.
|
|
|
|
*/
|
2020-03-21 11:05:51 +01:00
|
|
|
struct TALER_AUDITORDB_Plugin *TALER_ARL_adb;
|
2020-03-20 22:34:17 +01:00
|
|
|
|
|
|
|
/**
|
2020-03-21 11:05:51 +01:00
|
|
|
* Our session with the #TALER_ARL_adb.
|
2020-03-20 22:34:17 +01:00
|
|
|
*/
|
2020-03-21 11:05:51 +01:00
|
|
|
struct TALER_AUDITORDB_Session *TALER_ARL_asession;
|
2020-03-20 22:34:17 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Master public key of the exchange to audit.
|
|
|
|
*/
|
2020-03-21 11:05:51 +01:00
|
|
|
struct TALER_MasterPublicKeyP TALER_ARL_master_pub;
|
2020-03-20 22:34:17 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* At what time did the auditor process start?
|
|
|
|
*/
|
|
|
|
struct GNUNET_TIME_Absolute start_time;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Results about denominations, cached per-transaction, maps denomination pub hashes
|
|
|
|
* to `struct TALER_DenominationKeyValidityPS`.
|
|
|
|
*/
|
|
|
|
static struct GNUNET_CONTAINER_MultiHashMap *denominations;
|
|
|
|
|
2020-07-15 14:07:32 +02:00
|
|
|
/**
|
|
|
|
* Flag that is raised to 'true' if the user
|
|
|
|
* presses CTRL-C to abort the audit.
|
|
|
|
*/
|
|
|
|
static volatile bool abort_flag;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Context for the SIG-INT (ctrl-C) handler.
|
|
|
|
*/
|
|
|
|
static struct GNUNET_SIGNAL_Context *sig_int;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Context for the SIGTERM handler.
|
|
|
|
*/
|
|
|
|
static struct GNUNET_SIGNAL_Context *sig_term;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test if the audit should be aborted because the user
|
|
|
|
* pressed CTRL-C.
|
|
|
|
*
|
|
|
|
* @return false to continue the audit, true to terminate
|
|
|
|
* cleanly as soon as possible
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
TALER_ARL_do_abort (void)
|
|
|
|
{
|
|
|
|
return abort_flag;
|
|
|
|
}
|
|
|
|
|
2020-03-20 22:34:17 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert absolute time to human-readable JSON string.
|
|
|
|
*
|
|
|
|
* @param at time to convert
|
|
|
|
* @return human-readable string representing the time
|
|
|
|
*/
|
|
|
|
json_t *
|
2020-03-21 12:56:16 +01:00
|
|
|
TALER_ARL_json_from_time_abs_nbo (struct GNUNET_TIME_AbsoluteNBO at)
|
2020-03-20 22:34:17 +01:00
|
|
|
{
|
|
|
|
return json_string
|
|
|
|
(GNUNET_STRINGS_absolute_time_to_string
|
|
|
|
(GNUNET_TIME_absolute_ntoh (at)));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert absolute time to human-readable JSON string.
|
|
|
|
*
|
|
|
|
* @param at time to convert
|
|
|
|
* @return human-readable string representing the time
|
|
|
|
*/
|
|
|
|
json_t *
|
2020-03-21 11:05:51 +01:00
|
|
|
TALER_ARL_json_from_time_abs (struct GNUNET_TIME_Absolute at)
|
2020-03-20 22:34:17 +01:00
|
|
|
{
|
|
|
|
return json_string
|
|
|
|
(GNUNET_STRINGS_absolute_time_to_string (at));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-03-23 21:00:08 +01:00
|
|
|
* Add @a object to the report @a array. Fail hard if this fails.
|
2020-03-20 22:34:17 +01:00
|
|
|
*
|
2020-03-23 21:00:08 +01:00
|
|
|
* @param array report array to append @a object to
|
2020-03-20 22:34:17 +01:00
|
|
|
* @param object object to append, should be check that it is not NULL
|
|
|
|
*/
|
|
|
|
void
|
2020-03-21 11:05:51 +01:00
|
|
|
TALER_ARL_report (json_t *array,
|
|
|
|
json_t *object)
|
2020-03-20 22:34:17 +01:00
|
|
|
{
|
|
|
|
GNUNET_assert (NULL != object);
|
|
|
|
GNUNET_assert (0 ==
|
|
|
|
json_array_append_new (array,
|
|
|
|
object));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function called with the results of select_denomination_info()
|
|
|
|
*
|
|
|
|
* @param cls closure, NULL
|
|
|
|
* @param issue issuing information with value, fees and other info about the denomination.
|
|
|
|
* @return #GNUNET_OK (to continue)
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
add_denomination (void *cls,
|
|
|
|
const struct TALER_DenominationKeyValidityPS *issue)
|
|
|
|
{
|
|
|
|
(void) cls;
|
|
|
|
if (NULL !=
|
|
|
|
GNUNET_CONTAINER_multihashmap_get (denominations,
|
|
|
|
&issue->denom_hash))
|
|
|
|
return GNUNET_OK; /* value already known */
|
2020-03-21 19:25:53 +01:00
|
|
|
#if GNUNET_EXTRA_LOGGING >= 1
|
2020-03-20 22:34:17 +01:00
|
|
|
{
|
|
|
|
struct TALER_Amount value;
|
|
|
|
|
|
|
|
TALER_amount_ntoh (&value,
|
|
|
|
&issue->value);
|
|
|
|
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
|
|
|
|
"Tracking denomination `%s' (%s)\n",
|
|
|
|
GNUNET_h2s (&issue->denom_hash),
|
|
|
|
TALER_amount2s (&value));
|
|
|
|
TALER_amount_ntoh (&value,
|
|
|
|
&issue->fee_withdraw);
|
2020-03-21 19:25:53 +01:00
|
|
|
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
|
2020-03-20 22:34:17 +01:00
|
|
|
"Withdraw fee is %s\n",
|
|
|
|
TALER_amount2s (&value));
|
2020-03-21 19:25:53 +01:00
|
|
|
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
|
2020-03-20 22:34:17 +01:00
|
|
|
"Start time is %s\n",
|
|
|
|
GNUNET_STRINGS_absolute_time_to_string
|
|
|
|
(GNUNET_TIME_absolute_ntoh (issue->start)));
|
2020-03-21 19:25:53 +01:00
|
|
|
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
|
2020-03-20 22:34:17 +01:00
|
|
|
"Expire deposit time is %s\n",
|
|
|
|
GNUNET_STRINGS_absolute_time_to_string
|
|
|
|
(GNUNET_TIME_absolute_ntoh (issue->expire_deposit)));
|
|
|
|
}
|
2020-03-21 19:25:53 +01:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
struct TALER_DenominationKeyValidityPS *i;
|
|
|
|
|
|
|
|
i = GNUNET_new (struct TALER_DenominationKeyValidityPS);
|
|
|
|
*i = *issue;
|
|
|
|
GNUNET_assert (GNUNET_OK ==
|
|
|
|
GNUNET_CONTAINER_multihashmap_put (denominations,
|
|
|
|
&issue->denom_hash,
|
|
|
|
i,
|
|
|
|
GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
|
|
|
|
}
|
2020-03-20 22:34:17 +01:00
|
|
|
return GNUNET_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Obtain information about a @a denom_pub.
|
|
|
|
*
|
|
|
|
* @param dh hash of the denomination public key to look up
|
|
|
|
* @param[out] issue set to detailed information about @a denom_pub, NULL if not found, must
|
|
|
|
* NOT be freed by caller
|
|
|
|
* @return transaction status code
|
|
|
|
*/
|
|
|
|
enum GNUNET_DB_QueryStatus
|
2020-03-21 11:05:51 +01:00
|
|
|
TALER_ARL_get_denomination_info_by_hash (const struct GNUNET_HashCode *dh,
|
|
|
|
const struct
|
|
|
|
TALER_DenominationKeyValidityPS **issue)
|
2020-03-20 22:34:17 +01:00
|
|
|
{
|
|
|
|
enum GNUNET_DB_QueryStatus qs;
|
|
|
|
|
|
|
|
if (NULL == denominations)
|
|
|
|
{
|
|
|
|
denominations = GNUNET_CONTAINER_multihashmap_create (256,
|
|
|
|
GNUNET_NO);
|
2020-03-21 11:05:51 +01:00
|
|
|
qs = TALER_ARL_adb->select_denomination_info (TALER_ARL_adb->cls,
|
|
|
|
TALER_ARL_asession,
|
|
|
|
&TALER_ARL_master_pub,
|
|
|
|
&add_denomination,
|
|
|
|
NULL);
|
2020-03-20 22:34:17 +01:00
|
|
|
if (0 > qs)
|
|
|
|
{
|
|
|
|
*issue = NULL;
|
|
|
|
return qs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
2020-03-21 19:25:53 +01:00
|
|
|
const struct TALER_DenominationKeyValidityPS *i;
|
|
|
|
|
|
|
|
i = GNUNET_CONTAINER_multihashmap_get (denominations,
|
|
|
|
dh);
|
|
|
|
if (NULL != i)
|
|
|
|
{
|
|
|
|
/* cache hit */
|
|
|
|
*issue = i;
|
|
|
|
return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT;
|
|
|
|
}
|
2020-03-20 22:34:17 +01:00
|
|
|
}
|
|
|
|
/* maybe database changed since we last iterated, give it one more shot */
|
2020-03-21 11:05:51 +01:00
|
|
|
qs = TALER_ARL_adb->select_denomination_info (TALER_ARL_adb->cls,
|
|
|
|
TALER_ARL_asession,
|
|
|
|
&TALER_ARL_master_pub,
|
|
|
|
&add_denomination,
|
|
|
|
NULL);
|
2020-03-20 22:34:17 +01:00
|
|
|
if (qs <= 0)
|
|
|
|
{
|
|
|
|
if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
|
|
|
|
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
|
|
|
|
"Denomination %s not found\n",
|
|
|
|
TALER_B2S (dh));
|
|
|
|
return qs;
|
|
|
|
}
|
|
|
|
{
|
2020-03-21 19:25:53 +01:00
|
|
|
const struct TALER_DenominationKeyValidityPS *i;
|
|
|
|
|
|
|
|
i = GNUNET_CONTAINER_multihashmap_get (denominations,
|
|
|
|
dh);
|
|
|
|
if (NULL != i)
|
|
|
|
{
|
|
|
|
/* cache hit */
|
|
|
|
*issue = i;
|
|
|
|
return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT;
|
|
|
|
}
|
2020-03-20 22:34:17 +01:00
|
|
|
}
|
|
|
|
/* We found more keys, but not the denomination we are looking for :-( */
|
|
|
|
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
|
|
|
|
"Denomination %s not found\n",
|
|
|
|
TALER_B2S (dh));
|
|
|
|
return GNUNET_DB_STATUS_SUCCESS_NO_RESULTS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Obtain information about a @a denom_pub.
|
|
|
|
*
|
|
|
|
* @param denom_pub key to look up
|
|
|
|
* @param[out] issue set to detailed information about @a denom_pub, NULL if not found, must
|
|
|
|
* NOT be freed by caller
|
|
|
|
* @param[out] dh set to the hash of @a denom_pub, may be NULL
|
|
|
|
* @return transaction status code
|
|
|
|
*/
|
|
|
|
enum GNUNET_DB_QueryStatus
|
2020-03-21 19:25:53 +01:00
|
|
|
TALER_ARL_get_denomination_info (
|
|
|
|
const struct TALER_DenominationPublicKey *denom_pub,
|
|
|
|
const struct TALER_DenominationKeyValidityPS **issue,
|
|
|
|
struct GNUNET_HashCode *dh)
|
2020-03-20 22:34:17 +01:00
|
|
|
{
|
|
|
|
struct GNUNET_HashCode hc;
|
|
|
|
|
|
|
|
if (NULL == dh)
|
|
|
|
dh = &hc;
|
|
|
|
GNUNET_CRYPTO_rsa_public_key_hash (denom_pub->rsa_public_key,
|
|
|
|
dh);
|
2020-03-21 11:05:51 +01:00
|
|
|
return TALER_ARL_get_denomination_info_by_hash (dh,
|
|
|
|
issue);
|
2020-03-20 22:34:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform the given @a analysis within a transaction scope.
|
|
|
|
* Commit on success.
|
|
|
|
*
|
|
|
|
* @param analysis analysis to run
|
|
|
|
* @param analysis_cls closure for @a analysis
|
|
|
|
* @return #GNUNET_OK if @a analysis succeessfully committed,
|
|
|
|
* #GNUNET_NO if we had an error on commit (retry may help)
|
|
|
|
* #GNUNET_SYSERR on hard errors
|
|
|
|
*/
|
2020-03-21 11:05:51 +01:00
|
|
|
static int
|
|
|
|
transact (TALER_ARL_Analysis analysis,
|
2020-03-20 22:34:17 +01:00
|
|
|
void *analysis_cls)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
enum GNUNET_DB_QueryStatus qs;
|
|
|
|
|
2020-03-21 11:05:51 +01:00
|
|
|
ret = TALER_ARL_adb->start (TALER_ARL_adb->cls,
|
|
|
|
TALER_ARL_asession);
|
2020-03-20 22:34:17 +01:00
|
|
|
if (GNUNET_OK != ret)
|
|
|
|
{
|
|
|
|
GNUNET_break (0);
|
|
|
|
return GNUNET_SYSERR;
|
|
|
|
}
|
2020-03-21 11:05:51 +01:00
|
|
|
TALER_ARL_edb->preflight (TALER_ARL_edb->cls,
|
|
|
|
TALER_ARL_esession);
|
|
|
|
ret = TALER_ARL_edb->start (TALER_ARL_edb->cls,
|
|
|
|
TALER_ARL_esession,
|
|
|
|
"auditor");
|
2020-03-20 22:34:17 +01:00
|
|
|
if (GNUNET_OK != ret)
|
|
|
|
{
|
|
|
|
GNUNET_break (0);
|
2020-03-21 19:25:53 +01:00
|
|
|
TALER_ARL_edb->rollback (TALER_ARL_edb->cls,
|
|
|
|
TALER_ARL_esession);
|
2020-03-20 22:34:17 +01:00
|
|
|
return GNUNET_SYSERR;
|
|
|
|
}
|
|
|
|
qs = analysis (analysis_cls);
|
|
|
|
if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == qs)
|
|
|
|
{
|
2020-03-21 11:05:51 +01:00
|
|
|
qs = TALER_ARL_edb->commit (TALER_ARL_edb->cls,
|
|
|
|
TALER_ARL_esession);
|
2020-03-20 22:34:17 +01:00
|
|
|
if (0 > qs)
|
|
|
|
{
|
|
|
|
GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
|
|
|
|
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
|
|
|
|
"Exchange DB commit failed, rolling back transaction\n");
|
2020-03-21 11:05:51 +01:00
|
|
|
TALER_ARL_adb->rollback (TALER_ARL_adb->cls,
|
|
|
|
TALER_ARL_asession);
|
2020-03-20 22:34:17 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-03-21 11:05:51 +01:00
|
|
|
qs = TALER_ARL_adb->commit (TALER_ARL_adb->cls,
|
|
|
|
TALER_ARL_asession);
|
2020-03-20 22:34:17 +01:00
|
|
|
if (0 > qs)
|
|
|
|
{
|
|
|
|
GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
|
|
|
|
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
|
|
|
|
"Auditor DB commit failed!\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
|
|
|
|
"Processing failed (or no changes), rolling back transaction\n");
|
2020-03-21 11:05:51 +01:00
|
|
|
TALER_ARL_adb->rollback (TALER_ARL_adb->cls,
|
|
|
|
TALER_ARL_asession);
|
|
|
|
TALER_ARL_edb->rollback (TALER_ARL_edb->cls,
|
|
|
|
TALER_ARL_esession);
|
2020-03-20 22:34:17 +01:00
|
|
|
}
|
|
|
|
switch (qs)
|
|
|
|
{
|
|
|
|
case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
|
|
|
|
return GNUNET_OK;
|
|
|
|
case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
|
|
|
|
return GNUNET_OK;
|
|
|
|
case GNUNET_DB_STATUS_SOFT_ERROR:
|
|
|
|
return GNUNET_NO;
|
|
|
|
case GNUNET_DB_STATUS_HARD_ERROR:
|
|
|
|
return GNUNET_SYSERR;
|
|
|
|
}
|
|
|
|
return GNUNET_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize DB sessions and run the analysis.
|
|
|
|
*
|
|
|
|
* @param ana analysis to run
|
2020-03-22 21:36:42 +01:00
|
|
|
* @param ana_cls closure for @a ana
|
2020-03-20 22:34:17 +01:00
|
|
|
* @return #GNUNET_OK on success
|
|
|
|
*/
|
|
|
|
int
|
2020-03-21 11:05:51 +01:00
|
|
|
TALER_ARL_setup_sessions_and_run (TALER_ARL_Analysis ana,
|
|
|
|
void *ana_cls)
|
2020-03-20 22:34:17 +01:00
|
|
|
{
|
2020-03-21 11:05:51 +01:00
|
|
|
TALER_ARL_esession = TALER_ARL_edb->get_session (TALER_ARL_edb->cls);
|
|
|
|
if (NULL == TALER_ARL_esession)
|
2020-03-20 22:34:17 +01:00
|
|
|
{
|
2020-03-21 19:25:53 +01:00
|
|
|
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
|
|
|
|
"Failed to initialize exchange session.\n");
|
2020-03-20 22:34:17 +01:00
|
|
|
return GNUNET_SYSERR;
|
|
|
|
}
|
2020-03-21 11:05:51 +01:00
|
|
|
TALER_ARL_asession = TALER_ARL_adb->get_session (TALER_ARL_adb->cls);
|
|
|
|
if (NULL == TALER_ARL_asession)
|
2020-03-20 22:34:17 +01:00
|
|
|
{
|
2020-03-21 19:25:53 +01:00
|
|
|
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
|
|
|
|
"Failed to initialize auditor session.\n");
|
2020-03-20 22:34:17 +01:00
|
|
|
return GNUNET_SYSERR;
|
|
|
|
}
|
|
|
|
|
2020-03-22 14:51:36 +01:00
|
|
|
if (0 > transact (ana,
|
|
|
|
ana_cls))
|
|
|
|
return GNUNET_SYSERR;
|
2020-03-20 22:34:17 +01:00
|
|
|
return GNUNET_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-03-21 11:05:51 +01:00
|
|
|
* Test if the given @a mpub matches the #TALER_ARL_master_pub.
|
2020-03-20 22:34:17 +01:00
|
|
|
* If so, set "found" to GNUNET_YES.
|
|
|
|
*
|
|
|
|
* @param cls a `int *` pointing to "found"
|
|
|
|
* @param mpub exchange master public key to compare
|
|
|
|
* @param exchange_url URL of the exchange (ignored)
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
test_master_present (void *cls,
|
|
|
|
const struct TALER_MasterPublicKeyP *mpub,
|
|
|
|
const char *exchange_url)
|
|
|
|
{
|
|
|
|
int *found = cls;
|
|
|
|
|
|
|
|
(void) exchange_url;
|
|
|
|
if (0 == GNUNET_memcmp (mpub,
|
2020-03-21 11:05:51 +01:00
|
|
|
&TALER_ARL_master_pub))
|
2020-03-20 22:34:17 +01:00
|
|
|
*found = GNUNET_YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-08 23:52:01 +02:00
|
|
|
/**
|
|
|
|
* Perform addition of amounts. If the addition fails, logs
|
|
|
|
* a detailed error and calls exit() to terminate the process (!).
|
|
|
|
*
|
|
|
|
* Do not call this function directly, use #TALER_ARL_amount_add().
|
|
|
|
*
|
|
|
|
* @param[out] sum where to store @a a1 + @a a2, set to "invalid" on overflow
|
|
|
|
* @param a1 first amount to add
|
|
|
|
* @param a2 second amount to add
|
|
|
|
* @param filename where is the addition called
|
|
|
|
* @param functionname name of the function where the addition is called
|
|
|
|
* @param line line number of the addition
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
TALER_ARL_amount_add_ (struct TALER_Amount *sum,
|
|
|
|
const struct TALER_Amount *a1,
|
|
|
|
const struct TALER_Amount *a2,
|
|
|
|
const char *filename,
|
|
|
|
const char *functionname,
|
|
|
|
unsigned int line)
|
|
|
|
{
|
|
|
|
enum TALER_AmountArithmeticResult aar;
|
|
|
|
const char *msg;
|
|
|
|
char *a2s;
|
|
|
|
|
|
|
|
aar = TALER_amount_add (sum,
|
|
|
|
a1,
|
|
|
|
a2);
|
|
|
|
if (aar >= 0)
|
|
|
|
return;
|
|
|
|
switch (aar)
|
|
|
|
{
|
|
|
|
case TALER_AAR_INVALID_RESULT_OVERFLOW:
|
|
|
|
msg =
|
|
|
|
"arithmetic overflow in amount addition (likely the database is corrupt, see manual)";
|
|
|
|
break;
|
|
|
|
case TALER_AAR_INVALID_NORMALIZATION_FAILED:
|
|
|
|
msg =
|
|
|
|
"normalization failed in amount addition (likely the database is corrupt, see manual)";
|
|
|
|
break;
|
|
|
|
case TALER_AAR_INVALID_CURRENCIES_INCOMPATIBLE:
|
|
|
|
msg =
|
|
|
|
"incompatible currencies in amount addition (likely bad configuration and auditor code missing a sanity check, see manual)";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
GNUNET_assert (0); /* should be impossible */
|
|
|
|
}
|
|
|
|
a2s = TALER_amount_to_string (a2);
|
|
|
|
fprintf (stderr,
|
|
|
|
"Aborting audit due to fatal error in function %s at %s:%d trying to add %s to %s: %s\n",
|
|
|
|
functionname,
|
|
|
|
filename,
|
|
|
|
line,
|
|
|
|
TALER_amount2s (a1),
|
|
|
|
a2s,
|
|
|
|
msg);
|
|
|
|
GNUNET_free (a2s);
|
|
|
|
exit (42);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform subtraction of amounts. If the subtraction fails, logs
|
|
|
|
* a detailed error and calls exit() to terminate the process (!).
|
|
|
|
*
|
|
|
|
* Do not call this function directly, use #TALER_ARL_amount_subtract().
|
|
|
|
*
|
|
|
|
* @param[out] diff where to store (@a a1 - @a a2)
|
|
|
|
* @param a1 amount to subtract from
|
|
|
|
* @param a2 amount to subtract
|
|
|
|
* @param filename where is the addition called
|
|
|
|
* @param functionname name of the function where the addition is called
|
|
|
|
* @param line line number of the addition
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
TALER_ARL_amount_subtract_ (struct TALER_Amount *diff,
|
|
|
|
const struct TALER_Amount *a1,
|
|
|
|
const struct TALER_Amount *a2,
|
|
|
|
const char *filename,
|
|
|
|
const char *functionname,
|
|
|
|
unsigned int line)
|
|
|
|
{
|
|
|
|
enum TALER_AmountArithmeticResult aar;
|
|
|
|
const char *msg;
|
|
|
|
char *a2s;
|
|
|
|
|
|
|
|
aar = TALER_amount_subtract (diff,
|
|
|
|
a1,
|
|
|
|
a2);
|
|
|
|
if (aar >= 0)
|
|
|
|
return;
|
|
|
|
switch (aar)
|
|
|
|
{
|
|
|
|
case TALER_AAR_INVALID_NEGATIVE_RESULT:
|
|
|
|
msg =
|
|
|
|
"negative result in amount subtraction (likely the database is corrupt, see manual)";
|
|
|
|
break;
|
|
|
|
case TALER_AAR_INVALID_NORMALIZATION_FAILED:
|
|
|
|
msg =
|
|
|
|
"normalization failed in amount subtraction (likely the database is corrupt, see manual)";
|
|
|
|
break;
|
|
|
|
case TALER_AAR_INVALID_CURRENCIES_INCOMPATIBLE:
|
|
|
|
msg =
|
|
|
|
"currencies incompatible in amount subtraction (likely bad configuration and auditor code missing a sanity check, see manual)";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
GNUNET_assert (0); /* should be impossible */
|
|
|
|
}
|
|
|
|
a2s = TALER_amount_to_string (a2);
|
|
|
|
fprintf (stderr,
|
|
|
|
"Aborting audit due to fatal error in function %s at %s:%d trying to subtract %s from %s: %s\n",
|
|
|
|
functionname,
|
|
|
|
filename,
|
|
|
|
line,
|
|
|
|
a2s,
|
|
|
|
TALER_amount2s (a1),
|
|
|
|
msg);
|
|
|
|
GNUNET_free (a2s);
|
|
|
|
exit (42);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform subtraction of amounts. Negative results should be signalled by the
|
|
|
|
* return value (leaving @a diff set to 'invalid'). If the subtraction fails
|
|
|
|
* for other reasons (currency missmatch, normalization failure), logs a
|
|
|
|
* detailed error and calls exit() to terminate the process (!).
|
|
|
|
*
|
|
|
|
* Do not call this function directly, use #TALER_ARL_amount_subtract_neg().
|
|
|
|
*
|
|
|
|
* @param[out] diff where to store (@a a1 - @a a2)
|
|
|
|
* @param a1 amount to subtract from
|
|
|
|
* @param a2 amount to subtract
|
|
|
|
* @param filename where is the addition called
|
|
|
|
* @param functionname name of the function where the addition is called
|
|
|
|
* @param line line number of the addition
|
|
|
|
* @return #TALER_ARL_SR_NEGATIVE if the result was negative (and @a diff is now invalid),
|
|
|
|
* #TALER_ARL_SR_ZERO if the result was zero,
|
|
|
|
* #TALER_ARL_SR_POSITIVE if the result is positive
|
|
|
|
*/
|
|
|
|
enum TALER_ARL_SubtractionResult
|
|
|
|
TALER_ARL_amount_subtract_neg_ (struct TALER_Amount *diff,
|
|
|
|
const struct TALER_Amount *a1,
|
|
|
|
const struct TALER_Amount *a2,
|
|
|
|
const char *filename,
|
|
|
|
const char *functionname,
|
|
|
|
unsigned int line)
|
|
|
|
{
|
|
|
|
enum TALER_AmountArithmeticResult aar;
|
|
|
|
const char *msg;
|
|
|
|
char *a2s;
|
|
|
|
|
|
|
|
aar = TALER_amount_subtract (diff,
|
|
|
|
a1,
|
|
|
|
a2);
|
|
|
|
switch (aar)
|
|
|
|
{
|
|
|
|
case TALER_AAR_RESULT_POSITIVE:
|
|
|
|
return TALER_ARL_SR_POSITIVE;
|
|
|
|
case TALER_AAR_RESULT_ZERO:
|
|
|
|
return TALER_ARL_SR_ZERO;
|
|
|
|
case TALER_AAR_INVALID_NEGATIVE_RESULT:
|
|
|
|
return TALER_ARL_SR_INVALID_NEGATIVE;
|
|
|
|
case TALER_AAR_INVALID_NORMALIZATION_FAILED:
|
|
|
|
msg =
|
|
|
|
"normalization failed in amount subtraction (likely the database is corrupt, see manual)";
|
|
|
|
break;
|
|
|
|
case TALER_AAR_INVALID_CURRENCIES_INCOMPATIBLE:
|
|
|
|
msg =
|
|
|
|
"currencies incompatible in amount subtraction (likely bad configuration and auditor code missing a sanity check, see manual)";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
GNUNET_assert (0); /* should be impossible */
|
|
|
|
}
|
|
|
|
a2s = TALER_amount_to_string (a2);
|
|
|
|
fprintf (stderr,
|
|
|
|
"Aborting audit due to fatal error in function %s at %s:%d trying to subtract %s from %s: %s\n",
|
|
|
|
functionname,
|
|
|
|
filename,
|
|
|
|
line,
|
|
|
|
a2s,
|
|
|
|
TALER_amount2s (a1),
|
|
|
|
msg);
|
|
|
|
GNUNET_free (a2s);
|
|
|
|
exit (42);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-15 14:07:32 +02:00
|
|
|
/**
|
|
|
|
* Signal handler called for signals that should cause us to shutdown.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
handle_sigint (void)
|
|
|
|
{
|
|
|
|
abort_flag = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-21 11:05:51 +01:00
|
|
|
/**
|
|
|
|
* Setup global variables based on configuration.
|
|
|
|
*
|
|
|
|
* @param c configuration to use
|
|
|
|
* @return #GNUNET_OK on success
|
|
|
|
*/
|
2020-03-20 22:34:17 +01:00
|
|
|
int
|
2020-03-21 11:05:51 +01:00
|
|
|
TALER_ARL_init (const struct GNUNET_CONFIGURATION_Handle *c)
|
2020-03-20 22:34:17 +01:00
|
|
|
{
|
2020-03-21 11:05:51 +01:00
|
|
|
TALER_ARL_cfg = c;
|
2020-03-20 22:34:17 +01:00
|
|
|
start_time = GNUNET_TIME_absolute_get ();
|
2020-03-21 11:05:51 +01:00
|
|
|
if (0 == GNUNET_is_zero (&TALER_ARL_master_pub))
|
2020-03-20 22:34:17 +01:00
|
|
|
{
|
|
|
|
/* -m option not given, try configuration */
|
2020-03-21 11:05:51 +01:00
|
|
|
char *TALER_ARL_master_public_key_str;
|
2020-03-20 22:34:17 +01:00
|
|
|
|
|
|
|
if (GNUNET_OK !=
|
2020-03-21 11:05:51 +01:00
|
|
|
GNUNET_CONFIGURATION_get_value_string (TALER_ARL_cfg,
|
2020-03-20 22:34:17 +01:00
|
|
|
"exchange",
|
|
|
|
"MASTER_PUBLIC_KEY",
|
2020-03-21 11:05:51 +01:00
|
|
|
&TALER_ARL_master_public_key_str))
|
2020-03-20 22:34:17 +01:00
|
|
|
{
|
2020-03-21 19:25:53 +01:00
|
|
|
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
|
|
|
|
"Pass option -m or set MASTER_PUBLIC_KEY in the configuration!\n");
|
2020-03-20 22:34:17 +01:00
|
|
|
GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
|
|
|
|
"exchange",
|
|
|
|
"MASTER_PUBLIC_KEY");
|
|
|
|
return GNUNET_SYSERR;
|
|
|
|
}
|
|
|
|
if (GNUNET_OK !=
|
2020-03-21 11:05:51 +01:00
|
|
|
GNUNET_CRYPTO_eddsa_public_key_from_string (
|
|
|
|
TALER_ARL_master_public_key_str,
|
|
|
|
strlen (
|
|
|
|
TALER_ARL_master_public_key_str),
|
|
|
|
&TALER_ARL_master_pub.
|
|
|
|
eddsa_pub))
|
2020-03-20 22:34:17 +01:00
|
|
|
{
|
2020-03-21 19:25:53 +01:00
|
|
|
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
|
|
|
|
"Malformed master public key given in configuration file.");
|
2020-03-21 11:05:51 +01:00
|
|
|
GNUNET_free (TALER_ARL_master_public_key_str);
|
2020-03-20 22:34:17 +01:00
|
|
|
return GNUNET_SYSERR;
|
|
|
|
}
|
2020-03-21 11:05:51 +01:00
|
|
|
GNUNET_free (TALER_ARL_master_public_key_str);
|
2020-03-20 22:34:17 +01:00
|
|
|
} /* end of -m not given */
|
|
|
|
|
|
|
|
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
|
|
|
|
"Taler auditor running for exchange master public key %s\n",
|
2020-03-21 11:05:51 +01:00
|
|
|
TALER_B2S (&TALER_ARL_master_pub));
|
2020-03-20 22:34:17 +01:00
|
|
|
|
|
|
|
if (GNUNET_OK !=
|
2020-03-21 11:05:51 +01:00
|
|
|
TALER_config_get_currency (TALER_ARL_cfg,
|
|
|
|
&TALER_ARL_currency))
|
2020-03-20 22:34:17 +01:00
|
|
|
{
|
|
|
|
return GNUNET_SYSERR;
|
|
|
|
}
|
|
|
|
{
|
2020-07-15 21:41:09 +02:00
|
|
|
if ( (GNUNET_OK !=
|
|
|
|
TALER_config_get_amount (TALER_ARL_cfg,
|
|
|
|
"taler",
|
|
|
|
"CURRENCY_ROUND_UNIT",
|
|
|
|
&TALER_ARL_currency_round_unit)) ||
|
|
|
|
( (0 != TALER_ARL_currency_round_unit.fraction) &&
|
|
|
|
(0 != TALER_ARL_currency_round_unit.value) ) )
|
2020-03-20 22:34:17 +01:00
|
|
|
{
|
|
|
|
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
|
2020-07-15 21:41:09 +02:00
|
|
|
"Need non-zero value in section `TALER' under `CURRENCY_ROUND_UNIT'\n");
|
2020-03-20 22:34:17 +01:00
|
|
|
return GNUNET_SYSERR;
|
|
|
|
}
|
|
|
|
}
|
2020-07-15 14:07:32 +02:00
|
|
|
sig_int = GNUNET_SIGNAL_handler_install (SIGINT,
|
|
|
|
&handle_sigint);
|
|
|
|
if (NULL == sig_int)
|
|
|
|
{
|
|
|
|
GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
|
|
|
|
"signal");
|
|
|
|
TALER_ARL_done (NULL);
|
|
|
|
return GNUNET_SYSERR;
|
|
|
|
}
|
|
|
|
sig_term = GNUNET_SIGNAL_handler_install (SIGTERM,
|
|
|
|
&handle_sigint);
|
|
|
|
if (NULL == sig_term)
|
|
|
|
{
|
|
|
|
GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
|
|
|
|
"signal");
|
|
|
|
TALER_ARL_done (NULL);
|
|
|
|
return GNUNET_SYSERR;
|
|
|
|
}
|
2020-03-20 22:34:17 +01:00
|
|
|
if (NULL ==
|
2020-03-21 11:05:51 +01:00
|
|
|
(TALER_ARL_edb = TALER_EXCHANGEDB_plugin_load (TALER_ARL_cfg)))
|
2020-03-20 22:34:17 +01:00
|
|
|
{
|
2020-03-21 19:25:53 +01:00
|
|
|
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
|
|
|
|
"Failed to initialize exchange database plugin.\n");
|
2020-07-15 14:07:32 +02:00
|
|
|
TALER_ARL_done (NULL);
|
2020-03-20 22:34:17 +01:00
|
|
|
return GNUNET_SYSERR;
|
|
|
|
}
|
|
|
|
if (NULL ==
|
2020-03-21 11:05:51 +01:00
|
|
|
(TALER_ARL_adb = TALER_AUDITORDB_plugin_load (TALER_ARL_cfg)))
|
2020-03-20 22:34:17 +01:00
|
|
|
{
|
2020-03-21 19:25:53 +01:00
|
|
|
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
|
|
|
|
"Failed to initialize auditor database plugin.\n");
|
|
|
|
TALER_ARL_done (NULL);
|
2020-03-20 22:34:17 +01:00
|
|
|
return GNUNET_SYSERR;
|
|
|
|
}
|
|
|
|
{
|
2020-03-21 19:25:53 +01:00
|
|
|
struct TALER_AUDITORDB_Session *as;
|
|
|
|
int found;
|
|
|
|
|
|
|
|
as = TALER_ARL_adb->get_session (TALER_ARL_adb->cls);
|
|
|
|
if (NULL == as)
|
|
|
|
{
|
|
|
|
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
|
|
|
|
"Failed to start session with auditor database.\n");
|
|
|
|
TALER_ARL_done (NULL);
|
|
|
|
return GNUNET_SYSERR;
|
|
|
|
}
|
|
|
|
found = GNUNET_NO;
|
|
|
|
(void) TALER_ARL_adb->list_exchanges (TALER_ARL_adb->cls,
|
|
|
|
as,
|
|
|
|
&test_master_present,
|
|
|
|
&found);
|
|
|
|
if (GNUNET_NO == found)
|
|
|
|
{
|
|
|
|
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
|
|
|
|
"Exchange's master public key `%s' not known to auditor DB. Did you forget to run `taler-auditor-exchange`?\n",
|
|
|
|
GNUNET_p2s (&TALER_ARL_master_pub.eddsa_pub));
|
|
|
|
TALER_ARL_done (NULL);
|
|
|
|
return GNUNET_SYSERR;
|
|
|
|
}
|
2020-03-20 22:34:17 +01:00
|
|
|
}
|
|
|
|
return GNUNET_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-21 11:05:51 +01:00
|
|
|
/**
|
|
|
|
* Generate the report and close connectios to the database.
|
|
|
|
*
|
|
|
|
* @param report the report to output, may be NULL for no report
|
|
|
|
*/
|
2020-03-20 22:34:17 +01:00
|
|
|
void
|
2020-03-21 11:05:51 +01:00
|
|
|
TALER_ARL_done (json_t *report)
|
2020-03-20 22:34:17 +01:00
|
|
|
{
|
|
|
|
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
|
|
|
|
"Audit complete\n");
|
2020-07-15 14:07:32 +02:00
|
|
|
if (NULL != sig_int)
|
|
|
|
{
|
|
|
|
GNUNET_SIGNAL_handler_uninstall (sig_int);
|
|
|
|
sig_int = NULL;
|
|
|
|
}
|
|
|
|
if (NULL != sig_term)
|
|
|
|
{
|
|
|
|
GNUNET_SIGNAL_handler_uninstall (sig_term);
|
|
|
|
sig_term = NULL;
|
|
|
|
}
|
2020-03-21 19:25:53 +01:00
|
|
|
if (NULL != TALER_ARL_adb)
|
|
|
|
{
|
|
|
|
TALER_AUDITORDB_plugin_unload (TALER_ARL_adb);
|
|
|
|
TALER_ARL_adb = NULL;
|
|
|
|
}
|
|
|
|
if (NULL != TALER_ARL_edb)
|
|
|
|
{
|
|
|
|
TALER_EXCHANGEDB_plugin_unload (TALER_ARL_edb);
|
|
|
|
TALER_ARL_edb = NULL;
|
|
|
|
}
|
2020-03-21 11:05:51 +01:00
|
|
|
if (NULL != report)
|
|
|
|
{
|
|
|
|
json_dumpf (report,
|
|
|
|
stdout,
|
|
|
|
JSON_INDENT (2));
|
|
|
|
json_decref (report);
|
|
|
|
}
|
2020-03-20 22:34:17 +01:00
|
|
|
}
|
2020-03-21 19:25:53 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* end of report-lib.c */
|