rc.initial.setlanip 19.4 KB
Newer Older
1
#!/usr/local/bin/php
Ad Schellevis's avatar
Ad Schellevis committed
2
<?php
3

Ad Schellevis's avatar
Ad Schellevis committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
	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.
*/

30 31
/* parse the configuration and include all functions used below */
require_once("config.inc");
32
require_once("interfaces.inc");
33
require_once("openvpn.inc");
34
require_once("util.inc");
35
require_once("ipsec.inc");
36 37 38
require_once("filter.inc");
require_once("rrd.inc");
require_once("util.inc");
39
require_once("services.inc");
40
require_once("unbound.inc");
41
require_once("system.inc");
42

Ad Schellevis's avatar
Ad Schellevis committed
43 44 45
$options = getopt("hn", array("dry-run", "help"));

if (isset($options["h"]) || isset($options["help"])) {
46 47 48 49
    echo "usage: rc.initial.setlanip [option ...]\n";
    echo "  -h, --help       show this message\n";
    echo "  -n, --dry-run    do not make any configuration changes\n";
    exit(0);
Ad Schellevis's avatar
Ad Schellevis committed
50 51 52 53
}

$dry_run = isset($options["n"]) || isset($options["dry-run"]);
if ($dry_run) {
54
    echo "DRY RUN MODE IS ON\n";
Ad Schellevis's avatar
Ad Schellevis committed
55 56 57
}


58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
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;
78 79
}

80 81
function console_get_interface_from_ppp($realif)
{
82 83 84 85 86 87 88 89 90 91 92 93
    global $config;

    if (isset($config['ppps']['ppp'])) {
        foreach ($config['ppps']['ppp'] as $pppid => $ppp) {
            if ($realif == $ppp['if']) {
                $ifaces = explode(',', $ppp['ports']);
                return $ifaces[0];
            }
        }
    }

    return '';
Ad Schellevis's avatar
Ad Schellevis committed
94 95
}

96 97 98 99 100 101 102 103 104
function prompt_for_enable_dhcp_server($version = 4)
{
    global $config, $fp, $interface;
    if ($interface == "wan") {
        if ($config['interfaces']['lan']) {
            return false;
        }
    }
    /* only allow DHCP server to be enabled when static IP is
Ad Schellevis's avatar
Ad Schellevis committed
105
	   configured on this interface */
106 107 108 109 110 111 112 113 114 115 116 117
    if ($version === 6) {
        $is_ipaddr = is_ipaddrv6($config['interfaces'][$interface]['ipaddrv6']);
    } else {
        $is_ipaddr = is_ipaddrv4($config['interfaces'][$interface]['ipaddr']);
    }
    if (!($is_ipaddr)) {
        return false;
    }

    $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));
Ad Schellevis's avatar
Ad Schellevis committed
118 119
}

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
function get_interface_config_description($iface)
{
    global $config;
    $c = $config['interfaces'][$iface];
    if (!$c) {
        return null;
    }
    $if = $c['if'];
    $result = $if;
    $result2 = array();
    $ipaddr = $c['ipaddr'];
    $ipaddrv6 = $c['ipaddrv6'];
    if (is_ipaddr($ipaddr)) {
        $result2[] = "static";
    } elseif ($ipaddr == "dhcp") {
        $result2[] = "dhcp";
    }
    if (is_ipaddr($ipaddrv6)) {
        $result2[] = "staticv6";
    } elseif ($ipaddrv6 == "dhcp6") {
        $result2[] = "dhcp6";
    }
    if (count($result2)) {
        $result .= " - " . implode(", ", $result2);
    }
    return $result;
Ad Schellevis's avatar
Ad Schellevis committed
146 147 148 149 150 151 152
}

$fp = fopen('php://stdin', 'r');

/* build an interface collection */
$ifdescrs = get_configured_interface_with_descr(false, true);
$count = count($ifdescrs);
153

Ad Schellevis's avatar
Ad Schellevis committed
154 155 156
/* grab interface that we will operate on, unless there is only one
   interface */
if ($count > 1) {
157 158 159 160 161 162 163 164 165
    echo "Available interfaces:\n\n";
    $x=1;
    foreach ($ifdescrs as $iface => $ifdescr) {
        $config_descr = get_interface_config_description($iface);
        echo "{$x} - {$ifdescr} ({$config_descr})\n";
        $x++;
    }
    echo "\nEnter the number of the interface you wish to configure: ";
    $intnum = chop(fgets($fp));
Ad Schellevis's avatar
Ad Schellevis committed
166
} else {
167
    $intnum = $count;
Ad Schellevis's avatar
Ad Schellevis committed
168
}
169

170 171 172 173 174 175
if ($intnum < 1) {
    exit;
}
if ($intnum > $count) {
    exit;
}
176

Ad Schellevis's avatar
Ad Schellevis committed
177 178
$index = 1;
foreach ($ifdescrs as $ifname => $ifdesc) {
179 180 181 182 183 184
    if ($intnum == $index) {
        $interface = $ifname;
        break;
    } else {
        $index++;
    }
185
}
186 187 188
if (!$interface) {
    echo "Invalid interface!\n";
    exit;
Ad Schellevis's avatar
Ad Schellevis committed
189 190 191 192
}

$ifaceassigned = "";

193 194
function next_unused_gateway_name($interface)
{
195 196
    global $config;

197 198
    $new_name = "GW_" . strtoupper($interface);

199
    if (!isset($config['gateways']['gateway_item'])) {
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
        return $new_name;
    }
    $count = 1;
    do {
        $existing = false;
        foreach ($config['gateways']['gateway_item'] as $item) {
            if ($item['name'] === $new_name) {
                $existing = true;
                break;
            }
        }
        if ($existing) {
            $count += 1;
            $new_name = "GW_" . strtoupper($interface) . "_" . $count;
        }
    } while ($existing);
    return $new_name;
Ad Schellevis's avatar
Ad Schellevis committed
217 218
}

219 220
function add_gateway_to_config($interface, $gatewayip, $inet_type)
{
221
    global $config, $dry_run;
222 223

    if (!isset($config['gateways']['gateway_item'])) {
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
        $config['gateways']['gateway_item'] = array();
    }
    $a_gateways = &$config['gateways']['gateway_item'];
    if ($dry_run) {
        print_r($a_gateways);
    }
    $new_name = '';
    $is_default = true;
    foreach ($a_gateways as $item) {
        if ($item['ipprotocol'] === $inet_type) {
            if (isset($item['defaultgw'])) {
                $is_default = false;
            }
            if (($item['interface'] === $interface) && ($item['gateway'] === $gatewayip)) {
                $new_name = $item['name'];
            }
        }
    }
    if ($new_name == '') {
        $new_name = next_unused_gateway_name($interface);
        $item = array(
            "interface" => $interface,
            "gateway" => $gatewayip,
            "name" => $new_name,
            "weight" => 1,
            "ipprotocol" => $inet_type,
            "interval" => true,
            "descr" => "Interface $interface Gateway",
            "defaultgw" => $is_default
        );
        if ($dry_run) {
            print_r($item);
        }
        $a_gateways[] = $item;
    }

    return $new_name;
Ad Schellevis's avatar
Ad Schellevis committed
261 262
}

263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
function console_configure_ip_address($version)
{
    global $g, $config, $interface, $restart_dhcpd, $ifaceassigned, $fp;

    $label_IPvX = ($version === 6) ? "IPv6"   : "IPv4";
    $maxbits    = ($version === 6) ? 127      : 31;
    $label_DHCP = ($version === 6) ? "DHCP6"  : "DHCP";

    $upperifname = strtoupper($interface);

    if ($interface == "wan") {
        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;
            }
            $intip = ($version === 6) ? "dhcp6" : "dhcp";
            $intbits = "";
            $isintdhcp = true;
            $restart_dhcpd = true;
        }
    }

    if ($isintdhcp == false or $interface <> "wan") {
        while (true) {
            do {
                echo "\n" . sprintf(
                    gettext("Enter the new %s %s address.  Press <ENTER> for none:"),
                    $upperifname,
                    $label_IPvX
                ) . "\n> ";
                $intip = chop(fgets($fp));
                $is_ipaddr = ($version === 6) ? is_ipaddrv6($intip) : is_ipaddrv4($intip);
296
                if ($is_ipaddr && is_ipaddr_configured($intip, $interface)) {
297 298 299 300 301 302 303
                    $ip_conflict = true;
                    echo gettext("This IP address conflicts with another interface or a VIP") . "\n";
                } else {
                    $ip_conflict = false;
                }
            } while (($ip_conflict === true) || !($is_ipaddr || $intip == ''));
            if ($intip != '') {
304
                echo "\n" . gettext("Subnet masks are entered as bit counts (like CIDR notation).") . "\n";
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
                if ($version === 6) {
                    echo "e.g. ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00 = 120\n";
                    echo "     ffff:ffff:ffff:ffff:ffff:ffff:ffff:0    = 112\n";
                    echo "     ffff:ffff:ffff:ffff:ffff:ffff:0:0       =  96\n";
                    echo "     ffff:ffff:ffff:ffff:ffff:0:0:0          =  80\n";
                    echo "     ffff:ffff:ffff:ffff:0:0:0:0             =  64\n";
                } else {
                    echo "e.g. 255.255.255.0 = 24\n";
                    echo "     255.255.0.0   = 16\n";
                    echo "     255.0.0.0     = 8\n";
                }
                do {
                    $upperifname = strtoupper($interface);
                    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));
                    $restart_dhcpd = true;

                    if ($version === 4 && $intbits < $maxbits) {
                        if ($intip == gen_subnet($intip, $intbits)) {
                            echo gettext("You cannot set network address to an interface");
                            continue 2;
                            $intbits_ok = false;
                        } elseif ($intip == gen_subnet_max($intip, $intbits)) {
                            echo gettext("You cannot set broadcast address to an interface");
                            continue 2;
                            $intbits_ok = false;
                        }
                    }
                } while (!$intbits_ok);

                if ($version === 6) {
                    $subnet = gen_subnetv6($intip, $intbits);
                } else {
                    $subnet = gen_subnet($intip, $intbits);
                }
                do {
                    echo "\n" . sprintf(gettext("For a WAN, enter the new %s %s upstream gateway address."), $upperifname, $label_IPvX) . "\n" .
                                gettext("For a LAN, press <ENTER> for none:") . "\n> ";
                    $gwip = chop(fgets($fp));
                    $is_ipaddr = ($version === 6) ? is_ipaddrv6($gwip) : is_ipaddrv4($gwip);
                    $is_in_subnet = $is_ipaddr && ip_in_subnet($gwip, $subnet . "/" . $intbits);
                    if ($gwip != '') {
                        if (!$is_ipaddr) {
                            echo sprintf(gettext("not an %s IP address!"), $label_IPvX) . "\n";
                        } elseif (!$is_in_subnet) {
                            echo gettext("not in subnet!") . "\n";
                        }
                    }
                } while (!($gwip == '' || ($is_ipaddr && $is_in_subnet)));

                if ($gwip != '') {
                    $inet_type = ($version === 6) ? "inet6" : "inet";
                    $gwname = add_gateway_to_config($interface, $gwip, $inet_type);
                }
            }
            $ifppp = console_get_interface_from_ppp(get_real_interface($interface));
            if (!empty($ifppp)) {
                $ifaceassigned = $ifppp;
            }
            break;
        }
    }

    return array($intip, $intbits, $gwname);
Ad Schellevis's avatar
Ad Schellevis committed
375 376 377 378 379
}

list($intip,  $intbits,  $gwname)  = console_configure_ip_address(4);
list($intip6, $intbits6, $gwname6) = console_configure_ip_address(6);

380 381 382
if (!empty($ifaceassigned)) {
    $config['interfaces'][$interface]['if'] = $ifaceassigned;
}
Ad Schellevis's avatar
Ad Schellevis committed
383 384 385 386 387 388 389 390
$config['interfaces'][$interface]['ipaddr']    = $intip;
$config['interfaces'][$interface]['subnet']    = $intbits;
$config['interfaces'][$interface]['gateway']   = $gwname;
$config['interfaces'][$interface]['ipaddrv6']  = $intip6;
$config['interfaces'][$interface]['subnetv6']  = $intbits6;
$config['interfaces'][$interface]['gatewayv6'] = $gwname6;
$config['interfaces'][$interface]['enable']    = true;

391 392
function console_configure_dhcpd($version = 4)
{
393
    global $config, $restart_dhcpd, $fp, $interface, $dry_run, $intip, $intbits, $intip6, $intbits6;
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444

    $label_IPvX = ($version === 6) ? "IPv6"    : "IPv4";
    $dhcpd      = ($version === 6) ? "dhcpdv6" : "dhcpd";

    if (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 {
            do {
                echo sprintf(gettext("Enter the start address of the %s client address range:"), $label_IPvX) . " ";
                $dhcpstartip = chop(fgets($fp));
                if ($dhcpstartip === "") {
                    fclose($fp);
                    exit(0);
                }
                $is_ipaddr = ($version === 6) ? is_ipaddrv6($dhcpstartip) : is_ipaddrv4($dhcpstartip);
                $is_inrange = is_inrange($dhcpstartip, $subnet_start, $subnet_end);
                if (!$is_inrange) {
                    echo gettext("This IP address must be in the interface's subnet") . "\n";
                }
            } while (!$is_ipaddr || !$is_inrange);

            do {
                echo sprintf(gettext("Enter the end address of the %s client address range:"), $label_IPvX) . " ";
                $dhcpendip = chop(fgets($fp));
                if ($dhcpendip === "") {
                    fclose($fp);
                    exit(0);
                }
                $is_ipaddr = ($version === 6) ? is_ipaddrv6($dhcpendip) : is_ipaddrv4($dhcpendip);
                $is_inrange = is_inrange($dhcpendip, $subnet_start, $subnet_end);
                if (!$is_inrange) {
                    echo gettext("This IP address must be in the interface's subnet") . "\n";
                }
                $not_inorder = ($version === 6) ? (inet_pton($dhcpendip) < inet_pton($dhcpstartip)) : ip_less_than($dhcpendip, $dhcpstartip);
                if ($not_inorder) {
                    echo gettext("The end address of the DHCP range must be >= the start address") . "\n";
                }
            } while (!$is_ipaddr || !$is_inrange);
        } while ($not_inorder);
        $restart_dhcpd = true;
        $config[$dhcpd][$interface]['enable'] = true;
        $config[$dhcpd][$interface]['range']['from'] = $dhcpstartip;
        $config[$dhcpd][$interface]['range']['to'] = $dhcpendip;
    } else {
        if (isset($config[$dhcpd][$interface]['enable'])) {
            unset($config[$dhcpd][$interface]['enable']);
            printf(gettext("Disabling %s DHCPD..."), $label_IPvX);
            $restart_dhcpd = true;
        }
    }
Ad Schellevis's avatar
Ad Schellevis committed
445 446 447 448
}

console_configure_dhcpd(4);
console_configure_dhcpd(6);
449

Ad Schellevis's avatar
Ad Schellevis committed
450 451 452
//*****************************************************************************

if ($config['system']['webgui']['protocol'] == "https") {
453 454 455 456
    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;
    }
Ad Schellevis's avatar
Ad Schellevis committed
457 458 459
}

if (isset($config['system']['webgui']['noantilockout'])) {
460 461
    echo "\n" . sprintf(gettext("Note: the anti-lockout rule on %s has been re-enabled."), $interface) . "\n";
    unset($config['system']['webgui']['noantilockout']);
Ad Schellevis's avatar
Ad Schellevis committed
462 463
}

464 465 466 467 468 469 470 471 472 473 474
if ($config['interfaces']['lan']) {
    if ($config['dhcpd']) {
        if ($config['dhcpd']['wan']) {
            unset($config['dhcpd']['wan']);
        }
    }
    if ($config['dhcpdv6']) {
        if ($config['dhcpdv6']['wan']) {
            unset($config['dhcpdv6']['wan']);
        }
    }
Ad Schellevis's avatar
Ad Schellevis committed
475 476
}

477 478 479 480 481 482 483 484 485 486 487 488 489
if (!$config['interfaces']['lan']) {
    unset($config['interfaces']['lan']);
    if ($config['dhcpd']['lan']) {
        unset($config['dhcpd']['lan']);
    }
    if ($config['dhcpdv6']['lan']) {
        unset($config['dhcpdv6']['lan']);
    }
    unset($config['nat']);
    if (!$dry_run) {
        system("rm /var/dhcpd/var/db/* >/dev/null 2>/dev/null");
        $restart_dhcpd = true;
    }
Ad Schellevis's avatar
Ad Schellevis committed
490 491 492 493
}

$upperifname = strtoupper($interface);
if (!$dry_run) {
494
    echo "\nPlease wait while the changes are saved to {$upperifname}...";
495
    write_config(sprintf('%s IP configuration from console menu', $interface));
496 497 498 499 500 501 502 503 504 505
    interface_reconfigure(strtolower($upperifname));
    echo "\n Reloading filter...";
    filter_configure_sync();
    if ($restart_dhcpd) {
        echo "\n DHCPD...";
        services_dhcpd_configure();
    }
    if ($restart_webgui) {
        mwexec('/usr/local/etc/rc.restart_webgui');
    }
Ad Schellevis's avatar
Ad Schellevis committed
506
}
507

Ad Schellevis's avatar
Ad Schellevis committed
508
if ($intip != '') {
509 510 511 512 513 514 515 516 517 518 519 520 521
    if (is_ipaddr($intip)) {
        echo "\n\n" . sprintf(
            gettext("The IPv4 %s address has been set to %s"),
            $upperifname,
            "{$intip}/{$intbits}"
        ) . "\n";
    } else {
        echo "\n\n" . sprintf(
            gettext("The IPv4 %s address has been set to %s"),
            $upperifname,
            $intip
        ) . "\n";
    }
Ad Schellevis's avatar
Ad Schellevis committed
522 523
}
if ($intip6 != '') {
524 525 526 527 528 529 530 531 532 533 534 535 536
    if (is_ipaddr($intip6)) {
        echo "\n\n" . sprintf(
            gettext("The IPv6 %s address has been set to %s"),
            $upperifname,
            "${intip6}/${intbits6}"
        ) . "\n";
    } else {
        echo "\n\n" . sprintf(
            gettext("The IPv6 %s address has been set to %s"),
            $upperifname,
            $intip6
        ) . "\n";
    }
Ad Schellevis's avatar
Ad Schellevis committed
537 538 539
}

if ($intip != '' || $intip6 != '') {
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
    if (count($ifdescrs) == "1" or $interface == "lan") {
        echo gettext('You can now access the webConfigurator by opening the following URL in your web browser:') . "\n";
        if (!empty($config['system']['webgui']['port'])) {
            $webuiport = $config['system']['webgui']['port'];
            if ($intip != '') {
                echo "		{$config['system']['webgui']['protocol']}://{$intip}:{$webuiport}/\n";
            }
            if ($intip6 != '') {
                if (is_ipaddr($intip6)) {
                    echo "		{$config['system']['webgui']['protocol']}://[{$intip6}]:{$webuiport}/\n";
                } else {
                    echo "		{$config['system']['webgui']['protocol']}://{$intip6}:{$webuiport}/\n";
                }
            }
        } else {
            if ($intip != '') {
                echo "		{$config['system']['webgui']['protocol']}://{$intip}/\n";
            }
            if ($intip6 != '') {
                if (is_ipaddr($intip6)) {
                    echo "		{$config['system']['webgui']['protocol']}://[{$intip6}]/\n";
                } else {
                    echo "		{$config['system']['webgui']['protocol']}://{$intip6}/\n";
                }
            }
        }
    }
Ad Schellevis's avatar
Ad Schellevis committed
567 568 569 570 571 572
}

echo "\n" . gettext('Press <ENTER> to continue.');

fgets($fp);
fclose($fp);