3 9Pe+@sXddlmZddlZddlZddlZddlZddlZddlZddlZGddde Z dS)) OrderedDictNc@sleZdZdZdddddddZd d Zd d ZdddZddZddZ ddZ ddZ ddZ ddZ dS)rndczRNDC protocol client library)md5Zsha1Zsha224Zsha256Zsha384Zsha512cCsb||_|j}|jdr$|dd}||_tt||_tj||_ t j dd|_ d|_ |jdS)zCreates 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 encodedzhmac-Nri)hostlower startswithalgogetattrhashlibhlalgobase64 b64decodesecretrandomZrandintsernonce_rndc__connect_login)selfrrrr/usr/lib/python3.6/rndc.py__init__$s    z rndc.__init__cCst|j|ddS)zCall a RNDC command, all parsing is done on the server side cmd - a complete string with a command (eg 'reload zone example.com') )type_data)dict_rndc__command)rcmdrrrcall5sz rndc.callFcCst}x|jD]\}}|r(|dkr(q|tjdt||jd7}t|tkrt|tjddt||jd7}qt|tkr|tjddt||7}qt|tkr|tjddt||7}qt|t kr|j |}|tjddt||7}qt dt|qW|S)N_authBasciiz>BIr z#Cannot serialize element of type %s) bytearrayitemsstructpacklenencoder strbytesr_rndc__serialize_dictNotImplementedError)rdata ignore_authrvkvZsdrrrZ__serialize_dict;s"  "    zrndc.__serialize_dictc Os,|jd7_ttj}t||}t}t|d<t|d<t|j|dd<t||dd<t|d|dd<|jdk r|j|dd<||d <|j|d d }tj|j ||j j }t j |}|jd krtjd ||dd<n"ttjd|j|j||dd<|j|}tjdt|dd|}|S)Nr r&_ctrlZ_serZ_tim<Z_exp_noncer!T)r5r Z22shmd5ZB88shshaz>II)rinttimerr0rr2hmacnewrrdigestrZ b64encoderr,r-r* _rndc__algosr.) rargskwargsZnowr4dmsghashbhashrrrZ__prep_messageOs,        zrndc.__prep_messagecCs|jdk r |dd|jkr dS|jdkr8|dd}n|dddd}t|tkrb|jd }|d d t|d 7}tj|}|j|d d }t j |j ||j j }||kS)Nr9r;Fr r&r<r=r r(=r>T)r5)rrr r1decoder.rrr2rArBrrrC)rrHrJZ remote_hashZmy_msgZmy_hashrrrZ __verify_msgjs    zrndc.__verify_msgc Os|j||}|jj|}|t|kr,td|jjd}t|dkrLtdtjd|\}}|dkrptd||d8}|jj|tj }t||krtdt |t krt |}|j |}|j|std |S) NzCannot send the messagezCan't read response headerz>IIr zWrong message version %dr>zCan't read response datazAuthentication failure)_rndc__prep_messagesocketsendr.IOErrorZrecvr,unpackr3Z MSG_WAITALLr r0r*_rndc__parse_message_rndc__verify_msg) rrErFrHZsentheaderZlengthversionr4rrrZ __commandys(          zrndc.__commandcCs2tj|j|_d|_|jdd}|dd|_dS)NZnull)r r9r;)rOZcreate_connectionrrr#)rrHrrrZ__connect_logins zrndc.__connect_loginc Csd}||}|d7}||||jd}||7}||}|d7}tjd|||dd}|d7}||||}||7}||d}|dkr|||fS|dkrt} x(t|dkr|j|\} } }| | | <qW|| |fStd|dS)Nrr r(z>Ir>r)zUnknown element type %d)rLr,rRrr._rndc__parse_elementr3) rinputposZlabellenlabelr Zdatalenr4restrGZilabelvaluerrrZ__parse_elements*    zrndc.__parse_elementcCs8t}d}x(t|dkr2|j|\}}}|||<q W|S)Nr)rr.rW)rrXr6ZhdatarZr\rrrZ__parse_messages  zrndc.__parse_messageN)F)__name__ __module__ __qualname____doc__rDrr%r2rNrTr#rrWrSrrrrrs  r) collectionsrr@r,rrArrrOobjectrrrrrs