4439e903defdf2438beeb025e7ed3f928d33c4d6
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 $this->OrigCn = $this->attrs['cn'][0];
48 /* Hide all dns specific code, if dns is not available
49 */
50 $DNSenabled = false;
51 foreach($this->config->data['TABS']['SERVTABS'] as $tab){
52 if(preg_match("/^servdns$/",$tab['CLASS'])){
53 $this->DNSenabled = true;
54 }
55 }
56 if(!$this->DNSenabled){
57 $this->DNS_is_account = false;
58 return;
59 }
61 /* Get Zones
62 */
63 $this->Zones = getAvailableZones($config);
65 /* Get Entry
66 */
67 $this->dnsEntry = getDNSHostEntries($config,$this->OrigCn);
69 /* Remove A record which equals $this->ipHostNumber
70 */
71 foreach($this->dnsEntry['RECORDS'] as $key => $rec){
72 if(($rec['type'] == "aRecord") && ($rec['value'] == $this->ipHostNumber)){
73 unset($this->dnsEntry['RECORDS'][$key]);
74 }
75 }
77 /* Get Record types
78 */
79 $this->RecordTypes = getDnsRecordTypes();
81 /* If there is at least one entry in this -> types, we have DNS enabled
82 */
83 if($this->dnsEntry['exists']){
84 $this->DNS_is_account = true;
85 }else{
86 $this->DNS_is_account = false;
87 }
89 /* Store initally account settings
90 */
91 $this->DNSinitially_was_account = $this->DNS_is_account;
92 }
95 function getVarsForSaving($attrs)
96 {
97 foreach($this->attributes as $attr){
98 if(!empty($this->$attr)){
99 $attrs[$attr] = $this->$attr;
100 }
101 }
102 return($attrs);
103 }
105 function execute()
106 {
107 /* Call parent execute */
108 $smarty= get_smarty();
109 $display= "";
111 $smarty->assign("staticAddress", "");
113 /* There is no dns available
114 */
115 if($this->DNSenabled == false){
117 /* Is IP address must ? */
118 $smarty->assign("DNS_is_account",false);
119 $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
121 /* Assign smarty all non DNs attributes */
122 foreach($this->attributes as $attr){
123 $smarty->assign($attr,$this->$attr);
124 }
125 $smarty->assign("staticAddress", "");
126 $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
127 return($display);
128 }else{
129 $smarty->assign("DNS_is_account",true);
130 }
132 /* Add new empty array to our record list */
133 if(isset($_POST['AddNewRecord'])){
134 $this->dnsEntry['RECORDS'][] =array("type"=>"aRecord","value"=>"");
135 }
137 /* Handle all posts */
138 $only_once =true;
139 foreach($_POST as $name => $value){
141 /* Check if we have to delete a record entry */
142 if((preg_match("/RemoveRecord_/",$name))&&($only_once)) {
144 /* Avoid performing this once again */
145 $only_once = false;
147 /* Extract id for specified entry */
148 $id = preg_replace("/RemoveRecord_/","",$name);
149 $id = preg_replace("/_.*$/","",$id);
151 /* Delete this record, mark edited entries to be able to delete them */
152 if(isset($this->dnsEntry['RECORDS'][$id])){
153 unset($this->dnsEntry['RECORDS'][$id]);
154 }
155 }
156 }
158 /* Assign smarty all non DNs attributes */
159 foreach($this->attributes as $attr){
160 $smarty->assign($attr,$this->$attr);
161 }
163 /* Assign smarty all DNS attributes */
164 foreach($this->DNSattributes as $attr){
165 $smarty->assign($attr,$this->dnsEntry[$attr]);
166 }
168 /* Assign all needed vars */
169 $smarty->assign("DNSAccount",$this->DNS_is_account);
170 $smarty->assign("Zones",$this->Zones);
171 $smarty->assign("ZoneKeys",($this->Zones));
172 $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
174 $tmp = $this->generateRecordsList();
176 $changeStateForRecords = $tmp['changeStateForRecords'];
178 $smarty->assign("records",$tmp['str']);
179 $smarty->assign("changeStateForRecords",$changeStateForRecords);
180 $smarty->assign("staticAddress","<font class=\"must\">*</font>");
182 $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
183 return($display);
184 }
186 function remove_from_parent()
187 {
188 /*
189 $ldap = $this->config->get_ldap_link();
190 $ldap->cd($this->orig_dn);
191 $ldap->search("(&(objectClass=dNSZone)(zoneName=*)(!(relativeDomainName=@)))",array("relativeDomainName","zoneName"));
192 while($attr = $ldap->fetch()){
193 $ldap->cd($attr['dn']);
194 $ldap->rmDir($attr['dn']);
195 }
196 */
197 }
199 /* Save data to object */
200 function save_object()
201 {
202 /* Save all posted vars */
203 plugin::save_object();
205 /* Ge all non dns attributes (IP/MAC)*/
206 foreach($this->attributes as $attr){
207 if(isset($_POST[$attr])){
208 $this->$attr = $_POST[$attr];
209 }
210 }
212 /* Get dns attributes */
213 if(isset($_POST['network_tpl_posted'])){
215 /* Check for posted record changes */
216 foreach($this->dnsEntry['RECORDS'] as $key => $value){
218 /* Check if type has changed */
219 if(isset($_POST['RecordTypeSelectedFor_'.$key])){
220 $this->dnsEntry['RECORDS'][$key]['type'] = $_POST['RecordTypeSelectedFor_'.$key];
221 }
222 /* Check if value has changed */
223 if(isset($_POST['RecordValue_'.$key])){
224 $this->dnsEntry['RECORDS'][$key]['value'] = $_POST['RecordValue_'.$key];
225 }
226 }
228 /* Get all basic DNS attributes (TTL, Clas ..)*/
229 foreach($this->DNSattributes as $attr){
230 if(isset($_POST[$attr])){
231 $this->dnsEntry[$attr] = $_POST[$attr];
232 }
233 }
235 /* Enable diable DNS */
236 if(isset($_POST['enableDNS'])){
237 $this->DNS_is_account = true;
238 }else{
239 $this->DNS_is_account = false;
240 }
241 }
242 }
245 /* Check supplied data */
246 function check()
247 {
248 $message= array();
250 /* Check if ip must be given
251 */
252 if(($this->IPisMust)||($this->DNS_is_account)){
254 /* Check if ip is empty
255 */
256 if ($this->ipHostNumber == "" && chkacl ($this->acl, "ipHostNumber") == ""){
257 $message[]= _("The required field 'IP-address' is not set.");
258 }
260 /* check if given ip is valid ipi
261 */
262 $num="(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])";
263 if (!preg_match("/^$num\\.$num\\.$num\\.$num$/", $this->ipHostNumber)){
264 $message[]= _("Wrong IP format in field IP-address.");
265 }
266 }
268 /* Check if mac is empty
269 */
270 if ($this->macAddress == "" && chkacl ($this->acl, "macAddress") == ""){
271 $message[]= _("The required field 'MAC-address' is not set.");
272 }
274 /* Check if given mac is valid mac
275 */
276 $tr = count(split(":",$this->macAddress));
277 if($tr!=6){
278 $message[]=(_("The given macaddress is invalid. There must be 6 1byte segments seperated by ':'."));
279 }
281 /* only perfrom this checks if this is a valid DNS account */
282 if($this->DNS_is_account){
284 $checkArray = array();
285 $onlyOnce = array();
287 $onlyOnce['cNAMERecord'] = 0;
289 /* Walk through all entries and detect duplicates or mismatches
290 */
291 foreach($this->dnsEntry['RECORDS'] as $name => $values){
293 /* Count record values, to detect duplicate entries for a specific record
294 */
295 if(!isset($checkArray[$values['type']][$values['value']])){
296 $checkArray[$values['type']][$values['value']] = 0;
297 }else{
298 $message[] = sprintf(_("Found duplicate value for record type '%s'."),$values['type']);
299 }
301 /* Check if given entries in $onlyOnce are used more than once
302 */
303 if(isset($onlyOnce[$values['type']])){
304 $onlyOnce[$values['type']] ++;
305 if($onlyOnce[$values['type']] > 1){
306 $message[] = sprintf(_("Found more than one entry for the uniqe record type '%s'."),$values['type']);
307 }
308 }
310 /* Skip txt record ...
311 */
312 if($values['type'] == "tXTRecord") continue;
314 /* Check if there is an aRecord defined which uses the same IP as used in IPhostAddress
315 */
316 if(($values['type'] == "aRecord")&&($values['value'] == $this->ipHostNumber)){
317 $message[]=sprintf(_("The device IP '%s' is added as 'A Record', this will be done automatically, please remove the record."),
318 $this->ipHostNumber);
319 }
321 /* only lower-case is allowed in record entries ...
322 */
323 if($values['value'] != strtolower($values['value'])){
324 $message[] = sprintf(_("Only lowercase is allowed, please check your '%ss'."),$values['type']);
325 }
326 }
327 }
328 return ($message);
329 }
332 /* Save to LDAP */
333 function save($dn)
334 {
335 $ldap= $this->config->get_ldap_link();
337 /*******************/
338 /* IP-MAC HANDLING */
339 /*******************/
341 /* $dn was posted as parameter */
342 $this->dn = $dn;
344 /* Save DNS setting & ip/Mac*/
345 plugin::save();
347 /* Write back to ldap */
348 $ldap->cd($this->dn);
349 $this->cleanup();
350 $ldap->modify ($this->attrs);
352 /****************/
353 /* DNS HANDLING */
354 /****************/
356 /* If isn't DNS account but initially was DNS account
357 remove all DNS entries
358 */
360 /* Add ipHostNumber to aRecords
361 */
362 $this->dnsEntry['RECORDS'][] = array("type"=>"aRecord","value"=>$this->ipHostNumber);
364 /* Create diff and follow instructions
365 * If Account was disabled, remove account by setting exists to false
366 */
367 if((!$this->DNS_is_account)&&($this->DNSinitially_was_account)){
368 $this->dnsEntry['exists'] = false;
369 $tmp = getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
370 }else{
371 $tmp = getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
372 }
374 /* Delete dns */
375 foreach($tmp['del'] as $dn => $del){
376 $ldap->cd($dn);
377 $ldap->rmdir_recursive($dn);
378 }
380 /* move follwoing entries
381 */
382 foreach($tmp['move'] as $src => $dst){
383 $this->recursive_move($src,$dst);
384 }
386 /* Add || Update new DNS entries
387 */
388 foreach($tmp['add'] as $dn => $attrs){
389 $ldap->cd($dn);
390 $ldap->cat($dn);
391 if(count($ldap->fetch())){
392 $ldap->cd($dn);
393 $ldap->modify ($attrs);
394 }else{
395 $ldap->cd($dn);
396 $ldap->add($attrs);
397 }
398 }
400 /* Display errors
401 */
402 if($ldap->get_error() != "Success"){
403 show_ldap_error($ldap->get_error());
404 }
405 }
407 /* Create html table with all used record types
408 */
409 function generateRecordsList()
410 {
411 $changeStateForRecords = "";
413 if(!$this->DNS_is_account) {
414 $str = "<input type='submit' value='"._("Add")."' name='AddNewRecord' id='AddNewRecord' disabled>";
415 return $str;
416 }
418 $str = "<table summary='' width='100%'>";
419 foreach($this->dnsEntry['RECORDS'] as $key => $entry){
421 $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n";
422 $changeStateForRecords.= "changeState('RecordValue_".$key."');\n";
423 $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n";
425 $str.=" <tr>".
426 " <td>".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."</td>".
427 " <td><input type='text' value='".$entry['value']."' name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
428 " <td><input type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
429 "</tr>";
430 }
432 $str.= " <tr>".
433 " <td colspan=2 width='50%'></td><td>".
434 " <input type='submit' value='"._("Add")."' name='AddNewRecord'>".
435 " </td>".
436 " </tr>".
437 "</table>";
438 $ret = array("str" => $str, "changeStateForRecords" => $changeStateForRecords);
439 return($ret);
440 }
443 /* Create a html select box which allows us to select different types of records
444 */
445 function generateRecordListBox($selected,$name)
446 {
447 $str = "<select name='".$name."' id='".$name."'>";
448 foreach($this->RecordTypes as $type => $value){
449 $use = "";
450 if($type == $selected){
451 $use = " selected ";
452 }
453 $str.="\n <option value='".$type."' ".$use.">".strtoupper(preg_replace("/record/i","",$type))."</option>";
454 }
455 $str.="</select>";
456 return($str);
457 }
458 }
460 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
461 ?>