3 L]|!@sldZddlmZddlZddlZddlZddlZddlZddlZddl m Z m Z m Z m Z ddlmZmZmZmZmZddlmZd3dd Zere ndZere nd Zd4d d Zd5ddZddZddZddZddZddZddZ ddZ!dd Z"d!d"Z#ia$da%ej&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) SSHConfigFcCstd}d}| r2t|dkr2t|ddkr2d}t|drbt}|rJt}|dt|d|}xtjd|||dd}qtW|r|tddt|>8}|S)zTturns a normalized byte string into a long-int (adapted from Crypto.Util.number)r z>I)rlenr rrrangestructunpack)sZalways_positiveoutnegativeZfillerir3/tmp/pip-install-wfra5znf/paramiko/paramiko/util.py inflate_long%s" (rTcCst}t|}x0|dkr>|dkr>tjd|t@|}|dL}qWxRt|D]0}|dkrd|dtkrdP|dkrJ|dtkrJPqJWd }|dkrt}nt }||dd}|r|dkrt |ddkrt|}|d krt |ddkrt |}|S) zTturns a long-int into a normalized byte string (adapted from Crypto.Util.number)rrz>IrNr r)rr) bytesrrpackr enumerate deflate_zero deflate_ffrrr )nZadd_sign_paddingrrrrr deflate_long>s* r%cstd}g}x6t||dkr>|jt|||d|d7}q W|t|krb|jt||dfdd|DS)Nrcsg|] }|qSrr).0line)prefixrr esz!format_binary..)rappendformat_binary_line)datar*xrr)r*r format_binary]s  r0cCs4djdd|D}djdd|D}dj||S)N cSsg|]}djt|qS)z{:02X})formatr )r(crrrr+isz&format_binary_line..r&cSs*g|]"}djt|t|ddqS)z.{:c}..?_)r2r )r(r3rrrr+ksz {:50s} {})joinr2)r.leftrightrrrr-hsr-cCsVd}xL|D]D}t|}d|ko(dknr<|t|7}q |tdj|7}q W|S)Nrz%{:02X})r r r r2)rrr3rrrr safe_stringps r;c Csny|jStk rht|d}t|d}|dkr:dSt|d}x|d@sb|dK}|d8}qHW|SXdS)NFrrrr ) bit_lengthAttributeErrorr%r r)r$ZnormZhbyteZbitlenrrrr<{s     r<cCsdjtjtjjdS)Nr& )r6 tracebackformat_exceptionsysexc_infosplitrrrr tb_stringssrDcCst}t}t|dkr$|dd}xn|dkr|}t|dkrJ|j||jt||j||j}t|t|}||d|7}||8}q&W|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)rrupdater digestmin)Zhash_algZsaltkeynbytesZkeydatarFZhash_objsizerrrgenerate_key_bytess       rKcCsddlm}||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.hostkeysrL)filenamerLrrrload_host_keyss rNcCst}|j||S)zM Provided only as a backward-compatible wrapper around `.SSHConfig`. )r parse)Zfile_objconfigrrrparse_ssh_configs rQcCs |j|S)zM Provided only as a backward-compatible wrapper around `.SSHConfig`. )lookup)hostnamerPrrrlookup_ssh_host_configsrTc Csdd|}}}dd|}}}xJ|dkrj||}||||}}||||}}||||}}q"W|dkr|||7}|S)Nrrr) r/mu1u2u3Zv1Zv2Zv3qrrr mod_inverses rZcCsZttj}yt|Stk rTtjztd7at}t|<WdtjX|SXdS)Nr) id threading currentThread _g_thread_idsKeyError_g_thread_lockacquire_g_thread_counterrelease)tidretrrr get_thread_ids  rfcCsftjd}t|jdkrdS|j|t|d}tj|}d}|d7}|jtj|d|j |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)rMlevelloggerfhandlerfrmrrr log_to_files    rvc@seZdZddZdS)PFiltercCs t|_dS)NT)rfZ _threadid)selfrecordrrrfilterszPFilter.filterN)__name__ __module__ __qualname__rzrrrrrwsrwcCstj|}|jt|S)N)rhri addFilter_pfilter)namerrrrr get_loggers  rcCsBxsH