2 * keydb.c - Routines to store and fetch keys.
4 * Jonathan McDowell <noodles@earth.li>
6 * Copyright 2002-2004 Project Purple
19 #include "charfuncs.h"
22 #include "keystructs.h"
26 #include "onak-conf.h"
30 * initdb - Initialize the key database.
32 * This is just a no-op for flat file access.
34 void initdb(bool readonly)
39 * cleanupdb - De-initialize the key database.
41 * This is just a no-op for flat file access.
48 * starttrans - Start a transaction.
50 * This is just a no-op for flat file access.
58 * endtrans - End a transaction.
60 * This is just a no-op for flat file access.
68 * fetch_key - Given a keyid fetch the key from storage.
69 * @keyid: The keyid to fetch.
70 * @publickey: A pointer to a structure to return the key in.
71 * @intrans: If we're already in a transaction.
73 * We use the hex representation of the keyid as the filename to fetch the
74 * key from. The key is stored in the file as a binary OpenPGP stream of
75 * packets, so we can just use read_openpgp_stream() to read the packets
76 * in and then parse_keys() to parse the packets into a publickey
79 int fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
82 struct openpgp_packet_list *packets = NULL;
86 snprintf(keyfile, 1023, "%s/0x%llX", config.db_dir,
88 fd = open(keyfile, O_RDONLY); // | O_SHLOCK);
91 read_openpgp_stream(file_fetchchar, &fd, &packets, 0);
92 parse_keys(packets, publickey);
93 free_packet_list(packets);
102 * store_key - Takes a key and stores it.
103 * @publickey: A pointer to the public key to store.
104 * @intrans: If we're already in a transaction.
105 * @update: If true the key exists and should be updated.
107 * Again we just use the hex representation of the keyid as the filename
108 * to store the key to. We flatten the public key to a list of OpenPGP
109 * packets and then use write_openpgp_stream() to write the stream out to
112 int store_key(struct openpgp_publickey *publickey, bool intrans, bool update)
114 struct openpgp_packet_list *packets = NULL;
115 struct openpgp_packet_list *list_end = NULL;
116 struct openpgp_publickey *next = NULL;
120 snprintf(keyfile, 1023, "%s/0x%llX", config.db_dir,
121 get_keyid(publickey) & 0xFFFFFFFF);
122 fd = open(keyfile, O_WRONLY | O_CREAT, 0664); // | O_EXLOCK);
125 next = publickey -> next;
126 publickey -> next = NULL;
127 flatten_publickey(publickey, &packets, &list_end);
128 publickey -> next = next;
130 write_openpgp_stream(file_putchar, &fd, packets);
132 free_packet_list(packets);
140 * delete_key - Given a keyid delete the key from storage.
141 * @keyid: The keyid to delete.
142 * @intrans: If we're already in a transaction.
144 * This function deletes a public key from whatever storage mechanism we
145 * are using. Returns 0 if the key existed.
147 int delete_key(uint64_t keyid, bool intrans)
151 snprintf(keyfile, 1023, "%s/0x%llX", config.db_dir,
154 return unlink(keyfile);
158 * fetch_key_text - Trys to find the keys that contain the supplied text.
159 * @search: The text to search for.
160 * @publickey: A pointer to a structure to return the key in.
162 * This function searches for the supplied text and returns the keys that
165 * TODO: Write for flat file access. Some sort of grep?
167 int fetch_key_text(const char *search, struct openpgp_publickey **publickey)
173 * iterate_keys - call a function once for each key in the db.
174 * @iterfunc: The function to call.
175 * @ctx: A context pointer
177 * Calls iterfunc once for each key in the database. ctx is passed
178 * unaltered to iterfunc. This function is intended to aid database dumps
179 * and statistic calculations.
181 * Returns the number of keys we iterated over.
183 int iterate_keys(void (*iterfunc)(void *ctx, struct openpgp_publickey *key),
187 struct openpgp_packet_list *packets = NULL;
188 struct openpgp_publickey *key = NULL;
192 struct dirent *curfile = NULL;
194 dir = opendir(config.db_dir);
197 while ((curfile = readdir(dir)) != NULL) {
198 if (curfile->d_name[0] == '0' &&
199 curfile->d_name[1] == 'x') {
200 snprintf(keyfile, 1023, "%s/%s",
203 fd = open(keyfile, O_RDONLY);
206 read_openpgp_stream(file_fetchchar,
210 parse_keys(packets, &key);
216 free_packet_list(packets);
233 * dumpdb - dump the key database
234 * @filenamebase: The base filename to use for the dump.
236 * Dumps the database into one or more files, which contain pure OpenPGP
237 * that can be reimported into onak or gpg. filenamebase provides a base
238 * file name for the dump; several files may be created, all of which will
239 * begin with this string and then have a unique number and a .pgp
242 int dumpdb(char *filenamebase)
248 * Include the basic keydb routines.
250 #define NEED_KEYID2UID 1
251 #define NEED_GETKEYSIGS 1
252 #define NEED_GETFULLKEYID 1
253 #define NEED_UPDATEKEYS 1