
    b,                     f    d dl Z d dl mZmZmZ d dlmZmZ  G d d          Z G d d          ZdS )    N)FLAG_MOD_DELETEFLAG_MOD_ADDFLAG_MOD_REPLACE)DOMAIN_PASSWORD_COMPLEXDOMAIN_PASSWORD_STORE_CLEARTEXTc                   F    e Zd ZddZd Zd Zd Zd Zd Zd Z	d	 Z
d
 ZdS )TestUserNc                    d}|| _         || _        d|d|pdd| j                                        | _        |g| _        |g| _        | j                            |||           | j                            d|z             d | _        d S )Nz
Initial12#CN=,zCN=Users)userouz(sAMAccountName=%s))	nameldb	domain_dndnall_old_passwordspwd_historynewuserenable_accountlast_pso)selfusernamesamdbr   initial_passwords        1/usr/lib/python3/dist-packages/samba/tests/pso.py__init__zTestUser.__init__   s    '	#+88f.B
.B.B#'8#5#5#7#7#79
 #3!3,-#3FCCC 5 @AAA    c                 2    |dk    rg S | j         | d         S )z0Returns the expected password history for the DCr   N)r   r   hist_lens     r   old_invalid_passwordszTestUser.old_invalid_passwords-   s&    q==I 	

++r   c                     |dk    r| j         dd         S t          t          | j                  |          }| j         d|          S )zAReturns old passwords that fall outside the DC's expected historyr   N)r   minlenr   r   s     r   old_valid_passwordszTestUser.old_valid_passwords5   sQ     q==)!!!,,
 s4+,,h77 %jyj11r   c                     || j         v r| j                             |           | j                             |           || j        v r| j                            |           | j                            |           dS )z@Updates the user's password history to reflect a password changeN)r   removeappendr   )r   new_passwords     r   update_pwd_historyzTestUser.update_pwd_historyC   s     4111")),777%%l3334+++##L111-----r   c                     | j                             | j        dg          }d|d         v r!t          |d         d         d                   S dS )z=Returns the DN of the applicable PSO, or None if none applieszmsDS-ResultantPSO)attrsr   N)r   searchr   str)r   ress     r   get_resultant_PSOzTestUser.get_resultant_PSOR   sP    hoodg.A-BoCC#a&((s1v12156664r   c                     | j         d         S )z#Returns the user's current password)r   )r   s    r   get_passwordzTestUser.get_password[   s     %b))r   c                     d| j         d|                                 d|d}| j                            |           |                     |           dS )z$Attempts to change a user's passwordz
dn: z7
changetype: modify
delete: userPassword
userPassword: z!
add: userPassword
userPassword: 
N)r   r3   r   modify_ldifr*   )r   r)   ldifs      r   set_passwordzTestUser.set_password`   sb      www!!####\\\3 	T"""-----r   c                     t          ||          }|dk    rg | _        n-|t          | j                  k     r| j        | d         | _        |dk    r"|dk    r|                                 g| _        dS dS dS )ay  
        Updates the effective password history, to reflect changes on the DC.
        When the PasswordHistoryLength applied to a user changes from a low
        setting (e.g. 2) to a higher setting (e.g. 4), passwords #3 and #4
        won't actually have been stored on the DC, so we need to make sure they
        are removed them from our mirror pwd_history list.
        r   N)r#   r   r$   r3   )r   old_hist_lennew_hist_lenr    s       r   pwd_history_changezTestUser.pwd_history_changep   s     |\22q==!DD,----#/	

;D 1!1!1 $ 1 1 3 34D !1!1r   c                 P   | j                             |dgt           j                  }|d         d         }t          j                    }t          j        | j         | j                  |_        t          j        |t          d          |d<   | j                             |           dS )z>Sets a user's primaryGroupID to be that of the specified groupprimaryGroupToken)baser,   scoper   primaryGroupIDN)	r   r-   
SCOPE_BASEMessageDnr   MessageElementr   modify)r   group_dnr/   group_idms        r   set_primary_groupzTestUser.set_primary_group   s     hoo84G3H$'N  4 4q6-. KMMvdh((!0;K1AC C
r   N)__name__
__module____qualname__r   r!   r%   r*   r0   r3   r8   r<   rJ    r   r   r	   r	      s           , , ,2 2 2. . .  * * *
. . . 5 5 52    r   r	   c                   B    e Zd Zd Z	 	 	 	 dd
Zd ZefdZd ZddZ	d	S )PasswordSettingsc                    g d}|                     |                                t          j        |          }d| _        d| _        || _        d| _        t          |d         d         d                   t          z  | _	        t          |d         d         d                   t          z  | _        t          |d         d         d                   | _        t          |d         d         d                   | _        t          |d         d	         d                   | _        t          |d         d
         d                   t          d           z  | _        t          |d         d         d                   t          d           z  | _        t          |d         d         d                   t          d           z  | _        t          |d         d         d                   t          d           z  | _        dS )z
        Returns a object representing the default password settings that will
        take effect (i.e. when no other Fine-Grained Password Policy applies)
        )		minPwdAgelockoutDurationlockOutObservationWindowlockoutThreshold	maxPwdAgerS   minPwdLengthpwdHistoryLengthpwdProperties)r@   r,   DefaultsNr   rZ   rX   rV   rY   rT       cArU   rS   rW   )r-   r   r   rB   r   r   
precedenceintr   
complexityr   store_plaintextpassword_lenlockout_attemptshistory_lenlockout_durationlockout_windowpassword_age_minpassword_age_max)r   r   pw_attrsr/   s       r   default_settingsz!PasswordSettings.default_settings   s   
I I I ll5??,,CN!)  + + 	A'*++.EE 	 A'*++.MM 	A~ 6q 9:: #CF+=$>q$A B Bs1v&89!<== #CF+<$=a$@ A ASXXI MA121566#c((B 	 #CF;$7$: ; ;s3xxi G #CF;$7$: ; ;s3xxi Gr   
   Tr       '    FNc                    ||                      |          S ||                                }d|z  }|| _        d|d|| _        || _        || _        || _        || _        || _        || _	        |
| _
        || _        || _        || _        |	| _        | j                            |                                            d S )Nz+CN=Password Settings Container,CN=System,%sr   r   )ri   r   r   r   r   r]   r_   r`   ra   rb   rc   rd   re   rf   rg   add_ldifget_ldif)r   r   r   r]   r_   ra   rb   rd   rf   rg   rc   r`   	containerbase_dns                 r   r   zPasswordSettings.__init__   s     <((/// oo''GEOI		 $ii0$$.( 0& 0 / 0 0 	$--//*****r   c                 b   | j         rdnd}| j        rdnd}t          | j        dz             }t          | j        dz             }t          | j        dz             }t          | j        dz             }d                    | j        | j	        || j
        || j        ||| j        ||          }|S )NTRUEFALSEr\   av  
dn: {0}
objectClass: msDS-PasswordSettings
msDS-PasswordSettingsPrecedence: {1}
msDS-PasswordReversibleEncryptionEnabled: {2}
msDS-PasswordHistoryLength: {3}
msDS-PasswordComplexityEnabled: {4}
msDS-MinimumPasswordLength: {5}
msDS-MinimumPasswordAge: {6}
msDS-MaximumPasswordAge: {7}
msDS-LockoutThreshold: {8}
msDS-LockoutObservationWindow: {9}
msDS-LockoutDuration: {10}
)r_   r`   r^   rd   re   rf   rg   formatr   r]   rc   ra   rb   )r   complexity_strplaintext_strrd   re   min_agemax_ager7   s           r   rp   zPasswordSettings.get_ldif   s    #'??"&"6CG   5 =>>>d1S9:::t,4555t,4555 F47DO]D4D4,gw .2BD D 	" r   c                     t          j                    }t          j        | j         | j                  |_        t          j        ||d          |d<   | j                             |           dS )zAUpdates this Password Settings Object to apply to a user or groupzmsDS-PSOAppliesToN)r   rC   rD   r   rE   rF   )r   
user_group	operationrI   s       r   apply_tozPasswordSettings.apply_to   s_    KMMvdh((!$!3J	4G"I "I
r   c                 >    |                      |t                     dS )z6Updates this PSO to no longer apply to a user or group)r}   N)r~   r   )r   r|   s     r   unapplyzPasswordSettings.unapply  s      	jO<<<<<r   c                 h    || j         }d| j        |fz  }|                    |           || _        d S )Nzh
dn: %s
changetype: modify
replace: msDS-PasswordSettingsPrecedence
msDS-PasswordSettingsPrecedence: %u
)r   r   r6   r]   )r   new_precedencer   r7   s       r   set_precedencezPasswordSettings.set_precedence  sG    =HE
 w  	$(r   )
rj   Trj   r   rk   r   rl   rm   FNrK   )
rL   rM   rN   ri   r   rp   r   r~   r   r   rO   r   r   rQ   rQ      s        H H H: ?CGH6GAE"+ "+ "+ "+H  < .:    = = =

) 
) 
) 
) 
) 
)r   rQ   )	r   r   r   r   samba.dcerpc.samrr   r   r	   rQ   rO   r   r   <module>r      s   . 


 ? ? ? ? ? ? ? ? ? ?@ @ @ @ @ @ @ @y y y y y y y yxw) w) w) w) w) w) w) w) w) w)r   