3 L]3@sddlZddlZddlZejdddkr8ddlmZn ddlmZddlmZddl m Z ddl m Z m Z mZmZddlmZdd lmZdd lmZmZdd lmZdd lmZdd lmZGdddeZGdddeZGdddZ dS)N)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|_|dk r|j|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)selffilenamer7/tmp/pip-install-wfra5znf/paramiko/paramiko/hostkeys.py__init__5szHostKeys.__init__cCsJx0|jD]&}||jkr|jj|kr||_dSqW|jjt|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.addcCst|d}xt|dD]\}}|j}t|dks|ddkrBqytj||}Wntk rhwYnX|dk r|j}x&|D]}|j||j r~|jj |q~Wt|jr|j j |qWWdQRXdS)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.loadc Cs@t|d,}x$|jD]}|j}|r|j|qWWdQRXdS)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.savecsZGfdddt}g}x$|jD]}|j|r |j|q Wt|dkrNdS|||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||_||_||_dS)N)Z _hostnamer _hostkeys)rrentriesZhostkeysrrrrsz)HostKeys.lookup..SubDict.__init__cssx|jD] }|Vq WdS)N)keys)rkrrr__iter__sz)HostKeys.lookup..SubDict.__iter__cSs t|jS)N)r)r7)rrrr__len__sz(HostKeys.lookup..SubDict.__len__cSs:x4t|jD]}|jj|kr |jj|q Wt|dS)N)listrrrr,KeyError)rrr!rrr __delitem__sz,HostKeys.lookup..SubDict.__delitem__cSs0x"|jD]}|jj|kr|jSqWt|dS)N)rrrr<)rrr!rrr __getitem__s  z,HostKeys.lookup..SubDict.__getitem__cs\xV|jD]&}|jdkrq|jj|kr||_PqWtg|}|jj||jjj|dS)N)rrrrrr5)rrvalr!)rrr __setitem__s    z,HostKeys.lookup..SubDict.__setitem__cSsdd|jDS)NcSs g|]}|jdk r|jjqS)N)rr).0r!rrr sz9HostKeys.lookup..SubDict.keys..)r)rrrrr7sz%HostKeys.lookup..SubDict.keysN) __name__ __module__ __qualname__rr9r:r=r>r@r7r)rrrSubDicts rFrN)rr_hostname_matchesrr))rrrFr6r!r)rrlookups /   zHostKeys.lookupcCsHxB|jD]8}||ks<|jdr|jd rt|j|||rdSqWdS)zl Tests whether ``hostname`` string matches given SubDict ``entry``. :returns bool: z|1|TF)r startswithr hash_host)rrentryr0rrrrGs   zHostKeys._hostname_matchescCsB|j|}|dkrdS|j|jd}|dkr2dS|j|jkS)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)rHgetrZasbytes)rrrr8Zhost_keyrrrr+s zHostKeys.checkcCs g|_dS)z; Remove all host keys from the dictionary. N)r)rrrrclearszHostKeys.clearccsx|jD] }|Vq WdS)N)r7)rr8rrrr9szHostKeys.__iter__cCs t|jS)N)r)r7)rrrrr:szHostKeys.__len__cCs|j|}|dkrt||S)N)rHr<)rrretrrrr>s zHostKeys.__getitem__cCsNd}x(t|jD]\}}|j||r|}PqW|dkr>t||jj|dS)N)r'rrGr<pop)rrindexirKrrrr=s zHostKeys.__delitem__cCst|dkr$|jjt|gddSxf|jD]Z}d}x4|jD]*}||jkr>|jj|kr>|||_d}q>W|s.|jjt|g||q.WdS)NrFT)r)rrrr7rrr)rrrKZkey_typefoundr!rrrr@s   zHostKeys.__setitem__cCs:g}x0|jD]&}x |jD]}||kr|j|qWq W|S)N)rrr)rrNr!r0rrrr7s   z HostKeys.keyscCs*g}x |jD]}|j|j|qW|S)N)r7rrH)rrNr8rrrvaluesszHostKeys.valuescCs|dkrtjtj}n$|jdr0|jdd}tt|}t|tjksPt t |t|tj }dj t t|t t|}|jddS)aM 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` Nz|1||rz|1|{}|{} )osurandomr digest_sizerIsplitr rr)AssertionErrorrdigestformatrr replace)rZsalthmacZhostkeyrrrrJ"s   zHostKeys.hash_host)N)N)rCrDrE__doc__rr"rr4rHrGr+rMr9r:r>r=r@r7rS staticmethodrJrrrrr)s$   B  rc@seZdZddZdS)InvalidHostKeycCs||_||_||f|_dS)N)r/excargs)rr/rcrrrr:szInvalidHostKey.__init__N)rCrDrErrrrrrb9srbc@s8eZdZdZd ddZed ddZddZd d ZdS) rzJ Representation of a line in an OpenSSH-style "known hosts" file. NcCs"|dk o|dk |_||_||_dS)N)validrr)rrrrrrrEszHostKeyEntry.__init__c Cstd}|jd}t|dkr8d}|j|j||dS|dd}|\}}}|jd}yt|}|dkrztt|d}nb|d krtt|d}nJ|t j krt t|d d }n,|d krt t|d}n|jd j|dSWn0t j k r} zt|| WYdd} ~ XnX|||S)a 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 zparamiko.hostkeys rz8Not enough fields found in known_hosts in line {} ({!r})N,zssh-rsa)datazssh-dssF)rhZvalidate_pointz ssh-ed25519zUnable to handle key of type {})r rZr)infor]rr r r rZ supported_key_format_identifiersrbinasciiErrorrb) clsr/r.logfieldsmsgnamesr rr!rrrr*Js0       zHostKeyEntry.from_linecCs,|jr(djdj|j|jj|jjSdS)z 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. z {} {} {} rgN)rer]joinrrrZ get_base64)rrrrr2xs   zHostKeyEntry.to_linecCsdj|j|jS)Nz)r]rr)rrrr__repr__szHostKeyEntry.__repr__)NN)N) rCrDrEr`r classmethodr*r2rrrrrrr@s   -r)rr)!rjrWsys version_infocollections.abcr collectionshashlibrr_rZparamiko.py3compatrrr r Zparamiko.dsskeyr Zparamiko.rsakeyr Z paramiko.utilr rZparamiko.ecdsakeyrZparamiko.ed25519keyrZparamiko.ssh_exceptionrr Exceptionrbrrrrrs&