OpenDNSSEC-signer  1.4.9
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  case ADAPTER_DNS:
179  ods_log_verbose("[%s] read zone %s from dns input adapter %s",
180  adapter_str, adzone->name, adzone->adinbound->configstr);
181  return addns_read(zone);
182  default:
183  ods_log_error("[%s] unable to read zone %s from adapter: unknown "
184  "adapter", adapter_str, adzone->name);
185  return ODS_STATUS_ERR;
186  }
187  /* not reached */
188  return ODS_STATUS_ERR;
189 }
190 
191 
197 adapter_write(void* zone)
198 {
199  zone_type* adzone = (zone_type*) zone;
200  if (!adzone || !adzone->db || !adzone->adoutbound) {
201  ods_log_error("[%s] unable to write zone: no output adapter",
202  adapter_str);
203  return ODS_STATUS_ASSERT_ERR;
204  }
205  ods_log_assert(adzone->name);
207 
208  switch(adzone->adoutbound->type) {
209  case ADAPTER_FILE:
210  ods_log_verbose("[%s] write zone %s serial %u to output file "
211  "adapter %s", adapter_str, adzone->name,
212  adzone->db->intserial, adzone->adoutbound->configstr);
213  return adfile_write(zone, adzone->adoutbound->configstr);
214  case ADAPTER_DNS:
215  return addns_write(zone);
216  default:
217  ods_log_error("[%s] unable to write zone %s to adapter: unknown "
218  "adapter", adapter_str, adzone->name);
219  return ODS_STATUS_ERR;
220  }
221  /* not reached */
222  return ODS_STATUS_ERR;
223 }
224 
225 
230 int
232 {
233  if (!a1 && !a2) {
234  return 0;
235  } else if (!a1) {
236  return -1;
237  } else if (!a2) {
238  return 1;
239  } else if (a1->inbound != a2->inbound) {
240  return a1->inbound - a2->inbound;
241  } else if (a1->type != a2->type) {
242  return a1->type - a2->type;
243  }
244  return ods_strcmp(a1->configstr, a2->configstr);
245 }
246 
247 
252 void
254 {
255  allocator_type* allocator = NULL;
256  if (!adapter) {
257  return;
258  }
259  allocator = adapter->allocator;
260  allocator_deallocate(allocator, (void*) adapter->configstr);
261  switch(adapter->type) {
262  case ADAPTER_FILE:
263  break;
264  case ADAPTER_DNS:
265  if (adapter->inbound) {
266  dnsin_cleanup((dnsin_type*) adapter->config);
267  } else { /* outbound */
268  dnsout_cleanup((dnsout_type*) adapter->config);
269  }
270  break;
271  default:
272  break;
273  }
274  allocator_deallocate(allocator, (void*) adapter);
275  allocator_cleanup(allocator);
276  return;
277 }
uint32_t intserial
Definition: namedb.h:52
int adapter_compare(adapter_type *a1, adapter_type *a2)
Definition: adapter.c:231
void dnsout_cleanup(dnsout_type *addns)
Definition: addns.c:953
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:932
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:812
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:197
void adapter_cleanup(adapter_type *adapter)
Definition: adapter.c:253
dnsout_type * dnsout_create(void)
Definition: addns.c:531