1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
pub mod dnssec;
use std::fs::File;
use std::io::Read;
use std::net::{Ipv4Addr, Ipv6Addr};
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::time::Duration;
use log;
use toml;
use proto::error::ProtoResult;
use trust_dns::rr::Name;
use authority::ZoneType;
use error::{ConfigError, ConfigResult};
use store::StoreConfig;
static DEFAULT_PATH: &'static str = "/var/named";
static DEFAULT_PORT: u16 = 53;
static DEFAULT_TLS_PORT: u16 = 853;
static DEFAULT_HTTPS_PORT: u16 = 443;
static DEFAULT_TCP_REQUEST_TIMEOUT: u64 = 5;
#[derive(Deserialize, Debug)]
pub struct Config {
#[serde(default)]
listen_addrs_ipv4: Vec<String>,
#[serde(default)]
listen_addrs_ipv6: Vec<String>,
listen_port: Option<u16>,
tls_listen_port: Option<u16>,
https_listen_port: Option<u16>,
tcp_request_timeout: Option<u64>,
log_level: Option<String>,
directory: Option<String>,
#[serde(default)]
zones: Vec<ZoneConfig>,
tls_cert: Option<dnssec::TlsCertConfig>,
}
impl Config {
pub fn read_config(path: &Path) -> ConfigResult<Config> {
let mut file: File = File::open(path)?;
let mut toml: String = String::new();
file.read_to_string(&mut toml)?;
toml.parse().map_err(Into::into)
}
pub fn get_listen_addrs_ipv4(&self) -> Vec<Ipv4Addr> {
self.listen_addrs_ipv4
.iter()
.map(|s| s.parse().unwrap())
.collect()
}
pub fn get_listen_addrs_ipv6(&self) -> Vec<Ipv6Addr> {
self.listen_addrs_ipv6
.iter()
.map(|s| s.parse().unwrap())
.collect()
}
pub fn get_listen_port(&self) -> u16 {
self.listen_port.unwrap_or(DEFAULT_PORT)
}
pub fn get_tls_listen_port(&self) -> u16 {
self.tls_listen_port.unwrap_or(DEFAULT_TLS_PORT)
}
pub fn get_https_listen_port(&self) -> u16 {
self.https_listen_port.unwrap_or(DEFAULT_HTTPS_PORT)
}
pub fn get_tcp_request_timeout(&self) -> Duration {
Duration::from_secs(
self.tcp_request_timeout
.unwrap_or(DEFAULT_TCP_REQUEST_TIMEOUT),
)
}
pub fn get_log_level(&self) -> log::Level {
if let Some(ref level_str) = self.log_level {
match level_str as &str {
"Trace" => log::Level::Trace,
"Debug" => log::Level::Debug,
"Info" => log::Level::Info,
"Warn" => log::Level::Warn,
"Error" => log::Level::Error,
_ => log::Level::Info,
}
} else {
log::Level::Info
}
}
pub fn get_directory(&self) -> &Path {
self.directory
.as_ref()
.map_or(Path::new(DEFAULT_PATH), |s| Path::new(s))
}
pub fn get_zones(&self) -> &[ZoneConfig] {
&self.zones
}
pub fn get_tls_cert(&self) -> Option<&dnssec::TlsCertConfig> {
self.tls_cert.as_ref()
}
}
impl FromStr for Config {
type Err = ConfigError;
fn from_str(toml: &str) -> ConfigResult<Config> {
toml::de::from_str(toml).map_err(Into::into)
}
}
#[derive(Deserialize, PartialEq, Debug)]
pub struct ZoneConfig {
pub zone: String,
pub zone_type: ZoneType,
pub file: Option<String>,
pub allow_update: Option<bool>,
pub allow_axfr: Option<bool>,
pub enable_dnssec: Option<bool>,
#[serde(default)]
pub keys: Vec<dnssec::KeyConfig>,
#[serde(default)]
pub stores: Option<StoreConfig>,
}
impl ZoneConfig {
pub fn new(
zone: String,
zone_type: ZoneType,
file: String,
allow_update: Option<bool>,
allow_axfr: Option<bool>,
enable_dnssec: Option<bool>,
keys: Vec<dnssec::KeyConfig>,
) -> Self {
ZoneConfig {
zone,
zone_type,
file: Some(file),
allow_update,
allow_axfr,
enable_dnssec,
keys,
stores: None,
}
}
pub fn get_zone(&self) -> ProtoResult<Name> {
Name::parse(&self.zone, Some(&Name::new()))
}
pub fn get_zone_type(&self) -> ZoneType {
self.zone_type
}
pub fn get_file(&self) -> PathBuf {
PathBuf::from(self.file.as_ref().unwrap())
}
pub fn is_update_allowed(&self) -> bool {
self.allow_update.unwrap_or(false)
}
pub fn is_axfr_allowed(&self) -> bool {
self.allow_axfr.unwrap_or(false)
}
pub fn is_dnssec_enabled(&self) -> bool {
self.enable_dnssec.unwrap_or(false)
}
pub fn get_keys(&self) -> &[dnssec::KeyConfig] {
&self.keys
}
}