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