Cron and FTP queries
[hcoop/domtool2.git] / src / msg.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 (* Network messages *)
20
21 structure Msg :> MSG = struct
22
23 open OpenSSL MsgTypes Slave
24
25 val a2i = fn Add => 0
26 | Delete => 1
27 | Modify => 2
28
29 val i2a = fn 0 => Add
30 | 1 => Delete
31 | 2 => Modify
32 | _ => raise OpenSSL.OpenSSL "Bad action number to deserialize"
33
34 fun sendAcl (bio, {user, class, value}) =
35 (OpenSSL.writeString (bio, user);
36 OpenSSL.writeString (bio, class);
37 OpenSSL.writeString (bio, value))
38
39 fun recvAcl bio =
40 case (OpenSSL.readString bio, OpenSSL.readString bio, OpenSSL.readString bio) of
41 (SOME user, SOME class, SOME value) => SOME {user = user, class = class, value = value}
42 | _ => NONE
43
44 fun sendList f (bio, ls) =
45 (app (fn x =>
46 (OpenSSL.writeInt (bio, 1);
47 f (bio, x))) ls;
48 OpenSSL.writeInt (bio, 0))
49
50 fun recvList f bio =
51 let
52 fun loop ls =
53 case OpenSSL.readInt bio of
54 SOME 0 => SOME (rev ls)
55 | SOME 1 =>
56 (case f bio of
57 SOME x => loop (x :: ls)
58 | NONE => NONE)
59 | _ => NONE
60 in
61 loop []
62 end
63
64 fun sendOption f (bio, opt) =
65 case opt of
66 NONE => OpenSSL.writeInt (bio, 0)
67 | SOME x => (OpenSSL.writeInt (bio, 1);
68 f (bio, x))
69
70 fun recvOption f bio =
71 case OpenSSL.readInt bio of
72 SOME 0 => SOME NONE
73 | SOME 1 =>
74 (case f bio of
75 SOME x => SOME (SOME x)
76 | NONE => NONE)
77 | _ => NONE
78
79 fun sendBool (bio, b) =
80 if b then
81 OpenSSL.writeInt (bio, 1)
82 else
83 OpenSSL.writeInt (bio, 0)
84
85 fun recvBool bio =
86 case OpenSSL.readInt bio of
87 SOME 0 => SOME false
88 | SOME 1 => SOME true
89 | _ => NONE
90
91 fun sendQuery (bio, q) =
92 case q of
93 QApt s => (OpenSSL.writeInt (bio, 0);
94 OpenSSL.writeString (bio, s))
95 | QCron s => (OpenSSL.writeInt (bio, 1);
96 OpenSSL.writeString (bio, s))
97 | QFtp s => (OpenSSL.writeInt (bio, 2);
98 OpenSSL.writeString (bio, s))
99
100 fun recvQuery bio =
101 case OpenSSL.readInt bio of
102 SOME n =>
103 (case n of
104 0 => Option.map QApt (OpenSSL.readString bio)
105 | 1 => Option.map QCron (OpenSSL.readString bio)
106 | 2 => Option.map QFtp (OpenSSL.readString bio)
107 | _ => NONE)
108 | NONE => NONE
109
110 fun send (bio, m) =
111 case m of
112 MsgOk => OpenSSL.writeInt (bio, 1)
113 | MsgError s => (OpenSSL.writeInt (bio, 2);
114 OpenSSL.writeString (bio, s))
115 | MsgConfig s => (OpenSSL.writeInt (bio, 3);
116 OpenSSL.writeString (bio, s))
117 | MsgFile {action, domain, dir, file} =>
118 (OpenSSL.writeInt (bio, 4);
119 OpenSSL.writeInt (bio, a2i action);
120 OpenSSL.writeString (bio, domain);
121 OpenSSL.writeString (bio, dir);
122 OpenSSL.writeString (bio, file))
123 | MsgDoFiles => OpenSSL.writeInt (bio, 5)
124 | MsgGrant acl => (OpenSSL.writeInt (bio, 6);
125 sendAcl (bio, acl))
126 | MsgRevoke acl => (OpenSSL.writeInt (bio, 7);
127 sendAcl (bio, acl))
128 | MsgListPerms user => (OpenSSL.writeInt (bio, 8);
129 OpenSSL.writeString (bio, user))
130 | MsgPerms classes => (OpenSSL.writeInt (bio, 9);
131 sendList (fn (bio, (class, values)) =>
132 (OpenSSL.writeString (bio, class);
133 sendList OpenSSL.writeString (bio, values)))
134 (bio, classes))
135 | MsgWhoHas {class, value} => (OpenSSL.writeInt (bio, 10);
136 OpenSSL.writeString (bio, class);
137 OpenSSL.writeString (bio, value))
138 | MsgWhoHasResponse users => (OpenSSL.writeInt (bio, 11);
139 sendList OpenSSL.writeString (bio, users))
140 | MsgMultiConfig codes => (OpenSSL.writeInt (bio, 12);
141 sendList OpenSSL.writeString (bio, codes))
142 | MsgRmdom doms => (OpenSSL.writeInt (bio, 13);
143 sendList OpenSSL.writeString (bio, doms))
144 | MsgRegenerate => OpenSSL.writeInt (bio, 14)
145 | MsgRmuser dom => (OpenSSL.writeInt (bio, 15);
146 OpenSSL.writeString (bio, dom))
147 | MsgCreateDbUser {dbtype, passwd} => (OpenSSL.writeInt (bio, 16);
148 OpenSSL.writeString (bio, dbtype);
149 sendOption OpenSSL.writeString (bio, passwd))
150 | MsgCreateDbTable {dbtype, dbname} => (OpenSSL.writeInt (bio, 17);
151 OpenSSL.writeString (bio, dbtype);
152 OpenSSL.writeString (bio, dbname))
153 | MsgNewMailbox {domain, user, passwd, mailbox} =>
154 (OpenSSL.writeInt (bio, 18);
155 OpenSSL.writeString (bio, domain);
156 OpenSSL.writeString (bio, user);
157 OpenSSL.writeString (bio, passwd);
158 OpenSSL.writeString (bio, mailbox))
159 | MsgPasswdMailbox {domain, user, passwd} =>
160 (OpenSSL.writeInt (bio, 19);
161 OpenSSL.writeString (bio, domain);
162 OpenSSL.writeString (bio, user);
163 OpenSSL.writeString (bio, passwd))
164 | MsgRmMailbox {domain, user} =>
165 (OpenSSL.writeInt (bio, 20);
166 OpenSSL.writeString (bio, domain);
167 OpenSSL.writeString (bio, user))
168 | MsgListMailboxes domain =>
169 (OpenSSL.writeInt (bio, 21);
170 OpenSSL.writeString (bio, domain))
171 | MsgMailboxes users =>
172 (OpenSSL.writeInt (bio, 22);
173 sendList (fn (bio, {user, mailbox}) =>
174 (OpenSSL.writeString (bio, user);
175 OpenSSL.writeString (bio, mailbox)))
176 (bio, users))
177 | MsgSaQuery addr => (OpenSSL.writeInt (bio, 23);
178 OpenSSL.writeString (bio, addr))
179 | MsgSaStatus b => (OpenSSL.writeInt (bio, 24);
180 sendBool (bio, b))
181 | MsgSaSet (addr, b) => (OpenSSL.writeInt (bio, 25);
182 OpenSSL.writeString (bio, addr);
183 sendBool (bio, b))
184 | MsgSmtpLogReq domain => (OpenSSL.writeInt (bio, 26);
185 OpenSSL.writeString (bio, domain))
186 | MsgSmtpLogRes domain => (OpenSSL.writeInt (bio, 27);
187 OpenSSL.writeString (bio, domain))
188 | MsgDbPasswd {dbtype, passwd} => (OpenSSL.writeInt (bio, 28);
189 OpenSSL.writeString (bio, dbtype);
190 OpenSSL.writeString (bio, passwd))
191 | MsgShutdown => OpenSSL.writeInt (bio, 29)
192 | MsgYes => OpenSSL.writeInt (bio, 30)
193 | MsgNo => OpenSSL.writeInt (bio, 31)
194 | MsgQuery q => (OpenSSL.writeInt (bio, 32);
195 sendQuery (bio, q))
196
197 fun checkIt v =
198 case v of
199 NONE => raise OpenSSL.OpenSSL "Bad Msg format"
200 | _ => v
201
202 fun recv bio =
203 case OpenSSL.readInt bio of
204 NONE => NONE
205 | SOME n =>
206 checkIt (case n of
207 1 => SOME MsgOk
208 | 2 => Option.map MsgError (OpenSSL.readString bio)
209 | 3 => Option.map MsgConfig (OpenSSL.readString bio)
210 | 4 => (case (OpenSSL.readInt bio,
211 OpenSSL.readString bio,
212 OpenSSL.readString bio,
213 OpenSSL.readString bio) of
214 (SOME action, SOME domain, SOME dir, SOME file) =>
215 SOME (MsgFile {action = i2a action,
216 domain = domain,
217 dir = dir,
218 file = file})
219 | _ => NONE)
220 | 5 => SOME MsgDoFiles
221 | 6 => (case recvAcl bio of
222 SOME acl => SOME (MsgGrant acl)
223 | _ => NONE)
224 | 7 => (case recvAcl bio of
225 SOME acl => SOME (MsgRevoke acl)
226 | _ => NONE)
227 | 8 => (case OpenSSL.readString bio of
228 SOME user => SOME (MsgListPerms user)
229 | _ => NONE)
230 | 9 => Option.map MsgPerms
231 (recvList (fn bio =>
232 case (OpenSSL.readString bio,
233 recvList OpenSSL.readString bio) of
234 (SOME class, SOME values) => SOME (class, values)
235 | _ => NONE) bio)
236 | 10 => (case (OpenSSL.readString bio, OpenSSL.readString bio) of
237 (SOME class, SOME value) => SOME (MsgWhoHas {class = class, value = value})
238 | _ => NONE)
239 | 11 => Option.map MsgWhoHasResponse
240 (recvList OpenSSL.readString bio)
241 | 12 => Option.map MsgMultiConfig
242 (recvList OpenSSL.readString bio)
243 | 13 => Option.map MsgRmdom (recvList OpenSSL.readString bio)
244 | 14 => SOME MsgRegenerate
245 | 15 => Option.map MsgRmuser (OpenSSL.readString bio)
246 | 16 => (case (OpenSSL.readString bio, recvOption OpenSSL.readString bio) of
247 (SOME dbtype, SOME passwd) =>
248 SOME (MsgCreateDbUser {dbtype = dbtype, passwd = passwd})
249 | _ => NONE)
250 | 17 => (case (OpenSSL.readString bio, OpenSSL.readString bio) of
251 (SOME dbtype, SOME dbname) =>
252 SOME (MsgCreateDbTable {dbtype = dbtype, dbname = dbname})
253 | _ => NONE)
254 | 18 => (case (OpenSSL.readString bio, OpenSSL.readString bio,
255 OpenSSL.readString bio, OpenSSL.readString bio) of
256 (SOME domain, SOME user, SOME passwd, SOME mailbox) =>
257 SOME (MsgNewMailbox {domain = domain, user = user,
258 passwd = passwd, mailbox = mailbox})
259 | _ => NONE)
260 | 19 => (case (OpenSSL.readString bio, OpenSSL.readString bio,
261 OpenSSL.readString bio) of
262 (SOME domain, SOME user, SOME passwd) =>
263 SOME (MsgPasswdMailbox {domain = domain, user = user,
264 passwd = passwd})
265 | _ => NONE)
266 | 20 => (case (OpenSSL.readString bio, OpenSSL.readString bio) of
267 (SOME domain, SOME user) =>
268 SOME (MsgRmMailbox {domain = domain, user = user})
269 | _ => NONE)
270 | 21 => Option.map MsgListMailboxes (OpenSSL.readString bio)
271 | 22 => Option.map MsgMailboxes (recvList
272 (fn bio =>
273 case (OpenSSL.readString bio,
274 OpenSSL.readString bio) of
275 (SOME user, SOME mailbox) =>
276 SOME {user = user, mailbox = mailbox}
277 | _ => NONE)
278 bio)
279 | 23 => Option.map MsgSaQuery (OpenSSL.readString bio)
280 | 24 => Option.map MsgSaStatus (recvBool bio)
281 | 25 => (case (OpenSSL.readString bio, recvBool bio) of
282 (SOME user, SOME b) => SOME (MsgSaSet (user, b))
283 | _ => NONE)
284 | 26 => Option.map MsgSmtpLogReq (OpenSSL.readString bio)
285 | 27 => Option.map MsgSmtpLogRes (OpenSSL.readString bio)
286 | 28 => (case (OpenSSL.readString bio, OpenSSL.readString bio) of
287 (SOME dbtype, SOME passwd) =>
288 SOME (MsgDbPasswd {dbtype = dbtype, passwd = passwd})
289 | _ => NONE)
290 | 29 => SOME MsgShutdown
291 | 30 => SOME MsgYes
292 | 31 => SOME MsgNo
293 | 32 => Option.map MsgQuery (recvQuery bio)
294 | _ => NONE)
295
296 end