Project

General

Profile

Download (2.73 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 38809d47 Renato Botelho do Couto
 * Copyright (c) 2010-2013 BSD Perimeter
8
 * Copyright (c) 2013-2016 Electric Sheep Fencing
9 a68f7a3d Luiz Otavio O Souza
 * Copyright (c) 2014-2024 Rubicon Communications, LLC (Netgate)
10 ac24dc24 Renato Botelho
 * All rights reserved.
11
 *
12 b12ea3fb Renato Botelho
 * 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 ac24dc24 Renato Botelho
 *
16 b12ea3fb Renato Botelho
 * http://www.apache.org/licenses/LICENSE-2.0
17 ac24dc24 Renato Botelho
 *
18 b12ea3fb Renato Botelho
 * 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 ac24dc24 Renato Botelho
 */
24
25 c7de8be4 jim-p
require_once("config.inc");
26 d16f73ce Reid Linnemann
require_once("config.lib.inc");
27 c7de8be4 jim-p
require_once("util.inc");
28
require_once("pfsense-utils.inc");
29
30 d16f73ce Reid Linnemann
$aliases = config_get_path('aliases/alias');
31
if (!is_array($aliases)) {
32 c7de8be4 jim-p
	// No aliases
33 9ea554ee Ermal LUÇI
	return;
34 c7de8be4 jim-p
}
35
36
// Gather list of urltable aliases
37
$todo = array();
38 d16f73ce Reid Linnemann
foreach ($aliases as $alias) {
39 dd042c51 Renato Botelho
	if (preg_match('/urltable/i', $alias['type'])) {
40 c7de8be4 jim-p
		$tmp = array();
41 dd042c51 Renato Botelho
		$tmp['type'] = $alias['type'];
42 c7de8be4 jim-p
		$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 f6622167 NOYB
	// 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 c7de8be4 jim-p
	log_error("{$argv[0]}: Starting URL table alias updates");
67
68 dd042c51 Renato Botelho
	$filter_reload = false;
69 c7de8be4 jim-p
	foreach ($todo as $t) {
70 f6622167 NOYB
71
		// Update a specifically named URL table only.
72
		if (!empty($argv[3]) && ($argv[3] != $t['name'])) {
73
			continue;
74
		}
75
76 3b07f4fe NOYB
		$r = process_alias_urltable($t['name'], $t['type'], $t['url'], $t['freq'], $forceupdate);
77 c7de8be4 jim-p
		if ($r == 1) {
78
			$result = "";
79 dd042c51 Renato Botelho
			// TODO: Change it when pf supports tables with ports
80 e173dd74 Phil Davis
			if ($t['type'] == "urltable") {
81 dd042c51 Renato Botelho
				exec("/sbin/pfctl -t " . escapeshellarg($t['name']) . " -T replace -f /var/db/aliastables/" . escapeshellarg($t['name']) . ".txt 2>&1", $result);
82 e173dd74 Phil Davis
			} else {
83 dd042c51 Renato Botelho
				$filter_reload = true;
84 e173dd74 Phil Davis
			}
85 c7de8be4 jim-p
			log_error("{$argv[0]}: Updated {$t['name']} content from {$t['url']}: {$result[0]}");
86
		} elseif ($r == -1) {
87 869dfb66 Renato Botelho
			log_error("{$argv[0]}: {$t['name']} does not need updating.");
88 c7de8be4 jim-p
		} else {
89
			log_error("{$argv[0]}: ERROR: could not update {$t['name']} content from {$t['url']}");
90
		}
91
	}
92 dd042c51 Renato Botelho
93 e173dd74 Phil Davis
	if ($filter_reload) {
94 dd042c51 Renato Botelho
		send_event("filter reload");
95 e173dd74 Phil Davis
	}
96 c7de8be4 jim-p
}
97 dd042c51 Renato Botelho
?>