Plan 9 from Bell Labs’s /usr/web/sources/contrib/stallion/root/sys/lib/python2.7/rexec.pyc

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


�`^c@s#dZddlmZeddd�[ddlZddlZddlZddlZddlZdgZdfd	��YZ	d
e	fd��YZ
dZd
e	fd��YZdej
fd��YZejZejZdejfd��YZd�Zedkreje��ndS(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/sys/lib/python2.7/rexec.pyR stFileWrappercBseZd�Zd�ZRS(cCsSxL|jD]A}t||�r
t||�r
t||t||��q
q
WdS(N(Rthasattrtsetattrtgetattr(tselftftm((s/sys/lib/python2.7/rexec.pyt__init__+scCs|j�dS(N(R(R((s/sys/lib/python2.7/rexec.pytclose0s(RRRR(((s/sys/lib/python2.7/rexec.pyR's	sL
def %s(self, *args):
        return getattr(self.mod, self.name).%s(*args)
tFileDelegatecBs;eZd�Zx'ejdD]ZeeefdUqWRS(cCs||_||_dS(N(tmodtname(RRR((s/sys/lib/python2.7/rexec.pyR;s	RN(sclose(RRRRRRtTEMPLATE(((s/sys/lib/python2.7/rexec.pyR9s	tRHookscBs}eZd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Z	d�Z
d	�Zd
�Zd�Z
d�ZRS(
cGs�d}d}|rEt|d�td�krE|d}|d }n|rut|dd�ru|d}|d}n|r�td�ntjj||�||_dS(Nii�t	__class__istoo many arguments(tNonettypeRt	TypeErrortihookstHooksRtrexec(RtargstverboseR'((s/sys/lib/python2.7/rexec.pyREs"



cCs
||_dS(N(R'(RR'((s/sys/lib/python2.7/rexec.pyt	set_rexecVscCs
|jj�S(N(R'tget_suffixes(R((s/sys/lib/python2.7/rexec.pyR+ZscCs|jj|�S(N(R't
is_builtin(RR((s/sys/lib/python2.7/rexec.pyR,]scCst|�}|jj|d�S(N((t
__import__R'tcopy_except(RRR((s/sys/lib/python2.7/rexec.pytinit_builtin`scCs
td�dS(Nsdon't use this(tSystemError(RR((s/sys/lib/python2.7/rexec.pytinit_frozendscGs
td�dS(Nsdon't use this(R0(RR(((s/sys/lib/python2.7/rexec.pytload_sourceescGs
td�dS(Nsdon't use this(R0(RR(((s/sys/lib/python2.7/rexec.pyt
load_compiledfscGs
td�dS(Nsdon't use this(R0(RR(((s/sys/lib/python2.7/rexec.pytload_packagegscCs|jj|||�S(N(R'tload_dynamic(RRtfilenametfile((s/sys/lib/python2.7/rexec.pyR5iscCs|jj|�S(N(R't
add_module(RR((s/sys/lib/python2.7/rexec.pyR8lscCs
|jjS(N(R'tmodules(R((s/sys/lib/python2.7/rexec.pytmodules_dictoscCs|jjdjS(Ntsys(R'R9tpath(R((s/sys/lib/python2.7/rexec.pytdefault_pathrs(RRRR*R+R,R/R1R2R3R4R5R8R:R=(((s/sys/lib/python2.7/rexec.pyR Cs												cBs�eZdZeej�ZdXZdYZdZZ	d[Z
ejej
fZd\d3d4�Zd5�Zd6�Zd7�Zd8�Zd9�Zd:�Zd;�Zd<�Zd=�Zd>�Zd?�Zd@�ZdA�ZdB�ZdC�ZdD�Z iigdE�Z!dF�Z"dG�Z#dH�Z$dI�Z%dJ�Z&dK�Z'dL�Z(d]idM�Z)dN�Z*dO�Z+dP�Z,dQ�Z-dR�Z.dS�Z/dTdUdV�Z0dW�Z1RS(^s�Basic 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	copyrighttexittgetdefaultencodingtgetrefcountt
hexversiontmaxintt
maxunicodetplatformtps1tps2tversiontversion_infotopenR7treloadR-icCs�td�tjj||�|p+t|�|_|jj|�i|_|j|_	g}x0|jD]%}|t
jkrf|j|�qfqfWt
|�|_|j�|j�|j�|j�t|j|�|_t|j|�|_dS(s�Returns 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/sys/lib/python2.7/rexec.pyR�s"		



cCsttjjtj�|_dS(N(tfiltertosR<tisabsR;ttrusted_path(R((s/sys/lib/python2.7/rexec.pyRv�scCsi||jkrtd|�n|tjkr>tj|}ntj|||�}|j|g�}|S(Nsuntrusted dynamic module: %s(RrtImportErrorR;R9timpR5R.(RRR6R7tsrctdst((s/sys/lib/python2.7/rexec.pyR5�scCs|j�|j�dS(N(t	make_maintmake_osname(R((s/sys/lib/python2.7/rexec.pyRx�s
cCs0gtj�D]}|d|jkr
|^q
S(Ni(R�R+t
ok_file_types(Rtitem((s/sys/lib/python2.7/rexec.pyR+�scCs
||jkS(N(Rq(RR((s/sys/lib/python2.7/rexec.pyR,�scCsD|jt|j�}|j|_|j|_|j|_|_	dS(N(
R.t__builtin__tnok_builtin_namestr_importR-tr_reloadRmtr_openRlR7(RR((s/sys/lib/python2.7/rexec.pyRw�scCs|jd�dS(Nt__main__(R8(R((s/sys/lib/python2.7/rexec.pyR��scCsetj}t|�}|j||j�}i|_}x'tjj�D]\}}|||<qGWdS(N(R�RR-t	copy_onlytok_posix_namestenvirontitems(RtosnameR�R�tetkeytvalue((s/sys/lib/python2.7/rexec.pyR��s	
cCs�|jt|j�}|j|_dg|_td|j�|_|j	|_
|jd}|jj�t|j
�}|j�t|�|_dS(Nt
RESTRICTEDR;(R�R;tok_sys_namesR9targvtmapR"tok_pathR<t
r_exc_infotexc_infotkeysR~RqtsortRuRs(RRtl((s/sys/lib/python2.7/rexec.pyRys

cCsy|j|�}x-t|�D]}t||t||��qWx3|D]+}yt||�WqFtk
rpqFXqFW|S(N(t	copy_nonetdirRRtdelattrtAttributeError(RR�t
exceptionsR�R((s/sys/lib/python2.7/rexec.pyR.s

cCs^|j|�}xH|D]@}yt||�}Wntk
rEqnXt|||�qW|S(N(R�RR�R(RR�tnamesR�RR�((s/sys/lib/python2.7/rexec.pyR�s

cCs"|j|j�}|j|_|S(N(R8Rt__doc__(RR�R((s/sys/lib/python2.7/rexec.pyR�#scCsR|jj|�}|dkr>|jj|�|j|<}n|jd|_|S(NR�(R9tgetR"Rpt
new_modulet__builtins__(RRR((s/sys/lib/python2.7/rexec.pyR8*s
 cBs|jd�}||jUdS(sExecute 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.

        R�N(R8t__dict__(RtcodeR((s/sys/lib/python2.7/rexec.pytr_exec3scCs|jd�}t||j�S(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�(RR�R((s/sys/lib/python2.7/rexec.pytr_eval>s	cCs#|jd�}t||j�dS(sfExecute the Python code in the file in the restricted
        environment's __main__ module.

        R�N(R8texecfileR�(RR7R((s/sys/lib/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/sys/lib/python2.7/rexec.pyR�Rs	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/sys/lib/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/sys/lib/python2.7/rexec.pytr_unloadgs
cCs}|jd}t|d�|_t|d�|_t|d�|_ttj�|_ttj	�|_
ttj�|_dS(NR;tstdintstdouttstderr(
R9Rtdelegate_stdintdelegate_stdouttdelegate_stderrRR;R�trestricted_stdinR�trestricted_stdoutR�trestricted_stderr(Rts((s/sys/lib/python2.7/rexec.pytmake_delegate_filesus
cCs�t|d�s|j�nt|d�s8|j�n|jd}|j|_|j|_|j|_	|j
t_|jt_|j
t_	dS(Nt
save_stdinR�R;(Rt
save_filesR�R9R�R�R�R�R�R�R�R;R�R�(RR�((s/sys/lib/python2.7/rexec.pyt	set_files~s


cCs?|j�|jd}|j|_|j|_|j|_dS(NR;(t
restore_filesR9R�R�R�R�R�R�(RR�((s/sys/lib/python2.7/rexec.pytreset_files�s


cCs(tj|_tj|_tj|_dS(N(R;R�R�R�tsave_stdoutR�tsave_stderr(R((s/sys/lib/python2.7/rexec.pyR��scCs(|jt_|jt_|jt_dS(N(R�R;R�R�R�R�R�(R((s/sys/lib/python2.7/rexec.pyR��scCs9|j�z|j�|||�}Wd|j�X|S(N(R�R�R�(RtfuncR(tkwtr((s/sys/lib/python2.7/rexec.pyts_apply�s

cGs|j|j|�S(s�Execute 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.

        (R�R�(RR(((s/sys/lib/python2.7/rexec.pyts_exec�scGs|j|j|�S(s�Evaluate 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.

        (R�R�(RR(((s/sys/lib/python2.7/rexec.pyts_eval�s
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.

        (R�R�(RR(((s/sys/lib/python2.7/rexec.pyt
s_execfile�s	cGs|j|j|�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.

        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.

        (R�R�(RR(((s/sys/lib/python2.7/rexec.pyts_import�s
cGs|j|j|�S(s�Reload 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.

        (R�R�(RR(((s/sys/lib/python2.7/rexec.pyts_reload�scGs|j|j|�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.

        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.

        (R�R�(RR(((s/sys/lib/python2.7/rexec.pyts_unload�sR�i�cCs4t|�}|dkr$td�nt|||�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.

        R�trbs/can't open files for writing in restricted mode(R�srb(tstrtIOErrorRl(RR7tmodetbuf((s/sys/lib/python2.7/rexec.pyR��s
cCs(tj�\}}}d}|||fS(N(R;R�R"(Rttytvattr((s/sys/lib/python2.7/rexec.pyR�s(R>sarrayR@RAserrnoRCsmarshalRERFRGRHRIRJs_sreRLsstructRNs_weakref(serrorsfstatslistdirslstatsreadlinksstatstimessunamesgetpidsgetppidsgetcwdsgetuidsgetgidsgeteuidsgetegid(
s	byteorders	copyrightsexitsgetdefaultencodingsgetrefcounts
hexversionsmaxints
maxunicodesplatformRhRisversionsversion_info(sopensfilesreloads
__import__N((2RRR�RuR;R<R�RqR�R�R�R�tC_EXTENSIONt	PY_SOURCER�R"RRvR5RxR+R,RwR�R�RyR.R�R�R8R�R�R�R�R�R�R�R�R�R�R�R�R�R�R�R�R�R�R�R�(((s/sys/lib/python2.7/rexec.pyR{sd3		
							
		
							
				
										csddl}ddl}|jtjdd�\}}d}g}xI|D]A\}}|dkro|d}n|dkrJ|j|�qJqJWtd|��|r��jt|��_n|r|�jd_�jdj	j
dtj	j|d��n�jdj	j
dd	�tj
}|r�|dd
kr�yt|d�}Wq�tk
r}}	dtjd|dfGHdSXn|j�ryddl}
Wntk
r�nXddl�d�jf��fd
��Y}y|�jdj�j�Wq{tk
r}|SXnd|j�}
|j�t|
|jd�}y�j|�Wn%tk
ri}|S|j�dSXdS(Ni�isvt:is-vs-tR)R;tt-s%s: can't open file %rtRestrictedConsolecseZ��fd�ZRS(cs4�jd|jd<�j�jj||f�dS(NR�R�(R9R�R�tInteractiveConsoletruncode(Rtco(R�R�(s/sys/lib/python2.7/rexec.pyR�7s(RRR�((R�R�(s/sys/lib/python2.7/rexec.pyR�6sR�texec(tgetoptt	tracebackR;R�RtRRqRuR9R<tinsertR�tdirnameR�RlR�RRR�R�R�R�tinteractt
SystemExitRRtcompileRR�t	print_exc(R�R�toptsR(R)ttrustedtotatfptmsgRR�tnttexttc((R�R�s/sys/lib/python2.7/rexec.pyttestsX
-	
"

R�(R�twarningsRR;R�R�R%R�t__all__RRRRR&R tFancyModuleLoaderRztModuleImporterR|RoRR�RRa(((s/sys/lib/python2.7/rexec.pyt<module>s*	
4		�4

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].