interp.n

来自「tcl是工具命令语言」· N 代码 · 共 611 行 · 第 1/2 页

N
611
字号
\fIslave \fBalias \fIsrcCmd targetCmd \fR?\fIarg ..\fR?Creates an alias such that whenever \fIsrcCmd\fR is invokedin \fIslave\fR, \fItargetCmd\fR is invoked in the master.The \fIarg\fR arguments will be passed to \fItargetCmd\fR as additionalarguments, prepended before any arguments passed in the invocation of\fIsrcCmd\fR.See ALIAS INVOCATION below for details..TP\fIslave \fBeval \fIarg \fR?\fIarg ..\fR?This command concatenates all of the \fIarg\fR arguments inthe same fashion as the \fBconcat\fR command, then evaluatesthe resulting string as a Tcl script in \fIslave\fR.The result of this evaluation (including error informationsuch as the \fBerrorInfo\fR and \fBerrorCode\fR variables, if anerror occurs) is returned to the invoking interpreter..VS "" BR.TP\fIslave \fBexpose \fIhiddenName \fR?\fIexposedCmdName\fR?This command exposes the hidden command \fIhiddenName\fR, eventually bringingit back under a new \fIexposedCmdName\fR name (this name is currentlyaccepted only if it is a valid global name space name without any ::),in \fIslave\fR.If an exposed command with the targeted name already exists, this commandfails.For more details on hidden commands, see HIDDEN COMMANDS, below..TP\fIslave \fBhide \fIexposedCmdName\fR ?\fIhiddenCmdName\fR?This command hides the exposed command \fIexposedCmdName\fR, renaming it to the hidden command \fIhiddenCmdName\fR, or keeping the same name if thethe argument is not given, in the \fIslave\fR interpreter.If a hidden command with the targeted name already exists, this commandfails.Currently both \fIexposedCmdName\fR and \fIhiddenCmdName\fR can not contain namespace qualifiers, or an error is raised.Commands to be hidden are looked up in the globalnamespace even if the current namespace is not the global one. Thisprevents slaves from fooling a master interpreter into hiding the wrongcommand, by making the current namespace be different from the global one.For more details on hidden commands, see HIDDEN COMMANDS, below..TP\fIslave \fBhidden\fRReturns a list of the names of all hidden commands in \fIslave\fR..TP\fIslave \fBinvokehidden\fR ?\fB-global\fR \fIhiddenName \fR?\fIarg ..\fR?This command invokes the hidden command \fIhiddenName\fR with thesupplied arguments, in \fIslave\fR. No substitutions or evaluations areapplied to the arguments.If the \fB-global\fR flag is given, the command is invoked at the globallevel in the slave; otherwise it is invoked at the current call frame andcan access local variables in that or outer call frames.For more details on hidden commands, see HIDDENCOMMANDS, below..VE.TP\fIslave \fBissafe\fRReturns  \fB1\fR if the slave interpreter is safe, \fB0\fR otherwise..VS "" BR.TP\fIslave \fBmarktrusted\fRMarks the slave interpreter as trusted. Can only be invoked by atrusted interpreter. This command does not expose any hiddencommands in the slave interpreter. The command has no effect if the slaveis already trusted..VE.TP\fIslave\fR \fBrecursionlimit\fR ?\fInewlimit\fR?Returns the maximum allowable nesting depth for the \fIslave\fR interpreter.If \fInewlimit\fR is specified, the recursion limit in \fIslave\fR will beset so that nesting of more than \fInewlimit\fR calls to \fBTcl_Eval()\fRand related procedures in \fIslave\fR will return an error.The \fInewlimit\fR value is also returned.The \fInewlimit\fR value must be a positive integer between 1 and themaximum value of a non-long integer on the platform.  .spThe command sets the maximum size of the Tcl call stack only. It cannotby itself prevent stack overflows on the C stack being used by theapplication. If your machine has a limit on the size of the C stack, youmay get stack overflows before reaching the limit set by the command. Ifthis happens, see if there is a mechanism in your system for increasingthe maximum size of the C stack. .SH "SAFE INTERPRETERS".PPA safe interpreter is one with restricted functionality, so thatis safe to execute an arbitrary script from your worst enemy withoutfear of that script damaging the enclosing application or the restof your computing environment.  In order to make an interpretersafe, certain commands and variables are removed from the interpreter.For example, commands to create files on disk are removed, and the\fBexec\fR command is removed, since it could be used to cause damagethrough subprocesses.Limited access to these facilities can be provided, by creatingaliases to the master interpreter which check their arguments carefullyand provide restricted access to a safe subset of facilities.For example, file creation might be allowed in a particular subdirectoryand subprocess invocation might be allowed for a carefully selected andfixed set of programs..PPA safe interpreter is created by specifying the \fB\-safe\fR switchto the \fBinterp create\fR command.  Furthermore, any slave createdby a safe interpreter will also be safe..PPA safe interpreter is created with exactly the following set ofbuilt-in commands:.DS.ta 1.2i 2.4i 3.6i\fBafter	append	array	binarybreak	case	catch	clockclose	concat	continue	eoferror	eval	expr	fblockedfcopy	fileevent	flush	forforeach	format	gets	globalif	incr	info	interpjoin	lappend	lindex	linsertlist	llength	lrange	lreplacelsearch	lsort	namespace	packagepid	proc	puts	readregexp	regsub	rename	returnscan	seek	set	splitstring	subst	switch	telltime	trace	unset	updateuplevel	upvar	variable	vwaitwhile\fR.DE.VS ""  BRThe following commands are hidden by \fBinterp create\fR when itcreates a safe interpreter:.DS.ta 1.2i 2.4i 3.6i\fBcd	encoding	exec	exitfconfigure file	glob	loadopen	pwd	socket	source\fR.DEThese commands can be recreated later as Tcl procedures or aliases, orre-exposed by \fBinterp expose\fR..PPThe following commands from Tcl's library of support procedures arenot present in a safe interpreter:.DS.ta 1.6i 3.2i\fBauto_exec_ok	auto_import	auto_loadauto_load_index	auto_qualify	unknown\fR.DENote in particular that safe interpreters have no default \fBunknown\fRcommand, so Tcl's default autoloading facilities are not available.  Autoload access to Tcl's commands that are normally autoloaded:.DS.ta 2.1i\fBauto_mkindex	auto_mkindex_oldauto_reset	historyparray	pkg_mkIndex::pkg::create	::safe::interpAddToAccessPath::safe::interpCreate	::safe::interpConfigure::safe::interpDelete	::safe::interpFindInAccessPath::safe::interpInit	::safe::setLogCmdtcl_endOfWord	tcl_findLibrarytcl_startOfNextWord	tcl_startOfPreviousWordtcl_wordBreakAfter	tcl_wordBreakBefore\fR.DEcan only be provided by explicit definition of an \fBunknown\fR commandin the safe interpreter.  This will involve exposing the \fBsource\fRcommand.  This is most easily accomplished by creating the safe interpreterwith Tcl's \fBSafe\-Tcl\fR mechanism.  \fBSafe\-Tcl\fR provides safeversions of \fBsource\fR, \fBload\fR, and other Tcl commands neededto support autoloading of commands and the loading of packages..VE.PPIn addition, the \fBenv\fR variable is not present in a safe interpreter,so it cannot share environment variables with other interpreters. The\fBenv\fR variable poses a security risk, because users can storesensitive information in an environment variable. For example, the PGPmanual recommends storing the PGP private key protection password inthe environment variable \fIPGPPASS\fR. Making this variable availableto untrusted code executing in a safe interpreter would incur asecurity risk..PPIf extensions are loaded into a safe interpreter, they may also restricttheir own functionality to eliminate unsafe commands. For a discussion ofmanagement of extensions for safety see the manual entries for\fBSafe\-Tcl\fR and the \fBload\fR Tcl command..PPA safe interpreter may not alter the recursion limit of any interpreter,including itself..SH "ALIAS INVOCATION".PPThe alias mechanism has been carefully designed so that it canbe used safely when an untrusted script is executingin a safe slave and the target of the alias is a trustedmaster.  The most important thing in guaranteeing safety is toensure that information passed from the slave to the master isnever evaluated or substituted in the master;  if this were tooccur, it would enable an evil script in the slave to invokearbitrary functions in the master, which would compromise security..PPWhen the source for an alias is invoked in the slave interpreter, theusual Tcl substitutions are performed when parsing that command.These substitutions are carried out in the source interpreter justas they would be for any other command invoked in that interpreter.The command procedure for the source command takes its argumentsand merges them with the \fItargetCmd\fR and \fIarg\fRs for thealias to create a new array of arguments.  If the wordsof \fIsrcCmd\fR were ``\fIsrcCmd arg1 arg2 ... argN\fR'',the new set of words will be``\fItargetCmd arg arg ... arg arg1 arg2 ... argN\fR'',where \fItargetCmd\fR and \fIarg\fRs are the values supplied when thealias was created.  \fITargetCmd\fR is then used to locate a commandprocedure in the target interpreter, and that command procedureis invoked with the new set of arguments.  An error occurs ifthere is no command named \fItargetCmd\fR in the target interpreter.No additional substitutions are performed on the words:  thetarget command procedure is invoked directly, withoutgoing through the normal Tcl evaluation mechanism.Substitutions are thus performed on each word exactly once:\fItargetCmd\fR and \fIargs\fR were substituted when parsing the commandthat created the alias, and \fIarg1 - argN\fR are substituted whenthe alias's source command is parsed in the source interpreter..PPWhen writing the \fItargetCmd\fRs for aliases in safe interpreters,it is very important that the arguments to that command never beevaluated or substituted, since this would provide an escapemechanism whereby the slave interpreter could execute arbitrarycode in the master.  This in turn would compromise the securityof the system..VS.SH "HIDDEN COMMANDS".PPSafe interpreters greatly restrict the functionality available to Tclprograms executing within them.Allowing the untrusted Tcl program to have direct access to thisfunctionality is unsafe, because it can be used for a variety ofattacks on the environment.However, there are times when there is a legitimate need to use thedangerous functionality in the context of the safe interpreter. Forexample, sometimes a program must be \fBsource\fRd into the interpreter.Another example is Tk, where windows are bound to the hierarchy of windowsfor a specific interpreter; some potentially dangerous functions, e.g.window management, must be performed on these windows within theinterpreter context..PPThe \fBinterp\fR command provides a solution to this problem in the form of\fIhidden commands\fR. Instead of removing the dangerous commands entirelyfrom a safe interpreter, these commands are hidden so they becomeunavailable to Tcl scripts executing in the interpreter. However, suchhidden commands can be invoked by any trusted ancestor of the safeinterpreter, in the context of the safe interpreter, using \fBinterpinvoke\fR. Hidden commands and exposed commands reside in separate namespaces. It is possible to define a hidden command and an exposed command bythe same name within one interpreter..PPHidden commands in a slave interpreter can be invoked in the body ofprocedures called in the master during alias invocation. For example, analias for \fBsource\fR could be created in a slave interpreter. When it isinvoked in the slave interpreter, a procedure is called in the masterinterpreter to check that the operation is allowable (e.g. it asks tosource a file that the slave interpreter is allowed to access). Theprocedure then it invokes the hidden \fBsource\fR command in the slaveinterpreter to actually source in the contents of the file. Note that twocommands named \fBsource\fR exist in the slave interpreter: the alias, andthe hidden command..PPBecause a master interpreter may invoke a hidden command as part ofhandling an alias invocation, great care must be taken to avoid evaluatingany arguments passed in through the alias invocation.Otherwise, malicious slave interpreters could cause a trusted masterinterpreter to execute dangerous commands on their behalf. See the sectionon ALIAS INVOCATION for a more complete discussion of this topic.To help avoid this problem, no substitutions or evaluations areapplied to arguments of \fBinterp invokehidden\fR..PPSafe interpreters are not allowed to invoke hidden commands in themselvesor in their descendants. This prevents safe slaves from gaining access tohidden functionality in themselves or their descendants..PPThe set of hidden commands in an interpreter can be manipulated by a trustedinterpreter using \fBinterp expose\fR and \fBinterp hide\fR. The \fBinterpexpose\fR command moves a hidden command to theset of exposed commands in the interpreter identified by \fIpath\fR,potentially renaming the command in the process. If an exposed command bythe targeted name already exists, the operation fails. Similarly,\fBinterp hide\fR moves an exposed command to the set of hidden commands inthat interpreter. Safe interpreters are not allowed to move commandsbetween the set of hidden and exposed commands, in either themselves ortheir descendants..PPCurrently, the names of hidden commands cannot contain namespacequalifiers, and you must first rename a command in a namespace to theglobal namespace before you can hide it.Commands to be hidden by \fBinterp hide\fR are looked up in the globalnamespace even if the current namespace is not the global one. Thisprevents slaves from fooling a master interpreter into hiding the wrongcommand, by making the current namespace be different from the global one..VE.SH CREDITS.PPThis mechanism is based on the Safe-Tcl prototype implementedby Nathaniel Borenstein and Marshall Rose..SH "SEE ALSO"load(n), safe(n), Tcl_CreateSlave(3).SH KEYWORDSalias, master interpreter, safe interpreter, slave interpreter

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?