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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
extern crate regex;
extern crate ini;

use self::regex::Regex;
use std::error::Error;
use std::fs;
use std::fs::File;
use std::io::prelude::*;
use std::io;
use self::ini::Ini;
use std::net::Ipv4Addr;
use std::str::FromStr;
use std::path::Path;

//FIXME: Handle unwrap() - try not to use in library

pub struct IPv4Network {
    pub ip: Ipv4Addr,
    pub prefix: u8
}

//FIXME: This is probably wrong, but at least its descriptive
pub type ValidationErrorString = &'static str;

#[derive(Debug)]
pub enum StaticIPError {
    Io(io::Error),
    Validation(ValidationErrorString),
}

pub trait NetworkConfigLoader {

    fn new(path: &'static str) -> Self;

    fn is_dhcp(&self) -> bool;

    fn ip_address(&self) -> Option<IPv4Network>;

    fn gateway(&self) -> Option<Ipv4Addr>;

    fn get_settings_section(&self) -> Option<String>;

    fn dns(&self) -> Vec<Ipv4Addr>;

    fn enable_dhcp(&self) -> Result<(), io::Error>;

    fn static_ip(&self, address: &str, gateway: &str) -> Result<(), StaticIPError>;

}

fn ip_from_match(address_match: Option<&str>, network_match: Option<&str> ) -> Option<IPv4Network> {

    let network: u8 = network_match.unwrap_or("0").parse().unwrap_or(0);

    match address_match {
        Some(result) => {
            match Ipv4Addr::from_str(result) {
                Ok(address) => {
                    return Some(IPv4Network{ ip: address, prefix: network })
                }
                Err(_) => {
                    return None
                }
            }
        },
        None => None
    }
}

fn read_to_string(path: &Path) -> Result<String,io::Error> {
    let display = path.display();
    let mut file = match File::open(path) {
        Err(why) => return Err(why),
        Ok(file) => file,
    };

    let mut s = String::new();

    //FIXME: Add the name of the file in the expect clause
    file.read_to_string(&mut s).ok().expect("Could not read file");

    Ok(s)
}

pub struct SystemdNetworkConfig<'a> {
    path: &'a Path
}

impl<'a> NetworkConfigLoader for SystemdNetworkConfig<'a> {

    fn new(path: &'static str) -> SystemdNetworkConfig {
        let default_path = "/etc/systemd/network/eth0.network";

        if path == "" {
            return SystemdNetworkConfig { path: Path::new(default_path) }
        }

        SystemdNetworkConfig { path: Path::new(path) }
    }


    fn is_dhcp(&self) -> bool {
        let conf = Ini::load_from_file(self.path.to_str().expect("Could to parse path")).unwrap();

        let section = conf.section(Some("Network".to_owned())).unwrap();

        let dhcp = section.get("DHCP").map_or("", |s| &s[..]);

        let re = Regex::new(r"(both|yes|ipv4|ipv6)").unwrap();
        return re.is_match(dhcp)
    }

    fn get_settings_section(&self) -> Option<String> {
        let content = read_to_string(self.path).unwrap_or("".to_string());
        let re = Regex::new(r"(?P<settings_section>\[Network\][^\[\0]+)").unwrap();
        return match re.captures(&content[..]) {
            Some(matched) => {
                let mut settings_section = String::new();
                settings_section = settings_section + matched.name("settings_section").unwrap();
                Some(settings_section)
            },
            None => None
        };
    }

    fn ip_address(&self) -> Option<IPv4Network> {
        let content = self.get_settings_section().unwrap_or(String::new());
        //FIXME: This regex will only work on the first address
        let re = Regex::new(r"Address=(?P<address>[^/]+)\b/\b(?P<network>(?:3[0-2]|[12][0-9]|[1-9]))\b").unwrap();
        return match re.captures(&content[..]) {
            Some(matched) => ip_from_match(matched.name("address"), matched.name("network")),
            None => { None }
        };
    }

    fn gateway(&self) -> Option<Ipv4Addr> {
        let content = self.get_settings_section().unwrap_or(String::new());
        //FIXME: This regex will only work on the first address
        let re = Regex::new(r"Gateway=(?P<address>[\d.]+)").unwrap();
        return match re.captures(&content[..]) {
            Some(matched) => {
                let ip_address = ip_from_match(matched.name("address"),Some("0"));
                return Some(ip_address.unwrap().ip);
            },
            None => { None }
        };
    }

    fn dns(&self) -> Vec<Ipv4Addr> {
        let content = self.get_settings_section().unwrap_or(String::new());
        let re = Regex::new(r"DNS=(?P<address>[\d.]+)").unwrap();

        let dns_servers = re.captures_iter(&content[..]).filter_map(|cap| {

            match ip_from_match(cap.name("address"),Some("0")) {
                Some(ip_struct) => Some(ip_struct.ip),
                None => None
            }

        }).collect::<Vec<Ipv4Addr>>();

        dns_servers
    }

    fn enable_dhcp(&self) -> Result<(), io::Error>  {
        let mut conf = Ini::load_from_file(self.path.to_str().expect("Could to parse path")).unwrap();

        conf.section_mut(Some("Network")).unwrap().insert("DHCP".into(), "both".into());
        conf.delete_from(Some("Network".to_owned()), "Address");
        conf.delete_from(Some("Network".to_owned()), "Gateway");

        conf.write_to_file(self.path.to_str().expect("Could to parse path"))
    }

    fn static_ip(&self, address: &str, gateway: &str) -> Result<(), StaticIPError>  {
        let re_ip_with_cidr = Regex::new(r"^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(?:/\b(?P<network>(?:3[0-2]|[12][0-9]|[1-9]))\b)$").unwrap();
        let re_ip = Regex::new(r"^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])$").unwrap();

        if !(re_ip_with_cidr.is_match(address) && re_ip.is_match(gateway)) {
            return Err(StaticIPError::Validation("Validation failed"));
        }

        //FIXME: Validate the inputs
        let mut conf = Ini::load_from_file(self.path.to_str().expect("Could to parse path")).unwrap();

        conf.section_mut(Some("Network")).unwrap().insert("DHCP".into(), "no".into());
        conf.section_mut(Some("Network")).unwrap().insert("Address".into(), address.into());
        conf.section_mut(Some("Network")).unwrap().insert("Gateway".into(), gateway.into());

        match conf.write_to_file(self.path.to_str().expect("Could to parse path")) {
            Ok(()) => return Ok(()),
            Err(err) => return Err(StaticIPError::Io(err))
        }
    }

}

struct UbuntuUpstartNetworkConfig<'a> {
    path: &'a Path
}

impl<'a> NetworkConfigLoader for UbuntuUpstartNetworkConfig<'a> {

    fn new(path: &'static str) -> UbuntuUpstartNetworkConfig {
        let default_path = "/etc/network/interfaces";

        if path == "" {
            return UbuntuUpstartNetworkConfig { path: Path::new(default_path) }
        }

        UbuntuUpstartNetworkConfig { path: Path::new(path) }
    }

    fn is_dhcp(&self) -> bool {
        unreachable!("Not implemented yet");
    }

    fn ip_address(&self) -> Option<IPv4Network> {
        unreachable!("Not implemented yet");
    }

    fn gateway(&self) -> Option<Ipv4Addr> {
        unreachable!("Not implemented yet");
    }

    fn get_settings_section(&self) -> Option<String> {
        unreachable!("Not implemented yet");
    }

    fn dns(&self) -> Vec<Ipv4Addr> {
       unreachable!("Not implemented yet");
    }

    fn enable_dhcp(&self) -> Result<(), io::Error>{
        unreachable!("Not implemented yet");
    }

    fn static_ip(&self, address: &str, gateway: &str) -> Result<(), StaticIPError> {
        unreachable!("Not implemented yet");
    }

}

enum NetworkConfig<'a> {
    Systemd(SystemdNetworkConfig<'a>),
    Ubuntu(UbuntuUpstartNetworkConfig<'a>)
}

#[test]
fn default_config_systemd() {
    let config = SystemdNetworkConfig::new("");
    assert!(config.path.to_str().unwrap() == "/etc/systemd/network/eth0.network");
}

#[test]
fn default_config_ubuntu_upstart() {
    let config = UbuntuUpstartNetworkConfig::new("");
    assert!(config.path.to_str().unwrap() == "/etc/network/interfaces");
}

#[test]
fn config_section_systemd() {
    let config = SystemdNetworkConfig::new("./tests/eth0.network.dhcp");
    let content = config.get_settings_section().unwrap_or(String::new());
    assert!(content.contains("[Network]"));
}

#[test]
fn no_config_section_systemd() {
    let config = SystemdNetworkConfig::new("./tests/eth0.network.nonetwork");
    let content = config.get_settings_section().unwrap_or(String::new());
    assert!(!content.contains("[Network]"));
}

#[test]
fn read_dhcp_config_systemd() {
    let config = SystemdNetworkConfig::new("./tests/eth0.network.dhcp");
    assert!(config.is_dhcp() == true);
}

#[test]
fn read_non_dhcp_config_systemd() {
    let config = SystemdNetworkConfig::new("./tests/eth0.network.static");
    assert!(config.is_dhcp() == false);
}

#[test]
fn read_static_config_systemd() {
    let config = SystemdNetworkConfig::new("./tests/eth0.network.static");
    let comparitor_ip = Ipv4Addr::from_str("192.168.1.3").unwrap();
    let ip_address = config.ip_address().expect("No ip address found");
    assert!(ip_address.ip == comparitor_ip);
    assert!(ip_address.prefix == 24);

    let comparitor_gw = Ipv4Addr::from_str("192.168.1.1").unwrap();
    assert!(config.gateway().expect("No gateway address found") == comparitor_gw);
}

#[test]
fn read_dns_config_systemd() {
    let config = SystemdNetworkConfig::new("./tests/eth0.network.static_with_dns");
    let dns_servers = config.dns();
    assert!(dns_servers.len() == 4);
}

#[test]
fn read_no_dns_config_systemd() {
    let config = SystemdNetworkConfig::new("./tests/eth0.network.static");
    let dns_servers = config.dns();
    assert!(dns_servers.len() == 0);
}

#[test]
fn enable_dhcp_systemd() {
    fs::copy("./tests/eth0.network.static", "./tests/eth0.network.static.test").unwrap();

    let config = SystemdNetworkConfig::new("./tests/eth0.network.static.test");
    assert!(config.is_dhcp() == false);
    config.enable_dhcp().unwrap();
    assert!(config.is_dhcp() == true);
}

#[test]
fn static_ip_systemd() {
    fs::copy("./tests/eth0.network.dhcp", "./tests/eth0.network.dhcp.test").unwrap();

    let config = SystemdNetworkConfig::new("./tests/eth0.network.dhcp.test");
    assert!(config.is_dhcp() == true);
    config.static_ip("192.168.1.3/24","192.168.1.1").unwrap();
    assert!(config.is_dhcp() == false);

    let comparitor_ip = Ipv4Addr::from_str("192.168.1.3").unwrap();
    let ip_address = config.ip_address().expect("No ip address found");
    assert!(ip_address.ip == comparitor_ip);
    assert!(ip_address.prefix == 24);

    let comparitor_gw = Ipv4Addr::from_str("192.168.1.1").unwrap();
    assert!(config.gateway().expect("No gateway address found") == comparitor_gw);
}

#[test]
fn static_ip_invalid_values_systemd() {
    fs::copy("./tests/eth0.network.dhcp", "./tests/eth0.network.dhcp.validation").unwrap();

    let config = SystemdNetworkConfig::new("./tests/eth0.network.dhcp.validation");
    assert!(config.is_dhcp() == true);
    assert!(config.static_ip("192.broken.1.3/24","192.168.1.1").is_err());
    assert!(config.static_ip("192.300.1.3/24","192.168.1.1").is_err());
    assert!(config.static_ip("192.300.1.3/24","192.168.1000.1").is_err());
    assert!(config.static_ip("192.300.1.3/33","192.168.1.1").is_err());
    assert!(config.static_ip("192.168.1.3/33","192.168.1.1").is_err());
    assert!(config.static_ip("192.168.1.3","192.168.1.1").is_err());
    assert!(config.static_ip("192.168.1.3/31","192.168.1.1").is_ok());

}