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
;
122 if !ErrorMsg
.anyErrors
then
130 val (G
, body
) = check fname
132 if !ErrorMsg
.anyErrors
then
138 val body
' = Reduce
.reduceExp G body
140 (*printd (PD
.hovBox (PD
.PPS
.Rel
0,
141 [PD
.string "Result:",
152 if !ErrorMsg
.anyErrors
then
155 Eval
.exec (Defaults
.eInit ()) body
'
156 | NONE
=> raise ErrorMsg
.Error
161 if !ErrorMsg
.anyErrors
then
164 ignore (Eval
.exec
' (Defaults
.eInit ()) body
')
165 | NONE
=> raise ErrorMsg
.Error
168 Config
.dispatcher ^
":" ^
Int.toString Config
.dispatcherPort
170 fun requestContext f
=
172 val uid
= Posix
.ProcEnv
.getuid ()
173 val user
= Posix
.SysDB
.Passwd
.name (Posix
.SysDB
.getpwuid uid
)
175 val () = Acl
.read Config
.aclFile
176 val () = Domain
.setUser user
180 val context
= OpenSSL
.context (Config
.certDir ^
"/" ^ user ^
".pem",
181 Config
.keyDir ^
"/" ^ user ^
"/key.pem",
189 val (user
, context
) = requestContext f
191 (user
, OpenSSL
.connect (context
, dispatcher
))
196 val (user
, bio
) = requestBio (fn () => ignore (check fname
))
198 val inf
= TextIO.openIn fname
201 case TextIO.inputLine inf
of
202 NONE
=> String.concat (List.rev lines
)
203 | SOME line
=> loop (line
:: lines
)
208 Msg
.send (bio
, MsgConfig code
);
210 NONE
=> print
"Server closed connection unexpectedly.\n"
213 MsgOk
=> print
"Configuration succeeded.\n"
214 | MsgError s
=> print ("Configuration failed: " ^ s ^
"\n")
215 | _
=> print
"Unexpected server reply.\n";
218 handle ErrorMsg
.Error
=> ()
220 fun requestDir dname
=
222 val _
= ErrorMsg
.reset ()
224 val (user
, bio
) = requestBio (fn () => checkDir dname
)
228 val dir
= Posix
.FileSys
.opendir dname
231 case Posix
.FileSys
.readdir dir
of
232 NONE
=> (Posix
.FileSys
.closedir dir
;
236 loop (OS
.Path
.joinDirFile
{dir
= dname
,
243 val (_
, files
) = Order
.order (SOME b
) files
245 val _
= if !ErrorMsg
.anyErrors
then
250 val codes
= map (fn fname
=>
252 val inf
= TextIO.openIn fname
255 case TextIO.inputLine inf
of
256 NONE
=> String.concat (rev lines
)
257 | SOME line
=> loop (line
:: lines
)
260 before TextIO.closeIn inf
263 if !ErrorMsg
.anyErrors
then
266 (Msg
.send (bio
, MsgMultiConfig codes
);
268 NONE
=> print
"Server closed connection unexpectedly.\n"
271 MsgOk
=> print
"Configuration succeeded.\n"
272 | MsgError s
=> print ("Configuration failed: " ^ s ^
"\n")
273 | _
=> print
"Unexpected server reply.\n";
276 handle ErrorMsg
.Error
=> ()
278 fun requestGrant acl
=
280 val (user
, bio
) = requestBio (fn () => ())
282 Msg
.send (bio
, MsgGrant acl
);
284 NONE
=> print
"Server closed connection unexpectedly.\n"
287 MsgOk
=> print
"Grant succeeded.\n"
288 | MsgError s
=> print ("Grant failed: " ^ s ^
"\n")
289 | _
=> print
"Unexpected server reply.\n";
293 fun requestRevoke acl
=
295 val (user
, bio
) = requestBio (fn () => ())
297 Msg
.send (bio
, MsgRevoke acl
);
299 NONE
=> print
"Server closed connection unexpectedly.\n"
302 MsgOk
=> print
"Revoke succeeded.\n"
303 | MsgError s
=> print ("Revoke failed: " ^ s ^
"\n")
304 | _
=> print
"Unexpected server reply.\n";
308 fun requestListPerms user
=
310 val (_
, bio
) = requestBio (fn () => ())
312 Msg
.send (bio
, MsgListPerms user
);
313 (case Msg
.recv bio
of
314 NONE
=> (print
"Server closed connection unexpectedly.\n";
318 MsgPerms perms
=> SOME perms
319 | MsgError s
=> (print ("Listing failed: " ^ s ^
"\n");
321 | _
=> (print
"Unexpected server reply.\n";
323 before OpenSSL
.close bio
326 fun requestWhoHas perm
=
328 val (_
, bio
) = requestBio (fn () => ())
330 Msg
.send (bio
, MsgWhoHas perm
);
331 (case Msg
.recv bio
of
332 NONE
=> (print
"Server closed connection unexpectedly.\n";
336 MsgWhoHasResponse users
=> SOME users
337 | MsgError s
=> (print ("whohas failed: " ^ s ^
"\n");
339 | _
=> (print
"Unexpected server reply.\n";
341 before OpenSSL
.close bio
344 fun requestRegen () =
346 val (_
, bio
) = requestBio (fn () => ())
348 Msg
.send (bio
, MsgRegenerate
);
350 NONE
=> print
"Server closed connection unexpectedly.\n"
353 MsgOk
=> print
"Regeneration succeeded.\n"
354 | MsgError s
=> print ("Regeneration failed: " ^ s ^
"\n")
355 | _
=> print
"Unexpected server reply.\n";
359 fun requestRmdom dom
=
361 val (_
, bio
) = requestBio (fn () => ())
363 Msg
.send (bio
, MsgRmdom dom
);
365 NONE
=> print
"Server closed connection unexpectedly.\n"
368 MsgOk
=> print
"Removal succeeded.\n"
369 | MsgError s
=> print ("Removal failed: " ^ s ^
"\n")
370 | _
=> print
"Unexpected server reply.\n";
374 fun regenerate context
=
377 val () = Tycheck
.disallowExterns ()
379 val () = Domain
.resetGlobal ()
381 fun contactNode (node
, ip
) =
382 if node
= Config
.defaultNode
then
385 val bio
= OpenSSL
.connect (context
,
388 ^
Int.toString Config
.slavePort
)
390 Msg
.send (bio
, MsgRegenerate
);
392 NONE
=> print
"Slave closed connection unexpectedly\n"
395 MsgOk
=> print ("Slave " ^ node ^
" pre-regeneration finished\n")
396 | MsgError s
=> print ("Slave " ^ node
397 ^
" returned error: " ^
399 | _
=> print ("Slave " ^ node
400 ^
" returned unexpected command\n");
406 val _
= Domain
.setUser user
407 val _
= ErrorMsg
.reset ()
409 val dname
= Config
.domtoolDir user
411 val dir
= Posix
.FileSys
.opendir dname
414 case Posix
.FileSys
.readdir dir
of
415 NONE
=> (Posix
.FileSys
.closedir dir
;
419 loop (OS
.Path
.joinDirFile
{dir
= dname
,
426 val (_
, files
) = Order
.order (SOME b
) files
428 if !ErrorMsg
.anyErrors
then
429 print ("User " ^ user ^
"'s configuration has errors!\n")
434 | OS
.SysErr (s
, _
) => print ("System error processing user " ^ user ^
": " ^ s ^
"\n")
436 app contactNode Config
.nodeIps
;
438 app
doUser (Acl
.users ());
444 val () = Acl
.read Config
.aclFile
446 val context
= OpenSSL
.context (Config
.serverCert
,
449 val _
= Domain
.set_context context
451 val sock
= OpenSSL
.listen (context
, Config
.dispatcherPort
)
454 case OpenSSL
.accept sock
of
458 val user
= OpenSSL
.peerCN bio
459 val () = print ("\nConnection from " ^ user ^
"\n")
460 val () = Domain
.setUser user
464 val _
= print
"Configuration:\n"
465 val _
= app (fn s
=> (print s
; print
"\n")) codes
468 val outname
= OS
.FileSys
.tmpName ()
472 val outf
= TextIO.openOut outname
474 TextIO.output (outf
, code
);
475 TextIO.closeOut outf
;
482 Msg
.send (bio
, MsgOk
))
483 handle ErrorMsg
.Error
=>
484 (print
"Compilation error\n";
486 MsgError
"Error during configuration evaluation"))
487 | OpenSSL
.OpenSSL s
=>
488 (print
"OpenSSL error\n";
491 ("Error during configuration evaluation: "
493 OS
.FileSys
.remove outname
;
494 (ignore (OpenSSL
.readChar bio
);
496 handle OpenSSL
.OpenSSL _
=> ();
502 NONE
=> (OpenSSL
.close bio
503 handle OpenSSL
.OpenSSL _
=> ();
507 MsgConfig code
=> doConfig
[code
]
508 | MsgMultiConfig codes
=> doConfig codes
511 if Acl
.query
{user
= user
, class
= "priv", value
= "all"} then
513 Acl
.write Config
.aclFile
;
514 Msg
.send (bio
, MsgOk
);
515 print ("Granted permission " ^ #value acl ^
" to " ^ #user acl ^
" in " ^ #class acl ^
".\n"))
516 handle OpenSSL
.OpenSSL s
=>
517 (print
"OpenSSL error\n";
520 ("Error during granting: "
522 (ignore (OpenSSL
.readChar bio
);
524 handle OpenSSL
.OpenSSL _
=> ();
527 ((Msg
.send (bio
, MsgError
"Not authorized to grant privileges");
528 print
"Unauthorized user asked to grant a permission!\n";
529 ignore (OpenSSL
.readChar bio
);
531 handle OpenSSL
.OpenSSL _
=> ();
535 if Acl
.query
{user
= user
, class
= "priv", value
= "all"} then
537 Acl
.write Config
.aclFile
;
538 Msg
.send (bio
, MsgOk
);
539 print ("Revoked permission " ^ #value acl ^
" from " ^ #user acl ^
" in " ^ #class acl ^
".\n"))
540 handle OpenSSL
.OpenSSL s
=>
541 (print
"OpenSSL error\n";
544 ("Error during revocation: "
546 (ignore (OpenSSL
.readChar bio
);
548 handle OpenSSL
.OpenSSL _
=> ();
551 ((Msg
.send (bio
, MsgError
"Not authorized to revoke privileges");
552 print
"Unauthorized user asked to revoke a permission!\n";
553 ignore (OpenSSL
.readChar bio
);
555 handle OpenSSL
.OpenSSL _
=> ();
558 | MsgListPerms user
=>
559 ((Msg
.send (bio
, MsgPerms (Acl
.queryAll user
));
560 print ("Sent permission list for user " ^ user ^
".\n"))
561 handle OpenSSL
.OpenSSL s
=>
562 (print
"OpenSSL error\n";
565 ("Error during permission listing: "
567 (ignore (OpenSSL
.readChar bio
);
569 handle OpenSSL
.OpenSSL _
=> ();
573 ((Msg
.send (bio
, MsgWhoHasResponse (Acl
.whoHas perm
));
574 print ("Sent whohas response for " ^ #class perm ^
" / " ^ #value perm ^
".\n"))
575 handle OpenSSL
.OpenSSL s
=>
576 (print
"OpenSSL error\n";
579 ("Error during whohas: "
581 (ignore (OpenSSL
.readChar bio
);
583 handle OpenSSL
.OpenSSL _
=> ();
587 if Acl
.query
{user
= user
, class
= "priv", value
= "all"}
588 orelse Acl
.query
{user
= user
, class
= "domain", value
= dom
} then
590 Msg
.send (bio
, MsgOk
);
591 print ("Removed domain " ^ dom ^
".\n"))
592 handle OpenSSL
.OpenSSL s
=>
593 (print
"OpenSSL error\n";
596 ("Error during revocation: "
598 (ignore (OpenSSL
.readChar bio
);
600 handle OpenSSL
.OpenSSL _
=> ();
603 ((Msg
.send (bio
, MsgError
"Not authorized to remove that domain");
604 print
"Unauthorized user asked to remove a domain!\n";
605 ignore (OpenSSL
.readChar bio
);
607 handle OpenSSL
.OpenSSL _
=> ();
611 if Acl
.query
{user
= user
, class
= "priv", value
= "regen"}
612 orelse Acl
.query
{user
= user
, class
= "priv", value
= "all"} then
613 ((regenerate context
;
614 Msg
.send (bio
, MsgOk
);
615 print
"Regenerated all configuration.\n")
616 handle OpenSSL
.OpenSSL s
=>
617 (print
"OpenSSL error\n";
620 ("Error during regeneration: "
622 (ignore (OpenSSL
.readChar bio
);
624 handle OpenSSL
.OpenSSL _
=> ();
627 ((Msg
.send (bio
, MsgError
"Not authorized to regeneration");
628 print
"Unauthorized user asked to regenerate!\n";
629 ignore (OpenSSL
.readChar bio
);
631 handle OpenSSL
.OpenSSL _
=> ();
635 (Msg
.send (bio
, MsgError
"Unexpected command")
636 handle OpenSSL
.OpenSSL _
=> ();
638 handle OpenSSL
.OpenSSL _
=> ();
643 handle OpenSSL
.OpenSSL s
=>
644 (print ("OpenSSL error: " ^ s ^
"\n");
646 handle OpenSSL
.OpenSSL _
=> ();
648 | OS
.SysErr (s
, _
) =>
649 (print ("System error: " ^ s ^
"\n");
651 handle OpenSSL
.OpenSSL _
=> ();
654 print
"Listening for connections....\n";
656 OpenSSL
.shutdown sock
661 val host
= Slave
.hostname ()
663 val context
= OpenSSL
.context (Config
.certDir ^
"/" ^ host ^
".pem",
664 Config
.keyDir ^
"/" ^ host ^
"/key.pem",
667 val sock
= OpenSSL
.listen (context
, Config
.slavePort
)
670 case OpenSSL
.accept sock
of
674 val peer
= OpenSSL
.peerCN bio
675 val () = print ("\nConnection from " ^ peer ^
"\n")
677 if peer
<> Config
.dispatcherName
then
678 (print
"Not authorized!\n";
684 NONE
=> print
"Dispatcher closed connection unexpectedly\n"
687 MsgFile file
=> loop
' (file
:: files
)
688 | MsgDoFiles
=> (Slave
.handleChanges files
;
689 Msg
.send (bio
, MsgOk
))
690 | MsgRegenerate
=> (Domain
.resetLocal ();
691 Msg
.send (bio
, MsgOk
))
692 | _
=> (print
"Dispatcher sent unexpected command\n";
693 Msg
.send (bio
, MsgError
"Unexpected command"))
696 ignore (OpenSSL
.readChar bio
);
700 end handle OpenSSL
.OpenSSL s
=>
701 (print ("OpenSSL error: "^ s ^
"\n");
703 handle OpenSSL
.OpenSSL _
=> ();
705 | OS
.SysErr (s
, _
) =>
706 (print ("System error: "^ s ^
"\n");
708 handle OpenSSL
.OpenSSL _
=> ();
712 OpenSSL
.shutdown sock
715 fun autodocBasis outdir
=
717 val dir
= Posix
.FileSys
.opendir Config
.libRoot
720 case Posix
.FileSys
.readdir dir
of
721 NONE
=> (Posix
.FileSys
.closedir dir
;
724 if String.isSuffix
".dtl" fname
then
725 loop (OS
.Path
.joinDirFile
{dir
= Config
.libRoot
,
733 Autodoc
.autodoc
{outdir
= outdir
, infiles
= files
}