1 (* Initialise the toplevel environment. -*- tuareg -*-
2 * $Id: init.in,v 1.8 2008/02/13 17:35:32 rjones Exp $
3 * - Removes the Pervasives module and any dangerous functions.
4 * - Loads just the modules we want to give access to, and just
5 * the functions within those modules that we want to give.
6 * - Sets up our custom camlp4 grammar which removes "external"
8 * - Chroot to somewhere safe.
11 (* Load some libraries. *)
16 (* Remove the Pervasives module. *)
17 module Pervasives = struct end;;
19 (* Remove any unsafe imported functions. *)
28 let close_out_noerr = ()
29 let set_binary_mode_out = ()
39 let input_binary_int = ()
43 let in_channel_length = ()
45 let close_in_noerr = ()
46 let set_binary_mode_in = ()
48 module LargeFile = struct end;;
50 (* let exit = () -- do this later *)
52 let valid_float_lexem = ()
53 let unsafe_really_input = ()
56 (* Modules which have been checked and appear to be all safe. *)
57 let _ = Big_int.zero_big_int
60 let _ = Hashtbl.create
61 let _ = Int32.to_string
62 let _ = Int64.to_string
65 let _ = Nativeint.to_string
68 let _ = Ratio.null_denominator
72 (* Allow only safe functions from String. *)
74 external length : string -> int = "%string_length"
75 external get : string -> int -> char = "%string_safe_get"
76 external set : string -> int -> char -> unit = "%string_safe_set"
77 external create : int -> string = "caml_create_string"
78 val make : int -> char -> string
79 val copy : string -> string
80 val sub : string -> int -> int -> string
81 val fill : string -> int -> int -> char -> unit
82 val blit : string -> int -> string -> int -> int -> unit
83 val concat : string -> string list -> string
84 val iter : (char -> unit) -> string -> unit
85 val escaped : string -> string
86 val index : string -> char -> int
87 val rindex : string -> char -> int
88 val index_from : string -> int -> char -> int
89 val rindex_from : string -> int -> char -> int
90 val contains : string -> char -> bool
91 val contains_from : string -> int -> char -> bool
92 val rcontains_from : string -> int -> char -> bool
93 val uppercase : string -> string
94 val lowercase : string -> string
95 val capitalize : string -> string
96 val uncapitalize : string -> string
98 val compare: t -> t -> int
99 end = struct include String end
101 (* Allow only safe functions from Filename. *)
102 module Filename : sig
103 val current_dir_name : string
104 val parent_dir_name : string
105 val concat : string -> string -> string
106 val is_relative : string -> bool
107 val is_implicit : string -> bool
108 val check_suffix : string -> string -> bool
109 val chop_suffix : string -> string -> string
110 val chop_extension : string -> string
111 val basename : string -> string
112 val dirname : string -> string
113 val quote : string -> string
114 end = struct include Filename end
116 (* Allow only safe functions from Char. *)
118 external code : char -> int = "%identity"
119 val chr : int -> char
120 val escaped : char -> string
121 val lowercase : char -> char
122 val uppercase : char -> char
124 val compare: t -> t -> int
125 end = struct include Char end
127 (* Allow only safe functions from Array. *)
129 external length : 'a array -> int = "%array_length"
130 external get : 'a array -> int -> 'a = "%array_safe_get"
131 external set : 'a array -> int -> 'a -> unit = "%array_safe_set"
132 external make : int -> 'a -> 'a array = "caml_make_vect"
133 external create : int -> 'a -> 'a array = "caml_make_vect"
134 val init : int -> (int -> 'a) -> 'a array
135 val make_matrix : int -> int -> 'a -> 'a array array
136 val create_matrix : int -> int -> 'a -> 'a array array
137 val append : 'a array -> 'a array -> 'a array
138 val concat : 'a array list -> 'a array
139 val sub : 'a array -> int -> int -> 'a array
140 val copy : 'a array -> 'a array
141 val fill : 'a array -> int -> int -> 'a -> unit
142 val blit : 'a array -> int -> 'a array -> int -> int -> unit
143 val to_list : 'a array -> 'a list
144 val of_list : 'a list -> 'a array
145 val iter : ('a -> unit) -> 'a array -> unit
146 val map : ('a -> 'b) -> 'a array -> 'b array
147 val iteri : (int -> 'a -> unit) -> 'a array -> unit
148 val mapi : (int -> 'a -> 'b) -> 'a array -> 'b array
149 val fold_left : ('a -> 'b -> 'a) -> 'a -> 'b array -> 'a
150 val fold_right : ('b -> 'a -> 'a) -> 'b array -> 'a -> 'a
151 val sort : ('a -> 'a -> int) -> 'a array -> unit
152 val stable_sort : ('a -> 'a -> int) -> 'a array -> unit
153 val fast_sort : ('a -> 'a -> int) -> 'a array -> unit
154 end = struct include Array end
156 (* Allow only safe functions from Printf. *)
158 val printf : ('a, out_channel, unit) format -> 'a
159 val sprintf : ('a, unit, string) format -> 'a
160 end = struct include Printf end
162 (* Allow only safe functions from Scanf. *)
164 module Scanning : sig
166 val stdib : scanbuf;;
167 val from_string : string -> scanbuf;;
168 val from_function : (unit -> char) -> scanbuf;;
169 val end_of_input : scanbuf -> bool;;
170 val beginning_of_input : scanbuf -> bool;;
171 val name_of_input : scanbuf -> string;;
173 exception Scan_failure of string;;
174 type ('a, 'b, 'c, 'd) scanner =
175 ('a, Scanning.scanbuf, 'b, 'c, 'a -> 'd, 'd) format6 -> 'c;;
176 val bscanf : Scanning.scanbuf -> ('a, 'b, 'c, 'd) scanner;;
177 val sscanf : string -> ('a, 'b, 'c, 'd) scanner;;
178 val scanf : ('a, 'b, 'c, 'd) scanner;;
180 Scanning.scanbuf -> (Scanning.scanbuf -> exn -> 'd) ->
181 ('a, 'b, 'c, 'd) scanner;;
183 Scanning.scanbuf -> ('a, 'b, 'c, 'd, 'e, 'f) format6 ->
184 (('a, 'b, 'c, 'd, 'e, 'f) format6 -> 'g) -> 'g;;
186 string -> ('a, 'b, 'c, 'd, 'e, 'f) format6 ->
187 (('a, 'b, 'c, 'd, 'e, 'f) format6 -> 'g) -> 'g;;
188 val format_from_string :
190 ('a, 'b, 'c, 'd, 'e, 'f) format6 -> ('a, 'b, 'c, 'd, 'e, 'f) format6;;
191 end = struct include Scanf end
194 module StringSet = Set.Make(String)
195 module StringMap = Map.Make(String)
197 (* Create an object, so we get the CamlinternalOO module ... *)
200 (* ... but prevent public access to CamlinternalOO. *)
201 module CamlinternalOO = struct end
203 #load "camlp4o.cma";;
204 #load "./pa_noexternal.cmo";;
206 (* Chroot and setuid to nobody. If this fails, die. *)
209 let pw = Unix.getpwnam "@OCAMLUSER@" in
210 Unix.chdir "@CHROOTDIR@";
211 Unix.chroot "@CHROOTDIR@";
212 Unix.setgid pw.Unix.pw_gid;
213 Unix.setuid pw.Unix.pw_uid
216 print_endline (Printexc.to_string exn);
219 (* Kill the Unix and UnixLabels modules, and exit function. *)
220 module Unix = struct end
221 module UnixLabels = struct end