
    RSi0                         d dl Z d dlZd dlZ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          ZdS )	    N)SSL)_RetryError)_ERRNOS_DISCONNECTED)debug)loggerSSLConnectionMixinc                        e Zd ZdZdZdZdZdZ fdZ fdZ	 fdZ
d Zej        d             Zd Zd	 Z fd
Zd Z fdZ fdZ fdZ fdZ fdZ fdZ fdZ xZS )r   z)An AsyncChat subclass supporting TLS/SSL.Fc                 d     t                      j        |i | d| _        d| _        d| _        d S NF)super__init___error_ssl_want_read_ssl_want_write)selfargskwargs	__class__s      O/home/jrussi/.local/lib/python3.11/site-packages/pyftpdlib/handlers/ftps/ssl.pyr   zSSLConnectionMixin.__init__   s<    $)&)))#$    c                 `    | j         p&| j        pt                                                      S N)_ssl_acceptingr   r   readabler   r   s    r   r   zSSLConnectionMixin.readable"   s)    "Od&9OUWW=M=M=O=OOr   c                 R    | j         pt                                                      S r   )r   r   writabler   s    r   r   zSSLConnectionMixin.writable%   s!    #9uww'7'7'9'99r   c                    t          d|            d| _        	 t          j        || j                  | _        | j                                         d| _        dS # t          $ r3}t          d|d|            |                                  Y d}~dS d}~wt          $ r2 | j        
                                dk    rt          d|            Y dS  w xY w)zLSecure the connection switching from plain-text to
        SSL/TLS.
        zsecuring SSL connectionTz7call: secure_connection(); can't secure SSL connection z	; closingNz.ValueError and fd == -1 on secure_connection())r   _ssl_requestedr   
Connectionsocketset_accept_stater   OSErrorclose
ValueErrorfileno)r   ssl_contexterrs      r   secure_connectionz$SSLConnectionMixin.secure_connection(   s    	'..."	'.dkBBDK$ K((***"&D%  	 	 	 $$ $ $  
 JJLLLLLLLLL 	 	 	 {!!##r))FMMM	s   A 
C$(B:CCc              #     K   | j         p| j        }	 d V  n3# t          j        $ r
 d| _         Y nt          j        $ r
 d| _        Y nw xY w| j         r+|                     | j        | j        j        z  d           d S | j        r+|                     | j        | j        j	        z  d           d S |r|                     | j                   d S d S )NT)logdebug)
r   r   r   WantReadErrorWantWriteErrormodify_ioloop_events_wanted_io_eventsioloopREADWRITE)r   prev_row_pendings     r   _handle_ssl_want_rwz&SSLConnectionMixin._handle_ssl_want_rwD   s3     .F$2F	(EEEE  	' 	' 	'"&D! 	( 	( 	(#'D   	(  		>%%&)99D &      ! 	>%%&)::T &       	>%%d&<=====	> 	>s    AAAc                    d| _         d| _        d| _        	 | j                                         t          d|            d| _         d| _        |                                  d S # t          j	        $ r d| _        t          d|            Y d S t          j
        $ r d| _        t          d|            Y d S t          j        $ rf}t          d||            |j        \  }}|dk    r|d	k    s|d
k    r*|                     d           |                                  n Y d }~d S d }~wt          j        $ r3}t          d||            |                                  Y d }~d S d }~ww xY w)NTFzSSL connection establishedz+call: _do_ssl_handshake, err: ssl-want-readinstz,call: _do_ssl_handshake, err: ssl-want-writezcall: _do_ssl_handshake, err: r   Unexpected EOFr   zUnexpected SSL EOF.)r   r   r   r"   do_handshaker   _ssl_establishedhandle_ssl_establishedr   r-   r.   SysCallErrorr   logr%   Errorhandle_failed_ssl_handshake)r   r)   retvaldescs       r   _do_ssl_handshakez$SSLConnectionMixin._do_ssl_handshake[   s   "#$	*K$$&&&2 .555"'D$(D!'')))))7   	L 	L 	L"&D?dKKKKKKK! 	M 	M 	M#'D @tLLLLLLL 	 	 	:3::FFFF8LFD")9!9!9fqjj .///

  y 	/ 	/ 	/:3::FFFF,,.........	/s0   A$ $'E+&E+7E+AD&&E+8(E&&E+c                     dS )z(Called when SSL handshake has completed.N r   s    r   r<   z)SSLConnectionMixin.handle_ssl_established~   s      r   c                 H    t                                                       dS )z)Called when SSL shutdown() has completed.N)r   r%   r   s    r   handle_ssl_shutdownz&SSLConnectionMixin.handle_ssl_shutdown   s    r   c                      t          d          )Nzmust be implemented in subclass)NotImplementedErrorrF   s    r   r@   z.SSLConnectionMixin.handle_failed_ssl_handshake   s    !"CDDDr   c                 r   | j         s"t                                                       d S |                                 5  d| _        | j        r|                                  n<| j        r|                                  n t                                                       d d d            d S # 1 swxY w Y   d S r   )	r    r   handle_read_eventr5   r   r   rC   _ssl_closing_do_ssl_shutdownr   s    r   rL   z$SSLConnectionMixin.handle_read_event   s    " 
	0GG%%'''''))++ 0 0&+#& 0**,,,,& 0))++++GG--///0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0   A B,,B03B0c                 r   | j         s"t                                                       d S |                                 5  d| _        | j        r|                                  n<| j        r|                                  n t                                                       d d d            d S # 1 swxY w Y   d S r   )	r    r   handle_write_eventr5   r   r   rC   rM   rN   r   s    r   rQ   z%SSLConnectionMixin.handle_write_event   s    " 
	1GG&&((((())++ 1 1',$& 1**,,,,& 1))++++GG..0001 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1rO   c                    d| _         	  # t          $ r |                     |            Y nw xY w	 t                                                       d S # t          $ r) t          j        t          j                               Y d S w xY w)NT)	r   	Exceptionlog_exceptionr   r%   r   critical	traceback
format_excr   s    r   handle_errorzSSLConnectionMixin.handle_error   s    	% 	% 	% 	%t$$$$$	%
	4GGMMOOOOO 	4 	4 	4OI022333333	4s    -- A /BBc                    t          |t                    st          |          }	 t                                          |          S # t          j        $ r t          d|            d| _        Y dS t          j        $ r t          d|            d| _	        Y dS t          j
        $ r5 t          d|            t                                                       Y dS t          j        $ ro}t          d||            |j        \  }}|t          j        k    rY d }~dS |t           v s|dk    r&t                                                       Y d }~dS  d }~ww xY w)	Nz call: send(), err: ssl-want-readr7   Tr   z!call: send(), err: ssl-want-writez,call: send() -> shutdown(), err: zero-returnzcall: send(), err: r9   )
isinstancebytesr   sendr   r-   r   r   r.   r   ZeroReturnErrorhandle_closer=   r   errnoEWOULDBLOCKr   )r   datar)   errnumerrstrr   s        r   r\   zSSLConnectionMixin.send   s   $&& 	;;D	77<<%%%  	 	 	44@@@@"&D11! 	 	 	5DAAAA#'D 11" 	 	 	@tLLLLGG  """11 		 		 		///d;;;; XNFF***qqqqq///6=M3M3M$$&&&qqqqq		s5    A 'E2&E?EE+.E/EEEc                 j   	 t                                          |          S # t          j        $ r%}t	          d|            d| _        t          |d }~wt          j        $ r%}t	          d|            d| _        t          |d }~wt          j	        $ r5 t	          d|            t                      
                                 Y dS t          j        $ rY}t	          d||            |j        \  }}|t          v s|dk    r&t                      
                                 Y d }~dS  d }~ww xY w)	Nz call: recv(), err: ssl-want-readr7   Tz!call: recv(), err: ssl-want-writez,call: recv() -> shutdown(), err: zero-returnr   zcall: recv(), err: r9   )r   recvr   r-   r   r   r   r.   r   r]   r^   r=   r   r   )r   buffer_sizer)   rb   rc   r   s        r   re   zSSLConnectionMixin.recv   sd   	77<<,,,  	' 	' 	'44@@@@"&D3&! 	' 	' 	'5DAAAA#'D 3&" 	 	 	@tLLLLGG  """33 	 	 	///d;;;; XNFF---;K1K1K$$&&&sssss	s:    $ D2 AD2% BAD2D2AD-,D--D2c                    d| _         t          j        dk    r	 t          j        | j                                        d           n# t          $ r}}t          d||            |j        t          j	        t          j
        t          j        hv rY d}~dS |j        t          v r%t                                                      cY d}~S  d}~ww xY w	 | j                                        }| j                            |t"          j        z             | j                                        }|t"          j        z  s$| j                            t"          j                   |r5t          d|            d| _        d| _         |                                  dS t          d	|            dS # t"          j        $ r d| _        t          d
|            Y dS t"          j        $ r d| _        t          d|            Y dS t"          j        $ r5 t          d|            t                                                       Y dS t"          j        $ rZ}t          d||            |j        \  }}|t          v s|dk    r!t                                                       n Y d}~dS d}~wt"          j        $ r9}t          d||            |j        rt?          |dd          sn Y d}~dS d}~wt          $ rO}t          d||            |j        t          v r!t                                                       n Y d}~dS d}~ww xY w)zExecutes a SSL_shutdown() call to revert the connection
        back to clear-text.
        twisted/internet/tcp.py code has been used as an example.
        Tposixr   z+call: _do_ssl_shutdown() -> os.write, err: r7   Nz,call: _do_ssl_shutdown(), shutdown completedFz4call: _do_ssl_shutdown(), shutdown not completed yetz*call: _do_ssl_shutdown, err: ssl-want-readz+call: _do_ssl_shutdown, err: ssl-want-writez8call: _do_ssl_shutdown() -> shutdown(), err: zero-returnz-call: _do_ssl_shutdown() -> shutdown(), err: r9   r_   ) rM   osnamewriter"   r'   r$   r   r_   EINTRr`   ENOBUFSr   r   r%   get_shutdownset_shutdownr   RECEIVED_SHUTDOWNshutdownSENT_SHUTDOWNr;   rH   r-   r   r.   r   r]   r=   r   r?   getattr)r   r)   	laststatedonerb   rc   r   s         r   rN   z#SSLConnectionMixin._do_ssl_shutdown   s   
 !7g++--s3333   I#II    9K%M!  
 FFFFFY"666 77==??******@>	0022IK$$Y1F%FGGG;''))Ds44 <(():;;;Z  B    ).%$)!((*****J     k   	K 	K 	K"&D>TJJJJJJJ! 	L 	L 	L#'D ?dKKKKKKK" 	 	 	J    GGMMOOOOOO 		 		 		GGG    !XNFF---;K1K1K   y 	 	 	GGG    x Wd ; ;    	 	 	GGG    y000  	sn   ,A 
C>C	-C	CC		CBF( ('M&M;?M=MAJ  M2.K&&M3AL==Mc                     | j         r| j        s|                                  d S d| _        d| _         d| _        t                                                       d S r   )r;   r   rN   r   rM   r   r%   r   s    r   r%   zSSLConnectionMixin.closeP  s\      	 	!!#####"'D$)D! %DGGMMOOOOOr   )__name__
__module____qualname____doc__r   r;   rM   r    r   r   r   r*   
contextlibcontextmanagerr5   rC   r<   rH   r@   rL   rQ   rX   r\   re   rN   r%   __classcell__)r   s   @r   r   r      s       33NLN% % % % %P P P P P: : : : :' ' '8 > > >,!* !* !*F7 7 7    E E E0 0 0 0 01 1 1 1 14 4 4 4 4    8    0j j j j jX        r   )r{   r_   ri   rV   OpenSSLr   pyftpdlib.exceptionsr   pyftpdlib.ioloopr   pyftpdlib.logr   r   __all__r   rE   r   r   <module>r      s   
      				           , , , , , , 1 1 1 1 1 1                  
 C C C C C C C C C Cr   