^c@sdZddlmZddlmZddlmZdZdejfdYZd Z d dd YZ d Z d Z dddYZ dS(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(tutili(t audit2whycCsUt|dkrM|ddkrMyt|dWntk rHtSXtStSdS(sDetermine 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 iit$N(tlentintt ValueErrortFalsetTrue(tid((s5/usr/lib64/python2.7/site-packages/sepolgen/access.pyt is_idparam's" t AccessVectorcBsYeZdZd dZdZdZdZdZdZ dZ dZ 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] cCs|r|j|nZd|_d|_d|_tj|_g|_t j |_ g|_ d|_ d|_d|_d|_dS(N(t from_listtNonetsrc_typettgt_typet obj_classRtIdSettpermst audit_msgsRtTERULEttypetdatatobj_patht base_typet__hash__t info_flow_dir(tselft init_list((s5/usr/lib64/python2.7/site-packages/sepolgen/access.pyt__init__Rs         cCslt|dkr+tdt|n|d|_|d|_|d|_tj|d|_dS(sxInitialize 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. is+List must contain at least four elements %siiiiN( RRtstrRRRRRR(Rtlist((s5/usr/lib64/python2.7/site-packages/sepolgen/access.pyR gs    cCs2|j|j|jg}|jt|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. (RRRtextendtsortedR(Rtl((s5/usr/lib64/python2.7/site-packages/sepolgen/access.pytto_list|scCs |jS(N(t to_string(R((s5/usr/lib64/python2.7/site-packages/sepolgen/access.pyt__str__scCs&d|j|j|j|jjfS(Nsallow %s %s:%s %s;(RRRRt to_space_str(R((s5/usr/lib64/python2.7/site-packages/sepolgen/access.pyR$scCs"g}|j|j|jg}|S(N(RRR(Rtbase_type_array((s5/usr/lib64/python2.7/site-packages/sepolgen/access.pytbase_file_typescCs|j|jkr%t|j|jS|j|jkrJt|j|jS|j|jkrot|j|jSt|jt|jkrtt|jt|jSt|j}|jt|j}|jx6t||D]%\}}||krt||SqWdS(Ni( RtcmpRRRRRtsorttzip(Rtothertxtytpatpb((s5/usr/lib64/python2.7/site-packages/sepolgen/access.pyt__cmp__s    cCsyyt|j}|j|j|j|f}t|j}|j|j|j|j|j|f}|||SWnttfk rtSXdS(N( RRRRRR*tAttributeErrort TypeErrortNotImplemented(RR,tmethodR-taR.tb((s5/usr/lib64/python2.7/site-packages/sepolgen/access.pyt_compares  N( t__name__t __module__t__doc__R RR R#R%R$R(R1R8(((s5/usr/lib64/python2.7/site-packages/sepolgen/access.pyR 8s      cCst|tr|gSg}x||jD]q}xh|jD]]}xT|jD]I}t}||_||_||_|jj |_|j |qFWq6Wq&W|S(sQConvert 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_classesRRRRtcopytappend(tavruleR6RRRtaccess((s5/usr/lib64/python2.7/site-packages/sepolgen/access.pytavrule_to_access_vectorss     tAccessVectorSetcBsbeZdZdZdZdZdZdZddde j gdZ ddZ 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; cCsi|_d|_dS(s)Initialize an access vector set. N(tsrcR tinfo_dir(R((s5/usr/lib64/python2.7/site-packages/sepolgen/access.pyRs ccsQxJ|jjD]9}x0|jD]"}x|jD] }|Vq6Wq#WqWdS(s9Iterate over all of the unique access vectors in the set.N(RFtvalues(Rttgtstobjstav((s5/usr/lib64/python2.7/site-packages/sepolgen/access.pyt__iter__scCsKd}x>|jjD]-}x$|jD]}|t|7}q)WqW|S(s5Return 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(RFRHR(RR"RIRJ((s5/usr/lib64/python2.7/site-packages/sepolgen/access.pyt__len__s cCs.g}x!|D]}|j|jq W|S(sbReturn 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. (RAR#(RR"RK((s5/usr/lib64/python2.7/site-packages/sepolgen/access.pyR#s cCs(x!|D]}|jt|qWdS(sAdd 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(tadd_avR (RR"RK((s5/usr/lib64/python2.7/site-packages/sepolgen/access.pyR s c Cs|jj|i} | j|i} ||f| krL| ||f} nXt} || _|| _|| _|| _| | _|| _|| _ | | ||f<| j j ||r| j j |ndS(s)Add an access vector to the set. N(RFt setdefaultR RRRRRRRRtupdateRRA( RRRRRRRt audit_msgtavc_typeRttgttclsRC((s5/usr/lib64/python2.7/site-packages/sepolgen/access.pytadds         cCs&|j|j|j|j|jdS(s Add an access vector to the set.N(RURRRR(RRKRQ((s5/usr/lib64/python2.7/site-packages/sepolgen/access.pyRN5sN( R9R:R;RRLRMR#R R RRRURN(((s5/usr/lib64/python2.7/site-packages/sepolgen/access.pyREs    cCsAtj}x.|D]&}|j|j|j|jqW|S(N(RRRURR(tavsttypesRK((s5/usr/lib64/python2.7/site-packages/sepolgen/access.pytavs_extract_types:s   cCsci}xV|D]N}|j|kr2||j}ntj}|||j<|j|jq W|S(N(RRRRPR(RVRRKts((s5/usr/lib64/python2.7/site-packages/sepolgen/access.pytavs_extract_obj_permsBs   t RoleTypeSetcBs2eZdZdZdZdZdZRS(sA 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. cCs i|_dS(s Initialize an access vector set.N(t role_types(R((s5/usr/lib64/python2.7/site-packages/sepolgen/access.pyRSsccs#x|jjD] }|VqWdS(sAIterate over all of the unique role allows statements in the set.N(R\RH(Rt role_type((s5/usr/lib64/python2.7/site-packages/sepolgen/access.pyRLWscCst|jjS(s2Return the unique number of role allow statements.(RR\tkeys(R((s5/usr/lib64/python2.7/site-packages/sepolgen/access.pyRM\scCsU||jkr|j|}n"tj}||_||j|<|jj|dS(N(R\RtRoleTypetroleRWRU(RR`RR]((s5/usr/lib64/python2.7/site-packages/sepolgen/access.pyRU`s    (R9R:R;RRLRMRU(((s5/usr/lib64/python2.7/site-packages/sepolgen/access.pyR[Ms    N(((R;tRRtselinuxRR t ComparisonR RDRERXRZR[(((s5/usr/lib64/python2.7/site-packages/sepolgen/access.pyt s | m