Initial revision
[hcoop/zz_old/domtool.git] / src / vmail / vmail.sml
1 (*
2 Domtool (http://hcoop.sf.net/)
3 Copyright (C) 2004 Adam Chlipala
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 *)
19
20 (* Administration of Courier IMAP virtual mailboxes *)
21
22 structure Vmail :> VMAIL =
23 struct
24 open Config VmailConfig Util
25
26 fun main args =
27 case args of
28 domain::rest =>
29 if validDomain domain then
30 let
31 val tooldir = dataDir ^ toDir domain
32 val maildir = mailboxDir ^ "/" ^ domain
33 in
34 if not (Posix.FileSys.access (tooldir, [])) then
35 (print ("Domain not in domtool (would be " ^ tooldir ^ ")\n");
36 OS.Process.failure)
37 else if not (Posix.FileSys.access (tooldir, [Posix.FileSys.A_WRITE])) then
38 (print "Access denied\n";
39 OS.Process.failure)
40 else
41 case rest of
42 ["list"] =>
43 (let
44 val dir = Posix.FileSys.opendir maildir
45
46 fun loop () =
47 case Posix.FileSys.readdir dir of
48 NONE => ()
49 | SOME user => (print (user ^ "\n");
50 loop ())
51 in
52 loop ();
53 Posix.FileSys.closedir dir;
54 OS.Process.success
55 end handle Io => OS.Process.success)
56 | ["add", user] =>
57 if validUser user then
58 let
59 val _ = if Posix.FileSys.access (maildir, []) then
60 ()
61 else
62 ignore (OS.Process.system ("mkdir " ^ maildir))
63
64 val umaildir = maildir ^ "/" ^ user
65 in
66 if Posix.FileSys.access (umaildir, []) then
67 (print "Mailbox already exists\n";
68 OS.Process.failure)
69 else if OS.Process.system (courierDir ^ "/bin/maildirmake " ^ umaildir) = OS.Process.success
70 andalso OS.Process.system (sudo ^ " " ^ courierDir ^ "/sbin/userdb \"" ^ domain ^ "/" ^ user ^ "@" ^ domain ^ "\" set home=/home/vmail mail=/home/vmail/" ^ domain ^ "/" ^ user ^ " uid=1042 gid=1043") = OS.Process.success
71 andalso OS.Process.system (sudo ^ " " ^ courierDir ^ "/sbin/userdbpw | " ^ sudo ^ " " ^ courierDir ^ "/sbin/userdb \"" ^ domain ^ "/" ^ user ^ "@" ^ domain ^ "\" set systempw") = OS.Process.success then
72 (print "Mailbox created\n";
73 OS.Process.success)
74 else
75 (print "Error creating mailbox\n";
76 OS.Process.failure)
77 end
78 else
79 (print "Invalid mailbox name\n";
80 OS.Process.failure)
81 | ["passwd", user] =>
82 if not (validUser user) then
83 (print "Invalid mailbox name\n";
84 OS.Process.failure)
85 else if not (Posix.FileSys.access (maildir ^ "/" ^ user, [])) then
86 (print "Mailbox does not exist\n";
87 OS.Process.failure)
88 else if OS.Process.system (sudo ^ " " ^ courierDir ^ "/sbin/userdbpw | " ^ sudo ^ " " ^ courierDir ^ "/sbin/userdb \"" ^ domain ^ "/" ^ user ^ "@" ^ domain ^ "\" set systempw") = OS.Process.success then
89 (print "Password set\n";
90 OS.Process.success)
91 else
92 (print "Unable to set password\n";
93 OS.Process.failure)
94 | _ => (print "Unknown command\n";
95 OS.Process.failure)
96 end
97 else
98 (print "Invalid domain\n";
99 OS.Process.failure)
100 | _ => (print "Invalid command\n";
101 OS.Process.failure)
102 end