blib/lib/Mail/Toaster/Setup/Config.pm untested

filter: Statementstmt 0.0% 0 / 226 Branchbran 0.0% 0 / 90 Conditioncond 0.0% 0 / 65 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 24 Podpod 0.0% 0 / 17 totaltotal 0.0% 0 / 422 SCAR 76.8
CC
46
Cov
0%
tweaks
182.0
apply_tweaks
110.0
openssl
30.0
CRAP 2162.0
1 package Mail::Toaster::Setup::Config;
2 use strict;
3 use warnings;
4  
5 our $VERSION = '5.50';
6  
7 #use Carp;
8 #use Config;
9 #use Cwd;
10 #use Data::Dumper;
11 #use File::Copy;
12 #use File::Path;
13 use English '-no_match_vars';
14 use Params::Validate ':all';
15 use Sys::Hostname;
16  
17 use lib 'lib';
18 use parent 'Mail::Toaster::Base';
19  
20 sub config {
21 my $self = shift;
22 my %p = validate( @_, { $self->get_std_opts } );
23  
24 return $p{test_ok} if defined $p{test_ok}; # for testing only
25  
26 # apply the platform specific changes to the config file
27 $self->tweaks();
28  
29 my $file_name = "toaster-watcher.conf";
30 my $file_path = $self->util->find_config( $file_name );
31 $self->setup->refresh_config( $file_path ) or return;
32  
33 ### start questions
34 $self->config_hostname();
35 $self->postmaster();
36 $self->test_email();
37 $self->test_email_pass();
38 $self->vpopmail_mysql_pass();
39 $self->openssl();
40 $self->webmail_passwords();
41 ### end questions
42 ### don't forget to add changed fields to the list in save_changes
43  
44 $self->save_changes( $file_path );
45 $self->install($file_name, $file_path );
46 };
47  
48 sub apply_tweaks {
49 my $self = shift;
50 my %p = validate( @_,
51 { file => { type => SCALAR },
52 changes => { type => ARRAYREF },
53 $self->get_std_opts
54 },
55 );
56  
57 # changes is a list (array) of changes to apply to a text file
58 # each change is a hash with two elements: search, replace. the contents of
59 # file is searched for lines that matches search. Matches are replaced by the
60 # replace string. If search2 is also supplied and search does not match,
61 # search2 will be replaced.
62 # Ex:
63 # $changes = (
64 # { search => '#ssl_cert = /etc/ssl/certs/server.pem',
65 # replace => 'ssl_cert = /etc/ssl/certs/server.pem',
66 # },
67 # );
68 #
69 # read in file
70 my @lines = $self->util->file_read( $p{file} ) or return;
71  
72 my $total_found = 0;
73 foreach my $e ( @{ $p{changes} } ) {
74 my $search = $e->{search} or next;
75 my $replace = $e->{replace} or next;
76 my $search2 = $e->{search2};
77 my $found = 0;
78  
79 if ( $search2 && $search2 eq 'section' ) {
80 # look for a multiline pattern such as: protocol manageseive { .... }
81 my (@after, $in);
82 foreach my $line ( @lines ) {
83 if ( $in ) {
84 next if $line !~ /^ \s* \} \s* $/xms;
85 $in = 0;
86 next;
87 }
88 if ( $search eq $line ) {
89 $found++;
90 $in++;
91 next;
92 };
93 push @after, $line if ! $in;
94 };
95 @lines = @after;
96 };
97 # search entire file for $search string
98 for ( my $i = 0; $i < scalar @lines; $i++ ) {
99 if ( $lines[$i] eq $search ) {
100 $lines[$i] = $replace;
101 $found++;
102 };
103 }
104 # search entire file for $search2 string
105 if ( ! $found && $search2 ) {
106 for ( my $i = 0; $i < scalar @lines; $i++ ) {
107 if ( $lines[$i] eq $search2 ) {
108 $lines[$i] = $replace;
109 $found++;
110 };
111 }
112 };
113 $self->error( "attempt to replace\n$search\n\twith\n$replace\n\tfailed",
114 frames => 1,
115 fatal => 0) if ( ! $found && ! $e->{nowarn} );
116 $total_found += $found;
117 };
118  
119 $self->audit( "config tweaks replaced $total_found lines",verbose=>$p{verbose} );
120  
121 $self->util->file_write( $p{file}, lines => \@lines );
122 };
123  
124 sub config_hostname {
125 my $self = shift;
126  
127 return if ( $self->conf->{'toaster_hostname'} && $self->conf->{'toaster_hostname'} ne "mail.example.com" );
128  
129 $self->conf->{'toaster_hostname'} = $self->util->ask(
130 "the hostname of this mail server",
131 default => hostname,
132 );
133 chomp $self->conf->{'toaster_hostname'};
134  
135 $self->audit( "toaster hostname set to " . $self->conf->{'toaster_hostname'} );
136 };
137  
138 sub install {
139 my $self = shift;
140 my ($file_name, $file_path) = @_;
141  
142 # install $file_path in $prefix/etc/toaster-watcher.conf if it doesn't exist
143 # already
144 my $config_dir = $self->conf->{'system_config_dir'} || '/usr/local/etc';
145  
146 # if $config_dir is missing, create it
147 $self->util->mkdir_system( dir => $config_dir ) if ! -e $config_dir;
148  
149 my @configs = (
150 { newfile => $file_path, existing => "$config_dir/$file_name", mode => '0640', overwrite => 0 },
151 { newfile => $file_path, existing => "$config_dir/$file_name-dist", mode => '0640', overwrite => 1 },
152 { newfile => 'toaster.conf-dist', existing => "$config_dir/toaster.conf", mode => '0644', overwrite => 0 },
153 { newfile => 'toaster.conf-dist', existing => "$config_dir/toaster.conf-dist", mode => '0644', overwrite => 1 },
154 );
155  
156 foreach ( @configs ) {
157 next if -e $_->{existing} && ! $_->{overwrite};
158 $self->util->install_if_changed(
159 newfile => $_->{newfile},
160 existing => $_->{existing},
161 mode => $_->{mode},
162 clean => 0,
163 notify => 1,
164 verbose => 0,
165 );
166 };
167 }
168  
169 sub openssl {
170 my $self = shift;
171 # OpenSSL certificate settings
172  
173 # country
174 if ( $self->conf->{'ssl_country'} eq "SU" ) {
175 print " SSL certificate defaults\n";
176 $self->conf->{'ssl_country'} =
177 uc( $self->util->ask( "your 2 digit country code (US)", default => "US" )
178 );
179 }
180 $self->audit( "config: ssl_country, (" . $self->conf->{'ssl_country'} . ")" );
181  
182 # state
183 if ( $self->conf->{'ssl_state'} eq "saxeT" ) {
184 $self->conf->{'ssl_state'} =
185 $self->util->ask( "the name (non abbreviated) of your state" );
186 }
187 $self->audit( "config: ssl_state, (" . $self->conf->{'ssl_state'} . ")" );
188  
189 # locality (city)
190 if ( $self->conf->{'ssl_locality'} eq "dnalraG" ) {
191 $self->conf->{'ssl_locality'} =
192 $self->util->ask( "the name of your locality/city" );
193 }
194 $self->audit( "config: ssl_locality, (" . $self->conf->{'ssl_locality'} . ")" );
195  
196 # organization
197 if ( $self->conf->{'ssl_organization'} eq "moc.elpmaxE" ) {
198 $self->conf->{'ssl_organization'} = $self->util->ask( "the name of your organization" );
199 }
200 $self->audit( "config: ssl_organization, (" . $self->conf->{'ssl_organization'} . ")" );
201 };
202  
203 sub webmail_passwords {
204 my $self = shift;
205  
206 if ( $self->conf->{install_squirrelmail} &&
207 $self->conf->{install_squirrelmail_sql} &&
208 $self->conf->{install_squirrelmail_sql_pass} eq 'chAnge7his' ) {
209 $self->conf->{install_squirrelmail_sql_pass} =
210 $self->util->ask("squirrelmail database password");
211 };
212  
213 if ( $self->conf->{install_roundcube} &&
214 $self->conf->{install_roundcube_db_pass} eq 'To4st3dR0ndc@be' ) {
215 $self->conf->{install_roundcube_db_pass} =
216 $self->util->ask("roundcube database password");
217 };
218  
219 if ( $self->conf->{install_spamassassin} &&
220 $self->conf->{install_spamassassin_sql} &&
221 $self->conf->{install_spamassassin_dbpass} eq 'assSPAMing' ) {
222 $self->conf->{install_spamassassin_dbpass} =
223 $self->util->ask("spamassassin database password");
224 };
225  
226 if ( $self->conf->{install_phpmyadmin} &&
227 $self->conf->{phpMyAdmin_controlpassword} eq 'pmapass') {
228 $self->conf->{phpMyAdmin_controlpassword} =
229 $self->util->ask("phpMyAdmin control password");
230 };
231  
232 return 1;
233 };
234  
235 sub postmaster {
236 my $self = shift;
237  
238 return if ( $self->conf->{'toaster_admin_email'} && $self->conf->{'toaster_admin_email'} ne "postmaster\@example.com" );
239  
240 $self->conf->{'toaster_admin_email'} = $self->util->ask(
241 "the email address for administrative emails and notices\n".
242 " (probably yours!)",
243 default => "postmaster",
244 ) || 'root';
245  
246 $self->audit(
247 "toaster admin emails sent to " . $self->conf->{'toaster_admin_email'} );
248 };
249  
250 sub save_changes {
251 my $self = shift;
252 my ($file_path) = @_;
253  
254 my @fields = qw/ toaster_hostname toaster_admin_email toaster_test_email
255 toaster_test_email_pass vpopmail_mysql_pass ssl_country ssl_state
256 ssl_locality ssl_organization install_squirrelmail_sql_pass
257 install_roundcube_db_pass install_spamassassin_dbpass
258 phpMyAdmin_controlpassword
259 /;
260 push @fields, 'vpopmail_mysql_pass' if $self->conf->{'vpopmail_mysql'};
261  
262 my @lines = $self->util->file_read( $file_path, verbose => 0 );
263 foreach my $key ( @fields ) {
264 foreach my $line (@lines) {
265 if ( $line =~ /^$key\s*=/ ) {
266 # format the config entries to match config file format
267 $line = sprintf( '%-34s = %s', $key, $self->conf->{$key} );
268 }
269 };
270 };
271  
272 $self->util->file_write( "/tmp/toaster-watcher.conf", lines => \@lines );
273  
274 my $r = $self->util->install_if_changed(
275 newfile => "/tmp/toaster-watcher.conf",
276 existing => $file_path,
277 mode => '0640',
278 clean => 1,
279 notify => -e $file_path ? 1 : 0,
280 )
281 or return $self->error( "installing /tmp/toaster-watcher.conf to $file_path failed!" );
282  
283 my $status = $r == 1 ? "ok" : "ok (current)";
284 $self->audit( "config: updating $file_path, $status" );
285 return $r;
286 };
287  
288 sub test_email {
289 my $self = shift;
290  
291 return if $self->conf->{'toaster_test_email'} ne "test\@example.com";
292  
293 $self->conf->{'toaster_test_email'} = $self->util->ask(
294 "an email account for running tests",
295 default => "postmaster\@" . $self->conf->{'toaster_hostname'}
296 );
297  
298 $self->audit( "toaster test account set to ".$self->conf->{'toaster_test_email'} );
299 };
300  
301 sub test_email_pass {
302 my $self = shift;
303  
304 return if ( $self->conf->{'toaster_test_email_pass'} && $self->conf->{'toaster_test_email_pass'} ne "cHanGeMe" );
305  
306 $self->conf->{'toaster_test_email_pass'} = $self->util->ask( "the test email account password" );
307  
308 $self->audit(
309 "toaster test password set to ".$self->conf->{'toaster_test_email_pass'} );
310 };
311  
312 sub tweaks {
313 my $self = shift;
314 my %p = validate( @_, { $self->get_std_opts } );
315  
316 my $status = "ok";
317  
318 my $file = $self->util->find_config( 'toaster-watcher.conf' );
319  
320 # verify that find_config worked and $file is readable
321 return $self->error( "tweaks: read test on $file, FAILED",
322 fatal => $p{fatal} ) if ! -r $file;
323  
324 my %changes;
325 %changes = $self->config_tweaks_freebsd() if $OSNAME eq 'freebsd';
326 %changes = $self->config_tweaks_darwin() if $OSNAME eq 'darwin';
327 %changes = $self->config_tweaks_linux() if $OSNAME eq 'linux';
328  
329 %changes = $self->config_tweaks_testing(%changes);
330 %changes = $self->config_tweaks_mysql(%changes);
331  
332 # foreach key of %changes, apply to $conf
333 my @lines = $self->util->file_read( $file );
334 foreach my $line (@lines) {
335 next if $line =~ /^#/; # comment lines
336 next if $line !~ /=/; # not a key = value
337  
338 my ( $key, $val ) = $self->util->parse_line( $line, strip => 0 );
339  
340 if ( defined $changes{$key} && $changes{$key} ne $val ) {
341 $status = "changed";
342 #print "\t setting $key to ". $changes{$key} . "\n";
343 $line = sprintf( '%-34s = %s', $key, $changes{$key} );
344 print "\t$line\n";
345 }
346 }
347 return 1 unless ( $status && $status eq "changed" );
348  
349 # ask the user for permission to install
350 return 1
351 if ! $self->util->yes_or_no(
352 'config tweaks: The changes shown above are recommended for use on your system.
353 May I apply the changes for you?',
354 timeout => 30,
355 );
356  
357 # write $conf to temp file
358 $self->util->file_write( "/tmp/toaster-watcher.conf", lines => \@lines );
359  
360 # if the file ends with -dist, then save it back with out the -dist suffix
361 # the find_config sub will automatically prefer the newer non-suffixed one
362 if ( $file =~ m/(.*)-dist\z/ ) {
363 $file = $1;
364 };
365  
366 # update the file if there are changes
367 my $r = $self->util->install_if_changed(
368 newfile => "/tmp/toaster-watcher.conf",
369 existing => $file,
370 clean => 1,
371 notify => 0,
372 verbose => 0,
373 );
374  
375 return 0 unless $r;
376 $r == 1 ? $r = "ok" : $r = "ok (current)";
377 $self->audit( "config tweaks: updated $file, $r" );
378 }
379  
380 sub config_tweaks_darwin {
381 my $self = shift;
382  
383 $self->audit( "config tweaks: applying Darwin tweaks" );
384  
385 return (
386 toaster_http_base => '/Library/WebServer',
387 toaster_http_docs => '/Library/WebServer/Documents',
388 toaster_cgi_bin => '/Library/WebServer/CGI-Executables',
389 toaster_prefix => '/opt/local',
390 toaster_src_dir => '/opt/local/src',
391 system_config_dir => '/opt/local/etc',
392 vpopmail_valias => '0',
393 install_mysql => '0 # 0, 1, 2, 3, 40, 41, 5',
394 install_portupgrade => '0',
395 filtering_maildrop_filter_file => '/opt/local/etc/mail/mailfilter',
396 qmail_mysql_include => '/opt/local/lib/mysql/libmysqlclient.a',
397 vpopmail_home_dir => '/opt/local/vpopmail',
398 vpopmail_mysql => '0',
399 smtpd_use_mysql_relay_table => '0',
400 qmailadmin_spam_command => '| /opt/local/bin/maildrop /opt/local/etc/mail/mailfilter',
401 qmailadmin_http_images => '/Library/WebServer/Documents/images',
402 );
403 };
404  
405 sub config_tweaks_freebsd {
406 my $self = shift;
407 $self->audit( "config tweaks: applying FreeBSD tweaks" );
408  
409 return (
410 install_squirrelmail => 'port # 0, ver, port',
411 install_autorespond => 'port # 0, ver, port',
412 install_ezmlm => 'port # 0, ver, port',
413 install_courier_imap => '0 # 0, ver, port',
414 install_dovecot => 'port # 0, ver, port',
415 install_clamav => 'port # 0, ver, port',
416 install_ripmime => 'port # 0, ver, port',
417 install_cronolog => 'port # ver, port',
418 install_daemontools => 'port # ver, port',
419 install_qmailadmin => 'port # 0, ver, port',
420 install_djbdns => 'port # ver, port',
421 )
422 }
423  
424 sub config_tweaks_linux {
425 my $self = shift;
426 $self->audit( "config tweaks: applying Linux tweaks " );
427  
428 return (
429 toaster_http_base => '/var/www',
430 toaster_http_docs => '/var/www',
431 toaster_cgi_bin => '/usr/lib/cgi-bin',
432 vpopmail_valias => '0',
433 install_mysql => '0 # 0, 1, 2, 3, 40, 41, 5',
434 vpopmail_mysql => '0',
435 smtpd_use_mysql_relay_table => '0',
436 qmailadmin_http_images => '/var/www/images',
437 install_dovecot => '1.0.2',
438 )
439 }
440  
441 sub config_tweaks_mysql {
442 my ($self, %changes) = @_;
443  
444 return %changes if $self->conf->{install_mysql};
445 return %changes if ! $self->util->yes_or_no("Enable MySQL support?");
446  
447 $self->audit( "config tweaks: applying MT testing tweaks" );
448  
449 $changes{'install_mysql'} = '55 # 0, 1, 2, 3, 40, 41, 5, 55';
450 $changes{'install_mysqld'} = '1 # 0, 1';
451 $changes{'vpopmail_mysql'} = '1 # enables all mysql options';
452 $changes{'smtpd_use_mysql_relay_table'} = 0;
453 $changes{'install_squirrelmail_sql'} = 1;
454 $changes{'install_spamassassin_sql'} = 1;
455  
456 return %changes;
457 };
458  
459 sub config_tweaks_testing {
460 my ($self, %changes) = @_;
461  
462 my $hostname = hostname;
463 return %changes if ( ! $hostname || $hostname ne 'jail.simerson.net' );
464  
465 $self->audit( "config tweaks: applying MT testing tweaks" );
466  
467 $changes{'toaster_hostname'} = 'jail.simerson.net';
468 $changes{'toaster_admin_email'} = 'postmaster@jail.simerson.net';
469 $changes{'toaster_test_email'} = 'test@jail.simerson.net';
470 $changes{'toaster_test_email_pass'} = 'sdfsdf';
471 $changes{'install_squirrelmail_sql'} = '1';
472 $changes{'install_phpmyadmin'} = '1';
473 $changes{'install_sqwebmail'} = 'port';
474 $changes{'install_vqadmin'} = 'port';
475 $changes{'install_openldap_client'} = '1';
476 $changes{'install_ezmlm_cgi'} = '1';
477 $changes{'install_dspam'} = '1';
478 $changes{'install_pyzor'} = '1';
479 $changes{'install_bogofilter'} = '1';
480 $changes{'install_dcc'} = '1';
481 $changes{'install_lighttpd'} = '1';
482 $changes{'install_courier_imap'} = 'port';
483 $changes{'install_gnupg'} = 'port';
484 $changes{'vpopmail_default_domain'} = 'jail.simerson.net';
485 $changes{'pop3_ssl_daemon'} = 'qpop3d';
486 $changes{'install_spamassassin_flags'}= '-v -u spamd -q -A 10.0.1.67 -H /var/spool/spamd -x';
487 $changes{'install_isoqlog'} = 'port # 0, ver, port';
488  
489 return %changes;
490 }
491  
492 sub vpopmail_mysql_pass {
493 my $self = shift;
494  
495 return if ! $self->conf->{'vpopmail_mysql'};
496 return if ( $self->conf->{'vpopmail_mysql_pass'}
497 && $self->conf->{'vpopmail_mysql_pass'} ne "supersecretword" );
498  
499 $self->conf->{'vpopmail_mysql_pass'} =
500 $self->util->ask( "the password for securing vpopmails "
501 . "database connection. You MUST enter a password here!",
502 );
503  
504 $self->audit( "vpopmail MySQL password set to ".$self->conf->{'vpopmail_mysql_pass'});
505 }
506  
507 1;
508