1 <?php
3 class servdhcp extends plugin
4 {
5 /* attribute list for save action */
6 var $attributes= array("dhcpServiceDN");
7 var $objectclasses= array("dhcpServer");
9 var $dhcpServiceDN= "";
11 /* Section storage */
12 var $dhcpSections= array();
13 var $dhcpObjectCache= array();
14 var $current_object= "";
15 var $types= array();
16 var $serviceDN= "";
18 var $quote_option = array("domain-name");
20 var $orig_dn = "";
22 var $dhcp_server_list = array("ENTRIES"=> array(),"FOR_LIST"=> array());
23 var $take_over_id = -1;
24 var $display_warning = TRUE;
26 function servdhcp ($config, $dn= NULL, $parent= NULL)
27 {
28 plugin::plugin ($config, $dn, $parent);
30 $this->serviceDN = "cn=dhcp,".$dn;
31 $this->orig_dn = $dn;
33 $this->types= array( "dhcpLog" => _("Logging"),
34 "dhcpService" => _("Global options"),
35 "dhcpClass" => _("Class"),
36 "dhcpSubClass" => _("Subclass"),
37 "dhcpHost" => _("Host"),
38 "dhcpGroup" => _("Group"),
39 "dhcpPool" => _("Pool"),
40 "dhcpSubnet" => _("Subnet"),
41 "dhcpFailOverPeer" => _("Failover peer"),
42 "dhcpSharedNetwork" => _("Shared network"));
45 /* Backport: PHP4 compatibility */
46 foreach($this->types as $type => $translation){
47 $this->types[strtolower($type)] = $translation;
48 }
50 /* Load information about available services */
51 $this->reload();
52 if (!count($this->dhcpSections)){
53 $this->is_account= FALSE;
54 $this->dhcp_server_list = $this->get_list_of_dhcp_servers();
55 }
56 }
59 function get_list_of_dhcp_servers()
60 {
61 $ret = array("ENTRIES"=> array(),"FOR_LIST"=> array());
62 $ldap = $this->config->get_ldap_link();
63 $ldap->cd($this->config->current['BASE']);
64 $ldap->search("(&(objectClass=goServer)(dhcpServiceDN=*))",array("dn","cn","dhcpServiceDN"));
65 while($attrs = $ldap->fetch()){
67 /* Skip own config */
68 if($this->dn != "new" && preg_match("/".normalizePreg($this->dn)."$/",$attrs['dn'])){
69 continue;
70 }
72 $ret['ENTRIES'][] = $attrs;
73 }
74 foreach($ret['ENTRIES'] as $key => $data){
75 $ret['FOR_LIST'][$key] = $data['cn'][0];
76 }
77 return($ret);
78 }
81 function execute()
82 {
83 /* Call parent execute */
84 plugin::execute();
86 /* Fill templating stuff */
87 $smarty= get_smarty();
88 $smarty->assign("dns_take_over",FALSE);
89 $display= "";
92 /*****************/
93 /* Handle Take Over Actions
94 /*****************/
96 /* Give smarty the required informations */
97 $smarty->assign("dhcp_server_list", $this->dhcp_server_list['FOR_LIST']);
98 $smarty->assign("dhcp_server_list_cnt", count($this->dhcp_server_list['FOR_LIST']));
100 /* Take over requested, save id */
101 if(isset($_POST['take_over_src']) && isset($_POST['take_over'])){
102 $id = $_POST['take_over_src'];
103 if(isset($this->dhcp_server_list['ENTRIES'][$id])){
104 $this->take_over_id = $id;
105 }
106 }
108 /* Abort take over action */
109 if(isset($_POST['cancel_take_over'])){
110 $this->dialog =false;
111 $this->take_over_id = -1;
112 $this->dhcp_server_list = $this->get_list_of_dhcp_servers();
113 }
115 /* Display informartion about take over that will be started when saving this server
116 * and hide default dhcp output
117 */
118 if($this->take_over_id != -1){
119 $this->dialog = FALSE;
120 $id = $this->take_over_id;
121 $smarty->assign("dns_take_over",TRUE);
123 $warning = sprintf(_("You are going to migrate the DHCP setup from server '%s'."),$this->dhcp_server_list['ENTRIES'][$id]['cn'][0]);
124 $warning.= " "._("The migration will be started when you save this system. To cancel this action, use the cancel button below.");
126 if($this->display_warning){
127 print_red($warning);
128 $this->display_warning = FALSE;
129 }
130 return($smarty->fetch(get_template_path('servdhcp.tpl', TRUE)));
131 }
134 /*****************/
135 /* List handling
136 /*****************/
138 /* Section Creation? */
139 if (isset($_POST['create_section']) && isset($_POST['section'])){
140 $section= $_POST['section'];
141 $tmp = new dhcpNewSectionDialog(NULL);
142 if (isset($tmp->sectionMap[$section])){
143 $this->dialog= new $section($this->current_object);
144 $this->current_object= "";
145 } else {
146 $this->dialog= NULL;
147 }
148 }
150 /* Cancel section creation? */
151 if (isset($_POST['cancel_section']) || isset($_POST['cancel_dhcp'])){
152 $this->dialog= NULL;
153 }
155 /* Save changes */
156 if (isset($_POST['save_dhcp'])){
157 $this->dialog->save_object();
158 $messages= $this->dialog->check($this->dhcpObjectCache);
159 if (count($messages)){
160 show_errors($messages);
161 } else {
162 $dn= $this->dialog->dn;
163 $class= get_class($this->dialog);
164 $type= $this->types[$class];
165 if(empty($this->serviceDN)){
166 $indent= substr_count(preg_replace("/".$this->dn."/", '', $dn), ",") -1;
167 }else{
168 $indent= substr_count(preg_replace("/".$this->serviceDN."/", '', $dn), ",");
169 }
170 $spaces= "";
171 for ($i= 0; $i<$indent; $i++){
172 $spaces.= " ";
173 }
174 $data= $this->dialog->save();
175 if ($this->current_object == ""){
176 /* New object */
177 $newsects= array();
178 foreach ($this->dhcpSections as $key => $dsc){
179 $newsects[$key]= $dsc;
180 if ($key == $dn){
181 $spaces.= " ";
182 $newsects[$data['dn']]= "$spaces$type '".preg_replace('/^[^=]+=([^,]+),.*$/', '\1', $data['dn'])."'";
183 }
184 }
185 $this->dhcpObjectCache[$data['dn']]= $data;
186 $this->dhcpSections= $newsects;
187 } else {
188 if ($dn != $data['dn']){
189 /* Old object, new name */
190 $this->dhcpObjectCache[$dn]= array();
191 $this->dhcpObjectCache[$data['dn']]= $data;
193 /* If we renamed a section, we've to rename a couple of objects, too */
194 foreach ($this->dhcpObjectCache as $key => $dsc){
195 if (preg_match("/,$dn$/", $key)){
196 $new_dn= preg_replace("/,$dn$/", ",".$data['dn'], $key);
197 $dsc['MODIFIED']= TRUE;
198 $this->dhcpObjectCache[$new_dn]= $dsc;
199 unset($this->dhcpObjectCache[$key]);
200 }
201 }
202 $newsects= array();
203 foreach ($this->dhcpSections as $key => $dsc){
204 if ($key == $dn){
205 $newsects[$data['dn']]= "$spaces$type '".preg_replace('/^[^=]+=([^,]+),.*$/', '\1', $data['dn'])."'";
206 continue;
207 }
208 if (preg_match("/,$dn$/", $key)){
209 $new_dn= preg_replace("/,$dn$/", ",".$data['dn'], $key);
210 $newsects[$new_dn]= $dsc;
211 } else {
212 $newsects[$key]= $dsc;
213 }
214 }
215 $this->dhcpSections= $newsects;
217 } else {
218 /* Old object, old name */
219 $this->dhcpObjectCache[$data['dn']]= $data;
220 }
221 }
222 $this->dialog= NULL;
223 }
224 }
226 /* Remove section? */
227 if (isset($_POST['delete_dhcp_confirm'])){
228 if (chkacl($this->acl, "delete") == ""){
229 unset($this->dhcpSections[$this->current_object]);
230 unset($this->dhcpObjectCache[$this->current_object]);
231 $this->dhcpObjectCache[$this->current_object]= array();
232 foreach ($this->dhcpSections as $key => $value){
233 if (preg_match("/".$this->current_object."$/", $key)){
234 unset($this->dhcpSections[$key]);
235 unset($this->dhcpObjectCache[$key]);
236 $this->dhcpObjectCache[$key]= array();
237 }
238 }
239 } else {
240 print_red(_("You're not allowed to remove DHCP sections!"));
241 }
242 $this->dialog= NULL;
243 }
245 /* Look for post entries */
246 foreach($_POST as $name => $value){
248 /* Insert new section? */
249 if (preg_match('/^insertDhcp_.*_x$/', $name)){
250 $dn= base64_decode(preg_replace('/^insertDhcp_([^_]+)_x$/', '\1', $name));
251 if (isset($this->dhcpObjectCache[$dn])){
252 $this->dialog= new dhcpNewSectionDialog($this->objectType($dn));
253 $this->current_object= $dn;
254 $this->dialog->acl= $this->acl;
255 }
256 }
258 /* Edit section? */
259 if (preg_match('/^editDhcp_.*_x$/', $name)){
260 $dn= base64_decode(preg_replace('/^editDhcp_([^_]+)_x$/', '\1', $name));
261 if (isset($this->dhcpObjectCache[$dn])){
262 $section= $this->objectType($dn);
263 $this->current_object= $dn;
264 $this->dialog= new $section($this->dhcpObjectCache[$dn]);
265 }
266 }
268 /* Remove section? */
269 if (preg_match('/^delDhcp_.*_x$/', $name)){
270 $dn= base64_decode(preg_replace('/^delDhcp_([^_]+)_x$/', '\1', $name));
271 if (isset($this->dhcpObjectCache[$dn])){
272 $this->current_object= $dn;
273 $this->dialog= 1;
274 $smarty->assign("warning", sprintf(_("You're about to delete the DHCP section '%s'."), $dn));
275 return($smarty->fetch(get_template_path('remove_dhcp.tpl', TRUE)));
276 }
277 }
279 }
281 if(isset($_GET['act']) && $_GET['act']=="edit" && isset($_GET['id'])){
282 $dn = base64_decode($_GET['id']);
283 if (isset($this->dhcpObjectCache[$dn])){
284 $section= $this->objectType($dn);
285 $this->current_object= $dn;
286 $this->dialog= new $section($this->dhcpObjectCache[$dn]);
287 }
288 }
291 /* Do we need to flip is_account state? */
292 if (isset($_POST['modify_state'])){
293 $this->is_account= !$this->is_account;
294 }
296 /* Show tab dialog headers */
297 if ($this->is_account){
298 $display= $this->show_header(_("Remove DHCP service"),
299 _("This server has DHCP features enabled. You can disable them by clicking below."));
301 if (!count($this->dhcpObjectCache)){
302 $attrs= array();
303 $attrs['dn']= 'cn=dhcp,'.$this->dn;
304 $attrs['cn']= array('dhcp');
305 $attrs['objectClass']= array('top', 'dhcpService');
306 $attrs['dhcpPrimaryDN']= array($this->dn);
307 $attrs['dhcpStatements']= array("default-lease-time 600",
308 "max-lease-time 1200",
309 "authoritative",
310 "ddns-update-style none");
311 $attrs['MODIFIED']= TRUE;
312 $this->dhcpSections['cn=dhcp,'.$this->dn]= _("Global options");
313 $this->dhcpObjectCache['cn=dhcp,'.$this->dn]= $attrs;
314 }
316 } else {
317 $display= $this->show_header(_("Add DHCP service"),
318 _("This server has DHCP features disabled. You can enable them by clicking below."));
319 return ($display);
320 }
323 /* Show dialog
324 */
325 if($this->dialog != NULL && !is_int($this->dialog)){
326 $this->dialog->save_object();
327 $this->dialog->parent = $this;
328 return($this->dialog->execute());
329 }
331 /* Create Listbox with existing Zones
332 */
333 $DhcpList = new divSelectBox("dhcpSections");
334 $DhcpList->SetHeight(400);
336 /* Add entries to divlist
337 */
338 $editImgIns = "<input type='image' src='images/list_new.png' name='insertDhcp_%s' title='"._("Insert new DHCP section")."'>".
339 "<input type='image' src='images/edit.png' name='editDhcp_%s' title='"._("Edit DHCP section")."'>".
340 "<input type='image' src='images/edittrash.png' name='delDhcp_%s' title='"._("Remove DHCP section")."'>";
341 $editImgInsNoDel = "<input type='image' src='images/list_new.png' name='insertDhcp_%s' title='"._("Insert new DHCP section")."'>".
342 "<input type='image' src='images/edit.png' name='editDhcp_%s' title='"._("Edit DHCP section")."'>";
343 $editImg = "<input type='image' src='images/edit.png' name='editDhcp_%s' title='"._("Edit DHCP section")."'>".
344 "<input type='image' src='images/edittrash.png' name='delDhcp_%s' title='"._("Remove DHCP section")."'>";
346 $tmp = new dhcpNewSectionDialog(NULL);
347 foreach($this->dhcpSections as $section => $values ){
349 $values = "<a href='?plug=".$_GET['plug']."&act=edit&id=".base64_encode($section)."'>".$values."</a>";
351 if (count($tmp->sectionMap[$this->objectType($section)])){
352 if ($this->objectType($section) == "dhcpService"){
353 $DhcpList->AddEntry(array(
354 array("string" => $values),
355 array("string" => str_replace("%s",base64_encode($section),$editImgInsNoDel), "attach" => "style='text-align:right;'")
356 ));
357 } else {
358 $DhcpList->AddEntry(array(
359 array("string" => $values),
360 array("string" => str_replace("%s",base64_encode($section),$editImgIns), "attach" => "style='text-align:right;'")
361 ));
362 }
363 } else {
364 $DhcpList->AddEntry(array(
365 array("string" => $values),
366 array("string" => str_replace("%s",base64_encode($section),$editImg), "attach" => "style='text-align:right;'")
367 ));
368 }
369 }
371 $smarty->assign("dhcpACL",chkacl($this->acl,"servdhcp"));
373 /* Display tempalte */
374 $smarty->assign("DhcpList",$DhcpList->DrawList());
375 $display.= $smarty->fetch(get_template_path('servdhcp.tpl', TRUE));
376 return($display);
377 }
380 function remove_from_parent()
381 {
382 /* Cancel if there's nothing to do here */
383 if (!$this->initially_was_account){
384 return;
385 }
387 /* Remove subtrees */
388 $ldap= $this->config->get_ldap_link();
389 foreach ($this->dhcpObjectCache as $dn => $content){
390 if ($this->objectType($dn) == 'dhcpService'){
391 $ldap->rmdir_recursive($dn);
392 show_ldap_error($ldap->get_error(), _("Removing DHCP entries failed"));
393 }
394 }
396 /* Remove from self */
397 $ldap= $this->config->get_ldap_link();
399 /* Remove and write to LDAP */
400 plugin::remove_from_parent();
402 @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->attributes, "Save");
403 $ldap->cd($this->dn);
404 $this->cleanup();
405 $ldap->modify ($this->attrs);
407 show_ldap_error($ldap->get_error(), _("Removing DHCP entries failed"));
409 /* Optionally execute a command after we're done */
410 $this->handle_post_events("remove");
411 }
414 /* Save data to object */
415 function save_object()
416 {
417 plugin::save_object();
418 }
421 /* Check supplied data */
422 function check()
423 {
424 /* Call common method to give check the hook */
425 $message= plugin::check();
427 return ($message);
428 }
431 /* Save to LDAP */
432 function save()
433 {
434 /* Take over handling
435 * - Load servdhcp class and dhcpObjectCache for the source dhcp setup.
436 * - Assign dhcpObjectCache to this configuration.
437 * - Save this setup and remove source setup from ldap.
438 */
439 if($this->take_over_id != -1){
440 $id = $this->take_over_id;
441 $src = preg_replace("/cn=dhcp,/","",$this->dhcp_server_list['ENTRIES'][$id]['dn']);
442 $tmp = new servdhcp ($this->config, $src);
443 $this->orig_dn = $src;
444 $this->dhcpObjectCache = $tmp->dhcpObjectCache;
445 }
447 /* Save dhcp setttings */
448 $ldap= $this->config->get_ldap_link();
449 foreach ($this->dhcpObjectCache as $dn => $data){
451 if($this->dn != $this->orig_dn){
452 $dn = preg_replace("/".normalizePreg($this->orig_dn)."$/i",$this->dn,$dn);
453 }
455 /* Remove entry? */
456 if (count($data) == 0){
457 /* Check if exists, then remove... */
458 if($ldap->cat($dn)){
459 $ldap->rmdir_recursive($dn);
460 show_ldap_error($ldap->get_error(), _("Can't remove DHCP object!"));
461 }
462 continue;
463 }
465 /* Opdate dhcp option 'server-name' to actual server name */
466 if($this->dn != $this->orig_dn){
467 $fixed = FALSE;
468 foreach(array("dhcpHost","dhcpSubnet","dhcpGroup","dhcpSharedNetwork") as $object){
469 if(in_array($object,$data['objectClass']) && isset($data['dhcpOption'])){
470 foreach($data['dhcpOption'] as $key => $option){
471 if(preg_match("/^server-name /",$option)){
472 $data['dhcpOption'][$key] = "server-name ".$this->cn;
473 $data['MODIFIED'] = TRUE;
474 break;
475 }
476 }
477 }
479 /* Skip next loops if entry is updated */
480 if($fixed){
481 break;
482 }
483 }
484 }
486 /* Modify existing entry? */
487 if (isset($data['MODIFIED']) || $this->orig_dn != $this->dn){
489 if($ldap->cat($dn)){
490 $modify= TRUE;
491 } else {
492 $modify= FALSE;
493 }
495 /* Build new entry */
496 $attrs= array();
497 foreach ($data as $attribute => $values){
498 if ($attribute == "MODIFIED" || $attribute == "dn"){
499 continue;
500 }
502 if(in_array($attribute,array("dhcpPrimaryDN","dhcpSecondaryDN","dhcpServerDN","dhcpFailOverPeerDN"))){
503 foreach($values as $v_key => $value){
504 $values[$v_key] = preg_replace("/".normalizePreg($this->orig_dn)."$/i",$this->dn,$value);
505 }
506 }
508 if (count($values)){
510 if($attribute == "dhcpOption"){
511 foreach($values as $key => $value){
512 $option_name = trim(preg_replace("/[^ ]*$/","",$value));
513 $option_value= trim(preg_replace("/^[^ ]*/","",$value));
514 if(in_array($option_name,$this->quote_option)){
515 $values[$key] = $option_name." \"".$option_value."\"";
516 }
517 }
518 }
519 if (count($values) == 1){
520 $attrs[$attribute]= $values[0];
521 } else {
522 $attrs[$attribute]= $values;
523 }
524 } else {
525 if ($modify){
526 $attrs[$attribute]= array();
527 }
528 }
529 }
531 $ldap->cd($dn);
532 if ($modify){
533 $ldap->modify($attrs);
534 show_ldap_error($ldap->get_error(), _("Can't save DHCP object!"));
535 } else {
536 $ldap->add($attrs);
537 show_ldap_error($ldap->get_error(), _("Can't save DHCP object!"));
538 }
539 }
540 }
542 $this->dhcpServiceDN= $this->serviceDN;
543 if($this->dn != $this->orig_dn){
544 $this->dhcpServiceDN= preg_replace("/".normalizePreg($this->orig_dn)."$/i",$this->dn,$this->dhcpServiceDN);
545 }
547 /* Replace 'new' dn */
548 if(preg_match("/new$/",$this->dhcpServiceDN)){
549 $this->dhcpServiceDN = preg_replace("/new$/",$this->dn,$this->dhcpServiceDN);
550 }
552 plugin::save();
554 /* Save data to LDAP */
555 $ldap->cd($this->dn);
556 $this->cleanup();
557 $ldap->modify ($this->attrs);
559 show_ldap_error($ldap->get_error(), _("Saving DHCP service failed"));
561 /* Optionally execute a command after we're done */
562 if ($this->initially_was_account == $this->is_account){
563 if ($this->is_modified){
564 $this->handle_post_events("modify");
565 }
566 } else {
567 $this->handle_post_events("add");
568 }
570 /* Take over handling
571 * - Remove old dhcp config from source server
572 */
573 if($this->take_over_id != -1){
574 $id = $this->take_over_id;
575 $src = $this->dhcp_server_list['ENTRIES'][$id]['dn'];
576 $tmp = new servdhcp ($this->config, $src);
577 $tmp->remove_from_parent();
578 }
579 }
582 function reload()
583 {
584 /* Init LDAP and load list */
585 $ldap= $this->config->get_ldap_link();
586 $ui= get_userinfo();
587 $me= $this->dn;
589 $list= get_list("(&(objectClass=dhcpService)(|(dhcpPrimaryDN=$me)(dhcpSecondaryDN=$me)(dhcpServerDN=$me)(dhcpFailOverPeerDN=$me)))", $ui->subtreeACL, $this->config->current['BASE'], array("cn"));
590 $final= array();
591 foreach ($list as $value){
593 /* Set header */
594 $sortpart= split(",", $value['dn']);
595 $sortpart= array_reverse($sortpart);
596 $tmp= implode(",", $sortpart);
598 $final[$value['dn']]= $tmp."!"._("Global options");
600 /* Read all sub entries to place here */
601 $ldap->cd($value['dn']);
602 $ldap->search("(|(objectClass=dhcpService)(objectClass=dhcpLog)(objectClass=dhcpClass)(objectClass=dhcpSubClass)(objectClass=dhcpHost)(objectClass=dhcpGroup)(objectClass=dhcpPool)(objectClass=dhcpSubnet)(objectClass=dhcpSharedNetwork)(objectClass=dhcpOptions)(objectClass=dhcpTSigKey)(objectClass=dhcpDnsZone)(objectClass=dhcpFailOverPeer))", array());
603 $this->serviceDN= $value['dn'];
605 while ($attrs= $ldap->fetch()){
606 $sattrs= array();
607 for ($i= 0; $i<$attrs['count']; $i++){
608 $sattrs[$attrs[$i]]= $attrs[$attrs[$i]];
609 unset($sattrs[$attrs[$i]]['count']);
610 }
611 $sattrs['dn']= $ldap->getDN();
613 foreach($sattrs as $name => $values){
614 if($name == "dhcpOption"){
615 foreach($values as $key => $value){
616 $value_name = trim(preg_replace("/[^ ]*$/","",$value));
617 $value_value= trim(preg_replace("/^[^ ]*/","",$value));
618 if(in_array($value_name,$this->quote_option)){
619 $value_value = preg_replace("/^\"/","",$value_value);
620 $value_value = preg_replace("/\"$/","",$value_value);
621 $sattrs[$name][$key] = $value_name." ".$value_value;
622 }
623 }
624 }
625 }
627 $this->dhcpObjectCache[$ldap->getDN()]= $sattrs;
628 $tmp= preg_replace("/".$this->serviceDN."/", "", $ldap->getDN());
629 $indent= substr_count($tmp, ",");
630 $spaces= "";
631 for ($i= 0; $i<$indent; $i++){
632 $spaces.= " ";
633 }
635 foreach ($this->types as $key => $val){
636 if (in_array("$key", $attrs['objectClass'])){
637 $type= $val;
638 break;
639 }
640 }
642 /* Prepare for sorting... */
643 $sortpart= split(",", $ldap->getDN());
644 $sortpart= array_reverse($sortpart);
645 $tmp= implode(",", $sortpart);
646 $final[$ldap->getDN()]= $tmp."!".$spaces.$type." '".$attrs['cn'][0]."'";
647 }
648 }
650 /* Sort it... */
651 natsort($final);
652 $this->dhcpSections= array();
653 foreach ($final as $key => $val){
654 $this->dhcpSections[$key]= preg_replace('/^[^!]+!(.*)$/', '\\1', $val);
655 }
657 }
660 function objectType($dn)
661 {
662 $type= "";
663 $types= array("dhcpService", "dhcpClass", "dhcpSubClass", "dhcpHost",
664 "dhcpGroup", "dhcpPool", "dhcpSubnet", "dhcpSharedNetwork");
666 foreach ($this->dhcpObjectCache[$dn]['objectClass'] as $oc){
667 if (in_array($oc, $types)){
668 $type= $oc;
669 break;
670 }
671 }
673 /* That should not happen... */
674 if ($type == ""){
675 print_red(_("DHCP configuration set is unknown. Please contact your system administrator."));
676 }
678 return ($type);
679 }
681 }
683 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
684 ?>