Adding domain description
[hcoop/domtool2.git] / src / main.sml
... / ...
CommitLineData
1(* HCoop Domtool (http://hcoop.sourceforge.net/)
2 * Copyright (c) 2006-2007, 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.
17 *)
18
19(* Main interface *)
20
21structure Main :> MAIN = struct
22
23open Ast MsgTypes Print
24
25structure SM = StringMap
26
27fun init () = Acl.read Config.aclFile
28
29fun check' G fname =
30 let
31 val prog = Parse.parse fname
32 in
33 if !ErrorMsg.anyErrors then
34 G
35 else
36 Tycheck.checkFile G (Defaults.tInit ()) prog
37 end
38
39fun basis () =
40 let
41 val dir = Posix.FileSys.opendir Config.libRoot
42
43 fun loop files =
44 case Posix.FileSys.readdir dir of
45 NONE => (Posix.FileSys.closedir dir;
46 files)
47 | SOME fname =>
48 if String.isSuffix ".dtl" fname then
49 loop (OS.Path.joinDirFile {dir = Config.libRoot,
50 file = fname}
51 :: files)
52 else
53 loop files
54
55 val files = loop []
56 val (_, files) = Order.order NONE files
57 in
58 if !ErrorMsg.anyErrors then
59 Env.empty
60 else
61 (Tycheck.allowExterns ();
62 foldl (fn (fname, G) => check' G fname) Env.empty files
63 before Tycheck.disallowExterns ())
64 end
65
66fun check fname =
67 let
68 val _ = ErrorMsg.reset ()
69 val _ = Env.preTycheck ()
70
71 val b = basis ()
72 in
73 if !ErrorMsg.anyErrors then
74 raise ErrorMsg.Error
75 else
76 let
77 val _ = Tycheck.disallowExterns ()
78 val _ = ErrorMsg.reset ()
79 val prog = Parse.parse fname
80 in
81 if !ErrorMsg.anyErrors then
82 raise ErrorMsg.Error
83 else
84 let
85 val G' = Tycheck.checkFile b (Defaults.tInit ()) prog
86 in
87 if !ErrorMsg.anyErrors then
88 raise ErrorMsg.Error
89 else
90 (G', #3 prog)
91 end
92 end
93 end
94
95fun notTmp s =
96 String.sub (s, 0) <> #"."
97 andalso CharVector.all (fn ch => Char.isAlphaNum ch orelse ch = #"." orelse ch = #"_" orelse ch = #"-") s
98
99fun setupUser () =
100 let
101 val user =
102 case Posix.ProcEnv.getenv "DOMTOOL_USER" of
103 NONE =>
104 let
105 val uid = Posix.ProcEnv.getuid ()
106 in
107 Posix.SysDB.Passwd.name (Posix.SysDB.getpwuid uid)
108 end
109 | SOME user => user
110 in
111 Acl.read Config.aclFile;
112 Domain.setUser user;
113 user
114 end
115
116fun checkDir' dname =
117 let
118 val b = basis ()
119
120 val dir = Posix.FileSys.opendir dname
121
122 fun loop files =
123 case Posix.FileSys.readdir dir of
124 NONE => (Posix.FileSys.closedir dir;
125 files)
126 | SOME fname =>
127 if notTmp fname then
128 loop (OS.Path.joinDirFile {dir = dname,
129 file = fname}
130 :: files)
131 else
132 loop files
133
134 val files = loop []
135 val (_, files) = Order.order (SOME b) files
136 in
137 if !ErrorMsg.anyErrors then
138 raise ErrorMsg.Error
139 else
140 (foldl (fn (fname, G) => check' G fname) b files;
141 if !ErrorMsg.anyErrors then
142 raise ErrorMsg.Error
143 else
144 ())
145 end
146
147fun checkDir dname =
148 (setupUser ();
149 checkDir' dname)
150
151fun reduce fname =
152 let
153 val (G, body) = check fname
154 in
155 if !ErrorMsg.anyErrors then
156 NONE
157 else
158 case body of
159 SOME body =>
160 let
161 val body' = Reduce.reduceExp G body
162 in
163 (*printd (PD.hovBox (PD.PPS.Rel 0,
164 [PD.string "Result:",
165 PD.space 1,
166 p_exp body']))*)
167 SOME body'
168 end
169 | _ => NONE
170 end
171
172fun eval fname =
173 case reduce fname of
174 (SOME body') =>
175 if !ErrorMsg.anyErrors then
176 raise ErrorMsg.Error
177 else
178 Eval.exec (Defaults.eInit ()) body'
179 | NONE => raise ErrorMsg.Error
180
181fun eval' fname =
182 case reduce fname of
183 (SOME body') =>
184 if !ErrorMsg.anyErrors then
185 raise ErrorMsg.Error
186 else
187 ignore (Eval.exec' (Defaults.eInit ()) body')
188 | NONE => raise ErrorMsg.Error
189
190val dispatcher =
191 Config.dispatcher ^ ":" ^ Int.toString Config.dispatcherPort
192
193val self =
194 "localhost:" ^ Int.toString Config.slavePort
195
196fun context x =
197 (OpenSSL.context false x)
198 handle e as OpenSSL.OpenSSL s =>
199 (print "Couldn't find your certificate.\nYou probably haven't been given any Domtool privileges.\n";
200 print ("I looked in: " ^ #1 x ^ "\n");
201 print ("Additional information: " ^ s ^ "\n");
202 raise e)
203
204fun requestContext f =
205 let
206 val user = setupUser ()
207
208 val () = f ()
209
210 val context = context (Config.certDir ^ "/" ^ user ^ ".pem",
211 Config.keyDir ^ "/" ^ user ^ "/key.pem",
212 Config.trustStore)
213 in
214 (user, context)
215 end
216
217fun requestBio f =
218 let
219 val (user, context) = requestContext f
220 in
221 (user, OpenSSL.connect (context, dispatcher))
222 end
223
224fun requestSlaveBio () =
225 let
226 val (user, context) = requestContext (fn () => ())
227 in
228 (user, OpenSSL.connect (context, self))
229 end
230
231fun request fname =
232 let
233 val (user, bio) = requestBio (fn () => ignore (check fname))
234
235 val inf = TextIO.openIn fname
236
237 fun loop lines =
238 case TextIO.inputLine inf of
239 NONE => String.concat (List.rev lines)
240 | SOME line => loop (line :: lines)
241
242 val code = loop []
243 in
244 TextIO.closeIn inf;
245 Msg.send (bio, MsgConfig code);
246 case Msg.recv bio of
247 NONE => print "Server closed connection unexpectedly.\n"
248 | SOME m =>
249 case m of
250 MsgOk => print "Configuration succeeded.\n"
251 | MsgError s => print ("Configuration failed: " ^ s ^ "\n")
252 | _ => print "Unexpected server reply.\n";
253 OpenSSL.close bio
254 end
255 handle ErrorMsg.Error => ()
256
257fun requestDir dname =
258 let
259 val _ = if Posix.FileSys.access (dname, []) then
260 ()
261 else
262 (print ("Can't access " ^ dname ^ ".\n");
263 print "Did you mean to run domtool on a specific file, instead of asking for all\n";
264 print "files in your ~/.domtool directory?\n";
265 OS.Process.exit OS.Process.failure)
266
267 val _ = ErrorMsg.reset ()
268
269 val (user, bio) = requestBio (fn () => checkDir' dname)
270
271 val b = basis ()
272
273 val dir = Posix.FileSys.opendir dname
274
275 fun loop files =
276 case Posix.FileSys.readdir dir of
277 NONE => (Posix.FileSys.closedir dir;
278 files)
279 | SOME fname =>
280 if notTmp fname then
281 loop (OS.Path.joinDirFile {dir = dname,
282 file = fname}
283 :: files)
284 else
285 loop files
286
287 val files = loop []
288 val (_, files) = Order.order (SOME b) files
289
290 val _ = if !ErrorMsg.anyErrors then
291 raise ErrorMsg.Error
292 else
293 ()
294
295 val codes = map (fn fname =>
296 let
297 val inf = TextIO.openIn fname
298
299 fun loop lines =
300 case TextIO.inputLine inf of
301 NONE => String.concat (rev lines)
302 | SOME line => loop (line :: lines)
303 in
304 loop []
305 before TextIO.closeIn inf
306 end) files
307 in
308 if !ErrorMsg.anyErrors then
309 ()
310 else
311 (Msg.send (bio, MsgMultiConfig codes);
312 case Msg.recv bio of
313 NONE => print "Server closed connection unexpectedly.\n"
314 | SOME m =>
315 case m of
316 MsgOk => print "Configuration succeeded.\n"
317 | MsgError s => print ("Configuration failed: " ^ s ^ "\n")
318 | _ => print "Unexpected server reply.\n";
319 OpenSSL.close bio)
320 end
321 handle ErrorMsg.Error => ()
322
323fun requestPing () =
324 let
325 val (_, bio) = requestBio (fn () => ())
326 in
327 OpenSSL.close bio;
328 OS.Process.success
329 end
330 handle _ => OS.Process.failure
331
332fun requestShutdown () =
333 let
334 val (_, bio) = requestBio (fn () => ())
335 in
336 Msg.send (bio, MsgShutdown);
337 case Msg.recv bio of
338 NONE => print "Server closed connection unexpectedly.\n"
339 | SOME m =>
340 case m of
341 MsgOk => print "Shutdown begun.\n"
342 | MsgError s => print ("Shutdown failed: " ^ s ^ "\n")
343 | _ => print "Unexpected server reply.\n";
344 OpenSSL.close bio
345 end
346
347fun requestSlavePing () =
348 let
349 val (_, bio) = requestSlaveBio ()
350 in
351 OpenSSL.close bio;
352 OS.Process.success
353 end
354 handle _ => OS.Process.failure
355
356fun requestSlaveShutdown () =
357 let
358 val (_, bio) = requestSlaveBio ()
359 in
360 Msg.send (bio, MsgShutdown);
361 case Msg.recv bio of
362 NONE => print "Server closed connection unexpectedly.\n"
363 | SOME m =>
364 case m of
365 MsgOk => print "Shutdown begun.\n"
366 | MsgError s => print ("Shutdown failed: " ^ s ^ "\n")
367 | _ => print "Unexpected server reply.\n";
368 OpenSSL.close bio
369 end
370
371fun requestGrant acl =
372 let
373 val (user, bio) = requestBio (fn () => ())
374 in
375 Msg.send (bio, MsgGrant acl);
376 case Msg.recv bio of
377 NONE => print "Server closed connection unexpectedly.\n"
378 | SOME m =>
379 case m of
380 MsgOk => print "Grant succeeded.\n"
381 | MsgError s => print ("Grant failed: " ^ s ^ "\n")
382 | _ => print "Unexpected server reply.\n";
383 OpenSSL.close bio
384 end
385
386fun requestRevoke acl =
387 let
388 val (user, bio) = requestBio (fn () => ())
389 in
390 Msg.send (bio, MsgRevoke acl);
391 case Msg.recv bio of
392 NONE => print "Server closed connection unexpectedly.\n"
393 | SOME m =>
394 case m of
395 MsgOk => print "Revoke succeeded.\n"
396 | MsgError s => print ("Revoke failed: " ^ s ^ "\n")
397 | _ => print "Unexpected server reply.\n";
398 OpenSSL.close bio
399 end
400
401fun requestListPerms user =
402 let
403 val (_, bio) = requestBio (fn () => ())
404 in
405 Msg.send (bio, MsgListPerms user);
406 (case Msg.recv bio of
407 NONE => (print "Server closed connection unexpectedly.\n";
408 NONE)
409 | SOME m =>
410 case m of
411 MsgPerms perms => SOME perms
412 | MsgError s => (print ("Listing failed: " ^ s ^ "\n");
413 NONE)
414 | _ => (print "Unexpected server reply.\n";
415 NONE))
416 before OpenSSL.close bio
417 end
418
419fun requestWhoHas perm =
420 let
421 val (_, bio) = requestBio (fn () => ())
422 in
423 Msg.send (bio, MsgWhoHas perm);
424 (case Msg.recv bio of
425 NONE => (print "Server closed connection unexpectedly.\n";
426 NONE)
427 | SOME m =>
428 case m of
429 MsgWhoHasResponse users => SOME users
430 | MsgError s => (print ("whohas failed: " ^ s ^ "\n");
431 NONE)
432 | _ => (print "Unexpected server reply.\n";
433 NONE))
434 before OpenSSL.close bio
435 end
436
437fun requestRegen () =
438 let
439 val (_, bio) = requestBio (fn () => ())
440 in
441 Msg.send (bio, MsgRegenerate);
442 case Msg.recv bio of
443 NONE => print "Server closed connection unexpectedly.\n"
444 | SOME m =>
445 case m of
446 MsgOk => print "Regeneration succeeded.\n"
447 | MsgError s => print ("Regeneration failed: " ^ s ^ "\n")
448 | _ => print "Unexpected server reply.\n";
449 OpenSSL.close bio
450 end
451
452fun requestRegenTc () =
453 let
454 val (_, bio) = requestBio (fn () => ())
455 in
456 Msg.send (bio, MsgRegenerateTc);
457 case Msg.recv bio of
458 NONE => print "Server closed connection unexpectedly.\n"
459 | SOME m =>
460 case m of
461 MsgOk => print "All configuration validated.\n"
462 | MsgError s => print ("Configuration validation failed: " ^ s ^ "\n")
463 | _ => print "Unexpected server reply.\n";
464 OpenSSL.close bio
465 end
466
467fun requestRmdom dom =
468 let
469 val (_, bio) = requestBio (fn () => ())
470 in
471 Msg.send (bio, MsgRmdom dom);
472 case Msg.recv bio of
473 NONE => print "Server closed connection unexpectedly.\n"
474 | SOME m =>
475 case m of
476 MsgOk => print "Removal succeeded.\n"
477 | MsgError s => print ("Removal failed: " ^ s ^ "\n")
478 | _ => print "Unexpected server reply.\n";
479 OpenSSL.close bio
480 end
481
482fun requestRmuser user =
483 let
484 val (_, bio) = requestBio (fn () => ())
485 in
486 Msg.send (bio, MsgRmuser user);
487 case Msg.recv bio of
488 NONE => print "Server closed connection unexpectedly.\n"
489 | SOME m =>
490 case m of
491 MsgOk => print "Removal succeeded.\n"
492 | MsgError s => print ("Removal failed: " ^ s ^ "\n")
493 | _ => print "Unexpected server reply.\n";
494 OpenSSL.close bio
495 end
496
497fun requestDbUser dbtype =
498 let
499 val (_, bio) = requestBio (fn () => ())
500 in
501 Msg.send (bio, MsgCreateDbUser dbtype);
502 case Msg.recv bio of
503 NONE => print "Server closed connection unexpectedly.\n"
504 | SOME m =>
505 case m of
506 MsgOk => print "Your user has been created.\n"
507 | MsgError s => print ("Creation failed: " ^ s ^ "\n")
508 | _ => print "Unexpected server reply.\n";
509 OpenSSL.close bio
510 end
511
512fun requestDbPasswd rc =
513 let
514 val (_, bio) = requestBio (fn () => ())
515 in
516 Msg.send (bio, MsgDbPasswd rc);
517 case Msg.recv bio of
518 NONE => print "Server closed connection unexpectedly.\n"
519 | SOME m =>
520 case m of
521 MsgOk => print "Your password has been changed.\n"
522 | MsgError s => print ("Password set failed: " ^ s ^ "\n")
523 | _ => print "Unexpected server reply.\n";
524 OpenSSL.close bio
525 end
526
527fun requestDbTable p =
528 let
529 val (user, bio) = requestBio (fn () => ())
530 in
531 Msg.send (bio, MsgCreateDb p);
532 case Msg.recv bio of
533 NONE => print "Server closed connection unexpectedly.\n"
534 | SOME m =>
535 case m of
536 MsgOk => print ("Your database " ^ user ^ "_" ^ #dbname p ^ " has been created.\n")
537 | MsgError s => print ("Creation failed: " ^ s ^ "\n")
538 | _ => print "Unexpected server reply.\n";
539 OpenSSL.close bio
540 end
541
542fun requestDbDrop p =
543 let
544 val (user, bio) = requestBio (fn () => ())
545 in
546 Msg.send (bio, MsgDropDb p);
547 case Msg.recv bio of
548 NONE => print "Server closed connection unexpectedly.\n"
549 | SOME m =>
550 case m of
551 MsgOk => print ("Your database " ^ user ^ "_" ^ #dbname p ^ " has been dropped.\n")
552 | MsgError s => print ("Drop failed: " ^ s ^ "\n")
553 | _ => print "Unexpected server reply.\n";
554 OpenSSL.close bio
555 end
556
557fun requestDbGrant p =
558 let
559 val (user, bio) = requestBio (fn () => ())
560 in
561 Msg.send (bio, MsgGrantDb p);
562 case Msg.recv bio of
563 NONE => print "Server closed connection unexpectedly.\n"
564 | SOME m =>
565 case m of
566 MsgOk => print ("You've been granted all allowed privileges to database " ^ user ^ "_" ^ #dbname p ^ ".\n")
567 | MsgError s => print ("Grant failed: " ^ s ^ "\n")
568 | _ => print "Unexpected server reply.\n";
569 OpenSSL.close bio
570 end
571
572fun requestListMailboxes domain =
573 let
574 val (_, bio) = requestBio (fn () => ())
575 in
576 Msg.send (bio, MsgListMailboxes domain);
577 (case Msg.recv bio of
578 NONE => Vmail.Error "Server closed connection unexpectedly."
579 | SOME m =>
580 case m of
581 MsgMailboxes users => (Msg.send (bio, MsgOk);
582 Vmail.Listing users)
583 | MsgError s => Vmail.Error ("Listing failed: " ^ s)
584 | _ => Vmail.Error "Unexpected server reply.")
585 before OpenSSL.close bio
586 end
587
588fun requestNewMailbox p =
589 let
590 val (_, bio) = requestBio (fn () => ())
591 in
592 Msg.send (bio, MsgNewMailbox p);
593 case Msg.recv bio of
594 NONE => print "Server closed connection unexpectedly.\n"
595 | SOME m =>
596 case m of
597 MsgOk => print ("A mapping for " ^ #user p ^ "@" ^ #domain p ^ " has been created.\n")
598 | MsgError s => print ("Creation failed: " ^ s ^ "\n")
599 | _ => print "Unexpected server reply.\n";
600 OpenSSL.close bio
601 end
602
603fun requestPasswdMailbox p =
604 let
605 val (_, bio) = requestBio (fn () => ())
606 in
607 Msg.send (bio, MsgPasswdMailbox p);
608 case Msg.recv bio of
609 NONE => print "Server closed connection unexpectedly.\n"
610 | SOME m =>
611 case m of
612 MsgOk => print ("The password for " ^ #user p ^ "@" ^ #domain p ^ " has been changed.\n")
613 | MsgError s => print ("Set failed: " ^ s ^ "\n")
614 | _ => print "Unexpected server reply.\n";
615 OpenSSL.close bio
616 end
617
618fun requestRmMailbox p =
619 let
620 val (_, bio) = requestBio (fn () => ())
621 in
622 Msg.send (bio, MsgRmMailbox p);
623 case Msg.recv bio of
624 NONE => print "Server closed connection unexpectedly.\n"
625 | SOME m =>
626 case m of
627 MsgOk => print ("The mapping for mailbox " ^ #user p ^ "@" ^ #domain p ^ " has been deleted.\n")
628 | MsgError s => print ("Remove failed: " ^ s ^ "\n")
629 | _ => print "Unexpected server reply.\n";
630 OpenSSL.close bio
631 end
632
633fun requestSaQuery addr =
634 let
635 val (_, bio) = requestBio (fn () => ())
636 in
637 Msg.send (bio, MsgSaQuery addr);
638 (case Msg.recv bio of
639 NONE => print "Server closed connection unexpectedly.\n"
640 | SOME m =>
641 case m of
642 MsgSaStatus b => (print ("SpamAssassin filtering for " ^ addr ^ " is "
643 ^ (if b then "ON" else "OFF") ^ ".\n");
644 Msg.send (bio, MsgOk))
645 | MsgError s => print ("Query failed: " ^ s ^ "\n")
646 | _ => print "Unexpected server reply.\n")
647 before OpenSSL.close bio
648 end
649
650fun requestSaSet p =
651 let
652 val (_, bio) = requestBio (fn () => ())
653 in
654 Msg.send (bio, MsgSaSet p);
655 case Msg.recv bio of
656 NONE => print "Server closed connection unexpectedly.\n"
657 | SOME m =>
658 case m of
659 MsgOk => print ("SpamAssassin filtering for " ^ #1 p ^ " is now "
660 ^ (if #2 p then "ON" else "OFF") ^ ".\n")
661 | MsgError s => print ("Set failed: " ^ s ^ "\n")
662 | _ => print "Unexpected server reply.\n";
663 OpenSSL.close bio
664 end
665
666fun requestSmtpLog domain =
667 let
668 val (_, bio) = requestBio (fn () => ())
669
670 val _ = Msg.send (bio, MsgSmtpLogReq domain)
671
672 fun loop () =
673 case Msg.recv bio of
674 NONE => print "Server closed connection unexpectedly.\n"
675 | SOME m =>
676 case m of
677 MsgOk => ()
678 | MsgSmtpLogRes line => (print line;
679 loop ())
680 | MsgError s => print ("Log search failed: " ^ s ^ "\n")
681 | _ => print "Unexpected server reply.\n"
682 in
683 loop ();
684 OpenSSL.close bio
685 end
686
687fun requestMysqlFixperms () =
688 let
689 val (_, bio) = requestBio (fn () => ())
690 in
691 Msg.send (bio, MsgMysqlFixperms);
692 case Msg.recv bio of
693 NONE => print "Server closed connection unexpectedly.\n"
694 | SOME m =>
695 case m of
696 MsgOk => print "Permissions granted.\n"
697 | MsgError s => print ("Failed: " ^ s ^ "\n")
698 | _ => print "Unexpected server reply.\n";
699 OpenSSL.close bio
700 end
701
702fun requestApt {node, pkg} =
703 let
704 val (user, context) = requestContext (fn () => ())
705 val bio = OpenSSL.connect (context, if node = Config.masterNode then
706 dispatcher
707 else
708 Domain.nodeIp node ^ ":" ^ Int.toString Config.slavePort)
709
710 val _ = Msg.send (bio, MsgQuery (QApt pkg))
711
712 fun loop () =
713 case Msg.recv bio of
714 NONE => (print "Server closed connection unexpectedly.\n";
715 OS.Process.failure)
716 | SOME m =>
717 case m of
718 MsgYes => (print "Package is installed.\n";
719 OS.Process.success)
720 | MsgNo => (print "Package is not installed.\n";
721 OS.Process.failure)
722 | MsgError s => (print ("APT query failed: " ^ s ^ "\n");
723 OS.Process.failure)
724 | _ => (print "Unexpected server reply.\n";
725 OS.Process.failure)
726 in
727 loop ()
728 before OpenSSL.close bio
729 end
730
731fun requestCron {node, uname} =
732 let
733 val (user, context) = requestContext (fn () => ())
734 val bio = OpenSSL.connect (context, if node = Config.masterNode then
735 dispatcher
736 else
737 Domain.nodeIp node ^ ":" ^ Int.toString Config.slavePort)
738
739 val _ = Msg.send (bio, MsgQuery (QCron uname))
740
741 fun loop () =
742 case Msg.recv bio of
743 NONE => (print "Server closed connection unexpectedly.\n";
744 OS.Process.failure)
745 | SOME m =>
746 case m of
747 MsgYes => (print "User has cron permissions.\n";
748 OS.Process.success)
749 | MsgNo => (print "User does not have cron permissions.\n";
750 OS.Process.failure)
751 | MsgError s => (print ("Cron query failed: " ^ s ^ "\n");
752 OS.Process.failure)
753 | _ => (print "Unexpected server reply.\n";
754 OS.Process.failure)
755 in
756 loop ()
757 before OpenSSL.close bio
758 end
759
760fun requestFtp {node, uname} =
761 let
762 val (user, context) = requestContext (fn () => ())
763 val bio = OpenSSL.connect (context, if node = Config.masterNode then
764 dispatcher
765 else
766 Domain.nodeIp node ^ ":" ^ Int.toString Config.slavePort)
767
768 val _ = Msg.send (bio, MsgQuery (QFtp uname))
769
770 fun loop () =
771 case Msg.recv bio of
772 NONE => (print "Server closed connection unexpectedly.\n";
773 OS.Process.failure)
774 | SOME m =>
775 case m of
776 MsgYes => (print "User has FTP permissions.\n";
777 OS.Process.success)
778 | MsgNo => (print "User does not have FTP permissions.\n";
779 OS.Process.failure)
780 | MsgError s => (print ("FTP query failed: " ^ s ^ "\n");
781 OS.Process.failure)
782 | _ => (print "Unexpected server reply.\n";
783 OS.Process.failure)
784 in
785 loop ()
786 before OpenSSL.close bio
787 end
788
789fun requestTrustedPath {node, uname} =
790 let
791 val (user, context) = requestContext (fn () => ())
792 val bio = OpenSSL.connect (context, if node = Config.masterNode then
793 dispatcher
794 else
795 Domain.nodeIp node ^ ":" ^ Int.toString Config.slavePort)
796
797 val _ = Msg.send (bio, MsgQuery (QTrustedPath uname))
798
799 fun loop () =
800 case Msg.recv bio of
801 NONE => (print "Server closed connection unexpectedly.\n";
802 OS.Process.failure)
803 | SOME m =>
804 case m of
805 MsgYes => (print "User has trusted path restriction.\n";
806 OS.Process.success)
807 | MsgNo => (print "User does not have trusted path restriction.\n";
808 OS.Process.failure)
809 | MsgError s => (print ("Trusted path query failed: " ^ s ^ "\n");
810 OS.Process.failure)
811 | _ => (print "Unexpected server reply.\n";
812 OS.Process.failure)
813 in
814 loop ()
815 before OpenSSL.close bio
816 end
817
818fun requestSocketPerm {node, uname} =
819 let
820 val (user, context) = requestContext (fn () => ())
821 val bio = OpenSSL.connect (context, if node = Config.masterNode then
822 dispatcher
823 else
824 Domain.nodeIp node ^ ":" ^ Int.toString Config.slavePort)
825
826 val _ = Msg.send (bio, MsgQuery (QSocket uname))
827
828 fun loop () =
829 case Msg.recv bio of
830 NONE => (print "Server closed connection unexpectedly.\n";
831 OS.Process.failure)
832 | SOME m =>
833 case m of
834 MsgSocket p => (case p of
835 Any => print "Any\n"
836 | Client => print "Client\n"
837 | Server => print "Server\n"
838 | Nada => print "Nada\n";
839 OS.Process.success)
840 | MsgError s => (print ("Socket permission query failed: " ^ s ^ "\n");
841 OS.Process.failure)
842 | _ => (print "Unexpected server reply.\n";
843 OS.Process.failure)
844 in
845 loop ()
846 before OpenSSL.close bio
847 end
848
849fun requestFirewall {node, uname} =
850 let
851 val (user, context) = requestContext (fn () => ())
852 val bio = OpenSSL.connect (context, if node = Config.masterNode then
853 dispatcher
854 else
855 Domain.nodeIp node ^ ":" ^ Int.toString Config.slavePort)
856
857 val _ = Msg.send (bio, MsgQuery (QFirewall uname))
858
859 fun loop () =
860 case Msg.recv bio of
861 NONE => (print "Server closed connection unexpectedly.\n";
862 OS.Process.failure)
863 | SOME m =>
864 case m of
865 MsgFirewall ls => (app (fn s => (print s; print "\n")) ls;
866 OS.Process.success)
867 | MsgError s => (print ("Firewall query failed: " ^ s ^ "\n");
868 OS.Process.failure)
869 | _ => (print "Unexpected server reply.\n";
870 OS.Process.failure)
871 in
872 loop ()
873 before OpenSSL.close bio
874 end
875
876fun requestDescribe dom =
877 let
878 val (_, bio) = requestBio (fn () => ())
879 in
880 Msg.send (bio, MsgDescribe dom);
881 case Msg.recv bio of
882 NONE => print "Server closed connection unexpectedly.\n"
883 | SOME m =>
884 case m of
885 MsgDescription s => print s
886 | MsgError s => print ("Describe failed: " ^ s ^ "\n")
887 | _ => print "Unexpected server reply.\n";
888 OpenSSL.close bio
889 end
890
891fun regenerateEither tc checker context =
892 let
893 fun ifReal f =
894 if tc then
895 ()
896 else
897 f ()
898
899 val _ = ErrorMsg.reset ()
900
901 val b = basis ()
902 val () = Tycheck.disallowExterns ()
903
904 val () = ifReal Domain.resetGlobal
905
906 val ok = ref true
907
908 fun contactNode (node, ip) =
909 if node = Config.defaultNode then
910 Domain.resetLocal ()
911 else let
912 val bio = OpenSSL.connect (context,
913 ip
914 ^ ":"
915 ^ Int.toString Config.slavePort)
916 in
917 Msg.send (bio, MsgRegenerate);
918 case Msg.recv bio of
919 NONE => print "Slave closed connection unexpectedly\n"
920 | SOME m =>
921 case m of
922 MsgOk => print ("Slave " ^ node ^ " pre-regeneration finished\n")
923 | MsgError s => print ("Slave " ^ node
924 ^ " returned error: " ^
925 s ^ "\n")
926 | _ => print ("Slave " ^ node
927 ^ " returned unexpected command\n");
928 OpenSSL.close bio
929 end
930 handle OpenSSL.OpenSSL s => print ("OpenSSL error: " ^ s ^ "\n")
931
932 fun doUser user =
933 let
934 val _ = Domain.setUser user
935 val _ = ErrorMsg.reset ()
936
937 val dname = Config.domtoolDir user
938 in
939 if Posix.FileSys.access (dname, []) then
940 let
941 val dir = Posix.FileSys.opendir dname
942
943 fun loop files =
944 case Posix.FileSys.readdir dir of
945 NONE => (Posix.FileSys.closedir dir;
946 files)
947 | SOME fname =>
948 if notTmp fname then
949 loop (OS.Path.joinDirFile {dir = dname,
950 file = fname}
951 :: files)
952 else
953 loop files
954
955 val files = loop []
956 val (_, files) = Order.order (SOME b) files
957 in
958 if !ErrorMsg.anyErrors then
959 (ErrorMsg.reset ();
960 print ("User " ^ user ^ "'s configuration has errors!\n"))
961 else
962 app checker files
963 end
964 else
965 ()
966 end
967 handle IO.Io {name, function, ...} =>
968 (print ("IO error processing user " ^ user ^ ": " ^ function ^ ": " ^ name ^ "\n");
969 ok := false)
970 | exn as OS.SysErr (s, _) => (print ("System error processing user " ^ user ^ ": " ^ s ^ "\n");
971 ok := false)
972 | ErrorMsg.Error => (ErrorMsg.reset ();
973 print ("User " ^ user ^ " had a compilation error.\n");
974 ok := false)
975 | _ => (print "Unknown exception during regeneration!\n";
976 ok := false)
977 in
978 ifReal (fn () => (app contactNode Config.nodeIps;
979 Env.pre ()));
980 app doUser (Acl.users ());
981 ifReal Env.post;
982 !ok
983 end
984
985val regenerate = regenerateEither false eval'
986val regenerateTc = regenerateEither true (ignore o check)
987
988fun rmuser user =
989 let
990 val doms = Acl.class {user = user, class = "domain"}
991 val doms = List.filter (fn dom =>
992 case Acl.whoHas {class = "domain", value = dom} of
993 [_] => true
994 | _ => false) (StringSet.listItems doms)
995 in
996 Acl.rmuser user;
997 Domain.rmdom doms
998 end
999
1000fun now () = Date.toString (Date.fromTimeUniv (Time.now ()))
1001
1002fun answerQuery q =
1003 case q of
1004 QApt pkg => if Apt.installed pkg then MsgYes else MsgNo
1005 | QCron user => if Cron.allowed user then MsgYes else MsgNo
1006 | QFtp user => if Ftp.allowed user then MsgYes else MsgNo
1007 | QTrustedPath user => if TrustedPath.query user then MsgYes else MsgNo
1008 | QSocket user => MsgSocket (SocketPerm.query user)
1009 | QFirewall user => MsgFirewall (Firewall.query user)
1010
1011fun describeQuery q =
1012 case q of
1013 QApt pkg => "Requested installation status of package " ^ pkg
1014 | QCron user => "Asked about cron permissions for user " ^ user
1015 | QFtp user => "Asked about FTP permissions for user " ^ user
1016 | QTrustedPath user => "Asked about trusted path settings for user " ^ user
1017 | QSocket user => "Asked about socket permissions for user " ^ user
1018 | QFirewall user => "Asked about firewall rules for user " ^ user
1019
1020fun service () =
1021 let
1022 val () = Acl.read Config.aclFile
1023
1024 val context = context (Config.serverCert,
1025 Config.serverKey,
1026 Config.trustStore)
1027 val _ = Domain.set_context context
1028
1029 val sock = OpenSSL.listen (context, Config.dispatcherPort)
1030
1031 fun loop () =
1032 (case OpenSSL.accept sock of
1033 NONE => ()
1034 | SOME bio =>
1035 let
1036 val user = OpenSSL.peerCN bio
1037 val () = print ("\nConnection from " ^ user ^ " at " ^ now () ^ "\n")
1038 val () = Domain.setUser user
1039
1040 fun doIt f cleanup =
1041 ((case f () of
1042 (msgLocal, SOME msgRemote) =>
1043 (print msgLocal;
1044 print "\n";
1045 Msg.send (bio, MsgError msgRemote))
1046 | (msgLocal, NONE) =>
1047 (print msgLocal;
1048 print "\n";
1049 Msg.send (bio, MsgOk)))
1050 handle e as (OpenSSL.OpenSSL s) =>
1051 (print ("OpenSSL error: " ^ s ^ "\n");
1052 app (fn x => print (x ^ "\n")) (SMLofNJ.exnHistory e);
1053 Msg.send (bio, MsgError ("OpenSSL error: " ^ s))
1054 handle OpenSSL.OpenSSL _ => ())
1055 | OS.SysErr (s, _) =>
1056 (print "System error: ";
1057 print s;
1058 print "\n";
1059 Msg.send (bio, MsgError ("System error: " ^ s))
1060 handle OpenSSL.OpenSSL _ => ())
1061 | Fail s =>
1062 (print "Failure: ";
1063 print s;
1064 print "\n";
1065 Msg.send (bio, MsgError ("Failure: " ^ s))
1066 handle OpenSSL.OpenSSL _ => ())
1067 | ErrorMsg.Error =>
1068 (print "Compilation error\n";
1069 Msg.send (bio, MsgError "Error during configuration evaluation")
1070 handle OpenSSL.OpenSSL _ => ());
1071 (cleanup ();
1072 ignore (OpenSSL.readChar bio);
1073 OpenSSL.close bio)
1074 handle OpenSSL.OpenSSL _ => ();
1075 loop ())
1076
1077 fun doConfig codes =
1078 let
1079 val _ = print "Configuration:\n"
1080 val _ = app (fn s => (print s; print "\n")) codes
1081 val _ = print "\n"
1082
1083 val outname = OS.FileSys.tmpName ()
1084
1085 fun doOne code =
1086 let
1087 val outf = TextIO.openOut outname
1088 in
1089 TextIO.output (outf, code);
1090 TextIO.closeOut outf;
1091 eval' outname
1092 end
1093 in
1094 doIt (fn () => (Env.pre ();
1095 app doOne codes;
1096 Env.post ();
1097 Msg.send (bio, MsgOk);
1098 ("Configuration complete.", NONE)))
1099 (fn () => OS.FileSys.remove outname)
1100 end
1101
1102 fun checkAddr s =
1103 case String.fields (fn ch => ch = #"@") s of
1104 [user'] =>
1105 if user = user' then
1106 SOME (SetSA.User s)
1107 else
1108 NONE
1109 | [user', domain] =>
1110 if Domain.validEmailUser user' andalso Domain.yourDomain domain then
1111 SOME (SetSA.Email s)
1112 else
1113 NONE
1114 | _ => NONE
1115
1116 fun cmdLoop () =
1117 case Msg.recv bio of
1118 NONE => (OpenSSL.close bio
1119 handle OpenSSL.OpenSSL _ => ();
1120 loop ())
1121 | SOME m =>
1122 case m of
1123 MsgConfig code => doConfig [code]
1124 | MsgMultiConfig codes => doConfig codes
1125
1126 | MsgShutdown =>
1127 if Acl.query {user = user, class = "priv", value = "all"}
1128 orelse Acl.query {user = user, class = "priv", value = "shutdown"} then
1129 print ("Domtool dispatcher shutting down at " ^ now () ^ "\n\n")
1130 else
1131 (print "Unauthorized shutdown command!\n";
1132 OpenSSL.close bio
1133 handle OpenSSL.OpenSSL _ => ();
1134 loop ())
1135
1136 | MsgGrant acl =>
1137 doIt (fn () =>
1138 if Acl.query {user = user, class = "priv", value = "all"} then
1139 (Acl.grant acl;
1140 Acl.write Config.aclFile;
1141 ("Granted permission " ^ #value acl ^ " to " ^ #user acl ^ " in " ^ #class acl ^ ".",
1142 NONE))
1143 else
1144 ("Unauthorized user asked to grant a permission!",
1145 SOME "Not authorized to grant privileges"))
1146 (fn () => ())
1147
1148 | MsgRevoke acl =>
1149 doIt (fn () =>
1150 if Acl.query {user = user, class = "priv", value = "all"} then
1151 (Acl.revoke acl;
1152 Acl.write Config.aclFile;
1153 ("Revoked permission " ^ #value acl ^ " from " ^ #user acl ^ " in " ^ #class acl ^ ".",
1154 NONE))
1155 else
1156 ("Unauthorized user asked to revoke a permission!",
1157 SOME "Not authorized to revoke privileges"))
1158 (fn () => ())
1159
1160 | MsgListPerms user =>
1161 doIt (fn () =>
1162 (Msg.send (bio, MsgPerms (Acl.queryAll user));
1163 ("Sent permission list for user " ^ user ^ ".",
1164 NONE)))
1165 (fn () => ())
1166
1167 | MsgWhoHas perm =>
1168 doIt (fn () =>
1169 (Msg.send (bio, MsgWhoHasResponse (Acl.whoHas perm));
1170 ("Sent whohas response for " ^ #class perm ^ " / " ^ #value perm ^ ".",
1171 NONE)))
1172 (fn () => ())
1173
1174 | MsgRmdom doms =>
1175 doIt (fn () =>
1176 if Acl.query {user = user, class = "priv", value = "all"}
1177 orelse List.all (fn dom => Acl.query {user = user, class = "domain", value = dom}) doms then
1178 (Domain.rmdom doms;
1179 app (fn dom =>
1180 Acl.revokeFromAll {class = "domain", value = dom}) doms;
1181 Acl.write Config.aclFile;
1182 ("Removed domains" ^ foldl (fn (d, s) => s ^ " " ^ d) "" doms ^ ".",
1183 NONE))
1184 else
1185 ("Unauthorized user asked to remove a domain!",
1186 SOME "Not authorized to remove that domain"))
1187 (fn () => ())
1188
1189 | MsgRegenerate =>
1190 doIt (fn () =>
1191 if Acl.query {user = user, class = "priv", value = "regen"}
1192 orelse Acl.query {user = user, class = "priv", value = "all"} then
1193 (if regenerate context then
1194 ("Regenerated all configuration.",
1195 NONE)
1196 else
1197 ("Error regenerating configuration!",
1198 SOME "Error regenerating configuration! Consult /var/log/domtool.log."))
1199 else
1200 ("Unauthorized user asked to regenerate!",
1201 SOME "Not authorized to regenerate"))
1202 (fn () => ())
1203
1204 | MsgRegenerateTc =>
1205 doIt (fn () =>
1206 if Acl.query {user = user, class = "priv", value = "regen"}
1207 orelse Acl.query {user = user, class = "priv", value = "all"} then
1208 (if regenerateTc context then
1209 ("Checked all configuration.",
1210 NONE)
1211 else
1212 ("Found a compilation error!",
1213 SOME "Found a compilation error! Consult /var/log/domtool.log."))
1214 else
1215 ("Unauthorized user asked to regenerate -tc!",
1216 SOME "Not authorized to regenerate -tc"))
1217 (fn () => ())
1218
1219 | MsgRmuser user' =>
1220 doIt (fn () =>
1221 if Acl.query {user = user, class = "priv", value = "all"} then
1222 (rmuser user';
1223 Acl.write Config.aclFile;
1224 ("Removed user " ^ user' ^ ".",
1225 NONE))
1226 else
1227 ("Unauthorized user asked to remove a user!",
1228 SOME "Not authorized to remove users"))
1229 (fn () => ())
1230
1231 | MsgCreateDbUser {dbtype, passwd} =>
1232 doIt (fn () =>
1233 case Dbms.lookup dbtype of
1234 NONE => ("Database user creation request with unknown datatype type " ^ dbtype,
1235 SOME ("Unknown database type " ^ dbtype))
1236 | SOME handler =>
1237 case #adduser handler {user = user, passwd = passwd} of
1238 NONE => ("Added " ^ dbtype ^ " user " ^ user ^ ".",
1239 NONE)
1240 | SOME msg =>
1241 ("Error adding a " ^ dbtype ^ " user " ^ user ^ ": " ^ msg,
1242 SOME ("Error adding user: " ^ msg)))
1243 (fn () => ())
1244
1245 | MsgDbPasswd {dbtype, passwd} =>
1246 doIt (fn () =>
1247 case Dbms.lookup dbtype of
1248 NONE => ("Database passwd request with unknown datatype type " ^ dbtype,
1249 SOME ("Unknown database type " ^ dbtype))
1250 | SOME handler =>
1251 case #passwd handler {user = user, passwd = passwd} of
1252 NONE => ("Changed " ^ dbtype ^ " password of user " ^ user ^ ".",
1253 NONE)
1254 | SOME msg =>
1255 ("Error setting " ^ dbtype ^ " password of user " ^ user ^ ": " ^ msg,
1256 SOME ("Error adding user: " ^ msg)))
1257 (fn () => ())
1258
1259 | MsgCreateDb {dbtype, dbname} =>
1260 doIt (fn () =>
1261 if Dbms.validDbname dbname then
1262 case Dbms.lookup dbtype of
1263 NONE => ("Database creation request with unknown datatype type " ^ dbtype,
1264 SOME ("Unknown database type " ^ dbtype))
1265 | SOME handler =>
1266 case #createdb handler {user = user, dbname = dbname} of
1267 NONE => ("Created database " ^ user ^ "_" ^ dbname ^ ".",
1268 NONE)
1269 | SOME msg => ("Error creating database " ^ user ^ "_" ^ dbname ^ ": " ^ msg,
1270 SOME ("Error creating database: " ^ msg))
1271 else
1272 ("Invalid database name " ^ user ^ "_" ^ dbname,
1273 SOME ("Invalid database name " ^ dbname)))
1274 (fn () => ())
1275
1276 | MsgDropDb {dbtype, dbname} =>
1277 doIt (fn () =>
1278 if Dbms.validDbname dbname then
1279 case Dbms.lookup dbtype of
1280 NONE => ("Database drop request with unknown datatype type " ^ dbtype,
1281 SOME ("Unknown database type " ^ dbtype))
1282 | SOME handler =>
1283 case #dropdb handler {user = user, dbname = dbname} of
1284 NONE => ("Drop database " ^ user ^ "_" ^ dbname ^ ".",
1285 NONE)
1286 | SOME msg => ("Error dropping database " ^ user ^ "_" ^ dbname ^ ": " ^ msg,
1287 SOME ("Error dropping database: " ^ msg))
1288 else
1289 ("Invalid database name " ^ user ^ "_" ^ dbname,
1290 SOME ("Invalid database name " ^ dbname)))
1291 (fn () => ())
1292
1293 | MsgGrantDb {dbtype, dbname} =>
1294 doIt (fn () =>
1295 if Dbms.validDbname dbname then
1296 case Dbms.lookup dbtype of
1297 NONE => ("Database drop request with unknown datatype type " ^ dbtype,
1298 SOME ("Unknown database type " ^ dbtype))
1299 | SOME handler =>
1300 case #grant handler {user = user, dbname = dbname} of
1301 NONE => ("Grant permissions to database " ^ user ^ "_" ^ dbname ^ ".",
1302 NONE)
1303 | SOME msg => ("Error granting permissions to database " ^ user ^ "_" ^ dbname ^ ": " ^ msg,
1304 SOME ("Error granting permissions to database: " ^ msg))
1305 else
1306 ("Invalid database name " ^ user ^ "_" ^ dbname,
1307 SOME ("Invalid database name " ^ dbname)))
1308 (fn () => ())
1309
1310 | MsgListMailboxes domain =>
1311 doIt (fn () =>
1312 if not (Domain.yourDomain domain) then
1313 ("User wasn't authorized to list mailboxes for " ^ domain,
1314 SOME "You're not authorized to configure that domain.")
1315 else
1316 case Vmail.list domain of
1317 Vmail.Listing users => (Msg.send (bio, MsgMailboxes users);
1318 ("Sent mailbox list for " ^ domain,
1319 NONE))
1320 | Vmail.Error msg => ("Error listing mailboxes for " ^ domain ^ ": " ^ msg,
1321 SOME msg))
1322 (fn () => ())
1323
1324 | MsgNewMailbox {domain, user = emailUser, passwd, mailbox} =>
1325 doIt (fn () =>
1326 if not (Domain.yourDomain domain) then
1327 ("User wasn't authorized to add a mailbox to " ^ domain,
1328 SOME "You're not authorized to configure that domain.")
1329 else if not (Domain.validEmailUser emailUser) then
1330 ("Invalid e-mail username " ^ emailUser,
1331 SOME "Invalid e-mail username")
1332 else if not (CharVector.all Char.isGraph passwd) then
1333 ("Invalid password",
1334 SOME "Invalid password; may only contain printable, non-space characters")
1335 else if not (Domain.yourPath mailbox) then
1336 ("User wasn't authorized to add a mailbox at " ^ mailbox,
1337 SOME ("You're not authorized to use that mailbox location. ("
1338 ^ mailbox ^ ")"))
1339 else
1340 case Vmail.add {requester = user,
1341 domain = domain, user = emailUser,
1342 passwd = passwd, mailbox = mailbox} of
1343 NONE => ("Added mailbox " ^ emailUser ^ "@" ^ domain ^ " at " ^ mailbox,
1344 NONE)
1345 | SOME msg => ("Error adding mailbox " ^ emailUser ^ "@" ^ domain ^ ": " ^ msg,
1346 SOME msg))
1347 (fn () => ())
1348
1349 | MsgPasswdMailbox {domain, user = emailUser, passwd} =>
1350 doIt (fn () =>
1351 if not (Domain.yourDomain domain) then
1352 ("User wasn't authorized to change password of a mailbox for " ^ domain,
1353 SOME "You're not authorized to configure that domain.")
1354 else if not (Domain.validEmailUser emailUser) then
1355 ("Invalid e-mail username " ^ emailUser,
1356 SOME "Invalid e-mail username")
1357 else if not (CharVector.all Char.isGraph passwd) then
1358 ("Invalid password",
1359 SOME "Invalid password; may only contain printable, non-space characters")
1360 else
1361 case Vmail.passwd {domain = domain, user = emailUser,
1362 passwd = passwd} of
1363 NONE => ("Changed password of mailbox " ^ emailUser ^ "@" ^ domain,
1364 NONE)
1365 | SOME msg => ("Error changing mailbox password for " ^ emailUser ^ "@" ^ domain ^ ": " ^ msg,
1366 SOME msg))
1367 (fn () => ())
1368
1369 | MsgRmMailbox {domain, user = emailUser} =>
1370 doIt (fn () =>
1371 if not (Domain.yourDomain domain) then
1372 ("User wasn't authorized to change password of a mailbox for " ^ domain,
1373 SOME "You're not authorized to configure that domain.")
1374 else if not (Domain.validEmailUser emailUser) then
1375 ("Invalid e-mail username " ^ emailUser,
1376 SOME "Invalid e-mail username")
1377 else
1378 case Vmail.rm {domain = domain, user = emailUser} of
1379 NONE => ("Deleted mailbox " ^ emailUser ^ "@" ^ domain,
1380 NONE)
1381 | SOME msg => ("Error deleting mailbox " ^ emailUser ^ "@" ^ domain ^ ": " ^ msg,
1382 SOME msg))
1383 (fn () => ())
1384
1385 | MsgSaQuery addr =>
1386 doIt (fn () =>
1387 case checkAddr addr of
1388 NONE => ("User tried to query SA filtering for " ^ addr,
1389 SOME "You aren't allowed to configure SA filtering for that recipient.")
1390 | SOME addr' => (Msg.send (bio, MsgSaStatus (SetSA.query addr'));
1391 ("Queried SA filtering status for " ^ addr,
1392 NONE)))
1393 (fn () => ())
1394
1395 | MsgSaSet (addr, b) =>
1396 doIt (fn () =>
1397 case checkAddr addr of
1398 NONE => ("User tried to set SA filtering for " ^ addr,
1399 SOME "You aren't allowed to configure SA filtering for that recipient.")
1400 | SOME addr' => (SetSA.set (addr', b);
1401 Msg.send (bio, MsgOk);
1402 ("Set SA filtering status for " ^ addr ^ " to "
1403 ^ (if b then "ON" else "OFF"),
1404 NONE)))
1405 (fn () => ())
1406
1407 | MsgSmtpLogReq domain =>
1408 doIt (fn () =>
1409 if not (Domain.yourDomain domain) then
1410 ("Unauthorized user tried to request SMTP logs for " ^ domain,
1411 SOME "You aren't authorized to configure that domain.")
1412 else
1413 (SmtpLog.search (fn line => Msg.send (bio, MsgSmtpLogRes line))
1414 domain;
1415 ("Requested SMTP logs for " ^ domain,
1416 NONE)))
1417 (fn () => ())
1418
1419 | MsgQuery q =>
1420 doIt (fn () => (Msg.send (bio, answerQuery q);
1421 (describeQuery q,
1422 NONE)))
1423 (fn () => ())
1424
1425 | MsgMysqlFixperms =>
1426 doIt (fn () => if OS.Process.isSuccess
1427 (OS.Process.system "/usr/bin/sudo -H /afs/hcoop.net/common/etc/scripts/mysql-grant-table-drop") then
1428 ("Requested mysql-fixperms",
1429 NONE)
1430 else
1431 ("Requested mysql-fixperms, but execution failed!",
1432 SOME "Script execution failed."))
1433 (fn () => ())
1434
1435 | MsgDescribe dom =>
1436 doIt (fn () => (if Domain.validDomain dom then
1437 (Msg.send (bio, MsgDescription (Domain.describe dom));
1438 ("Requested description of domain " ^ dom,
1439 NONE))
1440 else
1441 ("Requested description of invalid domain " ^ dom,
1442 SOME "Invalid domain name")))
1443 (fn () => ())
1444
1445 | _ =>
1446 doIt (fn () => ("Unexpected command",
1447 SOME "Unexpected command"))
1448 (fn () => ())
1449 in
1450 cmdLoop ()
1451 end
1452 handle e as (OpenSSL.OpenSSL s) =>
1453 (print ("OpenSSL error: " ^ s ^ "\n");
1454 app (fn x => print (x ^ "\n")) (SMLofNJ.exnHistory e);
1455 OpenSSL.close bio
1456 handle OpenSSL.OpenSSL _ => ();
1457 loop ())
1458 | OS.SysErr (s, _) =>
1459 (print ("System error: " ^ s ^ "\n");
1460 OpenSSL.close bio
1461 handle OpenSSL.OpenSSL _ => ();
1462 loop ())
1463 | IO.Io {name, function, cause} =>
1464 (print ("IO error: " ^ function ^ " for " ^ name ^ "\n");
1465 app (fn x => print (x ^ "\n")) (SMLofNJ.exnHistory cause);
1466 OpenSSL.close bio
1467 handle OpenSSL.OpenSSL _ => ();
1468 loop ())
1469 | OS.Path.InvalidArc =>
1470 (print "Invalid arc\n";
1471 OpenSSL.close bio
1472 handle OpenSSL.OpenSSL _ => ();
1473 loop ())
1474 | e =>
1475 (print "Unknown exception in main loop!\n";
1476 app (fn x => print (x ^ "\n")) (SMLofNJ.exnHistory e);
1477 OpenSSL.close bio
1478 handle OpenSSL.OpenSSL _ => ();
1479 loop ()))
1480 handle e as (OpenSSL.OpenSSL s) =>
1481 (print ("OpenSSL error: " ^ s ^ "\n");
1482 app (fn x => print (x ^ "\n")) (SMLofNJ.exnHistory e);
1483 loop ())
1484 | OS.SysErr (s, _) =>
1485 (print ("System error: " ^ s ^ "\n");
1486 loop ())
1487 | IO.Io {name, function, cause} =>
1488 (print ("IO error: " ^ function ^ " for " ^ name ^ "\n");
1489 app (fn x => print (x ^ "\n")) (SMLofNJ.exnHistory cause);
1490 loop ())
1491 | e =>
1492 (print "Unknown exception in main loop!\n";
1493 app (fn x => print (x ^ "\n")) (SMLofNJ.exnHistory e);
1494 loop ())
1495 in
1496 print ("Domtool dispatcher starting up at " ^ now () ^ "\n");
1497 print "Listening for connections....\n";
1498 loop ();
1499 OpenSSL.shutdown sock
1500 end
1501
1502fun slave () =
1503 let
1504 val host = Slave.hostname ()
1505
1506 val context = context (Config.certDir ^ "/" ^ host ^ ".pem",
1507 Config.keyDir ^ "/" ^ host ^ "/key.pem",
1508 Config.trustStore)
1509
1510 val sock = OpenSSL.listen (context, Config.slavePort)
1511
1512 val _ = print ("Slave server starting at " ^ now () ^ "\n")
1513
1514 fun loop () =
1515 case OpenSSL.accept sock of
1516 NONE => ()
1517 | SOME bio =>
1518 let
1519 val peer = OpenSSL.peerCN bio
1520 val () = print ("\nConnection from " ^ peer ^ " at " ^ now () ^ "\n")
1521 in
1522 if peer = Config.dispatcherName then let
1523 fun loop' files =
1524 case Msg.recv bio of
1525 NONE => print "Dispatcher closed connection unexpectedly\n"
1526 | SOME m =>
1527 case m of
1528 MsgFile file => loop' (file :: files)
1529 | MsgDoFiles => (Slave.handleChanges files;
1530 Msg.send (bio, MsgOk))
1531 | MsgRegenerate => (Domain.resetLocal ();
1532 Msg.send (bio, MsgOk))
1533 | _ => (print "Dispatcher sent unexpected command\n";
1534 Msg.send (bio, MsgError "Unexpected command"))
1535 in
1536 loop' [];
1537 ignore (OpenSSL.readChar bio);
1538 OpenSSL.close bio;
1539 loop ()
1540 end
1541 else if peer = "domtool" then
1542 case Msg.recv bio of
1543 SOME MsgShutdown => (OpenSSL.close bio;
1544 print ("Shutting down at " ^ now () ^ "\n\n"))
1545 | _ => (OpenSSL.close bio;
1546 loop ())
1547 else
1548 case Msg.recv bio of
1549 SOME (MsgQuery q) => (print (describeQuery q ^ "\n");
1550 Msg.send (bio, answerQuery q);
1551 ignore (OpenSSL.readChar bio);
1552 OpenSSL.close bio;
1553 loop ())
1554 | _ => (OpenSSL.close bio;
1555 loop ())
1556 end handle OpenSSL.OpenSSL s =>
1557 (print ("OpenSSL error: "^ s ^ "\n");
1558 OpenSSL.close bio
1559 handle OpenSSL.OpenSSL _ => ();
1560 loop ())
1561 | e as OS.SysErr (s, _) =>
1562 (app (fn s => print (s ^ "\n")) (SMLofNJ.exnHistory e);
1563 print ("System error: "^ s ^ "\n");
1564 OpenSSL.close bio
1565 handle OpenSSL.OpenSSL _ => ();
1566 loop ())
1567 in
1568 loop ();
1569 OpenSSL.shutdown sock
1570 end
1571
1572fun listBasis () =
1573 let
1574 val dir = Posix.FileSys.opendir Config.libRoot
1575
1576 fun loop files =
1577 case Posix.FileSys.readdir dir of
1578 NONE => (Posix.FileSys.closedir dir;
1579 files)
1580 | SOME fname =>
1581 if String.isSuffix ".dtl" fname then
1582 loop (OS.Path.joinDirFile {dir = Config.libRoot,
1583 file = fname}
1584 :: files)
1585 else
1586 loop files
1587 in
1588 loop []
1589 end
1590
1591fun autodocBasis outdir =
1592 Autodoc.autodoc {outdir = outdir, infiles = listBasis ()}
1593
1594end