2 * keydb_db2.c - Routines to store and fetch keys in a DB2 file (a la pksd)
4 * Jonathan McDowell <noodles@earth.li>
6 * Copyright 2002 Project Purple
19 #include "charfuncs.h"
23 #include "keystructs.h"
26 #include "onak-conf.h"
29 #define KEYDB_KEYID_BYTES 4
32 * db2_numdb - The number of database files we have.
34 static int db2_numdb = 16;
37 * db2_keydbfiles - An array of DB structs for our key database files.
39 static DB **db2_keydbfiles = NULL;
42 * db2_env - Database environment variable.
44 static DB_ENV db2_env;
49 * keyid's are 8 bytes, msb first. so start from the end. use 16
50 * bits, since that's enough to divide by any small number of db files.
52 unsigned char *keydata = (unsigned char *) key->data;
53 unsigned long keyidnum;
55 keyidnum = (keydata[KEYDB_KEYID_BYTES-2]<<8)|keydata[KEYDB_KEYID_BYTES-1];
56 return(db2_keydbfiles[keyidnum % db2_numdb]);
60 * initdb - Initialize the key database.
62 * This function should be called before any of the other functions in
63 * this file are called in order to allow the DB to be initialized ready
66 void initdb(bool readonly)
75 snprintf(buf, sizeof(buf) - 1, "%s/num_keydb", config.db_dir);
76 numdb = fopen(buf, "r");
78 if (fgets(buf, sizeof(buf), numdb) != NULL) {
79 db2_numdb = atoi(buf);
83 logthing(LOGTHING_ERROR, "Couldn't open num_keydb: %s",
87 memset(&db2_env, 0, sizeof(db2_env));
90 * Tunable param. Just using what pksd does for the moment. Bigger uses
91 * more memory but improves performance. Bigger than physical memory
94 db2_env.mp_size = 20 * 1024 * 1024;
96 ret = db_appinit(config.db_dir, NULL,
97 &db2_env, DB_INIT_MPOOL|DB_INIT_LOCK);
99 db2_keydbfiles = (DB **) malloc(sizeof (DB *) * db2_numdb);
100 memset(&keydbinfo, 0, sizeof(keydbinfo));
101 keydbinfo.db_pagesize = 8192;
102 for (i = 0; i < db2_numdb; i++) {
103 db2_keydbfiles[i] = NULL;
104 snprintf(keydbname, 19, "keydb%03d", i);
105 ret = db_open(keydbname, DB_HASH, DB_RDONLY, 0644,
106 &db2_env, &keydbinfo,
109 logthing(LOGTHING_CRITICAL,
110 "Error opening db file %d (errno %d)",
116 logthing(LOGTHING_CRITICAL, "Error initializing db (%d).",
123 * cleanupdb - De-initialize the key database.
125 * This function should be called upon program exit to allow the DB to
126 * cleanup after itself.
132 for (i = 0; i < db2_numdb; i++) {
133 if (db2_keydbfiles[i] != NULL) {
134 (*(db2_keydbfiles[i]->close))(db2_keydbfiles[i], 0);
135 db2_keydbfiles[i] = NULL;
139 db_appexit(&db2_env);
143 * starttrans - Start a transaction.
145 * Start a transaction. Intended to be used if we're about to perform many
146 * operations on the database to help speed it all up, or if we want
147 * something to only succeed if all relevant operations are successful.
149 bool starttrans(void)
155 * endtrans - End a transaction.
157 * Ends a transaction.
165 * fetch_key - Given a keyid fetch the key from storage.
166 * @keyid: The keyid to fetch.
167 * @publickey: A pointer to a structure to return the key in.
168 * @intrans: If we're already in a transaction.
170 * We use the hex representation of the keyid as the filename to fetch the
171 * key from. The key is stored in the file as a binary OpenPGP stream of
172 * packets, so we can just use read_openpgp_stream() to read the packets
173 * in and then parse_keys() to parse the packets into a publickey
176 int fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
179 struct openpgp_packet_list *packets = NULL;
182 char id[KEYDB_KEYID_BYTES];
183 struct buffer_ctx fetchbuf;
185 memset(&key, 0, sizeof(key));
186 memset(&data, 0, sizeof(data));
188 id[0] = (keyid >> 24) & 0xFF;
189 id[1] = (keyid >> 16) & 0xFF;
190 id[2] = (keyid >> 8) & 0xFF;
191 id[3] = keyid & 0xFF;
194 key.size = KEYDB_KEYID_BYTES;
196 ret = (*(keydb(&key)->get))(keydb(&key), NULL, &key, &data, 0);
198 fetchbuf.buffer = data.data;
200 fetchbuf.size = data.size;
201 read_openpgp_stream(buffer_fetchchar, &fetchbuf, &packets, 0);
202 parse_keys(packets, publickey);
203 free_packet_list(packets);
211 * fetch_key_text - Trys to find the keys that contain the supplied text.
212 * @search: The text to search for.
213 * @publickey: A pointer to a structure to return the key in.
215 * This function searches for the supplied text and returns the keys that
218 int fetch_key_text(const char *search, struct openpgp_publickey **publickey)
224 * store_key - Takes a key and stores it.
225 * @publickey: A pointer to the public key to store.
226 * @intrans: If we're already in a transaction.
227 * @update: If true the key exists and should be updated.
229 * Again we just use the hex representation of the keyid as the filename
230 * to store the key to. We flatten the public key to a list of OpenPGP
231 * packets and then use write_openpgp_stream() to write the stream out to
234 int store_key(struct openpgp_publickey *publickey, bool intrans, bool update)
240 * delete_key - Given a keyid delete the key from storage.
241 * @keyid: The keyid to delete.
242 * @intrans: If we're already in a transaction.
244 * This function deletes a public key from whatever storage mechanism we
245 * are using. Returns 0 if the key existed.
247 int delete_key(uint64_t keyid, bool intrans)
253 * dumpdb - dump the key database
254 * @filenamebase: The base filename to use for the dump.
256 * Dumps the database into one or more files, which contain pure OpenPGP
257 * that can be reimported into onak or gpg. filenamebase provides a base
258 * file name for the dump; several files may be created, all of which will
259 * begin with this string and then have a unique number and a .pgp
262 int dumpdb(char *filenamebase)
269 * Include the basic keydb routines.
271 #define NEED_KEYID2UID 1
272 #define NEED_GETKEYSIGS 1
273 #define NEED_GETFULLKEYID 1
274 #define NEED_UPDATEKEYS 1