�
=OXc@s+ddlZddlZddlmZddlmZddlmZmZm Z m
Z
yddlmZWn!e
k
r�ddlmZnXddlmZddlmZdd lmZmZdd
lmZddlmZdefd
��YZdefd��YZddd��YZdS(i����N(tsha1(tHMAC(tbtutencodebytestdecodebytes(tMutableMapping(t DictMixin(tDSSKey(tRSAKey(t
get_loggertconstant_time_bytes_eq(tECDSAKey(tSSHExceptiontHostKeyscBs�eZdZdd�Zd�Zd�Zd�Zd�Zd�Z d�Z
d�Zd �Zd
�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_nametappendtHostKeyEntry(RthostnametkeytypeRte((s5/usr/lib/python2.7/site-packages/paramiko/hostkeys.pytaddAs
$ cCst|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|jj
|�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.pyR�s 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__�scs�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
startswithRt 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<�scCs�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�scCs7g}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
�}dtt|��tt|��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(RtosturandomRtdigest_sizeRAtsplitRRRtdigestRRtreplace(Rtsaltthmacthostkey((s5/usr/lib/python2.7/site-packages/paramiko/hostkeys.pyRBs
(N(R?R@t__doc__RRRRR/RCR%RHR7R8R;R<R>R5RNtstaticmethodRB(((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�dS|d }|\}}}|jd�}y�t|�}|dkr�tdt|��}nl|dkr�tdt|��}nH|t j
�kr�t dt|�d t�}n|jd
|f�dSWn%tj
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 RRRt 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<HostKeyEntry %r: %r>(RR(R((s5/usr/lib/python2.7/site-packages/paramiko/hostkeys.pyt__repr__dsN( R?R@RZRRtclassmethodR$R-Rn(((s5/usr/lib/python2.7/site-packages/paramiko/hostkeys.pyR#s+ ((RfRQthashlibRRXRtparamiko.py3compatRRRRtcollectionsRtImportErrortUserDictRtparamiko.dsskeyRtparamiko.rsakeyR t
paramiko.utilR
Rtparamiko.ecdsakeyRtparamiko.ssh_exceptionR
Rt ExceptionR\R(((s5/usr/lib/python2.7/site-packages/paramiko/hostkeys.pyt<module>s "
� |