nfc@s#dZddlmZeddd[ddlZddlZddlZddlZddlZdgZdfd YZ d e fd YZ d Z d e fdYZ dej fdYZejZejZdejfdYZdZedkrejendS(s9Restricted execution facilities. The class RExec exports methods r_exec(), r_eval(), r_execfile(), and r_import(), which correspond roughly to the built-in operations exec, eval(), execfile() and import, but executing the code in an environment that only exposes those built-in operations that are deemed safe. To this end, a modest collection of 'fake' modules is created which mimics the standard modules by the same names. It is a policy decision which built-in modules and operations are made available; this module provides a reasonable default, but derived classes can change the policies e.g. by overriding or extending class variables like ok_builtin_modules or methods like make_sys(). XXX To do: - r_open should allow writing tmp dir - r_exec etc. with explicit globals/locals? (Use rexec("exec ... in ...")?) i(twarnpy3ks/the rexec module has been removed in Python 3.0t stackleveliNtRExectFileBasec BseZd ZRS( tfilenotflushtisattytreadtreadlinet readlinestseekttelltwritet writelinest xreadlinest__iter__( sfilenosflushsisattysreadsreadlines readlinessseekstellswrites writeliness xreadliness__iter__(t__name__t __module__tok_file_methods(((s/usr/lib64/python2.7/rexec.pyR st FileWrappercBseZdZdZRS(cCsSxL|jD]A}t|| r t||r t||t||q q WdS(N(Rthasattrtsetattrtgetattr(tselftftm((s/usr/lib64/python2.7/rexec.pyt__init__+scCs|jdS(N(R(R((s/usr/lib64/python2.7/rexec.pytclose0s(RRRR(((s/usr/lib64/python2.7/rexec.pyR's sL def %s(self, *args): return getattr(self.mod, self.name).%s(*args) t FileDelegatecBs;eZdZx'ejdD]ZeeefdUqWRS(cCs||_||_dS(N(tmodtname(RRR((s/usr/lib64/python2.7/rexec.pyR;s RN(sclose(RRRRRRtTEMPLATE(((s/usr/lib64/python2.7/rexec.pyR9s tRHookscBs}eZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d ZRS( cGsd}d}|rEt|dtdkrE|d}|d }n|rut|ddru|d}|d}n|rtdntjj||||_dS(Niit __class__istoo many arguments(tNonettypeRt TypeErrortihookstHooksRtrexec(RtargstverboseR'((s/usr/lib64/python2.7/rexec.pyREs"     cCs ||_dS(N(R'(RR'((s/usr/lib64/python2.7/rexec.pyt set_rexecVscCs |jjS(N(R't get_suffixes(R((s/usr/lib64/python2.7/rexec.pyR+ZscCs|jj|S(N(R't is_builtin(RR((s/usr/lib64/python2.7/rexec.pyR,]scCst|}|jj|dS(N((t __import__R't copy_except(RRR((s/usr/lib64/python2.7/rexec.pyt init_builtin`s cCs tddS(Nsdon't use this(t SystemError(RR((s/usr/lib64/python2.7/rexec.pyt init_frozendscGs tddS(Nsdon't use this(R0(RR(((s/usr/lib64/python2.7/rexec.pyt load_sourceescGs tddS(Nsdon't use this(R0(RR(((s/usr/lib64/python2.7/rexec.pyt load_compiledfscGs tddS(Nsdon't use this(R0(RR(((s/usr/lib64/python2.7/rexec.pyt load_packagegscCs|jj|||S(N(R't load_dynamic(RRtfilenametfile((s/usr/lib64/python2.7/rexec.pyR5iscCs|jj|S(N(R't add_module(RR((s/usr/lib64/python2.7/rexec.pyR8lscCs |jjS(N(R'tmodules(R((s/usr/lib64/python2.7/rexec.pyt modules_dictoscCs|jjdjS(Ntsys(R'R9tpath(R((s/usr/lib64/python2.7/rexec.pyt default_pathrs(RRRR*R+R,R/R1R2R3R4R5R8R:R=(((s/usr/lib64/python2.7/rexec.pyR Cs            cBseZdZeejZdXZdYZdZZ d[Z e j e j fZd\d3d4Zd5Zd6Zd7Zd8Zd9Zd:Zd;Zd<Zd=Zd>Zd?Zd@ZdAZdBZdCZdDZ iigdEZ!dFZ"dGZ#dHZ$dIZ%dJZ&dKZ'dLZ(d]idMZ)dNZ*dOZ+dPZ,dQZ-dRZ.dSZ/dTdUdVZ0dWZ1RS(^sBasic restricted execution framework. Code executed in this restricted environment will only have access to modules and functions that are deemed safe; you can subclass RExec to add or remove capabilities as desired. The RExec class can prevent code from performing unsafe operations like reading or writing disk files, or using TCP/IP sockets. However, it does not protect against code using extremely large amounts of memory or processor time. taudiooptarraytbinasciitcmathterrnotimageoptmarshaltmathtmd5toperatortparsertselecttshat_sretstroptstructttimet_weakrefterrortfstattlistdirtlstattreadlinktstatttimestunametgetpidtgetppidtgetcwdtgetuidtgetgidtgeteuidtgetegidt byteordert copyrighttexittgetdefaultencodingt getrefcountt hexversiontmaxintt maxunicodetplatformtps1tps2tversiont version_infotopenR7treloadR-icCstdtjj|||p+t||_|jj|i|_|j|_ g}x0|jD]%}|t j krf|j |qfqfWt ||_|j|j|j|jt|j||_t|j||_dS(sReturns an instance of the RExec class. The hooks parameter is an instance of the RHooks class or a subclass of it. If it is omitted or None, the default RHooks class is instantiated. Whenever the RExec module searches for a module (even a built-in one) or reads a module's code, it doesn't actually go out to the file system itself. Rather, it calls methods of an RHooks instance that was passed to or created by its constructor. (Actually, the RExec object doesn't make these calls --- they are made by a module loader object that's part of the RExec object. This allows another level of flexibility, which can be useful when changing the mechanics of import within the restricted environment.) By providing an alternate RHooks object, we can control the file system accesses made to import a module, without changing the actual algorithm that controls the order in which those accesses are made. For instance, we could substitute an RHooks object that passes all filesystem requests to a file server elsewhere, via some RPC mechanism such as ILU. Grail's applet loader uses this to support importing applets from a URL for a directory. If the verbose parameter is true, additional debugging output may be sent to standard output. s/This code is not secure in Python 2.2 and laterN(t RuntimeErrorR%t_VerboseRR thooksR*R9tok_builtin_modulestok_dynamic_modulesR;tbuiltin_module_namestappendttupletset_trusted_patht make_builtintmake_initial_modulestmake_syst RModuleLoadertloadertRModuleImportertimporter(RRpR)tlisttmname((s/usr/lib64/python2.7/rexec.pyRs"       cCsttjjtj|_dS(N(tfiltertosR<tisabsR;t trusted_path(R((s/usr/lib64/python2.7/rexec.pyRvscCsi||jkrtd|n|tjkr>tj|}ntj|||}|j|g}|S(Nsuntrusted dynamic module: %s(Rrt ImportErrorR;R9timpR5R.(RRR6R7tsrctdst((s/usr/lib64/python2.7/rexec.pyR5scCs|j|jdS(N(t make_maint make_osname(R((s/usr/lib64/python2.7/rexec.pyRxs cCs0gtjD]}|d|jkr |^q S(Ni(RR+t ok_file_types(Rtitem((s/usr/lib64/python2.7/rexec.pyR+scCs ||jkS(N(Rq(RR((s/usr/lib64/python2.7/rexec.pyR,scCsD|jt|j}|j|_|j|_|j|_|_ dS(N( R.t __builtin__tnok_builtin_namestr_importR-tr_reloadRmtr_openRlR7(RR((s/usr/lib64/python2.7/rexec.pyRws  cCs|jddS(Nt__main__(R8(R((s/usr/lib64/python2.7/rexec.pyRscCsetj}t|}|j||j}i|_}x'tjjD]\}}||||jj||j|<}n|jd|_|S(NR(R9tgetR"Rpt new_modulet __builtins__(RRR((s/usr/lib64/python2.7/rexec.pyR8*s   cBs|jd}||jUdS(s Execute code within a restricted environment. The code parameter must either be a string containing one or more lines of Python code, or a compiled code object, which will be executed in the restricted environment's __main__ module. RN(R8t__dict__(RtcodeR((s/usr/lib64/python2.7/rexec.pytr_exec3scCs|jd}t||jS(sFEvaluate code within a restricted environment. The code parameter must either be a string containing a Python expression, or a compiled code object, which will be evaluated in the restricted environment's __main__ module. The value of the expression or code object will be returned. R(R8tevalR(RRR((s/usr/lib64/python2.7/rexec.pytr_eval>s cCs#|jd}t||jdS(sfExecute the Python code in the file in the restricted environment's __main__ module. RN(R8texecfileR(RR7R((s/usr/lib64/python2.7/rexec.pyt r_execfileJscCs|jj||||S(s=Import a module, raising an ImportError exception if the module is considered unsafe. This method is implicitly called by code executing in the restricted environment. Overriding this method in a subclass is used to change the policies enforced by a restricted environment. (R}t import_module(RRtglobalstlocalstfromlist((s/usr/lib64/python2.7/rexec.pyRRs cCs|jj|S(sReload the module object, re-parsing and re-initializing it. This method is implicitly called by code executing in the restricted environment. Overriding this method in a subclass is used to change the policies enforced by a restricted environment. (R}Rm(RR((s/usr/lib64/python2.7/rexec.pyR]scCs|jj|S(s@Unload the module. Removes it from the restricted environment's sys.modules dictionary. This method is implicitly called by code executing in the restricted environment. Overriding this method in a subclass is used to change the policies enforced by a restricted environment. (R}tunload(RR((s/usr/lib64/python2.7/rexec.pytr_unloadgs cCs}|jd}t|d|_t|d|_t|d|_ttj|_ttj |_ ttj |_ dS(NR;tstdintstdouttstderr( R9Rtdelegate_stdintdelegate_stdouttdelegate_stderrRR;Rtrestricted_stdinRtrestricted_stdoutRtrestricted_stderr(Rts((s/usr/lib64/python2.7/rexec.pytmake_delegate_filesus cCst|ds|jnt|ds8|jn|jd}|j|_|j|_|j|_ |j t _|j t _|j t _ dS(Nt save_stdinRR;(Rt save_filesRR9RRRRRRRR;RR(RR((s/usr/lib64/python2.7/rexec.pyt set_files~s        cCs?|j|jd}|j|_|j|_|j|_dS(NR;(t restore_filesR9RRRRRR(RR((s/usr/lib64/python2.7/rexec.pyt reset_filess     cCs(tj|_tj|_tj|_dS(N(R;RRRt save_stdoutRt save_stderr(R((s/usr/lib64/python2.7/rexec.pyRs  cCs(|jt_|jt_|jt_dS(N(RR;RRRRR(R((s/usr/lib64/python2.7/rexec.pyRs  cCs9|jz|j|||}Wd|jX|S(N(RRR(RtfuncR(tkwtr((s/usr/lib64/python2.7/rexec.pyts_applys    cGs|j|j|S(sExecute code within a restricted environment. Similar to the r_exec() method, but the code will be granted access to restricted versions of the standard I/O streams sys.stdin, sys.stderr, and sys.stdout. The code parameter must either be a string containing one or more lines of Python code, or a compiled code object, which will be executed in the restricted environment's __main__ module. (RR(RR(((s/usr/lib64/python2.7/rexec.pyts_execs cGs|j|j|S(sEvaluate code within a restricted environment. Similar to the r_eval() method, but the code will be granted access to restricted versions of the standard I/O streams sys.stdin, sys.stderr, and sys.stdout. The code parameter must either be a string containing a Python expression, or a compiled code object, which will be evaluated in the restricted environment's __main__ module. The value of the expression or code object will be returned. (RR(RR(((s/usr/lib64/python2.7/rexec.pyts_evals cGs|j|j|S(s!Execute the Python code in the file in the restricted environment's __main__ module. Similar to the r_execfile() method, but the code will be granted access to restricted versions of the standard I/O streams sys.stdin, sys.stderr, and sys.stdout. (RR(RR(((s/usr/lib64/python2.7/rexec.pyt s_execfiles cGs|j|j|S(sImport a module, raising an ImportError exception if the module is considered unsafe. This method is implicitly called by code executing in the restricted environment. Overriding this method in a subclass is used to change the policies enforced by a restricted environment. Similar to the r_import() method, but has access to restricted versions of the standard I/O streams sys.stdin, sys.stderr, and sys.stdout. (RR(RR(((s/usr/lib64/python2.7/rexec.pyts_imports cGs|j|j|S(sReload the module object, re-parsing and re-initializing it. This method is implicitly called by code executing in the restricted environment. Overriding this method in a subclass is used to change the policies enforced by a restricted environment. Similar to the r_reload() method, but has access to restricted versions of the standard I/O streams sys.stdin, sys.stderr, and sys.stdout. (RR(RR(((s/usr/lib64/python2.7/rexec.pyts_reloads cGs|j|j|S(sUnload the module. Removes it from the restricted environment's sys.modules dictionary. This method is implicitly called by code executing in the restricted environment. Overriding this method in a subclass is used to change the policies enforced by a restricted environment. Similar to the r_unload() method, but has access to restricted versions of the standard I/O streams sys.stdin, sys.stderr, and sys.stdout. (RR(RR(((s/usr/lib64/python2.7/rexec.pyts_unloadsRicCs4t|}|dkr$tdnt|||S(sGMethod called when open() is called in the restricted environment. The arguments are identical to those of the open() function, and a file object (or a class instance compatible with file objects) should be returned. RExec's default behaviour is allow opening any file for reading, but forbidding any attempt to write a file. This method is implicitly called by code executing in the restricted environment. Overriding this method in a subclass is used to change the policies enforced by a restricted environment. Rtrbs/can't open files for writing in restricted mode(Rsrb(tstrtIOErrorRl(RR7tmodetbuf((s/usr/lib64/python2.7/rexec.pyRs   cCs(tj\}}}d}|||fS(N(R;RR"(Rttytvattr((s/usr/lib64/python2.7/rexec.pyRs(R>sarrayR@RAserrnoRCsmarshalRERFRGRHRIRJs_sreRLsstructRNs_weakref(serrorsfstatslistdirslstatsreadlinksstatstimessunamesgetpidsgetppidsgetcwdsgetuidsgetgidsgeteuidsgetegid( s byteorders copyrightsexitsgetdefaultencodings getrefcounts hexversionsmaxints maxunicodesplatformRhRisversions version_info(sopensfilesreloads __import__N((2RRRRuR;R<RRqRRRRt C_EXTENSIONt PY_SOURCERR"RRvR5RxR+R,RwRRRyR.RRR8RRRRRRRRRRRRRRRRRRRR(((s/usr/lib64/python2.7/rexec.pyR{sd 3                  csddl}ddl}|jtjdd\}}d}g}xI|D]A\}}|dkro|d}n|dkrJ|j|qJqJWtd||rjt|_n|r|jd_jdj j dt j j |dnjdj j dd tj }|r|dd kryt|d}Wqtk r}} d tjd|dfGHdSXn|jryddl} Wntk rnXddld jffd Y} y| jdjjWq{tk r} | SXnd|j} |jt| |jd}yj|Wn%tk ri} | S|jdSXdS(Niisvt:is-vs-tR)R;tt-s%s: can't open file %rtRestrictedConsolecseZfdZRS(cs4jd|jd<jjj||fdS(NRR(R9RRtInteractiveConsoletruncode(Rtco(RR(s/usr/lib64/python2.7/rexec.pyR7s(RRR((RR(s/usr/lib64/python2.7/rexec.pyR6sRtexec(tgetoptt tracebackR;RRtRRqRuR9R<tinsertRtdirnameRRlRRRRRRRtinteractt SystemExitRRtcompileRRt print_exc(RRtoptsR(R)ttrustedtotatfptmsgRRtnttexttc((RRs/usr/lib64/python2.7/rexec.pyttestsX   -    "   R(RtwarningsRR;RRR%Rt__all__RRRRR&R tFancyModuleLoaderRztModuleImporterR|RoRRRRa(((s/usr/lib64/python2.7/rexec.pyts*       4   4