<s> """<STR_LIT>""" <EOL> import argparse <EOL> import readline <EOL> import os <EOL> import sys <EOL> import re <EOL> import logging <EOL> from sealib . policy import Policy <EOL> import sealib . device <EOL> def device_picker ( devices ) : <EOL> """<STR_LIT>""" <EOL> if not devices : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> if len ( devices ) == <NUM_LIT:1> : <EOL> return devices . keys ( ) [ <NUM_LIT:0> ] <EOL> if len ( devices ) > <NUM_LIT:1> : <EOL> devs = { } <EOL> for i , name in enumerate ( devices ) : <EOL> devs [ int ( i ) ] = name <EOL> while True : <EOL> print "<STR_LIT>" <EOL> for i , name in devs . iteritems ( ) : <EOL> print "<STR_LIT>" . format ( i , name ) <EOL> choice = raw_input ( "<STR_LIT>" ) <EOL> if int ( choice ) in devs : <EOL> return devs [ int ( choice ) ] <EOL> def polinfo ( args ) : <EOL> """<STR_LIT>""" <EOL> if args . verbosity == <NUM_LIT:4> : <EOL> logging . basicConfig ( level = logging . DEBUG ) <EOL> elif args . verbosity == <NUM_LIT:3> : <EOL> logging . basicConfig ( level = logging . INFO ) <EOL> elif args . verbosity == <NUM_LIT:2> : <EOL> logging . basicConfig ( level = logging . WARNING ) <EOL> elif args . verbosity == <NUM_LIT:1> : <EOL> logging . basicConfig ( level = logging . ERROR ) <EOL> elif args . verbosity == <NUM_LIT:0> : <EOL> logging . basicConfig ( level = logging . CRITICAL ) <EOL> try : <EOL> if not args . policy : <EOL> device = get_device ( args . device , args . adb ) <EOL> if not args . device : <EOL> args . device = device . name <EOL> p = Policy ( device ) <EOL> else : <EOL> p = Policy ( None , args . policy ) <EOL> except ( ValueError , RuntimeError ) : <EOL> logging . critical ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> if not args . policy : <EOL> print "<STR_LIT>" . format ( <EOL> device , device . android_version , device . selinux_mode ) <EOL> if args . info_domains : <EOL> print "<STR_LIT>" . format ( p . domains_count ) <EOL> for dom in p . domains . keys ( ) : <EOL> print dom <EOL> else : <EOL> print "<STR_LIT>" . format ( p . classes_count ) <EOL> print "<STR_LIT>" . format ( p . types_count ) <EOL> print "<STR_LIT>" . format ( p . attrs_count ) <EOL> print "<STR_LIT>" . format ( p . domains_count ) <EOL> print "<STR_LIT>" . format ( p . policy . initialsids_count ) <EOL> print "<STR_LIT>" . format ( p . policy . polcap_count ) <EOL> print "<STR_LIT>" . format ( p . policy . role_count ) <EOL> print "<STR_LIT>" . format ( p . policy . user_count ) <EOL> print "<STR_LIT>" . format ( p . policy . fs_use_count ) <EOL> print "<STR_LIT>" . format ( p . policy . genfscon_count ) <EOL> print "<STR_LIT>" . format ( p . policy . portcon_count ) <EOL> print "<STR_LIT>" . format ( p . policy . level_count ) <EOL> print "<STR_LIT>" . format ( p . policy . category_count ) <EOL> print "<STR_LIT>" . format ( p . policy . constraint_count ) <EOL> print "<STR_LIT>" . format ( p . policy . permissives_count ) <EOL> print "<STR_LIT>" . format ( p . policy . allow_count ) <EOL> print "<STR_LIT>" . format ( p . policy . auditallow_count ) <EOL> print "<STR_LIT>" . format ( p . policy . dontaudit_count ) <EOL> print "<STR_LIT>" . format ( p . policy . type_transition_count ) <EOL> print "<STR_LIT>" . format ( p . policy . type_change_count ) <EOL> print "<STR_LIT>" . format ( p . policy . type_member_count ) <EOL> print "<STR_LIT>" . format ( p . policy . role_allow_count ) <EOL> print "<STR_LIT>" . format ( p . policy . role_transition_count ) <EOL> print "<STR_LIT>" . format ( p . policy . range_transition_count ) <EOL> def process_picker ( args , proclist ) : <EOL> """<STR_LIT>""" <EOL> candidate = None <EOL> if args . pid and args . pid in proclist : <EOL> candidate = proclist [ args . pid ] <EOL> elif args . process : <EOL> for proc in proclist . values ( ) : <EOL> if proc . name == args . process : <EOL> candidate = proc <EOL> break <EOL> if candidate is None : <EOL> for proc in proclist . values ( ) : <EOL> if args . process in proc . name : <EOL> candidate = proc <EOL> break <EOL> return candidate <EOL> def get_device ( name , adb ) : <EOL> """<STR_LIT>""" <EOL> if adb : <EOL> devices = sealib . device . Device . get_devices ( adb ) <EOL> else : <EOL> devices = sealib . device . Device . get_devices ( ) <EOL> if not name : <EOL> name = device_picker ( devices ) <EOL> else : <EOL> if name not in devices : <EOL> logging . critical ( "<STR_LIT>" , name ) <EOL> raise ValueError <EOL> try : <EOL> if adb : <EOL> device = sealib . device . Device ( name , adb ) <EOL> else : <EOL> device = sealib . device . Device ( name ) <EOL> except ValueError as e : <EOL> logging . critical ( e ) <EOL> logging . critical ( "<STR_LIT>" ) <EOL> raise RuntimeError <EOL> return device <EOL> def files ( args ) : <EOL> """<STR_LIT>""" <EOL> if args . verbosity == <NUM_LIT:4> : <EOL> logging . basicConfig ( level = logging . DEBUG ) <EOL> elif args . verbosity == <NUM_LIT:3> : <EOL> logging . basicConfig ( level = logging . INFO ) <EOL> elif args . verbosity == <NUM_LIT:2> : <EOL> logging . basicConfig ( level = logging . WARNING ) <EOL> elif args . verbosity == <NUM_LIT:1> : <EOL> logging . basicConfig ( level = logging . ERROR ) <EOL> elif args . verbosity == <NUM_LIT:0> : <EOL> logging . basicConfig ( level = logging . CRITICAL ) <EOL> try : <EOL> device = get_device ( args . device , args . adb ) <EOL> except ( RuntimeError , ValueError ) : <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> if not args . device : <EOL> args . device = device . name <EOL> if not args . pid and not args . process : <EOL> files_dict = device . get_files ( ) <EOL> print_files ( args , None , files_dict , None ) <EOL> else : <EOL> try : <EOL> p = Policy ( device ) <EOL> except ( ValueError , RuntimeError ) : <EOL> logging . critical ( "<STR_LIT>" ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> process = process_picker ( args , device . get_processes ( ) ) <EOL> if process is None : <EOL> if args . pid : <EOL> logging . critical ( "<STR_LIT>" <EOL> "<STR_LIT>" , args . pid ) <EOL> elif args . process : <EOL> logging . critical ( "<STR_LIT>" <EOL> "<STR_LIT>" , args . process ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> logging . info ( "<STR_LIT>" <EOL> "<STR_LIT>" , process . name , process . pid , <EOL> process . context ) <EOL> files_dict = device . get_files ( ) <EOL> file_permissions = get_process_permissions ( p , process , files_dict ) <EOL> print_files ( args , process , files_dict , file_permissions ) <EOL> def get_process_permissions ( policy , process , files_dict ) : <EOL> """<STR_LIT>""" <EOL> file_permissions = { } <EOL> accessible_types = policy . get_types_accessible_by ( process . context ) <EOL> for fname , f in files_dict . iteritems ( ) : <EOL> if f . context . type in accessible_types : <EOL> for rule in accessible_types [ f . context . type ] : <EOL> if f . security_class == rule . tclass : <EOL> if fname in file_permissions : <EOL> file_permissions [ fname ] . update ( rule . perms ) <EOL> else : <EOL> file_permissions [ fname ] = rule . perms <EOL> return file_permissions <EOL> def print_files ( args , process , files_dict , file_permissions ) : <EOL> """<STR_LIT>""" <EOL> extension = "<STR_LIT>" <EOL> device_out = re . sub ( r'<STR_LIT>' , r'<STR_LIT:->' , args . device ) <EOL> if args . out : <EOL> file_out = os . path . abspath ( args . out ) <EOL> if process : <EOL> process_out = re . sub ( r'<STR_LIT>' , r'<STR_LIT:->' , process . name ) <EOL> if args . out : <EOL> if process : <EOL> output = "<STR_LIT>" . format ( <EOL> file_out , device_out , process , process_out , extension ) <EOL> else : <EOL> output = "<STR_LIT>" . format ( file_out , device_out , extension ) <EOL> logging . info ( "<STR_LIT>" , output ) <EOL> with open ( output , "<STR_LIT:w>" ) as thefile : <EOL> if process and file_permissions : <EOL> for fname , f in files_dict . iteritems ( ) : <EOL> if fname in file_permissions : <EOL> out_line = fname <EOL> if args . context : <EOL> out_line = str ( f . context ) + "<STR_LIT:U+0020>" + out_line <EOL> if args . permissions : <EOL> out_line += "<STR_LIT:\t>" + f . security_class + "<STR_LIT>" <EOL> out_line += "<STR_LIT:U+0020>" . join ( <EOL> sorted ( file_permissions [ fname ] ) ) + "<STR_LIT:}>" <EOL> print >> thefile , out_line <EOL> else : <EOL> for fname , f in files_dict . iteritems ( ) : <EOL> out_line = fname <EOL> if args . context : <EOL> out_line = str ( f . context ) + "<STR_LIT:U+0020>" + out_line <EOL> print >> thefile , out_line <EOL> else : <EOL> if process and file_permissions : <EOL> for fname , f in files_dict . iteritems ( ) : <EOL> if fname in file_permissions : <EOL> out_line = fname <EOL> if args . context : <EOL> out_line = str ( f . context ) + "<STR_LIT:U+0020>" + out_line <EOL> if args . permissions : <EOL> out_line += "<STR_LIT:\t>" + f . security_class + "<STR_LIT>" <EOL> out_line += "<STR_LIT:U+0020>" . join ( <EOL> sorted ( file_permissions [ fname ] ) ) + "<STR_LIT:}>" <EOL> print out_line <EOL> else : <EOL> for fname , f in files_dict . iteritems ( ) : <EOL> out_line = fname <EOL> if args . context : <EOL> out_line = str ( f . context ) + "<STR_LIT:U+0020>" + out_line <EOL> print out_line <EOL> print "<STR_LIT>" . format ( len ( files_dict ) ) <EOL> if process and file_permissions : <EOL> print "<STR_LIT>" . format ( <EOL> len ( file_permissions ) ) <EOL> def processes ( args ) : <EOL> """<STR_LIT>""" <EOL> if args . verbosity == <NUM_LIT:4> : <EOL> logging . basicConfig ( level = logging . DEBUG ) <EOL> elif args . verbosity == <NUM_LIT:3> : <EOL> logging . basicConfig ( level = logging . INFO ) <EOL> elif args . verbosity == <NUM_LIT:2> : <EOL> logging . basicConfig ( level = logging . WARNING ) <EOL> elif args . verbosity == <NUM_LIT:1> : <EOL> logging . basicConfig ( level = logging . ERROR ) <EOL> elif args . verbosity == <NUM_LIT:0> : <EOL> logging . basicConfig ( level = logging . CRITICAL ) <EOL> try : <EOL> device = get_device ( args . device , args . adb ) <EOL> except ( ValueError , RuntimeError ) : <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> if not args . device : <EOL> args . device = device . name <EOL> processes_dict = device . get_processes ( ) <EOL> if not args . file and not args . path : <EOL> print_processes ( args , None , processes_dict , None ) <EOL> else : <EOL> try : <EOL> p = Policy ( device ) <EOL> except ( ValueError , RuntimeError ) : <EOL> logging . critical ( "<STR_LIT>" ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> if args . file : <EOL> files_dict = device . get_file ( args . file ) <EOL> if files_dict is None : <EOL> logging . critical ( <EOL> "<STR_LIT>" , args . file ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> else : <EOL> files_dict = device . get_files ( args . path ) <EOL> if files_dict is None : <EOL> logging . critical ( <EOL> "<STR_LIT>" , args . file ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> proc_permissions = get_file_permissions ( p , files_dict , <EOL> processes_dict ) <EOL> print_processes ( args , files_dict , processes_dict , proc_permissions ) <EOL> def get_file_permissions ( policy , files_dict , processes_dict ) : <EOL> """<STR_LIT>""" <EOL> hugemap = { } <EOL> for f in files_dict . values ( ) : <EOL> if f . context . type not in hugemap : <EOL> hugemap [ f . context . type ] = { } <EOL> if f . security_class not in hugemap [ f . context . type ] : <EOL> dmns = policy . get_domains_allowed_to ( f . context , f . security_class ) <EOL> hugemap [ f . context . type ] [ f . security_class ] = dmns <EOL> outmap = { } <EOL> for fname , f in files_dict . iteritems ( ) : <EOL> if fname not in outmap : <EOL> outmap [ fname ] = { } <EOL> for pname , p in processes_dict . iteritems ( ) : <EOL> if p . context . type in hugemap [ f . context . type ] [ f . security_class ] : <EOL> if pname not in outmap [ fname ] : <EOL> outmap [ fname ] [ pname ] = set ( ) <EOL> for rule in hugemap [ f . context . type ] [ f . security_class ] [ p . context . type ] : <EOL> outmap [ fname ] [ pname ] . update ( rule . perms ) <EOL> return outmap <EOL> def print_processes ( args , files_dict , processes_dict , proc_permissions ) : <EOL> """<STR_LIT>""" <EOL> extension = "<STR_LIT>" <EOL> device_out = re . sub ( r'<STR_LIT>' , r'<STR_LIT:->' , args . device ) <EOL> if args . out : <EOL> file_out = os . path . basename ( args . out ) <EOL> if args . file : <EOL> filep_out = re . sub ( r'<STR_LIT>' , r'<STR_LIT:->' , args . file ) <EOL> if args . path : <EOL> path_out = re . sub ( r'<STR_LIT>' , r'<STR_LIT:->' , args . path ) <EOL> if args . out : <EOL> if files_dict and proc_permissions : <EOL> if args . file : <EOL> output = "<STR_LIT>" . format ( <EOL> file_out , device_out , filep_out , extension ) <EOL> else : <EOL> output = "<STR_LIT>" . format ( <EOL> file_out , device_out , path_out , extension ) <EOL> else : <EOL> output = "<STR_LIT>" . format ( <EOL> file_out , device_out , extension ) <EOL> logging . info ( "<STR_LIT>" , output ) <EOL> with open ( output , "<STR_LIT:w>" ) as thefile : <EOL> if files_dict and proc_permissions : <EOL> tmp = "<STR_LIT>" <EOL> print >> thefile , tmp . format ( len ( processes_dict ) ) <EOL> for fname , procperm_dict in proc_permissions . iteritems ( ) : <EOL> f = files_dict [ fname ] <EOL> tmp = "<STR_LIT>" "<STR_LIT>" <EOL> print >> thefile , tmp . format ( f . security_class , fname , <EOL> f . context , len ( procperm_dict ) ) <EOL> for pid , perms in procperm_dict . iteritems ( ) : <EOL> p = processes_dict [ pid ] <EOL> out_line = "<STR_LIT>" . format ( pid , p . name ) <EOL> if args . context : <EOL> out_line = "<STR_LIT:\t>" + str ( p . context ) + out_line <EOL> if args . permissions : <EOL> out_line += "<STR_LIT>" + "<STR_LIT:U+0020>" . join ( sorted ( perms ) ) + "<STR_LIT:}>" <EOL> print >> thefile , out_line <EOL> else : <EOL> tmp = "<STR_LIT>" <EOL> print >> thefile , tmp . format ( len ( processes_dict ) ) <EOL> for pid , p in processes_dict . iteritems ( ) : <EOL> out_line = "<STR_LIT>" . format ( pid , p . name ) <EOL> if args . context : <EOL> out_line = "<STR_LIT:\t>" + str ( p . context ) + out_line <EOL> print >> thefile , out_line <EOL> else : <EOL> if files_dict and proc_permissions : <EOL> tmp = "<STR_LIT>" <EOL> print tmp . format ( len ( processes_dict ) ) <EOL> for fname , procperm_dict in proc_permissions . iteritems ( ) : <EOL> f = files_dict [ fname ] <EOL> tmp = "<STR_LIT>" "<STR_LIT>" <EOL> print tmp . format ( f . security_class , fname , <EOL> f . context , len ( procperm_dict ) ) <EOL> for pid , perms in procperm_dict . iteritems ( ) : <EOL> p = processes_dict [ pid ] <EOL> out_line = "<STR_LIT>" . format ( pid , p . name ) <EOL> if args . context : <EOL> out_line = "<STR_LIT:\t>" + str ( p . context ) + out_line <EOL> if args . permissions : <EOL> out_line += "<STR_LIT>" + "<STR_LIT:U+0020>" . join ( sorted ( perms ) ) + "<STR_LIT:}>" <EOL> print out_line <EOL> else : <EOL> tmp = "<STR_LIT>" <EOL> print tmp . format ( len ( processes_dict ) ) <EOL> for pid , p in processes_dict . iteritems ( ) : <EOL> out_line = "<STR_LIT>" . format ( pid , p . name ) <EOL> if args . context : <EOL> out_line = "<STR_LIT:\t>" + str ( p . context ) + out_line <EOL> print out_line <EOL> def main ( ) : <EOL> """<STR_LIT>""" <EOL> parser = argparse . ArgumentParser ( ) <EOL> parser . add_argument ( '<STR_LIT>' , metavar = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_argument ( "<STR_LIT>" , "<STR_LIT>" , metavar = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> subparsers = parser . add_subparsers ( help = '<STR_LIT>' ) <EOL> parser_polinfo = subparsers . add_parser ( '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser_polinfo . add_argument ( '<STR_LIT>' , metavar = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> parser_polinfo . add_argument ( '<STR_LIT>' , <EOL> help = "<STR_LIT>" , <EOL> action = '<STR_LIT:store_true>' , dest = "<STR_LIT>" ) <EOL> parser_polinfo . add_argument ( "<STR_LIT>" , "<STR_LIT>" , metavar = "<STR_LIT>" , <EOL> choices = [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] , type = int , default = <NUM_LIT:0> , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> parser_polinfo . set_defaults ( func = polinfo ) <EOL> parser_files = subparsers . add_parser ( '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser_files . add_argument ( '<STR_LIT>' , "<STR_LIT>" , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> parser_files . add_argument ( '<STR_LIT>' , <EOL> help = "<STR_LIT>" , <EOL> metavar = "<STR_LIT>" ) <EOL> parser_files . add_argument ( '<STR_LIT>' , <EOL> help = "<STR_LIT>" , <EOL> metavar = "<STR_LIT>" ) <EOL> parser_files . add_argument ( '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' ) <EOL> parser_files . add_argument ( "<STR_LIT>" , "<STR_LIT>" , metavar = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> parser_files . add_argument ( "<STR_LIT>" , "<STR_LIT>" , metavar = "<STR_LIT>" , <EOL> choices = [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] , type = int , default = <NUM_LIT:0> , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> parser_files . set_defaults ( func = files ) <EOL> parser_processes = subparsers . add_parser ( '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser_processes . add_argument ( '<STR_LIT>' , "<STR_LIT>" , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> parser_processes . add_argument ( '<STR_LIT>' , <EOL> help = "<STR_LIT>" , <EOL> metavar = "<STR_LIT>" ) <EOL> parser_processes . add_argument ( '<STR_LIT>' , <EOL> help = "<STR_LIT>" , <EOL> metavar = "<STR_LIT>" ) <EOL> parser_processes . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> parser_processes . add_argument ( "<STR_LIT>" , "<STR_LIT>" , metavar = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> parser_processes . add_argument ( "<STR_LIT>" , "<STR_LIT>" , metavar = "<STR_LIT>" , <EOL> choices = [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] , type = int , default = <NUM_LIT:0> , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> parser_processes . set_defaults ( func = processes ) <EOL> args = parser . parse_args ( ) <EOL> args . func ( args ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) </s>
