/usr/share/perl5/Net/Server/Mail/SMTP.pm is in libnet-server-mail-perl 0.23-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 | package Net::Server::Mail::SMTP;
use 5.006;
use strict;
use base 'Net::Server::Mail';
our $VERSION = "0.23";
=pod
=head1 NAME
Net::Server::Mail::SMTP - A module to implement the SMTP protocol
=head1 SYNOPSIS
use Net::Server::Mail::SMTP;
my @local_domains = qw(example.com example.org);
my $server = IO::Socket::INET->new( Listen => 1, LocalPort => 25 );
my $conn;
while($conn = $server->accept)
{
my $smtp = Net::Server::Mail::SMTP->new( socket => $conn );
$smtp->set_callback(RCPT => \&validate_recipient);
$smtp->set_callback(DATA => \&queue_message);
$smtp->process();
$conn->close();
}
sub validate_recipient
{
my($session, $recipient) = @_;
my $domain;
if($recipient =~ /@(.*)>\s*$/)
{
$domain = $1;
}
if(not defined $domain)
{
return(0, 513, 'Syntax error.');
}
elsif(not(grep $domain eq $_, @local_domains))
{
return(0, 554, "$recipient: Recipient address rejected: Relay access denied");
}
return(1);
}
sub queue_message
{
my($session, $data) = @_;
my $sender = $session->get_sender();
my @recipients = $session->get_recipients();
return(0, 554, 'Error: no valid recipients')
unless(@recipients);
my $msgid = add_queue($sender, \@recipients, $data)
or return(0);
return(1, 250, "message queued $msgid");
}
=head1 DESCRIPTION
This class implement the SMTP (RFC 821) protocol. Notice that it don't
implement the extension mechanism introduce in RFC 2821. You have to
use Net::Server::Mail::ESMTP if you want this capability.
This class inherit from Net::Server::Mail. Please see
L<Net::Server::Mail> for documentation of common methods.
=head1 METHODS
SMTP specific methods.
=cut
sub init {
my ( $self, @args ) = @_;
my $rv = $self->SUPER::init(@args);
return $rv unless $rv eq $self;
$self->def_verb( HELO => 'helo' );
$self->def_verb( VRFY => 'vrfy' );
$self->def_verb( EXPN => 'expn' );
$self->def_verb( TURN => 'turn' );
$self->def_verb( HELP => 'help' );
$self->def_verb( NOOP => 'noop' );
$self->def_verb( MAIL => 'mail' );
$self->def_verb( RCPT => 'rcpt' );
$self->def_verb( SEND => 'send' );
$self->def_verb( SOML => 'soml' );
$self->def_verb( SAML => 'saml' );
$self->def_verb( DATA => 'data' );
$self->def_verb( RSET => 'rset' );
$self->def_verb( QUIT => 'quit' );
# go to the initial step
$self->step_reverse_path(0);
$self->step_forward_path(0);
$self->step_maildata_path(0);
# handle data after the end of data indicator (.)
$self->{data_handle_more_data} = 0;
return $self;
}
sub step_reverse_path {
my ( $self, $bool ) = @_;
if ( defined $bool ) {
$self->{reverse_path} = $bool;
}
return $self->{reverse_path};
}
sub step_forward_path {
my ( $self, $bool ) = @_;
if ( defined $bool ) {
$self->{forward_path} = $bool;
}
return $self->{forward_path};
}
sub step_maildata_path {
my ( $self, $bool ) = @_;
if ( defined $bool ) {
$self->{maildata_path} = $bool;
# initialise data container
if ( not $bool ) {
$self->{_data} = '';
}
}
return $self->{maildata_path};
}
sub get_protoname {
return 'SMTP';
}
=pod
=head2 get_sender
Returns the sender of the current session. Return undefined if the
reverse path step is not complete.
=cut
sub get_sender {
my ($self) = @_;
my $sender = $self->step_reverse_path();
return ( $sender ? $sender : undef );
}
=pod
=head2 get_recipients
Returns the list of recipients supplied by client. Returns undef if
forward_path step is not engaged. Returns an empty list if not
recipients succeed.
=cut
sub get_recipients {
my ($self) = @_;
my $recipients = $self->step_forward_path();
return ( ref $recipients ? @$recipients : undef );
}
=pod
=head1 EVENTS
Descriptions of callback who's can be used with set_callback
method. All handle takes the Net::Server::Mail::SMTP object as first
argument and specific callback's arguments.
=head2 HELO
Takes the hostname given as argument. Engage the reverse path step on
success.
sub helo_handle
{
my($session, $hostname) = @_;
if($hostname eq 'localhost')
{
return(0, 553, q(I don't like this hostname, try again.));
}
# don't forgot to return a success reply if you are happy with
# command's value
return 1;
}
=cut
sub helo {
my ( $self, $hostname ) = @_;
unless ( defined $hostname && length $hostname ) {
$self->reply( 501, 'Syntax error in parameters or arguments' );
return;
}
$self->make_event(
name => 'HELO',
arguments => [$hostname],
on_success => sub {
# according to the RFC, HELO ensures "that both the SMTP client
# and the SMTP server are in the initial state"
$self->step_reverse_path(1);
$self->step_forward_path(0);
$self->step_maildata_path(0);
},
success_reply => [ 250, 'Requested mail action okay, completed' ],
);
return;
}
=pod
=head2 NOOP
This handler takes no argument
=cut
sub noop {
my ($self) = @_;
$self->make_event( name => 'NOOP' );
return;
}
=pod
=head2 EXPN
Command not yet implemented.
Handler takes address as argument.
=cut
sub expn {
my ( $self, $address ) = @_;
$self->make_event(
name => 'EXPN',
arguments => [$address],
default_reply => [ 502, 'Command not implemented' ]
);
return;
}
=pod
=head2 EXPN
Command not implemented, deprecated by RFC 2821
Handler takes no argument.
=cut
sub turn {
# deprecated in RFC 2821
my ($self) = @_;
$self->reply( 502, 'Command not implemented' );
$self->make_event(
name => 'TURN',
default_reply => [ 502, 'Command not implemented' ]
);
return;
}
=pod
=head2 VRFY
Command not yet implemented.
Handler takes address as argument.
=cut
sub vrfy {
my ( $self, $address ) = @_;
$self->make_event(
name => 'VRFY',
arguments => [$address],
default_reply => [ 502, 'Command not implemented' ]
);
return;
}
=pod
=head2 HELP
Command not yet implemented.
Handler takes a command name as argument.
=cut
sub help {
my ( $self, $command ) = @_;
$self->make_event(
name => 'HELP',
arguments => [$command],
default_reply => [ 502, 'Command not implemented' ]
);
return;
}
=pod
=head2 MAIL
Handler takes address as argument. On success, engage the forward path
step and keep the given address for later use (get it with
get_sender() method).
=cut
sub mail {
my ( $self, $args ) = @_;
unless ( $self->step_reverse_path ) {
$self->reply( 503, 'Bad sequence of commands' );
return;
}
unless ( $args =~ s/^from:\s*//i ) {
$self->reply( 501, 'Syntax error in parameters or arguments' );
return;
}
if ( $self->step_forward_path ) {
$self->reply( 503, 'Bad sequence of commands' );
return;
}
my ( $address, $rest, @options );
unless ( ( $address, $rest ) = $args =~ /^<(.*?)>(?: (\S.*))?$/ ) {
$self->reply( 501, 'Syntax error in parameters or arguments' );
return;
}
if ($rest) {
@options = split ' ', $rest;
}
unless ( $self->handle_options( 'MAIL', $address, @options ) ) {
return;
}
$self->make_event(
name => 'MAIL',
arguments => [$address],
on_success => sub {
$self->step_reverse_path($address);
$self->step_forward_path(1);
},
success_reply => [ 250, "sender $address OK" ],
failure_reply => [ 550, 'Failure' ],
);
return;
}
=pod
=head2 RCPT
Handler takes address as argument. On success, engage the mail data path step and
push the given address into the recipient list for later use (get it
with get_recipients() method).
=cut
sub rcpt {
my ( $self, $args ) = @_;
unless ( $self->step_forward_path ) {
$self->reply( 503, 'Bad sequence of commands' );
return;
}
unless ( $args =~ s/^to:\s*//i ) {
$self->reply( 501, 'Syntax error in parameters or arguments' );
return;
}
my ( $address, $rest, @options );
unless ( ( $address, $rest ) = $args =~ /^<(.*?)>(?: (\S.*))?$/ ) {
$self->reply( 501, 'Syntax error in parameters or arguments' );
return;
}
if ($rest) {
@options = split ' ', $rest;
}
unless ( $self->handle_options( 'RCPT', $address, @options ) ) {
return;
}
$self->make_event(
name => 'RCPT',
arguments => [$address],
on_success => sub {
my $buffer = $self->step_forward_path();
$buffer = [] unless ref $buffer eq 'ARRAY';
push( @$buffer, $address );
$self->step_forward_path($buffer);
$self->step_maildata_path(1);
},
success_reply => [ 250, "recipient $address OK" ],
failure_reply => [ 550, 'Failure' ],
);
return;
}
=pod
=head2 SEND
Command not implemented.
Handler takes no argument.
=cut
# we overwrite a perl command... we shouldn't need it in this class,
# but take care.
sub send {
my ($self) = @_;
$self->make_event(
name => 'SEND',
default_reply => [ 502, 'Command not implemented' ]
);
return;
}
=pod
=head2 SOML
Command not implemented.
Handler takes no argument.
=cut
sub soml {
my ($self) = @_;
$self->make_event(
name => 'SOML',
default_reply => [ 502, 'Command not implemented' ]
);
return;
}
=pod
=head2 SAML
Command not implemented.
Handler takes no argument.
=cut
sub saml {
my ($self) = @_;
$self->make_event(
name => 'SAML',
default_reply => [ 502, 'Command not implemented' ]
);
return;
}
=pod
=head2 DATA
This handler is called after the final . sent by client. It takes data
as argument in a scalar reference. You should queue the message and
reply with the queue ID.
=head2 DATA-INIT
This handler is called before enter in the "waiting for data" step. The
default success reply is a 354 code telling the client to send the
mail content.
=head2 DATA-PART
This handler is called at each parts of mail content sent. It takes as
argument a scalar reference to the part of data received. It is
deprecated to change the contents of this scalar.
=cut
sub data {
my ( $self, $args ) = @_;
unless ( $self->step_maildata_path ) {
$self->reply( 503, 'Bad sequence of commands' );
return;
}
if ( defined $args && length $args ) {
$self->reply( 501, 'Syntax error in parameters or arguments' );
return;
}
$self->{last_chunk} = '';
$self->make_event(
name => 'DATA-INIT',
on_success => sub { $self->next_input_to( \&data_part ); },
success_reply => [ 354, 'Start mail input; end with <CRLF>.<CRLF>' ]
);
return;
}
# Because data is cut into pieces (4096 bytes), we have to search
# "\r\n.\r\n" sequence in 2 consecutive pieces. $self->{last_chunk}
# contains the last 5 bytes.
sub data_part {
my ( $self, $data ) = @_;
# search for end of data indicator
$data ||= '';
if ( "$self->{last_chunk}$data" =~ /\r?\n\.\r?\n/s ) {
my $more_data = $';
if ( length $more_data ) {
# Client sent a command after the end of data indicator ".".
if ( !$self->{data_handle_more_data} ) {
$self->reply( 453,
"Command received prior to completion of"
. " previous command sequence" );
return;
}
}
# RFC 821 compliance.
( $data = "$self->{last_chunk}$data" ) =~
s/(\r?\n)\.\r?\n(QUIT\r?\n)?$/$1/s;
$self->{_data} .= $data;
# RFC 2821 by the letter
$self->{_data} =~ s/^\.(.+\015\012)(?!\n)/$1/gm;
return $self->data_finished($more_data);
}
my $tmp = $self->{last_chunk};
$self->{last_chunk} = substr $data, -5;
$data = $tmp . ( $data ? substr( $data, 0, -5 ) : '' );
$self->make_event(
name => 'DATA-PART',
arguments => [ \$data ],
on_success => sub {
$self->{_data} .= $data;
# please, recall me soon !
$self->next_input_to( \&data_part );
},
success_reply => '', # don't send any reply !
);
return;
}
sub data_finished {
my ( $self, $more_data ) = @_;
$self->make_event(
name => 'DATA',
arguments => [ \$self->{_data} ],
success_reply => [ 250, 'message sent' ],
);
# reinitiate the connection
$self->step_reverse_path(1);
$self->step_forward_path(0);
$self->step_maildata_path(0);
# if more data, handle it
if ($more_data) {
return $self->{process_operation}( $self, $more_data );
}
else {
return;
}
}
=pod
=head2 RSET
Handler takes no argument.
On success, all step are initialized and sender/recipients list are
flushed.
=cut
sub rset {
my ($self) = @_;
$self->make_event(
name => 'RSET',
on_success => sub {
$self->step_reverse_path(1)
if ( $self->step_reverse_path() );
$self->step_forward_path(0);
$self->step_maildata_path(0);
},
success_reply => [ 250, 'Requested mail action okay, completed' ],
);
return;
}
=pod
=head2 QUIT
Handler takes no argument.
Connection is closed after this command. This behavior may change in
future, you will probably be able to control the closing of
connection.
=cut
sub quit {
my ($self) = @_;
$self->make_event(
name => 'QUIT',
success_reply => [
221, $self->get_hostname . ' Service closing transmission channel'
],
);
return 1; # close cnx
}
##########################################################################
sub handle_options {
# handle options for verb MAIL and RCPT
my ( $self, $verb, $address, @options ) = @_;
if (@options) {
$self->reply( 555, "Unsupported option: $options[0]" );
return 0;
}
return 1;
}
=pod
=head1 SEE ALSO
Please, see L<Net::Server::Mail>, L<Net::Server::Mail::ESMTP>
and L<Net::Server::Mail::LMTP>.
=head1 AUTHOR
Olivier Poitrey E<lt>rs@rhapsodyk.netE<gt>
=head1 AVAILABILITY
Available on CPAN.
anonymous Git repository:
git clone git://github.com/rs/net-server-mail.git
Git repository on the web:
L<https://github.com/rs/net-server-mail>
=head1 BUGS
Please use CPAN system to report a bug (http://rt.cpan.org/).
=head1 LICENCE
This library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA
=head1 COPYRIGHT
Copyright (C) 2002 - Olivier Poitrey, 2007 - Xavier Guimard
=cut
1;
|