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;
12 var $autonet = false;
14 /* Basic informations
15 */
16 var $attributes = array("ipHostNumber","macAddress");
17 var $objectclasses = array("whatever");
19 var $ipHostNumber = ""; // IP address
20 var $macAddress = ""; // Mac address
21 var $cn = ""; // CN of currently edited device
22 var $OrigCn = ""; // Initial cn
23 var $IPisMust = false;
24 var $MACisMust = false;
26 /* DNS attributes
27 */
28 var $DNSattributes = array("dNSClass","zoneName","dNSTTL");
29 var $DNS_is_account = false;
30 var $initially_was_account = false;
31 var $dnsEntry = array();
32 var $DNSenabled = false;
34 /* Terminal dns
35 */
36 function termDNS ($config, $dn,$objectClasses,$IPisMust = false)
37 {
38 /* We need to know which objectClasses are used, to store the ip/mac
39 * Because of different type of devices
40 */
41 $this->objectclasses = $objectClasses;
42 $this->IPisMust = $IPisMust;
44 plugin::plugin ($config, $dn);
46 if(isset($this->attrs['cn'][0])){
47 $this->OrigCn = $this->attrs['cn'][0];
48 $this->cn = $this->attrs['cn'][0];
49 }
51 /* Do we have autonet support? */
52 if (isset($this->config->data['MAIN']['AUTO_NETWORK_HOOK'])){
53 $this->autonet= true;
54 }
56 /* Hide all dns specific code, if dns is not available
57 */
58 $DNSenabled = false;
59 foreach($this->config->data['TABS']['SERVERSERVICE'] as $tab){
60 if(preg_match("/^servdns$/",$tab['CLASS'])){
61 $this->DNSenabled = true;
62 }
63 }
64 if(!$this->DNSenabled){
65 $this->DNS_is_account = false;
66 return;
67 }
69 if($this->DNSenabled){
71 /* Get Zones
72 */
73 $this->Zones = getAvailableZones($config);
75 /* Get Entry
76 */
77 $this->dnsEntry = getDNSHostEntries($config,$this->OrigCn);
79 /* Remove A record which equals $this->ipHostNumber
80 */
81 foreach($this->dnsEntry['RECORDS'] as $key => $rec){
82 if(($rec['type'] == "aRecord") && ($rec['value'] == $this->ipHostNumber)){
83 unset($this->dnsEntry['RECORDS'][$key]);
84 }
85 }
87 /* Get Record types
88 */
89 $this->RecordTypes = getDnsRecordTypes();
91 /* If there is at least one entry in this -> types, we have DNS enabled
92 */
93 if($this->dnsEntry['exists']){
94 $this->DNS_is_account = true;
95 }else{
96 $this->DNS_is_account = false;
97 }
98 }
100 /* Store initally account settings
101 */
102 $this->initially_was_account = $this->DNS_is_account;
103 }
106 function getVarsForSaving($attrs)
107 {
108 foreach($this->attributes as $attr){
109 if(!empty($this->$attr)){
110 $attrs[$attr] = $this->$attr;
111 }
112 }
113 return($attrs);
114 }
116 function execute()
117 {
118 /* Call parent execute */
119 $smarty= get_smarty();
121 $tmp = $this->plInfo();
122 foreach($tmp['plProvidedAcls'] as $name => $translation){
123 $smarty->assign($name."ACL",$this->getacl($name));
124 }
126 $display= "";
128 $smarty->assign("staticAddress", "");
129 $smarty->assign("autonet", "");
131 /* Check for autonet button */
132 if ($this->autonet && isset($_POST['autonet'])){
133 $cmd= $this->config->data['MAIN']['AUTO_NETWORK_HOOK'];
134 if(!empty($cmd) && $this->cn != ""){
135 $res = shell_exec($cmd." ".$this->cn);
136 if(!$res){
137 print_red(sprintf(_("Can't execute specified AUTO_NETWORK_HOOK '%s'. Please check your gosa.conf."),$cmd));
138 } else {
139 $res= split(';', trim($res));
140 if (isset($res[0]) && $res[0] != ""){
141 $this->ipHostNumber= $res[0];
142 }
143 if (isset($res[1]) && $res[1] != ""){
144 $this->macAddress= $res[1];
145 }
146 }
147 }
148 }
150 /* There is no dns available
151 */
152 if($this->DNSenabled == false){
154 /* Is IP address must ? */
155 $smarty->assign("DNS_enabled",false);
156 $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
158 /* Assign smarty all non DNs attributes */
159 foreach($this->attributes as $attr){
160 $smarty->assign($attr,$this->$attr);
161 }
162 $smarty->assign("staticAddress","<font class=\"must\">*</font>");
164 if ($this->autonet){
165 $smarty->assign("autonet", "true");
166 } else {
167 $smarty->assign("autonet", "");
168 }
169 $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
170 return($display);
171 }else{
172 $smarty->assign("DNS_enabled",true);
173 }
175 $accountACL = false;
176 if($this->DNS_is_account && $this->acl_is_removeable()){
177 $accountACL = true;
178 }elseif(!$this->DNS_is_account && $this->acl_is_createable()){
179 $accountACL = true;
180 }
181 $smarty->assign("accountACL",$accountACL);
183 /* Add new empty array to our record list */
184 if(isset($_POST['AddNewRecord'])){
185 $this->dnsEntry['RECORDS'][] =array("type"=>"aRecord","value"=>"");
186 }
188 /* Handle all posts */
189 $only_once =true;
190 foreach($_POST as $name => $value){
192 /* Check if we have to delete a record entry */
193 if((preg_match("/RemoveRecord_/",$name))&&($only_once)) {
195 /* Avoid performing this once again */
196 $only_once = false;
198 /* Extract id for specified entry */
199 $id = preg_replace("/RemoveRecord_/","",$name);
200 $id = preg_replace("/_.*$/","",$id);
202 /* Delete this record, mark edited entries to be able to delete them */
203 if(isset($this->dnsEntry['RECORDS'][$id])){
204 unset($this->dnsEntry['RECORDS'][$id]);
205 }
206 }
207 }
209 /* Assign smarty all non DNs attributes */
210 foreach($this->attributes as $attr){
211 $smarty->assign($attr,$this->$attr);
212 }
214 /* Assign smarty all DNS attributes */
215 foreach($this->DNSattributes as $attr){
216 $smarty->assign($attr,$this->dnsEntry[$attr]);
217 }
219 /* Assign all needed vars */
220 $smarty->assign("DNS_is_account",$this->DNS_is_account);
221 $smarty->assign("Zones",$this->Zones);
222 $smarty->assign("ZoneKeys",($this->Zones));
223 $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
225 $tmp = $this->generateRecordsList();
227 $changeStateForRecords = $tmp['changeStateForRecords'];
229 $smarty->assign("records",$tmp['str']);
230 $smarty->assign("changeStateForRecords",$changeStateForRecords);
231 $smarty->assign("staticAddress","<font class=\"must\">*</font>");
233 $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
234 return($display);
235 }
238 function remove_from_parent()
239 {
240 if($this->initially_was_account){
242 $ldap = $this->config->get_ldap_link();
244 $tmp = array();
245 $this->dnsEntry['exists'] = false;
246 $tmp = getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
248 /* Delete dns */
249 foreach($tmp['del'] as $dn => $del){
250 $ldap->cd($dn);
251 $ldap->rmdir_recursive($dn);
252 }
253 }
254 }
257 /* Save data to object */
258 function save_object()
259 {
260 /* Save all posted vars */
261 plugin::save_object();
263 if(isset($_POST['network_tpl_posted'])){
265 /* Ge all non dns attributes (IP/MAC)*/
266 foreach($this->attributes as $attr){
267 if(isset($_POST[$attr]) && $this->acl_is_writeable($attr)){
268 $this->$attr = $_POST[$attr];
269 }
270 }
272 /* Check if DNS should be enabled / disabled */
273 if($this->DNS_is_account && $this->acl_is_removeable() && !isset($_POST['DNS_is_account'])){
274 $this->DNS_is_account = false;
275 }elseif(!$this->DNS_is_account && $this->acl_is_createable() && isset($_POST['DNS_is_account'])){
276 $this->DNS_is_account = true;
277 }
279 /* Get dns attributes */
280 if(($this->DNSenabled) && (isset($_POST['network_tpl_posted']))){
282 /* Check for posted record changes */
283 if(is_array($this->dnsEntry['RECORDS']) && $this->acl_is_writeable("Records")){
284 foreach($this->dnsEntry['RECORDS'] as $key => $value){
286 /* Check if type has changed */
287 if(isset($_POST['RecordTypeSelectedFor_'.$key])){
288 $this->dnsEntry['RECORDS'][$key]['type'] = $_POST['RecordTypeSelectedFor_'.$key];
289 }
290 /* Check if value has changed */
291 if(isset($_POST['RecordValue_'.$key])){
292 $this->dnsEntry['RECORDS'][$key]['value'] = $_POST['RecordValue_'.$key];
293 }
294 }
295 }
296 /* Get all basic DNS attributes (TTL, Clas ..)*/
297 foreach($this->DNSattributes as $attr){
298 if(isset($_POST[$attr]) && $this->acl_is_writeable($attr)){
299 $this->dnsEntry[$attr] = $_POST[$attr];
300 }
301 }
304 }
305 }
306 }
309 /* Check supplied data */
310 function check()
311 {
312 /* Call common method to give check the hook */
313 $message= plugin::check();
315 /* Check if ip must be given
316 */
317 if(($this->IPisMust)||($this->DNS_is_account)){
318 if (empty($this->ipHostNumber)){
319 $message[]= _("The required field 'IP-address' is not set.");
320 }
322 if (!is_ip($this->ipHostNumber)){
323 $message[]= _("Wrong IP format in field IP-address.");
324 }
325 }
327 /* Check if mac is empty
328 */
329 if ($this->macAddress == "" ){
330 $message[]= _("The required field 'MAC-address' is not set.");
331 }
332 if(!is_mac($this->macAddress)){
333 $message[]=(_("The given macaddress is invalid. There must be 6 2byte segments seperated by ':'."));
334 }
336 /* only perfrom this checks if this is a valid DNS account */
337 if($this->DNS_is_account){
339 $checkArray = array();
340 $onlyOnce = array();
342 // $onlyOnce['cNAMERecord'] = 0;
344 /* Walk through all entries and detect duplicates or mismatches
345 */
346 foreach($this->dnsEntry['RECORDS'] as $name => $values){
348 /* Count record values, to detect duplicate entries for a specific record
349 */
350 if(!isset($checkArray[$values['type']][$values['value']])){
351 $checkArray[$values['type']][$values['value']] = 0;
352 }else{
353 $message[] = sprintf(_("Found duplicate value for record type '%s'."),$values['type']);
354 }
356 /* Check if given entries in $onlyOnce are used more than once
357 */
358 if(isset($onlyOnce[$values['type']])){
359 $onlyOnce[$values['type']] ++;
360 if($onlyOnce[$values['type']] > 1){
361 $message[] = sprintf(_("Found more than one entry for the uniqe record type '%s'."),$values['type']);
362 }
363 }
365 /* Skip txt record ...
366 */
367 if($values['type'] == "tXTRecord") continue;
369 /* Check if there is an aRecord defined which uses the same IP as used in IPhostAddress
370 */
371 if(($values['type'] == "aRecord")&&($values['value'] == $this->ipHostNumber)){
372 $message[]=sprintf(_("The device IP '%s' is added as 'A Record', this will be done automatically, please remove the record."),
373 $this->ipHostNumber);
374 }
376 /* only lower-case is allowed in record entries ...
377 */
378 if($values['value'] != strtolower($values['value'])){
379 $message[] = sprintf(_("Only lowercase is allowed, please check your '%ss'."),$values['type']);
380 }
381 }
382 }
383 return ($message);
384 }
387 /* Save to LDAP */
388 function save($dn)
389 {
390 $ldap= $this->config->get_ldap_link();
392 /*******************/
393 /* IP-MAC HANDLING */
394 /*******************/
396 /* $dn was posted as parameter */
397 $this->dn = $dn;
399 /* Save DNS setting & ip/Mac*/
400 plugin::save();
402 /* Write back to ldap */
403 $ldap->cd($this->dn);
404 $this->cleanup();
405 $ldap->modify ($this->attrs);
407 /****************/
408 /* DNS HANDLING */
409 /****************/
411 /* If isn't DNS account but initially was DNS account
412 remove all DNS entries
413 */
414 if((!$this->DNSenabled) || ((!$this->DNS_is_account)&&(!$this->initially_was_account))){
415 return;
416 }else{
418 /* Add ipHostNumber to aRecords
419 */
420 $backup_dnsEntry = $this->dnsEntry;
421 $this->dnsEntry['RECORDS'][] = array("type"=>"aRecord","value"=>$this->ipHostNumber);
423 /* Create diff and follow instructions
424 * If Account was disabled, remove account by setting exists to false
425 */
426 if((!$this->DNS_is_account)&&($this->initially_was_account)){
427 $this->dnsEntry['exists'] = false;
428 $tmp = getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
429 }else{
430 $this->dnsEntry['exists'] = $this->DNS_is_account;
431 $tmp = getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
432 }
434 /* move follwoing entries
435 */
436 foreach($tmp['move'] as $src => $dst){
437 $this->recursive_move($src,$dst);
438 }
440 /* Delete dns */
441 foreach($tmp['del'] as $dn => $del){
442 $ldap->cd($dn);
443 $ldap->rmdir_recursive($dn);
444 }
446 /* Add || Update new DNS entries
447 */
448 foreach($tmp['add'] as $dn => $attrs){
449 $ldap->cd($dn);
450 $ldap->cat($dn, array('dn'));
451 if(count($ldap->fetch())){
452 $ldap->cd($dn);
453 $ldap->modify ($attrs);
454 }else{
455 $ldap->cd($dn);
456 $ldap->add($attrs);
457 }
458 }
460 /* Display errors
461 */
462 if($ldap->get_error() != "Success"){
463 show_ldap_error($ldap->get_error(), sprintf(_("Saving of terminal/dns account with dn '%s' failed."),$this->dn));
464 }
466 $this->dnsEntry = $backup_dnsEntry;
467 }
468 }
470 /* Create html table with all used record types
471 */
472 function generateRecordsList()
473 {
474 $changeStateForRecords = "";
476 if(!$this->DNS_is_account) {
477 $str = "<input type='submit' value='"._("Add")."' name='AddNewRecord' id='AddNewRecord' disabled='disabled'>";
478 return $str;
479 }
481 $str = "<table summary='' width='100%'>";
482 foreach($this->dnsEntry['RECORDS'] as $key => $entry){
484 $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n";
485 $changeStateForRecords.= "changeState('RecordValue_".$key."');\n";
486 $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n";
488 $str.=" <tr>".
489 " <td>".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."</td>".
490 " <td><input type='text' value='".$entry['value']."' name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
491 " <td><input type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
492 "</tr>";
493 }
495 $str.= " <tr>".
496 " <td colspan=2 width='50%'></td><td>".
497 " <input type='submit' value='"._("Add")."' name='AddNewRecord' id='AddNewRecord'>".
498 " </td>".
499 " </tr>".
500 "</table>";
501 $ret = array("str" => $str, "changeStateForRecords" => $changeStateForRecords);
502 return($ret);
503 }
506 /* Create a html select box which allows us to select different types of records
507 */
508 function generateRecordListBox($selected,$name)
509 {
510 $str = "<select name='".$name."' id='".$name."'>";
511 foreach($this->RecordTypes as $type => $value){
512 $use = "";
513 if($type == $selected){
514 $use = " selected ";
515 }
516 $str.="\n <option value='".$type."' ".$use.">".strtoupper(preg_replace("/record/i","",$type))."</option>";
517 }
518 $str.="</select>";
519 return($str);
520 }
523 /* Return plugin informations for acl handling */
524 function plInfo()
525 {
526 $tmp = array(
527 "plShortName" => _("DNS"),
528 "plDescription" => _("DNS settings"),
529 "plSelfModify" => FALSE,
530 "plDepends" => array(),
531 "plPriority" => 5,
532 "plSection" => array("administration"),
533 "plCategory" => array("workstation","terminal","phone","server","component","printer","winworkstation"),
535 "plProvidedAcls"=> array(
536 "ipHostNumber" => _("IP address"),
537 "macAddress" => _("MAC address"))
538 );
540 /* Hide all dns specific code, if dns is not available
541 */
542 $DNSenabled = false;
543 foreach($_SESSION['config']->data['TABS']['SERVERSERVICE'] as $tab){
544 if(preg_match("/^servdns$/",$tab['CLASS'])){
545 $tmp['plProvidedAcls']["Records"] = _("DNS records");
546 $tmp['plProvidedAcls']["zoneName"] = _("Zone name");
547 $tmp['plProvidedAcls']["dNSTTL"] = _("TTL");
548 }
549 }
550 return($tmp);
551 }
552 }
554 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
555 ?>