Fwctl/Services/ftp.pm untested

filter: Statementstmt 0.0% 0 / 94 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 175 CC 21 SCAR 40.9
CC
21
Cov
0%
accept_rules
90.0
account_rules
90.0
block_rules
30.0
CRAP 59.5
1 #
2 # ftp.pm: Fwctl service module to handle the ftp protocol.
3 #
4 # This file is part of Fwctl.
5 #
6 # Author: Francis J. Lacoste <francis@iNsu.COM>
7 #
8 # Copyright (c) 1999,2000 iNsu Innovations Inc.
9 #
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
14 #
15 package Fwctl::Services::ftp;
16  
17 use strict;
18  
19 use Fwctl::RuleSet qw(:tcp_rulesets :ip_rulesets :masq :ports);
20 use IPChains;
21 use Carp;
22  
23 sub new {
24 my $proto = shift;
25 my $class = ref $proto || $proto;
26 bless { pasv_ports => UNPRIVILEGED_PORTS,
27 pasv => 1,
28 port => 1,
29 data_port => "ftp-data",
30 ctrl_port => "ftp",
31 }, $class;
32 }
33  
34 sub prototypes {
35 my ($self,$target,$options) = @_;
36  
37 # Build prototypes
38 my $pasv_ports = $options->{pasv_ports} || $self->{pasv_ports};
39 my $ctrl_port = $options->{ctrl_port} || $self->{ctrl_port};
40 my $data_port = $options->{data_port} || $self->{data_port};
41 (
42 IPChains->new(
43 Rule => $target,
44 Prot => 'tcp',
45 SourcePort => UNPRIVILEGED_PORTS,
46 DestPort => $ctrl_port,
47 %{$options->{ipchains}},
48 ),
49 IPChains->new(
50 Rule => $target,
51 Prot => 'tcp',
52 SourcePort => UNPRIVILEGED_PORTS,
53 DestPort => $pasv_ports,
54 %{$options->{ipchains}},
55 ),
56 IPChains->new(
57 Rule => $target,
58 Prot => 'tcp',
59 SourcePort => $data_port,
60 DestPort => UNPRIVILEGED_PORTS,
61 %{$options->{ipchains}},
62 ),
63 );
64 }
65  
66 sub block_rules {
67 my $self = shift;
68 my ( $target, $src, $src_if, $dst, $dst_if, $options ) = @_;
69  
70  
71 my $do_pasv = $options->{pasv};
72 $do_pasv = $self->{pasv} unless defined $do_pasv;
73 my $do_port = $options->{port};
74 $do_port = $self->{pasv} unless defined $do_port;
75  
76 my ($ftp,$pasv,$port) = $self->prototypes( $target, $options );
77 block_tcp_ruleset( $ftp, $src, $src_if, $dst, $dst_if );
78 if ( $do_pasv ) {
79 block_tcp_ruleset( $pasv, $src, $src_if, $dst, $dst_if );
80 }
81 if ($do_port ) {
82 block_tcp_ruleset( $port, $dst, $dst_if, $src, $src_if );
83 }
84 }
85  
86 sub accept_rules {
87 my $self = shift;
88 my ( $target, $src, $src_if, $dst, $dst_if, $options ) = @_;
89  
90 my $do_pasv = $options->{pasv};
91 $do_pasv = $self->{pasv} unless defined $do_pasv;
92 my $do_port = $options->{port};
93  
94 $do_port = $self->{pasv} unless defined $do_port;
95  
96 my ($ftp,$pasv,$port) = $self->prototypes( $target, $options );
97  
98 my $masq = defined $options->{portfw} ? PORTFW :
99 $options->{masq} ? MASQ : NOMASQ;
100  
101 accept_tcp_ruleset( $ftp, $src, $src_if, $dst, $dst_if,
102 $masq, $options->{portfw} );
103  
104 if ( $do_pasv ) {
105 accept_tcp_ruleset( $pasv, $src, $src_if, $dst, $dst_if,
106 $masq, $options->{portfw} );
107 }
108  
109 if ( $do_port ) {
110 if ( $masq & PORTFW ) {
111 # We must portfw the ACK and not SYN. Thats why we must roll
112 # our own
113 accept_ip_ruleset( $port, $dst, $dst_if, $src, $src_if,
114 UNPORTFW, $options->{portfw} );
115 $port->attribute( 'SYN', '!' );
116  
117 my $src_port = $port->attribute( 'SourcePort' );
118 my $dst_port = $port->attribute( 'DestPort' );
119 $port->attribute( 'DestPort', $src_port );
120 $port->attribute( 'SourcePort', $dst_port );
121  
122 accept_ip_ruleset( $port, $src, $src_if, $dst, $dst_if,
123 PORTFW, $options->{portfw} );
124 } elsif ( $masq & MASQ ) {
125 # The data ports destination were rewritten.
126 accept_ip_ruleset( $port, $dst, $dst_if, $src, $src_if, UNMASQ );
127  
128 $port->attribute( 'SYN', '!' );
129 my $src_port = $port->attribute( 'SourcePort' );
130 my $dst_port = $port->attribute( 'DestPort' );
131 $port->attribute( 'DestPort', $src_port );
132 $port->attribute( 'SourcePort', $dst_port );
133  
134 accept_ip_ruleset( $port, $src, $src_if, $dst, $dst_if, MASQ );
135  
136 # Load the necessary kernel module.
137 system ( "/sbin/modprobe", "ip_masq_ftp" ) == 0
138 or carp __PACKAGE__, ": couldn't load ip_masq_ftp: $?\n";
139 } else {
140 accept_tcp_ruleset( $port, $dst, $dst_if, $src, $src_if );
141 }
142 }
143 }
144  
145 sub account_rules {
146 my $self = shift;
147 my ( $target, $src, $src_if, $dst, $dst_if, $options ) = @_;
148  
149 my $do_pasv = $options->{pasv};
150 $do_pasv = $self->{pasv} unless defined $do_pasv;
151 my $do_port = $options->{port};
152 $do_port = $self->{pasv} unless defined $do_port;
153  
154 my ($ftp,$pasv,$port) = $self->prototypes( $target, $options );
155  
156 my $masq = defined $options->{portfw} ? PORTFW :
157 $options->{masq} ? MASQ : NOMASQ;
158  
159 acct_tcp_ruleset( $ftp, $src, $src_if, $dst, $dst_if, $masq );
160 if ( $do_pasv ) {
161 acct_tcp_ruleset( $pasv, $src, $src_if, $dst, $dst_if, $masq );
162 }
163 if ( $do_port ) {
164 if ( $masq & PORTFW ) {
165 # We must portfw the ACK and not SYN. Thats why we must roll
166 # our own
167 acct_ip_ruleset( $port, $dst, $dst_if, $src, $src_if,
168 UNPORTFW, $options->{portfw} );
169 $port->{SYN} = '!';
170 my $src_port = $port->attribute( 'SourcePort' );
171 my $dst_port = $port->attribute( 'DestPort' );
172 $port->attribute( 'DestPort', $src_port );
173 $port->attribute( 'SourcePort', $dst_port );
174  
175 acct_ip_ruleset( $port, $src, $src_if, $dst, $dst_if,
176 PORTFW, $options->{portfw} );
177 } elsif ( $masq & MASQ ) {
178 # The data ports destination were rewritten.
179 acct_ip_ruleset( $port, $dst, $dst_if, $src, $src_if, UNMASQ );
180  
181 $port->attribute( 'SYN', '!' );
182 my $src_port = $port->attribute( 'SourcePort' );
183 my $dst_port = $port->attribute( 'DestPort' );
184 $port->attribute( 'DestPort', $src_port );
185 $port->attribute( 'SourcePort', $dst_port );
186  
187 acct_ip_ruleset( $port, $src, $src_if, $dst, $dst_if, MASQ );
188 } else {
189 acct_tcp_ruleset( $port, $dst, $dst_if, $src, $src_if,
190 $masq );
191 }
192 }
193 }
194  
195 sub valid_options {
196 my $self = shift;
197 ( "ctrl_port=s", "data_port=s", "pasv_ports=s", "pasv!", "port!" );
198 }
199  
200 1;
201 =pod
202  
203 =head1 NAME
204  
205 Fwctl::Services::ftp - Fwctl module to handle the FTP service.
206  
207 =head1 SYNOPSIS
208  
209 accept ftp -src INTERNAL_NET -dst INTERNET -noport -masq
210 accept ftp -src PROXY_SERVER -dst INTERNET -noport --account
211 accept ftp -src INTERNET -d FTP_SERVER -noport
212 accept ftp -src INTERNAL_NET -dst INT_IP --ctrl_port hylafax
213  
214 =head1 DESCRIPTION
215  
216 The ftp module is used to handle the FTP protocol. By default it
217 handles both PORT and PASV based protocol. If maquerading is asked for,
218 it also loads the proper kernel module.
219  
220 =head1 OPTIONS
221  
222 In additions to the standard options it takes the following ones :
223  
224 =over
225  
226 =item --port or --noport
227  
228 Sets whether rules for the PORT part of the FTP protocol will be added.
229  
230 =item --pasv or --nopasv
231  
232 Sets whether rules for the PASV part of the FTP protocol will be added.
233  
234 =item --pasv_ports
235  
236 Sets the port accepted for PASV connections. Defaults to UNPRIVILEGED_PORTS.
237  
238 =item --ctrl_port
239  
240 Sets the port used for the control connection. This is the port on which
241 the ftp server listens. Defaults to ftp.
242  
243 =item --data_port
244  
245 Sets the port for the data connection. This is the port from which PORT
246 connections of the server originates.
247  
248 =back
249  
250 =head1 AUTHOR
251  
252 Francis J. Lacoste <francis.lacoste@iNsu.COM>
253  
254 =head1 COPYRIGHT
255  
256 Copyright (c) 1999,2000 iNsu Innovations Inc.
257 All rights reserved.
258  
259 This program is free software; you can redistribute it and/or modify
260 it under the terms of the GNU General Public License as published by
261 the Free Software Foundation; either version 2 of the License, or
262 (at your option) any later version.
263  
264 =head1 SEE ALSO
265  
266 fwctl(8) Fwctl(3) Fwctl::RuleSet(3)
267  
268 =cut
269