improved error handling

This commit is contained in:
Özgür Kesim 2022-10-04 11:33:21 +02:00
parent 424bbddaa3
commit 3ec07d62c3
Signed by: oec
GPG Key ID: 3D76A56D79EDD9D7

View File

@ -33,6 +33,9 @@
/* Path to the replay program. */ /* Path to the replay program. */
static char *replay_program; static char *replay_program;
/* supported currency */
static char *currency;
/* This is basically BRANDT_Result with an extra string field */ /* This is basically BRANDT_Result with an extra string field */
struct result struct result
{ {
@ -42,28 +45,54 @@ struct result
}; };
/* /*
* TODO: Transcript information * @brief Transcript information
*
*/ */
struct transcript struct transcript
{ {
// All fields from json come here. /*
uint16_t n; // #bidders + 1 * The first couple of fields are from a JSON transcript
uint16_t k; // #prices */
uint16_t m; // type of auction
struct GNUNET_TIME_Absolute time_start; /* Public key of seller */
struct GNUNET_TIME_Relative time_round; struct GNUNET_CRYPTO_EddsaPublicKey seller_pub;
/* Payto URL */
const char *payto;
/* Number of bidders + 1 (for seller) */
uint16_t n;
/* (n-1) public keys of bidders */
struct GNUNET_CRYPTO_EddsaPublicKey *bidder_pub;
/* Type of auction, see libbrandt */
uint16_t m;
/* Auction public outcome? */
bool public; bool public;
char **prices; // Must be of length k. We do not parse those
// struct msg *msgs; // Array must be of length 4*n
// struct BRANDT_Auction *auction; /* Start date of the auction */
struct GNUNET_TIME_Timestamp time_start;
struct result *results; /* End date of the auction */
size_t results_len; struct GNUNET_TIME_Relative time_round;
/* Number of prices */
uint16_t k;
/* Prices, must be length k */
struct TALER_Amount *prices;
/* Expected winner(s), maybe NULL */
struct result *expected; struct result *expected;
size_t expected_len; size_t expected_len;
uint16_t id;
struct GNUNET_CRYPTO_EccDlogContext *edc; /*
* These are the results from the replay via the external program.
*/
struct result *results;
size_t results_len;
}; };
/** /**
@ -71,16 +100,25 @@ struct transcript
*/ */
static enum GNUNET_GenericReturnValue static enum GNUNET_GenericReturnValue
json_error (json_t **output, json_error (json_t **output,
char *error) char *error, ...)
{ {
va_list ap;
int n = 0;
char buf[4096];
GNUNET_assert (error); GNUNET_assert (error);
*output = json_pack ("{s:s}", "error", error); va_start (ap, error);
GNUNET_assert (*output); n = vsprintf (buf, error, ap);
va_end (ap);
GNUNET_log (GNUNET_ERROR_TYPE_INFO, GNUNET_log (GNUNET_ERROR_TYPE_INFO,
LOG_PREFIX "got error: %s\n", LOG_PREFIX "got error: %s\n",
error); n < 0 ? error: buf);
*output = json_pack ("{s:s}",
"error",
n < 0 ? error : buf);
GNUNET_assert (*output);
return GNUNET_SYSERR; return GNUNET_SYSERR;
}; };
@ -129,118 +167,153 @@ json_result (json_t **output,
* @brief Parses a given json as transcript. * @brief Parses a given json as transcript.
* *
* @param[in] jtr JSON input * @param[in] jtr JSON input
* @param[out] transcript Parsed transcript data * @param[out] tr Parsed transcript data
* @param[out] jresult JSON output, both, for results or errors * @param[out] jerror JSON output for errors
* @return GNUNET_OK on succes * @return GNUNET_OK on succes
* *
* TODO: * TODO:
* - fix leakages
* - parse and verify signatures * - parse and verify signatures
*/ */
static enum GNUNET_GenericReturnValue static enum GNUNET_GenericReturnValue
parse_transcript (const json_t *jtr, parse_transcript (const json_t *jtr,
struct transcript *tr, struct transcript *tr,
json_t **output) json_t **jerror)
{ {
json_t *auc;
// TODO: struct GNUNET_CRYPTO_EddsaSignature sig; // TODO: struct GNUNET_CRYPTO_EddsaSignature sig;
GNUNET_assert (jtr); GNUNET_assert (jtr);
GNUNET_assert (tr); GNUNET_assert (tr);
// Parse auction
{ {
json_t *auc;
char *perr; char *perr;
unsigned int eline; unsigned int eline;
struct GNUNET_JSON_Specification au_spec[] = { struct GNUNET_JSON_Specification au_spec[] = {
GNUNET_JSON_spec_bool ("public", &tr->public), GNUNET_JSON_spec_bool ("public", &tr->public),
GNUNET_JSON_spec_uint16 ("type", &tr->m), GNUNET_JSON_spec_uint16 ("type", &tr->m),
GNUNET_JSON_spec_fixed_auto ("pubkey", &tr->seller_pub),
GNUNET_JSON_spec_timestamp ("time_start", &tr->time_start),
GNUNET_JSON_spec_relative_time ("time_round", &tr->time_round),
GNUNET_JSON_spec_string ("payto", &tr->payto),
GNUNET_JSON_spec_end () GNUNET_JSON_spec_end ()
}; };
auc = json_object_get (jtr, "auction"); auc = json_object_get (jtr, "auction");
if (NULL == auc) if (NULL == auc)
return json_error (output, "no auction found in transcript"); return json_error (jerror,
"no auction found in transcript");
if (GNUNET_OK != if (GNUNET_OK !=
GNUNET_JSON_parse (auc, GNUNET_JSON_parse (auc,
au_spec, au_spec,
(const char **) &perr, (const char **) &perr,
&eline)) &eline))
return json_error (output, perr); return json_error (jerror,
perr);
// Prices... // Prices...
{ {
json_t *prices = json_object_get (auc, "prices");
size_t idx; size_t idx;
json_t *val; json_t *val;
json_t *prices;
prices = json_object_get (auc, "prices");
if (! json_is_array (prices)) if (! json_is_array (prices))
// TODO: fix leak!? return json_error (jerror,
return json_error (output, "no prices found"); "no prices found");
tr->k = json_array_size (prices); tr->k = json_array_size (prices);
tr->prices = GNUNET_new_array (tr->k, char *); tr->prices = GNUNET_new_array (tr->k, struct TALER_Amount);
json_array_foreach (prices, idx, val) json_array_foreach (prices, idx, val)
{ {
if (! json_is_string (val)) struct GNUNET_JSON_Specification spec[] = {
// TODO: fix leak!_ TALER_JSON_spec_amount (NULL,
return json_error (output, "prices not strings"); currency,
&(tr->prices[idx])),
GNUNET_JSON_spec_end (),
};
tr->prices[idx] = (char *) json_string_value (val); if (GNUNET_OK !=
GNUNET_JSON_parse (val,
// TODO: parse prices spec,
NULL,
NULL))
return json_error (jerror,
"price no. %ld couldn't be parsed",
idx + 1);
} }
} }
} }
// Bidders // Bidders
{ {
size_t idx;
json_t *val;
json_t *bidders; json_t *bidders;
bidders = json_object_get (jtr, "bidders"); bidders = json_object_get (jtr, "bidders");
if (! bidders || ! json_is_array (bidders)) if (! bidders || ! json_is_array (bidders))
// TODO: fix leak!_ return json_error (jerror,
return json_error (output, "bidders not found"); "no bidders found");
// TODO: parse bidders as pub keys;
tr->n = json_array_size (bidders); tr->n = json_array_size (bidders);
tr->bidder_pub = GNUNET_new_array (tr->n, struct
GNUNET_CRYPTO_EddsaPublicKey);
json_array_foreach (bidders, idx, val)
{
struct GNUNET_JSON_Specification spec[] = {
GNUNET_JSON_spec_fixed_auto (NULL,
&(tr->bidder_pub[idx])),
GNUNET_JSON_spec_end (),
};
if (GNUNET_OK !=
GNUNET_JSON_parse (val,
spec,
NULL,
NULL))
return json_error (jerror,
"bidder no %ld public key couldn't be parsed",
idx + 1);
}
} }
// TODO: parse and verify signatures from bidders of the auction
// Messages // Messages
{ {
json_t *messages;
size_t nm; size_t nm;
json_t *messages = json_object_get (jtr, "transcript");
messages = json_object_get (jtr, "transcript");
if (! json_is_array (messages)) if (! json_is_array (messages))
// TODO: fix leak! return json_error (jerror,
return json_error (output, "no messages found"); "no messages found");
nm = json_array_size (messages); nm = json_array_size (messages);
if (nm != (4 * tr->n)) if (nm != (4 * tr->n))
// TODO: fix leak! return json_error (jerror,
return json_error (output, "not the right no. of messages found"); "not the right no. of messages found");
/* TODO: parse and evaluate signatures */
} }
// Winners // Winners
{ {
json_t *winners;
size_t idx; size_t idx;
json_t *val; json_t *val;
json_t *winners = json_object_get (jtr, "winners");
winners = json_object_get (jtr, "winners");
if (! json_is_array (winners)) if (! json_is_array (winners))
{ {
GNUNET_log (GNUNET_ERROR_TYPE_WARNING, GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
LOG_PREFIX "winners not provided, continuing without\n"); LOG_PREFIX "winners not provided, continuing without\n");
// TODO: fix leakage goto DONE;
goto CONT;
} }
tr->expected_len = json_array_size (winners); tr->expected_len = json_array_size (winners);
@ -248,8 +321,6 @@ parse_transcript (const json_t *jtr,
struct result); struct result);
json_array_foreach (winners, idx, val) { json_array_foreach (winners, idx, val) {
char *error;
struct GNUNET_JSON_Specification spec[] = { struct GNUNET_JSON_Specification spec[] = {
GNUNET_JSON_spec_uint16 ("bidder", GNUNET_JSON_spec_uint16 ("bidder",
&(tr->expected[idx].bidder)), &(tr->expected[idx].bidder)),
@ -263,16 +334,21 @@ parse_transcript (const json_t *jtr,
if (GNUNET_OK != if (GNUNET_OK !=
GNUNET_JSON_parse (val, GNUNET_JSON_parse (val,
spec, spec,
(const char**) &error, NULL,
NULL)) NULL))
// TODO: fix leak! return json_error (jerror,
return json_error (output, "couldn't parse winners"); "couldn't parse winner no. %ld",
idx + 1);
} }
CONT:
// TODO: fix leakages
} }
*output = NULL;
// TODO: parse and evalue sig of seller
// TODO: check for max values
DONE:
*jerror = NULL;
return GNUNET_OK; return GNUNET_OK;
} }
@ -285,7 +361,7 @@ CONT:
* @param[out] result The JSON result from the program * @param[out] result The JSON result from the program
* @return GNUNET_OK on success * @return GNUNET_OK on success
* *
* TODO: Make this asynchronous (giving out a replay-ID to poll from)? * TODO: Make this resumable
*/ */
static enum GNUNET_GenericReturnValue static enum GNUNET_GenericReturnValue
replay_transcript (const json_t*root, replay_transcript (const json_t*root,
@ -338,6 +414,9 @@ replay_transcript (const json_t*root,
{ {
ssize_t sz; ssize_t sz;
char buf[MAX_RESULT_SIZE]; char buf[MAX_RESULT_SIZE];
json_error_t error;
json_t *res;
fd = GNUNET_DISK_pipe_handle (po, GNUNET_DISK_PIPE_END_READ); fd = GNUNET_DISK_pipe_handle (po, GNUNET_DISK_PIPE_END_READ);
sz = GNUNET_DISK_file_read (fd, sz = GNUNET_DISK_file_read (fd,
@ -347,21 +426,62 @@ replay_transcript (const json_t*root,
{ {
GNUNET_log (GNUNET_ERROR_TYPE_INFO, GNUNET_log (GNUNET_ERROR_TYPE_INFO,
LOG_PREFIX "couldn't read data from replay_program\n"); LOG_PREFIX "couldn't read data from replay_program\n");
return json_error (result, "internal error");
} }
else
buf[sz] = 0;
res = json_loads (buf,
JSON_DECODE_ANY,
&error);
if (! res)
{ {
json_error_t error; GNUNET_log (GNUNET_ERROR_TYPE_INFO,
json_t *res; LOG_PREFIX
res = json_loads (buf, "couldn't parse response from replay_program: %s (for '%s')\n",
JSON_DECODE_ANY, error.text,
&error); buf);
return json_error (result, error.text);
}
if (! res) // Handle error case first
return json_error (result, error.text); {
json_t *err = json_object_get (res,
"error");
if (NULL != err)
{
*result = json_copy (res);
json_decref (res);
return GNUNET_SYSERR;
}
}
// Parse the result
{
json_t *winners = json_object_get (res,
"winners");
if ((NULL == winners) ||
(! json_is_array (winners)))
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
LOG_PREFIX
"replay program didn't return a known result type, instead: '%s'\n",
json_dumps (res, JSON_INDENT (2)));
return json_error (result, "internal error");
}
{
// TODO: check each winner with tr->expected, if applicable
json_object_set (res, "exchange_sig", json_string (
"sig(priv_E, winners)"));
}
// TODO: return own result object.
*result = json_copy (res); *result = json_copy (res);
json_decref (res); json_decref (res);
} }
} }
if (GNUNET_OK != GNUNET_OS_process_wait (proc)) if (GNUNET_OK != GNUNET_OS_process_wait (proc))
@ -375,7 +495,6 @@ replay_transcript (const json_t*root,
} }
// TODO: parse result
return GNUNET_OK; return GNUNET_OK;
} }
@ -389,6 +508,7 @@ static void
auction_disable ( auction_disable (
struct TALER_Extension *ext) struct TALER_Extension *ext)
{ {
/* TODO: cleanup configuration */
ext->enabled = false; ext->enabled = false;
} }
@ -418,7 +538,7 @@ static json_t *
auction_config_to_json ( auction_config_to_json (
const struct TALER_Extension *ext) const struct TALER_Extension *ext)
{ {
/* This extension has no configuration */ /* TODO: add configuration */
return GNUNET_JSON_PACK ( return GNUNET_JSON_PACK (
GNUNET_JSON_pack_bool ("critical", ext->critical), GNUNET_JSON_pack_bool ("critical", ext->critical),
GNUNET_JSON_pack_string ("version", ext->version)); GNUNET_JSON_pack_string ("version", ext->version));
@ -436,7 +556,7 @@ auction_load_json_config (
struct TALER_Extension *ext, struct TALER_Extension *ext,
json_t *jconfig) json_t *jconfig)
{ {
/* This extension has no configuration */ /* TODO: add configuration */
ext->enabled = true; ext->enabled = true;
return GNUNET_OK; return GNUNET_OK;
} }
@ -465,6 +585,8 @@ auction_http_get_handler (
/** /**
* @brief implements the TALER_Extension.http_post_handler * @brief implements the TALER_Extension.http_post_handler
*
* TODO: make this non-blocking
*/ */
static MHD_RESULT static MHD_RESULT
auction_http_post_handler ( auction_http_post_handler (
@ -505,7 +627,7 @@ struct TALER_Extension TE_auction_brandt = {
.critical = false, .critical = false,
.version = "0", .version = "0",
.enabled = false, /* disabled per default */ .enabled = false, /* disabled per default */
.has_config = false, /* This extension has no configuration */ .has_config = true,
.config = NULL, .config = NULL,
.config_json = NULL, .config_json = NULL,
.disable = &auction_disable, .disable = &auction_disable,
@ -537,6 +659,12 @@ libtaler_extension_auction_brandt_init (void *arg)
{ {
const struct GNUNET_CONFIGURATION_Handle *cfg = arg; const struct GNUNET_CONFIGURATION_Handle *cfg = arg;
if (GNUNET_OK !=
TALER_config_get_currency (cfg,
&currency))
return NULL;
if (GNUNET_SYSERR == if (GNUNET_SYSERR ==
GNUNET_CONFIGURATION_get_value_string (cfg, GNUNET_CONFIGURATION_get_value_string (cfg,
TALER_EXTENSION_SECTION_PREFIX TALER_EXTENSION_SECTION_PREFIX
@ -549,11 +677,37 @@ libtaler_extension_auction_brandt_init (void *arg)
"REPLAY_PROGRAM"); "REPLAY_PROGRAM");
return NULL; return NULL;
} }
/* TODO: check if replay_program is actually executable */
/* check if replay_program is actually an executable */
{
struct stat sb;
if (0 != stat (replay_program, &sb))
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
LOG_PREFIX "replay_program '%s' not found\n",
replay_program);
return NULL;
}
if ( (sb.st_mode & S_IFDIR) ||
! (sb.st_mode & S_IXUSR))
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
LOG_PREFIX "replay_program '%s' is not an executable\n",
replay_program);
return NULL;
}
}
GNUNET_log (GNUNET_ERROR_TYPE_INFO, GNUNET_log (GNUNET_ERROR_TYPE_INFO,
LOG_PREFIX "loading... using replay_program '%s'\n", LOG_PREFIX "loading... using replay_program '%s'\n",
replay_program); replay_program);
/* TODO: read other config parameters and generate configuration */
return &TE_auction_brandt; return &TE_auction_brandt;
} }