forked from getnamingo/bind9-api-server
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhelpers.php
More file actions
375 lines (332 loc) · 11.6 KB
/
helpers.php
File metadata and controls
375 lines (332 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
<?php
require __DIR__ . '/vendor/autoload.php';
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Formatter\LineFormatter;
use Swoole\Http\Server;
use Swoole\Http\Request;
use Swoole\Http\Response;
use Badcow\DNS\Classes;
use Badcow\DNS\Zone;
use Badcow\DNS\Rdata\Factory;
use Badcow\DNS\ResourceRecord;
use Badcow\DNS\AlignedBuilder;
use Namingo\Rately\Rately;
/**
* Sets up and returns a Logger instance.
*/
function setupLogger($logFilePath, $channelName = 'app') {
$log = new Logger($channelName);
$consoleHandler = new StreamHandler('php://stdout', Logger::DEBUG);
$consoleFormatter = new LineFormatter(
"[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n",
"Y-m-d H:i:s.u",
true,
true
);
$consoleHandler->setFormatter($consoleFormatter);
$log->pushHandler($consoleHandler);
$fileHandler = new RotatingFileHandler($logFilePath, 0, Logger::DEBUG);
$fileFormatter = new LineFormatter(
"[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n",
"Y-m-d H:i:s.u"
);
$fileHandler->setFormatter($fileFormatter);
$log->pushHandler($fileHandler);
return $log;
}
function isIpWhitelisted($ip, $pdo) {
if (!filter_var($ip, FILTER_VALIDATE_IP)) {
return false;
}
$stmt = $pdo->prepare("SELECT COUNT(*) FROM whitelist WHERE ip_address = :ip");
$stmt->execute(['ip' => $ip]);
$count = $stmt->fetchColumn();
return $count > 0;
}
/**
* Save the zone file.
*/
function saveZone($zone) {
$zoneDir = $_ENV['BIND9_ZONE_DIR'];
$zoneName = rtrim($zone->getName(), '.');
if (!isValidDomainName($zoneName)) {
throw new Exception("Invalid zone name");
}
$zoneFile = "$zoneDir/" . $zoneName . ".zone";
$realPath = realpath($zoneFile);
$builder = new AlignedBuilder();
if (file_put_contents($zoneFile, $builder->build($zone), LOCK_EX) === false) {
throw new Exception("Failed to save zone file at $zoneFile");
}
}
/**
* Backup the configuration file.
*/
function backupConfigFile(string $configFile): void {
$backupFile = $configFile . '.bak.' . date('YmdHis');
if (!file_exists($configFile) || !is_readable($configFile)) {
throw new Exception("Invalid configuration file");
}
if (!copy($configFile, $backupFile)) {
throw new Exception("Failed to create backup of $configFile");
}
}
/**
* Remove a zone block from named.conf.local.
*/
function removeZoneFromConfig(string $zoneName): void {
$configFile = $_ENV['BIND9_CONF_FILE'];
backupConfigFile($configFile);
$configContent = file_get_contents($configFile);
if ($configContent === false) {
throw new Exception("Unable to read $configFile");
}
$pattern = '/zone\s+"'.preg_quote($zoneName, '/').'"\s*\{[^}]*\};\n?/i';
if (!preg_match($pattern, $configContent)) {
throw new Exception("Zone block for '$zoneName' not found in $configFile");
}
$newConfigContent = preg_replace($pattern, '', $configContent, 1);
if ($newConfigContent === null) {
throw new Exception("Error occurred while removing the zone block");
}
if (file_put_contents($configFile, $newConfigContent, LOCK_EX) === false) {
throw new Exception("Unable to write to $configFile");
}
}
/**
* Remove a slave zone block from named.conf.local.
*/
function removeSlaveZoneFromConfig(string $zoneName): void {
$configFile = $_ENV['BIND9_CONF_FILE'];
backupConfigFile($configFile);
$configContent = file_get_contents($configFile);
if ($configContent === false) {
throw new Exception("Unable to read $configFile");
}
$pattern = '/zone\s+"'.preg_quote($zoneName, '/').'"\s*\{[^}]*\};\n?/i';
if (!preg_match($pattern, $configContent)) {
throw new Exception("Slave zone block for '$zoneName' not found in $configFile");
}
$newConfigContent = preg_replace($pattern, '', $configContent, 1);
if ($newConfigContent === null) {
throw new Exception("Error occurred while removing the slave zone block");
}
if (file_put_contents($configFile, $newConfigContent, LOCK_EX) === false) {
throw new Exception("Unable to write to $configFile");
}
}
/**
* Append a new zone block to named.conf.local.
*/
function addZoneToConfig(string $zoneName, string $zoneFilePath): void {
$configFile = $_ENV['BIND9_CONF_FILE'];
backupConfigFile($configFile);
$zoneBlock = "\nzone \"$zoneName\" {\n type master;\n file \"$zoneFilePath\";\n};\n";
if (file_put_contents($configFile, $zoneBlock, FILE_APPEND | LOCK_EX) === false) {
throw new Exception("Unable to write to $configFile");
}
}
/**
* Append a new slave zone block to named.conf.local.
*/
function addSlaveZoneToConfig(string $zoneName, string $masterIp): void {
$configFile = $_ENV['BIND9_CONF_FILE'];
backupConfigFile($configFile);
$zoneBlock = "\nzone \"$zoneName\" {\n type slave;\n masters { $masterIp; };\n file \"/var/cache/bind/$zoneName.zone\";\n};\n";
if (file_put_contents($configFile, $zoneBlock, FILE_APPEND | LOCK_EX) === false) {
throw new Exception("Unable to write to $configFile");
}
}
/**
* Load a zone file.
*/
function loadZone($zoneName) {
$zoneDir = $_ENV['BIND9_ZONE_DIR'];
if (!isValidDomainName($zoneName)) {
throw new Exception("Invalid zone name");
}
$zoneFile = "$zoneDir/$zoneName.zone";
if (!file_exists($zoneFile)) {
throw new Exception("Zone file not found.");
}
$file = file_get_contents($zoneFile);
$zone = Badcow\DNS\Parser\Parser::parse($zoneName.'.', $file);
return $zone;
}
/**
* Reload BIND9 configuration and notify slaves.
*/
function reloadBIND9() {
exec('sudo rndc reload', $output, $return_var);
if ($return_var !== 0) {
throw new Exception(implode("\n", $output));
}
exec('sudo rndc notify 2>&1', $notify_output, $notify_return_var);
if ($notify_return_var !== 0) {
error_log("Failed to notify slave servers: " . implode("\n", $notify_output));
}
}
/**
* Authentication middleware.
*/
function authenticate($request, $pdo, $log) {
$authHeader = $request->header['authorization'] ?? '';
if (!$authHeader) {
return false;
}
$authParts = explode(' ', $authHeader, 2);
if (count($authParts) !== 2 || strcasecmp($authParts[0], 'Bearer') !== 0) {
return false;
}
$token = $authParts[1];
if (!$token) {
return false;
}
try {
$stmt = $pdo->prepare('
SELECT s.user_id, u.username, s.expires_at, s.ip_address, s.user_agent
FROM sessions s
JOIN users u ON s.user_id = u.id
WHERE s.token = :token
LIMIT 1
');
$stmt->execute(['token' => $token]);
$session = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$session) {
return false;
}
if (strtotime($session['expires_at']) < time()) {
return false;
}
return [
'user_id' => $session['user_id'],
'username' => $session['username']
];
} catch (Exception $e) {
$log->error('Authentication error: ' . $e->getMessage());
return false;
}
}
function generateInitialSerialNumber() {
$currentDate = date('Ymd');
return $currentDate . '01';
}
function getCurrentSerialNumber($pdo, $domainName) {
$stmt = $pdo->prepare('SELECT current_soa FROM zones WHERE domain_name = :domain_name');
$stmt->execute([':domain_name' => $domainName]);
return $stmt->fetchColumn();
}
function insertInitialSerialNumber($pdo, $domainName) {
$serialNumber = generateInitialSerialNumber();
$stmt = $pdo->prepare('INSERT INTO zones (domain_name, current_soa, created_at, updated_at) VALUES (:domain_name, :serial_number, DATETIME(\'now\'), DATETIME(\'now\'))');
$stmt->execute([':domain_name' => $domainName, ':serial_number' => $serialNumber]);
return $serialNumber;
}
function updateSerialNumber($pdo, $domainName) {
$currentSerial = getCurrentSerialNumber($pdo, $domainName);
$currentDate = date('Ymd');
$serialDate = substr($currentSerial, 0, 8);
$changeNumber = (int)substr($currentSerial, 8, 2);
if ($serialDate === $currentDate) {
$changeNumber++;
$changeNumber = str_pad($changeNumber, 2, '0', STR_PAD_LEFT);
} else {
$changeNumber = '01';
}
$newSerial = $currentDate . $changeNumber;
$stmt = $pdo->prepare('UPDATE zones SET current_soa = :serial_number WHERE domain_name = :domain_name');
$stmt->execute([':serial_number' => $newSerial, ':domain_name' => $domainName]);
return $newSerial;
}
/**
* Update the SOA record in the zone by updating its serial number.
*/
function updateZoneSoa($zone, $zoneName, $pdo) {
$newSerial = updateSerialNumberFromZone($zone);
foreach ($zone->getResourceRecords() as $record) {
if (strtoupper($record->getType()) === 'SOA') {
$soaRdata = $record->getRdata();
$soaRdata->setSerial($newSerial);
$record->setRdata($soaRdata);
break;
}
}
saveZone($zone);
}
function isValidDomainName($domain) {
return preg_match('/^(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}$/', $domain) || // Regular domain
preg_match('/^xn--[a-zA-Z0-9-]+$/', $domain); // IDN (Punycode)
}
function getPdo(string $path): PDO {
static $pdo = null;
static $cachedPath = null;
if ($pdo === null || $cachedPath !== $path) {
$pdo = new PDO('sqlite:' . $path);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$cachedPath = $path;
}
return $pdo;
}
function updateSerialNumberFromZone($zone) {
$currentDate = date('Ymd');
$newSerial = null;
foreach ($zone->getResourceRecords() as $record) {
if (strtoupper($record->getType()) === 'SOA') {
$currentSerial = $record->getRdata()->getSerial();
$serialDate = substr($currentSerial, 0, 8);
$changeNumber = (int)substr($currentSerial, 8, 2);
if ($serialDate === $currentDate) {
$changeNumber++;
$changeNumber = str_pad($changeNumber, 2, '0', STR_PAD_LEFT);
} else {
$changeNumber = '01';
}
$newSerial = $currentDate . $changeNumber;
break;
}
}
return $newSerial;
}
function normalizeMxRdata($rdata): array {
if (is_string($rdata)) {
$parts = preg_split('/\s+/', trim($rdata), 2);
if (count($parts) === 2 && is_numeric($parts[0])) {
return [
'preference' => (int) $parts[0],
'exchange' => rtrim($parts[1], '.') . '.',
];
}
// fallback
return [
'preference' => 10,
'exchange' => rtrim($rdata, '.') . '.',
];
}
return [
'preference' => (int)($rdata['preference'] ?? 10),
'exchange' => rtrim($rdata['exchange'] ?? '', '.') . '.',
];
}
function normalizeSpfRdata($rdata): string
{
// Allow array or string input
if (is_array($rdata)) {
$rdata = implode(' ', $rdata);
}
$rdata = trim((string)$rdata);
if ($rdata === '') {
return '';
}
// Strip surrounding quotes if present
$first = $rdata[0];
$last = $rdata[strlen($rdata) - 1];
if (($first === '"' && $last === '"') || ($first === "'" && $last === "'")) {
$rdata = substr($rdata, 1, -1);
}
// Collapse internal whitespace
$rdata = preg_replace('/\s+/', ' ', $rdata);
// SPF is case-insensitive; normalize to lowercase for comparison
return strtolower($rdata);
}