
    RSi                      n    d dl Z d dlmZ d dlmZ d dlmZ ddlmZ ddl	m
Z
 dgZ G d	 de
e          ZdS )
    N)SSL)
FTPHandler)logger   )TLS_DTPHandler)SSLConnectionMixinTLS_FTPHandlerc            
           e Zd ZdZdZdZdZdZej	        Z
ej        ej        z  Z eed          r
eej        z  ZdZeZej                                        Ze                     edddd           edddd           edddd	          d
           d fd	Zd Zed             Zd Zd Z fdZd Zd Z d Z!d Z"d Z# xZ$S )r	   a  A FTPHandler subclass supporting TLS/SSL.
    Implements AUTH, PBSZ and PROT commands (RFC-2228 and RFC-4217).

    Configurable attributes:

     - (bool) tls_control_required:
        When True requires SSL/TLS to be established on the control
        channel, before logging in.  This means the user will have
        to issue AUTH before USER/PASS (default False).

     - (bool) tls_data_required:
        When True requires SSL/TLS to be established on the data
        channel.  This means the user will have to issue PROT
        before PASV or PORT (default False).

    SSL-specific options:

     - (string) certfile:
        the path to the file which contains a certificate to be
        used to identify the local side of the connection.
        This  must always be specified, unless context is provided
        instead.

     - (string) keyfile:
        the path to the file containing the private RSA key;
        can be omitted if certfile already contains the private
        key (defaults: None).

     - (int) ssl_protocol:
        the desired SSL protocol version to use. This defaults to
        TLS_SERVER_METHOD, which includes TLSv1, TLSv1.1, TLSv1.2
        and TLSv1.3. The actual protocol version used will be
        negotiated to the highest version mutually supported by the
        client and the server.

     - (int) ssl_options:
        specific OpenSSL options. These default to:
        SSL.OP_NO_SSLv2 | SSL.OP_NO_SSLv3 | SSL.OP_NO_COMPRESSION
        ...which are all considered insecure features.
        Can be set to None in order to improve compatibility with
        older (insecure) FTP clients.

      - (instance) ssl_context:
        a SSL Context object previously configured; if specified
        all other parameters will be ignored.
        (default None).
    FNOP_NO_COMPRESSIONTz:Syntax: AUTH <SP> TLS|SSL (set up secure control channel).)permautharghelpz+Syntax: PBSZ <SP> 0 (negotiate TLS buffer).z8Syntax: PROT <SP> [C|P] (set up un/secure data channel).)AUTHPBSZPROTc                     t                                          |||           | j        sd S g d| _        d| _        d| _        |                                 | _        d S )N)zAUTH TLSzAUTH SSLr   r   F)super__init__	connected_extra_feats_pbsz_protget_ssl_contextssl_context)selfconnserverioloop	__class__s       S/home/jrussi/.local/lib/python3.11/site-packages/pyftpdlib/handlers/ftps/control.pyr   zTLS_FTPHandler.__init__k   sd    vv...~ 	FDDD

//11    c                 *    t          j        |           S N)r   __repr__r   s    r!   r%   zTLS_FTPHandler.__repr__t   s    "4(((r"   c                    | j         | j        t          d          t          j        | j                  | _         | j        s| j        | _        | j        | j        fD ]:}t          j        	                    | j                  s|d}t          |          ;| j                             | j                   | j                             | j                   | j        r| j                             | j                   | j         S )Nz#at least certfile must be specifiedz does not exist)r   certfile
ValueErrorr   Contextssl_protocolkeyfileospathisfileFileNotFoundErroruse_certificate_chain_fileuse_privatekey_filessl_optionsset_options)clsfilemsgs      r!   r   zTLS_FTPHandler.get_ssl_contextw   s    ?"|# !FGGG!k#*:;;CO; +!ls{3 1 1w~~cl33 1!444C+C0001 O66s|DDDO//<<< =++CO<<<r"   c                 J    t          j        |            d| _        d| _        d S )NF)r   flush_accountr   r   r&   s    r!   r9   zTLS_FTPHandler.flush_account   s%     &&&



r"   c                 d   |dv rI| j         rA| j        s:d}|                     d|z              |                     ||d         d|           d S nL|dv rH| j        rA| j        s:d}|                     d|z              |                     ||d         d|           d S t          j        | |g|R i | d S )N)USERPASSz(SSL/TLS required on the control channel.z550 r   i&  )PASVEPSVPORTEPRTz%SSL/TLS required on the data channel.)tls_control_required_ssl_establishedrespondlog_cmdtls_data_requiredr   r   process_command)r   cmdargskwargsr7   s        r!   rF   zTLS_FTPHandler.process_command   s    """( 1F @Vc\***S$q'3444444% dj =Vc\***S$q'3444"4>t>>>v>>>>>r"   c                     | j         r*t          j        d           |                                  dS t	                                                       dS )zfCalled when client does not send any command within the time
        specified in <timeout> attribute.zSSL handshake timeoutN)_ssl_acceptingr   infocloser   handle_timeout)r   r    s    r!   rN   zTLS_FTPHandler.handle_timeout   sN      	%K/000JJLLLLLGG""$$$$$r"   c                 V    t          j        |            t          j        |            d S r$   )r   rM   r   r&   s    r!   rM   zTLS_FTPHandler.close   s*     &&&r"   c                 X    |                      d           |                                  d S )NzSSL handshake failed.)logrM   r&   s    r!   handle_failed_ssl_handshakez*TLS_FTPHandler.handle_failed_ssl_handshake   s(    
 	()))

r"   c                 6   |                                 }t          | j        t          j                  r|                     d           dS |dv r5|                     d| d           |                     | j                   dS |                     d           dS )zSet up secure control channel.z503 Already using TLS.)TLSzTLS-Cr   zTLS-Pz	234 AUTH z successful.z2502 Unrecognized encryption type (use TLS or SSL).N)upper
isinstancesocketr   
ConnectionrC   secure_connectionr   r   liner   s      r!   ftp_AUTHzTLS_FTPHandler.ftp_AUTH   s    jjlldk3>22 		OLL122222444 LL6S666777""4#344444LLMNNNNNr"   c                     t          | j        t          j                  s|                     d           dS |                     d           d| _        dS )zNegotiate size of buffer for secure data transfer.
        For TLS/SSL the only valid value for the parameter is '0'.
        Any other value is accepted but ignored.
        z4503 PBSZ not allowed on insecure control connection.z200 PBSZ=0 successful.TN)rV   rW   r   rX   rC   r   )r   r[   s     r!   ftp_PBSZzTLS_FTPHandler.ftp_PBSZ   s]    
 $+s~66 	LLF     LL1222DJJJr"   c                    |                                 }t          | j        t          j                  s|                     d           dS | j        s|                     d           dS |dk    r|                     d           d| _        dS |dk    r|                     d           d| _        dS |d	v r|                     d
| d           dS |                     d           dS )zSetup un/secure data channel.z4503 PROT not allowed on insecure control connection.z2503 You must issue the PBSZ command prior to PROT.Cz200 Protection set to ClearFPz200 Protection set to PrivateT)SEz	521 PROT z unsupported (use C or P).z(502 Unrecognized PROT type (use C or P).N)rU   rV   rW   r   rX   rC   r   r   rZ   s      r!   ftp_PROTzTLS_FTPHandler.ftp_PROT   s   jjll$+s~66 	ELLF      	ELLMNNNNNCZZLL6777DJJJCZZLL8999DJJJJLLDSDDDEEEEELLCDDDDDr"   r$   )%__name__
__module____qualname____doc__rA   rE   r(   r,   r   TLS_SERVER_METHODr+   OP_NO_SSLv2OP_NO_SSLv3r3   hasattrr   r   r   dtp_handlerr   
proto_cmdscopyupdatedictr   r%   classmethodr   r9   rF   rN   rM   rR   r\   r^   rd   __classcell__)r    s   @r!   r	   r	      s       . .b !HG(L
 /CO3Kws'(( -s,,K !K&++--JM	
 
 
 >	
 
 
 K	
 
 
    *2 2 2 2 2 2) ) )   [0  
? ? ?% % % % %    O O O  E E E E E E Er"   )r-   OpenSSLr   pyftpdlib.handlers.ftp.controlr   pyftpdlib.logr   datar   sslr   __all__r	    r"   r!   <module>r{      s   
 
			       5 5 5 5 5 5                         # # # # # #
WE WE WE WE WE' WE WE WE WE WEr"   