Debian Codegraph @

plugins/stunnel

raw
1 #!perl -w
2
3 =head1 NAME
4
5 stunnel - stunnel proxy protocol client ip helper.
6
7 =head1 DESCRIPTION
8
9 stunnel proxy protocol remote ip,port setting feature added for smtps.
10 reference : http://www.stunnel.org/static/stunnel.html
11 protocol spec : http://haproxy.1wt.eu/download/1.5/doc/proxy-protocol.txt
12
13 config/plugins file example
14 stunnel proxy on
15 ...
16
17 =head1 CONFIGURATION
18
19 =head2 proxy [ ON | OFF ]
20
21 proxy protocol handler on/off
22
23 =cut
24
25 use strict;
26 use warnings;
27 use Qpsmtpd::Constants;
28
29 my $proxy_enabled;
30 sub init {
31 my ($self, $qp, %args) = @_;
32
33 return if ( uc $args{proxy} ne 'ON' );
34
35 $self->log(LOGINFO, "proxy protocol enabled");
36 $proxy_enabled = 1;
37 }
38
39 sub hook_unrecognized_command {
40 my ($self, $transaction, $cmd, @args) = @_;
41
42 return OK if ( uc $cmd ne 'PROXY' );
43 return OK if ( !defined $proxy_enabled );
44 return DENY_DISCONNECT if ( $self->connection->remote_ip() ne '127.0.0.1' );
45 return DENY_DISCONNECT if ( $self->connection->notes('proxy') );
46
47 # TCP4 192.168.41.227 10.27.11.106 50060 465
48 if ( $args[0] =~ m/^(.*?) (.*?) (.*?) (.*?) (.*?)$/ ) {
49 my $protocol = $1;
50 my $remote_ip = $2;
51 my $local_ip = $3;
52 my $remote_port = $4;
53 my $local_port = $5;
54 $self->connection->remote_ip( $remote_ip );
55 $self->connection->remote_port( $remote_port );
56 $self->connection->remote_info( "[$remote_ip]");
57
58 $self->connection->notes('proxy', 'YES');
59 $self->connection->notes('protocol', $protocol);
60 $self->connection->notes('remote_ip', $remote_ip);
61 $self->connection->notes('remote_port', $remote_port);
62 $self->connection->notes('local_ip', $local_ip);
63 $self->connection->notes('local_port', $local_port);
64 $self->log(LOGINFO, "stunnel : $remote_ip:$remote_port");
65
66 # DNS reverse
67 if ( $self->isa('Qpsmtpd::PollServer') ) {
68 eval {
69 use ParaDNS;
70 ParaDNS->new(
71 finished => sub { $self->continue_read() },
72 callback => sub { $self->connection->remote_host($_[0]) },
73 host => $remote_ip,
74 );
75
76 };
77 }
78 else {
79 my $res = Net::DNS::Resolver->new( dnsrch => 0 );
80 $res->tcp_timeout(3);
81 $res->udp_timeout(3);
82 my $query = $res->query( $remote_ip, 'PTR' );
83 if ($query) {
84 foreach my $rr ($query->answer) {
85 next if $rr->type ne 'PTR';
86 $self->connection->remote_host( $rr->ptrdname );
87 }
88 }
89 }
90 }
91 else {
92 return DENY_DISCONNECT;
93 }
94 return DONE;
95 }
96