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 NONE 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
95 val notTmp
= CharVector
.all (fn ch
=> Char.isAlphaNum ch
orelse ch
= #
"." orelse ch
= #
"_" orelse ch
= #
"-")
101 val dir
= Posix
.FileSys
.opendir dname
104 case Posix
.FileSys
.readdir dir
of
105 NONE
=> (Posix
.FileSys
.closedir dir
;
109 loop (OS
.Path
.joinDirFile
{dir
= dname
,
116 val (_
, files
) = Order
.order (SOME b
) files
118 if !ErrorMsg
.anyErrors
then
121 (foldl (fn (fname
, G
) => check
' G fname
) b files
;
127 val (G
, body
) = check fname
129 if !ErrorMsg
.anyErrors
then
135 val body
' = Reduce
.reduceExp G body
137 (*printd (PD
.hovBox (PD
.PPS
.Rel
0,
138 [PD
.string "Result:",
149 if !ErrorMsg
.anyErrors
then
152 Eval
.exec (Defaults
.eInit ()) body
'
153 | NONE
=> raise ErrorMsg
.Error
156 Config
.dispatcher ^
":" ^
Int.toString Config
.dispatcherPort
158 fun requestContext f
=
160 val uid
= Posix
.ProcEnv
.getuid ()
161 val user
= Posix
.SysDB
.Passwd
.name (Posix
.SysDB
.getpwuid uid
)
163 val () = Acl
.read Config
.aclFile
164 val () = Domain
.setUser user
168 val context
= OpenSSL
.context (Config
.certDir ^
"/" ^ user ^
".pem",
169 Config
.keyDir ^
"/" ^ user ^
"/key.pem",
177 val (user
, context
) = requestContext f
179 (user
, OpenSSL
.connect (context
, dispatcher
))
184 val (user
, bio
) = requestBio (fn () => ignore (check fname
))
186 val inf
= TextIO.openIn fname
189 case TextIO.inputLine inf
of
190 NONE
=> String.concat (List.rev lines
)
191 | SOME line
=> loop (line
:: lines
)
196 Msg
.send (bio
, MsgConfig code
);
198 NONE
=> print
"Server closed connection unexpectedly.\n"
201 MsgOk
=> print
"Configuration succeeded.\n"
202 | MsgError s
=> print ("Configuration failed: " ^ s ^
"\n")
203 | _
=> print
"Unexpected server reply.\n";
206 handle ErrorMsg
.Error
=> ()
208 fun requestDir dname
=
210 val (user
, bio
) = requestBio (fn () => ignore (checkDir dname
))
214 val dir
= Posix
.FileSys
.opendir dname
217 case Posix
.FileSys
.readdir dir
of
218 NONE
=> (Posix
.FileSys
.closedir dir
;
222 loop (OS
.Path
.joinDirFile
{dir
= dname
,
229 val (_
, files
) = Order
.order (SOME b
) files
231 val _
= if !ErrorMsg
.anyErrors
then
236 val codes
= map (fn fname
=>
238 val inf
= TextIO.openIn fname
241 case TextIO.inputLine inf
of
242 NONE
=> String.concat (rev lines
)
243 | SOME line
=> loop (line
:: lines
)
246 before TextIO.closeIn inf
249 Msg
.send (bio
, MsgMultiConfig codes
);
251 NONE
=> print
"Server closed connection unexpectedly.\n"
254 MsgOk
=> print
"Configuration succeeded.\n"
255 | MsgError s
=> print ("Configuration failed: " ^ s ^
"\n")
256 | _
=> print
"Unexpected server reply.\n";
259 handle ErrorMsg
.Error
=> ()
261 fun requestGrant acl
=
263 val (user
, bio
) = requestBio (fn () => ())
265 Msg
.send (bio
, MsgGrant acl
);
267 NONE
=> print
"Server closed connection unexpectedly.\n"
270 MsgOk
=> print
"Grant succeeded.\n"
271 | MsgError s
=> print ("Grant failed: " ^ s ^
"\n")
272 | _
=> print
"Unexpected server reply.\n";
276 fun requestRevoke acl
=
278 val (user
, bio
) = requestBio (fn () => ())
280 Msg
.send (bio
, MsgRevoke acl
);
282 NONE
=> print
"Server closed connection unexpectedly.\n"
285 MsgOk
=> print
"Revoke succeeded.\n"
286 | MsgError s
=> print ("Revoke failed: " ^ s ^
"\n")
287 | _
=> print
"Unexpected server reply.\n";
291 fun requestListPerms user
=
293 val (_
, bio
) = requestBio (fn () => ())
295 Msg
.send (bio
, MsgListPerms user
);
296 (case Msg
.recv bio
of
297 NONE
=> (print
"Server closed connection unexpectedly.\n";
301 MsgPerms perms
=> SOME perms
302 | MsgError s
=> (print ("Listing failed: " ^ s ^
"\n");
304 | _
=> (print
"Unexpected server reply.\n";
306 before OpenSSL
.close bio
309 fun requestWhoHas perm
=
311 val (_
, bio
) = requestBio (fn () => ())
313 Msg
.send (bio
, MsgWhoHas perm
);
314 (case Msg
.recv bio
of
315 NONE
=> (print
"Server closed connection unexpectedly.\n";
319 MsgWhoHasResponse users
=> SOME users
320 | MsgError s
=> (print ("whohas failed: " ^ s ^
"\n");
322 | _
=> (print
"Unexpected server reply.\n";
324 before OpenSSL
.close bio
327 fun requestRmdom dom
=
329 val (_
, bio
) = requestBio (fn () => ())
331 Msg
.send (bio
, MsgRmdom dom
);
333 NONE
=> print
"Server closed connection unexpectedly.\n"
336 MsgOk
=> print
"Removal succeeded.\n"
337 | MsgError s
=> print ("Removal failed: " ^ s ^
"\n")
338 | _
=> print
"Unexpected server reply.\n";
344 val () = Acl
.read Config
.aclFile
346 val context
= OpenSSL
.context (Config
.serverCert
,
349 val _
= Domain
.set_context context
351 val sock
= OpenSSL
.listen (context
, Config
.dispatcherPort
)
354 case OpenSSL
.accept sock
of
358 val user
= OpenSSL
.peerCN bio
359 val () = print ("\nConnection from " ^ user ^
"\n")
360 val () = Domain
.setUser user
364 val _
= print
"Configuration:\n"
365 val _
= app (fn s
=> (print s
; print
"\n")) codes
368 val outname
= OS
.FileSys
.tmpName ()
372 val outf
= TextIO.openOut outname
374 TextIO.output (outf
, code
);
375 TextIO.closeOut outf
;
380 Msg
.send (bio
, MsgOk
))
381 handle ErrorMsg
.Error
=>
382 (print
"Compilation error\n";
384 MsgError
"Error during configuration evaluation"))
385 | OpenSSL
.OpenSSL s
=>
386 (print
"OpenSSL error\n";
389 ("Error during configuration evaluation: "
391 OS
.FileSys
.remove outname
;
392 (ignore (OpenSSL
.readChar bio
);
394 handle OpenSSL
.OpenSSL _
=> ();
400 NONE
=> (OpenSSL
.close bio
401 handle OpenSSL
.OpenSSL _
=> ();
405 MsgConfig code
=> doConfig
[code
]
406 | MsgMultiConfig codes
=> doConfig codes
409 if Acl
.query
{user
= user
, class
= "priv", value
= "all"} then
411 Acl
.write Config
.aclFile
;
412 Msg
.send (bio
, MsgOk
);
413 print ("Granted permission " ^ #value acl ^
" to " ^ #user acl ^
" in " ^ #class acl ^
".\n"))
414 handle OpenSSL
.OpenSSL s
=>
415 (print
"OpenSSL error\n";
418 ("Error during granting: "
420 (ignore (OpenSSL
.readChar bio
);
422 handle OpenSSL
.OpenSSL _
=> ();
425 ((Msg
.send (bio
, MsgError
"Not authorized to grant privileges");
426 print
"Unauthorized user asked to grant a permission!\n";
427 ignore (OpenSSL
.readChar bio
);
429 handle OpenSSL
.OpenSSL _
=> ();
433 if Acl
.query
{user
= user
, class
= "priv", value
= "all"} then
435 Acl
.write Config
.aclFile
;
436 Msg
.send (bio
, MsgOk
);
437 print ("Revoked permission " ^ #value acl ^
" from " ^ #user acl ^
" in " ^ #class acl ^
".\n"))
438 handle OpenSSL
.OpenSSL s
=>
439 (print
"OpenSSL error\n";
442 ("Error during revocation: "
444 (ignore (OpenSSL
.readChar bio
);
446 handle OpenSSL
.OpenSSL _
=> ();
449 ((Msg
.send (bio
, MsgError
"Not authorized to revoke privileges");
450 print
"Unauthorized user asked to revoke a permission!\n";
451 ignore (OpenSSL
.readChar bio
);
453 handle OpenSSL
.OpenSSL _
=> ();
456 | MsgListPerms user
=>
457 ((Msg
.send (bio
, MsgPerms (Acl
.queryAll user
));
458 print ("Sent permission list for user " ^ user ^
".\n"))
459 handle OpenSSL
.OpenSSL s
=>
460 (print
"OpenSSL error\n";
463 ("Error during permission listing: "
465 (ignore (OpenSSL
.readChar bio
);
467 handle OpenSSL
.OpenSSL _
=> ();
471 ((Msg
.send (bio
, MsgWhoHasResponse (Acl
.whoHas perm
));
472 print ("Sent whohas response for " ^ #class perm ^
" / " ^ #value perm ^
".\n"))
473 handle OpenSSL
.OpenSSL s
=>
474 (print
"OpenSSL error\n";
477 ("Error during whohas: "
479 (ignore (OpenSSL
.readChar bio
);
481 handle OpenSSL
.OpenSSL _
=> ();
485 if Acl
.query
{user
= user
, class
= "priv", value
= "all"}
486 orelse Acl
.query
{user
= user
, class
= "domain", value
= dom
} then
488 Msg
.send (bio
, MsgOk
);
489 print ("Removed domain " ^ dom ^
".\n"))
490 handle OpenSSL
.OpenSSL s
=>
491 (print
"OpenSSL error\n";
494 ("Error during revocation: "
496 (ignore (OpenSSL
.readChar bio
);
498 handle OpenSSL
.OpenSSL _
=> ();
501 ((Msg
.send (bio
, MsgError
"Not authorized to remove that domain");
502 print
"Unauthorized user asked to remove a domain!\n";
503 ignore (OpenSSL
.readChar bio
);
505 handle OpenSSL
.OpenSSL _
=> ();
509 (Msg
.send (bio
, MsgError
"Unexpected command")
510 handle OpenSSL
.OpenSSL _
=> ();
512 handle OpenSSL
.OpenSSL _
=> ();
517 handle OpenSSL
.OpenSSL s
=>
518 (print ("OpenSSL error: " ^ s ^
"\n");
520 handle OpenSSL
.OpenSSL _
=> ();
522 | OS
.SysErr (s
, _
) =>
523 (print ("System error: " ^ s ^
"\n");
525 handle OpenSSL
.OpenSSL _
=> ();
528 print
"Listening for connections....\n";
530 OpenSSL
.shutdown sock
535 val host
= Slave
.hostname ()
537 val context
= OpenSSL
.context (Config
.certDir ^
"/" ^ host ^
".pem",
538 Config
.keyDir ^
"/" ^ host ^
"/key.pem",
541 val sock
= OpenSSL
.listen (context
, Config
.slavePort
)
544 case OpenSSL
.accept sock
of
548 val peer
= OpenSSL
.peerCN bio
549 val () = print ("\nConnection from " ^ peer ^
"\n")
551 if peer
<> Config
.dispatcherName
then
552 (print
"Not authorized!\n";
558 NONE
=> print
"Dispatcher closed connection unexpectedly\n"
561 MsgFile file
=> loop
' (file
:: files
)
562 | MsgDoFiles
=> (Slave
.handleChanges files
;
563 Msg
.send (bio
, MsgOk
))
564 | _
=> (print
"Dispatcher sent unexpected command\n";
565 Msg
.send (bio
, MsgError
"Unexpected command"))
568 ignore (OpenSSL
.readChar bio
);
572 end handle OpenSSL
.OpenSSL s
=>
573 (print ("OpenSSL error: "^ s ^
"\n");
575 handle OpenSSL
.OpenSSL _
=> ();
577 | OS
.SysErr (s
, _
) =>
578 (print ("System error: "^ s ^
"\n");
580 handle OpenSSL
.OpenSSL _
=> ();
584 OpenSSL
.shutdown sock
587 fun autodocBasis outdir
=
589 val dir
= Posix
.FileSys
.opendir Config
.libRoot
592 case Posix
.FileSys
.readdir dir
of
593 NONE
=> (Posix
.FileSys
.closedir dir
;
596 if String.isSuffix
".dtl" fname
then
597 loop (OS
.Path
.joinDirFile
{dir
= Config
.libRoot
,
605 Autodoc
.autodoc
{outdir
= outdir
, infiles
= files
}