Version 0.5.
[xavierbot.git] / init.in
1 (* Initialise the toplevel environment. -*- tuareg -*-
2  * $Id: init.in,v 1.4 2007/06/29 19:39:13 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"
7  *   keyword.
8  * - Chroot to somewhere safe.
9  *)
10
11 (* Load some libraries. *)
12 #load "nums.cma";;
13 #load "unix.cma";;
14 #load "str.cma";;
15
16 (* Remove the Pervasives module. *)
17 module Pervasives = struct end;;
18
19 (* Remove any unsafe imported functions. *)
20 let open_out = ()
21 let open_out_bin = ()
22 let open_out_gen = ()
23 let flush = ()
24 let flush_all = ()
25 let output_value = ()
26 let seek_out = ()
27 let close_out = ()
28 let close_out_noerr = ()
29 let set_binary_mode_out = ()
30
31 let open_in = ()
32 let open_in_bin = ()
33 let open_in_gen = ()
34 let input_char = ()
35 let input_line = ()
36 let input = ()
37 let really_input = ()
38 let input_byte = ()
39 let input_binary_int = ()
40 let input_value = ()
41 let seek_in = ()
42 let pos_in = ()
43 let in_channel_length = ()
44 let close_in = ()
45 let close_in_noerr = ()
46 let set_binary_mode_in = ()
47
48 module LargeFile = struct end;;
49
50 (* let exit = () -- do this later *)
51 let at_exit = ()
52 let valid_float_lexem = ()
53 let unsafe_really_input = ()
54 let do_at_exit = ()
55
56 (* Modules which have been checked and appear to be all safe. *)
57 let _ = Big_int.zero_big_int
58 let _ = Buffer.create
59 let _ = Complex.zero
60 let _ = Hashtbl.create
61 let _ = Int32.to_string
62 let _ = Int64.to_string
63 let _ = List.length
64 let _ = Nativeint.to_string
65 let _ = Num.add_num
66 let _ = Ratio.null_denominator
67 let _ = Stack.create
68 let _ = Str.regexp
69
70 (* Allow only safe functions from String. *)
71 module String : sig
72   external length : string -> int = "%string_length"
73   external get : string -> int -> char = "%string_safe_get"
74   external set : string -> int -> char -> unit = "%string_safe_set"
75   external create : int -> string = "caml_create_string"
76   val make : int -> char -> string
77   val copy : string -> string
78   val sub : string -> int -> int -> string
79   val fill : string -> int -> int -> char -> unit
80   val blit : string -> int -> string -> int -> int -> unit
81   val concat : string -> string list -> string
82   val iter : (char -> unit) -> string -> unit
83   val escaped : string -> string
84   val index : string -> char -> int
85   val rindex : string -> char -> int
86   val index_from : string -> int -> char -> int
87   val rindex_from : string -> int -> char -> int
88   val contains : string -> char -> bool
89   val contains_from : string -> int -> char -> bool
90   val rcontains_from : string -> int -> char -> bool
91   val uppercase : string -> string
92   val lowercase : string -> string
93   val capitalize : string -> string
94   val uncapitalize : string -> string
95   type t = string
96   val compare: t -> t -> int
97 end = struct include String end
98
99 (* Allow only safe functions from Filename. *)
100 module Filename : sig
101   val current_dir_name : string
102   val parent_dir_name : string
103   val concat : string -> string -> string
104   val is_relative : string -> bool
105   val is_implicit : string -> bool
106   val check_suffix : string -> string -> bool
107   val chop_suffix : string -> string -> string
108   val chop_extension : string -> string
109   val basename : string -> string
110   val dirname : string -> string
111   val quote : string -> string
112 end = struct include Filename end
113
114 (* Allow only safe functions from Char. *)
115 module Char : sig
116   external code : char -> int = "%identity"
117   val chr : int -> char
118   val escaped : char -> string
119   val lowercase : char -> char
120   val uppercase : char -> char
121   type t = char
122   val compare: t -> t -> int
123 end = struct include Char end
124
125 (* Allow only safe functions from Array. *)
126 module Array : sig
127   external length : 'a array -> int = "%array_length"
128   external get : 'a array -> int -> 'a = "%array_safe_get"
129   external set : 'a array -> int -> 'a -> unit = "%array_safe_set"
130   external make : int -> 'a -> 'a array = "caml_make_vect"
131   external create : int -> 'a -> 'a array = "caml_make_vect"
132   val init : int -> (int -> 'a) -> 'a array
133   val make_matrix : int -> int -> 'a -> 'a array array
134   val create_matrix : int -> int -> 'a -> 'a array array
135   val append : 'a array -> 'a array -> 'a array
136   val concat : 'a array list -> 'a array
137   val sub : 'a array -> int -> int -> 'a array
138   val copy : 'a array -> 'a array
139   val fill : 'a array -> int -> int -> 'a -> unit
140   val blit : 'a array -> int -> 'a array -> int -> int -> unit
141   val to_list : 'a array -> 'a list
142   val of_list : 'a list -> 'a array
143   val iter : ('a -> unit) -> 'a array -> unit
144   val map : ('a -> 'b) -> 'a array -> 'b array
145   val iteri : (int -> 'a -> unit) -> 'a array -> unit
146   val mapi : (int -> 'a -> 'b) -> 'a array -> 'b array
147   val fold_left : ('a -> 'b -> 'a) -> 'a -> 'b array -> 'a
148   val fold_right : ('b -> 'a -> 'a) -> 'b array -> 'a -> 'a
149   val sort : ('a -> 'a -> int) -> 'a array -> unit
150   val stable_sort : ('a -> 'a -> int) -> 'a array -> unit
151   val fast_sort : ('a -> 'a -> int) -> 'a array -> unit
152 end = struct include Array end
153
154 (* Allow only safe functions from Printf. *)
155 module Printf : sig
156   val printf : ('a, out_channel, unit) format -> 'a
157   val sprintf : ('a, unit, string) format -> 'a
158 end = struct include Printf end
159
160 (* Allow only safe functions from Scanf. *)
161 module Scanf : sig
162   module Scanning : sig
163     type scanbuf;;
164     val stdib : scanbuf;;
165     val from_string : string -> scanbuf;;
166     val from_function : (unit -> char) -> scanbuf;;
167     val end_of_input : scanbuf -> bool;;
168     val beginning_of_input : scanbuf -> bool;;
169     val name_of_input : scanbuf -> string;;
170   end;;
171   exception Scan_failure of string;;
172   type ('a, 'b, 'c, 'd) scanner =
173        ('a, Scanning.scanbuf, 'b, 'c, 'a -> 'd, 'd) format6 -> 'c;;
174   val bscanf : Scanning.scanbuf -> ('a, 'b, 'c, 'd) scanner;;
175   val sscanf : string -> ('a, 'b, 'c, 'd) scanner;;
176   val scanf : ('a, 'b, 'c, 'd) scanner;;
177   val kscanf :
178     Scanning.scanbuf -> (Scanning.scanbuf -> exn -> 'd) ->
179       ('a, 'b, 'c, 'd) scanner;;
180   val bscanf_format :
181     Scanning.scanbuf -> ('a, 'b, 'c, 'd, 'e, 'f) format6 ->
182       (('a, 'b, 'c, 'd, 'e, 'f) format6 -> 'g) -> 'g;;
183   val sscanf_format :
184     string -> ('a, 'b, 'c, 'd, 'e, 'f) format6 ->
185       (('a, 'b, 'c, 'd, 'e, 'f) format6 -> 'g) -> 'g;;
186   val format_from_string :
187     string ->
188       ('a, 'b, 'c, 'd, 'e, 'f) format6 -> ('a, 'b, 'c, 'd, 'e, 'f) format6;;
189 end = struct include Scanf end
190
191 (* Set and Map. *)
192 module StringSet = Set.Make(String)
193 module StringMap = Map.Make(String)
194
195 (* Create an object, so we get the CamlinternalOO module. *)
196 (* XXX Are any of the methods unsafe? *)
197 let _ = object end
198
199 (* Load our custom grammar, which disables "external". *)
200
201 #load "camlp4o.cma";;
202 #load "./pa_noexternal.cmo";;
203
204 (* Chroot and setuid to nobody.  If this fails, die. *)
205 let () =
206   try
207     let pw = Unix.getpwnam "@OCAMLUSER@" in
208     Unix.chdir "@CHROOTDIR@";
209     Unix.chroot "@CHROOTDIR@";
210     Unix.setgid pw.Unix.pw_gid;
211     Unix.setuid pw.Unix.pw_uid
212   with
213     exn ->
214       print_endline (Printexc.to_string exn);
215       exit 1
216
217 (* Kill the Unix and UnixLabels modules, and exit function. *)
218 module Unix = struct end
219 module UnixLabels = struct end
220 let exit = ()