2 * keydb.c - Routines to store and fetch keys.
4 * Jonathan McDowell <noodles@earth.li>
6 * Copyright 2002 Project Purple
18 #include "charfuncs.h"
21 #include "keystructs.h"
24 #include "onak-conf.h"
28 * initdb - Initialize the key database.
30 * This is just a no-op for flat file access.
32 void initdb(bool readonly)
37 * cleanupdb - De-initialize the key database.
39 * This is just a no-op for flat file access.
46 * starttrans - Start a transaction.
48 * This is just a no-op for flat file access.
56 * endtrans - End a transaction.
58 * This is just a no-op for flat file access.
66 * fetch_key - Given a keyid fetch the key from storage.
67 * @keyid: The keyid to fetch.
68 * @publickey: A pointer to a structure to return the key in.
69 * @intrans: If we're already in a transaction.
71 * We use the hex representation of the keyid as the filename to fetch the
72 * key from. The key is stored in the file as a binary OpenPGP stream of
73 * packets, so we can just use read_openpgp_stream() to read the packets
74 * in and then parse_keys() to parse the packets into a publickey
77 int fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
80 struct openpgp_packet_list *packets = NULL;
84 snprintf(keyfile, 1023, "%s/0x%llX", config.db_dir,
86 fd = open(keyfile, O_RDONLY); // | O_SHLOCK);
89 read_openpgp_stream(file_fetchchar, &fd, &packets, 0);
90 parse_keys(packets, publickey);
91 free_packet_list(packets);
100 * store_key - Takes a key and stores it.
101 * @publickey: A pointer to the public key to store.
102 * @intrans: If we're already in a transaction.
103 * @update: If true the key exists and should be updated.
105 * Again we just use the hex representation of the keyid as the filename
106 * to store the key to. We flatten the public key to a list of OpenPGP
107 * packets and then use write_openpgp_stream() to write the stream out to
110 int store_key(struct openpgp_publickey *publickey, bool intrans, bool update)
112 struct openpgp_packet_list *packets = NULL;
113 struct openpgp_packet_list *list_end = NULL;
114 struct openpgp_publickey *next = NULL;
118 snprintf(keyfile, 1023, "%s/0x%llX", config.db_dir,
119 get_keyid(publickey) & 0xFFFFFFFF);
120 fd = open(keyfile, O_WRONLY | O_CREAT, 0664); // | O_EXLOCK);
123 next = publickey -> next;
124 publickey -> next = NULL;
125 flatten_publickey(publickey, &packets, &list_end);
126 publickey -> next = next;
128 write_openpgp_stream(file_putchar, &fd, packets);
130 free_packet_list(packets);
138 * delete_key - Given a keyid delete the key from storage.
139 * @keyid: The keyid to delete.
140 * @intrans: If we're already in a transaction.
142 * This function deletes a public key from whatever storage mechanism we
143 * are using. Returns 0 if the key existed.
145 int delete_key(uint64_t keyid, bool intrans)
149 snprintf(keyfile, 1023, "%s/0x%llX", config.db_dir,
152 return unlink(keyfile);
156 * fetch_key_text - Trys to find the keys that contain the supplied text.
157 * @search: The text to search for.
158 * @publickey: A pointer to a structure to return the key in.
160 * This function searches for the supplied text and returns the keys that
163 * TODO: Write for flat file access. Some sort of grep?
165 int fetch_key_text(const char *search, struct openpgp_publickey **publickey)
171 * dumpdb - dump the key database
172 * @filenamebase: The base filename to use for the dump.
174 * Dumps the database into one or more files, which contain pure OpenPGP
175 * that can be reimported into onak or gpg. filenamebase provides a base
176 * file name for the dump; several files may be created, all of which will
177 * begin with this string and then have a unique number and a .pgp
180 int dumpdb(char *filenamebase)
186 * Include the basic keydb routines.
188 #define NEED_KEYID2UID 1
189 #define NEED_GETKEYSIGS 1
190 #define NEED_GETFULLKEYID 1
191 #define NEED_UPDATEKEYS 1