Update dns_cron.php

This commit is contained in:
deepend 2024-01-11 08:06:53 +00:00
parent 075ad6377a
commit 6a6d54701f
1 changed files with 124 additions and 123 deletions

View File

@ -1,123 +1,124 @@
<?php <?php
require_once 'includes/initdb.php'; require_once 'includes/initdb.php';
error_reporting(E_ALL); error_reporting(E_ALL);
ini_set('display_errors', 1); ini_set('display_errors', 1);
// Open a log file for writing // Open a log file for writing
$logFile = fopen("webchangelog.log", "a") or die("Unable to open file!"); $logFile = fopen("webchangelog.log", "a") or die("Unable to open file!");
// Function to write to log // Function to write to log
function writeToLog($message, $logFile) { function writeToLog($message, $logFile) {
fwrite($logFile, date('Y-m-d H:i:s') . " - " . $message . "\n"); fwrite($logFile, date('Y-m-d H:i:s') . " - " . $message . "\n");
} }
// Function to fetch all domain names // Function to fetch all domain names
function getAllDomainNames($pdo) { function getAllDomainNames($pdo) {
$stmt = $pdo->query("SELECT domain_name FROM domains"); $stmt = $pdo->query("SELECT domain_name FROM domains");
return $stmt->fetchAll(PDO::FETCH_COLUMN, 0); return $stmt->fetchAll(PDO::FETCH_COLUMN, 0);
} }
// Function to fetch all domains // Function to fetch all domains
function getAllDomains($pdo) { function getAllDomains($pdo) {
$stmt = $pdo->query("SELECT domain_name, ip_address FROM domains"); $stmt = $pdo->query("SELECT domain_name, ip_address FROM domains");
return $stmt->fetchAll(PDO::FETCH_ASSOC); return $stmt->fetchAll(PDO::FETCH_ASSOC);
} }
// Function to generate BIND DNS file content // Function to generate BIND DNS file content
function generateDnsFileContent($domain, $ipAddress) { function generateDnsFileContent($domain, $ipAddress) {
$content = "; BIND data file for $domain\n"; $content = "; BIND data file for $domain\n";
$content .= "\$TTL 604800\n"; $content .= "\$TTL 604800\n";
$content .= "@ IN SOA ns1.master.tilde. root.$domain. (\n"; $content .= "@ IN SOA ns1.master.tilde. root.$domain. (\n";
$content .= " " . date('Ymd') . "01 ; Serial\n"; // Date-based serial $content .= " " . date('Ymd') . "01 ; Serial\n"; // Date-based serial
$content .= " 604800 ; Refresh\n"; $content .= " 604800 ; Refresh\n";
$content .= " 86400 ; Retry\n"; $content .= " 86400 ; Retry\n";
$content .= " 2419200 ; Expire\n"; $content .= " 2419200 ; Expire\n";
$content .= " 604800 ) ; Negative Cache TTL\n"; $content .= " 604800 ) ; Negative Cache TTL\n";
$content .= ";\n"; $content .= ";\n";
$content .= "@ IN NS ns1.master.tilde.\n"; $content .= "@ IN NS ns1.master.tilde.\n";
$content .= "@ IN NS ns2.master.tilde.\n"; $content .= "@ IN NS ns2.master.tilde.\n";
$content .= "www IN CNAME $domain\n"; $content .= "@ IN NS ns3.master.tilde.\n";
$content .= "@ IN A $ipAddress\n"; $content .= "www IN CNAME $domain\n";
$content .= "* IN A $ipAddress\n"; // Wildcard A record $content .= "@ IN A $ipAddress\n";
// Add more DNS records as needed $content .= "* IN A $ipAddress\n"; // Wildcard A record
return $content; // Add more DNS records as needed
} return $content;
}
// Change to the Git repository directory
chdir('/home/retrodig/dottilde'); // Change to the Git repository directory
chdir('/home/retrodig/dottilde');
// Perform a git pull to ensure the repository is up to date
exec('git pull'); // Perform a git pull to ensure the repository is up to date
exec('git pull');
// Fetch domain names from the database
$databaseDomains = getAllDomainNames($pdo); // Fetch domain names from the database
$databaseDomains = getAllDomainNames($pdo);
// Fetch domains and generate/update DNS files
$domains = getAllDomains($pdo); // Fetch domains and generate/update DNS files
$currentFiles = glob('db.*'); // Get all current db files $domains = getAllDomains($pdo);
$changes = false; $currentFiles = glob('db.*'); // Get all current db files
$changes = false;
foreach ($domains as $domain) {
$filename = "db." . $domain['domain_name']; foreach ($domains as $domain) {
$content = generateDnsFileContent($domain['domain_name'], $domain['ip_address']); $filename = "db." . $domain['domain_name'];
$content = generateDnsFileContent($domain['domain_name'], $domain['ip_address']);
if (!file_exists($filename) || $content !== file_get_contents($filename)) {
file_put_contents($filename, $content); if (!file_exists($filename) || $content !== file_get_contents($filename)) {
$changes = true; file_put_contents($filename, $content);
writeToLog("Updated or created DNS file: $filename", $logFile); $changes = true;
} writeToLog("Updated or created DNS file: $filename", $logFile);
}
// Remove filename from the list of current files if it's in the database
if (($key = array_search($filename, $currentFiles)) !== false) { // Remove filename from the list of current files if it's in the database
unset($currentFiles[$key]); if (($key = array_search($filename, $currentFiles)) !== false) {
} unset($currentFiles[$key]);
} }
}
// Function to update named.conf.local file
function updateNamedConfLocal($domains, $namedConfPath, $logFile) { // Function to update named.conf.local file
$confContent = "// Dynamic BIND configuration\n\n"; function updateNamedConfLocal($domains, $namedConfPath, $logFile) {
$confContent = "// Dynamic BIND configuration\n\n";
foreach ($domains as $domain) {
$zoneEntry = "zone \"" . $domain['domain_name'] . "\" {\n"; foreach ($domains as $domain) {
$zoneEntry .= "\ttype master;\n"; $zoneEntry = "zone \"" . $domain['domain_name'] . "\" {\n";
$zoneEntry .= "\tfile \"/etc/bind/db." . $domain['domain_name'] . "\";\n"; $zoneEntry .= "\ttype master;\n";
$zoneEntry .= "};\n\n"; $zoneEntry .= "\tfile \"/etc/bind/db." . $domain['domain_name'] . "\";\n";
$confContent .= $zoneEntry; $zoneEntry .= "};\n\n";
} $confContent .= $zoneEntry;
}
// Write the new configuration to the file
file_put_contents($namedConfPath, $confContent); // Write the new configuration to the file
writeToLog("Updated named.conf.local", $logFile); file_put_contents($namedConfPath, $confContent);
} writeToLog("Updated named.conf.local", $logFile);
}
// Define the path to named.conf.local
$namedConfPath = '/home/retrodig/dottilde/named.conf.local'; // Define the path to named.conf.local
$namedConfPath = '/home/retrodig/dottilde/named.conf.local';
// Update named.conf.local with current domains
updateNamedConfLocal($domains, $namedConfPath, $logFile); // Update named.conf.local with current domains
updateNamedConfLocal($domains, $namedConfPath, $logFile);
// List of DNS files that should never be deleted
$protectedFiles = ['db.master.tilde', 'db.tilde.tilde', 'db.nic.tilde']; // Add your protected filenames here // List of DNS files that should never be deleted
$protectedFiles = ['db.master.tilde', 'db.tilde.tilde', 'db.nic.tilde']; // Add your protected filenames here
// Delete any remaining files that are no longer in the database
foreach ($currentFiles as $file) { // Delete any remaining files that are no longer in the database
$domainName = substr($file, 3); // Extract domain name from filename foreach ($currentFiles as $file) {
if (!in_array($domainName, $databaseDomains) && !in_array($file, $protectedFiles)) { $domainName = substr($file, 3); // Extract domain name from filename
unlink($file); if (!in_array($domainName, $databaseDomains) && !in_array($file, $protectedFiles)) {
$changes = true; unlink($file);
writeToLog("Deleted orphaned DNS file: $file", $logFile); $changes = true;
} writeToLog("Deleted orphaned DNS file: $file", $logFile);
} }
}
// Close the log file
fclose($logFile); // Close the log file
fclose($logFile);
// Git commit and push if there are changes
if ($changes) { // Git commit and push if there are changes
exec('git add .'); if ($changes) {
exec('git commit -m "Updated DNS files"'); exec('git add .');
exec('git push origin master'); exec('git commit -m "Updated DNS files"');
} exec('git push origin master');
}