m %UIc@sdZdkZdkZdkZdkZdkZdgZdfdYZdefdYZdZ defd YZ d ei fd YZ ei ZeiZdeifd YZd ZedjoeiendS(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 ...")?) NtRExectFileBasec BstZd ZRS(Ntfilenotflushtisattytreadtreadlinet readlinestseekttelltwritet writelinest xreadlinest__iter__( sfilenosflushsisattysreadsreadlines readlinessseekstellswrites writeliness xreadliness__iter__(t__name__t __module__tok_file_methods(((t"/data/zmath/lib/python2.4/rexec.pyRst FileWrappercBstZdZdZRS(NcCsVxO|iD]D}t|| o-t||ot||t||q q WdS(N(tselfRtmthasattrtftsetattrtgetattr(RRR((Rt__init__(s !cCs|idS(N(RR(R((Rtclose-s(RRRR(((RR$s sL def %s(self, *args): return getattr(self.mod, self.name).%s(*args) t FileDelegatecBs;tZdZx'eidD]ZeeefdUqWRS(NcCs||_||_dS(N(tmodRtname(RRR((RR8s R(sclose(RRRRRRtTEMPLATE(((RR6s tRHookscBs}tZdZdZdZdZdZdZdZdZ d Z d Z d Z d Z d ZRS(NcGsd}d}|o5t|dtdjo|d}|d }n|o,t|ddo|d}|d}n|o tdntii ||||_dS(Niit __class__istoo many arguments( tverbosetNonetrexectargsttypeRt TypeErrortihookstHooksRR(RR$R!R#((RRBs$   cCs ||_dS(N(R#R(RR#((Rt set_rexecSscCs |iiS(N(RR#t get_suffixes(R((RR*WscCs|ii|S(N(RR#t is_builtinR(RR((RR+ZscCst|}|ii|fS(N(t __import__RRRR#t copy_except(RRR((Rt init_builtin]s cCs tddS(Nsdon't use this(t SystemError(RR((Rt init_frozenascGs tddS(Nsdon't use this(R/(RR$((Rt load_sourcebscGs tddS(Nsdon't use this(R/(RR$((Rt load_compiledcscGs tddS(Nsdon't use this(R/(RR$((Rt load_packagedscCs|ii|||S(N(RR#t load_dynamicRtfilenametfile(RRR5R6((RR4fscCs|ii|S(N(RR#t add_moduleR(RR((RR7iscCs |iiS(N(RR#tmodules(R((Rt modules_dictlscCs|iidiS(Ntsys(RR#R8tpath(R((Rt default_pathos(RRRR)R*R+R.R0R1R2R3R4R7R9R<(((RR@s            cBstZdZeeiZdYZdZZd[Z d\Z e i e i fZed4d5Zd6Zd7Zd8Zd9Zd:Zd;Zd<Zd=Zd>Zd?Zd@ZdAZdBZdCZdDZdEZ hhgdFZ!dGZ"dHZ#dIZ$dJZ%dKZ&dLZ'dMZ(fhdNZ)dOZ*dPZ+dQZ,dRZ-dSZ.dTZ/dUdVdWZ0dXZ1RS(]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. taudiooptarraytbinasciitcmathterrnotimageoptmarshaltmathtmd5toperatortparsertregextselecttshat_sretstroptstructttimet_weakrefterrortfstattlistdirtlstattreadlinktstatttimestunametgetpidtgetppidtgetcwdtgetuidtgetgidtgeteuidtgetegidt byteordert copyrighttexittgetdefaultencodingt getrefcountt hexversiontmaxintt maxunicodetplatformtps1tps2tversiont version_infotopenR6treloadR,icCstdtii|||p t||_|ii|h|_ |i |_ g}x2|i D]'}|tijo|i|qgqgWt||_ |i|i|i|it|i||_t|i||_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 2.3N(t RuntimeErrorR't_VerboseRRR!thooksRR)R8tok_builtin_modulestok_dynamic_modulestlisttmnameR:tbuiltin_module_namestappendttupletset_trusted_patht make_builtintmake_initial_modulestmake_syst RModuleLoadertloadertRModuleImportertimporter(RRpR!RsRt((RRs&        cCsttiiti|_dS(N(tfiltertosR;tisabsR:Rt trusted_path(R((RRxscCsm||ijotd|n|tijoti|}nti|||}|i |g}|S(Nsuntrusted dynamic module: %s( RRRrt ImportErrorR:R8tsrctimpR4R5R6R-tdst(RRR5R6RR((RR4scCs|i|idS(N(Rt make_maint make_osname(R((RRzs cCs=g}tiD]%}|d|ijo ||qq~S(Ni(t_[1]RR*titemRt ok_file_types(RRR((RR*scCs ||ijS(N(RtRRq(RRt((RR+scCsD|it|i}|i|_|i|_|i |_ |_ dS(N( RR-t __builtin__tnok_builtin_namesRtr_importR,tr_reloadRmtr_openRlR6(RR((RRys  cCs|id}dS(Nt__main__(RR7R(RR((RRscCseti}t|}|i||i}h|_ }x'ti i D]\}}||||id_idiidtii|dnidiiddti}|o_|dd joNyt|d}Wqtj o'}d tid|dfGHdSqXn|ioy dk}Wntj onXdkd ifd Y}y|id ii Wqt!j o } | SqXnm|i#}|i%t&||i'd} yi)| Wn-t!j o } | Sn| i*dSnXdS(Nisvt:is-vs-tR!R:tt-s%s: can't open file %rtRestrictedConsolecstZdZRS(Ncs4id|id<iii||fdS(NRR( RR8RRRRtInteractiveConsoletruncodetco(RR(RR(RR4s(RRR((RR(RR3sRtexec(+tgetoptt tracebackR:RtoptsR$R!ttrustedtotaRvRRRqRwR8R;tinsertRtdirnameRtfpRlRtmsgRRRRRRRtinteractt SystemExittnRttextRtcompileRtcRt print_exc(RR!RRRRR$RRRRRRRRRR((RRRttestsZ   .         R(RR:RRR'Rt__all__RRRRR(RtFancyModuleLoaderR|tModuleImporterR~RoRRRRa(RR|R~RRRRRR:R'RRRRR((Rt?s$        4   4