Ñò ÔãcJc @sŽdZddkZddklZlZddkZddkTddklZddk l Z dd d„ƒYZ d ei fd „ƒYZ dS( s L{HostKeys} iÿÿÿÿN(tSHAtHMAC(t*(tDSSKey(tRSAKeyt HostKeyEntrycBsDeZdZddd„Zd„ZeeƒZd„Zd„ZRS(sJ Representation of a line in an OpenSSH-style "known hosts" file. cCs2|dj o |dj |_||_||_dS(N(tNonetvalidt hostnamestkey(tselfRR ((s5/usr/lib/python2.6/site-packages/paramiko/hostkeys.pyt__init__%s cCs²|idƒ}t|ƒdjodS|d }|\}}}|idƒ}|djotdti|ƒƒ}n.|djotdti|ƒƒ}ndS|||ƒ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 line: a line from an OpenSSH known_hosts file @type line: str t it,sssh-rsatdatasssh-dssN(tsplittlenRRtbase64t decodestringR(tclstlinetfieldstnamestkeytypeR ((s5/usr/lib/python2.6/site-packages/paramiko/hostkeys.pyt from_line*s    cCs>|io0ddi|iƒ|iiƒ|iiƒ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 R N(RtjoinRR tget_namet get_base64R(R ((s5/usr/lib/python2.6/site-packages/paramiko/hostkeys.pytto_lineLs cCsd|i|ifS(Ns(RR (R ((s5/usr/lib/python2.6/site-packages/paramiko/hostkeys.pyt__repr__WsN( t__name__t __module__t__doc__RR Rt classmethodRR(((s5/usr/lib/python2.6/site-packages/paramiko/hostkeys.pyR s   tHostKeyscBsŒeZdZd d„Zd„Zd„Zd„Zd„Zd„Z d„Z d„Z d „Z d „Z d „Zd d „ZeeƒZRS(sV 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 L{lookup}. @since: 1.5.3 cCs+g|_|dj o|i|ƒndS(sÞ Create a new HostKeys object, optionally loading keys from an openssh style host-key file. @param filename: filename to load host keys from, or C{None} @type filename: str N(t_entriesRtload(R tfilename((s5/usr/lib/python2.6/site-packages/paramiko/hostkeys.pyR gs  cCshxE|iD]:}||ijo$|iiƒ|jo||_dSq W|iit|g|ƒƒdS(sw Add a host key entry to the table. Any existing entry for a C{(hostname, keytype)} pair will be replaced. @param hostname: the hostname (or IP) to add @type hostname: str @param keytype: key type (C{"ssh-rsa"} or C{"ssh-dss"}) @type keytype: str @param key: the key to add @type key: L{PKey} N(R#RR RtappendR(R thostnameRR te((s5/usr/lib/python2.6/site-packages/paramiko/hostkeys.pytaddts &  cCs•t|dƒ}xu|D]m}|iƒ}t|ƒdjp|ddjoqnti|ƒ}|dj o|ii|ƒqqW|iƒdS(s† 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 C{os.path.expanduser("~/.ssh/known_hosts")}. If this method is called multiple times, the host keys are merged, not cleared. So multiple calls to C{load} will just call L{add}, replacing any existing entries and adding new ones. @param filename: name of the file to read host keys from @type filename: str @raise IOError: if there was an error reading the file trit#N( topentstripRRRRR#R&tclose(R R%tfRR(((s5/usr/lib/python2.6/site-packages/paramiko/hostkeys.pyR$†s $ cCsUt|dƒ}x5|iD]*}|iƒ}|o|i|ƒqqW|iƒdS(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 filename: name of the file to write @type filename: str @raise IOError: if there was an error writing the file @since: 1.6.1 twN(R,R#RtwriteR.(R R%R/R(R((s5/usr/lib/python2.6/site-packages/paramiko/hostkeys.pytsave s  cs¼dtif‡fd†ƒY}g}xl|iD]a}xX|iD]M}|idƒo|iˆ|ƒ|jp |ˆjo|i|ƒq?q?Wq/Wt|ƒdjodS|ˆ||ƒS(s¬ Find a hostkey entry for a given hostname or IP. If no entry is found, C{None} is returned. Otherwise a dictionary of keytype to key is returned. The keytype will be either C{"ssh-rsa"} or C{"ssh-dss"}. @param hostname: the hostname (or IP) to lookup @type hostname: str @return: keys associated with this host (or C{None}) @rtype: dict(str, L{PKey}) tSubDictcs2eZd„Zd„Z‡fd†Zd„ZRS(cSs||_||_||_dS(N(t _hostnameR#t _hostkeys(R R'tentriesthostkeys((s5/usr/lib/python2.6/site-packages/paramiko/hostkeys.pyR Ás  cSsBx/|iD]$}|iiƒ|jo|iSq Wt|ƒ‚dS(N(R#R RtKeyError(R R R(((s5/usr/lib/python2.6/site-packages/paramiko/hostkeys.pyt __getitem__Æs   csˆx|iD]A}|idjoq n|iiƒ|jo||_Pq q Wtˆg|ƒ}|ii|ƒ|iii|ƒdS(N(R#R RRRR&R5(R R tvalR((R'(s5/usr/lib/python2.6/site-packages/paramiko/hostkeys.pyt __setitem__Ìs   cSs?g}|iD]*}|idj o||iiƒqq~S(N(R#R RR(R t_[1]R(((s5/usr/lib/python2.6/site-packages/paramiko/hostkeys.pytkeysÚs(RRR R9R;R=((R'(s5/usr/lib/python2.6/site-packages/paramiko/hostkeys.pyR3Às  s|1|iN( tUserDictt DictMixinR#Rt startswitht hash_hostR&RR(R R'R3R6R(th((R's5/usr/lib/python2.6/site-packages/paramiko/hostkeys.pytlookupµs   6cCsa|i|ƒ}|djotS|i|iƒdƒ}|djotSt|ƒt|ƒjS(s‡ Return True if the given key is associated with the given hostname in this dictionary. @param hostname: hostname (or IP) of the SSH server @type hostname: str @param key: the key to check @type key: L{PKey} @return: C{True} if the key is associated with the hostname; C{False} if not @rtype: bool N(RCRtFalsetgetRtstr(R R'R tkthost_key((s5/usr/lib/python2.6/site-packages/paramiko/hostkeys.pytcheckæs   cCs g|_dS(s; Remove all host keys from the dictionary. N(R#(R ((s5/usr/lib/python2.6/site-packages/paramiko/hostkeys.pytclearûscCs0|i|ƒ}|djot|ƒ‚n|S(N(RCRR8(R R tret((s5/usr/lib/python2.6/site-packages/paramiko/hostkeys.pyR9s cCsÑt|ƒdjo!|iit|gdƒƒdSx–|iƒD]ˆ}t}xN|iD]C}||ijo-|ii ƒ|jo|||_t }qWqWW|p$|iit|g||ƒƒqAqAWdS(Ni( RR#R&RRR=RDRR RtTrue(R R'tentrytkey_typetfoundR(((s5/usr/lib/python2.6/site-packages/paramiko/hostkeys.pyR;s  & cCsPg}xC|iD]8}x/|iD]$}||jo|i|ƒq q WqW|S(N(R#RR&(R RKR(RB((s5/usr/lib/python2.6/site-packages/paramiko/hostkeys.pyR=s   cCs7g}x*|iƒD]}|i|i|ƒƒqW|S(N(R=R&RC(R RKRG((s5/usr/lib/python2.6/site-packages/paramiko/hostkeys.pytvaluess  cCsÃ|djotitiƒ}n7|idƒo|idƒd}nti|ƒ}t |ƒtijpt ‚t i ||tƒi ƒ}dti |ƒti |ƒf}|iddƒS(s~ Return a "hashed" form of the hostname, as used by openssh when storing hashed hostnames in the known_hosts file. @param hostname: the hostname to hash @type hostname: str @param salt: optional salt to use when hashing (must be 20 bytes long) @type salt: str @return: the hashed hostname @rtype: str s|1|t|is|1|%s|%ss tN(Rtrandpoolt get_bytesRt digest_sizeR@RRRRtAssertionErrorRtdigestt encodestringtreplace(R'tsaltthmacthostkey((s5/usr/lib/python2.6/site-packages/paramiko/hostkeys.pyRA%s "N(RRR RR R)R$R2RCRIRJR9R;R=RPRAt staticmethod(((s5/usr/lib/python2.6/site-packages/paramiko/hostkeys.pyR"[s     1      ((R Rt Crypto.HashRRR>tparamiko.commontparamiko.dsskeyRtparamiko.rsakeyRRR?R"(((s5/usr/lib/python2.6/site-packages/paramiko/hostkeys.pyts   ;