ungc@s~ddlmZddlZddlZddlZddlZddlZddlZddlZde fdYZ dS(i(t OrderedDictNtrndccBseZdZidd6dd6dd6dd6d d 6d d 6Zd ZdZedZdZdZ dZ dZ dZ dZ RS(sRNDC protocol client libraryitmd5itsha1itsha224itsha256itsha384itsha512cCs||_|j}|jdr1|d}n||_tt||_tj||_ t j dd|_ d|_|jdS(sCreates a persistent connection to RNDC and logs in host - (ip, port) tuple algo - HMAC algorithm: one of md5, sha1, sha224, sha256, sha384, sha512 (with optional prefix 'hmac-') secret - HMAC secret, base64 encodedshmac-iiiiNi(thosttlowert startswithtalgotgetattrthashlibthlalgotbase64t b64decodetsecrettrandomtrandinttsertNonetnoncet_rndc__connect_login(tselfRR R((s,/usr/lib/python2.7/site-packages/isc/rndc.pyt__init__$s     cCst|jd|dS(sCall a RNDC command, all parsing is done on the server side cmd - a complete string with a command (eg 'reload zone example.com') ttypet_data(tdictt_rndc__command(Rtcmd((s,/usr/lib/python2.7/site-packages/isc/rndc.pytcall5scCsyt}xi|jD][\}}|r:|dkr:qn|tjdt||jd7}t|tkr|tjddt||jd7}qt|tkr|tjddt||7}qt|tkr|tjddt||7}qt|t kr[|j |}|tjddt||7}qt dt|qW|S(Nt_authtBtasciis>BIiis#Cannot serialize element of type %s( t bytearraytitemststructtpacktlentencodeRtstrtbytesRt_rndc__serialize_dicttNotImplementedError(Rtdatat ignore_authtrvtktvtsd((s,/usr/lib/python2.7/site-packages/isc/rndc.pyt__serialize_dict;s" )/&&&c Os|jd7_ttj}t||}t}t|dIIi(RtintttimeRR)RRR+tTruethmactnewRRtdigestRt b64encodeR R%R&R#t _rndc__algosR'( RtargstkwargstnowR-tdtmsgthashtbhash((s,/usr/lib/python2.7/site-packages/isc/rndc.pyt__prep_messageOs,    ! !#cCs|jdk r*|dd|jkr*tS|jdkrJ|dd}n|ddd}t|tkr|jd}n|d d t|d 7}tj |}|j |d t }t j |j||jj}||kS( NR4R8RR R:R<iR"t=iR.(RRtFalseR RR*tdecodeR'RRR+R?R@RARRRB(RRIRKt remote_hashtmy_msgtmy_hash((s,/usr/lib/python2.7/site-packages/isc/rndc.pyt __verify_msgjs&!c OsD|j||}|jj|}|t|krEtdn|jjd}t|dkrxtdntjd|\}}|dkrtd|n|d8}|jj|tj }t||krtdnt |t krt |}n|j |}|j|s@td n|S( NsCannot send the messageisCan't read response headers>IIisWrong message version %disCan't read response datasAuthentication failure(t_rndc__prep_messagetsockettsendR'tIOErrortrecvR%tunpackR,t MSG_WAITALLRR)R#t_rndc__parse_messaget_rndc__verify_msg( RRERFRItsenttheadertlengthtversionR-((s,/usr/lib/python2.7/site-packages/isc/rndc.pyt __commandys(  cCsEtj|j|_d|_|jdd}|dd|_dS(NRtnullR4R8(RUtcreate_connectionRRRR(RRI((s,/usr/lib/python2.7/site-packages/isc/rndc.pyt__connect_logins c Cs,d}||}|d7}||||!jd}||7}||}|d7}tjd|||d!d}|d7}||||!}||7}||}|dkr|||fS|dkrt} x8t|dkr |j|\} } }| | | IiisUnknown element type %d(ROR%RYRR't_rndc__parse_elementR,( RtinputtpostlabellentlabelRtdatalenR-trestRHtilabeltvalue((s,/usr/lib/python2.7/site-packages/isc/rndc.pyt__parse_elements*     !        cCsNt}d}x8t|dkrI|j|\}}}|||R%R R@RRRUtobjectR(((s,/usr/lib/python2.7/site-packages/isc/rndc.pyts