=OXc@sdZddlZddlZddlZddlZddlZddlZddlmZddl m Z ddl m Z m Z mZmZmZmZmZddlmZmZddlmZmZddlmZd Zd efd YZd efd YZdS(s Packet handling iN(tHMAC(tutil(t linefeed_bytet cr_byte_valuetasbytest MSG_NAMEStDEBUGt xfffffffft zero_byte(tutbyte_ord(t SSHExceptiontProxyCommandFailure(tMessagecCst|||jS(N(Rtdigest(tkeytmessaget digest_class((s3/usr/lib/python2.7/site-packages/paramiko/packet.pyt compute_hmac'stNeedRekeyExceptioncBseZRS((t__name__t __module__(((s3/usr/lib/python2.7/site-packages/paramiko/packet.pyR+st PacketizercBsReZdZeddZeddZeddZeddZdZe dZ dZ e dZ dZdZd Zd Zd Zd Zd ZdZdZdZdZdZdZdZe dZdZdZdZdZ dZ!dZ"dZ#dZ$dZ%RS(s9 Implementation of the base SSH packet protocol. iicCsT||_d|_t|_t|_t|_d|_t|_ d|_ d|_ d|_ d|_ d|_d|_d|_d|_d|_d|_d|_d|_t|_d|_d|_t|_t|_d|_d|_d|_d|_tj |_!d|_"t#j#|_$d|_%d|_&t|_'t|_(dS(Nii()t_Packetizer__sockettNonet_Packetizer__loggertFalset_Packetizer__closedt_Packetizer__dump_packetst_Packetizer__need_rekeyt_Packetizer__init_counttbytest_Packetizer__remaindert_Packetizer__sent_bytest_Packetizer__sent_packetst_Packetizer__received_bytest_Packetizer__received_packetst$_Packetizer__received_bytes_overflowt&_Packetizer__received_packets_overflowt_Packetizer__block_size_outt_Packetizer__block_size_int_Packetizer__mac_size_outt_Packetizer__mac_size_int_Packetizer__block_engine_outt_Packetizer__block_engine_int_Packetizer__sdctr_outt_Packetizer__mac_engine_outt_Packetizer__mac_engine_int_Packetizer__mac_key_outt_Packetizer__mac_key_int _Packetizer__compress_engine_outt_Packetizer__compress_engine_int _Packetizer__sequence_number_outt_Packetizer__sequence_number_int threadingtRLockt_Packetizer__write_lockt_Packetizer__keepalive_intervalttimet_Packetizer__keepalive_lastt_Packetizer__keepalive_callbackt_Packetizer__timert_Packetizer__handshake_completet_Packetizer__timer_expired(tselftsocket((s3/usr/lib/python2.7/site-packages/paramiko/packet.pyt__init__<sF                                cCs|jS(N(R(R@((s3/usr/lib/python2.7/site-packages/paramiko/packet.pytclosedjscCs ||_dS(s? Set the Python log object to use for logging. N(R(R@tlog((s3/usr/lib/python2.7/site-packages/paramiko/packet.pytset_lognscCs||_||_||_||_||_||_d|_d|_|jdO_|jdkr{d|_t |_ ndS(s. Switch outbound data cipher. iiiN( R+R-R'R.R)R0R!R"RRR(R@t block_enginet block_sizet mac_enginetmac_sizetmac_keytsdctr((s3/usr/lib/python2.7/site-packages/paramiko/packet.pytset_outbound_cipherts         cCs||_||_||_||_||_d|_d|_d|_d|_|j dO_ |j dkrd|_ t |_ ndS(s- Switch inbound data cipher. iiiN( R,R(R/R*R1R#R$R%R&RRR(R@RFRGRHRIRJ((s3/usr/lib/python2.7/site-packages/paramiko/packet.pytset_inbound_ciphers          cCs ||_dS(N(R2(R@t compressor((s3/usr/lib/python2.7/site-packages/paramiko/packet.pytset_outbound_compressorscCs ||_dS(N(R3(R@RN((s3/usr/lib/python2.7/site-packages/paramiko/packet.pytset_inbound_compressorscCst|_|jjdS(N(tTrueRRtclose(R@((s3/usr/lib/python2.7/site-packages/paramiko/packet.pyRRs cCs ||_dS(N(R(R@thexdump((s3/usr/lib/python2.7/site-packages/paramiko/packet.pyt set_hexdumpscCs|jS(N(R(R@((s3/usr/lib/python2.7/site-packages/paramiko/packet.pyt get_hexdumpscCs|jS(N(R*(R@((s3/usr/lib/python2.7/site-packages/paramiko/packet.pytget_mac_size_inscCs|jS(N(R)(R@((s3/usr/lib/python2.7/site-packages/paramiko/packet.pytget_mac_size_outscCs|jS(s Returns ``True`` if a new set of keys needs to be negotiated. This will be triggered during a packet read or write, so it should be checked after every read or write, or at least after every few. (R(R@((s3/usr/lib/python2.7/site-packages/paramiko/packet.pyt need_rekeyscCs%||_||_tj|_dS(s Turn on/off the callback keepalive. If ``interval`` seconds pass with no data read from or written to the socket, the callback will be executed and the timer will be reset. N(R9R<R:R;(R@tintervaltcallback((s3/usr/lib/python2.7/site-packages/paramiko/packet.pyt set_keepalives  cCs t|_dS(N(RQR?(R@((s3/usr/lib/python2.7/site-packages/paramiko/packet.pyt read_timerscCs;|js7tjt||j|_|jjndS(s Tells `Packetizer` that the handshake process started. Starts a book keeping timer that can signal a timeout in the handshake process. :param float timeout: amount of seconds to wait before timing out N(R=R6tTimertfloatR\tstart(R@ttimeout((s3/usr/lib/python2.7/site-packages/paramiko/packet.pytstart_handshakes cCs!|js tS|jrtS|jS(sR Checks if the handshake has timed out. If `start_handshake` wasn't called before the call to this function, the return value will always be `False`. If the handshake completed before a timeout was reached, the return value will be `False` :return: handshake time out status, as a `bool` (R=RR>R?(R@((s3/usr/lib/python2.7/site-packages/paramiko/packet.pythandshake_timed_outs  cCs/|jr+|jjt|_t|_ndS(sF Tells `Packetizer` that the handshake has completed. N(R=tcancelRR?RQR>(R@((s3/usr/lib/python2.7/site-packages/paramiko/packet.pytcomplete_handshakes   cCst}t|jdkrN|j| }|j||_|t|8}nx|dkrt}|jr{tnyN|jj|}t|dkrtn||7}|t|8}Wntj k rt }ntj k r}t |j tkr@t|j dkr@|j dtjkr@t }qt |j tkrt|j dkr|j dtjkrq|jrtqnX|rQ|jrtn|rt|dkr|jrtn|jqQqQW|S(s Read as close to N bytes as possible, blocking as long as necessary. :param int n: number of bytes to read :return: the data read, as a `str` :raises EOFError: if the socket was closed before all the bytes could be read i(RtlenR RRbtEOFErrorRtrecvRAR`RQterrorttypetargsttupleterrnotEAGAINtEINTRRRRt_check_keepalive(R@tnt check_rekeytoutt got_timeouttxte((s3/usr/lib/python2.7/site-packages/paramiko/packet.pytread_alls@       @ @    ! cCstj|_d}xt|dkrt}y|jj|}Wntjk rbt}ntj k r}t |j t krt|j dkr|j dt jkrt}q9t |j t krt|j dkr|j dt jkrt}q9d}n*tk r"ntk r8d}nX|rZd}|jrd}qn+|dkr{|dkr{d}n|d7}|dkrtn|t|krPn||}qWdS(Niii i(R:R;ReRRtsendRAR`RQRhRiRjRkRlRmRnR t ExceptionRRf(R@Rrt#iteration_with_zero_as_return_valuet retry_writeRpRu((s3/usr/lib/python2.7/site-packages/paramiko/packet.pyt write_alls> @ @           cCs|j}x#t|kr.||j|7}q W|jt}||d|_|| }t|dkr|dtkr|d }nt|S(s Read a line from the socket. We assume no data is pending after the line, so it's okay to attempt large reads. iii(R Rt _read_timeouttindexReRR (R@R`tbufRp((s3/usr/lib/python2.7/site-packages/paramiko/packet.pytreadlineDs  " cCs t|}t|d}|tkr5t|}n d|}t|}|jjz|jdk r||j|}n|j|}|j r|j t d||f|j t t j |dn|jdk r|jj|}n|}|jdk rEtjd|j|}|t|j||j|j 7}n|jdt@|_|j||jt|7_|jd7_|j|jks|j|jkr|j r|j t d|j|jfd|_d|_|j nWd|jj!XdS( sR Write a block of data using the current cipher, as an SSH block. is$%xsWrite packet <%s>, length %dsOUT: s>Iis(Rekeying (hit %d packets, %d bytes sent)N("RR RReR8tacquireR2Rt _build_packetRt_logRRt format_binaryR+tupdatetstructtpackR4RR0R.R)RR{R!R"t REKEY_PACKETSt REKEY_BYTESRR%R&t_trigger_rekeytrelease(R@tdatatcmdtcmd_nametorig_lentpacketRrtpayload((s3/usr/lib/python2.7/site-packages/paramiko/packet.pyt send_messageSs@       & $    cCs|j|jdt}|jdk r<|jj|}n|jrd|jtt j |dnt j d|d d}|d}|t ||jdkrtdn|j||jt |}||t | }||t |}|jdk r |jj|}n|jrH|jtt j |dn||}|jdkr||j }t jd|j||}t|j||j|j } t j| |stdqnt|d} |d || !} |jr|jtd || fn|jdk r5|j| } nt| d } |j| _|jd t@|_||jd} |j| 7_|jd 7_|jr|j| 7_|jd 7_|j|j ks|j|j!krTtd qTnc|j|j"ks|j|j#krT|jtd |j|jfd|_d|_|j$nt| d}|t%kr}t%|}n d |}|jr|jtd|t | fn|| fS(s Only one thread should ever be in this function (no other locking is done). :raises SSHException: if the packet is mangled :raises NeedRekeyException: if the transport should rekey RqsIN: s>IiisInvalid packet blockings>IIsMismatched MACis"Got payload (%d bytes, %d padding)s+Remote transport is ignoring rekey requestss,Rekeying (hit %d packets, %d bytes received)s$%xsRead packet <%s>, length %dN(&RvR(RQR,RRRRRRRRtunpackReR R*RR5RR1R/tconstant_time_bytes_eqR R3R tseqnoRR#R$RR%R&tREKEY_PACKETS_OVERFLOW_MAXtREKEY_BYTES_OVERFLOW_MAXRRRR(R@theadert packet_sizetleftoverR~Rt post_packettmact mac_payloadtmy_mactpaddingRtmsgtraw_packet_sizeRR((s3/usr/lib/python2.7/site-packages/paramiko/packet.pyt read_messagesn                 #cCsf|jdkrdStt|trOx7|D]}|jj||q/Wn|jj||dS(N(RRt issubclassRitlistRD(R@tlevelRtm((s3/usr/lib/python2.7/site-packages/paramiko/packet.pyRs  cCs]|j s|j s|jr!dStj}||j|jkrY|j||_ndS(N(R9R+RR:R;R<(R@tnow((s3/usr/lib/python2.7/site-packages/paramiko/packet.pyRos   cCstj}xtry5|jjd}t|dkrHtnPWnltjk r`nYtk r}t |j t krt|j dkr|j dt j krqnX|jrtntj}|||krtjqqW|S(Nii(R:RQRRgReRfRAR`tEnvironmentErrorRiRjRkRlRnR(R@R`R_RtRuR((s3/usr/lib/python2.7/site-packages/paramiko/packet.pyR|s(   *   cCs|j}d|t|d|}tjdt||d|}||7}|jsj|jdkr{|t|7}n|tj |7}|S(Niis>IBi( R'ReRRR-R+RRtosturandom(R@RtbsizeRR((s3/usr/lib/python2.7/site-packages/paramiko/packet.pyRs # cCs t|_dS(N(RQR(R@((s3/usr/lib/python2.7/site-packages/paramiko/packet.pyRs(&RRt__doc__tpowRRRRRBtpropertyRCRERRLRMRORPRRRTRURVRWRXR[R\RaRbRdRvR{RRRRRoR|RR(((s3/usr/lib/python2.7/site-packages/paramiko/packet.pyR/sB .              1 *  , O  ( RRlRRARR6R:thmacRtparamikoRtparamiko.commonRRRRRRRtparamiko.py3compatR R tparamiko.ssh_exceptionR R tparamiko.messageR RRxRtobjectR(((s3/usr/lib/python2.7/site-packages/paramiko/packet.pyts      4