diff --git a/plugins/network/network.py b/plugins/network/network.py index 2fb1aef..090ee41 100644 --- a/plugins/network/network.py +++ b/plugins/network/network.py @@ -30,7 +30,10 @@ import cryptobox.plugins.base ## specify (in seconds), how long we should wait before redirecting and ip change REDIRECT_DELAY = 10 -CHANGE_IP_DELAY = 2 +CHANGE_IP_DELAY = 5 +## default network interface (may be overriden by the "interface" setting of the +## network plugin in cryptobox.conf +DEFAULT_INTERFACE = "eth0" class network(cryptobox.plugins.base.CryptoBoxPlugin): """The network feature of the CryptoBox. @@ -80,7 +83,7 @@ class network(cryptobox.plugins.base.CryptoBoxPlugin): except IOError: self.cbox.log.warn("Could not write plugin configuration") self.__prepare_form_data() - return "form_network" + return "empty" else: self.cbox.log.warn("failed to change IP address to: %s" % new_ip) self.hdf["Data.Warning"] = "Plugins.network.InvalidIP" @@ -149,7 +152,7 @@ class network(cryptobox.plugins.base.CryptoBoxPlugin): stdout = subprocess.PIPE, args = [ root_action_plug.IFCONFIG_BIN, - root_action_plug.IFACE]) + self.__get_interface()]) (stdout, stderr) = proc.communicate() if proc.returncode != 0: return (0, 0, 0, 0) @@ -180,8 +183,9 @@ class network(cryptobox.plugins.base.CryptoBoxPlugin): self.cbox.prefs["Programs"]["CryptoBoxRootActions"], "plugin", os.path.join(self.plugin_dir, "root_action.py"), - new_ip, - "change_ip"]) + "change_ip", + self.__get_interface(), + new_ip]) proc.wait() if proc.returncode != 0: self.cbox.log.warn("failed to change IP address: %s" % new_ip) @@ -193,4 +197,14 @@ class network(cryptobox.plugins.base.CryptoBoxPlugin): thread.start() # TODO: how could we guess, if it failed? return True + + + def __get_interface(self): + """Return the name of the configured network interface + """ + if "interface" in self.defaults: + return self.defaults["interface"] + else: + return DEFAULT_INTERFACE + diff --git a/plugins/network/root_action.py b/plugins/network/root_action.py index c6d6d1e..37be472 100755 --- a/plugins/network/root_action.py +++ b/plugins/network/root_action.py @@ -29,30 +29,26 @@ PLUGIN_TYPE = "cryptobox" IFCONFIG_BIN = "/sbin/ifconfig" GWCONFIG_BIN = "/sbin/route" -#TODO: put IFACE in config -IFACE = "eth1" import subprocess import re import sys -import os -def __changeIP(address): - match = re.search(r'^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$', args[0]) +def __changeIP(interface, address): + match = re.search(r'^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$', address) ## did we match? If yes, then: are there wrong values inside? if not match or [e for e in match.groups() if int(e) > 255]: - sys.stderr.write("%s: illegal argument (%s)\n" % (self_bin, args[0])) + sys.stderr.write("%s: illegal argument (%s)\n" % (self_bin, address)) sys.exit(1) - proc = subprocess.Popen( shell = False, - args = [IFCONFIG_BIN, IFACE, args[0]]) + args = [IFCONFIG_BIN, interface, address]) proc.wait() - sys.exit(proc.returncode) + return proc.returncode == 0 def __changeGW(address): - sys.exit(23) + return False if __name__ == "__main__": @@ -66,8 +62,8 @@ if __name__ == "__main__": try: if args[0] == "change_ip": - if len(args) != 2: raise "InvalidArgNum" - result = __changeIP(args[1]) + if len(args) != 3: raise "InvalidArgNum" + result = __changeIP(args[1], args[2]) elif args[0] == "change_gw": if len(args) != 2: raise "InvalidArgNum" result = __changeGW(args[1])