Skip to content

Commit

Permalink
Pass rpmPubkey instance to rpmtxnDeletePubkey
Browse files Browse the repository at this point in the history
Use the matchingKeys() in rpmkeys to acquire those rpmPubkey instances.

Use EXIT_FAILURE as exit code for rpmkeys --delete instead of the
count of errors.
  • Loading branch information
ffesti authored and pmatilai committed Oct 15, 2024
1 parent 9618fcd commit 1181544
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 30 deletions.
6 changes: 3 additions & 3 deletions include/rpm/rpmts.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <rpm/rpmsw.h>
#include <rpm/rpmfi.h>
#include <rpm/rpmcallback.h>
#include <rpm/rpmkeyring.h>

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -353,13 +354,12 @@ rpmRC rpmtxnImportPubkey(rpmtxn txn, const unsigned char * pkt, size_t pktlen);
/** \ingroup rpmts
* Delete public key from transaction keystore.
* @param txn transaction handle
* @param keyid key fingerprint or keyid (in hex)
* @param key public key
* @return RPMRC_OK on success
* RPMRC_NOTFOUND if key not found
* RPMRC_NOKEY on invalid keyid
* RPMRC_FAIL on other failure
*/
rpmRC rpmtxnDeletePubkey(rpmtxn txn, const char *keyid);
rpmRC rpmtxnDeletePubkey(rpmtxn txn, rpmPubkey key);

/** \ingroup rpmts
* Retrieve handle for keyring used for this transaction set
Expand Down
16 changes: 5 additions & 11 deletions lib/rpmts.cc
Original file line number Diff line number Diff line change
Expand Up @@ -774,17 +774,10 @@ rpmRC rpmtxnImportPubkey(rpmtxn txn, const unsigned char * pkt, size_t pktlen)
return rc;
}

rpmRC rpmtxnDeletePubkey(rpmtxn txn, const char *keyid)
rpmRC rpmtxnDeletePubkey(rpmtxn txn, rpmPubkey key)
{
rpmRC rc = RPMRC_FAIL;
size_t klen = strlen(keyid);

/* Allow short keyid while we're transitioning */
if (klen != 40 && klen != 16 && klen != 8)
return RPMRC_NOKEY;

if (!rpmIsValidHex(keyid, klen))
return RPMRC_NOKEY;
char * keyid = rpmPubkeyKeyIDAsHex(key);

if (txn) {
/* force keyring load */
Expand All @@ -797,12 +790,13 @@ rpmRC rpmtxnDeletePubkey(rpmtxn txn, const char *keyid)
rc = RPMRC_OK;
if (!(rpmtsFlags(txn->ts) & RPMTRANS_FLAG_TEST)) {
if (txn->ts->keyringtype == KEYRING_FS)
rc = rpmtsDeleteFSKey(txn, keyid);
rc = rpmtsDeleteFSKey(txn, keyid+8);
else
rc = rpmtsDeleteDBKey(txn, keyid);
rc = rpmtsDeleteDBKey(txn, keyid+8);
}
rpmKeyringFree(keyring);
}
free(keyid);
return rc;
}

Expand Down
4 changes: 2 additions & 2 deletions tests/rpmsigdig.at
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ runroot rpmkeys -Kv /data/RPMS/hello-2.0-1.x86_64-signed-with-new-subkey.rpm
RPMTEST_CHECK([
runroot rpmkeys --delete abcd gimmekey 1111aaaa2222bbbb
],
[3],
[1],
[],
[error: invalid key id: abcd
error: invalid key id: gimmekey
Expand Down Expand Up @@ -143,7 +143,7 @@ runroot rpmkeys -Kv /data/RPMS/hello-2.0-1.x86_64-signed-with-new-subkey.rpm
RPMTEST_CHECK([
runroot rpmkeys --delete abcd gimmekey 1111aaaa2222bbbb
],
[3],
[1],
[],
[error: invalid key id: abcd
error: invalid key id: gimmekey
Expand Down
22 changes: 8 additions & 14 deletions tools/rpmkeys.cc
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ static int printKey(rpmPubkey key, void * data)
return 0;
}

static int deleteKey(rpmPubkey key, void * data)
{
rpmtxn txn = (rpmtxn) data;
rpmtxnDeletePubkey(txn, key);
return 0;
}

int main(int argc, char *argv[])
{
int ec = EXIT_FAILURE;
Expand Down Expand Up @@ -151,20 +158,7 @@ int main(int argc, char *argv[])
{
rpmtxn txn = rpmtxnBegin(ts, RPMTXN_WRITE);
if (txn) {
int nfail = 0;
for (char const * const *arg = args; *arg && **arg; arg++) {
rpmRC delrc = rpmtxnDeletePubkey(txn, *arg);
if (delrc) {
if (delrc == RPMRC_NOTFOUND)
rpmlog(RPMLOG_ERR, ("key not found: %s\n"), *arg);
else if (delrc == RPMRC_NOKEY)
rpmlog(RPMLOG_ERR, ("invalid key id: %s\n"), *arg);
else if (delrc == RPMRC_FAIL)
rpmlog(RPMLOG_ERR, ("failed to delete key: %s\n"), *arg);
nfail++;
}
}
ec = nfail;
ec = matchingKeys(ts, args, deleteKey, txn);
rpmtxnEnd(txn);
}
break;
Expand Down

0 comments on commit 1181544

Please sign in to comment.