U vfN%@s@dZddlZddlZddlZddlZddlZddlmZmZm Z m Z m Z m Z ddl mZd5ddZd6d d Zd7d d ZddZddZddZddZddZddZddZddZddZeadaead d!Z efd"d#Z!Gd$d%d%Z"e"Z#d&d'Z$d(d)Z%Gd*d+d+Z&d,d-Z'd.d/Z(d8d1d2Z)d9d3d4Z*dS):z0 Useful functions used by the rest of paramiko. N)DEBUG zero_byte xffffffffmax_bytebyte_ordbyte_chr) SSHConfigFc Csd}d}|s,t|dkr,t|ddkr,d}t|dr\t}|rDt}|dt|d|}tdt|dD](}|d>td|||dd}ql|r|ddt|>8}|S)zTturns a normalized byte string into a long-int (adapted from Crypto.Util.number)r >I)lenrrrrangestructunpack)sZalways_positiveoutnegativeZfillerir>/opt/alt/python38/lib/python3.8/site-packages/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  r-cCs4ddd|D}ddd|D}d||S)N cSsg|]}dt|qS)z{:02X}formatrr$crrrr(isz&format_binary_line..r"cSs*g|]"}dt|t|ddqS)z.{:c}..?_r/r1rrrr(ksz {:50s} {})joinr0)r+leftrightrrrr*hs  r*cCsRd}|D]D}t|}d|kr(dkr:nn|t|7}q|td|7}q|S)Nr z%{:02X})rrbr0)rrr2rrrr safe_stringpsr;cCsrz |WStk rlt|d}t|d}|dkr>YdSt|d}|d@sd|dK}|d8}qJ|YSXdS)NFrr rr ) bit_lengthAttributeErrorr!rr)r ZnormZhbyteZbitlenrrrr<{s     r<cCsdtjtdS)Nr" )r5 tracebackformat_exceptionsysexc_infosplitrrrr tb_stringssrDcCst}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 bytes salt: Hash salt bytes. :param str key: human-entered password or passphrase. :param int nbytes: number of bytes to generate. :return: Key data, as `bytes`. rNr)rrupdater:digestmin)Zhash_algsaltkeynbytesZkeydatarFZhash_objsizerrrgenerate_key_bytess      rLcCsddlm}||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.hostkeysrM)filenamerMrrrload_host_keyss rOcCst}|||S)z Provided only as a backward-compatible wrapper around `.SSHConfig`. .. deprecated:: 2.7 Use `SSHConfig.from_file` instead. )rparse)Zfile_objconfigrrrparse_ssh_configs rRcCs ||S)zM Provided only as a backward-compatible wrapper around `.SSHConfig`. )lookup)hostnamerQrrrlookup_ssh_host_configsrUc Cs|dd|}}}dd|}}}|dkrh||}||||}}||||}}||||}}q |dkrx||7}|S)Nr rr) r,mu1u2u3v1v2Zv3qrrr mod_inversesr]c CsFztjWStk r@ttd7att_W5QRXtjYSXdS)Nr )_g_thread_dataidr=_g_thread_lock_g_thread_counterrrrr get_thread_idsrbcCsftd}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)rNlevelloggerfhandlerfrmrrr log_to_files    rrc@seZdZddZdS)PFiltercCs t|_dS)NT)rbZ _threadid)selfrecordrrrfilterszPFilter.filterN)__name__ __module__ __qualname__rvrrrrrssrscCst|}|t|SN)rdre addFilter_pfilter)namernrrr get_logger s  r~cCsNt|t|krdSd}tt|D] }|t||t||AO}q$|dkS)NFr)rrr)rcr:resrrrrconstant_time_bytes_eqs rc@seZdZddZddZdS)ClosingContextManagercCs|Srzr)rtrrr __enter__szClosingContextManager.__enter__cCs |dSrz)close)rttypevaluer?rrr__exit__!szClosingContextManager.__exit__N)rwrxryrrrrrrrsrcCst|t||Srz)maxrG)minimumvalmaximumrrr clamp_value%src CsPz t|WStk rJz|WYStk rD|YYSXYnXdS)z: Coerce to bytes if possible or return unchanged. N)r: TypeErrorasbytesr=)rrrrr)s rutf8cCs8t|tr|St|tr"||Stdt|dS)zcast unicode or bytes to bytesExpected unicode or bytes, got N) isinstancerstrencoderrrencodingrrrr:?s    r:cCs8t|tr||St|tr"|Stdt|dS)z cast bytes or unicode to unicoderN)rrdecoderrrrrrruJs    r)F)T)r")r)r)+__doc__rArr? threadingrdZparamiko.commonrrrrrrZparamiko.configrrr!r-r*r;r<rDrLrOrRrUr]localr^raLockr`rbrrrsr|r~rrrrr:rrrrrsB