From 655aef43bbbacee10ad19ab107e885fc590d89a8 Mon Sep 17 00:00:00 2001 From: Yughandhar Kamavaram Date: Sun, 1 Dec 2024 18:03:19 +0530 Subject: [PATCH 1/8] Update ftplib.py Accept 229 Extended passive mode error as acceptible ouput with PASV command --- Lib/ftplib.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/Lib/ftplib.py b/Lib/ftplib.py index 50771e8c17c250..cb2eeec6dee927 100644 --- a/Lib/ftplib.py +++ b/Lib/ftplib.py @@ -324,11 +324,16 @@ def makeport(self): def makepasv(self): """Internal: Does the PASV or EPSV handshake -> (address, port)""" if self.af == socket.AF_INET: - untrusted_host, port = parse227(self.sendcmd('PASV')) - if self.trust_server_pasv_ipv4_address: - host = untrusted_host - else: - host = self.sock.getpeername()[0] + try: + untrusted_host, port = parse227(self.sendcmd('PASV')) + if self.trust_server_pasv_ipv4_address: + host = untrusted_host + else: + host = self.sock.getpeername()[0] + except error_reply as resp: + resp = str(resp) + if resp[:3] == '229': + host, port = parse229(resp, self.sock.getpeername()) else: host, port = parse229(self.sendcmd('EPSV'), self.sock.getpeername()) return host, port From 88a5531b66476b0704a6a5f5eb132f99ad3513f0 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sun, 1 Dec 2024 12:51:04 +0000 Subject: [PATCH 2/8] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Documentation/2024-12-01-12-51-03.gh-issue-127478.mFSRpa.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Documentation/2024-12-01-12-51-03.gh-issue-127478.mFSRpa.rst diff --git a/Misc/NEWS.d/next/Documentation/2024-12-01-12-51-03.gh-issue-127478.mFSRpa.rst b/Misc/NEWS.d/next/Documentation/2024-12-01-12-51-03.gh-issue-127478.mFSRpa.rst new file mode 100644 index 00000000000000..ca2ae1ed70c582 --- /dev/null +++ b/Misc/NEWS.d/next/Documentation/2024-12-01-12-51-03.gh-issue-127478.mFSRpa.rst @@ -0,0 +1 @@ +Accept 229 response as valid PASV command output From 94eee2e6c04d6851d95cf9367d79eeaca39f6d75 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sun, 1 Dec 2024 16:43:39 +0000 Subject: [PATCH 3/8] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2024-12-01-16-43-38.gh-issue-127478.fHI_y9.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2024-12-01-16-43-38.gh-issue-127478.fHI_y9.rst diff --git a/Misc/NEWS.d/next/Library/2024-12-01-16-43-38.gh-issue-127478.fHI_y9.rst b/Misc/NEWS.d/next/Library/2024-12-01-16-43-38.gh-issue-127478.fHI_y9.rst new file mode 100644 index 00000000000000..6f219d74fd0def --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-12-01-16-43-38.gh-issue-127478.fHI_y9.rst @@ -0,0 +1 @@ +Support "229 Entering Extended Passive Mode" response in :meth:`ftplib.FTP.sendcmd`. From 6f08557bdc3911a356fb39fc9902702860846ea2 Mon Sep 17 00:00:00 2001 From: Yughandhar Kamavaram Date: Sun, 1 Dec 2024 22:14:56 +0530 Subject: [PATCH 4/8] remove NEWS from Documentation --- .../Documentation/2024-12-01-12-51-03.gh-issue-127478.mFSRpa.rst | 1 - 1 file changed, 1 deletion(-) delete mode 100644 Misc/NEWS.d/next/Documentation/2024-12-01-12-51-03.gh-issue-127478.mFSRpa.rst diff --git a/Misc/NEWS.d/next/Documentation/2024-12-01-12-51-03.gh-issue-127478.mFSRpa.rst b/Misc/NEWS.d/next/Documentation/2024-12-01-12-51-03.gh-issue-127478.mFSRpa.rst deleted file mode 100644 index ca2ae1ed70c582..00000000000000 --- a/Misc/NEWS.d/next/Documentation/2024-12-01-12-51-03.gh-issue-127478.mFSRpa.rst +++ /dev/null @@ -1 +0,0 @@ -Accept 229 response as valid PASV command output From 4fae73e4ca843825c0b373b8d9cdf7c8b7622118 Mon Sep 17 00:00:00 2001 From: Yughandhar Kamavaram Date: Sun, 1 Dec 2024 23:40:16 +0530 Subject: [PATCH 5/8] retry with EPSV when error occured with PASV --- Lib/ftplib.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Lib/ftplib.py b/Lib/ftplib.py index cb2eeec6dee927..5ca6dca88161fe 100644 --- a/Lib/ftplib.py +++ b/Lib/ftplib.py @@ -330,10 +330,8 @@ def makepasv(self): host = untrusted_host else: host = self.sock.getpeername()[0] - except error_reply as resp: - resp = str(resp) - if resp[:3] == '229': - host, port = parse229(resp, self.sock.getpeername()) + except: + host, port = parse229(self.sendcmd('EPSV'), self.sock.getpeername()) else: host, port = parse229(self.sendcmd('EPSV'), self.sock.getpeername()) return host, port From 886b32601259eee9a53df51f7f4b817c97f649b6 Mon Sep 17 00:00:00 2001 From: Yughandhar Kamavaram Date: Mon, 2 Dec 2024 00:15:55 +0530 Subject: [PATCH 6/8] changes per review suggestions --- Lib/ftplib.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Lib/ftplib.py b/Lib/ftplib.py index 5ca6dca88161fe..fa1325f84a43a6 100644 --- a/Lib/ftplib.py +++ b/Lib/ftplib.py @@ -326,12 +326,12 @@ def makepasv(self): if self.af == socket.AF_INET: try: untrusted_host, port = parse227(self.sendcmd('PASV')) - if self.trust_server_pasv_ipv4_address: - host = untrusted_host - else: - host = self.sock.getpeername()[0] - except: - host, port = parse229(self.sendcmd('EPSV'), self.sock.getpeername()) + except error_reply: + untrusted_host, port = parse229(self.sendcmd('EPSV'), self.sock.getpeername()) + if self.trust_server_pasv_ipv4_address: + host = untrusted_host + else: + host = self.sock.getpeername()[0] else: host, port = parse229(self.sendcmd('EPSV'), self.sock.getpeername()) return host, port From 11601b21fc3f16ef6598f0605ee558346c426fc5 Mon Sep 17 00:00:00 2001 From: yugandhar2587 <156336617+yugandhar2587@users.noreply.github.com> Date: Mon, 2 Dec 2024 00:17:58 +0530 Subject: [PATCH 7/8] Update 2024-12-01-16-43-38.gh-issue-127478.fHI_y9.rst --- .../next/Library/2024-12-01-16-43-38.gh-issue-127478.fHI_y9.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2024-12-01-16-43-38.gh-issue-127478.fHI_y9.rst b/Misc/NEWS.d/next/Library/2024-12-01-16-43-38.gh-issue-127478.fHI_y9.rst index 6f219d74fd0def..0cc4fc1a92d6d2 100644 --- a/Misc/NEWS.d/next/Library/2024-12-01-16-43-38.gh-issue-127478.fHI_y9.rst +++ b/Misc/NEWS.d/next/Library/2024-12-01-16-43-38.gh-issue-127478.fHI_y9.rst @@ -1 +1 @@ -Support "229 Entering Extended Passive Mode" response in :meth:`ftplib.FTP.sendcmd`. +Enhancement to support FTP connection with NAT scenario. From d6cc6ff4877368c17c91b977da9980e2ccd58273 Mon Sep 17 00:00:00 2001 From: Yughandhar Kamavaram Date: Mon, 2 Dec 2024 06:56:50 +0530 Subject: [PATCH 8/8] Update ftplib.py --- Lib/ftplib.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Lib/ftplib.py b/Lib/ftplib.py index fa1325f84a43a6..2f2b0078a5577f 100644 --- a/Lib/ftplib.py +++ b/Lib/ftplib.py @@ -324,10 +324,11 @@ def makeport(self): def makepasv(self): """Internal: Does the PASV or EPSV handshake -> (address, port)""" if self.af == socket.AF_INET: + pasv_response = self.sendcmd('PASV') try: - untrusted_host, port = parse227(self.sendcmd('PASV')) + untrusted_host, port = parse227(pasv_response) except error_reply: - untrusted_host, port = parse229(self.sendcmd('EPSV'), self.sock.getpeername()) + untrusted_host, port = parse229(pasv_response, self.sock.getpeername()) if self.trust_server_pasv_ipv4_address: host = untrusted_host else: