Project

General

Profile

Download (2.69 KB) Statistics
| Branch: | Tag: | Revision:
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-2020 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("util.inc");
27
require_once("pfsense-utils.inc");
28

    
29
if (!is_array($config['aliases']['alias'])) {
30
	// No aliases
31
	return;
32
}
33

    
34
// Gather list of urltable aliases
35
$todo = array();
36
foreach ($config['aliases']['alias'] as $alias) {
37
	if (preg_match('/urltable/i', $alias['type'])) {
38
		$tmp = array();
39
		$tmp['type'] = $alias['type'];
40
		$tmp['name'] = $alias['name'];
41
		$tmp['url']  = $alias['url'];
42
		$tmp['freq'] = $alias['updatefreq'];
43
		$todo[] = $tmp;
44
	}
45
}
46

    
47
if (count($todo) > 0) {
48
	log_error("{$argv[0]}: Starting up.");
49

    
50
	if ($argv[1] != "now") {
51
		// Wait a little before updating.
52
		$wait = mt_rand(5, 60);
53
		log_error("{$argv[0]}: Sleeping for {$wait} seconds.");
54
		sleep($wait);
55
	}
56

    
57
	// Set whether or not to force the table update before it's time.
58
	if (!empty($argv[2]) && ($argv[2] == "forceupdate")) {
59
		$forceupdate = true;
60
	} else {
61
		$forceupdate = false;
62
	}
63

    
64
	log_error("{$argv[0]}: Starting URL table alias updates");
65

    
66
	$filter_reload = false;
67
	foreach ($todo as $t) {
68

    
69
		// Update a specifically named URL table only.
70
		if (!empty($argv[3]) && ($argv[3] != $t['name'])) {
71
			continue;
72
		}
73

    
74
		$r = process_alias_urltable($t['name'], $t['type'], $t['url'], $t['freq'], $forceupdate);
75
		if ($r == 1) {
76
			$result = "";
77
			// TODO: Change it when pf supports tables with ports
78
			if ($t['type'] == "urltable") {
79
				exec("/sbin/pfctl -t " . escapeshellarg($t['name']) . " -T replace -f /var/db/aliastables/" . escapeshellarg($t['name']) . ".txt 2>&1", $result);
80
			} else {
81
				$filter_reload = true;
82
			}
83
			log_error("{$argv[0]}: Updated {$t['name']} content from {$t['url']}: {$result[0]}");
84
		} elseif ($r == -1) {
85
			log_error("{$argv[0]}: {$t['name']} does not need updating.");
86
		} else {
87
			log_error("{$argv[0]}: ERROR: could not update {$t['name']} content from {$t['url']}");
88
		}
89
	}
90

    
91
	if ($filter_reload) {
92
		send_event("filter reload");
93
	}
94
}
95
?>
(80-80/82)