parent
77185b28a5
commit
afd9aadcaa
|
@ -26,18 +26,26 @@ Help {
|
||||||
}
|
}
|
||||||
|
|
||||||
WarningMessage {
|
WarningMessage {
|
||||||
Failure {
|
AddressChangeFailed {
|
||||||
Title = Failure
|
Title = Failure
|
||||||
Text = The address could not be changed.
|
Text = The address of the CryptoBox server could not be changed.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
GatewayChangeFailed {
|
||||||
|
Title = Failure
|
||||||
|
Text = The gateway of the CryptoBox server could not be set. Maybe the address you entered is unreachable?
|
||||||
|
}
|
||||||
|
|
||||||
InvalidServerIP {
|
InvalidServerIP {
|
||||||
Title = Invalid value
|
Title = Invalid value
|
||||||
Text = The server address is not valid.
|
Text = The server address is not valid.
|
||||||
}
|
}
|
||||||
|
|
||||||
InvalidNetmask {
|
InvalidNetmask {
|
||||||
Title = Invalid value
|
Title = Invalid value
|
||||||
Text = The netmask is not valid.
|
Text = The netmask is not valid.
|
||||||
}
|
}
|
||||||
|
|
||||||
InvalidGatewayIP {
|
InvalidGatewayIP {
|
||||||
Title = Invalid value
|
Title = Invalid value
|
||||||
Text = The gateway address is not valid.
|
Text = The gateway address is not valid.
|
||||||
|
|
|
@ -37,7 +37,7 @@ REDIRECT_DELAY = 10
|
||||||
CHANGE_IP_DELAY = 5
|
CHANGE_IP_DELAY = 5
|
||||||
## default network interface (may be overriden by the "interface" setting of the
|
## default network interface (may be overriden by the "interface" setting of the
|
||||||
## network plugin in cryptobox.conf
|
## network plugin in cryptobox.conf
|
||||||
DEFAULT_INTERFACE = "eth2"
|
DEFAULT_INTERFACE = "eth0"
|
||||||
|
|
||||||
class network(cryptobox.plugins.base.CryptoBoxPlugin):
|
class network(cryptobox.plugins.base.CryptoBoxPlugin):
|
||||||
"""The network feature of the CryptoBox.
|
"""The network feature of the CryptoBox.
|
||||||
|
@ -78,11 +78,12 @@ class network(cryptobox.plugins.base.CryptoBoxPlugin):
|
||||||
self.__prepare_form_data()
|
self.__prepare_form_data()
|
||||||
return "form_network"
|
return "form_network"
|
||||||
if self.__set_ip(new_ip, new_nm):
|
if self.__set_ip(new_ip, new_nm):
|
||||||
self.cbox.log.info("the IP was successfully changed: %s" % new_ip)
|
self.cbox.log.info("[network] the IP was successfully changed: %s" % new_ip)
|
||||||
self.hdf["Data.Success"] = "Plugins.network.IPChanged"
|
self.hdf["Data.Success"] = "Plugins.network.IPChanged"
|
||||||
self.hdf["Data.Redirect.URL"] = self.__get_redirect_destination(new_ip)
|
self.hdf["Data.Redirect.URL"] = self.__get_redirect_destination(new_ip)
|
||||||
self.hdf["Data.Redirect.Delay"] = REDIRECT_DELAY
|
self.hdf["Data.Redirect.Delay"] = REDIRECT_DELAY
|
||||||
self.prefs["_address"] = new_ip
|
self.prefs["_address"] = new_ip
|
||||||
|
self.prefs["_netmask"] = new_nm
|
||||||
try:
|
try:
|
||||||
self.cbox.prefs.plugin_conf.write()
|
self.cbox.prefs.plugin_conf.write()
|
||||||
except IOError:
|
except IOError:
|
||||||
|
@ -90,28 +91,37 @@ class network(cryptobox.plugins.base.CryptoBoxPlugin):
|
||||||
self.__prepare_form_data()
|
self.__prepare_form_data()
|
||||||
return "empty"
|
return "empty"
|
||||||
else:
|
else:
|
||||||
self.cbox.log.warn("Failed to change IP address to: %s" % new_ip)
|
self.cbox.log.warn("[network] failed to change IP address to: %s" % \
|
||||||
self.hdf["Data.Warning"] = "Plugins.network.Failure"
|
new_ip)
|
||||||
|
self.hdf["Data.Warning"] = "Plugins.network.AddressChangeFailed"
|
||||||
self.__prepare_form_data()
|
self.__prepare_form_data()
|
||||||
return "form_network"
|
return "form_network"
|
||||||
## request for default gateway change
|
## request for default gateway change
|
||||||
elif store == "set_gateway":
|
elif store == "set_gateway":
|
||||||
old_ip = __get_current_gw()
|
old_gw = self.__get_current_gw()
|
||||||
|
old_gw_str = ".".join([str(e) for e in old_gw])
|
||||||
if self.__IP_is_valid(ip1, ip2, ip3, ip4):
|
if self.__IP_is_valid(ip1, ip2, ip3, ip4):
|
||||||
new_ip = "%d.%d.%d.%d" % (int(ip1), int(ip2), int(ip3), int(ip4))
|
new_gw = (int(ip1), int(ip2), int(ip3), int(ip4))
|
||||||
|
new_gw_str = ".".join([str(e) for e in new_gw])
|
||||||
else:
|
else:
|
||||||
self.hdf["Data.Warning"] = "Plugins.network.InvalidGatewayIP"
|
self.hdf["Data.Warning"] = "Plugins.network.InvalidGatewayIP"
|
||||||
self.__prepare_form_data()
|
self.__prepare_form_data()
|
||||||
return "form_network"
|
return "form_network"
|
||||||
if self.__set_gw(old_ip, new_ip):
|
if self.__set_gw(old_gw_str, new_gw_str):
|
||||||
self.cbox.log.info("the gateway IP was successfully changed: %s" % new_ip)
|
self.cbox.log.info( "[network] successfully changed gateway address:" \
|
||||||
|
+ new_gw_str)
|
||||||
self.hdf["Data.Success"] = "Plugins.network.GWChanged"
|
self.hdf["Data.Success"] = "Plugins.network.GWChanged"
|
||||||
|
self.prefs["_gateway"] = new_gw_str
|
||||||
|
try:
|
||||||
|
self.cbox.prefs.plugin_conf.write()
|
||||||
|
except IOError:
|
||||||
|
self.cbox.log.warn("Could not write plugin configuration")
|
||||||
else:
|
else:
|
||||||
self.cbox.log.warn("Failed to change gateway address to: %s" % new_ip)
|
self.cbox.log.warn("[network] failed to change gateway address to: %s" \
|
||||||
self.hdf["Data.Warning"] = "Plugins.network.Failure"
|
% new_gw_str)
|
||||||
self.__prepare_form_data()
|
self.hdf["Data.Warning"] = "Plugins.network.GatewayChangeFailed"
|
||||||
return "form_network"
|
self.__prepare_form_data()
|
||||||
|
return "form_network"
|
||||||
else:
|
else:
|
||||||
## invalid action was requested -> show default form
|
## invalid action was requested -> show default form
|
||||||
self.cbox.log.debug("network plugin: invalid request (%s)" % str(store))
|
self.cbox.log.debug("network plugin: invalid request (%s)" % str(store))
|
||||||
|
@ -132,7 +142,14 @@ class network(cryptobox.plugins.base.CryptoBoxPlugin):
|
||||||
"""
|
"""
|
||||||
if event == "bootup":
|
if event == "bootup":
|
||||||
if "_address" in self.prefs:
|
if "_address" in self.prefs:
|
||||||
self.__set_ip(self.prefs["_address"])
|
if "_netmask" in self.prefs:
|
||||||
|
self.__set_ip(self.prefs["_address"], self.prefs["_netmask"])
|
||||||
|
else:
|
||||||
|
## no netmask setting stored
|
||||||
|
self.__set_ip(self.prefs["_address"])
|
||||||
|
if "_gateway" in self.prefs:
|
||||||
|
self.__set_gw(".".join([str(e) for e in self.__get_current_gw()]),
|
||||||
|
self.prefs["_gateway"])
|
||||||
|
|
||||||
|
|
||||||
def get_warnings(self):
|
def get_warnings(self):
|
||||||
|
@ -172,7 +189,7 @@ class network(cryptobox.plugins.base.CryptoBoxPlugin):
|
||||||
self.hdf[self.hdf_prefix + "nm.oc2"] = oc2
|
self.hdf[self.hdf_prefix + "nm.oc2"] = oc2
|
||||||
self.hdf[self.hdf_prefix + "nm.oc3"] = oc3
|
self.hdf[self.hdf_prefix + "nm.oc3"] = oc3
|
||||||
self.hdf[self.hdf_prefix + "nm.oc4"] = oc4
|
self.hdf[self.hdf_prefix + "nm.oc4"] = oc4
|
||||||
(oc1, oc2, oc3, oc4) = self.__get_current_ip("gw")
|
(oc1, oc2, oc3, oc4) = self.__get_current_gw()
|
||||||
self.hdf[self.hdf_prefix + "gw.oc1"] = oc1
|
self.hdf[self.hdf_prefix + "gw.oc1"] = oc1
|
||||||
self.hdf[self.hdf_prefix + "gw.oc2"] = oc2
|
self.hdf[self.hdf_prefix + "gw.oc2"] = oc2
|
||||||
self.hdf[self.hdf_prefix + "gw.oc3"] = oc3
|
self.hdf[self.hdf_prefix + "gw.oc3"] = oc3
|
||||||
|
@ -209,24 +226,41 @@ class network(cryptobox.plugins.base.CryptoBoxPlugin):
|
||||||
return tuple([int(e) for e in match.groups()])
|
return tuple([int(e) for e in match.groups()])
|
||||||
else:
|
else:
|
||||||
return (0, 0, 0, 0)
|
return (0, 0, 0, 0)
|
||||||
elif type == "gw":
|
|
||||||
gw = self.__get_current_gw()
|
|
||||||
return gw
|
|
||||||
|
|
||||||
def __get_current_gw(self):
|
def __get_current_gw(self):
|
||||||
proc = subprocess.Popen(
|
proc = subprocess.Popen(
|
||||||
shell = False,
|
shell = False,
|
||||||
stdout = subprocess.PIPE,
|
stdout = subprocess.PIPE,
|
||||||
|
stderr = subprocess.PIPE,
|
||||||
args = [
|
args = [
|
||||||
self.root_action.ROUTE_BIN,
|
self.root_action.ROUTE_BIN,
|
||||||
"-n"])
|
"-n"])
|
||||||
(stdout, stderr) = proc.communicate()
|
(stdout, stderr) = proc.communicate()
|
||||||
if proc.returncode != 0:
|
if proc.returncode != 0:
|
||||||
self.cbox.log.warn(stderr)
|
self.cbox.log.warn(
|
||||||
|
"[network] failed to retrieve gateway address: %s" % stdout)
|
||||||
return (0, 0, 0, 0)
|
return (0, 0, 0, 0)
|
||||||
#TODO: Lars, please do some regex voodoo here
|
current_interface = self.__get_interface()
|
||||||
gwIP = "192.168.0.123"
|
## skip the first two heading lines
|
||||||
return gwIP
|
for line in stdout.splitlines()[2:]:
|
||||||
|
attrs = line.split()
|
||||||
|
if len(attrs) != 8:
|
||||||
|
self.cbox.log.info("[network] misformed route entry: %s" % line)
|
||||||
|
continue
|
||||||
|
interface = attrs[7]
|
||||||
|
netmask = attrs[2]
|
||||||
|
gateway = attrs[1]
|
||||||
|
destination = attrs[0]
|
||||||
|
if (destination == "0.0.0.0") and (netmask == "0.0.0.0") and \
|
||||||
|
(interface == current_interface):
|
||||||
|
gw_octet = tuple(gateway.split("."))
|
||||||
|
if len(gw_octet) != 4:
|
||||||
|
self.cbox.log.info(
|
||||||
|
"[network] ignored invalid gateway setting: %s" % gateway)
|
||||||
|
else:
|
||||||
|
return gw_octet
|
||||||
|
return (0, 0, 0, 0)
|
||||||
|
|
||||||
|
|
||||||
def __set_ip(self, new_ip, new_nm="255.255.255.0"):
|
def __set_ip(self, new_ip, new_nm="255.255.255.0"):
|
||||||
|
@ -267,32 +301,24 @@ class network(cryptobox.plugins.base.CryptoBoxPlugin):
|
||||||
def __set_gw(self, old_ip, new_ip):
|
def __set_gw(self, old_ip, new_ip):
|
||||||
"""Change the gateway IP adress
|
"""Change the gateway IP adress
|
||||||
"""
|
"""
|
||||||
import threading
|
proc = subprocess.Popen(
|
||||||
def delayed_ip_change():
|
shell = False,
|
||||||
import time
|
stdout = subprocess.PIPE,
|
||||||
time.sleep(CHANGE_IP_DELAY)
|
stderr = subprocess.PIPE,
|
||||||
proc = subprocess.Popen(
|
args = [
|
||||||
shell = False,
|
self.cbox.prefs["Programs"]["super"],
|
||||||
stderr = subprocess.PIPE,
|
self.cbox.prefs["Programs"]["CryptoBoxRootActions"],
|
||||||
args = [
|
"plugin",
|
||||||
self.cbox.prefs["Programs"]["super"],
|
os.path.join(self.plugin_dir, "root_action.py"),
|
||||||
self.cbox.prefs["Programs"]["CryptoBoxRootActions"],
|
"change_gw",
|
||||||
"plugin",
|
old_ip,
|
||||||
os.path.join(self.plugin_dir, "root_action.py"),
|
new_ip])
|
||||||
"change_gw",
|
(output, error) = proc.communicate()
|
||||||
old_ip,
|
if proc.returncode != 0:
|
||||||
new_ip])
|
self.cbox.log.warn("[network] gateway setting failed: %s" % str(error))
|
||||||
proc.wait()
|
return False
|
||||||
if proc.returncode != 0:
|
else:
|
||||||
self.cbox.log.warn("failed to change IP address: %s" % new_ip)
|
return True
|
||||||
self.cbox.log.warn("error output: %s" % str(proc.stderr.read()))
|
|
||||||
return
|
|
||||||
thread = threading.Thread()
|
|
||||||
thread.run = delayed_ip_change
|
|
||||||
thread.setDaemon(True)
|
|
||||||
thread.start()
|
|
||||||
# TODO: how could we guess, if it failed?
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
def __get_interface(self):
|
def __get_interface(self):
|
||||||
|
|
|
@ -52,23 +52,23 @@ def __changeIP(interface, ipaddress, netmask="0"):
|
||||||
return proc.returncode == 0
|
return proc.returncode == 0
|
||||||
|
|
||||||
|
|
||||||
def __changeGW(old_gw, gwaddress):
|
def __changeGW(old_gw, new_gw):
|
||||||
__check_address(old_gw)
|
__check_address(old_gw)
|
||||||
__check_address(gwaddress)
|
__check_address(new_gw)
|
||||||
if old_gw != "0.0.0.0":
|
if old_gw != "0.0.0.0":
|
||||||
"""assuming that a default route exists and deleting it
|
## assume that a default route exists and delete it
|
||||||
"""
|
|
||||||
proc = subprocess.Popen(
|
proc = subprocess.Popen(
|
||||||
shell = False,
|
shell = False,
|
||||||
args = [ROUTE_BIN, "del", "default", "gw", old_gw])
|
args = [ROUTE_BIN, "del", "default", "gw", old_gw])
|
||||||
proc.wait()
|
proc.wait()
|
||||||
|
## ignore errors
|
||||||
proc = subprocess.Popen(
|
proc = subprocess.Popen(
|
||||||
shell = False,
|
shell = False,
|
||||||
args = [ROUTE_BIN, "add", "default", "gw", gwaddress])
|
args = [ROUTE_BIN, "add", "default", "gw", new_gw])
|
||||||
proc.wait()
|
proc.wait()
|
||||||
return proc.returncode == 0
|
return proc.returncode == 0
|
||||||
|
|
||||||
|
|
||||||
def __check_address(address):
|
def __check_address(address):
|
||||||
"""Check for correct numbers in given address
|
"""Check for correct numbers in given address
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in a new issue