2019-07-19 04:54:47 +01:00
|
|
|
#!/usr/bin/perl
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
use IO::File;
|
|
|
|
use IO::Pipe;
|
|
|
|
use feature 'switch';
|
|
|
|
|
2019-07-19 19:34:46 +01:00
|
|
|
my ($filename, $conf);
|
2019-07-19 04:54:47 +01:00
|
|
|
|
|
|
|
$filename = '/boot/firmware/sysconf.txt';
|
|
|
|
|
2019-07-19 19:34:46 +01:00
|
|
|
logger('info', "Reading the system configuration settings from $filename");
|
|
|
|
$conf = read_conf($filename);
|
2019-07-19 04:54:47 +01:00
|
|
|
|
2019-07-19 19:34:46 +01:00
|
|
|
if (my $pass = delete($conf->{root_pw})) {
|
2019-07-19 15:34:17 +01:00
|
|
|
my $pipe;
|
2019-07-19 19:34:46 +01:00
|
|
|
logger('debug', 'Resetting root password');
|
2019-07-20 02:40:14 +01:00
|
|
|
unless (open($pipe, '|-', '/usr/sbin/chpasswd')) {
|
|
|
|
my $err = $!;
|
|
|
|
logger('error', "Could not run chpasswd: $err");
|
|
|
|
die $err;
|
|
|
|
}
|
2019-07-19 19:34:46 +01:00
|
|
|
$pipe->print("root:$pass");
|
2019-07-19 15:34:17 +01:00
|
|
|
close($pipe);
|
2019-07-19 04:54:47 +01:00
|
|
|
}
|
|
|
|
|
2020-08-15 06:01:54 +01:00
|
|
|
if (my $root_authorized_key = delete($conf->{root_authorized_key})) {
|
|
|
|
my $fh;
|
|
|
|
logger('debug', "Adding key to root's authorized_keys");
|
|
|
|
if(! -d "/root/.ssh") {
|
|
|
|
if(!mkdir("/root/.ssh", 0700)) {
|
|
|
|
my $err = sprintf("Could not create /root/.ssh directory: %s", $!);
|
|
|
|
logger('error', $err);
|
|
|
|
die $err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unless ($fh = IO::File->new('/root/.ssh/authorized_keys', 'w', 0600)) {
|
|
|
|
my $err = $!;
|
|
|
|
logger('error', "Could not write /root/.ssh/authorized_keys: $err");
|
|
|
|
die $err;
|
|
|
|
}
|
|
|
|
$fh->print($root_authorized_key);
|
|
|
|
$fh->close;
|
|
|
|
}
|
|
|
|
|
2019-07-19 19:34:46 +01:00
|
|
|
if (my $name = delete($conf->{hostname})) {
|
|
|
|
my $fh;
|
|
|
|
logger('debug', "Setting hostname to '$name'");
|
2019-07-20 02:40:14 +01:00
|
|
|
unless ($fh = IO::File->new('/etc/hostname', 'w')) {
|
|
|
|
my $err = $!;
|
|
|
|
logger('error', "Could not write hostname '$name': $err");
|
|
|
|
die $err;
|
|
|
|
}
|
2019-07-19 19:34:46 +01:00
|
|
|
$fh->print($name);
|
|
|
|
$fh->close;
|
|
|
|
system('hostname', '--file', '/etc/hostname');
|
|
|
|
}
|
|
|
|
|
2019-07-20 02:40:14 +01:00
|
|
|
rewrite_conf_file($filename, $conf);
|
2019-07-19 15:34:17 +01:00
|
|
|
|
|
|
|
exit 0;
|
2019-07-19 04:54:47 +01:00
|
|
|
|
2019-07-19 19:34:46 +01:00
|
|
|
sub read_conf {
|
|
|
|
my ($file, $conf, $fh);
|
|
|
|
$file = shift;
|
|
|
|
|
|
|
|
$conf = {};
|
2019-07-20 02:40:14 +01:00
|
|
|
unless ($fh = IO::File->new($filename, 'r')) {
|
|
|
|
my $err = $!;
|
|
|
|
logger('error', "Could not read from configuration file '$filename': $err");
|
|
|
|
# Not finding the config file is not fatal: there is just
|
|
|
|
# nothing to configure!
|
|
|
|
return $conf;
|
|
|
|
}
|
2019-07-19 19:34:46 +01:00
|
|
|
while (my $line = $fh->getline) {
|
|
|
|
my ($key, $value);
|
|
|
|
# Allow for comments, and properly ignore them
|
|
|
|
$line =~ s/#.+//;
|
|
|
|
if ( ($key, $value) = ($line =~ m/^\s*([^=]+)\s*=\s*(.*)\s*$/)) {
|
|
|
|
$key = lc($key);
|
|
|
|
if (exists($conf->{$key})) {
|
|
|
|
logger('warn',
|
|
|
|
"Repeated configuration key: $key. " .
|
|
|
|
"Overwriting with new value ($value)");
|
|
|
|
}
|
|
|
|
$conf->{$key} = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$fh->close;
|
|
|
|
|
|
|
|
return $conf;
|
|
|
|
}
|
|
|
|
|
2019-07-19 15:34:17 +01:00
|
|
|
sub logger {
|
|
|
|
my ($prio, $msg) = @_;
|
2019-07-20 02:40:14 +01:00
|
|
|
system('logger', '-p', "daemon.$prio",
|
2019-07-19 16:46:52 +01:00
|
|
|
'-t', 'rpi-set-sysconf', $msg);
|
2019-07-19 15:34:17 +01:00
|
|
|
}
|
2019-07-20 02:40:14 +01:00
|
|
|
|
|
|
|
sub rewrite_conf_file {
|
|
|
|
my ($filename, $conf) = @_;
|
|
|
|
my $fh;
|
|
|
|
unless ($fh = IO::File->new($filename, 'w')) {
|
|
|
|
my $err = $!;
|
|
|
|
logger('error', "Could not write to configuration file '$filename': $err");
|
|
|
|
die $err;
|
|
|
|
}
|
|
|
|
$fh->print(
|
|
|
|
q(# This file will be automatically evaluated and installed at next boot
|
|
|
|
# time, and regenerated (to avoid leaking passwords and such information).
|
|
|
|
#
|
|
|
|
# To force it to be evaluated immediately, you can run (as root):
|
|
|
|
#
|
2020-09-08 17:25:35 +01:00
|
|
|
# /usr/local/sbin/rpi-set-sysconf
|
2019-07-20 02:40:14 +01:00
|
|
|
#
|
|
|
|
# You can disable the file evaluation by disabling the rpi-set-sysconf
|
|
|
|
# service in systemd:
|
|
|
|
#
|
|
|
|
# systemctl disable rpi-set-sysconf
|
|
|
|
#
|
|
|
|
# Comments (all portions of a line following a '#' character) are
|
|
|
|
# ignored. This file is read line by line. Valid
|
|
|
|
# configuration lines are of the form 'key=value'. Whitespace around
|
|
|
|
# 'key' and 'value' is ignored. This file will be _regenerated_ every
|
|
|
|
# time it is evaluated.
|
|
|
|
#
|
|
|
|
# We follow the convention to indent with one space comments, and
|
|
|
|
# leave no space to indicate the line is an example that could be
|
|
|
|
# uncommented.
|
|
|
|
|
|
|
|
# root_pw - Set a password for the root user (by default, it allows
|
|
|
|
# for a passwordless login)
|
|
|
|
#root_pw=FooBar
|
|
|
|
|
2020-08-15 06:01:54 +01:00
|
|
|
# root_authorized_key - Set an authorized key for a root ssh login
|
|
|
|
#root_authorized_key=
|
|
|
|
|
2019-07-20 02:40:14 +01:00
|
|
|
# hostname - Set the system hostname.
|
|
|
|
#hostname=rpi
|
|
|
|
));
|
|
|
|
|
|
|
|
if (scalar keys %$conf) {
|
|
|
|
logger('warn', 'Unprocessed keys left in $filename: ' .
|
|
|
|
join(', ', sort keys %$conf));
|
|
|
|
$fh->print(
|
|
|
|
q(
|
|
|
|
# We found the following unhandled keys - That means, the
|
|
|
|
# configuration script does not know how to handle them. Please
|
|
|
|
# double-check them!
|
|
|
|
));
|
|
|
|
$fh->print(join('', map {sprintf("%s=%s\n", $_, $conf->{$_})} sort keys %$conf));
|
|
|
|
}
|
|
|
|
$fh->close;
|
|
|
|
}
|
|
|
|
|
|
|
|
|