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