| 1 |
|
|---|
| 2 |
package Proxy::SMTP; |
|---|
| 3 |
|
|---|
| 4 |
use Proxy::Proxy; |
|---|
| 5 |
@ISA = ("Proxy::Proxy"); |
|---|
| 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 |
use strict; |
|---|
| 31 |
use warnings; |
|---|
| 32 |
use locale; |
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
my $eol = "\015\012"; |
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
sub new |
|---|
| 43 |
{ |
|---|
| 44 |
my $type = shift; |
|---|
| 45 |
my $self = Proxy::Proxy->new(); |
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
bless $self, $type; |
|---|
| 50 |
|
|---|
| 51 |
$self->name( 'smtp' ); |
|---|
| 52 |
|
|---|
| 53 |
$self->{child_} = \&child__; |
|---|
| 54 |
$self->{connection_timeout_error_} = '554 Transaction failed'; |
|---|
| 55 |
$self->{connection_failed_error_} = '554 Transaction failed, can\'t connect to'; |
|---|
| 56 |
$self->{good_response_} = '^[23]'; |
|---|
| 57 |
|
|---|
| 58 |
return $self; |
|---|
| 59 |
} |
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
sub initialize |
|---|
| 69 |
{ |
|---|
| 70 |
my ( $self ) = @_; |
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
$self->config_( 'force_fork', ($^O eq 'MSWin32')?0:1 ); |
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
$self->config_( 'port', 25 ); |
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
$self->config_( 'chain_server', '' ); |
|---|
| 80 |
$self->config_( 'chain_port', 25 ); |
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
$self->config_( 'chain_unix_socket', '' ); |
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
$self->config_( 'local', 1 ); |
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
$self->config_( 'welcome_string', "SMTP POPFile ($self->{version_}) welcome" ); |
|---|
| 90 |
|
|---|
| 91 |
if ( !$self->SUPER::initialize() ) { |
|---|
| 92 |
return 0; |
|---|
| 93 |
} |
|---|
| 94 |
|
|---|
| 95 |
$self->config_( 'enabled', 0 ); |
|---|
| 96 |
|
|---|
| 97 |
return 1; |
|---|
| 98 |
} |
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
sub start |
|---|
| 108 |
{ |
|---|
| 109 |
my ( $self ) = @_; |
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
if ( $self->config_( 'enabled' ) == 0 ) { |
|---|
| 114 |
return 2; |
|---|
| 115 |
} |
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 |
$self->register_configuration_item_( 'configuration', |
|---|
| 121 |
'smtp_fork_and_port', |
|---|
| 122 |
'smtp-configuration.thtml', |
|---|
| 123 |
$self ); |
|---|
| 124 |
|
|---|
| 125 |
$self->register_configuration_item_( 'security', |
|---|
| 126 |
'smtp_local', |
|---|
| 127 |
'smtp-security-local.thtml', |
|---|
| 128 |
$self ); |
|---|
| 129 |
|
|---|
| 130 |
$self->register_configuration_item_( 'chain', |
|---|
| 131 |
'smtp_server', |
|---|
| 132 |
'smtp-chain-server.thtml', |
|---|
| 133 |
$self ); |
|---|
| 134 |
|
|---|
| 135 |
$self->register_configuration_item_( 'chain', |
|---|
| 136 |
'smtp_server_port', |
|---|
| 137 |
'smtp-chain-server-port.thtml', |
|---|
| 138 |
$self ); |
|---|
| 139 |
|
|---|
| 140 |
if ( $self->config_( 'welcome_string' ) =~ /^SMTP POPFile \(v\d+\.\d+\.\d+\) welcome$/ ) { |
|---|
| 141 |
$self->config_( 'welcome_string', "SMTP POPFile ($self->{version_}) welcome" ); |
|---|
| 142 |
} |
|---|
| 143 |
|
|---|
| 144 |
return $self->SUPER::start();; |
|---|
| 145 |
} |
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 |
|
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 |
sub child__ |
|---|
| 159 |
{ |
|---|
| 160 |
my ( $self, $client, $session ) = @_; |
|---|
| 161 |
|
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 |
my $count = 0; |
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 |
|
|---|
| 168 |
my $mail; |
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 |
$self->tee_( $client, "220 " . $self->config_( 'welcome_string' ) . "$eol" ); |
|---|
| 174 |
|
|---|
| 175 |
|
|---|
| 176 |
|
|---|
| 177 |
|
|---|
| 178 |
while ( <$client> ) { |
|---|
| 179 |
my $command; |
|---|
| 180 |
|
|---|
| 181 |
$command = $_; |
|---|
| 182 |
|
|---|
| 183 |
|
|---|
| 184 |
$command =~ s/(\015|\012)//g; |
|---|
| 185 |
|
|---|
| 186 |
$self->log_( 2, "Command: --$command--" ); |
|---|
| 187 |
|
|---|
| 188 |
if ( $command =~ /HELO/i ) { |
|---|
| 189 |
if ( $self->config_( 'chain_server' ) || |
|---|
| 190 |
$self->config_( 'chain_unix_socket' ) ) { |
|---|
| 191 |
if ( $mail = $self->verify_connected_( |
|---|
| 192 |
$mail, |
|---|
| 193 |
$client, |
|---|
| 194 |
$self->config_( 'chain_server' ), |
|---|
| 195 |
$self->config_( 'chain_port' ), |
|---|
| 196 |
0, |
|---|
| 197 |
$self->config_( 'chain_unix_socket' ) ) ) { |
|---|
| 198 |
|
|---|
| 199 |
$self->smtp_echo_response_( $mail, $client, $command ); |
|---|
| 200 |
} else { |
|---|
| 201 |
last; |
|---|
| 202 |
} |
|---|
| 203 |
} else { |
|---|
| 204 |
$self->tee_( $client, "421 service not available$eol" ); |
|---|
| 205 |
} |
|---|
| 206 |
|
|---|
| 207 |
next; |
|---|
| 208 |
} |
|---|
| 209 |
|
|---|
| 210 |
|
|---|
| 211 |
|
|---|
| 212 |
if ( $command =~ /EHLO/i ) { |
|---|
| 213 |
if ( $self->config_( 'chain_server' ) || |
|---|
| 214 |
$self->config_( 'chain_unix_socket' ) ) { |
|---|
| 215 |
if ( $mail = $self->verify_connected_( |
|---|
| 216 |
$mail, |
|---|
| 217 |
$client, |
|---|
| 218 |
$self->config_( 'chain_server' ), |
|---|
| 219 |
$self->config_( 'chain_port' ), |
|---|
| 220 |
0, |
|---|
| 221 |
$self->config_( 'chain_unix_socket' ) ) ) { |
|---|
| 222 |
|
|---|
| 223 |
|
|---|
| 224 |
|
|---|
| 225 |
|
|---|
| 226 |
|
|---|
| 227 |
my $unsupported; |
|---|
| 228 |
|
|---|
| 229 |
|
|---|
| 230 |
|
|---|
| 231 |
|
|---|
| 232 |
|
|---|
| 233 |
|
|---|
| 234 |
$unsupported .= "CHUNKING|BINARYMIME|XEXCH50"; |
|---|
| 235 |
|
|---|
| 236 |
|
|---|
| 237 |
|
|---|
| 238 |
|
|---|
| 239 |
$unsupported = qr/250\-$unsupported/; |
|---|
| 240 |
|
|---|
| 241 |
$self->smtp_echo_response_( $mail, $client, $command, $unsupported ); |
|---|
| 242 |
|
|---|
| 243 |
|
|---|
| 244 |
} else { |
|---|
| 245 |
last; |
|---|
| 246 |
} |
|---|
| 247 |
} else { |
|---|
| 248 |
$self->tee_( $client, "421 service not available$eol" ); |
|---|
| 249 |
} |
|---|
| 250 |
|
|---|
| 251 |
next; |
|---|
| 252 |
} |
|---|
| 253 |
|
|---|
| 254 |
if ( ( $command =~ /MAIL FROM:/i ) || |
|---|
| 255 |
( $command =~ /RCPT TO:/i ) || |
|---|
| 256 |
( $command =~ /VRFY/i ) || |
|---|
| 257 |
( $command =~ /EXPN/i ) || |
|---|
| 258 |
( $command =~ /NOOP/i ) || |
|---|
| 259 |
( $command =~ /HELP/i ) || |
|---|
| 260 |
( $command =~ /RSET/i ) ) { |
|---|
| 261 |
$self->smtp_echo_response_( $mail, $client, $command ); |
|---|
| 262 |
next; |
|---|
| 263 |
} |
|---|
| 264 |
|
|---|
| 265 |
if ( $command =~ /DATA/i ) { |
|---|
| 266 |
|
|---|
| 267 |
|
|---|
| 268 |
if ( $self->smtp_echo_response_( $mail, $client, $command ) ) { |
|---|
| 269 |
$count += 1; |
|---|
| 270 |
|
|---|
| 271 |
my ( $class, $history_file ) = $self->{classifier__}->classify_and_modify( $session, $client, $mail, 0, '', 0 ); |
|---|
| 272 |
|
|---|
| 273 |
my $response = $self->slurp_( $mail ); |
|---|
| 274 |
$self->tee_( $client, $response ); |
|---|
| 275 |
next; |
|---|
| 276 |
} |
|---|
| 277 |
} |
|---|
| 278 |
|
|---|
| 279 |
|
|---|
| 280 |
|
|---|
| 281 |
|
|---|
| 282 |
if ( $command =~ /QUIT/i ) { |
|---|
| 283 |
if ( $mail ) { |
|---|
| 284 |
$self->smtp_echo_response_( $mail, $client, $command ); |
|---|
| 285 |
close $mail; |
|---|
| 286 |
} else { |
|---|
| 287 |
$self->tee_( $client, "221 goodbye$eol" ); |
|---|
| 288 |
} |
|---|
| 289 |
last; |
|---|
| 290 |
} |
|---|
| 291 |
|
|---|
| 292 |
|
|---|
| 293 |
if ( $mail && $mail->connected ) { |
|---|
| 294 |
$self->smtp_echo_response_( $mail, $client, $command ); |
|---|
| 295 |
next; |
|---|
| 296 |
} else { |
|---|
| 297 |
$self->tee_( $client, "500 unknown command or bad syntax$eol" ); |
|---|
| 298 |
last; |
|---|
| 299 |
} |
|---|
| 300 |
} |
|---|
| 301 |
|
|---|
| 302 |
if ( defined( $mail ) ) { |
|---|
| 303 |
$self->done_slurp_( $mail ); |
|---|
| 304 |
close $mail; |
|---|
| 305 |
} |
|---|
| 306 |
|
|---|
| 307 |
close $client; |
|---|
| 308 |
$self->mq_post_( 'CMPLT', $$ ); |
|---|
| 309 |
$self->log_( 0, "SMTP proxy done" ); |
|---|
| 310 |
} |
|---|
| 311 |
|
|---|
| 312 |
|
|---|
| 313 |
|
|---|
| 314 |
|
|---|
| 315 |
|
|---|
| 316 |
|
|---|
| 317 |
|
|---|
| 318 |
|
|---|
| 319 |
|
|---|
| 320 |
|
|---|
| 321 |
|
|---|
| 322 |
|
|---|
| 323 |
|
|---|
| 324 |
|
|---|
| 325 |
|
|---|
| 326 |
|
|---|
| 327 |
|
|---|
| 328 |
|
|---|
| 329 |
|
|---|
| 330 |
sub smtp_echo_response_ |
|---|
| 331 |
{ |
|---|
| 332 |
my ($self, $mail, $client, $command, $suppress) = @_; |
|---|
| 333 |
my ( $response, $ok ) = $self->get_response_( $mail, $client, $command ); |
|---|
| 334 |
|
|---|
| 335 |
if ( $response =~ /^\d\d\d-/ ) { |
|---|
| 336 |
$self->echo_to_regexp_($mail, $client, qr/^\d\d\d /, 1, $suppress); |
|---|
| 337 |
} |
|---|
| 338 |
return ( $response =~ /$self->{good_response_}/ ); |
|---|
| 339 |
} |
|---|
| 340 |
|
|---|
| 341 |
|
|---|
| 342 |
|
|---|
| 343 |
|
|---|
| 344 |
|
|---|
| 345 |
|
|---|
| 346 |
|
|---|
| 347 |
|
|---|
| 348 |
|
|---|
| 349 |
|
|---|
| 350 |
|
|---|
| 351 |
|
|---|
| 352 |
sub configure_item |
|---|
| 353 |
{ |
|---|
| 354 |
my ( $self, $name, $templ, $language ) = @_; |
|---|
| 355 |
|
|---|
| 356 |
if ( $name eq 'smtp_fork_and_port' ) { |
|---|
| 357 |
$templ->param( 'smtp_port' => $self->config_( 'port' ) ); |
|---|
| 358 |
$templ->param( 'smtp_force_fork_on' => $self->config_( 'force_fork' ) ); |
|---|
| 359 |
} |
|---|
| 360 |
|
|---|
| 361 |
if ( $name eq 'smtp_local' ) { |
|---|
| 362 |
$templ->param( 'smtp_local_on' => $self->config_( 'local' ) ); |
|---|
| 363 |
} |
|---|
| 364 |
|
|---|
| 365 |
if ( $name eq 'smtp_server' ) { |
|---|
| 366 |
$templ->param( 'smtp_chain_server' => $self->config_( 'chain_server' ) ); |
|---|
| 367 |
} |
|---|
| 368 |
|
|---|
| 369 |
if ( $name eq 'smtp_server_port' ) { |
|---|
| 370 |
$templ->param( 'smtp_chain_port' => $self->config_( 'chain_port' ) ); |
|---|
| 371 |
} |
|---|
| 372 |
|
|---|
| 373 |
|
|---|
| 374 |
|
|---|
| 375 |
} |
|---|
| 376 |
|
|---|
| 377 |
|
|---|
| 378 |
|
|---|
| 379 |
|
|---|
| 380 |
|
|---|
| 381 |
|
|---|
| 382 |
|
|---|
| 383 |
|
|---|
| 384 |
|
|---|
| 385 |
|
|---|
| 386 |
|
|---|
| 387 |
|
|---|
| 388 |
|
|---|
| 389 |
sub validate_item |
|---|
| 390 |
{ |
|---|
| 391 |
my ( $self, $name, $templ, $language, $form ) = @_; |
|---|
| 392 |
|
|---|
| 393 |
if ( $name eq 'smtp_fork_and_port' ) { |
|---|
| 394 |
|
|---|
| 395 |
if ( defined($$form{smtp_force_fork}) ) { |
|---|
| 396 |
$self->config_( 'force_fork', $$form{smtp_force_fork} ); |
|---|
| 397 |
} |
|---|
| 398 |
|
|---|
| 399 |
if ( defined($$form{smtp_port}) ) { |
|---|
| 400 |
if ( ( $$form{smtp_port} >= 1 ) && ( $$form{smtp_port} < 65536 ) ) { |
|---|
| 401 |
$self->config_( 'port', $$form{smtp_port} ); |
|---|
| 402 |
$templ->param( 'smtp_port_feedback' => sprintf( $$language{Configuration_SMTPUpdate}, $self->config_( 'port' ) ) ); |
|---|
| 403 |
} else { |
|---|
| 404 |
$templ->param( 'smtp_port_feedback' => "<div class=\"error01\">$$language{Configuration_Error3}</div>" ); |
|---|
| 405 |
} |
|---|
| 406 |
} |
|---|
| 407 |
} |
|---|
| 408 |
|
|---|
| 409 |
if ( $name eq 'smtp_local' ) { |
|---|
| 410 |
if ( defined $$form{smtp_local} ) { |
|---|
| 411 |
$self->config_( 'local', $$form{smtp_local} ); |
|---|
| 412 |
} |
|---|
| 413 |
} |
|---|
| 414 |
|
|---|
| 415 |
if ( $name eq 'smtp_server' ) { |
|---|
| 416 |
if ( defined $$form{smtp_chain_server} ) { |
|---|
| 417 |
$self->config_( 'chain_server', $$form{smtp_chain_server} ); |
|---|
| 418 |
$templ->param( 'smtp_server_feedback' => sprintf $$language{Security_SMTPServerUpdate}, $self->config_( 'chain_server' ) ) ; |
|---|
| 419 |
} |
|---|
| 420 |
} |
|---|
| 421 |
|
|---|
| 422 |
if ( $name eq 'smtp_server_port' ) { |
|---|
| 423 |
if ( defined $$form{smtp_chain_server_port} ) { |
|---|
| 424 |
|
|---|
| 425 |
if ( ( $$form{smtp_chain_server_port} >= 1 ) && ( $$form{smtp_chain_server_port} < 65536 ) ) { |
|---|
| 426 |
$self->config_( 'chain_port', $$form{smtp_chain_server_port} ); |
|---|
| 427 |
$templ->param( 'smtp_port_feedback' => sprintf $$language{Security_SMTPPortUpdate}, $self->config_( 'chain_port' ) ); |
|---|
| 428 |
} |
|---|
| 429 |
else { |
|---|
| 430 |
$templ->param( 'smtp_port_feedback' => "<div class=\"error01\">$$language{Security_Error1}</div>" ); |
|---|
| 431 |
} |
|---|
| 432 |
} |
|---|
| 433 |
} |
|---|
| 434 |
|
|---|
| 435 |
|
|---|
| 436 |
|
|---|
| 437 |
} |
|---|
| 438 |
|
|---|
| 439 |
1; |
|---|