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
22 #include "keystructs.h"
24 #include "onak_conf.h"
27 #define KEYDB_KEYID_BYTES 4
30 * db2_numdb - The number of database files we have.
32 static int db2_numdb = 16;
35 * db2_keydbfiles - An array of DB structs for our key database files.
37 static DB **db2_keydbfiles = NULL;
40 * db2_env - Database environment variable.
42 static DB_ENV db2_env;
45 * Shared with CGI buffer stuff...
54 * keydb_fetchchar - Fetches a char from a buffer.
56 int keydb_fetchchar(void *ctx, int count, unsigned char *c)
58 struct db2_get_ctx *buf = NULL;
61 buf = (struct db2_get_ctx *) ctx;
62 for (i = 0; i < count; i++) {
63 c[i] = buf->buffer[buf->offset++];
66 return (((buf->offset) == (buf->size)) ? 1 : 0);
70 * keydb_putchar - Puts a char to a file.
72 static int keydb_putchar(void *fd, unsigned char c)
74 // return !(lo_write(dbconn, *(int *) fd, &c, sizeof(c)));
81 * keyid's are 8 bytes, msb first. so start from the end. use 16
82 * bits, since that's enough to divide by any small number of db files.
84 unsigned char *keydata = (unsigned char *) key->data;
85 unsigned long keyidnum;
87 keyidnum = (keydata[KEYDB_KEYID_BYTES-2]<<8)|keydata[KEYDB_KEYID_BYTES-1];
88 return(db2_keydbfiles[keyidnum % db2_numdb]);
92 * initdb - Initialize the key database.
94 * This function should be called before any of the other functions in
95 * this file are called in order to allow the DB to be initialized ready
105 memset(&db2_env, 0, sizeof(db2_env));
108 * Tunable param. Just using what pksd does for the moment. Bigger uses
109 * more memory but improves performance. Bigger than physical memory
112 db2_env.mp_size = 20 * 1024 * 1024;
114 ret = db_appinit(config.db2_dbpath, NULL,
115 &db2_env, DB_INIT_MPOOL|DB_INIT_LOCK);
117 db2_keydbfiles = (DB **) malloc(sizeof (DB *) * db2_numdb);
118 memset(&keydbinfo, 0, sizeof(keydbinfo));
119 keydbinfo.db_pagesize = 8192;
120 for (i = 0; i < db2_numdb; i++) {
121 db2_keydbfiles[i] = NULL;
122 snprintf(keydbname, 19, "keydb%03d", i);
123 ret = db_open(keydbname, DB_HASH, DB_RDONLY, 0644,
124 &db2_env, &keydbinfo,
127 fprintf(stderr, "Error opening db file %d (errno %d)\n",
133 fprintf(stderr, "Error initializing db (%d).\n", ret);
139 * cleanupdb - De-initialize the key database.
141 * This function should be called upon program exit to allow the DB to
142 * cleanup after itself.
148 for (i = 0; i < db2_numdb; i++) {
149 if (db2_keydbfiles[i] != NULL) {
150 (*(db2_keydbfiles[i]->close))(db2_keydbfiles[i], 0);
151 db2_keydbfiles[i] = NULL;
155 db_appexit(&db2_env);
159 * fetch_key - Given a keyid fetch the key from storage.
160 * @keyid: The keyid to fetch.
161 * @publickey: A pointer to a structure to return the key in.
163 * We use the hex representation of the keyid as the filename to fetch the
164 * key from. The key is stored in the file as a binary OpenPGP stream of
165 * packets, so we can just use read_openpgp_stream() to read the packets
166 * in and then parse_keys() to parse the packets into a publickey
169 int fetch_key(uint64_t keyid, struct openpgp_publickey **publickey)
171 struct openpgp_packet_list *packets = NULL;
174 char id[KEYDB_KEYID_BYTES];
175 struct db2_get_ctx fetchbuf;
177 memset(&key, 0, sizeof(key));
178 memset(&data, 0, sizeof(data));
180 id[0] = (keyid >> 24) & 0xFF;
181 id[1] = (keyid >> 16) & 0xFF;
182 id[2] = (keyid >> 8) & 0xFF;
183 id[3] = keyid & 0xFF;
186 key.size = KEYDB_KEYID_BYTES;
188 ret = (*(keydb(&key)->get))(keydb(&key), NULL, &key, &data, 0);
190 //do stuff with data.
191 fetchbuf.buffer = data.data;
193 fetchbuf.size = data.size;
194 read_openpgp_stream(keydb_fetchchar, &fetchbuf, &packets);
195 parse_keys(packets, publickey);
202 * store_key - Takes a key and stores it.
203 * @publickey: A pointer to the public key to store.
205 * Again we just use the hex representation of the keyid as the filename
206 * to store the key to. We flatten the public key to a list of OpenPGP
207 * packets and then use write_openpgp_stream() to write the stream out to
210 int store_key(struct openpgp_publickey *publickey)
216 * delete_key - Given a keyid delete the key from storage.
217 * @keyid: The keyid to delete.
219 * This function deletes a public key from whatever storage mechanism we
220 * are using. Returns 0 if the key existed.
222 int delete_key(uint64_t keyid)
228 * Include the basic keydb routines.
230 #define NEED_KEYID2UID 1
231 #define NEED_GETKEYSIGS 1