3 # py-rsbac - RSBAC Python bindings
4 # Copyright (C) 2006 Frederic Jolliton <pyrsbac@tuxee.net>
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 __all__ = [ 'Module' , 'all' ] # plus instances exported by _createModuleObjects
22 from ctypes import c_int, byref
27 from rsbac.errors import Error, raiseIfError
29 def switch( module , value ) :
30 return raiseIfError( lib.rsbac_switch( module , value ) )
32 def isAvailable( module ) :
33 return lib.rsbac_get_switch( module , None , None ) >= 0
35 def _checkGetSwitch() :
36 # rsbac_get_switch is only available with a patched RSBAC
37 if lib.rsbac_get_switch is None :
38 raise RuntimeError , 'rsbac_get_switch function not available. You need a patch for RSBAC.'
40 def getSwitch( module ) :
44 raiseIfError( lib.rsbac_get_switch( module , byref( state ) , byref( control ) ) )
45 return state.value , control.value
47 class Module( object ) :
48 def __new__( cls , id , *args , **kwargs ) :
49 instance = _g_modules.get( id )
51 instance = object.__new__( cls )
52 instance.__init_singleton__( id , *args , **kwargs )
53 _g_modules[ id ] = instance
55 def __init_singleton__( self , moduleId , name = None ) :
56 super( Module , self ).__init__()
59 self._available = None
61 return int( self._id )
62 def __long__( self ) :
63 return long( self._id )
64 def __repr__( self ) :
65 if lib.rsbac_get_switch is None :
66 return '<Module %s [Unknown state]>' % ( self._name , )
70 if self.getEnabled() :
71 info.append( 'Enabled' )
73 info.append( 'Disabled' )
74 if self.getSoftmode() :
75 info.append( 'Softmode' )
76 info = ' [' + ','.join( info ) + ']'
78 info = ' [NOT AVAILABLE]'
79 return '<Module %s%s>' % ( self._name , info )
85 id = property( getId )
91 name = property( getName )
95 def getAvailable( self ) :
96 return isAvailable( self._id )
97 available = property( getAvailable )
101 def getEnabled( self ) :
102 state = getSwitch( self._id )[ 0 ]
103 return bool( state & 1 )
104 def setEnabled( self , value ) :
106 switch( self._id , 1 )
108 switch( self._id , 0 )
109 enabled = property( getEnabled , setEnabled )
111 # switchable properties
113 def getSwitchableOn( self ) :
114 return bool( getSwitch( self._id )[ 1 ]&1 )
115 switchableOn = property( getSwitchableOn )
116 def getSwitchableOff( self ) :
117 return bool( getSwitch( self._id )[ 1 ]&2 )
118 switchableOff = property( getSwitchableOff )
119 def getSoftmodeSwitchableOn( self ) :
120 return bool( getSwitch( self._id )[ 1 ]&4 )
121 softmodeSwitchableOn = property( getSoftmodeSwitchableOn )
122 def getSoftmodeSwitchableOff( self ) :
123 return bool( getSwitch( self._id )[ 1 ]&8 )
124 softmodeSwitchableOff = property( getSoftmodeSwitchableOff )
128 def getSoftmode( self ) :
129 state = getSwitch( self._id )[ 0 ]
130 return bool( state & 2 )
131 def setSoftmode( self , value ) :
133 switch( self._id , 3 )
135 switch( self._id , 2 )
136 softmode = property( getSoftmode , setSoftmode )
138 def _createModuleObjects() :
141 m = sys.modules[ __name__ ]
143 for k , v in headers.__dict__.items() :
144 if k.startswith( prefix ) :
145 name = k[ len( prefix ) : ]
146 if name not in ( 'NONE' , ) :
147 module = Module( v , name )
148 setattr( m , name , module )
150 __all__.append( name )
151 setattr( m , 'all' , all )
153 _createModuleObjects()
155 del _createModuleObjects
157 DAZ.flushCache = lambda : raiseIfError( lib.rsbac_daz_flush_cache() )
160 # indent-tabs-mode: nil