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);
122 $warning= " "._("This includes 'all' DNS zones that are located within this server. Please double check if your really want to do this.");
123 $warning.= " "._("The migration will be startet when you save this system. To cancel this action, use the cancel button below.");
125 if($this->display_warning){
126 print_red($warning);
127 $this->display_warning = FALSE;
128 }
129 return($smarty->fetch(get_template_path('servdhcp.tpl', TRUE)));
130 }
133 /*****************/
134 /* List handling
135 /*****************/
137 /* Section Creation? */
138 if (isset($_POST['create_section']) && isset($_POST['section'])){
139 $section= $_POST['section'];
140 $tmp = new dhcpNewSectionDialog(NULL);
141 if (isset($tmp->sectionMap[$section])){
142 $this->dialog= new $section($this->current_object);
143 $this->current_object= "";
144 } else {
145 $this->dialog= NULL;
146 }
147 }
149 /* Cancel section creation? */
150 if (isset($_POST['cancel_section']) || isset($_POST['cancel_dhcp'])){
151 $this->dialog= NULL;
152 }
154 /* Save changes */
155 if (isset($_POST['save_dhcp'])){
156 $this->dialog->save_object();
157 $messages= $this->dialog->check($this->dhcpObjectCache);
158 if (count($messages)){
159 show_errors($messages);
160 } else {
161 $dn= $this->dialog->dn;
162 $class= get_class($this->dialog);
163 $type= $this->types[$class];
164 if(empty($this->serviceDN)){
165 $indent= substr_count(preg_replace("/".$this->dn."/", '', $dn), ",") -1;
166 }else{
167 $indent= substr_count(preg_replace("/".$this->serviceDN."/", '', $dn), ",");
168 }
169 $spaces= "";
170 for ($i= 0; $i<$indent; $i++){
171 $spaces.= " ";
172 }
173 $data= $this->dialog->save();
174 if ($this->current_object == ""){
175 /* New object */
176 $newsects= array();
177 foreach ($this->dhcpSections as $key => $dsc){
178 $newsects[$key]= $dsc;
179 if ($key == $dn){
180 $spaces.= " ";
181 $newsects[$data['dn']]= "$spaces$type '".preg_replace('/^[^=]+=([^,]+),.*$/', '\1', $data['dn'])."'";
182 }
183 }
184 $this->dhcpObjectCache[$data['dn']]= $data;
185 $this->dhcpSections= $newsects;
186 } else {
187 if ($dn != $data['dn']){
188 /* Old object, new name */
189 $this->dhcpObjectCache[$dn]= array();
190 $this->dhcpObjectCache[$data['dn']]= $data;
192 /* If we renamed a section, we've to rename a couple of objects, too */
193 foreach ($this->dhcpObjectCache as $key => $dsc){
194 if (preg_match("/,$dn$/", $key)){
195 $new_dn= preg_replace("/,$dn$/", ",".$data['dn'], $key);
196 $dsc['MODIFIED']= TRUE;
197 $this->dhcpObjectCache[$new_dn]= $dsc;
198 unset($this->dhcpObjectCache[$key]);
199 }
200 }
201 $newsects= array();
202 foreach ($this->dhcpSections as $key => $dsc){
203 if ($key == $dn){
204 $newsects[$data['dn']]= "$spaces$type '".preg_replace('/^[^=]+=([^,]+),.*$/', '\1', $data['dn'])."'";
205 continue;
206 }
207 if (preg_match("/,$dn$/", $key)){
208 $new_dn= preg_replace("/,$dn$/", ",".$data['dn'], $key);
209 $newsects[$new_dn]= $dsc;
210 } else {
211 $newsects[$key]= $dsc;
212 }
213 }
214 $this->dhcpSections= $newsects;
216 } else {
217 /* Old object, old name */
218 $this->dhcpObjectCache[$data['dn']]= $data;
219 }
220 }
221 $this->dialog= NULL;
222 }
223 }
225 /* Remove section? */
226 if (isset($_POST['delete_dhcp_confirm'])){
227 if (chkacl($this->acl, "delete") == ""){
228 unset($this->dhcpSections[$this->current_object]);
229 unset($this->dhcpObjectCache[$this->current_object]);
230 $this->dhcpObjectCache[$this->current_object]= array();
231 foreach ($this->dhcpSections as $key => $value){
232 if (preg_match("/".$this->current_object."$/", $key)){
233 unset($this->dhcpSections[$key]);
234 unset($this->dhcpObjectCache[$key]);
235 $this->dhcpObjectCache[$key]= array();
236 }
237 }
238 } else {
239 print_red(_("You're not allowed to remove DHCP sections!"));
240 }
241 $this->dialog= NULL;
242 }
244 /* Look for post entries */
245 foreach($_POST as $name => $value){
247 /* Insert new section? */
248 if (preg_match('/^insertDhcp_.*_x$/', $name)){
249 $dn= base64_decode(preg_replace('/^insertDhcp_([^_]+)_x$/', '\1', $name));
250 if (isset($this->dhcpObjectCache[$dn])){
251 $this->dialog= new dhcpNewSectionDialog($this->objectType($dn));
252 $this->current_object= $dn;
253 $this->dialog->acl= $this->acl;
254 }
255 }
257 /* Edit section? */
258 if (preg_match('/^editDhcp_.*_x$/', $name)){
259 $dn= base64_decode(preg_replace('/^editDhcp_([^_]+)_x$/', '\1', $name));
260 if (isset($this->dhcpObjectCache[$dn])){
261 $section= $this->objectType($dn);
262 $this->current_object= $dn;
263 $this->dialog= new $section($this->dhcpObjectCache[$dn]);
264 }
265 }
267 /* Remove section? */
268 if (preg_match('/^delDhcp_.*_x$/', $name)){
269 $dn= base64_decode(preg_replace('/^delDhcp_([^_]+)_x$/', '\1', $name));
270 if (isset($this->dhcpObjectCache[$dn])){
271 $this->current_object= $dn;
272 $this->dialog= 1;
273 $smarty->assign("warning", sprintf(_("You're about to delete the DHCP section '%s'."), $dn));
274 return($smarty->fetch(get_template_path('remove_dhcp.tpl', TRUE)));
275 }
276 }
278 }
280 /* Do we need to flip is_account state? */
281 if (isset($_POST['modify_state'])){
282 $this->is_account= !$this->is_account;
283 }
285 /* Show tab dialog headers */
286 if ($this->is_account){
287 $display= $this->show_header(_("Remove DHCP service"),
288 _("This server has DHCP features enabled. You can disable them by clicking below."));
290 if (!count($this->dhcpObjectCache)){
291 $attrs= array();
292 $attrs['dn']= 'cn=dhcp,'.$this->dn;
293 $attrs['cn']= array('dhcp');
294 $attrs['objectClass']= array('top', 'dhcpService');
295 $attrs['dhcpPrimaryDN']= array($this->dn);
296 $attrs['dhcpStatements']= array("default-lease-time 600",
297 "max-lease-time 1200",
298 "authoritative",
299 "ddns-update-style none");
300 $attrs['MODIFIED']= TRUE;
301 $this->dhcpSections['cn=dhcp,'.$this->dn]= _("Global options");
302 $this->dhcpObjectCache['cn=dhcp,'.$this->dn]= $attrs;
303 }
305 } else {
306 $display= $this->show_header(_("Add DHCP service"),
307 _("This server has DHCP features disabled. You can enable them by clicking below."));
308 return ($display);
309 }
312 /* Show dialog
313 */
314 if($this->dialog != NULL && !is_int($this->dialog)){
315 $this->dialog->save_object();
316 $this->dialog->parent = $this;
317 return($this->dialog->execute());
318 }
320 /* Create Listbox with existing Zones
321 */
322 $DhcpList = new divSelectBox("dhcpSections");
323 $DhcpList->SetHeight(400);
325 /* Add entries to divlist
326 */
327 $editImgIns = "<input type='image' src='images/list_new.png' name='insertDhcp_%s' title='"._("Insert new DHCP section")."'>".
328 "<input type='image' src='images/edit.png' name='editDhcp_%s' title='"._("Edit DHCP section")."'>".
329 "<input type='image' src='images/edittrash.png' name='delDhcp_%s' title='"._("Remove DHCP section")."'>";
330 $editImgInsNoDel = "<input type='image' src='images/list_new.png' name='insertDhcp_%s' title='"._("Insert new DHCP section")."'>".
331 "<input type='image' src='images/edit.png' name='editDhcp_%s' title='"._("Edit DHCP section")."'>";
332 $editImg = "<input type='image' src='images/edit.png' name='editDhcp_%s' title='"._("Edit DHCP section")."'>".
333 "<input type='image' src='images/edittrash.png' name='delDhcp_%s' title='"._("Remove DHCP section")."'>";
335 $tmp = new dhcpNewSectionDialog(NULL);
336 foreach($this->dhcpSections as $section => $values ){
338 if (count($tmp->sectionMap[$this->objectType($section)])){
339 if ($this->objectType($section) == "dhcpService"){
340 $DhcpList->AddEntry(array(
341 array("string" => $values),
342 array("string" => str_replace("%s",base64_encode($section),$editImgInsNoDel), "attach" => "style='text-align:right;'")
343 ));
344 } else {
345 $DhcpList->AddEntry(array(
346 array("string" => $values),
347 array("string" => str_replace("%s",base64_encode($section),$editImgIns), "attach" => "style='text-align:right;'")
348 ));
349 }
350 } else {
351 $DhcpList->AddEntry(array(
352 array("string" => $values),
353 array("string" => str_replace("%s",base64_encode($section),$editImg), "attach" => "style='text-align:right;'")
354 ));
355 }
356 }
358 $smarty->assign("dhcpACL",chkacl($this->acl,"servdhcp"));
360 /* Display tempalte */
361 $smarty->assign("DhcpList",$DhcpList->DrawList());
362 $display.= $smarty->fetch(get_template_path('servdhcp.tpl', TRUE));
363 return($display);
364 }
367 function remove_from_parent()
368 {
369 /* Cancel if there's nothing to do here */
370 if (!$this->initially_was_account){
371 return;
372 }
374 /* Remove subtrees */
375 $ldap= $this->config->get_ldap_link();
376 foreach ($this->dhcpObjectCache as $dn => $content){
377 if ($this->objectType($dn) == 'dhcpService'){
378 $ldap->rmdir_recursive($dn);
379 show_ldap_error($ldap->get_error(), _("Removing DHCP entries failed"));
380 }
381 }
383 /* Remove from self */
384 $ldap= $this->config->get_ldap_link();
386 /* Remove and write to LDAP */
387 plugin::remove_from_parent();
389 @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->attributes, "Save");
390 $ldap->cd($this->dn);
391 $this->cleanup();
392 $ldap->modify ($this->attrs);
394 show_ldap_error($ldap->get_error(), _("Removing DHCP entries failed"));
396 /* Optionally execute a command after we're done */
397 $this->handle_post_events("remove");
398 }
401 /* Save data to object */
402 function save_object()
403 {
404 plugin::save_object();
405 }
408 /* Check supplied data */
409 function check()
410 {
411 /* Call common method to give check the hook */
412 $message= plugin::check();
414 return ($message);
415 }
418 /* Save to LDAP */
419 function save()
420 {
421 /* Take over handling
422 * - Load servdhcp class and dhcpObjectCache for the source dhcp setup.
423 * - Assign dhcpObjectCache to this configuration.
424 * - Save this setup and remove source setup from ldap.
425 */
426 if($this->take_over_id != -1){
427 $id = $this->take_over_id;
428 $src = preg_replace("/cn=dhcp,/","",$this->dhcp_server_list['ENTRIES'][$id]['dn']);
429 $tmp = new servdhcp ($this->config, $src);
430 $this->orig_dn = $src;
431 $this->dhcpObjectCache = $tmp->dhcpObjectCache;
432 }
434 /* Save dhcp setttings */
435 $ldap= $this->config->get_ldap_link();
436 foreach ($this->dhcpObjectCache as $dn => $data){
438 if($this->dn != $this->orig_dn){
439 $dn = preg_replace("/".normalizePreg($this->orig_dn)."$/i",$this->dn,$dn);
440 }
442 /* Remove entry? */
443 if (count($data) == 0){
444 /* Check if exists, then remove... */
445 if($ldap->cat($dn)){
446 $ldap->rmdir_recursive($dn);
447 show_ldap_error($ldap->get_error(), _("Can't remove DHCP object!"));
448 }
449 continue;
450 }
452 /* Modify existing entry? */
453 if (isset($data['MODIFIED']) || $this->orig_dn != $this->dn){
455 if($ldap->cat($dn)){
456 $modify= TRUE;
457 } else {
458 $modify= FALSE;
459 }
461 /* Build new entry */
462 $attrs= array();
463 foreach ($data as $attribute => $values){
464 if ($attribute == "MODIFIED" || $attribute == "dn"){
465 continue;
466 }
468 if(in_array($attribute,array("dhcpPrimaryDN","dhcpSecondaryDN","dhcpServerDN","dhcpFailOverPeerDN"))){
469 foreach($values as $v_key => $value){
470 $values[$v_key] = preg_replace("/".normalizePreg($this->orig_dn)."$/i",$this->dn,$value);
471 }
472 }
474 if (count($values)){
476 if($attribute == "dhcpOption"){
477 foreach($values as $key => $value){
478 $option_name = trim(preg_replace("/[^ ]*$/","",$value));
479 $option_value= trim(preg_replace("/^[^ ]*/","",$value));
480 if(in_array($option_name,$this->quote_option)){
481 $values[$key] = $option_name." \"".$option_value."\"";
482 }
483 }
484 }
485 if (count($values) == 1){
486 $attrs[$attribute]= $values[0];
487 } else {
488 $attrs[$attribute]= $values;
489 }
490 } else {
491 if ($modify){
492 $attrs[$attribute]= array();
493 }
494 }
495 }
497 $ldap->cd($dn);
498 if ($modify){
499 $ldap->modify($attrs);
500 show_ldap_error($ldap->get_error(), _("Can't save DHCP object!"));
501 } else {
502 $ldap->add($attrs);
503 show_ldap_error($ldap->get_error(), _("Can't save DHCP object!"));
504 }
505 }
506 }
508 $this->dhcpServiceDN= $this->serviceDN;
509 if($this->dn != $this->orig_dn){
510 $this->dhcpServiceDN= preg_replace("/".normalizePreg($this->orig_dn)."$/i",$this->dn,$this->dhcpServiceDN);
511 }
513 /* Replace 'new' dn */
514 if(preg_match("/new$/",$this->dhcpServiceDN)){
515 $this->dhcpServiceDN = preg_replace("/new$/",$this->dn,$this->dhcpServiceDN);
516 }
518 plugin::save();
520 /* Save data to LDAP */
521 $ldap->cd($this->dn);
522 $this->cleanup();
523 $ldap->modify ($this->attrs);
525 show_ldap_error($ldap->get_error(), _("Saving DHCP service failed"));
527 /* Optionally execute a command after we're done */
528 if ($this->initially_was_account == $this->is_account){
529 if ($this->is_modified){
530 $this->handle_post_events("modify");
531 }
532 } else {
533 $this->handle_post_events("add");
534 }
536 /* Take over handling
537 * - Remove old dhcp config from source server
538 */
539 if($this->take_over_id != -1){
540 $id = $this->take_over_id;
541 $src = $this->dhcp_server_list['ENTRIES'][$id]['dn'];
542 $tmp = new servdhcp ($this->config, $src);
543 $tmp->remove_from_parent();
544 }
545 }
548 function reload()
549 {
550 /* Init LDAP and load list */
551 $ldap= $this->config->get_ldap_link();
552 $ui= get_userinfo();
553 $me= $this->dn;
555 $list= get_list("(&(objectClass=dhcpService)(|(dhcpPrimaryDN=$me)(dhcpSecondaryDN=$me)(dhcpServerDN=$me)(dhcpFailOverPeerDN=$me)))", $ui->subtreeACL, $this->config->current['BASE'], array("cn"));
556 $final= array();
557 foreach ($list as $value){
559 /* Set header */
560 $sortpart= split(",", $value['dn']);
561 $sortpart= array_reverse($sortpart);
562 $tmp= implode(",", $sortpart);
564 $final[$value['dn']]= $tmp."!"._("Global options");
566 /* Read all sub entries to place here */
567 $ldap->cd($value['dn']);
568 $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());
569 $this->serviceDN= $value['dn'];
571 while ($attrs= $ldap->fetch()){
572 $sattrs= array();
573 for ($i= 0; $i<$attrs['count']; $i++){
574 $sattrs[$attrs[$i]]= $attrs[$attrs[$i]];
575 unset($sattrs[$attrs[$i]]['count']);
576 }
577 $sattrs['dn']= $ldap->getDN();
579 foreach($sattrs as $name => $values){
580 if($name == "dhcpOption"){
581 foreach($values as $key => $value){
582 $value_name = trim(preg_replace("/[^ ]*$/","",$value));
583 $value_value= trim(preg_replace("/^[^ ]*/","",$value));
584 if(in_array($value_name,$this->quote_option)){
585 $value_value = preg_replace("/^\"/","",$value_value);
586 $value_value = preg_replace("/\"$/","",$value_value);
587 $sattrs[$name][$key] = $value_name." ".$value_value;
588 }
589 }
590 }
591 }
593 $this->dhcpObjectCache[$ldap->getDN()]= $sattrs;
594 $tmp= preg_replace("/".$this->serviceDN."/", "", $ldap->getDN());
595 $indent= substr_count($tmp, ",");
596 $spaces= "";
597 for ($i= 0; $i<$indent; $i++){
598 $spaces.= " ";
599 }
601 foreach ($this->types as $key => $val){
602 if (in_array("$key", $attrs['objectClass'])){
603 $type= $val;
604 break;
605 }
606 }
608 /* Prepare for sorting... */
609 $sortpart= split(",", $ldap->getDN());
610 $sortpart= array_reverse($sortpart);
611 $tmp= implode(",", $sortpart);
612 $final[$ldap->getDN()]= $tmp."!".$spaces.$type." '".$attrs['cn'][0]."'";
613 }
614 }
616 /* Sort it... */
617 natsort($final);
618 $this->dhcpSections= array();
619 foreach ($final as $key => $val){
620 $this->dhcpSections[$key]= preg_replace('/^[^!]+!(.*)$/', '\\1', $val);
621 }
623 }
626 function objectType($dn)
627 {
628 $type= "";
629 $types= array("dhcpService", "dhcpClass", "dhcpSubClass", "dhcpHost",
630 "dhcpGroup", "dhcpPool", "dhcpSubnet", "dhcpSharedNetwork");
632 foreach ($this->dhcpObjectCache[$dn]['objectClass'] as $oc){
633 if (in_array($oc, $types)){
634 $type= $oc;
635 break;
636 }
637 }
639 /* That should not happen... */
640 if ($type == ""){
641 print_red(_("DHCP configuration set is unknown. Please contact your system administrator."));
642 }
644 return ($type);
645 }
647 }
649 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
650 ?>