3
######################################################################
5
## Simple add/delete/change share command script for Samba
7
## Copyright (C) Gerald Carter 2004.
9
## This program is free software; you can redistribute it and/or modify
10
## it under the terms of the GNU General Public License as published by
11
## the Free Software Foundation; either version 3 of the License, or
12
## (at your option) any later version.
14
## This program is distributed in the hope that it will be useful,
15
## but WITHOUT ANY WARRANTY; without even the implied warranty of
16
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
## GNU General Public License for more details.
19
## You should have received a copy of the GNU General Public License
20
## along with this program; if not, see <http://www.gnu.org/licenses/>.
22
######################################################################
29
my $delete_mode = undef;
31
my $tmp_file_name = undef;
34
## check for correct parameters
42
print "Usage: $0 configfile share [path] [comment]\n";
46
## first param is always the config file
47
open (CONFIGFILE, "$ARGV[0]") || die "Unable to open $ARGV[0] for reading!\n";
49
## FIXME!! Right now we throw away all comments in the file.
50
while (<CONFIGFILE>) {
54
## eat leading whitespace
57
## eat trailing whitespace
61
## throw away comments
62
next if (($_ =~ /^#/) || ($_ =~ /^;/));
64
## set the current section name for storing the hash
65
if ($_ =~ /^\[.*\]$/) {
67
$_ = substr($_, 1, length($_)-2);
73
print "Bad Section Name - no closing ]\n";
80
## check for a param = value
82
($param, $value) = split (/=/, $_,2);
87
$config{$section}{$param} = $value;
92
## should have a hash of hashes indexed by section name
97
## We have the smb.conf in our hash of hashes now.
101
$config{$ARGV[1]}{'path'} = $ARGV[2];
102
$config{$ARGV[1]}{'comment'} = $ARGV[3];
103
$config{$ARGV[1]}{'max connections'} = $ARGV[4];
105
elsif ($delete_mode) {
106
delete $config{$ARGV[1]};
110
## Print the resulting configuration
113
# $tmp_file_name = tmpnam();
114
# print "Using temporary file - $tmp_file_name\n";
115
#} while (!sysopen(TMP, $tmp_file_name, O_RDWR|O_CREAT|O_EXCL));
116
$tmp_file_name = tmpnam();
117
open (TMP, ">$tmp_file_name") || die "Unable to open temporary file for writing!\n";
119
PrintConfigFile(TMP);
121
## now overwrite the original config file
123
system ("cp -pf $ARGV[0] $ARGV[0].bak");
124
system ("cp -pf $tmp_file_name $ARGV[0]");
125
unlink $tmp_file_name;
134
#######################################################################################
137
sub PrintConfigFile {
140
## print the file back out, beginning with the global section
141
print $output "#\n# Generated by $0\n#\n";
143
PrintSection ($output, 'global', $config{'global'});
145
foreach $section (keys %config) {
147
if ("$section" ne "global") {
148
print $output "## Section - [$section]\n";
149
PrintSection ($output, $section, $config{$section});
153
print $output "#\n# end of generated smb.conf\n#\n";
156
#######################################################################################
160
my ($outfile, $name, $section) = @_;
162
print $outfile "[$name]\n";
163
foreach $param (keys %$section) {
164
print $outfile "\t$param".' 'x(25-length($param)). " = $$section{$param}\n";