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 2 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, write to the Free Software
21 ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 ######################################################################
30 my $delete_mode = undef;
32 my $tmp_file_name = undef;
35 ## check for correct parameters
43 print "Usage: $0 configfile share [path] [comment]\n";
47 ## first param is always the config file
48 open (CONFIGFILE, "$ARGV[0]") || die "Unable to open $ARGV[0] for reading!\n";
50 ## FIXME!! Right now we throw away all comments in the file.
51 while (<CONFIGFILE>) {
55 ## eat leading whitespace
58 ## eat trailing whitespace
62 ## throw away comments
63 next if (($_ =~ /^#/) || ($_ =~ /^;/));
65 ## set the current section name for storing the hash
66 if ($_ =~ /^\[.*\]$/) {
68 $_ = substr($_, 1, length($_)-2);
74 print "Bad Section Name - no closing ]\n";
81 ## check for a param = value
83 ($param, $value) = split (/=/, $_,2);
88 $config{$section}{$param} = $value;
93 ## should have a hash of hashes indexed by section name
98 ## We have the smb.conf in our hash of hashes now.
102 $config{$ARGV[1]}{'path'} = $ARGV[2];
103 $config{$ARGV[1]}{'comment'} = $ARGV[3];
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";