#!/usr/local/bin/php-cgi -q
<?php
/*
 * aliasmod
 *
 * part of pfSense (https://www.pfsense.org)
 * Copyright (c) 2010-2013 BSD Perimeter
 * Copyright (c) 2013-2016 Electric Sheep Fencing
 * Copyright (c) 2014-2021 Rubicon Communications, LLC (Netgate)
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


require_once("globals.inc");
require_once("config.inc");
require_once("pfsense-utils.inc");
require_once("filter.inc");

$message = "";

if (($argc > 1) && !empty($argv[1])) {
	$message = "";
	$message = aliasmod($argv[1], $argv[2], $argv[3]);
	echo $message . "\n";
} else {
	// Print usage:
	echo "usage:\n";
	echo " Add IP/FQDN entry to the Alias\n";
	echo "     " . basename($argv[0]) . " add <alias> <IP/FQDN>\n";
	echo "\n";
	echo " Remove IP/FQDN entry from the Alias\n";
	echo "     " . basename($argv[0]) . " del <alias> <IP/FQDN>\n";
	echo "\n";
	echo " Add example:\n";
	echo "     " . basename($argv[0]) . " add publicdns 1.1.1.1\n";
	echo "\n";
	echo " Remove example:\n";
	echo "     " . basename($argv[0]) . " del localservers 192.168.1.10\n";
	echo "\n";
}

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

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

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

	if (!is_ipaddr($entry) && !is_domain($entry)) {
		return false;
	}

	foreach ($config['aliases']['alias'] as & $als) {
		if ($als['name'] != $alias) {
			continue;
		}
		$addresses = explode(' ', $als['address']);
		$details = explode('||', $als['detail']);
		if (($act == 'add') && !in_array($entry, $addresses)) {
			$als['address'] = implode(' ', array_merge($addresses, array($entry)));
			$als['detail'] = implode('||', array_merge($details, array(sprintf(gettext("Entry added %s"), date('r')))));
			break;
		} elseif (($act == 'del') && in_array($entry, $addresses)) {
			foreach ($addresses as $id => $addr) {
				if ($addr == $entry) {
					unset($details[$id]);
					unset($addresses[$id]);
					break;
				}
			}
			$als['address'] = implode(' ', $addresses);
			$als['detail'] = implode('||', $details);
			break;
		} else {
			return false;
		}
	}
	write_config(gettext("Edited a firewall alias by aliasmod."));

	$retval = 0;
        $retval |= filter_configure();

        if ($retval == 0) {
                clear_subsystem_dirty('aliases');
        }
}
?>
