-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.php
147 lines (119 loc) · 4.47 KB
/
api.php
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
<?php
/**
* api.php
*
* Index file handling input data
*
* @author Johnson Liu
* @version v1.1
*
*/
// ini_set('display_errors', 'on');
require_once(__DIR__ . '/libNsdata.php');
require_once(__DIR__ . '/libIpPool.php');
require_once(__DIR__ . '/libConfig.php');
loadConfig();
$input_key = $_GET['key'];
$input_action = $_GET['action'];
if ($input_key == getSecret()) {
// Auth success
switch($input_action) {
case 'new_zone':
if ($_GET['name']) {
echo "[ddns-server] " . "[INFO] Adding zone [" . $_GET['name'] . "]\n";
addZone($_GET['name'], $_GET['v4_addr'], $_GET['v6_addr'], $_GET['v6_prefix'], $_GET['v6_cidr']);
};
break;
case 'new_client':
if ($_GET['name']) {
echo "[ddns-server] " . "[INFO] Adding client [" . $_GET['name'] . "]\n";
addClient($_GET['name'], $_GET['v4_addr'], $_GET['v6_addr'], $_GET['v6_suffix']);
};
break;
case 'mod_zone':
if ($_GET['name'] && getZoneByName($_GET['name'])) {
echo "[ddns-server] " . "[INFO] Modifying zone [" . $_GET['name'] . "]\n";
modifyZoneByName($_GET['name'], $_GET['name'], $_GET['v4_addr'], $_GET['v6_addr'], $_GET['v6_prefix'], $_GET['v6_cidr']);
} else {
echo "[ddns-server] " . "[ERR] Invalid parameters." . "\n";
}
break;
case 'mod_client':
if ($_GET['name'] && getClientByName($_GET['name'])) {
echo "[ddns-server] " . "[INFO] Modifying client [" . $_GET['name'] . "]\n";
modifyClientByName($_GET['name'], $_GET['name'], $_GET['v4_addr'], $_GET['v6_addr'], $_GET['v6_suffix']);
} else {
echo "[ddns-server] " . "[ERR] Invalid parameters." . "\n";
}
break;
case 'update_zone':
if ($_GET['name'] && getZoneByName($_GET['name'])) {
echo "[ddns-server] " . "[INFO] Updating zone [" . $_GET['name'] . "]\n";
updateZoneByName($_GET['name'], $_GET['name'], $_GET['v4_addr'], $_GET['v6_addr'], $_GET['v6_prefix'], $_GET['v6_cidr']);
} else {
echo "[ddns-server] " . "[ERR] Invalid parameters." . "\n";
}
break;
case 'update_client':
if ($_GET['name'] && getClientByName($_GET['name'])) {
echo "[ddns-server] " . "[INFO] Updating client [" . $_GET['name'] . "]\n";
updateClientByName($_GET['name'], $_GET['name'], $_GET['v4_addr'], $_GET['v6_addr'], $_GET['v6_suffix']);
} else {
echo "[ddns-server] " . "[ERR] Invalid parameters." . "\n";
}
break;
case 'del_zone':
if ($_GET['name']) {
echo "[ddns-server] " . "[INFO] Deleting zone [" . $_GET['name'] . "]\n";
deleteZoneByName($_GET['name']);
} else {
echo "[ddns-server] " . "[ERR] Invalid parameters." . "\n";
}
break;
case 'del_client':
if ($_GET['name']) {
echo "[ddns-server] " . "[INFO] Deleting client [" . $_GET['name'] . "]\n";
deleteClientByName($_GET['name']);
} else {
echo "[ddns-server] " . "[ERR] Invalid parameters." . "\n";
}
break;
default:
echo "[ddns-server] " . "[ERR] Operation not supported." . "\n";
exit;
}
} else {
// Auth failed
echo "[ddns-server] " . "[ERR] Authentication failed." . "\n";
exit;
}
// write config.php
saveConfig();
// old nsdata
if (file_exists(getZonefilePath())) {
$nsdata_old = file_get_contents(getZonefilePath());
} else {
$nsdata_old = '';
}
// new nsdata generated with old serial
$ip_pool = getIpPool();
$nsdata_raw = readNsdata();
$serial_old = getSerialOld();
$nsdata = translateNsdata($ip_pool, $nsdata_raw, $serial_old);
// compare nsdata
if ($nsdata_old == $nsdata) {
echo "[ddns-server] " . "[INFO] Same profile. Ignoring update request." . "\n";
exit;
}
// write nsdata
$serial = date('YmdH');
$nsdata = translateNsdata($ip_pool, $nsdata_raw, $serial);
$GLOBALS['config']['serial'] = $serial;
file_put_contents(getZonefilePath(), $nsdata);
// inform bind9 to reload
echo "[ddns-server] " . "[INFO] ";
system('rndc reload');
// write config.php
saveConfig();
echo "[ddns-server] " . "[OK] Script exec success." . "\n";
?>