Generation of slash-tilde waklog directives for each user
[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 | MsgReUsers => OpenSSL.writeInt (bio, 41)
240
241 fun checkIt v =
242 case v of
243 NONE => raise OpenSSL.OpenSSL "Bad Msg format"
244 | _ => v
245
246 fun recv bio =
247 case OpenSSL.readInt bio of
248 NONE => NONE
249 | SOME n =>
250 checkIt (case n of
251 1 => SOME MsgOk
252 | 2 => Option.map MsgError (OpenSSL.readString bio)
253 | 3 => Option.map MsgConfig (OpenSSL.readString bio)
254 | 4 => (case (OpenSSL.readInt bio,
255 OpenSSL.readString bio,
256 OpenSSL.readString bio,
257 OpenSSL.readString bio) of
258 (SOME action, SOME domain, SOME dir, SOME file) =>
259 SOME (MsgFile {action = i2a action,
260 domain = domain,
261 dir = dir,
262 file = file})
263 | _ => NONE)
264 | 5 => SOME MsgDoFiles
265 | 6 => (case recvAcl bio of
266 SOME acl => SOME (MsgGrant acl)
267 | _ => NONE)
268 | 7 => (case recvAcl bio of
269 SOME acl => SOME (MsgRevoke acl)
270 | _ => NONE)
271 | 8 => (case OpenSSL.readString bio of
272 SOME user => SOME (MsgListPerms user)
273 | _ => NONE)
274 | 9 => Option.map MsgPerms
275 (recvList (fn bio =>
276 case (OpenSSL.readString bio,
277 recvList OpenSSL.readString bio) of
278 (SOME class, SOME values) => SOME (class, values)
279 | _ => NONE) bio)
280 | 10 => (case (OpenSSL.readString bio, OpenSSL.readString bio) of
281 (SOME class, SOME value) => SOME (MsgWhoHas {class = class, value = value})
282 | _ => NONE)
283 | 11 => Option.map MsgWhoHasResponse
284 (recvList OpenSSL.readString bio)
285 | 12 => Option.map MsgMultiConfig
286 (recvList OpenSSL.readString bio)
287 | 13 => Option.map MsgRmdom (recvList OpenSSL.readString bio)
288 | 14 => SOME MsgRegenerate
289 | 15 => Option.map MsgRmuser (OpenSSL.readString bio)
290 | 16 => (case (OpenSSL.readString bio, recvOption OpenSSL.readString bio) of
291 (SOME dbtype, SOME passwd) =>
292 SOME (MsgCreateDbUser {dbtype = dbtype, passwd = passwd})
293 | _ => NONE)
294 | 17 => (case (OpenSSL.readString bio, OpenSSL.readString bio, recvOption OpenSSL.readString bio) of
295 (SOME dbtype, SOME dbname, SOME encoding) =>
296 SOME (MsgCreateDb {dbtype = dbtype, dbname = dbname, encoding = encoding})
297 | _ => NONE)
298 | 18 => (case (OpenSSL.readString bio, OpenSSL.readString bio,
299 OpenSSL.readString bio, OpenSSL.readString bio) of
300 (SOME domain, SOME user, SOME passwd, SOME mailbox) =>
301 SOME (MsgNewMailbox {domain = domain, user = user,
302 passwd = passwd, mailbox = mailbox})
303 | _ => NONE)
304 | 19 => (case (OpenSSL.readString bio, OpenSSL.readString bio,
305 OpenSSL.readString bio) of
306 (SOME domain, SOME user, SOME passwd) =>
307 SOME (MsgPasswdMailbox {domain = domain, user = user,
308 passwd = passwd})
309 | _ => NONE)
310 | 20 => (case (OpenSSL.readString bio, OpenSSL.readString bio) of
311 (SOME domain, SOME user) =>
312 SOME (MsgRmMailbox {domain = domain, user = user})
313 | _ => NONE)
314 | 21 => Option.map MsgListMailboxes (OpenSSL.readString bio)
315 | 22 => Option.map MsgMailboxes (recvList
316 (fn bio =>
317 case (OpenSSL.readString bio,
318 OpenSSL.readString bio) of
319 (SOME user, SOME mailbox) =>
320 SOME {user = user, mailbox = mailbox}
321 | _ => NONE)
322 bio)
323 | 23 => Option.map MsgSaQuery (OpenSSL.readString bio)
324 | 24 => Option.map MsgSaStatus (recvBool bio)
325 | 25 => (case (OpenSSL.readString bio, recvBool bio) of
326 (SOME user, SOME b) => SOME (MsgSaSet (user, b))
327 | _ => NONE)
328 | 26 => Option.map MsgSmtpLogReq (OpenSSL.readString bio)
329 | 27 => Option.map MsgSmtpLogRes (OpenSSL.readString bio)
330 | 28 => (case (OpenSSL.readString bio, OpenSSL.readString bio) of
331 (SOME dbtype, SOME passwd) =>
332 SOME (MsgDbPasswd {dbtype = dbtype, passwd = passwd})
333 | _ => NONE)
334 | 29 => SOME MsgShutdown
335 | 30 => SOME MsgYes
336 | 31 => SOME MsgNo
337 | 32 => Option.map MsgQuery (recvQuery bio)
338 | 33 => Option.map MsgSocket (recvSockPerm bio)
339 | 34 => Option.map MsgFirewall (recvList OpenSSL.readString bio)
340 | 35 => SOME MsgRegenerateTc
341 | 36 => (case (OpenSSL.readString bio, OpenSSL.readString bio) of
342 (SOME dbtype, SOME dbname) =>
343 SOME (MsgDropDb {dbtype = dbtype, dbname = dbname})
344 | _ => NONE)
345 | 37 => (case (OpenSSL.readString bio, OpenSSL.readString bio) of
346 (SOME dbtype, SOME dbname) =>
347 SOME (MsgGrantDb {dbtype = dbtype, dbname = dbname})
348 | _ => NONE)
349 | 38 => SOME MsgMysqlFixperms
350 | 39 => Option.map MsgDescribe (OpenSSL.readString bio)
351 | 40 => Option.map MsgDescription (OpenSSL.readString bio)
352 | 41 => SOME MsgReUsers
353 | _ => NONE)
354
355 end