* added part of netmask change feature; Lars please add a regex

* gateway change is in work..
* dhcp is stalled
This commit is contained in:
age 2007-01-30 03:30:07 +00:00
parent 6359da105a
commit 0cdf82b7aa
4 changed files with 181 additions and 68 deletions

View File

@ -7,10 +7,17 @@
<legend><?cs var:html_escape(Lang.Plugins.network.Title.IP) ?></legend>
<?cs call:print_form_header("static", "network") ?>
<?cs call:show_help(Lang.Plugins.network.Help.Network) ?>
<table>
<tr><td>
<?cs var:html_escape(Lang.Plugins.network.Text.IP) ?>:
</td><td>
<!-- maybe display the used network interface?
<tr><td>
<?cs var:html_escape(Lang.Plugins.network.Interface) ?>:
</td><td>
</td></tr>
-->
<input class="ipnum" type="text" tabindex="1" name="ip1" size="3" id="ip"
value="<?cs var:Data.Plugins.network.ip.oc1
?>" />.<input class="ipnum" type="text" tabindex="2" name="ip2" size="3"
@ -32,28 +39,46 @@
?>" />.<input class="ipnum" type="text" tabindex="4" name="nm4" size="3"
value="<?cs var:Data.Plugins.network.nm.oc4 ?>" />
</td></tr><tr><td>
<?cs var:html_escape(Lang.Plugins.network.Text.GW) ?>:
</td><td>
<input class="ipnum" type="text" tabindex="1" name="gw1" size="3" id="gw"
value="<?cs var:Data.Plugins.network.gw.oc1
?>" />.<input class="ipnum" type="text" tabindex="2" name="gw2" size="3"
value="<?cs var:Data.Plugins.network.gw.oc2
?>" />.<input class="ipnum" type="text" tabindex="3" name="gw3" size="3"
value="<?cs var:Data.Plugins.network.gw.oc3
?>" />.<input class="ipnum" type="text" tabindex="4" name="gw4" size="3"
value="<?cs var:Data.Plugins.network.gw.oc4 ?>" />
</td></tr>
</table>
<?cs call:show_help(Lang.Plugins.network.Help.DHCP) ?>
<p>
<input type="checkbox" name="dhcp" value="1" id="dhcp" /><label for="dhcp"><?cs var:html_escape(Lang.Plugins.network.Text.DHCP) ?></label>
</p>
<p>
<input type="hidden" name="store" value="set_network" />
<input type="hidden" name="store" value="set_ip" />
<button type="submit"><?cs var:html_escape(Lang.Plugins.network.Button.Network) ?></button>
</p>
</form>
</fieldset>
<fieldset>
<legend><?cs var:html_escape(Lang.Plugins.network.Title.GW) ?></legend>
<?cs call:print_form_header("static", "network") ?>
<?cs call:show_help(Lang.Plugins.network.Help.Gateway) ?>
<table>
<tr><td>
<?cs var:html_escape(Lang.Plugins.network.Text.GW) ?>:
</td><td>
<input class="ipnum" type="text" tabindex="1" name="ip1" size="3" id="ip"
value="<?cs var:Data.Plugins.network.gw.oc1
?>" />.<input class="ipnum" type="text" tabindex="2" name="ip2" size="3"
value="<?cs var:Data.Plugins.network.gw.oc2
?>" />.<input class="ipnum" type="text" tabindex="3" name="ip3" size="3"
value="<?cs var:Data.Plugins.network.gw.oc3
?>" />.<input class="ipnum" type="text" tabindex="4" name="ip4" size="3"
value="<?cs var:Data.Plugins.network.gw.oc4 ?>" />
</td></tr>
</table>
<p>
<input type="hidden" name="store" value="set_gateway" />
<button type="submit"><?cs var:html_escape(Lang.Plugins.network.Button.Gateway) ?></button>
</p>
</form>
<!-- DHCP comes later
<?cs call:show_help(Lang.Plugins.network.Help.DHCP) ?>
<p>
<input type="checkbox" name="dhcp" value="1" id="dhcp" /><label for="dhcp"><?cs var:html_escape(Lang.Plugins.network.Text.DHCP) ?></label>
</p>
-->
</fieldset>

View File

@ -2,25 +2,45 @@ Name = Configure network
Link = Network
Title = Network settings
Title.IP = CryptoBox server
Title {
IP = CryptoBox server IP
GW = Gateway address
}
Text {
IP = IP address
NM = Network mask
GW = Default gateway
IP = Address
NM = Netmask
GW = Gateway
DHCP = Automatic configuration (Caution!)
}
Button.Network = Change network settings
Button {
Network = Change server address
Gateway = Change default gateway
}
Help {
Network = Insert the network address of the CryptoBox server, the network mask and the default gateway. Be aware that you may lose your connection to the server under some circumstances. The actual values are visible in the form fields.
Gateway = If you have a gateway in your LAN and want to make use of it, type it's address in here. It will be added as default route.
DHCP = If you enable automatic network configuration make sure you have a well configured DHCP server running. All values will be overridden by the settings from the DHCP server. If you don't know what DHCP means leave this box unchecked.
}
WarningMessage {
InvalidIP {
Title = Invalid value
Text = An invalid network address (IP) was supplied. Please try again.
Failure {
Title = Failure
Text = The address could not be changed.
}
InvalidServerIP {
Title = Invalid value
Text = The server address is not a valid number.
}
InvalidNetmaskIP {
Title = Invalid value
Text = The netmask address is not a valid number.
}
InvalidGatewayIP {
Title = Invalid value
Text = The gateway address is not a valid number.
}
}
@ -29,6 +49,10 @@ SuccessMessage {
Title = Network address changed
Text = The network address has been changed. In a few seconds you will get redirected to the new address.
}
GWChanged {
Title = Gateway address changed
Text = The gateway was added to your default route.
}
}
EnvironmentWarning {

View File

@ -48,7 +48,7 @@ class network(cryptobox.plugins.base.CryptoBoxPlugin):
request_auth = True
rank = 30
def do_action(self, store=None, redirected="", ip1="", ip2="", ip3="", ip4="", gw1="", gw2="", gw3="", gw4=""):
def do_action(self, store=None, redirected="", ip1="", ip2="", ip3="", ip4="", nm1="", nm2="", nm3="", nm4=""):
"""Show a form containing the current IP - change it if requested.
"""
## if we were redirected, then we should display the default page
@ -59,24 +59,25 @@ class network(cryptobox.plugins.base.CryptoBoxPlugin):
## check possible actions
if store is None:
## no action was requested -> just show the form
self.cbox.log.debug("network plugin: show form")
self.cbox.log.debug("network plugin: show form (interface %s)" % self.__get_interface())
self.__prepare_form_data()
return "form_network"
## change of ip address and/or netmask requested
elif store == "set_ip":
## change ip address
self.cbox.log.debug("network plugin: changing IP")
try:
for ip_in in (ip1, ip2, ip3, ip4):
if (int(ip_in) < 0) or (int(ip_in) > 255):
self.cbox.log.info("invalid CryptoBox IP supplied: %s" % \
str((ip1, ip2, ip3, ip4)))
raise ValueError
self.cbox.log.debug("network plugin: changing server IP")
if self.__IP_is_valid(ip1, ip2, ip3, ip4):
new_ip = "%d.%d.%d.%d" % (int(ip1), int(ip2), int(ip3), int(ip4))
except ValueError:
self.hdf["Data.Warning"] = "Plugins.network.InvalidIP"
else:
self.hdf["Data.Warning"] = "Plugins.network.InvalidServerIP"
self.__prepare_form_data()
return "form_network"
if self.__set_ip(new_ip):
if self.__IP_is_valid(nm1, nm2, nm3, nm4):
new_nm = "%d.%d.%d.%d" % (int(nm1), int(nm2), int(nm3), int(nm4))
else:
self.hdf["Data.Warning"] = "Plugins.network.InvalidNetmaskIP"
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.hdf["Data.Success"] = "Plugins.network.IPChanged"
self.hdf["Data.Redirect.URL"] = self.__get_redirect_destination(new_ip)
@ -90,13 +91,26 @@ class network(cryptobox.plugins.base.CryptoBoxPlugin):
return "empty"
else:
self.cbox.log.warn("failed to change IP address to: %s" % new_ip)
self.hdf["Data.Warning"] = "Plugins.network.InvalidIP"
self.hdf["Data.Warning"] = "Plugins.network.Failure"
self.__prepare_form_data()
return "form_network"
## request for default gateway change
elif store == "set_gateway":
if self.__IP_is_valid(ip1, ip2, ip3, ip4):
new_ip = "%d.%d.%d.%d" % (int(ip1), int(ip2), int(ip3), int(ip4))
else:
self.hdf["Data.Warning"] = "Plugins.network.InvalidGatewayIP"
self.__prepare_form_data()
return "form_network"
if self.__set_gw(new_ip):
self.cbox.log.info("the gateway IP was successfully changed: %s" % new_ip)
self.hdf["Data.Success"] = "Plugins.network.GWChanged"
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"
## request for default gateway change?
elif store == "set_gw":
## TODO: route add default gw ...
pass
else:
## invalid action was requested -> show default form
self.cbox.log.debug("network plugin: invalid request (%s)" % str(store))
@ -146,14 +160,18 @@ class network(cryptobox.plugins.base.CryptoBoxPlugin):
def __prepare_form_data(self):
"""Set some hdf values.
"""
(oc1, oc2, oc3, oc4) = self.__get_current_ip()
(oc1, oc2, oc3, oc4) = self.__get_current_ip("ip")
self.hdf[self.hdf_prefix + "ip.oc1"] = oc1
self.hdf[self.hdf_prefix + "ip.oc2"] = oc2
self.hdf[self.hdf_prefix + "ip.oc3"] = oc3
self.hdf[self.hdf_prefix + "ip.oc4"] = oc4
(oc1, oc2, oc3, oc4) = self.__get_current_ip("nm")
self.hdf[self.hdf_prefix + "nm.oc1"] = oc1
self.hdf[self.hdf_prefix + "nm.oc2"] = oc2
self.hdf[self.hdf_prefix + "nm.oc3"] = oc3
self.hdf[self.hdf_prefix + "nm.oc4"] = oc4
def __get_current_ip(self):
def __get_current_ip(self, type="ip"):
"""Retrieve the current IP.
"""
import re
@ -168,16 +186,22 @@ class network(cryptobox.plugins.base.CryptoBoxPlugin):
if proc.returncode != 0:
return (0, 0, 0, 0)
## this regex matches the four numbers of the IP
match = re.search(r'inet [\w]+:(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\s', stdout)
if match:
## use the previously matched numbers
return tuple([int(e) for e in match.groups()])
else:
return (0, 0, 0, 0)
if type == "ip":
match = re.search(r'inet [\w]+:(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\s', stdout)
if match:
## use the previously matched numbers
return tuple([int(e) for e in match.groups()])
else:
return (0, 0, 0, 0)
elif type == "nm":
##TODO: Lars please leave a regex note for fetching the
## netmask with ifconfig
#match = re.search(r'inet [\w]+:(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\s', stdout)
return ("L", "A", "3", "S")
def __set_ip(self, new_ip):
"""Change the IP.
def __set_ip(self, new_ip, new_nm="0"):
"""Change the IP, additionally a netmask can be applied
"""
import threading
## call the root_action script after some seconds - so we can deliver the page before
@ -196,7 +220,8 @@ class network(cryptobox.plugins.base.CryptoBoxPlugin):
os.path.join(self.plugin_dir, "root_action.py"),
"change_ip",
self.__get_interface(),
new_ip])
new_ip,
new_nm])
proc.wait()
if proc.returncode != 0:
self.cbox.log.warn("failed to change IP address: %s" % new_ip)
@ -210,6 +235,14 @@ class network(cryptobox.plugins.base.CryptoBoxPlugin):
return True
def __set_gw(self, new_ip):
"""Change the gateway IP adress
"""
## TODO: route add default gw ...
## as soon as lazy age has implemented this return True
return False
def __get_interface(self):
"""Return the name of the configured network interface
"""
@ -219,3 +252,18 @@ class network(cryptobox.plugins.base.CryptoBoxPlugin):
return DEFAULT_INTERFACE
def __IP_is_valid(self, ip1, ip2, ip3, ip4):
try:
for ip_in in (ip1, ip2, ip3, ip4):
if (int(ip_in) < 0) or (int(ip_in) > 255):
## we give an info only and a webwarning
## further reaction depends on the case
self.cbox.log.info("IP number is invalid: %s" % \
str((ip1, ip2, ip3, ip4)))
raise ValueError
except ValueError:
## handled by individual caller
#self.hdf["Data.Warning"] = "Plugins.network.InvalidIP"
return False
return True

View File

@ -34,21 +34,37 @@ import subprocess
import re
import sys
def __changeIP(interface, address):
def __changeIP(interface, ipaddress, netmask="0"):
__check_address(ipaddress)
if netmask == "0":
## change the IP only
proc = subprocess.Popen(
shell = False,
args = [IFCONFIG_BIN, interface, ipaddress])
proc.wait()
else:
## someone wants to change the netmask too
__check_address(netmask)
proc = subprocess.Popen(
shell = False,
args = [IFCONFIG_BIN, interface, ipaddress, "netmask", netmask])
proc.wait()
return proc.returncode == 0
def __changeGW(gwaddress):
##TODO
return False
def __check_address(address):
"""Check for correct numbers in given 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, address))
sys.exit(1)
proc = subprocess.Popen(
shell = False,
args = [IFCONFIG_BIN, interface, address])
proc.wait()
return proc.returncode == 0
def __changeGW(address):
return False
return
if __name__ == "__main__":
@ -62,8 +78,8 @@ if __name__ == "__main__":
try:
if args[0] == "change_ip":
if len(args) != 3: raise "InvalidArgNum"
result = __changeIP(args[1], args[2])
if len(args) != 4: raise "InvalidArgNum"
result = __changeIP(args[1], args[2], args[3])
elif args[0] == "change_gw":
if len(args) != 2: raise "InvalidArgNum"
result = __changeGW(args[1])