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.
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);
90 parse_keys(packets, publickey);
98 * store_key - Takes a key and stores it.
99 * @publickey: A pointer to the public key to store.
100 * @intrans: If we're already in a transaction.
101 * @update: If true the key exists and should be updated.
103 * Again we just use the hex representation of the keyid as the filename
104 * to store the key to. We flatten the public key to a list of OpenPGP
105 * packets and then use write_openpgp_stream() to write the stream out to
108 int store_key(struct openpgp_publickey *publickey, bool intrans, bool update)
110 struct openpgp_packet_list *packets = NULL;
111 struct openpgp_packet_list *list_end = NULL;
112 struct openpgp_publickey *next = NULL;
116 snprintf(keyfile, 1023, "%s/0x%llX", config.db_dir,
117 get_keyid(publickey) & 0xFFFFFFFF);
118 fd = open(keyfile, O_WRONLY | O_CREAT, 0664); // | O_EXLOCK);
121 next = publickey -> next;
122 publickey -> next = NULL;
123 flatten_publickey(publickey, &packets, &list_end);
124 publickey -> next = next;
126 write_openpgp_stream(file_putchar, &fd, packets);
134 * delete_key - Given a keyid delete the key from storage.
135 * @keyid: The keyid to delete.
136 * @intrans: If we're already in a transaction.
138 * This function deletes a public key from whatever storage mechanism we
139 * are using. Returns 0 if the key existed.
141 int delete_key(uint64_t keyid, bool intrans)
145 snprintf(keyfile, 1023, "%s/0x%llX", config.db_dir,
148 return unlink(keyfile);
152 * fetch_key_text - Trys to find the keys that contain the supplied text.
153 * @search: The text to search for.
154 * @publickey: A pointer to a structure to return the key in.
156 * This function searches for the supplied text and returns the keys that
159 * TODO: Write for flat file access. Some sort of grep?
161 int fetch_key_text(const char *search, struct openpgp_publickey **publickey)
167 * Include the basic keydb routines.
169 #define NEED_KEYID2UID 1
170 #define NEED_GETKEYSIGS 1
171 #define NEED_GETFULLKEYID 1