U vf3@sddlmZmZddlZddlZddlZddlmZddlm Z ddl m Z ddl m Z mZddlmZmZmZmZddlmZGd d d eZGd d d eZGd ddZdS)) encodebytes decodebytesN)MutableMapping)sha1)HMAC)PKeyUnknownKeyType) get_loggerconstant_time_bytes_eqbu) 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||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)selffilenamerB/opt/alt/python38/lib/python3.8/site-packages/paramiko/hostkeys.py__init__/szHostKeys.__init__cCsH|jD](}||jkr|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)rhostnameZkeytypererrradd;s z HostKeys.addc Cst|d}t|dD]\}}|}t|dks|ddkr@qzt||}Wntk rhYqYnX|dk r|j}|D]}|||j r||j |q|t|jr|j |qW5QRXdS)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_liner rcheckrremoverr)rrflinenolineentryZ _hostnameshrrrrJs    z HostKeys.loadc Cs<t|d(}|jD]}|}|r||qW5QRXdS)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(rr*rrrsavejs  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] }|VqdSr1keysrkrrr__iter__s z)HostKeys.lookup..SubDict.__iter__cSs t|Sr1r$r5rrrr__len__sz(HostKeys.lookup..SubDict.__len__cSs:t|jD]"}|j|kr |j|q6q t|dSr1)listrrrr'KeyErrorrrrrrr __delitem__s  z,HostKeys.lookup..SubDict.__delitem__cSs0|jD]}|j|kr|jSqt|dSr1)rrrr=r>rrr __getitem__s  z,HostKeys.lookup..SubDict.__getitem__csZ|jD](}|jdkrq|j|kr||_qVqtg|}|j||jj|dSr1)rrrrrr2)rrvalrrrr __setitem__s    z,HostKeys.lookup..SubDict.__setitem__cSsdd|jDS)NcSs g|]}|jdk r|jqSr1)rr).0rrrr s z9HostKeys.lookup..SubDict.keys..rr:rrrr5sz%HostKeys.lookup..SubDict.keysN) __name__ __module__ __qualname__rr8r;r?r@rCr5rrBrrSubDicts rJrN)rr_hostname_matchesrr$)rrrJr3rrrBrlookup}s 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)rrr+r,rrrrKs zHostKeys._hostname_matchescCsB||}|dkrdS||d}|dkr2dS||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)rLgetrZasbytes)rrrr7Zhost_keyrrrr&s zHostKeys.checkcCs g|_dS)z; Remove all host keys from the dictionary. NrFr:rrrclearszHostKeys.clearccs|D] }|VqdSr1r4r6rrrr8s zHostKeys.__iter__cCs t|Sr1r9r:rrrr;szHostKeys.__len__cCs||}|dkrt||Sr1)rLr=)rrretrrrr@s zHostKeys.__getitem__cCsLd}t|jD]\}}|||r|}q,q|dkr)rcrrr:rrr__repr__szHostKeyEntry.__repr__)NN)N) rGrHrIrgr classmethodr%r.rzrrrrr:s   ,r)base64rrrqr]rocollections.abcrhashlibrrfrZ paramiko.pkeyrrZ paramiko.utilr r r r Zparamiko.ssh_exceptionr r Exceptionrirrrrrs