
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
3
^"                 @   st   d dl mZ ddlmZ ddlmZ ddlmZmZm	Z	 ddl
mZ ddlmZ G d	d
 d
eZG dd de	ZdS )    )
namedtuple   )ConstraintRuletype   )DiffResultDescriptor)
DifferenceSymbolWrapperWrapper)class_wrapper_factory)type_wrapper_factoryc               @   s   e Zd ZdZedZedZedZedZedZ	edZ
edZedZdZdZdZdZdZdZdZdZdd Zd	d
 Zdd Zdd Zdd Zdd ZdS )ConstraintsDifferencea  
    Determine the difference in constraints between two policies.

    Since the compiler does not union constraints, there may be multiple
    constraints with the same ruletype, object class, and permission
    set, so constraints can only be added or removed, not modified.

    The constraint expressions are compared only on a basic level.
    Expressions that are logically equivalent but are structurally
    different, for example, by associativity, will be considered
    different.  Type and role attributes are also not expanded,
    so if there are changes to attribute members, it will not
    be reflected as a difference.
    diff_constrainsdiff_mlsconstrainsdiff_validatetransdiff_mlsvalidatetransNc             C   s`   | j jdj|  | jdks&| jdkr.| j  | jdd | jD dd | jD \| _| _}dS )zAGenerate the difference in constraint rules between the policies.zJGenerating constraint differences from {0.left_policy} to {0.right_policy}Nc             s   s   | ]}t |V  qd S )N)ConstraintWrapper).0c r   #/usr/lib64/python3.6/constraints.py	<genexpr>Q   s    z8ConstraintsDifference.diff_constrains.<locals>.<genexpr>c             s   s   | ]}t |V  qd S )N)r   )r   r   r   r   r   r   R   s    )	loginfoformat_left_constrains_right_constrains_create_constrain_lists	_set_diffadded_constrainsremoved_constrains)self_r   r   r   r   G   s    
z%ConstraintsDifference.diff_constrainsc             C   s`   | j jdj|  | jdks&| jdkr.| j  | jdd | jD dd | jD \| _| _}dS )zEGenerate the difference in MLS constraint rules between the policies.zNGenerating MLS constraint differences from {0.left_policy} to {0.right_policy}Nc             s   s   | ]}t |V  qd S )N)r   )r   r   r   r   r   r   _   s    z;ConstraintsDifference.diff_mlsconstrains.<locals>.<genexpr>c             s   s   | ]}t |V  qd S )N)r   )r   r   r   r   r   r   `   s    )	r   r   r   _left_mlsconstrains_right_mlsconstrainsr   r   added_mlsconstrainsremoved_mlsconstrains)r    r!   r   r   r   r   T   s    z(ConstraintsDifference.diff_mlsconstrainsc             C   s`   | j jdj|  | jdks&| jdkr.| j  | jdd | jD dd | jD \| _| _}dS )zDGenerate the difference in validatetrans rules between the policies.zMGenerating validatetrans differences from {0.left_policy} to {0.right_policy}Nc             s   s   | ]}t |V  qd S )N)r   )r   r   r   r   r   r   m   s    z;ConstraintsDifference.diff_validatetrans.<locals>.<genexpr>c             s   s   | ]}t |V  qd S )N)r   )r   r   r   r   r   r   n   s    )	r   r   r   _left_validatetrans_right_validatetransr   r   added_validatetransremoved_validatetrans)r    r!   r   r   r   r   b   s    z(ConstraintsDifference.diff_validatetransc             C   s`   | j jdj|  | jdks&| jdkr.| j  | jdd | jD dd | jD \| _| _}dS )zHGenerate the difference in MLS validatetrans rules between the policies.zPGenerating mlsvalidatetrans differences from {0.left_policy} to {0.right_policy}Nc             s   s   | ]}t |V  qd S )N)r   )r   r   r   r   r   r   {   s    z>ConstraintsDifference.diff_mlsvalidatetrans.<locals>.<genexpr>c             s   s   | ]}t |V  qd S )N)r   )r   r   r   r   r   r   |   s    )	r   r   r   _left_mlsvalidatetrans_right_mlsvalidatetransr   r   added_mlsvalidatetransremoved_mlsvalidatetrans)r    r!   r   r   r   r   p   s    z+ConstraintsDifference.diff_mlsvalidatetransc             C   sZ  g | _ g | _g | _g | _x| jj D ]}|jtjkrB| j j	| q$|jtj
kr\| jj	| q$|jtjkrv| jj	| q$|jtjkr| jj	| q$| jjdj|j q$W g | _g | _g | _g | _x| jj D ]}|jtjkr| jj	| q|jtj
kr| jj	| q|jtjkr"| jj	| q|jtjkr>| jj	| q| jjdj|j qW dS )z$Create rule lists for both policies.z/Unknown rule type: {0} (This is an SETools bug)N)r   r"   r&   r*   Zleft_policyZconstraintsruletyper   Z	constrainappendZmlsconstrainZvalidatetransZmlsvalidatetransr   errorr   r   r#   r'   r+   Zright_policy)r    ruler   r   r   r      s<    

z-ConstraintsDifference._create_constrain_listsc             C   sp   | j jd d| _d| _d| _d| _d| _d| _d| _d| _	d| _
d| _d| _d| _d| _d| _d| _d| _dS )z%Reset diff results on policy changes.z%Resetting all constraints differencesN)r   debugr   r   r$   r%   r(   r)   r,   r-   r   r"   r&   r*   r   r#   r'   r+   )r    r   r   r   _reset_diff   s"    z!ConstraintsDifference._reset_diff)__name__
__module____qualname____doc__r   r   r   r$   r%   r(   r)   r,   r-   r   r   r"   r#   r&   r'   r*   r+   r   r   r   r   r   r3   r   r   r   r   r      s.   $r   c               @   s4   e Zd ZdZdZdd Zdd	 Zd
d Zdd ZdS )r   z#Wrap constraints for diff purposes.r.   tclasspermsexprc             C   s   || _ |j| _t|j| _y|j| _W n tk
r@   d | _Y nX t|| _g | _x@|j	D ]6}t
|tr| jjtdd |D  qZ| jj| qZW d S )Nc             s   s   | ]}t |V  qd S )N)r   )r   itemr   r   r   r      s    z-ConstraintWrapper.__init__.<locals>.<genexpr>)originr.   r
   r8   r9   AttributeErrorhashkeyr:   Z
expression
isinstance	frozensetr/   )r    r1   opr   r   r   __init__   s    

zConstraintWrapper.__init__c             C   s   | j S )N)r?   )r    r   r   r   __hash__   s    zConstraintWrapper.__hash__c             C   s   | j |j k S )N)r?   )r    otherr   r   r   __lt__   s    zConstraintWrapper.__lt__c             C   s0   | j |j ko.| j|jko.| j|jko.| j|jkS )N)r.   r8   r9   r:   )r    rE   r   r   r   __eq__   s    zConstraintWrapper.__eq__N)r.   r8   r9   r:   )	r4   r5   r6   r7   	__slots__rC   rD   rF   rG   r   r   r   r   r      s   r   N)collectionsr   Z	policyrepr   Zdescriptorsr   
differencer   r   r	   Zobjclassr
   typesr   r   r   r   r   r   r   <module>   s     