home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / tcltk / tcl8.5 / safe.tcl < prev    next >
Encoding:
Text File  |  2009-11-22  |  30.3 KB  |  1,056 lines

  1. # safe.tcl --
  2. #
  3. # This file provide a safe loading/sourcing mechanism for safe interpreters.
  4. # It implements a virtual path mecanism to hide the real pathnames from the
  5. # slave. It runs in a master interpreter and sets up data structure and
  6. # aliases that will be invoked when used from a slave interpreter.
  7. # See the safe.n man page for details.
  8. #
  9. # Copyright (c) 1996-1997 Sun Microsystems, Inc.
  10. #
  11. # See the file "license.terms" for information on usage and redistribution
  12. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  13. #
  14. # RCS: @(#) $Id: safe.tcl,v 1.16.4.2 2009/11/04 04:47:59 dgp Exp $
  15.  
  16. #
  17. # The implementation is based on namespaces. These naming conventions
  18. # are followed:
  19. # Private procs starts with uppercase.
  20. # Public  procs are exported and starts with lowercase
  21. #
  22.  
  23. # Needed utilities package
  24. package require opt 0.4.1;
  25.  
  26. # Create the safe namespace
  27. namespace eval ::safe {
  28.  
  29.     # Exported API:
  30.     namespace export interpCreate interpInit interpConfigure interpDelete \
  31.         interpAddToAccessPath interpFindInAccessPath setLogCmd
  32.  
  33.     ####
  34.     #
  35.     # Setup the arguments parsing
  36.     #
  37.     ####
  38.  
  39.     # Make sure that our temporary variable is local to this
  40.     # namespace.  [Bug 981733]
  41.     variable temp
  42.  
  43.     # Share the descriptions
  44.     set temp [::tcl::OptKeyRegister {
  45.     {-accessPath -list {} "access path for the slave"}
  46.     {-noStatics "prevent loading of statically linked pkgs"}
  47.     {-statics true "loading of statically linked pkgs"}
  48.     {-nestedLoadOk "allow nested loading"}
  49.     {-nested false "nested loading"}
  50.     {-deleteHook -script {} "delete hook"}
  51.     }]
  52.  
  53.     # create case (slave is optional)
  54.     ::tcl::OptKeyRegister {
  55.     {?slave? -name {} "name of the slave (optional)"}
  56.     } ::safe::interpCreate
  57.     # adding the flags sub programs to the command program
  58.     # (relying on Opt's internal implementation details)
  59.     lappend ::tcl::OptDesc(::safe::interpCreate) $::tcl::OptDesc($temp)
  60.  
  61.     # init and configure (slave is needed)
  62.     ::tcl::OptKeyRegister {
  63.     {slave -name {} "name of the slave"}
  64.     } ::safe::interpIC
  65.     # adding the flags sub programs to the command program
  66.     # (relying on Opt's internal implementation details)
  67.     lappend ::tcl::OptDesc(::safe::interpIC) $::tcl::OptDesc($temp)
  68.     # temp not needed anymore
  69.     ::tcl::OptKeyDelete $temp
  70.  
  71.  
  72.     # Helper function to resolve the dual way of specifying staticsok
  73.     # (either by -noStatics or -statics 0)
  74.     proc InterpStatics {} {
  75.     foreach v {Args statics noStatics} {
  76.         upvar $v $v
  77.     }
  78.     set flag [::tcl::OptProcArgGiven -noStatics];
  79.     if {$flag && (!$noStatics == !$statics) 
  80.               && ([::tcl::OptProcArgGiven -statics])} {
  81.         return -code error\
  82.             "conflicting values given for -statics and -noStatics"
  83.     }
  84.     if {$flag} {
  85.         return [expr {!$noStatics}]
  86.     } else {
  87.         return $statics
  88.     }
  89.     }
  90.  
  91.     # Helper function to resolve the dual way of specifying nested loading
  92.     # (either by -nestedLoadOk or -nested 1)
  93.     proc InterpNested {} {
  94.     foreach v {Args nested nestedLoadOk} {
  95.         upvar $v $v
  96.     }
  97.     set flag [::tcl::OptProcArgGiven -nestedLoadOk];
  98.     # note that the test here is the opposite of the "InterpStatics"
  99.     # one (it is not -noNested... because of the wanted default value)
  100.     if {$flag && (!$nestedLoadOk != !$nested) 
  101.               && ([::tcl::OptProcArgGiven -nested])} {
  102.         return -code error\
  103.             "conflicting values given for -nested and -nestedLoadOk"
  104.     }
  105.     if {$flag} {
  106.         # another difference with "InterpStatics"
  107.         return $nestedLoadOk
  108.     } else {
  109.         return $nested
  110.     }
  111.     }
  112.  
  113.     ####
  114.     #
  115.     #  API entry points that needs argument parsing :
  116.     #
  117.     ####
  118.  
  119.  
  120.     # Interface/entry point function and front end for "Create"
  121.     proc interpCreate {args} {
  122.     set Args [::tcl::OptKeyParse ::safe::interpCreate $args]
  123.     InterpCreate $slave $accessPath \
  124.         [InterpStatics] [InterpNested] $deleteHook
  125.     }
  126.  
  127.     proc interpInit {args} {
  128.     set Args [::tcl::OptKeyParse ::safe::interpIC $args]
  129.     if {![::interp exists $slave]} {
  130.         return -code error "\"$slave\" is not an interpreter"
  131.     }
  132.     InterpInit $slave $accessPath \
  133.         [InterpStatics] [InterpNested] $deleteHook;
  134.     }
  135.  
  136.     proc CheckInterp {slave} {
  137.     if {![IsInterp $slave]} {
  138.         return -code error \
  139.             "\"$slave\" is not an interpreter managed by ::safe::"
  140.     }
  141.     }
  142.  
  143.     # Interface/entry point function and front end for "Configure"
  144.     # This code is awfully pedestrian because it would need
  145.     # more coupling and support between the way we store the
  146.     # configuration values in safe::interp's and the Opt package
  147.     # Obviously we would like an OptConfigure
  148.     # to avoid duplicating all this code everywhere. -> TODO
  149.     # (the app should share or access easily the program/value
  150.     #  stored by opt)
  151.     # This is even more complicated by the boolean flags with no values
  152.     # that we had the bad idea to support for the sake of user simplicity
  153.     # in create/init but which makes life hard in configure...
  154.     # So this will be hopefully written and some integrated with opt1.0
  155.     # (hopefully for tcl8.1 ?)
  156.     proc interpConfigure {args} {
  157.     switch [llength $args] {
  158.         1 {
  159.         # If we have exactly 1 argument
  160.         # the semantic is to return all the current configuration
  161.         # We still call OptKeyParse though we know that "slave"
  162.         # is our given argument because it also checks
  163.         # for the "-help" option.
  164.         set Args [::tcl::OptKeyParse ::safe::interpIC $args]
  165.         CheckInterp $slave
  166.         set res {}
  167.         lappend res [list -accessPath [Set [PathListName $slave]]]
  168.         lappend res [list -statics    [Set [StaticsOkName $slave]]]
  169.         lappend res [list -nested     [Set [NestedOkName $slave]]]
  170.         lappend res [list -deleteHook [Set [DeleteHookName $slave]]]
  171.         join $res
  172.         }
  173.         2 {
  174.         # If we have exactly 2 arguments
  175.         # the semantic is a "configure get"
  176.         ::tcl::Lassign $args slave arg
  177.         # get the flag sub program (we 'know' about Opt's internal
  178.         # representation of data)
  179.         set desc [lindex [::tcl::OptKeyGetDesc ::safe::interpIC] 2]
  180.         set hits [::tcl::OptHits desc $arg]
  181.                 if {$hits > 1} {
  182.                     return -code error [::tcl::OptAmbigous $desc $arg]
  183.                 } elseif {$hits == 0} {
  184.                     return -code error [::tcl::OptFlagUsage $desc $arg]
  185.                 }
  186.         CheckInterp $slave
  187.         set item [::tcl::OptCurDesc $desc]
  188.         set name [::tcl::OptName $item]
  189.         switch -exact -- $name {
  190.             -accessPath {
  191.             return [list -accessPath [Set [PathListName $slave]]]
  192.             }
  193.             -statics {
  194.             return [list -statics    [Set [StaticsOkName $slave]]]
  195.             }
  196.             -nested {
  197.             return [list -nested     [Set [NestedOkName $slave]]]
  198.             }
  199.             -deleteHook {
  200.             return [list -deleteHook [Set [DeleteHookName $slave]]]
  201.             }
  202.             -noStatics {
  203.             # it is most probably a set in fact
  204.             # but we would need then to jump to the set part
  205.             # and it is not *sure* that it is a set action
  206.             # that the user want, so force it to use the
  207.             # unambigous -statics ?value? instead:
  208.             return -code error\
  209.                 "ambigous query (get or set -noStatics ?)\
  210.                 use -statics instead"
  211.             }
  212.             -nestedLoadOk {
  213.             return -code error\
  214.                 "ambigous query (get or set -nestedLoadOk ?)\
  215.                 use -nested instead"
  216.             }
  217.             default {
  218.             return -code error "unknown flag $name (bug)"
  219.             }
  220.         }
  221.         }
  222.         default {
  223.         # Otherwise we want to parse the arguments like init and create
  224.         # did
  225.         set Args [::tcl::OptKeyParse ::safe::interpIC $args]
  226.         CheckInterp $slave
  227.         # Get the current (and not the default) values of
  228.         # whatever has not been given:
  229.         if {![::tcl::OptProcArgGiven -accessPath]} {
  230.             set doreset 1
  231.             set accessPath [Set [PathListName $slave]]
  232.         } else {
  233.             set doreset 0
  234.         }
  235.         if {(![::tcl::OptProcArgGiven -statics]) \
  236.             && (![::tcl::OptProcArgGiven -noStatics]) } {
  237.             set statics    [Set [StaticsOkName $slave]]
  238.         } else {
  239.             set statics    [InterpStatics]
  240.         }
  241.         if {([::tcl::OptProcArgGiven -nested]) \
  242.             || ([::tcl::OptProcArgGiven -nestedLoadOk]) } {
  243.             set nested     [InterpNested]
  244.         } else {
  245.             set nested     [Set [NestedOkName $slave]]
  246.         }
  247.         if {![::tcl::OptProcArgGiven -deleteHook]} {
  248.             set deleteHook [Set [DeleteHookName $slave]]
  249.         }
  250.         # we can now reconfigure :
  251.         InterpSetConfig $slave $accessPath $statics $nested $deleteHook
  252.         # auto_reset the slave (to completly synch the new access_path)
  253.         if {$doreset} {
  254.             if {[catch {::interp eval $slave {auto_reset}} msg]} {
  255.             Log $slave "auto_reset failed: $msg"
  256.             } else {
  257.             Log $slave "successful auto_reset" NOTICE
  258.             }
  259.         }
  260.         }
  261.     }
  262.     }
  263.  
  264.  
  265.     ####
  266.     #
  267.     #  Functions that actually implements the exported APIs
  268.     #
  269.     ####
  270.  
  271.  
  272.     #
  273.     # safe::InterpCreate : doing the real job
  274.     #
  275.     # This procedure creates a safe slave and initializes it with the
  276.     # safe base aliases.
  277.     # NB: slave name must be simple alphanumeric string, no spaces,
  278.     # no (), no {},...  {because the state array is stored as part of the name}
  279.     #
  280.     # Returns the slave name.
  281.     #
  282.     # Optional Arguments : 
  283.     # + slave name : if empty, generated name will be used
  284.     # + access_path: path list controlling where load/source can occur,
  285.     #                if empty: the master auto_path will be used.
  286.     # + staticsok  : flag, if 0 :no static package can be loaded (load {} Xxx)
  287.     #                      if 1 :static packages are ok.
  288.     # + nestedok: flag, if 0 :no loading to sub-sub interps (load xx xx sub)
  289.     #                      if 1 : multiple levels are ok.
  290.     
  291.     # use the full name and no indent so auto_mkIndex can find us
  292.     proc ::safe::InterpCreate {
  293.     slave 
  294.     access_path
  295.     staticsok
  296.     nestedok
  297.     deletehook
  298.     } {
  299.     # Create the slave.
  300.     if {$slave ne ""} {
  301.         ::interp create -safe $slave
  302.     } else {
  303.         # empty argument: generate slave name
  304.         set slave [::interp create -safe]
  305.     }
  306.     Log $slave "Created" NOTICE
  307.  
  308.     # Initialize it. (returns slave name)
  309.     InterpInit $slave $access_path $staticsok $nestedok $deletehook
  310.     }
  311.  
  312.  
  313.     #
  314.     # InterpSetConfig (was setAccessPath) :
  315.     #    Sets up slave virtual auto_path and corresponding structure
  316.     #    within the master. Also sets the tcl_library in the slave
  317.     #    to be the first directory in the path.
  318.     #    Nb: If you change the path after the slave has been initialized
  319.     #    you probably need to call "auto_reset" in the slave in order that it
  320.     #    gets the right auto_index() array values.
  321.  
  322.     proc ::safe::InterpSetConfig {slave access_path staticsok\
  323.         nestedok deletehook} {
  324.  
  325.     # determine and store the access path if empty
  326.     if {$access_path eq ""} {
  327.         set access_path [uplevel \#0 set auto_path]
  328.         # Make sure that tcl_library is in auto_path
  329.         # and at the first position (needed by setAccessPath)
  330.         set where [lsearch -exact $access_path [info library]]
  331.         if {$where == -1} {
  332.         # not found, add it.
  333.         set access_path [concat [list [info library]] $access_path]
  334.         Log $slave "tcl_library was not in auto_path,\
  335.             added it to slave's access_path" NOTICE
  336.         } elseif {$where != 0} {
  337.         # not first, move it first
  338.         set access_path [concat [list [info library]]\
  339.             [lreplace $access_path $where $where]]
  340.         Log $slave "tcl_libray was not in first in auto_path,\
  341.             moved it to front of slave's access_path" NOTICE
  342.         
  343.         }
  344.  
  345.         # Add 1st level sub dirs (will searched by auto loading from tcl
  346.         # code in the slave using glob and thus fail, so we add them
  347.         # here so by default it works the same).
  348.         set access_path [AddSubDirs $access_path]
  349.     }
  350.  
  351.     Log $slave "Setting accessPath=($access_path) staticsok=$staticsok\
  352.         nestedok=$nestedok deletehook=($deletehook)" NOTICE
  353.  
  354.     # clear old autopath if it existed
  355.     set nname [PathNumberName $slave]
  356.     if {[Exists $nname]} {
  357.         set n [Set $nname]
  358.         for {set i 0} {$i<$n} {incr i} {
  359.         Unset [PathToken $i $slave]
  360.         }
  361.     }
  362.  
  363.     # build new one
  364.     set slave_auto_path {}
  365.     set i 0
  366.     foreach dir $access_path {
  367.         Set [PathToken $i $slave] $dir
  368.         lappend slave_auto_path "\$[PathToken $i]"
  369.         incr i
  370.     }
  371.     # Extend the access list with the paths used to look for Tcl
  372.     # Modules. We safe the virtual form separately as well, as
  373.     # syncing it with the slave has to be defered until the
  374.     # necessary commands are present for setup.
  375.  
  376.     set morepaths [::tcl::tm::list]
  377.     while {[llength $morepaths]} {
  378.         set addpaths $morepaths
  379.         set morepaths {}
  380.  
  381.         foreach dir $addpaths {
  382.         lappend access_path $dir
  383.         Set [PathToken $i $slave] $dir
  384.         lappend slave_auto_path "\$[PathToken $i]"
  385.         lappend slave_tm_path   "\$[PathToken $i]"
  386.         incr i
  387.  
  388.         # [Bug 2854929]
  389.         # Recursively find deeper paths which may contain
  390.         # modules. Required to handle modules with names like
  391.         # 'platform::shell', which translate into
  392.         # 'platform/shell-X.tm', i.e arbitrarily deep
  393.         # subdirectories. The catch prevents complaints when
  394.         # no paths are added. Do nothing gracefully is 8.6+.
  395.  
  396.         catch {
  397.             lappend morepaths {*}[glob -nocomplain -directory $dir -type d *]
  398.     }
  399.         }
  400.     }
  401.  
  402.     Set [TmPathListName      $slave] $slave_tm_path
  403.     Set $nname $i
  404.     Set [PathListName        $slave] $access_path
  405.     Set [VirtualPathListName $slave] $slave_auto_path
  406.  
  407.     Set [StaticsOkName  $slave] $staticsok
  408.     Set [NestedOkName   $slave] $nestedok
  409.     Set [DeleteHookName $slave] $deletehook
  410.  
  411.     SyncAccessPath $slave
  412.     }
  413.  
  414.     #
  415.     #
  416.     # FindInAccessPath:
  417.     #    Search for a real directory and returns its virtual Id
  418.     #    (including the "$")
  419. proc ::safe::interpFindInAccessPath {slave path} {
  420.     set access_path [GetAccessPath $slave]
  421.     set where [lsearch -exact $access_path $path]
  422.     if {$where == -1} {
  423.         return -code error "$path not found in access path $access_path"
  424.     }
  425.     return "\$[PathToken $where]"
  426.     }
  427.  
  428.     #
  429.     # addToAccessPath:
  430.     #    add (if needed) a real directory to access path
  431.     #    and return its virtual token (including the "$").
  432. proc ::safe::interpAddToAccessPath {slave path} {
  433.     # first check if the directory is already in there
  434.     if {![catch {interpFindInAccessPath $slave $path} res]} {
  435.         return $res
  436.     }
  437.     # new one, add it:
  438.     set nname [PathNumberName $slave]
  439.     set n [Set $nname]
  440.     Set [PathToken $n $slave] $path
  441.  
  442.     set token "\$[PathToken $n]"
  443.  
  444.     Lappend [VirtualPathListName $slave] $token
  445.     Lappend [PathListName $slave] $path
  446.     Set $nname [expr {$n+1}]
  447.  
  448.     SyncAccessPath $slave
  449.  
  450.     return $token
  451.     }
  452.  
  453.     # This procedure applies the initializations to an already existing
  454.     # interpreter. It is useful when you want to install the safe base
  455.     # aliases into a preexisting safe interpreter.
  456.     proc ::safe::InterpInit {
  457.     slave 
  458.     access_path
  459.     staticsok
  460.     nestedok
  461.     deletehook
  462.     } {
  463.  
  464.     # Configure will generate an access_path when access_path is
  465.     # empty.
  466.     InterpSetConfig $slave $access_path $staticsok $nestedok $deletehook
  467.  
  468.     # These aliases let the slave load files to define new commands
  469.  
  470.     # NB we need to add [namespace current], aliases are always
  471.     # absolute paths.
  472.     ::interp alias $slave source {} [namespace current]::AliasSource $slave
  473.     ::interp alias $slave load   {} [namespace current]::AliasLoad $slave
  474.  
  475.     # This alias lets the slave use the encoding names, convertfrom,
  476.     # convertto, and system, but not "encoding system <name>" to set
  477.     # the system encoding.
  478.  
  479.     ::interp alias $slave encoding {} [namespace current]::AliasEncoding \
  480.         $slave
  481.  
  482.     # Handling Tcl Modules, we need a restricted form of Glob.
  483.     ::interp alias $slave glob {} [namespace current]::AliasGlob \
  484.         $slave
  485.  
  486.     # This alias lets the slave have access to a subset of the 'file'
  487.     # command functionality.
  488.  
  489.     AliasSubset $slave file file dir.* join root.* ext.* tail \
  490.         path.* split
  491.  
  492.     # This alias interposes on the 'exit' command and cleanly terminates
  493.     # the slave.
  494.  
  495.     ::interp alias $slave exit {} [namespace current]::interpDelete $slave
  496.  
  497.     # The allowed slave variables already have been set
  498.     # by Tcl_MakeSafe(3)
  499.  
  500.  
  501.     # Source init.tcl and tm.tcl into the slave, to get auto_load
  502.     # and other procedures defined:
  503.  
  504.     if {[catch {::interp eval $slave \
  505.         {source [file join $tcl_library init.tcl]}} msg]} {
  506.         Log $slave "can't source init.tcl ($msg)"
  507.         error "can't source init.tcl into slave $slave ($msg)"
  508.     }
  509.  
  510.     if {[catch {::interp eval $slave \
  511.         {source [file join $tcl_library tm.tcl]}} msg]} {
  512.         Log $slave "can't source tm.tcl ($msg)"
  513.         error "can't source tm.tcl into slave $slave ($msg)"
  514.     }
  515.  
  516.     # Sync the paths used to search for Tcl modules. This can be
  517.     # done only now, after tm.tcl was loaded.
  518.     ::interp eval $slave [list ::tcl::tm::add {*}[Set [TmPathListName $slave]]]
  519.  
  520.     return $slave
  521.     }
  522.  
  523.  
  524.     # Add (only if needed, avoid duplicates) 1 level of
  525.     # sub directories to an existing path list.
  526.     # Also removes non directories from the returned list.
  527.     proc AddSubDirs {pathList} {
  528.     set res {}
  529.     foreach dir $pathList {
  530.         if {[file isdirectory $dir]} {
  531.         # check that we don't have it yet as a children
  532.         # of a previous dir
  533.         if {[lsearch -exact $res $dir]<0} {
  534.             lappend res $dir
  535.         }
  536.         foreach sub [glob -directory $dir -nocomplain *] {
  537.             if {([file isdirectory $sub]) \
  538.                 && ([lsearch -exact $res $sub]<0) } {
  539.             # new sub dir, add it !
  540.                     lappend res $sub
  541.                 }
  542.         }
  543.         }
  544.     }
  545.     return $res
  546.     }
  547.  
  548.     # This procedure deletes a safe slave managed by Safe Tcl and
  549.     # cleans up associated state:
  550.  
  551. proc ::safe::interpDelete {slave} {
  552.  
  553.         Log $slave "About to delete" NOTICE
  554.  
  555.     # If the slave has a cleanup hook registered, call it.
  556.     # check the existance because we might be called to delete an interp
  557.     # which has not been registered with us at all
  558.     set hookname [DeleteHookName $slave]
  559.     if {[Exists $hookname]} {
  560.         set hook [Set $hookname]
  561.         if {![::tcl::Lempty $hook]} {
  562.         # remove the hook now, otherwise if the hook
  563.         # calls us somehow, we'll loop
  564.         Unset $hookname
  565.         if {[catch {{*}$hook $slave} err]} {
  566.             Log $slave "Delete hook error ($err)"
  567.         }
  568.         }
  569.     }
  570.  
  571.     # Discard the global array of state associated with the slave, and
  572.     # delete the interpreter.
  573.  
  574.     set statename [InterpStateName $slave]
  575.     if {[Exists $statename]} {
  576.         Unset $statename
  577.     }
  578.  
  579.     # if we have been called twice, the interp might have been deleted
  580.     # already
  581.     if {[::interp exists $slave]} {
  582.         ::interp delete $slave
  583.         Log $slave "Deleted" NOTICE
  584.     }
  585.  
  586.     return
  587.     }
  588.  
  589.     # Set (or get) the loging mecanism 
  590.  
  591. proc ::safe::setLogCmd {args} {
  592.     variable Log
  593.     if {[llength $args] == 0} {
  594.     return $Log
  595.     } else {
  596.     if {[llength $args] == 1} {
  597.         set Log [lindex $args 0]
  598.     } else {
  599.         set Log $args
  600.     }
  601.     }
  602. }
  603.  
  604.     # internal variable
  605.     variable Log {}
  606.  
  607.     # ------------------- END OF PUBLIC METHODS ------------
  608.  
  609.  
  610.     #
  611.     # sets the slave auto_path to the master recorded value.
  612.     # also sets tcl_library to the first token of the virtual path.
  613.     #
  614.     proc SyncAccessPath {slave} {
  615.     set slave_auto_path [Set [VirtualPathListName $slave]]
  616.     ::interp eval $slave [list set auto_path $slave_auto_path]
  617.     Log $slave "auto_path in $slave has been set to $slave_auto_path"\
  618.         NOTICE
  619.     ::interp eval $slave [list set tcl_library [lindex $slave_auto_path 0]]
  620.     }
  621.  
  622.     # base name for storing all the slave states
  623.     # the array variable name for slave foo is thus "Sfoo"
  624.     # and for sub slave {foo bar} "Sfoo bar" (spaces are handled
  625.     # ok everywhere (or should))
  626.     # We add the S prefix to avoid that a slave interp called "Log"
  627.     # would smash our "Log" variable.
  628.     proc InterpStateName {slave} {
  629.     return "S$slave"
  630.     }
  631.  
  632.     # Check that the given slave is "one of us"
  633.     proc IsInterp {slave} {
  634.     expr {[Exists [InterpStateName $slave]] && [::interp exists $slave]}
  635.     }
  636.  
  637.     # returns the virtual token for directory number N
  638.     # if the slave argument is given, 
  639.     # it will return the corresponding master global variable name
  640.     proc PathToken {n {slave ""}} {
  641.     if {$slave ne ""} {
  642.         return "[InterpStateName $slave](access_path,$n)"
  643.     } else {
  644.         # We need to have a ":" in the token string so
  645.         # [file join] on the mac won't turn it into a relative
  646.         # path.
  647.         return "p(:$n:)"
  648.     }
  649.     }
  650.     # returns the variable name of the complete path list
  651.     proc PathListName {slave} {
  652.     return "[InterpStateName $slave](access_path)"
  653.     }
  654.     # returns the variable name of the complete path list
  655.     proc VirtualPathListName {slave} {
  656.     return "[InterpStateName $slave](access_path_slave)"
  657.     }
  658.     # returns the variable name of the complete tm path list
  659.     proc TmPathListName {slave} {
  660.     return "[InterpStateName $slave](tm_path_slave)"
  661.     }
  662.     # returns the variable name of the number of items
  663.     proc PathNumberName {slave} {
  664.     return "[InterpStateName $slave](access_path,n)"
  665.     }
  666.     # returns the staticsok flag var name
  667.     proc StaticsOkName {slave} {
  668.     return "[InterpStateName $slave](staticsok)"
  669.     }
  670.     # returns the nestedok flag var name
  671.     proc NestedOkName {slave} {
  672.     return "[InterpStateName $slave](nestedok)"
  673.     }
  674.     # Run some code at the namespace toplevel
  675.     proc Toplevel {args} {
  676.     namespace eval [namespace current] $args
  677.     }
  678.     # set/get values
  679.     proc Set {args} {
  680.     Toplevel set {*}$args
  681.     }
  682.     # lappend on toplevel vars
  683.     proc Lappend {args} {
  684.     Toplevel lappend {*}$args
  685.     }
  686.     # unset a var/token (currently just an global level eval)
  687.     proc Unset {args} {
  688.     Toplevel unset {*}$args
  689.     }
  690.     # test existance 
  691.     proc Exists {varname} {
  692.     Toplevel info exists $varname
  693.     }
  694.     # short cut for access path getting
  695.     proc GetAccessPath {slave} {
  696.     Set [PathListName $slave]
  697.     }
  698.     # short cut for statics ok flag getting
  699.     proc StaticsOk {slave} {
  700.     Set [StaticsOkName $slave]
  701.     }
  702.     # short cut for getting the multiples interps sub loading ok flag
  703.     proc NestedOk {slave} {
  704.     Set [NestedOkName $slave]
  705.     }
  706.     # interp deletion storing hook name
  707.     proc DeleteHookName {slave} {
  708.     return [InterpStateName $slave](cleanupHook)
  709.     }
  710.  
  711.     #
  712.     # translate virtual path into real path
  713.     #
  714.     proc TranslatePath {slave path} {
  715.     # somehow strip the namespaces 'functionality' out (the danger
  716.     # is that we would strip valid macintosh "../" queries... :
  717.     if {[string match "*::*" $path] || [string match "*..*" $path]} {
  718.         error "invalid characters in path $path"
  719.     }
  720.     set n [expr {[Set [PathNumberName $slave]]-1}]
  721.     for {} {$n>=0} {incr n -1} {
  722.         # fill the token virtual names with their real value
  723.         set [PathToken $n] [Set [PathToken $n $slave]]
  724.     }
  725.     # replaces the token by their value
  726.     subst -nobackslashes -nocommands $path
  727.     }
  728.  
  729.  
  730.     # Log eventually log an error
  731.     # to enable error logging, set Log to {puts stderr} for instance
  732.     proc Log {slave msg {type ERROR}} {
  733.     variable Log
  734.     if {[info exists Log] && [llength $Log]} {
  735.         {*}$Log "$type for slave $slave : $msg"
  736.     }
  737.     }
  738.  
  739.  
  740.     # file name control (limit access to files/ressources that should be
  741.     # a valid tcl source file)
  742.     proc CheckFileName {slave file} {
  743.     # This used to limit what can be sourced to ".tcl" and forbid files
  744.     # with more than 1 dot and longer than 14 chars, but I changed that
  745.     # for 8.4 as a safe interp has enough internal protection already
  746.     # to allow sourcing anything. - hobbs
  747.  
  748.     if {![file exists $file]} {
  749.         # don't tell the file path
  750.         error "no such file or directory"
  751.     }
  752.  
  753.     if {![file readable $file]} {
  754.         # don't tell the file path
  755.         error "not readable"
  756.     }
  757.     }
  758.  
  759.     # AliasGlob is the target of the "glob" alias in safe interpreters.
  760.  
  761.     proc AliasGlob {slave args} {
  762.     Log $slave "GLOB ! $args" NOTICE
  763.     set cmd {}
  764.     set at 0
  765.  
  766.     set dir        {}
  767.     set virtualdir {}
  768.  
  769.     while {$at < [llength $args]} {
  770.         switch -glob -- [set opt [lindex $args $at]] {
  771.         -nocomplain -
  772.         -join       { lappend cmd $opt ; incr at }
  773.         -directory  {
  774.             lappend cmd $opt ; incr at
  775.             set virtualdir [lindex $args $at]
  776.  
  777.             # get the real path from the virtual one.
  778.             if {[catch {set dir [TranslatePath $slave $virtualdir]} msg]} {
  779.             Log $slave $msg
  780.             return -code error "permission denied"
  781.             }
  782.             # check that the path is in the access path of that slave
  783.             if {[catch {DirInAccessPath $slave $dir} msg]} {
  784.             Log $slave $msg
  785.             return -code error "permission denied"
  786.             }
  787.             lappend cmd $dir ; incr at
  788.         }
  789.         pkgIndex.tcl {
  790.             # Oops, this is globbing a subdirectory in regular
  791.             # package search. That is not wanted. Abort,
  792.             # handler does catch already (because glob was not
  793.             # defined before). See package.tcl, lines 484ff in
  794.             # tclPkgUnknown.
  795.             error "unknown command glob"
  796.         }
  797.         -* {
  798.             Log $slave "Safe base rejecting glob option '$opt'"
  799.             error      "Safe base rejecting glob option '$opt'"
  800.         }
  801.         default {
  802.             lappend cmd $opt ; incr at
  803.         }
  804.         }
  805.     }
  806.  
  807.     Log $slave "GLOB = $cmd" NOTICE
  808.  
  809.     if {[catch {::interp invokehidden $slave glob {*}$cmd} msg]} {
  810.         Log $slave $msg
  811.         return -code error "script error"
  812.     }
  813.  
  814.     Log $slave "GLOB @ $msg" NOTICE
  815.  
  816.     # Translate path back to what the slave should see.
  817.     set res {}
  818.     foreach p $msg {
  819.         regsub -- ^$dir $p $virtualdir p
  820.         lappend res $p
  821.     }
  822.  
  823.     Log $slave "GLOB @ $res" NOTICE
  824.     return $res
  825.     }
  826.  
  827.     # AliasSource is the target of the "source" alias in safe interpreters.
  828.  
  829.     proc AliasSource {slave args} {
  830.  
  831.     set argc [llength $args]
  832.     # Extended for handling of Tcl Modules to allow not only
  833.     # "source filename", but "source -encoding E filename" as
  834.     # well.
  835.     if {[lindex $args 0] eq "-encoding"} {
  836.         incr argc -2
  837.         set encoding [lrange $args 0 1]
  838.         set at 2
  839.     } else {
  840.         set at 0
  841.         set encoding {}
  842.     }
  843.     if {$argc != 1} {
  844.         set msg "wrong # args: should be \"source ?-encoding E? fileName\""
  845.         Log $slave "$msg ($args)"
  846.         return -code error $msg
  847.     }
  848.     set file [lindex $args $at]
  849.     
  850.     # get the real path from the virtual one.
  851.     if {[catch {set file [TranslatePath $slave $file]} msg]} {
  852.         Log $slave $msg
  853.         return -code error "permission denied"
  854.     }
  855.     
  856.     # check that the path is in the access path of that slave
  857.     if {[catch {FileInAccessPath $slave $file} msg]} {
  858.         Log $slave $msg
  859.         return -code error "permission denied"
  860.     }
  861.  
  862.     # do the checks on the filename :
  863.     if {[catch {CheckFileName $slave $file} msg]} {
  864.         Log $slave "$file:$msg"
  865.         return -code error $msg
  866.     }
  867.  
  868.     # passed all the tests , lets source it:
  869.     if {[catch {::interp invokehidden $slave source {*}$encoding $file} msg]} {
  870.         Log $slave $msg
  871.         return -code error "script error"
  872.     }
  873.     return $msg
  874.     }
  875.  
  876.     # AliasLoad is the target of the "load" alias in safe interpreters.
  877.  
  878.     proc AliasLoad {slave file args} {
  879.  
  880.     set argc [llength $args]
  881.     if {$argc > 2} {
  882.         set msg "load error: too many arguments"
  883.         Log $slave "$msg ($argc) {$file $args}"
  884.         return -code error $msg
  885.     }
  886.  
  887.     # package name (can be empty if file is not).
  888.     set package [lindex $args 0]
  889.  
  890.     # Determine where to load. load use a relative interp path
  891.     # and {} means self, so we can directly and safely use passed arg.
  892.     set target [lindex $args 1]
  893.     if {$target ne ""} {
  894.         # we will try to load into a sub sub interp
  895.         # check that we want to authorize that.
  896.         if {![NestedOk $slave]} {
  897.         Log $slave "loading to a sub interp (nestedok)\
  898.             disabled (trying to load $package to $target)"
  899.         return -code error "permission denied (nested load)"
  900.         }
  901.         
  902.     }
  903.  
  904.     # Determine what kind of load is requested
  905.     if {$file eq ""} {
  906.         # static package loading
  907.         if {$package eq ""} {
  908.         set msg "load error: empty filename and no package name"
  909.         Log $slave $msg
  910.         return -code error $msg
  911.         }
  912.         if {![StaticsOk $slave]} {
  913.         Log $slave "static packages loading disabled\
  914.             (trying to load $package to $target)"
  915.         return -code error "permission denied (static package)"
  916.         }
  917.     } else {
  918.         # file loading
  919.  
  920.         # get the real path from the virtual one.
  921.         if {[catch {set file [TranslatePath $slave $file]} msg]} {
  922.         Log $slave $msg
  923.         return -code error "permission denied"
  924.         }
  925.  
  926.         # check the translated path
  927.         if {[catch {FileInAccessPath $slave $file} msg]} {
  928.         Log $slave $msg
  929.         return -code error "permission denied (path)"
  930.         }
  931.     }
  932.  
  933.     if {[catch {::interp invokehidden\
  934.         $slave load $file $package $target} msg]} {
  935.         Log $slave $msg
  936.         return -code error $msg
  937.     }
  938.  
  939.     return $msg
  940.     }
  941.  
  942.     # FileInAccessPath raises an error if the file is not found in
  943.     # the list of directories contained in the (master side recorded) slave's
  944.     # access path.
  945.  
  946.     # the security here relies on "file dirname" answering the proper
  947.     # result.... needs checking ?
  948.     proc FileInAccessPath {slave file} {
  949.  
  950.     set access_path [GetAccessPath $slave]
  951.  
  952.     if {[file isdirectory $file]} {
  953.         error "\"$file\": is a directory"
  954.     }
  955.     set parent [file dirname $file]
  956.  
  957.     # Normalize paths for comparison since lsearch knows nothing of
  958.     # potential pathname anomalies.
  959.     set norm_parent [file normalize $parent]
  960.     foreach path $access_path {
  961.         lappend norm_access_path [file normalize $path]
  962.     }
  963.  
  964.     if {[lsearch -exact $norm_access_path $norm_parent] == -1} {
  965.         error "\"$file\": not in access_path"
  966.     }
  967.     }
  968.  
  969.     proc DirInAccessPath {slave dir} {
  970.     set access_path [GetAccessPath $slave]
  971.  
  972.     if {[file isfile $dir]} {
  973.         error "\"$dir\": is a file"
  974.     }
  975.  
  976.     # Normalize paths for comparison since lsearch knows nothing of
  977.     # potential pathname anomalies.
  978.     set norm_dir [file normalize $dir]
  979.     foreach path $access_path {
  980.         lappend norm_access_path [file normalize $path]
  981.     }
  982.  
  983.     if {[lsearch -exact $norm_access_path $norm_dir] == -1} {
  984.         error "\"$dir\": not in access_path"
  985.     }
  986.     }
  987.  
  988.     # This procedure enables access from a safe interpreter to only a subset of
  989.     # the subcommands of a command:
  990.  
  991.     proc Subset {slave command okpat args} {
  992.     set subcommand [lindex $args 0]
  993.     if {[regexp $okpat $subcommand]} {
  994.         return [$command {*}$args]
  995.     }
  996.     set msg "not allowed to invoke subcommand $subcommand of $command"
  997.     Log $slave $msg
  998.     error $msg
  999.     }
  1000.  
  1001.     # This procedure installs an alias in a slave that invokes "safesubset"
  1002.     # in the master to execute allowed subcommands. It precomputes the pattern
  1003.     # of allowed subcommands; you can use wildcards in the pattern if you wish
  1004.     # to allow subcommand abbreviation.
  1005.     #
  1006.     # Syntax is: AliasSubset slave alias target subcommand1 subcommand2...
  1007.  
  1008.     proc AliasSubset {slave alias target args} {
  1009.     set pat ^(; set sep ""
  1010.     foreach sub $args {
  1011.         append pat $sep$sub
  1012.         set sep |
  1013.     }
  1014.     append pat )\$
  1015.     ::interp alias $slave $alias {}\
  1016.         [namespace current]::Subset $slave $target $pat
  1017.     }
  1018.  
  1019.     # AliasEncoding is the target of the "encoding" alias in safe interpreters.
  1020.  
  1021.     proc AliasEncoding {slave args} {
  1022.  
  1023.     set argc [llength $args]
  1024.  
  1025.     set okpat "^(name.*|convert.*)\$"
  1026.     set subcommand [lindex $args 0]
  1027.  
  1028.     if {[regexp $okpat $subcommand]} {
  1029.         return [::interp invokehidden $slave encoding {*}$args]
  1030.     }
  1031.  
  1032.     if {[string first $subcommand system] == 0} {
  1033.         if {$argc == 1} {
  1034.         # passed all the tests , lets source it:
  1035.         if {[catch {::interp invokehidden \
  1036.             $slave encoding system} msg]} {
  1037.             Log $slave $msg
  1038.             return -code error "script error"
  1039.         }
  1040.         } else {
  1041.         set msg "wrong # args: should be \"encoding system\""
  1042.         Log $slave $msg
  1043.         error $msg
  1044.         }
  1045.     } else {
  1046.         set msg "wrong # args: should be \"encoding option ?arg ...?\""
  1047.         Log $slave $msg
  1048.         error $msg
  1049.     }
  1050.  
  1051.     return $msg
  1052.     }
  1053.  
  1054. }
  1055.