Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ftplib cannot connect to server which has banner / welcome message configured #123911

Closed
kegabor opened this issue Sep 10, 2024 · 8 comments
Closed
Labels
pending The issue will be closed if no feedback is provided stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@kegabor
Copy link

kegabor commented Sep 10, 2024

Bug report

Bug description:

Scenario:
Setup: There is a running FTP server configured to send welcome message to user, like:
Hello, this is my FTP server, please login
Test: Connect to this FTP server and validate the login banner.
Unexpected result:

    welcome_msg = new_ftp.connect(host, port, timeout)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.12/ftplib.py", line 162, in connect
    self.welcome = self.getresp()
                   ^^^^^^^^^^^^^^
  File "/usr/lib64/python3.12/ftplib.py", line 255, in getresp
    raise error_proto(resp)
ftplib.error_proto: test disclaimer

Based on code https://github.com/python/cpython/blame/main/Lib/ftplib.py#L162 the welcome message is handled the same way as any FTP response, but there is no restriction I could find related to banner.
It means if it's not starting with number 1-3 then error is raised. Moreover if the 4th character is not a '-', then only 1 line is read.

My suggestion is to simply read out the buffer in connect() like:

  def connect(...)
    ...
    self.welcome = self.readwelcome()
    return self.welcome

  def readwelcome(self):
    '''Get the welcome message during connect()'''
    welcome = self.sock.recv(self.maxline).decode()
    if self.debugging:
      print('*welcome* ', self.sanitize(welcome))
    return welcome

CPython versions tested on:

3.9, 3.12

Operating systems tested on:

Linux, Windows

@kegabor kegabor added the type-bug An unexpected behavior, bug, or error label Sep 10, 2024
@picnixz picnixz added the stdlib Python modules in the Lib dir label Sep 10, 2024
@picnixz
Copy link
Contributor

picnixz commented Sep 10, 2024

The server is expected to reply using standard reply codes as defined by RFC 959 and should format its message accordingly. The welcome/banner message must in particular send first the reply code and the whatever is after (see §4.2 of the RFC and §6 for the state automaton).

@picnixz picnixz added the pending The issue will be closed if no feedback is provided label Sep 10, 2024
@kegabor
Copy link
Author

kegabor commented Sep 11, 2024

I see your point and tend to accept, but I still curious, what code should be used in the welcome message?
The RFC does not mention this special message, that comes before "220 Service ready" and not command initiated.
It is even not fit into the mentioned state diagrams in §6 in "Login sequence", as there the first step is asking USER name, but welcome message comes earlier.
The closest is 214 as a help message.

@picnixz
Copy link
Contributor

picnixz commented Sep 11, 2024

Ah sorry §6. I thought it had more details. Actually, the RFC is poorly documented so my bad for this. I think the server should send something like TLS/SSL1

220- Hello this is the welcome message.
220- This is the second line of the welcome.
220 The last line does not contain a hyphenation.

This should do the trick. If it doesn't, I think we have a doc issue.

EDIT: The RFC mentions in §5.4:

One important group of informational replies is the connection
greetings. Under normal circumstances, a server will send a 220
reply, "awaiting input", when the connection is completed. The
user should wait for this greeting message before sending any
commands. If the server is unable to accept input right away, a
120 "expected delay" reply should be sent immediately and a 220
reply when ready. The user will then know not to hang up if there
is a delay.


Footnotes

  1. §4.2 mentions:

    Thus the format for multi-line replies is that the first line
    will begin with the exact required reply code, followed
    immediately by a Hyphen, "-" (also known as Minus), followed by
    text. The last line will begin with the same code, followed
    immediately by Space , optionally some text, and the Telnet
    end-of-line code.

    For example:

      123-First line
      Second line
        234 A line beginning with numbers
      123 The last line
    

@kegabor
Copy link
Author

kegabor commented Sep 11, 2024

Good news, the code is needed only in the first and last line, so between them can be anything (except any code in the first 3 position).
The code 220 is for Service ready for new user, but it's a separate message after the "greeting"

@picnixz
Copy link
Contributor

picnixz commented Sep 11, 2024

AFAIU, 220 is used for greeting as well (emphasis mine):

One important group of informational replies is the connection
greetings. Under normal circumstances, a server will send a 220
reply, "awaiting input", when the connection is completed. The
user should wait for this greeting message
before sending any
commands

I can't find a specific reply code to use for the welcome message only (one that is not 220).

@kegabor
Copy link
Author

kegabor commented Sep 11, 2024

I'll close this ticket as no issue, and I'll suggest to the server's owners to format their welcome message accordingly.
I have to mention that there are FTP clients which accepts any kind of greetings, and needs only properly formatted FTP messages after the communication is established.

214 Help message. On how to use the server or the meaning of a particular non-standard command. This reply is useful only to the human user.

Thanks @picnixz for your time and help!

@kegabor kegabor closed this as completed Sep 11, 2024
@picnixz
Copy link
Contributor

picnixz commented Sep 11, 2024

214 Help message. On how to use the server or the meaning of a particular non-standard command.

I incorrectly assumed that this one would only be used as a reply of the help command actually. But if it can be used as the initial greeting message, that should be fine.

@kegabor
Copy link
Author

kegabor commented Sep 11, 2024

I've tried with a test client-server, and it also accepts two 220 messages. First is the welcome message, while the second is the default "220 Service ready for new user", that I couldn't modify.

@terryjreedy terryjreedy closed this as not planned Won't fix, can't repro, duplicate, stale Sep 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pending The issue will be closed if no feedback is provided stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants