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 requestRmuser user
=
376 val (_
, bio
) = requestBio (fn () => ())
378 Msg
.send (bio
, MsgRmuser user
);
380 NONE
=> print
"Server closed connection unexpectedly.\n"
383 MsgOk
=> print
"Removal succeeded.\n"
384 | MsgError s
=> print ("Removal failed: " ^ s ^
"\n")
385 | _
=> print
"Unexpected server reply.\n";
389 fun requestDbUser dbtype
=
391 val (_
, bio
) = requestBio (fn () => ())
393 Msg
.send (bio
, MsgCreateDbUser dbtype
);
395 NONE
=> print
"Server closed connection unexpectedly.\n"
398 MsgOk
=> print
"Your user has been created.\n"
399 | MsgError s
=> print ("Creation failed: " ^ s ^
"\n")
400 | _
=> print
"Unexpected server reply.\n";
404 fun requestDbTable p
=
406 val (user
, bio
) = requestBio (fn () => ())
408 Msg
.send (bio
, MsgCreateDbTable p
);
410 NONE
=> print
"Server closed connection unexpectedly.\n"
413 MsgOk
=> print ("Your database " ^ user ^
"_" ^ #dbname p ^
" has been created.\n")
414 | MsgError s
=> print ("Creation failed: " ^ s ^
"\n")
415 | _
=> print
"Unexpected server reply.\n";
419 fun regenerate context
=
422 val () = Tycheck
.disallowExterns ()
424 val () = Domain
.resetGlobal ()
426 fun contactNode (node
, ip
) =
427 if node
= Config
.defaultNode
then
430 val bio
= OpenSSL
.connect (context
,
433 ^
Int.toString Config
.slavePort
)
435 Msg
.send (bio
, MsgRegenerate
);
437 NONE
=> print
"Slave closed connection unexpectedly\n"
440 MsgOk
=> print ("Slave " ^ node ^
" pre-regeneration finished\n")
441 | MsgError s
=> print ("Slave " ^ node
442 ^
" returned error: " ^
444 | _
=> print ("Slave " ^ node
445 ^
" returned unexpected command\n");
451 val _
= Domain
.setUser user
452 val _
= ErrorMsg
.reset ()
454 val dname
= Config
.domtoolDir user
456 val dir
= Posix
.FileSys
.opendir dname
459 case Posix
.FileSys
.readdir dir
of
460 NONE
=> (Posix
.FileSys
.closedir dir
;
464 loop (OS
.Path
.joinDirFile
{dir
= dname
,
471 val (_
, files
) = Order
.order (SOME b
) files
473 if !ErrorMsg
.anyErrors
then
474 print ("User " ^ user ^
"'s configuration has errors!\n")
479 | OS
.SysErr (s
, _
) => print ("System error processing user " ^ user ^
": " ^ s ^
"\n")
481 app contactNode Config
.nodeIps
;
483 app
doUser (Acl
.users ());
489 val doms
= Acl
.class
{user
= user
, class
= "domain"}
490 val doms
= List.filter (fn dom
=>
491 case Acl
.whoHas
{class
= "domain", value
= dom
} of
493 | _
=> false) (StringSet
.listItems doms
)
501 val () = Acl
.read Config
.aclFile
503 val context
= OpenSSL
.context (Config
.serverCert
,
506 val _
= Domain
.set_context context
508 val sock
= OpenSSL
.listen (context
, Config
.dispatcherPort
)
511 case OpenSSL
.accept sock
of
515 val user
= OpenSSL
.peerCN bio
516 val () = print ("\nConnection from " ^ user ^
"\n")
517 val () = Domain
.setUser user
521 val _
= print
"Configuration:\n"
522 val _
= app (fn s
=> (print s
; print
"\n")) codes
525 val outname
= OS
.FileSys
.tmpName ()
529 val outf
= TextIO.openOut outname
531 TextIO.output (outf
, code
);
532 TextIO.closeOut outf
;
539 Msg
.send (bio
, MsgOk
))
540 handle ErrorMsg
.Error
=>
541 (print
"Compilation error\n";
543 MsgError
"Error during configuration evaluation"))
544 | OpenSSL
.OpenSSL s
=>
545 (print
"OpenSSL error\n";
548 ("Error during configuration evaluation: "
550 OS
.FileSys
.remove outname
;
551 (ignore (OpenSSL
.readChar bio
);
553 handle OpenSSL
.OpenSSL _
=> ();
559 NONE
=> (OpenSSL
.close bio
560 handle OpenSSL
.OpenSSL _
=> ();
564 MsgConfig code
=> doConfig
[code
]
565 | MsgMultiConfig codes
=> doConfig codes
568 if Acl
.query
{user
= user
, class
= "priv", value
= "all"} then
570 Acl
.write Config
.aclFile
;
571 Msg
.send (bio
, MsgOk
);
572 print ("Granted permission " ^ #value acl ^
" to " ^ #user acl ^
" in " ^ #class acl ^
".\n"))
573 handle OpenSSL
.OpenSSL s
=>
574 (print
"OpenSSL error\n";
577 ("Error during granting: "
579 (ignore (OpenSSL
.readChar bio
);
581 handle OpenSSL
.OpenSSL _
=> ();
584 ((Msg
.send (bio
, MsgError
"Not authorized to grant privileges");
585 print
"Unauthorized user asked to grant a permission!\n";
586 ignore (OpenSSL
.readChar bio
);
588 handle OpenSSL
.OpenSSL _
=> ();
592 if Acl
.query
{user
= user
, class
= "priv", value
= "all"} then
594 Acl
.write Config
.aclFile
;
595 Msg
.send (bio
, MsgOk
);
596 print ("Revoked permission " ^ #value acl ^
" from " ^ #user acl ^
" in " ^ #class acl ^
".\n"))
597 handle OpenSSL
.OpenSSL s
=>
598 (print
"OpenSSL error\n";
601 ("Error during revocation: "
603 (ignore (OpenSSL
.readChar bio
);
605 handle OpenSSL
.OpenSSL _
=> ();
608 ((Msg
.send (bio
, MsgError
"Not authorized to revoke privileges");
609 print
"Unauthorized user asked to revoke a permission!\n";
610 ignore (OpenSSL
.readChar bio
);
612 handle OpenSSL
.OpenSSL _
=> ();
615 | MsgListPerms user
=>
616 ((Msg
.send (bio
, MsgPerms (Acl
.queryAll user
));
617 print ("Sent permission list for user " ^ user ^
".\n"))
618 handle OpenSSL
.OpenSSL s
=>
619 (print
"OpenSSL error\n";
622 ("Error during permission listing: "
624 (ignore (OpenSSL
.readChar bio
);
626 handle OpenSSL
.OpenSSL _
=> ();
630 ((Msg
.send (bio
, MsgWhoHasResponse (Acl
.whoHas perm
));
631 print ("Sent whohas response for " ^ #class perm ^
" / " ^ #value perm ^
".\n"))
632 handle OpenSSL
.OpenSSL s
=>
633 (print
"OpenSSL error\n";
636 ("Error during whohas: "
638 (ignore (OpenSSL
.readChar bio
);
640 handle OpenSSL
.OpenSSL _
=> ();
644 if Acl
.query
{user
= user
, class
= "priv", value
= "all"}
645 orelse List.all (fn dom
=> Acl
.query
{user
= user
, class
= "domain", value
= dom
}) doms
then
648 Acl
.revokeFromAll
{class
= "domain", value
= dom
}) doms
;
649 Acl
.write Config
.aclFile
;
650 Msg
.send (bio
, MsgOk
);
651 print ("Removed domains" ^
foldl (fn (d
, s
) => s ^
" " ^ d
) "" doms ^
".\n"))
652 handle OpenSSL
.OpenSSL s
=>
653 (print
"OpenSSL error\n";
656 ("Error during revocation: "
658 (ignore (OpenSSL
.readChar bio
);
660 handle OpenSSL
.OpenSSL _
=> ();
663 ((Msg
.send (bio
, MsgError
"Not authorized to remove that domain");
664 print
"Unauthorized user asked to remove a domain!\n";
665 ignore (OpenSSL
.readChar bio
);
667 handle OpenSSL
.OpenSSL _
=> ();
671 if Acl
.query
{user
= user
, class
= "priv", value
= "regen"}
672 orelse Acl
.query
{user
= user
, class
= "priv", value
= "all"} then
673 ((regenerate context
;
674 Msg
.send (bio
, MsgOk
);
675 print
"Regenerated all configuration.\n")
676 handle OpenSSL
.OpenSSL s
=>
677 (print
"OpenSSL error\n";
680 ("Error during regeneration: "
682 (ignore (OpenSSL
.readChar bio
);
684 handle OpenSSL
.OpenSSL _
=> ();
687 ((Msg
.send (bio
, MsgError
"Not authorized to regeneration");
688 print
"Unauthorized user asked to regenerate!\n";
689 ignore (OpenSSL
.readChar bio
);
691 handle OpenSSL
.OpenSSL _
=> ();
695 if Acl
.query
{user
= user
, class
= "priv", value
= "all"} then
697 Acl
.write Config
.aclFile
;
698 Msg
.send (bio
, MsgOk
);
699 print ("Removed user " ^ user
' ^
".\n"))
700 handle OpenSSL
.OpenSSL s
=>
701 (print
"OpenSSL error\n";
704 ("Error during revocation: "
706 (ignore (OpenSSL
.readChar bio
);
708 handle OpenSSL
.OpenSSL _
=> ();
711 ((Msg
.send (bio
, MsgError
"Not authorized to remove users");
712 print
"Unauthorized user asked to remove a user!\n";
713 ignore (OpenSSL
.readChar bio
);
715 handle OpenSSL
.OpenSSL _
=> ();
718 | MsgCreateDbUser
{dbtype
, passwd
} =>
719 (case Dbms
.lookup dbtype
of
720 NONE
=> ((Msg
.send (bio
, MsgError ("Unknown database type " ^ dbtype
));
721 print ("Database user creation request with unknown datatype type " ^ dbtype
);
722 ignore (OpenSSL
.readChar bio
))
723 handle OpenSSL
.OpenSSL _
=> ();
725 handle OpenSSL
.OpenSSL _
=> ();
728 case #adduser handler
{user
= user
, passwd
= passwd
} of
729 NONE
=> ((Msg
.send (bio
, MsgOk
);
730 print ("Added " ^ dbtype ^
" user " ^ user ^
".\n"))
731 handle OpenSSL
.OpenSSL s
=>
732 (print
"OpenSSL error\n";
735 ("Error during creation: "
737 (ignore (OpenSSL
.readChar bio
);
739 handle OpenSSL
.OpenSSL _
=> ();
741 | SOME msg
=> ((Msg
.send (bio
, MsgError ("Error adding user: " ^ msg
));
742 print ("Error adding a " ^ dbtype ^
" user " ^ user ^
": " ^ msg ^
"\n");
743 ignore (OpenSSL
.readChar bio
);
745 handle OpenSSL
.OpenSSL _
=> ();
748 | MsgCreateDbTable
{dbtype
, dbname
} =>
749 if Dbms
.validDbname dbname
then
750 (case Dbms
.lookup dbtype
of
751 NONE
=> ((Msg
.send (bio
, MsgError ("Unknown database type " ^ dbtype
));
752 print ("Database creation request with unknown datatype type " ^ dbtype
);
753 ignore (OpenSSL
.readChar bio
))
754 handle OpenSSL
.OpenSSL _
=> ();
756 handle OpenSSL
.OpenSSL _
=> ();
759 case #createdb handler
{user
= user
, dbname
= dbname
} of
760 NONE
=> ((Msg
.send (bio
, MsgOk
);
761 print ("Created database " ^ user ^
"_" ^ dbname ^
".\n"))
762 handle OpenSSL
.OpenSSL s
=>
763 (print
"OpenSSL error\n";
766 ("Error during creation: "
768 (ignore (OpenSSL
.readChar bio
);
770 handle OpenSSL
.OpenSSL _
=> ();
772 | SOME msg
=> ((Msg
.send (bio
, MsgError ("Error creating database: " ^ msg
));
773 print ("Error creating database " ^ user ^
"_" ^ dbname ^
": " ^ msg ^
"\n");
774 ignore (OpenSSL
.readChar bio
);
776 handle OpenSSL
.OpenSSL _
=> ();
779 ((Msg
.send (bio
, MsgError ("Invalid database name " ^ dbname
));
780 print ("Invalid database name " ^ user ^
"_" ^ dbname ^
"\n");
781 ignore (OpenSSL
.readChar bio
);
783 handle OpenSSL
.OpenSSL _
=> ();
787 (Msg
.send (bio
, MsgError
"Unexpected command")
788 handle OpenSSL
.OpenSSL _
=> ();
790 handle OpenSSL
.OpenSSL _
=> ();
795 handle OpenSSL
.OpenSSL s
=>
796 (print ("OpenSSL error: " ^ s ^
"\n");
798 handle OpenSSL
.OpenSSL _
=> ();
800 | OS
.SysErr (s
, _
) =>
801 (print ("System error: " ^ s ^
"\n");
803 handle OpenSSL
.OpenSSL _
=> ();
806 print
"Listening for connections....\n";
808 OpenSSL
.shutdown sock
813 val host
= Slave
.hostname ()
815 val context
= OpenSSL
.context (Config
.certDir ^
"/" ^ host ^
".pem",
816 Config
.keyDir ^
"/" ^ host ^
"/key.pem",
819 val sock
= OpenSSL
.listen (context
, Config
.slavePort
)
822 case OpenSSL
.accept sock
of
826 val peer
= OpenSSL
.peerCN bio
827 val () = print ("\nConnection from " ^ peer ^
"\n")
829 if peer
<> Config
.dispatcherName
then
830 (print
"Not authorized!\n";
836 NONE
=> print
"Dispatcher closed connection unexpectedly\n"
839 MsgFile file
=> loop
' (file
:: files
)
840 | MsgDoFiles
=> (Slave
.handleChanges files
;
841 Msg
.send (bio
, MsgOk
))
842 | MsgRegenerate
=> (Domain
.resetLocal ();
843 Msg
.send (bio
, MsgOk
))
844 | _
=> (print
"Dispatcher sent unexpected command\n";
845 Msg
.send (bio
, MsgError
"Unexpected command"))
848 ignore (OpenSSL
.readChar bio
);
852 end handle OpenSSL
.OpenSSL s
=>
853 (print ("OpenSSL error: "^ s ^
"\n");
855 handle OpenSSL
.OpenSSL _
=> ();
857 | OS
.SysErr (s
, _
) =>
858 (print ("System error: "^ s ^
"\n");
860 handle OpenSSL
.OpenSSL _
=> ();
864 OpenSSL
.shutdown sock
869 val dir
= Posix
.FileSys
.opendir Config
.libRoot
872 case Posix
.FileSys
.readdir dir
of
873 NONE
=> (Posix
.FileSys
.closedir dir
;
876 if String.isSuffix
".dtl" fname
then
877 loop (OS
.Path
.joinDirFile
{dir
= Config
.libRoot
,
886 fun autodocBasis outdir
=
887 Autodoc
.autodoc
{outdir
= outdir
, infiles
= listBasis ()}