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