author | wenzelm |
Sat, 26 Jan 2008 17:08:41 +0100 | |
changeset 25982 | caee173104d3 |
parent 25753 | 99c9fc5e11f2 |
child 26080 | d920e4c8ba82 |
permissions | -rw-r--r-- |
20925 | 1 |
(* Title: Pure/General/secure.ML |
2 |
ID: $Id$ |
|
3 |
Author: Makarius |
|
4 |
||
5 |
Secure critical operations. |
|
6 |
*) |
|
7 |
||
8 |
signature SECURE = |
|
9 |
sig |
|
20977 | 10 |
val set_secure: unit -> unit |
20925 | 11 |
val is_secure: unit -> bool |
22144 | 12 |
val use_text: string -> (string -> unit) * (string -> 'a) -> bool -> string -> unit |
21770 | 13 |
val use_file: (string -> unit) * (string -> 'a) -> bool -> string -> unit |
20925 | 14 |
val use: string -> unit |
15 |
val commit: unit -> unit |
|
20992 | 16 |
val execute: string -> string |
17 |
val system: string -> int |
|
20925 | 18 |
end; |
19 |
||
20 |
structure Secure: SECURE = |
|
21 |
struct |
|
22 |
||
20992 | 23 |
(** secure flag **) |
20925 | 24 |
|
25 |
val secure = ref false; |
|
26 |
||
20977 | 27 |
fun set_secure () = secure := true; |
20925 | 28 |
fun is_secure () = ! secure; |
29 |
||
22567
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents:
22144
diff
changeset
|
30 |
fun deny_secure msg = if is_secure () then error msg else (); |
20925 | 31 |
|
32 |
||
25753 | 33 |
|
20992 | 34 |
(** critical operations **) |
35 |
||
36 |
(* ML evaluation *) |
|
20925 | 37 |
|
38 |
fun secure_mltext () = deny_secure "Cannot evaluate ML source in secure mode"; |
|
39 |
||
24600
5877b88f262c
use_text/file: tune text (cf. ML_Parse.fix_ints);
wenzelm
parents:
24058
diff
changeset
|
40 |
fun orig_use_text x = use_text ML_Parse.fix_ints x; |
5877b88f262c
use_text/file: tune text (cf. ML_Parse.fix_ints);
wenzelm
parents:
24058
diff
changeset
|
41 |
fun orig_use_file x = use_file ML_Parse.fix_ints x; |
20925 | 42 |
|
25698
8c335b4641a5
use_text/use_file: non-critical (Poly/ML compiler is thread-safe);
wenzelm
parents:
25503
diff
changeset
|
43 |
fun use_text name pr verbose txt = |
8c335b4641a5
use_text/use_file: non-critical (Poly/ML compiler is thread-safe);
wenzelm
parents:
25503
diff
changeset
|
44 |
(secure_mltext (); orig_use_text name pr verbose txt); |
23922
707639e9497d
marked some CRITICAL sections (for multithreading);
wenzelm
parents:
22567
diff
changeset
|
45 |
|
25698
8c335b4641a5
use_text/use_file: non-critical (Poly/ML compiler is thread-safe);
wenzelm
parents:
25503
diff
changeset
|
46 |
fun use_file pr verbose name = |
8c335b4641a5
use_text/use_file: non-critical (Poly/ML compiler is thread-safe);
wenzelm
parents:
25503
diff
changeset
|
47 |
(secure_mltext (); orig_use_file pr verbose name); |
23922
707639e9497d
marked some CRITICAL sections (for multithreading);
wenzelm
parents:
22567
diff
changeset
|
48 |
|
23978 | 49 |
fun use name = use_file Output.ml_output true name; |
50 |
||
20925 | 51 |
(*commit is dynamically bound!*) |
25503
fe14c6857f1d
commit: non-critical, otherwise session restart will result in deadlock!
wenzelm
parents:
25204
diff
changeset
|
52 |
fun commit () = orig_use_text "" Output.ml_output false "commit();"; |
20925 | 53 |
|
20992 | 54 |
|
55 |
(* shell commands *) |
|
56 |
||
57 |
fun secure_shell () = deny_secure "Cannot execute shell commands in secure mode"; |
|
58 |
||
59 |
val orig_execute = execute; |
|
60 |
val orig_system = system; |
|
61 |
||
24858 | 62 |
fun execute s = (secure_shell (); orig_execute s); |
63 |
fun system s = (secure_shell (); orig_system s); |
|
20992 | 64 |
|
20925 | 65 |
end; |
66 |
||
23978 | 67 |
(*override previous toplevel bindings!*) |
21770 | 68 |
val use_text = Secure.use_text; |
69 |
val use_file = Secure.use_file; |
|
24663
015a8838e656
improved error behaviour of use (bootstrap version);
wenzelm
parents:
24600
diff
changeset
|
70 |
fun use s = Secure.use s handle ERROR msg => (writeln msg; raise Fail "ML error"); |
20992 | 71 |
val execute = Secure.execute; |
72 |
val system = Secure.system; |