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 foldl (fn (fname
, G
) => check
' G fname
) Env
.empty files
66 val uid
= Posix
.ProcEnv
.getuid ()
67 val user
= Posix
.SysDB
.Passwd
.name (Posix
.SysDB
.getpwuid uid
)
69 val () = Acl
.read Config
.aclFile
70 val () = Domain
.setUser user
72 val _
= ErrorMsg
.reset ()
73 val _
= Env
.preTycheck ()
77 if !ErrorMsg
.anyErrors
then
81 val _
= ErrorMsg
.reset ()
82 val prog
= Parse
.parse fname
84 if !ErrorMsg
.anyErrors
then
88 val G
' = Tycheck
.checkFile
b (Defaults
.tInit ()) prog
90 if !ErrorMsg
.anyErrors
then
100 val (G
, body
) = check fname
102 if !ErrorMsg
.anyErrors
then
108 val body
' = Reduce
.reduceExp G body
110 (*printd (PD
.hovBox (PD
.PPS
.Rel
0,
111 [PD
.string "Result:",
122 if !ErrorMsg
.anyErrors
then
125 Eval
.exec (Defaults
.eInit ()) body
'
126 | NONE
=> raise ErrorMsg
.Error
129 Config
.dispatcher ^
":" ^
Int.toString Config
.dispatcherPort
133 val inf
= TextIO.openIn
"/etc/hostname"
135 case TextIO.inputLine inf
of
136 NONE
=> (TextIO.closeIn inf
; raise Fail
"No line in /etc/hostname")
137 | SOME line
=> (TextIO.closeIn inf
; String.substring (line
, 0, size line
- 1))
144 val uid
= Posix
.ProcEnv
.getuid ()
145 val user
= Posix
.SysDB
.Passwd
.name (Posix
.SysDB
.getpwuid uid
)
147 val context
= OpenSSL
.context (Config
.certDir ^
"/" ^ user ^
".pem",
148 Config
.keyDir ^
"/" ^ user ^
".pem",
151 val bio
= OpenSSL
.connect (context
, dispatcher
)
153 val inf
= TextIO.openIn fname
156 case TextIO.inputLine inf
of
157 NONE
=> String.concat (List.rev lines
)
158 | SOME line
=> loop (line
:: lines
)
163 Msg
.send (bio
, MsgConfig code
);
165 NONE
=> print
"Server closed connection unexpectedly.\n"
168 MsgOk
=> print
"Configuration succeeded.\n"
169 | MsgError s
=> print ("Configuration failed: " ^ s ^
"\n")
170 | _
=> print
"Unexpected server reply.\n";
173 handle ErrorMsg
.Error
=> ()
177 val () = Acl
.read Config
.aclFile
179 val context
= OpenSSL
.context (Config
.serverCert
,
182 val _
= Domain
.set_context context
184 val sock
= OpenSSL
.listen (context
, Config
.dispatcherPort
)
187 case OpenSSL
.accept sock
of
191 val user
= OpenSSL
.peerCN bio
192 val () = print ("\nConnection from " ^ user ^
"\n")
193 val () = Domain
.setUser user
197 NONE
=> (OpenSSL
.close bio
198 handle OpenSSL
.OpenSSL _
=> ();
204 val _
= print
"Configuration:\n"
208 val outname
= OS
.FileSys
.tmpName ()
209 val outf
= TextIO.openOut outname
211 TextIO.output (outf
, code
);
212 TextIO.closeOut outf
;
214 Msg
.send (bio
, MsgOk
))
215 handle ErrorMsg
.Error
=>
216 (print
"Compilation error\n";
218 MsgError
"Error during configuration evaluation"))
219 | OpenSSL
.OpenSSL s
=>
220 (print
"OpenSSL error\n";
223 ("Error during configuration evaluation: "
225 OS
.FileSys
.remove outname
;
226 (ignore (OpenSSL
.readChar bio
);
228 handle OpenSSL
.OpenSSL _
=> ();
232 (Msg
.send (bio
, MsgError
"Unexpected command")
233 handle OpenSSL
.OpenSSL _
=> ();
235 handle OpenSSL
.OpenSSL _
=> ();
241 print
"Listening for connections....\n";
243 OpenSSL
.shutdown sock
248 val host
= hostname ()
250 val context
= OpenSSL
.context (Config
.certDir ^
"/" ^ host ^
".pem",
251 Config
.keyDir ^
"/" ^ host ^
".pem",
254 val sock
= OpenSSL
.listen (context
, Config
.slavePort
)
257 case OpenSSL
.accept sock
of
261 val peer
= OpenSSL
.peerCN bio
262 val () = print ("\nConnection from " ^ peer ^
"\n")
264 if peer
<> Config
.dispatcherName
then
265 (print
"Not authorized!\n";
271 NONE
=> print
"Dispatcher closed connection unexpectedly\n"
274 MsgFile file
=> loop
' (file
:: files
)
275 | MsgDoFiles
=> (Slave
.handleChanges files
;
276 Msg
.send (bio
, MsgOk
))
277 | _
=> (print
"Dispatcher sent unexpected command\n";
278 Msg
.send (bio
, MsgError
"Unexpected command"))
281 ignore (OpenSSL
.readChar bio
);
285 end handle OpenSSL
.OpenSSL s
=>
286 (print ("OpenSSL error: "^ s ^
"\n");
288 handle OpenSSL
.OpenSSL _
=> ();
292 OpenSSL
.shutdown sock
295 fun autodocBasis outdir
=
297 val dir
= Posix
.FileSys
.opendir Config
.libRoot
300 case Posix
.FileSys
.readdir dir
of
301 NONE
=> (Posix
.FileSys
.closedir dir
;
304 if String.isSuffix
".dtl" fname
then
305 loop (OS
.Path
.joinDirFile
{dir
= Config
.libRoot
,
313 Autodoc
.autodoc
{outdir
= outdir
, infiles
= files
}