U q`!@sldZddlmZddlZddlZddlZddlZddlZddlZddl m Z m Z m Z m Z ddlmZmZmZmZmZddlmZd3dd Zere ndZere nd Zd4d d Zd5ddZddZddZddZddZddZddZ ddZ!dd Z"d!d"Z#ia$da%e&a'd#d$Z(e fd%d&Z)Gd'd(d(e*Z+e+Z,d)d*Z-d+d,Z.d-d.Z/Gd/d0d0e*Z0d1d2Z1dS)6z0 Useful functions used by the rest of paramiko. ) generatorsN)DEBUG zero_byte xffffffffmax_byte)PY2longbyte_chrbyte_ordb) SSHConfigFc Cstd}d}|s0t|dkr0t|ddkr0d}t|dr`t}|rHt}|dt|d|}tdt|dD](}|d>td|||dd}qp|r|tddt|>8}|S)zTturns a normalized byte string into a long-int (adapted from Crypto.Util.number)r >I)rlenr rrrangestructunpack)sZalways_positiveoutnegativeZfillerir4/tmp/pip-target-nv4zd3e_/lib/python/paramiko/util.py inflate_long%s  &rTcCst}t|}|dkr<|dkrs*  r&cspd}g}t||dkr<|t|||d|d7}q|t|kr^|t||dfdd|DS)Nrcsg|] }|qSrr).0lineprefixrr esz!format_binary..)rappendformat_binary_line)datar,xrrr+r format_binary]s  r2cCs4ddd|D}ddd|D}d||S)N cSsg|]}dt|qS)z{:02X}formatr r)crrrr-isz&format_binary_line..r'cSs*g|]"}dt|t|ddqS)z.{:c}..?_r4r6rrrr-ksz {:50s} {})joinr5)r0leftrightrrrr/hs  r/cCsRd}|D]D}t|}d|kr(dkr:nn|t|7}q|td|7}q|S)Nrz%{:02X})r r r r5)rrr7rrrr safe_stringpsr?cCsrz |WStk rlt|d}t|d}|dkr>YdSt|d}|d@sd|dK}|d8}qJ|YSXdS)NFrrrr ) bit_lengthAttributeErrorr&r r)r%ZnormZhbyteZbitlenrrrr@{s     r@cCsdtjtdS)Nr' )r: tracebackformat_exceptionsysexc_infosplitrrrr tb_stringssrHcCst}t}t|dkr$|dd}|dkr|}t|dkrH|||t||||}t|t|}||d|7}||8}q$|S)a Given a password, passphrase, or other human-source key, scramble it through a secure hash into some keyworthy bytes. This specific algorithm is used for encrypting/decrypting private key files. :param function hash_alg: A function which creates a new hash object, such as ``hashlib.sha256``. :param salt: data to salt the hash with. :type salt: byte string :param str key: human-entered password or passphrase. :param int nbytes: number of bytes to generate. :return: Key data `str` rNr)r rupdater digestmin)Zhash_algZsaltkeynbytesZkeydatarJZhash_objsizerrrgenerate_key_bytess      rOcCsddlm}||S)a Read a file of known SSH host keys, in the format used by openssh, and return a compound dict of ``hostname -> keytype ->`` `PKey `. The hostname may be an IP address or DNS name. The keytype will be either ``"ssh-rsa"`` or ``"ssh-dss"``. 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")``. Since 1.5.3, this is just a wrapper around `.HostKeys`. :param str filename: name of the file to read host keys from :return: nested dict of `.PKey` objects, indexed by hostname and then keytype r)HostKeys)Zparamiko.hostkeysrP)filenamerPrrrload_host_keyss rRcCst}|||S)z Provided only as a backward-compatible wrapper around `.SSHConfig`. .. deprecated:: 2.7 Use `SSHConfig.from_file` instead. )r parse)Zfile_objconfigrrrparse_ssh_configs rUcCs ||S)zM Provided only as a backward-compatible wrapper around `.SSHConfig`. )lookup)hostnamerTrrrlookup_ssh_host_configsrXc Cs|dd|}}}dd|}}}|dkrh||}||||}}||||}}||||}}q |dkrx||7}|S)Nrrr) r1mu1u2u3Zv1Zv2Zv3qrrr mod_inversesr^c Cs`tt}z t|WStk rZtztd7at}t|<W5tX|YSXdS)Nr) id threading currentThread _g_thread_idsKeyError_g_thread_lockacquirerelease_g_thread_counter)tidretrrr get_thread_ids   rjcCsftd}t|jdkrdS||t|d}t|}d}|d7}|t|d| |dS)zKsend paramiko logs to a logfile, if they're not already going somewhereZparamikorNaz>%(levelname)-.3s [%(asctime)s.%(msecs)03d] thr=%(_threadid)-3dz %(name)s: %(message)sz%Y%m%d-%H:%M:%S) logging getLoggerrhandlerssetLevelopen StreamHandler setFormatter Formatter addHandler)rQlevelloggerfhandlerfrmrrr log_to_files    rzc@seZdZddZdS)PFiltercCs t|_dS)NT)rjZ _threadid)selfrecordrrrfilter szPFilter.filterN)__name__ __module__ __qualname__r~rrrrr{sr{cCst|}|t|SN)rlrm addFilter_pfilter)namervrrr get_loggers  rc Cs@z|WStk r8}z|jtjkr(W5d}~XYqXqdS)z6Retries function until it doesn't raise an EINTR errorN)EnvironmentErrorerrnoZEINTR)functionerrrretry_on_signals  rcCsVt|t|krdSd}tr tntt|D] }|t||t||AO}q,|dkS)NFr)rrxrangerr )rkr resrrrrconstant_time_bytes_eq!s rc@seZdZddZddZdS)ClosingContextManagercCs|Srr)r|rrr __enter__,szClosingContextManager.__enter__cCs |dSr)close)r|typevaluerCrrr__exit__/szClosingContextManager.__exit__N)rrrrrrrrrr+srcCst|t||Sr)maxrK)Zminimumvalmaximumrrr clamp_value3sr)F)T)r')2__doc__ __future__rrrErrCr`rlZparamiko.commonrrrrZparamiko.py3compatrrr r r Zparamiko.configr rr#r$r&r2r/r?r@rHrOrRrUrXr^rbrgLockrdrjrzobjectr{rrrrrrrrrrsH