system.php 19.9 KB
Newer Older
Ad Schellevis's avatar
Ad Schellevis committed
1
<?php
2

Ad Schellevis's avatar
Ad Schellevis committed
3
/*
4
	Copyright (C) 2014-2015 Deciso B.V.
Ad Schellevis's avatar
Ad Schellevis committed
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
require_once("guiconfig.inc");
31
require_once("functions.inc");
Ad Schellevis's avatar
Ad Schellevis committed
32 33 34
require_once("filter.inc");
require_once("shaper.inc");

35 36 37 38 39 40 41 42
function get_locale_list()
{
	$locales = array('en_US' => gettext('English'));
	asort($locales);
	return $locales;
}


Ad Schellevis's avatar
Ad Schellevis committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
$pconfig['hostname'] = $config['system']['hostname'];
$pconfig['domain'] = $config['system']['domain'];
list($pconfig['dns1'],$pconfig['dns2'],$pconfig['dns3'],$pconfig['dns4']) = $config['system']['dnsserver'];

$arr_gateways = return_gateways_array();

$pconfig['dns1gw'] = $config['system']['dns1gw'];
$pconfig['dns2gw'] = $config['system']['dns2gw'];
$pconfig['dns3gw'] = $config['system']['dns3gw'];
$pconfig['dns4gw'] = $config['system']['dns4gw'];

$pconfig['dnsallowoverride'] = isset($config['system']['dnsallowoverride']);
$pconfig['timezone'] = $config['system']['timezone'];
$pconfig['timeupdateinterval'] = $config['system']['time-update-interval'];
$pconfig['timeservers'] = $config['system']['timeservers'];
$pconfig['theme'] = $config['system']['theme'];
$pconfig['language'] = $config['system']['language'];

$pconfig['dnslocalhost'] = isset($config['system']['dnslocalhost']);

if (!isset($pconfig['timeupdateinterval']))
	$pconfig['timeupdateinterval'] = 300;
if (!$pconfig['timezone'])
	$pconfig['timezone'] = "Etc/UTC";
if (!$pconfig['timeservers'])
	$pconfig['timeservers'] = "pool.ntp.org";

$changedesc = gettext("System") . ": ";
$changecount = 0;

if($pconfig['timezone'] <> $_POST['timezone']) {
	filter_pflog_start(true);
}

77 78 79 80
$timezonelist = array_map(
	function ($path) { return str_replace('/usr/share/zoneinfo/', '', $path); },
	glob('/usr/share/zoneinfo/*/*')
);
Ad Schellevis's avatar
Ad Schellevis committed
81 82 83 84 85 86 87 88 89 90 91 92

$multiwan = false;
$interfaces = get_configured_interface_list();
foreach($interfaces as $interface) {
	if(interface_has_gateway($interface)) {
		$multiwan = true;
	}
}

if ($_POST) {

	$changecount++;
93

Ad Schellevis's avatar
Ad Schellevis committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
	unset($input_errors);
	$pconfig = $_POST;

	/* input validation */
	$reqdfields = explode(" ", "hostname domain");
	$reqdfieldsn = array(gettext("Hostname"),gettext("Domain"));

	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);

	if ($_POST['hostname'] && !is_hostname($_POST['hostname'])) {
		$input_errors[] = gettext("The hostname may only contain the characters a-z, 0-9 and '-'.");
	}
	if ($_POST['domain'] && !is_domain($_POST['domain'])) {
		$input_errors[] = gettext("The domain may only contain the characters a-z, 0-9, '-' and '.'.");
	}

	$ignore_posted_dnsgw = array();

	for ($dnscounter=1; $dnscounter<5; $dnscounter++){
		$dnsname="dns{$dnscounter}";
		$dnsgwname="dns{$dnscounter}gw";
		if (($_POST[$dnsname] && !is_ipaddr($_POST[$dnsname]))) {
			$input_errors[] = gettext("A valid IP address must be specified for DNS server $dnscounter.");
		} else {
			if(($_POST[$dnsgwname] <> "") && ($_POST[$dnsgwname] <> "none")) {
				// A real gateway has been selected.
				if (is_ipaddr($_POST[$dnsname])) {
					if ((is_ipaddrv4($_POST[$dnsname])) && (validate_address_family($_POST[$dnsname], $_POST[$dnsgwname]) === false )) {
						$input_errors[] = gettext("You can not specify IPv6 gateway '{$_POST[$dnsgwname]}' for IPv4 DNS server '{$_POST[$dnsname]}'");
					}
					if ((is_ipaddrv6($_POST[$dnsname])) && (validate_address_family($_POST[$dnsname], $_POST[$dnsgwname]) === false )) {
						$input_errors[] = gettext("You can not specify IPv4 gateway '{$_POST[$dnsgwname]}' for IPv6 DNS server '{$_POST[$dnsname]}'");
					}
				} else {
					// The user selected a gateway but did not provide a DNS address. Be nice and set the gateway back to "none".
					$ignore_posted_dnsgw[$dnsgwname] = true;
				}
			}
		}
	}

	$direct_networks_list = explode(" ", filter_get_direct_networks_list());
	for ($dnscounter=1; $dnscounter<5; $dnscounter++) {
		$dnsitem = "dns{$dnscounter}";
		$dnsgwitem = "dns{$dnscounter}gw";
		if ($_POST[$dnsgwitem]) {
			if(interface_has_gateway($_POST[$dnsgwitem])) {
				foreach($direct_networks_list as $direct_network) {
					if(ip_in_subnet($_POST[$dnsitem], $direct_network)) {
						$input_errors[] = sprintf(gettext("You can not assign a gateway to DNS '%s' server which is on a directly connected network."),$_POST[$dnsitem]);
					}
				}
			}
		}
	}

	$t = (int)$_POST['timeupdateinterval'];
	if (($t < 0) || (($t > 0) && ($t < 6)) || ($t > 1440)) {
		$input_errors[] = gettext("The time update interval must be either 0 (disabled) or between 6 and 1440.");
	}
	# it's easy to have a little too much whitespace in the field, clean it up for the user before processing.
	$_POST['timeservers'] = preg_replace('/[[:blank:]]+/', ' ', $_POST['timeservers']);
	$_POST['timeservers'] = trim($_POST['timeservers']);
	foreach (explode(' ', $_POST['timeservers']) as $ts) {
		if (!is_domain($ts)) {
			$input_errors[] = gettext("A NTP Time Server name may only contain the characters a-z, 0-9, '-' and '.'.");
		}
	}

	if (!$input_errors) {
		update_if_changed("hostname", $config['system']['hostname'], $_POST['hostname']);
		update_if_changed("domain", $config['system']['domain'], $_POST['domain']);

		update_if_changed("timezone", $config['system']['timezone'], $_POST['timezone']);
		update_if_changed("NTP servers", $config['system']['timeservers'], strtolower($_POST['timeservers']));
		update_if_changed("NTP update interval", $config['system']['time-update-interval'], $_POST['timeupdateinterval']);

		if($_POST['language'] && $_POST['language'] != $config['system']['language']) {
			$config['system']['language'] = $_POST['language'];
			set_language($config['system']['language']);
		}

176
		update_if_changed("System Theme", $config['theme'], $_POST['theme']);
Ad Schellevis's avatar
Ad Schellevis committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 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

		/* XXX - billm: these still need updating after figuring out how to check if they actually changed */
		$olddnsservers = $config['system']['dnsserver'];
		unset($config['system']['dnsserver']);
		if ($_POST['dns1'])
			$config['system']['dnsserver'][] = $_POST['dns1'];
		if ($_POST['dns2'])
			$config['system']['dnsserver'][] = $_POST['dns2'];
		if ($_POST['dns3'])
			$config['system']['dnsserver'][] = $_POST['dns3'];
		if ($_POST['dns4'])
			$config['system']['dnsserver'][] = $_POST['dns4'];

		$olddnsallowoverride = $config['system']['dnsallowoverride'];

		unset($config['system']['dnsallowoverride']);
		$config['system']['dnsallowoverride'] = $_POST['dnsallowoverride'] ? true : false;

		if($_POST['dnslocalhost'] == "yes")
			$config['system']['dnslocalhost'] = true;
		else
			unset($config['system']['dnslocalhost']);

		/* which interface should the dns servers resolve through? */
		$outdnscounter = 0;
		for ($dnscounter=1; $dnscounter<5; $dnscounter++) {
			$dnsname="dns{$dnscounter}";
			$dnsgwname="dns{$dnscounter}gw";
			$olddnsgwname = $config['system'][$dnsgwname];

			if ($ignore_posted_dnsgw[$dnsgwname])
				$thisdnsgwname = "none";
			else
				$thisdnsgwname = $pconfig[$dnsgwname];

			// "Blank" out the settings for this index, then we set them below using the "outdnscounter" index.
			$config['system'][$dnsgwname] = "none";
			$pconfig[$dnsgwname] = "none";
			$pconfig[$dnsname] = "";

			if ($_POST[$dnsname]) {
				// Only the non-blank DNS servers were put into the config above.
				// So we similarly only add the corresponding gateways sequentially to the config (and to pconfig), as we find non-blank DNS servers.
				// This keeps the DNS server IP and corresponding gateway "lined up" when the user blanks out a DNS server IP in the middle of the list.
				$outdnscounter++;
				$outdnsname="dns{$outdnscounter}";
				$outdnsgwname="dns{$outdnscounter}gw";
				$pconfig[$outdnsname] = $_POST[$dnsname];
				if($_POST[$dnsgwname]) {
					$config['system'][$outdnsgwname] = $thisdnsgwname;
					$pconfig[$outdnsgwname] = $thisdnsgwname;
				} else {
					// Note: when no DNS GW name is chosen, the entry is set to "none", so actually this case never happens.
					unset($config['system'][$outdnsgwname]);
					$pconfig[$outdnsgwname] = "";
				}
			}
			if (($olddnsgwname != "") && ($olddnsgwname != "none") && (($olddnsgwname != $thisdnsgwname) || ($olddnsservers[$dnscounter-1] != $_POST[$dnsname]))) {
				// A previous DNS GW name was specified. It has now gone or changed, or the DNS server address has changed.
				// Remove the route. Later calls will add the correct new route if needed.
				if (is_ipaddrv4($olddnsservers[$dnscounter-1]))
					mwexec("/sbin/route delete " . escapeshellarg($olddnsservers[$dnscounter-1]));
				else
					if (is_ipaddrv6($olddnsservers[$dnscounter-1]))
						mwexec("/sbin/route delete -inet6 " . escapeshellarg($olddnsservers[$dnscounter-1]));
			}
		}

		if ($changecount > 0)
			write_config($changedesc);

		$retval = 0;
		$retval = system_hostname_configure();
		$retval |= system_hosts_generate();
		$retval |= system_resolvconf_generate();
		if (isset($config['dnsmasq']['enable']))
			$retval |= services_dnsmasq_configure();
		elseif (isset($config['unbound']['enable']))
			$retval |= services_unbound_configure();
		$retval |= system_timezone_configure();
		$retval |= system_ntp_configure();

259
		if ($olddnsallowoverride != $config['system']['dnsallowoverride']) {
260
			configd_run("dns reload");
261
		}
Ad Schellevis's avatar
Ad Schellevis committed
262 263 264

		// Reload the filter - plugins might need to be run.
		$retval |= filter_configure();
265

Ad Schellevis's avatar
Ad Schellevis committed
266 267 268 269 270 271 272 273 274 275 276
		$savemsg = get_std_save_message($retval);
	}

	unset($ignore_posted_dnsgw);
}

$pgtitle = array(gettext("System"),gettext("General Setup"));
include("head.inc");

?>

277 278
<body>
    <?php include("fbegin.inc"); ?>
279

280 281 282
<!-- row -->
<section class="page-content-main">
	<div class="container-fluid">
283

284 285
        <div class="row">
            <?php
286 287
		if ($input_errors) print_input_errors($input_errors);
		if ($savemsg) print_info_box($savemsg);
288
            ?>
289 290
            <section class="col-xs-12">

291
                <div class="content-box table-responsive">
292 293 294 295 296 297 298 299 300 301 302 303 304 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

			<form action="system.php" method="post">
				<table width="100%" border="0" cellpadding="6" cellspacing="0" summary="main area" class="table table-striped">
					<thead>
						<tr>
							<th colspan="2" valign="top" class="listtopic"><?=gettext("System"); ?></th>
						</tr>
					</thead>
					<tbody>
						<tr>
							<td width="22%" valign="top" class="vncellreq"><?=gettext("Hostname"); ?></td>
							<td width="78%" class="vtable"> <input name="hostname" type="text" class="formfld unknown" id="hostname" size="40" value="<?=htmlspecialchars($pconfig['hostname']);?>" />
								<br />
								<span class="vexpl">
									<?=gettext("Name of the firewall host, without domain part"); ?>
									<br />
									<?=gettext("e.g."); ?> <em>firewall</em>
								</span>
							</td>
						</tr>
						<tr>
							<td width="22%" valign="top" class="vncellreq"><?=gettext("Domain"); ?></td>
							<td width="78%" class="vtable"> <input name="domain" type="text" class="formfld unknown" id="domain" size="40" value="<?=htmlspecialchars($pconfig['domain']);?>" />
								<br />
								<span class="vexpl">
									<?=gettext("Do not use 'local' as a domain name. It will cause local hosts running mDNS (avahi, bonjour, etc.) to be unable to resolve local hosts not running mDNS."); ?>
									<br />
									<?=gettext("e.g."); ?> <em><?=gettext("mycorp.com, home, office, private, etc."); ?></em>
								</span>
							</td>
						</tr>
						<tr>
							<td width="22%" valign="top" class="vncell"><?=gettext("DNS servers"); ?></td>
							<td width="78%" class="vtable">

								<table border="0" cellpadding="0" cellspacing="0" summary="dns servers and gateways" class="table table-striped">
									<thead>
										<tr>
											<th><?=gettext("DNS Server"); ?></th>
											<?php if ($multiwan): ?>
											<th><?=gettext("Use gateway"); ?></th>
											<?php endif; ?>
										</tr>
									</thead>
									<tbody>
									<?php
										for ($dnscounter=1; $dnscounter<5; $dnscounter++):
											$fldname="dns{$dnscounter}gw";
									?>
									<tr>
342
										<td width="348px">
343 344 345
											<input name="dns<?php echo $dnscounter;?>" type="text" class="formfld unknown" id="dns<?php echo $dnscounter;?>" size="28" value="<?php echo $pconfig['dns'.$dnscounter];?>" />
										</td>
										<td>
346
                                                    <?php if ($multiwan): ?>
347
											<select name='<?=$fldname;?>' class='selectpicker' data-style='btn-default' data-width='auto'>
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
												<?php
													$gwname = "none";
													$dnsgw = "dns{$dnscounter}gw";
													if($pconfig[$dnsgw] == $gwname) {
														$selected = "selected=\"selected\"";
													} else {
														$selected = "";
													}
													echo "<option value='$gwname' $selected>$gwname</option>\n";
													foreach($arr_gateways as $gwname => $gwitem) {
														//echo $pconfig[$dnsgw];
														if((is_ipaddrv4(lookup_gateway_ip_by_name($pconfig[$dnsgw])) && (is_ipaddrv6($gwitem['gateway'])))) {
															continue;
														}
														if((is_ipaddrv6(lookup_gateway_ip_by_name($pconfig[$dnsgw])) && (is_ipaddrv4($gwitem['gateway'])))) {
															continue;
														}
														if($pconfig[$dnsgw] == $gwname) {
															$selected = "selected=\"selected\"";
														} else {
															$selected = "";
														}
														echo "<option value='$gwname' $selected>$gwname - {$gwitem['friendlyiface']} - {$gwitem['gateway']}</option>\n";
													}
												?>
											</select>
374
                                                        <?php endif; ?>
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
										</td>
									</tr>
									<?php endfor; ?>
									</tbody>
								</table>

								<span class="vexpl">
									<?=gettext("Enter IP addresses to be used by the system for DNS resolution. " .
									"These are also used for the DHCP service, DNS forwarder and for PPTP VPN clients."); ?>
									<br />
									<?php if($multiwan): ?>
									<br />
									<?=gettext("In addition, optionally select the gateway for each DNS server. " .
									"When using multiple WAN connections there should be at least one unique DNS server per gateway."); ?>
									<br />
									<?php endif; ?>
									<br />
									<input name="dnsallowoverride" type="checkbox" id="dnsallowoverride" value="yes" <?php if ($pconfig['dnsallowoverride']) echo "checked=\"checked\""; ?> />
									<strong>
										<?=gettext("Allow DNS server list to be overridden by DHCP/PPP on WAN"); ?>
									</strong>
									<br />
									<?php printf(gettext("If this option is set, %s will " .
									"use DNS servers assigned by a DHCP/PPP server on WAN " .
									"for its own purposes (including the DNS forwarder). " .
									"However, they will not be assigned to DHCP and PPTP " .
									"VPN clients."), $g['product_name']); ?>
									<br />
									<br />
									<input name="dnslocalhost" type="checkbox" id="dnslocalhost" value="yes" <?php if ($pconfig['dnslocalhost']) echo "checked=\"checked\""; ?> />
									<strong>
										<?=gettext("Do not use the DNS Forwarder as a DNS server for the firewall"); ?>
									</strong>
									<br />
									<?=gettext("By default localhost (127.0.0.1) will be used as the first DNS server where the DNS Forwarder or DNS Resolver is enabled and set to listen on Localhost, so system can use the local DNS service to perform lookups. ".
									"Checking this box omits localhost from the list of DNS servers."); ?>
								</span>

							</td>
						</tr>
						<tr>
							<td width="22%" valign="top" class="vncell"><?=gettext("Time zone"); ?></td>
							<td width="78%" class="vtable">
418
								<select name="timezone" id="timezone" class="selectpicker" data-style="btn-default" data-live-search="true">
419 420 421 422 423 424 425 426 427 428 429 430 431
									<?php foreach ($timezonelist as $value): ?>
									<?php if(strstr($value, "GMT")) continue; ?>
									<option value="<?=htmlspecialchars($value);?>" <?php if ($value == $pconfig['timezone']) echo "selected=\"selected\""; ?>>
										<?=htmlspecialchars($value);?>
									</option>
									<?php endforeach; ?>
								</select>
								<br />
								<span class="vexpl">
									<?=gettext("Select the location closest to you"); ?>
								</span>
							</td>
						</tr>
432
                        <!--
433 434 435 436 437 438 439 440 441 442 443
						<tr>
							<td width="22%" valign="top" class="vncell">Time update interval</td>
							<td width="78%" class="vtable">
								<input name="timeupdateinterval" type="text" class="formfld unknown" id="timeupdateinterval" size="4" value="<?=htmlspecialchars($pconfig['timeupdateinterval']);?>" />
								<br />
								<span class="vexpl">
									Minutes between network time sync. 300 recommended,
									or 0 to disable
								</span>
							</td>
						</tr>
444
                        -->
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
						<tr>
							<td width="22%" valign="top" class="vncell"><?=gettext("NTP time server"); ?></td>
							<td width="78%" class="vtable">
								<input name="timeservers" type="text" class="formfld unknown" id="timeservers" size="40" value="<?=htmlspecialchars($pconfig['timeservers']);?>" />
								<br />
								<span class="vexpl">
									<?=gettext("Use a space to separate multiple hosts (only one " .
									"required). Remember to set up at least one DNS server " .
									"if you enter a host name here!"); ?>
								</span>
							</td>
						</tr>
						<tr>
							<td width="22%" valign="top" class="vncell"><?php echo gettext("Language");?></td>
							<td width="78%" class="vtable">
460
								<select name="language" class="selectpicker" data-style="btn-default" data-width="auto">
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
									<?php
									foreach(get_locale_list() as $lcode => $ldesc) {
										$selected = ' selected="selected"';
										if($lcode != $pconfig['language'])
											$selected = '';
										echo "<option value=\"{$lcode}\"{$selected}>{$ldesc}</option>";
									}
									?>
								</select>
								<strong>
									<?=gettext("Choose a language for the webConfigurator"); ?>
								</strong>
							</td>
						</tr>
						<tr>
476
							<td width="22%" valign="top" class="vncell"><?=gettext("Theme"); ?></td>
477
							<td width="78%" class="vtable">
478
								<select name="theme" class="selectpicker" data-style="btn-default" data-width="auto">
479
									<?php
480 481 482 483 484 485 486 487
										$files = return_dir_as_array('/usr/local/www/themes/');
										$curtheme = get_current_theme();

										foreach ($files as $file):
											$selected = '';
											if ($file == $curtheme) {
												$selected = ' selected="selected"';
											}
488
									?>
489
									<option <?=$selected;?>><?=$file;?></option>
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
									<?php endforeach; ?>
								</select>
								<strong>
									<?=gettext("This will change the look and feel of"); ?>
									<?=$g['product_name'];?>.
								</strong>
							</td>
						</tr>
						<tr>
							<td width="22%" valign="top">&nbsp;</td>
							<td width="78%">
								<input name="Submit" type="submit" class="btn btn-primary" value="<?=gettext("Save");?>" />
							</td>
						</tr>
					</tbody>
				</table>
506 507 508 509
                    </form>

                </div>
            </section>
510

511
        </div>
512

513 514 515 516
	</div>
</section>

<?php include("foot.inc"); ?>