OpenDNSSEC-signer  1.4.6
adapter.c
Go to the documentation of this file.
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 
32 #include "adapter/adapter.h"
33 #include "shared/allocator.h"
34 #include "shared/file.h"
35 #include "shared/log.h"
36 #include "shared/status.h"
37 #include "signer/zone.h"
38 
39 #include <stdlib.h>
40 
41 static const char* adapter_str = "adapter";
42 
43 
49 adapter_create(const char* str, adapter_mode type, unsigned in)
50 {
51  adapter_type* adapter = NULL;
52  allocator_type* allocator = NULL;
53  allocator = allocator_create(malloc, free);
54  if (!allocator) {
55  ods_log_error("[%s] unable to create adapter: allocator_create() "
56  "failed", adapter_str);
57  return NULL;
58  }
59  adapter = (adapter_type*) allocator_alloc(allocator, sizeof(adapter_type));
60  if (!adapter) {
61  ods_log_error("[%s] unable to create adapter: allocator_alloc() "
62  "failed", adapter_str);
63  allocator_cleanup(allocator);
64  return NULL;
65  }
66  adapter->allocator = allocator;
67  adapter->type = type;
68  adapter->inbound = in;
69  adapter->error = 0;
70  adapter->config = NULL;
71  adapter->config_last_modified = 0;
72  adapter->configstr = allocator_strdup(allocator, str);
73  if (!adapter->configstr) {
74  ods_log_error("[%s] unable to create adapter: allocator_strdup() "
75  "failed", adapter_str);
76  adapter_cleanup(adapter);
77  return NULL;
78  }
79  /* type specific */
80  switch(adapter->type) {
81  case ADAPTER_FILE:
82  break;
83  case ADAPTER_DNS:
84  if (adapter->inbound) {
85  adapter->config = (void*) dnsin_create();
86  if (!adapter->config) {
87  ods_log_error("[%s] unable to create adapter: "
88  "dnsin_create() failed", adapter_str);
89  adapter_cleanup(adapter);
90  return NULL;
91  }
92  } else {
93  adapter->config = (void*) dnsout_create();
94  if (!adapter->config) {
95  ods_log_error("[%s] unable to create adapter: "
96  "dnsout_create() failed", adapter_str);
97  adapter_cleanup(adapter);
98  return NULL;
99  }
100  }
101  break;
102  default:
103  break;
104  }
105  return adapter;
106 }
107 
108 
115 {
116  dnsin_type* dnsin = NULL;
117  dnsout_type* dnsout = NULL;
118  ods_status status = ODS_STATUS_OK;
119 
120  if (!adapter || !adapter->configstr) {
121  return ODS_STATUS_ASSERT_ERR;
122  }
123  /* type specific */
124  switch(adapter->type) {
125  case ADAPTER_FILE:
126  break;
127  case ADAPTER_DNS:
128  ods_log_assert(adapter->config);
129  if (adapter->inbound) {
130  status = dnsin_update(&dnsin, adapter->configstr,
131  &adapter->config_last_modified);
132  if (status == ODS_STATUS_OK) {
133  ods_log_assert(dnsin);
134  dnsin_cleanup((dnsin_type*) adapter->config);
135  adapter->config = (void*) dnsin;
136  } else if (status != ODS_STATUS_UNCHANGED) {
137  return status;
138  }
139  return ODS_STATUS_OK;
140  } else { /* outbound */
141  status = dnsout_update(&dnsout, adapter->configstr,
142  &adapter->config_last_modified);
143  if (status == ODS_STATUS_OK) {
144  ods_log_assert(dnsout);
145  dnsout_cleanup((dnsout_type*) adapter->config);
146  adapter->config = (void*) dnsout;
147  } else if (status != ODS_STATUS_UNCHANGED) {
148  return status;
149  }
150  }
151  break;
152  default:
153  break;
154  }
155  return ODS_STATUS_OK;
156 }
157 
158 
159 /*
160  * Read zone from input adapter.
161  *
162  */
164 adapter_read(void* zone)
165 {
166  zone_type* adzone = (zone_type*) zone;
167  if (!adzone || !adzone->adinbound) {
168  ods_log_error("[%s] unable to read zone: no input adapter",
169  adapter_str);
170  return ODS_STATUS_ASSERT_ERR;
171  }
173  switch (adzone->adinbound->type) {
174  case ADAPTER_FILE:
175  ods_log_verbose("[%s] read zone %s from file input adapter %s",
176  adapter_str, adzone->name, adzone->adinbound->configstr);
177  return adfile_read(zone);
178  break;
179  case ADAPTER_DNS:
180  ods_log_verbose("[%s] read zone %s from dns input adapter %s",
181  adapter_str, adzone->name, adzone->adinbound->configstr);
182  return addns_read(zone);
183  break;
184  default:
185  ods_log_error("[%s] unable to read zone %s from adapter: unknown "
186  "adapter", adapter_str, adzone->name);
187  return ODS_STATUS_ERR;
188  break;
189  }
190  /* not reached */
191  return ODS_STATUS_ERR;
192 }
193 
194 
200 adapter_write(void* zone)
201 {
202  zone_type* adzone = (zone_type*) zone;
203  if (!adzone || !adzone->db || !adzone->adoutbound) {
204  ods_log_error("[%s] unable to write zone: no output adapter",
205  adapter_str);
206  return ODS_STATUS_ASSERT_ERR;
207  }
208  ods_log_assert(adzone->name);
210 
211  switch(adzone->adoutbound->type) {
212  case ADAPTER_FILE:
213  ods_log_verbose("[%s] write zone %s serial %u to output file "
214  "adapter %s", adapter_str, adzone->name,
215  adzone->db->intserial, adzone->adoutbound->configstr);
216  return adfile_write(zone, adzone->adoutbound->configstr);
217  break;
218  case ADAPTER_DNS:
219  return addns_write(zone);
220  break;
221  default:
222  ods_log_error("[%s] unable to write zone %s to adapter: unknown "
223  "adapter", adapter_str, adzone->name);
224  return ODS_STATUS_ERR;
225  break;
226  }
227  /* not reached */
228  return ODS_STATUS_ERR;
229 }
230 
231 
236 int
238 {
239  if (!a1 && !a2) {
240  return 0;
241  } else if (!a1) {
242  return -1;
243  } else if (!a2) {
244  return 1;
245  } else if (a1->inbound != a2->inbound) {
246  return a1->inbound - a2->inbound;
247  } else if (a1->type != a2->type) {
248  return a1->type - a2->type;
249  }
250  return ods_strcmp(a1->configstr, a2->configstr);
251 }
252 
253 
258 void
260 {
261  allocator_type* allocator = NULL;
262  if (!adapter) {
263  return;
264  }
265  allocator = adapter->allocator;
266  allocator_deallocate(allocator, (void*) adapter->configstr);
267  switch(adapter->type) {
268  case ADAPTER_FILE:
269  break;
270  case ADAPTER_DNS:
271  if (adapter->inbound) {
272  dnsin_cleanup((dnsin_type*) adapter->config);
273  } else { /* outbound */
274  dnsout_cleanup((dnsout_type*) adapter->config);
275  }
276  break;
277  default:
278  break;
279  }
280  allocator_deallocate(allocator, (void*) adapter);
281  allocator_cleanup(allocator);
282  return;
283 }
uint32_t intserial
Definition: namedb.h:52
int adapter_compare(adapter_type *a1, adapter_type *a2)
Definition: adapter.c:237
void dnsout_cleanup(dnsout_type *addns)
Definition: addns.c:945
void * config
Definition: adapter.h:61
void * allocator_alloc(allocator_type *allocator, size_t size)
Definition: allocator.c:66
ods_status adapter_read(void *zone)
Definition: adapter.c:164
const char * configstr
Definition: adapter.h:60
ods_status adapter_load_config(adapter_type *adapter)
Definition: adapter.c:114
unsigned error
Definition: adapter.h:63
enum ods_enum_status ods_status
Definition: status.h:90
void ods_log_error(const char *format,...)
Definition: log.c:334
adapter_mode type
Definition: adapter.h:58
int ods_strcmp(const char *s1, const char *s2)
Definition: file.c:320
adapter_type * adoutbound
Definition: zone.h:82
allocator_type * allocator
Definition: adapter.h:57
ods_status adfile_read(void *zone)
Definition: adfile.c:298
namedb_type * db
Definition: zone.h:86
void dnsin_cleanup(dnsin_type *addns)
Definition: addns.c:924
allocator_type * allocator_create(void *(*allocator)(size_t size), void(*deallocator)(void *))
Definition: allocator.c:47
ods_status adfile_write(void *zone, const char *filename)
Definition: adfile.c:326
time_t config_last_modified
Definition: adapter.h:59
adapter_type * adinbound
Definition: zone.h:81
char * allocator_strdup(allocator_type *allocator, const char *string)
Definition: allocator.c:121
adapter_type * adapter_create(const char *str, adapter_mode type, unsigned in)
Definition: adapter.c:49
ods_status addns_read(void *zone)
Definition: addns.c:729
enum adapter_mode_enum adapter_mode
Definition: adapter.h:49
ods_status dnsout_update(dnsout_type **addns, const char *filename, time_t *last_mod)
Definition: addns.c:665
void ods_log_verbose(const char *format,...)
Definition: log.c:286
void allocator_cleanup(allocator_type *allocator)
Definition: allocator.c:151
const char * name
Definition: zone.h:76
void allocator_deallocate(allocator_type *allocator, void *data)
Definition: allocator.c:135
ods_status addns_write(void *zone)
Definition: addns.c:806
dnsin_type * dnsin_create(void)
Definition: addns.c:502
ods_status dnsin_update(dnsin_type **addns, const char *filename, time_t *last_mod)
Definition: addns.c:596
#define ods_log_assert(x)
Definition: log.h:154
unsigned inbound
Definition: adapter.h:62
ods_status adapter_write(void *zone)
Definition: adapter.c:200
void adapter_cleanup(adapter_type *adapter)
Definition: adapter.c:259
dnsout_type * dnsout_create(void)
Definition: addns.c:531