Skip to content

Commit

Permalink
Improve matchingKeys
Browse files Browse the repository at this point in the history
Pass in rpmts instead of only a keyring
Move userdata pointer to end of signature and make it optional
Check for invalid character
  • Loading branch information
ffesti committed Oct 14, 2024
1 parent aa85c3f commit 8703bbe
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions tools/rpmkeys.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,12 @@ static struct poptOption optionsTable[] = {
POPT_TABLEEND
};

static int matchingKeys(rpmKeyring keyring, ARGV_const_t args, void * userdata, int callback(rpmPubkey, void*))
static int matchingKeys(rpmts ts, ARGV_const_t args, int callback(rpmPubkey, void*), void * userdata = NULL)
{
int ec = EXIT_SUCCESS;
rpmKeyring keyring = rpmtsGetKeyring(ts, 1);
char * c;

if (args) {
for (char * const * arg = args; *arg; arg++) {
int found = false;
Expand All @@ -59,6 +62,17 @@ static int matchingKeys(rpmKeyring keyring, ARGV_const_t args, void * userdata,
continue;
}

/* Check for valid hex chars */
for (c=*arg; *c; c++) {
if (strchr("0123456789abcdefABCDEF", *c) == NULL)
break;
}
if (*c) {
rpmlog(RPMLOG_ERR, ("invalid key id: %s\n"), *arg);
ec = EXIT_FAILURE;
continue;
}

auto iter = rpmKeyringInitIterator(keyring, 0);
rpmPubkey key = NULL;
while ((key = rpmKeyringIteratorNext(iter))) {
Expand Down Expand Up @@ -95,6 +109,7 @@ static int matchingKeys(rpmKeyring keyring, ARGV_const_t args, void * userdata,
ec = EXIT_FAILURE;
}
}
rpmKeyringFree(keyring);
return ec;
}

Expand All @@ -113,7 +128,6 @@ int main(int argc, char *argv[])
poptContext optCon = NULL;
rpmts ts = NULL;
ARGV_const_t args = NULL;
rpmKeyring keyring = NULL;

optCon = rpmcliInit(argc, argv, optionsTable);

Expand All @@ -129,7 +143,6 @@ int main(int argc, char *argv[])

ts = rpmtsCreate();
rpmtsSetRootDir(ts, rpmcliRootDir);
keyring = rpmtsGetKeyring(ts, 1);

switch (mode) {
case MODE_CHECKSIG:
Expand Down Expand Up @@ -164,15 +177,14 @@ int main(int argc, char *argv[])
}
case MODE_LISTKEY:
{
ec = matchingKeys(keyring, args, NULL, printKey);
ec = matchingKeys(ts, args, printKey);
break;
}
default:
argerror(_("only one major mode may be specified"));
}

exit:
rpmKeyringFree(keyring);
rpmtsFree(ts);
rpmcliFini(optCon);
fflush(stderr);
Expand Down

0 comments on commit 8703bbe

Please sign in to comment.