Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation of y/n answers in setlanip #1382

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
72 changes: 40 additions & 32 deletions etc/rc.initial.setlanip
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,27 @@ require_once("filter.inc");
require_once("shaper.inc");
require_once("rrd.inc");

function console_prompt_for_yn ($prompt_text) {
global $fp;

$good_answer = false;

do {
echo "\n" . $prompt_text . " (y/n) ";
$yn = strtolower(chop(fgets($fp)));
if (($yn == "y") || ($yn == "yes")) {
$boolean_answer = true;
$good_answer = true;
}
if (($yn == "n") || ($yn == "no")) {
$boolean_answer = false;
$good_answer = true;
}
} while (!$good_answer);

return $boolean_answer;
}

function console_get_interface_from_ppp($realif) {
global $config;

Expand All @@ -74,7 +95,7 @@ function prompt_for_enable_dhcp_server($version = 4) {
global $config, $fp, $interface;
if($interface == "wan") {
if($config['interfaces']['lan'])
return "n";
return false;
}
/* only allow DHCP server to be enabled when static IP is
configured on this interface */
Expand All @@ -83,19 +104,13 @@ function prompt_for_enable_dhcp_server($version = 4) {
} else {
$is_ipaddr = is_ipaddrv4($config['interfaces'][$interface]['ipaddr']);
}
if ($is_ipaddr) {
$label_DHCP = ($version === 6) ? "DHCP6" : "DHCP";
do {
$good = false;
$upperifname = strtoupper($interface);
echo "\n" . sprintf(gettext("Do you want to enable the %s server on %s? [y|n]"),
$label_DHCP, $upperifname) . " ";
$yn = strtolower(chop(fgets($fp)));
if ($yn[0] == "y" or $yn[0] == "n")
$good = true;
} while (!$good);
if (!($is_ipaddr)) {
return false;
}
return $yn;

$label_DHCP = ($version === 6) ? "DHCP6" : "DHCP";
$upperifname = strtoupper($interface);
return console_prompt_for_yn (sprintf(gettext("Do you want to enable the %s server on %s?"), $label_DHCP, $upperifname));
}

function get_interface_config_description($iface) {
Expand Down Expand Up @@ -238,10 +253,7 @@ function console_configure_ip_address($version) {
$upperifname = strtoupper($interface);

if($interface == "wan") {
echo sprintf(gettext("Configure %s address %s interface via %s? [y|n]"),
$label_IPvX, $upperifname, $label_DHCP) . "\n> ";
$intdhcp = chop(fgets($fp));
if(strtolower($intdhcp) == "y" || strtolower($intdhcp) == "yes") {
if (console_prompt_for_yn (sprintf(gettext("Configure %s address %s interface via %s?"), $label_IPvX, $upperifname, $label_DHCP))) {
$ifppp = console_get_interface_from_ppp(get_real_interface("wan"));
if (!empty($ifppp))
$ifaceassigned = $ifppp;
Expand Down Expand Up @@ -281,10 +293,10 @@ function console_configure_ip_address($version) {
}
do {
$upperifname = strtoupper($interface);
echo "\n" . sprintf(gettext("Enter the new %s %s subnet bit count:"),
$upperifname, $label_IPvX) . "\n> ";
echo "\n" . sprintf(gettext("Enter the new %s %s subnet bit count (1 to %s):"),
$upperifname, $label_IPvX, $maxbits) . "\n> ";
$intbits = chop(fgets($fp));
$intbits_ok = is_numeric($intbits) && (($intbits >= 1) || ($intbits <= $maxbits));
$intbits_ok = is_numeric($intbits) && (($intbits >= 1) && ($intbits <= $maxbits));
$restart_dhcpd = true;

if ($version === 4 && $intbits < $maxbits) {
Expand Down Expand Up @@ -354,9 +366,7 @@ function console_configure_dhcpd($version = 4) {
$label_IPvX = ($version === 6) ? "IPv6" : "IPv4";
$dhcpd = ($version === 6) ? "dhcpdv6" : "dhcpd";

if($g['services_dhcp_server_enable'])
$yn = prompt_for_enable_dhcp_server($version);
if ($yn == "y") {
if($g['services_dhcp_server_enable'] && prompt_for_enable_dhcp_server($version)) {
$subnet_start = ($version === 6) ? gen_subnetv6($intip6, $intbits6) : gen_subnet($intip, $intbits);
$subnet_end = ($version === 6) ? gen_subnetv6_max($intip6, $intbits6) : gen_subnet_max($intip, $intbits);
do {
Expand Down Expand Up @@ -413,20 +423,18 @@ if (console_configure_dhcpd(6) == 0)

if ($config['system']['webgui']['protocol'] == "https") {

do {
$good = false;
echo "\n" . gettext("Do you want to revert to HTTP as the webConfigurator protocol? (y/n)") . " ";
$yn = strtolower(chop(fgets($fp)));
if ($yn[0] == "y" or $yn[0] == "n")
$good = true;
} while (!$good);

if ($yn == "y") {
if (console_prompt_for_yn (gettext("Do you want to revert to HTTP as the webConfigurator protocol?"))) {
$config['system']['webgui']['protocol'] = "http";
$restart_webgui = true;
}
}

if (!(console_prompt_for_yn (gettext("Do you want to apply these changes?")))) {
echo "\n" . gettext("Changes have not been applied.\n");
fclose($fp);
return 0;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure it's going to work? I was reading code above and it clearly touch $config (e.g. line 404). Users are going to believe all those changes were ignored when they answer no here, and it's not true, or did I miss something?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking that the values in $config are just pulled in by the require_once() of config.inc
Then if they are never written back to config.xml (no write_config is done), they will just be effectively thrown away when the code returns, and thus any changes would be gone.
So maybe this is not true - maybe the caller rc.initial is going to now have a part-modified $config[] array and if some other mod is done later and write_config() is called, then these bits of changes will end up getting saved.
Global variables that are not read-only are are a pain!
I will have a think - I guess I can save up any changes in other vars and set them in $config after the user has confirmed.


if (isset($config['system']['webgui']['noantilockout'])) {
echo "\n" . sprintf(gettext("Note: the anti-lockout rule on %s has been re-enabled."), $interface) . "\n";
unset($config['system']['webgui']['noantilockout']);
Expand Down