2006-04-07 20:58:02 +02:00
|
|
|
package Qpsmtpd::Command;
|
|
|
|
|
|
|
|
=head1 NAME
|
|
|
|
|
|
|
|
Qpsmtpd::Command - parse arguments to SMTP commands
|
|
|
|
|
|
|
|
=head1 DESCRIPTION
|
|
|
|
|
|
|
|
B<Qpsmtpd::Command> provides just one public sub routine: B<parse()>.
|
|
|
|
|
|
|
|
This sub expects two or three arguments. The first is the name of the
|
|
|
|
SMTP command (such as I<HELO>, I<MAIL>, ...). The second must be the remaining
|
|
|
|
of the line the client sent.
|
|
|
|
|
|
|
|
If no third argument is given (or it's not a reference to a CODE) it parses
|
|
|
|
the line according to RFC 1869 (SMTP Service Extensions) for the I<MAIL> and
|
|
|
|
I<RCPT> commands and splitting by spaces (" ") for all other.
|
|
|
|
|
|
|
|
Any module can supply it's own parsing routine by returning a sub routine
|
|
|
|
reference from a hook_*_parse. This sub will be called with I<$self>, I<$cmd>
|
|
|
|
and I<$line>.
|
|
|
|
|
|
|
|
On successfull parsing it MUST return B<OK> (the constant from
|
|
|
|
I<Qpsmtpd::Constants>) success as first argument and a list of
|
|
|
|
values, which will be the arguments to the hook for this command.
|
|
|
|
|
|
|
|
If parsing failed, the second returned value (if any) will be returned to the
|
|
|
|
client as error message.
|
|
|
|
|
|
|
|
=head1 EXAMPLE
|
|
|
|
|
|
|
|
Inside a plugin
|
|
|
|
|
|
|
|
sub hook_unrecognized_command_parse {
|
|
|
|
my ($self, $transaction, $cmd) = @_;
|
2014-09-18 07:22:17 +02:00
|
|
|
if ($cmd eq 'bdat') {
|
|
|
|
return OK, \&bdat_parser;
|
|
|
|
};
|
2006-04-07 20:58:02 +02:00
|
|
|
}
|
2007-09-03 17:47:08 +02:00
|
|
|
|
2006-04-07 20:58:02 +02:00
|
|
|
sub bdat_parser {
|
|
|
|
my ($self,$cmd,$line) = @_;
|
|
|
|
# .. do something with $line...
|
2014-09-18 07:22:17 +02:00
|
|
|
if ($some_reason_why_there_is_a_syntax_error) {
|
|
|
|
return DENY, "Invalid arguments";
|
|
|
|
};
|
2014-09-18 03:28:51 +02:00
|
|
|
return OK, @args;
|
2006-04-07 20:58:02 +02:00
|
|
|
}
|
2007-09-03 17:47:08 +02:00
|
|
|
|
2006-04-07 20:58:02 +02:00
|
|
|
sub hook_unrecognized_command {
|
|
|
|
my ($self, $transaction, $cmd, @args) = @_;
|
2014-09-18 03:28:51 +02:00
|
|
|
return DECLINED if $self->qp->connection->hello eq 'helo';
|
|
|
|
return DECLINED if $cmd ne 'bdat';
|
2006-04-07 20:58:02 +02:00
|
|
|
....
|
|
|
|
}
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
2012-05-04 22:06:19 +02:00
|
|
|
use strict;
|
|
|
|
|
2006-04-07 20:58:02 +02:00
|
|
|
use Qpsmtpd::Constants;
|
|
|
|
use vars qw(@ISA);
|
|
|
|
@ISA = qw(Qpsmtpd::SMTP);
|
|
|
|
|
|
|
|
sub parse {
|
2013-04-21 06:08:43 +02:00
|
|
|
my ($me, $cmd, $line, $sub) = @_;
|
2014-09-18 03:28:51 +02:00
|
|
|
return OK if !defined $line; # trivial case
|
2006-04-07 20:58:02 +02:00
|
|
|
my $self = {};
|
|
|
|
bless $self, $me;
|
2006-12-08 20:37:47 +01:00
|
|
|
$cmd = lc $cmd;
|
2006-04-07 20:58:02 +02:00
|
|
|
if ($sub and (ref($sub) eq 'CODE')) {
|
|
|
|
my @ret = eval { $sub->($self, $cmd, $line); };
|
|
|
|
if ($@) {
|
|
|
|
$self->log(LOGERROR, "Failed to parse command [$cmd]: $@");
|
2014-09-18 03:28:51 +02:00
|
|
|
return DENY, $line;
|
2006-04-07 20:58:02 +02:00
|
|
|
}
|
|
|
|
## my @log = @ret;
|
2014-09-18 07:22:17 +02:00
|
|
|
## for (@log) { $_ ||= ""; }
|
2006-04-07 20:58:02 +02:00
|
|
|
## $self->log(LOGDEBUG, "parse($cmd) => [".join("], [", @log)."]");
|
|
|
|
return @ret;
|
2013-04-21 06:08:43 +02:00
|
|
|
}
|
2006-04-07 20:58:02 +02:00
|
|
|
my $parse = "parse_$cmd";
|
|
|
|
if ($self->can($parse)) {
|
2013-04-21 06:08:43 +02:00
|
|
|
|
2006-04-07 20:58:02 +02:00
|
|
|
# print "CMD=$cmd,line=$line\n";
|
|
|
|
my @out = eval { $self->$parse($cmd, $line); };
|
|
|
|
if ($@) {
|
|
|
|
$self->log(LOGERROR, "$parse($cmd,$line) failed: $@");
|
2014-09-18 03:28:51 +02:00
|
|
|
return DENY, "Failed to parse line";
|
2006-04-07 20:58:02 +02:00
|
|
|
}
|
|
|
|
return @out;
|
|
|
|
}
|
2014-09-18 03:28:51 +02:00
|
|
|
return OK, split(/ +/, $line); # default :)
|
2006-04-07 20:58:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub parse_rcpt {
|
2013-04-21 06:08:43 +02:00
|
|
|
my ($self, $cmd, $line) = @_;
|
2014-09-18 07:22:17 +02:00
|
|
|
if ($line !~ s/^to:\s*//i) {
|
|
|
|
return DENY, "Syntax error in command";
|
|
|
|
};
|
|
|
|
return _get_mail_params($cmd, $line);
|
2006-04-07 20:58:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub parse_mail {
|
2013-04-21 06:08:43 +02:00
|
|
|
my ($self, $cmd, $line) = @_;
|
2014-09-18 07:22:17 +02:00
|
|
|
if ($line !~ s/^from:\s*//i) {
|
|
|
|
return DENY, "Syntax error in command";
|
|
|
|
};
|
|
|
|
return _get_mail_params($cmd, $line);
|
2006-04-07 20:58:02 +02:00
|
|
|
}
|
|
|
|
### RFC 1869:
|
|
|
|
## 6. MAIL FROM and RCPT TO Parameters
|
|
|
|
## [...]
|
|
|
|
##
|
|
|
|
## esmtp-cmd ::= inner-esmtp-cmd [SP esmtp-parameters] CR LF
|
|
|
|
## esmtp-parameters ::= esmtp-parameter *(SP esmtp-parameter)
|
|
|
|
## esmtp-parameter ::= esmtp-keyword ["=" esmtp-value]
|
|
|
|
## esmtp-keyword ::= (ALPHA / DIGIT) *(ALPHA / DIGIT / "-")
|
|
|
|
##
|
|
|
|
## ; syntax and values depend on esmtp-keyword
|
|
|
|
## esmtp-value ::= 1*<any CHAR excluding "=", SP, and all
|
|
|
|
## control characters (US ASCII 0-31
|
|
|
|
## inclusive)>
|
|
|
|
##
|
|
|
|
## ; The following commands are extended to
|
|
|
|
## ; accept extended parameters.
|
|
|
|
## inner-esmtp-cmd ::= ("MAIL FROM:" reverse-path) /
|
|
|
|
## ("RCPT TO:" forward-path)
|
|
|
|
sub _get_mail_params {
|
2013-04-21 06:08:43 +02:00
|
|
|
my ($cmd, $line) = @_;
|
2006-04-07 20:58:02 +02:00
|
|
|
my @params = ();
|
|
|
|
$line =~ s/\s*$//;
|
|
|
|
|
|
|
|
while ($line =~ s/\s+([A-Za-z0-9][A-Za-z0-9\-]*(=[^= \x00-\x1f]+)?)$//) {
|
|
|
|
push @params, $1;
|
|
|
|
}
|
|
|
|
@params = reverse @params;
|
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
# the above will "fail" (i.e. all of the line in @params) on
|
2006-04-07 20:58:02 +02:00
|
|
|
# some addresses without <> like
|
|
|
|
# MAIL FROM: user=name@example.net
|
|
|
|
# or RCPT TO: postmaster
|
|
|
|
|
|
|
|
# let's see if $line contains nothing and use the first value as address:
|
|
|
|
if ($line) {
|
2013-04-21 06:08:43 +02:00
|
|
|
|
|
|
|
# parameter syntax error, i.e. not all of the arguments were
|
2006-04-07 20:58:02 +02:00
|
|
|
# stripped by the while() loop:
|
2014-09-18 07:22:17 +02:00
|
|
|
if ($line =~ /\@.*\s/) {
|
|
|
|
return DENY, "Syntax error in parameters";
|
|
|
|
};
|
2014-09-18 03:28:51 +02:00
|
|
|
return OK, $line, @params;
|
2006-04-07 20:58:02 +02:00
|
|
|
}
|
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
$line = shift @params;
|
2014-09-18 07:22:17 +02:00
|
|
|
if ($cmd eq 'mail') {
|
|
|
|
return OK, '<>' if !$line; # 'MAIL FROM:' --> 'MAIL FROM:<>'
|
|
|
|
if ($line =~ /\@.*\s/) {
|
|
|
|
return DENY, "Syntax error in parameters";
|
2006-04-07 20:58:02 +02:00
|
|
|
}
|
2014-09-18 07:22:17 +02:00
|
|
|
return OK, $line, @params;
|
2006-04-07 20:58:02 +02:00
|
|
|
}
|
2014-09-18 07:22:17 +02:00
|
|
|
|
|
|
|
if ($line =~ /\@/) {
|
|
|
|
return DENY, "Syntax error in parameters" if $line =~ /\@.*\s/;
|
|
|
|
return OK, $line, @params;
|
|
|
|
}
|
|
|
|
|
|
|
|
return DENY, "Syntax error in parameters" if $line =~ /\s/;
|
|
|
|
return DENY, "Syntax error in address" if $line !~ /^(postmaster|abuse)$/i;
|
2014-09-18 03:28:51 +02:00
|
|
|
return OK, $line, @params;
|
2006-04-07 20:58:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|