1 (* HCoop
Domtool (http
://hcoop
.sourceforge
.net
/)
2 * Copyright (c
) 2006, Adam Chlipala
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
.
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
.
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
.
21 structure Main
:> MAIN
= struct
23 open Ast MsgTypes Print
25 structure SM
= StringMap
27 fun init () = Acl
.read Config
.aclFile
31 val prog
= Parse
.parse fname
33 if !ErrorMsg
.anyErrors
then
36 Tycheck
.checkFile
G (Defaults
.tInit ()) prog
41 val dir
= Posix
.FileSys
.opendir Config
.libRoot
44 case Posix
.FileSys
.readdir dir
of
45 NONE
=> (Posix
.FileSys
.closedir dir
;
48 if String.isSuffix
".dtl" fname
then
49 loop (OS
.Path
.joinDirFile
{dir
= Config
.libRoot
,
56 val (_
, files
) = Order
.order files
58 if !ErrorMsg
.anyErrors
then
61 (Tycheck
.allowExterns ();
62 foldl (fn (fname
, G
) => check
' G fname
) Env
.empty files
63 before Tycheck
.disallowExterns ())
68 val _
= ErrorMsg
.reset ()
69 val _
= Env
.preTycheck ()
73 if !ErrorMsg
.anyErrors
then
77 val _
= Tycheck
.disallowExterns ()
78 val _
= ErrorMsg
.reset ()
79 val prog
= Parse
.parse fname
81 if !ErrorMsg
.anyErrors
then
85 val G
' = Tycheck
.checkFile
b (Defaults
.tInit ()) prog
87 if !ErrorMsg
.anyErrors
then
97 val (G
, body
) = check fname
99 if !ErrorMsg
.anyErrors
then
105 val body
' = Reduce
.reduceExp G body
107 (*printd (PD
.hovBox (PD
.PPS
.Rel
0,
108 [PD
.string "Result:",
119 if !ErrorMsg
.anyErrors
then
122 Eval
.exec (Defaults
.eInit ()) body
'
123 | NONE
=> raise ErrorMsg
.Error
126 Config
.dispatcher ^
":" ^
Int.toString Config
.dispatcherPort
128 fun requestContext f
=
130 val uid
= Posix
.ProcEnv
.getuid ()
131 val user
= Posix
.SysDB
.Passwd
.name (Posix
.SysDB
.getpwuid uid
)
133 val () = Acl
.read Config
.aclFile
134 val () = Domain
.setUser user
138 val context
= OpenSSL
.context (Config
.certDir ^
"/" ^ user ^
".pem",
139 Config
.keyDir ^
"/" ^ user ^
"/key.pem",
147 val (user
, context
) = requestContext f
149 (user
, OpenSSL
.connect (context
, dispatcher
))
154 val (user
, bio
) = requestBio (fn () => ignore (check fname
))
156 val inf
= TextIO.openIn fname
159 case TextIO.inputLine inf
of
160 NONE
=> String.concat (List.rev lines
)
161 | SOME line
=> loop (line
:: lines
)
166 Msg
.send (bio
, MsgConfig code
);
168 NONE
=> print
"Server closed connection unexpectedly.\n"
171 MsgOk
=> print
"Configuration succeeded.\n"
172 | MsgError s
=> print ("Configuration failed: " ^ s ^
"\n")
173 | _
=> print
"Unexpected server reply.\n";
176 handle ErrorMsg
.Error
=> ()
178 fun requestGrant acl
=
180 val (user
, bio
) = requestBio (fn () => ())
182 Msg
.send (bio
, MsgGrant acl
);
184 NONE
=> print
"Server closed connection unexpectedly.\n"
187 MsgOk
=> print
"Grant succeeded.\n"
188 | MsgError s
=> print ("Grant failed: " ^ s ^
"\n")
189 | _
=> print
"Unexpected server reply.\n";
193 fun requestRevoke acl
=
195 val (user
, bio
) = requestBio (fn () => ())
197 Msg
.send (bio
, MsgRevoke acl
);
199 NONE
=> print
"Server closed connection unexpectedly.\n"
202 MsgOk
=> print
"Revoke succeeded.\n"
203 | MsgError s
=> print ("Revoke failed: " ^ s ^
"\n")
204 | _
=> print
"Unexpected server reply.\n";
208 fun requestListPerms user
=
210 val (_
, bio
) = requestBio (fn () => ())
212 Msg
.send (bio
, MsgListPerms user
);
213 (case Msg
.recv bio
of
214 NONE
=> (print
"Server closed connection unexpectedly.\n";
218 MsgPerms perms
=> SOME perms
219 | MsgError s
=> (print ("Listing failed: " ^ s ^
"\n");
221 | _
=> (print
"Unexpected server reply.\n";
223 before OpenSSL
.close bio
226 fun requestWhoHas perm
=
228 val (_
, bio
) = requestBio (fn () => ())
230 Msg
.send (bio
, MsgWhoHas perm
);
231 (case Msg
.recv bio
of
232 NONE
=> (print
"Server closed connection unexpectedly.\n";
236 MsgWhoHasResponse users
=> SOME users
237 | MsgError s
=> (print ("whohas failed: " ^ s ^
"\n");
239 | _
=> (print
"Unexpected server reply.\n";
241 before OpenSSL
.close bio
246 val () = Acl
.read Config
.aclFile
248 val context
= OpenSSL
.context (Config
.serverCert
,
251 val _
= Domain
.set_context context
253 val sock
= OpenSSL
.listen (context
, Config
.dispatcherPort
)
256 case OpenSSL
.accept sock
of
260 val user
= OpenSSL
.peerCN bio
261 val () = print ("\nConnection from " ^ user ^
"\n")
262 val () = Domain
.setUser user
266 NONE
=> (OpenSSL
.close bio
267 handle OpenSSL
.OpenSSL _
=> ();
273 val _
= print
"Configuration:\n"
277 val outname
= OS
.FileSys
.tmpName ()
278 val outf
= TextIO.openOut outname
280 TextIO.output (outf
, code
);
281 TextIO.closeOut outf
;
283 Msg
.send (bio
, MsgOk
))
284 handle ErrorMsg
.Error
=>
285 (print
"Compilation error\n";
287 MsgError
"Error during configuration evaluation"))
288 | OpenSSL
.OpenSSL s
=>
289 (print
"OpenSSL error\n";
292 ("Error during configuration evaluation: "
294 OS
.FileSys
.remove outname
;
295 (ignore (OpenSSL
.readChar bio
);
297 handle OpenSSL
.OpenSSL _
=> ();
302 if Acl
.query
{user
= user
, class
= "priv", value
= "all"} then
304 Acl
.write Config
.aclFile
;
305 Msg
.send (bio
, MsgOk
);
306 print ("Granted permission " ^ #value acl ^
" to " ^ #user acl ^
" in " ^ #class acl ^
".\n"))
307 handle OpenSSL
.OpenSSL s
=>
308 (print
"OpenSSL error\n";
311 ("Error during granting: "
313 (ignore (OpenSSL
.readChar bio
);
315 handle OpenSSL
.OpenSSL _
=> ();
318 ((Msg
.send (bio
, MsgError
"Not authorized to grant privileges");
319 print
"Unauthorized user asked to grant a permission!\n";
320 ignore (OpenSSL
.readChar bio
);
322 handle OpenSSL
.OpenSSL _
=> ();
326 if Acl
.query
{user
= user
, class
= "priv", value
= "all"} then
328 Acl
.write Config
.aclFile
;
329 Msg
.send (bio
, MsgOk
);
330 print ("Revoked permission " ^ #value acl ^
" from " ^ #user acl ^
" in " ^ #class acl ^
".\n"))
331 handle OpenSSL
.OpenSSL s
=>
332 (print
"OpenSSL error\n";
335 ("Error during revocation: "
337 (ignore (OpenSSL
.readChar bio
);
339 handle OpenSSL
.OpenSSL _
=> ();
342 ((Msg
.send (bio
, MsgError
"Not authorized to revoke privileges");
343 print
"Unauthorized user asked to revoke a permission!\n";
344 ignore (OpenSSL
.readChar bio
);
346 handle OpenSSL
.OpenSSL _
=> ();
349 | MsgListPerms user
=>
350 ((Msg
.send (bio
, MsgPerms (Acl
.queryAll user
));
351 print ("Sent permission list for user " ^ user ^
".\n"))
352 handle OpenSSL
.OpenSSL s
=>
353 (print
"OpenSSL error\n";
356 ("Error during permission listing: "
358 (ignore (OpenSSL
.readChar bio
);
360 handle OpenSSL
.OpenSSL _
=> ();
364 ((Msg
.send (bio
, MsgWhoHasResponse (Acl
.whoHas perm
));
365 print ("Sent whohas response for " ^ #class perm ^
" / " ^ #value perm ^
".\n"))
366 handle OpenSSL
.OpenSSL s
=>
367 (print
"OpenSSL error\n";
370 ("Error during whohas: "
372 (ignore (OpenSSL
.readChar bio
);
374 handle OpenSSL
.OpenSSL _
=> ();
378 (Msg
.send (bio
, MsgError
"Unexpected command")
379 handle OpenSSL
.OpenSSL _
=> ();
381 handle OpenSSL
.OpenSSL _
=> ();
386 handle OpenSSL
.OpenSSL s
=>
387 (print ("OpenSSL error: " ^ s ^
"\n");
389 handle OpenSSL
.OpenSSL _
=> ();
391 | OS
.SysErr (s
, _
) =>
392 (print ("System error: " ^ s ^
"\n");
394 handle OpenSSL
.OpenSSL _
=> ();
397 print
"Listening for connections....\n";
399 OpenSSL
.shutdown sock
404 val host
= Slave
.hostname ()
406 val context
= OpenSSL
.context (Config
.certDir ^
"/" ^ host ^
".pem",
407 Config
.keyDir ^
"/" ^ host ^
"/key.pem",
410 val sock
= OpenSSL
.listen (context
, Config
.slavePort
)
413 case OpenSSL
.accept sock
of
417 val peer
= OpenSSL
.peerCN bio
418 val () = print ("\nConnection from " ^ peer ^
"\n")
420 if peer
<> Config
.dispatcherName
then
421 (print
"Not authorized!\n";
427 NONE
=> print
"Dispatcher closed connection unexpectedly\n"
430 MsgFile file
=> loop
' (file
:: files
)
431 | MsgDoFiles
=> (Slave
.handleChanges files
;
432 Msg
.send (bio
, MsgOk
))
433 | _
=> (print
"Dispatcher sent unexpected command\n";
434 Msg
.send (bio
, MsgError
"Unexpected command"))
437 ignore (OpenSSL
.readChar bio
);
441 end handle OpenSSL
.OpenSSL s
=>
442 (print ("OpenSSL error: "^ s ^
"\n");
444 handle OpenSSL
.OpenSSL _
=> ();
446 | OS
.SysErr (s
, _
) =>
447 (print ("System error: "^ s ^
"\n");
449 handle OpenSSL
.OpenSSL _
=> ();
453 OpenSSL
.shutdown sock
456 fun autodocBasis outdir
=
458 val dir
= Posix
.FileSys
.opendir Config
.libRoot
461 case Posix
.FileSys
.readdir dir
of
462 NONE
=> (Posix
.FileSys
.closedir dir
;
465 if String.isSuffix
".dtl" fname
then
466 loop (OS
.Path
.joinDirFile
{dir
= Config
.libRoot
,
474 Autodoc
.autodoc
{outdir
= outdir
, infiles
= files
}