= 0) { if (file_exists("{$log_file}.{$i}")) { rename("{$log_file}.{$i}", "{$log_file}" . ($i + 1)); } $i = $i - 1; } // rotate current log if (file_exists("$log_file")) { rename("{$log_file}", "{$log_file}.0"); } } function run_sarg($id = -1) { global $config, $g, $sarg_proxy; // mount filesystem writeable conf_mount_rw(); $cmd = SARG_DIR . "/bin/sarg"; if ($id >= 0 && is_array($config['installedpackages']['sargschedule']['config'])) { $args = $config['installedpackages']['sargschedule']['config'][$id]['args']; $action = $config['installedpackages']['sargschedule']['config'][$id]['action']; $gzip = $config['installedpackages']['sargschedule']['config'][$id]['gzip']; $find = $config['installedpackages']['sargschedule']['config'][$id]['find']; $gziplevel = $config['installedpackages']['sargschedule']['config'][$id]['gziplevel']; $daylimit = $config['installedpackages']['sargschedule']['config'][$id]['daylimit']; } else { $args = $_POST['args']; $action = $_POST['action']; $gzip = $_POST['gzip']; $find = $_POST['find']; $gziplevel = $_POST['gziplevel']; $daylimit = ""; } $find = (preg_match("/(\d+)/", $find, $find_matches) ? $find_matches[1] : "60"); log_error("Sarg: force refresh now with {$args} args, compress({$gzip}) and {$action} action after sarg finish."); $gzip_script = "#!/bin/sh\n"; if ($gzip == "on") { // remove old file if exists unlink_if_exists("/root/sarg_run_{$id}.sh"); $gzip_script .= << "*/" . $matches[1], "hour" => "*", "mday" => "*", "month" => "*", "wday" => "*", "who" => "root", "command" => $cron_cmd . " " . $sarg_schedule_id); $config['cron'] = $new_cron; $cron_found++; } if (preg_match('/(\d+)h/', $sarg_schedule['frequency'], $matches) && $sarg_schedule['enable']) { $new_cron['item'][] = array("minute" => "0", "hour" => "*/" . $matches[1], "mday" => "*", "month" => "*", "wday" => "*", "who" => "root", "command" => $cron_cmd . " " . $sarg_schedule_id); $config['cron'] = $new_cron; $cron_found++; } if (preg_match('/(\d+)d/', $sarg_schedule['frequency'], $matches) && $sarg_schedule['enable']) { $new_cron['item'][] = array("minute" => "0", "hour" => "0", "mday" => "*/" . $matches[1], "month" => "*", "wday" => "*", "who" => "root", "command"=> $cron_cmd . " " . $sarg_schedule_id); $config['cron']=$new_cron; $cron_found++; } $sarg_schedule_id++; } } // update cron if ($cron_found > 0) { $config['cron'] = $new_cron; write_config(); configure_cron(); } } // write config if any file from filesystem was loaded if ($update_conf > 0) { write_config(); } // mount filesystem readonly conf_mount_ro(); sarg_sync_on_changes(); } function sarg_validate_input($post, &$input_errors) { global $config, $g; foreach ($post as $key => $value) { if (empty($value)) { continue; } // check dansguardian if (substr($key, 0, 12) == "proxy_server" && $value == "dansguardian") { if (is_array($config['installedpackages']['dansguardianlog'])) { if ($config['installedpackages']['dansguardianlog']['config'][0]['logfileformat'] != 3) { $input_errors[] = 'Sarg is only compatible with dansguardian squid mode log'; $input_errors[] = 'Please change it on Services -> Dansguardian -> Report and log -> Log file format'; } } else { $input_errors[]='dansguardian package not detected'; } } # check squidguard if (substr($key, 0, 10) == "proxy_server" && $value == "squidguard") if (!is_array($config['installedpackages']['squidguardgeneral'])) $input_errors[]='squidguard package not detected'; # check squid if (substr($key, 0, 5) == "proxy_server" && $value == "squid") { if (is_array($config['installedpackages']['squid'])) { if (!$config['installedpackages']['squid']['log_enabled']) { $input_errors[]='squidlogs not enabled'; } } else { $input_errors[]='squid package not installed'; } } if (substr($key, 0, 11) == "description" && !preg_match("@^[a-zA-Z0-9 _/.-]+$@", $value)) { $input_errors[] = "Do not use special characters in description"; } if (substr($key, 0, 8) == "fullfile" && !preg_match("@^[a-zA-Z0-9_/.-]+$@", $value)) { $input_errors[] = "Do not use special characters in filename"; } // check cron option if ($key == "frequency" && (!preg_match("/^\d+(h|m|d)$/", $value) || $value == 0)) { $input_errors[] = "A valid number with a time reference is required for the field 'Update Frequency'"; } } } /* Uses XMLRPC to synchronize the changes to a remote node */ function sarg_sync_on_changes() { global $config, $g; if (is_array($config['installedpackages']['sargsync']['config'])) { $sarg_sync = $config['installedpackages']['sargsync']['config'][0]; $synconchanges = $sarg_sync['synconchanges']; $synctimeout = $sarg_sync['synctimeout']; switch ($synconchanges) { case "manual": if (is_array($sarg_sync[row])) { $rs = $sarg_sync[row]; } else { log_error("[sarg] xmlrpc sync is enabled but there is no hosts to push on sarg config."); return; } break; case "auto": if (is_array($config['installedpackages']['carpsettings']) && is_array($config['installedpackages']['carpsettings']['config'])) { $system_carp = $config['installedpackages']['carpsettings']['config'][0]; $rs[0]['ipaddress'] = $system_carp['synchronizetoip']; $rs[0]['username'] = $system_carp['username']; $rs[0]['password'] = $system_carp['password']; if ($system_carp['synchronizetoip'] == "" || $system_carp['username'] == "") { log_error("[sarg] xmlrpc sync is enabled but there are no system backup hosts to push sarg config."); return; } } else { log_error("[sarg] xmlrpc sync is enabled but there are no system backup hosts to push sarg config."); return; } break; default: return; break; } if (is_array($rs)) { log_error("[sarg] xmlrpc sync is starting."); foreach($rs as $sh) { $sync_to_ip = $sh['ipaddress']; $password = $sh['password']; if ($sh['username']) { $username = $sh['username']; } else { $username = 'admin'; } if ($password && $sync_to_ip) { sarg_do_xmlrpc_sync($sync_to_ip, $username, $password,$synctimeout); } } log_error("[sarg] xmlrpc sync is ending."); } } } /* do the actual XMLRPC sync */ function sarg_do_xmlrpc_sync($sync_to_ip, $username, $password,$synctimeout) { global $config, $g; if (!$username) { return; } if (!$password) { return; } if (!$sync_to_ip) { return; } if (!$synctimeout) { $synctimeout="250"; } $xmlrpc_sync_neighbor = $sync_to_ip; if ($config['system']['webgui']['protocol'] != "") { $synchronizetoip = $config['system']['webgui']['protocol']; $synchronizetoip .= "://"; } $port = $config['system']['webgui']['port']; /* if port is empty, let's rely on the protocol selection */ if ($port == "") { if ($config['system']['webgui']['protocol'] == "http") { $port = "80"; } else { $port = "443"; } } $synchronizetoip .= $sync_to_ip; /* xml will hold the sections to sync */ $xml = array(); $xml['sarg'] = $config['installedpackages']['sarg']; $xml['sarguser'] = $config['installedpackages']['sarguser']; /* assemble xmlrpc payload */ $params = array( XML_RPC_encode($password), XML_RPC_encode($xml) ); /* set a few variables needed for sync code */ $url = $synchronizetoip; log_error("Beginning sarg XMLRPC sync to {$url}:{$port}."); $method = 'pfsense.merge_installedpackages_section_xmlrpc'; $msg = new XML_RPC_Message($method, $params); $cli = new XML_RPC_Client('/xmlrpc.php', $url, $port); $cli->setCredentials($username, $password); if ($g['debug']) { $cli->setDebug(1); } /* send our XMLRPC message and timeout after $synctimeout seconds */ $resp = $cli->send($msg, $synctimeout); if (!$resp) { $error = "A communications error occurred while attempting sarg XMLRPC sync with {$url}:{$port}."; log_error($error); file_notice("sync_settings", $error, "sarg Settings Sync", ""); } elseif ($resp->faultCode()) { $cli->setDebug(1); $resp = $cli->send($msg, $synctimeout); $error = "An error code was received while attempting sarg XMLRPC sync with {$url}:{$port} - Code " . $resp->faultCode() . ": " . $resp->faultString(); log_error($error); file_notice("sync_settings", $error, "sarg Settings Sync", ""); } else { log_error("sarg XMLRPC sync successfully completed with {$url}:{$port}."); } /* tell sarg to reload our settings on the destionation sync host. */ $method = 'pfsense.exec_php'; $execcmd = "require_once('/usr/local/pkg/sarg.inc');\n"; $execcmd .= "sync_package_sarg();"; /* assemble xmlrpc payload */ $params = array( XML_RPC_encode($password), XML_RPC_encode($execcmd) ); log_error("sarg XMLRPC reload data {$url}:{$port}."); $msg = new XML_RPC_Message($method, $params); $cli = new XML_RPC_Client('/xmlrpc.php', $url, $port); $cli->setCredentials($username, $password); $resp = $cli->send($msg, $synctimeout); if (!$resp) { $error = "A communications error occurred while attempting sarg XMLRPC sync with {$url}:{$port} (pfsense.exec_php)."; log_error($error); file_notice("sync_settings", $error, "sarg Settings Sync", ""); } elseif ($resp->faultCode()) { $cli->setDebug(1); $resp = $cli->send($msg, $synctimeout); $error = "An error code was received while attempting sarg XMLRPC sync with {$url}:{$port} - Code " . $resp->faultCode() . ": " . $resp->faultString(); log_error($error); file_notice("sync_settings", $error, "sarg Settings Sync", ""); } else { log_error("sarg XMLRPC reload data success with {$url}:{$port} (pfsense.exec_php)."); } } ?>