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