Trusted-path permission checking
[hcoop/domtool2.git] / src / main.sml
... / ...
CommitLineData
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
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
95val notTmp = CharVector.all (fn ch => Char.isAlphaNum ch orelse ch = #"." orelse ch = #"_" orelse ch = #"-")
96
97fun 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
128fun 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
149fun 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
158fun 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
167val dispatcher =
168 Config.dispatcher ^ ":" ^ Int.toString Config.dispatcherPort
169
170val self =
171 "localhost:" ^ Int.toString Config.slavePort
172
173fun requestContext f =
174 let
175 val uid = Posix.ProcEnv.getuid ()
176 val user = Posix.SysDB.Passwd.name (Posix.SysDB.getpwuid uid)
177
178 val () = Acl.read Config.aclFile
179 val () = Domain.setUser user
180
181 val () = f ()
182
183 val context = OpenSSL.context (Config.certDir ^ "/" ^ user ^ ".pem",
184 Config.keyDir ^ "/" ^ user ^ "/key.pem",
185 Config.trustStore)
186 in
187 (user, context)
188 end
189
190fun requestBio f =
191 let
192 val (user, context) = requestContext f
193 in
194 (user, OpenSSL.connect (context, dispatcher))
195 end
196
197fun requestSlaveBio () =
198 let
199 val (user, context) = requestContext (fn () => ())
200 in
201 (user, OpenSSL.connect (context, self))
202 end
203
204fun request fname =
205 let
206 val (user, bio) = requestBio (fn () => ignore (check fname))
207
208 val inf = TextIO.openIn fname
209
210 fun loop lines =
211 case TextIO.inputLine inf of
212 NONE => String.concat (List.rev lines)
213 | SOME line => loop (line :: lines)
214
215 val code = loop []
216 in
217 TextIO.closeIn inf;
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";
226 OpenSSL.close bio
227 end
228 handle ErrorMsg.Error => ()
229
230fun requestDir dname =
231 let
232 val _ = ErrorMsg.reset ()
233
234 val (user, bio) = requestBio (fn () => checkDir dname)
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
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)
285 end
286 handle ErrorMsg.Error => ()
287
288fun 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
297fun 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
312fun 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
321fun 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
336fun 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
351fun 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
366fun 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
384fun 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
402fun 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
417fun 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
432fun 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
447fun 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
462fun 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
477fun 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
492fun requestListMailboxes domain =
493 let
494 val (_, bio) = requestBio (fn () => ())
495 in
496 Msg.send (bio, MsgListMailboxes domain);
497 (case Msg.recv bio of
498 NONE => Vmail.Error "Server closed connection unexpectedly."
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)
504 | _ => Vmail.Error "Unexpected server reply.")
505 before OpenSSL.close bio
506 end
507
508fun 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
523fun 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
538fun 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
553fun 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
570fun 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
586fun 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
607fun requestApt {node, pkg} =
608 let
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)
614
615 val _ = Msg.send (bio, MsgQuery (QApt pkg))
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
636fun 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
665fun 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
694fun 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
723fun regenerate context =
724 let
725 val b = basis ()
726 val () = Tycheck.disallowExterns ()
727
728 val () = Domain.resetGlobal ()
729
730 fun contactNode (node, ip) =
731 if node = Config.defaultNode then
732 Domain.resetLocal ()
733 else let
734 val bio = OpenSSL.connect (context,
735 ip
736 ^ ":"
737 ^ Int.toString Config.slavePort)
738 in
739 Msg.send (bio, MsgRegenerate);
740 case Msg.recv bio of
741 NONE => print "Slave closed connection unexpectedly\n"
742 | SOME m =>
743 case m of
744 MsgOk => print ("Slave " ^ node ^ " pre-regeneration finished\n")
745 | MsgError s => print ("Slave " ^ node
746 ^ " returned error: " ^
747 s ^ "\n")
748 | _ => print ("Slave " ^ node
749 ^ " returned unexpected command\n");
750 OpenSSL.close bio
751 end
752
753 fun doUser user =
754 let
755 val _ = Domain.setUser user
756 val _ = ErrorMsg.reset ()
757
758 val dname = Config.domtoolDir user
759
760 val dir = Posix.FileSys.opendir dname
761
762 fun loop files =
763 case Posix.FileSys.readdir dir of
764 NONE => (Posix.FileSys.closedir dir;
765 files)
766 | SOME fname =>
767 if notTmp fname then
768 loop (OS.Path.joinDirFile {dir = dname,
769 file = fname}
770 :: files)
771 else
772 loop files
773
774 val files = loop []
775 val (_, files) = Order.order (SOME b) files
776 in
777 if !ErrorMsg.anyErrors then
778 print ("User " ^ user ^ "'s configuration has errors!\n")
779 else
780 app eval' files
781 end
782 handle IO.Io _ => ()
783 | OS.SysErr (s, _) => print ("System error processing user " ^ user ^ ": " ^ s ^ "\n")
784 | ErrorMsg.Error => print ("User " ^ user ^ " had a compilation error.\n")
785 in
786 app contactNode Config.nodeIps;
787 Env.pre ();
788 app doUser (Acl.users ());
789 Env.post ()
790 end
791
792fun rmuser user =
793 let
794 val doms = Acl.class {user = user, class = "domain"}
795 val doms = List.filter (fn dom =>
796 case Acl.whoHas {class = "domain", value = dom} of
797 [_] => true
798 | _ => false) (StringSet.listItems doms)
799 in
800 Acl.rmuser user;
801 Domain.rmdom doms
802 end
803
804fun now () = Date.toString (Date.fromTimeUniv (Time.now ()))
805
806fun answerQuery q =
807 case q of
808 QApt pkg => if Apt.installed pkg then MsgYes else MsgNo
809 | QCron user => if Cron.allowed user then MsgYes else MsgNo
810 | QFtp user => if Ftp.allowed user then MsgYes else MsgNo
811 | QTrustedPath user => if TrustedPath.query user then MsgYes else MsgNo
812
813fun describeQuery q =
814 case q of
815 QApt pkg => "Requested installation status of package " ^ pkg
816 | QCron user => "Asked about cron permissions for user " ^ user
817 | QFtp user => "Asked about FTP permissions for user " ^ user
818 | QTrustedPath user => "Asked about trusted path settings for user " ^ user
819
820fun service () =
821 let
822 val () = Acl.read Config.aclFile
823
824 val context = OpenSSL.context (Config.serverCert,
825 Config.serverKey,
826 Config.trustStore)
827 val _ = Domain.set_context context
828
829 val sock = OpenSSL.listen (context, Config.dispatcherPort)
830
831 fun loop () =
832 case OpenSSL.accept sock of
833 NONE => ()
834 | SOME bio =>
835 let
836 val user = OpenSSL.peerCN bio
837 val () = print ("\nConnection from " ^ user ^ " at " ^ now () ^ "\n")
838 val () = Domain.setUser user
839
840 fun doIt f cleanup =
841 ((case f () of
842 (msgLocal, SOME msgRemote) =>
843 (print msgLocal;
844 print "\n";
845 Msg.send (bio, MsgError msgRemote))
846 | (msgLocal, NONE) =>
847 (print msgLocal;
848 print "\n";
849 Msg.send (bio, MsgOk)))
850 handle OpenSSL.OpenSSL _ =>
851 print "OpenSSL error\n"
852 | OS.SysErr (s, _) =>
853 (print "System error: ";
854 print s;
855 print "\n";
856 Msg.send (bio, MsgError ("System error: " ^ s))
857 handle OpenSSL.OpenSSL _ => ())
858 | Fail s =>
859 (print "Failure: ";
860 print s;
861 print "\n";
862 Msg.send (bio, MsgError ("Failure: " ^ s))
863 handle OpenSSL.OpenSSL _ => ())
864 | ErrorMsg.Error =>
865 (print "Compilation error\n";
866 Msg.send (bio, MsgError "Error during configuration evaluation")
867 handle OpenSSL.OpenSSL _ => ());
868 (cleanup ();
869 ignore (OpenSSL.readChar bio);
870 OpenSSL.close bio)
871 handle OpenSSL.OpenSSL _ => ();
872 loop ())
873
874 fun doConfig codes =
875 let
876 val _ = print "Configuration:\n"
877 val _ = app (fn s => (print s; print "\n")) codes
878 val _ = print "\n"
879
880 val outname = OS.FileSys.tmpName ()
881
882 fun doOne code =
883 let
884 val outf = TextIO.openOut outname
885 in
886 TextIO.output (outf, code);
887 TextIO.closeOut outf;
888 eval' outname
889 end
890 in
891 doIt (fn () => (Env.pre ();
892 app doOne codes;
893 Env.post ();
894 Msg.send (bio, MsgOk);
895 ("Configuration complete.", NONE)))
896 (fn () => OS.FileSys.remove outname)
897 end
898
899 fun checkAddr s =
900 case String.fields (fn ch => ch = #"@") s of
901 [user'] =>
902 if user = user' then
903 SOME (SetSA.User s)
904 else
905 NONE
906 | [user', domain] =>
907 if Domain.validEmailUser user' andalso Domain.yourDomain domain then
908 SOME (SetSA.Email s)
909 else
910 NONE
911 | _ => NONE
912
913 fun cmdLoop () =
914 case Msg.recv bio of
915 NONE => (OpenSSL.close bio
916 handle OpenSSL.OpenSSL _ => ();
917 loop ())
918 | SOME m =>
919 case m of
920 MsgConfig code => doConfig [code]
921 | MsgMultiConfig codes => doConfig codes
922
923 | MsgShutdown =>
924 if Acl.query {user = user, class = "priv", value = "all"}
925 orelse Acl.query {user = user, class = "priv", value = "shutdown"} then
926 print ("Domtool dispatcher shutting down at " ^ now () ^ "\n\n")
927 else
928 (print "Unauthorized shutdown command!\n";
929 OpenSSL.close bio
930 handle OpenSSL.OpenSSL _ => ();
931 loop ())
932
933 | MsgGrant acl =>
934 doIt (fn () =>
935 if Acl.query {user = user, class = "priv", value = "all"} then
936 (Acl.grant acl;
937 Acl.write Config.aclFile;
938 ("Granted permission " ^ #value acl ^ " to " ^ #user acl ^ " in " ^ #class acl ^ ".",
939 NONE))
940 else
941 ("Unauthorized user asked to grant a permission!",
942 SOME "Not authorized to grant privileges"))
943 (fn () => ())
944
945 | MsgRevoke acl =>
946 doIt (fn () =>
947 if Acl.query {user = user, class = "priv", value = "all"} then
948 (Acl.revoke acl;
949 Acl.write Config.aclFile;
950 ("Revoked permission " ^ #value acl ^ " from " ^ #user acl ^ " in " ^ #class acl ^ ".",
951 NONE))
952 else
953 ("Unauthorized user asked to revoke a permission!",
954 SOME "Not authorized to revoke privileges"))
955 (fn () => ())
956
957 | MsgListPerms user =>
958 doIt (fn () =>
959 (Msg.send (bio, MsgPerms (Acl.queryAll user));
960 ("Sent permission list for user " ^ user ^ ".",
961 NONE)))
962 (fn () => ())
963
964 | MsgWhoHas perm =>
965 doIt (fn () =>
966 (Msg.send (bio, MsgWhoHasResponse (Acl.whoHas perm));
967 ("Sent whohas response for " ^ #class perm ^ " / " ^ #value perm ^ ".",
968 NONE)))
969 (fn () => ())
970
971 | MsgRmdom doms =>
972 doIt (fn () =>
973 if Acl.query {user = user, class = "priv", value = "all"}
974 orelse List.all (fn dom => Acl.query {user = user, class = "domain", value = dom}) doms then
975 (Domain.rmdom doms;
976 app (fn dom =>
977 Acl.revokeFromAll {class = "domain", value = dom}) doms;
978 Acl.write Config.aclFile;
979 ("Removed domains" ^ foldl (fn (d, s) => s ^ " " ^ d) "" doms ^ ".",
980 NONE))
981 else
982 ("Unauthorized user asked to remove a domain!",
983 SOME "Not authorized to remove that domain"))
984 (fn () => ())
985
986 | MsgRegenerate =>
987 doIt (fn () =>
988 if Acl.query {user = user, class = "priv", value = "regen"}
989 orelse Acl.query {user = user, class = "priv", value = "all"} then
990 (regenerate context;
991 ("Regenerated all configuration.",
992 NONE))
993 else
994 ("Unauthorized user asked to regenerate!",
995 SOME "Not authorized to regenerate"))
996 (fn () => ())
997
998 | MsgRmuser user' =>
999 doIt (fn () =>
1000 if Acl.query {user = user, class = "priv", value = "all"} then
1001 (rmuser user';
1002 Acl.write Config.aclFile;
1003 ("Removed user " ^ user' ^ ".",
1004 NONE))
1005 else
1006 ("Unauthorized user asked to remove a user!",
1007 SOME "Not authorized to remove users"))
1008 (fn () => ())
1009
1010 | MsgCreateDbUser {dbtype, passwd} =>
1011 doIt (fn () =>
1012 case Dbms.lookup dbtype of
1013 NONE => ("Database user creation request with unknown datatype type " ^ dbtype,
1014 SOME ("Unknown database type " ^ dbtype))
1015 | SOME handler =>
1016 case #adduser handler {user = user, passwd = passwd} of
1017 NONE => ("Added " ^ dbtype ^ " user " ^ user ^ ".",
1018 NONE)
1019 | SOME msg =>
1020 ("Error adding a " ^ dbtype ^ " user " ^ user ^ ": " ^ msg,
1021 SOME ("Error adding user: " ^ msg)))
1022 (fn () => ())
1023
1024 | MsgDbPasswd {dbtype, passwd} =>
1025 doIt (fn () =>
1026 case Dbms.lookup dbtype of
1027 NONE => ("Database passwd request with unknown datatype type " ^ dbtype,
1028 SOME ("Unknown database type " ^ dbtype))
1029 | SOME handler =>
1030 case #passwd handler {user = user, passwd = passwd} of
1031 NONE => ("Changed " ^ dbtype ^ " password of user " ^ user ^ ".",
1032 NONE)
1033 | SOME msg =>
1034 ("Error setting " ^ dbtype ^ " password of user " ^ user ^ ": " ^ msg,
1035 SOME ("Error adding user: " ^ msg)))
1036 (fn () => ())
1037
1038 | MsgCreateDbTable {dbtype, dbname} =>
1039 doIt (fn () =>
1040 if Dbms.validDbname dbname then
1041 case Dbms.lookup dbtype of
1042 NONE => ("Database creation request with unknown datatype type " ^ dbtype,
1043 SOME ("Unknown database type " ^ dbtype))
1044 | SOME handler =>
1045 case #createdb handler {user = user, dbname = dbname} of
1046 NONE => ("Created database " ^ user ^ "_" ^ dbname ^ ".",
1047 NONE)
1048 | SOME msg => ("Error creating database " ^ user ^ "_" ^ dbname ^ ": " ^ msg,
1049 SOME ("Error creating database: " ^ msg))
1050 else
1051 ("Invalid database name " ^ user ^ "_" ^ dbname,
1052 SOME ("Invalid database name " ^ dbname)))
1053 (fn () => ())
1054
1055 | MsgListMailboxes domain =>
1056 doIt (fn () =>
1057 if not (Domain.yourDomain domain) then
1058 ("User wasn't authorized to list mailboxes for " ^ domain,
1059 SOME "You're not authorized to configure that domain.")
1060 else
1061 case Vmail.list domain of
1062 Vmail.Listing users => (Msg.send (bio, MsgMailboxes users);
1063 ("Sent mailbox list for " ^ domain,
1064 NONE))
1065 | Vmail.Error msg => ("Error listing mailboxes for " ^ domain ^ ": " ^ msg,
1066 SOME msg))
1067 (fn () => ())
1068
1069 | MsgNewMailbox {domain, user = emailUser, passwd, mailbox} =>
1070 doIt (fn () =>
1071 if not (Domain.yourDomain domain) then
1072 ("User wasn't authorized to add a mailbox to " ^ domain,
1073 SOME "You're not authorized to configure that domain.")
1074 else if not (Domain.validEmailUser emailUser) then
1075 ("Invalid e-mail username " ^ emailUser,
1076 SOME "Invalid e-mail username")
1077 else if not (CharVector.all Char.isGraph passwd) then
1078 ("Invalid password",
1079 SOME "Invalid password; may only contain printable, non-space characters")
1080 else if not (Domain.yourPath mailbox) then
1081 ("User wasn't authorized to add a mailbox at " ^ mailbox,
1082 SOME "You're not authorized to use that mailbox location.")
1083 else
1084 case Vmail.add {requester = user,
1085 domain = domain, user = emailUser,
1086 passwd = passwd, mailbox = mailbox} of
1087 NONE => ("Added mailbox " ^ emailUser ^ "@" ^ domain ^ " at " ^ mailbox,
1088 NONE)
1089 | SOME msg => ("Error adding mailbox " ^ emailUser ^ "@" ^ domain ^ ": " ^ msg,
1090 SOME msg))
1091 (fn () => ())
1092
1093 | MsgPasswdMailbox {domain, user = emailUser, passwd} =>
1094 doIt (fn () =>
1095 if not (Domain.yourDomain domain) then
1096 ("User wasn't authorized to change password of a mailbox for " ^ domain,
1097 SOME "You're not authorized to configure that domain.")
1098 else if not (Domain.validEmailUser emailUser) then
1099 ("Invalid e-mail username " ^ emailUser,
1100 SOME "Invalid e-mail username")
1101 else if not (CharVector.all Char.isGraph passwd) then
1102 ("Invalid password",
1103 SOME "Invalid password; may only contain printable, non-space characters")
1104 else
1105 case Vmail.passwd {domain = domain, user = emailUser,
1106 passwd = passwd} of
1107 NONE => ("Changed password of mailbox " ^ emailUser ^ "@" ^ domain,
1108 NONE)
1109 | SOME msg => ("Error changing mailbox password for " ^ emailUser ^ "@" ^ domain ^ ": " ^ msg,
1110 SOME msg))
1111 (fn () => ())
1112
1113 | MsgRmMailbox {domain, user = emailUser} =>
1114 doIt (fn () =>
1115 if not (Domain.yourDomain domain) then
1116 ("User wasn't authorized to change password of a mailbox for " ^ domain,
1117 SOME "You're not authorized to configure that domain.")
1118 else if not (Domain.validEmailUser emailUser) then
1119 ("Invalid e-mail username " ^ emailUser,
1120 SOME "Invalid e-mail username")
1121 else
1122 case Vmail.rm {domain = domain, user = emailUser} of
1123 NONE => ("Deleted mailbox " ^ emailUser ^ "@" ^ domain,
1124 NONE)
1125 | SOME msg => ("Error deleting mailbox " ^ emailUser ^ "@" ^ domain ^ ": " ^ msg,
1126 SOME msg))
1127 (fn () => ())
1128
1129 | MsgSaQuery addr =>
1130 doIt (fn () =>
1131 case checkAddr addr of
1132 NONE => ("User tried to query SA filtering for " ^ addr,
1133 SOME "You aren't allowed to configure SA filtering for that recipient.")
1134 | SOME addr' => (Msg.send (bio, MsgSaStatus (SetSA.query addr'));
1135 ("Queried SA filtering status for " ^ addr,
1136 NONE)))
1137 (fn () => ())
1138
1139 | MsgSaSet (addr, b) =>
1140 doIt (fn () =>
1141 case checkAddr addr of
1142 NONE => ("User tried to set SA filtering for " ^ addr,
1143 SOME "You aren't allowed to configure SA filtering for that recipient.")
1144 | SOME addr' => (SetSA.set (addr', b);
1145 Msg.send (bio, MsgOk);
1146 ("Set SA filtering status for " ^ addr ^ " to "
1147 ^ (if b then "ON" else "OFF"),
1148 NONE)))
1149 (fn () => ())
1150
1151 | MsgSmtpLogReq domain =>
1152 doIt (fn () =>
1153 if not (Domain.yourDomain domain) then
1154 ("Unauthorized user tried to request SMTP logs for " ^ domain,
1155 SOME "You aren't authorized to configure that domain.")
1156 else
1157 (SmtpLog.search (fn line => Msg.send (bio, MsgSmtpLogRes line))
1158 domain;
1159 ("Requested SMTP logs for " ^ domain,
1160 NONE)))
1161 (fn () => ())
1162
1163 | MsgQuery q =>
1164 doIt (fn () => (Msg.send (bio, answerQuery q);
1165 (describeQuery q,
1166 NONE)))
1167 (fn () => ())
1168
1169 | _ =>
1170 doIt (fn () => ("Unexpected command",
1171 SOME "Unexpected command"))
1172 (fn () => ())
1173 in
1174 cmdLoop ()
1175 end
1176 handle OpenSSL.OpenSSL s =>
1177 (print ("OpenSSL error: " ^ s ^ "\n");
1178 OpenSSL.close bio
1179 handle OpenSSL.OpenSSL _ => ();
1180 loop ())
1181 | OS.SysErr (s, _) =>
1182 (print ("System error: " ^ s ^ "\n");
1183 OpenSSL.close bio
1184 handle OpenSSL.OpenSSL _ => ();
1185 loop ())
1186 in
1187 print ("Domtool dispatcher starting up at " ^ now () ^ "\n");
1188 print "Listening for connections....\n";
1189 loop ();
1190 OpenSSL.shutdown sock
1191 end
1192
1193fun slave () =
1194 let
1195 val host = Slave.hostname ()
1196
1197 val context = OpenSSL.context (Config.certDir ^ "/" ^ host ^ ".pem",
1198 Config.keyDir ^ "/" ^ host ^ "/key.pem",
1199 Config.trustStore)
1200
1201 val sock = OpenSSL.listen (context, Config.slavePort)
1202
1203 val _ = print ("Slave server starting at " ^ now () ^ "\n")
1204
1205 fun loop () =
1206 case OpenSSL.accept sock of
1207 NONE => ()
1208 | SOME bio =>
1209 let
1210 val peer = OpenSSL.peerCN bio
1211 val () = print ("\nConnection from " ^ peer ^ " at " ^ now () ^ "\n")
1212 in
1213 if peer = Config.dispatcherName then let
1214 fun loop' files =
1215 case Msg.recv bio of
1216 NONE => print "Dispatcher closed connection unexpectedly\n"
1217 | SOME m =>
1218 case m of
1219 MsgFile file => loop' (file :: files)
1220 | MsgDoFiles => (Slave.handleChanges files;
1221 Msg.send (bio, MsgOk))
1222 | MsgRegenerate => (Domain.resetLocal ();
1223 Msg.send (bio, MsgOk))
1224 | _ => (print "Dispatcher sent unexpected command\n";
1225 Msg.send (bio, MsgError "Unexpected command"))
1226 in
1227 loop' [];
1228 ignore (OpenSSL.readChar bio);
1229 OpenSSL.close bio;
1230 loop ()
1231 end
1232 else if peer = "domtool" then
1233 case Msg.recv bio of
1234 SOME MsgShutdown => (OpenSSL.close bio;
1235 print ("Shutting down at " ^ now () ^ "\n\n"))
1236 | _ => (OpenSSL.close bio;
1237 loop ())
1238 else
1239 case Msg.recv bio of
1240 SOME (MsgQuery q) => (print (describeQuery q ^ "\n");
1241 Msg.send (bio, answerQuery q);
1242 ignore (OpenSSL.readChar bio);
1243 OpenSSL.close bio;
1244 loop ())
1245 | _ => (OpenSSL.close bio;
1246 loop ())
1247 end handle OpenSSL.OpenSSL s =>
1248 (print ("OpenSSL error: "^ s ^ "\n");
1249 OpenSSL.close bio
1250 handle OpenSSL.OpenSSL _ => ();
1251 loop ())
1252 | OS.SysErr (s, _) =>
1253 (print ("System error: "^ s ^ "\n");
1254 OpenSSL.close bio
1255 handle OpenSSL.OpenSSL _ => ();
1256 loop ())
1257 in
1258 loop ();
1259 OpenSSL.shutdown sock
1260 end
1261
1262fun listBasis () =
1263 let
1264 val dir = Posix.FileSys.opendir Config.libRoot
1265
1266 fun loop files =
1267 case Posix.FileSys.readdir dir of
1268 NONE => (Posix.FileSys.closedir dir;
1269 files)
1270 | SOME fname =>
1271 if String.isSuffix ".dtl" fname then
1272 loop (OS.Path.joinDirFile {dir = Config.libRoot,
1273 file = fname}
1274 :: files)
1275 else
1276 loop files
1277 in
1278 loop []
1279 end
1280
1281fun autodocBasis outdir =
1282 Autodoc.autodoc {outdir = outdir, infiles = listBasis ()}
1283
1284end