^cYddlZddlZddlmZddlmZddlmZmZmZm Z m Z m Z ddl m Z mZmZdZdd ZGd d Zd Zdd ZdZdZGdde jZddZddZdS)N)_)getattr)errorpycompatutilwireprototypeswireprotov1peerwireprotov1server)procutil stringutilurlutilcj|s|Stjd|r|Sd|ddzS)z=quote a string for the remote shell ... which we assume is shs[a-zA-Z0-9@%_+=:,./-]*$s'%s''s'\'')rematchreplace)ss 3/usr/lib/python3/dist-packages/mercurial/sshpeer.py _serverquotersB  x*A.. QYYtX.. ..Fc|r^|jsYtj|}|rE|r|jn|j}|D]"}|t d|ddSdSdSdS)zYdisplay all data currently available on pipe as remote output. This is non blocking.remote:  N)closedr readpipewarnstatus splitlinesr)uipiperrdisplayls r_forwardoutputr$&s 2DK2  d # #  2!%4bgg29G\\^^ 2 2+51111 2222 2 2 2 2rc`eZdZdZdZdZdZdZdZdZ d d Z d Z e d Z d ZdS) doublepipea3Operate a side-channel pipe in addition of a main one The side-channel pipe contains server output to be forwarded to the user input. The double pipe will behave as the "main" pipe, but will ensure the content of the "side" pipe is properly processed while we wait for blocking call on the "main" pipe. If large amounts of data are read from "main", the forward will cease after the first bytes start to appear. This simplifies the implementation without affecting actual output of sshpeer too much as we rarely issue large read for data not yet emitted by the server. The main pipe is expected to be a 'bufferedinputpipe' from the util module that handle all the os specific bits. This class lives in this module because it focus on behavior specific to the ssh protocol.c0||_||_||_dSN)_ui_main_side)selfr mainsides r__init__zdoublepipe.__init__Cs  rc|t|jtjr|jjrdS|j|jg} tj|}n#t$r|}YnwxYw|j|v|j|vfS)zwait until some data are available on main or side return a pair of boolean (ismainready, issideready) (This will only wait for data if the setup is supported by `util.poll`) )TT) isinstancer*rbufferedinputpipe hasbufferfilenor+pollNotImplementedError)r,fdsacts r_waitzdoublepipe._waitHs tz4#9 : :  $  :z  ""DJ$5$5$7$78 )C..CC"   CCC  !!##s*DJ,=,=,?,?3,FGGs!A66 BBc.|d|S)Nswrite_call)r,datas rwritezdoublepipe.write^szz(D)))rcv|d|}|dkr|st|j|j|S)Nsreadrr<r$r)r+r,sizers rreadzdoublepipe.readas= JJw % % 199Q9 48TZ 0 0 0rcv|d|}|dkr|st|j|j|S)Nsunbufferedreadrr@rAs runbufferedreadzdoublepipe.unbufferedreadls> JJ($ / / 199Q9 48TZ 0 0 0rc,|dS)Nsreadliner;r,s rreadlinezdoublepipe.readlinewszz+&&&rNc ||r |jjrt|j|jdS |\}}|rt|j|j|r,t |j|}| |S||Sb)zBcall on "main", forward output of "side" while blockingNr)r*rr$r)r+r9r)r,methnamer= mainready sidereadymeths rr<zdoublepipe._callzs  T dj.?  48TZ 0 0 03 &#'::<< Iy 5tx444 &tz844<466M4::% &rc4|jSr()r*closerHs rrPzdoublepipe.closez!!!rc|jjSr()r*rrHs rrzdoublepipe.closeds z  rc4|jSr()r*flushrHs rrTzdoublepipe.flushrQrr()__name__ __module__ __qualname____doc__r/r9r>rDrFrIr<rPpropertyrrTrrr&r&2sBB  HHH,***      '''&&&&""""!!X!"""""rr&cd}|r|jsd}||r|jsd}||r^|jsWd} |D]%}|td|&n#tt f$rYnwxYw||r||d|zdSdSdS)z)Clean up pipes used by an SSH connection.FTrNs.missing close on SSH connection created at: %s)rrPrrIOError ValueError develwarn)r pipeipipeopipeer didsomethingr#s r _cleanuppipesrcs L U\   U\   U\    - - !K..!,,,, -$    D   P( G$NOOOOO PP((s (A66B  B c d||tjdt|t|fzfz}|d|ztj|d|\}}} } | ||| fS)zCreate an SSH connection to a server. Returns a tuple of (process, stdin, stdout, stderr) for the spawned process. %s %s %ss%s -R %s serve --stdio running %s r)bufsizeenv)r shellquoterdebugpopen4) r sshcmdargs remotecmdpathsshenvcmdstdinstdoutstderrprocs r_makeconnectionrvs   %I&& T(:(:; <   CHH_s "### #+/#qf"M"M"ME664  &&rcdh}dtjtjD}|dd|z|S)zwReturn list of capabilities of this client. Returns a list of capabilities that are supported by this client. s partial-pullc@g|]}|jSrZ)wireprotosupportname).0es r z'_clientcapabilities..s7     !   rscomp=%s,)r compenginessupportedwireengines CLIENTROLEaddjoin) protoparamscompss r_clientcapabilitiesrsh ##K  !66tGG   EOOJ5!1!11222 rcfd}dd}tjtt j} ddz}dddt |z|g}|r:d d t |zd d |d || n#t$r |YnwxYwtj } tjdt!j|z} d dg} d} | dr| r |} t'd| | }|r.|d} d| znt| ddkr| dkrna| rd| | | | dz} n#t$r |YnwxYw| dr| |t/}| tj krqt1| D]a} | drJ|| ddddnb|s |t'd| |fS)Nctdtd}dd}tj||)NTrs#no suitable response from remote hguis ssherrorhinthint)r$rconfigr RepoError)msgrrtr s r badresponsez&_performhandshake..badresponsesP r6----677yy00oc----rdeveldebug.peer-requests%s-%s)(0000000000000000000000000000000000000000rshello sbetween s pairs %d s"devel-peer-request: hello+between s&devel-peer-request: pairs: %d bytes ssending hello command ssending between command rs^upgraded %s (.*)$sdummyiTrrsprotocol upgraded to %s s1 rrs capabilities::) configboolrsysbytesstruuiduuid4lenrjr>rrTr\r SSHV1rcompiler reescaperIr$rgroupappendsetreversed startswithupdatesplit)r rrrsrtr requestlogtokenpairsarg handshake protoname reupgradedlines max_noiser#mcapss` ` r_performhandshakersW . . . . . .@x)>??J  c$*,,// 0 0E44   CMM )     Q HH; < < < HH?#h--O P P P +,,, -... CHHY''(((  $I1J4G4N4NNOOJ (OEI )  !!A 2vD 1 1 1 1  ##A GGAJJ 5 ABBB RyF""qEzz )a((( LLOOO NII    KMMMMM 3 ) 8  55DN(((%  A||,--  AcrcFLL..q17799:::   2vD)))) d?s2B=DD'&D'0A*H!H!.2H!!H87H8ceZdZ ddZdhZdZdZdZdZdZ d Z d Z d Z dd Z dZddZdZdZdZdZdZdZdZddZd S) sshv1peerTc :||_||_||_|r4t|t j||}t|||}||_||_||_||_ ||_ d t j d|_ dS)aCreate a peer from an existing SSH connection. ``proc`` is a handle on the underlying SSH process. ``stdin``, ``stdout``, and ``stderr`` are handles on the stdio pipes for that process. ``caps`` is a set of capabilities supported by the remote. ``autoreadstderr`` denotes whether to automatically read from stderr and to forward its output. rrN)_urlr _subprocessr&rr2_pipeo_pipei_pipee_caps_autoreadstderrrgetstackframes _initstack) r,r urlrurrrsrtrautoreadstderrs rr/zsshv1peer.__init__vs    2D$:6$B$BFKKFr5&11E    -((4#6q#9#9::rsbatchc|jSr()rrHs rrz sshv1peer.urls yrcdSr(rZrHs rlocalzsshv1peer.localtrc|Sr(rZrHs rpeerzsshv1peer.peers rcdS)NTrZrHs rcanpushzsshv1peer.canpushrrc.|dSr(_cleanuprHs rrPzsshv1peer.closes rc|jSr()rrHs r capabilitieszsshv1peer.capabilitiess zrc:t|j|jdSr()r$r rrHs r_readerrzsshv1peer._readerrstw ,,,,,rc.||r(r)r, exceptions r_abortzsshv1peer._aborts rNcVt|j|j|j|j|dSNr)rcr rrr)r,rs rrzsshv1peer._cleanups)dgt{DK4PPPPPPrc<||jdSr)rrrHs r__del__zsshv1peer.__del__s 4? +++++rFc B|jjr|jddr|jj}d}|||zt |D]\}}t |ts!||dz|t|fz;t |D]&\}} ||dz||t| fz'|jd|z|j d|ztj |\} } | } i} | D]}|dkr|| d<n||| |<||=t | D]\}}|j d |t|fzt |tra|D]K\}} |j d |t| fz|j | L|j ||j |r.|}t!j|j|S|jS) Nrrsdevel-peer-request: %s s %s: %d bytess %s-%s: %dssending %s command s%s *s%s %d )r debugflagrrjsorteditemsr1dictrrr>r commandsrrT _getamountr cappedreaderr)r,rqrmframeddbglinekeyvaluedkdv_funcnameskeyswireargskvamounts r _sendrequestzsshv1peer._sendrequests 7  H!3!3 +" " H'-C.D Cs OOO$TZZ\\22 H H U!%..HC00CU3DDEEEE"("7"7HHBD>1S"c"gg4FFGGGGH  -3444 'C-((((1#6 u{{}}  ADyy!%"1g GG8>>++,, % %DAq K  jAs1vv;6 7 7 7!T"" %ggii**FBK%%jBB=&@AAAK%%b))))* !!!$$$$   :__&&F$T[&99 9{rc htj|}|||||jvSNrr byteskwargsr_FRAMED_COMMANDSr,rqrms r _callstreamzsshv1peer._callstream5#D))  d3$:O3O PPPrc htj|}|||||jvSrrrs r_callcompressablezsshv1peer._callcompressablerrc ~tj|}|||dS)NTr)rrrrDrs rr<zsshv1peer._calls9#D))  d4 88==???rc |j|fi|}|rd|fStfddD]}|||dd|}|rd|fS|dfS)Nrc.dSNirDfpsrz%sshv1peer._callpush..bggdmmrTrT)r<iter _writeframed _readframedr,rqrrmrCds ` r _callpushzsshv1peer._callpushs DJs # #d # #  6M++++S11 ! !A   a  #T***       6M!!3&&rc |j|fi|}|r$tjtd|zt fddD]}|||dd|jS)Nsunexpected remote reply: %sc.dSrrrsrrz-sshv1peer._calltwowaystream..rrrTr)r<rAbortrrrrrs ` r_calltwowaystreamzsshv1peer._calltwowaystreams DJs # #d # #  E+a >??!CDD D++++S11 ! !A   a  #T***{rc|j}|dkrR|jr|t d}|t j||jr| t|S#t$r9|t j t d|YdSwxYw)Nrscheck previous remote outputrsunexpected response:) rrIrrrrrOutOfBandErrorintr] ResponseError)r,r#rs rrzsshv1peer._getamount s K " " ::#  344C KK,#666 7 7 7    MMOOO Lq66M L L L KK+A.E,F,FJJ K K K K K K LsB?C C cf|}|sdS|j|S)Nr)rrrD)r,rBs rrzsshv1peer._readframed.s4   3{%%%rc|jdt|z|r|j||r|j|jr|dSdS)Ns%d )rr>rrTrr)r,r=rTs rrzsshv1peer._writeframed5s 'CII-...  $ K  d # # #  K         MMOOOOO  rTr(F)rUrVrWr/rrrrrrPrrrrrrrrr<rr rrrrZrrrrusvIM;;;;D   ---QQQQ,,,++++ZQQQQQQ@@@'''* L L L&&&rrTc 4 t||||\}}n##t$rt||||dwxYw|tjkrt ||||||||St||||dt jtd|z)aMake a peer instance from existing pipes. ``path`` and ``proc`` are stored on the eventual peer instance and may not be used for anything meaningful. ``stdin``, ``stdout``, and ``stderr`` are the pipes connected to the SSH server's stdio handles. This function is factored out to allow creating peers that don't actually spawn a new process. It is useful for starting SSH protocol servers and clients via non-standard means, which can be useful for testing. Nr)rs#unknown version of SSH protocol: %s) r Exceptionrcr rrrrr) r rorurrrsrtrrrs rmakepeerr?s+BvvFF 44 b&%d;;;; N(((       )     b&%d;;;;o 4 5 5 A   s 8c Htj|dd}|jdks|jr|j$t jtd|ztj||j !t jtd| dd}| dd}t| d }tj|} |jpd } tj||j|j|j} |r|r!t jtd d || tjd t'|t'| fzfz} |d| z|| d| } | dkr!t jtdt-||| || | \}}}}t/||||||}d|vr |ddt7t9nC#t:$r6|t jtdwxYw|S)ziCreate an SSH peer. The returned object conforms to the ``wireprotov1peer.wirepeer`` interface. F) parsequery parsefragmentssshNscouldn't parse location %sspassword in URL not supportedrs remotecmdssshenv.s8cannot create remote SSH repositories with extra optionsres %s init %srfssshpeer) blockedtagenvironrscould not create remote repos protocaps )rscapability exchange failed)rrschemehostrorrr checksafesshpasswdrr configitemsr shellenvironsshargsuserportrirrjsystemrvrrr<rrrr\r)r rocreateintents createoptsurlrn sshaddenvrp remotepathrmrqresrurrrsrtrs rinstancer/es  DU%@@@Ax616>oa =>>EFFF xoa @AABBB YYuf % %F %..IR^^I..//I  "9 - -F4J  FAFAFAF ; ;D F  /*      **L,D,DEF     3&'''ii FiCC !88/!$C"D"DEE E"1 FD)Z##D% BdE66 : :Dt((**** D JJ499V4G4I4I-J-J#K#K      D D D MMOOO/!$A"B"BCC C D KsAIAJrr(r)NN)rri18nrrrrrr r r utilsr r rrr$r&rcrvrrwirepeerrrr/rZrrr4s /// 2 2 2 2a"a"a"a"a"a"a"a"HPPP<''''2   WWWtGGGGG(GGGT# # # # LAAAAAAr