modify_samba_config.pl [plain text]
use POSIX qw(tmpnam);
my $delete_mode = undef;
my $add_mode = undef;
my $tmp_file_name = undef;
if ($ $delete_mode = 1;
}
elsif ($ $add_mode = 1;
}
else {
print "Usage: $0 configfile share [path] [comment]\n";
exit -1;
}
open (CONFIGFILE, "$ARGV[0]") || die "Unable to open $ARGV[0] for reading!\n";
while (<CONFIGFILE>) {
chomp($_);
$_ =~ s/^\s*//;
$_ =~ s/\s*$//;
next if (($_ =~ /^
if ($_ =~ /^\[.*\]$/) {
$_ = substr($_, 1, length($_)-2);
if ( length($_) ) {
$section = $_;
}
else {
print "Bad Section Name - no closing ]\n";
exit -1;
}
next;
}
if ($_ =~ /=/) {
($param, $value) = split (/=/, $_);
$param =~ s/./\l$&/g;
$param =~ s/\s+//g;
$value =~ s/^\s+//;
$config{$section}{$param} = $value;
next;
}
}
close (CONFIGFILE);
if ($add_mode) {
$config{$ARGV[1]}{'path'} = $ARGV[2];
$config{$ARGV[1]}{'comment'} = $ARGV[3];
}
elsif ($delete_mode) {
delete $config{$ARGV[1]};
}
$tmp_file_name = tmpnam();
open (TMP, ">$tmp_file_name") || die "Unable to open temporary file for writing!\n";
PrintConfigFile(TMP);
close (TMP);
system ("cp -pf $ARGV[0] $ARGV[0].bak");
system ("cp -pf $tmp_file_name $ARGV[0]");
unlink $tmp_file_name;
exit 0;
sub PrintConfigFile {
my ($output) = @_;
print $output "#\n# Generated by $0\n#\n";
PrintSection ($output, 'global', $config{'global'});
foreach $section (keys %config) {
if ("$section" ne "global") {
print $output "## Section - [$section]\n";
PrintSection ($output, $section, $config{$section});
}
}
print $output "#\n# end of generated smb.conf\n#\n";
}
sub PrintSection {
my ($outfile, $name, $section) = @_;
print $outfile "[$name]\n";
foreach $param (keys %$section) {
print $outfile "\t$param".' 'x(25-length($param)). " = $$section{$param}\n";
}
print $outfile "\n";
}