-
Notifications
You must be signed in to change notification settings - Fork 1
/
_statics.php
366 lines (282 loc) · 10.6 KB
/
_statics.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
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
<?php
require_once('include/common.php');
# Some functions in this script require higher privileges, but this
# should catch users not logged in for many cases.
AccessControl('2', null, false); # null means no log, false means don't redirect
$op = (empty($_GET['op'])) ? 'default' : $_GET['op'];
switch($op){
case "edit";
edit_static();
break;
case "edit_acl";
AccessControl('3', null, false); # null means no log, false means don't redirect
edit_acl();
break;
case "ping";
ping_host();
break;
case "guidance";
ip_guidance();
break;
case "edit_guidance";
AccessControl('3', null, false); # null means no log, false means don't redirect
edit_guidance();
break;
case "delete";
delete_static();
break;
case "delete_acl";
AccessControl('3', null, false); # null means no log, false means don't redirect
delete_acl();
break;
case "toggle_stale-scan";
toggle_stalescan();
break;
}
function edit_static(){
global $COLLATE;
$dbo = getdbo();
include 'include/validation_functions.php';
$static_id = (empty($_GET['static_id'])) ? '' : $_GET['static_id'];
$edit = (empty($_GET['edit'])) ? '' : $_GET['edit'];
$edit = ($edit == 'name') ? 'staticname' : $edit;
$value = (empty($_POST['value'])) ? '' : $_POST['value'];
$username = (isset($COLLATE['user']['username'])) ? $COLLATE['user']['username'] : 'unknown';
if(empty($static_id) || !is_numeric($static_id) || !preg_match('/staticname|contact|note/', $edit)){
header("HTTP/1.1 400 Bad Request");
echo $COLLATE['languages']['selected']['invalidrequest'];
exit();
}
$return = validate_text($value,$edit);
if($return['0'] === false){
header("HTTP/1.1 400 Bad Request");
echo $COLLATE['languages']['selected'][$return['error']];
exit();
}
else{
$value = $return['1'];
}
$result = $dbo -> query("SELECT ip, name FROM statics WHERE id='$static_id'");
list($long_ip, $name) = $result -> fetch(PDO::FETCH_NUM);
$static_ip = long2ip($long_ip);
if($edit == 'staticname'){
collate_log('2', "static IP $static_ip name changed from $name to $value");
$sql = "UPDATE statics SET name='$value', modified_by='$username', modified_at=NOW() WHERE id='$static_id'";
}
elseif($edit == 'contact'){
collate_log('2', "static IP $static_ip ($name) contact edited");
$sql = "UPDATE statics SET contact='$value', modified_by='$username', modified_at=NOW() WHERE id='$static_id'";
}
else{
collate_log('2', "static IP $static_ip ($name) note edited");
$sql = "UPDATE statics SET note='$value', modified_by='$username', modified_at=NOW() WHERE id='$static_id'";
}
$dbo -> query($sql);
echo $value;
exit();
} // Ends edit_static function
function edit_acl(){
global $COLLATE;
$dbo = getdbo();
include 'include/validation_functions.php';
$acl_id = (isset($_GET['acl_id']) && is_numeric($_GET['acl_id'])) ? $_GET['acl_id'] : '';
$value = (isset($_POST['value'])) ? $_POST['value'] : '';
if(empty($acl_id)){
header("HTTP/1.1 400 Bad Request");
echo $COLLATE['languages']['selected']['invalidrequest'];
exit();
}
$result = validate_text($value,'aclname');
if($result['0'] === false){
header("HTTP/1.1 400 Bad Request");
echo $COLLATE['languages']['selected'][$result['error']];
exit();
}
else{
$value = $result['1'];
}
$sql = "SELECT name FROM subnets WHERE id=(SELECT subnet_id FROM acl WHERE id='$acl_id')";
$result = $dbo -> query($sql);
if($result -> rowCount() != '1'){
header("HTTP/1.1 400 Bad Request");
echo $COLLATE['languages']['selected']['invalidrequest'];
exit();
}
$subnet_name = $result -> fetchColumn();
collate_log('3', "ACL statement name updated in $subnet_name subnet");
$sql = "UPDATE acl SET name='$value' where id='$acl_id'";
$dbo -> query($sql);
echo $value;
exit();
} // Ends edit_acl function
function ping_host(){
$ip = (empty($_GET['ip'])) ? "" : escapeshellcmd($_GET['ip']);
if(empty($ip)){ exit(); }
if(!ip2decimal($ip)){
header("HTTP/1.1 400 Bad Request");
echo $COLLATE['languages']['selected']['invalidrequest'];
exit();
}
echo "<pre>";
if (!strstr($_SERVER['DOCUMENT_ROOT'], ":")){ // *nix system
system ("ping -A -c 3 -n -W 1 $ip");
}
else{ // Windows system
system("ping -n 3 -w 500 $ip");
}
echo "</pre>";
exit();
} // Ends ping_host function
function ip_guidance(){
global $COLLATE;
$dbo = getdbo();
$subnet_id = (isset($_GET['subnet_id']) && is_numeric($_GET['subnet_id'])) ? $_GET['subnet_id'] : '';
if(empty($subnet_id)){
header("HTTP/1.1 400 Bad Request");
echo $COLLATE['languages']['selected']['invalidrequest'];
exit();
}
$sql = "SELECT guidance FROM subnets WHERE id='$subnet_id'";
$result = $dbo -> query($sql);
$guidance = $result -> fetchColumn();
if(!empty($guidance)){
$help = $guidance;
}
else{
$help = $COLLATE['settings']['guidance'];
}
echo "<pre><span id=\"guidance\">$help</span></pre>";
if($COLLATE['user']['accesslevel'] >= '3' || $COLLATE['settings']['perms'] > '3'){
echo "<script type=\"text/javascript\"><!--\n".
" new Ajax.InPlaceEditorWithEmptyText('guidance', '_statics.php?op=edit_guidance&subnet_id=$subnet_id',
{
clickToEditText: '".$COLLATE['languages']['selected']['ClicktoEdit']."',
highlightcolor: '#a5ddf8', rows: '7', cols: '49',
callback:
function(form) {
new Element.update('notice', '');
return Form.serialize(form);
},
onFailure:
function(transport) {
new Element.update('notice', transport.responseText.stripTags());
}}
);
--></script>";
}
exit();
} // Ends ip_guidance function
function edit_guidance(){
global $COLLATE;
$dbo = getdbo();
include 'include/validation_functions.php';
$subnet_id = (isset($_GET['subnet_id']) && is_numeric($_GET['subnet_id'])) ? $_GET['subnet_id'] : '';
$value = (empty($_POST['value'])) ? '' : clean($_POST['value']); # the guidance column is a longtext field, so is the settings value
$username = (isset($COLLATE['user']['username'])) ? $COLLATE['user']['username'] : 'unknown';
if(empty($subnet_id)){
header("HTTP/1.1 400 Bad Request");
echo $COLLATE['languages']['selected']['invalidrequest'];
exit();
}
$sql = "SELECT name FROM subnets WHERE id='$subnet_id'";
$result = $dbo -> query($sql);
if($result -> rowCount() != '1'){
header("HTTP/1.1 400 Bad Request");
echo $COLLATE['languages']['selected']['invalidrequest'];
exit();
}
$name = $result -> fetchColumn();
collate_log('3', "IP Guidance edited for $name subnet");
$sql = "UPDATE subnets SET guidance='$value', modified_by='$username', modified_at=NOW() WHERE id='$subnet_id'";
$dbo -> query($sql);
if(empty($value)){
$value = $COLLATE['settings']['guidance'];
}
echo $value;
exit();
} // End edit_guidance function
function delete_static(){
global $COLLATE;
$dbo = getdbo();
$long_ip = (isset($_GET['static_ip'])) ? ip2decimal($_GET['static_ip']) : '';
if(empty($long_ip) || $long_ip === false){
header("HTTP/1.1 400 Bad Request");
echo $COLLATE['languages']['selected']['invalidrequest'];
exit();
}
$static_ip = long2ip($long_ip);
$long_ip = ip2decimal($static_ip);
collate_log('2', "Static IP deleted: $static_ip");
$sql = "DELETE FROM statics WHERE ip='$long_ip' LIMIT 1";
$dbo -> query($sql);
// no success message. the user sees the static entry in the table fade away as feedback
exit();
} // Ends delete_static function
function delete_acl(){
global $COLLATE;
$dbo = getdbo();
$acl_id = (isset($_GET['acl_id']) && is_numeric($_GET['acl_id'])) ? $_GET['acl_id'] : '';
if(empty($acl_id)){
header("HTTP/1.1 400 Bad Request");
echo $COLLATE['languages']['selected']['invalidrequest'];
exit();
}
$sql = "SELECT name FROM subnets WHERE id=(SELECT subnet_id FROM acl WHERE id='$acl_id')";
$result = $dbo -> query($sql);
if($result -> rowCount() != '1'){
header("HTTP/1.1 400 Bad Request");
echo $COLLATE['languages']['selected']['invalidrequest'];
exit();
}
$subnet_name = $result -> fetchColumn();
collate_log('3', "ACL Statement #$acl_id deleted in $subnet_name subnet");
$sql = "DELETE FROM acl WHERE id='$acl_id'";
$dbo -> query($sql);
exit();
} // Ends delete_acl function
function toggle_stalescan(){
global $COLLATE;
global $dbo;
$static_ip = (isset($_GET['static_ip'])) ? $_GET['static_ip'] : '';
$long_ip = ip2decimal($static_ip);
$referer = (isset($_SERVER['HTTP_REFERER'])) ? $_SERVER['HTTP_REFERER'] : './search.php?notice=';
if(stristr($referer, 'notice')){
$referer=preg_replace("/¬ice=.*/", "", $referer);
}
$referer=$referer.'¬ice=';
if(empty($long_ip) || $long_ip === false){
header("HTTP/1.1 400 Bad Request");
echo "test1";
exit();
}
# make sure we aren't being asked to toggle for a subnet that has stale scan disabled:
$sql = "SELECT stalescan_enabled FROM subnets WHERE
CAST('$long_ip' & 0xFFFFFFFF AS UNSIGNED) & CAST(mask & 0xFFFFFFFF AS UNSIGNED) = CAST(start_ip & 0xFFFFFFFF AS UNSIGNED)";
$result = $dbo -> query($sql);
$subnet_status = $result -> fetchColumn();
if($subnet_status == false){
header("HTTP/1.1 400 Bad Request");
exit();
}
$sql = "SELECT failed_scans from statics where ip='$long_ip'";
$result = $dbo -> query($sql);
$current_count = $result -> fetchColumn();
if($current_count == -1){
$new_status = 'on';
$new_count = 0;
$new_icon = 'scanning.png';
$new_icon_text = $COLLATE['languages']['selected']['disablestalescan'];
}
else{
$new_status = 'off';
$new_count = -1;
$new_icon = 'skipping.png';
$new_icon_text = $COLLATE['languages']['selected']['enablestalescan'];
}
collate_log('2', "Stale Scan toggled $new_status for IP: $static_ip");
$sql = "UPDATE statics SET failed_scans='$new_count' WHERE ip='$long_ip' LIMIT 1";
$dbo -> query($sql);
echo "<img src=\"./images/$new_icon\" alt=\"\" title=\"$new_icon_text\" />";
exit();
} // Ends delete_static function
?>