1
|
#!/usr/local/bin/php-cgi -q
|
2
|
<?php
|
3
|
/*
|
4
|
* rc.update_urltables
|
5
|
*
|
6
|
* part of pfSense (https://www.pfsense.org)
|
7
|
* Copyright (c) 2010-2013 BSD Perimeter
|
8
|
* Copyright (c) 2013-2016 Electric Sheep Fencing
|
9
|
* Copyright (c) 2014-2024 Rubicon Communications, LLC (Netgate)
|
10
|
* All rights reserved.
|
11
|
*
|
12
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
13
|
* you may not use this file except in compliance with the License.
|
14
|
* You may obtain a copy of the License at
|
15
|
*
|
16
|
* http://www.apache.org/licenses/LICENSE-2.0
|
17
|
*
|
18
|
* Unless required by applicable law or agreed to in writing, software
|
19
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
20
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
21
|
* See the License for the specific language governing permissions and
|
22
|
* limitations under the License.
|
23
|
*/
|
24
|
|
25
|
require_once("config.inc");
|
26
|
require_once("config.lib.inc");
|
27
|
require_once("util.inc");
|
28
|
require_once("pfsense-utils.inc");
|
29
|
|
30
|
$aliases = config_get_path('aliases/alias');
|
31
|
if (!is_array($aliases)) {
|
32
|
// No aliases
|
33
|
return;
|
34
|
}
|
35
|
|
36
|
// Gather list of urltable aliases
|
37
|
$todo = array();
|
38
|
foreach ($aliases as $alias) {
|
39
|
if (preg_match('/urltable/i', $alias['type'])) {
|
40
|
$tmp = array();
|
41
|
$tmp['type'] = $alias['type'];
|
42
|
$tmp['name'] = $alias['name'];
|
43
|
$tmp['url'] = $alias['url'];
|
44
|
$tmp['freq'] = $alias['updatefreq'];
|
45
|
$todo[] = $tmp;
|
46
|
}
|
47
|
}
|
48
|
|
49
|
if (count($todo) > 0) {
|
50
|
log_error("{$argv[0]}: Starting up.");
|
51
|
|
52
|
if ($argv[1] != "now") {
|
53
|
// Wait a little before updating.
|
54
|
$wait = mt_rand(5, 60);
|
55
|
log_error("{$argv[0]}: Sleeping for {$wait} seconds.");
|
56
|
sleep($wait);
|
57
|
}
|
58
|
|
59
|
// Set whether or not to force the table update before it's time.
|
60
|
if (!empty($argv[2]) && ($argv[2] == "forceupdate")) {
|
61
|
$forceupdate = true;
|
62
|
} else {
|
63
|
$forceupdate = false;
|
64
|
}
|
65
|
|
66
|
log_error("{$argv[0]}: Starting URL table alias updates");
|
67
|
|
68
|
$filter_reload = false;
|
69
|
foreach ($todo as $t) {
|
70
|
|
71
|
// Update a specifically named URL table only.
|
72
|
if (!empty($argv[3]) && ($argv[3] != $t['name'])) {
|
73
|
continue;
|
74
|
}
|
75
|
|
76
|
$r = process_alias_urltable($t['name'], $t['type'], $t['url'], $t['freq'], $forceupdate);
|
77
|
if ($r == 1) {
|
78
|
$result = "";
|
79
|
// TODO: Change it when pf supports tables with ports
|
80
|
if ($t['type'] == "urltable") {
|
81
|
exec("/sbin/pfctl -t " . escapeshellarg($t['name']) . " -T replace -f /var/db/aliastables/" . escapeshellarg($t['name']) . ".txt 2>&1", $result);
|
82
|
} else {
|
83
|
$filter_reload = true;
|
84
|
}
|
85
|
log_error("{$argv[0]}: Updated {$t['name']} content from {$t['url']}: {$result[0]}");
|
86
|
} elseif ($r == -1) {
|
87
|
log_error("{$argv[0]}: {$t['name']} does not need updating.");
|
88
|
} else {
|
89
|
log_error("{$argv[0]}: ERROR: could not update {$t['name']} content from {$t['url']}");
|
90
|
}
|
91
|
}
|
92
|
|
93
|
if ($filter_reload) {
|
94
|
send_event("filter reload");
|
95
|
}
|
96
|
}
|
97
|
?>
|