gateway setting finished (Closes: #89)

minor language change
This commit is contained in:
lars 2007-02-07 08:19:55 +00:00
parent 77185b28a5
commit afd9aadcaa
3 changed files with 90 additions and 56 deletions

View file

@ -26,18 +26,26 @@ Help {
}
WarningMessage {
Failure {
AddressChangeFailed {
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 {
Title = Invalid value
Text = The server address is not valid.
}
InvalidNetmask {
Title = Invalid value
Text = The netmask is not valid.
}
InvalidGatewayIP {
Title = Invalid value
Text = The gateway address is not valid.

View file

@ -37,7 +37,7 @@ REDIRECT_DELAY = 10
CHANGE_IP_DELAY = 5
## default network interface (may be overriden by the "interface" setting of the
## network plugin in cryptobox.conf
DEFAULT_INTERFACE = "eth2"
DEFAULT_INTERFACE = "eth0"
class network(cryptobox.plugins.base.CryptoBoxPlugin):
"""The network feature of the CryptoBox.
@ -78,11 +78,12 @@ class network(cryptobox.plugins.base.CryptoBoxPlugin):
self.__prepare_form_data()
return "form_network"
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.Redirect.URL"] = self.__get_redirect_destination(new_ip)
self.hdf["Data.Redirect.Delay"] = REDIRECT_DELAY
self.prefs["_address"] = new_ip
self.prefs["_netmask"] = new_nm
try:
self.cbox.prefs.plugin_conf.write()
except IOError:
@ -90,28 +91,37 @@ class network(cryptobox.plugins.base.CryptoBoxPlugin):
self.__prepare_form_data()
return "empty"
else:
self.cbox.log.warn("Failed to change IP address to: %s" % new_ip)
self.hdf["Data.Warning"] = "Plugins.network.Failure"
self.cbox.log.warn("[network] failed to change IP address to: %s" % \
new_ip)
self.hdf["Data.Warning"] = "Plugins.network.AddressChangeFailed"
self.__prepare_form_data()
return "form_network"
## request for default gateway change
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):
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:
self.hdf["Data.Warning"] = "Plugins.network.InvalidGatewayIP"
self.__prepare_form_data()
return "form_network"
if self.__set_gw(old_ip, new_ip):
self.cbox.log.info("the gateway IP was successfully changed: %s" % new_ip)
if self.__set_gw(old_gw_str, new_gw_str):
self.cbox.log.info( "[network] successfully changed gateway address:" \
+ new_gw_str)
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:
self.cbox.log.warn("Failed to change gateway address to: %s" % new_ip)
self.hdf["Data.Warning"] = "Plugins.network.Failure"
self.__prepare_form_data()
return "form_network"
self.cbox.log.warn("[network] failed to change gateway address to: %s" \
% new_gw_str)
self.hdf["Data.Warning"] = "Plugins.network.GatewayChangeFailed"
self.__prepare_form_data()
return "form_network"
else:
## invalid action was requested -> show default form
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 "_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):
@ -172,7 +189,7 @@ class network(cryptobox.plugins.base.CryptoBoxPlugin):
self.hdf[self.hdf_prefix + "nm.oc2"] = oc2
self.hdf[self.hdf_prefix + "nm.oc3"] = oc3
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.oc2"] = oc2
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()])
else:
return (0, 0, 0, 0)
elif type == "gw":
gw = self.__get_current_gw()
return gw
def __get_current_gw(self):
proc = subprocess.Popen(
shell = False,
stdout = subprocess.PIPE,
stderr = subprocess.PIPE,
args = [
self.root_action.ROUTE_BIN,
"-n"])
(stdout, stderr) = proc.communicate()
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)
#TODO: Lars, please do some regex voodoo here
gwIP = "192.168.0.123"
return gwIP
current_interface = self.__get_interface()
## skip the first two heading lines
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"):
@ -267,32 +301,24 @@ class network(cryptobox.plugins.base.CryptoBoxPlugin):
def __set_gw(self, old_ip, new_ip):
"""Change the gateway IP adress
"""
import threading
def delayed_ip_change():
import time
time.sleep(CHANGE_IP_DELAY)
proc = subprocess.Popen(
shell = False,
stderr = subprocess.PIPE,
args = [
self.cbox.prefs["Programs"]["super"],
self.cbox.prefs["Programs"]["CryptoBoxRootActions"],
"plugin",
os.path.join(self.plugin_dir, "root_action.py"),
"change_gw",
old_ip,
new_ip])
proc.wait()
if proc.returncode != 0:
self.cbox.log.warn("failed to change IP address: %s" % new_ip)
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
proc = subprocess.Popen(
shell = False,
stdout = subprocess.PIPE,
stderr = subprocess.PIPE,
args = [
self.cbox.prefs["Programs"]["super"],
self.cbox.prefs["Programs"]["CryptoBoxRootActions"],
"plugin",
os.path.join(self.plugin_dir, "root_action.py"),
"change_gw",
old_ip,
new_ip])
(output, error) = proc.communicate()
if proc.returncode != 0:
self.cbox.log.warn("[network] gateway setting failed: %s" % str(error))
return False
else:
return True
def __get_interface(self):

View file

@ -52,23 +52,23 @@ def __changeIP(interface, ipaddress, netmask="0"):
return proc.returncode == 0
def __changeGW(old_gw, gwaddress):
def __changeGW(old_gw, new_gw):
__check_address(old_gw)
__check_address(gwaddress)
__check_address(new_gw)
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(
shell = False,
args = [ROUTE_BIN, "del", "default", "gw", old_gw])
proc.wait()
## ignore errors
proc = subprocess.Popen(
shell = False,
args = [ROUTE_BIN, "add", "default", "gw", gwaddress])
args = [ROUTE_BIN, "add", "default", "gw", new_gw])
proc.wait()
return proc.returncode == 0
def __check_address(address):
"""Check for correct numbers in given address
"""