| 1 | package Mail::POP3::Server; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use IO::Socket; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use IO::File; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use POSIX; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use IO::Select; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | =head2 POP3 command-handlers: | ||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | =item commandUSER | ||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | =item commandPASS | ||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | =item commandLIST | ||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | =item commandSTAT | ||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | =item commandRETR | ||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | =item commandDELE | ||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | =item commandRSET | ||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | =item commandQUIT | ||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | =item commandNOOP | ||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | =item commandUIDL | ||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | =item commandTOP | ||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | # These are the only commands accepted | ||||||||||||||||||||||||||||||||||||||||||
| 40 | 4 | my %COMMAND2FUNC = ( | |||||||||||||||||||||||||||||||||||||||||
| 41 | USER => \&commandUSER, | ||||||||||||||||||||||||||||||||||||||||||
| 42 | PASS => \&commandPASS, | ||||||||||||||||||||||||||||||||||||||||||
| 43 | LIST => \&commandLIST, | ||||||||||||||||||||||||||||||||||||||||||
| 44 | STAT => \&commandSTAT, | ||||||||||||||||||||||||||||||||||||||||||
| 45 | RETR => \&commandRETR, | ||||||||||||||||||||||||||||||||||||||||||
| 46 | DELE => \&commandDELE, | ||||||||||||||||||||||||||||||||||||||||||
| 47 | RSET => \&commandRSET, | ||||||||||||||||||||||||||||||||||||||||||
| 48 | QUIT => \&commandQUIT, | ||||||||||||||||||||||||||||||||||||||||||
| 49 | NOOP => \&commandNOOP, | ||||||||||||||||||||||||||||||||||||||||||
| 50 | UIDL => \&commandUIDL, | ||||||||||||||||||||||||||||||||||||||||||
| 51 | TOP => \&commandTOP, | ||||||||||||||||||||||||||||||||||||||||||
| 52 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 53 | 4 | my %COMMAND2OKNOLOGIN = ( | |||||||||||||||||||||||||||||||||||||||||
| 54 | USER => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 55 | PASS => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 56 | QUIT => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 57 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 58 | 4 | my $CRLF = "\015\012"; | |||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | =head2 new | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | Takes config hash-ref. | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | 2 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 2 | my ($class, $config) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 68 | 2 | my $self = {}; | |||||||||||||||||||||||||||||||||||||||||
| 69 | 2 | bless $self, $class; | |||||||||||||||||||||||||||||||||||||||||
| 70 | 2 | $self->{CONFIG} = $config; | |||||||||||||||||||||||||||||||||||||||||
| 71 | 2 | $self->{CLIENT_CRYPT_PASSWD} = ''; | |||||||||||||||||||||||||||||||||||||||||
| 72 | 2 | $self->{MAILDIR} = ""; | |||||||||||||||||||||||||||||||||||||||||
| 73 | 2 | $self->{RECEIVED_HEADER} = ""; # these two are set in commandUSER | |||||||||||||||||||||||||||||||||||||||||
| 74 | 2 | $self->{ADDEDBYTES} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 75 | 2 | $self->{MAILBOX_OPENED} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 76 | 2 | $self->{PASSWORD_GIVEN} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 77 | 2 | $self->{AUTH_TYPE} = $self->{CONFIG}->{auth_type}; | |||||||||||||||||||||||||||||||||||||||||
| 78 | 2 | $self; | |||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | 6 | ▶ | sub _make_closure { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_make_closure: called CC=1 SCAR=0.4
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | 6 | my ($self, $subref) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | sub { $subref->($self) }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | =head2 start | ||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | Takes input, output filehandles, and client IP. | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | # Do the security checks and then get the first command | ||||||||||||||||||||||||||||||||||||||||||
| 93 | 1 | ▶ | sub start { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
start: called CC=19 SCAR=32.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 1 | my ($self, $input_fh, $output_fh, $client_ip) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 95 | 1 | $self->{CLIENT_IP} = $client_ip; | |||||||||||||||||||||||||||||||||||||||||
| 96 | 1 | $self->{CLIENT_FQDN} = $self->peer_lookup($self->{CLIENT_IP}); | |||||||||||||||||||||||||||||||||||||||||
| 97 | 1 | $self->{INPUT_FH} = $input_fh; | |||||||||||||||||||||||||||||||||||||||||
| 98 | 1 | $self->{OUTPUT_FH} = $output_fh; | |||||||||||||||||||||||||||||||||||||||||
| 99 | # Set the default output file handle | ||||||||||||||||||||||||||||||||||||||||||
| 100 | 1 | select $self->{OUTPUT_FH}; | |||||||||||||||||||||||||||||||||||||||||
| 101 | 1 | $| = 1; | |||||||||||||||||||||||||||||||||||||||||
| 102 | # Try and catch anything nasty and restore mailbox. This can | ||||||||||||||||||||||||||||||||||||||||||
| 103 | # lead to emails being downloaded more than once but at least they | ||||||||||||||||||||||||||||||||||||||||||
| 104 | # shouldn't be lost. | ||||||||||||||||||||||||||||||||||||||||||
| 105 | 1 | local $SIG{HUP} = $self->_make_closure(\&force_shutdown); | |||||||||||||||||||||||||||||||||||||||||
| 106 | 1 | local $SIG{TERM} = $self->_make_closure(\&force_shutdown); | |||||||||||||||||||||||||||||||||||||||||
| 107 | 1 | local $SIG{PIPE} = $self->_make_closure(\&force_shutdown); | |||||||||||||||||||||||||||||||||||||||||
| 108 | 1 | ▶ | local $SIG{USR1} = $self->_make_closure(\&force_shutdown) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | unless $^O =~ /MSWin32/; | ||||||||||||||||||||||||||||||||||||||||||
| 110 | 1 | local $SIG{SEGV} = $self->_make_closure(\&force_shutdown); | |||||||||||||||||||||||||||||||||||||||||
| 111 | # Catch kernel alarms and close gracefully if the client stalls | ||||||||||||||||||||||||||||||||||||||||||
| 112 | 1 | local $SIG{ALRM} = $self->_make_closure(\&force_shutdown); | |||||||||||||||||||||||||||||||||||||||||
| 113 | my $security_connection = $self->{CONFIG}->{connection_class}->new( | ||||||||||||||||||||||||||||||||||||||||||
| 114 | $self->{CONFIG} | ||||||||||||||||||||||||||||||||||||||||||
| 115 | 1 | ); | |||||||||||||||||||||||||||||||||||||||||
| 116 | my ($was_ok, $log_entry) = $security_connection->check( | ||||||||||||||||||||||||||||||||||||||||||
| 117 | $client_ip, | ||||||||||||||||||||||||||||||||||||||||||
| 118 | $self->{CLIENT_FQDN}, | ||||||||||||||||||||||||||||||||||||||||||
| 119 | 1 | ); | |||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | map { $self->log_entry($_) } @$log_entry if $self->{CONFIG}->{debug} == 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | 1 | ▶ | $self->shutdown unless $was_ok; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | # Log the connection IP and time if global debugging is on | ||||||||||||||||||||||||||||||||||||||||||
| 123 | 1 | ▶ | if ($self->{CONFIG}->{debug} and $self->{CONFIG}->{debug} == 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"CONFIG"}{"debug"} and $self->{"CONFIG"}{"debug"} == 1
MC/DC0%0/2
$self->{"CONFIG"}{"debug"} and $self->{"CONFIG"}{"debug"} == 1
$self->{"CONFIG"}{"debug"}
$self->{"CONFIG"}{"debug"} == 1
Truth table33%1/3
A and B
A= $self->{"CONFIG"}{"debug"}B= $self->{"CONFIG"}{"debug"} == 1
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | 1 | $self->log_entry("$self->{CLIENT_IP}\tconnected at"); | |||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||
| 126 | # Send the mpopd greeting. | ||||||||||||||||||||||||||||||||||||||||||
| 127 | 1 | print "+OK $self->{CONFIG}->{greeting}$CRLF"; | |||||||||||||||||||||||||||||||||||||||||
| 128 | 1 | while (1) { | |||||||||||||||||||||||||||||||||||||||||
| 129 | 8 | my $request = ""; | |||||||||||||||||||||||||||||||||||||||||
| 130 | 8 | my $char; | |||||||||||||||||||||||||||||||||||||||||
| 131 | 8 | my $select = IO::Select->new; | |||||||||||||||||||||||||||||||||||||||||
| 132 | 8 | $select->add($input_fh); | |||||||||||||||||||||||||||||||||||||||||
| 133 | 8 | while (1) { | |||||||||||||||||||||||||||||||||||||||||
| 134 | # Set the kernel alarm for $self->{CONFIG}->{timeout} | ||||||||||||||||||||||||||||||||||||||||||
| 135 | # seconds and then only wait that long for the next command | ||||||||||||||||||||||||||||||||||||||||||
| 136 | # from the client. | ||||||||||||||||||||||||||||||||||||||||||
| 137 | # The whole read process is eval'ed. See man perlfunc -> portability | ||||||||||||||||||||||||||||||||||||||||||
| 138 | 56 | eval { | |||||||||||||||||||||||||||||||||||||||||
| 139 | 56 | ▶ | if ($^O !~ /MSWin32/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | # can_read doesn't work on win32! rely on it just blocking | ||||||||||||||||||||||||||||||||||||||||||
| 141 | die "alarm\n" | ||||||||||||||||||||||||||||||||||||||||||
| 142 | 56 | ▶ | unless $select->can_read($self->{CONFIG}->{timeout}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||
| 144 | 56 | sysread $input_fh, $char, 1; | |||||||||||||||||||||||||||||||||||||||||
| 145 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 146 | 56 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | ▶ | $self->force_shutdown('timeout') if $@ eq "alarm\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 148 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 149 | 56 | ▶ | last unless defined $char; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 150 | 56 | ▶ | last if $char eq "\012"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 151 | 48 | $request .= $char; | |||||||||||||||||||||||||||||||||||||||||
| 152 | # commented because one should "be liberal in what one accepts" | ||||||||||||||||||||||||||||||||||||||||||
| 153 | # $self->force_shutdown('line too long') if (length($request) > 50); | ||||||||||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||||||||||||
| 156 | 8 | ▶ | $self->force_shutdown unless defined $char; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | # remove all but alphanumeric chars and whitespace from the | ||||||||||||||||||||||||||||||||||||||||||
| 158 | # request, and only accept 3-50 chars total (UIDL could be long'ish) | ||||||||||||||||||||||||||||||||||||||||||
| 159 | # commented because one should "be liberal in what one accepts" | ||||||||||||||||||||||||||||||||||||||||||
| 160 | # $request =~ s/^([\s\w]{3,50})/$1/g; | ||||||||||||||||||||||||||||||||||||||||||
| 161 | 8 | $request =~ s/\r|\n//g; | |||||||||||||||||||||||||||||||||||||||||
| 162 | 8 | my ($command, $arg, $arg1) = split /\s+/, $request, 3; | |||||||||||||||||||||||||||||||||||||||||
| 163 | 8 | ▶ | $arg = '' unless defined $arg; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 164 | 8 | ▶ | $arg1 = '' unless defined $arg1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 165 | 8 | $command = uc $command; | |||||||||||||||||||||||||||||||||||||||||
| 166 | 8 | $self->log_user_entry("$command $arg $arg1"); | |||||||||||||||||||||||||||||||||||||||||
| 167 | # Close and warn if an invalid command is received | ||||||||||||||||||||||||||||||||||||||||||
| 168 | 8 | ▶ | unless ($COMMAND2FUNC{$command}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 169 | 0 | ▶ | $self->log_entry("$self->{CLIENT_IP}\tWARN no command sent, port scan? at") if $self->{CONFIG}->{debug} == 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 170 | 0 | $self->force_shutdown("So, that's the way you want it... :\("); | |||||||||||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||||||||||||
| 172 | 8 | ▶ | if (!$COMMAND2OKNOLOGIN{$command} and !$self->{PASSWORD_GIVEN}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$COMMAND2OKNOLOGIN{$command} or $self->{"PASSWORD_GIVEN"}
MC/DC0%0/2
$COMMAND2OKNOLOGIN{$command} or $self->{"PASSWORD_GIVEN"}
$COMMAND2OKNOLOGIN{$command}
$self->{"PASSWORD_GIVEN"}
Truth table66%2/3
A or B
A= $COMMAND2OKNOLOGIN{$command}B= $self->{"PASSWORD_GIVEN"}
| |||||||||||||||||||||||||||||||||||||||||||
| 173 | 0 | $self->send_to_user("-ERR not logged in yet!"); | |||||||||||||||||||||||||||||||||||||||||
| 174 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||||||||||||
| 176 | 8 | $COMMAND2FUNC{$command}->($self, $arg, $arg1); | |||||||||||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||
| 180 | 1 | ▶ | sub commandUSER { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
commandUSER: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 181 | 1 | my ($self, $arg, $arg1) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 182 | 1 | my $user_name = $arg; | |||||||||||||||||||||||||||||||||||||||||
| 183 | 1 | $self->{CLIENT_USERNAME} = $user_name; | |||||||||||||||||||||||||||||||||||||||||
| 184 | 1 | ▶ | if ($self->{CONFIG}->{addreceived}->{$user_name}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 185 | $self->{RECEIVED_HEADER} = | ||||||||||||||||||||||||||||||||||||||||||
| 186 | 1 | "Received: from $self->{CONFIG}->{receivedfrom}$CRLF" . | |||||||||||||||||||||||||||||||||||||||||
| 187 | " by mpopd V$self->{CONFIG}->{mpopd_conf_version}$CRLF" . | ||||||||||||||||||||||||||||||||||||||||||
| 188 | " for $user_name; " . | ||||||||||||||||||||||||||||||||||||||||||
| 189 | localtime(time) . | ||||||||||||||||||||||||||||||||||||||||||
| 190 | " $self->{CONFIG}->{timezone}$CRLF"; | ||||||||||||||||||||||||||||||||||||||||||
| 191 | 1 | $self->{ADDEDBYTES} = length($self->{RECEIVED_HEADER}); | |||||||||||||||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||||||||||||||||
| 193 | $self->bad_user unless $self->{CONFIG}->{user_check}->( | ||||||||||||||||||||||||||||||||||||||||||
| 194 | $self->{CONFIG}, | ||||||||||||||||||||||||||||||||||||||||||
| 195 | $user_name, | ||||||||||||||||||||||||||||||||||||||||||
| 196 | $self->{CLIENT_FQDN}, | ||||||||||||||||||||||||||||||||||||||||||
| 197 | 1 | ▶ | ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 198 | 1 | $self->log_user_open($user_name); | |||||||||||||||||||||||||||||||||||||||||
| 199 | 1 | $self->log_user_entry("USER $user_name"); | |||||||||||||||||||||||||||||||||||||||||
| 200 | 1 | $self->send_to_user("+OK $user_name send me your password"); | |||||||||||||||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||
| 203 | 1 | ▶ | sub commandPASS { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
commandPASS: called CC=7 SCAR=27.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 204 | 1 | my ($self, $arg, $arg1) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 205 | 1 | ▶ | if ($self->{MAILBOX_OPENED}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 206 | 0 | $self->send_to_user("-ERR already authenticated"); | |||||||||||||||||||||||||||||||||||||||||
| 207 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||||||||||||||||
| 209 | 1 | ▶ | unless ($self->{CLIENT_USERNAME}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 210 | 0 | $self->send_to_user("-ERR I need your USER name first!"); | |||||||||||||||||||||||||||||||||||||||||
| 211 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||||||||||||||||
| 213 | # Check the password supplied | ||||||||||||||||||||||||||||||||||||||||||
| 214 | $self->{PASSWORD_GIVEN} = $self->{CONFIG}->{password_check}->( | ||||||||||||||||||||||||||||||||||||||||||
| 215 | $self->{CONFIG}, | ||||||||||||||||||||||||||||||||||||||||||
| 216 | $self->{CLIENT_USERNAME}, | ||||||||||||||||||||||||||||||||||||||||||
| 217 | $self->{CLIENT_FQDN}, | ||||||||||||||||||||||||||||||||||||||||||
| 218 | 1 | $arg, | |||||||||||||||||||||||||||||||||||||||||
| 219 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 220 | 1 | ▶ | unless ($self->{PASSWORD_GIVEN}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 221 | 0 | $self->send_to_user( | |||||||||||||||||||||||||||||||||||||||||
| 222 | "-ERR access denied $self->{CLIENT_USERNAME} $arg" | ||||||||||||||||||||||||||||||||||||||||||
| 223 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 224 | 0 | $self->shutdown; | |||||||||||||||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||||||||||||||||
| 226 | 1 | load_class($self->{CONFIG}->{mailbox_class}); | |||||||||||||||||||||||||||||||||||||||||
| 227 | $self->{MAILBOX} = $self->{CONFIG}->{mailbox_class}->new( | ||||||||||||||||||||||||||||||||||||||||||
| 228 | $self->{CLIENT_USERNAME}, | ||||||||||||||||||||||||||||||||||||||||||
| 229 | $arg, | ||||||||||||||||||||||||||||||||||||||||||
| 230 | $self->{CONFIG}->{mailbox_args}->( | ||||||||||||||||||||||||||||||||||||||||||
| 231 | $self->{CONFIG}, | ||||||||||||||||||||||||||||||||||||||||||
| 232 | $self->{CLIENT_USERNAME}, | ||||||||||||||||||||||||||||||||||||||||||
| 233 | $self->{CLIENT_FQDN}, | ||||||||||||||||||||||||||||||||||||||||||
| 234 | 1 | ), | |||||||||||||||||||||||||||||||||||||||||
| 235 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 236 | 1 | my $lockcnt = 0; | |||||||||||||||||||||||||||||||||||||||||
| 237 | 1 | ▶ | until ($self->{MAILBOX}->lock_acquire) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 238 | 0 | ▶ | if ( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $self->{"CONFIG"}{"retry_on_lock"} or $self->{"CONFIG"}{"retry_on_lock"} == $lockcnt
MC/DC0%0/2
not $self->{"CONFIG"}{"retry_on_lock"} or $self->{"CONFIG"}{"retry_on_lock"} == $lockcnt
not $self->{"CONFIG"}{"retry_on_lock"}
$self->{"CONFIG"}{"retry_on_lock"} == $lockcnt
Truth table0%0/3
not(A) or B
A= not $self->{"CONFIG"}{"retry_on_lock"}B= $self->{"CONFIG"}{"retry_on_lock"} == $lockcnt
| |||||||||||||||||||||||||||||||||||||||||||
| 239 | !$self->{CONFIG}->{retry_on_lock} or | ||||||||||||||||||||||||||||||||||||||||||
| 240 | $self->{CONFIG}->{retry_on_lock} == $lockcnt | ||||||||||||||||||||||||||||||||||||||||||
| 241 | ) { | ||||||||||||||||||||||||||||||||||||||||||
| 242 | 0 | $self->send_to_user("-ERR Could not get a lock on mailbox!"); | |||||||||||||||||||||||||||||||||||||||||
| 243 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||||||||||||||||
| 245 | 0 | $lockcnt++; # here so if retry == 1, don't drop out first time | |||||||||||||||||||||||||||||||||||||||||
| 246 | 0 | sleep 1; | |||||||||||||||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||||||||||||||||
| 248 | 1 | $self->send_to_user("+OK thanks $self->{CLIENT_USERNAME}..."); | |||||||||||||||||||||||||||||||||||||||||
| 249 | 1 | $self->{MAILBOX_OPENED} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||||||||||||
| 252 | =head2 load_class | ||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||
| 254 | Loads class - function, not method. | ||||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||
| 256 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||
| 258 | 1 | ▶ | sub load_class { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
load_class: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 259 | 1 | my ($class) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 260 | 1 | my $class_file = $class; | |||||||||||||||||||||||||||||||||||||||||
| 261 | 1 | $class_file =~ s#::#/#g; | |||||||||||||||||||||||||||||||||||||||||
| 262 | 1 | $class_file .= '.pm'; | |||||||||||||||||||||||||||||||||||||||||
| 263 | 1 | require $class_file; | |||||||||||||||||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||||||
| 266 | 0 | ▶ | sub commandSTAT { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
commandSTAT: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 267 | 0 | my ($self, $arg, $arg1) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 268 | $self->send_to_user( | ||||||||||||||||||||||||||||||||||||||||||
| 269 | "+OK ".$self->{MAILBOX}->messages." ".$self->{MAILBOX}->octets | ||||||||||||||||||||||||||||||||||||||||||
| 270 | 0 | ); | |||||||||||||||||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||
| 273 | 0 | ▶ | sub commandLIST { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
commandLIST: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 274 | 0 | my ($self, $arg, $arg1) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 275 | 0 | ▶ | if ($arg) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 276 | 0 | ▶ | if (!$self->{MAILBOX}->is_valid($arg)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 277 | 0 | $self->send_to_user("-ERR message $arg is not valid"); | |||||||||||||||||||||||||||||||||||||||||
| 278 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||||||||||||||||
| 280 | 0 | $self->send_to_user("+OK $arg " . $self->{MAILBOX}->octets($arg)); | |||||||||||||||||||||||||||||||||||||||||
| 281 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||||||||||||||||
| 283 | 0 | $self->send_to_user("+OK ".$self->{MAILBOX}->messages." messages"); | |||||||||||||||||||||||||||||||||||||||||
| 284 | 0 | for (1..$self->{MAILBOX}->messages) { | |||||||||||||||||||||||||||||||||||||||||
| 285 | 0 | ▶ | if (!$self->{MAILBOX}->is_deleted($_)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 286 | 0 | print "$_ " . $self->{MAILBOX}->octets($_) . " octets$CRLF"; | |||||||||||||||||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||||||||||||||||
| 289 | 0 | print ".$CRLF"; | |||||||||||||||||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||
| 292 | # Send the email requested by $arg to the client | ||||||||||||||||||||||||||||||||||||||||||
| 293 | 1 | ▶ | sub commandRETR { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
commandRETR: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 294 | 1 | my ($self, $arg, $arg1) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 295 | 1 | ▶ | if (!$self->{MAILBOX}->is_valid($arg)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 296 | 0 | $self->send_to_user("-ERR message $arg is not valid"); | |||||||||||||||||||||||||||||||||||||||||
| 297 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||||||||||||||||
| 299 | 1 | my $octets = $self->{MAILBOX}->octets($arg) + $self->{ADDEDBYTES}; | |||||||||||||||||||||||||||||||||||||||||
| 300 | 1 | print "+OK $octets octets$CRLF"; | |||||||||||||||||||||||||||||||||||||||||
| 301 | 1 | print $self->{RECEIVED_HEADER}; | |||||||||||||||||||||||||||||||||||||||||
| 302 | 1 | $self->{MAILBOX}->retrieve($arg, $self->{OUTPUT_FH}); | |||||||||||||||||||||||||||||||||||||||||
| 303 | 1 | print ".$CRLF"; | |||||||||||||||||||||||||||||||||||||||||
| 304 | 1 | $self->log_user_entry("RETRieved\t$octets"); | |||||||||||||||||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||||
| 307 | 1 | ▶ | sub commandDELE { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
commandDELE: called CC=2 SCAR=7.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 308 | 1 | my ($self, $arg, $arg1) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 309 | 1 | ▶ | if (!$self->{MAILBOX}->is_valid($arg)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 310 | 0 | $self->send_to_user("-ERR message $arg is not valid"); | |||||||||||||||||||||||||||||||||||||||||
| 311 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||||||||||||||||
| 313 | 1 | $self->send_to_user("+OK message $arg flagged for deletion"); | |||||||||||||||||||||||||||||||||||||||||
| 314 | 1 | $self->{MAILBOX}->delete($arg); | |||||||||||||||||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||
| 317 | 0 | ▶ | sub commandNOOP { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
commandNOOP: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 318 | 0 | my ($self, $arg, $arg1) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 319 | 0 | $self->send_to_user("+OK"); | |||||||||||||||||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||
| 322 | 0 | ▶ | sub commandRSET { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
commandRSET: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 323 | 0 | my ($self, $arg, $arg1) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 324 | 0 | $self->{MAILBOX}->reset; | |||||||||||||||||||||||||||||||||||||||||
| 325 | 0 | $self->send_to_user("+OK all message flags reset"); | |||||||||||||||||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||
| 328 | 2 | ▶ | sub commandUIDL { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
commandUIDL: called CC=3 SCAR=16.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 329 | 2 | my ($self, $arg, $arg1) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 330 | #print Data::Dumper::Dumper($self->{MAILBOX}); | ||||||||||||||||||||||||||||||||||||||||||
| 331 | 2 | ▶ | if ($arg) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 332 | 0 | ▶ | if (!$self->{MAILBOX}->is_valid($arg)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 333 | 0 | $self->send_to_user("-ERR message $arg is not valid"); | |||||||||||||||||||||||||||||||||||||||||
| 334 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 335 | } | ||||||||||||||||||||||||||||||||||||||||||
| 336 | # must be valid | ||||||||||||||||||||||||||||||||||||||||||
| 337 | 0 | $self->send_to_user("+OK $arg " . $self->{MAILBOX}->uidl($arg)); | |||||||||||||||||||||||||||||||||||||||||
| 338 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||||||||||||||||
| 340 | 2 | $self->send_to_user("+OK unique-id listing follows"); | |||||||||||||||||||||||||||||||||||||||||
| 341 | 2 | $self->{MAILBOX}->uidl_list($self->{OUTPUT_FH}); | |||||||||||||||||||||||||||||||||||||||||
| 342 | } | ||||||||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||||||||
| 344 | 1 | ▶ | sub commandTOP { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
commandTOP: called CC=3 SCAR=12.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 345 | 1 | my ($self, $arg, $arg1) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 346 | 1 | my $cnt; | |||||||||||||||||||||||||||||||||||||||||
| 347 | 1 | ▶ | if (!$self->{MAILBOX}->is_valid($arg)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 348 | 0 | $self->send_to_user("-ERR message $arg is not valid"); | |||||||||||||||||||||||||||||||||||||||||
| 349 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 350 | } | ||||||||||||||||||||||||||||||||||||||||||
| 351 | 1 | ▶ | unless ($arg1 >= 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 352 | 0 | $self->send_to_user("-ERR TOP with wrong number of lines ($arg1)"); | |||||||||||||||||||||||||||||||||||||||||
| 353 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||||||||||||||||
| 355 | 1 | $self->send_to_user("+OK top of message $arg follows"); | |||||||||||||||||||||||||||||||||||||||||
| 356 | 1 | print $self->{RECEIVED_HEADER}; | |||||||||||||||||||||||||||||||||||||||||
| 357 | my $top_bytes = | ||||||||||||||||||||||||||||||||||||||||||
| 358 | $self->{MAILBOX}->top($arg, $self->{OUTPUT_FH}, $arg1) + | ||||||||||||||||||||||||||||||||||||||||||
| 359 | 1 | $self->{ADDEDBYTES}; | |||||||||||||||||||||||||||||||||||||||||
| 360 | 1 | print ".$CRLF"; | |||||||||||||||||||||||||||||||||||||||||
| 361 | 1 | $self->log_user_entry( | |||||||||||||||||||||||||||||||||||||||||
| 362 | "TOPped\t$top_bytes" | ||||||||||||||||||||||||||||||||||||||||||
| 363 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 364 | } | ||||||||||||||||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||||||||||||||||
| 366 | 1 | ▶ | sub commandQUIT { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
commandQUIT: called CC=4 SCAR=16.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 367 | 1 | my ($self, $arg, $arg1) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 368 | 1 | ▶ | if (!$self->{CONFIG}->{user_debug}->{$self->{CLIENT_USERNAME}}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 369 | 1 | eval { $self->{MAILBOX}->flush_delete; }; | |||||||||||||||||||||||||||||||||||||||||
| 370 | 1 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 371 | 0 | chomp $@; | |||||||||||||||||||||||||||||||||||||||||
| 372 | $self->log_entry( | ||||||||||||||||||||||||||||||||||||||||||
| 373 | "$self->{CLIENT_IP}\t$self->{CLIENT_USERNAME} $@" | ||||||||||||||||||||||||||||||||||||||||||
| 374 | 0 | ▶ | ) if $self->{CONFIG}->{debug} == 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||||||||||||||||
| 376 | } | ||||||||||||||||||||||||||||||||||||||||||
| 377 | 1 | $self->force_shutdown("+OK TTFN $self->{CLIENT_USERNAME}..."); | |||||||||||||||||||||||||||||||||||||||||
| 378 | } | ||||||||||||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||||||||||||
| 380 | =head2 bad_user | ||||||||||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||||||||||
| 382 | Handles bad user. | ||||||||||||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||||||||||
| 384 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||||||||||||
| 386 | # Reject bogus login name and exit or fake a password auth | ||||||||||||||||||||||||||||||||||||||||||
| 387 | 0 | ▶ | sub bad_user { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
bad_user: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 388 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 389 | 0 | ▶ | $self->log_entry("$self->{CLIENT_IP}\tBOGUS user name given at") if $self->{CONFIG}->{debug} == 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 390 | 0 | ▶ | if ($self->{CONFIG}->{reject_bogus_user} == 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 391 | 0 | print "-ERR no record here of $self->{CLIENT_USERNAME},...$CRLF"; | |||||||||||||||||||||||||||||||||||||||||
| 392 | 0 | $self->shutdown; | |||||||||||||||||||||||||||||||||||||||||
| 393 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 394 | 0 | my $request; | |||||||||||||||||||||||||||||||||||||||||
| 395 | 0 | print "+OK $self->{CLIENT_USERNAME} send me your password....$CRLF"; | |||||||||||||||||||||||||||||||||||||||||
| 396 | 0 | alarm 10; | |||||||||||||||||||||||||||||||||||||||||
| 397 | 0 | sysread $self->{INPUT_FH}, $request, 1; | |||||||||||||||||||||||||||||||||||||||||
| 398 | 0 | alarm 0; | |||||||||||||||||||||||||||||||||||||||||
| 399 | 0 | print "-ERR access denied$CRLF"; | |||||||||||||||||||||||||||||||||||||||||
| 400 | 0 | $self->shutdown; | |||||||||||||||||||||||||||||||||||||||||
| 401 | } | ||||||||||||||||||||||||||||||||||||||||||
| 402 | } | ||||||||||||||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||||||||||||||
| 404 | =head2 peer_lookup | ||||||||||||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||||||||||||
| 406 | Reverse lookup. | ||||||||||||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||||||||||||
| 408 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||||||||||||
| 410 | # do a reverse lookup | ||||||||||||||||||||||||||||||||||||||||||
| 411 | 1 | ▶ | sub peer_lookup { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
peer_lookup: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 412 | 1 | my ($self, $ip) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 413 | 1 | lc gethostbyaddr(inet_aton($ip), IO::Socket::AF_INET); | |||||||||||||||||||||||||||||||||||||||||
| 414 | } | ||||||||||||||||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||||||||||||||||
| 416 | =head2 log_user_open | ||||||||||||||||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||||||||||||||||
| 418 | Optional per-user brief logging of connection times | ||||||||||||||||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||||||||||||||||
| 420 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||||||||||||
| 422 | 1 | ▶ | sub log_user_open { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
log_user_open: called CC=3 SCAR=20.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 423 | 1 | my ($self, $user_name) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 424 | 1 | ▶ | return unless defined $self->{CONFIG}->{user_log}->{$user_name}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 425 | 0 | ▶ | if (!-d $self->{CONFIG}->{user_log_dir}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 426 | 0 | mkdir $self->{CONFIG}->{user_log_dir}; | |||||||||||||||||||||||||||||||||||||||||
| 427 | 0 | chmod 01777, $self->{CONFIG}->{user_log_dir}; | |||||||||||||||||||||||||||||||||||||||||
| 428 | } | ||||||||||||||||||||||||||||||||||||||||||
| 429 | 0 | my $logfile = "$self->{CONFIG}->{user_log_dir}/${user_name}_log"; | |||||||||||||||||||||||||||||||||||||||||
| 430 | 0 | $self->{USERLOG_FH} = IO::File->new( | |||||||||||||||||||||||||||||||||||||||||
| 431 | ">>$logfile" | ||||||||||||||||||||||||||||||||||||||||||
| 432 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 433 | 0 | eval { | |||||||||||||||||||||||||||||||||||||||||
| 434 | # in case we're on Windoze... | ||||||||||||||||||||||||||||||||||||||||||
| 435 | 0 | chown((getpwnam $self->{CLIENT_USERNAME})[2], $logfile); | |||||||||||||||||||||||||||||||||||||||||
| 436 | 0 | chmod 0600, $logfile; | |||||||||||||||||||||||||||||||||||||||||
| 437 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 438 | 0 | $self->log_user_entry("CONNECTION OPENED"); | |||||||||||||||||||||||||||||||||||||||||
| 439 | } | ||||||||||||||||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||||||||||||||||
| 441 | =head2 log_user_close | ||||||||||||||||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||||||||||||||||
| 443 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||||||||||||||||
| 445 | 1 | ▶ | sub log_user_close { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
log_user_close: called CC=3 SCAR=14.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 446 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 447 | return unless | ||||||||||||||||||||||||||||||||||||||||||
| 448 | $self->{USERLOG_FH} and | ||||||||||||||||||||||||||||||||||||||||||
| 449 | 1 | ▶ | defined $self->{CONFIG}->{user_log}->{$self->{CLIENT_USERNAME}}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"USERLOG_FH"} and defined $self->{"CONFIG"}{"user_log"}{$self->{"CLIENT_USERNAME"}}
MC/DC0%0/2
$self->{"USERLOG_FH"} and defined $self->{"CONFIG"}{"user_log"}{$self->{"CLIENT_USERNAME"}}
$self->{"USERLOG_FH"}
defined $self->{"CONFIG"}{"user_log"}{$self->{"CLIENT_USERNAME"}}
Truth table33%1/3
A and B
A= $self->{"USERLOG_FH"}B= defined $self->{"CONFIG"}{"user_log"}{$self->{"CLIENT_USERNAME"}}
| |||||||||||||||||||||||||||||||||||||||||||
| 450 | 0 | close $self->{USERLOG_FH}; | |||||||||||||||||||||||||||||||||||||||||
| 451 | } | ||||||||||||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||||||||||||
| 453 | =head2 log_user_entry | ||||||||||||||||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||||||||||||||||
| 455 | Record mpopd conversations in the individual mailbox log. | ||||||||||||||||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||||||||||||||||
| 457 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||||||||||||||||
| 459 | 18 | ▶ | sub log_user_entry { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
log_user_entry: called CC=6 SCAR=29.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 460 | 18 | my ($self, $response) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 461 | return unless | ||||||||||||||||||||||||||||||||||||||||||
| 462 | $self->{USERLOG_FH} and | ||||||||||||||||||||||||||||||||||||||||||
| 463 | $self->{CONFIG}->{user_log}->{$self->{CLIENT_USERNAME}} and | ||||||||||||||||||||||||||||||||||||||||||
| 464 | 18 | ▶ | $self->{CONFIG}->{user_log}->{$self->{CLIENT_USERNAME}} == 2; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$self->{"USERLOG_FH"} and $self->{"CONFIG"}{"user_log"}{$self->{"CLIENT_USERNAME"}} and $self->{"CONFIG"}{"user_log"}{$self->{"CLIENT_USERNAME"}} == 2
$self->{"USERLOG_FH"} and $self->{"CONFIG"}{"user_log"}{$self->{"CLIENT_USERNAME"}}
MC/DC0%0/3
$self->{"USERLOG_FH"} and $self->{"CONFIG"}{"user_log"}{$self->{"CLIENT_USERNAME"}} and $self->{"CONFIG"}{"user_log"}{$self->{"CLIENT_USERNAME"}} == 2
$self->{"USERLOG_FH"}
$self->{"CONFIG"}{"user_log"}{$self->{"CLIENT_USERNAME"}}
$self->{"CONFIG"}{"user_log"}{$self->{"CLIENT_USERNAME"}} == 2
Truth table25%1/4
A and B and C
A= $self->{"USERLOG_FH"}B= $self->{"CONFIG"}{"user_log"}{$self->{"CLIENT_USERNAME"}}C= $self->{"CONFIG"}{"user_log"}{$self->{"CLIENT_USERNAME"}} == 2
| |||||||||||||||||||||||||||||||||||||||||||
| 465 | 0 | ▶ | if ($response =~ /^PASS\s+(.*)/ and $self->{CONFIG}->{passsecret}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$response =~ /^PASS\s+(.*)/ and $self->{"CONFIG"}{"passsecret"}
MC/DC0%0/2
$response =~ /^PASS\s+(.*)/ and $self->{"CONFIG"}{"passsecret"}
$response =~ /^PASS\s+(.*)/
$self->{"CONFIG"}{"passsecret"}
Truth table0%0/3
A and B
A= $response =~ /^PASS\s+(.*)/B= $self->{"CONFIG"}{"passsecret"}
| |||||||||||||||||||||||||||||||||||||||||||
| 466 | 0 | $response =~ s/$1/******/; | |||||||||||||||||||||||||||||||||||||||||
| 467 | } | ||||||||||||||||||||||||||||||||||||||||||
| 468 | 0 | $self->{USERLOG_FH}->print(localtime() . " $response\n"); | |||||||||||||||||||||||||||||||||||||||||
| 469 | } | ||||||||||||||||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||||||||||||||||
| 471 | =head2 send_to_user | ||||||||||||||||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||||||||||||||||
| 473 | Takes C<$text>, C<$log_suppress>. | ||||||||||||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||||||||||||
| 475 | CRLF is added here, and also logged if C<$log_suppress> is false | ||||||||||||||||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||||||||||||||||
| 477 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||||||||||||||||
| 479 | 7 | ▶ | sub send_to_user { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
send_to_user: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 480 | 7 | my ($self, $text, $log_suppress) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 481 | 7 | print "$text$CRLF"; | |||||||||||||||||||||||||||||||||||||||||
| 482 | 7 | ▶ | $self->log_user_entry($text) unless $log_suppress; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 483 | } | ||||||||||||||||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||||||||||||||||
| 485 | =head2 force_shutdown | ||||||||||||||||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||||||||||||||||
| 487 | # Close the mailbox in a sane state and close the connection | ||||||||||||||||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||||||||||||||||
| 489 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||||||||||||||||
| 491 | 1 | ▶ | sub force_shutdown { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
force_shutdown: called CC=5 SCAR=18.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 492 | 1 | my ($self, $signoff) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 493 | 1 | ▶ | if ($signoff) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 494 | 1 | ▶ | if ($signoff eq "ALRM") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 495 | 0 | $signoff = "Haven't got all day you know..."; | |||||||||||||||||||||||||||||||||||||||||
| 496 | } elsif ($signoff eq "USR1") { | ||||||||||||||||||||||||||||||||||||||||||
| 497 | 0 | $signoff = "My parent told me to close..."; | |||||||||||||||||||||||||||||||||||||||||
| 498 | } | ||||||||||||||||||||||||||||||||||||||||||
| 499 | $self->send_to_user( | ||||||||||||||||||||||||||||||||||||||||||
| 500 | 1 | $signoff | |||||||||||||||||||||||||||||||||||||||||
| 501 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 502 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 503 | 0 | $self->send_to_user( | |||||||||||||||||||||||||||||||||||||||||
| 504 | "Sorry your time is up :)" | ||||||||||||||||||||||||||||||||||||||||||
| 505 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 506 | } | ||||||||||||||||||||||||||||||||||||||||||
| 507 | 1 | $self->log_user_close; | |||||||||||||||||||||||||||||||||||||||||
| 508 | 1 | ▶ | if ($self->{MAILBOX_OPENED}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 509 | 1 | $self->{MAILBOX}->lock_release; | |||||||||||||||||||||||||||||||||||||||||
| 510 | } | ||||||||||||||||||||||||||||||||||||||||||
| 511 | 1 | $self->shutdown; | |||||||||||||||||||||||||||||||||||||||||
| 512 | } | ||||||||||||||||||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||||||||||||||||
| 514 | =head2 log_entry | ||||||||||||||||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||||||||||||||||
| 516 | Write something in the main mpopd log | ||||||||||||||||||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||||||||||||||||
| 518 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 519 | |||||||||||||||||||||||||||||||||||||||||||
| 520 | 1 | ▶ | sub log_entry { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
log_entry: called CC=7 SCAR=20.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 521 | 1 | my ($self, $error) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 522 | 1 | ▶ | return unless defined $self->{CONFIG}->{debug_log}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 523 | 1 | $> = 0; | |||||||||||||||||||||||||||||||||||||||||
| 524 | 1 | ▶ | unless ($self->{DEBUG_FH}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 525 | 1 | my ($debuglog_dir) = $self->{CONFIG}->{debug_log} =~ /^(.+)\//; | |||||||||||||||||||||||||||||||||||||||||
| 526 | 1 | ▶ | if (!-d $debuglog_dir) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 527 | 0 | mkdir $debuglog_dir, 0700; | |||||||||||||||||||||||||||||||||||||||||
| 528 | } | ||||||||||||||||||||||||||||||||||||||||||
| 529 | 1 | ▶ | $self->{DEBUG_FH} = IO::File->new(">>$self->{CONFIG}->{debug_log}") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 530 | or die "open >>$self->{CONFIG}->{debug_log}: $!\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 531 | 1 | ▶ | my $gid = $^O =~ /MSWin32/ ? 0 : getgrnam("root"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 532 | 1 | chown 0, $gid, $self->{CONFIG}->{debug_log}; | |||||||||||||||||||||||||||||||||||||||||
| 533 | 1 | chmod 0600, $self->{CONFIG}->{debug_log}; | |||||||||||||||||||||||||||||||||||||||||
| 534 | } | ||||||||||||||||||||||||||||||||||||||||||
| 535 | 1 | my $logtime = localtime(time); | |||||||||||||||||||||||||||||||||||||||||
| 536 | 1 | $self->{DEBUG_FH}->print("$error\t$logtime\n"); | |||||||||||||||||||||||||||||||||||||||||
| 537 | 1 | ▶ | $> = $self->{CLIENT_USER_ID} if $self->{CLIENT_USER_ID}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 538 | } | ||||||||||||||||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||||||||||||||||
| 540 | =head2 shutdown | ||||||||||||||||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||||||||||||||||
| 542 | Clean up and exit | ||||||||||||||||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||||||||||||||||
| 544 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||||||||||||||||
| 546 | 1 | ▶ | sub shutdown { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
shutdown: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 547 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 548 | 1 | close $self->{INPUT_FH}; | |||||||||||||||||||||||||||||||||||||||||
| 549 | 1 | exit(0); | |||||||||||||||||||||||||||||||||||||||||
| 550 | } | ||||||||||||||||||||||||||||||||||||||||||
| 551 | |||||||||||||||||||||||||||||||||||||||||||
| 552 | 4 | 1; | |||||||||||||||||||||||||||||||||||||||||