Skip to content

Commit

Permalink
added config checks, custom entries and deletion via UI
Browse files Browse the repository at this point in the history
  • Loading branch information
geek-at committed Dec 27, 2023
1 parent 9533509 commit 7a2d45f
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 25 deletions.
1 change: 1 addition & 0 deletions web/inc/api.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ private function clearIPs()
$data['ipv4'] = '';
$data['ipv6'] = '';
$data['lastupdated'] = date('Y-m-d H:i:s');
header('HX-Refresh:true');
updateHostname($hostname,$data);
return 'OK';
}
Expand Down
52 changes: 42 additions & 10 deletions web/inc/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,76 @@ function restartDNSMASQ() {
error_log("[i] Restarted dnsmasq");
}

function checkDNSMASQConfig($configfile)
{
//dnsmasq --test -C $configfile

$output = [];
$return = 0;
exec("dnsmasq --test -C $configfile",$output,$return);
if($return!=0)
{
error_log("[!] dnsmasq config test failed");
error_log(implode("\n",$output));
return false;
}
return true;
}

function updateHostname($hostname,$config)
{
$file = ROOT.DS.'..'.DS.'data'.DS."$hostname.conf";
$tmpfile = '/tmp/'.$hostname.'.conf';
$data = "# ".json_encode($config);
if($config['ipv4'])
$data.= "\naddress=/$hostname/".$config['ipv4'];
if($config['ipv6'])
$data.= "\naddress=/$hostname/".$config['ipv6'];

if($config['advanceddns'])
{
foreach($config['advanceddns'] as $entry)
{
$subhost = $entry['hostname'];
if($subhost=='@')
$subhost=false;
switch($entry['type']){
case 'TXT':
$data.= "\ntxt-record=".$entry['hostname'].".$hostname,\"".$entry['value'].'"';
$data.= "\ntxt-record=".($subhost?"$subhost.":'')."$hostname,\"".$entry['value'].'"';
break;
case 'CNAME':
$data.= "\ncname=".$entry['hostname'].".$hostname,".$entry['value'];
$data.= "\ncname=".($subhost?"$subhost.":'')."$hostname,".$entry['value'];
break;
case 'A':
$data.= "\naddress=".$entry['hostname'].".$hostname,".$entry['value'];
$data.= "\naddress=".($subhost?"$subhost.":'')."$hostname,".$entry['value'];
break;
case 'AAAA':
$data.= "\naddress6=".$entry['hostname'].".$hostname,".$entry['value'];
$data.= "\naddress6=".($subhost?"$subhost.":'')."$hostname,".$entry['value'];
break;
case 'MX':
$data.= "\nmx-host=".$entry['hostname'].".$hostname,".$entry['value'];
$data.= "\nmx-host=".($subhost?"$subhost.":'')."$hostname,".$entry['value'].",".$entry['priority'];
break;
case 'SRV':
$data.= "\nsrv-host=".$entry['hostname'].".$hostname,".$entry['value'];
$data.= "\nsrv-host=".($subhost?"$subhost.":'')."$hostname,".$entry['value'].",".$entry['priority'].",".$entry['weight'];
break;
}
}
}

if(!file_put_contents($tmpfile, $data))exit('Failed to write to temp file');

if(!checkDNSMASQConfig($tmpfile))
{
error_log("[!] dnsmasq config test failed not saving changes");
return false;
}
else {
if(file_exists($tmpfile))
unlink($tmpfile);
if(!file_put_contents($file, $data))exit('Failed to write to config file');
}

if(!file_put_contents($file, $data))exit('Failed to write to file');
//time to restart the service?
if($config['ipv4'] || $config['ipv6'])
restartDNSMASQ();
restartDNSMASQ();
error_log("[i] Updated hostfile for $hostname");
}

Expand Down
27 changes: 19 additions & 8 deletions web/inc/htmx.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,17 @@ function act()

private function deleteDNS(){
$fulldomain = $this->url[1];
$todelete_hostname = $this->url[2];
$todelete_type = $this->url[3];
$todelete = intval($this->url[2]);
if(!$_SESSION[$fulldomain]) return error('Invalid session');
if(!$todelete_hostname || !$todelete_type) return error('Something is missing in your request');
if(!is_numeric($todelete)) return error('Something is missing in your request');
if(!preg_match('/^[a-z0-9-.]+$/',$fulldomain)) return error('Invalid hostname');

$hostdata = getHostData($fulldomain);
if(!$hostdata) return error('Invalid hostname');
$new_advanceddns = [];
foreach($hostdata['advanceddns'] as $entry)
foreach($hostdata['advanceddns'] as $key => $entry)
{
if($entry['hostname'] != $todelete_hostname || $entry['type'] != $todelete_type)
if($key != $todelete)
$new_advanceddns[] = $entry;
}
$hostdata['advanceddns'] = $new_advanceddns;
Expand All @@ -60,24 +59,36 @@ private function advancedDNS(){
$new_hostname = $_REQUEST['new_hostname'];
$new_type = $_REQUEST['new_type'];
$new_value = $_REQUEST['new_value'];

// $new_ttl = $_REQUEST['new_ttl'];
// $new_priority = $_REQUEST['new_priority'];

if(!preg_match('/^[a-z0-9-.]+$/',$new_hostname)) return error('Invalid hostname');
if($new_hostname!='@' && !preg_match('/^[a-z0-9-.]+$/',$new_hostname)) return error('Invalid hostname');
if(!in_array($new_type,['A','AAAA','CNAME','MX','TXT','SRV','NS','CAA'])) return error('Invalid type');
// if(!preg_match('/^[0-9]+$/',$new_ttl)) return error('Invalid TTL');
// if(!preg_match('/^[0-9]+$/',$new_priority)) return error('Invalid priority');

switch($new_type){
case 'TXT':
$new_value = '"'.addslashes($new_value).'"';
$new_value = addslashes(str_replace('"','',$new_value));
break;
case 'CNAME':
if(!filter_var($new_value, FILTER_VALIDATE_IP) && !filter_var($new_value, FILTER_FLAG_HOSTNAME))
if(!filter_var($new_value, FILTER_VALIDATE_IP) && !filter_var($new_value, FILTER_VALIDATE_DOMAIN,FILTER_FLAG_HOSTNAME ))
$error= error('Invalid value. CNAME record values have to be IP Addresses or hostnames');
break;
}

/*
"v=DKIM1; k=rsa; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA3sGmbQA4anI5Tw1jOkOWHFN9giT98U+8Fc0KXu53bnbFOhqzgePHNb324bjBOV0/SZkCEd/+Hv5089cCXNXptpN4rwVGcZuMGYAuF44CtyXO4ZA/jqOKuYh6y14NQRcH4ntCv5YC9ZSh/PGZmnmr4tCSsvK/au7ooJyRjy5OaI51hA+qtregvr9tmvRF6GSw/9F0pz+cJwUWHhHb21+tXZb6C39MNyBCOncy0I1PyscQeixKTBe5Zlo1Jbyea7i4j1jhtVrATf+y6oIRA+MBeSbJtAQH6Kpy6qpW0PVz1PU1qRQwQ3yI5l88/sgkU8IKVsUy7puV4ey15GP0wuTuJQIDAQAB"
*/


foreach($hostdata['advanceddns'] as $entry)
{
if($entry['hostname'] == $new_hostname && $entry['type'] == $new_type && $entry['value'] == $new_value)
$error = error('This entry already exists');
}

if(!$error)
{
$hostdata['advanceddns'][] = [
Expand Down
24 changes: 20 additions & 4 deletions web/templates/advanced_dns.html.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php if(is_array($hostdata['advanceddns']) && count($hostdata['advanceddns'])) : ?>
<?php if((is_array($hostdata['advanceddns']) && count($hostdata['advanceddns']))||$hostdata['ipv4'] || $hostdata['ipv6']) : ?>
<table role="grid">
<thead>
<tr>
Expand All @@ -9,12 +9,28 @@
</tr>
</thead>
<tbody>
<?php if($hostdata['ipv4']) : ?>
<tr>
<td><?=$fulldomain?></td>
<td>A</td>
<td><?= $hostdata['ipv4'] ?></td>
<td>set via API</td>
</tr>
<?php endif; ?>
<?php if($hostdata['ipv6']) : ?>
<tr>
<td><?=$fulldomain?></td>
<td>AAAA</td>
<td><?= $hostdata['ipv6'] ?></td>
<td>set via API</td>
</tr>
<?php endif; ?>
<?php foreach ($hostdata['advanceddns'] as $key => $entry) : ?>
<tr>
<td><?= $entry['hostname'] ?></td>
<td><?php if($entry['hostname']!='@'): ?><?= $entry['hostname'] ?>.<?php endif; ?><?=$fulldomain?></td>
<td><?= $entry['type'] ?></td>
<td><?= $entry['value'] ?></td>
<td><button hx-get="/htmx/deletedns/<?= $fulldomain ?>/<?= $entry['hostname'] ?>/<?= $entry['type'] ?>" hx-confirm="Do you really want to delete this entry?" hx-target="#advanced"><i class="fas fa-trash"></i></button></td>
<td><input type="text" value="<?= escape($entry['value']) ?>" disabled></td>
<td><button hx-get="/htmx/deletedns/<?= $fulldomain ?>/<?= $key ?>" hx-confirm="Do you really want to delete this entry?" hx-target="#advanced"><i class="fas fa-trash"></i></button></td>
</tr>
<?php endforeach; ?>
</tbody>
Expand Down
7 changes: 4 additions & 3 deletions web/templates/host.html.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<label>IPv6: <?= escape($hostdata['ipv6']) ?: 'Not set' ?></label>
</div>
<button hx-get="/htmx/updateip/<?= $fulldomain ?>" hx-target="#ips">Set to current IP (<?= getUserIP() ?>)</button>
<button hx-get="/api/clearips/<?= $fulldomain ?>?secret=<?= $hostdata['secret']; ?>" hx-target="#ips" class="contrast">Clear IPs</button>
<label>Last updated: <?= escape($hostdata['lastupdated'] ?: 'Never') ?></label>
<details>
<summary>Show secret</summary>
Expand Down Expand Up @@ -57,8 +58,8 @@
<div class="grid">
<div>
<label for="new_hostname">
Hostname
<input type="text" id="new_hostname" name="new_hostname" required>
Hostname <small>(use @ for root domain)</small>
<input type="text" id="new_hostname" name="new_hostname" placeholder="eg: www" value="@" required>
</label>
</div>
<div>
Expand All @@ -74,7 +75,7 @@
<div>
<label for="new_value">
Value
<input type="text" id="new_value" name="new_value" required>
<input type="text" id="new_value" name="new_value" placeholder="eg 1.1.1.1" required>
</label>
</div>
</div>
Expand Down

0 comments on commit 7a2d45f

Please sign in to comment.