Leosac  0.7.0
OpenSourceAccessControl
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 }
std::string script_directory() const
Return the path to the scripts directory.
Definition: kernel.cpp:354
int run(const std::string &args=std::string())
This is the header file for a generated source file, GitSHA1.cpp.
Definition: log.hpp:37
NetworkConfig class declaration.
UnixShellScript class declaration.
static constexpr const char * NetCfgFile
void setEnabled(bool state)
Core of Leosac.
Definition: kernel.hpp:73
void setDHCP(bool enabled)
const Kernel & kernel_
void setCustomIP(bool enabled)
NetworkConfig(const Kernel &k, const boost::property_tree::ptree &cfg)