<s> """<STR_LIT>""" <EOL> import importlib <EOL> import os <EOL> import pdb <EOL> import time <EOL> from django import http <EOL> from django import shortcuts <EOL> from django import template <EOL> from django . views . decorators import csrf <EOL> import psutil <EOL> import logging <EOL> from grr . gui import http_api <EOL> from grr . gui import renderers <EOL> from grr . gui import urls <EOL> from grr . gui import webauth <EOL> from grr . lib import access_control <EOL> from grr . lib import aff4 <EOL> from grr . lib import config_lib <EOL> from grr . lib import flags <EOL> from grr . lib import rdfvalue <EOL> from grr . lib import registry <EOL> from grr . lib import stats <EOL> class ViewsInit ( registry . InitHook ) : <EOL> def RunOnce ( self ) : <EOL> """<STR_LIT>""" <EOL> stats . STATS . RegisterEventMetric ( <EOL> "<STR_LIT>" , fields = [ ( "<STR_LIT>" , str ) ] ) <EOL> stats . STATS . RegisterEventMetric ( <EOL> "<STR_LIT>" , fields = [ ( "<STR_LIT>" , str ) ] , <EOL> units = stats . MetricUnits . BYTES ) <EOL> stats . STATS . RegisterCounterMetric ( <EOL> "<STR_LIT>" , fields = [ ( "<STR_LIT>" , str ) ] ) <EOL> stats . STATS . RegisterCounterMetric ( "<STR_LIT>" ) <EOL> stats . STATS . RegisterCounterMetric ( "<STR_LIT>" ) <EOL> stats . STATS . RegisterCounterMetric ( "<STR_LIT>" ) <EOL> @ webauth . SecurityCheck <EOL> @ csrf . ensure_csrf_cookie <EOL> def Homepage ( request ) : <EOL> """<STR_LIT>""" <EOL> renderers_js_files = set ( [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ] ) <EOL> create_time = psutil . Process ( os . getpid ( ) ) . create_time ( ) <EOL> context = { "<STR_LIT>" : config_lib . CONFIG [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : config_lib . CONFIG [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : config_lib . CONFIG [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : config_lib . CONFIG [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : config_lib . CONFIG [ <EOL> "<STR_LIT>" ] , <EOL> "<STR_LIT>" : renderers_js_files , <EOL> "<STR_LIT>" : create_time } <EOL> return shortcuts . render_to_response ( <EOL> "<STR_LIT>" , context , context_instance = template . RequestContext ( request ) ) <EOL> @ webauth . SecurityCheck <EOL> def RenderBinaryDownload ( request ) : <EOL> """<STR_LIT>""" <EOL> path , filename = request . path . split ( "<STR_LIT:/>" , <NUM_LIT:2> ) [ - <NUM_LIT:1> ] . rsplit ( "<STR_LIT:/>" , <NUM_LIT:1> ) <EOL> if not path or not filename : <EOL> return AccessDenied ( "<STR_LIT>" ) <EOL> request . REQ = request . REQUEST <EOL> def Generator ( ) : <EOL> with aff4 . FACTORY . Open ( aff4_path , aff4_type = "<STR_LIT>" , <EOL> token = BuildToken ( request , <NUM_LIT> ) ) as fd : <EOL> while True : <EOL> data = fd . Read ( <NUM_LIT> ) <EOL> if not data : break <EOL> yield data <EOL> base_path = rdfvalue . RDFURN ( "<STR_LIT>" ) <EOL> aff4_path = base_path . Add ( path ) . Add ( filename ) <EOL> if not aff4_path . RelativeName ( base_path ) : <EOL> return AccessDenied ( "<STR_LIT>" ) <EOL> filename = aff4_path . Basename ( ) <EOL> response = http . StreamingHttpResponse ( <EOL> streaming_content = Generator ( ) , <EOL> content_type = "<STR_LIT>" ) <EOL> response [ "<STR_LIT>" ] = ( "<STR_LIT>" % filename ) <EOL> return response <EOL> @ webauth . SecurityCheck <EOL> @ renderers . ErrorHandler ( ) <EOL> def RenderApi ( request ) : <EOL> """<STR_LIT>""" <EOL> return http_api . RenderHttpResponse ( request ) <EOL> @ webauth . SecurityCheck <EOL> @ renderers . ErrorHandler ( ) <EOL> def RenderGenericRenderer ( request ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> action , renderer_name = request . path . split ( "<STR_LIT:/>" ) [ - <NUM_LIT:2> : ] <EOL> renderer_cls = renderers . Renderer . GetPlugin ( name = renderer_name ) <EOL> except KeyError : <EOL> stats . STATS . IncrementCounter ( "<STR_LIT>" ) <EOL> return AccessDenied ( "<STR_LIT>" % renderer_name ) <EOL> [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] . index ( action ) <EOL> renderer = renderer_cls ( ) <EOL> result = http . HttpResponse ( content_type = "<STR_LIT>" ) <EOL> if flags . FLAGS . debug : <EOL> request . REQ = request . POST . dict ( ) <EOL> request . REQ . update ( request . GET . dict ( ) ) <EOL> else : <EOL> request . REQ = request . POST . dict ( ) <EOL> request . token = BuildToken ( request , renderer . max_execution_time ) <EOL> request . canary_mode = "<STR_LIT>" in request . COOKIES <EOL> renderer . CheckAccess ( request ) <EOL> try : <EOL> method = getattr ( renderer , action ) <EOL> start_time = time . time ( ) <EOL> try : <EOL> result = method ( request , result ) or result <EOL> finally : <EOL> total_time = time . time ( ) - start_time <EOL> stats . STATS . RecordEvent ( "<STR_LIT>" , <EOL> total_time , fields = [ renderer_name ] ) <EOL> except access_control . UnauthorizedAccess , e : <EOL> result = http . HttpResponse ( content_type = "<STR_LIT>" ) <EOL> result = renderers . Renderer . GetPlugin ( "<STR_LIT>" ) ( ) . Layout ( <EOL> request , result , exception = e ) <EOL> except Exception : <EOL> stats . STATS . IncrementCounter ( "<STR_LIT>" , <EOL> fields = [ renderer_name ] ) <EOL> if flags . FLAGS . debug : <EOL> pdb . post_mortem ( ) <EOL> raise <EOL> if not isinstance ( result , ( http . HttpResponse , <EOL> http . StreamingHttpResponse ) ) : <EOL> raise RuntimeError ( "<STR_LIT>" % result ) <EOL> return result <EOL> def RedirectToRemoteHelp ( path ) : <EOL> """<STR_LIT>""" <EOL> target_path = os . path . join ( config_lib . CONFIG [ "<STR_LIT>" ] , <EOL> path . replace ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> response = http . HttpResponse ( ) <EOL> response . write ( """<STR_LIT>""" % target_path ) <EOL> return response <EOL> @ webauth . SecurityCheck <EOL> def RenderHelp ( request , path , document_root = None , content_type = None ) : <EOL> """<STR_LIT>""" <EOL> _ = document_root <EOL> _ = content_type <EOL> request . REQ = request . REQUEST <EOL> help_path = request . path . split ( "<STR_LIT:/>" , <NUM_LIT:2> ) [ - <NUM_LIT:1> ] <EOL> if not help_path : <EOL> return AccessDenied ( "<STR_LIT>" ) <EOL> try : <EOL> user_record = aff4 . FACTORY . Open ( <EOL> aff4 . ROOT_URN . Add ( "<STR_LIT>" ) . Add ( request . user ) , "<STR_LIT>" , <EOL> token = BuildToken ( request , <NUM_LIT> ) ) <EOL> settings = user_record . Get ( user_record . Schema . GUI_SETTINGS ) <EOL> except IOError : <EOL> settings = aff4 . GRRUser . SchemaCls . GUI_SETTINGS ( ) <EOL> if settings . docs_location == settings . DocsLocation . REMOTE : <EOL> return RedirectToRemoteHelp ( help_path ) <EOL> else : <EOL> static_handler_components = urls . static_handler . split ( "<STR_LIT:.>" ) <EOL> static_handler_module = importlib . import_module ( "<STR_LIT:.>" . join ( <EOL> static_handler_components [ <NUM_LIT:0> : - <NUM_LIT:1> ] ) ) <EOL> static_handler = getattr ( static_handler_module , <EOL> static_handler_components [ - <NUM_LIT:1> ] ) <EOL> return static_handler ( <EOL> request , path , document_root = config_lib . CONFIG [ "<STR_LIT>" ] ) <EOL> def BuildToken ( request , execution_time ) : <EOL> """<STR_LIT>""" <EOL> token = access_control . ACLToken ( <EOL> username = request . user , <EOL> reason = request . REQ . get ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> process = "<STR_LIT>" , <EOL> expiry = rdfvalue . RDFDatetime ( ) . Now ( ) + execution_time ) <EOL> for field in [ "<STR_LIT>" , "<STR_LIT>" ] : <EOL> remote_addr = request . META . get ( field , "<STR_LIT>" ) <EOL> if remote_addr : <EOL> token . source_ips . append ( remote_addr ) <EOL> return token <EOL> def AccessDenied ( message ) : <EOL> """<STR_LIT>""" <EOL> response = shortcuts . render_to_response ( "<STR_LIT>" , { "<STR_LIT:message>" : message } ) <EOL> logging . warn ( message ) <EOL> response . status_code = <NUM_LIT> <EOL> stats . STATS . IncrementCounter ( "<STR_LIT>" ) <EOL> return response <EOL> def ServerError ( unused_request , template_name = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> stats . STATS . IncrementCounter ( "<STR_LIT>" ) <EOL> response = shortcuts . render_to_response ( template_name ) <EOL> response . status_code = <NUM_LIT> <EOL> return response </s>
