| 1 | package Net::Server::Mail::ESMTP::AUTH; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use base qw(Net::Server::Mail::ESMTP::Extension); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use MIME::Base64; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use vars qw( $VERSION ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | $VERSION = '0.2'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | =head1 NAME | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | Net::Server::Mail::ESMTP::AUTH - SMTP Authentification extensions for Net::Server::Mail::ESMTP | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | use Net::Server::Mail::ESMTP; | ||||||||||||||||||||||||||||
| 18 | my @local_domains = qw(example.com example.org); | ||||||||||||||||||||||||||||
| 19 | my $server = new IO::Socket::INET Listen => 1, LocalPort => 25; | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | my $conn; | ||||||||||||||||||||||||||||
| 22 | while($conn = $server->accept) | ||||||||||||||||||||||||||||
| 23 | { | ||||||||||||||||||||||||||||
| 24 | my $esmtp = new Net::Server::Mail::ESMTP socket => $conn; | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | # activate AUTH extension | ||||||||||||||||||||||||||||
| 27 | $esmtp->register('Net::Server::Mail::ESMTP::AUTH'); | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | # adding AUTH handler | ||||||||||||||||||||||||||||
| 30 | $esmtp->set_callback(AUTH => \&validate_auth); | ||||||||||||||||||||||||||||
| 31 | $esmtp->set_callback(DATA => \&queue_message); | ||||||||||||||||||||||||||||
| 32 | $esmtp->process; | ||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | sub validate_auth | ||||||||||||||||||||||||||||
| 36 | { | ||||||||||||||||||||||||||||
| 37 | my ($session, $username, $password) = @_; | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | if ($username eq 'ROBERT' and $password eq 'TOTO04') { | ||||||||||||||||||||||||||||
| 40 | # AUTH SUCCESFULL | ||||||||||||||||||||||||||||
| 41 | $session->{AUTH}->{ok} = 1; | ||||||||||||||||||||||||||||
| 42 | return 1; | ||||||||||||||||||||||||||||
| 43 | } else { | ||||||||||||||||||||||||||||
| 44 | # AUTH FAILED | ||||||||||||||||||||||||||||
| 45 | return 0; | ||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | sub queue_message { | ||||||||||||||||||||||||||||
| 50 | my($session, $data) = @_; | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | # providing AUTH doesn't make it mandatory. | ||||||||||||||||||||||||||||
| 53 | # A client might not use AUTH at all! | ||||||||||||||||||||||||||||
| 54 | # You must deal now with permissions: | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | unless ($session->{AUTH}->{ok}) { | ||||||||||||||||||||||||||||
| 57 | # warn "no AUTH supplied!"; | ||||||||||||||||||||||||||||
| 58 | return(0, 530, 'Error: Authentication required'); | ||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | ... do stuff | ||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | =head1 FEATURES | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | * AUTH LOGIN method support | ||||||||||||||||||||||||||||
| 66 | * AUTH PLAIN method support | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | "Net::Server::Mail::ESMTP::AUTH" is an extension to provide | ||||||||||||||||||||||||||||
| 71 | ESMTP Authentification support to Net::Server::Mail::ESMTP module. | ||||||||||||||||||||||||||||
| 72 | Actually only AUTH LOGIN and AUTH PLAIN methods are supported. | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | AUTH callback is called with login and password who was given | ||||||||||||||||||||||||||||
| 75 | by user's mail client, AUTH callback should return 1 when authentification | ||||||||||||||||||||||||||||
| 76 | mechanism was succesfull otherwise 0. | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =cut | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 1 | our $verb = 'AUTH'; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | sub init | ||||||||||||||||||||||||||||
| 83 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
init: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 84 | 0 | my ($self, $parent) = @_; | |||||||||||||||||||||||||||
| 85 | 0 | $self->{AUTH} = (); | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | return $self; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | sub verb | ||||||||||||||||||||||||||||
| 91 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
verb: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 92 | 0 | return ( [ 'AUTH' => \&process, ],); | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | sub keyword | ||||||||||||||||||||||||||||
| 96 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
keyword: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 97 | 0 | return 'AUTH LOGIN PLAIN'; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | sub reply | ||||||||||||||||||||||||||||
| 101 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
reply: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 102 | 0 | return ( [ 'AUTH', ]); | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | sub process_authlogin_username | ||||||||||||||||||||||||||||
| 106 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
process_authlogin_username: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 107 | 0 | my ($self, $operation) = @_; | |||||||||||||||||||||||||||
| 108 | 0 | $self->{AUTH}->{username} = decode_base64($operation); | |||||||||||||||||||||||||||
| 109 | 0 | $self->{AUTH}->{password} = ''; | |||||||||||||||||||||||||||
| 110 | 0 | $self->reply(334, "UGFzc3dvcmQ6"); | |||||||||||||||||||||||||||
| 111 | 0 | $self->next_input_to(\&process_authlogin_password); | |||||||||||||||||||||||||||
| 112 | 0 | return (); | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | sub process_authlogin_password | ||||||||||||||||||||||||||||
| 116 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
process_authlogin_password: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 117 | 0 | my ($self, $operation) = @_; | |||||||||||||||||||||||||||
| 118 | 0 | $self->{AUTH}->{password} = decode_base64($operation); | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 0 | return exec_auth_callback($self); | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | sub exec_auth_callback | ||||||||||||||||||||||||||||
| 124 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
exec_auth_callback: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 125 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 0 | my $authok=0; | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 0 | my $ref = $self->{callback}->{AUTH}; | |||||||||||||||||||||||||||
| 130 | 0 | ▶ | if (ref $ref eq 'ARRAY' && ref $ref->[0] eq 'CODE') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $ref eq "ARRAY" and ref $ref->[0] eq "CODE"
MC/DC0%0/2
ref $ref eq "ARRAY" and ref $ref->[0] eq "CODE"
ref $ref eq "ARRAY"
ref $ref->[0] eq "CODE"
Truth table0%0/3
A and B
A= ref $ref eq "ARRAY"B= ref $ref->[0] eq "CODE"
| |||||||||||||||||||||||||||||
| 131 | 0 | my $code = $ref->[0]; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 0 | $authok = &$code($self, $self->{AUTH}->{username}, $self->{AUTH}->{password}); | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 0 | ▶ | if ($authok) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 0 | $self->reply(235, "Authentification successful."); | |||||||||||||||||||||||||||
| 138 | 0 | return (); | |||||||||||||||||||||||||||
| 139 | } else { | ||||||||||||||||||||||||||||
| 140 | 0 | $self->reply(535, "Authentification failed."); | |||||||||||||||||||||||||||
| 141 | 0 | return 1; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | sub process | ||||||||||||||||||||||||||||
| 146 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
process: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 147 | 0 | my ($self, $data) = @_; | |||||||||||||||||||||||||||
| 148 | 0 | ▶ | my ($operation, $param) = $data=~/^(.+?)\s(.*)$/ ? ($1, $2) : ($data, ''); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 0 | $self->{AUTH}->{type} = $operation; | |||||||||||||||||||||||||||
| 151 | 0 | map { $self->{AUTH}->{$_} = '' } ('username', 'password', 'challenge', 'ticket', ); | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 0 | ▶ | if ($operation eq '*') { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 154 | 0 | $self->reply(501, "Authentification aborted."); | |||||||||||||||||||||||||||
| 155 | 0 | return (); | |||||||||||||||||||||||||||
| 156 | } elsif ($operation eq 'PLAIN') { | ||||||||||||||||||||||||||||
| 157 | 0 | $param=decode_base64($param); | |||||||||||||||||||||||||||
| 158 | 0 | my @plaindata = split /\0/, $param; | |||||||||||||||||||||||||||
| 159 | 0 | ▶ | unless (@plaindata > 2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | 0 | $self->reply(535, "Authentification failed."); | |||||||||||||||||||||||||||
| 161 | 0 | return (); | |||||||||||||||||||||||||||
| 162 | } else { | ||||||||||||||||||||||||||||
| 163 | 0 | $self->{AUTH}->{username} = $plaindata[@plaindata-2]; | |||||||||||||||||||||||||||
| 164 | 0 | $self->{AUTH}->{password} = $plaindata[@plaindata-1]; | |||||||||||||||||||||||||||
| 165 | 0 | return exec_auth_callback($self); | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | } elsif ($operation eq 'LOGIN') { | ||||||||||||||||||||||||||||
| 168 | 0 | $param=decode_base64($param); | |||||||||||||||||||||||||||
| 169 | # warn " ==> LOGIN ==> $param\n"; | ||||||||||||||||||||||||||||
| 170 | 0 | $self->reply(334, "VXNlcm5hbWU6"); | |||||||||||||||||||||||||||
| 171 | 0 | $self->next_input_to(\&process_authlogin_username); | |||||||||||||||||||||||||||
| 172 | 0 | return (); | |||||||||||||||||||||||||||
| 173 | } else { | ||||||||||||||||||||||||||||
| 174 | 0 | $self->reply(504, "Unrecognized authentification type."); | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 0 | return (); | |||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | =pod | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | Please, see L<Net::Server::Mail::SMTP> and L<Net::Server::Mail::ESMTP> for | ||||||||||||||||||||||||||||
| 185 | more documentations. | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | Sylvain Cresto E<lt>scresto@gmail.comE<gt> | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | Thanks to Chris E<lt>chris at u- club.deE<gt> | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | =head1 BUGS | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | Please send bug-reports to scresto@gmail.com. | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | =head1 LICENCE | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 200 | it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | Copyright (C) 2004, 2016 - Sylvain Cresto | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | =cut | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 1 | 1; | |||||||||||||||||||||||||||