Leosac  0.8.0
Open Source Access Control
networkconfig.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
26 #include "networkconfig.hpp"
27 #include "core/kernel.hpp"
28 #include "tools/log.hpp"
30 #include <boost/property_tree/ptree_fwd.hpp>
31 
32 using namespace Leosac::Tools;
33 using namespace Leosac;
34 
35 NetworkConfig::NetworkConfig(const Kernel &k, const boost::property_tree::ptree &cfg)
36  : config_(cfg)
37  , _enabled(false)
38  , _dhcpEnabled(false)
39  , kernel_(k)
40 {
41  _enabled = cfg.get<bool>("enabled", false);
42 
43  if (_enabled)
44  {
45  _interface = cfg.get<std::string>("interface");
46  _dhcpEnabled = cfg.get<bool>("dhcp");
47  _netmask = cfg.get<std::string>("netmask");
48  _defaultIp = cfg.get<std::string>("default_ip");
49  _ip = cfg.get<std::string>("ip", _defaultIp);
50  _gateway = cfg.get<std::string>("gateway");
51 
52  INFO("Network settings:" << std::endl
53  << '\t' << "enabled=" << _enabled << std::endl
54  << '\t' << "dhcp=" << _dhcpEnabled << std::endl
55  << '\t' << "ip=" << _ip << std::endl
56  << '\t' << "netmask=" << _netmask << std::endl
57  << '\t' << "gateway=" << _gateway << std::endl
58  << '\t' << "default=" << _defaultIp);
59  }
60  else
61  INFO("Network configuration disabled. Will not touch system setting.");
62 }
63 
65 {
66  UnixShellScript builder(kernel_.script_directory() + "build_ipconfig.sh");
67  UnixShellScript apply(kernel_.script_directory() + "load_ipconfig.sh");
68 
69  if (!_enabled)
70  return;
71 
72  builder.run(UnixShellScript::toCmdLine(_dhcpEnabled, NetCfgFile, _interface, _ip,
73  _netmask, _gateway, "1&>/dev/null"));
74  if (!builder.getOutput().empty())
75  throw(ScriptException(builder.getOutput()));
76 
77  apply.run(UnixShellScript::toCmdLine(NetCfgFile, _interface, "1&>/dev/null"));
78  if (!apply.getOutput().empty())
79  INFO("ScriptOutput:\n" << apply.getOutput() << "\n");
80  INFO("JUST LOADED IFCONFIG CONFIGURATION");
81 }
82 
83 void NetworkConfig::setEnabled(bool state)
84 {
85  _enabled = state;
86 }
87 
88 void NetworkConfig::setDHCP(bool enabled)
89 {
90  if (_dhcpEnabled != enabled)
91  {
92  INFO("DHCP set to" << enabled);
93  _dhcpEnabled = enabled;
94  reload();
95  }
96 }
97 
98 void NetworkConfig::setCustomIP(bool enabled)
99 {
100  INFO("CustomIP set to" << enabled);
101  _ip = _defaultIp;
102 }
unixshellscript.hpp
UnixShellScript class declaration.
Leosac::NetworkConfig::reload
void reload()
Definition: networkconfig.cpp:64
Leosac::NetworkConfig::_ip
std::string _ip
Definition: networkconfig.hpp:72
Leosac::NetworkConfig::_netmask
std::string _netmask
Definition: networkconfig.hpp:71
INFO
@ INFO
Definition: log.hpp:34
Leosac::NetworkConfig::setDHCP
void setDHCP(bool enabled)
Definition: networkconfig.cpp:88
Leosac::Tools::UnixShellScript::run
int run(const std::string &args=std::string())
Definition: unixshellscript.cpp:46
Leosac::NetworkConfig::setEnabled
void setEnabled(bool state)
Definition: networkconfig.cpp:83
Leosac::NetworkConfig::_enabled
bool _enabled
Definition: networkconfig.hpp:68
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
kernel.hpp
networkconfig.hpp
NetworkConfig class declaration.
Leosac::NetworkConfig::NetCfgFile
static constexpr const char * NetCfgFile
Definition: networkconfig.hpp:42
Leosac::NetworkConfig::setCustomIP
void setCustomIP(bool enabled)
Definition: networkconfig.cpp:98
Leosac::Kernel
Core of Leosac.
Definition: kernel.hpp:73
Leosac::NetworkConfig::NetworkConfig
NetworkConfig(const Kernel &k, const boost::property_tree::ptree &cfg)
Definition: networkconfig.cpp:35
Leosac::NetworkConfig::_defaultIp
std::string _defaultIp
Definition: networkconfig.hpp:73
Leosac::Tools::UnixShellScript::getOutput
const std::string & getOutput() const
Definition: unixshellscript.cpp:66
Leosac::Kernel::script_directory
std::string script_directory() const
Return the path to the scripts directory.
Definition: kernel.cpp:354
Leosac::NetworkConfig::_dhcpEnabled
bool _dhcpEnabled
Definition: networkconfig.hpp:70
log.hpp
Leosac::NetworkConfig::_gateway
std::string _gateway
Definition: networkconfig.hpp:74
Leosac::Tools::UnixShellScript
Definition: unixshellscript.hpp:36
Leosac::Tools
Definition: DatabaseLogSink.hpp:27
ScriptException
Definition: scriptexception.hpp:33
Leosac::NetworkConfig::kernel_
const Kernel & kernel_
Definition: networkconfig.hpp:76
Leosac::NetworkConfig::_interface
std::string _interface
Definition: networkconfig.hpp:69