Project

General

Profile

Download (2.6 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-2016 Rubicon Communications, LLC (Netgate)
8
 * All rights reserved.
9
 *
10
 * Licensed under the Apache License, Version 2.0 (the "License");
11
 * you may not use this file except in compliance with the License.
12
 * You may obtain a copy of the License at
13
 *
14
 * http://www.apache.org/licenses/LICENSE-2.0
15
 *
16
 * Unless required by applicable law or agreed to in writing, software
17
 * distributed under the License is distributed on an "AS IS" BASIS,
18
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
 * See the License for the specific language governing permissions and
20
 * limitations under the License.
21
 */
22

    
23
require_once("config.inc");
24
require_once("util.inc");
25
require_once("pfsense-utils.inc");
26

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

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

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

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

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

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

    
64
	$filter_reload = false;
65
	foreach ($todo as $t) {
66

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

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

    
89
	if ($filter_reload) {
90
		send_event("filter reload");
91
	}
92
}
93
?>
(77-77/79)