Server executing client's requested configuration with the right permissions
[hcoop/domtool2.git] / src / main.sml
CommitLineData
234b917a
AC
1(* HCoop Domtool (http://hcoop.sourceforge.net/)
2 * Copyright (c) 2006, Adam Chlipala
3 *
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.
8 *
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.
13 *
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.
dac62e84 17 *)
234b917a
AC
18
19(* Main interface *)
20
21structure Main :> MAIN = struct
22
492c1cff 23open Ast Print
234b917a 24
6ae327f8
AC
25structure SM = StringMap
26
aa56e112 27fun init () = Acl.read Config.aclFile
234b917a 28
d189ec0e 29fun check' G fname =
a3698041
AC
30 let
31 val prog = Parse.parse fname
32 in
33 if !ErrorMsg.anyErrors then
d189ec0e 34 G
a3698041 35 else
aa56e112 36 Tycheck.checkFile G (Defaults.tInit ()) prog
a3698041
AC
37 end
38
d189ec0e 39fun basis () =
234b917a 40 let
d189ec0e
AC
41 val dir = Posix.FileSys.opendir Config.libRoot
42
43 fun loop files =
44 case Posix.FileSys.readdir dir of
d612d62c
AC
45 NONE => (Posix.FileSys.closedir dir;
46 files)
d189ec0e
AC
47 | SOME fname =>
48 if String.isSuffix ".dtl" fname then
d612d62c
AC
49 loop (OS.Path.joinDirFile {dir = Config.libRoot,
50 file = fname}
d189ec0e
AC
51 :: files)
52 else
53 loop files
54
55 val files = loop []
56 val files = Order.order files
57 in
6ae327f8
AC
58 if !ErrorMsg.anyErrors then
59 Env.empty
60 else
61 foldl (fn (fname, G) => check' G fname) Env.empty files
d189ec0e
AC
62 end
63
64fun check fname =
65 let
66 val _ = ErrorMsg.reset ()
12adf55a 67 val _ = Env.preTycheck ()
d189ec0e
AC
68
69 val b = basis ()
234b917a
AC
70 in
71 if !ErrorMsg.anyErrors then
d189ec0e 72 (b, NONE)
234b917a
AC
73 else
74 let
7f012ffd 75 val _ = ErrorMsg.reset ()
d189ec0e 76 val prog = Parse.parse fname
234b917a 77 in
492c1cff 78 if !ErrorMsg.anyErrors then
d189ec0e 79 (Env.empty, NONE)
492c1cff 80 else
d189ec0e 81 let
aa56e112 82 val G' = Tycheck.checkFile b (Defaults.tInit ()) prog
d189ec0e
AC
83 in
84 (G', #3 prog)
85 end
234b917a
AC
86 end
87 end
88
d189ec0e 89fun reduce fname =
a3698041 90 let
d189ec0e 91 val (G, body) = check fname
a3698041
AC
92 in
93 if !ErrorMsg.anyErrors then
d189ec0e 94 NONE
a3698041 95 else
d189ec0e
AC
96 case body of
97 SOME body =>
98 let
99 val body' = Reduce.reduceExp G body
100 in
101 (*printd (PD.hovBox (PD.PPS.Rel 0,
102 [PD.string "Result:",
103 PD.space 1,
104 p_exp body']))*)
105 SOME body'
106 end
107 | _ => NONE
a3698041
AC
108 end
109
d189ec0e
AC
110fun eval fname =
111 case reduce fname of
112 (SOME body') =>
113 if !ErrorMsg.anyErrors then
114 ()
115 else
aa56e112 116 Eval.exec (Defaults.eInit ()) body'
d189ec0e
AC
117 | NONE => ()
118
3b267643
AC
119val dispatcher =
120 Config.dispatcher ^ ":" ^ Int.toString Config.dispatcherPort
559e89e9 121
3b267643 122fun request fname =
07cc384c 123 let
aa56e112
AC
124 val uid = Posix.ProcEnv.getuid ()
125 val user = Posix.SysDB.Passwd.name (Posix.SysDB.getpwuid uid)
126
127 val () = Acl.read Config.aclFile
128 val () = Domain.setUser user
129 val _ = check fname
130
131 val context = OpenSSL.context (Config.certDir ^ "/" ^ user ^ ".pem",
132 Config.keyDir ^ "/" ^ user ^ ".pem",
3b267643 133 Config.trustStore)
07cc384c 134
3b267643 135 val bio = OpenSSL.connect (context, dispatcher)
559e89e9 136
3b267643
AC
137 val inf = TextIO.openIn fname
138
139 fun loop () =
140 case TextIO.inputLine inf of
141 NONE => ()
142 | SOME line => (OpenSSL.writeAll (bio, line);
143 loop ())
559e89e9 144 in
3b267643
AC
145 loop ();
146 TextIO.closeIn inf;
147 OpenSSL.close bio
559e89e9 148 end
aa56e112 149 handle ErrorMsg.Error => ()
559e89e9 150
3b267643 151fun service () =
07cc384c 152 let
aa56e112
AC
153 val () = Acl.read Config.aclFile
154
3b267643
AC
155 val context = OpenSSL.context (Config.serverCert,
156 Config.serverKey,
157 Config.trustStore)
158
60534712 159 val sock = OpenSSL.listen (context, Config.dispatcherPort)
3b267643
AC
160
161 fun loop () =
60534712 162 case OpenSSL.accept sock of
3b267643
AC
163 NONE => ()
164 | SOME bio =>
165 let
aa56e112
AC
166 val user = OpenSSL.peerCN bio
167 val () = print ("\nConnection from " ^ user ^ "\n")
168 val () = Domain.setUser user
169
170 val outname = OS.FileSys.tmpName ()
171 val outf = TextIO.openOut outname
3b267643
AC
172
173 fun loop' () =
174 case OpenSSL.readOne bio of
175 NONE => ()
aa56e112 176 | SOME line => (TextIO.output (outf, line);
3b267643
AC
177 loop' ())
178 in
aa56e112
AC
179 (loop' ();
180 TextIO.closeOut outf;
181 eval outname
182 handle ErrorMsg.Error => ();
183 OS.FileSys.remove outname;
184 OpenSSL.close bio)
185 handle OpenSSL.OpenSSL _ => ();
3b267643
AC
186 loop ()
187 end
07cc384c 188 in
3b267643
AC
189 loop ();
190 OpenSSL.shutdown sock
07cc384c
AC
191 end
192
234b917a 193end