Listing permissions
[hcoop/domtool2.git] / src / main.sml
1 (* HCoop Domtool (http://hcoop.sourceforge.net/)
2 * Copyright (c) 2006, Adam Chlipala
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 *)
18
19 (* Main interface *)
20
21 structure Main :> MAIN = struct
22
23 open Ast MsgTypes Print
24
25 structure SM = StringMap
26
27 fun init () = Acl.read Config.aclFile
28
29 fun check' G fname =
30 let
31 val prog = Parse.parse fname
32 in
33 if !ErrorMsg.anyErrors then
34 G
35 else
36 Tycheck.checkFile G (Defaults.tInit ()) prog
37 end
38
39 fun basis () =
40 let
41 val dir = Posix.FileSys.opendir Config.libRoot
42
43 fun loop files =
44 case Posix.FileSys.readdir dir of
45 NONE => (Posix.FileSys.closedir dir;
46 files)
47 | SOME fname =>
48 if String.isSuffix ".dtl" fname then
49 loop (OS.Path.joinDirFile {dir = Config.libRoot,
50 file = fname}
51 :: files)
52 else
53 loop files
54
55 val files = loop []
56 val (_, files) = Order.order files
57 in
58 if !ErrorMsg.anyErrors then
59 Env.empty
60 else
61 (Tycheck.allowExterns ();
62 foldl (fn (fname, G) => check' G fname) Env.empty files
63 before Tycheck.disallowExterns ())
64 end
65
66 fun check fname =
67 let
68 val _ = ErrorMsg.reset ()
69 val _ = Env.preTycheck ()
70
71 val b = basis ()
72 in
73 if !ErrorMsg.anyErrors then
74 raise ErrorMsg.Error
75 else
76 let
77 val _ = Tycheck.disallowExterns ()
78 val _ = ErrorMsg.reset ()
79 val prog = Parse.parse fname
80 in
81 if !ErrorMsg.anyErrors then
82 raise ErrorMsg.Error
83 else
84 let
85 val G' = Tycheck.checkFile b (Defaults.tInit ()) prog
86 in
87 if !ErrorMsg.anyErrors then
88 raise ErrorMsg.Error
89 else
90 (G', #3 prog)
91 end
92 end
93 end
94
95 fun reduce fname =
96 let
97 val (G, body) = check fname
98 in
99 if !ErrorMsg.anyErrors then
100 NONE
101 else
102 case body of
103 SOME body =>
104 let
105 val body' = Reduce.reduceExp G body
106 in
107 (*printd (PD.hovBox (PD.PPS.Rel 0,
108 [PD.string "Result:",
109 PD.space 1,
110 p_exp body']))*)
111 SOME body'
112 end
113 | _ => NONE
114 end
115
116 fun eval fname =
117 case reduce fname of
118 (SOME body') =>
119 if !ErrorMsg.anyErrors then
120 raise ErrorMsg.Error
121 else
122 Eval.exec (Defaults.eInit ()) body'
123 | NONE => raise ErrorMsg.Error
124
125 val dispatcher =
126 Config.dispatcher ^ ":" ^ Int.toString Config.dispatcherPort
127
128 fun requestContext f =
129 let
130 val uid = Posix.ProcEnv.getuid ()
131 val user = Posix.SysDB.Passwd.name (Posix.SysDB.getpwuid uid)
132
133 val () = Acl.read Config.aclFile
134 val () = Domain.setUser user
135
136 val () = f ()
137
138 val context = OpenSSL.context (Config.certDir ^ "/" ^ user ^ ".pem",
139 Config.keyDir ^ "/" ^ user ^ "/key.pem",
140 Config.trustStore)
141 in
142 (user, context)
143 end
144
145 fun requestBio f =
146 let
147 val (user, context) = requestContext f
148 in
149 (user, OpenSSL.connect (context, dispatcher))
150 end
151
152 fun request fname =
153 let
154 val (user, bio) = requestBio (fn () => ignore (check fname))
155
156 val inf = TextIO.openIn fname
157
158 fun loop lines =
159 case TextIO.inputLine inf of
160 NONE => String.concat (List.rev lines)
161 | SOME line => loop (line :: lines)
162
163 val code = loop []
164 in
165 TextIO.closeIn inf;
166 Msg.send (bio, MsgConfig code);
167 case Msg.recv bio of
168 NONE => print "Server closed connection unexpectedly.\n"
169 | SOME m =>
170 case m of
171 MsgOk => print "Configuration succeeded.\n"
172 | MsgError s => print ("Configuration failed: " ^ s ^ "\n")
173 | _ => print "Unexpected server reply.\n";
174 OpenSSL.close bio
175 end
176 handle ErrorMsg.Error => ()
177
178 fun requestGrant acl =
179 let
180 val (user, bio) = requestBio (fn () => ())
181 in
182 Msg.send (bio, MsgGrant acl);
183 case Msg.recv bio of
184 NONE => print "Server closed connection unexpectedly.\n"
185 | SOME m =>
186 case m of
187 MsgOk => print "Grant succeeded.\n"
188 | MsgError s => print ("Grant failed: " ^ s ^ "\n")
189 | _ => print "Unexpected server reply.\n";
190 OpenSSL.close bio
191 end
192
193 fun requestRevoke acl =
194 let
195 val (user, bio) = requestBio (fn () => ())
196 in
197 Msg.send (bio, MsgRevoke acl);
198 case Msg.recv bio of
199 NONE => print "Server closed connection unexpectedly.\n"
200 | SOME m =>
201 case m of
202 MsgOk => print "Revoke succeeded.\n"
203 | MsgError s => print ("Revoke failed: " ^ s ^ "\n")
204 | _ => print "Unexpected server reply.\n";
205 OpenSSL.close bio
206 end
207
208 fun requestListPerms user =
209 let
210 val (_, bio) = requestBio (fn () => ())
211 in
212 Msg.send (bio, MsgListPerms user);
213 (case Msg.recv bio of
214 NONE => (print "Server closed connection unexpectedly.\n";
215 NONE)
216 | SOME m =>
217 case m of
218 MsgPerms perms => SOME perms
219 | MsgError s => (print ("Listing failed: " ^ s ^ "\n");
220 NONE)
221 | _ => (print "Unexpected server reply.\n";
222 NONE))
223 before OpenSSL.close bio
224 end
225
226 fun service () =
227 let
228 val () = Acl.read Config.aclFile
229
230 val context = OpenSSL.context (Config.serverCert,
231 Config.serverKey,
232 Config.trustStore)
233 val _ = Domain.set_context context
234
235 val sock = OpenSSL.listen (context, Config.dispatcherPort)
236
237 fun loop () =
238 case OpenSSL.accept sock of
239 NONE => ()
240 | SOME bio =>
241 let
242 val user = OpenSSL.peerCN bio
243 val () = print ("\nConnection from " ^ user ^ "\n")
244 val () = Domain.setUser user
245
246 fun cmdLoop () =
247 case Msg.recv bio of
248 NONE => (OpenSSL.close bio
249 handle OpenSSL.OpenSSL _ => ();
250 loop ())
251 | SOME m =>
252 case m of
253 MsgConfig code =>
254 let
255 val _ = print "Configuration:\n"
256 val _ = print code
257 val _ = print "\n"
258
259 val outname = OS.FileSys.tmpName ()
260 val outf = TextIO.openOut outname
261 in
262 TextIO.output (outf, code);
263 TextIO.closeOut outf;
264 (eval outname;
265 Msg.send (bio, MsgOk))
266 handle ErrorMsg.Error =>
267 (print "Compilation error\n";
268 Msg.send (bio,
269 MsgError "Error during configuration evaluation"))
270 | OpenSSL.OpenSSL s =>
271 (print "OpenSSL error\n";
272 Msg.send (bio,
273 MsgError
274 ("Error during configuration evaluation: "
275 ^ s)));
276 OS.FileSys.remove outname;
277 (ignore (OpenSSL.readChar bio);
278 OpenSSL.close bio)
279 handle OpenSSL.OpenSSL _ => ();
280 loop ()
281 end
282
283 | MsgGrant acl =>
284 if Acl.query {user = user, class = "group", value = "root"} then
285 ((Acl.grant acl;
286 Acl.write Config.aclFile;
287 Msg.send (bio, MsgOk);
288 print ("Granted permission " ^ #value acl ^ " to " ^ #user acl ^ " in " ^ #class acl ^ ".\n"))
289 handle OpenSSL.OpenSSL s =>
290 (print "OpenSSL error\n";
291 Msg.send (bio,
292 MsgError
293 ("Error during granting: "
294 ^ s)));
295 (ignore (OpenSSL.readChar bio);
296 OpenSSL.close bio)
297 handle OpenSSL.OpenSSL _ => ();
298 loop ())
299 else
300 ((Msg.send (bio, MsgError "Not authorized to grant privileges");
301 print "Unauthorized user asked to grant a permission!\n";
302 ignore (OpenSSL.readChar bio);
303 OpenSSL.close bio)
304 handle OpenSSL.OpenSSL _ => ();
305 loop ())
306
307 | MsgRevoke acl =>
308 if Acl.query {user = user, class = "group", value = "root"} then
309 ((Acl.revoke acl;
310 Acl.write Config.aclFile;
311 Msg.send (bio, MsgOk);
312 print ("Revoked permission " ^ #value acl ^ " from " ^ #user acl ^ " in " ^ #class acl ^ ".\n"))
313 handle OpenSSL.OpenSSL s =>
314 (print "OpenSSL error\n";
315 Msg.send (bio,
316 MsgError
317 ("Error during revocation: "
318 ^ s)));
319 (ignore (OpenSSL.readChar bio);
320 OpenSSL.close bio)
321 handle OpenSSL.OpenSSL _ => ();
322 loop ())
323 else
324 ((Msg.send (bio, MsgError "Not authorized to revoke privileges");
325 print "Unauthorized user asked to revoke a permission!\n";
326 ignore (OpenSSL.readChar bio);
327 OpenSSL.close bio)
328 handle OpenSSL.OpenSSL _ => ();
329 loop ())
330
331 | MsgListPerms user =>
332 ((Msg.send (bio, MsgPerms (Acl.queryAll user));
333 print ("Sent permission list for user " ^ user ^ ".\n"))
334 handle OpenSSL.OpenSSL s =>
335 (print "OpenSSL error\n";
336 Msg.send (bio,
337 MsgError
338 ("Error during permission listing: "
339 ^ s)));
340 (ignore (OpenSSL.readChar bio);
341 OpenSSL.close bio)
342 handle OpenSSL.OpenSSL _ => ();
343 loop ())
344
345 | _ =>
346 (Msg.send (bio, MsgError "Unexpected command")
347 handle OpenSSL.OpenSSL _ => ();
348 OpenSSL.close bio
349 handle OpenSSL.OpenSSL _ => ();
350 loop ())
351 in
352 cmdLoop ()
353 end
354 handle OpenSSL.OpenSSL s =>
355 (print ("OpenSSL error: " ^ s ^ "\n");
356 OpenSSL.close bio
357 handle OpenSSL.OpenSSL _ => ();
358 loop ())
359 | OS.SysErr (s, _) =>
360 (print ("System error: " ^ s ^ "\n");
361 OpenSSL.close bio
362 handle OpenSSL.OpenSSL _ => ();
363 loop ())
364 in
365 print "Listening for connections....\n";
366 loop ();
367 OpenSSL.shutdown sock
368 end
369
370 fun slave () =
371 let
372 val host = Slave.hostname ()
373
374 val context = OpenSSL.context (Config.certDir ^ "/" ^ host ^ ".pem",
375 Config.keyDir ^ "/" ^ host ^ "/key.pem",
376 Config.trustStore)
377
378 val sock = OpenSSL.listen (context, Config.slavePort)
379
380 fun loop () =
381 case OpenSSL.accept sock of
382 NONE => ()
383 | SOME bio =>
384 let
385 val peer = OpenSSL.peerCN bio
386 val () = print ("\nConnection from " ^ peer ^ "\n")
387 in
388 if peer <> Config.dispatcherName then
389 (print "Not authorized!\n";
390 OpenSSL.close bio;
391 loop ())
392 else let
393 fun loop' files =
394 case Msg.recv bio of
395 NONE => print "Dispatcher closed connection unexpectedly\n"
396 | SOME m =>
397 case m of
398 MsgFile file => loop' (file :: files)
399 | MsgDoFiles => (Slave.handleChanges files;
400 Msg.send (bio, MsgOk))
401 | _ => (print "Dispatcher sent unexpected command\n";
402 Msg.send (bio, MsgError "Unexpected command"))
403 in
404 loop' [];
405 ignore (OpenSSL.readChar bio);
406 OpenSSL.close bio;
407 loop ()
408 end
409 end handle OpenSSL.OpenSSL s =>
410 (print ("OpenSSL error: "^ s ^ "\n");
411 OpenSSL.close bio
412 handle OpenSSL.OpenSSL _ => ();
413 loop ())
414 | OS.SysErr (s, _) =>
415 (print ("System error: "^ s ^ "\n");
416 OpenSSL.close bio
417 handle OpenSSL.OpenSSL _ => ();
418 loop ())
419 in
420 loop ();
421 OpenSSL.shutdown sock
422 end
423
424 fun autodocBasis outdir =
425 let
426 val dir = Posix.FileSys.opendir Config.libRoot
427
428 fun loop files =
429 case Posix.FileSys.readdir dir of
430 NONE => (Posix.FileSys.closedir dir;
431 files)
432 | SOME fname =>
433 if String.isSuffix ".dtl" fname then
434 loop (OS.Path.joinDirFile {dir = Config.libRoot,
435 file = fname}
436 :: files)
437 else
438 loop files
439
440 val files = loop []
441 in
442 Autodoc.autodoc {outdir = outdir, infiles = files}
443 end
444
445 end