zfc@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__( RRRRRR R R R R RR(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(R(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_frozendtcGs tddS(Nsdon't use this(R0(RR(((s/usr/lib64/python2.7/rexec.pyt load_sourceeR2cGs tddS(Nsdon't use this(R0(RR(((s/usr/lib64/python2.7/rexec.pyt load_compiledfR2cGs tddS(Nsdon't use this(R0(RR(((s/usr/lib64/python2.7/rexec.pyt load_packagegR2cCs|jj|||S(N(R't load_dynamic(RRtfilenametfile((s/usr/lib64/python2.7/rexec.pyR6iscCs|jj|S(N(R't add_module(RR((s/usr/lib64/python2.7/rexec.pyR9lscCs |jjS(N(R'tmodules(R((s/usr/lib64/python2.7/rexec.pyt modules_dictoscCs|jjdjS(Ntsys(R'R:tpath(R((s/usr/lib64/python2.7/rexec.pyt default_pathrs(RRRR*R+R,R/R1R3R4R5R6R9R;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_infotopenR8treloadR-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*R:tok_builtin_modulestok_dynamic_modulesR<tbuiltin_module_namestappendttupletset_trusted_patht make_builtintmake_initial_modulestmake_syst RModuleLoadertloadertRModuleImportertimporter(RRqR)tlisttmname((s/usr/lib64/python2.7/rexec.pyRs"       cCsttjjtj|_dS(N(tfiltertosR=tisabsR<t trusted_path(R((s/usr/lib64/python2.7/rexec.pyRwscCsi||jkrtd|n|tjkr>tj|}ntj|||}|j|g}|S(Nsuntrusted dynamic module: %s(Rst ImportErrorR<R:timpR6R.(RRR7R8tsrctdst((s/usr/lib64/python2.7/rexec.pyR6scCs|j|jdS(N(t make_maint make_osname(R((s/usr/lib64/python2.7/rexec.pyRys cCs0gtjD]}|d|jkr |^q S(Ni(RR+t ok_file_types(Rtitem((s/usr/lib64/python2.7/rexec.pyR+scCs ||jkS(N(Rr(RR((s/usr/lib64/python2.7/rexec.pyR,scCsD|jt|j}|j|_|j|_|j|_|_ dS(N( R.t __builtin__tnok_builtin_namestr_importR-tr_reloadRntr_openRmR8(RR((s/usr/lib64/python2.7/rexec.pyRxs  cCs|jddS(Nt__main__(R9(R((s/usr/lib64/python2.7/rexec.pyRscCsetj}t|}|j||j}i|_}x'tjjD]\}}||||jj||j|<}n|jd|_|S(NR(R:tgetR"Rqt new_modulet __builtins__(RRR((s/usr/lib64/python2.7/rexec.pyR9*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(R9t__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(R9tevalR(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(R9texecfileR(RR8R((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~Rn(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( R:Rtdelegate_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_filesRR:RRRRRRRR<RR(RR((s/usr/lib64/python2.7/rexec.pyt set_files~s        cCs?|j|jd}|j|_|j|_|j|_dS(NR<(t restore_filesR:RRRRRR(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(RR(tstrtIOErrorRm(RR8tmodetbuf((s/usr/lib64/python2.7/rexec.pyRs   cCs(tj\}}}d}|||fS(N(R<RR"(Rttytvattr((s/usr/lib64/python2.7/rexec.pyRs(R?R@RARBRCRDRERFRGRHRIRJRKRLRMRNRORP(RQRRRSRTRURVRWRXRYRZR[R\R]R^R_( R`RaRbRcRdReRfRgRhRiRjRkRl(RmR8RnR-N((2RRRRvR<R=RRrRRRRt C_EXTENSIONt PY_SOURCERR"RRwR6RyR+R,RxRRRzR.RRR9RRRRRRRRRRRRRRRRRRRR(((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<R2t-s%s: can't open file %rtRestrictedConsolecseZfdZRS(cs4jd|jd<jjj||fdS(NRR(R:RRtInteractiveConsoletruncode(Rtco(RR(s/usr/lib64/python2.7/rexec.pyR7s(RRR((RR(s/usr/lib64/python2.7/rexec.pyR6sRtexec(tgetoptt tracebackR<RRuRRrRvR:R=tinsertRtdirnameRRmRRRRRRRtinteractt SystemExitRRtcompileRRt print_exc(RRtoptsR(R)ttrustedtotatfptmsgRRtnttexttc((RRs/usr/lib64/python2.7/rexec.pyttestsX   -    "   R(RtwarningsRR<RRR%Rt__all__RRRRR&R tFancyModuleLoaderR{tModuleImporterR}RpRRRRb(((s/usr/lib64/python2.7/rexec.pyts*       4   4