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
73 memset(&db2_env, 0, sizeof(db2_env));
76 * Tunable param. Just using what pksd does for the moment. Bigger uses
77 * more memory but improves performance. Bigger than physical memory
80 db2_env.mp_size = 20 * 1024 * 1024;
82 ret = db_appinit(config.db_dir, NULL,
83 &db2_env, DB_INIT_MPOOL|DB_INIT_LOCK);
85 db2_keydbfiles = (DB **) malloc(sizeof (DB *) * db2_numdb);
86 memset(&keydbinfo, 0, sizeof(keydbinfo));
87 keydbinfo.db_pagesize = 8192;
88 for (i = 0; i < db2_numdb; i++) {
89 db2_keydbfiles[i] = NULL;
90 snprintf(keydbname, 19, "keydb%03d", i);
91 ret = db_open(keydbname, DB_HASH, DB_RDONLY, 0644,
95 logthing(LOGTHING_CRITICAL,
96 "Error opening db file %d (errno %d)",
102 logthing(LOGTHING_CRITICAL, "Error initializing db (%d).",
109 * cleanupdb - De-initialize the key database.
111 * This function should be called upon program exit to allow the DB to
112 * cleanup after itself.
118 for (i = 0; i < db2_numdb; i++) {
119 if (db2_keydbfiles[i] != NULL) {
120 (*(db2_keydbfiles[i]->close))(db2_keydbfiles[i], 0);
121 db2_keydbfiles[i] = NULL;
125 db_appexit(&db2_env);
129 * starttrans - Start a transaction.
131 * Start a transaction. Intended to be used if we're about to perform many
132 * operations on the database to help speed it all up, or if we want
133 * something to only succeed if all relevant operations are successful.
135 bool starttrans(void)
141 * endtrans - End a transaction.
143 * Ends a transaction.
151 * fetch_key - Given a keyid fetch the key from storage.
152 * @keyid: The keyid to fetch.
153 * @publickey: A pointer to a structure to return the key in.
154 * @intrans: If we're already in a transaction.
156 * We use the hex representation of the keyid as the filename to fetch the
157 * key from. The key is stored in the file as a binary OpenPGP stream of
158 * packets, so we can just use read_openpgp_stream() to read the packets
159 * in and then parse_keys() to parse the packets into a publickey
162 int fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
165 struct openpgp_packet_list *packets = NULL;
168 char id[KEYDB_KEYID_BYTES];
169 struct buffer_ctx fetchbuf;
171 memset(&key, 0, sizeof(key));
172 memset(&data, 0, sizeof(data));
174 id[0] = (keyid >> 24) & 0xFF;
175 id[1] = (keyid >> 16) & 0xFF;
176 id[2] = (keyid >> 8) & 0xFF;
177 id[3] = keyid & 0xFF;
180 key.size = KEYDB_KEYID_BYTES;
182 ret = (*(keydb(&key)->get))(keydb(&key), NULL, &key, &data, 0);
184 fetchbuf.buffer = data.data;
186 fetchbuf.size = data.size;
187 read_openpgp_stream(buffer_fetchchar, &fetchbuf, &packets);
188 parse_keys(packets, publickey);
189 free_packet_list(packets);
197 * fetch_key_text - Trys to find the keys that contain the supplied text.
198 * @search: The text to search for.
199 * @publickey: A pointer to a structure to return the key in.
201 * This function searches for the supplied text and returns the keys that
204 int fetch_key_text(const char *search, struct openpgp_publickey **publickey)
210 * store_key - Takes a key and stores it.
211 * @publickey: A pointer to the public key to store.
212 * @intrans: If we're already in a transaction.
213 * @update: If true the key exists and should be updated.
215 * Again we just use the hex representation of the keyid as the filename
216 * to store the key to. We flatten the public key to a list of OpenPGP
217 * packets and then use write_openpgp_stream() to write the stream out to
220 int store_key(struct openpgp_publickey *publickey, bool intrans, bool update)
226 * delete_key - Given a keyid delete the key from storage.
227 * @keyid: The keyid to delete.
228 * @intrans: If we're already in a transaction.
230 * This function deletes a public key from whatever storage mechanism we
231 * are using. Returns 0 if the key existed.
233 int delete_key(uint64_t keyid, bool intrans)
239 * Include the basic keydb routines.
241 #define NEED_KEYID2UID 1
242 #define NEED_GETKEYSIGS 1
243 #define NEED_GETFULLKEYID 1