-1

I have a file called named.conf, which is the configuration file for BIND. It has entries enclosed in curly braces. I want to edit and enter a new zone entry into both the internal and external views using PHP. How can I do this. Is there any library available for editing this type of config files?

view "internal"
{

match-clients { localnets; };
match-destinations { localnets; };
recursion yes;

include "/etc/named.root.hints";
    
zone "my.internal.zone" {
type master;
file "my.internal.zone.db";
};

};


view "external"
{

match-clients { !localnets; !localhost; };
match-destinations { !localnets; !localhost; };

recursion no;
    
include "/etc/named.root.hints";
    
zone "my.external.zone" {
type master;
file "my.external.zone.db";
};
};
Akhilesh
  • 1,243
  • 4
  • 16
  • 49

2 Answers2

1

You can use file_get_content(), update the file content, then push the new data with file_put_content().

What we need is some comments where you need to insert new line :

view "internal"
{

match-clients { localnets; };
match-destinations { localnets; };
recursion yes;

include "/etc/named.root.hints";
    
#INTERNAL
zone "my.internal.zone" {
type master;
file "my.internal.zone.db";
};

};


view "external"
{

match-clients { !localnets; !localhost; };
match-destinations { !localnets; !localhost; };

recursion no;
    
include "/etc/named.root.hints";

#EXTERNAL
zone "my.external.zone" {
type master;
file "my.external.zone.db";
};
};

So you can catch your comments with PHP and add content after :

$confText = file_get_contents('your_file_path') ;

$newExternalZone = PHP_EOL.
'zone "my.new.external.zone" {
type master;
file "my.new.external.zone.db";
};'.PHP_EOL ;

preg_replace("/(#EXTERNAL)/", "$1".$newExternalZone, $confText) ;

file_put_contents('your_file_path', $confText) ;

The code is quite simple here, catch the #External and put $newExternalZone after ! You can update and use data comming from POST,GET or other for newExternalZone.

Camille
  • 847
  • 1
  • 7
  • 19
  • Thanks. It works, but the problem is that I want it to be done through the program. I don't want to manually add the comment. Is there a way I could find the end of `internal view` and `external view`? – Akhilesh Jun 21 '21 at 10:18
  • Is your config files still the same ? if you can't update adding a comment, you can use some regex catching special place when you want inject the code. Exemple, can we catch ```recursion yes; include "/etc/named.root.hints";``` or is it used somewhere else in the config file ? – Camille Jun 21 '21 at 10:37
  • Regex would be : ```preg_replace('/recursion yes;\n\ninclude "\/etc\/named\.root\.hints";/', "$1".$newExternalZone, $confText) ;)``` – Camille Jun 21 '21 at 10:43
0

I was able to do it using the below code. The code accepts arguments using PHP CLI and creates a new file with the values.

$file = file('named.conf');

$arg =  getopt("", array('zone:', 'type:', 'file:'));

$internal_end = 0;
$external_end = 0;
$flag = false;
$count = 0;
foreach ($file as $index => $line) {
    if (preg_match('/view\s*"internal"\s*{/i', $line) !== 1 && !$flag) {
        continue;
    }
    $flag = true;

    $ob =  substr_count($line, '{');
    $count += $ob;
    $cb =  substr_count($line, '}');
    $count -= $cb;
    if ($count == 0) {
        $internal_end = $index;
        break;
    }
}



array_splice($file, $internal_end, 0, array(
    "\n",
    "zone \"".$arg['zone']."\" {\n",
    "\ttype ".$arg['type'].";\n",
    "\tfile \"".$arg['file']."\";\n",
    "};\n",
    "\n"
));

$flag = false;
$count = 0;
foreach ($file as $index => $line) {
    if (preg_match('/view\s*"external"\s*{/i', $line) !== 1 && !$flag) {
        continue;
    }
    $flag = true;

    $ob =  substr_count($line, '{');
    $count += $ob;
    $cb =  substr_count($line, '}');
    $count -= $cb;
    if ($count == 0) {
        $external_end = $index;
        break;
    }
}


array_splice($file, $external_end, 0, array(
    "\n",
    "zone \"".$arg['zone']."\" {\n",
    "\ttype ".$arg['type'].";\n",
    "\tfile \"".$arg['file']."\";\n",
    "};\n",
    "\n"
));


file_put_contents('named_new.conf', implode('', $file));

To execute the code call php script.php --zone=my.new.external.zone --type=master --file=my.new.external.zone.db

Akhilesh
  • 1,243
  • 4
  • 16
  • 49