a
    Wa!                     @   s   d dl Zd dlmZmZ d dlmZ d dlmZ zd dl	m
Z
 d dlmZ W n ey^   Y n0 G dd deZG d	d
 d
eZG dd deZdS )    N)gp_ext_setter
gp_inf_ext)system_session)
get_string)LdbError)SamDBc                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )inf_to_kdc_tdbc                 C   s   dt | jd  S )N%d<   intvalself r   2/usr/lib/python3/dist-packages/samba/gp_sec_ext.pymins_to_hours   s    zinf_to_kdc_tdb.mins_to_hoursc                 C   s   dt | jd  S )Nr	      r   r   r   r   r   days_to_hours!   s    zinf_to_kdc_tdb.days_to_hoursc                 C   s   | j j| j}| jd| j||f  |d urj| j j| jt| | j t| | j|rbt|nd  n$| j j	| j | j 	t| | j d S )Nz%s was changed from %s to %s)
gp_dbZgpostoreget	attributeloggerinfostorer   strdeleter   r   Zold_valr   r   r   set_kdc_tdb$   s    $zinf_to_kdc_tdb.set_kdc_tdbc                 C   s$   | j | jf| j | jf| j | jfdS )N)kdc:user_ticket_lifetimekdc:service_ticket_lifetimekdc:renewal_lifetime)r   explicitr   r   r   r   r   r   mapper/   s    
zinf_to_kdc_tdb.mapperc                 C   s   dS )NKerberos Policyr   r   r   r   r   __str__7   s    zinf_to_kdc_tdb.__str__N)__name__
__module____qualname__r   r   r   r#   r%   r   r   r   r   r      s
   r   c                       sX   e Zd ZdZ fddZdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Z  ZS )
inf_to_ldbzThis class takes the .inf file parameter (essentially a GPO file mapped
    to a GUID), hashmaps it to the Samba parameter, which then uses an ldb
    object to update the parameter to Samba4. Not registry oriented whatsoever.
    c              	      s`   t t| |||||| z"t| j t | j| jd| _W n t	t
fyZ   tdY n0 d S )N)Zsession_infoZcredentialslpz/Failed to load SamDB for assigning Group Policy)superr)   __init__r   r*   Z	samdb_urlr   credsldb	NameErrorr   	Exception)r   r   r   r*   r-   keyvalue	__class__r   r   r,   A   s    
zinf_to_ldb.__init__c                 C   sH   | j  }| jd||f  | jt| | jt| | j | d S )Nz2KDC Minimum Password age was changed from %s to %s)	r.   Zget_minPwdAger   r   r   r   r   r   Zset_minPwdAger   r   r   r   ch_minPwdAgeK   s    
zinf_to_ldb.ch_minPwdAgec                 C   sH   | j  }| jd||f  | jt| | jt| | j | d S )Nz2KDC Maximum Password age was changed from %s to %s)	r.   Zget_maxPwdAger   r   r   r   r   r   Zset_maxPwdAger   r   r   r   ch_maxPwdAgeR   s    
zinf_to_ldb.ch_maxPwdAgec                 C   sH   | j  }| jd||f  | jt| | jt| | j | d S )Nz5KDC Minimum Password length was changed from %s to %s)	r.   Zget_minPwdLengthr   r   r   r   r   r   Zset_minPwdLengthr   r   r   r   ch_minPwdLengthY   s    
zinf_to_ldb.ch_minPwdLengthc                 C   sH   | j  }| jd||f  | jt| | jt| | j | d S )Nz2KDC Password Properties were changed from %s to %s)	r.   Zget_pwdPropertiesr   r   r   r   r   r   Zset_pwdPropertiesr   r   r   r   ch_pwdPropertiesa   s    
zinf_to_ldb.ch_pwdPropertiesc                 C   s8   d}d}d}d}| j }t|}t|| | | |  S )Nr
   r   i )r   r   r   )r   ZsecondsZminutesZhoursZsam_addr   r   r   r   days2rel_nttimeh   s    zinf_to_ldb.days2rel_nttimec                 C   s.   | j | jf| j| jf| j| jf| j| jfdS )zldap value : samba setter)	minPwdAge	maxPwdAgeminPwdLengthpwdProperties)r5   r9   r6   r7   r"   r8   r   r   r   r   r#   q   s
    



zinf_to_ldb.mapperc                 C   s   dS )NSystem Accessr   r   r   r   r   r%   |   s    zinf_to_ldb.__str__)r&   r'   r(   __doc__r,   r5   r6   r7   r8   r9   r#   r%   __classcell__r   r   r3   r   r)   ;   s   
	r)   c                   @   s,   e Zd ZdZdZdd Zdd Zdd Zd	S )

gp_sec_extzThis class does the following two things:
        1) Identifies the GPO if it has a certain kind of filepath,
        2) Finally parses it.
    r   c                 C   s   dS )NzSecurity GPO extensionr   r   r   r   r   r%      s    zgp_sec_ext.__str__c                 C   s8   dt fdt fdt fdt fddtfdtfdtfd	d
S )Nr:   r;   r<   r=   )ZMinimumPasswordAgeZMaximumPasswordAgeZMinimumPasswordLengthZPasswordComplexityr   r    r!   )ZMaxTicketAgeZMaxServiceAgeZMaxRenewAge)r>   r$   )r)   r   r   r   r   r   	apply_map   s0    
zgp_sec_ext.apply_mapc              
   C   s  | j ddkrd S d}|  }|D ]}| j|d  |d  D ]}||}|sXqD|d |  D ]\}}	d }
| D ]\}}|d |kr||d }
q||
rh|	r|	ddn|	}	|
| j| j| j | j	||	
  | j
|| | j  qhqDq$|D ]}|jr| j|j tj|j|}| |}|s,q| D ]~}||}|sLq4||D ]X\}}	||rV||\}}
|	dd}	|
| j| j| j | j	||	  | j  qVq4qd S )Nzserver rolez"active directory domain controllerz0MACHINE/Microsoft/Windows NT/SecEdit/GptTmpl.infr      asciiignore)r*   r   rB   r   Zset_guidkeysitemsencoder   r-   r   ZcommitZfile_sys_pathnameospathjoinparseZsectionsZupdate_samba)r   Zdeleted_gpo_listZchanged_gpo_listZinf_filerB   ZgpoZsectionZcurrent_sectionr1   r2   setter_tuprK   Zinf_confZattr   r   r   process_group_policy   sX    



zgp_sec_ext.process_group_policyN)r&   r'   r(   r?   countr%   rB   rQ   r   r   r   r   rA      s
   rA   )os.pathrJ   Zsamba.gpclassr   r   Z
samba.authr   Zsamba.compatr   r.   r   Zsamba.samdbr   ImportErrorr   r)   rA   r   r   r   r   <module>   s   E