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
8 * $Id: keydb_db2.c,v 1.10 2003/09/30 20:40:10 noodles Exp $
11 #include <sys/types.h>
21 #include "charfuncs.h"
25 #include "keystructs.h"
28 #include "onak-conf.h"
31 #define KEYDB_KEYID_BYTES 4
34 * db2_numdb - The number of database files we have.
36 static int db2_numdb = 16;
39 * db2_keydbfiles - An array of DB structs for our key database files.
41 static DB **db2_keydbfiles = NULL;
44 * db2_env - Database environment variable.
46 static DB_ENV db2_env;
51 * keyid's are 8 bytes, msb first. so start from the end. use 16
52 * bits, since that's enough to divide by any small number of db files.
54 unsigned char *keydata = (unsigned char *) key->data;
55 unsigned long keyidnum;
57 keyidnum = (keydata[KEYDB_KEYID_BYTES-2]<<8)|keydata[KEYDB_KEYID_BYTES-1];
58 return(db2_keydbfiles[keyidnum % db2_numdb]);
62 * initdb - Initialize the key database.
64 * This function should be called before any of the other functions in
65 * this file are called in order to allow the DB to be initialized ready
77 snprintf(buf, sizeof(buf) - 1, "%s/num_keydb", config.db_dir);
78 numdb = fopen(buf, "r");
80 if (fgets(buf, sizeof(buf), numdb) != NULL) {
81 db2_numdb = atoi(buf);
85 logthing(LOGTHING_ERROR, "Couldn't open num_keydb: %s",
89 memset(&db2_env, 0, sizeof(db2_env));
92 * Tunable param. Just using what pksd does for the moment. Bigger uses
93 * more memory but improves performance. Bigger than physical memory
96 db2_env.mp_size = 20 * 1024 * 1024;
98 ret = db_appinit(config.db_dir, NULL,
99 &db2_env, DB_INIT_MPOOL|DB_INIT_LOCK);
101 db2_keydbfiles = (DB **) malloc(sizeof (DB *) * db2_numdb);
102 memset(&keydbinfo, 0, sizeof(keydbinfo));
103 keydbinfo.db_pagesize = 8192;
104 for (i = 0; i < db2_numdb; i++) {
105 db2_keydbfiles[i] = NULL;
106 snprintf(keydbname, 19, "keydb%03d", i);
107 ret = db_open(keydbname, DB_HASH, DB_RDONLY, 0644,
108 &db2_env, &keydbinfo,
111 logthing(LOGTHING_CRITICAL,
112 "Error opening db file %d (errno %d)",
118 logthing(LOGTHING_CRITICAL, "Error initializing db (%d).",
125 * cleanupdb - De-initialize the key database.
127 * This function should be called upon program exit to allow the DB to
128 * cleanup after itself.
134 for (i = 0; i < db2_numdb; i++) {
135 if (db2_keydbfiles[i] != NULL) {
136 (*(db2_keydbfiles[i]->close))(db2_keydbfiles[i], 0);
137 db2_keydbfiles[i] = NULL;
141 db_appexit(&db2_env);
145 * starttrans - Start a transaction.
147 * Start a transaction. Intended to be used if we're about to perform many
148 * operations on the database to help speed it all up, or if we want
149 * something to only succeed if all relevant operations are successful.
151 bool starttrans(void)
157 * endtrans - End a transaction.
159 * Ends a transaction.
167 * fetch_key - Given a keyid fetch the key from storage.
168 * @keyid: The keyid to fetch.
169 * @publickey: A pointer to a structure to return the key in.
170 * @intrans: If we're already in a transaction.
172 * We use the hex representation of the keyid as the filename to fetch the
173 * key from. The key is stored in the file as a binary OpenPGP stream of
174 * packets, so we can just use read_openpgp_stream() to read the packets
175 * in and then parse_keys() to parse the packets into a publickey
178 int fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
181 struct openpgp_packet_list *packets = NULL;
184 char id[KEYDB_KEYID_BYTES];
185 struct buffer_ctx fetchbuf;
187 memset(&key, 0, sizeof(key));
188 memset(&data, 0, sizeof(data));
190 id[0] = (keyid >> 24) & 0xFF;
191 id[1] = (keyid >> 16) & 0xFF;
192 id[2] = (keyid >> 8) & 0xFF;
193 id[3] = keyid & 0xFF;
196 key.size = KEYDB_KEYID_BYTES;
198 ret = (*(keydb(&key)->get))(keydb(&key), NULL, &key, &data, 0);
200 fetchbuf.buffer = data.data;
202 fetchbuf.size = data.size;
203 read_openpgp_stream(buffer_fetchchar, &fetchbuf, &packets, 0);
204 parse_keys(packets, publickey);
205 free_packet_list(packets);
213 * fetch_key_text - Trys to find the keys that contain the supplied text.
214 * @search: The text to search for.
215 * @publickey: A pointer to a structure to return the key in.
217 * This function searches for the supplied text and returns the keys that
220 int fetch_key_text(const char *search, struct openpgp_publickey **publickey)
226 * store_key - Takes a key and stores it.
227 * @publickey: A pointer to the public key to store.
228 * @intrans: If we're already in a transaction.
229 * @update: If true the key exists and should be updated.
231 * Again we just use the hex representation of the keyid as the filename
232 * to store the key to. We flatten the public key to a list of OpenPGP
233 * packets and then use write_openpgp_stream() to write the stream out to
236 int store_key(struct openpgp_publickey *publickey, bool intrans, bool update)
242 * delete_key - Given a keyid delete the key from storage.
243 * @keyid: The keyid to delete.
244 * @intrans: If we're already in a transaction.
246 * This function deletes a public key from whatever storage mechanism we
247 * are using. Returns 0 if the key existed.
249 int delete_key(uint64_t keyid, bool intrans)
255 * dumpdb - dump the key database
256 * @filenamebase: The base filename to use for the dump.
258 * Dumps the database into one or more files, which contain pure OpenPGP
259 * that can be reimported into onak or gpg. filenamebase provides a base
260 * file name for the dump; several files may be created, all of which will
261 * begin with this string and then have a unique number and a .pgp
264 int dumpdb(char *filenamebase)
271 * Include the basic keydb routines.
273 #define NEED_KEYID2UID 1
274 #define NEED_GETKEYSIGS 1
275 #define NEED_GETFULLKEYID 1