a
    I_,                     @   sL   d dl Z d dl mZmZmZ d dlmZmZ G dd dZG dd dZdS )    N)FLAG_MOD_DELETEFLAG_MOD_ADDFLAG_MOD_REPLACE)DOMAIN_PASSWORD_COMPLEXDOMAIN_PASSWORD_STORE_CLEARTEXTc                   @   sV   e Zd ZdddZdd Zdd Zdd	 Zd
d Zdd Zdd Z	dd Z
dd ZdS )TestUserNc                 C   sf   d}|| _ || _d||pd| j f | _|g| _|g| _| jj|||d | jd|  d | _d S )Nz
Initial12#zCN=%s,%s,%szCN=Users)userouz(sAMAccountName=%s))	nameldb	domain_dndnall_old_passwordspwd_historyZnewuserZenable_accountZlast_pso)selfZusernamesamdbr   Zinitial_password r   1/usr/lib/python3/dist-packages/samba/tests/pso.py__init__   s    
zTestUser.__init__c                 C   s   |dkrg S | j | d S )z0Returns the expected password history for the DCr   N)r   r   hist_lenr   r   r   old_invalid_passwords-   s    zTestUser.old_invalid_passwordsc                 C   s6   |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   r   r   r   old_valid_passwords5   s    zTestUser.old_valid_passwordsc                 C   sH   || j v r| j | | j | || jv r8| j| | j| dS )z@Updates the user's password history to reflect a password changeN)r   removeappendr   )r   new_passwordr   r   r   update_pwd_historyC   s    

zTestUser.update_pwd_historyc                 C   s<   | j j| jdgd}d|d v r4t|d d d S 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   resr   r   r   get_resultant_PSOR   s    zTestUser.get_resultant_PSOc                 C   s
   | j d S )z#Returns the user's current password)r   )r   r   r   r   get_password[   s    zTestUser.get_passwordc                 C   s.   d| j |  |f }| j| | | dS )z$Attempts to change a user's passwordzd
dn: %s
changetype: modify
delete: userPassword
userPassword: %s
add: userPassword
userPassword: %s
N)r   r$   r
   modify_ldifr   )r   r   ldifr   r   r   set_password`   s
    zTestUser.set_passwordc                 C   sZ   t ||}|dkrg | _n |t| jk r:| j| d | _|dkrV|dkrV|  g| _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   r$   )r   Zold_hist_lenZnew_hist_lenr   r   r   r   pwd_history_changep   s    
zTestUser.pwd_history_changec                 C   s^   | j j|dgt jd}|d d }t  }t | j | j|_t |td|d< | j | dS )z>Sets a user's primaryGroupID to be that of the specified groupZprimaryGroupToken)baser   scoper   ZprimaryGroupIDN)	r
   r   
SCOPE_BASEMessageDnr   MessageElementr   modify)r   Zgroup_dnr!   Zgroup_idmr   r   r   set_primary_group   s    zTestUser.set_primary_group)N)__name__
__module____qualname__r   r   r   r   r"   r$   r'   r(   r1   r   r   r   r   r      s   
	r   c                
   @   sD   e Zd Zdd ZdddZdd ZefddZdd ZdddZ	d
S )PasswordSettingsc                 C   s0  g d}|j | tj|d}d| _d| _|| _d| _t|d d d t@ | _	t|d d d t
@ | _t|d d d | _t|d d d | _t|d d	 d | _t|d d
 d td  | _t|d d d td  | _t|d d d td  | _t|d d d td  | _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	maxPwdAger6   minPwdLengthpwdHistoryLengthpwdProperties)r*   r   ZDefaultsNr   r=   r;   r9   r<   r7       cAr8   r6   r:   )r   r   r
   r+   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   Zpw_attrsr!   r   r   r   default_settings   s(      z!PasswordSettings.default_settings
   Tr       '    FNc                 C   s   |d u r|  |S |d u r*| }d| }|| _d||f | _|| _|| _|| _|| _|| _|| _	|
| _
|| _|| _|| _|	| _| j|   d S )Nz+CN=Password Settings Container,CN=System,%szCN=%s,%s)rJ   r   r	   r   r
   r?   rA   rB   rC   rD   rE   rF   rG   rH   rI   Zadd_ldifget_ldif)r   r	   r   r?   rA   rC   rD   rF   rH   rI   rE   rB   Z	containerZbase_dnr   r   r   r      s&    
zPasswordSettings.__init__c                 C   s   | j r
dnd}| jrdnd}t| jd  }t| jd  }t| jd  }t| jd  }d| j| j	|| j
|| j||| j||}|S )NZTRUEZ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}
)rA   rB   r@   rF   rG   rH   rI   formatr   r?   rE   rC   rD   )r   Zcomplexity_strZplaintext_strrF   rG   Zmin_ageZmax_ager&   r   r   r   rO      s    
zPasswordSettings.get_ldifc                 C   s<   t  }t | j | j|_t ||d|d< | j | dS )zAUpdates this Password Settings Object to apply to a user or groupzmsDS-PSOAppliesToN)r
   r,   r-   r   r.   r/   )r   
user_group	operationr0   r   r   r   apply_to   s    zPasswordSettings.apply_toc                 C   s   | j |td dS )z6Updates this PSO to no longer apply to a user or group)rR   N)rS   r   )r   rQ   r   r   r   unapply  s    zPasswordSettings.unapplyc                 C   s0   |d u r| j }d| j|f }|| || _d S )Nzh
dn: %s
changetype: modify
replace: msDS-PasswordSettingsPrecedence
msDS-PasswordSettingsPrecedence: %u
)r
   r   r%   r?   )r   Znew_precedencer   r&   r   r   r   set_precedence  s    
zPasswordSettings.set_precedence)
rK   TrK   r   rL   r   rM   rN   FN)N)
r2   r3   r4   rJ   r   rO   r   rS   rT   rU   r   r   r   r   r5      s       
$r5   )	r
   r   r   r   Zsamba.dcerpc.samrr   r   r   r5   r   r   r   r   <module>   s   |