3 # migrate old samba2 smb.conf settings to new samba3 setup
4 # as well as merge local configuration settings
5 # Dec 3 2003 Stew Benedict <sbenedict@mandrakesoft.com>
6 # revised Jan 6 2004 - dropping some parameters
7 # revised Feb 11 2004 - don't try to process a config a second time
9 # check command line arguments
12 print "useage: smb-migrate test|test-commit|commit\n";
16 # define some variables
18 my $new_conf_file = "/etc/samba/smb.conf";
20 my $merged_conf_file = "/etc/samba/smb.conf";
21 my $merge_comment = "# *** merged from original smb.conf: ***\n";
22 my $uncomment_comment = "# *** uncommented from original smb.conf: ***\n";
23 my $unique_comment = "# *** unique added from original smb.conf: ***\n";
25 my $log_file = "/var/log/samba/smb-migrate.log";
26 my $to_merge = "/etc/samba/smb.conf.tomerge";
28 if ($ARGV[0] eq "test" || $ARGV[0] eq "test-commit") {
29 $to_merge = "smb.conf";
30 $log_file = "smb-migrate.log";
31 $merged_conf_file = "smb.conf.merged";
34 # if the file has already been processed, don't do it again
35 my $processed = `grep -c 'original smb.conf: ***' $to_merge`;
37 `cp $to_merge $new_conf_file`;
38 print "Already processed, aborting.\n";
42 # get the stripped, uncommented data from old smb.conf
43 my @old_conf = `grep -v "^#" $to_merge | grep -v "^;" | grep -v "^\$"` or die;
45 # use a clean config file as a starting point
46 `cp /usr/share/samba/smb.conf.clean $new_conf_file` if $ARGV[0] !~ /test/;
48 # and the whole new conf file we're going to merge with
49 my @new_conf_org = `cat $new_conf_file` or die;
51 mlog
("Data to change/add in standard sections of smb.conf:\n\n");
55 if ($ARGV[0] eq "test") {
58 push @merge_log, @dstring;
63 my ($header, $new_value) = @_;
64 my @parmlist = split " = ", $new_value;
67 $comment = $unique_comment if $continuation = 0;
70 # find the header in question
72 foreach (@new_conf_org) {
73 if (/^\[$header\]|;\[$header\]|^; \[$header\]|^\[$header\$\]|;\[$header\$\]|^; \[$header\$\]/) {
75 $header = "print" . '$' if $header eq "print";
76 # if the header is commented, remove the comment
77 if (/^;\[|^#\[|^; \[/) {
79 @new_conf_org[$index] =~ s/^;|^; |^#//g;
80 mlog
("uncomment: $header line $index: $entry -> @new_conf_org[$index]");
81 splice(@new_conf_org, $index, 0, $uncomment_comment);
85 # print "[$header]: $start_loc\n";
91 my $elements = @new_conf_org;
93 # walk through this header's entries, update as needed
94 for ($i = $start_loc + 1; $i < $elements; $i++) {
95 # if we hit a new header, may be commented - bail out
96 my $is_header = @new_conf_org[$i];
97 $is_header =~ s/^ |\t|\n//;
98 if ($is_header =~ /^\[|;\[|#\[/) {
99 # print "new header: $is_header at $i\n";
101 # it's possible the parameter is continued across multiple lines
102 $continuation = 0 if $new_value !~ /\\$/;
103 # completely new entry, try to place it under the correct header
104 # print "new entry for [$header]: $new_value\n";
105 mlog
("unique: $header line $last_index: $new_value");
106 splice(@new_conf_org, $last_index + 1, 0, $comment, $new_value);
107 $last_index++;$last_index++;
112 # some syntax changes
113 if ($new_value =~ /winbind/) {
114 $old_value = $new_value;
115 $new_value =~ s/winbind/idmap/;
116 mlog
("syntax: $header: $old_value -> $new_value");
119 # partial match, decide whether to add or replace
120 if (@new_conf_org[$i] =~ /@parmlist[0]/) {
121 if (@new_conf_org[$i] !~ /^;|^#/) {
122 if (@new_conf_org[$i] ne $_) {
123 mlog
("update: $header line $i: @new_conf_org[$i] -> $new_value");
124 @new_conf_org[$i] = ";" . $new_conf_org[$i];
125 splice(@new_conf_org, $i + 1, 0, $merge_comment, $new_value);
130 # is it really a definition or just a comment?
131 if (@new_conf_org[$i] =~ / = /) {
132 # commented in new config, add the old entry
133 mlog
("add: $header line $i: @new_conf_org[$i] -> $new_value");
134 splice(@new_conf_org, $i + 1, 0, $merge_comment, $new_value);
139 # $match = 1 if $new_value eq @new_conf_org[$i];
141 return if ($match eq 1);
148 foreach (@old_conf) {
149 # check for section headers
152 if (!/^\[global\]|^\[homes\]|^\[netlogon\]|^\[Profiles\]|^\[printers\]|^\[print\$\]|^\[pdf-generator\]/) {
153 # non-standard - add to new config
155 push (@new_conf, $_);
164 # non-standard - add to new config
165 if ($user_parms == 1) {
166 push (@new_conf, $_);
168 # now we're working with standard settings
169 # update new config with values if they differ or are commented out
170 # translate any old nomenclature to the new style
171 # may still be some commented lines buried
172 # throw those out and try to merge into new config
173 if (!/^[ ]+#|^[ ]+;|^#|^;/) {
174 # print "$header: $_\n";
175 merge_conf
($bare_header, $_);
181 # write the user config data to new smb.conf
183 mlog
("\nNew data for smb.conf:\n\n");
186 if ($ARGV[0] eq "commit" || $ARGV[0] eq "test-commit") {
188 open(NEWCONF
, "> $merged_conf_file");
189 print NEWCONF
@new_conf_org;
190 print NEWCONF
@new_conf;
193 open(LOGFILE
, "> $log_file");
194 print LOGFILE
@merge_log;