use strict;
$0 = 'smb-config-upgrade';
my $pkg_path = shift || '';
my $install_dest = shift || '';
my $install_volume = shift || '';
my $install_operation = $ENV{SCRIPT_NAME} ? $ENV{SCRIPT_NAME} : 'postupgrade';
my $SMB_BACKUP = $ENV{SMB_BACKUP} ? $ENV{SMB_BACKUP}
: "$install_volume/etc/smb.conf.old";
my $SMB_CONF = $ENV{SMB_CONF} ? $ENV{SMB_CONF}
: "$install_volume/etc/smb.conf";
my $SMB_TEMPLATE = $ENV{SMB_TEMPLATE} ? $ENV{SMB_TEMPLATE}
: "$install_volume/etc/smb.conf.template";
sub slurp
{
my $path = shift;
my $fh;
my $data;
local $/=undef;
open ($fh, "< $path") or return undef;
$data = <$fh>;
close $fh;
return $data;
}
sub write_file
{
require File::Temp;
my $config = shift;
my $data = shift;
my ($fh, $name) = File::Temp::tempfile(
TEMPLATE =>'.smb.upgrade.XXXXXXXX',
UNLINK => 0,
DIR => "$install_volume/etc");
$fh->write($data);
$fh->flush();
$fh->sync();
rename $name, $config
or die "$0: unable to replace $config: $!";
chmod 0644, $config;
$fh->close();
return 1;
}
my $template; my $smbconf; my $required;
unless ($template = slurp($SMB_TEMPLATE)) {
die "$0: smb.conf template is corrupt or missing";
}
unless ($smbconf = slurp($SMB_CONF)) {
warn "$0: smb.conf is corrupt or missing";
$smbconf = "";
}
$template =~
m/
(
;\s+BEGIN\s+required
.*
;\s+END\s+required[^\n]*$
)
/smx;
if (!defined($1) || length($1) == 0) {
print "$0: missing or corrupt required configuration section in $SMB_TEMPLATE\n";
exit 1;
}
$required=$1;
unless ($smbconf =~
s/
;\s+BEGIN\s+required
.*
;\s+END\s+required[^\n]*$
/$required/smx) {
warn "$0: replacing entire contents of $SMB_CONF";
$smbconf = $template;
}
rename $SMB_CONF, $SMB_BACKUP
or warn "$0: unable to back $SMB_CONF up to $SMB_BACKUP: $!";
unless (write_file($SMB_CONF, $smbconf)) {
die "$0: unable to replace $SMB_CONF";
}
exit 0;