From 206abcab698395b6c61297782e8c304c4e77dc56 Mon Sep 17 00:00:00 2001 From: cajus Date: Thu, 2 Aug 2007 14:58:33 +0000 Subject: [PATCH] Updated dhcp handling git-svn-id: https://oss.gonicus.de/repositories/gosa/branches/2.5@6959 594d385d-05f5-0310-b6e9-bd551577e9d8 --- plugins/admin/systems/class_dhcpAdvanced.inc | 50 +++++++- plugins/admin/systems/class_dhcpHost.inc | 120 ++++++++++--------- plugins/admin/systems/class_dhcpNetwork.inc | 34 +----- plugins/admin/systems/class_servDHCP.inc | 27 ++++- plugins/admin/systems/dhcp_advanced.tpl | 17 ++- plugins/admin/systems/dhcphost.tpl | 10 -- 6 files changed, 155 insertions(+), 103 deletions(-) diff --git a/plugins/admin/systems/class_dhcpAdvanced.inc b/plugins/admin/systems/class_dhcpAdvanced.inc index e9a40c15b..271c4ed6e 100644 --- a/plugins/admin/systems/class_dhcpAdvanced.inc +++ b/plugins/admin/systems/class_dhcpAdvanced.inc @@ -23,6 +23,9 @@ class dhcpAdvanced extends plugin /* Used attributes */ var $options= array(); var $statements= array(); + var $show_advanced= FALSE; + var $autoStatements= array(); + var $autoOptions= array(); /* attribute list for save action */ var $attributes= array(); @@ -32,6 +35,8 @@ class dhcpAdvanced extends plugin { /* This is always an account */ $this->is_account= TRUE; + $this->setAutoStatements(); + $this->setAutoOptions(); } function execute() @@ -44,7 +49,11 @@ class dhcpAdvanced extends plugin } if (isset($_POST['delete_statement']) && isset($_POST['dhcpstatements'])){ $key= preg_replace('/([a-z0-9-]+)\s(.*)$/', '\\1', $_POST['dhcpstatements']); - unset($this->statements[$key]); + if (in_array($key, $this->autoStatements)){ + print_red(_("Can't delete automatic statements. Please use the fields above.")); + } else { + unset($this->statements[$key]); + } } if (isset($_POST['add_option']) && $_POST['addoption'] != ""){ $key= preg_replace('/^([a-z0-9-]+)\s(.*)$/', '\\1', $_POST['addoption']); @@ -53,7 +62,11 @@ class dhcpAdvanced extends plugin } if (isset($_POST['delete_option']) && isset($_POST['dhcpoptions'])){ $key= preg_replace('/([a-z0-9-]+)\s(.*)$/', '\\1', $_POST['dhcpoptions']); - unset($this->options[$key]); + if (in_array($key, $this->autoOptions)){ + print_red(_("Can't delete automatic options. Please use the fields above.")); + } else { + unset($this->options[$key]); + } } $smarty= get_smarty(); @@ -61,16 +74,25 @@ class dhcpAdvanced extends plugin /* Assign arrays */ $statements= array(); foreach ($this->statements as $key => $val){ - $statements[$key]= "$key $val"; + if (in_array($key, $this->autoStatements)){ + $statements[$key]= "$key $val ["._("automatic")."]"; + } else { + $statements[$key]= "$key $val"; + } } $smarty->assign("dhcpstatements", $statements); $options= array(); foreach ($this->options as $key => $val){ - $options[$key]= "$key $val"; + if (in_array($key, $this->autoOptions)){ + $options[$key]= "$key $val ["._("automatic")."]"; + } else { + $options[$key]= "$key $val"; + } } $smarty->assign("dhcpoptions", $options); /* Show main page */ + $smarty->assign("show_advanced", $this->show_advanced); return ($smarty->fetch (get_template_path('dhcp_advanced.tpl', TRUE))); } @@ -82,6 +104,12 @@ class dhcpAdvanced extends plugin /* Save data to object */ function save_object() { + if (isset($_POST['show_advanced'])){ + $this->show_advanced= TRUE; + } + if (isset($_POST['hide_advanced'])){ + $this->show_advanced= FALSE; + } } @@ -99,6 +127,20 @@ class dhcpAdvanced extends plugin { } + + function setAutoOptions($addopt= array()) + { + $options= array("routers", "domain-name", "subnet-mask", "broadcast-address"); + $this->autoOptions= array_merge($options, $addopt); + } + + + function setAutoStatements($addstat= array()) + { + $statements= array("filename", "next-server", "get-lease-hostnames", "use-host-decl-names"); + $this->autoStatements= array_merge($statements, $addstat); + } + } ?> diff --git a/plugins/admin/systems/class_dhcpHost.inc b/plugins/admin/systems/class_dhcpHost.inc index 2f6573856..2c710f986 100644 --- a/plugins/admin/systems/class_dhcpHost.inc +++ b/plugins/admin/systems/class_dhcpHost.inc @@ -22,10 +22,11 @@ class dhcpHost extends plugin { /* Used attributes */ var $cn= ""; + var $orig_cn= ""; var $dhcpHWAddress= ""; var $options= array(); var $statements= array(); - var $dn= "new"; + var $dn= ""; /* Subobjects */ var $network; @@ -38,8 +39,9 @@ class dhcpHost extends plugin function dhcpHost($attrs) { /* Load statements / options */ - if ($attrs != NULL){ - $this->dn= ""; + if (is_array($attrs)){ + $this->dn= $attrs['dn']; + $this->new= FALSE; /* Load attributes */ foreach (array("cn", "dhcpHWAddress") as $attr){ @@ -68,6 +70,10 @@ class dhcpHost extends plugin } } + } else { + /* We keep the parent dn here if it's new */ + $this->dn= $attrs; + $this->new= TRUE; } /* Load network module */ @@ -77,6 +83,10 @@ class dhcpHost extends plugin $this->advanced= new dhcpAdvanced(); $this->advanced->options= $this->options; $this->advanced->statements= $this->statements; + $this->advanced->setAutoStatements(array("fixed-address")); + + /* Save CN for later reference */ + $this->orig_cn= $this->cn; } function execute() @@ -98,9 +108,23 @@ class dhcpHost extends plugin $smarty->assign("hwtypes", array("ethernet" => _("Ethernet"), "fddi" => _("FDDI"), "token-ring" => _("Token Ring"))); - /* Show main page */ - return($smarty->fetch(get_template_path('dhcphost.tpl', TRUE)).$this->network->execute().$this->advanced->execute()); + $display= $smarty->fetch(get_template_path('dhcphost.tpl', TRUE)).$this->network->execute(); + + /* Merge arrays for advanced view */ + foreach (array("options", "statements") as $type){ + $tmp= array_merge($this->$type, $this->network->$type); + $this->advanced->$type= $tmp; + } + + $display.= $this->advanced->execute(); + + /* Add footer */ + $display.= "
". + " 
"; + + + return ($display); } @@ -128,11 +152,21 @@ class dhcpHost extends plugin unset ($this->statements['fixed-address']); } } + + /* Save sub-objects */ + $this->network->save_object(); + $this->advanced->save_object(); + + /* Merge arrays for advanced view */ + foreach (array("options", "statements") as $type){ + $tmp= array_merge($this->$type, $this->network->$type); + $this->advanced->$type= $tmp; + } } /* Check values */ - function check() + function check($cache) { $message= array(); @@ -142,10 +176,14 @@ class dhcpHost extends plugin } /* cn already used? */ - if ($this->dn != "new"){ - # Ask parents container object - echo "FIXME: Need to check container object!"; - $message[]= _("The name for this host section is already used!"); + if ($this->orig_cn != $this->cn || $this->new){ + + foreach($cache as $dn => $dummy){ + if (preg_match("/^cn=".$this->cn.",/", $dn)){ + $message[]= _("The name for this host section is already used!"); + break; + } + } } /* Check syntax of MAC address */ @@ -154,6 +192,11 @@ class dhcpHost extends plugin $message[]= _("The hardware address specified by you is not valid!"); } + /* Check external plugins */ + $net= $this->network->check(); + $adv= $this->advanced->check(); + $message= array_merge($message, $net, $adv); + return $message; } @@ -161,30 +204,17 @@ class dhcpHost extends plugin /* Save to LDAP */ function save() { - echo "FIXME: Need to implement modified save()!"; - return; - - # If this->dn == "new", create new entry - # If this->dn != "new", modify existing entry and place a "modifyied" inside to notify - # the parent to move/handle it. - # If CN has changed from former CN, empty out old entry and - # create a new one. - - #-------------------------------- Not yet modified from this line on ------------------------------- - - /* Generate new dn */ - if ($this->parent->parent != NULL){ - $this->dn= "cn=".$this->cn.",".$this->parent->parent; - } else { - $tmp= preg_replace('/^cn=[^,]+/', '', $this->dn); - $this->dn= "cn=".$this->cn.$tmp; + /* Merge arrays for network and advanced view */ + foreach (array("options", "statements") as $type){ + $tmp= array_merge($this->$type, $this->network->$type, $this->advanced->$type); + $this->$type= $tmp; } - /* Get ldap mode */ - if ($this->orig_dn != $this->dn){ - $mode= "add"; + /* Add cn if we're new */ + if ($this->new){ + $this->dn= "cn=".$this->cn.",".$this->dn; } else { - $mode= "modify"; + $this->dn= "cn=".$this->cn.preg_replace('/^cn=[^,]+/', '', $this->dn); } /* Assemble new entry - options */ @@ -193,10 +223,6 @@ class dhcpHost extends plugin foreach ($this->options as $key => $val){ $this->attrs['dhcpOption'][]= "$key $val"; } - } else { - if ($mode == "modify"){ - $this->attrs['dhcpOption']= array(); - } } /* Assemble new entry - statements */ @@ -205,28 +231,12 @@ class dhcpHost extends plugin foreach ($this->statements as $key => $val){ $this->attrs['dhcpStatements'][]= "$key $val"; } - } else { - if ($mode == "modify"){ - $this->attrs['dhcpStatements']= array(); - } } - /* Do LDAP action */ - $ldap= $this->config->get_ldap_link(); - if ($mode == "add"){ - $ldap->cd($this->config->current['BASE']); - $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn)); - } - $ldap->cd($this->dn); - $ldap->$mode($this->attrs); - show_ldap_error($ldap->get_error()); - - /* Name changed? Remove orig? */ - if ($this->orig_dn != $this->dn && $this->orig_dn != "new"){ - echo "Remove old
"; - $ldap->rmdir($this->orig_dn); - show_ldap_error($ldap->get_error()); - } + /* Move dn to the result */ + $this->attrs['dn']= $this->dn; + + return ($this->attrs); } } diff --git a/plugins/admin/systems/class_dhcpNetwork.inc b/plugins/admin/systems/class_dhcpNetwork.inc index c3d97de49..9cef322e7 100644 --- a/plugins/admin/systems/class_dhcpNetwork.inc +++ b/plugins/admin/systems/class_dhcpNetwork.inc @@ -154,7 +154,8 @@ class dhcpNetwork extends plugin */ /* Options */ - foreach (array("routers" => "routers", "domain-name" => "domain") as $key => $val){ + foreach (array("routers" => "routers", "domain-name" => "domain", "subnet-mask" => "subnet_mask", + "broadcast-address" => "broadcast_address") as $key => $val){ if ($_POST["$val"] == ''){ unset($this->options["$key"]); @@ -172,21 +173,6 @@ class dhcpNetwork extends plugin } } - /* Netmask / Broadcast */ - foreach (array("subnet-mask" => "nm", "broadcast-address" => "bc") - as $key => $val){ - - $tmp= ""; - for ($i= 0; $i<4; $i++){ - $tmp.= $_POST["$val$i"]."."; - } - if ($tmp == "...."){ - unset($this->options["$key"]); - } else { - $this->options["$key"]= preg_replace('/\.$/', '', $tmp); - } - } - /* Flags */ if (isset ($_POST['autohost'])){ $this->statements['get-lease-hostnames']= "true"; @@ -214,23 +200,11 @@ class dhcpNetwork extends plugin } $tmp= preg_replace('/^[^\s]+\s/', '', $this->options["$key"]); - list($n0,$n1,$n2,$n3)= split('\.', $tmp); - for ($i= 0; $i<4; $i++){ - $name= "n$i"; - if (preg_match('/^[0-9]+$/', $$name)){ - $val= (int)($$name); - if ($val < 0 || $val > 255){ - $message[]= sprintf(_("Error in definition of '%s'!"), $typ); - break; - } - } else { - $message[]= sprintf(_("Illegal characters in definition of '%s'!"), $typ); - break; - } + if (!is_ip($tmp)){ + $message[]= sprintf(_("Error in definition of '%s'!"), $typ); } } - #FIXME: There are some more things we could test -> valid netmask, range return $message; } diff --git a/plugins/admin/systems/class_servDHCP.inc b/plugins/admin/systems/class_servDHCP.inc index e0bd6988e..42dd962a3 100644 --- a/plugins/admin/systems/class_servDHCP.inc +++ b/plugins/admin/systems/class_servDHCP.inc @@ -34,17 +34,39 @@ class servdhcp extends plugin if (isset($_POST['create_section']) && isset($_POST['section'])){ $section= $_POST['section']; if (isset(dhcpNewSectionDialog::$sectionMap[$section])){ - $this->dialog= new $section(NULL); + $this->dialog= new $section($this->dn); + $this->current_object= ""; } else { $this->dialog= NULL; } } /* Cancel section creation? */ - if (isset($_POST['cancel_section'])){ + if (isset($_POST['cancel_section']) || isset($_POST['cancel_dhcp'])){ $this->dialog= NULL; } + /* Save changes */ + if (isset($_POST['save_dhcp'])){ + $this->dialog->save_object(); + $messages= $this->dialog->check($this->dhcpObjectCache); + if (count($messages)){ + show_errors($messages); + } else { + $dn= $this->dialog->dn; + $data= $this->dialog->save(); + if ($this->current_object= ""){ + echo "SAVE new entry"; + } else { + if ($dn != $data['dn']){ + echo "SAVE old entry with new dn"; + } else { + echo "SAVE old entry"; + } + } + } + } + /* Remove section? */ if (isset($_POST['delete_dhcp_confirm'])){ if (chkacl($this->acl, "delete") == ""){ @@ -82,6 +104,7 @@ class servdhcp extends plugin $dn= base64_decode(preg_replace('/^editDhcp_([^_]+)_x$/', '\1', $name)); if (isset($this->dhcpObjectCache[$dn])){ $section= $this->objectType($dn); + $this->current_object= $dn; $this->dialog= new $section($this->dhcpObjectCache[$dn]); } } diff --git a/plugins/admin/systems/dhcp_advanced.tpl b/plugins/admin/systems/dhcp_advanced.tpl index e155a4ca9..5ce0e4600 100644 --- a/plugins/admin/systems/dhcp_advanced.tpl +++ b/plugins/admin/systems/dhcp_advanced.tpl @@ -1,5 +1,12 @@ {* GOsa dhcp sharedNetwork - smarty template *} +

+
+ +{if $show_advanced} + + + @@ -7,7 +14,7 @@
{t}DHCP statements{/t}
- {html_options values=$dhcpstatements output=$dhcpstatements}
@@ -20,7 +27,7 @@
{t}DHCP options{/t}
- {html_options values=$dhcpoptions output=$dhcpoptions}
@@ -31,3 +38,9 @@
+{else} + + + +{/if} +

 

diff --git a/plugins/admin/systems/dhcphost.tpl b/plugins/admin/systems/dhcphost.tpl index dc1b1912a..a2a2915e0 100644 --- a/plugins/admin/systems/dhcphost.tpl +++ b/plugins/admin/systems/dhcphost.tpl @@ -43,16 +43,6 @@

- - - - - -
-Temporary escape: - -
-