summaryrefslogtreecommitdiff
path: root/packaging/Mandrake/smb-migrate
blob: 08a5023c135554cd225258fe13aec44a7aab0d77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/usr/bin/perl

# migrate old samba2 smb.conf settings to new samba3 setup
# as well as merge local configuration settings
# Dec 3 2003 Stew Benedict <sbenedict@mandrakesoft.com>
# revised Jan 6 2004 - dropping some parameters
# revised Feb 11 2004 - don't try to process a config a second time

# check command line arguments
my $numargs = @ARGV;
if ($numargs lt 1) {
	print "useage: smb-migrate test|test-commit|commit\n";
	exit(1);
}

# define some variables
my $user_parms = 0;
my $new_conf_file = "/etc/samba/smb.conf";
my @new_conf;
my $merged_conf_file = "/etc/samba/smb.conf";
my $merge_comment = "# *** merged from original smb.conf: ***\n";
my $uncomment_comment = "# *** uncommented from original smb.conf: ***\n";
my $unique_comment = "# *** unique added from original smb.conf: ***\n";
my @merge_log;
my $log_file = "/var/log/samba/smb-migrate.log";
my $to_merge = "/etc/samba/smb.conf.tomerge";

if ($ARGV[0] eq "test" || $ARGV[0] eq "test-commit") {
	$to_merge = "smb.conf"; 
	$log_file = "smb-migrate.log";
	$merged_conf_file = "smb.conf.merged";
}

# if the file has already been processed, don't do it again
my $processed = `grep -c 'original smb.conf: ***' $to_merge`;
if ($processed > 0) {
	`cp $to_merge $new_conf_file`;
	print "Already processed, aborting.\n";	
	exit 0;
}
	
# get the stripped, uncommented data from old smb.conf
my @old_conf = `grep -v "^#" $to_merge | grep -v "^;" | grep -v "^\$"` or die;

# use a clean config file as a starting point
`cp /usr/share/samba/smb.conf.clean $new_conf_file` if $ARGV[0] !~ /test/;

# and the whole new conf file we're going to merge with
my @new_conf_org = `cat $new_conf_file` or die;

mlog("Data to change/add in standard sections of smb.conf:\n\n");

sub mlog {
	my (@dstring) = @_;
	if ($ARGV[0] eq "test") {
		print "@dstring";
	} else {
		push @merge_log, @dstring;
	}
}

sub merge_conf {
	my ($header, $new_value) = @_;
	my @parmlist = split " = ", $new_value;
	my $match = 0;
	my $comment = '';
	$comment = $unique_comment if $continuation = 0;
	$continuation = 1;
	
	# find the header in question
	$index = 0;
	foreach (@new_conf_org) {
 		if (/^\[$header\]|;\[$header\]|^; \[$header\]|^\[$header\$\]|;\[$header\$\]|^; \[$header\$\]/) {
			# restore print$
			$header = "print" . '$' if $header eq "print";
			# if the header is commented, remove the comment
			if (/^;\[|^#\[|^; \[/) {
				my $entry = $_;
				@new_conf_org[$index] =~ s/^;|^; |^#//g;
				mlog("uncomment: $header line $index: $entry -> @new_conf_org[$index]");
				splice(@new_conf_org, $index, 0, $uncomment_comment);
				$index++
			}
			$start_loc = $index;
#			print "[$header]: $start_loc\n";
			last;
		}
		$index++
	}
	
	my $elements = @new_conf_org;

	# walk through this header's entries, update as needed
	for ($i = $start_loc + 1; $i < $elements; $i++) {
		# if we hit a new header, may be commented - bail out
		my $is_header = @new_conf_org[$i];
		$is_header =~ s/^ |\t|\n//;
		if ($is_header =~ /^\[|;\[|#\[/) { 
#			print "new header: $is_header at $i\n";
			if ($match == 0) {
				# it's possible the parameter is continued across multiple lines
				$continuation = 0 if  $new_value !~ /\\$/;
				# completely new entry, try to place it under the correct header
#				print "new entry for [$header]: $new_value\n";
				mlog("unique: $header line $last_index: $new_value");
				splice(@new_conf_org, $last_index + 1, 0, $comment, $new_value);
				$last_index++;$last_index++;
			}
			return;
		}

		# some syntax changes
		if ($new_value =~ /winbind/) {
			$old_value = $new_value; 
			$new_value =~ s/winbind/idmap/;
			mlog("syntax: $header: $old_value -> $new_value");
		}

		# partial match, decide whether to add or replace
		if (@new_conf_org[$i] =~ /@parmlist[0]/) {
			if (@new_conf_org[$i] !~ /^;|^#/) {
				if (@new_conf_org[$i] ne $_) { 
					mlog("update: $header line $i: @new_conf_org[$i] -> $new_value");
					@new_conf_org[$i] = ";" . $new_conf_org[$i];
					splice(@new_conf_org, $i + 1, 0, $merge_comment, $new_value);
#					$match = 1;
				}
				$match = 1;
			} else {
				# is it really a definition or just a comment?
				if (@new_conf_org[$i] =~ / = /) {
					# commented in new config, add the old entry
					mlog("add: $header line $i: @new_conf_org[$i] -> $new_value");
					splice(@new_conf_org, $i + 1, 0, $merge_comment, $new_value);
					$i++;$i++;
					$match = 1;
				}
			}
#			$match = 1 if $new_value eq @new_conf_org[$i];
			$last_index = $i;
			return if ($match eq 1);
			$match = 0;
		}	
	}
	return;
}

foreach (@old_conf) {
	# check for section headers
	if (/^\[/) {
		# standard headers?
		if (!/^\[global\]|^\[homes\]|^\[netlogon\]|^\[Profiles\]|^\[printers\]|^\[print\$\]|^\[pdf-generator\]/) {
			# non-standard - add to new config
			$user_parms = 1;
			push (@new_conf, $_);
		} else {
			$user_parms = 0;
			chop;
			$header = $_;
			s/\[|\]|\$//g;
			$bare_header = $_;
		}
	} else {
		# non-standard - add to new config
		if ($user_parms == 1) {
			push (@new_conf, $_);
		} else {
			# now we're working with standard settings
			# update new config with values if they differ or are commented out
			# translate any old nomenclature to the new style
			# may still be some commented lines buried
			# throw those out and try to merge into new config
			if (!/^[ ]+#|^[ ]+;|^#|^;/) {
#				print "$header: $_\n";
				merge_conf($bare_header, $_);
			}
		}
	}
}

# write the user config data to new smb.conf

mlog("\nNew data for smb.conf:\n\n");
mlog("@new_conf");

if ($ARGV[0] eq "commit" || $ARGV[0] eq "test-commit") {
	local *NEWCONF;
	open(NEWCONF, "> $merged_conf_file");
	print NEWCONF @new_conf_org;
	print NEWCONF @new_conf;
	close NEWCONF;
	local *LOGFILE;
	open(LOGFILE, "> $log_file");
	print LOGFILE @merge_log;
	close LOGFILE
}