This repository was archived by the owner on Feb 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbb-check.pl
More file actions
executable file
·311 lines (254 loc) · 8.59 KB
/
bb-check.pl
File metadata and controls
executable file
·311 lines (254 loc) · 8.59 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#!/usr/bin/perl -w
#
#License GPLv3
# A simple script to analyze postfix configuration files
# 2012 (c) Babel srl
#
# - starts from main.cf and master.cf
# - Verify existing files
# - Verify
#
use strict;
use diagnostics;
use Getopt::Std;
use File::Find;
my $verbose = 0;
sub usage() {
print "usage: $0 options [-c config_dir | -f file]\n"
. "Allowed options:\n"
. "\t-c use the given postfix configuration directory\n"
. "\t-f check the given files\n"
. "\t-v verbose output\n"
. "\t-u normalize the given configuration file (postconf format)\n";
exit(1);
}
# find files referenced in postfix configuration but missing
# in the current directories
sub find_missing_files(@) { #files
print "\nMissing files:\n" . "-------------------------------\n";
# pattern used to match configuration files. May not be exhaustive
our $file_pattern = qq|[ :"'](/[A-z0-9_\-][/A-z0-9_.\-]+[A-z0-9])|;
my @skip_files =
qw|\.\.?$ post-install postfix-script postfix-files LICENSE TLS_LICENSE license prng_exch|;
my $re_skip_files = join( "|", @skip_files );
my $a;
foreach my $file (@_) {
my $errors = 0;
next if ( $file =~ m|$re_skip_files| );
# Skip directories
next if ( -d $file );
open FH, "<", $file;
printf "Scanning file: %-60s...", $file;
while (<FH>) {
next if ( $_ =~ m/^\s*#/ );
#print $_ if ($verbose);
# match every file, even more files on the same line
while ( $_ =~ m|$file_pattern|g ) {
$a = $1;
print "\n\tfile: $a" if ($verbose);
# print unexistent files
if ( not -e $a ) {
$errors++;
printf( "\n\tmissing file: %60s", $a );
next;
}
# scripts must be executable
# print error and continue
# with syntax checking
if ( $a =~ m/.*\.(sh|pl|py|php)$/ and not -x $a ) {
$errors++;
printf( "\n\tscript not executable: %60s", $a );
}
# check perl syntax
if ( $a =~ m/.*\.pl$/ ) {
if ( not `perl -c -- $a` ) {
$errors++;
printf( "\n\tscript with errors: %60s", $a );
}
else {
printf( "\n\tscript syntax ok: %60s", $a );
}
}
next;
}
}
close FH;
printf( "%3s\n", $errors ? "" : "OK" );
}
print "\n\n";
}
# find files unmentioned in postfix configuration
sub find_unused_files(@) { #postfix_dir, list of files to check
my $postfix_dir = shift;
print "\nUnused files:\n" . "-------------------------------\n";
my @skip_files =
qw|\.\.?$ main.cf master.cf post-install postfix-script postfix-files LICENSE TLS_LICENSE license prng_exch|;
my $re_skip_files = join( "|", @skip_files );
foreach my $file (@_) {
next if ( $file =~ m/$re_skip_files/ );
next if ( -d $file );
$file =~ s|/+|/|g;
grep { !/.svn/ } `grep -ril -- "$file" "$postfix_dir" `
or print("unused file: $file\n");
}
}
# check for possible typos in main.cf and master.cf
sub check_typos(@) { #list of files to check
my $spaces_before_vars = qq/^\\s+[A-z0-9]+=/;
my $trailing_comment = qq/^\\s*[A-z].*\\s*#/;
my @files_to_check = qw|main.cf master.cf|;
print "\nTypos in configuration files:\n"
. "-------------------------------\n";
foreach my $file (@_) {
next if ( $file =~ m|/\.\.?$| );
next unless ( $file =~ m!join("|", @files_to_check)! );
my ( $nl, $lno, $err ) = qw(0 1 "");
open FH, $file or die("cannot open file $file");
while (<FH>) {
$err = "space before vars" if ( $_ =~ m/$spaces_before_vars/ );
$err = "trailing comment" if ( $_ =~ m/$trailing_comment/ );
if ( $err ne "" ) {
print "$file:$lno: $_";
$nl++;
}
}
continue {
$lno++;
$err = "";
}
close(FH);
print "file: $file \t\tko$nl lines with syntax errors\n" if ($nl);
print "file: $file \t\tok\n" if ( !$nl );
}
print "\n";
}
#
# Checks main.cf and master.cf for sasl
#
sub check_sasl(@) { # files
my $re_sasl_client = qq/^[^#]+smtp_sasl_auth_enable\\s\*=\\s\*yes/;
my $re_sasl_server = qq/^[^#]+smtpd_sasl_auth_enable\\s\*=\\s\*yes/;
my $re_sasl_client_type = qq/^[^#]+smtp_sasl_type/;
my $re_sasl_server_type = qq/^[^#]+smtpd_sasl_type/;
my @sasl_type_client = ();
my @sasl_type_server = ();
my ( $sasl_client, $sasl_server ) = ( 0, 0 );
print "\nSASL support:\n" . "-------------------------------\n";
foreach my $f (@_) {
# check only main|master .cf
next unless ( "$f" =~ m/main.cf|master.cf/ );
open( FH, "<", $f ) or die("cannot open $f");
while (<FH>) {
$sasl_client = 1 if (m/$re_sasl_client/);
$sasl_server = 1 if (m/$re_sasl_server/);
push( @sasl_type_client, $1 )
if (m/$re_sasl_client_type\s*=\s*(.+)/);
push( @sasl_type_client, $1 )
if (m/$re_sasl_server_type\s*=\s*(.+)/);
}
close FH;
}
printf "\tSASL client enabled\n" if ($sasl_client);
printf "\tSASL server enabled\n" if ($sasl_server);
#
# Check client sasl support
#
my @postconf_client = `postconf -A`;
foreach my $a (@sasl_type_client) {
next if ( $a =~ m/join("|",@postconf_client)/ );
printf "\tunsupported SASL client type: $a\n";
}
#
# Check server sasl support
#
my @postconf_server = `postconf -a`;
foreach my $a (@sasl_type_server) {
next if ( $a =~ m/join("|",@postconf_server)/ );
printf "\tunsupported SASL client type: $a\n";
}
#
# Check SASL library
#
my @sasl_libs = ();
my $rpm = "/bin/rpm";
my $dpkg = "/usr/bin/dpkg";
if ( -e $rpm ) {
@sasl_libs = `$rpm -qa \*sasl\*;`;
}
elsif ( -e $dpkg ) {
foreach my $lib (`$dpkg -l \*sasl\*;`) {
next unless ( $lib =~ m/^ii\s+([^ ]+)/ );
push( @sasl_libs, $1 );
}
}
if ( @sasl_libs > 0 ) {
printf "\tSASL packages: %s\n", join( ",", @sasl_libs );
printf "\tCAVEAT: YOUR POSTFIX MAY NEED OTHER PACKAGES!\n"
. "\t\tIf you have issues with SASL please check you have \n"
. "\t\tall the required .so libraries\n";
}
else {
printf "\tcannot determine SASL packages\n";
}
print "\n";
}
#
# Uncomment and normalize main.cf
#
sub normalize_cf($) { #file to normalize
my $re_blank_comments = qq/^\\s*(#|\$)/;
my ($file) = @_;
open( FH, "<", $file ) or die("cannot open file: $file.");
# strip blank and comments
my @ret = grep { !/$re_blank_comments/ } <FH>;
close(FH);
#
# Join lines starting with blanks with the previous one
#
my $p = "";
my $c = "";
foreach my $line (@ret) {
foreach my $c ( split //, $line ) {
next if ( $c eq "\n" );
print $p if ( ( $c !~ m/\s+/ ) and ( $p eq "\n" ) );
# replace multiple space with a single " "
next if ( $c =~ '\s' and $p =~ '\s' );
print( $c =~ '\s' ? " " : $c );
}
continue { $p = $c; }
}
}
sub main {
my $postfix_dir = "/etc/postfix";
my @files = ();
my %options = ();
getopts( "vu:c:f:", \%options );
usage() if not scalar( keys(%options) );
$verbose = ( defined $options{'v'} );
$postfix_dir = $options{'c'} if ( $options{'c'} );
@files = ( $options{'f'} ) if ( $options{'f'} );
if ( defined $options{'u'} ) {
normalize_cf( $options{'u'} );
exit(0);
}
if ( $#files < 0 ) {
opendir( DIR, $postfix_dir )
or die("missing directory $postfix_dir.");
foreach my $f ( readdir(DIR) ) {
# strip double slashes
my $s = "$postfix_dir/$f";
$s =~ s|/+|/|g;
push( @files, $s );
}
closedir(DIR);
}
die("no files to analyze. Check your configuration directory!")
if ( $#files <= 0 );
print "Postfix Configuration Checker: directory $postfix_dir\n\n";
check_sasl(@files);
find_missing_files(@files);
find_unused_files( $postfix_dir, @files );
check_typos(@files);
exit 0;
}
&main;