a b3@sddlZddlZddlZejdddkr8ddlmZn ddlmZddlmZddl m Z ddl m Z m Z mZmZddlmZdd lmZdd lmZmZdd lmZdd lmZdd lmZGdddeZGdddeZGdddZ dS)N)r)MutableMapping)sha1)HMAC)bu encodebytes decodebytes)DSSKey)RSAKey) get_loggerconstant_time_bytes_eq)ECDSAKey) Ed25519Key) SSHExceptionc@seZdZdZd#ddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ ddZ ddZddZddZddZdd Zed$d!d"ZdS)%HostKeysaZ 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 NcCsg|_|dur||dS)z 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)_entriesload)selffilenamer8/tmp/pip-target-98j97qn4/lib/python/paramiko/hostkeys.py__init__5szHostKeys.__init__cCsH|jD](}||jvr|j|kr||_dSq|jt|g|dS)a. 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)r hostnameskeyget_nameappend HostKeyEntry)rhostnamekeytypererrraddAs z HostKeys.addc Cst|d}t|dD]\}}|}t|dks|ddkr@qzt||}WntyfYqYn0|dur|j}|D]}|||j rz|j |qzt|jr|j |qWdn1s0YdS)a] 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 rr#N) open enumeratestriplenr from_linerrcheckrremoverr)rrflinenoliner!Z _hostnameshrrrrPs     z HostKeys.loadcCsPt|d2}|jD]}|}|r||qWdn1sB0YdS)a 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 wN)r&rto_linewrite)rrr-r!r/rrrsaveps  z HostKeys.savecsVGfdddt}g}|jD]}||r||qt|dkrJdS|||S)a 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``) csHeZdZddZddZddZddZd d Zfd d Zd dZ dS)z HostKeys.lookup..SubDictcSs||_||_||_dSN)Z _hostnamer _hostkeys)rrentriesZhostkeysrrrrsz)HostKeys.lookup..SubDict.__init__css|D] }|VqdSr5keysrkrrr__iter__s z)HostKeys.lookup..SubDict.__iter__cSs t|Sr5r)r9rrrr__len__sz(HostKeys.lookup..SubDict.__len__cSs:t|jD]"}|j|kr |j|q6q t|dSr5)listrrrr,KeyErrorrrr!rrr __delitem__s  z,HostKeys.lookup..SubDict.__delitem__cSs0|jD]}|j|kr|jSqt|dSr5)rrrrArBrrr __getitem__s  z,HostKeys.lookup..SubDict.__getitem__csZ|jD](}|jdurq|j|kr||_qVqtg|}|j||jj|dSr5)rrrrrr6)rrvalr!rrr __setitem__s    z,HostKeys.lookup..SubDict.__setitem__cSsdd|jDS)NcSs g|]}|jdur|jqSr5)rr).0r!rrr s z9HostKeys.lookup..SubDict.keys..rr>rrrr9sz%HostKeys.lookup..SubDict.keysN) __name__ __module__ __qualname__rr<r?rCrDrGr9rrFrrSubDicts rNrN)rr_hostname_matchesrr))rrrNr7r!rrFrlookups 0    zHostKeys.lookupcCsD|jD]8}||ks8|dr|dst||||rdSqdS)zl Tests whether ``hostname`` string matches given SubDict ``entry``. :returns bool: |1|TF)r startswithr hash_host)rrentryr0rrrrOs zHostKeys._hostname_matchescCsB||}|durdS||d}|dur2dS||kS)a= 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`` NF)rPgetrZasbytes)rrrr;Zhost_keyrrrr+s zHostKeys.checkcCs g|_dS)z; Remove all host keys from the dictionary. NrJr>rrrclearszHostKeys.clearccs|D] }|VqdSr5r8r:rrrr<s zHostKeys.__iter__cCs t|Sr5r=r>rrrr?szHostKeys.__len__cCs||}|durt||Sr5)rPrA)rrretrrrrDs zHostKeys.__getitem__cCsLd}t|jD]\}}|||r|}q,q|durrrrr2ys zHostKeyEntry.to_linecCsd|j|jS)Nz)rerrr>rrr__repr__szHostKeyEntry.__repr__)NN)N) rKrLrMrir classmethodr*r2r{rrrrrAs   -r)!rsr`sys version_infocollections.abcr collectionshashlibrrhrZparamiko.py3compatrrr r Zparamiko.dsskeyr Zparamiko.rsakeyr Z paramiko.utilr rZparamiko.ecdsakeyrZparamiko.ed25519keyrZparamiko.ssh_exceptionrr Exceptionrkrrrrrs&