Skip to content

Commit f706f0a

Browse files
committed
attach the ftp helper when a port forward sends another public port to port 21
1 parent 15004a7 commit f706f0a

2 files changed

Lines changed: 66 additions & 0 deletions

File tree

‎systemvm/debian/opt/cloud/bin/configure.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,6 +1411,25 @@ def processForwardRule(self, rule):
14111411
self.forward_vpc(rule)
14121412
else:
14131413
self.forward_vr(rule)
1414+
self.forward_ftp_helper(rule)
1415+
1416+
def forward_ftp_helper(self, rule):
1417+
# Linux 6.0 no longer attaches the ftp helper after DNAT, so attach it
1418+
# to the public port that is forwarded to port 21. Public port 21 is
1419+
# already covered by the rule from CsAddress.py
1420+
if rule["protocol"] != "tcp" or "any" in [rule["public_ports"], rule["internal_ports"]]:
1421+
return
1422+
public_start = int(rule["public_ports"].split(":")[0])
1423+
internal_ports = rule["internal_ports"].split(":")
1424+
internal_start = int(internal_ports[0])
1425+
internal_end = int(internal_ports[-1])
1426+
if not internal_start <= 21 <= internal_end:
1427+
return
1428+
public_port = public_start + 21 - internal_start
1429+
if public_port == 21:
1430+
return
1431+
self.fw.append(["raw", "", "-A PREROUTING -d %s/32 -p tcp -m tcp --dport %s -j CT --helper ftp" %
1432+
(rule["public_ip"], public_port)])
14141433

14151434
def forward_vr(self, rule):
14161435
# Prefetch iptables variables
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
import unittest
19+
from configure import CsForwardingRules
20+
21+
22+
class TestCsForwardingRules(unittest.TestCase):
23+
24+
def ftp_helper_rules(self, protocol, public_ports, internal_ports):
25+
forwarding = CsForwardingRules.__new__(CsForwardingRules)
26+
forwarding.fw = []
27+
forwarding.forward_ftp_helper({"public_ip": "10.0.0.2", "internal_ip": "10.1.1.10", "protocol": protocol,
28+
"public_ports": public_ports, "internal_ports": internal_ports})
29+
return forwarding.fw
30+
31+
def test_ftp_helper_on_other_public_port(self):
32+
self.assertEqual(self.ftp_helper_rules("tcp", "2121:2121", "21:21"),
33+
[["raw", "", "-A PREROUTING -d 10.0.0.2/32 -p tcp -m tcp --dport 2121 -j CT --helper ftp"]])
34+
35+
def test_ftp_helper_on_port_range(self):
36+
self.assertEqual(self.ftp_helper_rules("tcp", "1020:1030", "20:30"),
37+
[["raw", "", "-A PREROUTING -d 10.0.0.2/32 -p tcp -m tcp --dport 1021 -j CT --helper ftp"]])
38+
39+
def test_no_ftp_helper(self):
40+
self.assertEqual(self.ftp_helper_rules("tcp", "21:21", "21:21"), [])
41+
self.assertEqual(self.ftp_helper_rules("tcp", "2121:2121", "2121:2121"), [])
42+
self.assertEqual(self.ftp_helper_rules("udp", "2121:2121", "21:21"), [])
43+
self.assertEqual(self.ftp_helper_rules("tcp", "any", "any"), [])
44+
45+
46+
if __name__ == '__main__':
47+
unittest.main()

0 commit comments

Comments
 (0)