3 # Support procs to use Tk in safe interpreters.
5 # RCS: @(#) $Id: safetk.tcl,v 1.12 2008/03/27 21:05:09 hobbs Exp $
7 # Copyright (c) 1997 Sun Microsystems, Inc.
9 # See the file "license.terms" for information on usage and redistribution
10 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
12 # see safetk.n for documentation
16 # Note: It is now ok to let untrusted code being executed
17 # between the creation of the interp and the actual loading
18 # of Tk in that interp because the C side Tk_Init will
19 # now look up the master interp and ask its safe::TkInit
20 # for the actual parameters to use for it's initialization (if allowed),
21 # not relying on the slave state.
24 # We use opt (optional arguments parsing)
25 package require opt
0.4.1;
27 namespace eval ::safe {
29 # counter for safe toplevels
34 # tkInterpInit : prepare the slave interpreter for tk loading
35 # most of the real job is done by loadTk
36 # returns the slave name (tkInterpInit does)
38 proc ::safe::tkInterpInit {slave argv
} {
41 # We have to make sure that the tk_library variable is normalized.
42 set tk_library
[file normalize
$tk_library]
44 # Clear Tk's access for that interp (path).
47 # Ensure tk_library and subdirs (eg, ttk) are on the access path
48 ::interp eval $slave [list set tk_library
[::safe::interpAddToAccessPath $slave $tk_library]]
49 foreach subdir
[::safe::AddSubDirs [list $tk_library]] {
50 ::safe::interpAddToAccessPath $slave $subdir
57 # Do additional configuration as needed (calling tkInterpInit)
58 # and actually load Tk into the slave.
60 # Either contained in the specified windowId (-use) or
61 # creating a decorated toplevel for it.
63 # empty definition for auto_mkIndex
64 proc ::safe::loadTk {} {}
66 ::tcl::OptProc ::safe::loadTk {
67 {slave
-interp "name of the slave interpreter"}
68 {-use -windowId {} "window Id to use (new toplevel otherwise)"}
69 {-display -displayName {} "display name to use (current one otherwise)"}
71 set displayGiven
[::tcl::OptProcArgGiven "-display"]
73 # Try to get the current display from "."
74 # (which might not exist if the master is tk-less)
75 if {[catch {set display
[winfo screen .
]}]} {
76 if {[info exists
::env(DISPLAY
)]} {
77 set display
$::env(DISPLAY
)
79 Log
$slave "no winfo screen . nor env(DISPLAY)" WARNING
84 if {![::tcl::OptProcArgGiven "-use"]} {
85 # create a decorated toplevel
86 ::tcl::Lassign [tkTopLevel
$slave $display] w use
88 # set our delete hook (slave arg is added by interpDelete)
89 # to clean up both window related code and tkInit(slave)
90 Set
[DeleteHookName
$slave] [list tkDelete
{} $w]
94 # set our delete hook (slave arg is added by interpDelete)
95 # to clean up tkInit(slave)
96 Set
[DeleteHookName
$slave] [list disallowTk
]
98 # Let's be nice and also accept tk window names instead of ids
99 if {[string match
".*" $use]} {
101 set use
[winfo id
$windowName]
102 set nDisplay
[winfo screen
$windowName]
104 # Check for a better -display value
105 # (works only for multi screens on single host, but not
106 # cross hosts, for that a tk window name would be better
107 # but embeding is also usefull for non tk names)
108 if {![catch {winfo pathname
$use} name
]} {
109 set nDisplay
[winfo screen
$name]
111 # Can't have a better one
112 set nDisplay
$display
115 if {$nDisplay ne
$display} {
117 error "conflicting -display $display and -use\
120 set display
$nDisplay
125 # Prepares the slave for tk with those parameters
126 tkInterpInit
$slave [list "-use" $use "-display" $display]
133 proc ::safe::TkInit {interpPath
} {
135 if {[info exists tkInit
($interpPath)]} {
136 set value
$tkInit($interpPath)
137 Log
$interpPath "TkInit called, returning \"$value\"" NOTICE
140 Log
$interpPath "TkInit called for interp with clearance:\
141 preventing Tk init" ERROR
148 # Set tkInit(interpPath) to allow Tk to be initialized in
152 # interpPath slave interpreter handle
153 # argv arguments passed to safe::TkInterpInit
158 proc ::safe::allowTk {interpPath argv
} {
160 set tkInit
($interpPath) $argv
165 # safe::disallowTk --
167 # Unset tkInit(interpPath) to disallow Tk from getting initialized
171 # interpPath slave interpreter handle
176 proc ::safe::disallowTk {interpPath
} {
178 # This can already be deleted by the DeleteHook of the interp
179 if {[info exists tkInit
($interpPath)]} {
180 unset tkInit
($interpPath)
188 # Clean up the window associated with the interp being deleted.
191 # interpPath slave interpreter handle
196 proc ::safe::tkDelete {W window slave
} {
198 # we are going to be called for each widget... skip untill it's
201 Log
$slave "Called tkDelete $W $window" NOTICE
202 if {[::interp exists
$slave]} {
203 if {[catch {::safe::interpDelete $slave} msg
]} {
204 Log
$slave "Deletion error : $msg"
207 if {[winfo exists
$window]} {
208 Log
$slave "Destroy toplevel $window" NOTICE
212 # clean up tkInit(slave)
217 proc ::safe::tkTopLevel {slave display
} {
220 set w
".safe$tkSafeId"
221 if {[catch {toplevel $w -screen $display -class SafeTk
} msg
]} {
222 return -code error "Unable to create toplevel for\
223 safe slave \"$slave\" ($msg)"
225 Log
$slave "New toplevel $w" NOTICE
227 set msg
"Untrusted Tcl applet ($slave)"
230 # Control frame (we must create a style for it)
231 ttk
::style layout TWarningFrame
{WarningFrame.border
-sticky nswe
}
232 ttk
::style configure TWarningFrame
-background red
235 ttk
::frame $wc -relief ridge
-borderwidth 4 -style TWarningFrame
237 # We will destroy the interp when the window is destroyed
238 bindtags $wc [concat Safe$wc [bindtags $wc]]
239 bind Safe$wc <Destroy
> [list ::safe::tkDelete %W
$w $slave]
241 ttk
::label $wc.l
-text $msg -anchor w
243 # We want the button to be the last visible item
244 # (so be packed first) and at the right and not resizing horizontally
246 # frame the button so it does not expand horizontally
247 # but still have the default background instead of red one from the parent
248 ttk
::frame $wc.fb
-borderwidth 0
249 ttk
::button $wc.fb.b
-text "Delete" \
250 -command [list ::safe::tkDelete $w $w $slave]
251 pack $wc.fb.b
-side right
-fill both
252 pack $wc.fb
-side right
-fill both
-expand 1
253 pack $wc.l
-side left
-fill both
-expand 1 -ipady 2
254 pack $wc -side bottom
-fill x
257 frame $w.c
-container 1
258 pack $w.c
-fill both
-expand 1
260 # return both the toplevel window name and the id to use for embedding
261 list $w [winfo id
$w.c
]