
    bU                         d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ d dlmZ d dlmZ d dlZd	Zd
Zd Z G d de          ZdS )    N)SambaToolCmdTest)Credentials)SamDB)system_session)
ndr_unpack)drsblobs)dsdbCryptSHATestUserz"password hash userPassword schemesc                     t          j        d|z   dz             }|                     d          D ]0}|                    |          }|r|                    d          c S 1dS )N^z	:\s+(\S+)
    )recompilesplitmatchgroup)outnameplinems        M/usr/lib/python3/dist-packages/samba/tests/samba_tool/user_virtualCryptSHA.py_get_attributer   )   sj    

3:+,,A		$  GGDMM 	771::	2    c                        e Zd ZdZg ZdZ fdZddZ fdZd dZ	d	 Z
d
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z xZ S )!UserCmdCryptShaTestCasezz
    Tests for samba-tool user subcommands generation of the virtualCryptSHA256
    and virtualCryptSHA512 attributes
    Nc                 V    t          t          |                                            d S )N)superr   setUpself	__class__s    r   r!   zUserCmdCryptShaTestCase.setUp:   s&    %t,,2244444r   r   c                    t           j                                        | _        | j                            t
          |           t                      | _        t                      | _	        t          | j	        | j        | j                  | _        |                                 }|                     ddt          |           d S )N)session_infocredentialslpusercreate)sambatestsenv_loadparmr(   setHASH_OPTIONr   credsr   sessionr   ldbrandom_password	runsubcmd	USER_NAME)r#   hashespasswords      r   add_userz UserCmdCryptShaTestCase.add_user=   s    +**,, 	K((( ]]
%''
w  
 ''))v 	! 	! 	! 	! 	!r   c                     t          t          |                                            |                     ddt                     d S )Nr)   delete)r    r   tearDownr4   r5   r"   s    r   r;   z UserCmdCryptShaTestCase.tearDownP   s<    %t,,55777vx33333r   Fc                     ddt           d|g}|r|                    d            | j        | \  }}}|                     |||d           |                     |dd           |                     |dd|z             |S )	Nr)   getpasswordz--attributesz--decrypt-samba-gpgzEnsure getpassword runsr   zGot password OKzgetpassword out[%s])r5   appendr4   assertCmdSuccessassertEqualassertMatch)r#   
attributesdecryptcommandresultr   errs          r   _get_passwordz%UserCmdCryptShaTestCase._get_passwordT   s     !	
  	2NN0111+T^W5cf!!7	9 	9 	9 	b-000*.4	6 	6 	6 
r   c                 F   | j                             dt          z  t           j                  }t          j                    }|d         j        |_        t          j        dt           j        d          |d<   | j                             |dt          j
        z  g           d S )Nzcn=%s)
expressionscoper   s   ABCDEF1234567890
unicodePwdzlocal_oid:%s:0)controls)r2   searchr5   SCOPE_SUBTREEMessagednMessageElementFLAG_MOD_REPLACEmodifyr	   %DSDB_CONTROL_BYPASS_PASSWORD_HASH_OID)r#   resmsgs      r   _change_nt_hashz'UserCmdCryptShaTestCase._change_nt_hashk   s    hoo7Y+>+.+<  > >kmmQ./B/2/C/;= =L 	&@A B 	 	C 	C 	C 	C 	Cr   c                     |                                   |                     d          }|                     d|v           |                     d|v           |                     d|v           d S )N%virtualCryptSHA256,virtualCryptSHA512virtualCryptSHA256:virtualCryptSHA512:rounds=r8   rG   
assertTruer#   r   s     r   !test_no_gpg_both_hashes_no_roundsz9UserCmdCryptShaTestCase.test_no_gpg_both_hashes_no_rounds|   sp      !HII-S8999-S8999	,-----r   c                     |                                   |                     d          }|                     d|v           |                     d|v           |                     d|v           d S )NvirtualCryptSHA256rZ   r[   r\   r]   r_   s     r   test_no_gpg_sha256_no_roundsz4UserCmdCryptShaTestCase.test_no_gpg_sha256_no_rounds   p      !566-S8999-S8999	,-----r   c                     |                                   |                     d          }|                     d|v           |                     d|v           |                     d|v           d S )NvirtualCryptSHA512rZ   r[   r\   r]   r_   s     r   test_no_gpg_sha512_no_roundsz4UserCmdCryptShaTestCase.test_no_gpg_sha512_no_rounds   rd   r   c                     |                                   |                     d          }|                     d|v           |                     d|v           |                     d|v           d S )NvirtualCryptSHA128rZ   r[   r\   r]   r_   s     r   !test_no_gpg_invalid_alg_no_roundsz9UserCmdCryptShaTestCase.test_no_gpg_invalid_alg_no_rounds   rd   r   c                     |                                   |                     dd          }|                     d|v            |                     d|v            |                     d|v           d S NrY   TrZ   r[   r\   r]   r_   s     r   test_gpg_both_hashes_no_roundsz6UserCmdCryptShaTestCase.test_gpg_both_hashes_no_rounds   sr      !H$OO-4555-4555	,-----r   c                     |                                   |                     dd          }|                     d|v            |                     d|v           |                     d|v           d S Nrb   TrZ   r[   r\   r]   r_   s     r   test_gpg_sha256_no_roundsz1UserCmdCryptShaTestCase.test_gpg_sha256_no_rounds   sr      !5t<<-4555-S8999	,-----r   c                     |                                   |                     dd          }|                     d|v           |                     d|v            |                     d|v           d S )Nrf   TrZ   r[   r\   r]   r_   s     r   test_gpg_sha512_no_roundsz1UserCmdCryptShaTestCase.test_gpg_sha512_no_rounds   sr      !5t<<-S8999-4555	,-----r   c                     |                                   |                     dd          }|                     d|v           |                     d|v           |                     d|v           d S )Nri   TrZ   r[   r\   r]   r_   s     r   test_gpg_invalid_alg_no_roundsz6UserCmdCryptShaTestCase.test_gpg_invalid_alg_no_rounds   sr      !5t<<-S8999-S8999	,-----r   c                    |                                   |                                  |                     dd          }|                     d|v           |                     d|v           |                     d|v           d S rl   r8   rW   rG   r^   r_   s     r   *test_gpg_both_hashes_no_rounds_pwd_changedzBUserCmdCryptShaTestCase.test_gpg_both_hashes_no_rounds_pwd_changed   s      !H$OO-S8999-S8999	,-----r   c                    |                                   |                                  |                     dd          }|                     d|v           |                     d|v           |                     d|v           d S ro   rv   r_   s     r   %test_gpg_sha256_no_rounds_pwd_changedz=UserCmdCryptShaTestCase.test_gpg_sha256_no_rounds_pwd_changed         !5t<<-S8999-S8999	,-----r   c                    |                                   |                                  |                     dd          }|                     d|v           |                     d|v           |                     d|v           d S ro   rv   r_   s     r   %test_gpg_sha512_no_rounds_pwd_changedz=UserCmdCryptShaTestCase.test_gpg_sha512_no_rounds_pwd_changed  rz   r   c                    |                                   |                     dd          }|                     d|v            |                     d|v            t          |d          }|                     |                    d                     t          |d          }|                     |                    d                     d S )	Nz?virtualCryptSHA256;rounds=10123,virtualCryptSHA512;rounds=10456TrZ   r[   rb   z{CRYPT}$5$rounds=10123$rf   z{CRYPT}$6$rounds=10456$r8   rG   r^   r   
startswithr#   r   sha256sha512s       r    test_gpg_both_hashes_both_roundsz8UserCmdCryptShaTestCase.test_gpg_both_hashes_both_rounds  s      M  	-4555-4555%9::))*CDDEEE%9::))*CDDEEEEEr   c                    |                                   |                     dd          }|                     d|v            |                     d|v            t          |d          }|                     |                    d                     |                     d|v           t          |d          }|                     |                    d	                     d S )
Nz@virtualCryptSHA256;rounds=invalid,virtualCryptSHA512;rounds=3125TrZ   r[   rb   z
{CRYPT}$5$roundsrf   z{CRYPT}$6$rounds=3125$r~   r   s       r   *test_gpg_both_hashes_sha256_rounds_invalidzBUserCmdCryptShaTestCase.test_gpg_both_hashes_sha256_rounds_invalid)  s      N  	-4555-4555%9::)),77888.///%9::))*BCCDDDDDr   c                    |                      d           |                     d          }|                     d|v            |                     d|v            |                     d|v           t          |d          }t          |d          }|                     d          }|                     |t          |d                     |                     |t          |d                     d S )NCryptSHA512 CryptSHA256rY   rZ   r[   r\   rb   rf   r8   rG   r^   r   r@   r   s       r   /test_no_gpg_both_hashes_no_rounds_stored_hasheszGUserCmdCryptShaTestCase.test_no_gpg_both_hashes_no_rounds_stored_hashes=  s    /000  !HII-4555-4555	,---  %9::%9::  !HII5I!J!JKKK5I!J!JKKKKKr   c                    |                      d           |                     d          }|                     d|v            |                     d|v            |                     d|v           t          |d          }t          |d          }|                     d          }|                     |t          |d                     |                     |t          |d                     d S )	Nr   =virtualCryptSHA256;rounds=2561,virtualCryptSHA512;rounds=5129rZ   r[   r\   rb   rf   rY   r   r   s       r   ,test_no_gpg_both_hashes_rounds_stored_hasheszDUserCmdCryptShaTestCase.test_no_gpg_both_hashes_rounds_stored_hashesS  s    /000   "B C C 	-4555-4555	,---  %9::%9::  !HII5I!J!JKKK5I!J!JKKKKKr   c                 ~   |                      d           |                     d          }|                     d|v            |                     d|v            |                     d|v            t          |d          }t          |d          }|                     d          }|                     |t          |d                     |                     |t          |d                     |                     |                    d                     |                     |                    d	                     d S )
NGCryptSHA512 CryptSHA256 CryptSHA512:rounds=5129 CryptSHA256:rounds=2561r   rZ   r[   r\   rb   rf   {CRYPT}$5$rounds=2561{CRYPT}$6$rounds=5129r8   rG   r^   r   r@   r   r   s       r   8test_no_gpg_both_hashes_rounds_stored_hashes_with_roundszPUserCmdCryptShaTestCase.test_no_gpg_both_hashes_rounds_stored_hashes_with_roundsj  sP    0 	1 	1 	1
    "B C C 	-4555-4555	S()))  %9::%9::   "B C C5I!J!JKKK5I!J!JKKK 	))*ABBCCC))*ABBCCCCCr   c                    |                      d           |                     d          }|                     d|v            |                     d|v            |                     d|v           t          |d          }t          |d          }|                     d          }|                     |t          |d                     |                     |t          |d                     |                     d          }|                     |t          |d                     |                     |t          |d                     d S )	Nr   =virtualCryptSHA256;rounds=4000,virtualCryptSHA512;rounds=5000rZ   r[   r\   rb   rf   rY   r   r   s       r   Atest_no_gpg_both_hashes_rounds_stored_hashes_with_rounds_no_matchzYUserCmdCryptShaTestCase.test_no_gpg_both_hashes_rounds_stored_hashes_with_rounds_no_match  sj    0 	1 	1 	1
    "B C C 	-4555-4555	,---  %9::%9::   "B C C5I!J!JKKK5I!J!JKKK    "6 7 75I!J!JKKK5I!J!JKKKKKr   c                    |                      d           |                     dd          }|                     d|v            |                     d|v            |                     d|v           t          |d          }t          |d          }|                     dd          }|                     |t          |d                     |                     |t          |d                     d S )	Nr   rY   TrZ   r[   r\   rb   rf   r   r   s       r   ,test_gpg_both_hashes_no_rounds_stored_hasheszDUserCmdCryptShaTestCase.test_gpg_both_hashes_no_rounds_stored_hashes  s    /000  !H$OO-4555-4555	,---  %9::%9::  !H$OO5I!J!JKKK5I!J!JKKKKKr   c                    |                      d           |                     dd          }|                     d|v            |                     d|v            |                     d|v            t          |d          }t          |d          }|                     dd          }|                     |t          |d          k               |                     |t          |d          k               |                     |                    d	                     |                     |                    d
                     d S )Nr   r   TrZ   r[   r\   rb   rf   r   r   r8   rG   r^   r   assertFalser   r   s       r   )test_gpg_both_hashes_rounds_stored_hasheszAUserCmdCryptShaTestCase.test_gpg_both_hashes_rounds_stored_hashes  sP   /000   "B!%' ' 	-4555-4555	S()))  %9::%9::   "B!%' ' 	>#7K#L#LLMMM>#7K#L#LLMMM 	))*ABBCCC))*ABBCCCCCr   c                    |                      d           |                     dd          }|                     d|v            |                     d|v            |                     d|v            t          |d          }t          |d          }|                     dd          }|                     |t          |d                     |                     |t          |d                     |                     |                    d	                     |                     |                    d
                     d S )Nr   r   TrZ   r[   r\   rb   rf   r   r   r   r   s       r   5test_gpg_both_hashes_rounds_stored_hashes_with_roundszMUserCmdCryptShaTestCase.test_gpg_both_hashes_rounds_stored_hashes_with_rounds  sT    0 	1 	1 	1
    "B!%' ' 	-4555-4555	S()))  %9::%9::   "B!%' ' 	5I!J!JKKK5I!J!JKKK 	))*ABBCCC))*ABBCCCCCr   c                    |                      d           |                     dd          }|                     d|v            |                     d|v            |                     d|v            t          |d          }t          |d          }|                     dd          }|                     |t          |d          k               |                     |t          |d          k               |                     |                    d	                     |                     |                    d
                     d S )Nr   r   TrZ   r[   r\   rb   rf   z{CRYPT}$5$rounds=4000z{CRYPT}$6$rounds=5000r   r   s       r   >test_gpg_both_hashes_rounds_stored_hashes_with_rounds_no_matchzVUserCmdCryptShaTestCase.test_gpg_both_hashes_rounds_stored_hashes_with_rounds_no_match  sZ    0 	1 	1 	1
    "B!%' ' 	-4555-4555	S()))  %9::%9::   "B!%' ' 	>#7K#L#LLMMM>#7K#L#LLMMM 	))*ABBCCC))*ABBCCCCCr   )r   )F)!__name__
__module____qualname____doc__userssamdbr!   r8   r;   rG   rW   r`   rc   rg   rj   rm   rp   rr   rt   rw   ry   r|   r   r   r   r   r   r   r   r   r   r   __classcell__)r$   s   @r   r   r   2   s         EE5 5 5 5 5! ! ! !&4 4 4 4 4   .C C C". . .. . .. . .. . .. . .. . .. . .. . .. . .. . .. . .F F F(E E E(L L L,L L L.D D D@L L LDL L L,D D D<D D DDD D D D D D Dr   r   )ostimebase64r2   r+   samba.tests.samba_tool.baser   samba.credentialsr   samba.samdbr   
samba.authr   	samba.ndrr   samba.dcerpcr   r	   r   r5   r/   r   r    r   r   <module>r      s  & 
			   



  8 8 8 8 8 8 ) ) ) ) ) )       % % % % % %             ! ! ! ! ! !       					2  kD kD kD kD kD. kD kD kD kD kDr   