[ UP ] update cgi. add display percentege bar icons.
[archserver.git] / cgi / samba / modify_samba_config.pl
blobd02bccb2acb822472cd7976302368c9c43a3ea21
1 #!/usr/bin/perl
3 ######################################################################
4 ##
5 ## Simple add/delete/change share command script for Samba
6 ##
7 ## Copyright (C) Gerald Carter 2004.
8 ##
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 ######################################################################
24 use POSIX qw(tmpnam);
27 ## local variables
29 my $delete_mode = undef;
30 my $add_mode = undef;
31 my $tmp_file_name = undef;
34 ## check for correct parameters
35 if ($#ARGV == 1) {
36 $delete_mode = 1;
38 elsif ($#ARGV == 4) {
39 $add_mode = 1;
41 else {
42 print "Usage: $0 configfile share [path] [comment] [writable]\n";
43 exit -1;
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>) {
52 chomp($_);
54 ## eat leading whitespace
55 $_ =~ s/^\s*//;
57 ## eat trailing whitespace
58 $_ =~ s/\s*$//;
61 ## throw away comments
62 next if (($_ =~ /^#/) || ($_ =~ /^;/));
64 ## set the current section name for storing the hash
65 if ($_ =~ /^\[.*\]$/) {
67 $_ = substr($_, 1, length($_)-2);
69 if ( length($_) ) {
70 $section = $_;
72 else {
73 print "Bad Section Name - no closing ]\n";
74 exit -1;
77 next;
80 ## check for a param = value
81 if ($_ =~ /=/) {
82 ($param, $value) = split (/=/, $_,2);
83 $param =~ s/./\l$&/g;
84 $param =~ s/\s+//g;
85 $value =~ s/^\s+//;
87 $config{$section}{$param} = $value;
89 next;
92 ## should have a hash of hashes indexed by section name
94 close (CONFIGFILE);
97 ## We have the smb.conf in our hash of hashes now.
98 ## Add or delete
100 if ($add_mode) {
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
114 #do {
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
124 close (TMP);
125 system ("cp -pf $ARGV[0] $ARGV[0].bak");
126 system ("cp -pf $tmp_file_name $ARGV[0]");
127 unlink $tmp_file_name;
130 exit 0;
136 #######################################################################################
137 ## PrintConfigFile()
139 sub PrintConfigFile {
140 my ($output) = @_;
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 #######################################################################################
159 ## PrintSection()
161 sub PrintSection {
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";
168 print $outfile "\n";