summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorGerald Carter <jerry@samba.org>2001-05-04 15:36:43 +0000
committerGerald Carter <jerry@samba.org>2001-05-04 15:36:43 +0000
commitc6b7455764f5b5f9491bc22a6f70f794f2668fd5 (patch)
tree8dd425d263d775115cc3d952b1bcb2a537bea97c /examples
parent4ec4436c960103326eca9e6d3f6c3fc4f5acf683 (diff)
downloadsamba-c6b7455764f5b5f9491bc22a6f70f794f2668fd5.tar.gz
samba-c6b7455764f5b5f9491bc22a6f70f794f2668fd5.tar.xz
samba-c6b7455764f5b5f9491bc22a6f70f794f2668fd5.zip
simple perl script to parse smb.conf into a hash of hashes.
I'll include a wrapper in a few minutes which will give us a sample add|delete share command
Diffstat (limited to 'examples')
-rwxr-xr-xexamples/misc/modify_samba_config.pl126
1 files changed, 126 insertions, 0 deletions
diff --git a/examples/misc/modify_samba_config.pl b/examples/misc/modify_samba_config.pl
new file mode 100755
index 00000000000..df03ee52741
--- /dev/null
+++ b/examples/misc/modify_samba_config.pl
@@ -0,0 +1,126 @@
+#!/usr/bin/perl
+
+##
+## local variables
+##
+my $delete_mode = undef;
+my $add_mode = undef;
+
+
+## check for correct parameters
+if ($#ARGV == 1) {
+ $delete_mode = 1;
+}
+elsif ($#ARGV == 3) {
+ $add_mode = 1;
+}
+else {
+ print "Usage: $0 configfile share [path] [comment]\n";
+ exit -1;
+}
+
+## first param is always the config file
+open (CONFIGFILE, "$ARGV[0]") || die "Unable to open $ARGV[0] for reading!\n";
+
+## FIXME!! Right now we throw away all comments in the file.
+while (<CONFIGFILE>) {
+
+ chomp($_);
+
+ ## eat leading whitespace
+ $_ =~ s/^\s*//;
+
+ ## eat trailing whitespace
+ $_ =~ s/\s*$//;
+
+
+ ## throw away comments
+ next if (($_ =~ /^#/) || ($_ =~ /^;/));
+
+ ## set the current section name for storing the hash
+ if ($_ =~ /^\[.*\]$/) {
+
+ $_ = substr($_, 1, length($_)-2);
+
+ if ( length($_) ) {
+ $section = $_;
+ }
+ else {
+ print "Bad Section Name - no closing ]\n";
+ exit -1;
+ }
+
+ next;
+ }
+
+ ## check for a param = value
+ if ($_ =~ /=/) {
+ ($param, $value) = split (/=/, $_);
+ $param =~ s/./\l$&/g;
+ $param =~ s/\s+//g;
+
+ $config{$section}{$param} = $value;
+
+ next;
+ }
+
+ ## should have a hash of hashes indexed by section name
+}
+
+##
+## We have the smb.conf in our hash of hashes now.
+## Add or delete
+##
+if ($add_mode) {
+ $config{$ARGV[1]}{'path'} = $ARGV[2];
+ $config{$ARGV[1]}{'comment'} = $ARGV[3];
+}
+elsif ($delete_mode) {
+ delete $config{$ARGV[1]};
+}
+
+##
+## Print the resulting configuration
+##
+PrintConfigFile();
+
+exit 0;
+
+
+
+
+
+#######################################################################################
+## PrintConfigFile()
+##
+sub PrintConfigFile {
+
+ ## print the file back out, beginning with the global section
+ print "#\n# Generated by $0\n#\n";
+
+ PrintSection ('global', $config{'global'});
+
+ foreach $section (keys %config) {
+
+ if ("$section" ne "global") {
+ print "## Section - [$section]\n";
+ PrintSection ($section, $config{$section});
+ }
+ }
+
+ print "#\n# end of generated smb.conf\n#\n";
+}
+
+#######################################################################################
+## PrintSection()
+##
+sub PrintSection {
+ my ($name, $section) = @_;
+
+ print "[$name]\n";
+ foreach $param (keys %$section) {
+ print "\t$param".' 'x(25-length($param)). " = $$section{$param}\n";
+ }
+ print "\n";
+
+}