
    iL!                         d dl Z d dlZd dlZd dlZd dlmZmZmZmZ d dl	m
Z
 ddlmZmZmZmZ ddlmZmZ ddlmZmZ  ej,                  e      Z G d d	e
j2                        Z G d
 de      Z G d de      Zy)    N)DictListOptionalTuple)ferny   )
JsonObject	JsonValueget_strget_str_or_none)Peer	PeerError)RouterRoutingRulec                       e Zd ZU  ej                  d      Zee   ed<   e	e
eeeeef      ed<   ee   ed<   eed<   dee   fdZdeded	ed
ededefdZdedededee   fdZy)PasswordResponderz!Enter passphrase for key '(.*)': passwordhostkeys_seenerror_messagepassword_attemptsc                 <    || _         g | _        d | _        d| _        y )Nr   )r   r   r   r   )selfr   s     0/usr/lib/python3/dist-packages/cockpit/remote.py__init__zPasswordResponder.__init__   s!     !!"    reasonhost	algorithmkeyfingerprintreturnc                 L   K   | j                   j                  |||||f       yw)NF)r   append)r   r   r   r   r   r    s         r   
do_hostkeyzPasswordResponder.do_hostkey%   s(     !!64C"MNs   "$messagesprompthintc                 ^  K   t         j                  d||       t        j                  j	                  |      }|2|j                  d      }t         j                  d|       d| | _        y | j                  J | j                  dk(  sJ | xj                  dz  c_        | j                  S w)NzGot askpass(%s): %sr   zCThis is a passphrase request for %s, but we don't do those.  Abort.zlocked identity: r   )	loggerdebugr   PASSPHRASE_RE	fullmatchgroupr   r   r   )r   r%   r&   r'   matchpaths         r   
do_askpasszPasswordResponder.do_askpass)   s     *D&9!//99&A ;;q>DLL^`de#4TF!;D}}(((%%***!#}}s   B+B-N)__name__
__module____qualname__recompiler+   r   str__annotations__r   r   intr   boolr$   r0    r   r   r   r      s    BJJCDMsmc3S#5677C= ## #s # # C ^a fj  c  RU r   r   c                        e Zd ZU dZeej                     ed<   eed<   ee   ed<   ee   ed<   e	ed<   ddZ
dd	d
d	deddf fdZdeddfdZddZdededee   dede	ddf fdZ xZS )SshPeerNsessionr   userr   privater!   c           
        K   | j                   J t        j                  d| j                  | j                  | j
                         | j                  j                  d      \  }}}|r|j                         r|}t        |      }n| j                  }d }t        | j                        }ddi}| j                  |j                  d       n|j                  dd       	 | j                   j                  || j                  || j
                  ||	       d {    | j                   j?                  dg      }| jA                  |g        d {    y 7 ;# t        t        j                  f$ r4}t        j                  d
||       t!        ddt#        |            |d }~wt$        j&                  $ r}|j(                  r#|j(                  d   \  }	}}
}}| d|
 d| |d}ni }t+        |t$        j,                        rd}n| j
                  rd}nd}t        j                  dt/        |      || j
                  |j(                  ||       t!        |||i       |d }~wt$        j0                  $ r}t        j                  d||       t2        j5                  |j6                  d      }d|v r&| j                  |j8                  dk(  rd|d<   nd|d<   t!        d|j:                  xs d|      |d }~wt$        j<                  $ r3}t        j                  d||       t!        dt#        |            |d }~ww xY w7 w)Nz1Starting ssh session user=%s, host=%s, private=%s:StrictHostKeyCheckingyes1)NumberOfPasswordPromptsno)PasswordAuthenticationKbdInteractiveAuthentication)
login_nameporthandle_host_keyoptionsinteraction_responderz connecting to host %s failed: %szno-host)errormessager    )zhost-keyzhost-fingerprintzinvalid-hostkeyzunknown-hostkeyzunknown-hostzPSshPeer got a %s %s; private %s, seen hostkeys %r; raising %s with extra args %r)rN   auth_method_resultsz$authentication to host %s failed: %sznot-providedr   z	not-trieddeniedzauthentication-failedz)unknown failure connecting to host %s: %szinternal-error)rO   zcockpit-bridge)!r=   r)   r*   r>   r   r?   
rpartitionisdigitr8   r   r   updateconnectOSErrorsocketgaierrorr   r6   r   SshHostKeyErrorr   
isinstanceSshChangedHostKeyErrortypeSshAuthenticationErrordictfromkeysmethodsr   r   SshErrorwrap_subprocess_argsspawn)r   basenamecolonportstrr   rJ   	responderrL   exc_reasonr   r   r    
error_argsrN   resultsargss                    r   do_connect_transportzSshPeer.do_connect_transportC   s    ||'''H$))UYU^U^`d`l`lm#'99#7#7#< %W__&Dw<D99DD%dmm4	*E2==$NN3N7NN$UYNZ.	I,,&&t		7;||W=F ' H H H^ ||002B1CDjjr"""aH ) 	SLL;T3GIYCIsR$$ 	]&& >G=T=TUV=W:y#{-1F!I;au*E[fg

#u;;<)) 'LLkcCy7N7NPUWacE:UPRSY\\++ 	BLL?sKmmCKK@GW$)B..!3*5GJ'*2GJ'3"+"9"9"T=T079>AB ~~ 	ILLDdCP,c#h?SH	I
 	#sn   CL> 8E EE 1L>L;L>E L8//FL84B'IL81A>K//L8.L33L88L>z
str | Noner-   rO   c                 n    || j                   k(  r| j                          y |t        |   |||       y y N)r   closesuperdo_kill)r   r   r-   rO   	__class__s       r   rs   zSshPeer.do_kill   s2    499JJL\GOD%1 r   c                     t        |d      j                  d      r6t        |d      }| j                  d|| j                  xs d       d | _        y y )N	challengezplain1:cookie	authorize )commandrw   response)r   
startswithwrite_controlr   )r   rO   rw   s      r   do_authorizezSshPeer.do_authorize   sN    7K(33I>Wh/F{6DMML_]_` DM ?r   c                     d | _         y rp   )r   )r   s    r   do_superuser_init_donezSshPeer.do_superuser_init_done   s	    r   routerrL   c                    t         |   |       || _        || _        t	        |dd       | _        || _        t        j                         | _	        t        |dd       }|dv rd}nd|i}| j                  ||       y )Nr   zinit-superuser)NnoneFid)	init_host	superuser)rr   r   r   r>   r   r   r?   r   Sessionr=   r   start_in_background)	r   r   r   r>   rL   r?   init_superuserr   rt   s	           r   r   zSshPeer.__init__   s}     		T:}} )2BDI^+I~.I  49 Er   r!   N)r1   r2   r3   r=   r   r   r   r7   r6   r9   rn   r	   rs   r~   r   r   r   __classcell__rt   s   @r   r<   r<   <   s    '+GXemm$+
I
3-smME#N2L 2 2
 2W[ 2!J !4 !Fv FS F FPZ Fhl Fqu F Fr   r<   c                   j     e Zd ZU eeeee   ee   f   ef   ed<    fdZ	de
dee   fdZddZ xZS )HostRoutingRuleremotesc                 2    t         |   |       i | _        y rp   )rr   r   r   )r   r   rt   s     r   r   zHostRoutingRule.__init__   s     r   rL   r!   c                 ^     j                   J  j                   j                  J t        |d j                   j                        }| j                   j                  k(  ry t        |dd       }|t        j                         k(  rd }|s|j                  d      \  }}}|xs d }t        |dd       dk(  rt        |d      }nd }t        |t              sJ |t        |t              sJ |t        |t              sJ |||ft        j                  d|       t        j                  d        j                  vrat        j                  d	 j                         t         j                   ||||d u
      }|j                   fd       | j                  <    j                     S )Nr   r>   @r=   r?   channelz!Request for channel %s is remote.zkey=%szC%s is not among the existing remotes %s.  Opening a new connection.)r?   c                  :    j                   j                         S rp   )r   __delitem__)r   r   s   r   <lambda>z,HostRoutingRule.apply_rule.<locals>.<lambda>   s    4<<+C+CC+H r   )r   r   r   getpassgetuserrS   r[   r6   r)   r*   r   r<   add_done_callback)	r   rL   r   r>   user_from_host_noncepeerr   s	   `       @r   
apply_rulezHostRoutingRule.apply_rule   s   {{&&&{{$$000w(=(=>4;;(((w- 7??$$D#'??3#7 NAq!)TD7It,	9GY/EE$$$$|z$444}
5# 666D%8'BXs#dll"LL^`ceieqeqr4;;dGURVEVWD""#HI $DLL||C  r   c                 t    t        | j                  j                               D ]  }|j                           y rp   )setr   valuesrq   )r   r   s     r   shutdownzHostRoutingRule.shutdown   s,    ++-. 	DJJL	r   r   )r1   r2   r3   r   r   r6   r   r   r7   r   r	   r   r   r   r   s   @r   r   r      sI    %Xc]HSM9:D@AA&!* &!$ &!Pr   r   )r   loggingr4   rX   typingr   r   r   r   cockpit._vendorr   jsonutilr	   r
   r   r   r   r   r   r   r   r   	getLoggerr1   r)   AskpassHandlerr   r<   r   r:   r   r   <module>r      sj      	  . . ! E E ! '			8	$$,, $NmFd mF`1k 1r   