Commit 559787c0 authored by Ad Schellevis's avatar Ad Schellevis Committed by Franco Fichtner

(legacy) restructure setup_serial_port, improve /etc/ttys handling after crash during boot

(cherry picked from commit 2e6f92f6)
(cherry picked from commit 53e87f7a)
(cherry picked from commit 398ae5bb)
(cherry picked from commit 845b96d4)
parent b7d1af0c
......@@ -193,25 +193,6 @@ function write_config($desc = '', $backup = true)
return $config;
}
/****f* config/reset_factory_defaults
* NAME
* reset_factory_defaults - Reset the system to its default configuration.
******/
function reset_factory_defaults($sync = true)
{
mwexec('/bin/rm -r /conf/*');
disable_security_checks();
setup_serial_port(false);
/* as we go through a special case directly reboot */
$shutdown_cmd = '/sbin/shutdown -or now';
if ($sync) {
mwexec($shutdown_cmd);
} else {
mwexec_bg($shutdown_cmd);
}
}
function config_restore($conffile)
{
global $config;
......
......@@ -174,116 +174,6 @@ function reload_interfaces()
configd_run('interface reload');
}
function setup_serial_port($sync = true)
{
global $config;
$serialspeed = (is_numeric($config['system']['serialspeed'])) ? $config['system']['serialspeed'] : '115200';
$serial_enabled = isset($config['system']['enableserial']);
$loader_conf_file = '/boot/loader.conf';
$boot_config_file = '/boot.config';
/* serial console - write out /boot.config */
if (file_exists($boot_config_file)) {
$boot_config = file_get_contents($boot_config_file);
} else {
$boot_config = '';
}
$boot_config_split = explode("\n", $boot_config);
$fd = @fopen($boot_config_file, 'w');
if ($fd) {
foreach ($boot_config_split as $bcs) {
if (stristr($bcs, '-D') || stristr($bcs, '-h')) {
continue;
}
if ($bcs != '') {
@fwrite($fd, "{$bcs}\n");
}
}
if ($serial_enabled) {
@fwrite($fd, "-S{$serialspeed} -D\n");
}
fclose($fd);
}
$boot_config = @file_get_contents($loader_conf_file);
$boot_config_split = explode("\n", $boot_config);
if(count($boot_config_split) > 0) {
$new_boot_config = array();
// Loop through and only add lines that are not empty, and which
// do not contain a console directive.
foreach($boot_config_split as $bcs) {
if(!empty($bcs)
&& (stripos($bcs, "console") === false)
&& (stripos($bcs, "boot_multicons") === false)
&& (stripos($bcs, "boot_serial") === false)
&& (stripos($bcs, "hw.usb.no_pf") === false)
&& (stripos($bcs, "autoboot_delay") === false)) {
$new_boot_config[] = $bcs;
}
}
if ($serial_enabled) {
$new_boot_config[] = 'boot_multicons="YES"';
$new_boot_config[] = 'boot_serial="YES"';
$primaryconsole = $config['system']['primaryconsole'];
switch ($primaryconsole) {
case "video":
$new_boot_config[] = 'console="vidconsole,comconsole"';
break;
case "serial":
default:
$new_boot_config[] = 'console="comconsole,vidconsole"';
}
}
$new_boot_config[] = 'comconsole_speed="' . $serialspeed . '"';
$new_boot_config[] = 'hw.usb.no_pf="1"';
$new_boot_config[] = 'autoboot_delay="3"';
@file_put_contents($loader_conf_file, implode("\n", $new_boot_config) . "\n");
}
$ttys = file_get_contents("/etc/ttys");
$ttys_split = explode("\n", $ttys);
$fd = fopen("/etc/ttys", "w");
$on_off = $serial_enabled ? 'on' : 'off';
if (isset($config['system']['disableconsolemenu'])) {
$console_type = 'Pc';
$serial_type = 'std.' . $serialspeed;
} else {
$console_type = 'al.Pc';
$serial_type = 'al.' . $serialspeed;
}
foreach($ttys_split as $tty) {
if (stristr($tty, "ttyv0")) {
fwrite($fd, "ttyv0 \"/usr/libexec/getty {$console_type}\" cons25 on secure\n");
} elseif (stristr($tty, "ttyu0")) {
fwrite($fd, "ttyu0 \"/usr/libexec/getty {$serial_type}\" cons25 {$on_off} secure\n");
} else {
fwrite($fd, $tty . "\n");
}
}
unset($on_off, $console_type, $serial_type);
fclose($fd);
if ($sync) {
reload_ttys();
}
}
function reload_ttys()
{
/* force init(8) to reload /etc/ttys */
exec('/bin/kill -HUP 1');
}
/* Any PPPoE servers enabled? */
function is_pppoe_server_enabled()
{
......
......@@ -1827,11 +1827,6 @@ function system_reboot($sync = false)
}
}
function system_console_configure()
{
setup_serial_port();
}
function system_setup_sysctl()
{
activate_sysctls();
......@@ -1895,3 +1890,102 @@ function get_possible_traffic_source_addresses($include_ipv6_link_local=false) {
}
return $sourceips;
}
function system_console_configure()
{
global $config;
$serialspeed = (!empty($config['system']['serialspeed']) && is_numeric($config['system']['serialspeed'])) ? $config['system']['serialspeed'] : '115200';
$serial_enabled = isset($config['system']['enableserial']);
// ** serial console - write out /boot.config
if ($serial_enabled) {
file_put_contents('/boot.config', "-S{$serialspeed} -D\n");
} else {
@unlink('/boot.config');
}
// ** console settings in /boot/loader.conf
$new_boot_config = array();
$new_boot_config['boot_multicons'] = $serial_enabled ? '"YES"' : null;
$new_boot_config['boot_serial'] = $serial_enabled ? '"YES"' : null;
if ($serial_enabled) {
$primaryconsole = !empty($config['system']['primaryconsole']) ? $config['system']['primaryconsole'] : "";
$new_boot_config['console'] = $primaryconsole == "video" ? '"vidconsole,comconsole"' : '"comconsole,vidconsole"';
} else {
$new_boot_config['console'] = null;
}
$new_boot_config['comconsole_speed'] = '"'.$serialspeed.'"';
$new_boot_config['hw.usb.no_pf'] = '"1"';
$new_boot_config['autoboot_delay'] = '"3"';
$new_loader_conf = "";
// construct OPNsense config for /boot/loader.conf
foreach ($new_boot_config as $param => $value) {
if (!empty($value)) {
$new_loader_conf .= "{$param}={$value}\n";
}
}
// copy non matched settings in /boot/loader.conf
foreach (explode("\n", @file_get_contents('/boot/loader.conf')) as $line) {
if (!empty($line) && !array_key_exists(trim(explode('=', $line)[0]), $new_boot_config)) {
$new_loader_conf .= $line . "\n";
}
}
@file_put_contents('/boot/loader.conf', $new_loader_conf);
// ** setup /etc/ttys
$etc_ttys_lines = explode("\n", file_get_contents('/etc/ttys'));
$fd = fopen('/etc/ttys', 'w');
$on_off_secure = $serial_enabled ? 'onifconsole secure' : 'off secure';
$terminal_type = 'cons25'; /* XXX standard is 'xterm' for virtual, 'vt100' for serial */
if (isset($config['system']['disableconsolemenu'])) {
$console_type = 'Pc';
$serial_type = 'std.' . $serialspeed;
} else {
$console_type = 'al.Pc';
$serial_type = 'al.' . $serialspeed;
}
foreach ($etc_ttys_lines as $tty) {
if (strpos($tty, 'ttyv0') === 0) {
/* first virtual terminal */
fwrite($fd, "ttyv0\t\"/usr/libexec/getty {$console_type}\"\t\t{$terminal_type}\ton secure\n");
continue;
}
foreach (array('ttyu0', 'ttyu1', 'ttyu2', 'ttyu3') as $serialport) {
if (strpos($tty, $serialport) === 0) {
/* each serial terminal */
fwrite($fd, "{$serialport}\t\"/usr/libexec/getty {$serial_type}\"\t{$terminal_type}\t{$on_off_secure}\n");
/* skip to next line in outer loop */
continue 2;
}
}
if (!empty($tty)) {
/* all other lines stay the same */
fwrite($fd, $tty . "\n");
}
}
fclose($fd);
/* force init(8) to reload /etc/ttys */
exec('/bin/kill -HUP 1');
}
/****f* config/reset_factory_defaults
* NAME
* reset_factory_defaults - Reset the system to its default configuration.
******/
function reset_factory_defaults($sync = true)
{
mwexec('/bin/rm -r /conf/*');
disable_security_checks();
/* as we go through a special case directly reboot */
$shutdown_cmd = '/sbin/shutdown -or now';
if ($sync) {
mwexec($shutdown_cmd);
} else {
mwexec_bg($shutdown_cmd);
}
}
......@@ -168,6 +168,9 @@ set_device_perms();
unmute_kernel_msgs();
echo "done.\n";
/* configure console menu */
system_console_configure();
/* Display live system's early boot options */
if (is_install_media()) {
rescue_detect_keypress();
......@@ -292,9 +295,6 @@ echo "done.\n";
/* start load balancer daemon */
relayd_configure();
/* configure console menu */
system_console_configure();
/* start DHCP service */
services_dhcpd_configure();
......@@ -322,9 +322,6 @@ $ipsec_dynamic_hosts = ipsec_configure();
/* start SNMP service */
services_snmpd_configure();
/* lock down console if necessary */
reload_ttys();
/* load graphing functions */
enable_rrd_graphing();
......
......@@ -27,10 +27,9 @@
POSSIBILITY OF SUCH DAMAGE.
*/
/* parse the configuration and include all functions used below */
require_once("util.inc");
require_once("config.lib.inc");
require_once("pfsense-utils.inc");
require_once("system.inc");
$fp = fopen('php://stdin', 'r');
$yes_no_prompt = '[y|n]? ';
......
......@@ -109,6 +109,59 @@ $etc_shells = <<<EOF
EOF;
$etc_ttys = <<<EOF
#
# \$FreeBSD$
# @(#)ttys 5.1 (Berkeley) 4/17/89
#
# This file specifies various information about terminals on the system.
# It is used by several different programs. Common entries for the
# various columns include:
#
# name The name of the terminal device.
#
# getty The program to start running on the terminal. Typically a
# getty program, as the name implies. Other common entries
# include none, when no getty is needed, and xdm, to start the
# X Window System.
#
# type The initial terminal type for this port. For hardwired
# terminal lines, this will contain the type of terminal used.
# For virtual consoles, the correct type is typically xterm.
# Other common values include dialup for incoming modem ports, and
# unknown when the terminal type cannot be predetermined.
#
# status Must be on or off. If on, init will run the getty program on
# the specified port. If the word "secure" appears, this tty
# allows root login.
#
# name getty type status comments
#
# If console is marked "insecure", then init will ask for the root password
# when going to single-user mode.
console none unknown off secure
#
ttyv0 "/usr/libexec/getty Pc" xterm on secure
# Virtual terminals
ttyv1 "/usr/libexec/getty Pc" xterm on secure
ttyv2 "/usr/libexec/getty Pc" xterm on secure
ttyv3 "/usr/libexec/getty Pc" xterm on secure
ttyv4 "/usr/libexec/getty Pc" xterm on secure
ttyv5 "/usr/libexec/getty Pc" xterm on secure
ttyv6 "/usr/libexec/getty Pc" xterm on secure
ttyv7 "/usr/libexec/getty Pc" xterm on secure
ttyv8 "/usr/local/bin/xdm -nodaemon" xterm off secure
# Serial terminals
# The 'dialup' keyword identifies dialin lines to login, fingerd etc.
ttyu0 "/usr/libexec/getty 3wire" vt100 onifconsole secure
ttyu1 "/usr/libexec/getty 3wire" vt100 onifconsole secure
ttyu2 "/usr/libexec/getty 3wire" vt100 onifconsole secure
ttyu3 "/usr/libexec/getty 3wire" vt100 onifconsole secure
# Dumb console
dcons "/usr/libexec/getty std.9600" vt100 off secure
EOF;
function recover_ports()
{
$actions = array(
......@@ -151,7 +204,7 @@ function recover_rebuild()
passthru('/bin/sync');
}
function recover_base($etc_group, $etc_master_passwd, $etc_shells)
function recover_base($etc_group, $etc_master_passwd, $etc_shells, $etc_ttys)
{
echo "===> Restoring /etc/group\n";
file_put_contents('/etc/group', $etc_group);
......@@ -162,6 +215,9 @@ function recover_base($etc_group, $etc_master_passwd, $etc_shells)
echo "===> Restoring /etc/shells\n";
file_put_contents('/etc/shells', $etc_shells);
echo "===> Restoring /etc/ttys\n";
file_put_contents('/etc/ttys', $etc_ttys);
recover_rebuild();
}
......@@ -175,13 +231,13 @@ $stage = isset($argv[1]) ? $argv[1] : 'both';
switch ($stage) {
case 'base':
recover_base($etc_group, $etc_master_passwd, $etc_shells);
recover_base($etc_group, $etc_master_passwd, $etc_shells, $etc_ttys);
break;
case 'pkg':
recover_pkg();
break;
default:
recover_base($etc_group, $etc_master_passwd, $etc_shells);
recover_base($etc_group, $etc_master_passwd, $etc_shells, $etc_ttys);
recover_pkg();
break;
}
......@@ -390,7 +390,6 @@ if ($_SERVER['REQUEST_METHOD'] === 'GET') {
convert_config();
$savemsg = gettext("The m0n0wall configuration has been restored and upgraded to OPNsense.");
}
setup_serial_port();
} else {
$input_errors[] = gettext("The configuration could not be restored.");
}
......
......@@ -29,11 +29,11 @@
*/
require_once("guiconfig.inc");
require_once("pfsense-utils.inc");
require_once("system.inc");
include("head.inc");
?>
?>
<body>
<?php include("fbegin.inc"); ?>
......@@ -69,11 +69,12 @@ include("head.inc");
</div>
</div>
</section>
<?php include("foot.inc");
// reset to factory defaults when submit is pressed.
<?php
include("foot.inc");
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (!empty($_POST['Submit'])) {
reset_factory_defaults(false);
}
}
?>
......@@ -231,7 +231,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'GET') {
$savemsg .= sprintf("<br />" . gettext("One moment...redirecting to %s in 20 seconds."), $url);
}
setup_serial_port();
system_console_configure();
system_hosts_generate();
// Restart DNS in case dns rebinding toggled
......@@ -506,7 +506,7 @@ include("head.inc");
<td><a id="help_for_enableserial" href="#" class="showhelp"><i class="fa fa-info-circle"></i></a> <?=gettext("Serial Terminal"); ?></td>
<td width="78%">
<input name="enableserial" type="checkbox" id="enableserial" value="yes" <?=!empty($pconfig['enableserial']) ? "checked=\"checked\"" : "";?> />
<strong><?=gettext("Enables the first serial port with 115200/8/N/1 by default, or another speed selectable below."); ?></strong>
<strong><?=gettext("Enable serial ports with 115200/8/N/1 by default, or another speed selectable below."); ?></strong>
<div class="hidden" for="help_for_enableserial">
<?=gettext("Note: This will redirect the console output and messages to the serial port. You can still access the console menu from the internal video card/keyboard. A null modem serial cable or adapter is required to use the serial console."); ?>
</div>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment