ó =OXc@s+ddlZddlZddlmZddlmZddlmZmZm Z m Z yddl m Z Wn!e k r‘ddlmZ nXddlmZddlmZdd lmZmZdd lmZdd lmZd e fd „ƒYZdefd„ƒYZddd„ƒYZdS(iÿÿÿÿN(tsha1(tHMAC(tbtut encodebytest decodebytes(tMutableMapping(t DictMixin(tDSSKey(tRSAKey(t get_loggertconstant_time_bytes_eq(tECDSAKey(t SSHExceptiontHostKeyscBs¡eZdZdd„Zd„Zd„Zd„Zd„Zd„Z d„Z d„Z d „Z d „Z d „Zd „Zd „Zd„Zedd„ƒZRS(sZ Representation of an OpenSSH-style "known hosts" file. Host keys can be read from one or more files, and then individual hosts can be looked up to verify server keys during SSH negotiation. A `.HostKeys` object can be treated like a dict; any dict lookup is equivalent to calling `lookup`. .. versionadded:: 1.5.3 cCs)g|_|dk r%|j|ƒndS(s¿ Create a new HostKeys object, optionally loading keys from an OpenSSH style host-key file. :param str filename: filename to load host keys from, or ``None`` N(t_entriestNonetload(tselftfilename((s5/usr/lib/python2.7/site-packages/paramiko/hostkeys.pyt__init__5s  cCsexB|jD]7}||jkr |jjƒ|kr ||_dSq W|jjt|g|ƒƒdS(s. Add a host key entry to the table. Any existing entry for a ``(hostname, keytype)`` pair will be replaced. :param str hostname: the hostname (or IP) to add :param str keytype: key type (``"ssh-rsa"`` or ``"ssh-dss"``) :param .PKey key: the key to add N(Rt hostnamestkeytget_nametappendt HostKeyEntry(RthostnametkeytypeRte((s5/usr/lib/python2.7/site-packages/paramiko/hostkeys.pytaddAs $ c Cst|dƒû}xñt|dƒD]à\}}|jƒ}t|ƒdks"|ddkrbq"nytj||ƒ}Wntk rŽq"nX|dk r"|j}x6|D].}|j ||j ƒr«|jj |ƒq«q«Wt|jƒr|j j |ƒqq"q"WWdQXdS(sV Read a file of known SSH host keys, in the format used by OpenSSH. This type of file unfortunately doesn't exist on Windows, but on posix, it will usually be stored in ``os.path.expanduser("~/.ssh/known_hosts")``. If this method is called multiple times, the host keys are merged, not cleared. So multiple calls to `load` will just call `add`, replacing any existing entries and adding new ones. :param str filename: name of the file to read host keys from :raises IOError: if there was an error reading the file triit#N(topent enumeratetstriptlenRt from_lineR RRtcheckRtremoveRR(RRtftlinenotlineRt _hostnamesth((s5/usr/lib/python2.7/site-packages/paramiko/hostkeys.pyRPs  "    cCsRt|dƒ=}x3|jD](}|jƒ}|r|j|ƒqqWWdQXdS(sà Save host keys into a file, in the format used by OpenSSH. The order of keys in the file will be preserved when possible (if these keys were loaded from a file originally). The single exception is that combined lines will be split into individual key lines, which is arguably a bug. :param str filename: name of the file to write :raises IOError: if there was an error writing the file .. versionadded:: 1.6.1 twN(R Rtto_linetwrite(RRR'RR)((s5/usr/lib/python2.7/site-packages/paramiko/hostkeys.pytsaveps  csÆdtf‡fd†ƒY}g}x{|jD]p}xg|jD]\}|jdƒr|ˆjdƒ r|t|jˆ|ƒ|ƒsˆ|ˆkr<|j|ƒq<q<Wq,Wt|ƒdkr¶dS|ˆ||ƒS(s† Find a hostkey entry for a given hostname or IP. If no entry is found, ``None`` is returned. Otherwise a dictionary of keytype to key is returned. The keytype will be either ``"ssh-rsa"`` or ``"ssh-dss"``. :param str hostname: the hostname (or IP) to lookup :return: dict of `str` -> `.PKey` keys associated with this host (or ``None``) tSubDictcsMeZd„Zd„Zd„Zd„Zd„Z‡fd†Zd„ZRS(cSs||_||_||_dS(N(t _hostnameRt _hostkeys(RRtentriesthostkeys((s5/usr/lib/python2.7/site-packages/paramiko/hostkeys.pyRs  css x|jƒD] }|Vq WdS(N(tkeys(Rtk((s5/usr/lib/python2.7/site-packages/paramiko/hostkeys.pyt__iter__’scSst|jƒƒS(N(R#R5(R((s5/usr/lib/python2.7/site-packages/paramiko/hostkeys.pyt__len__–scSsRxKt|jƒD].}|jjƒ|kr|jj|ƒqqWt|ƒ‚dS(N(tlistRRRR&tKeyError(RRR((s5/usr/lib/python2.7/site-packages/paramiko/hostkeys.pyt __delitem__™scSs@x-|jD]"}|jjƒ|kr |jSq Wt|ƒ‚dS(N(RRRR:(RRR((s5/usr/lib/python2.7/site-packages/paramiko/hostkeys.pyt __getitem__ s cs„x}|jD]=}|jdkr%q n|jjƒ|kr ||_Pq q Wtˆg|ƒ}|jj|ƒ|jjj|ƒdS(N(RRRRRRR2(RRtvalR(R(s5/usr/lib/python2.7/site-packages/paramiko/hostkeys.pyt __setitem__¦s cSs2g|jD]$}|jdk r |jjƒ^q S(N(RRRR(RR((s5/usr/lib/python2.7/site-packages/paramiko/hostkeys.pyR5´s( t__name__t __module__RR7R8R;R<R>R5((R(s5/usr/lib/python2.7/site-packages/paramiko/hostkeys.pyR0Œs     s|1|iN( RRRt startswithR t hash_hostRR#R(RRR0R3RR+((Rs5/usr/lib/python2.7/site-packages/paramiko/hostkeys.pytlookupƒs +FcCs]|j|ƒ}|dkrtS|j|jƒdƒ}|dkrGtS|jƒ|jƒkS(s= Return True if the given key is associated with the given hostname in this dictionary. :param str hostname: hostname (or IP) of the SSH server :param .PKey key: the key to check :return: ``True`` if the key is associated with the hostname; else ``False`` N(RCRtFalsetgetRtasbytes(RRRR6thost_key((s5/usr/lib/python2.7/site-packages/paramiko/hostkeys.pyR%Às   cCs g|_dS(s; Remove all host keys from the dictionary. N(R(R((s5/usr/lib/python2.7/site-packages/paramiko/hostkeys.pytclearÒsccs x|jƒD] }|Vq WdS(N(R5(RR6((s5/usr/lib/python2.7/site-packages/paramiko/hostkeys.pyR7ØscCst|jƒƒS(N(R#R5(R((s5/usr/lib/python2.7/site-packages/paramiko/hostkeys.pyR8ÜscCs||}dS(N((RRR6((s5/usr/lib/python2.7/site-packages/paramiko/hostkeys.pyR;ßscCs.|j|ƒ}|dkr*t|ƒ‚n|S(N(RCRR:(RRtret((s5/usr/lib/python2.7/site-packages/paramiko/hostkeys.pyR<âs cCsÊt|ƒdkr2|jjt|gdƒƒdSx‘|jƒD]ƒ}t}xK|jD]@}||jkrU|jj ƒ|krU|||_t }qUqUW|s?|jjt|g||ƒƒq?q?WdS(Ni( R#RRRRR5RDRRRtTrue(RRtentrytkey_typetfoundR((s5/usr/lib/python2.7/site-packages/paramiko/hostkeys.pyR>ès$  cCsNg}xA|jD]6}x-|jD]"}||kr |j|ƒq q WqW|S(N(RRR(RRIRR+((s5/usr/lib/python2.7/site-packages/paramiko/hostkeys.pyR5÷s  cCs7g}x*|jƒD]}|j|j|ƒƒqW|S(N(R5RRC(RRIR6((s5/usr/lib/python2.7/site-packages/paramiko/hostkeys.pytvaluesscCs±|dkr$tjtƒjƒ}n7|jdƒrI|jdƒd}ntt|ƒƒ}t |t|ƒtƒj ƒ}dt t |ƒƒt t |ƒƒf}|j ddƒS(sA Return a "hashed" form of the hostname, as used by OpenSSH when storing hashed hostnames in the known_hosts file. :param str hostname: the hostname to hash :param str salt: optional salt to use when hashing (must be 20 bytes long) :return: the hashed hostname as a `str` s|1|t|is|1|%s|%ss tN(RtosturandomRt digest_sizeRAtsplitRRRtdigestRRtreplace(Rtsaltthmacthostkey((s5/usr/lib/python2.7/site-packages/paramiko/hostkeys.pyRBs (N(R?R@t__doc__RRRRR/RCR%RHR7R8R;R<R>R5RNt staticmethodRB(((s5/usr/lib/python2.7/site-packages/paramiko/hostkeys.pyR)s"    =        tInvalidHostKeycBseZd„ZRS(cCs%||_||_||f|_dS(N(R)texctargs(RR)R]((s5/usr/lib/python2.7/site-packages/paramiko/hostkeys.pyRs  (R?R@R(((s5/usr/lib/python2.7/site-packages/paramiko/hostkeys.pyR\sRcBsAeZdZddd„Zedd„ƒZd„Zd„ZRS(sJ Representation of a line in an OpenSSH-style "known hosts" file. cCs1|dk o|dk |_||_||_dS(N(RtvalidRR(RRR((s5/usr/lib/python2.7/site-packages/paramiko/hostkeys.pyR(s c CsEtdƒ}|jdƒ}t|ƒdkrH|jd||fƒd S|d }|\}}}|jdƒ}y t|ƒ}|dkr£tdt|ƒƒ}nl|dkrÇtdt|ƒƒ}nH|t j ƒkr÷t dt|ƒd t ƒ}n|jd |fƒd SWn%t j k r7}t||ƒ‚nX|||ƒS( så Parses the given line of text to find the names for the host, the type of key, and the key data. The line is expected to be in the format used by the OpenSSH known_hosts file. Lines are expected to not have leading or trailing whitespace. We don't bother to check for comments or empty lines. All of that should be taken care of before sending the line to us. :param str line: a line from an OpenSSH known_hosts file sparamiko.hostkeyst is6Not enough fields found in known_hosts in line %s (%r)t,sssh-rsatdatasssh-dsstvalidate_pointsUnable to handle key of type %sN(R RTR#tinfoRRR RRR t supported_key_format_identifiersRDtbinasciitErrorR\( tclsR)R(tlogtfieldstnamesRRR((s5/usr/lib/python2.7/site-packages/paramiko/hostkeys.pyR$-s,      cCs<|jr8ddj|jƒ|jjƒ|jjƒfSdS(s¦ Returns a string in OpenSSH known_hosts file format, or None if the object is not in a valid state. A trailing newline is included. s %s %s %s RaN(R_tjoinRRRt get_base64R(R((s5/usr/lib/python2.7/site-packages/paramiko/hostkeys.pyR-Ys cCsd|j|jfS(Ns(RR(R((s5/usr/lib/python2.7/site-packages/paramiko/hostkeys.pyt__repr__dsN( R?R@RZRRt classmethodR$R-Rn(((s5/usr/lib/python2.7/site-packages/paramiko/hostkeys.pyR#s + ((RfRQthashlibRRXRtparamiko.py3compatRRRRt collectionsRt ImportErrortUserDictRtparamiko.dsskeyRtparamiko.rsakeyR t paramiko.utilR R tparamiko.ecdsakeyR tparamiko.ssh_exceptionR Rt ExceptionR\R(((s5/usr/lib/python2.7/site-packages/paramiko/hostkeys.pyts  " ó