| 1 |
#!/usr/bin/perl -Tw |
| 2 |
# High performance pre-forking qpsmtpd daemon, Copyright (C) 2006 SoftScan |
| 3 |
# http://www.softscan.co.uk |
| 4 |
# |
| 5 |
# Based on qpsmtpd-forkserver Copyright (C) 2001 Ask Bjoern Hansen |
| 6 |
# See the LICENSE file for details. |
| 7 |
# |
| 8 |
# For more information see http://smtpd.github.io/qpsmtpd/ |
| 9 |
|
| 10 |
# safety guards |
| 11 |
use strict; |
| 12 |
|
| 13 |
BEGIN { |
| 14 |
# secure shell |
| 15 |
$ENV{'PATH'} = '/bin:/usr/bin'; |
| 16 |
delete @ENV{qw(IFS CDPATH ENV BASH_ENV)}; |
| 17 |
} |
| 18 |
|
| 19 |
# includes |
| 20 |
use IO::Socket; |
| 21 |
use IO::Select; |
| 22 |
use POSIX; |
| 23 |
use IPC::Shareable(':all'); |
| 24 |
use lib 'lib'; |
| 25 |
use Qpsmtpd::TcpServer::Prefork; |
| 26 |
use Qpsmtpd::Constants; |
| 27 |
use Getopt::Long; |
| 28 |
|
| 29 |
use Config; |
| 30 |
defined $Config{sig_name} || die "No signals?"; |
| 31 |
|
| 32 |
my $has_ipv6 = Qpsmtpd::TcpServer::has_ipv6; |
| 33 |
|
| 34 |
#use Time::HiRes qw(gettimeofday tv_interval); |
| 35 |
|
| 36 |
#get available signals |
| 37 |
my %sig_num; |
| 38 |
my $i = 0; |
| 39 |
foreach my $sig_name (split(/\s/, $Config{sig_name})) { |
| 40 |
$sig_num{$sig_name} = $i++; |
| 41 |
} |
| 42 |
|
| 43 |
# version |
| 44 |
my $VERSION = "1.0"; |
| 45 |
|
| 46 |
# qpsmtpd instances |
| 47 |
my ($qpsmtpd); |
| 48 |
|
| 49 |
# cmd's needed by IPC |
| 50 |
my $ipcrm = '/usr/bin/ipcrm'; |
| 51 |
my $ipcs = '/usr/bin/ipcs'; |
| 52 |
my $xargs = '/usr/bin/xargs'; |
| 53 |
|
| 54 |
# vars we need |
| 55 |
my $chld_shmem; # shared mem to keep track of children (and their connections) |
| 56 |
my %children; |
| 57 |
my $chld_pool; |
| 58 |
my $chld_busy; |
| 59 |
my @children_term; # terminated children, their death pending processing |
| 60 |
# by the main loop |
| 61 |
my $select = new IO::Select; # socket(s) |
| 62 |
|
| 63 |
# default settings |
| 64 |
my $pid_file; |
| 65 |
my $d_port = 25; |
| 66 |
my @d_addr; # default applied after getopt call |
| 67 |
|
| 68 |
my $debug = 0; |
| 69 |
my $max_children = 15; # max number of child processes to spawn |
| 70 |
my $idle_children = 5; # number of idle child processes to spawn |
| 71 |
my $maxconnip = 10; |
| 72 |
my $child_lifetime = 100; # number of times a child may be reused |
| 73 |
my $loop_sleep = 15; # seconds main_loop sleeps before checking children |
| 74 |
my $re_nice = 5; # substracted from parents current nice level |
| 75 |
my $d_start = 0; |
| 76 |
my $quiet = 0; |
| 77 |
my $status = 0; |
| 78 |
my $signal = ''; |
| 79 |
my $pretty = 0; |
| 80 |
my $detach = 0; |
| 81 |
my $user; |
| 82 |
|
| 83 |
# help text |
| 84 |
sub usage { |
| 85 |
print <<"EOT"; |
| 86 |
Usage: qpsmtpd-prefork [ options ] |
| 87 |
--quiet : Be quiet (even errors are suppressed) |
| 88 |
--version : Show version information |
| 89 |
--debug : Enable debug output |
| 90 |
--listen-address addr: Listen for connections on the address 'addr' (either |
| 91 |
an IP address or ip:port pair). Listens on all |
| 92 |
interfaces by default; may be specified multiple |
| 93 |
times. |
| 94 |
--port int : TCP port daemon should listen on (default: $d_port) |
| 95 |
--max-from-ip int : Limit number of connections from single IP (default: $maxconnip, 0 to disable) |
| 96 |
--children int : Max number of children that can be spawned (default: $max_children) |
| 97 |
--idle-children int : Number of idle children to spawn (default: $idle_children, 0 to disable) |
| 98 |
--pretty-child : Change child process name (default: 0) |
| 99 |
--user username : User the daemon should run as |
| 100 |
--pid-file path : Path to pid file |
| 101 |
--renice-parent int : Subtract value from parent process nice level (default: $re_nice) |
| 102 |
--detach : detach from controlling terminal (daemonize) |
| 103 |
--help : This message |
| 104 |
EOT |
| 105 |
exit 0; |
| 106 |
} |
| 107 |
|
| 108 |
# get arguments |
| 109 |
GetOptions( |
| 110 |
'quiet' => \$quiet, |
| 111 |
'version' => sub { print "Qpsmtpd Daemon - version $VERSION\n"; exit 0; }, |
| 112 |
'debug' => \$debug, |
| 113 |
'interface|listen-address=s' => \@d_addr, |
| 114 |
'port=i' => \$d_port, |
| 115 |
'max-from-ip=i' => \$maxconnip, |
| 116 |
'children=i' => \$max_children, |
| 117 |
'idle-children=i' => \$idle_children, |
| 118 |
'pretty-child' => \$pretty, |
| 119 |
'user=s' => \$user, |
| 120 |
'renice-parent=i' => \$re_nice, |
| 121 |
'detach' => \$detach, |
| 122 |
'pid-file=s' => \$pid_file, |
| 123 |
'help' => \&usage, |
| 124 |
) |
| 125 |
|| &usage; |
| 126 |
|
| 127 |
if ($user && $user =~ /^([\w\-]+)$/) { $user = $1 } |
| 128 |
else { &usage } |
| 129 |
|
| 130 |
if (@d_addr) { |
| 131 |
for my $i (0 .. $#d_addr) { |
| 132 |
if ($d_addr[$i] =~ /^(\[.*\]|[\d\w\-.]+)(?::(\d+))?$/) { |
| 133 |
$d_addr[$i] = {'addr' => $1, 'port' => $2 || $d_port}; |
| 134 |
} |
| 135 |
else { |
| 136 |
print STDERR "Malformed listen address '$d_addr[$i]'\n"; |
| 137 |
&usage; |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
else { |
| 142 |
@d_addr = ({addr => $has_ipv6 ? "[::]" : "0.0.0.0", port => $d_port}); |
| 143 |
} |
| 144 |
|
| 145 |
# set max from ip to max number of children if option is set to disabled |
| 146 |
$maxconnip = $max_children if ($maxconnip == 0); |
| 147 |
|
| 148 |
#to fix limit counter error in plugin <hosts_allow> |
| 149 |
$maxconnip++; |
| 150 |
|
| 151 |
#ensure that idle_children matches value given to max_children |
| 152 |
$idle_children = $max_children |
| 153 |
if (!$idle_children || $idle_children > $max_children || $idle_children < -1); |
| 154 |
$chld_pool = $idle_children; |
| 155 |
|
| 156 |
if ($pid_file) { |
| 157 |
if ($pid_file =~ m#^(/[\w\d/\-.]+)$#) { $pid_file = $1 } |
| 158 |
else { &usage } |
| 159 |
if (-e $pid_file) { |
| 160 |
open PID, "+<$pid_file" |
| 161 |
or die "open pid_file: $!\n"; |
| 162 |
my $running_pid = <PID> || ''; |
| 163 |
chomp $running_pid; |
| 164 |
if ($running_pid =~ /(\d+)/) { |
| 165 |
$running_pid = $1; |
| 166 |
die "Found an already running qpsmtpd with pid $running_pid.\n" |
| 167 |
if (kill 0, $running_pid); |
| 168 |
} |
| 169 |
seek PID, 0, 0 |
| 170 |
or die "Could not seek back to beginning of $pid_file: $!\n"; |
| 171 |
truncate PID, 0 |
| 172 |
or die "Could not truncate $pid_file at 0: $!"; |
| 173 |
} |
| 174 |
else { |
| 175 |
open PID, ">$pid_file" |
| 176 |
or die "open pid_file: $!\n"; |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
run(); |
| 181 |
|
| 182 |
#start daemon |
| 183 |
sub run { |
| 184 |
|
| 185 |
# get UUID/GUID |
| 186 |
my ($quid, $qgid, $groups); |
| 187 |
if ($user) { |
| 188 |
(undef, undef, $quid, $qgid) = getpwnam $user |
| 189 |
or die "unable to determine uid/gid for $user\n"; |
| 190 |
$groups = "$qgid $qgid"; |
| 191 |
while (my ($name, $passwd, $gid, $members) = getgrent()) { |
| 192 |
my @m = split(/ /, $members); |
| 193 |
if (grep { $_ eq $user } @m) { |
| 194 |
$groups .= " $gid"; |
| 195 |
} |
| 196 |
} |
| 197 |
endgrent; |
| 198 |
} |
| 199 |
|
| 200 |
for my $addr (@d_addr) { |
| 201 |
my @Socket_opts = ( |
| 202 |
LocalPort => $addr->{port}, |
| 203 |
LocalAddr => $addr->{addr}, |
| 204 |
Proto => 'tcp', |
| 205 |
Listen => SOMAXCONN, |
| 206 |
Reuse => 1, |
| 207 |
); |
| 208 |
|
| 209 |
# create new socket (used by clients to communicate with daemon) |
| 210 |
my $s; |
| 211 |
if ($has_ipv6) { |
| 212 |
$s = IO::Socket::INET6->new(@Socket_opts); |
| 213 |
} |
| 214 |
else { |
| 215 |
$s = IO::Socket::INET->new(@Socket_opts); |
| 216 |
} |
| 217 |
die "FATAL: Failed to open socket on $addr->{addr}:$addr->{port} ($@)" |
| 218 |
. "\nIt may be necessary to wait 20 secs before starting daemon" |
| 219 |
. " again." |
| 220 |
unless $s; |
| 221 |
$select->add($s); |
| 222 |
} |
| 223 |
|
| 224 |
info( "qpsmtpd-prefork daemon, version: $VERSION, staring on host: " |
| 225 |
. join(', ', map { "$_->{addr}:$_->{port}" } @d_addr) |
| 226 |
. " (user: $user [$<])"); |
| 227 |
|
| 228 |
# reset priority |
| 229 |
my $old_nice = getpriority(0, 0); |
| 230 |
my $new_nice = $old_nice - $re_nice; |
| 231 |
if ($new_nice < 20 && $new_nice > -20) { |
| 232 |
setpriority(0, 0, $1) if ($new_nice =~ /(\-?\d+)/); |
| 233 |
info("parent daemon nice level: $1"); |
| 234 |
} |
| 235 |
else { |
| 236 |
die "FATAL: new nice level: $new_nice is not between -19 and 19 " |
| 237 |
. "(old level = $old_nice, renice value = $re_nice)"; |
| 238 |
} |
| 239 |
|
| 240 |
if ($user) { |
| 241 |
|
| 242 |
# change UUID/UGID |
| 243 |
$) = $groups; |
| 244 |
POSIX::setgid($qgid) or die "unable to change gid: $!\n"; |
| 245 |
POSIX::setuid($quid) or die "unable to change uid: $!\n"; |
| 246 |
$> = $quid; |
| 247 |
die "FATAL: failed to setuid to user: $user, uid: $quid\n" |
| 248 |
if ($> != $quid and $> != ($quid - 2**32)); |
| 249 |
} |
| 250 |
|
| 251 |
# setup shared memory |
| 252 |
$chld_shmem = shmem($d_port . "qpsmtpd", 1); |
| 253 |
untie $chld_shmem; |
| 254 |
|
| 255 |
# Interrupt handler |
| 256 |
$SIG{INT} = $SIG{TERM} = sub { |
| 257 |
|
| 258 |
# terminate daemon (and children) |
| 259 |
my $sig = shift; |
| 260 |
|
| 261 |
# prevent another signal and disable reaper |
| 262 |
$SIG{$sig} = $SIG{CHLD} = $SIG{HUP} = 'IGNORE'; |
| 263 |
|
| 264 |
# a notice, before the sleep below |
| 265 |
info("shutting down"); |
| 266 |
|
| 267 |
# close socket(s) |
| 268 |
$_->close for $select->handles; |
| 269 |
|
| 270 |
# send signal to process group |
| 271 |
kill -$sig_num{$sig} => $$; |
| 272 |
|
| 273 |
# cleanup |
| 274 |
IPC::Shareable->clean_up; |
| 275 |
unlink($pid_file) if $pid_file; |
| 276 |
|
| 277 |
info("shutdown of daemon"); |
| 278 |
exit; |
| 279 |
}; |
| 280 |
|
| 281 |
# Hup handler |
| 282 |
$SIG{HUP} = sub { |
| 283 |
|
| 284 |
# reload qpmstpd plugins |
| 285 |
$qpsmtpd = qpsmtpd_instance('restart' => 1); # reload plugins... |
| 286 |
$qpsmtpd->load_plugins; |
| 287 |
kill 'HUP' => keys %children; |
| 288 |
info("reload daemon requested"); |
| 289 |
}; |
| 290 |
|
| 291 |
# setup qpsmtpd_instance |
| 292 |
$qpsmtpd = qpsmtpd_instance(); |
| 293 |
|
| 294 |
if ($detach) { |
| 295 |
open STDIN, '/dev/null' or die "/dev/null: $!"; |
| 296 |
open STDOUT, '>/dev/null' or die "/dev/null: $!"; |
| 297 |
open STDERR, '>&STDOUT' or die "open(stderr): $!"; |
| 298 |
defined(my $pid = fork) or die "fork: $!"; |
| 299 |
exit 0 if $pid; |
| 300 |
} |
| 301 |
POSIX::setsid or die "setsid: $!"; |
| 302 |
|
| 303 |
if ($pid_file) { |
| 304 |
print PID $$, "\n"; |
| 305 |
close PID; |
| 306 |
} |
| 307 |
|
| 308 |
# child reaper |
| 309 |
$SIG{CHLD} = \&reaper; |
| 310 |
spawn_children(); |
| 311 |
main_loop(); |
| 312 |
exit; |
| 313 |
} |
| 314 |
|
| 315 |
# initialize children (only done at daemon startup) |
| 316 |
sub spawn_children { |
| 317 |
|
| 318 |
# block signals while new children are being spawned |
| 319 |
my $sigset = block_signal(SIGCHLD); |
| 320 |
for (1 .. $chld_pool) { |
| 321 |
new_child(); |
| 322 |
} |
| 323 |
|
| 324 |
# reset block signals |
| 325 |
unblock_signal($sigset); |
| 326 |
} |
| 327 |
|
| 328 |
# cleanup after child dies |
| 329 |
sub reaper { |
| 330 |
my $stiff; |
| 331 |
while (($stiff = waitpid(-1, &WNOHANG)) > 0) { |
| 332 |
my $res = WEXITSTATUS($?); |
| 333 |
info("child terminated, pid: $stiff (status $?, res: $res)"); |
| 334 |
delete $children{$stiff}; # delete pid from children |
| 335 |
# add pid to array so it later can be removed from shared memory |
| 336 |
push @children_term, $stiff; |
| 337 |
} |
| 338 |
|
| 339 |
$SIG{CHLD} = \&reaper; |
| 340 |
} |
| 341 |
|
| 342 |
#main_loop: main loop. Either processes children that have exited or |
| 343 |
# periodically scans the shared memory for children that are not longer |
| 344 |
# alive. Spawns new children when necessary. |
| 345 |
#arg0: void |
| 346 |
#ret0: void |
| 347 |
sub main_loop { |
| 348 |
my $created_children = $idle_children; |
| 349 |
while (1) { |
| 350 |
|
| 351 |
# if there is no child death to process, then sleep EXPR seconds |
| 352 |
# or until signal (i.e. child death) is received |
| 353 |
sleep $loop_sleep / ($created_children * 2 + 1) unless @children_term; |
| 354 |
|
| 355 |
# block CHLD signals to avoid race |
| 356 |
my $sigset = block_signal(SIGCHLD); |
| 357 |
|
| 358 |
# get number of busy children |
| 359 |
if (@children_term) { |
| 360 |
|
| 361 |
# remove dead children info from shared memory |
| 362 |
$chld_busy = shmem_opt(undef, \@children_term, undef, undef); |
| 363 |
@children_term = (); |
| 364 |
} |
| 365 |
else { |
| 366 |
# just check the shared memory |
| 367 |
$chld_busy = shmem_opt(undef, undef, undef, undef, 1); |
| 368 |
} |
| 369 |
|
| 370 |
# calculate children in pool (if valid busy children number) |
| 371 |
if (defined($chld_busy)) { |
| 372 |
info("busy children: $chld_busy"); |
| 373 |
$chld_pool = $chld_busy + $idle_children; |
| 374 |
|
| 375 |
# ensure pool limit is max_children |
| 376 |
$chld_pool = $max_children if ($chld_pool > $max_children); |
| 377 |
info( "children pool: $chld_pool, spawned: " |
| 378 |
. scalar(keys %children) |
| 379 |
. ", busy: $chld_busy"); |
| 380 |
} |
| 381 |
else { |
| 382 |
|
| 383 |
# reset shared memory |
| 384 |
warn("unable to access shared memory - resetting it"); |
| 385 |
IPC::Shareable->clean_up; |
| 386 |
my $shmem = shmem($d_port . "qpsmtpd", 1); |
| 387 |
untie $shmem; |
| 388 |
} |
| 389 |
|
| 390 |
# spawn children |
| 391 |
$created_children = $chld_pool - keys %children; |
| 392 |
$created_children = 0 if $created_children < 0; |
| 393 |
new_child() for 1 .. $created_children; |
| 394 |
|
| 395 |
# unblock signals |
| 396 |
unblock_signal($sigset); |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
# block_signal: block signals |
| 401 |
# arg0..n: int with signal(s) to block |
| 402 |
# ret0: ref str with sigset (used to later unblock signal) |
| 403 |
sub block_signal { |
| 404 |
my @signal = @_; #arg0..n |
| 405 |
|
| 406 |
my ($sigset, $blockset); |
| 407 |
|
| 408 |
$sigset = POSIX::SigSet->new(); |
| 409 |
$blockset = POSIX::SigSet->new(@signal); |
| 410 |
sigprocmask(SIG_BLOCK, $blockset, $sigset) |
| 411 |
or die "Could not block @signal signals: $!\n"; |
| 412 |
|
| 413 |
return ($sigset); |
| 414 |
} |
| 415 |
|
| 416 |
# unblock_signal: unblock/reset and receive pending signals |
| 417 |
# arg0: ref str with sigset |
| 418 |
# ret0: void |
| 419 |
sub unblock_signal { |
| 420 |
my $sigset = shift; # arg0 |
| 421 |
sigprocmask(SIG_SETMASK, $sigset) |
| 422 |
or die "Could not restore signals: $!\n"; |
| 423 |
} |
| 424 |
|
| 425 |
# new_child: initialize new child |
| 426 |
# arg0: void |
| 427 |
# ret0: void |
| 428 |
sub new_child { |
| 429 |
|
| 430 |
# daemonize away from the parent process |
| 431 |
my $pid; |
| 432 |
die "Cannot fork child: $!\n" unless defined($pid = fork); |
| 433 |
if ($pid) { |
| 434 |
|
| 435 |
# in parent |
| 436 |
$children{$pid} = 1; |
| 437 |
info("new child, pid: $pid"); |
| 438 |
return; |
| 439 |
} |
| 440 |
|
| 441 |
# in child |
| 442 |
|
| 443 |
# reset priority |
| 444 |
setpriority 0, 0, getpriority(0, 0) + $re_nice; |
| 445 |
|
| 446 |
# reset signals |
| 447 |
my $sigset = POSIX::SigSet->new(); |
| 448 |
my $blockset = POSIX::SigSet->new(SIGCHLD); |
| 449 |
sigprocmask(SIG_UNBLOCK, $blockset, $sigset) |
| 450 |
or die "Could not unblock SIGCHLD signal: $!\n"; |
| 451 |
$SIG{CHLD} = $SIG{INT} = $SIG{TERM} = $SIG{ALRM} = 'DEFAULT'; |
| 452 |
|
| 453 |
# child should exit if it receives HUP signal (note: blocked while child |
| 454 |
# is busy, but restored once done) |
| 455 |
$SIG{HUP} = sub { |
| 456 |
info("signal HUP received, going to exit"); |
| 457 |
exit; |
| 458 |
}; |
| 459 |
|
| 460 |
# continue to accept connections until "old age" is reached |
| 461 |
for (my $i = 0 ; $i < $child_lifetime ; $i++) { |
| 462 |
|
| 463 |
# accept a connection |
| 464 |
if ($pretty) { |
| 465 |
$ENV{PROCESS} = $0 if not defined $ENV{PROCESS}; # 1st time only |
| 466 |
$0 = 'qpsmtpd child'; # set pretty child name in process listing |
| 467 |
} |
| 468 |
my @ready = $select->can_read(); |
| 469 |
next unless @ready; |
| 470 |
my $socket = $ready[0]; |
| 471 |
my ($client, $iinfo) = $socket->accept() |
| 472 |
or die |
| 473 |
"failed to create new object - $!"; # wait here until client connects |
| 474 |
info("connect from: " . $client->peerhost . ":" . $client->peerport); |
| 475 |
|
| 476 |
# clear a previously running instance by creating a new instance |
| 477 |
$qpsmtpd = qpsmtpd_instance(); |
| 478 |
|
| 479 |
# set STDIN/STDOUT and autoflush |
| 480 |
# ... no longer use POSIX::dup2: it failes after a few |
| 481 |
# million connections |
| 482 |
close(STDIN); |
| 483 |
open(STDIN, "+<&" . fileno($client)) |
| 484 |
or die "unable to duplicate filehandle to STDIN - $!"; |
| 485 |
|
| 486 |
close(STDOUT); |
| 487 |
open(STDOUT, "+>&" . fileno($client)) |
| 488 |
or die "unable to duplicate filehandle to STDOUT - $!"; |
| 489 |
select(STDOUT); |
| 490 |
$| = 1; |
| 491 |
|
| 492 |
# connection recieved, block signals |
| 493 |
my $sigset = block_signal(SIGHUP); |
| 494 |
|
| 495 |
# start a session if connection looks valid |
| 496 |
qpsmtpd_session($socket, $client, $iinfo, $qpsmtpd) if ($iinfo); |
| 497 |
|
| 498 |
# close connection and cleanup |
| 499 |
$client->shutdown(2); |
| 500 |
|
| 501 |
# unset block and receive pending signals |
| 502 |
unblock_signal($sigset); |
| 503 |
} |
| 504 |
exit; # this child has reached its end-of-life |
| 505 |
} |
| 506 |
|
| 507 |
# respond to client |
| 508 |
# arg0: ref to socket object (client) |
| 509 |
# arg1: int with SMTP reply code |
| 510 |
# arg2: arr with message |
| 511 |
# ret0: int 0|1 (0 = failure, 1 = success) |
| 512 |
sub respond_client { |
| 513 |
my ($client, $code, @message) = @_; |
| 514 |
$client->autoflush(1); |
| 515 |
while (my $msg = shift @message) { |
| 516 |
my $line = $code . (@message ? "-" : " ") . $msg; |
| 517 |
info("reply to client: <$line>"); |
| 518 |
print $client "$line\r\n" |
| 519 |
or (info("Could not print [$line]: $!"), return 0); |
| 520 |
} |
| 521 |
return 1; |
| 522 |
} |
| 523 |
|
| 524 |
# qpsmtpd_instance: setup qpsmtpd instance |
| 525 |
# arg0: void |
| 526 |
# ret0: ref to qpsmtpd_instance |
| 527 |
sub qpsmtpd_instance { |
| 528 |
my %args = @_; |
| 529 |
my $qpsmtpd = Qpsmtpd::TcpServer::Prefork->new(%args); |
| 530 |
$qpsmtpd->load_plugins; |
| 531 |
$qpsmtpd->spool_dir; |
| 532 |
$qpsmtpd->size_threshold; |
| 533 |
|
| 534 |
return ($qpsmtpd); |
| 535 |
} |
| 536 |
|
| 537 |
# shmem: tie to shared memory hash |
| 538 |
# arg0: str with glue |
| 539 |
# arg1: int 0|1 (0 = don't create shmem, 1 = create shmem) |
| 540 |
# ret0: ref to shared hash |
| 541 |
sub shmem { |
| 542 |
my $glue = shift; #arg0 |
| 543 |
my $create = shift || 0; #arg1 |
| 544 |
|
| 545 |
my %options = ( |
| 546 |
create => $create, |
| 547 |
exclusive => 0, |
| 548 |
mode => 0640, |
| 549 |
destroy => 0, |
| 550 |
); |
| 551 |
|
| 552 |
my %shmem_hash; |
| 553 |
eval { |
| 554 |
tie %shmem_hash, 'IPC::Shareable', $glue, {%options} |
| 555 |
|| die "unable to tie to shared memory - $!"; |
| 556 |
}; |
| 557 |
if ($@) { |
| 558 |
info("$@"); |
| 559 |
return; |
| 560 |
} |
| 561 |
|
| 562 |
return (\%shmem_hash); |
| 563 |
} |
| 564 |
|
| 565 |
# shmem_opt: connect to shared memory and perform options |
| 566 |
# arg0: ref to hash where shared memory should be copied to |
| 567 |
# arg1: ref to arr with pid(s) to delete |
| 568 |
# arg2: int with pid to add (key) |
| 569 |
# arg3: str with packed iaddr to add (value) |
| 570 |
# arg4: int 0|1 check and cleanup shared memory (0 = no, 1 = yes - default 0) |
| 571 |
# ret0: int with number of busy children (undef if error) |
| 572 |
sub shmem_opt { |
| 573 |
my $ref_shmem = shift; #arg0 |
| 574 |
my $ref_pid_del = shift; #arg1 |
| 575 |
my $pid_add_key = shift; #arg2 |
| 576 |
my $pid_add_value = shift; #arg3 |
| 577 |
my $check = shift || 0; #arg4 |
| 578 |
|
| 579 |
# check arguments |
| 580 |
if ( (defined($pid_add_key) && !defined($pid_add_value)) |
| 581 |
|| (!defined($pid_add_key) && defined($pid_add_value))) |
| 582 |
{ |
| 583 |
return; |
| 584 |
} |
| 585 |
|
| 586 |
my ($chld_shmem, $chld_busy); |
| 587 |
eval { |
| 588 |
$chld_shmem = |
| 589 |
&shmem($d_port . "qpsmtpd", 0); #connect to shared memory hash |
| 590 |
|
| 591 |
if (tied %{$chld_shmem}) { |
| 592 |
|
| 593 |
# lock shared memory |
| 594 |
eval { |
| 595 |
# ensure that hung shared memory is noticed |
| 596 |
local $SIG{ALRM} = sub { |
| 597 |
die "locking timed out\n"; |
| 598 |
}; |
| 599 |
alarm 15; |
| 600 |
|
| 601 |
(tied %{$chld_shmem})->shlock(LOCK_EX); |
| 602 |
|
| 603 |
alarm 0; |
| 604 |
}; |
| 605 |
die $@ if $@; |
| 606 |
|
| 607 |
# delete |
| 608 |
if ($ref_pid_del) { |
| 609 |
foreach my $pid_del (@{$ref_pid_del}) { |
| 610 |
delete $$chld_shmem{$pid_del}; |
| 611 |
} |
| 612 |
} |
| 613 |
|
| 614 |
# add |
| 615 |
$$chld_shmem{$pid_add_key} = $pid_add_value if ($pid_add_key); |
| 616 |
|
| 617 |
# copy |
| 618 |
%{$ref_shmem} = %{$chld_shmem} if ($ref_shmem); |
| 619 |
|
| 620 |
# check |
| 621 |
if ($check) { |
| 622 |
|
| 623 |
# loop through pid list and delete orphaned processes |
| 624 |
foreach my $pid (keys %{$chld_shmem}) { |
| 625 |
if (!kill 0, $pid) { |
| 626 |
delete $$chld_shmem{$pid}; |
| 627 |
warn("orphaned child, pid: $pid removed from memory"); |
| 628 |
} |
| 629 |
} |
| 630 |
} |
| 631 |
|
| 632 |
# number of busy children |
| 633 |
$chld_busy = scalar(keys %{$chld_shmem}); |
| 634 |
|
| 635 |
# unlock shared memory |
| 636 |
(tied %{$chld_shmem})->shunlock; |
| 637 |
|
| 638 |
# untie from shared memory |
| 639 |
untie $chld_shmem || die "unable to untie from shared memory"; |
| 640 |
} |
| 641 |
else { |
| 642 |
die "failed to connect to shared memory"; |
| 643 |
} |
| 644 |
}; |
| 645 |
|
| 646 |
# check for error |
| 647 |
if ($@) { |
| 648 |
undef($chld_busy); |
| 649 |
warn("$@"); |
| 650 |
} |
| 651 |
|
| 652 |
return ($chld_busy); |
| 653 |
} |
| 654 |
|
| 655 |
# info: write info |
| 656 |
# arg0: str with debug text |
| 657 |
sub info { |
| 658 |
my $text = shift; #arg0 |
| 659 |
return if (!$debug); |
| 660 |
|
| 661 |
my ($sec, $min, $hour, $mday, $mon, $year) = localtime(time); |
| 662 |
my $nowtime = sprintf "%02d/%02d/%02d %02d:%02d:%02d", $mday, $mon + 1, |
| 663 |
$year + 1900, $hour, $min, $sec; |
| 664 |
|
| 665 |
chomp($text); |
| 666 |
print STDERR "$nowtime:$$: $text\n"; |
| 667 |
} |
| 668 |
|
| 669 |
# start qpmstpd session |
| 670 |
# arg0: ref to socket object |
| 671 |
# arg1: ref to socket object |
| 672 |
# arg2: ref to qpsmtpd instance |
| 673 |
# ret0: void |
| 674 |
sub qpsmtpd_session { |
| 675 |
my $socket = shift; #arg0 |
| 676 |
my $client = shift; #arg1 |
| 677 |
my $iinfo = shift; #arg2 |
| 678 |
my $qpsmtpd = shift; #arg3 |
| 679 |
|
| 680 |
# get local/remote hostname, port and ip address |
| 681 |
my ($port, $iaddr, $lport, $laddr, $nto_iaddr, $nto_laddr) = |
| 682 |
Qpsmtpd::TcpServer::lrpip($socket, $client, $iinfo); |
| 683 |
|
| 684 |
# get current connected ip addresses (from shared memory) |
| 685 |
my %children; |
| 686 |
shmem_opt(\%children, undef, $$, $iaddr); |
| 687 |
|
| 688 |
my ($rc, @msg) = |
| 689 |
$qpsmtpd->run_hooks( |
| 690 |
"pre-connection", |
| 691 |
remote_ip => $nto_iaddr, |
| 692 |
remote_port => $port, |
| 693 |
local_ip => $nto_laddr, |
| 694 |
local_port => $lport, |
| 695 |
max_conn_ip => $maxconnip, |
| 696 |
child_addrs => [values %children], |
| 697 |
); |
| 698 |
if ( $rc == DENYSOFT |
| 699 |
|| $rc == DENYSOFT_DISCONNECT |
| 700 |
|| $rc == DENY |
| 701 |
|| $rc == DENY_DISCONNECT) |
| 702 |
{ |
| 703 |
#smtp return code to reply client with (seed with soft deny) |
| 704 |
my $rc_reply = 451; |
| 705 |
unless ($msg[0]) { |
| 706 |
if ($rc == DENYSOFT || $rc == DENYSOFT_DISCONNECT) { |
| 707 |
@msg = ("Sorry, try again later"); |
| 708 |
} |
| 709 |
else { |
| 710 |
@msg = ("Sorry, service not available to you"); |
| 711 |
$rc_reply = 550; |
| 712 |
} |
| 713 |
} |
| 714 |
respond_client($client, $rc_reply, @msg); |
| 715 |
|
| 716 |
# remove pid from shared memory |
| 717 |
shmem_opt(undef, [$$], undef, undef); |
| 718 |
|
| 719 |
# retur so child can be reused |
| 720 |
return; |
| 721 |
} |
| 722 |
|
| 723 |
# all children should have different seeds, to prevent conflicts |
| 724 |
srand(time ^ ($$ + ($$ << 15))); |
| 725 |
|
| 726 |
# ALRM handler |
| 727 |
$SIG{ALRM} = sub { |
| 728 |
print $client "421 Connection Timed Out\n"; |
| 729 |
info("Connection Timed Out"); |
| 730 |
|
| 731 |
# child terminates |
| 732 |
exit; |
| 733 |
}; |
| 734 |
|
| 735 |
# set enviroment variables |
| 736 |
($ENV{TCPLOCALIP}, $ENV{TCPREMOTEIP}, $ENV{TCPREMOTEHOST}) = |
| 737 |
Qpsmtpd::TcpServer::tcpenv($nto_laddr, $nto_iaddr); |
| 738 |
|
| 739 |
# run qpmsptd functions |
| 740 |
$SIG{__DIE__} = 'DEFAULT'; |
| 741 |
eval { |
| 742 |
$qpsmtpd->start_connection( |
| 743 |
local_ip => $ENV{TCPLOCALIP}, |
| 744 |
local_port => $lport, |
| 745 |
remote_ip => $ENV{TCPREMOTEIP}, |
| 746 |
remote_port => $client->peerport, |
| 747 |
); |
| 748 |
$qpsmtpd->run($client); |
| 749 |
$qpsmtpd->run_hooks("post-connection"); |
| 750 |
$qpsmtpd->connection->reset; |
| 751 |
}; |
| 752 |
if ($@ !~ /^(disconnect_tcpserver|died while reading from STDIN)/) { |
| 753 |
warn("$@"); |
| 754 |
} |
| 755 |
|
| 756 |
# child is now idle again |
| 757 |
info("disconnect from: $nto_iaddr:$port"); |
| 758 |
|
| 759 |
# remove pid from shared memory |
| 760 |
unless (defined(shmem_opt(undef, [$$], undef, undef))) { |
| 761 |
|
| 762 |
# exit because parent is down or shared memory is corrupted |
| 763 |
info("parent seems to be down, going to exit"); |
| 764 |
exit 1; |
| 765 |
} |
| 766 |
} |