ó
â„^c           @   s   d  Z  d d l m Z d d l m Z d d l m Z d „  Z d e j f d „  ƒ  YZ d	 „  Z	 d
 d d „  ƒ  YZ
 d „  Z d „  Z d d d „  ƒ  YZ d S(   sV  
Classes representing basic access.

SELinux - at the most basic level - represents access as
the 4-tuple subject (type or context), target (type or context),
object class, permission. The policy language elaborates this basic
access to faciliate more concise rules (e.g., allow rules can have multiple
source or target types - see refpolicy for more information).

This module has objects for representing the most basic access (AccessVector)
and sets of that access (AccessVectorSet). These objects are used in Madison
in a variety of ways, but they are the fundamental representation of access.
i   (   t	   refpolicy(   t   utiliÿÿÿÿ(   t	   audit2whyc         C   sU   t  |  ƒ d k rM |  d d k rM y t |  d ƒ Wn t k
 rH t SXt St Sd S(   s¬   Determine if an id is a paramater in the form $N, where N is
    an integer.

    Returns:
      True if the id is a paramater
      False if the id is not a paramater
    i   i    t   $N(   t   lent   intt
   ValueErrort   Falset   True(   t   id(    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/access.pyt
   is_idparam'   s    "t   AccessVectorc           B   sY   e  Z d  Z d	 d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z RS(
   sº  
    An access vector is the basic unit of access in SELinux.

    Access vectors are the most basic representation of access within
    SELinux. It represents the access a source type has to a target
    type in terms of an object class and a set of permissions.

    Access vectors are distinct from AVRules in that they can only
    store a single source type, target type, and object class. The
    simplicity of AccessVectors makes them useful for storing access
    in a form that is easy to search and compare.

    The source, target, and object are stored as string. No checking
    done to verify that the strings are valid SELinux identifiers.
    Identifiers in the form $N (where N is an integer) are reserved as
    interface parameters and are treated as wild cards in many
    circumstances.

    Properties:
     .src_type - The source type allowed access. [String or None]
     .tgt_type - The target type to which access is allowed. [String or None]
     .obj_class - The object class to which access is allowed. [String or None]
     .perms - The permissions allowed to the object class. [IdSet]
     .audit_msgs - The audit messages that generated this access vector [List of strings]
    c         C   s†   | r |  j  | ƒ nZ d  |  _ d  |  _ d  |  _ t j ƒ  |  _ g  |  _ t	 j
 |  _ g  |  _ d  |  _ d  |  _ d  |  _ d  |  _ d  S(   N(   t	   from_listt   Nonet   src_typet   tgt_typet	   obj_classR    t   IdSett   permst
   audit_msgsR   t   TERULEt   typet   datat   obj_patht	   base_typet   __hash__t   info_flow_dir(   t   selft	   init_list(    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/access.pyt   __init__R   s    								c         C   sl   t  | ƒ d k  r+ t d t | ƒ ƒ ‚ n  | d |  _ | d |  _ | d |  _ t j | d ƒ |  _ d S(   sx  Initialize an access vector from a list.

        Initialize an access vector from a list treating the list as
        positional arguments - i.e., 0 = src_type, 1 = tgt_type, etc.
        All of the list elements 3 and greater are treated as perms.
        For example, the list ['foo_t', 'bar_t', 'file', 'read', 'write']
        would create an access vector list with the source type 'foo_t',
        target type 'bar_t', object class 'file', and permissions 'read'
        and 'write'.

        This format is useful for very simple storage to strings or disc
        (see to_list) and for initializing access vectors.
        i   s+   List must contain at least four elements %si    i   i   i   N(	   R   R   t   strR   R   R   R    R   R   (   R   t   list(    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/access.pyR   g   s    c         C   s2   |  j  |  j |  j g } | j t |  j ƒ ƒ | S(   sî   
        Convert an access vector to a list.

        Convert an access vector to a list treating the list as positional
        values. See from_list for more information on how an access vector
        is represented in a list.
        (   R   R   R   t   extendt   sortedR   (   R   t   l(    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/access.pyt   to_list|   s    c         C   s
   |  j  ƒ  S(   N(   t	   to_string(   R   (    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/access.pyt   __str__ˆ   s    c         C   s&   d |  j  |  j |  j |  j j ƒ  f S(   Ns   allow %s %s:%s %s;(   R   R   R   R   t   to_space_str(   R   (    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/access.pyR$   ‹   s    c         C   s"   g  } |  j  |  j |  j g } | S(   N(   R   R   R   (   R   t   base_type_array(    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/access.pyt   base_file_type   s    c         C   s  |  j  | j  k r% t |  j  | j  ƒ S|  j | j k rJ t |  j | j ƒ S|  j |  j k ro t |  j | j ƒ St |  j ƒ t | j ƒ k r¬ t t |  j ƒ t | j ƒ ƒ St |  j ƒ } | j ƒ  t | j ƒ } | j ƒ  x6 t | | ƒ D]% \ } } | | k rî t | | ƒ Sqî Wd S(   Ni    (	   R   t   cmpR   R   R   R   R   t   sortt   zip(   R   t   othert   xt   yt   pat   pb(    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/access.pyt   __cmp__”   s     

c         C   s˜   yy t  |  j ƒ } |  j |  j |  j | f } t  | j ƒ } | j ƒ  | j ƒ  | j | j | j | f } | | | ƒ SWn t t f k
 r“ t SXd  S(   N(	   R   R   R   R   R   R*   t   AttributeErrort	   TypeErrort   NotImplemented(   R   R,   t   methodR-   t   aR.   t   b(    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/access.pyt   _compare¦   s    

N(   t   __name__t
   __module__t   __doc__R   R   R   R#   R%   R$   R(   R1   R8   (    (    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/access.pyR   8   s   						c         C   sŸ   t  |  t ƒ r |  g Sg  } x| |  j D]q } xh |  j D]] } xT |  j D]I } t ƒ  } | | _ | | _ | | _ |  j j	 ƒ  | _ | j
 | ƒ qF Wq6 Wq& W| S(   sQ  Convert an avrule into a list of access vectors.

    AccessVectors and AVRules are similary, but differ in that
    an AVRule can more than one source type, target type, and
    object class. This function expands a single avrule into a
    list of one or more AccessVectors representing the access
    defined in the AVRule.

    
    (   t
   isinstanceR   t	   src_typest	   tgt_typest   obj_classesR   R   R   R   t   copyt   append(   t   avruleR6   R   R   R   t   access(    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/access.pyt   avrule_to_access_vectors´   s    				t   AccessVectorSetc           B   sb   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d d d e	 j
 g  d „ Z d d „ Z RS(	   s/  A non-overlapping set of access vectors.

    An AccessVectorSet is designed to store one or more access vectors
    that are non-overlapping. Access can be added to the set
    incrementally and access vectors will be added or merged as
    necessary.  For example, adding the following access vectors using
    add_av:
       allow $1 etc_t : read;
       allow $1 etc_t : write;
       allow $1 var_log_t : read;
    Would result in an access vector set with the access vectors:
       allow $1 etc_t : { read write};
       allow $1 var_log_t : read;
    c         C   s   i  |  _  d |  _ d S(   s)   Initialize an access vector set.
        N(   t   srcR   t   info_dir(   R   (    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/access.pyR   Ü   s    	c         c   sQ   xJ |  j  j ƒ  D]9 } x0 | j ƒ  D]" } x | j ƒ  D] } | Vq6 Wq# Wq Wd S(   s9   Iterate over all of the unique access vectors in the set.N(   RF   t   values(   R   t   tgtst   objst   av(    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/access.pyt   __iter__å   s    c         C   sK   d } x> |  j  j ƒ  D]- } x$ | j ƒ  D] } | t | ƒ 7} q) Wq W| S(   s5  Return the number of unique access vectors in the set.

        Because of the inernal representation of the access vector set,
        __len__ is not a constant time operation. Worst case is O(N)
        where N is the number of unique access vectors, but the common
        case is probably better.
        i    (   RF   RH   R   (   R   R"   RI   RJ   (    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/access.pyt   __len__ì   s
    c         C   s.   g  } x! |  D] } | j  | j ƒ  ƒ q W| S(   sb  Return the unique access vectors in the set as a list.

        The format of the returned list is a set of nested lists,
        each access vector represented by a list. This format is
        designed to be simply  serializable to a file.

        For example, consider an access vector set with the following
        access vectors:
          allow $1 user_t : file read;
          allow $1 etc_t : file { read write};
        to_list would return the following:
          [[$1, user_t, file, read]
           [$1, etc_t, file, read, write]]

        See AccessVector.to_list for more information.
        (   RA   R#   (   R   R"   RK   (    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/access.pyR#   ú   s    c         C   s(   x! | D] } |  j  t | ƒ ƒ q Wd S(   s  Add access vectors stored in a list.

        See to list for more information on the list format that this
        method accepts.

        This will add all of the access from the list. Any existing
        access vectors in the set will be retained.
        N(   t   add_avR   (   R   R"   RK   (    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/access.pyR     s    	c
         C   sÑ   |  j  j | i  ƒ }
 |
 j | i  ƒ } | | f | k rL | | | f } nX t ƒ  } | | _ | | _ | | _ | | _ |	 | _ | | _ | | _	 | | | | f <| j
 j | ƒ | rÍ | j j | ƒ n  d S(   s)   Add an access vector to the set.
        N(   RF   t
   setdefaultR   R   R   R   R   R   R   R   R   t   updateR   RA   (   R   R   R   R   R   R   R   t	   audit_msgt   avc_typeR   t   tgtt   clsRC   (    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/access.pyt   add  s     								c         C   s&   |  j  | j | j | j | j ƒ d S(   s    Add an access vector to the set.N(   RU   R   R   R   R   (   R   RK   RQ   (    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/access.pyRN   5  s    N(   R9   R:   R;   R   RL   RM   R#   R   R   R   R   RU   RN   (    (    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/access.pyRE   Í   s   						c         C   sA   t  j ƒ  } x. |  D]& } | j | j ƒ | j | j ƒ q W| S(   N(   R    R   RU   R   R   (   t   avst   typesRK   (    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/access.pyt   avs_extract_types:  s
    c         C   sc   i  } xV |  D]N } | j  | k r2 | | j  } n t j ƒ  } | | | j  <| j | j ƒ q W| S(   N(   R   R    R   RP   R   (   RV   R   RK   t   s(    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/access.pyt   avs_extract_obj_permsB  s    t   RoleTypeSetc           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   s²   A non-overlapping set of role type statements.

    This clas allows the incremental addition of role type statements and
    maintains a non-overlapping list of statements.
    c         C   s   i  |  _  d S(   s    Initialize an access vector set.N(   t
   role_types(   R   (    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/access.pyR   S  s    c         c   s#   x |  j  j ƒ  D] } | Vq Wd S(   sA   Iterate over all of the unique role allows statements in the set.N(   R\   RH   (   R   t	   role_type(    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/access.pyRL   W  s    c         C   s   t  |  j j ƒ  ƒ S(   s2   Return the unique number of role allow statements.(   R   R\   t   keys(   R   (    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/access.pyRM   \  s    c         C   sU   | |  j  k r |  j  | } n" t j ƒ  } | | _ | |  j  | <| j j | ƒ d  S(   N(   R\   R    t   RoleTypet   roleRW   RU   (   R   R`   R   R]   (    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/access.pyRU   `  s    	(   R9   R:   R;   R   RL   RM   RU   (    (    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/access.pyR[   M  s
   			N(    (    (   R;   t    R    R   t   selinuxR   R
   t
   ComparisonR   RD   RE   RX   RZ   R[   (    (    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/access.pyt   <module>    s   	|	m		