OpenDNSSEC-signer  2.1.7
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 "file.h"
34 #include "log.h"
35 #include "status.h"
36 #include "signer/zone.h"
37 
38 #include <stdlib.h>
39 
40 static const char* adapter_str = "adapter";
41 
42 
48 adapter_create(const char* str, adapter_mode type, unsigned in)
49 {
50  adapter_type* adapter = NULL;
51  CHECKALLOC(adapter = (adapter_type*) malloc(sizeof(adapter_type)));
52  adapter->type = type;
53  adapter->inbound = in;
54  adapter->error = 0;
55  adapter->config = NULL;
56  adapter->config_last_modified = 0;
57  adapter->configstr = strdup(str);
58  if (!adapter->configstr) {
59  ods_log_error("[%s] unable to create adapter: allocator_strdup() "
60  "failed", adapter_str);
61  adapter_cleanup(adapter);
62  return NULL;
63  }
64  /* type specific */
65  switch(adapter->type) {
66  case ADAPTER_FILE:
67  break;
68  case ADAPTER_DNS:
69  if (adapter->inbound) {
70  adapter->config = (void*) dnsin_create();
71  if (!adapter->config) {
72  ods_log_error("[%s] unable to create adapter: "
73  "dnsin_create() failed", adapter_str);
74  adapter_cleanup(adapter);
75  return NULL;
76  }
77  } else {
78  adapter->config = (void*) dnsout_create();
79  if (!adapter->config) {
80  ods_log_error("[%s] unable to create adapter: "
81  "dnsout_create() failed", adapter_str);
82  adapter_cleanup(adapter);
83  return NULL;
84  }
85  }
86  break;
87  default:
88  break;
89  }
90  return adapter;
91 }
92 
93 
98 ods_status
100 {
101  dnsin_type* dnsin = NULL;
102  dnsout_type* dnsout = NULL;
103  ods_status status = ODS_STATUS_OK;
104 
105  if (!adapter || !adapter->configstr) {
106  return ODS_STATUS_ASSERT_ERR;
107  }
108  /* type specific */
109  switch(adapter->type) {
110  case ADAPTER_FILE:
111  break;
112  case ADAPTER_DNS:
113  ods_log_assert(adapter->config);
114  if (adapter->inbound) {
115  dnsin = (dnsin_type*)adapter->config;
116  status = dnsin_update(&dnsin, adapter->configstr,
117  &adapter->config_last_modified);
118  if (status == ODS_STATUS_OK) {
119  ods_log_assert(dnsin);
120  } else if (status != ODS_STATUS_UNCHANGED) {
121  return status;
122  }
123  return ODS_STATUS_OK;
124  } else { /* outbound */
125  dnsout = (dnsout_type*)adapter->config;
126  status = dnsout_update(&dnsout, adapter->configstr,
127  &adapter->config_last_modified);
128  if (status == ODS_STATUS_OK) {
129  ods_log_assert(dnsout);
130  } else if (status != ODS_STATUS_UNCHANGED) {
131  return status;
132  }
133  }
134  break;
135  default:
136  break;
137  }
138  return ODS_STATUS_OK;
139 }
140 
141 
142 /*
143  * Read zone from input adapter.
144  *
145  */
146 ods_status
148 {
149  if (!zone || !zone->adinbound) {
150  ods_log_error("[%s] unable to read zone: no input adapter",
151  adapter_str);
152  return ODS_STATUS_ASSERT_ERR;
153  }
154  ods_log_assert(zone->adinbound->configstr);
155  switch (zone->adinbound->type) {
156  case ADAPTER_FILE:
157  ods_log_verbose("[%s] read zone %s from file input adapter %s",
158  adapter_str, zone->name, zone->adinbound->configstr);
159  return adfile_read(zone);
160  case ADAPTER_DNS:
161  ods_log_verbose("[%s] read zone %s from dns input adapter %s",
162  adapter_str, zone->name, zone->adinbound->configstr);
163  return addns_read(zone);
164  default:
165  ods_log_error("[%s] unable to read zone %s from adapter: unknown "
166  "adapter", adapter_str, zone->name);
167  return ODS_STATUS_ERR;
168  }
169  /* not reached */
170  return ODS_STATUS_ERR;
171 }
172 
173 
178 ods_status
180 {
181  if (!zone || !zone->db || !zone->adoutbound) {
182  ods_log_error("[%s] unable to write zone: no output adapter",
183  adapter_str);
184  return ODS_STATUS_ASSERT_ERR;
185  }
186  ods_log_assert(zone->name);
187  ods_log_assert(zone->adoutbound->configstr);
188 
189  switch(zone->adoutbound->type) {
190  case ADAPTER_FILE:
191  ods_log_verbose("[%s] write zone %s serial %u to output file "
192  "adapter %s", adapter_str, zone->name,
193  zone->db->intserial, zone->adoutbound->configstr);
194  return adfile_write(zone, zone->adoutbound->configstr);
195  case ADAPTER_DNS:
196  return addns_write(zone);
197  default:
198  ods_log_error("[%s] unable to write zone %s to adapter: unknown "
199  "adapter", adapter_str, zone->name);
200  return ODS_STATUS_ERR;
201  }
202  /* not reached */
203  return ODS_STATUS_ERR;
204 }
205 
206 
211 int
213 {
214  if (!a1 && !a2) {
215  return 0;
216  } else if (!a1) {
217  return -1;
218  } else if (!a2) {
219  return 1;
220  } else if (a1->inbound != a2->inbound) {
221  return a1->inbound - a2->inbound;
222  } else if (a1->type != a2->type) {
223  return a1->type - a2->type;
224  }
225  return ods_strcmp(a1->configstr, a2->configstr);
226 }
227 
228 
233 void
235 {
236  if (!adapter) {
237  return;
238  }
239  free((void*)adapter->configstr);
240  switch(adapter->type) {
241  case ADAPTER_FILE:
242  break;
243  case ADAPTER_DNS:
244  if (adapter->inbound) {
245  dnsin_cleanup((dnsin_type*) adapter->config);
246  } else { /* outbound */
247  dnsout_cleanup((dnsout_type*) adapter->config);
248  }
249  break;
250  default:
251  break;
252  }
253  free(adapter);
254 }
adapter_struct::type
adapter_mode type
Definition: adapter.h:58
ADAPTER_DNS
@ ADAPTER_DNS
Definition: adapter.h:42
zone_struct
Definition: zone.h:60
adfile_write
ods_status adfile_write(void *zone, const char *filename)
Definition: adfile.c:326
adapter_struct
Definition: adapter.h:57
dnsin_cleanup
void dnsin_cleanup(dnsin_type *addns)
Definition: addns.c:893
dnsin_struct
Definition: addns.h:49
zone.h
adapter_load_config
ods_status adapter_load_config(adapter_type *adapter)
Definition: adapter.c:99
zone_struct::name
const char * name
Definition: zone.h:69
namedb_struct::intserial
uint32_t intserial
Definition: namedb.h:54
adapter_struct::config_last_modified
time_t config_last_modified
Definition: adapter.h:59
adapter_write
ods_status adapter_write(zone_type *zone)
Definition: adapter.c:179
adapter_struct::config
void * config
Definition: adapter.h:61
adapter_mode
enum adapter_mode_enum adapter_mode
Definition: adapter.h:44
adapter_create
adapter_type * adapter_create(const char *str, adapter_mode type, unsigned in)
Definition: adapter.c:48
adapter_struct::inbound
unsigned inbound
Definition: adapter.h:62
adfile_read
ods_status adfile_read(void *zone)
Definition: adfile.c:298
dnsin_create
dnsin_type * dnsin_create(void)
Definition: addns.c:502
zone_struct::db
namedb_type * db
Definition: zone.h:79
dnsout_create
dnsout_type * dnsout_create(void)
Definition: addns.c:518
dnsout_struct
Definition: addns.h:61
dnsin_update
ods_status dnsin_update(dnsin_type **addns, const char *filename, time_t *last_mod)
Definition: addns.c:568
dnsout_update
ods_status dnsout_update(dnsout_type **addns, const char *filename, time_t *last_mod)
Definition: addns.c:627
addns_read
ods_status addns_read(void *zone)
Definition: addns.c:685
adapter_read
ods_status adapter_read(zone_type *zone)
Definition: adapter.c:147
adapter.h
zone_struct::adoutbound
adapter_type * adoutbound
Definition: zone.h:75
ADAPTER_FILE
@ ADAPTER_FILE
Definition: adapter.h:41
adapter_struct::configstr
const char * configstr
Definition: adapter.h:60
adapter_compare
int adapter_compare(adapter_type *a1, adapter_type *a2)
Definition: adapter.c:212
adapter_struct::error
unsigned error
Definition: adapter.h:63
zone_struct::adinbound
adapter_type * adinbound
Definition: zone.h:74
adapter_cleanup
void adapter_cleanup(adapter_type *adapter)
Definition: adapter.c:234
dnsout_cleanup
void dnsout_cleanup(dnsout_type *addns)
Definition: addns.c:910
addns_write
ods_status addns_write(void *zone)
Definition: addns.c:768