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] [writable]\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]}{'writable'} = $ARGV[4];
104 $config{$ARGV[1]}{'read list'} = "admin";
105 $config{$ARGV[1]}{'write list'} = "administrator";
107 elsif ($delete_mode) {
108 delete $config{$ARGV[1]};
112 ## Print the resulting configuration
115 # $tmp_file_name = tmpnam();
116 # print "Using temporary file - $tmp_file_name\n";
117 #} while (!sysopen(TMP, $tmp_file_name, O_RDWR|O_CREAT|O_EXCL));
118 $tmp_file_name = tmpnam
();
119 open (TMP
, ">$tmp_file_name") || die "Unable to open temporary file for writing!\n";
121 PrintConfigFile
(TMP
);
123 ## now overwrite the original config file
125 system ("cp -pf $ARGV[0] $ARGV[0].bak");
126 system ("cp -pf $tmp_file_name $ARGV[0]");
127 unlink $tmp_file_name;
136 #######################################################################################
139 sub PrintConfigFile
{
142 ## print the file back out, beginning with the global section
143 print $output "#\n# Generated by $0\n#\n";
145 PrintSection
($output, 'global', $config{'global'});
147 foreach $section (keys %config) {
149 if ("$section" ne "global") {
150 print $output "## Section - [$section]\n";
151 PrintSection
($output, $section, $config{$section});
155 print $output "#\n# end of generated smb.conf\n#\n";
158 #######################################################################################
162 my ($outfile, $name, $section) = @_;
164 print $outfile "[$name]\n";
165 foreach $param (keys %$section) {
166 print $outfile "\t$param".' 'x
(25-length($param)). " = $$section{$param}\n";