Project

General

Profile

Download (2.6 KB) Statistics
| Branch: | Tag: | Revision:
1 cb7d18d5 Renato Botelho
#!/usr/local/bin/php-cgi -q
2 c7de8be4 jim-p
<?php
3 ac24dc24 Renato Botelho
/*
4
 * rc.update_urltables
5
 *
6
 * part of pfSense (https://www.pfsense.org)
7 81299b5c Renato Botelho
 * Copyright (c) 2010-2016 Rubicon Communications, LLC (Netgate)
8 ac24dc24 Renato Botelho
 * All rights reserved.
9
 *
10 b12ea3fb Renato Botelho
 * 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 ac24dc24 Renato Botelho
 *
14 b12ea3fb Renato Botelho
 * http://www.apache.org/licenses/LICENSE-2.0
15 ac24dc24 Renato Botelho
 *
16 b12ea3fb Renato Botelho
 * 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 ac24dc24 Renato Botelho
 */
22
23 c7de8be4 jim-p
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 9ea554ee Ermal LUÇI
	return;
30 c7de8be4 jim-p
}
31
32
// Gather list of urltable aliases
33
$todo = array();
34
foreach ($config['aliases']['alias'] as $alias) {
35 dd042c51 Renato Botelho
	if (preg_match('/urltable/i', $alias['type'])) {
36 c7de8be4 jim-p
		$tmp = array();
37 dd042c51 Renato Botelho
		$tmp['type'] = $alias['type'];
38 c7de8be4 jim-p
		$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 f6622167 NOYB
	// 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 c7de8be4 jim-p
	log_error("{$argv[0]}: Starting URL table alias updates");
63
64 dd042c51 Renato Botelho
	$filter_reload = false;
65 c7de8be4 jim-p
	foreach ($todo as $t) {
66 f6622167 NOYB
67
		// Update a specifically named URL table only.
68
		if (!empty($argv[3]) && ($argv[3] != $t['name'])) {
69
			continue;
70
		}
71
72 3b07f4fe NOYB
		$r = process_alias_urltable($t['name'], $t['type'], $t['url'], $t['freq'], $forceupdate);
73 c7de8be4 jim-p
		if ($r == 1) {
74
			$result = "";
75 dd042c51 Renato Botelho
			// TODO: Change it when pf supports tables with ports
76 e173dd74 Phil Davis
			if ($t['type'] == "urltable") {
77 dd042c51 Renato Botelho
				exec("/sbin/pfctl -t " . escapeshellarg($t['name']) . " -T replace -f /var/db/aliastables/" . escapeshellarg($t['name']) . ".txt 2>&1", $result);
78 e173dd74 Phil Davis
			} else {
79 dd042c51 Renato Botelho
				$filter_reload = true;
80 e173dd74 Phil Davis
			}
81 c7de8be4 jim-p
			log_error("{$argv[0]}: Updated {$t['name']} content from {$t['url']}: {$result[0]}");
82
		} elseif ($r == -1) {
83 869dfb66 Renato Botelho
			log_error("{$argv[0]}: {$t['name']} does not need updating.");
84 c7de8be4 jim-p
		} else {
85
			log_error("{$argv[0]}: ERROR: could not update {$t['name']} content from {$t['url']}");
86
		}
87
	}
88 dd042c51 Renato Botelho
89 e173dd74 Phil Davis
	if ($filter_reload) {
90 dd042c51 Renato Botelho
		send_event("filter reload");
91 e173dd74 Phil Davis
	}
92 c7de8be4 jim-p
}
93 dd042c51 Renato Botelho
?>