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 __policy_key_h
31 #define __policy_key_h
32 
33 #include "db_object.h"
34 
35 struct policy_key;
36 struct policy_key_list;
37 typedef struct policy_key policy_key_t;
38 typedef struct policy_key_list policy_key_list_t;
39 
40 typedef enum policy_key_role {
41     POLICY_KEY_ROLE_INVALID = -1,
42     POLICY_KEY_ROLE_KSK = 1,
43     POLICY_KEY_ROLE_ZSK = 2,
44     POLICY_KEY_ROLE_CSK = 3
45 } policy_key_role_t;
46 extern const db_enum_t policy_key_enum_set_role[];
47 
48 #include "policy_key_ext.h"
49 #include "policy.h"
50 
51 /**
52  * A policy key object.
53  */
54 struct policy_key {
55     db_object_t* dbo;
56     db_value_t id;
57     db_value_t rev;
58     db_value_t policy_id;
59     const policy_t* associated_policy_id;
60     policy_t* private_policy_id;
61     policy_key_role_t role;
62     unsigned int algorithm;
63     unsigned int bits;
64     unsigned int lifetime;
65     char* repository;
66     unsigned int standby;
67     unsigned int manual_rollover;
68     unsigned int rfc5011;
69     unsigned int minimize;
70 };
71 
72 /**
73  * Create a new policy key object.
74  * \param[in] connection a db_connection_t pointer.
75  * \return a policy_key_t pointer or NULL on error.
76  */
77 extern policy_key_t* policy_key_new(const db_connection_t* connection);
78 
79 /**
80  * Create a new policy key object that is a copy of another policy key object.
81  * \param[in] policy_key a policy_key_t pointer.
82  * \return a policy_key_t pointer or NULL on error.
83  */
84 extern policy_key_t* policy_key_new_copy(const policy_key_t* policy_key);
85 
86 /**
87  * Delete a policy key object, this does not delete it from the database.
88  * \param[in] policy_key a policy_key_t pointer.
89  */
90 extern void policy_key_free(policy_key_t* policy_key);
91 
92 /**
93  * Reset the content of a policy key object making it as if its new. This does not change anything in the database.
94  * \param[in] policy_key a policy_key_t pointer.
95  */
96 extern void policy_key_reset(policy_key_t* policy_key);
97 
98 /**
99  * Copy the content of a policy key object.
100  * \param[in] policy_key a policy_key_t pointer.
101  * \param[in] policy_key_copy a policy_key_t pointer.
102  * \return DB_ERROR_* on failure, otherwise DB_OK.
103  */
104 extern int policy_key_copy(policy_key_t* policy_key, const policy_key_t* policy_key_copy);
105 
106 /**
107  * Compare two policy key objects and return less than, equal to,
108  * or greater than zero if A is found, respectively, to be less than, to match,
109  * or be greater than B.
110  * \param[in] policy_key_a a policy_key_t pointer.
111  * \param[in] policy_key_b a policy_key_t pointer.
112  * \return less than, equal to, or greater than zero if A is found, respectively,
113  * to be less than, to match, or be greater than B.
114  */
115 extern int policy_key_cmp(const policy_key_t* policy_key_a, const policy_key_t* policy_key_b);
116 
117 /**
118  * Set the content of a policy key object based on a database result.
119  * \param[in] policy_key a policy_key_t pointer.
120  * \param[in] result a db_result_t pointer.
121  * \return DB_ERROR_* on failure, otherwise DB_OK.
122  */
123 extern int policy_key_from_result(policy_key_t* policy_key, const db_result_t* result);
124 
125 /**
126  * Get the policy_id of a policy key object.
127  * \param[in] policy_key a policy_key_t pointer.
128  * \return a db_value_t pointer or NULL on error.
129  */
130 extern const db_value_t* policy_key_policy_id(const policy_key_t* policy_key);
131 
132 /**
133  * Get the policy_id object related to a policy key object.
134  * \param[in] policy_key a policy_key_t pointer.
135  * \return a policy_t pointer or NULL on error or if no object could be found.
136  */
137 extern const policy_t* policy_key_policy(const policy_key_t* policy_key);
138 
139 /**
140  * Get the policy_id object related to a policy key object.
141  * The caller will be given ownership of this object and is responsible for freeing it.
142  * \param[in] policy_key a policy_key_t pointer.
143  * \return a policy_t pointer or NULL on error or if no object could be found.
144  */
145 extern policy_t* policy_key_get_policy(const policy_key_t* policy_key);
146 
147 /**
148  * Get the role of a policy key object.
149  * \param[in] policy_key a policy_key_t pointer.
150  * \return a policy_key_role_t which may be POLICY_KEY_ROLE_INVALID on error or if no role has been set.
151  */
152 extern policy_key_role_t policy_key_role(const policy_key_t* policy_key);
153 
154 /**
155  * Get the role as text of a policy key object.
156  * \param[in] policy_key a policy_key_t pointer.
157  * \return a character pointer or NULL on error or if no role has been set.
158  */
159 extern const char* policy_key_role_text(const policy_key_t* policy_key);
160 
161 /**
162  * Get the algorithm of a policy key object. Undefined behavior if `policy_key` is NULL.
163  * \param[in] policy_key a policy_key_t pointer.
164  * \return an unsigned integer.
165  */
166 extern unsigned int policy_key_algorithm(const policy_key_t* policy_key);
167 
168 /**
169  * Get the bits of a policy key object. Undefined behavior if `policy_key` is NULL.
170  * \param[in] policy_key a policy_key_t pointer.
171  * \return an unsigned integer.
172  */
173 extern unsigned int policy_key_bits(const policy_key_t* policy_key);
174 
175 /**
176  * Get the lifetime of a policy key object. Undefined behavior if `policy_key` is NULL.
177  * \param[in] policy_key a policy_key_t pointer.
178  * \return an unsigned integer.
179  */
180 extern unsigned int policy_key_lifetime(const policy_key_t* policy_key);
181 
182 /**
183  * Get the repository of a policy key object.
184  * \param[in] policy_key a policy_key_t pointer.
185  * \return a character pointer or NULL on error or if no repository has been set.
186  */
187 extern const char* policy_key_repository(const policy_key_t* policy_key);
188 
189 /**
190  * Get the standby of a policy key object. Undefined behavior if `policy_key` is NULL.
191  * \param[in] policy_key a policy_key_t pointer.
192  * \return an integer.
193  */
194 extern int policy_key_standby(const policy_key_t* policy_key);
195 
196 /**
197  * Get the manual_rollover of a policy key object. Undefined behavior if `policy_key` is NULL.
198  * \param[in] policy_key a policy_key_t pointer.
199  * \return an unsigned integer.
200  */
201 extern unsigned int policy_key_manual_rollover(const policy_key_t* policy_key);
202 
203 /**
204  * Get the rfc5011 of a policy key object. Undefined behavior if `policy_key` is NULL.
205  * \param[in] policy_key a policy_key_t pointer.
206  * \return an unsigned integer.
207  */
208 extern unsigned int policy_key_rfc5011(const policy_key_t* policy_key);
209 
210 /**
211  * Get the minimize of a policy key object. Undefined behavior if `policy_key` is NULL.
212  * \param[in] policy_key a policy_key_t pointer.
213  * \return an unsigned integer.
214  */
215 extern unsigned int policy_key_minimize(const policy_key_t* policy_key);
216 
217 /**
218  * Set the policy_id of a policy key object. If this fails the original value may have been lost.
219  * \param[in] policy_key a policy_key_t pointer.
220  * \param[in] policy_id a db_value_t pointer.
221  * \return DB_ERROR_* on failure, otherwise DB_OK.
222  */
223 extern int policy_key_set_policy_id(policy_key_t* policy_key, const db_value_t* policy_id);
224 
225 /**
226  * Set the role of a policy key object.
227  * \param[in] policy_key a policy_key_t pointer.
228  * \param[in] role a policy_key_role_t.
229  * \return DB_ERROR_* on failure, otherwise DB_OK.
230  */
231 extern int policy_key_set_role(policy_key_t* policy_key, policy_key_role_t role);
232 
233 /**
234  * Set the algorithm of a policy key object.
235  * \param[in] policy_key a policy_key_t pointer.
236  * \param[in] algorithm an unsigned integer with a maximum value of 255.
237  * \return DB_ERROR_* on failure, otherwise DB_OK.
238  */
239 extern int policy_key_set_algorithm(policy_key_t* policy_key, unsigned int algorithm);
240 
241 /**
242  * Set the bits of a policy key object.
243  * \param[in] policy_key a policy_key_t pointer.
244  * \param[in] bits an unsigned integer.
245  * \return DB_ERROR_* on failure, otherwise DB_OK.
246  */
247 extern int policy_key_set_bits(policy_key_t* policy_key, unsigned int bits);
248 
249 /**
250  * Set the lifetime of a policy key object.
251  * \param[in] policy_key a policy_key_t pointer.
252  * \param[in] lifetime an unsigned integer.
253  * \return DB_ERROR_* on failure, otherwise DB_OK.
254  */
255 extern int policy_key_set_lifetime(policy_key_t* policy_key, unsigned int lifetime);
256 
257 /**
258  * Set the repository of a policy key object.
259  * \param[in] policy_key a policy_key_t pointer.
260  * \param[in] repository_text a character pointer.
261  * \return DB_ERROR_* on failure, otherwise DB_OK.
262  */
263 extern int policy_key_set_repository(policy_key_t* policy_key, const char* repository_text);
264 
265 /**
266  * Set the standby of a policy key object.
267  * \param[in] policy_key a policy_key_t pointer.
268  * \param[in] standby an unsigned integer.
269  * \return DB_ERROR_* on failure, otherwise DB_OK.
270  */
271 extern int policy_key_set_standby(policy_key_t* policy_key, unsigned int standby);
272 
273 /**
274  * Set the manual_rollover of a policy key object.
275  * \param[in] policy_key a policy_key_t pointer.
276  * \param[in] manual_rollover an unsigned integer.
277  * \return DB_ERROR_* on failure, otherwise DB_OK.
278  */
279 extern int policy_key_set_manual_rollover(policy_key_t* policy_key, unsigned int manual_rollover);
280 
281 /**
282  * Set the rfc5011 of a policy key object.
283  * \param[in] policy_key a policy_key_t pointer.
284  * \param[in] rfc5011 an unsigned integer.
285  * \return DB_ERROR_* on failure, otherwise DB_OK.
286  */
287 extern int policy_key_set_rfc5011(policy_key_t* policy_key, unsigned int rfc5011);
288 
289 /**
290  * Set the minimize of a policy key object.
291  * \param[in] policy_key a policy_key_t pointer.
292  * \param[in] minimize an unsigned integer.
293  * \return DB_ERROR_* on failure, otherwise DB_OK.
294  */
295 extern int policy_key_set_minimize(policy_key_t* policy_key, unsigned int minimize);
296 
297 /**
298  * Create a policy key object in the database.
299  * \param[in] policy_key a policy_key_t pointer.
300  * \return DB_ERROR_* on failure, otherwise DB_OK.
301  */
302 extern int policy_key_create(policy_key_t* policy_key);
303 
304 /**
305  * Get a policy key object from the database by a id specified in `id`.
306  * \param[in] policy_key a policy_key_t pointer.
307  * \param[in] id a db_value_t pointer.
308  * \return DB_ERROR_* on failure, otherwise DB_OK.
309  */
310 extern int policy_key_get_by_id(policy_key_t* policy_key, const db_value_t* id);
311 
312 /**
313  * Delete a policy key object from the database.
314  * \param[in] policy_key a policy_key_t pointer.
315  * \return DB_ERROR_* on failure, otherwise DB_OK.
316  */
317 extern int policy_key_delete(policy_key_t* policy_key);
318 
319 /**
320  * A list of policy key objects.
321  */
322 struct policy_key_list {
323     db_object_t* dbo;
324     db_result_list_t* result_list;
325     const db_result_t* result;
326     policy_key_t* policy_key;
327     int object_store;
328     policy_key_t** object_list;
329     size_t object_list_size;
330     size_t object_list_position;
331     int object_list_first;
332     int associated_fetch;
333     policy_list_t* policy_id_list;
334 };
335 
336 /**
337  * Create a new policy key object list.
338  * \param[in] connection a db_connection_t pointer.
339  * \return a policy_key_list_t pointer or NULL on error.
340  */
341 extern policy_key_list_t* policy_key_list_new(const db_connection_t* connection);
342 
343 /**
344  * Create a new policy key object list that is a copy of another.
345  * \param[in] policy_key_list a policy_key_list_t pointer.
346  * \return a policy_key_list_t pointer or NULL on error.
347  */
348 extern policy_key_list_t* policy_key_list_new_copy(const policy_key_list_t* policy_key_copy);
349 
350 /**
351  * Specify that objects should be stored within the list as they are fetch,
352  * this is optimal if the list is to be iterated over more then once.
353  * \param[in] policy_key_list a policy_key_list_t pointer.
354  * \return DB_ERROR_* on failure, otherwise DB_OK.
355  */
356 extern int policy_key_list_object_store(policy_key_list_t* policy_key_list);
357 
358 /**
359  * Delete a policy key object list.
360  * \param[in] policy_key_list a policy_key_list_t pointer.
361  */
362 extern void policy_key_list_free(policy_key_list_t* policy_key_list);
363 
364 /**
365  * Copy the content of another policy key object list.
366  * \param[in] policy_key_list a policy_key_list_t pointer.
367  * \param[in] from_policy_key_list a policy_key_list_t pointer.
368  * \return DB_ERROR_* on failure, otherwise DB_OK.
369  */
370 extern int policy_key_list_copy(policy_key_list_t* policy_key_list, const policy_key_list_t* from_policy_key_list);
371 
372 /**
373  * Get policy key objects from the database by a clause list.
374  * \param[in] policy_key_list a policy_key_list_t pointer.
375  * \param[in] clause_list a db_clause_list_t pointer.
376  * \return DB_ERROR_* on failure, otherwise DB_OK.
377  */
378 extern int policy_key_list_get_by_clauses(policy_key_list_t* policy_key_list, const db_clause_list_t* clause_list);
379 
380 /**
381  * Get policy key objects from the database by a policy_id specified in `policy_id`.
382  * \param[in] policy_key_list a policy_key_list_t pointer.
383  * \param[in] policy_id a db_value_t pointer.
384  * \return DB_ERROR_* on failure, otherwise DB_OK.
385  */
386 extern int policy_key_list_get_by_policy_id(policy_key_list_t* policy_key_list, const db_value_t* policy_id);
387 
388 /**
389  * Get a new list of policy key objects from the database by a policy_id specified in `policy_id`.
390  * \param[in] connection a db_connection_t pointer.
391  * \param[in] policy_id a db_value_t pointer.
392  * \return a policy_key_list_t pointer or NULL on error.
393  */
394 extern policy_key_list_t* policy_key_list_new_get_by_policy_id(const db_connection_t* connection, const db_value_t* policy_id);
395 
396 /**
397  * Get the first policy key object in a policy key object list and reset the
398  * position of the list.
399  * \param[in] policy_key_list a policy_key_list_t pointer.
400  * \return a policy_key_t pointer or NULL on error or if there are no
401  * policy key objects in the policy key object list.
402  */
403 extern const policy_key_t* policy_key_list_begin(policy_key_list_t* policy_key_list);
404 
405 /**
406  * Get the next policy key object in a policy key object list.
407  * Ownership of this object is retained within the list and the object is only
408  * valid until the next call to this function.
409  * \param[in] policy_key_list a policy_key_list_t pointer.
410  * \return a policy_key_t pointer or NULL on error or if there are no more
411  * policy key objects in the policy key object list.
412  */
413 extern const policy_key_t* policy_key_list_next(policy_key_list_t* policy_key_list);
414 
415 /**
416  * Get the next policy key object in a policy key object list.
417  * The caller will be given ownership of this object and is responsible for
418  * freeing it.
419  * \param[in] policy_key_list a policy_key_list_t pointer.
420  * \return a policy_key_t pointer or NULL on error or if there are no more
421  * policy key objects in the policy key object list.
422  */
423 extern policy_key_t* policy_key_list_get_next(policy_key_list_t* policy_key_list);
424 
425 /**
426  * Get the size of a policy key object list.
427  * \param[in] policy_key_list a policy_key_list_t pointer.
428  * \return a size_t with the size of the list or zero on error, if the list is
429  * empty or if the backend does not support returning the size.
430  */
431 extern size_t policy_key_list_size(policy_key_list_t* policy_key_list);
432 
433 extern policy_key_t * policy_key_new_get_by_policyid_and_role (const db_connection_t* connection, const db_value_t* policyid, const policy_key_role_t role);
434 
435 extern int policy_key_get_by_policyid_and_role(policy_key_t* policy_key, const db_value_t* policyid , const policy_key_role_t role);
436 #endif
437