Project

General

Profile

Download (2.93 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php-cgi -q
2
<?php
3
/*
4
 * aliasmod
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-2022 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

    
26
require_once("globals.inc");
27
require_once("config.inc");
28
require_once("pfsense-utils.inc");
29
require_once("filter.inc");
30

    
31
$message = "";
32

    
33
if (($argc > 1) && !empty($argv[1])) {
34
	$message = "";
35
	$message = aliasmod($argv[1], $argv[2], $argv[3]);
36
	echo $message . "\n";
37
} else {
38
	// Print usage:
39
	echo "usage:\n";
40
	echo " Add IP/FQDN entry to the Alias\n";
41
	echo "     " . basename($argv[0]) . " add <alias> <IP/FQDN>\n";
42
	echo "\n";
43
	echo " Remove IP/FQDN entry from the Alias\n";
44
	echo "     " . basename($argv[0]) . " del <alias> <IP/FQDN>\n";
45
	echo "\n";
46
	echo " Add example:\n";
47
	echo "     " . basename($argv[0]) . " add publicdns 1.1.1.1\n";
48
	echo "\n";
49
	echo " Remove example:\n";
50
	echo "     " . basename($argv[0]) . " del localservers 192.168.1.10\n";
51
	echo "\n";
52
}
53

    
54
function aliasmod($act, $alias, $entry) {
55
	global $reserved_table_names, $config, $g;
56

    
57
	if (!in_array($act, array('add', 'del'))) {
58
		return false;
59
	}
60

    
61
	if (!is_array($config['aliases']) || !is_alias($alias) ||
62
	    in_array($alias, $reserved_table_names) || (alias_get_type($alias) != 'host')) {
63
		return false;
64
	}
65

    
66
	if (!is_ipaddr($entry) && !is_domain($entry)) {
67
		return false;
68
	}
69

    
70
	foreach ($config['aliases']['alias'] as & $als) {
71
		if ($als['name'] != $alias) {
72
			continue;
73
		}
74
		$addresses = explode(' ', $als['address']);
75
		$details = explode('||', $als['detail']);
76
		if (($act == 'add') && !in_array($entry, $addresses)) {
77
			$als['address'] = implode(' ', array_merge($addresses, array($entry)));
78
			$als['detail'] = implode('||', array_merge($details, array(sprintf(gettext("Entry added %s"), date('r')))));
79
			break;
80
		} elseif (($act == 'del') && in_array($entry, $addresses)) {
81
			foreach ($addresses as $id => $addr) {
82
				if ($addr == $entry) {
83
					unset($details[$id]);
84
					unset($addresses[$id]);
85
					break;
86
				}
87
			}
88
			$als['address'] = implode(' ', $addresses);
89
			$als['detail'] = implode('||', $details);
90
			break;
91
		} else {
92
			return false;
93
		}
94
	}
95
	write_config(gettext("Edited a firewall alias by aliasmod."));
96

    
97
	$retval = 0;
98
        $retval |= filter_configure();
99

    
100
        if ($retval == 0) {
101
                clear_subsystem_dirty('aliases');
102
        }
103
}
104
?>
(2-2/19)