]> the.earth.li Git - onak.git/blob - keydb.h
Add ability to drop overly large packets
[onak.git] / keydb.h
1 /**
2  * @file keydb.h
3  * @brief Routines to store and fetch keys.
4  *
5  * Copyright 2002-2004 Jonathan McDowell <noodles@earth.li>
6  *
7  * This program is free software: you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the Free
9  * Software Foundation; version 2 of the License.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * this program.  If not, see <https://www.gnu.org/licenses/>.
18  */
19
20 #ifndef __KEYDB_H__
21 #define __KEYDB_H__
22
23 #include <stdbool.h>
24 #include <inttypes.h>
25
26 #include "keystructs.h"
27 #include "ll.h"
28
29 /**
30  * @brief Context for a database backend
31  */
32 struct onak_dbctx {
33 /**
34  * @brief De-initialize the key database.
35  *
36  * This function should be called upon program exit to allow the DB to
37  * cleanup after itself.
38  */
39         void (*cleanupdb)(struct onak_dbctx *);
40
41 /**
42  * @brief Start a transaction.
43  *
44  * Start a transaction. Intended to be used if we're about to perform many
45  * operations on the database to help speed it all up, or if we want
46  * something to only succeed if all relevant operations are successful.
47  */
48         bool (*starttrans)(struct onak_dbctx *);
49
50 /**
51  * @brief End a transaction.
52  *
53  * Ends a transaction.
54  */
55         void (*endtrans)(struct onak_dbctx *);
56
57 /**
58  * @brief Given a keyid fetch the key from storage.
59  * @param keyid The keyid to fetch.
60  * @param publickey A pointer to a structure to return the key in.
61  * @param intrans  If we're already in a transaction.
62  *
63  * This function returns a public key from whatever storage mechanism we
64  * are using.
65  *
66  * TODO: What about keyid collisions? Should we use fingerprint instead?
67  */
68         int (*fetch_key_id)(struct onak_dbctx *,
69                         uint64_t keyid,
70                         struct openpgp_publickey **publickey,
71                         bool intrans);
72
73 /**
74  * @brief Given a fingerprint fetch the key from storage.
75  * @param fp The fingerprint to fetch.
76  * @param fpsize Number of bytes in the fingerprint (16 for v3, 20 for v4)
77  * @param publickey A pointer to a structure to return the key in.
78  * @param intrans  If we're already in a transaction.
79  *
80  * This function returns a public key from whatever storage mechanism we
81  * are using.
82  */
83         int (*fetch_key_fp)(struct onak_dbctx *,
84                         struct openpgp_fingerprint *fingerprint,
85                         struct openpgp_publickey **publickey,
86                         bool intrans);
87
88 /**
89  * @brief Takes a key and stores it.
90  * @param publickey A pointer to the public key to store.
91  * @param intrans If we're already in a transaction.
92  * @param update If true the key exists and should be updated.
93  *
94  * This function stores a public key in whatever storage mechanism we are
95  * using. intrans indicates if we're already in a transaction so don't
96  * need to start one. update indicates if the key already exists and is
97  * just being updated.
98  *
99  * TODO: Do we store multiple keys of the same id? Or only one and replace it?
100  */
101         int (*store_key)(struct onak_dbctx *,
102                         struct openpgp_publickey *publickey, bool intrans,
103                         bool update);
104
105 /**
106  * @brief Given a keyid delete the key from storage.
107  * @param keyid The keyid to delete.
108  * @param intrans If we're already in a transaction.
109  *
110  * This function deletes a public key from whatever storage mechanism we
111  * are using. Returns 0 if the key existed.
112  */
113         int (*delete_key)(struct onak_dbctx *, uint64_t keyid, bool intrans);
114
115 /**
116  * @brief Trys to find the keys that contain the supplied text.
117  * @param search The text to search for.
118  * @param publickey A pointer to a structure to return the key in.
119  *
120  * This function searches for the supplied text and returns the keys that
121  * contain it.
122  */
123         int (*fetch_key_text)(struct onak_dbctx *, const char *search,
124                         struct openpgp_publickey **publickey);
125
126 /**
127  * @brief Tries to find the keys from an SKS hash
128  * @param hash The hash to search for.
129  * @param publickey A pointer to a structure to return the key in.
130  *
131  * This function looks for the key that is referenced by the supplied
132  * SKS hash and returns it.
133  */
134         int (*fetch_key_skshash)(struct onak_dbctx *,
135                         const struct skshash *hash,
136                         struct openpgp_publickey **publickey);
137
138 /**
139  * @brief Takes a list of public keys and updates them in the DB.
140  * @param keys The keys to update in the DB.
141  * @param sendsync If we should send a keysync mail.
142  *
143  * Takes a list of keys and adds them to the database, merging them with
144  * the key in the database if it's already present there. The key list is
145  * update to contain the minimum set of updates required to get from what
146  * we had before to what we have now (ie the set of data that was added to
147  * the DB). Returns the number of entirely new keys added.
148  *
149  * If sendsync is true then we send out a keysync mail to our sync peers
150  * with the update.
151  */
152         int (*update_keys)(struct onak_dbctx *,
153                         struct openpgp_publickey **keys, bool sendsync);
154
155 /**
156  * @brief Takes a keyid and returns the primary UID for it.
157  * @param keyid The keyid to lookup.
158  *
159  * This function returns a UID for the given key. Returns NULL if the key
160  * isn't found.
161  */
162         char * (*keyid2uid)(struct onak_dbctx *, uint64_t keyid);
163
164 /**
165  * @brief Gets a linked list of the signatures on a key.
166  * @param keyid The keyid to get the sigs for.
167  * @param revoked Is the key revoked?
168  *
169  * This function gets the list of signatures on a key. Used for key 
170  * indexing and doing stats bits. If revoked is non-NULL then if the key
171  * is revoked it's set to true.
172  */
173         struct ll * (*getkeysigs)(struct onak_dbctx *,
174                         uint64_t keyid, bool *revoked);
175
176 /**
177  * @brief Gets the signatures on a key.
178  * @param keyid The key we want the signatures for.
179  *
180  * This function gets the signatures on a key. It's the same as the
181  * getkeysigs function above except we use the hash module to cache the
182  */
183         struct ll * (*cached_getkeysigs)(struct onak_dbctx *,
184                         uint64_t keyid);
185
186 /**
187  * @brief Maps a 32 bit key id to a 64 bit one.
188  * @param keyid The 32 bit keyid.
189  *
190  * This function maps a 32 bit key id to the full 64 bit one. It returns the
191  * full keyid. If the key isn't found a keyid of 0 is returned.
192  */
193         uint64_t (*getfullkeyid)(struct onak_dbctx *, uint64_t keyid);
194
195 /**
196  * @brief call a function once for each key in the db.
197  * @param iterfunc The function to call.
198  * @param ctx A context pointer
199  *
200  * Calls iterfunc once for each key in the database. ctx is passed
201  * unaltered to iterfunc. This function is intended to aid database dumps
202  * and statistic calculations.
203  *
204  * Returns the number of keys we iterated over.
205  */
206         int (*iterate_keys)(struct onak_dbctx *,
207                         void (*iterfunc)(void *ctx,
208                         struct openpgp_publickey *key), void *ctx);
209
210 /**
211  * @brief Configuration file information for this backend instance
212  */
213         struct onak_db_config *config;
214
215 /**
216  * @brief Private backend context information.
217  */
218         void *priv;
219 };
220
221 #endif /* __KEYDB_H__ */