1 /*
2  * Copyright (c) 2009 NLNet Labs. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
17  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
19  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
21  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
23  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  */
26 
27 #ifndef SIGNER_RRSET_H
28 #define SIGNER_RRSET_H
29 
30 #include "config.h"
31 #include <ldns/ldns.h>
32 
33 typedef struct rrsig_struct rrsig_type;
34 typedef struct rr_struct rr_type;
35 typedef struct rrset_struct rrset_type;
36 
37 #include "status.h"
38 #include "signer/stats.h"
39 #include "libhsm.h"
40 #include "domain.h"
41 #include "zone.h"
42 #include "datastructure.h"
43 
44 struct rrsig_struct {
45     ldns_rr* rr;
46     domain_type* owner;
47     const char* key_locator;
48     uint32_t key_flags;
49 };
50 
51 struct rr_struct {
52     ldns_rr* rr;
53     domain_type* owner;
54     unsigned exists : 1;
55     unsigned is_added : 1;
56     unsigned is_removed : 1;
57 };
58 
59 struct rrset_struct {
60     rrset_type* next;
61     zone_type* zone;
62     domain_type* domain;
63     ldns_rr_type rrtype;
64     rr_type* rrs;
65     size_t rr_count;
66     collection_t rrsigs;
67     unsigned needs_signing : 1;
68 };
69 
70 /**
71  * Log RR.
72  * \param[in] rr RR
73  * \param[in] pre log message
74  * \param[in] level log level
75  *
76  */
77 void log_rr(ldns_rr* rr, const char* pre, int level);
78 
79 /**
80  * Log RRset.
81  * \param[in] dname domain name
82  * \param[in] type RRtype
83  * \param[in] pre log message
84  * \param[in] level log level
85  *
86  */
87 void log_rrset(ldns_rdf* dname, ldns_rr_type type, const char* pre, int level);
88 
89 /**
90  * Get the string-format of RRtype.
91  * \param[in] type RRtype
92  * \return const char* string-format of RRtype
93  *
94  */
95 const char* rrset_type2str(ldns_rr_type type);
96 
97 /**
98  * Create RRset.
99  * \param[in] zoneptr zone reference
100  * \param[in] type RRtype
101  * \return rrset_type* RRset
102  *
103  */
104 rrset_type* rrset_create(zone_type* zone, ldns_rr_type type);
105 
106 /**
107  * Lookup RR in RRset.
108  * \param[in] rrset RRset
109  * \param[in] rr RR
110  * \return rr_type* RR if found
111  *
112  */
113 rr_type* rrset_lookup_rr(rrset_type* rrset, ldns_rr* rr);
114 
115 /**
116  * What TTL should new RR's in this RRS get?
117  *
118  */
119 uint32_t rrset_lookup_ttl(rrset_type* rrset, uint32_t default_ttl);
120 
121 /**
122  * Count the number of RRs in this RRset that have is_added.
123  * \param[in] rrset RRset
124  * \return size_t number of RRs
125  *
126  */
127 size_t rrset_count_rr_is_added(rrset_type* rrset);
128 
129 /**
130  * Add RR to RRset.
131  * \param[in] rrset RRset
132  * \param[in] rr RR
133  * \return rr_type* added RR
134  *
135  */
136 rr_type* rrset_add_rr(rrset_type* rrset, ldns_rr* rr);
137 
138 /**
139  * Delete RR from RRset.
140  * \param[in] rrset RRset
141  * \param[in] rrnum position of RR
142  *
143  */
144 void rrset_del_rr(rrset_type* rrset, uint16_t rrnum);
145 
146 /**
147  * Add RRSIG to RRset.
148  * \param[in] rrset RRset
149  * \param[in] rr RRSIG
150  * \param[in] locator key locator
151  * \param[in] flags key flags
152  *
153  */
154 void rrset_add_rrsig(rrset_type* rrset, ldns_rr* rr,
155     const char* locator, uint32_t flags);
156 
157 /**
158  * Delete all RRSIG from RRset and add then to the zone's outgoing IXFR as change.
159  * \param[in] rrset RRset
160  * \param[in] rrnum position of RRSIG
161  *
162  */
163 void rrset_drop_rrsigs(zone_type* zone, rrset_type* rrset);
164 
165 /**
166  * Apply differences at RRset.
167  * \param[in] rrset RRset
168  * \param[in] is_ixfr true if incremental change
169  * \param[in] more_coming more transactions possible
170  *
171  */
172 void rrset_diff(rrset_type* rrset, unsigned is_ixfr, unsigned more_coming);
173 
174 /**
175  * Sign RRset.
176  * \param[in] ctx HSM context
177  * \param[in] rrset RRset
178  * \param[in] signtime time when the zone is being signed
179  * \return ods_status status
180  *
181  */
182 ods_status rrset_sign(hsm_ctx_t* ctx, rrset_type* rrset, time_t signtime);
183 
184 /**
185  * Obtain a resource record (containing a signature of a dnskeyset or
186  * a dnskeyset, but that is not a hard requirement), from a raw string
187  * \param[out] dnskey the resulting resource record
188  * \param[in] resourcerecord the string representation of the resource record
189  * \param[in] ttl the time-to-live to use if non is specified
190  * \param[in] apex the owner domain is none can be determined from input string
191  */
192 ods_status rrset_getliteralrr(ldns_rr** dnskey, const char *resourcerecord, uint32_t ttl, ldns_rdf* apex);
193 
194 /**
195  * Print RRset.
196  * \param[in] fd file descriptor
197  * \param[in] rrset RRset to be printed
198  * \param[in] skip_rrsigs if true, don't print RRSIG records
199  * \param[out] status status
200  *
201  */
202 void rrset_print(FILE* fd, rrset_type* rrset, int skip_rrsigs,
203     ods_status* status);
204 
205 /**
206  * Clean up RRset.
207  * \param[in] rrset RRset to be cleaned up
208  *
209  */
210 void rrset_cleanup(rrset_type* rrset);
211 
212 /**
213  * Backup RRset.
214  * \param[in] fd file descriptor
215  * \param[in] rrset RRset
216  *
217  */
218 void rrset_backup2(FILE* fd, rrset_type* rrset);
219 
220 collection_class rrset_store_initialize(void);
221 
222 #endif /* SIGNER_RRSET_H */
223