OpenDNSSEC-signer  1.4.1
adapter.c
Go to the documentation of this file.
1 /*
2  * $Id: adapter.c 6478 2012-07-13 06:40:25Z matthijs $
3  *
4  * Copyright (c) 2009 NLNet Labs. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
21  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
23  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
25  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  */
28 
34 #include "adapter/adapter.h"
35 #include "shared/allocator.h"
36 #include "shared/file.h"
37 #include "shared/log.h"
38 #include "shared/status.h"
39 #include "signer/zone.h"
40 
41 #include <stdlib.h>
42 
43 static const char* adapter_str = "adapter";
44 
45 
51 adapter_create(const char* str, adapter_mode type, unsigned in)
52 {
53  adapter_type* adapter = NULL;
54  allocator_type* allocator = NULL;
55  allocator = allocator_create(malloc, free);
56  if (!allocator) {
57  ods_log_error("[%s] unable to create adapter: allocator_create() "
58  "failed", adapter_str);
59  return NULL;
60  }
61  adapter = (adapter_type*) allocator_alloc(allocator, sizeof(adapter_type));
62  if (!adapter) {
63  ods_log_error("[%s] unable to create adapter: allocator_alloc() "
64  "failed", adapter_str);
65  allocator_cleanup(allocator);
66  return NULL;
67  }
68  adapter->allocator = allocator;
69  adapter->type = type;
70  adapter->inbound = in;
71  adapter->error = 0;
72  adapter->config = NULL;
73  adapter->config_last_modified = 0;
74  adapter->configstr = allocator_strdup(allocator, str);
75  if (!adapter->configstr) {
76  ods_log_error("[%s] unable to create adapter: allocator_strdup() "
77  "failed", adapter_str);
78  adapter_cleanup(adapter);
79  return NULL;
80  }
81  /* type specific */
82  switch(adapter->type) {
83  case ADAPTER_FILE:
84  break;
85  case ADAPTER_DNS:
86  if (adapter->inbound) {
87  adapter->config = (void*) dnsin_create();
88  if (!adapter->config) {
89  ods_log_error("[%s] unable to create adapter: "
90  "dnsin_create() failed", adapter_str);
91  adapter_cleanup(adapter);
92  return NULL;
93  }
94  } else {
95  adapter->config = (void*) dnsout_create();
96  if (!adapter->config) {
97  ods_log_error("[%s] unable to create adapter: "
98  "dnsout_create() failed", adapter_str);
99  adapter_cleanup(adapter);
100  return NULL;
101  }
102  }
103  break;
104  default:
105  break;
106  }
107  return adapter;
108 }
109 
110 
117 {
118  dnsin_type* dnsin = NULL;
119  dnsout_type* dnsout = NULL;
120  ods_status status = ODS_STATUS_OK;
121 
122  if (!adapter || !adapter->configstr) {
123  return ODS_STATUS_ASSERT_ERR;
124  }
125  /* type specific */
126  switch(adapter->type) {
127  case ADAPTER_FILE:
128  break;
129  case ADAPTER_DNS:
130  ods_log_assert(adapter->config);
131  if (adapter->inbound) {
132  status = dnsin_update(&dnsin, adapter->configstr,
133  &adapter->config_last_modified);
134  if (status == ODS_STATUS_OK) {
135  ods_log_assert(dnsin);
136  dnsin_cleanup((dnsin_type*) adapter->config);
137  adapter->config = (void*) dnsin;
138  } else if (status != ODS_STATUS_UNCHANGED) {
139  return status;
140  }
141  return ODS_STATUS_OK;
142  } else { /* outbound */
143  status = dnsout_update(&dnsout, adapter->configstr,
144  &adapter->config_last_modified);
145  if (status == ODS_STATUS_OK) {
146  ods_log_assert(dnsout);
147  dnsout_cleanup((dnsout_type*) adapter->config);
148  adapter->config = (void*) dnsout;
149  } else if (status != ODS_STATUS_UNCHANGED) {
150  return status;
151  }
152  }
153  break;
154  default:
155  break;
156  }
157  return ODS_STATUS_OK;
158 }
159 
160 
161 /*
162  * Read zone from input adapter.
163  *
164  */
166 adapter_read(void* zone)
167 {
168  zone_type* adzone = (zone_type*) zone;
169  if (!adzone || !adzone->adinbound) {
170  ods_log_error("[%s] unable to read zone: no input adapter",
171  adapter_str);
172  return ODS_STATUS_ASSERT_ERR;
173  }
175  switch (adzone->adinbound->type) {
176  case ADAPTER_FILE:
177  ods_log_verbose("[%s] read zone %s from file input adapter %s",
178  adapter_str, adzone->name, adzone->adinbound->configstr);
179  return adfile_read(zone);
180  break;
181  case ADAPTER_DNS:
182  ods_log_verbose("[%s] read zone %s from dns input adapter %s",
183  adapter_str, adzone->name, adzone->adinbound->configstr);
184  return addns_read(zone);
185  break;
186  default:
187  ods_log_error("[%s] unable to read zone %s from adapter: unknown "
188  "adapter", adapter_str, adzone->name);
189  return ODS_STATUS_ERR;
190  break;
191  }
192  /* not reached */
193  return ODS_STATUS_ERR;
194 }
195 
196 
202 adapter_write(void* zone)
203 {
204  zone_type* adzone = (zone_type*) zone;
205  if (!adzone || !adzone->db || !adzone->adoutbound) {
206  ods_log_error("[%s] unable to write zone: no output adapter",
207  adapter_str);
208  return ODS_STATUS_ASSERT_ERR;
209  }
210  ods_log_assert(adzone->name);
212 
213  switch(adzone->adoutbound->type) {
214  case ADAPTER_FILE:
215  ods_log_verbose("[%s] write zone %s serial %u to output file "
216  "adapter %s", adapter_str, adzone->name,
217  adzone->db->intserial, adzone->adoutbound->configstr);
218  return adfile_write(zone, adzone->adoutbound->configstr);
219  break;
220  case ADAPTER_DNS:
221  return addns_write(zone);
222  break;
223  default:
224  ods_log_error("[%s] unable to write zone %s to adapter: unknown "
225  "adapter", adapter_str, adzone->name);
226  return ODS_STATUS_ERR;
227  break;
228  }
229  /* not reached */
230  return ODS_STATUS_ERR;
231 }
232 
233 
238 int
240 {
241  if (!a1 && !a2) {
242  return 0;
243  } else if (!a1) {
244  return -1;
245  } else if (!a2) {
246  return 1;
247  } else if (a1->inbound != a2->inbound) {
248  return a1->inbound - a2->inbound;
249  } else if (a1->type != a2->type) {
250  return a1->type - a2->type;
251  }
252  return ods_strcmp(a1->configstr, a2->configstr);
253 }
254 
255 
260 void
262 {
263  allocator_type* allocator = NULL;
264  if (!adapter) {
265  return;
266  }
267  allocator = adapter->allocator;
268  allocator_deallocate(allocator, (void*) adapter->configstr);
269  switch(adapter->type) {
270  case ADAPTER_FILE:
271  break;
272  case ADAPTER_DNS:
273  if (adapter->inbound) {
274  dnsin_cleanup((dnsin_type*) adapter->config);
275  } else { /* outbound */
276  dnsout_cleanup((dnsout_type*) adapter->config);
277  }
278  break;
279  default:
280  break;
281  }
282  allocator_deallocate(allocator, (void*) adapter);
283  allocator_cleanup(allocator);
284  return;
285 }
uint32_t intserial
Definition: namedb.h:54
int adapter_compare(adapter_type *a1, adapter_type *a2)
Definition: adapter.c:239
void dnsout_cleanup(dnsout_type *addns)
Definition: addns.c:865
void * config
Definition: adapter.h:63
void * allocator_alloc(allocator_type *allocator, size_t size)
Definition: allocator.c:68
ods_status adapter_read(void *zone)
Definition: adapter.c:166
const char * configstr
Definition: adapter.h:62
ods_status adapter_load_config(adapter_type *adapter)
Definition: adapter.c:116
unsigned error
Definition: adapter.h:65
enum ods_enum_status ods_status
Definition: status.h:89
void ods_log_error(const char *format,...)
Definition: log.c:336
adapter_mode type
Definition: adapter.h:60
int ods_strcmp(const char *s1, const char *s2)
Definition: file.c:312
adapter_type * adoutbound
Definition: zone.h:84
allocator_type * allocator
Definition: adapter.h:59
ods_status adfile_read(void *zone)
Definition: adfile.c:300
namedb_type * db
Definition: zone.h:88
void dnsin_cleanup(dnsin_type *addns)
Definition: addns.c:844
allocator_type * allocator_create(void *(*allocator)(size_t size), void(*deallocator)(void *))
Definition: allocator.c:49
ods_status adfile_write(void *zone, const char *filename)
Definition: adfile.c:328
time_t config_last_modified
Definition: adapter.h:61
adapter_type * adinbound
Definition: zone.h:83
char * allocator_strdup(allocator_type *allocator, const char *string)
Definition: allocator.c:123
adapter_type * adapter_create(const char *str, adapter_mode type, unsigned in)
Definition: adapter.c:51
ods_status addns_read(void *zone)
Definition: addns.c:647
enum adapter_mode_enum adapter_mode
Definition: adapter.h:51
ods_status dnsout_update(dnsout_type **addns, const char *filename, time_t *last_mod)
Definition: addns.c:583
void ods_log_verbose(const char *format,...)
Definition: log.c:288
void allocator_cleanup(allocator_type *allocator)
Definition: allocator.c:153
const char * name
Definition: zone.h:78
void allocator_deallocate(allocator_type *allocator, void *data)
Definition: allocator.c:137
ods_status addns_write(void *zone)
Definition: addns.c:726
dnsin_type * dnsin_create(void)
Definition: addns.c:420
ods_status dnsin_update(dnsin_type **addns, const char *filename, time_t *last_mod)
Definition: addns.c:514
#define ods_log_assert(x)
Definition: log.h:156
unsigned inbound
Definition: adapter.h:64
ods_status adapter_write(void *zone)
Definition: adapter.c:202
void adapter_cleanup(adapter_type *adapter)
Definition: adapter.c:261
dnsout_type * dnsout_create(void)
Definition: addns.c:449