src/Pure/General/secure.ML
author haftmann
Tue, 23 Sep 2008 18:11:44 +0200
changeset 28337 93964076e7b8
parent 28268 ac8431ecd57e
child 29606 fedb8be05f24
permissions -rw-r--r--
case default fallback for NBE
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
20925
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/General/secure.ML
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
     3
    Author:     Makarius
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
     4
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
     5
Secure critical operations.
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
     6
*)
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
     7
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
     8
signature SECURE =
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
     9
sig
20977
dbf1eca9b34e tuned signature;
wenzelm
parents: 20925
diff changeset
    10
  val set_secure: unit -> unit
20925
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
    11
  val is_secure: unit -> bool
26080
d920e4c8ba82 export deny_secure;
wenzelm
parents: 25753
diff changeset
    12
  val deny_secure: string -> unit
28268
ac8431ecd57e use_text/use_file now depend on explicit ML name space;
wenzelm
parents: 26883
diff changeset
    13
  val use_text: ML_NameSpace.nameSpace -> int * string ->
ac8431ecd57e use_text/use_file now depend on explicit ML name space;
wenzelm
parents: 26883
diff changeset
    14
    (string -> unit) * (string -> 'a) -> bool -> string -> unit
ac8431ecd57e use_text/use_file now depend on explicit ML name space;
wenzelm
parents: 26883
diff changeset
    15
  val use_file: ML_NameSpace.nameSpace ->
ac8431ecd57e use_text/use_file now depend on explicit ML name space;
wenzelm
parents: 26883
diff changeset
    16
    (string -> unit) * (string -> 'a) -> bool -> string -> unit
20925
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
    17
  val use: string -> unit
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
    18
  val commit: unit -> unit
26220
d34b68c21f9a common setup for system_out/system;
wenzelm
parents: 26080
diff changeset
    19
  val system_out: string -> string * int
20992
05c3703cc6c5 added execute/system;
wenzelm
parents: 20977
diff changeset
    20
  val system: string -> int
20925
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
    21
end;
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
    22
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
    23
structure Secure: SECURE =
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
    24
struct
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
    25
20992
05c3703cc6c5 added execute/system;
wenzelm
parents: 20977
diff changeset
    26
(** secure flag **)
20925
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
    27
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
    28
val secure = ref false;
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
    29
20977
dbf1eca9b34e tuned signature;
wenzelm
parents: 20925
diff changeset
    30
fun set_secure () = secure := true;
20925
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
    31
fun is_secure () = ! secure;
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
    32
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22144
diff changeset
    33
fun deny_secure msg = if is_secure () then error msg else ();
20925
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
    34
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
    35
25753
99c9fc5e11f2 tuned spaces;
wenzelm
parents: 25703
diff changeset
    36
20992
05c3703cc6c5 added execute/system;
wenzelm
parents: 20977
diff changeset
    37
(** critical operations **)
05c3703cc6c5 added execute/system;
wenzelm
parents: 20977
diff changeset
    38
05c3703cc6c5 added execute/system;
wenzelm
parents: 20977
diff changeset
    39
(* ML evaluation *)
20925
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
    40
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
    41
fun secure_mltext () = deny_secure "Cannot evaluate ML source in secure mode";
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
    42
28268
ac8431ecd57e use_text/use_file now depend on explicit ML name space;
wenzelm
parents: 26883
diff changeset
    43
fun raw_use_text ns = use_text ML_Parse.fix_ints (Position.str_of oo Position.line_file) ns;
ac8431ecd57e use_text/use_file now depend on explicit ML name space;
wenzelm
parents: 26883
diff changeset
    44
fun raw_use_file ns = use_file ML_Parse.fix_ints (Position.str_of oo Position.line_file) ns;
20925
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
    45
28268
ac8431ecd57e use_text/use_file now depend on explicit ML name space;
wenzelm
parents: 26883
diff changeset
    46
fun use_text ns pos pr verbose txt =
ac8431ecd57e use_text/use_file now depend on explicit ML name space;
wenzelm
parents: 26883
diff changeset
    47
  (secure_mltext (); raw_use_text ns pos pr verbose txt);
23922
707639e9497d marked some CRITICAL sections (for multithreading);
wenzelm
parents: 22567
diff changeset
    48
28268
ac8431ecd57e use_text/use_file now depend on explicit ML name space;
wenzelm
parents: 26883
diff changeset
    49
fun use_file ns pr verbose name =
ac8431ecd57e use_text/use_file now depend on explicit ML name space;
wenzelm
parents: 26883
diff changeset
    50
  (secure_mltext (); raw_use_file ns pr verbose name);
23922
707639e9497d marked some CRITICAL sections (for multithreading);
wenzelm
parents: 22567
diff changeset
    51
28268
ac8431ecd57e use_text/use_file now depend on explicit ML name space;
wenzelm
parents: 26883
diff changeset
    52
fun use name = use_file ML_NameSpace.global Output.ml_output true name;
23978
6e8d5a05ffe8 added use_noncritical;
wenzelm
parents: 23922
diff changeset
    53
20925
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
    54
(*commit is dynamically bound!*)
28268
ac8431ecd57e use_text/use_file now depend on explicit ML name space;
wenzelm
parents: 26883
diff changeset
    55
fun commit () = raw_use_text ML_NameSpace.global (0, "") Output.ml_output false "commit();";
20925
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
    56
20992
05c3703cc6c5 added execute/system;
wenzelm
parents: 20977
diff changeset
    57
05c3703cc6c5 added execute/system;
wenzelm
parents: 20977
diff changeset
    58
(* shell commands *)
05c3703cc6c5 added execute/system;
wenzelm
parents: 20977
diff changeset
    59
05c3703cc6c5 added execute/system;
wenzelm
parents: 20977
diff changeset
    60
fun secure_shell () = deny_secure "Cannot execute shell commands in secure mode";
05c3703cc6c5 added execute/system;
wenzelm
parents: 20977
diff changeset
    61
26220
d34b68c21f9a common setup for system_out/system;
wenzelm
parents: 26080
diff changeset
    62
val orig_system_out = system_out;
20992
05c3703cc6c5 added execute/system;
wenzelm
parents: 20977
diff changeset
    63
26220
d34b68c21f9a common setup for system_out/system;
wenzelm
parents: 26080
diff changeset
    64
fun system_out s = (secure_shell (); orig_system_out s);
26332
aa54cd3ddc9f system: writeln output, if available;
wenzelm
parents: 26220
diff changeset
    65
aa54cd3ddc9f system: writeln output, if available;
wenzelm
parents: 26220
diff changeset
    66
fun system s =
aa54cd3ddc9f system: writeln output, if available;
wenzelm
parents: 26220
diff changeset
    67
  (case system_out s of
aa54cd3ddc9f system: writeln output, if available;
wenzelm
parents: 26220
diff changeset
    68
    ("", rc) => rc
aa54cd3ddc9f system: writeln output, if available;
wenzelm
parents: 26220
diff changeset
    69
  | (out, rc) => (writeln (perhaps (try (unsuffix "\n")) out); rc));
20992
05c3703cc6c5 added execute/system;
wenzelm
parents: 20977
diff changeset
    70
20925
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
    71
end;
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
    72
23978
6e8d5a05ffe8 added use_noncritical;
wenzelm
parents: 23922
diff changeset
    73
(*override previous toplevel bindings!*)
21770
ea6f846d8c4b added use_file;
wenzelm
parents: 20992
diff changeset
    74
val use_text = Secure.use_text;
ea6f846d8c4b added use_file;
wenzelm
parents: 20992
diff changeset
    75
val use_file = Secure.use_file;
24663
015a8838e656 improved error behaviour of use (bootstrap version);
wenzelm
parents: 24600
diff changeset
    76
fun use s = Secure.use s handle ERROR msg => (writeln msg; raise Fail "ML error");
26220
d34b68c21f9a common setup for system_out/system;
wenzelm
parents: 26080
diff changeset
    77
val system_out = Secure.system_out;
20992
05c3703cc6c5 added execute/system;
wenzelm
parents: 20977
diff changeset
    78
val system = Secure.system;