From 58af5941527bcea30718ddbc0f4a92847a319427 Mon Sep 17 00:00:00 2001 From: Scott Ullrich Date: Mon, 29 Jan 2007 22:03:56 +0000 Subject: PPPoE automatic disconnect MFC Submitted-by: DSH Really, really, really wanted by: Hoba Commit #1 of #2 --- usr/local/www/interfaces_wan.php | 329 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 315 insertions(+), 14 deletions(-) (limited to 'usr/local/www/interfaces_wan.php') diff --git a/usr/local/www/interfaces_wan.php b/usr/local/www/interfaces_wan.php index fb1a28a..8325493 100755 --- a/usr/local/www/interfaces_wan.php +++ b/usr/local/www/interfaces_wan.php @@ -31,6 +31,41 @@ POSSIBILITY OF SUCH DAMAGE. */ +define("CRON_MONTHLY_PATTERN", "0 0 1 * *"); +define("CRON_WEEKLY_PATTERN", "0 0 * * 0"); +define("CRON_DAILY_PATTERN", "0 0 * * *"); +define("CRON_HOURLY_PATTERN", "0 * * * *"); +define("CRON_PPPOE_CMD_FILE", "/etc/pppoerestart"); +define("CRON_PPPOE_CMD", "#!/bin/sh echo '' | /usr/local/bin/php -q"); + +function getMPDCRONSettings() { + global $config; + + if (is_array($config['cron']['item'])) { + for ($i = 0; $i < count($config['cron']['item']); $i++) { + $item =& $config['cron']['item'][$i]; + + if (strpos($item['command'], CRON_PPPOE_CMD_FILE) !== false) { + return array("ID" => $i, "ITEM" => $item); + } + } + } + + return NULL; +} + +function getMPDResetTimeFromConfig() { + $itemhash = getMPDCRONSettings(); + $cronitem = $itemhash['ITEM']; + + if (isset($cronitem)) { + + return "{$cronitem['minute']} {$cronitem['hour']} {$cronitem['mday']} {$cronitem['month']} {$cronitem['wday']}"; + } else { + return NULL; + } +} + require("guiconfig.inc"); $wancfg = &$config['interfaces']['wan']; @@ -42,6 +77,46 @@ $pconfig['provider'] = $config['pppoe']['provider']; $pconfig['pppoe_dialondemand'] = isset($config['pppoe']['ondemand']); $pconfig['pppoe_idletimeout'] = $config['pppoe']['timeout']; +/* ================================================ */ +/* = force a connection reset at a specific time? = */ +/* ================================================ */ + +if (isset($wancfg['pppoe']['pppoe-reset-type'])) { + $resetTime = getMPDResetTimeFromConfig(); + $pconfig['pppoe_preset'] = true; + + if ($wancfg['pppoe']['pppoe-reset-type'] == "custom") { + $resetTime_a = split(" ", $resetTime); + $pconfig['pppoe_pr_custom'] = true; + $pconfig['pppoe_resetminute'] = $resetTime_a[0]; + $pconfig['pppoe_resethour'] = $resetTime_a[1]; + + /* just initialize $pconfig['pppoe_resetdate'] if the + * coresponding item contains appropriate numeric values. + */ + if ($resetTime_a[2] <> "*" && $resetTime_a[3] <> "*") { + $pconfig['pppoe_resetdate'] = "{$resetTime_a[3]}/{$resetTime_a[2]}/" . date("Y"); + } + } else if ($wancfg['pppoe']['pppoe-reset-type'] == "preset") { + $pconfig['pppoe_pr_preset'] = true; + + switch ($resetTime) { + case CRON_MONTHLY_PATTERN: + $pconfig['pppoe_monthly'] = true; + break; + case CRON_WEEKLY_PATTERN: + $pconfig['pppoe_weekly'] = true; + break; + case CRON_DAILY_PATTERN: + $pconfig['pppoe_daily'] = true; + break; + case CRON_HOURLY_PATTERN: + $pconfig['pppoe_hourly'] = true; + break; + } + } +} + $pconfig['pptp_username'] = $config['pptp']['username']; $pconfig['pptp_password'] = $config['pptp']['password']; $pconfig['pptp_local'] = $config['pptp']['local']; @@ -91,6 +166,21 @@ if ($_POST) { unset($input_errors); $pconfig = $_POST; + + /* okay first of all, cause we are just hidding the PPPoE HTML + * fields releated to PPPoE resets, we are going to unset $_POST + * vars, if the reset feature should not be used. Otherwise the + * data validation procedure below, may trigger a false error + * message. + */ + if (empty($_POST['pppoe_preset'])) { + unset($_POST['pppoe_pr_type']); + unset($_POST['pppoe_resethour']); + unset($_POST['pppoe_resetminute']); + unset($_POST['pppoe_resetdate']); + unset($_POST['pppoe_pr_preset_val']); + unlink_if_exists(CRON_PPPOE_CMD_FILE); + } if($_POST['gateway'] and $pconfig['gateway'] <> $_POST['gateway']) { /* enumerate slbd gateways and make sure we are not creating a route loop */ @@ -165,6 +255,21 @@ if ($_POST) { if (($_POST['pppoe_idletimeout'] != "") && !is_numericint($_POST['pppoe_idletimeout'])) { $input_errors[] = "The idle timeout value must be an integer."; } + if ($_POST['pppoe_resethour'] <> "" && + !is_numericint($_POST['pppoe_resethour']) && + $_POST['pppoe_resethour'] >= 0 && + $_POST['pppoe_resethour'] <=23) { + $input_errors[] = gettext("A valid PPPoE reset hour must be specified (0-23)."); + } + if ($_POST['pppoe_resetminute'] <> "" && + !is_numericint($_POST['pppoe_resetminute']) && + $_POST['pppoe_resetminute'] >= 0 && + $_POST['pppoe_resetminute'] <=59) { + $input_errors[] = gettext("A valid PPPoE reset minute must be specified (0-59)."); + } + if ($_POST['pppoe_resetdate'] <> "" && !is_numeric(str_replace("/", "", $_POST['pppoe_resetdate']))) { + $input_errors[] = gettext("A valid PPPoE reset date must be specified (mm/dd/yyyy)."); + } if (($_POST['pptp_local'] && !is_ipaddr($_POST['pptp_local']))) { $input_errors[] = "A valid PPTP local IP address must be specified."; } @@ -213,18 +318,23 @@ if ($_POST) { unset($config['interfaces']['wan']['gateway']); unset($wancfg['pointtopoint']); unset($wancfg['dhcphostname']); - unset($config['pppoe']['username']); - unset($config['pppoe']['password']); - unset($config['pppoe']['provider']); - unset($config['pppoe']['ondemand']); - unset($config['pppoe']['timeout']); - unset($config['pptp']['username']); - unset($config['pptp']['password']); - unset($config['pptp']['local']); - unset($config['pptp']['subnet']); - unset($config['pptp']['remote']); - unset($config['pptp']['ondemand']); - unset($config['pptp']['timeout']); + if (is_array($wancfg['pppoe'])) { + unset($config['pppoe']['username']); + unset($config['pppoe']['password']); + unset($config['pppoe']['provider']); + unset($config['pppoe']['ondemand']); + unset($config['pppoe']['timeout']); + unset($wancfg['pppoe']['pppoe-reset-type']); + } + if (is_array($wancfg['pptp'])) { + unset($config['pptp']['username']); + unset($config['pptp']['password']); + unset($config['pptp']['local']); + unset($config['pptp']['subnet']); + unset($config['pptp']['remote']); + unset($config['pptp']['ondemand']); + unset($config['pptp']['timeout']); + } unset($config['bigpond']['username']); unset($config['bigpond']['password']); unset($config['bigpond']['authserver']); @@ -256,6 +366,90 @@ if ($_POST) { $config['pppoe']['provider'] = $_POST['provider']; $config['pppoe']['ondemand'] = $_POST['pppoe_dialondemand'] ? true : false; $config['pppoe']['timeout'] = $_POST['pppoe_idletimeout']; + + /* perform a periodic reset? */ + if (isset($_POST['pppoe_preset'])) { + if (! is_array($config['cron']['item'])) { $config['cron']['item'] = array(); } + + $itemhash = getMPDCRONSettings(); + $item = $itemhash['ITEM']; + + if (empty($item)) { + $item = array(); + } + + if (isset($_POST['pppoe_pr_type']) && $_POST['pppoe_pr_type'] == "custom") { + $wancfg['pppoe']['pppoe-reset-type'] = "custom"; + $pconfig['pppoe_pr_custom'] = true; + + $item['minute'] = $_POST['pppoe_resetminute']; + $item['hour'] = $_POST['pppoe_resethour']; + + if (isset($_POST['pppoe_resetdate']) && + $_POST['pppoe_resetdate'] <> "" && + strlen($_POST['pppoe_resetdate']) == 10) { + $date = explode("/", $_POST['pppoe_resetdate']); + + $item['mday'] = $date[1]; + $item['month'] = $date[0]; + } else { + $item['mday'] = "*"; + $item['month'] = "*"; + } + + $item['wday'] = "*"; + $item['who'] = "root"; + $item['command'] = CRON_PPPOE_CMD_FILE; + } else if (isset($_POST['pppoe_pr_type']) && $_POST['pppoe_pr_type'] = "preset") { + $wancfg['pppoe']['pppoe-reset-type'] = "preset"; + $pconfig['pppoe_pr_preset'] = true; + + switch ($_POST['pppoe_pr_preset_val']) { + case "monthly": + $item['minute'] = "0"; + $item['hour'] = "0"; + $item['mday'] = "1"; + $item['month'] = "*"; + $item['wday'] = "*"; + $item['who'] = "root"; + $item['command'] = CRON_PPPOE_CMD_FILE; + break; + case "weekly": + $item['minute'] = "0"; + $item['hour'] = "0"; + $item['mday'] = "*"; + $item['month'] = "*"; + $item['wday'] = "0"; + $item['who'] = "root"; + $item['command'] = CRON_PPPOE_CMD_FILE; + break; + case "daily": + $item['minute'] = "0"; + $item['hour'] = "0"; + $item['mday'] = "*"; + $item['month'] = "*"; + $item['wday'] = "*"; + $item['who'] = "root"; + $item['command'] = CRON_PPPOE_CMD_FILE; + break; + case "hourly": + $item['minute'] = "0"; + $item['hour'] = "*"; + $item['mday'] = "*"; + $item['month'] = "*"; + $item['wday'] = "*"; + $item['who'] = "root"; + $item['command'] = CRON_PPPOE_CMD_FILE; + break; + } // end switch + } // end if + + if (isset($itemhash['ID'])) { + $config['cron']['item'][$itemhash['ID']] = $item; + } else { + $config['cron']['item'][] = $item; + } + } // end if } else if ($_POST['type'] == "PPTP") { $wancfg['ipaddr'] = "pptp"; $config['pptp']['username'] = $_POST['pptp_username']; @@ -273,6 +467,15 @@ if ($_POST) { $config['bigpond']['authdomain'] = $_POST['bigpond_authdomain']; $config['bigpond']['minheartbeatinterval'] = $_POST['bigpond_minheartbeatinterval']; } + + /* reset cron items if necessary */ + if (empty($_POST['pppoe_preset'])) { + /* test whether a cron item exists and unset() it if necessary */ + $itemhash = getMPDCRONSettings(); + $item = $itemhash['ITEM']; + + if (isset($item)) { unset($config['cron']['item'][$itemhash['ID']]); } + } if($_POST['blockpriv'] == "yes") $wancfg['blockpriv'] = true; @@ -288,6 +491,24 @@ if ($_POST) { $wancfg['mtu'] = $_POST['mtu']; write_config(); + + /* finally install the pppoerestart file */ + if (isset($_POST['pppoe_preset'])) { + config_lock(); + conf_mount_rw(); + + if (! file_exists(CRON_PPPOE_CMD_FILE)) { + file_put_contents(CRON_PPPOE_CMD_FILE, CRON_PPPOE_CMD); + chmod(CRON_PPPOE_CMD_FILE, 0700); + } + + /* regenerate cron settings/crontab file */ + configure_cron(); + sigkillbypid("{$g['varrun_path']}/cron.pid", "HUP"); + + conf_mount_ro(); + config_unlock(); + } $retval = 0; @@ -296,11 +517,21 @@ if ($_POST) { } $pgtitle = "Interfaces: WAN"; +$closehead = false; include("head.inc"); ?> - + + + + + + + + + - +

@@ -578,6 +822,63 @@ function type_change(enable_change,enable_change_pptp) { seconds
If no qualifying outgoing packets are transmitted for the specified number of seconds, the connection is brought down. An idle timeout of zero disables this feature. + + + onclick="Effect.toggle('presetwrap', 'appear', { duration: 1.0 });" /> + +
+ + + +
+ + + + + + + + -- cgit v1.1