1 (* Initialise the toplevel environment. -*- tuareg -*-
2 * $Id: init.in,v 1.5 2008/01/23 15:44:46 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
64 let _ = Nativeint.to_string
67 let _ = Ratio.null_denominator
71 (* Allow only safe functions from String. *)
73 external length : string -> int = "%string_length"
74 external get : string -> int -> char = "%string_safe_get"
75 external set : string -> int -> char -> unit = "%string_safe_set"
76 external create : int -> string = "caml_create_string"
77 val make : int -> char -> string
78 val copy : string -> string
79 val sub : string -> int -> int -> string
80 val fill : string -> int -> int -> char -> unit
81 val blit : string -> int -> string -> int -> int -> unit
82 val concat : string -> string list -> string
83 val iter : (char -> unit) -> string -> unit
84 val escaped : string -> string
85 val index : string -> char -> int
86 val rindex : string -> char -> int
87 val index_from : string -> int -> char -> int
88 val rindex_from : string -> int -> char -> int
89 val contains : string -> char -> bool
90 val contains_from : string -> int -> char -> bool
91 val rcontains_from : string -> int -> char -> bool
92 val uppercase : string -> string
93 val lowercase : string -> string
94 val capitalize : string -> string
95 val uncapitalize : string -> string
97 val compare: t -> t -> int
98 end = struct include String end
100 (* Allow only safe functions from Filename. *)
101 module Filename : sig
102 val current_dir_name : string
103 val parent_dir_name : string
104 val concat : string -> string -> string
105 val is_relative : string -> bool
106 val is_implicit : string -> bool
107 val check_suffix : string -> string -> bool
108 val chop_suffix : string -> string -> string
109 val chop_extension : string -> string
110 val basename : string -> string
111 val dirname : string -> string
112 val quote : string -> string
113 end = struct include Filename end
115 (* Allow only safe functions from Char. *)
117 external code : char -> int = "%identity"
118 val chr : int -> char
119 val escaped : char -> string
120 val lowercase : char -> char
121 val uppercase : char -> char
123 val compare: t -> t -> int
124 end = struct include Char end
126 (* Allow only safe functions from Array. *)
128 external length : 'a array -> int = "%array_length"
129 external get : 'a array -> int -> 'a = "%array_safe_get"
130 external set : 'a array -> int -> 'a -> unit = "%array_safe_set"
131 external make : int -> 'a -> 'a array = "caml_make_vect"
132 external create : int -> 'a -> 'a array = "caml_make_vect"
133 val init : int -> (int -> 'a) -> 'a array
134 val make_matrix : int -> int -> 'a -> 'a array array
135 val create_matrix : int -> int -> 'a -> 'a array array
136 val append : 'a array -> 'a array -> 'a array
137 val concat : 'a array list -> 'a array
138 val sub : 'a array -> int -> int -> 'a array
139 val copy : 'a array -> 'a array
140 val fill : 'a array -> int -> int -> 'a -> unit
141 val blit : 'a array -> int -> 'a array -> int -> int -> unit
142 val to_list : 'a array -> 'a list
143 val of_list : 'a list -> 'a array
144 val iter : ('a -> unit) -> 'a array -> unit
145 val map : ('a -> 'b) -> 'a array -> 'b array
146 val iteri : (int -> 'a -> unit) -> 'a array -> unit
147 val mapi : (int -> 'a -> 'b) -> 'a array -> 'b array
148 val fold_left : ('a -> 'b -> 'a) -> 'a -> 'b array -> 'a
149 val fold_right : ('b -> 'a -> 'a) -> 'b array -> 'a -> 'a
150 val sort : ('a -> 'a -> int) -> 'a array -> unit
151 val stable_sort : ('a -> 'a -> int) -> 'a array -> unit
152 val fast_sort : ('a -> 'a -> int) -> 'a array -> unit
153 end = struct include Array end
155 (* Allow only safe functions from Printf. *)
157 val printf : ('a, out_channel, unit) format -> 'a
158 val sprintf : ('a, unit, string) format -> 'a
159 end = struct include Printf end
161 (* Allow only safe functions from Scanf. *)
163 module Scanning : sig
165 val stdib : scanbuf;;
166 val from_string : string -> scanbuf;;
167 val from_function : (unit -> char) -> scanbuf;;
168 val end_of_input : scanbuf -> bool;;
169 val beginning_of_input : scanbuf -> bool;;
170 val name_of_input : scanbuf -> string;;
172 exception Scan_failure of string;;
173 type ('a, 'b, 'c, 'd) scanner =
174 ('a, Scanning.scanbuf, 'b, 'c, 'a -> 'd, 'd) format6 -> 'c;;
175 val bscanf : Scanning.scanbuf -> ('a, 'b, 'c, 'd) scanner;;
176 val sscanf : string -> ('a, 'b, 'c, 'd) scanner;;
177 val scanf : ('a, 'b, 'c, 'd) scanner;;
179 Scanning.scanbuf -> (Scanning.scanbuf -> exn -> 'd) ->
180 ('a, 'b, 'c, 'd) scanner;;
182 Scanning.scanbuf -> ('a, 'b, 'c, 'd, 'e, 'f) format6 ->
183 (('a, 'b, 'c, 'd, 'e, 'f) format6 -> 'g) -> 'g;;
185 string -> ('a, 'b, 'c, 'd, 'e, 'f) format6 ->
186 (('a, 'b, 'c, 'd, 'e, 'f) format6 -> 'g) -> 'g;;
187 val format_from_string :
189 ('a, 'b, 'c, 'd, 'e, 'f) format6 -> ('a, 'b, 'c, 'd, 'e, 'f) format6;;
190 end = struct include Scanf end
193 module StringSet = Set.Make(String)
194 module StringMap = Map.Make(String)
196 (* Create an object, so we get the CamlinternalOO module. *)
197 (* XXX Are any of the methods unsafe? *)
200 (* Load our custom grammar, which disables "external". *)
202 #load "camlp4o.cma";;
203 #load "./pa_noexternal.cmo";;
205 (* Chroot and setuid to nobody. If this fails, die. *)
208 let pw = Unix.getpwnam "@OCAMLUSER@" in
209 Unix.chdir "@CHROOTDIR@";
210 Unix.chroot "@CHROOTDIR@";
211 Unix.setgid pw.Unix.pw_gid;
212 Unix.setuid pw.Unix.pw_uid
215 print_endline (Printexc.to_string exn);
218 (* Kill the Unix and UnixLabels modules, and exit function. *)
219 module Unix = struct end
220 module UnixLabels = struct end