86f56a7639c4719219b1d63d2e46c17cc5e1384e
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 }
237 function remove_from_parent()
238 {
239 /*
240 $ldap = $this->config->get_ldap_link();
241 $ldap->cd($this->orig_dn);
242 $ldap->search("(&(objectClass=dNSZone)(zoneName=*)(!(relativeDomainName=@)))",array("relativeDomainName","zoneName"));
243 while($attr = $ldap->fetch()){
244 $ldap->cd($attr['dn']);
245 $ldap->rmDir($attr['dn']);
246 show_ldap_error($ldap->get_error(), sprintf(_("Removing of terminal/dns acoount with dn '%s' failed."),$this->dn));
247 }
248 */
249 }
251 /* Save data to object */
252 function save_object()
253 {
254 /* Save all posted vars */
255 plugin::save_object();
257 if(isset($_POST['network_tpl_posted'])){
259 /* Ge all non dns attributes (IP/MAC)*/
260 foreach($this->attributes as $attr){
261 if(isset($_POST[$attr]) && $this->acl_is_writeable($attr)){
262 $this->$attr = $_POST[$attr];
263 }
264 }
266 /* Check if DNS should be enabled / disabled */
267 if($this->DNS_is_account && $this->acl_is_removeable() && !isset($_POST['DNS_is_account'])){
268 $this->DNS_is_account = false;
269 }elseif(!$this->DNS_is_account && $this->acl_is_createable() && isset($_POST['DNS_is_account'])){
270 $this->DNS_is_account = true;
271 }
273 /* Get dns attributes */
274 if(($this->DNSenabled) && (isset($_POST['network_tpl_posted']))){
276 /* Check for posted record changes */
277 if(is_array($this->dnsEntry['RECORDS']) && $this->acl_is_writeable("Records")){
278 foreach($this->dnsEntry['RECORDS'] as $key => $value){
280 /* Check if type has changed */
281 if(isset($_POST['RecordTypeSelectedFor_'.$key])){
282 $this->dnsEntry['RECORDS'][$key]['type'] = $_POST['RecordTypeSelectedFor_'.$key];
283 }
284 /* Check if value has changed */
285 if(isset($_POST['RecordValue_'.$key])){
286 $this->dnsEntry['RECORDS'][$key]['value'] = $_POST['RecordValue_'.$key];
287 }
288 }
289 }
290 /* Get all basic DNS attributes (TTL, Clas ..)*/
291 foreach($this->DNSattributes as $attr){
292 if(isset($_POST[$attr]) && $this->acl_is_writeable($attr)){
293 $this->dnsEntry[$attr] = $_POST[$attr];
294 }
295 }
298 }
299 }
300 }
303 /* Check supplied data */
304 function check()
305 {
306 /* Call common method to give check the hook */
307 $message= plugin::check();
309 /* Check if ip must be given
310 */
311 if(($this->IPisMust)||($this->DNS_is_account)){
313 /* Check if ip is empty
314 */
315 if ($this->ipHostNumber == "" && $this->acl_is_writeable("ipHostNumber")){
316 $message[]= _("The required field 'IP-address' is not set.");
317 }
318 }
321 /* check if given ip is valid ip */
322 if ($this->ipHostNumber != "" && !is_ip($this->ipHostNumber)){
323 $message[]= _("Wrong IP format in field IP-address.");
324 }
326 /* Check if mac is empty
327 */
328 if ($this->macAddress == "" && $this->acl_is_writeable("macAddress")){
329 $message[]= _("The required field 'MAC-address' is not set.");
330 }
332 /* Check if given mac is valid mac
333 */
334 $tr = count(split(":",$this->macAddress));
335 if(!is_mac($this->macAddress)){
336 $message[]=(_("The given macaddress is invalid. There must be 6 2byte segments seperated by ':'."));
337 }
339 /* only perfrom this checks if this is a valid DNS account */
340 if($this->DNS_is_account){
342 $checkArray = array();
343 $onlyOnce = array();
345 // $onlyOnce['cNAMERecord'] = 0;
347 /* Walk through all entries and detect duplicates or mismatches
348 */
349 foreach($this->dnsEntry['RECORDS'] as $name => $values){
351 /* Count record values, to detect duplicate entries for a specific record
352 */
353 if(!isset($checkArray[$values['type']][$values['value']])){
354 $checkArray[$values['type']][$values['value']] = 0;
355 }else{
356 $message[] = sprintf(_("Found duplicate value for record type '%s'."),$values['type']);
357 }
359 /* Check if given entries in $onlyOnce are used more than once
360 */
361 if(isset($onlyOnce[$values['type']])){
362 $onlyOnce[$values['type']] ++;
363 if($onlyOnce[$values['type']] > 1){
364 $message[] = sprintf(_("Found more than one entry for the uniqe record type '%s'."),$values['type']);
365 }
366 }
368 /* Skip txt record ...
369 */
370 if($values['type'] == "tXTRecord") continue;
372 /* Check if there is an aRecord defined which uses the same IP as used in IPhostAddress
373 */
374 if(($values['type'] == "aRecord")&&($values['value'] == $this->ipHostNumber)){
375 $message[]=sprintf(_("The device IP '%s' is added as 'A Record', this will be done automatically, please remove the record."),
376 $this->ipHostNumber);
377 }
379 /* only lower-case is allowed in record entries ...
380 */
381 if($values['value'] != strtolower($values['value'])){
382 $message[] = sprintf(_("Only lowercase is allowed, please check your '%ss'."),$values['type']);
383 }
384 }
385 }
386 return ($message);
387 }
390 /* Save to LDAP */
391 function save($dn)
392 {
393 $ldap= $this->config->get_ldap_link();
395 /*******************/
396 /* IP-MAC HANDLING */
397 /*******************/
399 /* $dn was posted as parameter */
400 $this->dn = $dn;
402 /* Save DNS setting & ip/Mac*/
403 plugin::save();
405 /* Write back to ldap */
406 $ldap->cd($this->dn);
407 $this->cleanup();
408 $ldap->modify ($this->attrs);
410 /****************/
411 /* DNS HANDLING */
412 /****************/
414 /* If isn't DNS account but initially was DNS account
415 remove all DNS entries
416 */
417 if((!$this->DNSenabled) || ((!$this->DNS_is_account)&&(!$this->initially_was_account))){
418 return;
419 }else{
421 /* Add ipHostNumber to aRecords
422 */
423 $this->dnsEntry['RECORDS'][] = array("type"=>"aRecord","value"=>$this->ipHostNumber);
425 /* Create diff and follow instructions
426 * If Account was disabled, remove account by setting exists to false
427 */
428 if((!$this->DNS_is_account)&&($this->initially_was_account)){
429 $this->dnsEntry['exists'] = false;
430 $tmp = getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
431 }else{
432 $this->dnsEntry['exists'] = $this->DNS_is_account;
433 $tmp = getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
434 }
436 /* move follwoing entries
437 */
438 foreach($tmp['move'] as $src => $dst){
439 $this->recursive_move($src,$dst);
440 }
442 /* Delete dns */
443 foreach($tmp['del'] as $dn => $del){
444 $ldap->cd($dn);
445 $ldap->rmdir_recursive($dn);
446 }
448 /* Add || Update new DNS entries
449 */
450 foreach($tmp['add'] as $dn => $attrs){
451 $ldap->cd($dn);
452 $ldap->cat($dn, array('dn'));
453 if(count($ldap->fetch())){
454 $ldap->cd($dn);
455 $ldap->modify ($attrs);
456 }else{
457 $ldap->cd($dn);
458 $ldap->add($attrs);
459 }
460 }
462 /* Display errors
463 */
464 if($ldap->get_error() != "Success"){
465 show_ldap_error($ldap->get_error(), sprintf(_("Saving of terminal/dns account with dn '%s' failed."),$this->dn));
466 }
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 ?>