2
#------------------------------------------------------------------------
4
# $Sendmail: update_conf,v 8.14.3 2009-03-01 10:48:34 cowboy Exp $
6
# Parse and update /etc/mail/sendmail.conf and reflect its values in
7
# /etc/cron.d/sendmail and /etc/inetd.conf.
9
# Copyright (c) 2001-2009 Richard Nelson. All Rights Reserved.
11
#------------------------------------------------------------------------
13
use strict; # be kosher
14
use Cwd; # provide cwd()
15
use Env; # A few environmental references
16
use integer; # Peformance
17
use Sys::Hostname; # make sure we have a valid hostname
18
use Getopt::Long; # parameter handling
21
# Local libraries - for Debian Sendmail Perl helper functions
22
# BEGIN { $main::my_path = substr($0,$[,rindex($0,'/')) };
23
use lib ('.', substr($0,$[,rindex($0,'/')), "/usr/share/sendmail");
27
$main::program_name = $0; #'update_conf';
28
$main::program_version = '8.14.3';
29
$main::program_date = '2009-03-01 10:48:34 cowboy';
32
my $interp_pgm = "$^X";
34
$interp_vrm = ("$^V" | '000') if (defined $^V);
35
my $current_time = scalar localtime;
36
my $user = getlogin || (getpwuid($<))[0] || "Unknown!!";
37
my $hostname = hostname();
38
my $directory = getcwd();
40
my $Conffile = "/etc/mail/sendmail.conf";
42
my $ofh = new FileHandle;
46
#------------------------------------------------------------------------------
48
#------------------------------------------------------------------------------
51
#------------------------------------------------------------------------------
52
# Finally, some code (almost)
53
#------------------------------------------------------------------------------
56
# Argument handling...
58
$main::opt_output_file='';
59
$main::opt_input_file='';
65
output-file|output_file|o:s
66
input-file|input_file|i:s
71
my $result = GetOptions(@options);
73
die "Terminating due to parameter error";
75
if ( $main::opt_help ) {
76
warn "$main::program_name $main::program_version $main::program_date\n";
78
warn " -help\n" if $main::opt_help;
79
warn " -debug\n" if $main::opt_debug;
80
warn " -test\n" if $main::opt_debug;
81
warn " -static\n" if $main::opt_static;
82
warn " -o $main::opt_output_file\n" if $main::opt_output_file;
83
warn " -i $main::opt_input_file\n" if $main::opt_input_file;
87
if ( $main::opt_test ) {
94
&sm2cron_time("2w3d4h5m6s");
97
&sm2cron_time("150m");
100
&sm2cron_time("35d");
106
$output_file = $main::opt_output_file if ($main::opt_output_file);
107
my $input_file = $main::opt_input_file || $Conffile;
108
# $main::debug is used in Parse_mc !
109
$main::debug = $main::opt_debug || $main::debug;
111
# Read /etc/mail/sendmail.conf (if extant)
112
&Parse_conf::read_config($Conffile);
114
# [Re]write /etc/mail/sendmail.config
115
&Parse_conf::write_config($Conffile);
117
my ($ok, $value) = &Parse_conf::get_value('HANDS_OFF');
119
unlink "/etc/cron.d/sendmail";
123
# Reflect settings in /etc/cron.d/sendmail
125
if ( $output_file eq '' ) {
126
chown '0', '0', "/etc/cron.d/sendmail";
127
chmod 0644, "/etc/cron.d/sendmail";
130
# Reflect settings in /etc/inetd.conf
133
# Create/Delete files
139
#------------------------------------------------------------------------
140
# Check for nullclient mode in /etc/mail/sendmail.mc
141
#------------------------------------------------------------------------
142
sub check_nullclient {
144
my $in_file = "/etc/mail/sendmail.mc";
147
unless ( open($ifh, "<$in_file") ) {
148
warn("Could not open $in_file($!), ignoring it.\n");
151
line: while (<$ifh>) {
152
next line if /^$/; # skip empty lines
153
next line if /^#/; # skip comments
154
next line if /^dnl /; # skip comments
155
chomp; # drop tailing \n
157
if (/^\s*FEATURE\(\s*`?nullclient/) {
167
#------------------------------------------------------------------------
168
# Compute time setting for Crontab entry (simplistic)
169
# NOTE: It does the basics pretty darned well... *BUT*
170
# It fails, miserably, on things that would multiple lines:
171
# 90 minutes: does it at 30 minutes (90-60)
172
# Or are just edge conditions:
173
# 25 hours: treated as 24 hours
174
# 35 days: does it the 7rd of every month (35-28)
175
#------------------------------------------------------------------------
190
my $randstart = '00';
192
if ( $main::opt_test ) {
193
print "mm hh dom mon dow = sm2cron_time($sm);\n";
195
return ($cron, $seconds);
198
# Convert sendmail time
203
if ( $t =~ /^\d+$/ ) {
206
($week = $sm) =~ s/.*?(\d+)w.*/$1/ if ( $t =~ /w/ );
207
($day = $sm) =~ s/.*?(\d+)d.*/$1/ if ( $t =~ /d/ );
208
($hour = $sm) =~ s/.*?(\d+)h.*/$1/ if ( $t =~ /h/ );
209
($minute = $sm) =~ s/.*?(\d+)m.*/$1/ if ( $t =~ /m/ );
210
($second = $sm) =~ s/.*?(\d+)s.*/$1/ if ( $t =~ /s/ );
212
# Rationalize the time
213
$seconds = ($week * 7 * 24 * 60 * 60)
214
+ ($day * 24 * 60 * 60)
219
$month = $seconds / (4 * 7 * 24 * 60 * 60);
220
$seconds = $seconds % (4 * 7 * 24 * 60 * 60);
221
$week = $seconds / (7 * 24 * 60 * 60);
222
$seconds = $seconds % (7 * 24 * 60 * 60);
223
$day = $seconds / (24 * 60 * 60);
224
$seconds = $seconds % (24 * 60 * 60);
225
$hour = $seconds / (60 * 60);
226
$seconds = $seconds % (60 * 60);
227
$minute = $seconds / (60);
228
$seconds = $seconds % (60);
231
# Cron doesn't do seconds, round to minutes or ignore
232
$minute = $minute + 1 if ($second > 30);
234
# Minute of hour (0-59)
235
$minute = sprintf("%02d", $minute);
236
$randstart = sprintf("%02d", int(rand(60))) if (!$main::opt_static);
237
if (0 < $hour + $day + $week + $month) {
239
$cron = "$randstart "; }
241
$cron = "$minute "; }
243
elsif (1 >= $minute) {
251
$hour = sprintf("%02d", $hour);
252
$randstart = sprintf("%02d", int(rand(24))) if (!$main::opt_static);
253
if (0 < $day + $week + $month) {
255
$cron = "$cron $randstart "; }
257
$cron = "$cron $hour "; }
263
$cron = "$cron */$hour";
266
# Day of month (1-31)
267
$day = $day + (7 * $week);
268
$day = sprintf("%02d", $day);
269
$randstart = 1 + sprintf("%02d", int(rand(31))) if (!$main::opt_static);
272
$cron = "$cron $randstart "; }
274
$cron = "$cron $day "; }
280
$cron = "$cron */$day";
283
# Month in year (1-12)
284
$month = sprintf("%02d", $month);
285
$randstart = 1 + sprintf("%02d", int(rand(12))) if (!$main::opt_static);
290
$cron = "$cron */$month";
293
$cron = "$cron *"; # Day of week (0-7)
295
if ( $main::opt_test ) {
296
print "$cron = sm2cron_time($sm);\n";
298
return ($cron, $elapsed);
302
#------------------------------------------------------------------------
303
# Write updated cron file
304
#------------------------------------------------------------------------
309
my $test = "test -x /etc/init.d/sendmail";
319
($ok, $mailto) = &Parse_conf::get_value('CRON_MAILTO');
324
my $out_file = $output_file || "/etc/cron.d/sendmail";
325
print STDOUT "Writing $out_file.\n";
326
$out_file = '&STDOUT' if ($out_file eq '-');
327
unless ( open($ofh, ">$out_file") ) {
328
warn("Could not open $out_file($!), using STDOUT\n");
329
open($ofh, ">&STDOUT");
331
$out_file = '-' if ($out_file eq '&STDOUT');
334
####################################################################
335
##### This file is automagically generated -- edit at your own risk
337
##### file: ${out_file}
338
##### generated via: (${interp_pgm} ${interp_vrm})
339
##### ${main::program_name}
340
##### version: ${main::program_version} ${main::program_date}
341
##### by: ${user}\@${hostname}
342
##### on: ${current_time}
343
##### in: ${directory}
346
foreach my $file ( split(' ', $input_file) ) {
353
####################################################################
354
#------------------------------------------------------------------------------
358
# Copyright (c) 2001-2009 Richard Nelson. All Rights Reserved.
359
# Version: ${main::program_version}
360
# Time-stamp: <${main::program_date}>
362
# Sendmail crontab - Call sendmail at various times to do the following:
363
# 1) Age queues - move undelivered mail to a slower queue
364
# 2) Retry any mail queued by the message submission process
365
# 3) run the queues (deliver mail) if a standalone daemon is not desired
367
# Each processes is independant and guided by /etc/mail/sendmail.conf and
368
# {sendmail,submit}.mc files.
370
# There isn't anything here that should need touching.
372
# Any requisite queue/misc parameters must be set in /etc/mail/sendmail.conf
373
# and reflected herein via /usr/sbin/sendmailconfig (or more directly via
374
# ${main::program_name}).
376
#------------------------------------------------------------------------------
378
# use default path, shell, home
382
# send mail to this user, as `mail/smmsp` isn't real.
386
# m h dom mon dow user command
388
#------------------------------------------------------------------------------
389
# Every so often, give sendmail a chance to run the MSP queues.
392
($ok, $interval) = &Parse_conf::get_value('MSP_INTERVAL');
393
($cronint, $ok) = &sm2cron_time($interval);
394
$command = "$test && /usr/share/sendmail/sendmail cron-msp";
395
($ok, $mmode) = &Parse_conf::get_value('MSP_MODE');
396
($ok, $qmode) = &Parse_conf::get_value('QUEUE_MODE');
398
and ($interval ne '')
399
#or ($mmode eq 'None' and $qmode eq 'Cron')
402
"$cronint\t\tsmmsp\t$command";
406
"#$cronint\t\tsmmsp\t$command";
408
print $ofh "$msp_line\n";
412
#------------------------------------------------------------------------------
413
# Every so often, give sendmail a chance to run the MTA queues.
414
# Will also run MSP queues if enabled
417
($ok, $interval) = &Parse_conf::get_value('QUEUE_INTERVAL');
418
($cronint, $ok) = &sm2cron_time($interval);
419
$command = "$test && /usr/share/sendmail/sendmail cron-mta";
421
and ($interval ne '')
424
"$cronint\t\troot\t$command";
428
"#$cronint\t\troot\t$command";
430
print $ofh "$mta_line\n";
434
#------------------------------------------------------------------------------
435
# Every so often, give sendmail a chance to age the queues.
438
($ok, $var) = &Parse_conf::get_value('AGE_DATA');
439
my $tmpval = eval $var;
447
print $ofh "# No queue aging\n";
449
elsif (@{$var} == 0) {
450
print $ofh "# No queue aging\n";
453
foreach my $entry (@{$var}) {
454
($interval, $ok) = &sm2cron_time(@$entry[0]);
455
my $criteria = @$entry[1] || join('','-s ',$ok);
457
my $from = @$entry[3];
458
$command = "$test && /usr/share/sendmail/qtool.pl";
459
$from = "/var/spool/mqueue/$from"
461
$to = "/var/spool/mqueue/$to"
464
"$interval\t\troot\t$command $criteria $to $from";
465
print $ofh "$age_line >/dev/null\n";
476
#------------------------------------------------------------------------
477
# Update /etc/inetd.conf file
478
#------------------------------------------------------------------------
481
# Don't try to write if we're debugging
482
if ($output_file ne '') {
486
my ($ok, $mode) = &Parse_conf::get_value('DAEMON_MODE');
487
if ( $ok and -x '/usr/sbin/update-inetd' ) {
488
if ( $mode eq 'Inetd' ) {
489
system 'update-inetd --group MAIL --enable smtp,smtps,submission'
492
system 'update-inetd --group MAIL --disable smtp,smtps,submission'
499
#------------------------------------------------------------------------
500
# Update mail statistics information (create/delete files)
501
#------------------------------------------------------------------------
504
# Don't try to write if we're debugging
505
if ($output_file ne '') {
509
my ($class, $flags, $files, $options);
514
# Read the mc/m4 files
515
&Parse_mc::read_dbs('', '');
517
# Obtain entry for HOST_STATUS_DIRECTORY
518
($class, $flags, $files, $options) =
519
&Parse_mc::entry_dbs('confHOST_STATUS_DIRECTORY');
520
$file = @{$files}[0];
521
($ok, $stats) = &Parse_conf::get_value('DAEMON_HOSTSTATS');
522
if ( $ok and $file ne '-' ) {
523
if ( $stats and ! -d $file) {
524
print STDOUT "Enabling HOST statistics file($file).\n";
525
system "mkdir ${file}";
526
my $gid = getgrnam('smmsp');
527
chown '0', $gid, $file;
530
elsif ( ! $stats and -d $file ) {
531
print STDOUT "Disabling HOST statistics file($file).\n";
532
system "rm -rf ${file}";
536
# Obtain entry for STATUS_FILE
537
($class, $flags, $files, $options) =
538
&Parse_mc::entry_dbs('STATUS_FILE');
539
$file = @{$files}[0];
540
($ok, $stats) = &Parse_conf::get_value('DAEMON_MAILSTATS');
541
if ( $ok and $file ne '-' ) {
542
if ( $stats and ! -e $file) {
543
print STDOUT "Enabling MTA statistics file($file).\n";
544
open 'STATS', ">$file";
546
my $gid = getgrnam('smmsp');
547
chown '0', $gid, $file;
550
elsif ( ! $stats and -e $file ) {
551
print STDOUT "Disabling MTA statistics file($file).\n";
556
# Obtain entry for MSP_STATUS_FILE
557
($class, $flags, $files, $options) =
558
&Parse_mc::entry_dbs('MSP_STATUS_FILE');
559
$file = @{$files}[0];
560
($ok, $stats) = &Parse_conf::get_value('MSP_MAILSTATS');
561
if ( $ok and $file ne '-') {
562
if ( $stats and ! -e $file ) {
563
print STDOUT "Enabling MSP statistics file($file).\n";
564
open 'STATS', ">$file";
566
my $gid = getgrnam('smmsp');
567
chown '0', $gid, $file;
570
elsif ( ! $stats and -e $file ) {
571
print STDOUT "Disabling MSP statistics file($file).\n";