mirror of
git://git.gnupg.org/gnupg.git
synced 2025-03-28 22:49:59 +01:00
gpg: Factor some code code out of tofu.c
* g10/tofu.c (string_to_long): New. (string_to_ulong): New. (get_single_unsigned_long_cb): Replace strtol/strtoul by new function. (get_single_long_cb): Ditto. (signature_stats_collect_cb): Ditto. (get_policy): Ditto. (show_statistics): Ditto. Uese es_free instead of free. -- There is one minor semantic change: We now accept "nnn.0" always. The old code did not checked for ".0: in show_statistics. Signed-off-by: Werner Koch <wk@gnupg.org>
This commit is contained in:
parent
300b227cf4
commit
dcad99c986
164
g10/tofu.c
164
g10/tofu.c
@ -1,5 +1,5 @@
|
|||||||
/* tofu.c - TOFU trust model.
|
/* tofu.c - TOFU trust model.
|
||||||
* Copyright (C) 2015 g10 Code GmbH
|
* Copyright (C) 2015, 2016 g10 Code GmbH
|
||||||
*
|
*
|
||||||
* This file is part of GnuPG.
|
* This file is part of GnuPG.
|
||||||
*
|
*
|
||||||
@ -398,7 +398,66 @@ tofu_end_batch_update (void)
|
|||||||
end_transaction (db, 1);
|
end_transaction (db, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* Wrapper around strtol which prints a warning in case of a
|
||||||
|
* conversion error. On success the converted value is stored at
|
||||||
|
* R_VALUE and 0 is returned; on error FALLBACK is stored at R_VALUE
|
||||||
|
* and an error code is returned. */
|
||||||
|
static gpg_error_t
|
||||||
|
string_to_long (long *r_value, const char *string, long fallback, int line)
|
||||||
|
{
|
||||||
|
gpg_error_t err;
|
||||||
|
char *tail = NULL;
|
||||||
|
|
||||||
|
gpg_err_set_errno (0);
|
||||||
|
*r_value = strtol (string, &tail, 0);
|
||||||
|
if (errno || !(!strcmp (tail, ".0") || !*tail))
|
||||||
|
{
|
||||||
|
err = errno? gpg_error_from_errno (errno) : gpg_error (GPG_ERR_BAD_DATA);
|
||||||
|
log_debug ("%s:%d: "
|
||||||
|
"strtol failed for DB returned string (tail=%.10s): %s\n",
|
||||||
|
__FILE__, line, tail, gpg_strerror (err));
|
||||||
|
*r_value = fallback;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
err = 0;
|
||||||
|
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* Wrapper around strtoul which prints a warning in case of a
|
||||||
|
* conversion error. On success the converted value is stored at
|
||||||
|
* R_VALUE and 0 is returned; on error FALLBACK is stored at R_VALUE
|
||||||
|
* and an error code is returned. */
|
||||||
|
static gpg_error_t
|
||||||
|
string_to_ulong (unsigned long *r_value, const char *string,
|
||||||
|
unsigned long fallback, int line)
|
||||||
|
{
|
||||||
|
gpg_error_t err;
|
||||||
|
char *tail = NULL;
|
||||||
|
|
||||||
|
gpg_err_set_errno (0);
|
||||||
|
*r_value = strtoul (string, &tail, 0);
|
||||||
|
if (errno || !(!strcmp (tail, ".0") || !*tail))
|
||||||
|
{
|
||||||
|
err = errno? gpg_error_from_errno (errno) : gpg_error (GPG_ERR_BAD_DATA);
|
||||||
|
log_debug ("%s:%d: "
|
||||||
|
"strtoul failed for DB returned string (tail=%.10s): %s\n",
|
||||||
|
__FILE__, line, tail, gpg_strerror (err));
|
||||||
|
*r_value = fallback;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
err = 0;
|
||||||
|
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* Collect results of a select count (*) ...; style query. Aborts if
|
/* Collect results of a select count (*) ...; style query. Aborts if
|
||||||
the argument is not a valid integer (or real of the form X.0). */
|
the argument is not a valid integer (or real of the form X.0). */
|
||||||
static int
|
static int
|
||||||
@ -406,17 +465,13 @@ get_single_unsigned_long_cb (void *cookie, int argc, char **argv,
|
|||||||
char **azColName)
|
char **azColName)
|
||||||
{
|
{
|
||||||
unsigned long int *count = cookie;
|
unsigned long int *count = cookie;
|
||||||
char *tail = NULL;
|
|
||||||
|
|
||||||
(void) azColName;
|
(void) azColName;
|
||||||
|
|
||||||
log_assert (argc == 1);
|
log_assert (argc == 1);
|
||||||
|
|
||||||
errno = 0;
|
if (string_to_ulong (count, argv[0], 0, __LINE__))
|
||||||
*count = strtoul (argv[0], &tail, 0);
|
return 1; /* Abort. */
|
||||||
if (errno || ! (strcmp (tail, ".0") == 0 || *tail == '\0'))
|
|
||||||
/* Abort. */
|
|
||||||
return 1;
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1055,17 +1110,14 @@ static int
|
|||||||
get_single_long_cb (void *cookie, int argc, char **argv, char **azColName)
|
get_single_long_cb (void *cookie, int argc, char **argv, char **azColName)
|
||||||
{
|
{
|
||||||
long *count = cookie;
|
long *count = cookie;
|
||||||
char *tail = NULL;
|
|
||||||
|
|
||||||
(void) azColName;
|
(void) azColName;
|
||||||
|
|
||||||
log_assert (argc == 1);
|
log_assert (argc == 1);
|
||||||
|
|
||||||
errno = 0;
|
if (string_to_long (count, argv[0], 0, __LINE__))
|
||||||
*count = strtol (argv[0], &tail, 0);
|
return 1; /* Abort. */
|
||||||
if (errno || ! (strcmp (tail, ".0") == 0 || *tail == '\0'))
|
|
||||||
/* Abort. */
|
|
||||||
return 1;
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1356,43 +1408,28 @@ signature_stats_collect_cb (void *cookie, int argc, char **argv,
|
|||||||
char **azColName, sqlite3_stmt *stmt)
|
char **azColName, sqlite3_stmt *stmt)
|
||||||
{
|
{
|
||||||
struct signature_stats **statsp = cookie;
|
struct signature_stats **statsp = cookie;
|
||||||
char *tail;
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
enum tofu_policy policy;
|
enum tofu_policy policy;
|
||||||
long time_ago;
|
long time_ago;
|
||||||
unsigned long count;
|
unsigned long count;
|
||||||
|
long along;
|
||||||
|
|
||||||
(void) azColName;
|
(void) azColName;
|
||||||
(void) stmt;
|
(void) stmt;
|
||||||
|
|
||||||
i ++;
|
i ++;
|
||||||
|
|
||||||
tail = NULL;
|
if (string_to_long (&along, argv[i], 0, __LINE__))
|
||||||
errno = 0;
|
return 1; /* Abort */
|
||||||
policy = strtol (argv[i], &tail, 0);
|
policy = along;
|
||||||
if (errno || ! (strcmp (tail, ".0") == 0 || *tail == '\0'))
|
|
||||||
{
|
|
||||||
/* Abort. */
|
|
||||||
log_error ("%s: Error converting %s to an integer (tail = '%s')\n",
|
|
||||||
__func__, argv[i], tail);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
i ++;
|
i ++;
|
||||||
|
|
||||||
if (! argv[i])
|
if (! argv[i])
|
||||||
time_ago = 0;
|
time_ago = 0;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
tail = NULL;
|
if (string_to_long (&time_ago, argv[i], 0, __LINE__))
|
||||||
errno = 0;
|
return 1; /* Abort. */
|
||||||
time_ago = strtol (argv[i], &tail, 0);
|
|
||||||
if (errno || ! (strcmp (tail, ".0") == 0 || *tail == '\0'))
|
|
||||||
{
|
|
||||||
/* Abort. */
|
|
||||||
log_error ("%s: Error converting %s to an integer (tail = '%s')\n",
|
|
||||||
__func__, argv[i], tail);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
i ++;
|
i ++;
|
||||||
|
|
||||||
@ -1402,16 +1439,8 @@ signature_stats_collect_cb (void *cookie, int argc, char **argv,
|
|||||||
count = 0;
|
count = 0;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
tail = NULL;
|
if (string_to_ulong (&count, argv[i], 0, __LINE__))
|
||||||
errno = 0;
|
return 1; /* Abort */
|
||||||
count = strtoul (argv[i], &tail, 0);
|
|
||||||
if (errno || ! (strcmp (tail, ".0") == 0 || *tail == '\0'))
|
|
||||||
{
|
|
||||||
/* Abort. */
|
|
||||||
log_error ("%s: Error converting %s to an integer (tail = '%s')\n",
|
|
||||||
__func__, argv[i], tail);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
i ++;
|
i ++;
|
||||||
|
|
||||||
@ -1449,8 +1478,8 @@ get_policy (struct dbs *dbs, const char *fingerprint, const char *email,
|
|||||||
int rc;
|
int rc;
|
||||||
char *err = NULL;
|
char *err = NULL;
|
||||||
strlist_t strlist = NULL;
|
strlist_t strlist = NULL;
|
||||||
char *tail = NULL;
|
|
||||||
enum tofu_policy policy = _tofu_GET_POLICY_ERROR;
|
enum tofu_policy policy = _tofu_GET_POLICY_ERROR;
|
||||||
|
long along;
|
||||||
|
|
||||||
db = getdb (dbs, email, DB_EMAIL);
|
db = getdb (dbs, email, DB_EMAIL);
|
||||||
if (! db)
|
if (! db)
|
||||||
@ -1494,15 +1523,14 @@ get_policy (struct dbs *dbs, const char *fingerprint, const char *email,
|
|||||||
|
|
||||||
/* The result has the right form. */
|
/* The result has the right form. */
|
||||||
|
|
||||||
errno = 0;
|
if (string_to_long (&along, strlist->d, 0, __LINE__))
|
||||||
policy = strtol (strlist->d, &tail, 0);
|
|
||||||
if (errno || *tail != '\0')
|
|
||||||
{
|
{
|
||||||
log_error (_("error reading TOFU database: %s\n"),
|
log_error (_("error reading TOFU database: %s\n"),
|
||||||
gpg_strerror (GPG_ERR_BAD_DATA));
|
gpg_strerror (GPG_ERR_BAD_DATA));
|
||||||
print_further_info ("bad value for policy: %s", strlist->d);
|
print_further_info ("bad value for policy: %s", strlist->d);
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
policy = along;
|
||||||
|
|
||||||
if (! (policy == TOFU_POLICY_AUTO
|
if (! (policy == TOFU_POLICY_AUTO
|
||||||
|| policy == TOFU_POLICY_GOOD
|
|| policy == TOFU_POLICY_GOOD
|
||||||
@ -2347,52 +2375,24 @@ show_statistics (struct dbs *dbs, const char *fingerprint,
|
|||||||
fingerprint_pp);
|
fingerprint_pp);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
char *tail = NULL;
|
|
||||||
signed long messages;
|
signed long messages;
|
||||||
signed long first_seen_ago;
|
signed long first_seen_ago;
|
||||||
signed long most_recent_seen_ago;
|
signed long most_recent_seen_ago;
|
||||||
|
|
||||||
log_assert (strlist_length (strlist) == 3);
|
log_assert (strlist_length (strlist) == 3);
|
||||||
|
|
||||||
errno = 0;
|
string_to_long (&messages, strlist->d, -1, __LINE__);
|
||||||
messages = strtol (strlist->d, &tail, 0);
|
|
||||||
if (errno || *tail != '\0')
|
|
||||||
/* Abort. */
|
|
||||||
{
|
|
||||||
log_debug ("%s:%d: Couldn't convert %s (messages) to an int: %s.\n",
|
|
||||||
__func__, __LINE__, strlist->d, strerror (errno));
|
|
||||||
messages = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (messages == 0 && *strlist->next->d == '\0')
|
if (messages == 0 && *strlist->next->d == '\0')
|
||||||
/* min(NULL) => NULL => "". */
|
{ /* min(NULL) => NULL => "". */
|
||||||
{
|
|
||||||
first_seen_ago = -1;
|
first_seen_ago = -1;
|
||||||
most_recent_seen_ago = -1;
|
most_recent_seen_ago = -1;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
errno = 0;
|
string_to_long (&first_seen_ago, strlist->next->d, 0, __LINE__);
|
||||||
first_seen_ago = strtol (strlist->next->d, &tail, 0);
|
string_to_long (&most_recent_seen_ago, strlist->next->next->d, 0,
|
||||||
if (errno || *tail != '\0')
|
__LINE__);
|
||||||
/* Abort. */
|
|
||||||
{
|
|
||||||
log_debug ("%s:%d: Couldn't convert %s (first_seen) to an int: %s.\n",
|
|
||||||
__func__, __LINE__,
|
|
||||||
strlist->next->d, strerror (errno));
|
|
||||||
first_seen_ago = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
errno = 0;
|
|
||||||
most_recent_seen_ago = strtol (strlist->next->next->d, &tail, 0);
|
|
||||||
if (errno || *tail != '\0')
|
|
||||||
/* Abort. */
|
|
||||||
{
|
|
||||||
log_debug ("%s:%d: Couldn't convert %s (most_recent_seen) to an int: %s.\n",
|
|
||||||
__func__, __LINE__,
|
|
||||||
strlist->next->next->d, strerror (errno));
|
|
||||||
most_recent_seen_ago = 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (messages == -1 || first_seen_ago == 0)
|
if (messages == -1 || first_seen_ago == 0)
|
||||||
@ -2493,7 +2493,7 @@ show_statistics (struct dbs *dbs, const char *fingerprint,
|
|||||||
xfree (tmp);
|
xfree (tmp);
|
||||||
log_info ("%s", text);
|
log_info ("%s", text);
|
||||||
xfree (text);
|
xfree (text);
|
||||||
free (set_policy_command);
|
es_free (set_policy_command);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user