Code

8bbf7a5e2dbc6db7eef43869f7ae2af8d57d7a20
[gosa.git] / plugins / admin / systems / class_servDHCP.inc
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   }
58   
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       }
71   
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']));
99     
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     }
107  
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.= "&nbsp;"._("The migration will be startet 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     }
133     
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.= "&nbsp;&nbsp;&nbsp;&nbsp;";
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.= "&nbsp;&nbsp;&nbsp;&nbsp;";
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){
247       
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     /* Do we need to flip is_account state? */
282     if (isset($_POST['modify_state'])){
283       $this->is_account= !$this->is_account;
284     }
286     /* Show tab dialog headers */
287     if ($this->is_account){
288       $display= $this->show_header(_("Remove DHCP service"),
289           _("This server has DHCP features enabled. You can disable them by clicking below."));
291       if (!count($this->dhcpObjectCache)){
292         $attrs= array();
293         $attrs['dn']= 'cn=dhcp,'.$this->dn;
294         $attrs['cn']= array('dhcp');
295         $attrs['objectClass']= array('top', 'dhcpService');
296         $attrs['dhcpPrimaryDN']= array($this->dn);
297         $attrs['dhcpStatements']= array("default-lease-time 600",
298                                         "max-lease-time 1200",
299                                         "authoritative",
300                                         "ddns-update-style none");
301         $attrs['MODIFIED']= TRUE;
302         $this->dhcpSections['cn=dhcp,'.$this->dn]= _("Global options");
303         $this->dhcpObjectCache['cn=dhcp,'.$this->dn]= $attrs;
304       }
306     } else {
307       $display= $this->show_header(_("Add DHCP service"),
308           _("This server has DHCP features disabled. You can enable them by clicking below."));
309       return ($display);
310     }
313     /* Show dialog
314      */
315     if($this->dialog != NULL && !is_int($this->dialog)){
316       $this->dialog->save_object();
317       $this->dialog->parent = $this;
318       return($this->dialog->execute());
319     }
321     /* Create Listbox with existing Zones
322      */
323     $DhcpList = new divSelectBox("dhcpSections");
324     $DhcpList->SetHeight(400);
326     /* Add entries to divlist
327      */
328     $editImgIns = "<input type='image' src='images/list_new.png' name='insertDhcp_%s' title='"._("Insert new DHCP section")."'>".
329       "<input type='image' src='images/edit.png' name='editDhcp_%s' title='"._("Edit DHCP section")."'>".
330       "<input type='image' src='images/edittrash.png' name='delDhcp_%s' title='"._("Remove DHCP section")."'>";
331     $editImgInsNoDel = "<input type='image' src='images/list_new.png' name='insertDhcp_%s' title='"._("Insert new DHCP section")."'>".
332       "<input type='image' src='images/edit.png' name='editDhcp_%s' title='"._("Edit DHCP section")."'>";
333     $editImg = "<input type='image' src='images/edit.png' name='editDhcp_%s' title='"._("Edit DHCP section")."'>".
334       "<input type='image' src='images/edittrash.png' name='delDhcp_%s' title='"._("Remove DHCP section")."'>";
335         
336     $tmp = new dhcpNewSectionDialog(NULL);
337     foreach($this->dhcpSections as $section => $values ){
338         
339       if (count($tmp->sectionMap[$this->objectType($section)])){
340         if ($this->objectType($section) == "dhcpService"){
341           $DhcpList->AddEntry(array(
342               array("string" => $values),
343               array("string" => str_replace("%s",base64_encode($section),$editImgInsNoDel), "attach" => "style='text-align:right;'")
344               ));
345         } else {
346           $DhcpList->AddEntry(array(
347               array("string" => $values),
348               array("string" => str_replace("%s",base64_encode($section),$editImgIns), "attach" => "style='text-align:right;'")
349               ));
350         }
351       } else {
352         $DhcpList->AddEntry(array(
353               array("string" => $values),
354               array("string" => str_replace("%s",base64_encode($section),$editImg), "attach" => "style='text-align:right;'")
355               ));
356       }
357     }
359     $smarty->assign("dhcpACL",chkacl($this->acl,"servdhcp"));
361     /* Display tempalte */
362     $smarty->assign("DhcpList",$DhcpList->DrawList());
363     $display.= $smarty->fetch(get_template_path('servdhcp.tpl', TRUE));
364     return($display);
365   }
368   function remove_from_parent()
369   {
370     /* Cancel if there's nothing to do here */
371     if (!$this->initially_was_account){
372       return;
373     }
375     /* Remove subtrees */
376     $ldap= $this->config->get_ldap_link();
377     foreach ($this->dhcpObjectCache as $dn => $content){
378       if ($this->objectType($dn) == 'dhcpService'){
379         $ldap->rmdir_recursive($dn);
380         show_ldap_error($ldap->get_error(), _("Removing DHCP entries failed"));
381       }
382     }
384     /* Remove from self */
385     $ldap= $this->config->get_ldap_link();
387     /* Remove and write to LDAP */
388     plugin::remove_from_parent();
389     
390     @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->attributes, "Save");
391     $ldap->cd($this->dn);
392     $this->cleanup();
393     $ldap->modify ($this->attrs);
395     show_ldap_error($ldap->get_error(), _("Removing DHCP entries failed"));
397     /* Optionally execute a command after we're done */
398     $this->handle_post_events("remove");
399   }
402   /* Save data to object */
403   function save_object()
404   {
405     plugin::save_object();
406   }
409   /* Check supplied data */
410   function check()
411   {
412     /* Call common method to give check the hook */
413     $message= plugin::check();
414     
415     return ($message);
416   }
419   /* Save to LDAP */
420   function save()
421   {
422     /* Take over handling 
423      * - Load servdhcp class and dhcpObjectCache for the source dhcp setup.
424      * - Assign dhcpObjectCache to this configuration. 
425      * - Save this setup and remove source setup from ldap.
426      */
427     if($this->take_over_id != -1){
428       $id = $this->take_over_id;
429       $src = preg_replace("/cn=dhcp,/","",$this->dhcp_server_list['ENTRIES'][$id]['dn']);
430       $tmp = new servdhcp ($this->config, $src);
431       $this->orig_dn = $src;
432       $this->dhcpObjectCache =  $tmp->dhcpObjectCache;
433     }
435     /* Save dhcp setttings */
436     $ldap= $this->config->get_ldap_link();
437     foreach ($this->dhcpObjectCache as $dn => $data){
439       if($this->dn != $this->orig_dn){
440         $dn = preg_replace("/".normalizePreg($this->orig_dn)."$/i",$this->dn,$dn);
441       }
443       /* Remove entry? */
444       if (count($data) == 0){
445         /* Check if exists, then remove... */
446         if($ldap->cat($dn)){
447           $ldap->rmdir_recursive($dn);
448           show_ldap_error($ldap->get_error(), _("Can't remove DHCP object!"));
449         }
450         continue;
451       }
453       /* Modify existing entry? */
454       if (isset($data['MODIFIED']) || $this->orig_dn != $this->dn){
456         if($ldap->cat($dn)){
457           $modify= TRUE;
458         } else {
459           $modify= FALSE;
460         }
462         /* Build new entry */
463         $attrs= array();
464         foreach ($data as $attribute => $values){
465           if ($attribute == "MODIFIED" || $attribute == "dn"){
466             continue;
467           }
468       
469           if(in_array($attribute,array("dhcpPrimaryDN","dhcpSecondaryDN","dhcpServerDN","dhcpFailOverPeerDN"))){
470             foreach($values as $v_key => $value){
471               $values[$v_key] = preg_replace("/".normalizePreg($this->orig_dn)."$/i",$this->dn,$value);
472             }
473           }
475           if (count($values)){
477             if($attribute == "dhcpOption"){
478               foreach($values as $key => $value){
479                 $option_name = trim(preg_replace("/[^ ]*$/","",$value));
480                 $option_value= trim(preg_replace("/^[^ ]*/","",$value));
481                 if(in_array($option_name,$this->quote_option)){
482                   $values[$key] = $option_name." \"".$option_value."\"";
483                 }
484               }
485             }
486             if (count($values) == 1){
487               $attrs[$attribute]= $values[0];
488             } else {
489               $attrs[$attribute]= $values;
490             }
491           } else {
492             if ($modify){
493               $attrs[$attribute]= array();
494             }
495           }
496         }
498         $ldap->cd($dn);
499         if ($modify){
500           $ldap->modify($attrs);
501           show_ldap_error($ldap->get_error(), _("Can't save DHCP object!"));
502         } else {
503           $ldap->add($attrs);
504           show_ldap_error($ldap->get_error(), _("Can't save DHCP object!"));
505         }
506       }
507     }
509     $this->dhcpServiceDN= $this->serviceDN;
510     if($this->dn != $this->orig_dn){
511       $this->dhcpServiceDN= preg_replace("/".normalizePreg($this->orig_dn)."$/i",$this->dn,$this->dhcpServiceDN);
512     }
514     /* Replace 'new' dn */ 
515     if(preg_match("/new$/",$this->dhcpServiceDN)){
516       $this->dhcpServiceDN = preg_replace("/new$/",$this->dn,$this->dhcpServiceDN);
517     }
519     plugin::save();
520     
521     /* Save data to LDAP */
522     $ldap->cd($this->dn);
523     $this->cleanup();
524     $ldap->modify ($this->attrs);
526     show_ldap_error($ldap->get_error(), _("Saving DHCP service failed"));
528     /* Optionally execute a command after we're done */
529     if ($this->initially_was_account == $this->is_account){
530       if ($this->is_modified){
531         $this->handle_post_events("modify");
532       }
533     } else {
534       $this->handle_post_events("add");
535     }
537     /* Take over handling
538      * - Remove old dhcp config from source server 
539      */
540     if($this->take_over_id != -1){
541       $id = $this->take_over_id;
542       $src = $this->dhcp_server_list['ENTRIES'][$id]['dn'];
543       $tmp = new servdhcp ($this->config, $src);
544       $tmp->remove_from_parent();
545     }
546   }
549   function reload()
550   {
551     /* Init LDAP and load list */
552     $ldap= $this->config->get_ldap_link();
553     $ui= get_userinfo();
554     $me= $this->dn;
556     $list= get_list("(&(objectClass=dhcpService)(|(dhcpPrimaryDN=$me)(dhcpSecondaryDN=$me)(dhcpServerDN=$me)(dhcpFailOverPeerDN=$me)))", $ui->subtreeACL, $this->config->current['BASE'], array("cn"));
557     $final= array();
558     foreach ($list as $value){
560       /* Set header */
561       $sortpart= split(",", $value['dn']);
562       $sortpart= array_reverse($sortpart);
563       $tmp= implode(",", $sortpart);
565       $final[$value['dn']]= $tmp."!"._("Global options");
567       /* Read all sub entries to place here */
568       $ldap->cd($value['dn']);
569       $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());
570       $this->serviceDN= $value['dn'];
572       while ($attrs= $ldap->fetch()){
573         $sattrs= array();
574         for ($i= 0; $i<$attrs['count']; $i++){
575           $sattrs[$attrs[$i]]= $attrs[$attrs[$i]];
576           unset($sattrs[$attrs[$i]]['count']);
577         }
578         $sattrs['dn']= $ldap->getDN();
580         foreach($sattrs as $name => $values){
581           if($name == "dhcpOption"){
582             foreach($values as $key => $value){
583               $value_name = trim(preg_replace("/[^ ]*$/","",$value));
584               $value_value= trim(preg_replace("/^[^ ]*/","",$value));
585               if(in_array($value_name,$this->quote_option)){
586                 $value_value = preg_replace("/^\"/","",$value_value);
587                 $value_value = preg_replace("/\"$/","",$value_value);
588                 $sattrs[$name][$key] = $value_name." ".$value_value;
589               }
590             }
591           }
592         }
594         $this->dhcpObjectCache[$ldap->getDN()]= $sattrs;
595         $tmp= preg_replace("/".$this->serviceDN."/", "", $ldap->getDN());
596         $indent= substr_count($tmp, ",");
597         $spaces= "";
598         for ($i= 0; $i<$indent; $i++){
599           $spaces.= "&nbsp;&nbsp;&nbsp;&nbsp;";
600         }
602         foreach ($this->types as $key => $val){
603           if (in_array("$key", $attrs['objectClass'])){
604             $type= $val;
605             break;
606           }
607         }
609         /* Prepare for sorting... */
610         $sortpart= split(",", $ldap->getDN());
611         $sortpart= array_reverse($sortpart);
612         $tmp= implode(",", $sortpart);
613         $final[$ldap->getDN()]= $tmp."!".$spaces.$type." '".$attrs['cn'][0]."'";
614       }
615     }
617     /* Sort it... */
618     natsort($final);
619     $this->dhcpSections= array();
620     foreach ($final as $key => $val){
621       $this->dhcpSections[$key]= preg_replace('/^[^!]+!(.*)$/', '\\1', $val);
622     }
624   }
627   function objectType($dn)
628   {
629     $type= "";
630     $types= array("dhcpService", "dhcpClass", "dhcpSubClass", "dhcpHost",
631                   "dhcpGroup", "dhcpPool", "dhcpSubnet", "dhcpSharedNetwork");
633     foreach ($this->dhcpObjectCache[$dn]['objectClass'] as $oc){
634       if (in_array($oc, $types)){
635         $type= $oc;
636         break;
637       }
638     }
640     /* That should not happen... */
641     if ($type == ""){
642       print_red(_("DHCP configuration set is unknown. Please contact your system administrator."));
643     }
644     
645     return ($type);
646   }
650 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
651 ?>