/
dnsmasq.php
252 lines (183 loc) · 8.33 KB
/
dnsmasq.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
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
<?php
error_reporting(E_ALL ^ (E_NOTICE | E_WARNING));
$domainRegEx = "[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)*(?:\.[A-Za-z]{2,})";
$settings = array(
// Locations of binaries on web server
"expectPath" => "/usr/bin/expect",
"scpPath" => "/usr/bin/scp",
"sshPath" => "/usr/bin/ssh",
// URL where we can download dnsmasq config file from DNS server
"downloadUrl" => "http://192.168.1.1/dnsmasq.conf",
// IP address of DNS server
"dnsServer" => "192.168.1.1",
// Location of DNS service file on DNS server
"dnsService" => "/etc/init.d/dnsmasq",
// Name of user with admin rights on DNS server
"sshUser" => "root",
// Location where we will upload dnsmasq config file to DNS server
"uploadPath" => "/etc/dnsmasq.conf",
// IP address to forward DNS requests that pass whitelist
"upstreamDns" => "8.8.8.8"
);
function actionResult($success, $message) {
return array("success" => $success, "message" => $message);
}
function makeConfig($upstreamDns) {
return "#[Options]\nbogus-priv\ndomain-needed\nno-resolv\n
#[Category: Forums]\nserver=/stackauth.com/$upstreamDns\nserver=/stackexchange.com/$upstreamDns\nserver=/stackoverflow.com/$upstreamDns\n
#[Category: Games]\nserver=/steampowered.com/$upstreamDns\nserver=/teamfortress.com/$upstreamDns #This is a comment\nserver=/valvesoftware.com/$upstreamDns\n
#Games are awesome\nserver=/gog.com/$upstreamDns\n
#[Category: Search Engines]\nserver=/bing.com/$upstreamDns\nserver=/duckduckgo.com/$upstreamDns\nserver=/google.com/$upstreamDns\n";
}
function makeTempFile($tempDir, $fileContents) {
$tempFile = @tempnam($tempDir, "php");
if ($tempFile === false) {
return false;
}
$fileResult = @file_put_contents($tempFile, $fileContents);
if ($fileResult === false) {
return false;
}
return $tempFile;
}
function readConfig($downloadUrl, $upstreamDns) {
global $domainRegEx;
$categories = [];
$fileContents = @file_get_contents($downloadUrl);
if ($fileContents === false) {
$fileContents = makeConfig($upstreamDns);
}
foreach (preg_split("/\n/", $fileContents) as $line) {
$line = trim($line);
// e.g. "#[Category: Programming]"
if (preg_match("/^#\[Category: (.*)\]$/", $line, $matches)) {
$title = $matches[1];
$categories[$title] = "";
unset($matches);
continue;
}
// If we haven't found a category yet (i.e. we are still reading the first few lines of the config file)
// then get out
if (!isset($title)) {
continue;
}
// e.g. "server=/stackoverflow.com/8.8.8.8"
if (preg_match("/^server=\/($domainRegEx)\/\d\.\d\.\d\.\d$/", $line, $matches)) {
$domain = $matches[1];
$categories[$title] .= "$domain\n";
unset($matches);
continue;
}
// e.g. "server=/stackoverflow.com/8.8.8.8 #comment"
if (preg_match("/^server=\/($domainRegEx)\/\d\.\d\.\d\.\d ?(#.*)$/", $line, $matches)) {
$domain = $matches[1];
$comment = $matches[2];
$categories[$title] .= "$domain $comment\n";
unset($matches);
continue;
}
// e.g. "#comment"
if (preg_match("/^(#.*)$/", $line, $matches)) {
$comment = $matches[1];
$categories[$title] .= "\n$comment\n";
unset($matches);
continue;
}
}
ksort($categories);
return $categories;
}
function uploadConfig($dnsServer, $dnsService, $expectPath, $scpPath, $sshPath, $sshUser, $scriptPath, $tempDir, $uploadPath, $upstreamDns) {
if (!file_exists($expectPath) || !is_executable($expectPath)) {
return actionResult(false, "Could not find expect at $expectPath or it is not executable.");
}
if (!file_exists($scpPath) || !is_executable($scpPath)) {
return actionResult(false, "Could not find scp at $scpPath or it is not executable.");
}
if (!file_exists($sshPath) || !is_executable($sshPath)) {
return actionResult(false, "Could not find ssh at $sshPath or it is not executable.");
}
if (!file_exists($scriptPath)) {
return actionResult(false, "Could not find upload script at $scriptPath.");
}
$scriptPath = realpath($scriptPath);
$tempConfigPath = makeTempFile($tempDir, writeConfig($_POST["categories"], $upstreamDns));
if ($tempConfigPath === false) {
return actionResult(false, "Could not write config to file at $tempConfigPath.");
}
$tempConfigPath = realpath($tempConfigPath);
$tempPasswordPath = makeTempFile($tempDir, $_POST["password"]);
if ($tempPasswordPath === false) {
return actionResult(false, "Could not write password to file at $tempPasswordPath.");
}
$tempPasswordPath = realpath($tempPasswordPath);
$command = "$expectPath -f $scriptPath $dnsServer $dnsService $scpPath $sshPath $sshUser $tempConfigPath $tempPasswordPath $uploadPath";
exec($command, $output, $exitCode);
if ($exitCode == 6) {
unlink($tempPasswordPath);
return actionResult(false, "Invalid password.");
}
// I'm not sure what other exit codes are possible here
if ($exitCode != 0) {
unlink($tempPasswordPath);
return actionResult(false, "Upload script returned an unknown exit code ($exitCode).");
}
unlink($tempPasswordPath);
return actionResult(true, "Upload success.");
}
function writeConfig($categories, $upstreamDns) {
global $domainRegEx;
$fileContents = "#[Options]\nbogus-priv\ndomain-needed\nno-resolv\n";
foreach ($categories as $title => $contents) {
$title = trim($title);
$contents = trim($contents);
if ($title == "" || $contents == "") {
continue;
}
$fileContents .= "\n#[Category: $title]\n";
foreach (preg_split("/\n/", $contents) as $line) {
$line = trim($line);
// e.g. "stackoverflow.com"
if (preg_match("/^($domainRegEx)$/", $line, $matches)) {
$domain = $matches[1];
$fileContents .= "server=/$domain/$upstreamDns\n";
unset($matches);
continue;
}
// e.g. "stackoverflow.com #comment"
if (preg_match("/^($domainRegEx) ?(#.*)$/", $line, $matches)) {
$domain = $matches[1];
$comment = $matches[2];
$fileContents .= "server=/$domain/$upstreamDns $comment\n";
unset($matches);
continue;
}
// e.g. "#comment"
if (preg_match("/^(#.*)$/", $line, $matches)) {
$comment = $matches[1];
$fileContents .= "\n$comment\n";
unset($matches);
continue;
}
}
}
return $fileContents;
}
if (isset($_POST["action"]) && $_POST["action"] == "uploadConfig") {
$actionResult = uploadConfig($settings["dnsServer"],
$settings["dnsService"],
$settings["expectPath"],
$settings["scpPath"],
$settings["sshPath"],
$settings["sshUser"],
"bin/upload.exp",
"temp",
$settings["uploadPath"],
$settings["upstreamDns"]);
}
if (isset($_POST["categories"])) {
$viewData["categories"] = $_POST["categories"];
} else {
$viewData["categories"] = readConfig($settings["downloadUrl"], $settings["upstreamDns"]);
}
?>