Copyright (C) 2013 Marcello Coutinho Copyright (C) 2015 ESF, LLC All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ require_once('config.inc'); require_once('filter.inc'); require_once('globals.inc'); require_once("notices.inc"); require_once('pfsense-utils.inc'); require_once('pkg-utils.inc'); require_once('service-utils.inc'); require_once('util.inc'); require_once("xmlrpc.inc"); require_once("xmlrpc_client.inc"); require_once('squidguard_configurator.inc'); # ------------------------------------------------------------------------------ # fields define('F_NAME', 'name'); define('F_DEST', 'dest'); define('F_SOURCE', 'source'); define('F_DESTINATION', 'dest'); define('F_REWRITE', 'rewrite'); define('F_REDIRECT', 'redirect'); define('F_TIME', 'time'); define('F_OVERDESTINATION', 'overdestination'); define('F_OVERREWRITE', 'overrewrite'); define('F_OVERREDIRECT', 'overredirect'); define('F_TARGETURL', 'targeturl'); define('F_REPLACETO', 'replaceto'); define('F_TIMETYPE', 'timetype'); define('F_TIMEDAYS', 'timedays'); define('F_DATERANGE', 'daterange'); define('F_TIMERANGE', 'sg_timerange'); define('F_IPLIST', 'iplist'); define('F_DESCRIPTION', 'description'); define('F_EXPRESSIONS', 'expressions'); define('F_DOMAINS', 'domains'); define('F_URLS', 'urls'); define('F_DISABLED', 'disabled'); define('F_SQUIDGUARDENABLE', 'squidguard_enable'); define('F_BLACKLIST', 'blacklist'); # prefixes define('PREF_UPTIME', 'uptime_'); define('PREF_UPTIME_DENY', 'uptimedeny_'); define('PREF_OVERTIME', 'overtime_'); define('PREF_OVERTIME_DENY', 'overtimedeny_'); # modules define('MODULE_GENERAL', 'squidguardgeneral'); define('MODULE_DEFAULT', 'squidguarddefault'); define('MODULE_ACL', 'squidguardacl'); define('MODULE_DESTINATION', 'squidguarddest'); define('MODULE_REWRITE', 'squidguardrewrite'); define('MODULE_SOURCE', 'squidguardsrc'); define('MODULE_TIME', 'squidguardtime'); define('MODULE_LOG', 'squidguardlog'); # blacklist define('BLACKLIST_DEFAULT_URL', 'http://squidguard.mesd.k12.or.us/blacklists.tgz'); # 5Mb define('BLACKLIST_DEFAULT_URL1', 'http://www.shallalist.de/Downloads/shallalist.tar.gz'); # ~7Mb define('BLACKLIST_TMP_FILE', '/var/tmp/blacklists.tar.gz'); define('BLACKLIST_BTN_URL', 'Upload Url'); define('BLACKLIST_BTN_DEFAULT', 'Restore default'); define('BLACKLIST_LOGFILE', 'blacklist.log'); # define('APPLY_BTN', 'Apply'); define('SAFESEARCH', 'safesearch'); # ============================================================================== # Initialization # ============================================================================== # use global variable $squidguard_config, defined in squidguard_configurator.inc sg_init(convert_pfxml_to_sgxml()); # ============================================================================== # Validations # ============================================================================== function squidguard_validate(&$post, &$input_errors) { global $config, $g; $submit = isset($_GET['submit']) ? $_GET['submit'] : $_POST['submit']; # check config if 'Apply' if ($submit === APPLY_BTN) { // XXX:Bug #4608 if (($g['platform'] == "pfSense") && isset($config['system']['use_mfs_tmpvar'])) { $input_errors[] = "Please, disable 'Use RAM Disks' under System: Advanced: Miscellaneous to avoid SquidGuard configuration breakage on reboot."; } if ($g['platform'] != "pfSense") { if ($post['blacklist'] != "" || $post['blacklist_proxy'] != "" || $post['blacklist_url'] != "") { $input_errors[] = "Blacklists usage is NOT supported on NanoBSD. Disable 'Blacklist' option in 'General settings'."; } } sg_check_config_data($input_errors); } } # ------------------------------------------------------------------------------ # validate default # ------------------------------------------------------------------------------ function squidguard_validate_default(&$post, &$input_errors) { squidguard_validate_acl($post, $input_errors); } # ------------------------------------------------------------------------------ # validate acl # ------------------------------------------------------------------------------ function squidguard_validate_acl(&$post, &$input_errors) { $pass_up = array(); $deny_up = array(); $pass_up_val = ''; $pass_over = array(); $deny_over = array(); $pass_over_val = ''; $id = get_item_id(); # check name ('source') $name = trim($post[F_NAME]); if(!empty($name)) { # validate name format check_name_format($name, $input_errors); # check unique name if (!sg_check_unique_name(F_ACLS, $name)) $input_errors[] = "Name '$name' already exists."; # check reserved if (!sg_check_reserved_name($name)) $input_errors[] = "Name '$name' is reserved."; # check source $sgx = array(); $sgx[F_NAME] = $post[F_NAME]; $sgx[F_SOURCE] = $post[F_SOURCE]; sg_check_src($sgx, $input_errors); } # store destinations to 'dest' value foreach ($post as $key => $val) { if (substr_count($key, PREF_UPTIME) != 0) { $name = str_replace(PREF_UPTIME, '', $key); if ($name) { switch($val) { case "allow": $pass_up_val .= " $name"; break; case "white": $pass_up_val .= " ^$name"; break; case "deny" : $pass_up_val .= " !$name"; break; } } } elseif (substr_count($key, PREF_OVERTIME) != 0) { $name = str_replace(PREF_OVERTIME, '', $key); if ($name) { switch($val) { case "allow": $pass_over_val .= " $name"; break; case "white": $pass_over_val .= " ^$name"; break; case "deny" : $pass_over_val .= " !$name"; break; } } } } # !ATTENTION! on pfSense XML config must be must(shell) be '!all' instead of 'none' - it is a must for correct work GUI # if not exists key 'all', then add 'none' - default 'deny all' if ((substr_count($pass_up_val, 'all') == 0)) { $pass_up_val .= ' !all'; } if (!empty($pass_over_val) && (substr_count($pass_over_val, 'all') == 0)) { $pass_over_val .= ' !all'; } if (empty($pass_over_val)) $post[F_DEST] = "$pass_up_val"; else $post[F_DEST] = "$pass_up_val [$pass_over_val]"; # check redirect $errmsg = ''; if (!sg_check_redirect($post[F_RMOD], $post[F_REDIRECT], $errmsg)) { $input_errors[] = "Redirect info error. $errmsg"; } } # ------------------------------------------------------------------------------ # validate times # Format: # date: