a ì)g )ã @sÜddlmZmZmZeZddlZddlZddlZddl Z ddl Z ddl m Z ddl mZddlmZmZddlmZddlmZmZddlmZdZZd ZZzXddlZddlZdd l m!Z!m"Z"zdd l#m$Z$Wne%yðdd l&m$Z$Yn0d ZWn*e'�y"Z(ze(ZWYdZ([(n dZ([(00zddl)Z)d ZWn*e'�y^Z(ze(ZWYdZ([(n dZ([(00eƒZ*d gZ+e ,¡Z-dZ.e.ej/dfdd„Z0ddd„Z1Gdd„de2ƒZ3Gdd„de3ƒZ4Gdd„de3ƒZ5ddd„Z6ddd „Z7dS) é)Úabsolute_importÚdivisionÚprint_functionN)Ú namedtuple)Ú constants)Ú AnsibleErrorÚAnsibleAssertionError)Ú text_type)Úto_textÚto_bytes)ÚDisplayF)Ú HasRawSaltÚ PrefixWrapper)Úbcrypt64TÚ do_encryptécsXtˆtƒstdˆtˆƒfƒ‚|dur0t ¡‰n t |¡‰d ‡‡fdd„t|ƒDƒ¡S)aReturn a random password string of length containing only chars :kwarg length: The number of characters in the new password. Defaults to 20. :kwarg chars: The characters to choose from. The default is all ascii letters, ascii digits, and these symbols ``.,:-_`` z%s (%s) is not a text_typeNÚc3s|]}ˆ ˆ¡VqdS©N)Úchoice)Ú.0Zdummy©ÚcharsZrandom_generator©ú9/usr/lib/python3.9/site-packages/ansible/utils/encrypt.pyÚ Aóz"random_password..) Ú isinstancer rÚtypeÚrandomZ SystemRandomZRandomÚjoinÚrange)ÚlengthrZseedrrrÚrandom_password3s    r"écCstjtjd}t||d�S)zhReturn a text string suitable for use as a salt for the hash functions we use to encrypt passwords. z./)r!r)ÚstringZ ascii_lettersÚdigitsr")r!Z salt_charsrrrÚ random_saltDsr&c @sheZdZedgd¢ƒZedddddd�eddd d dd�ed d d ddd�edd dddd�dœZdd„ZdS)ÚBaseHashÚalgo)Úcrypt_idÚ salt_sizeÚimplicit_roundsÚ salt_exactÚimplicit_identÚ1r#NFZ2béé TÚ5éiØ)Ú6i€ )Z md5_cryptÚbcryptZ sha256_cryptZ sha512_cryptcCs ||_dSr)Ú algorithm©Úselfr5rrrÚ__init__VszBaseHash.__init__)Ú__name__Ú __module__Ú __qualname__rr(Ú algorithmsr8rrrrr'Msür'csFeZdZ‡fdd„Zddd„Zdd„Zdd „Zd d „Zd d „Z‡Z S)Ú CryptHashcsjtt|ƒ |¡ts tdtd�‚tj d¡r4tdƒ‚||j vrLtd|j ƒ‚t j ddd�|j ||_ dS) NzYcrypt.crypt cannot be used as the 'crypt' python library is not installed or is unusable.©Úorig_excÚdarwinzKcrypt.crypt not supported on Mac OS X/Darwin, install passlib python moduleú+crypt.crypt does not support '%s' algorithmzÇEncryption using the Python crypt module is deprecated. The Python crypt module is deprecated and will be removed from Python 3.13. Install the passlib library for continued encryption functionality.g\�Âõ(\@)Úversion)Úsuperr=r8Ú HAS_CRYPTrÚCRYPT_EÚsysÚplatformÚ startswithr<r5ÚdisplayZ deprecatedÚ algo_datar6©Ú __class__rrr8[s   ûzCryptHash.__init__NcCs0| ||¡}| |¡}| |¡}| ||||¡Sr)Ú_saltÚ_roundsÚ_identÚ_hash©r7ÚsecretÚsaltr*ÚroundsÚidentrrrÚhashqs   zCryptHash.hashcCsr|p |jj}|pt|ƒ}t d|¡r,tdƒ‚|jjrNt|ƒ|jjkrNtdƒ‚n |jjsnt|ƒ|jjkrntdƒ‚|S)Nz[^./0-9A-Za-z]zinvalid characters in saltzinvalid salt size)rJr*r&ÚreÚsearchrr,Úlen)r7rSr*ÚretrrrrMws    zCryptHash._saltcCs||jjkrdS|SdSr)rJr+)r7rTrrrrN‚s zCryptHash._roundscCs|s |jjS|jdkr|SdS©Nr4)rJr)r5)r7rUrrrrOŠs  zCryptHash._identc Csˆd}|rd|}|r |d|7}|d|7}zt ||¡}d}Wn,tyl}zd}|}WYd}~n d}~00|s„td|j|d�‚|S)Nrz$%sz $rounds=%drAr>)ÚcryptÚOSErrorrr5) r7rRrSrTrUZ saltstringÚresultr?ÚerrrrP‘s$   þzCryptHash._hash)NNNN) r9r:r;r8rVrMrNrOrPÚ __classcell__rrrKrr=Zs    r=csFeZdZ‡fdd„Zddd„Zdd„Zdd „Zd d „Zd d „Z‡Z S)Ú PasslibHashcsZtt|ƒ |¡ts$td|td�‚zttj|ƒ|_ Wnt yTtd|ƒ‚Yn0dS)Nz6passlib must be installed and usable to hash with '%s'r>z'passlib does not support '%s' algorithm) rCrar8ÚPASSLIB_AVAILABLErÚ PASSLIB_EÚgetattrÚpasslibrVÚ crypt_algoÚ Exceptionr6rKrrr8°s zPasslibHash.__init__NcCs2| |¡}| |¡}| |¡}|j|||||d�S)N©rSr*rTrU)Ú _clean_saltÚ _clean_roundsÚ _clean_identrPrQrrrrV»s   zPasslibHash.hashcCs:d}|s(|j|jvr$|j |j¡jS|S|jdkr6|S|Sr[)r5r<Úgetr-)r7rUrZrrrrkÁs  zPasslibHash._clean_identcCs^|sdStt|jtƒr|jjn|jtƒr8t|ddd�}nt|ddd�}|jdkrZt   |¡}|S)NÚasciiÚstrict)ÚencodingÚerrorsr4) Ú issubclassrrfrÚwrappedr r r r5rZ repair_unused)r7rSrZrrrriËs   zPasslibHash._clean_saltcCs.|j |j¡}|r|S|r&|jr&|jSdSdSr)r<rlr5r+)r7rTrJrrrrjÙs  zPasslibHash._clean_roundsc CsÞi}|r||d<|r||d<|r(||d<|r4||d<zZt|jdƒr\|jjfi|¤Ž |¡}n0t|jdƒr~|jj|fi|¤Ž}ntdtjƒ‚Wn0ty¾}ztd|d �‚WYd}~n d}~00|sÒtd |j ƒ‚t |d d �S) NrSr*rTrUrVÚencryptz*installed passlib version %s not supportedzCould not hash the secret.r>z"failed to hash with algorithm '%s'rn)rp) ÚhasattrrfZusingrVrsrreÚ __version__Ú ValueErrorr5r ) r7rRrSr*rTrUZsettingsr^r_rrrrPås(  "zPasslibHash._hash)NNNN) r9r:r;r8rVrkrirjrPr`rrrKrra¯s    racCsHtrt|ƒj|||||d�Str8t|ƒj|||||d�Stdtd�‚dS)NrhzFUnable to encrypt nor hash, either crypt or passlib must be installed.r>)rbrarVrDr=rrE)rRr5rSr*rTrUrrrÚpasslib_or_crypt s rwcCst|||||d�S)N)r*rSrU)rw)r^rsr*rSrUrrrrs)r#)NNNN)NNN)8Z __future__rrrrZ __metaclass__ZmultiprocessingrrWr$rFÚ collectionsrZansiblerÚCZansible.errorsrrZansible.module_utils.sixr Zansible.module_utils._textr r Zansible.utils.displayr rcrErDrbreZ passlib.hashZpasslib.utils.handlersr rZpasslib.utils.binaryrÚ ImportErrorZ passlib.utilsrgr_r\rIÚ__all__ÚLockZ_LOCKZDEFAULT_PASSWORD_LENGTHZDEFAULT_PASSWORD_CHARSr"r&Úobjectr'r=rarwrrrrrÚsR       U[