Specifying encoding on database creation
[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, encoding} => (OpenSSL.writeInt (bio, 17);
177 OpenSSL.writeString (bio, dbtype);
178 OpenSSL.writeString (bio, dbname);
179 sendOption OpenSSL.writeString (bio, encoding))
180 | MsgNewMailbox {domain, user, passwd, mailbox} =>
181 (OpenSSL.writeInt (bio, 18);
182 OpenSSL.writeString (bio, domain);
183 OpenSSL.writeString (bio, user);
184 OpenSSL.writeString (bio, passwd);
185 OpenSSL.writeString (bio, mailbox))
186 | MsgPasswdMailbox {domain, user, passwd} =>
187 (OpenSSL.writeInt (bio, 19);
188 OpenSSL.writeString (bio, domain);
189 OpenSSL.writeString (bio, user);
190 OpenSSL.writeString (bio, passwd))
191 | MsgRmMailbox {domain, user} =>
192 (OpenSSL.writeInt (bio, 20);
193 OpenSSL.writeString (bio, domain);
194 OpenSSL.writeString (bio, user))
195 | MsgListMailboxes domain =>
196 (OpenSSL.writeInt (bio, 21);
197 OpenSSL.writeString (bio, domain))
198 | MsgMailboxes users =>
199 (OpenSSL.writeInt (bio, 22);
200 sendList (fn (bio, {user, mailbox}) =>
201 (OpenSSL.writeString (bio, user);
202 OpenSSL.writeString (bio, mailbox)))
203 (bio, users))
204 | MsgSaQuery addr => (OpenSSL.writeInt (bio, 23);
205 OpenSSL.writeString (bio, addr))
206 | MsgSaStatus b => (OpenSSL.writeInt (bio, 24);
207 sendBool (bio, b))
208 | MsgSaSet (addr, b) => (OpenSSL.writeInt (bio, 25);
209 OpenSSL.writeString (bio, addr);
210 sendBool (bio, b))
211 | MsgSmtpLogReq domain => (OpenSSL.writeInt (bio, 26);
212 OpenSSL.writeString (bio, domain))
213 | MsgSmtpLogRes domain => (OpenSSL.writeInt (bio, 27);
214 OpenSSL.writeString (bio, domain))
215 | MsgDbPasswd {dbtype, passwd} => (OpenSSL.writeInt (bio, 28);
216 OpenSSL.writeString (bio, dbtype);
217 OpenSSL.writeString (bio, passwd))
218 | MsgShutdown => OpenSSL.writeInt (bio, 29)
219 | MsgYes => OpenSSL.writeInt (bio, 30)
220 | MsgNo => OpenSSL.writeInt (bio, 31)
221 | MsgQuery q => (OpenSSL.writeInt (bio, 32);
222 sendQuery (bio, q))
223 | MsgSocket p => (OpenSSL.writeInt (bio, 33);
224 sendSockPerm (bio, p))
225 | MsgFirewall ls => (OpenSSL.writeInt (bio, 34);
226 sendList OpenSSL.writeString (bio, ls))
227 | MsgRegenerateTc => OpenSSL.writeInt (bio, 35)
228 | MsgDropDb {dbtype, dbname} => (OpenSSL.writeInt (bio, 36);
229 OpenSSL.writeString (bio, dbtype);
230 OpenSSL.writeString (bio, dbname))
231 | MsgGrantDb {dbtype, dbname} => (OpenSSL.writeInt (bio, 37);
232 OpenSSL.writeString (bio, dbtype);
233 OpenSSL.writeString (bio, dbname))
234 | MsgMysqlFixperms => OpenSSL.writeInt (bio, 38)
235 | MsgDescribe dom => (OpenSSL.writeInt (bio, 39);
236 OpenSSL.writeString (bio, dom))
237 | MsgDescription s => (OpenSSL.writeInt (bio, 40);
238 OpenSSL.writeString (bio, s))
239
240 fun checkIt v =
241 case v of
242 NONE => raise OpenSSL.OpenSSL "Bad Msg format"
243 | _ => v
244
245 fun recv bio =
246 case OpenSSL.readInt bio of
247 NONE => NONE
248 | SOME n =>
249 checkIt (case n of
250 1 => SOME MsgOk
251 | 2 => Option.map MsgError (OpenSSL.readString bio)
252 | 3 => Option.map MsgConfig (OpenSSL.readString bio)
253 | 4 => (case (OpenSSL.readInt bio,
254 OpenSSL.readString bio,
255 OpenSSL.readString bio,
256 OpenSSL.readString bio) of
257 (SOME action, SOME domain, SOME dir, SOME file) =>
258 SOME (MsgFile {action = i2a action,
259 domain = domain,
260 dir = dir,
261 file = file})
262 | _ => NONE)
263 | 5 => SOME MsgDoFiles
264 | 6 => (case recvAcl bio of
265 SOME acl => SOME (MsgGrant acl)
266 | _ => NONE)
267 | 7 => (case recvAcl bio of
268 SOME acl => SOME (MsgRevoke acl)
269 | _ => NONE)
270 | 8 => (case OpenSSL.readString bio of
271 SOME user => SOME (MsgListPerms user)
272 | _ => NONE)
273 | 9 => Option.map MsgPerms
274 (recvList (fn bio =>
275 case (OpenSSL.readString bio,
276 recvList OpenSSL.readString bio) of
277 (SOME class, SOME values) => SOME (class, values)
278 | _ => NONE) bio)
279 | 10 => (case (OpenSSL.readString bio, OpenSSL.readString bio) of
280 (SOME class, SOME value) => SOME (MsgWhoHas {class = class, value = value})
281 | _ => NONE)
282 | 11 => Option.map MsgWhoHasResponse
283 (recvList OpenSSL.readString bio)
284 | 12 => Option.map MsgMultiConfig
285 (recvList OpenSSL.readString bio)
286 | 13 => Option.map MsgRmdom (recvList OpenSSL.readString bio)
287 | 14 => SOME MsgRegenerate
288 | 15 => Option.map MsgRmuser (OpenSSL.readString bio)
289 | 16 => (case (OpenSSL.readString bio, recvOption OpenSSL.readString bio) of
290 (SOME dbtype, SOME passwd) =>
291 SOME (MsgCreateDbUser {dbtype = dbtype, passwd = passwd})
292 | _ => NONE)
293 | 17 => (case (OpenSSL.readString bio, OpenSSL.readString bio, recvOption OpenSSL.readString bio) of
294 (SOME dbtype, SOME dbname, SOME encoding) =>
295 SOME (MsgCreateDb {dbtype = dbtype, dbname = dbname, encoding = encoding})
296 | _ => NONE)
297 | 18 => (case (OpenSSL.readString bio, OpenSSL.readString bio,
298 OpenSSL.readString bio, OpenSSL.readString bio) of
299 (SOME domain, SOME user, SOME passwd, SOME mailbox) =>
300 SOME (MsgNewMailbox {domain = domain, user = user,
301 passwd = passwd, mailbox = mailbox})
302 | _ => NONE)
303 | 19 => (case (OpenSSL.readString bio, OpenSSL.readString bio,
304 OpenSSL.readString bio) of
305 (SOME domain, SOME user, SOME passwd) =>
306 SOME (MsgPasswdMailbox {domain = domain, user = user,
307 passwd = passwd})
308 | _ => NONE)
309 | 20 => (case (OpenSSL.readString bio, OpenSSL.readString bio) of
310 (SOME domain, SOME user) =>
311 SOME (MsgRmMailbox {domain = domain, user = user})
312 | _ => NONE)
313 | 21 => Option.map MsgListMailboxes (OpenSSL.readString bio)
314 | 22 => Option.map MsgMailboxes (recvList
315 (fn bio =>
316 case (OpenSSL.readString bio,
317 OpenSSL.readString bio) of
318 (SOME user, SOME mailbox) =>
319 SOME {user = user, mailbox = mailbox}
320 | _ => NONE)
321 bio)
322 | 23 => Option.map MsgSaQuery (OpenSSL.readString bio)
323 | 24 => Option.map MsgSaStatus (recvBool bio)
324 | 25 => (case (OpenSSL.readString bio, recvBool bio) of
325 (SOME user, SOME b) => SOME (MsgSaSet (user, b))
326 | _ => NONE)
327 | 26 => Option.map MsgSmtpLogReq (OpenSSL.readString bio)
328 | 27 => Option.map MsgSmtpLogRes (OpenSSL.readString bio)
329 | 28 => (case (OpenSSL.readString bio, OpenSSL.readString bio) of
330 (SOME dbtype, SOME passwd) =>
331 SOME (MsgDbPasswd {dbtype = dbtype, passwd = passwd})
332 | _ => NONE)
333 | 29 => SOME MsgShutdown
334 | 30 => SOME MsgYes
335 | 31 => SOME MsgNo
336 | 32 => Option.map MsgQuery (recvQuery bio)
337 | 33 => Option.map MsgSocket (recvSockPerm bio)
338 | 34 => Option.map MsgFirewall (recvList OpenSSL.readString bio)
339 | 35 => SOME MsgRegenerateTc
340 | 36 => (case (OpenSSL.readString bio, OpenSSL.readString bio) of
341 (SOME dbtype, SOME dbname) =>
342 SOME (MsgDropDb {dbtype = dbtype, dbname = dbname})
343 | _ => NONE)
344 | 37 => (case (OpenSSL.readString bio, OpenSSL.readString bio) of
345 (SOME dbtype, SOME dbname) =>
346 SOME (MsgGrantDb {dbtype = dbtype, dbname = dbname})
347 | _ => NONE)
348 | 38 => SOME MsgMysqlFixperms
349 | 39 => Option.map MsgDescribe (OpenSSL.readString bio)
350 | 40 => Option.map MsgDescription (OpenSSL.readString bio)
351 | _ => NONE)
352
353 end