Skip to content

Commit 2e5730c

Browse files
pmatilaiffesti
authored andcommitted
Turn keystore into an interface class
Add an abstract rpm::keystore class and port our existing rpmdb and fs keystores to that. The keystore code as such doesn't really change at all in here, the bigger change is the way it's initialized because it's an object instead of just an integer in the rpmts struct. As a kind of side-effect, we introduce the rpm:: namespace here. Fixes: #3342
1 parent 43f3720 commit 2e5730c

File tree

4 files changed

+69
-83
lines changed

4 files changed

+69
-83
lines changed

lib/keystore.cc

+15-56
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@
1515
#include <rpm/rpmts.h>
1616
#include <rpm/rpmtypes.h>
1717

18-
#include "keystore.hh"
1918
#include "rpmts_internal.hh"
2019

2120
#include "debug.h"
2221

2322
using std::string;
23+
using namespace rpm;
2424

2525
static int makePubkeyHeader(rpmts ts, rpmPubkey key, Header * hdrp);
2626

27-
static rpmRC rpmtsLoadKeyringFromFiles(rpmtxn txn, rpmKeyring keyring)
27+
rpmRC keystore_fs::load_keys(rpmtxn txn, rpmKeyring keyring)
2828
{
2929
ARGV_t files = NULL;
3030
/* XXX TODO: deal with chroot path issues */
@@ -55,7 +55,7 @@ static rpmRC rpmtsLoadKeyringFromFiles(rpmtxn txn, rpmKeyring keyring)
5555
return RPMRC_OK;
5656
}
5757

58-
static rpmRC rpmtsDeleteFSKey(rpmtxn txn, const string & keyid, const string & newname = "")
58+
rpmRC keystore_fs::delete_key(rpmtxn txn, const string & keyid, const string & newname)
5959
{
6060
rpmRC rc = RPMRC_NOTFOUND;
6161
string keyglob = "gpg-pubkey-" + keyid + "*.key";
@@ -74,12 +74,12 @@ static rpmRC rpmtsDeleteFSKey(rpmtxn txn, const string & keyid, const string & n
7474
return rc;
7575
}
7676

77-
static rpmRC rpmtsDeleteFSKey(rpmtxn txn, rpmPubkey key)
77+
rpmRC keystore_fs::delete_key(rpmtxn txn, rpmPubkey key)
7878
{
79-
return rpmtsDeleteFSKey(txn, rpmPubkeyFingerprintAsHex(key));
79+
return delete_key(txn, rpmPubkeyFingerprintAsHex(key));
8080
}
8181

82-
static rpmRC rpmtsImportFSKey(rpmtxn txn, rpmPubkey key, rpmFlags flags, int replace)
82+
rpmRC keystore_fs::import_key(rpmtxn txn, rpmPubkey key, rpmFlags flags, int replace)
8383
{
8484
rpmRC rc = RPMRC_FAIL;
8585
const char *fp = rpmPubkeyFingerprintAsHex(key);
@@ -117,9 +117,9 @@ static rpmRC rpmtsImportFSKey(rpmtxn txn, rpmPubkey key, rpmFlags flags, int rep
117117

118118
if (!rc && replace) {
119119
/* find and delete the old pubkey entry */
120-
if (rpmtsDeleteFSKey(txn, fp, keyfmt) == RPMRC_NOTFOUND) {
120+
if (delete_key(txn, fp, keyfmt) == RPMRC_NOTFOUND) {
121121
/* make sure an old, short keyid version gets removed */
122-
rpmtsDeleteFSKey(txn, fp+32, keyfmt);
122+
delete_key(txn, fp+32, keyfmt);
123123
}
124124
}
125125

@@ -131,7 +131,7 @@ static rpmRC rpmtsImportFSKey(rpmtxn txn, rpmPubkey key, rpmFlags flags, int rep
131131
return rc;
132132
}
133133

134-
static rpmRC rpmtsLoadKeyringFromDB(rpmtxn txn, rpmKeyring keyring)
134+
rpmRC keystore_rpmdb::load_keys(rpmtxn txn, rpmKeyring keyring)
135135
{
136136
Header h;
137137
rpmdbMatchIterator mi;
@@ -170,7 +170,7 @@ static rpmRC rpmtsLoadKeyringFromDB(rpmtxn txn, rpmKeyring keyring)
170170
return RPMRC_OK;
171171
}
172172

173-
static rpmRC rpmtsDeleteDBKey(rpmtxn txn, const string & keyid, unsigned int newinstance = 0)
173+
rpmRC keystore_rpmdb::delete_key(rpmtxn txn, const string & keyid, unsigned int newinstance)
174174
{
175175
rpmts ts = rpmtxnTs(txn);
176176
if (rpmtsOpenDB(ts, (O_RDWR|O_CREAT)))
@@ -194,12 +194,12 @@ static rpmRC rpmtsDeleteDBKey(rpmtxn txn, const string & keyid, unsigned int new
194194
return rc;
195195
}
196196

197-
static rpmRC rpmtsDeleteDBKey(rpmtxn txn, rpmPubkey key)
197+
rpmRC keystore_rpmdb::delete_key(rpmtxn txn, rpmPubkey key)
198198
{
199-
return rpmtsDeleteDBKey(txn, rpmPubkeyFingerprintAsHex(key));
199+
return delete_key(txn, rpmPubkeyFingerprintAsHex(key));
200200
}
201201

202-
static rpmRC rpmtsImportDBKey(rpmtxn txn, rpmPubkey key, rpmFlags flags, int replace)
202+
rpmRC keystore_rpmdb::import_key(rpmtxn txn, rpmPubkey key, rpmFlags flags, int replace)
203203
{
204204
Header h = NULL;
205205
rpmRC rc = RPMRC_FAIL;
@@ -213,9 +213,9 @@ static rpmRC rpmtsImportDBKey(rpmtxn txn, rpmPubkey key, rpmFlags flags, int rep
213213
/* find and delete the old pubkey entry */
214214
unsigned int newinstance = headerGetInstance(h);
215215
char *keyid = headerFormat(h, "%{version}", NULL);
216-
if (rpmtsDeleteDBKey(txn, keyid, newinstance) == RPMRC_NOTFOUND) {
216+
if (delete_key(txn, keyid, newinstance) == RPMRC_NOTFOUND) {
217217
/* make sure an old, short keyid version gets removed */
218-
rpmtsDeleteDBKey(txn, keyid+32, newinstance);
218+
delete_key(txn, keyid+32, newinstance);
219219
}
220220
free(keyid);
221221
}
@@ -372,44 +372,3 @@ static int makePubkeyHeader(rpmts ts, rpmPubkey key, Header * hdrp)
372372

373373
return rc;
374374
}
375-
376-
rpmRC rpmKeystoreImportPubkey(rpmtxn txn, rpmPubkey key, int replace)
377-
{
378-
rpmRC rc = RPMRC_FAIL;
379-
rpmts ts = rpmtxnTs(txn);
380-
381-
/* Add header to database. */
382-
if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_TEST)) {
383-
if (ts->keyringtype == KEYRING_FS)
384-
rc = rpmtsImportFSKey(txn, key, 0, replace);
385-
else
386-
rc = rpmtsImportDBKey(txn, key, 0, replace);
387-
} else {
388-
rc = RPMRC_OK;
389-
}
390-
return rc;
391-
}
392-
393-
rpmRC rpmKeystoreDeletePubkey(rpmtxn txn, rpmPubkey key)
394-
{
395-
rpmRC rc = RPMRC_FAIL;
396-
rpmts ts = rpmtxnTs(txn);
397-
if (ts->keyringtype == KEYRING_FS)
398-
rc = rpmtsDeleteFSKey(txn, key);
399-
else
400-
rc = rpmtsDeleteDBKey(txn, key);
401-
return rc;
402-
}
403-
404-
rpmRC rpmKeystoreLoad(rpmtxn txn, rpmKeyring keyring)
405-
{
406-
rpmRC rc = RPMRC_FAIL;
407-
rpmts ts = rpmtxnTs(txn);
408-
if (ts->keyringtype == KEYRING_FS) {
409-
rc = rpmtsLoadKeyringFromFiles(txn, keyring);
410-
} else {
411-
rc = rpmtsLoadKeyringFromDB(txn, keyring);
412-
}
413-
return rc;
414-
}
415-

lib/keystore.hh

+31-9
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,42 @@
11
#ifndef _KEYSTORE_H
2+
#define _KEYSTORE_H
3+
4+
#include <string>
25

36
#include <rpm/rpmtypes.h>
47
#include <rpm/rpmutil.h>
58

6-
enum {
7-
KEYRING_RPMDB = 1,
8-
KEYRING_FS = 2,
9+
namespace rpm {
10+
11+
class keystore {
12+
public:
13+
virtual rpmRC load_keys(rpmtxn txn, rpmKeyring keyring) = 0;
14+
virtual rpmRC import_key(rpmtxn txn, rpmPubkey key, rpmFlags flags = 0, int replace = 1) = 0;
15+
virtual rpmRC delete_key(rpmtxn txn, rpmPubkey key) = 0;
16+
17+
virtual ~keystore() = default;
918
};
1019

11-
RPM_GNUC_INTERNAL
12-
rpmRC rpmKeystoreLoad(rpmtxn txn, rpmKeyring keyring);
20+
class keystore_fs : public keystore {
21+
public:
22+
virtual rpmRC load_keys(rpmtxn txn, rpmKeyring keyring);
23+
virtual rpmRC import_key(rpmtxn txn, rpmPubkey key, rpmFlags flags = 0, int replace = 1);
24+
virtual rpmRC delete_key(rpmtxn txn, rpmPubkey key);
1325

14-
RPM_GNUC_INTERNAL
15-
rpmRC rpmKeystoreImportPubkey(rpmtxn txn, rpmPubkey key, int replace = 0);
26+
private:
27+
rpmRC delete_key(rpmtxn txn, const std::string & keyid, const std::string & newname = "");
28+
};
29+
30+
class keystore_rpmdb : public keystore {
31+
public:
32+
virtual rpmRC load_keys(rpmtxn txn, rpmKeyring keyring);
33+
virtual rpmRC import_key(rpmtxn txn, rpmPubkey key, rpmFlags flags = 0, int replace = 1);
34+
virtual rpmRC delete_key(rpmtxn txn, rpmPubkey key);
35+
36+
private:
37+
rpmRC delete_key(rpmtxn txn, const std::string & keyid, unsigned int newinstance = 0);
38+
};
1639

17-
RPM_GNUC_INTERNAL
18-
rpmRC rpmKeystoreDeletePubkey(rpmtxn txn, rpmPubkey key);
40+
}; /* namespace */
1941

2042
#endif /* _KEYSTORE_H */

lib/rpmts.cc

+21-17
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "debug.h"
3737

3838
using std::string;
39+
using namespace rpm;
3940

4041
/**
4142
* Iterator across transaction elements, forward on install, backward on erase.
@@ -264,34 +265,37 @@ int rpmtsSetKeyring(rpmts ts, rpmKeyring keyring)
264265
return 0;
265266
}
266267

267-
static int getKeyringType(void)
268+
static keystore *getKeystore(rpmts ts)
268269
{
269-
int kt = KEYRING_RPMDB;
270-
char *krtype = rpmExpand("%{?_keyring}", NULL);
270+
if (ts->keystore == NULL) {
271+
char *krtype = rpmExpand("%{?_keyring}", NULL);
271272

272-
if (rstreq(krtype, "fs")) {
273-
kt = KEYRING_FS;
274-
} else if (*krtype && !rstreq(krtype, "rpmdb")) {
275-
/* Fall back to using rpmdb if unknown, for now at least */
276-
rpmlog(RPMLOG_WARNING,
277-
_("unknown keyring type: %s, using rpmdb\n"), krtype);
273+
if (rstreq(krtype, "fs")) {
274+
ts->keystore = new keystore_fs();
275+
} else if (rstreq(krtype, "rpmdb")) {
276+
ts->keystore = new keystore_rpmdb();
277+
} else {
278+
/* Fall back to using rpmdb if unknown, for now at least */
279+
rpmlog(RPMLOG_WARNING,
280+
_("unknown keyring type: %s, using rpmdb\n"), krtype);
281+
ts->keystore = new keystore_rpmdb();
282+
}
283+
free(krtype);
278284
}
279-
free(krtype);
280285

281-
return kt;
286+
return ts->keystore;
282287
}
283288

284289
static void loadKeyring(rpmts ts)
285290
{
286291
/* Never load the keyring if signature checking is disabled */
287292
if ((rpmtsVSFlags(ts) & RPMVSF_MASK_NOSIGNATURES) !=
288293
RPMVSF_MASK_NOSIGNATURES) {
289-
if (!ts->keyringtype)
290-
ts->keyringtype = getKeyringType();
294+
ts->keystore = getKeystore(ts);
291295
ts->keyring = rpmKeyringNew();
292296
rpmtxn txn = rpmtxnBegin(ts, RPMTXN_READ);
293297
if (txn) {
294-
rpmKeystoreLoad(txn, ts->keyring);
298+
ts->keystore->load_keys(txn, ts->keyring);
295299
rpmtxnEnd(txn);
296300
}
297301
}
@@ -365,7 +369,7 @@ rpmRC rpmtxnImportPubkey(rpmtxn txn, const unsigned char * pkt, size_t pktlen)
365369

366370
/* If we dont already have the key, make a persistent record of it */
367371
if (krc == 0) {
368-
rc = rpmKeystoreImportPubkey(txn, pubkey, oldkey ? 1 : 0);
372+
rc = ts->keystore->import_key(txn, pubkey, 0, oldkey ? 1 : 0);
369373
} else {
370374
rc = RPMRC_OK; /* already have key */
371375
}
@@ -395,7 +399,7 @@ rpmRC rpmtxnDeletePubkey(rpmtxn txn, rpmPubkey key)
395399
if ((rpmtsFlags(ts) & RPMTRANS_FLAG_TEST)) {
396400
rc = RPMRC_OK;
397401
} else {
398-
rc = rpmKeystoreDeletePubkey(txn, key);
402+
rc = ts->keystore->delete_key(txn, key);
399403
}
400404
rc = RPMRC_OK;
401405
rpmKeyringFree(keyring);
@@ -554,6 +558,7 @@ rpmts rpmtsFree(rpmts ts)
554558
(void) rpmtsCloseDB(ts);
555559

556560
delete ts->members;
561+
delete ts->keystore;
557562

558563
if (ts->scriptFd != NULL) {
559564
ts->scriptFd = fdFree(ts->scriptFd);
@@ -961,7 +966,6 @@ rpmts rpmtsCreate(void)
961966

962967
ts->rootDir = NULL;
963968
ts->keyring = NULL;
964-
ts->keyringtype = 0;
965969
ts->vfyflags = rpmExpandNumeric("%{?_pkgverify_flags}");
966970
ts->vfylevel = vfylevel_init();
967971

lib/rpmts_internal.hh

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "rpmal.hh" /* XXX availablePackage */
1313
#include "fprint.hh"
14+
#include "keystore.hh"
1415
#include "rpmlock.hh"
1516
#include "rpmdb_internal.hh"
1617
#include "rpmscript.hh"
@@ -84,7 +85,7 @@ struct rpmts_s {
8485
rpmVSFlags vfyflags; /*!< Package verification flags */
8586
int vfylevel; /*!< Package verification level */
8687
rpmKeyring keyring; /*!< Keyring in use. */
87-
int keyringtype; /*!< Keyring type */
88+
rpm::keystore *keystore; /*! <Keystore in use. */
8889

8990
ARGV_t netsharedPaths; /*!< From %{_netsharedpath} */
9091
ARGV_t installLangs; /*!< From %{_install_langs} */

0 commit comments

Comments
 (0)