Commit 398ae5bb authored by Franco Fichtner's avatar Franco Fichtner

system: migrate setup_serial_port() and friends

parent 951a4ceb
...@@ -193,25 +193,6 @@ function write_config($desc = '', $backup = true) ...@@ -193,25 +193,6 @@ function write_config($desc = '', $backup = true)
return $config; 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) function config_restore($conffile)
{ {
global $config; global $config;
......
...@@ -92,87 +92,6 @@ function restore_config_section($section_name, $new_contents) ...@@ -92,87 +92,6 @@ function restore_config_section($section_name, $new_contents)
return true; return true;
} }
function setup_serial_port($sync = true)
{
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");
} elseif (file_exists('/boot.config')) {
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
// minimize chances of /etc/ttys corruption, keep a copy of the original ttys file
if (!file_exists('/etc/ttys.opnsense') || filesize('/etc/ttys.opnsense') < 100) {
copy('/etc/ttys', '/etc/ttys.opnsense');
}
$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(explode("\n", file_get_contents("/etc/ttys.opnsense")) 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");
} elseif (!empty($tty)) {
fwrite($fd, $tty . "\n");
}
}
fclose($fd);
if ($sync) {
reload_ttys();
}
}
function reload_ttys()
{
/* force init(8) to reload /etc/ttys */
exec('/bin/kill -HUP 1');
}
/* This xml 2 array function is courtesy of the php.net comment section on xml_parse. /* This xml 2 array function is courtesy of the php.net comment section on xml_parse.
* it is roughly 4 times faster then our existing pfSense parser but due to the large * it is roughly 4 times faster then our existing pfSense parser but due to the large
* size of the RRD xml dumps this is required. * size of the RRD xml dumps this is required.
......
...@@ -1925,3 +1925,103 @@ function load_thermal_module() ...@@ -1925,3 +1925,103 @@ function load_thermal_module()
mwexecf('/sbin/kldload %s', $config['system']['thermal_hardware'], true); mwexecf('/sbin/kldload %s', $config['system']['thermal_hardware'], true);
} }
} }
function setup_serial_port($sync = true)
{
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");
} elseif (file_exists('/boot.config')) {
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
// minimize chances of /etc/ttys corruption, keep a copy of the original ttys file
if (!file_exists('/etc/ttys.opnsense') || filesize('/etc/ttys.opnsense') < 100) {
copy('/etc/ttys', '/etc/ttys.opnsense');
}
$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(explode("\n", file_get_contents("/etc/ttys.opnsense")) 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");
} elseif (!empty($tty)) {
fwrite($fd, $tty . "\n");
}
}
fclose($fd);
if ($sync) {
reload_ttys();
}
}
function reload_ttys()
{
/* 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();
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);
}
}
...@@ -27,10 +27,9 @@ ...@@ -27,10 +27,9 @@
POSSIBILITY OF SUCH DAMAGE. POSSIBILITY OF SUCH DAMAGE.
*/ */
/* parse the configuration and include all functions used below */
require_once("util.inc"); require_once("util.inc");
require_once("config.lib.inc"); require_once("config.lib.inc");
require_once("pfsense-utils.inc"); require_once("system.inc");
$fp = fopen('php://stdin', 'r'); $fp = fopen('php://stdin', 'r');
$yes_no_prompt = '[y|n]? '; $yes_no_prompt = '[y|n]? ';
......
...@@ -29,11 +29,11 @@ ...@@ -29,11 +29,11 @@
*/ */
require_once("guiconfig.inc"); require_once("guiconfig.inc");
require_once("pfsense-utils.inc"); require_once("system.inc");
include("head.inc"); include("head.inc");
?>
?>
<body> <body>
<?php include("fbegin.inc"); ?> <?php include("fbegin.inc"); ?>
...@@ -69,11 +69,12 @@ include("head.inc"); ...@@ -69,11 +69,12 @@ include("head.inc");
</div> </div>
</div> </div>
</section> </section>
<?php include("foot.inc"); <?php
// reset to factory defaults when submit is pressed.
include("foot.inc");
if ($_SERVER['REQUEST_METHOD'] === 'POST') { if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (!empty($_POST['Submit'])) { if (!empty($_POST['Submit'])) {
reset_factory_defaults(false); reset_factory_defaults(false);
} }
} }
?>
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