3a566d1e810ae355c75419d475c8a69119e58b33
[tprouty/samba.git] / examples / scripts / shares / perl / modify_samba_config.pl
1 #!/usr/bin/perl
2
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.
13 ##
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.
18 ##
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.
22 ##
23 ######################################################################
24
25 use POSIX qw(tmpnam);
26
27 ##
28 ## local variables
29 ##
30 my $delete_mode = undef;
31 my $add_mode = undef;
32 my $tmp_file_name = undef;
33
34
35 ## check for correct parameters
36 if ($#ARGV == 1) {
37         $delete_mode = 1;
38 }
39 elsif ($#ARGV == 4) {
40         $add_mode = 1;
41 }
42 else {
43         print "Usage: $0 configfile share [path] [comment]\n"; 
44         exit -1;
45 }
46
47 ## first param is always the config file
48 open (CONFIGFILE, "$ARGV[0]") || die "Unable to open $ARGV[0] for reading!\n";
49
50 ## FIXME!!  Right now we throw away all comments in the file.
51 while (<CONFIGFILE>) {
52
53         chomp($_);
54         
55         ## eat leading whitespace
56         $_ =~ s/^\s*//;
57         
58         ## eat trailing whitespace
59         $_ =~ s/\s*$//;
60         
61
62         ## throw away comments
63         next if (($_ =~ /^#/) || ($_ =~ /^;/));
64
65         ## set the current section name for storing the hash
66         if ($_ =~ /^\[.*\]$/) {
67         
68                 $_ = substr($_, 1, length($_)-2);
69                 
70                 if ( length($_) ) {
71                         $section = $_;
72                 }
73                 else {
74                         print "Bad Section Name - no closing ]\n";
75                         exit -1;
76                 }
77
78                 next;
79         }       
80         
81         ## check for a param = value
82         if ($_ =~ /=/) {
83                 ($param, $value) = split (/=/, $_,2);
84                 $param =~ s/./\l$&/g;
85                 $param =~ s/\s+//g;
86                 $value =~ s/^\s+//;
87                 
88                 $config{$section}{$param} = $value;
89                 
90                 next;
91         }
92
93         ## should have a hash of hashes indexed by section name
94 }
95 close (CONFIGFILE);
96
97 ##
98 ## We have the smb.conf in our hash of hashes now.
99 ## Add or delete 
100 ##
101 if ($add_mode) {
102         $config{$ARGV[1]}{'path'} = $ARGV[2];
103         $config{$ARGV[1]}{'comment'} = $ARGV[3];
104         $config{$ARGV[1]}{'max connections'} = $ARGV[4];
105 }
106 elsif ($delete_mode) {
107         delete $config{$ARGV[1]};
108 }
109
110 ##
111 ## Print the resulting configuration
112 ##
113 #do {
114 #       $tmp_file_name = tmpnam();
115 #       print "Using temporary file - $tmp_file_name\n";
116 #} while (!sysopen(TMP, $tmp_file_name, O_RDWR|O_CREAT|O_EXCL));
117 $tmp_file_name = tmpnam();
118 open (TMP, ">$tmp_file_name") || die "Unable to open temporary file for writing!\n";
119
120 PrintConfigFile(TMP);
121
122 ## now overwrite the original config file
123 close (TMP);
124 system ("cp -pf $ARGV[0] $ARGV[0].bak");
125 system ("cp -pf $tmp_file_name $ARGV[0]");
126 unlink $tmp_file_name; 
127
128
129 exit 0;
130
131
132
133
134
135 #######################################################################################
136 ## PrintConfigFile()
137 ##
138 sub PrintConfigFile {
139         my ($output) = @_;
140
141         ## print the file back out, beginning with the global section
142         print $output "#\n# Generated by $0\n#\n";
143
144         PrintSection ($output, 'global', $config{'global'});
145
146         foreach $section (keys %config) {
147
148                 if ("$section" ne "global") {
149                         print $output "## Section - [$section]\n";
150                         PrintSection ($output, $section, $config{$section});
151                 }
152         }
153
154         print $output "#\n# end of generated smb.conf\n#\n";
155 }
156
157 #######################################################################################
158 ## PrintSection()
159 ##
160 sub PrintSection {
161         my ($outfile, $name, $section) = @_;
162
163         print $outfile "[$name]\n";
164         foreach $param (keys %$section) {
165                 print $outfile "\t$param".' 'x(25-length($param)). " = $$section{$param}\n";
166         }
167         print $outfile "\n";
168
169 }