2005-04-26 04:46:45 +02:00
|
|
|
package Qpsmtpd::ConfigServer;
|
|
|
|
|
|
|
|
use base ('Danga::Client');
|
2005-04-28 23:38:43 +02:00
|
|
|
use Qpsmtpd::Constants;
|
|
|
|
|
|
|
|
use strict;
|
2005-04-26 04:46:45 +02:00
|
|
|
|
|
|
|
use fields qw(
|
2013-04-21 06:08:43 +02:00
|
|
|
_auth
|
|
|
|
_commands
|
|
|
|
_config_cache
|
|
|
|
_connection
|
|
|
|
_transaction
|
|
|
|
_test_mode
|
|
|
|
_extras
|
|
|
|
other_fds
|
|
|
|
);
|
2005-04-26 04:46:45 +02:00
|
|
|
|
2005-04-28 23:38:43 +02:00
|
|
|
my $PROMPT = "Enter command: ";
|
|
|
|
|
2005-04-26 04:46:45 +02:00
|
|
|
sub new {
|
|
|
|
my Qpsmtpd::ConfigServer $self = shift;
|
2013-04-21 06:08:43 +02:00
|
|
|
|
2005-04-26 04:46:45 +02:00
|
|
|
$self = fields::new($self) unless ref $self;
|
2013-04-21 06:08:43 +02:00
|
|
|
$self->SUPER::new(@_);
|
2005-04-28 23:38:43 +02:00
|
|
|
$self->write($PROMPT);
|
2005-04-26 04:46:45 +02:00
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
sub max_idle_time { 3600 } # one hour
|
2005-04-28 23:38:43 +02:00
|
|
|
|
2005-04-26 04:46:45 +02:00
|
|
|
sub process_line {
|
|
|
|
my $self = shift;
|
|
|
|
my $line = shift || return;
|
2013-04-21 06:08:43 +02:00
|
|
|
if ($::DEBUG > 1) { print "$$:" . ($self + 0) . "C($self->{mode}): $line"; }
|
2005-04-26 04:46:45 +02:00
|
|
|
local $SIG{ALRM} = sub {
|
|
|
|
my ($pkg, $file, $line) = caller();
|
|
|
|
die "ALARM: $pkg, $file, $line";
|
|
|
|
};
|
2013-04-21 06:08:43 +02:00
|
|
|
my $prev = alarm(2); # must process a command in < 2 seconds
|
2005-04-26 04:46:45 +02:00
|
|
|
my $resp = eval { $self->_process_line($line) };
|
|
|
|
alarm($prev);
|
|
|
|
if ($@) {
|
|
|
|
print STDERR "Error: $@\n";
|
|
|
|
}
|
|
|
|
return $resp || '';
|
|
|
|
}
|
|
|
|
|
|
|
|
sub respond {
|
|
|
|
my $self = shift;
|
|
|
|
my (@messages) = @_;
|
|
|
|
while (my $msg = shift @messages) {
|
|
|
|
$self->write("$msg\r\n");
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub fault {
|
2013-04-21 06:08:43 +02:00
|
|
|
my $self = shift;
|
|
|
|
my ($msg) = shift || "program fault - command not performed";
|
2014-09-11 18:58:31 +02:00
|
|
|
print STDERR "$0 [$$]: $msg\n";
|
|
|
|
print STDERR $name, "[$$]: Last system error: $!"
|
|
|
|
." (Likely irelevant--debug the crashed plugin to ensure it handles \$! properly)";
|
2013-04-21 06:08:43 +02:00
|
|
|
$self->respond("Error - " . $msg);
|
|
|
|
return $PROMPT;
|
2005-04-26 04:46:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub _process_line {
|
|
|
|
my $self = shift;
|
|
|
|
my $line = shift;
|
|
|
|
|
|
|
|
$line =~ s/\r?\n//;
|
|
|
|
my ($cmd, @params) = split(/ +/, $line);
|
2005-04-28 23:38:43 +02:00
|
|
|
my $meth = "cmd_" . lc($cmd);
|
|
|
|
if (my $lookup = $self->can($meth)) {
|
2013-04-21 06:08:43 +02:00
|
|
|
my $resp = eval { $lookup->($self, @params); };
|
2005-04-26 04:46:45 +02:00
|
|
|
if ($@) {
|
|
|
|
my $error = $@;
|
|
|
|
chomp($error);
|
2005-04-28 23:38:43 +02:00
|
|
|
Qpsmtpd->log(LOGERROR, "Command Error: $error");
|
2005-04-26 04:46:45 +02:00
|
|
|
return $self->fault("command '$cmd' failed unexpectedly");
|
|
|
|
}
|
2005-04-28 23:38:43 +02:00
|
|
|
return "$resp\n$PROMPT";
|
2005-04-26 04:46:45 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
# No such method - i.e. unrecognized command
|
|
|
|
return $self->fault("command '$cmd' unrecognised");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
my %helptext = (
|
2013-04-21 06:08:43 +02:00
|
|
|
help => "HELP [CMD] - Get help on all commands or a specific command",
|
2005-04-26 04:46:45 +02:00
|
|
|
status => "STATUS - Returns status information about current connections",
|
2013-04-21 06:08:43 +02:00
|
|
|
list =>
|
|
|
|
"LIST [LIMIT] - List the connections, specify limit or negative limit to shrink list",
|
|
|
|
kill =>
|
|
|
|
"KILL (\$IP | \$REF) - Disconnect all connections from \$IP or connection reference \$REF",
|
|
|
|
pause => "PAUSE - Stop accepting new connections",
|
2005-04-28 23:38:43 +02:00
|
|
|
continue => "CONTINUE - Resume accepting connections",
|
2013-04-21 06:08:43 +02:00
|
|
|
reload => "RELOAD - Reload all plugins and config",
|
|
|
|
quit => "QUIT - Exit the config server",
|
|
|
|
);
|
2005-04-26 04:46:45 +02:00
|
|
|
|
2005-04-28 23:38:43 +02:00
|
|
|
sub cmd_help {
|
2005-04-26 04:46:45 +02:00
|
|
|
my $self = shift;
|
|
|
|
my ($subcmd) = @_;
|
2013-04-21 06:08:43 +02:00
|
|
|
|
2005-04-28 23:38:43 +02:00
|
|
|
$subcmd ||= 'help';
|
2005-04-26 04:46:45 +02:00
|
|
|
$subcmd = lc($subcmd);
|
2013-04-21 06:08:43 +02:00
|
|
|
|
2005-04-28 23:38:43 +02:00
|
|
|
if ($subcmd eq 'help') {
|
2013-04-21 06:08:43 +02:00
|
|
|
my $txt = join("\n",
|
|
|
|
map { substr($_, 0, index($_, "-")) }
|
|
|
|
sort values(%helptext));
|
2005-04-28 23:38:43 +02:00
|
|
|
return "Available Commands:\n\n$txt\n";
|
|
|
|
}
|
2013-04-21 06:08:43 +02:00
|
|
|
my $txt = $helptext{$subcmd}
|
|
|
|
|| "Unrecognised help option. Try 'help' for a full list.";
|
2005-04-28 23:38:43 +02:00
|
|
|
return "$txt\n";
|
2005-04-26 04:46:45 +02:00
|
|
|
}
|
|
|
|
|
2005-04-28 23:38:43 +02:00
|
|
|
sub cmd_quit {
|
2005-04-26 04:46:45 +02:00
|
|
|
my $self = shift;
|
2005-04-28 23:38:43 +02:00
|
|
|
$self->close;
|
|
|
|
}
|
|
|
|
|
2007-02-03 00:47:26 +01:00
|
|
|
sub cmd_shutdown {
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2005-04-28 23:38:43 +02:00
|
|
|
sub cmd_pause {
|
|
|
|
my $self = shift;
|
2013-04-21 06:08:43 +02:00
|
|
|
|
2005-04-28 23:38:43 +02:00
|
|
|
my $other_fds = $self->OtherFds;
|
2013-04-21 06:08:43 +02:00
|
|
|
|
|
|
|
$self->{other_fds} = {%$other_fds};
|
2005-04-28 23:38:43 +02:00
|
|
|
%$other_fds = ();
|
|
|
|
return "PAUSED";
|
|
|
|
}
|
|
|
|
|
2005-05-09 15:43:40 +02:00
|
|
|
sub cmd_continue {
|
|
|
|
my $self = shift;
|
2013-04-21 06:08:43 +02:00
|
|
|
|
2005-05-09 15:43:40 +02:00
|
|
|
my $other_fds = $self->{other_fds};
|
2013-04-21 06:08:43 +02:00
|
|
|
|
|
|
|
$self->OtherFds(%$other_fds);
|
2005-05-09 15:43:40 +02:00
|
|
|
%$other_fds = ();
|
|
|
|
return "UNPAUSED";
|
|
|
|
}
|
|
|
|
|
2005-04-28 23:38:43 +02:00
|
|
|
sub cmd_status {
|
|
|
|
my $self = shift;
|
2013-04-21 06:08:43 +02:00
|
|
|
|
|
|
|
# Status should show:
|
|
|
|
# - Total time running
|
|
|
|
# - Total number of mails received
|
|
|
|
# - Total number of mails rejected (5xx)
|
|
|
|
# - Total number of mails tempfailed (5xx)
|
|
|
|
# - Avg number of mails/minute
|
|
|
|
# - Number of current connections
|
|
|
|
# - Number of outstanding DNS queries
|
|
|
|
|
2005-04-28 23:38:43 +02:00
|
|
|
my $output = "Current Status as of " . gmtime() . " GMT\n\n";
|
2013-04-21 06:08:43 +02:00
|
|
|
|
2006-06-27 22:28:36 +02:00
|
|
|
if (defined &Qpsmtpd::Plugin::stats::get_stats) {
|
2013-04-21 06:08:43 +02:00
|
|
|
|
2005-04-28 23:38:43 +02:00
|
|
|
# Stats plugin is loaded
|
2005-06-09 00:25:28 +02:00
|
|
|
$output .= Qpsmtpd::Plugin::stats->get_stats;
|
2005-04-28 23:38:43 +02:00
|
|
|
}
|
2013-04-21 06:08:43 +02:00
|
|
|
|
2005-04-26 04:46:45 +02:00
|
|
|
my $descriptors = Danga::Socket->DescriptorMap;
|
2013-04-21 06:08:43 +02:00
|
|
|
|
2005-04-26 04:46:45 +02:00
|
|
|
my $current_connections = 0;
|
2013-04-21 06:08:43 +02:00
|
|
|
my $current_dns = 0;
|
2005-04-26 04:46:45 +02:00
|
|
|
foreach my $fd (keys %$descriptors) {
|
|
|
|
my $pob = $descriptors->{$fd};
|
|
|
|
if ($pob->isa("Qpsmtpd::PollServer")) {
|
|
|
|
$current_connections++;
|
|
|
|
}
|
2007-02-03 00:47:26 +01:00
|
|
|
elsif ($pob->isa("ParaDNS::Resolver")) {
|
2005-04-26 04:46:45 +02:00
|
|
|
$current_dns = $pob->pending;
|
|
|
|
}
|
|
|
|
}
|
2013-04-21 06:08:43 +02:00
|
|
|
|
|
|
|
$output .= "Curr Connections: $current_connections / $::MAXconn\n"
|
|
|
|
. "Curr DNS Queries: $current_dns";
|
|
|
|
|
2005-04-28 23:38:43 +02:00
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub cmd_list {
|
|
|
|
my $self = shift;
|
|
|
|
my ($count) = @_;
|
2013-04-21 06:08:43 +02:00
|
|
|
|
2005-04-28 23:38:43 +02:00
|
|
|
my $descriptors = Danga::Socket->DescriptorMap;
|
2013-04-21 06:08:43 +02:00
|
|
|
|
|
|
|
my $list =
|
|
|
|
"Current"
|
|
|
|
. ($count ? (($count > 0) ? " Oldest $count" : " Newest " . -$count) : "")
|
|
|
|
. " Connections: \n\n";
|
2005-04-28 23:38:43 +02:00
|
|
|
my @all;
|
|
|
|
foreach my $fd (keys %$descriptors) {
|
|
|
|
my $pob = $descriptors->{$fd};
|
|
|
|
if ($pob->isa("Qpsmtpd::PollServer")) {
|
2013-04-21 06:08:43 +02:00
|
|
|
next unless $pob->connection->remote_ip; # haven't even started yet
|
|
|
|
push @all,
|
|
|
|
[
|
|
|
|
$pob + 0, $pob->connection->remote_ip,
|
|
|
|
$pob->connection->remote_host, $pob->uptime
|
|
|
|
];
|
2005-04-28 23:38:43 +02:00
|
|
|
}
|
|
|
|
}
|
2013-04-21 06:08:43 +02:00
|
|
|
|
2005-04-28 23:38:43 +02:00
|
|
|
@all = sort { $a->[3] <=> $b->[3] } @all;
|
|
|
|
if ($count) {
|
|
|
|
if ($count > 0) {
|
2013-04-21 06:08:43 +02:00
|
|
|
@all = @all[$#all - ($count - 1) .. $#all];
|
2005-04-28 23:38:43 +02:00
|
|
|
}
|
|
|
|
else {
|
2013-04-21 06:08:43 +02:00
|
|
|
@all = @all[0 .. (abs($count) - 1)];
|
2005-04-28 23:38:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
foreach my $item (@all) {
|
2013-04-21 06:08:43 +02:00
|
|
|
$list .= sprintf("%x : %s [%s] Connected %0.2fs\n",
|
|
|
|
map { defined() ? $_ : '' } @$item);
|
2005-04-28 23:38:43 +02:00
|
|
|
}
|
2013-04-21 06:08:43 +02:00
|
|
|
|
2005-04-28 23:38:43 +02:00
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub cmd_kill {
|
|
|
|
my $self = shift;
|
|
|
|
my ($match) = @_;
|
2013-04-21 06:08:43 +02:00
|
|
|
|
2005-04-28 23:38:43 +02:00
|
|
|
return "SYNTAX: KILL (\$IP | \$REF)\n" unless $match;
|
2013-04-21 06:08:43 +02:00
|
|
|
|
2005-04-28 23:38:43 +02:00
|
|
|
my $descriptors = Danga::Socket->DescriptorMap;
|
2013-04-21 06:08:43 +02:00
|
|
|
|
2005-04-28 23:38:43 +02:00
|
|
|
my $killed = 0;
|
|
|
|
my $is_ip = (index($match, '.') >= 0);
|
|
|
|
foreach my $fd (keys %$descriptors) {
|
|
|
|
my $pob = $descriptors->{$fd};
|
|
|
|
if ($pob->isa("Qpsmtpd::PollServer")) {
|
|
|
|
if ($is_ip) {
|
2013-04-21 06:08:43 +02:00
|
|
|
next
|
|
|
|
unless $pob->connection->remote_ip; # haven't even started yet
|
2005-04-28 23:38:43 +02:00
|
|
|
if ($pob->connection->remote_ip eq $match) {
|
2013-04-21 06:08:43 +02:00
|
|
|
$pob->write(
|
|
|
|
"550 Your connection has been killed by an administrator\r\n");
|
2005-04-28 23:38:43 +02:00
|
|
|
$pob->disconnect;
|
|
|
|
$killed++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
# match by ID
|
2013-04-21 06:08:43 +02:00
|
|
|
if ($pob + 0 == hex($match)) {
|
|
|
|
$pob->write(
|
|
|
|
"550 Your connection has been killed by an administrator\r\n");
|
2005-04-28 23:38:43 +02:00
|
|
|
$pob->disconnect;
|
|
|
|
$killed++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-04-21 06:08:43 +02:00
|
|
|
|
2005-04-28 23:38:43 +02:00
|
|
|
return "Killed $killed connection" . ($killed > 1 ? "s" : "") . "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
sub cmd_dump {
|
|
|
|
my $self = shift;
|
|
|
|
my ($ref) = @_;
|
2013-04-21 06:08:43 +02:00
|
|
|
|
2005-04-28 23:38:43 +02:00
|
|
|
return "SYNTAX: DUMP \$REF\n" unless $ref;
|
|
|
|
require Data::Dumper;
|
2013-04-21 06:08:43 +02:00
|
|
|
$Data::Dumper::Indent = 1;
|
|
|
|
|
2005-04-28 23:38:43 +02:00
|
|
|
my $descriptors = Danga::Socket->DescriptorMap;
|
|
|
|
foreach my $fd (keys %$descriptors) {
|
|
|
|
my $pob = $descriptors->{$fd};
|
|
|
|
if ($pob->isa("Qpsmtpd::PollServer")) {
|
2013-04-21 06:08:43 +02:00
|
|
|
if ($pob + 0 == hex($ref)) {
|
2005-04-28 23:38:43 +02:00
|
|
|
return Data::Dumper::Dumper($pob);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-04-21 06:08:43 +02:00
|
|
|
|
2005-04-28 23:38:43 +02:00
|
|
|
return "Unable to find the connection: $ref. Try the LIST command\n";
|
2005-04-26 04:46:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|
|
|
|
__END__
|
|
|
|
|
|
|
|
=head1 NAME
|
|
|
|
|
|
|
|
Qpsmtpd::ConfigServer - a configuration server for qpsmtpd
|
|
|
|
|
|
|
|
=head1 DESCRIPTION
|
|
|
|
|
|
|
|
When qpsmtpd runs in multiplex mode it also provides a config server that you
|
|
|
|
can connect to. This allows you to view current connection statistics and other
|
|
|
|
gumph that you probably don't care about.
|
|
|
|
|
2006-06-27 22:28:36 +02:00
|
|
|
=cut
|