06af668ebef3a149dd66c28334885c5c0e92c73f
1 <?php
3 class termDNS extends plugin
4 {
5 /* CLI vars */
6 var $cli_summary = "Manage server basic objects";
7 var $cli_description = "Some longer text\nfor help";
8 var $cli_parameters = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10 /* attribute list for save action */
11 var $ignore_account = TRUE;
13 /* Basic informations
14 */
15 var $attributes = array("ipHostNumber","macAddress");
16 var $objectclasses = array("whatever");
18 var $ipHostNumber = ""; // IP address
19 var $macAddress = ""; // Mac address
20 var $cn = ""; // CN of currently edited device
21 var $OrigCn = ""; // Initial cn
22 var $IPisMust = false;
23 var $MACisMust = false;
25 /* DNS attributes
26 */
27 var $DNSattributes = array("dNSClass","zoneName","dNSTTL");
28 var $DNS_is_Account = false;
29 var $DNSinitially_was_account = false;
30 var $dnsEntry = array();
31 var $DNSenabled = false;
33 /* Terminal dns
34 */
35 function termDNS ($config, $dn,$objectClasses,$IPisMust = false)
36 {
37 /* We need to know which objectClasses are used, to store the ip/mac
38 * Because of different type of devices
39 */
40 $this->objectclasses = $objectClasses;
41 $this->IPisMust = $IPisMust;
43 plugin::plugin ($config, $dn);
45 if(isset($this->attrs['cn'][0])){
46 $this->OrigCn = $this->attrs['cn'][0];
47 }
49 /* Hide all dns specific code, if dns is not available
50 */
51 $DNSenabled = false;
52 foreach($this->config->data['TABS']['SERVTABS'] as $tab){
53 if(preg_match("/^servdns$/",$tab['CLASS'])){
54 $this->DNSenabled = true;
55 }
56 }
57 if(!$this->DNSenabled){
58 $this->DNS_is_account = false;
59 return;
60 }
62 if($this->DNSenabled){
64 /* Get Zones
65 */
66 $this->Zones = getAvailableZones($config);
68 /* Get Entry
69 */
70 $this->dnsEntry = getDNSHostEntries($config,$this->OrigCn);
72 /* Remove A record which equals $this->ipHostNumber
73 */
74 foreach($this->dnsEntry['RECORDS'] as $key => $rec){
75 if(($rec['type'] == "aRecord") && ($rec['value'] == $this->ipHostNumber)){
76 unset($this->dnsEntry['RECORDS'][$key]);
77 }
78 }
80 /* Get Record types
81 */
82 $this->RecordTypes = getDnsRecordTypes();
84 /* If there is at least one entry in this -> types, we have DNS enabled
85 */
86 if($this->dnsEntry['exists']){
87 $this->DNS_is_account = true;
88 }else{
89 $this->DNS_is_account = false;
90 }
92 }
94 /* Store initally account settings
95 */
96 $this->DNSinitially_was_account = $this->DNS_is_account;
97 }
100 function getVarsForSaving($attrs)
101 {
102 foreach($this->attributes as $attr){
103 if(!empty($this->$attr)){
104 $attrs[$attr] = $this->$attr;
105 }
106 }
107 return($attrs);
108 }
110 function execute()
111 {
112 /* Call parent execute */
113 $smarty= get_smarty();
114 $display= "";
116 $smarty->assign("staticAddress", "");
118 /* There is no dns available
119 */
120 if($this->DNSenabled == false){
122 /* Is IP address must ? */
123 $smarty->assign("DNS_is_account",false);
124 $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
126 /* Assign smarty all non DNs attributes */
127 foreach($this->attributes as $attr){
128 $smarty->assign($attr,$this->$attr);
129 }
130 $smarty->assign("staticAddress","<font class=\"must\">*</font>");
131 $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
132 return($display);
133 }else{
134 $smarty->assign("DNS_is_account",true);
135 }
137 /* Add new empty array to our record list */
138 if(isset($_POST['AddNewRecord'])){
139 $this->dnsEntry['RECORDS'][] =array("type"=>"aRecord","value"=>"");
140 }
142 /* Handle all posts */
143 $only_once =true;
144 foreach($_POST as $name => $value){
146 /* Check if we have to delete a record entry */
147 if((preg_match("/RemoveRecord_/",$name))&&($only_once)) {
149 /* Avoid performing this once again */
150 $only_once = false;
152 /* Extract id for specified entry */
153 $id = preg_replace("/RemoveRecord_/","",$name);
154 $id = preg_replace("/_.*$/","",$id);
156 /* Delete this record, mark edited entries to be able to delete them */
157 if(isset($this->dnsEntry['RECORDS'][$id])){
158 unset($this->dnsEntry['RECORDS'][$id]);
159 }
160 }
161 }
163 /* Assign smarty all non DNs attributes */
164 foreach($this->attributes as $attr){
165 $smarty->assign($attr,$this->$attr);
166 }
168 /* Assign smarty all DNS attributes */
169 foreach($this->DNSattributes as $attr){
170 $smarty->assign($attr,$this->dnsEntry[$attr]);
171 }
173 /* Assign all needed vars */
174 $smarty->assign("DNSAccount",$this->DNS_is_account);
175 $smarty->assign("Zones",$this->Zones);
176 $smarty->assign("ZoneKeys",($this->Zones));
177 $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
179 $tmp = $this->generateRecordsList();
181 $changeStateForRecords = $tmp['changeStateForRecords'];
183 $smarty->assign("records",$tmp['str']);
184 $smarty->assign("changeStateForRecords",$changeStateForRecords);
185 $smarty->assign("staticAddress","<font class=\"must\">*</font>");
187 $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
188 return($display);
189 }
191 function remove_from_parent()
192 {
193 /*
194 $ldap = $this->config->get_ldap_link();
195 $ldap->cd($this->orig_dn);
196 $ldap->search("(&(objectClass=dNSZone)(zoneName=*)(!(relativeDomainName=@)))",array("relativeDomainName","zoneName"));
197 while($attr = $ldap->fetch()){
198 $ldap->cd($attr['dn']);
199 $ldap->rmDir($attr['dn']);
200 show_ldap_error("Record:".$ldap->get_error(), _("Removing terminal from DNS object failed"));
201 }
202 */
203 }
205 /* Save data to object */
206 function save_object()
207 {
208 /* Save all posted vars */
209 plugin::save_object();
211 /* Ge all non dns attributes (IP/MAC)*/
212 foreach($this->attributes as $attr){
213 if(isset($_POST[$attr])){
214 $this->$attr = $_POST[$attr];
215 }
216 }
218 /* Get dns attributes */
219 if(($this->DNSenabled) && (isset($_POST['network_tpl_posted']))){
221 /* Check for posted record changes */
222 if(is_array($this->dnsEntry['RECORDS'])){
223 foreach($this->dnsEntry['RECORDS'] as $key => $value){
225 /* Check if type has changed */
226 if(isset($_POST['RecordTypeSelectedFor_'.$key])){
227 $this->dnsEntry['RECORDS'][$key]['type'] = $_POST['RecordTypeSelectedFor_'.$key];
228 }
229 /* Check if value has changed */
230 if(isset($_POST['RecordValue_'.$key])){
231 $this->dnsEntry['RECORDS'][$key]['value'] = $_POST['RecordValue_'.$key];
232 }
233 }
234 }
235 /* Get all basic DNS attributes (TTL, Clas ..)*/
236 foreach($this->DNSattributes as $attr){
237 if(isset($_POST[$attr])){
238 $this->dnsEntry[$attr] = $_POST[$attr];
239 }
240 }
242 /* Enable diable DNS */
243 if(isset($_POST['enableDNS'])){
244 $this->DNS_is_account = true;
245 }else{
246 $this->DNS_is_account = false;
247 }
248 }
249 }
252 /* Check supplied data */
253 function check()
254 {
255 /* Call common method to give check the hook */
256 $message= plugin::check();
258 /* Check if ip must be given
259 */
260 if(($this->IPisMust)||($this->DNS_is_account)){
262 /* Check if ip is empty
263 */
264 if ($this->ipHostNumber == "" && chkacl ($this->acl, "ipHostNumber") == ""){
265 $message[]= _("The required field 'IP-address' is not set.");
266 }
268 /* check if given ip is valid ipi
269 */
270 $num="(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])";
271 if (!preg_match("/^$num\\.$num\\.$num\\.$num$/", $this->ipHostNumber)){
272 $message[]= _("Wrong IP format in field IP-address.");
273 }
274 }
276 /* Check if mac is empty
277 */
278 if ($this->macAddress == "" && chkacl ($this->acl, "macAddress") == ""){
279 $message[]= _("The required field 'MAC-address' is not set.");
280 }
282 /* Check if given mac is valid mac
283 */
284 $tr = count(split(":",$this->macAddress));
285 if($tr!=6){
286 $message[]=(_("The given macaddress is invalid. There must be 6 2byte segments seperated by ':'."));
287 }
289 /* only perfrom this checks if this is a valid DNS account */
290 if($this->DNS_is_account){
292 $checkArray = array();
293 $onlyOnce = array();
295 $onlyOnce['cNAMERecord'] = 0;
297 /* Walk through all entries and detect duplicates or mismatches
298 */
299 foreach($this->dnsEntry['RECORDS'] as $name => $values){
301 /* Count record values, to detect duplicate entries for a specific record
302 */
303 if(!isset($checkArray[$values['type']][$values['value']])){
304 $checkArray[$values['type']][$values['value']] = 0;
305 }else{
306 $message[] = sprintf(_("Found duplicate value for record type '%s'."),$values['type']);
307 }
309 /* Check if given entries in $onlyOnce are used more than once
310 */
311 if(isset($onlyOnce[$values['type']])){
312 $onlyOnce[$values['type']] ++;
313 if($onlyOnce[$values['type']] > 1){
314 $message[] = sprintf(_("Found more than one entry for the uniqe record type '%s'."),$values['type']);
315 }
316 }
318 /* Skip txt record ...
319 */
320 if($values['type'] == "tXTRecord") continue;
322 /* Check if there is an aRecord defined which uses the same IP as used in IPhostAddress
323 */
324 if(($values['type'] == "aRecord")&&($values['value'] == $this->ipHostNumber)){
325 $message[]=sprintf(_("The device IP '%s' is added as 'A Record', this will be done automatically, please remove the record."),
326 $this->ipHostNumber);
327 }
329 /* only lower-case is allowed in record entries ...
330 */
331 if($values['value'] != strtolower($values['value'])){
332 $message[] = sprintf(_("Only lowercase is allowed, please check your '%ss'."),$values['type']);
333 }
334 }
335 }
336 return ($message);
337 }
340 /* Save to LDAP */
341 function save($dn)
342 {
343 $ldap= $this->config->get_ldap_link();
345 /*******************/
346 /* IP-MAC HANDLING */
347 /*******************/
349 /* $dn was posted as parameter */
350 $this->dn = $dn;
352 /* Save DNS setting & ip/Mac*/
353 plugin::save();
355 /* Write back to ldap */
356 $ldap->cd($this->dn);
357 $this->cleanup();
358 $ldap->modify ($this->attrs);
360 /****************/
361 /* DNS HANDLING */
362 /****************/
364 /* If isn't DNS account but initially was DNS account
365 remove all DNS entries
366 */
367 if(!$this->DNSenabled){
368 return;
369 }else{
371 /* Add ipHostNumber to aRecords
372 */
373 $this->dnsEntry['RECORDS'][] = array("type"=>"aRecord","value"=>$this->ipHostNumber);
375 /* Create diff and follow instructions
376 * If Account was disabled, remove account by setting exists to false
377 */
378 if((!$this->DNS_is_account)&&($this->DNSinitially_was_account)){
379 $this->dnsEntry['exists'] = false;
380 $tmp = getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
381 }else{
382 $this->dnsEntry['exists'] = $this->DNS_is_account;
383 $tmp = getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
384 }
386 /* move follwoing entries
387 */
388 foreach($tmp['move'] as $src => $dst){
389 $this->recursive_move($src,$dst);
390 }
392 /* Delete dns */
393 foreach($tmp['del'] as $dn => $del){
394 $ldap->cd($dn);
395 $ldap->rmdir_recursive($dn);
396 }
398 /* Add || Update new DNS entries
399 */
400 foreach($tmp['add'] as $dn => $attrs){
401 $ldap->cd($dn);
402 $ldap->cat($dn, array('dn'));
403 if(count($ldap->fetch())){
404 $ldap->cd($dn);
405 $ldap->modify ($attrs);
406 }else{
407 $ldap->cd($dn);
408 $ldap->add($attrs);
409 }
410 }
412 /* Display errors
413 */
414 if($ldap->get_error() != "Success"){
415 show_ldap_error("Record:".$ldap->get_error(), _("Saving terminal to DNS object failed"));
416 }
417 }
418 }
420 /* Create html table with all used record types
421 */
422 function generateRecordsList()
423 {
424 $changeStateForRecords = "";
426 if(!$this->DNS_is_account) {
427 $str = "<input type='submit' value='"._("Add")."' name='AddNewRecord' id='AddNewRecord' disabled='disabled'>";
428 return $str;
429 }
431 $str = "<table summary='' width='100%'>";
432 foreach($this->dnsEntry['RECORDS'] as $key => $entry){
434 $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n";
435 $changeStateForRecords.= "changeState('RecordValue_".$key."');\n";
436 $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n";
438 $str.=" <tr>".
439 " <td>".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."</td>".
440 " <td><input type='text' value='".$entry['value']."' name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
441 " <td><input type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
442 "</tr>";
443 }
445 $str.= " <tr>".
446 " <td colspan=2 width='50%'></td><td>".
447 " <input type='submit' value='"._("Add")."' name='AddNewRecord'>".
448 " </td>".
449 " </tr>".
450 "</table>";
451 $ret = array("str" => $str, "changeStateForRecords" => $changeStateForRecords);
452 return($ret);
453 }
456 /* Create a html select box which allows us to select different types of records
457 */
458 function generateRecordListBox($selected,$name)
459 {
460 $str = "<select name='".$name."' id='".$name."'>";
461 foreach($this->RecordTypes as $type => $value){
462 $use = "";
463 if($type == $selected){
464 $use = " selected ";
465 }
466 $str.="\n <option value='".$type."' ".$use.">".strtoupper(preg_replace("/record/i","",$type))."</option>";
467 }
468 $str.="</select>";
469 return($str);
470 }
471 }
473 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
474 ?>