Debian Codegraph @

lib/Qpsmtpd/Command.pm

raw
1 package Qpsmtpd::Command;
2
3 =head1 NAME
4
5 Qpsmtpd::Command - parse arguments to SMTP commands
6
7 =head1 DESCRIPTION
8
9 B<Qpsmtpd::Command> provides just one public sub routine: B<parse()>.
10
11 This sub expects two or three arguments. The first is the name of the
12 SMTP command (such as I<HELO>, I<MAIL>, ...). The second must be the remaining
13 of the line the client sent.
14
15 If no third argument is given (or it's not a reference to a CODE) it parses
16 the line according to RFC 1869 (SMTP Service Extensions) for the I<MAIL> and
17 I<RCPT> commands and splitting by spaces (" ") for all other.
18
19 Any module can supply it's own parsing routine by returning a sub routine
20 reference from a hook_*_parse. This sub will be called with I<$self>, I<$cmd>
21 and I<$line>.
22
23 On successfull parsing it MUST return B<OK> (the constant from
24 I<Qpsmtpd::Constants>) success as first argument and a list of
25 values, which will be the arguments to the hook for this command.
26
27 If parsing failed, the second returned value (if any) will be returned to the
28 client as error message.
29
30 =head1 EXAMPLE
31
32 Inside a plugin
33
34 sub hook_unrecognized_command_parse {
35 my ($self, $transaction, $cmd) = @_;
36 return (OK, \&bdat_parser) if ($cmd eq 'bdat');
37 }
38
39 sub bdat_parser {
40 my ($self,$cmd,$line) = @_;
41 # .. do something with $line...
42 return (DENY, "Invalid arguments")
43 if $some_reason_why_there_is_a_syntax_error;
44 return (OK, @args);
45 }
46
47 sub hook_unrecognized_command {
48 my ($self, $transaction, $cmd, @args) = @_;
49 return (DECLINED) if ($self->qp->connection->hello eq 'helo');
50 return (DECLINED) unless ($cmd eq 'bdat');
51 ....
52 }
53
54 =cut
55
56 use strict;
57
58 use Qpsmtpd::Constants;
59 use vars qw(@ISA);
60 @ISA = qw(Qpsmtpd::SMTP);
61
62 sub parse {
63 my ($me, $cmd, $line, $sub) = @_;
64 return (OK) unless defined $line; # trivial case
65 my $self = {};
66 bless $self, $me;
67 $cmd = lc $cmd;
68 if ($sub and (ref($sub) eq 'CODE')) {
69 my @ret = eval { $sub->($self, $cmd, $line); };
70 if ($@) {
71 $self->log(LOGERROR, "Failed to parse command [$cmd]: $@");
72 return (DENY, $line, ());
73 }
74 ## my @log = @ret;
75 ## for (@log) {
76 ## $_ ||= "";
77 ## }
78 ## $self->log(LOGDEBUG, "parse($cmd) => [".join("], [", @log)."]");
79 return @ret;
80 }
81 my $parse = "parse_$cmd";
82 if ($self->can($parse)) {
83
84 # print "CMD=$cmd,line=$line\n";
85 my @out = eval { $self->$parse($cmd, $line); };
86 if ($@) {
87 $self->log(LOGERROR, "$parse($cmd,$line) failed: $@");
88 return (DENY, "Failed to parse line");
89 }
90 return @out;
91 }
92 return (OK, split(/ +/, $line)); # default :)
93 }
94
95 sub parse_rcpt {
96 my ($self, $cmd, $line) = @_;
97 return (DENY, "Syntax error in command") unless $line =~ s/^to:\s*//i;
98 return &_get_mail_params($cmd, $line);
99 }
100
101 sub parse_mail {
102 my ($self, $cmd, $line) = @_;
103 return (DENY, "Syntax error in command") unless $line =~ s/^from:\s*//i;
104 return &_get_mail_params($cmd, $line);
105 }
106 ### RFC 1869:
107 ## 6. MAIL FROM and RCPT TO Parameters
108 ## [...]
109 ##
110 ## esmtp-cmd ::= inner-esmtp-cmd [SP esmtp-parameters] CR LF
111 ## esmtp-parameters ::= esmtp-parameter *(SP esmtp-parameter)
112 ## esmtp-parameter ::= esmtp-keyword ["=" esmtp-value]
113 ## esmtp-keyword ::= (ALPHA / DIGIT) *(ALPHA / DIGIT / "-")
114 ##
115 ## ; syntax and values depend on esmtp-keyword
116 ## esmtp-value ::= 1*<any CHAR excluding "=", SP, and all
117 ## control characters (US ASCII 0-31
118 ## inclusive)>
119 ##
120 ## ; The following commands are extended to
121 ## ; accept extended parameters.
122 ## inner-esmtp-cmd ::= ("MAIL FROM:" reverse-path) /
123 ## ("RCPT TO:" forward-path)
124 sub _get_mail_params {
125 my ($cmd, $line) = @_;
126 my @params = ();
127 $line =~ s/\s*$//;
128
129 while ($line =~ s/\s+([A-Za-z0-9][A-Za-z0-9\-]*(=[^= \x00-\x1f]+)?)$//) {
130 push @params, $1;
131 }
132 @params = reverse @params;
133
134 # the above will "fail" (i.e. all of the line in @params) on
135 # some addresses without <> like
136 # MAIL FROM: [email protected]
137 # or RCPT TO: postmaster
138
139 # let's see if $line contains nothing and use the first value as address:
140 if ($line) {
141
142 # parameter syntax error, i.e. not all of the arguments were
143 # stripped by the while() loop:
144 return (DENY, "Syntax error in parameters")
145 if ($line =~ /\@.*\s/);
146 return (OK, $line, @params);
147 }
148
149 $line = shift @params;
150 if ($cmd eq "mail") {
151 return (OK, "<>") unless $line; # 'MAIL FROM:' --> 'MAIL FROM:<>'
152 return (DENY, "Syntax error in parameters")
153 if ($line =~ /\@.*\s/); # parameter syntax error
154 }
155 else {
156 if ($line =~ /\@/) {
157 return (DENY, "Syntax error in parameters")
158 if ($line =~ /\@.*\s/);
159 }
160 else {
161 # XXX: what about 'abuse' in Qpsmtpd::Address?
162 return (DENY, "Syntax error in parameters") if $line =~ /\s/;
163 return (DENY, "Syntax error in address")
164 unless ($line =~ /^(postmaster|abuse)$/i);
165 }
166 }
167 ## XXX: No: let this do a plugin, so it's not up to us to decide
168 ## if we require <> around an address :-)
169 ## unless ($line =~ /^<.*>$/) { $line = "<".$line.">"; }
170 return (OK, $line, @params);
171 }
172
173 1;