~zulcss/samba/server-dailies-3.4

« back to all changes in this revision

Viewing changes to examples/scripts/shares/perl/modify_samba_config.pl

  • Committer: Chuck Short
  • Date: 2010-09-28 20:38:39 UTC
  • Revision ID: zulcss@ubuntu.com-20100928203839-pgjulytsi9ue63x1
Initial version

Show diffs side-by-side

added added

removed removed

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