Commit | Line | Data |
---|---|---|
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 | ||
21 | structure Main :> MAIN = struct | |
22 | ||
36e42cb8 | 23 | open Ast MsgTypes Print |
234b917a | 24 | |
6ae327f8 AC |
25 | structure SM = StringMap |
26 | ||
aa56e112 | 27 | fun init () = Acl.read Config.aclFile |
234b917a | 28 | |
d189ec0e | 29 | fun 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 | 39 | fun 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 [] | |
c53e82e4 | 56 | val (_, files) = Order.order NONE files |
d189ec0e | 57 | in |
6ae327f8 AC |
58 | if !ErrorMsg.anyErrors then |
59 | Env.empty | |
60 | else | |
b3159a70 AC |
61 | (Tycheck.allowExterns (); |
62 | foldl (fn (fname, G) => check' G fname) Env.empty files | |
63 | before Tycheck.disallowExterns ()) | |
d189ec0e AC |
64 | end |
65 | ||
66 | fun check fname = | |
67 | let | |
68 | val _ = ErrorMsg.reset () | |
12adf55a | 69 | val _ = Env.preTycheck () |
d189ec0e AC |
70 | |
71 | val b = basis () | |
234b917a AC |
72 | in |
73 | if !ErrorMsg.anyErrors then | |
36e42cb8 | 74 | raise ErrorMsg.Error |
234b917a AC |
75 | else |
76 | let | |
b3159a70 | 77 | val _ = Tycheck.disallowExterns () |
7f012ffd | 78 | val _ = ErrorMsg.reset () |
d189ec0e | 79 | val prog = Parse.parse fname |
234b917a | 80 | in |
492c1cff | 81 | if !ErrorMsg.anyErrors then |
36e42cb8 | 82 | raise ErrorMsg.Error |
492c1cff | 83 | else |
d189ec0e | 84 | let |
aa56e112 | 85 | val G' = Tycheck.checkFile b (Defaults.tInit ()) prog |
d189ec0e | 86 | in |
36e42cb8 AC |
87 | if !ErrorMsg.anyErrors then |
88 | raise ErrorMsg.Error | |
89 | else | |
90 | (G', #3 prog) | |
d189ec0e | 91 | end |
234b917a AC |
92 | end |
93 | end | |
94 | ||
c53e82e4 AC |
95 | val notTmp = CharVector.all (fn ch => Char.isAlphaNum ch orelse ch = #"." orelse ch = #"_" orelse ch = #"-") |
96 | ||
97 | fun checkDir dname = | |
98 | let | |
99 | val b = basis () | |
100 | ||
101 | val dir = Posix.FileSys.opendir dname | |
102 | ||
103 | fun loop files = | |
104 | case Posix.FileSys.readdir dir of | |
105 | NONE => (Posix.FileSys.closedir dir; | |
106 | files) | |
107 | | SOME fname => | |
108 | if notTmp fname then | |
109 | loop (OS.Path.joinDirFile {dir = dname, | |
110 | file = fname} | |
111 | :: files) | |
112 | else | |
113 | loop files | |
114 | ||
115 | val files = loop [] | |
116 | val (_, files) = Order.order (SOME b) files | |
117 | in | |
118 | if !ErrorMsg.anyErrors then | |
1824f573 | 119 | raise ErrorMsg.Error |
c53e82e4 AC |
120 | else |
121 | (foldl (fn (fname, G) => check' G fname) b files; | |
1824f573 AC |
122 | if !ErrorMsg.anyErrors then |
123 | raise ErrorMsg.Error | |
124 | else | |
125 | ()) | |
c53e82e4 AC |
126 | end |
127 | ||
d189ec0e | 128 | fun reduce fname = |
a3698041 | 129 | let |
d189ec0e | 130 | val (G, body) = check fname |
a3698041 AC |
131 | in |
132 | if !ErrorMsg.anyErrors then | |
d189ec0e | 133 | NONE |
a3698041 | 134 | else |
d189ec0e AC |
135 | case body of |
136 | SOME body => | |
137 | let | |
138 | val body' = Reduce.reduceExp G body | |
139 | in | |
140 | (*printd (PD.hovBox (PD.PPS.Rel 0, | |
141 | [PD.string "Result:", | |
142 | PD.space 1, | |
143 | p_exp body']))*) | |
144 | SOME body' | |
145 | end | |
146 | | _ => NONE | |
a3698041 AC |
147 | end |
148 | ||
d189ec0e AC |
149 | fun eval fname = |
150 | case reduce fname of | |
151 | (SOME body') => | |
152 | if !ErrorMsg.anyErrors then | |
36e42cb8 | 153 | raise ErrorMsg.Error |
d189ec0e | 154 | else |
aa56e112 | 155 | Eval.exec (Defaults.eInit ()) body' |
36e42cb8 | 156 | | NONE => raise ErrorMsg.Error |
d189ec0e | 157 | |
1824f573 AC |
158 | fun eval' fname = |
159 | case reduce fname of | |
160 | (SOME body') => | |
161 | if !ErrorMsg.anyErrors then | |
162 | raise ErrorMsg.Error | |
163 | else | |
164 | ignore (Eval.exec' (Defaults.eInit ()) body') | |
165 | | NONE => raise ErrorMsg.Error | |
166 | ||
3b267643 AC |
167 | val dispatcher = |
168 | Config.dispatcher ^ ":" ^ Int.toString Config.dispatcherPort | |
559e89e9 | 169 | |
c9731b9b AC |
170 | val self = |
171 | "localhost:" ^ Int.toString Config.slavePort | |
172 | ||
5ee41dd0 | 173 | fun requestContext f = |
07cc384c | 174 | let |
a56cc2c3 AC |
175 | val uid = Posix.ProcEnv.getuid () |
176 | val user = Posix.SysDB.Passwd.name (Posix.SysDB.getpwuid uid) | |
5ee41dd0 | 177 | |
a56cc2c3 AC |
178 | val () = Acl.read Config.aclFile |
179 | val () = Domain.setUser user | |
5ee41dd0 AC |
180 | |
181 | val () = f () | |
aa56e112 | 182 | |
aa56e112 | 183 | val context = OpenSSL.context (Config.certDir ^ "/" ^ user ^ ".pem", |
a088cea6 | 184 | Config.keyDir ^ "/" ^ user ^ "/key.pem", |
3b267643 | 185 | Config.trustStore) |
5ee41dd0 AC |
186 | in |
187 | (user, context) | |
188 | end | |
07cc384c | 189 | |
5ee41dd0 AC |
190 | fun requestBio f = |
191 | let | |
192 | val (user, context) = requestContext f | |
193 | in | |
194 | (user, OpenSSL.connect (context, dispatcher)) | |
195 | end | |
196 | ||
c9731b9b AC |
197 | fun requestSlaveBio () = |
198 | let | |
199 | val (user, context) = requestContext (fn () => ()) | |
200 | in | |
201 | (user, OpenSSL.connect (context, self)) | |
202 | end | |
203 | ||
5ee41dd0 AC |
204 | fun request fname = |
205 | let | |
206 | val (user, bio) = requestBio (fn () => ignore (check fname)) | |
559e89e9 | 207 | |
3b267643 AC |
208 | val inf = TextIO.openIn fname |
209 | ||
36e42cb8 | 210 | fun loop lines = |
3b267643 | 211 | case TextIO.inputLine inf of |
36e42cb8 AC |
212 | NONE => String.concat (List.rev lines) |
213 | | SOME line => loop (line :: lines) | |
214 | ||
215 | val code = loop [] | |
559e89e9 | 216 | in |
3b267643 | 217 | TextIO.closeIn inf; |
36e42cb8 AC |
218 | Msg.send (bio, MsgConfig code); |
219 | case Msg.recv bio of | |
220 | NONE => print "Server closed connection unexpectedly.\n" | |
221 | | SOME m => | |
222 | case m of | |
223 | MsgOk => print "Configuration succeeded.\n" | |
224 | | MsgError s => print ("Configuration failed: " ^ s ^ "\n") | |
225 | | _ => print "Unexpected server reply.\n"; | |
3b267643 | 226 | OpenSSL.close bio |
559e89e9 | 227 | end |
aa56e112 | 228 | handle ErrorMsg.Error => () |
559e89e9 | 229 | |
c53e82e4 AC |
230 | fun requestDir dname = |
231 | let | |
1824f573 AC |
232 | val _ = ErrorMsg.reset () |
233 | ||
234 | val (user, bio) = requestBio (fn () => checkDir dname) | |
c53e82e4 AC |
235 | |
236 | val b = basis () | |
237 | ||
238 | val dir = Posix.FileSys.opendir dname | |
239 | ||
240 | fun loop files = | |
241 | case Posix.FileSys.readdir dir of | |
242 | NONE => (Posix.FileSys.closedir dir; | |
243 | files) | |
244 | | SOME fname => | |
245 | if notTmp fname then | |
246 | loop (OS.Path.joinDirFile {dir = dname, | |
247 | file = fname} | |
248 | :: files) | |
249 | else | |
250 | loop files | |
251 | ||
252 | val files = loop [] | |
253 | val (_, files) = Order.order (SOME b) files | |
254 | ||
255 | val _ = if !ErrorMsg.anyErrors then | |
256 | raise ErrorMsg.Error | |
257 | else | |
258 | () | |
259 | ||
260 | val codes = map (fn fname => | |
261 | let | |
262 | val inf = TextIO.openIn fname | |
263 | ||
264 | fun loop lines = | |
265 | case TextIO.inputLine inf of | |
266 | NONE => String.concat (rev lines) | |
267 | | SOME line => loop (line :: lines) | |
268 | in | |
269 | loop [] | |
270 | before TextIO.closeIn inf | |
271 | end) files | |
272 | in | |
1824f573 AC |
273 | if !ErrorMsg.anyErrors then |
274 | () | |
275 | else | |
276 | (Msg.send (bio, MsgMultiConfig codes); | |
277 | case Msg.recv bio of | |
278 | NONE => print "Server closed connection unexpectedly.\n" | |
279 | | SOME m => | |
280 | case m of | |
281 | MsgOk => print "Configuration succeeded.\n" | |
282 | | MsgError s => print ("Configuration failed: " ^ s ^ "\n") | |
283 | | _ => print "Unexpected server reply.\n"; | |
284 | OpenSSL.close bio) | |
c53e82e4 AC |
285 | end |
286 | handle ErrorMsg.Error => () | |
287 | ||
62260c5f AC |
288 | fun requestPing () = |
289 | let | |
290 | val (_, bio) = requestBio (fn () => ()) | |
291 | in | |
292 | OpenSSL.close bio; | |
293 | OS.Process.success | |
294 | end | |
295 | handle _ => OS.Process.failure | |
296 | ||
9f27d58f AC |
297 | fun requestShutdown () = |
298 | let | |
299 | val (_, bio) = requestBio (fn () => ()) | |
300 | in | |
301 | Msg.send (bio, MsgShutdown); | |
302 | case Msg.recv bio of | |
303 | NONE => print "Server closed connection unexpectedly.\n" | |
304 | | SOME m => | |
305 | case m of | |
306 | MsgOk => print "Shutdown begun.\n" | |
307 | | MsgError s => print ("Shutdown failed: " ^ s ^ "\n") | |
308 | | _ => print "Unexpected server reply.\n"; | |
309 | OpenSSL.close bio | |
310 | end | |
311 | ||
c9731b9b AC |
312 | fun requestSlavePing () = |
313 | let | |
314 | val (_, bio) = requestSlaveBio () | |
315 | in | |
316 | OpenSSL.close bio; | |
317 | OS.Process.success | |
318 | end | |
319 | handle _ => OS.Process.failure | |
320 | ||
321 | fun requestSlaveShutdown () = | |
322 | let | |
323 | val (_, bio) = requestSlaveBio () | |
324 | in | |
325 | Msg.send (bio, MsgShutdown); | |
326 | case Msg.recv bio of | |
327 | NONE => print "Server closed connection unexpectedly.\n" | |
328 | | SOME m => | |
329 | case m of | |
330 | MsgOk => print "Shutdown begun.\n" | |
331 | | MsgError s => print ("Shutdown failed: " ^ s ^ "\n") | |
332 | | _ => print "Unexpected server reply.\n"; | |
333 | OpenSSL.close bio | |
334 | end | |
335 | ||
5ee41dd0 AC |
336 | fun requestGrant acl = |
337 | let | |
338 | val (user, bio) = requestBio (fn () => ()) | |
339 | in | |
340 | Msg.send (bio, MsgGrant acl); | |
341 | case Msg.recv bio of | |
342 | NONE => print "Server closed connection unexpectedly.\n" | |
343 | | SOME m => | |
344 | case m of | |
345 | MsgOk => print "Grant succeeded.\n" | |
346 | | MsgError s => print ("Grant failed: " ^ s ^ "\n") | |
347 | | _ => print "Unexpected server reply.\n"; | |
348 | OpenSSL.close bio | |
349 | end | |
350 | ||
411a85f2 AC |
351 | fun requestRevoke acl = |
352 | let | |
353 | val (user, bio) = requestBio (fn () => ()) | |
354 | in | |
355 | Msg.send (bio, MsgRevoke acl); | |
356 | case Msg.recv bio of | |
357 | NONE => print "Server closed connection unexpectedly.\n" | |
358 | | SOME m => | |
359 | case m of | |
360 | MsgOk => print "Revoke succeeded.\n" | |
361 | | MsgError s => print ("Revoke failed: " ^ s ^ "\n") | |
362 | | _ => print "Unexpected server reply.\n"; | |
363 | OpenSSL.close bio | |
364 | end | |
365 | ||
08a04eb4 AC |
366 | fun requestListPerms user = |
367 | let | |
368 | val (_, bio) = requestBio (fn () => ()) | |
369 | in | |
370 | Msg.send (bio, MsgListPerms user); | |
371 | (case Msg.recv bio of | |
372 | NONE => (print "Server closed connection unexpectedly.\n"; | |
373 | NONE) | |
374 | | SOME m => | |
375 | case m of | |
376 | MsgPerms perms => SOME perms | |
377 | | MsgError s => (print ("Listing failed: " ^ s ^ "\n"); | |
378 | NONE) | |
379 | | _ => (print "Unexpected server reply.\n"; | |
380 | NONE)) | |
381 | before OpenSSL.close bio | |
382 | end | |
383 | ||
094877b1 AC |
384 | fun requestWhoHas perm = |
385 | let | |
386 | val (_, bio) = requestBio (fn () => ()) | |
387 | in | |
388 | Msg.send (bio, MsgWhoHas perm); | |
389 | (case Msg.recv bio of | |
390 | NONE => (print "Server closed connection unexpectedly.\n"; | |
391 | NONE) | |
392 | | SOME m => | |
393 | case m of | |
394 | MsgWhoHasResponse users => SOME users | |
395 | | MsgError s => (print ("whohas failed: " ^ s ^ "\n"); | |
396 | NONE) | |
397 | | _ => (print "Unexpected server reply.\n"; | |
398 | NONE)) | |
399 | before OpenSSL.close bio | |
400 | end | |
401 | ||
1824f573 AC |
402 | fun requestRegen () = |
403 | let | |
404 | val (_, bio) = requestBio (fn () => ()) | |
405 | in | |
406 | Msg.send (bio, MsgRegenerate); | |
407 | case Msg.recv bio of | |
408 | NONE => print "Server closed connection unexpectedly.\n" | |
409 | | SOME m => | |
410 | case m of | |
411 | MsgOk => print "Regeneration succeeded.\n" | |
412 | | MsgError s => print ("Regeneration failed: " ^ s ^ "\n") | |
413 | | _ => print "Unexpected server reply.\n"; | |
414 | OpenSSL.close bio | |
415 | end | |
416 | ||
c189cbe9 AC |
417 | fun requestRmdom dom = |
418 | let | |
419 | val (_, bio) = requestBio (fn () => ()) | |
420 | in | |
421 | Msg.send (bio, MsgRmdom dom); | |
422 | case Msg.recv bio of | |
423 | NONE => print "Server closed connection unexpectedly.\n" | |
424 | | SOME m => | |
425 | case m of | |
426 | MsgOk => print "Removal succeeded.\n" | |
427 | | MsgError s => print ("Removal failed: " ^ s ^ "\n") | |
428 | | _ => print "Unexpected server reply.\n"; | |
429 | OpenSSL.close bio | |
430 | end | |
431 | ||
e69e60cc AC |
432 | fun requestRmuser user = |
433 | let | |
434 | val (_, bio) = requestBio (fn () => ()) | |
435 | in | |
436 | Msg.send (bio, MsgRmuser user); | |
437 | case Msg.recv bio of | |
438 | NONE => print "Server closed connection unexpectedly.\n" | |
439 | | SOME m => | |
440 | case m of | |
441 | MsgOk => print "Removal succeeded.\n" | |
442 | | MsgError s => print ("Removal failed: " ^ s ^ "\n") | |
443 | | _ => print "Unexpected server reply.\n"; | |
444 | OpenSSL.close bio | |
445 | end | |
446 | ||
d541c618 AC |
447 | fun requestDbUser dbtype = |
448 | let | |
449 | val (_, bio) = requestBio (fn () => ()) | |
450 | in | |
451 | Msg.send (bio, MsgCreateDbUser dbtype); | |
452 | case Msg.recv bio of | |
453 | NONE => print "Server closed connection unexpectedly.\n" | |
454 | | SOME m => | |
455 | case m of | |
456 | MsgOk => print "Your user has been created.\n" | |
457 | | MsgError s => print ("Creation failed: " ^ s ^ "\n") | |
458 | | _ => print "Unexpected server reply.\n"; | |
459 | OpenSSL.close bio | |
460 | end | |
461 | ||
86aa5de7 AC |
462 | fun requestDbPasswd rc = |
463 | let | |
464 | val (_, bio) = requestBio (fn () => ()) | |
465 | in | |
466 | Msg.send (bio, MsgDbPasswd rc); | |
467 | case Msg.recv bio of | |
468 | NONE => print "Server closed connection unexpectedly.\n" | |
469 | | SOME m => | |
470 | case m of | |
471 | MsgOk => print "Your password has been changed.\n" | |
472 | | MsgError s => print ("Password set failed: " ^ s ^ "\n") | |
473 | | _ => print "Unexpected server reply.\n"; | |
474 | OpenSSL.close bio | |
475 | end | |
476 | ||
90dd48df AC |
477 | fun requestDbTable p = |
478 | let | |
479 | val (user, bio) = requestBio (fn () => ()) | |
480 | in | |
481 | Msg.send (bio, MsgCreateDbTable p); | |
482 | case Msg.recv bio of | |
483 | NONE => print "Server closed connection unexpectedly.\n" | |
484 | | SOME m => | |
485 | case m of | |
486 | MsgOk => print ("Your database " ^ user ^ "_" ^ #dbname p ^ " has been created.\n") | |
487 | | MsgError s => print ("Creation failed: " ^ s ^ "\n") | |
488 | | _ => print "Unexpected server reply.\n"; | |
489 | OpenSSL.close bio | |
490 | end | |
491 | ||
1d3ef80e AC |
492 | fun requestListMailboxes domain = |
493 | let | |
494 | val (_, bio) = requestBio (fn () => ()) | |
495 | in | |
496 | Msg.send (bio, MsgListMailboxes domain); | |
497 | (case Msg.recv bio of | |
2e96b9d4 | 498 | NONE => Vmail.Error "Server closed connection unexpectedly." |
1d3ef80e AC |
499 | | SOME m => |
500 | case m of | |
501 | MsgMailboxes users => (Msg.send (bio, MsgOk); | |
502 | Vmail.Listing users) | |
503 | | MsgError s => Vmail.Error ("Creation failed: " ^ s) | |
2e96b9d4 | 504 | | _ => Vmail.Error "Unexpected server reply.") |
1d3ef80e AC |
505 | before OpenSSL.close bio |
506 | end | |
507 | ||
08688401 AC |
508 | fun requestNewMailbox p = |
509 | let | |
510 | val (_, bio) = requestBio (fn () => ()) | |
511 | in | |
512 | Msg.send (bio, MsgNewMailbox p); | |
513 | case Msg.recv bio of | |
514 | NONE => print "Server closed connection unexpectedly.\n" | |
515 | | SOME m => | |
516 | case m of | |
517 | MsgOk => print ("A mapping for " ^ #user p ^ "@" ^ #domain p ^ " has been created.\n") | |
518 | | MsgError s => print ("Creation failed: " ^ s ^ "\n") | |
519 | | _ => print "Unexpected server reply.\n"; | |
520 | OpenSSL.close bio | |
521 | end | |
522 | ||
523 | fun requestPasswdMailbox p = | |
524 | let | |
525 | val (_, bio) = requestBio (fn () => ()) | |
526 | in | |
527 | Msg.send (bio, MsgPasswdMailbox p); | |
528 | case Msg.recv bio of | |
529 | NONE => print "Server closed connection unexpectedly.\n" | |
530 | | SOME m => | |
531 | case m of | |
532 | MsgOk => print ("The password for " ^ #user p ^ "@" ^ #domain p ^ " has been changed.\n") | |
533 | | MsgError s => print ("Set failed: " ^ s ^ "\n") | |
534 | | _ => print "Unexpected server reply.\n"; | |
535 | OpenSSL.close bio | |
536 | end | |
537 | ||
538 | fun requestRmMailbox p = | |
539 | let | |
540 | val (_, bio) = requestBio (fn () => ()) | |
541 | in | |
542 | Msg.send (bio, MsgRmMailbox p); | |
543 | case Msg.recv bio of | |
544 | NONE => print "Server closed connection unexpectedly.\n" | |
545 | | SOME m => | |
546 | case m of | |
547 | MsgOk => print ("The mapping for mailbox " ^ #user p ^ "@" ^ #domain p ^ " has been deleted.\n") | |
548 | | MsgError s => print ("Remove failed: " ^ s ^ "\n") | |
549 | | _ => print "Unexpected server reply.\n"; | |
550 | OpenSSL.close bio | |
551 | end | |
552 | ||
2e96b9d4 AC |
553 | fun requestSaQuery addr = |
554 | let | |
555 | val (_, bio) = requestBio (fn () => ()) | |
556 | in | |
557 | Msg.send (bio, MsgSaQuery addr); | |
558 | (case Msg.recv bio of | |
559 | NONE => print "Server closed connection unexpectedly.\n" | |
560 | | SOME m => | |
561 | case m of | |
562 | MsgSaStatus b => (print ("SpamAssassin filtering for " ^ addr ^ " is " | |
563 | ^ (if b then "ON" else "OFF") ^ ".\n"); | |
564 | Msg.send (bio, MsgOk)) | |
565 | | MsgError s => print ("Query failed: " ^ s ^ "\n") | |
566 | | _ => print "Unexpected server reply.\n") | |
567 | before OpenSSL.close bio | |
568 | end | |
569 | ||
570 | fun requestSaSet p = | |
571 | let | |
572 | val (_, bio) = requestBio (fn () => ()) | |
573 | in | |
574 | Msg.send (bio, MsgSaSet p); | |
575 | case Msg.recv bio of | |
576 | NONE => print "Server closed connection unexpectedly.\n" | |
577 | | SOME m => | |
578 | case m of | |
579 | MsgOk => print ("SpamAssassin filtering for " ^ #1 p ^ " is now " | |
580 | ^ (if #2 p then "ON" else "OFF") ^ ".\n") | |
581 | | MsgError s => print ("Set failed: " ^ s ^ "\n") | |
582 | | _ => print "Unexpected server reply.\n"; | |
583 | OpenSSL.close bio | |
584 | end | |
585 | ||
2bc5ed22 AC |
586 | fun requestSmtpLog domain = |
587 | let | |
588 | val (_, bio) = requestBio (fn () => ()) | |
589 | ||
590 | val _ = Msg.send (bio, MsgSmtpLogReq domain) | |
591 | ||
592 | fun loop () = | |
593 | case Msg.recv bio of | |
594 | NONE => print "Server closed connection unexpectedly.\n" | |
595 | | SOME m => | |
596 | case m of | |
597 | MsgOk => () | |
598 | | MsgSmtpLogRes line => (print line; | |
599 | loop ()) | |
600 | | MsgError s => print ("Log search failed: " ^ s ^ "\n") | |
601 | | _ => print "Unexpected server reply.\n" | |
602 | in | |
603 | loop (); | |
604 | OpenSSL.close bio | |
605 | end | |
606 | ||
75585a67 AC |
607 | fun requestApt {node, pkg} = |
608 | let | |
a95a0107 AC |
609 | val (user, context) = requestContext (fn () => ()) |
610 | val bio = OpenSSL.connect (context, if node = Config.masterNode then | |
611 | dispatcher | |
612 | else | |
613 | Domain.nodeIp node ^ ":" ^ Int.toString Config.slavePort) | |
75585a67 | 614 | |
a95a0107 | 615 | val _ = Msg.send (bio, MsgQuery (QApt pkg)) |
75585a67 AC |
616 | |
617 | fun loop () = | |
618 | case Msg.recv bio of | |
619 | NONE => (print "Server closed connection unexpectedly.\n"; | |
620 | OS.Process.failure) | |
621 | | SOME m => | |
622 | case m of | |
623 | MsgYes => (print "Package is installed.\n"; | |
624 | OS.Process.success) | |
625 | | MsgNo => (print "Package is not installed.\n"; | |
626 | OS.Process.failure) | |
627 | | MsgError s => (print ("APT query failed: " ^ s ^ "\n"); | |
628 | OS.Process.failure) | |
629 | | _ => (print "Unexpected server reply.\n"; | |
630 | OS.Process.failure) | |
631 | in | |
632 | loop () | |
633 | before OpenSSL.close bio | |
634 | end | |
635 | ||
d351d679 AC |
636 | fun requestCron {node, uname} = |
637 | let | |
638 | val (user, context) = requestContext (fn () => ()) | |
639 | val bio = OpenSSL.connect (context, if node = Config.masterNode then | |
640 | dispatcher | |
641 | else | |
642 | Domain.nodeIp node ^ ":" ^ Int.toString Config.slavePort) | |
643 | ||
644 | val _ = Msg.send (bio, MsgQuery (QCron uname)) | |
645 | ||
646 | fun loop () = | |
647 | case Msg.recv bio of | |
648 | NONE => (print "Server closed connection unexpectedly.\n"; | |
649 | OS.Process.failure) | |
650 | | SOME m => | |
651 | case m of | |
652 | MsgYes => (print "User has cron permissions.\n"; | |
653 | OS.Process.success) | |
654 | | MsgNo => (print "User does not have cron permissions.\n"; | |
655 | OS.Process.failure) | |
656 | | MsgError s => (print ("Cron query failed: " ^ s ^ "\n"); | |
657 | OS.Process.failure) | |
658 | | _ => (print "Unexpected server reply.\n"; | |
659 | OS.Process.failure) | |
660 | in | |
661 | loop () | |
662 | before OpenSSL.close bio | |
663 | end | |
664 | ||
665 | fun requestFtp {node, uname} = | |
666 | let | |
667 | val (user, context) = requestContext (fn () => ()) | |
668 | val bio = OpenSSL.connect (context, if node = Config.masterNode then | |
669 | dispatcher | |
670 | else | |
671 | Domain.nodeIp node ^ ":" ^ Int.toString Config.slavePort) | |
672 | ||
673 | val _ = Msg.send (bio, MsgQuery (QFtp uname)) | |
674 | ||
675 | fun loop () = | |
676 | case Msg.recv bio of | |
677 | NONE => (print "Server closed connection unexpectedly.\n"; | |
678 | OS.Process.failure) | |
679 | | SOME m => | |
680 | case m of | |
681 | MsgYes => (print "User has FTP permissions.\n"; | |
682 | OS.Process.success) | |
683 | | MsgNo => (print "User does not have FTP permissions.\n"; | |
684 | OS.Process.failure) | |
685 | | MsgError s => (print ("FTP query failed: " ^ s ^ "\n"); | |
686 | OS.Process.failure) | |
687 | | _ => (print "Unexpected server reply.\n"; | |
688 | OS.Process.failure) | |
689 | in | |
690 | loop () | |
691 | before OpenSSL.close bio | |
692 | end | |
693 | ||
4d5126e1 AC |
694 | fun requestTrustedPath {node, uname} = |
695 | let | |
696 | val (user, context) = requestContext (fn () => ()) | |
697 | val bio = OpenSSL.connect (context, if node = Config.masterNode then | |
698 | dispatcher | |
699 | else | |
700 | Domain.nodeIp node ^ ":" ^ Int.toString Config.slavePort) | |
701 | ||
702 | val _ = Msg.send (bio, MsgQuery (QTrustedPath uname)) | |
703 | ||
704 | fun loop () = | |
705 | case Msg.recv bio of | |
706 | NONE => (print "Server closed connection unexpectedly.\n"; | |
707 | OS.Process.failure) | |
708 | | SOME m => | |
709 | case m of | |
710 | MsgYes => (print "User has trusted path restriction.\n"; | |
711 | OS.Process.success) | |
712 | | MsgNo => (print "User does not have trusted path restriction.\n"; | |
713 | OS.Process.failure) | |
714 | | MsgError s => (print ("Trusted path query failed: " ^ s ^ "\n"); | |
715 | OS.Process.failure) | |
716 | | _ => (print "Unexpected server reply.\n"; | |
717 | OS.Process.failure) | |
718 | in | |
719 | loop () | |
720 | before OpenSSL.close bio | |
721 | end | |
722 | ||
737c68d4 AC |
723 | fun requestSocketPerm {node, uname} = |
724 | let | |
725 | val (user, context) = requestContext (fn () => ()) | |
726 | val bio = OpenSSL.connect (context, if node = Config.masterNode then | |
727 | dispatcher | |
728 | else | |
729 | Domain.nodeIp node ^ ":" ^ Int.toString Config.slavePort) | |
730 | ||
731 | val _ = Msg.send (bio, MsgQuery (QSocket uname)) | |
732 | ||
733 | fun loop () = | |
734 | case Msg.recv bio of | |
735 | NONE => (print "Server closed connection unexpectedly.\n"; | |
736 | OS.Process.failure) | |
737 | | SOME m => | |
738 | case m of | |
739 | MsgSocket p => (case p of | |
740 | Any => print "Any\n" | |
741 | | Client => print "Client\n" | |
742 | | Server => print "Server\n" | |
743 | | Nada => print "Nada\n"; | |
744 | OS.Process.success) | |
745 | | MsgError s => (print ("Socket permission query failed: " ^ s ^ "\n"); | |
746 | OS.Process.failure) | |
747 | | _ => (print "Unexpected server reply.\n"; | |
748 | OS.Process.failure) | |
749 | in | |
750 | loop () | |
751 | before OpenSSL.close bio | |
752 | end | |
753 | ||
71420f8b | 754 | fun regenerate context = |
1824f573 AC |
755 | let |
756 | val b = basis () | |
71420f8b AC |
757 | val () = Tycheck.disallowExterns () |
758 | ||
759 | val () = Domain.resetGlobal () | |
760 | ||
761 | fun contactNode (node, ip) = | |
762 | if node = Config.defaultNode then | |
763 | Domain.resetLocal () | |
764 | else let | |
765 | val bio = OpenSSL.connect (context, | |
766 | ip | |
767 | ^ ":" | |
768 | ^ Int.toString Config.slavePort) | |
769 | in | |
770 | Msg.send (bio, MsgRegenerate); | |
771 | case Msg.recv bio of | |
772 | NONE => print "Slave closed connection unexpectedly\n" | |
773 | | SOME m => | |
774 | case m of | |
775 | MsgOk => print ("Slave " ^ node ^ " pre-regeneration finished\n") | |
776 | | MsgError s => print ("Slave " ^ node | |
777 | ^ " returned error: " ^ | |
778 | s ^ "\n") | |
779 | | _ => print ("Slave " ^ node | |
780 | ^ " returned unexpected command\n"); | |
781 | OpenSSL.close bio | |
782 | end | |
1824f573 AC |
783 | |
784 | fun doUser user = | |
785 | let | |
786 | val _ = Domain.setUser user | |
787 | val _ = ErrorMsg.reset () | |
788 | ||
789 | val dname = Config.domtoolDir user | |
790 | ||
791 | val dir = Posix.FileSys.opendir dname | |
792 | ||
793 | fun loop files = | |
794 | case Posix.FileSys.readdir dir of | |
795 | NONE => (Posix.FileSys.closedir dir; | |
796 | files) | |
797 | | SOME fname => | |
798 | if notTmp fname then | |
799 | loop (OS.Path.joinDirFile {dir = dname, | |
800 | file = fname} | |
801 | :: files) | |
802 | else | |
803 | loop files | |
804 | ||
805 | val files = loop [] | |
806 | val (_, files) = Order.order (SOME b) files | |
807 | in | |
808 | if !ErrorMsg.anyErrors then | |
809 | print ("User " ^ user ^ "'s configuration has errors!\n") | |
810 | else | |
811 | app eval' files | |
812 | end | |
813 | handle IO.Io _ => () | |
814 | | OS.SysErr (s, _) => print ("System error processing user " ^ user ^ ": " ^ s ^ "\n") | |
434a7b1f | 815 | | ErrorMsg.Error => print ("User " ^ user ^ " had a compilation error.\n") |
1824f573 | 816 | in |
71420f8b | 817 | app contactNode Config.nodeIps; |
1824f573 AC |
818 | Env.pre (); |
819 | app doUser (Acl.users ()); | |
820 | Env.post () | |
821 | end | |
822 | ||
e69e60cc AC |
823 | fun rmuser user = |
824 | let | |
825 | val doms = Acl.class {user = user, class = "domain"} | |
826 | val doms = List.filter (fn dom => | |
827 | case Acl.whoHas {class = "domain", value = dom} of | |
828 | [_] => true | |
829 | | _ => false) (StringSet.listItems doms) | |
830 | in | |
831 | Acl.rmuser user; | |
832 | Domain.rmdom doms | |
833 | end | |
834 | ||
c9731b9b AC |
835 | fun now () = Date.toString (Date.fromTimeUniv (Time.now ())) |
836 | ||
a95a0107 AC |
837 | fun answerQuery q = |
838 | case q of | |
839 | QApt pkg => if Apt.installed pkg then MsgYes else MsgNo | |
d351d679 AC |
840 | | QCron user => if Cron.allowed user then MsgYes else MsgNo |
841 | | QFtp user => if Ftp.allowed user then MsgYes else MsgNo | |
4d5126e1 | 842 | | QTrustedPath user => if TrustedPath.query user then MsgYes else MsgNo |
737c68d4 | 843 | | QSocket user => MsgSocket (SocketPerm.query user) |
a95a0107 AC |
844 | |
845 | fun describeQuery q = | |
846 | case q of | |
847 | QApt pkg => "Requested installation status of package " ^ pkg | |
d351d679 AC |
848 | | QCron user => "Asked about cron permissions for user " ^ user |
849 | | QFtp user => "Asked about FTP permissions for user " ^ user | |
4d5126e1 | 850 | | QTrustedPath user => "Asked about trusted path settings for user " ^ user |
737c68d4 | 851 | | QSocket user => "Asked about socket permissions for user " ^ user |
a95a0107 | 852 | |
3b267643 | 853 | fun service () = |
07cc384c | 854 | let |
aa56e112 AC |
855 | val () = Acl.read Config.aclFile |
856 | ||
3b267643 AC |
857 | val context = OpenSSL.context (Config.serverCert, |
858 | Config.serverKey, | |
859 | Config.trustStore) | |
36e42cb8 | 860 | val _ = Domain.set_context context |
3b267643 | 861 | |
60534712 | 862 | val sock = OpenSSL.listen (context, Config.dispatcherPort) |
3b267643 AC |
863 | |
864 | fun loop () = | |
60534712 | 865 | case OpenSSL.accept sock of |
3b267643 AC |
866 | NONE => () |
867 | | SOME bio => | |
868 | let | |
aa56e112 | 869 | val user = OpenSSL.peerCN bio |
c9731b9b | 870 | val () = print ("\nConnection from " ^ user ^ " at " ^ now () ^ "\n") |
aa56e112 AC |
871 | val () = Domain.setUser user |
872 | ||
08688401 AC |
873 | fun doIt f cleanup = |
874 | ((case f () of | |
875 | (msgLocal, SOME msgRemote) => | |
876 | (print msgLocal; | |
877 | print "\n"; | |
878 | Msg.send (bio, MsgError msgRemote)) | |
879 | | (msgLocal, NONE) => | |
880 | (print msgLocal; | |
881 | print "\n"; | |
882 | Msg.send (bio, MsgOk))) | |
883 | handle OpenSSL.OpenSSL _ => | |
884 | print "OpenSSL error\n" | |
885 | | OS.SysErr (s, _) => | |
886 | (print "System error: "; | |
887 | print s; | |
888 | print "\n"; | |
889 | Msg.send (bio, MsgError ("System error: " ^ s)) | |
890 | handle OpenSSL.OpenSSL _ => ()) | |
891 | | Fail s => | |
892 | (print "Failure: "; | |
893 | print s; | |
894 | print "\n"; | |
895 | Msg.send (bio, MsgError ("Failure: " ^ s)) | |
896 | handle OpenSSL.OpenSSL _ => ()) | |
897 | | ErrorMsg.Error => | |
898 | (print "Compilation error\n"; | |
899 | Msg.send (bio, MsgError "Error during configuration evaluation") | |
900 | handle OpenSSL.OpenSSL _ => ()); | |
901 | (cleanup (); | |
902 | ignore (OpenSSL.readChar bio); | |
903 | OpenSSL.close bio) | |
904 | handle OpenSSL.OpenSSL _ => (); | |
905 | loop ()) | |
906 | ||
c53e82e4 AC |
907 | fun doConfig codes = |
908 | let | |
909 | val _ = print "Configuration:\n" | |
910 | val _ = app (fn s => (print s; print "\n")) codes | |
911 | val _ = print "\n" | |
912 | ||
913 | val outname = OS.FileSys.tmpName () | |
914 | ||
915 | fun doOne code = | |
916 | let | |
917 | val outf = TextIO.openOut outname | |
918 | in | |
919 | TextIO.output (outf, code); | |
920 | TextIO.closeOut outf; | |
1824f573 | 921 | eval' outname |
c53e82e4 AC |
922 | end |
923 | in | |
08688401 AC |
924 | doIt (fn () => (Env.pre (); |
925 | app doOne codes; | |
926 | Env.post (); | |
927 | Msg.send (bio, MsgOk); | |
928 | ("Configuration complete.", NONE))) | |
929 | (fn () => OS.FileSys.remove outname) | |
c53e82e4 AC |
930 | end |
931 | ||
2e96b9d4 AC |
932 | fun checkAddr s = |
933 | case String.fields (fn ch => ch = #"@") s of | |
934 | [user'] => | |
935 | if user = user' then | |
936 | SOME (SetSA.User s) | |
937 | else | |
938 | NONE | |
939 | | [user', domain] => | |
940 | if Domain.validEmailUser user' andalso Domain.yourDomain domain then | |
941 | SOME (SetSA.Email s) | |
942 | else | |
943 | NONE | |
944 | | _ => NONE | |
945 | ||
36e42cb8 AC |
946 | fun cmdLoop () = |
947 | case Msg.recv bio of | |
948 | NONE => (OpenSSL.close bio | |
949 | handle OpenSSL.OpenSSL _ => (); | |
950 | loop ()) | |
951 | | SOME m => | |
952 | case m of | |
c53e82e4 AC |
953 | MsgConfig code => doConfig [code] |
954 | | MsgMultiConfig codes => doConfig codes | |
5ee41dd0 | 955 | |
9f27d58f | 956 | | MsgShutdown => |
62260c5f AC |
957 | if Acl.query {user = user, class = "priv", value = "all"} |
958 | orelse Acl.query {user = user, class = "priv", value = "shutdown"} then | |
c9731b9b | 959 | print ("Domtool dispatcher shutting down at " ^ now () ^ "\n\n") |
9f27d58f | 960 | else |
62260c5f AC |
961 | (print "Unauthorized shutdown command!\n"; |
962 | OpenSSL.close bio | |
9f27d58f AC |
963 | handle OpenSSL.OpenSSL _ => (); |
964 | loop ()) | |
965 | ||
5ee41dd0 | 966 | | MsgGrant acl => |
08688401 AC |
967 | doIt (fn () => |
968 | if Acl.query {user = user, class = "priv", value = "all"} then | |
969 | (Acl.grant acl; | |
970 | Acl.write Config.aclFile; | |
971 | ("Granted permission " ^ #value acl ^ " to " ^ #user acl ^ " in " ^ #class acl ^ ".", | |
972 | NONE)) | |
973 | else | |
974 | ("Unauthorized user asked to grant a permission!", | |
975 | SOME "Not authorized to grant privileges")) | |
976 | (fn () => ()) | |
977 | ||
411a85f2 | 978 | | MsgRevoke acl => |
08688401 AC |
979 | doIt (fn () => |
980 | if Acl.query {user = user, class = "priv", value = "all"} then | |
981 | (Acl.revoke acl; | |
982 | Acl.write Config.aclFile; | |
983 | ("Revoked permission " ^ #value acl ^ " from " ^ #user acl ^ " in " ^ #class acl ^ ".", | |
984 | NONE)) | |
985 | else | |
986 | ("Unauthorized user asked to revoke a permission!", | |
987 | SOME "Not authorized to revoke privileges")) | |
988 | (fn () => ()) | |
5ee41dd0 | 989 | |
08a04eb4 | 990 | | MsgListPerms user => |
08688401 AC |
991 | doIt (fn () => |
992 | (Msg.send (bio, MsgPerms (Acl.queryAll user)); | |
993 | ("Sent permission list for user " ^ user ^ ".", | |
994 | NONE))) | |
995 | (fn () => ()) | |
08a04eb4 | 996 | |
094877b1 | 997 | | MsgWhoHas perm => |
08688401 AC |
998 | doIt (fn () => |
999 | (Msg.send (bio, MsgWhoHasResponse (Acl.whoHas perm)); | |
1000 | ("Sent whohas response for " ^ #class perm ^ " / " ^ #value perm ^ ".", | |
1001 | NONE))) | |
1002 | (fn () => ()) | |
094877b1 | 1003 | |
e69e60cc | 1004 | | MsgRmdom doms => |
08688401 AC |
1005 | doIt (fn () => |
1006 | if Acl.query {user = user, class = "priv", value = "all"} | |
1007 | orelse List.all (fn dom => Acl.query {user = user, class = "domain", value = dom}) doms then | |
1008 | (Domain.rmdom doms; | |
1009 | app (fn dom => | |
1010 | Acl.revokeFromAll {class = "domain", value = dom}) doms; | |
1011 | Acl.write Config.aclFile; | |
1012 | ("Removed domains" ^ foldl (fn (d, s) => s ^ " " ^ d) "" doms ^ ".", | |
1013 | NONE)) | |
1014 | else | |
1015 | ("Unauthorized user asked to remove a domain!", | |
1016 | SOME "Not authorized to remove that domain")) | |
1017 | (fn () => ()) | |
1824f573 AC |
1018 | |
1019 | | MsgRegenerate => | |
08688401 AC |
1020 | doIt (fn () => |
1021 | if Acl.query {user = user, class = "priv", value = "regen"} | |
1022 | orelse Acl.query {user = user, class = "priv", value = "all"} then | |
1023 | (regenerate context; | |
1024 | ("Regenerated all configuration.", | |
1025 | NONE)) | |
1026 | else | |
1027 | ("Unauthorized user asked to regenerate!", | |
1028 | SOME "Not authorized to regenerate")) | |
1029 | (fn () => ()) | |
e69e60cc | 1030 | |
05323cbc | 1031 | | MsgRmuser user' => |
08688401 AC |
1032 | doIt (fn () => |
1033 | if Acl.query {user = user, class = "priv", value = "all"} then | |
1034 | (rmuser user'; | |
1035 | Acl.write Config.aclFile; | |
1036 | ("Removed user " ^ user' ^ ".", | |
1037 | NONE)) | |
1038 | else | |
1039 | ("Unauthorized user asked to remove a user!", | |
1040 | SOME "Not authorized to remove users")) | |
1041 | (fn () => ()) | |
d541c618 | 1042 | |
21d921a5 | 1043 | | MsgCreateDbUser {dbtype, passwd} => |
08688401 AC |
1044 | doIt (fn () => |
1045 | case Dbms.lookup dbtype of | |
1046 | NONE => ("Database user creation request with unknown datatype type " ^ dbtype, | |
1047 | SOME ("Unknown database type " ^ dbtype)) | |
1048 | | SOME handler => | |
1049 | case #adduser handler {user = user, passwd = passwd} of | |
1050 | NONE => ("Added " ^ dbtype ^ " user " ^ user ^ ".", | |
1051 | NONE) | |
1052 | | SOME msg => | |
1053 | ("Error adding a " ^ dbtype ^ " user " ^ user ^ ": " ^ msg, | |
1054 | SOME ("Error adding user: " ^ msg))) | |
1055 | (fn () => ()) | |
c189cbe9 | 1056 | |
86aa5de7 AC |
1057 | | MsgDbPasswd {dbtype, passwd} => |
1058 | doIt (fn () => | |
1059 | case Dbms.lookup dbtype of | |
1060 | NONE => ("Database passwd request with unknown datatype type " ^ dbtype, | |
1061 | SOME ("Unknown database type " ^ dbtype)) | |
1062 | | SOME handler => | |
1063 | case #passwd handler {user = user, passwd = passwd} of | |
1064 | NONE => ("Changed " ^ dbtype ^ " password of user " ^ user ^ ".", | |
1065 | NONE) | |
1066 | | SOME msg => | |
1067 | ("Error setting " ^ dbtype ^ " password of user " ^ user ^ ": " ^ msg, | |
1068 | SOME ("Error adding user: " ^ msg))) | |
1069 | (fn () => ()) | |
1070 | ||
90dd48df | 1071 | | MsgCreateDbTable {dbtype, dbname} => |
08688401 AC |
1072 | doIt (fn () => |
1073 | if Dbms.validDbname dbname then | |
1074 | case Dbms.lookup dbtype of | |
1075 | NONE => ("Database creation request with unknown datatype type " ^ dbtype, | |
1076 | SOME ("Unknown database type " ^ dbtype)) | |
1077 | | SOME handler => | |
1078 | case #createdb handler {user = user, dbname = dbname} of | |
1079 | NONE => ("Created database " ^ user ^ "_" ^ dbname ^ ".", | |
1080 | NONE) | |
1081 | | SOME msg => ("Error creating database " ^ user ^ "_" ^ dbname ^ ": " ^ msg, | |
1082 | SOME ("Error creating database: " ^ msg)) | |
1083 | else | |
1084 | ("Invalid database name " ^ user ^ "_" ^ dbname, | |
1085 | SOME ("Invalid database name " ^ dbname))) | |
1086 | (fn () => ()) | |
1087 | ||
1d3ef80e AC |
1088 | | MsgListMailboxes domain => |
1089 | doIt (fn () => | |
1090 | if not (Domain.yourDomain domain) then | |
1091 | ("User wasn't authorized to list mailboxes for " ^ domain, | |
1092 | SOME "You're not authorized to configure that domain.") | |
1093 | else | |
1094 | case Vmail.list domain of | |
1095 | Vmail.Listing users => (Msg.send (bio, MsgMailboxes users); | |
1096 | ("Sent mailbox list for " ^ domain, | |
1097 | NONE)) | |
1098 | | Vmail.Error msg => ("Error listing mailboxes for " ^ domain ^ ": " ^ msg, | |
1099 | SOME msg)) | |
1100 | (fn () => ()) | |
1101 | ||
08688401 AC |
1102 | | MsgNewMailbox {domain, user = emailUser, passwd, mailbox} => |
1103 | doIt (fn () => | |
1104 | if not (Domain.yourDomain domain) then | |
1105 | ("User wasn't authorized to add a mailbox to " ^ domain, | |
1106 | SOME "You're not authorized to configure that domain.") | |
2e96b9d4 | 1107 | else if not (Domain.validEmailUser emailUser) then |
08688401 AC |
1108 | ("Invalid e-mail username " ^ emailUser, |
1109 | SOME "Invalid e-mail username") | |
1110 | else if not (CharVector.all Char.isGraph passwd) then | |
1111 | ("Invalid password", | |
1112 | SOME "Invalid password; may only contain printable, non-space characters") | |
1113 | else if not (Domain.yourPath mailbox) then | |
1114 | ("User wasn't authorized to add a mailbox at " ^ mailbox, | |
1115 | SOME "You're not authorized to use that mailbox location.") | |
1116 | else | |
1117 | case Vmail.add {requester = user, | |
1118 | domain = domain, user = emailUser, | |
1119 | passwd = passwd, mailbox = mailbox} of | |
1120 | NONE => ("Added mailbox " ^ emailUser ^ "@" ^ domain ^ " at " ^ mailbox, | |
1121 | NONE) | |
9ffe2f0f | 1122 | | SOME msg => ("Error adding mailbox " ^ emailUser ^ "@" ^ domain ^ ": " ^ msg, |
08688401 AC |
1123 | SOME msg)) |
1124 | (fn () => ()) | |
1125 | ||
1126 | | MsgPasswdMailbox {domain, user = emailUser, passwd} => | |
1127 | doIt (fn () => | |
1128 | if not (Domain.yourDomain domain) then | |
1129 | ("User wasn't authorized to change password of a mailbox for " ^ domain, | |
1130 | SOME "You're not authorized to configure that domain.") | |
2e96b9d4 | 1131 | else if not (Domain.validEmailUser emailUser) then |
08688401 AC |
1132 | ("Invalid e-mail username " ^ emailUser, |
1133 | SOME "Invalid e-mail username") | |
1134 | else if not (CharVector.all Char.isGraph passwd) then | |
1135 | ("Invalid password", | |
1136 | SOME "Invalid password; may only contain printable, non-space characters") | |
1137 | else | |
1138 | case Vmail.passwd {domain = domain, user = emailUser, | |
1139 | passwd = passwd} of | |
1140 | NONE => ("Changed password of mailbox " ^ emailUser ^ "@" ^ domain, | |
1141 | NONE) | |
9ffe2f0f | 1142 | | SOME msg => ("Error changing mailbox password for " ^ emailUser ^ "@" ^ domain ^ ": " ^ msg, |
08688401 AC |
1143 | SOME msg)) |
1144 | (fn () => ()) | |
1145 | ||
1146 | | MsgRmMailbox {domain, user = emailUser} => | |
1147 | doIt (fn () => | |
1148 | if not (Domain.yourDomain domain) then | |
1149 | ("User wasn't authorized to change password of a mailbox for " ^ domain, | |
1150 | SOME "You're not authorized to configure that domain.") | |
2e96b9d4 | 1151 | else if not (Domain.validEmailUser emailUser) then |
08688401 AC |
1152 | ("Invalid e-mail username " ^ emailUser, |
1153 | SOME "Invalid e-mail username") | |
1154 | else | |
1155 | case Vmail.rm {domain = domain, user = emailUser} of | |
1156 | NONE => ("Deleted mailbox " ^ emailUser ^ "@" ^ domain, | |
1157 | NONE) | |
9ffe2f0f | 1158 | | SOME msg => ("Error deleting mailbox " ^ emailUser ^ "@" ^ domain ^ ": " ^ msg, |
08688401 AC |
1159 | SOME msg)) |
1160 | (fn () => ()) | |
90dd48df | 1161 | |
2e96b9d4 AC |
1162 | | MsgSaQuery addr => |
1163 | doIt (fn () => | |
1164 | case checkAddr addr of | |
1165 | NONE => ("User tried to query SA filtering for " ^ addr, | |
1166 | SOME "You aren't allowed to configure SA filtering for that recipient.") | |
1167 | | SOME addr' => (Msg.send (bio, MsgSaStatus (SetSA.query addr')); | |
1168 | ("Queried SA filtering status for " ^ addr, | |
1169 | NONE))) | |
1170 | (fn () => ()) | |
1171 | ||
1172 | | MsgSaSet (addr, b) => | |
1173 | doIt (fn () => | |
1174 | case checkAddr addr of | |
1175 | NONE => ("User tried to set SA filtering for " ^ addr, | |
1176 | SOME "You aren't allowed to configure SA filtering for that recipient.") | |
1177 | | SOME addr' => (SetSA.set (addr', b); | |
1178 | Msg.send (bio, MsgOk); | |
1179 | ("Set SA filtering status for " ^ addr ^ " to " | |
1180 | ^ (if b then "ON" else "OFF"), | |
1181 | NONE))) | |
1182 | (fn () => ()) | |
1183 | ||
2bc5ed22 AC |
1184 | | MsgSmtpLogReq domain => |
1185 | doIt (fn () => | |
1186 | if not (Domain.yourDomain domain) then | |
1187 | ("Unauthorized user tried to request SMTP logs for " ^ domain, | |
1188 | SOME "You aren't authorized to configure that domain.") | |
1189 | else | |
1190 | (SmtpLog.search (fn line => Msg.send (bio, MsgSmtpLogRes line)) | |
1191 | domain; | |
1192 | ("Requested SMTP logs for " ^ domain, | |
1193 | NONE))) | |
1194 | (fn () => ()) | |
1195 | ||
a95a0107 AC |
1196 | | MsgQuery q => |
1197 | doIt (fn () => (Msg.send (bio, answerQuery q); | |
1198 | (describeQuery q, | |
75585a67 AC |
1199 | NONE))) |
1200 | (fn () => ()) | |
1201 | ||
36e42cb8 | 1202 | | _ => |
08688401 AC |
1203 | doIt (fn () => ("Unexpected command", |
1204 | SOME "Unexpected command")) | |
1205 | (fn () => ()) | |
36e42cb8 AC |
1206 | in |
1207 | cmdLoop () | |
1208 | end | |
97665758 AC |
1209 | handle OpenSSL.OpenSSL s => |
1210 | (print ("OpenSSL error: " ^ s ^ "\n"); | |
1211 | OpenSSL.close bio | |
1212 | handle OpenSSL.OpenSSL _ => (); | |
1213 | loop ()) | |
1214 | | OS.SysErr (s, _) => | |
1215 | (print ("System error: " ^ s ^ "\n"); | |
1216 | OpenSSL.close bio | |
1217 | handle OpenSSL.OpenSSL _ => (); | |
1218 | loop ()) | |
36e42cb8 | 1219 | in |
c9731b9b | 1220 | print ("Domtool dispatcher starting up at " ^ now () ^ "\n"); |
361a1e7f | 1221 | print "Listening for connections....\n"; |
36e42cb8 AC |
1222 | loop (); |
1223 | OpenSSL.shutdown sock | |
1224 | end | |
1225 | ||
1226 | fun slave () = | |
1227 | let | |
6e62228d | 1228 | val host = Slave.hostname () |
36e42cb8 AC |
1229 | |
1230 | val context = OpenSSL.context (Config.certDir ^ "/" ^ host ^ ".pem", | |
a088cea6 | 1231 | Config.keyDir ^ "/" ^ host ^ "/key.pem", |
36e42cb8 AC |
1232 | Config.trustStore) |
1233 | ||
1234 | val sock = OpenSSL.listen (context, Config.slavePort) | |
1235 | ||
c9731b9b AC |
1236 | val _ = print ("Slave server starting at " ^ now () ^ "\n") |
1237 | ||
36e42cb8 AC |
1238 | fun loop () = |
1239 | case OpenSSL.accept sock of | |
1240 | NONE => () | |
1241 | | SOME bio => | |
1242 | let | |
1243 | val peer = OpenSSL.peerCN bio | |
c9731b9b | 1244 | val () = print ("\nConnection from " ^ peer ^ " at " ^ now () ^ "\n") |
3b267643 | 1245 | in |
c9731b9b | 1246 | if peer = Config.dispatcherName then let |
36e42cb8 AC |
1247 | fun loop' files = |
1248 | case Msg.recv bio of | |
1249 | NONE => print "Dispatcher closed connection unexpectedly\n" | |
1250 | | SOME m => | |
1251 | case m of | |
1252 | MsgFile file => loop' (file :: files) | |
1253 | | MsgDoFiles => (Slave.handleChanges files; | |
1254 | Msg.send (bio, MsgOk)) | |
71420f8b AC |
1255 | | MsgRegenerate => (Domain.resetLocal (); |
1256 | Msg.send (bio, MsgOk)) | |
36e42cb8 AC |
1257 | | _ => (print "Dispatcher sent unexpected command\n"; |
1258 | Msg.send (bio, MsgError "Unexpected command")) | |
1259 | in | |
1260 | loop' []; | |
1261 | ignore (OpenSSL.readChar bio); | |
1262 | OpenSSL.close bio; | |
1263 | loop () | |
1264 | end | |
c9731b9b AC |
1265 | else if peer = "domtool" then |
1266 | case Msg.recv bio of | |
1267 | SOME MsgShutdown => (OpenSSL.close bio; | |
1268 | print ("Shutting down at " ^ now () ^ "\n\n")) | |
1269 | | _ => (OpenSSL.close bio; | |
1270 | loop ()) | |
1271 | else | |
a95a0107 AC |
1272 | case Msg.recv bio of |
1273 | SOME (MsgQuery q) => (print (describeQuery q ^ "\n"); | |
1274 | Msg.send (bio, answerQuery q); | |
1275 | ignore (OpenSSL.readChar bio); | |
1276 | OpenSSL.close bio; | |
1277 | loop ()) | |
1278 | | _ => (OpenSSL.close bio; | |
1279 | loop ()) | |
3196000d AC |
1280 | end handle OpenSSL.OpenSSL s => |
1281 | (print ("OpenSSL error: "^ s ^ "\n"); | |
1282 | OpenSSL.close bio | |
1283 | handle OpenSSL.OpenSSL _ => (); | |
1284 | loop ()) | |
7af7d4cb AC |
1285 | | OS.SysErr (s, _) => |
1286 | (print ("System error: "^ s ^ "\n"); | |
1287 | OpenSSL.close bio | |
1288 | handle OpenSSL.OpenSSL _ => (); | |
1289 | loop ()) | |
07cc384c | 1290 | in |
3b267643 AC |
1291 | loop (); |
1292 | OpenSSL.shutdown sock | |
07cc384c AC |
1293 | end |
1294 | ||
44a5ce2f | 1295 | fun listBasis () = |
3196000d AC |
1296 | let |
1297 | val dir = Posix.FileSys.opendir Config.libRoot | |
1298 | ||
1299 | fun loop files = | |
1300 | case Posix.FileSys.readdir dir of | |
1301 | NONE => (Posix.FileSys.closedir dir; | |
1302 | files) | |
1303 | | SOME fname => | |
1304 | if String.isSuffix ".dtl" fname then | |
1305 | loop (OS.Path.joinDirFile {dir = Config.libRoot, | |
1306 | file = fname} | |
1307 | :: files) | |
1308 | else | |
1309 | loop files | |
3196000d | 1310 | in |
44a5ce2f | 1311 | loop [] |
3196000d AC |
1312 | end |
1313 | ||
44a5ce2f AC |
1314 | fun autodocBasis outdir = |
1315 | Autodoc.autodoc {outdir = outdir, infiles = listBasis ()} | |
1316 | ||
234b917a | 1317 | end |