ϪfN5dZddlZddlZddlZddlZddlmZddlmZm Z m Z ddl m Z m Z mZddlmZmZddlmZddlmZeZGd d e j.ZGd d ej2ZGd dZdZdZgdZeej>Gddej2Z GddejBZ"dZ#dZ$dZ%dZ&y)z This module contains the implementation of SSHSession, which (by default) allows access to a shell and a python interpreter over SSH. Maintainer: Paul Swartz N) implementer)EnvironmentVariableNotPermittedISessionISessionSetEnv)channelcommon connection) interfacesprotocol)Logger) networkStringc\eZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d Zd ZdZy) SSHSessiona A generalized implementation of an SSH session. See RFC 4254, section 6. The precise implementation of the various operations that the remote end can send is left up to the avatar, usually via an adapter to an interface such as L{ISession}. @ivar buf: a buffer for data received before making a connection to a client. @type buf: L{bytes} @ivar client: a protocol for communication with a shell, an application program, or a subsystem (see RFC 4254, section 6.5). @type client: L{SSHSessionProcessProtocol} @ivar session: an object providing concrete implementations of session operations. @type session: L{ISession} ssessioncttjj|g|i|d|_d|_d|_yN)r SSHChannel__init__bufclientsession)selfargskws ;/usr/lib/python3/dist-packages/twisted/conch/ssh/session.pyrzSSHSession.__init__8s5##D64626  rcZtj|\}}tjd||jj ||}|rIt |}t|}|j||jt|||_ ytjdy)Nz"Asking for subsystem "{subsystem}") subsystemzFailed to get subsystemr) rgetNSloginfoavatarlookupSubsystemSSHSessionProcessProtocolwrapProcessProtocolmakeConnection wrapProtocolrerror)rdatarignoredrppprotos rrequest_subsystemzSSHSession.request_subsystem>s#\\$/ 7 5K,,Y= *40B'+E  ! !% (   l62 3DK II/ 0rctjd|jst|j|_ t |}|jj |||_y#t$rtjdYywxYw)Nz Getting shellrzError getting shellr) r r!rrr"r$ openShellr Exceptionfailure)rr)r+s r request_shellzSSHSession.request_shellMss !||#DKK0DL *40B LL " "2 & DK   KK- . s&A++B  B cX|jst|j|_tj|\}}t j d| t|}|jj||||_ y#t$rt jd|YywxYw)NzExecuting command "{f}")frzError executing command "{f}"r) rrr"rrr r!r$ execCommandrr0r1)rr)r4r+s r request_execzSSHSession.request_exec[s||#DKK0DL,,t$4 *a0 *40B LL $ $R + DK   KK71K = s'B B)(B)c"|jst|j|_t|\}}}tj d|| |jj |||y#t$rtjdYywxYw)Nz-Handling pty request: {term!r} {windowSize!r})term windowSizerzError handling pty requestr) rrr"parseRequest_pty_reqr r!getPtyr0r1)rr)r8r9modess rrequest_pty_reqzSSHSession.request_pty_reqjs||#DKK0DL"6t"<j%  ;!    LL  j% 8    KK4 5 sA--B Bc^|jst|j|_tj|jsyt j |d\}}} |jj||y#t$rYyt$rtjd|YywxYw)ag Process a request to pass an environment variable. @param data: The environment variable name and value, each encoded as an SSH protocol string and concatenated. @type data: L{bytes} @return: A true value if the request to pass this environment variable was accepted, otherwise a false value. rrz)Error setting environment variable {name})name) rrr"r providedByrrsetEnvrr0r r1)rr)r@values r request_envzSSHSession.request_env{s||#DKK0DL((6"LLq1eT  LL  e , /   KKC$K O s"A?? B, B,+B,c|jst|j|_t|} |jj |y#t $rt jdYywxYw)NrzError changing window sizer)rrr"parseRequest_window_change windowChangedr0r r1)rr)winSizes rrequest_window_changez SSHSession.request_window_changes`||#DKK0DL,T2  LL & &w /    KK4 5 sAA0/A0c|js|xj|z c_y|jjj|yN)rr transportwriterr)s r dataReceivedzSSHSession.dataReceiveds2{{ HH H  ##D)rc|tjk(rT|jrGt|jjdr&|jjj |yyyt jd|y)NwriteErrzWeird extended data: {dataType})dataType)r EXTENDED_DATA_STDERRrhasattrrLrQr warn)rrRr)s r extReceivedzSSHSession.extReceiveds] z66 6{{wt{{'<'L)rCrrrosrstatusr r!rrY sendRequestrNSr rstructpackr])rrtrr coreDumpeds rruz&SSHSessionProcessProtocol.processEndedIsM  ,,Czz%--cjj92{D1=",,JJCHHBGHT!%JHH-wH?!&J !!--LL"IImGABK89 *u7iin%iin%)1CLLI !!--LL.&++dCLL2Q ##%rc^|jjjjS)z> Return the host from my session's transport. )rrYrLgetHostr[s rrz!SSHSessionProcessProtocol.getHostj#||  **2244rc^|jjjjS)z> Return the peer from my session's transport. )rrYrLgetPeerr[s rrz!SSHSessionProcessProtocol.getPeerprrc:|jj|yrKrrNs rrMzSSHSessionProcessProtocol.writevrrcX|jjdj|yr)rrMr|r}s rrz'SSHSessionProcessProtocol.writeSequenceys 388C=)rc8|jjyrKrr[s rr]z(SSHSessionProcessProtocol.loseConnection|rrrK)r`rarbrcrrrlrorrrrrrrurrrMrr]rdrrr$r$sW &$ !I)! &1(&B5 5 !*&rr$ceZdZdZy)SSHSessionClientcT|jr|jj|yyrK)rLrMrNs rrOzSSHSessionClient.dataReceiveds >> NN  & rN)r`rarbrOrdrrrrs'rrc rtj|\}}tjd|dd\}}}}tj|dd\}}||||f} t dt |dz dD cgc]5} t || | dztjd|| dz| dzdf7}} || |fScc} w)zParse the data from a pty-req request into usable data. @returns: a tuple of (terminal type, (rows, cols, xpixel, ypixel), modes) >4LNrrr)rrrunpackrangelenord) r)r8restcolsrowsxpixelypixelr<r*rHis rr:r:s d#JD$!'ud3Bi!@D$\\$rs),NE7T66*Gq#e*q.!,   U1q1u   dE!a%!a%4H I! LM E  %   s3:B4c|\}}}}tj|}tjd||||}tj|} ||z| zS)z Pack a pty-req request so that it is suitable for sending. NOTE: modes must be packed before being sent here. @type geometry: L{tuple} @param geometry: A tuple of (rows, columns, xpixel, ypixel) r)rrrr) r8geometryr<rrrr termPacked winSizePacked modesPackeds rpackRequest_pty_reqrsS$, T44JKKtT66BM))E"K  % 33rcDtjd|\}}}}||||fS)zzParse the data from a window-change request into usuable data. @returns: a tuple of (rows, cols, xpixel, ypixel) r)rr)r)rrrrs rrFrFs, "(ud!;D$ vv %%rcB|\}}}}tjd||||S)z Pack a window-change request so that it is suitable for sending. @type geometry: L{tuple} @param geometry: A tuple of (rows, columns, xpixel, ypixel) r)rr)rrrrrs rpackRequest_window_changers)$, T4 ;;udD&& 99r)'rcrrrrzope.interfacertwisted.conch.interfacesrrrtwisted.conch.sshrrr twisted.internetr r twisted.loggerr twisted.python.compatr r rrProcessProtocolrfrwr%r'r ITransportr$rrr:rrFrrdrrrs  & :91!/ hc0##c0L*x//*$;;"" " Z " "#t& 8 8t&$t&n'x(('  4 &:r