o
    ¯bG  ã                   @   s¬  d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlmZm	Z	 d dl
mZmZ d dlmZ d dlmZ d dlmZmZmZmZmZ d dlmZmZmZ d dlmZmZ d d	lmZm Z  G d
d„ deƒZ!da"da#d a$da%d a&da'dd„ Z(dd„ Z)dd„ Z*dd„ Z+dd„ Z,dd„ Z-dd„ Z.dd„ Z/dd„ Z0G dd„ dƒZ1G d d!„ d!ej2ƒZ2G d"d#„ d#ej3ƒZ4G d$d%„ d%ej5ƒZ5G d&d'„ d'ej6ƒZ6d(d)„ Z7d*d+„ Z8e9d,krÔe(ƒ  dS dS )-é    N)ÚListÚTuple)ÚconnectÚdefault)ÚConchOptions)Ú
ConchError)ÚchannelÚcommonÚ
connectionÚ
forwardingÚsession)ÚreactorÚstdioÚtask)ÚlogÚusage)ÚioTypeÚnetworkStringc                	   @   sø   e Zd ZU dZdZg d¢g d¢g d¢gZg d¢g d¢g d¢g d	¢g d
¢g d¢gZejdgej	dd�ej	dd�dœe 
¡ ej	dd�ej	ddd�gd�Zg Zeeeeeef f  ed< g Zeeeeeef f  ed< dd„ Zdd„ Zdd„ Zdd„ ZdS ) ÚClientOptionsz(Usage:   conch [options] host [command]
zYconch is a SSHv2 client that allows logging into a remote machine and executing commands.)ÚescapeÚeú~)ÚlocalforwardÚLNz<listen-port:host:port   Forward local port to remote address)ÚremoteforwardÚRNz<listen-port:host:port   Forward remote port to local address)ÚnullÚnzRedirect input from /dev/null.)ÚforkÚfz(Fork to background after authentication.)ÚttyÚtz-Tty; allocate a tty even if command is given.)ÚnottyÚTzDo not allocate a tty.)ÚnoshellÚNz"Do not execute a shell or command.)Ú	subsystemÚsz-Invoke command (mandatory) as SSH2 subsystem.)r    r"   zlisten-port:host:port)Údescr)r   r   ÚcommandÚargumentT)r(   Úrepeat)ÚmutuallyExclusiveÚ
optActionsÚextraActionsÚlocalForwardsÚremoteForwardsc                 C   sv   |dkr
d| d< dS |d dkr$t |ƒdkr$tt|d ƒd ƒ| d< dS t |ƒdkr0|| d< dS t d	|› d
�¡ dS )z:
        Set escape character; ``none'' = disable
        ÚnoneNr   r   ú^é   é   é@   zBad escape character 'z'.)ÚlenÚchrÚordÚsysÚexit)ÚselfÚesc© r=   ú=/usr/lib/python3/dist-packages/twisted/conch/scripts/conch.pyÚ
opt_escapeP   s   zClientOptions.opt_escapec                 C   ó8   |  d¡\}}}t|ƒ}t|ƒ}| j |||ff¡ dS )zH
        Forward local port to remote address (lport:host:port)
        ú:N)ÚsplitÚintr/   Úappend)r;   r   Ú	localPortÚ
remoteHostÚ
remotePortr=   r=   r>   Úopt_localforward]   ó   zClientOptions.opt_localforwardc                 C   r@   )zH
        Forward remote port to local address (rport:host:port)
        rA   N)rB   rC   r0   rD   )r;   r   rG   ÚconnHostÚconnPortr=   r=   r>   Úopt_remoteforwardf   rI   zClientOptions.opt_remoteforwardc                 G   s   || d< d  |¡| d< d S )NÚhostú r)   )Újoin)r;   rM   r)   r=   r=   r>   Ú	parseArgso   s   zClientOptions.parseArgsN)Ú__name__Ú
__module__Ú__qualname__ÚsynopsisÚlongdescÚoptParametersÚoptFlagsr   ÚCompletionsÚ	CompleterÚCompleteUserAtHostÚcompDatar/   r   r   rC   Ú__annotations__r0   r?   rH   rL   rP   r=   r=   r=   r>   r      s>   
 ÿøú	

þ
ýú  		r   c                  C   sº  t jdd … } d| v r#|  d¡}| ||d … |  } | |d |d …= | d d … D ],}z!|  |¡}|d d… dkrK| |d  d dkrKg | ||d …< W q) tyU   Y q)w tƒ azt | ¡ W n# tjyƒ } zt	d|› �ƒ t 
¡  t  d¡ W Y d }~nd }~ww td	 r­td
 ržtd
 dkr–t j}nttd
 dƒ}nt j}t j}t |¡ |t _nt ¡  tƒ  t j ¡ }zt |¡aW n tyË   d aY nw zt tjdd„ ¡}W n tyâ   d }Y nw z0t ¡  W trót |tjt¡ |rüt tj|¡ td �rtd �std �st tjtj ¡ n.t�rt |tjt¡ |�r)t tj|¡ td �r3td �s8td �sAt tjtj ¡ w w t j !¡ �rVtd �sVt	d "td ¡ƒ t  t#¡ d S )Nr4   z-lr3   é   z-or   ú-zERROR: r   Úlogfileza+c                  W   s   t  dt¡S ©Nr   )r   Ú	callLaterÚ	reConnect)Úar=   r=   r>   Ú<lambda>§   s    zrun.<locals>.<lambda>r)   r    r"   zConnection to {} closed.rM   )$r9   ÚargvÚindexÚ
ValueErrorr   ÚoptionsÚparseOptionsr   Ú
UsageErrorÚprintÚopt_helpr:   ÚstdoutÚopenÚstderrr   ÚstartLoggingÚdiscardLogsÚ	doConnectÚstdinÚfilenor    Ú	tcgetattrÚoldÚBaseExceptionÚsignalÚSIGUSR1r   ÚrunÚ	tcsetattrÚTCSANOWÚSIGWINCHÚSIG_DFLÚisattyÚformatÚ
exitStatus)ÚargsÚiÚargÚur   ÚrealoutÚfdÚoldUSR1r=   r=   r>   rz   }   s„   

$€ÿ€ý

ÿ
ÿÿ
€ûÿrz   c                  C   s,   ddl m}  dat dt¡ t |  ¡ ¡ ‚ )Nr   ©Úfailurer3   g{®Gáz„?)	Útwisted.pythonrŠ   r�   r   ra   Ú_stopReactorr   ÚerrÚFailurer‰   r=   r=   r>   ÚhandleError¹   s
   r�   c                   C   s$   zt  ¡  W d S  ty   Y d S w ©N)r   Ústoprw   r=   r=   r=   r>   rŒ   Ã   s
   ÿrŒ   c                  C   sÔ   dt d v rt d  dd¡\t d< t d< t jsddgt _t d } t d s*t ¡ t d< t d s3dt d< ntt d ƒt d< t d } t d }tj}t d	 sRt | t ¡t d	< t 	t d t t
ƒ ¡}t | |t ||¡ t¡ d S )
Nú@rM   r4   Úuserz~/.ssh/id_rsaz~/.ssh/id_dsaÚporté   zhost-key-algorithms)rh   rB   Ú	identitysÚgetpassÚgetuserrC   r   ÚverifyHostKeyÚgetHostKeyAlgorithmsÚSSHUserAuthClientÚSSHConnectionr   Ú
addErrbackÚ_ebExit)rM   r”   ÚvhkÚuaor=   r=   r>   rr   Ê   s"   

rr   c                 C   s   d| › �a t dt¡ d S )Nzconch: exiting with error çš™™™™™¹?)r�   r   ra   rŒ   )r   r=   r=   r>   rž   ß   s   
rž   c                  C   s.  t tjdƒr
ttƒ tjr&tjD ]\} }t | t 	t|t
¡¡}tj |¡ qtjrItjD ]\}}t d|› d|› �¡ t ||¡ q,t ddt¡ td rQtd rWt tƒ ¡ td r“t ¡ rdt d	¡ t ¡  td
ƒD ](}zt |¡ W ql ty’ } zd	d l}|j|jkrˆ‚ W Y d }~qld }~ww d S d S )NÚ
sendIgnorez!asking for remote forwarding for rA   ÚbeforeÚshutdownr$   Úagentr   r   é   )ÚhasattrÚconnÚ	transportÚ
_KeepAliverh   r/   r   Ú	listenTCPr   ÚSSHListenForwardingFactoryÚ SSHListenClientForwardingChannelrD   r0   r   ÚmsgÚrequestRemoteForwardingÚaddSystemEventTriggerÚbeforeShutdownÚopenChannelÚ
SSHSessionÚosr   Ú_exitÚsetsidÚrangeÚcloseÚOSErrorÚerrnoÚEBADF)rE   Úhostportr'   rG   rƒ   r   rº   r=   r=   r>   Ú	onConnectå   sD   ÿþ
ÿ€ýùr½   c                   C   s   t ƒ  tjj ¡  d S r�   )r±   r¨   r©   ÚloseConnectionr=   r=   r=   r>   rb   	  s   rb   c                  C   s8   t j} | D ]\}}t d|› d|› �¡ t |¡ qd S )Nzcancelling rA   )rh   r0   r   r®   r¨   ÚcancelRemoteForwarding)r0   rG   r¼   r=   r=   r>   r±     s
   þr±   c                   C   s   t d st dt¡ d S d S )NÚ	reconnectr¡   )rh   r   ra   rŒ   r=   r=   r=   r>   ÚstopConnection  s   ÿrÁ   c                   @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
rª   c                 C   s*   || _ d | _t | j¡| _| j d¡ d S )Ni,  )r¨   ÚglobalTimeoutr   ÚLoopingCallÚ
sendGlobalÚlcÚstart)r;   r¨   r=   r=   r>   Ú__init__  s   z_KeepAlive.__init__c                 C   s2   | j jdddd�}| | j¡ t d| j¡| _d S )Ns"   conch-keep-alive@twistedmatrix.comó    r4   ©Ú	wantReplyé   )r¨   ÚsendGlobalRequestÚaddBothÚ	_cbGlobalr   ra   Ú	_ebGlobalrÂ   )r;   Údr=   r=   r>   rÄ   !  s
   ÿz_KeepAlive.sendGlobalc                 C   s   | j r| j  ¡  d | _ d S d S r�   )rÂ   Úcancel)r;   Úresr=   r=   r>   rÎ   (  s   

þz_KeepAlive._cbGlobalc                 C   s    | j rd | _ | jj ¡  d S d S r�   )rÂ   r¨   r©   r¾   ©r;   r=   r=   r>   rÏ   -  s   þz_KeepAlive._ebGlobalN)rQ   rR   rS   rÇ   rÄ   rÎ   rÏ   r=   r=   r=   r>   rª     s
    rª   c                   @   sL   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dS )rœ   c                 C   s   | a g | _i | _tƒ  d S r�   )r¨   r/   r0   r½   rÓ   r=   r=   r>   ÚserviceStarted4  s   
zSSHConnection.serviceStartedc                 C   s(   | j }g | _ |D ]}| ¡  qtƒ  d S r�   )r/   r¾   rÁ   )r;   Úlfr'   r=   r=   r>   ÚserviceStopped;  s
   

zSSHConnection.serviceStoppedc                 C   sX   t  d|f¡}| jd|dd�}t d|› d|› �¡ | | j||¡ | | j||¡ d S )Nú0.0.0.0s   tcpip-forwardr4   rÉ   zrequesting remote forwarding rA   )	r   ÚpackGlobal_tcpip_forwardrÌ   r   r®   ÚaddCallbackÚ_cbRemoteForwardingr�   Ú_ebRemoteForwarding)r;   rG   r¼   ÚdatarÐ   r=   r=   r>   r¯   B  s
   z%SSHConnection.requestRemoteForwardingc                 C   s4   t  d|› d|› �¡ || j|< t  t| jƒ¡ d S )Nzaccepted remote forwarding rA   )r   r®   r0   Úrepr)r;   ÚresultrG   r¼   r=   r=   r>   rÚ   I  s   
z!SSHConnection._cbRemoteForwardingc                 C   s&   t  d|› d|› d�¡ t  |¡ d S )Nzremote forwarding rA   z failed©r   r®   )r;   r   rG   r¼   r=   r=   r>   rÛ   N  s   z!SSHConnection._ebRemoteForwardingc                 C   s^   t  d|f¡}|  d|¡ t d|› �¡ z| j|= W n	 ty$   Y nw t t| jƒ¡ d S )Nr×   s   cancel-tcpip-forwardzcancelling remote forwarding )r   rØ   rÌ   r   r®   r0   Ú	ExceptionrÝ   )r;   rG   rÜ   r=   r=   r>   r¿   R  s   ÿz$SSHConnection.cancelRemoteForwardingc                 C   s|   t  d|›�¡ t |¡\}}t  | j¡ t  |¡ |d | jv r8| j|d  }t  d|› �¡ t|||| d�S ttjdƒ‚)NzFTCP r4   zconnect forwarding )ÚremoteWindowÚremoteMaxPacketr¨   zdon't know about that port)	r   r®   r   ÚunpackOpen_forwarded_tcpipr0   ÚSSHConnectForwardingChannelr   r
   ÚOPEN_CONNECT_FAILED)r;   Ú
windowSizeÚ	maxPacketrÜ   ÚremoteHPÚorigHPÚ	connectHPr=   r=   r>   Úchannel_forwarded_tcpip\  s   
ÿÿz%SSHConnection.channel_forwarded_tcpipc                 C   sV   t  d|› �¡ t  | j¡ t| jƒdkrt  d¡ tƒ  d S | jjd  | |¡ d S )Nzconnection closing r4   zstopping connectionr   )r   r®   Úchannelsr6   rÁ   Ú	__class__Ú	__bases__ÚchannelClosed)r;   r   r=   r=   r>   rï   l  s   

zSSHConnection.channelClosedN)rQ   rR   rS   rÔ   rÖ   r¯   rÚ   rÛ   r¿   rë   rï   r=   r=   r=   r>   rœ   3  s    
rœ   c                   @   sp   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )r³   s   sessionc           	         sÎ  t  dˆ j› d�¡ td rˆ jjˆ dddd�}| dd	„ ¡ td
 r%d S td r-td s1td s4tƒ  t 	¡ }td rHtd sHdˆ _
ˆ j|_nˆ j|_‡ fdd	„|_t |¡ˆ _d}td rnˆ j ˆ dt td ¡¡ d S td r¯td r tjd }t |tjd¡}t d|¡}t ||d¡}ˆ j ˆ d|¡ t tjˆ j¡ ˆ j ˆ dt td ¡¡ d S td sÝtjd }t |tjd¡}t d|¡}t ||d¡}ˆ j ˆ d|¡ t tjˆ j¡ ˆ j ˆ dd¡ d S )Nzsession z openr¥   s   auth-agent-req@openssh.comrÈ   r4   rÉ   c                 S   s
   t  | ¡S r�   rß   ©Úxr=   r=   r>   rd   �  s   
 z(SSHSession.channelOpen.<locals>.<lambda>r$   r)   r    r"   r   c                    s   ˆ   ¡ S r�   )ÚsendEOFrð   rÓ   r=   r>   rd   Œ  s    r   r&   s	   subsystemÚTERMÚ12345678Ú4HÚ s   pty-reqs   execs   shell)r   r®   Úidrh   r¨   ÚsendRequestrÍ   Ú_enterRawModer   ÚSSHSessionClientÚ
escapeModeÚhandleInputÚdataReceivedÚwriteÚconnectionLostr   Ú
StandardIOr	   ÚNSr´   ÚenvironÚfcntlÚioctlr    Ú
TIOCGWINSZÚstructÚunpackÚpackRequest_pty_reqrx   r}   Ú_windowResized)	r;   ÚfoorÐ   Úcr‡   ÚtermÚwinszÚwinSizeÚ
ptyReqDatar=   rÓ   r>   ÚchannelOpen{  sJ   ÿ


zSSHSession.channelOpenc              	   C   s&  |dv rd| _ |  |¡ d S | j dkr|td krd| _ d S | j dkr‰d| _ |dkr4t d¡ tƒ  d S |dkrDdd	„ }t d
|¡ d S |dkrUt d¡ | jj	 
¡  d S |dkr€| j d¡ | jj ¡ }| ¡  |D ]}| j td || jj| ¡ƒ¡ qkd S |  d| ¡ d S d
| _ |  |¡ d S )N)ó   
ó   r4   r   r3   ó   .zdisconnecting from escapeó   c                   S   s6   t ƒ  tj ¡  tj ¡  t t ¡ tj	¡ t
ƒ  d S r�   )Ú_leaveRawModer9   rm   Úflushrs   r´   ÚkillÚgetpidrx   ÚSIGTSTPrù   r=   r=   r=   r>   Ú_´  s
   


z!SSHSession.handleInput.<locals>._r   ó   Rzrekeying connectionó   #s'   
The following connections are open:
z
  #{} {}
ó   ~)rû   rþ   rh   r   r®   rÁ   r   ra   r¨   r©   ÚsendKexInitr   rì   ÚkeysÚsortr   r€   )r;   Úcharr  rì   Ú	channelIdr=   r=   r>   rü   ¦  sF   



ÿÿÿzSSHSession.handleInputc                 C   s   | j  |¡ d S r�   )r   rþ   ©r;   rÜ   r=   r=   r>   rý   Ó  ó   zSSHSession.dataReceivedc                 C   sT   |t jkr(t dt|ƒ› d�¡ ttjƒtkr tjj	 
|¡ d S tj 
|¡ d S d S )Nzgot z stderr data)r
   ÚEXTENDED_DATA_STDERRr   r®   r6   r   r9   ro   ÚstrÚbufferrþ   )r;   r!   rÜ   r=   r=   r>   ÚextReceivedÖ  s   
ûzSSHSession.extReceivedc                 C   s   t  d¡ | j ¡  d S )Nzgot eof)r   r®   r   ÚloseWriteConnectionrÓ   r=   r=   r>   ÚeofReceivedÞ  s   
zSSHSession.eofReceivedc                 C   s    t  d| › �¡ | j | ¡ d S )Nzremote side closed )r   r®   r¨   Ú	sendCloserÓ   r=   r=   r>   ÚcloseReceivedâ  s   zSSHSession.closeReceivedc                 C   s&   t  d| › �¡ t  t| jjƒ¡ d S )Nzclosed )r   r®   rÝ   r¨   rì   rÓ   r=   r=   r>   Úclosedæ  s   zSSHSession.closedc                 C   s(   t t d|¡d ƒat dt› �¡ d S )Nz>Lr   zexit status: )rC   r  r  r�   r   r®   r#  r=   r=   r>   Úrequest_exit_statusë  s   zSSHSession.request_exit_statusc                 C   s   | j  | ¡ d S r�   )r¨   rò   rÓ   r=   r=   r>   rò   ð  r$  zSSHSession.sendEOFc                 C   ó   | j  ¡  d S r�   )r   ÚpauseProducingrÓ   r=   r=   r>   ÚstopWritingó  ó   zSSHSession.stopWritingc                 C   r/  r�   )r   ÚresumeProducingrÓ   r=   r=   r>   ÚstartWritingö  r2  zSSHSession.startWritingc                 G   sZ   t  dtjd¡}t d|¡}|d |d |d |d f}| j | dtjdg|¢R Ž ¡ d S )	Nr   rô   rõ   r4   r3   r¦   s   window-changez!4L)	r  r  r    r  r  r  r¨   rø   Úpack)r;   r‚   r  r  ÚnewSizer=   r=   r>   r	  ù  s   "zSSHSession._windowResizedN)rQ   rR   rS   Únamer  rü   rý   r(  r*  r,  r-  r.  rò   r1  r4  r	  r=   r=   r=   r>   r³   w  s    +-r³   c                   @   ó   e Zd ZdS )r­   N©rQ   rR   rS   r=   r=   r=   r>   r­      ó    r­   c                   @   r8  )rä   Nr9  r=   r=   r=   r>   rä     r:  rä   c                  C   s*   t sd S tj ¡ } t | tjt¡ da d S r`   )Ú
_inRawModer9   rs   rt   r    r{   r|   Ú_savedRawMode)r‡   r=   r=   r>   r    s
   
r  c                  C   sV  t rd S tj ¡ } zt | ¡}|d d … }W n ty%   t d¡ Y d S w |d tj	B |d< |d tj
tjB tjB tjB tjB tjB tjB  @ |d< ttdƒrZ|d tj @ |d< |d tjtjB tjB tjB tjB tjB tjB  @ |d< ttdƒr…|d tj @ |d< |d tj @ |d< d|d tj< d|d tj< |at | tj|¡ da d S )Nznot a typewriter!r   ÚIUCLCr¦   ÚIEXTENr4   é   ) r;  r9   rs   rt   r    ru   rw   r   r®   ÚIGNPARÚISTRIPÚINLCRÚIGNCRÚICRNLÚIXONÚIXANYÚIXOFFr§   r=  ÚISIGÚICANONÚECHOÚECHOEÚECHOKÚECHONLr>  ÚOPOSTÚVMINÚVTIMEr<  r{   r|   )r‡   rv   Únewr=   r=   r>   rù     sd   

ÿÿþýüûú
ÿ
	ÿþýüûú
ÿ
	rù   Ú__main__):r  r—   r´   rx   r  r9   r    Útypingr   r   Útwisted.conch.clientr   r   Útwisted.conch.client.optionsr   Útwisted.conch.errorr   Útwisted.conch.sshr   r	   r
   r   r   Útwisted.internetr   r   r   r‹   r   r   Útwisted.python.compatr   r   r   rh   r¨   r�   rv   r;  r<  rz   r�   rŒ   rr   rž   r½   rb   r±   rÁ   rª   rœ   Ú
SSHChannelr³   r­   rä   r  rù   rQ   r=   r=   r=   r>   Ú<module>   sT   W<
$D 
	2
ÿ