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'
8 ## Author : Gerald (Jerry) Carter <jerry@samba.org>
16 my $delete_mode = undef;
18 my $tmp_file_name = undef;
21 ## check for correct parameters
29 print "Usage: $0 configfile share [path] [comment]\n";
33 ## first param is always the config file
34 open (CONFIGFILE
, "$ARGV[0]") || die "Unable to open $ARGV[0] for reading!\n";
36 ## FIXME!! Right now we throw away all comments in the file.
37 while (<CONFIGFILE
>) {
41 ## eat leading whitespace
44 ## eat trailing whitespace
48 ## throw away comments
49 next if (($_ =~ /^#/) || ($_ =~ /^;/));
51 ## set the current section name for storing the hash
52 if ($_ =~ /^\[.*\]$/) {
54 $_ = substr($_, 1, length($_)-2);
60 print "Bad Section Name - no closing ]\n";
67 ## check for a param = value
69 ($param, $value) = split (/=/, $_,2);
74 $config{$section}{$param} = $value;
79 ## should have a hash of hashes indexed by section name
84 ## We have the smb.conf in our hash of hashes now.
88 $config{$ARGV[1]}{'path'} = $ARGV[2];
89 $config{$ARGV[1]}{'comment'} = $ARGV[3];
91 elsif ($delete_mode) {
92 delete $config{$ARGV[1]};
96 ## Print the resulting configuration
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";
105 PrintConfigFile
(TMP
);
107 ## now overwrite the original config file
109 system ("cp -pf $ARGV[0] $ARGV[0].bak");
110 system ("cp -pf $tmp_file_name $ARGV[0]");
111 unlink $tmp_file_name;
120 #######################################################################################
123 sub PrintConfigFile
{
126 ## print the file back out, beginning with the global section
127 print $output "#\n# Generated by $0\n#\n";
129 PrintSection
($output, 'global', $config{'global'});
131 foreach $section (keys %config) {
133 if ("$section" ne "global") {
134 print $output "## Section - [$section]\n";
135 PrintSection
($output, $section, $config{$section});
139 print $output "#\n# end of generated smb.conf\n#\n";
142 #######################################################################################
146 my ($outfile, $name, $section) = @_;
148 print $outfile "[$name]\n";
149 foreach $param (keys %$section) {
150 print $outfile "\t$param".' 'x
(25-length($param)). " = $$section{$param}\n";