1 /*
2  * Copyright (c) 2014 Jerry Lundström <lundstrom.jerry@gmail.com>
3  * Copyright (c) 2014 .SE (The Internet Infrastructure Foundation).
4  * Copyright (c) 2014 OpenDNSSEC AB (svb)
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
22  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  */
29 
30 #ifndef __key_state_h
31 #define __key_state_h
32 
33 #include "db_object.h"
34 
35 struct key_state;
36 struct key_state_list;
37 typedef struct key_state key_state_t;
38 typedef struct key_state_list key_state_list_t;
39 
40 typedef enum key_state_type {
41     KEY_STATE_TYPE_INVALID = -1,
42     KEY_STATE_TYPE_DS = 0,
43     KEY_STATE_TYPE_RRSIG = 1,
44     KEY_STATE_TYPE_DNSKEY = 2,
45     KEY_STATE_TYPE_RRSIGDNSKEY = 3
46 } key_state_type_t;
47 extern const db_enum_t key_state_enum_set_type[];
48 
49 typedef enum key_state_state {
50     KEY_STATE_STATE_INVALID = -1,
51     KEY_STATE_STATE_HIDDEN = 0,
52     KEY_STATE_STATE_RUMOURED = 1,
53     KEY_STATE_STATE_OMNIPRESENT = 2,
54     KEY_STATE_STATE_UNRETENTIVE = 3,
55     KEY_STATE_STATE_NA = 4
56 } key_state_state_t;
57 extern const db_enum_t key_state_enum_set_state[];
58 
59 #include "key_state_ext.h"
60 #include "key_data.h"
61 
62 /**
63  * A key state object.
64  */
65 struct key_state {
66     db_object_t* dbo;
67     db_value_t id;
68     db_value_t rev;
69     db_value_t key_data_id;
70     const key_data_t* associated_key_data_id;
71     key_data_t* private_key_data_id;
72     key_state_type_t type;
73     key_state_state_t state;
74     unsigned int last_change;
75     unsigned int minimize;
76     unsigned int ttl;
77 };
78 
79 /**
80  * Create a new key state object.
81  * \param[in] connection a db_connection_t pointer.
82  * \return a key_state_t pointer or NULL on error.
83  */
84 extern key_state_t* key_state_new(const db_connection_t* connection);
85 
86 /**
87  * Create a new key state object that is a copy of another key state object.
88  * \param[in] key_state a key_state_t pointer.
89  * \return a key_state_t pointer or NULL on error.
90  */
91 extern key_state_t* key_state_new_copy(const key_state_t* key_state);
92 
93 /**
94  * Delete a key state object, this does not delete it from the database.
95  * \param[in] key_state a key_state_t pointer.
96  */
97 extern void key_state_free(key_state_t* key_state);
98 
99 /**
100  * Copy the content of a key state object.
101  * \param[in] key_state a key_state_t pointer.
102  * \param[in] key_state_copy a key_state_t pointer.
103  * \return DB_ERROR_* on failure, otherwise DB_OK.
104  */
105 extern int key_state_copy(key_state_t* key_state, const key_state_t* key_state_copy);
106 
107 /**
108  * Set the content of a key state object based on a database result.
109  * \param[in] key_state a key_state_t pointer.
110  * \param[in] result a db_result_t pointer.
111  * \return DB_ERROR_* on failure, otherwise DB_OK.
112  */
113 extern int key_state_from_result(key_state_t* key_state, const db_result_t* result);
114 
115 /**
116  * Get the key_data_id of a key state object.
117  * \param[in] key_state a key_state_t pointer.
118  * \return a db_value_t pointer or NULL on error.
119  */
120 extern const db_value_t* key_state_key_data_id(const key_state_t* key_state);
121 
122 /**
123  * Get the type of a key state object.
124  * \param[in] key_state a key_state_t pointer.
125  * \return a key_state_type_t which may be KEY_STATE_TYPE_INVALID on error or if no type has been set.
126  */
127 extern key_state_type_t key_state_type(const key_state_t* key_state);
128 
129 /**
130  * Get the type as text of a key state object.
131  * \param[in] key_state a key_state_t pointer.
132  * \return a character pointer or NULL on error or if no type has been set.
133  */
134 extern const char* key_state_type_text(const key_state_t* key_state);
135 
136 /**
137  * Get the state of a key state object.
138  * \param[in] key_state a key_state_t pointer.
139  * \return a key_state_state_t which may be KEY_STATE_STATE_INVALID on error or if no state has been set.
140  */
141 extern key_state_state_t key_state_state(const key_state_t* key_state);
142 
143 /**
144  * Get the state as text of a key state object.
145  * \param[in] key_state a key_state_t pointer.
146  * \return a character pointer or NULL on error or if no state has been set.
147  */
148 extern const char* key_state_state_text(const key_state_t* key_state);
149 
150 /**
151  * Get the last_change of a key state object. Undefined behavior if `key_state` is NULL.
152  * \param[in] key_state a key_state_t pointer.
153  * \return an unsigned integer.
154  */
155 extern unsigned int key_state_last_change(const key_state_t* key_state);
156 
157 /**
158  * Get the minimize of a key state object. Undefined behavior if `key_state` is NULL.
159  * \param[in] key_state a key_state_t pointer.
160  * \return an unsigned integer.
161  */
162 extern unsigned int key_state_minimize(const key_state_t* key_state);
163 
164 /**
165  * Get the ttl of a key state object. Undefined behavior if `key_state` is NULL.
166  * \param[in] key_state a key_state_t pointer.
167  * \return an unsigned integer.
168  */
169 extern unsigned int key_state_ttl(const key_state_t* key_state);
170 
171 /**
172  * Set the key_data_id of a key state object. If this fails the original value may have been lost.
173  * \param[in] key_state a key_state_t pointer.
174  * \param[in] key_data_id a db_value_t pointer.
175  * \return DB_ERROR_* on failure, otherwise DB_OK.
176  */
177 extern int key_state_set_key_data_id(key_state_t* key_state, const db_value_t* key_data_id);
178 
179 /**
180  * Set the type of a key state object.
181  * \param[in] key_state a key_state_t pointer.
182  * \param[in] type a key_state_type_t.
183  * \return DB_ERROR_* on failure, otherwise DB_OK.
184  */
185 extern int key_state_set_type(key_state_t* key_state, key_state_type_t type);
186 
187 /**
188  * Set the state of a key state object.
189  * \param[in] key_state a key_state_t pointer.
190  * \param[in] state a key_state_state_t.
191  * \return DB_ERROR_* on failure, otherwise DB_OK.
192  */
193 extern int key_state_set_state(key_state_t* key_state, key_state_state_t state);
194 
195 /**
196  * Set the last_change of a key state object.
197  * \param[in] key_state a key_state_t pointer.
198  * \param[in] last_change an unsigned integer.
199  * \return DB_ERROR_* on failure, otherwise DB_OK.
200  */
201 extern int key_state_set_last_change(key_state_t* key_state, unsigned int last_change);
202 
203 /**
204  * Set the minimize of a key state object.
205  * \param[in] key_state a key_state_t pointer.
206  * \param[in] minimize an unsigned integer.
207  * \return DB_ERROR_* on failure, otherwise DB_OK.
208  */
209 extern int key_state_set_minimize(key_state_t* key_state, unsigned int minimize);
210 
211 /**
212  * Set the ttl of a key state object.
213  * \param[in] key_state a key_state_t pointer.
214  * \param[in] ttl an unsigned integer.
215  * \return DB_ERROR_* on failure, otherwise DB_OK.
216  */
217 extern int key_state_set_ttl(key_state_t* key_state, unsigned int ttl);
218 
219 /**
220  * Create a clause for key_data_id of a key state object and add it to a database clause list.
221  * The clause operator is set to DB_CLAUSE_OPERATOR_AND and the clause type is
222  * set to DB_CLAUSE_EQUAL, if you want to change these you can do it with the
223  * returned db_clause_t pointer.
224  * \param[in] clause_list db_clause_list_t pointer.
225  * \param[in] key_data_id a db_value_t pointer.
226  * \return a db_clause_t pointer to the added clause or NULL on error.
227  */
228 extern db_clause_t* key_state_key_data_id_clause(db_clause_list_t* clause_list, const db_value_t* key_data_id);
229 
230 /**
231  * Create a key state object in the database.
232  * \param[in] key_state a key_state_t pointer.
233  * \return DB_ERROR_* on failure, otherwise DB_OK.
234  */
235 extern int key_state_create(key_state_t* key_state);
236 
237 /**
238  * Get a key state object from the database by a id specified in `id`.
239  * \param[in] key_state a key_state_t pointer.
240  * \param[in] id a db_value_t pointer.
241  * \return DB_ERROR_* on failure, otherwise DB_OK.
242  */
243 extern int key_state_get_by_id(key_state_t* key_state, const db_value_t* id);
244 
245 /**
246  * Update a key state object in the database.
247  * \param[in] key_state a key_state_t pointer.
248  * \return DB_ERROR_* on failure, otherwise DB_OK.
249  */
250 extern int key_state_update(key_state_t* key_state);
251 
252 /**
253  * Delete a key state object from the database.
254  * \param[in] key_state a key_state_t pointer.
255  * \return DB_ERROR_* on failure, otherwise DB_OK.
256  */
257 extern int key_state_delete(const key_state_t* key_state);
258 
259 /**
260  * A list of key state objects.
261  */
262 struct key_state_list {
263     db_object_t* dbo;
264     db_result_list_t* result_list;
265     const db_result_t* result;
266     key_state_t* key_state;
267     int object_store;
268     key_state_t** object_list;
269     size_t object_list_size;
270     size_t object_list_position;
271     int object_list_first;
272     int associated_fetch;
273     key_data_list_t* key_data_id_list;
274 };
275 
276 /**
277  * Create a new key state object list.
278  * \param[in] connection a db_connection_t pointer.
279  * \return a key_state_list_t pointer or NULL on error.
280  */
281 extern key_state_list_t* key_state_list_new(const db_connection_t* connection);
282 
283 /**
284  * Create a new key state object list that is a copy of another.
285  * \param[in] key_state_list a key_state_list_t pointer.
286  * \return a key_state_list_t pointer or NULL on error.
287  */
288 extern key_state_list_t* key_state_list_new_copy(const key_state_list_t* key_state_copy);
289 
290 /**
291  * Specify that objects should be stored within the list as they are fetch,
292  * this is optimal if the list is to be iterated over more then once.
293  * \param[in] key_state_list a key_state_list_t pointer.
294  * \return DB_ERROR_* on failure, otherwise DB_OK.
295  */
296 extern int key_state_list_object_store(key_state_list_t* key_state_list);
297 
298 /**
299  * Delete a key state object list.
300  * \param[in] key_state_list a key_state_list_t pointer.
301  */
302 extern void key_state_list_free(key_state_list_t* key_state_list);
303 
304 /**
305  * Copy the content of another key state object list.
306  * \param[in] key_state_list a key_state_list_t pointer.
307  * \param[in] from_key_state_list a key_state_list_t pointer.
308  * \return DB_ERROR_* on failure, otherwise DB_OK.
309  */
310 extern int key_state_list_copy(key_state_list_t* key_state_list, const key_state_list_t* from_key_state_list);
311 
312 
313 /**
314  * Get key state objects from the database by a clause list.
315  * \param[in] key_state_list a key_state_list_t pointer.
316  * \param[in] clause_list a db_clause_list_t pointer.
317  * \return DB_ERROR_* on failure, otherwise DB_OK.
318  */
319 extern int key_state_list_get_by_clauses(key_state_list_t* key_state_list, const db_clause_list_t* clause_list);
320 
321 /**
322  * Get key state objects from the database by a key_data_id specified in `key_data_id`.
323  * \param[in] key_state_list a key_state_list_t pointer.
324  * \param[in] key_data_id a db_value_t pointer.
325  * \return DB_ERROR_* on failure, otherwise DB_OK.
326  */
327 extern int key_state_list_get_by_key_data_id(key_state_list_t* key_state_list, const db_value_t* key_data_id);
328 
329 /**
330  * Get a new list of key state objects from the database by a key_data_id specified in `key_data_id`.
331  * \param[in] connection a db_connection_t pointer.
332  * \param[in] key_data_id a db_value_t pointer.
333  * \return a key_state_list_t pointer or NULL on error.
334  */
335 extern key_state_list_t* key_state_list_new_get_by_key_data_id(const db_connection_t* connection, const db_value_t* key_data_id);
336 
337 /**
338  * Get the first key state object in a key state object list and reset the
339  * position of the list.
340  * \param[in] key_state_list a key_state_list_t pointer.
341  * \return a key_state_t pointer or NULL on error or if there are no
342  * key state objects in the key state object list.
343  */
344 extern const key_state_t* key_state_list_begin(key_state_list_t* key_state_list);
345 
346 /**
347  * Get the first key state object in a key state object list and reset the
348  * position of the list. The caller will be given ownership of this object and
349  * is responsible for freeing it.
350  * \param[in] key_state_list a key_state_list_t pointer.
351  * \return a key_state_t pointer or NULL on error or if there are no
352  * key state objects in the key state object list.
353  */
354 extern key_state_t* key_state_list_get_begin(key_state_list_t* key_state_list);
355 
356 /**
357  * Get the next key state object in a key state object list.
358  * Ownership of this object is retained within the list and the object is only
359  * valid until the next call to this function.
360  * \param[in] key_state_list a key_state_list_t pointer.
361  * \return a key_state_t pointer or NULL on error or if there are no more
362  * key state objects in the key state object list.
363  */
364 extern const key_state_t* key_state_list_next(key_state_list_t* key_state_list);
365 
366 /**
367  * Get the next key state object in a key state object list.
368  * The caller will be given ownership of this object and is responsible for
369  * freeing it.
370  * \param[in] key_state_list a key_state_list_t pointer.
371  * \return a key_state_t pointer or NULL on error or if there are no more
372  * key state objects in the key state object list.
373  */
374 extern key_state_t* key_state_list_get_next(key_state_list_t* key_state_list);
375 
376 #endif
377