19a84b4f94c767c06e1e45e82ba0ddb2e2b8d04e
[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 sendSockPerm (bio, p) =
92 case p of
93 Any => OpenSSL.writeInt (bio, 0)
94 | Client => OpenSSL.writeInt (bio, 1)
95 | Server => OpenSSL.writeInt (bio, 2)
96 | Nada => OpenSSL.writeInt (bio, 3)
97
98 fun recvSockPerm bio =
99 case OpenSSL.readInt bio of
100 SOME 0 => SOME Any
101 | SOME 1 => SOME Client
102 | SOME 2 => SOME Server
103 | SOME 3 => SOME Nada
104 | _ => NONE
105
106 fun sendQuery (bio, q) =
107 case q of
108 QApt s => (OpenSSL.writeInt (bio, 0);
109 OpenSSL.writeString (bio, s))
110 | QCron s => (OpenSSL.writeInt (bio, 1);
111 OpenSSL.writeString (bio, s))
112 | QFtp s => (OpenSSL.writeInt (bio, 2);
113 OpenSSL.writeString (bio, s))
114 | QTrustedPath s => (OpenSSL.writeInt (bio, 3);
115 OpenSSL.writeString (bio, s))
116 | QSocket s => (OpenSSL.writeInt (bio, 4);
117 OpenSSL.writeString (bio, s))
118 | QFirewall s => (OpenSSL.writeInt (bio, 5);
119 OpenSSL.writeString (bio, s))
120
121 fun recvQuery bio =
122 case OpenSSL.readInt bio of
123 SOME n =>
124 (case n of
125 0 => Option.map QApt (OpenSSL.readString bio)
126 | 1 => Option.map QCron (OpenSSL.readString bio)
127 | 2 => Option.map QFtp (OpenSSL.readString bio)
128 | 3 => Option.map QTrustedPath (OpenSSL.readString bio)
129 | 4 => Option.map QSocket (OpenSSL.readString bio)
130 | 5 => Option.map QFirewall (OpenSSL.readString bio)
131 | _ => NONE)
132 | NONE => NONE
133
134 fun send (bio, m) =
135 case m of
136 MsgOk => OpenSSL.writeInt (bio, 1)
137 | MsgError s => (OpenSSL.writeInt (bio, 2);
138 OpenSSL.writeString (bio, s))
139 | MsgConfig s => (OpenSSL.writeInt (bio, 3);
140 OpenSSL.writeString (bio, s))
141 | MsgFile {action, domain, dir, file} =>
142 (OpenSSL.writeInt (bio, 4);
143 OpenSSL.writeInt (bio, a2i action);
144 OpenSSL.writeString (bio, domain);
145 OpenSSL.writeString (bio, dir);
146 OpenSSL.writeString (bio, file))
147 | MsgDoFiles => OpenSSL.writeInt (bio, 5)
148 | MsgGrant acl => (OpenSSL.writeInt (bio, 6);
149 sendAcl (bio, acl))
150 | MsgRevoke acl => (OpenSSL.writeInt (bio, 7);
151 sendAcl (bio, acl))
152 | MsgListPerms user => (OpenSSL.writeInt (bio, 8);
153 OpenSSL.writeString (bio, user))
154 | MsgPerms classes => (OpenSSL.writeInt (bio, 9);
155 sendList (fn (bio, (class, values)) =>
156 (OpenSSL.writeString (bio, class);
157 sendList OpenSSL.writeString (bio, values)))
158 (bio, classes))
159 | MsgWhoHas {class, value} => (OpenSSL.writeInt (bio, 10);
160 OpenSSL.writeString (bio, class);
161 OpenSSL.writeString (bio, value))
162 | MsgWhoHasResponse users => (OpenSSL.writeInt (bio, 11);
163 sendList OpenSSL.writeString (bio, users))
164 | MsgMultiConfig codes => (OpenSSL.writeInt (bio, 12);
165 sendList OpenSSL.writeString (bio, codes))
166 | MsgRmdom doms => (OpenSSL.writeInt (bio, 13);
167 sendList OpenSSL.writeString (bio, doms))
168 | MsgRegenerate => OpenSSL.writeInt (bio, 14)
169 | MsgRmuser dom => (OpenSSL.writeInt (bio, 15);
170 OpenSSL.writeString (bio, dom))
171 | MsgCreateDbUser {dbtype, passwd} => (OpenSSL.writeInt (bio, 16);
172 OpenSSL.writeString (bio, dbtype);
173 sendOption OpenSSL.writeString (bio, passwd))
174 | MsgCreateDbTable {dbtype, dbname} => (OpenSSL.writeInt (bio, 17);
175 OpenSSL.writeString (bio, dbtype);
176 OpenSSL.writeString (bio, dbname))
177 | MsgNewMailbox {domain, user, passwd, mailbox} =>
178 (OpenSSL.writeInt (bio, 18);
179 OpenSSL.writeString (bio, domain);
180 OpenSSL.writeString (bio, user);
181 OpenSSL.writeString (bio, passwd);
182 OpenSSL.writeString (bio, mailbox))
183 | MsgPasswdMailbox {domain, user, passwd} =>
184 (OpenSSL.writeInt (bio, 19);
185 OpenSSL.writeString (bio, domain);
186 OpenSSL.writeString (bio, user);
187 OpenSSL.writeString (bio, passwd))
188 | MsgRmMailbox {domain, user} =>
189 (OpenSSL.writeInt (bio, 20);
190 OpenSSL.writeString (bio, domain);
191 OpenSSL.writeString (bio, user))
192 | MsgListMailboxes domain =>
193 (OpenSSL.writeInt (bio, 21);
194 OpenSSL.writeString (bio, domain))
195 | MsgMailboxes users =>
196 (OpenSSL.writeInt (bio, 22);
197 sendList (fn (bio, {user, mailbox}) =>
198 (OpenSSL.writeString (bio, user);
199 OpenSSL.writeString (bio, mailbox)))
200 (bio, users))
201 | MsgSaQuery addr => (OpenSSL.writeInt (bio, 23);
202 OpenSSL.writeString (bio, addr))
203 | MsgSaStatus b => (OpenSSL.writeInt (bio, 24);
204 sendBool (bio, b))
205 | MsgSaSet (addr, b) => (OpenSSL.writeInt (bio, 25);
206 OpenSSL.writeString (bio, addr);
207 sendBool (bio, b))
208 | MsgSmtpLogReq domain => (OpenSSL.writeInt (bio, 26);
209 OpenSSL.writeString (bio, domain))
210 | MsgSmtpLogRes domain => (OpenSSL.writeInt (bio, 27);
211 OpenSSL.writeString (bio, domain))
212 | MsgDbPasswd {dbtype, passwd} => (OpenSSL.writeInt (bio, 28);
213 OpenSSL.writeString (bio, dbtype);
214 OpenSSL.writeString (bio, passwd))
215 | MsgShutdown => OpenSSL.writeInt (bio, 29)
216 | MsgYes => OpenSSL.writeInt (bio, 30)
217 | MsgNo => OpenSSL.writeInt (bio, 31)
218 | MsgQuery q => (OpenSSL.writeInt (bio, 32);
219 sendQuery (bio, q))
220 | MsgSocket p => (OpenSSL.writeInt (bio, 33);
221 sendSockPerm (bio, p))
222 | MsgFirewall ls => (OpenSSL.writeInt (bio, 34);
223 sendList OpenSSL.writeString (bio, ls))
224 | MsgRegenerateTc => OpenSSL.writeInt (bio, 35)
225
226 fun checkIt v =
227 case v of
228 NONE => raise OpenSSL.OpenSSL "Bad Msg format"
229 | _ => v
230
231 fun recv bio =
232 case OpenSSL.readInt bio of
233 NONE => NONE
234 | SOME n =>
235 checkIt (case n of
236 1 => SOME MsgOk
237 | 2 => Option.map MsgError (OpenSSL.readString bio)
238 | 3 => Option.map MsgConfig (OpenSSL.readString bio)
239 | 4 => (case (OpenSSL.readInt bio,
240 OpenSSL.readString bio,
241 OpenSSL.readString bio,
242 OpenSSL.readString bio) of
243 (SOME action, SOME domain, SOME dir, SOME file) =>
244 SOME (MsgFile {action = i2a action,
245 domain = domain,
246 dir = dir,
247 file = file})
248 | _ => NONE)
249 | 5 => SOME MsgDoFiles
250 | 6 => (case recvAcl bio of
251 SOME acl => SOME (MsgGrant acl)
252 | _ => NONE)
253 | 7 => (case recvAcl bio of
254 SOME acl => SOME (MsgRevoke acl)
255 | _ => NONE)
256 | 8 => (case OpenSSL.readString bio of
257 SOME user => SOME (MsgListPerms user)
258 | _ => NONE)
259 | 9 => Option.map MsgPerms
260 (recvList (fn bio =>
261 case (OpenSSL.readString bio,
262 recvList OpenSSL.readString bio) of
263 (SOME class, SOME values) => SOME (class, values)
264 | _ => NONE) bio)
265 | 10 => (case (OpenSSL.readString bio, OpenSSL.readString bio) of
266 (SOME class, SOME value) => SOME (MsgWhoHas {class = class, value = value})
267 | _ => NONE)
268 | 11 => Option.map MsgWhoHasResponse
269 (recvList OpenSSL.readString bio)
270 | 12 => Option.map MsgMultiConfig
271 (recvList OpenSSL.readString bio)
272 | 13 => Option.map MsgRmdom (recvList OpenSSL.readString bio)
273 | 14 => SOME MsgRegenerate
274 | 15 => Option.map MsgRmuser (OpenSSL.readString bio)
275 | 16 => (case (OpenSSL.readString bio, recvOption OpenSSL.readString bio) of
276 (SOME dbtype, SOME passwd) =>
277 SOME (MsgCreateDbUser {dbtype = dbtype, passwd = passwd})
278 | _ => NONE)
279 | 17 => (case (OpenSSL.readString bio, OpenSSL.readString bio) of
280 (SOME dbtype, SOME dbname) =>
281 SOME (MsgCreateDbTable {dbtype = dbtype, dbname = dbname})
282 | _ => NONE)
283 | 18 => (case (OpenSSL.readString bio, OpenSSL.readString bio,
284 OpenSSL.readString bio, OpenSSL.readString bio) of
285 (SOME domain, SOME user, SOME passwd, SOME mailbox) =>
286 SOME (MsgNewMailbox {domain = domain, user = user,
287 passwd = passwd, mailbox = mailbox})
288 | _ => NONE)
289 | 19 => (case (OpenSSL.readString bio, OpenSSL.readString bio,
290 OpenSSL.readString bio) of
291 (SOME domain, SOME user, SOME passwd) =>
292 SOME (MsgPasswdMailbox {domain = domain, user = user,
293 passwd = passwd})
294 | _ => NONE)
295 | 20 => (case (OpenSSL.readString bio, OpenSSL.readString bio) of
296 (SOME domain, SOME user) =>
297 SOME (MsgRmMailbox {domain = domain, user = user})
298 | _ => NONE)
299 | 21 => Option.map MsgListMailboxes (OpenSSL.readString bio)
300 | 22 => Option.map MsgMailboxes (recvList
301 (fn bio =>
302 case (OpenSSL.readString bio,
303 OpenSSL.readString bio) of
304 (SOME user, SOME mailbox) =>
305 SOME {user = user, mailbox = mailbox}
306 | _ => NONE)
307 bio)
308 | 23 => Option.map MsgSaQuery (OpenSSL.readString bio)
309 | 24 => Option.map MsgSaStatus (recvBool bio)
310 | 25 => (case (OpenSSL.readString bio, recvBool bio) of
311 (SOME user, SOME b) => SOME (MsgSaSet (user, b))
312 | _ => NONE)
313 | 26 => Option.map MsgSmtpLogReq (OpenSSL.readString bio)
314 | 27 => Option.map MsgSmtpLogRes (OpenSSL.readString bio)
315 | 28 => (case (OpenSSL.readString bio, OpenSSL.readString bio) of
316 (SOME dbtype, SOME passwd) =>
317 SOME (MsgDbPasswd {dbtype = dbtype, passwd = passwd})
318 | _ => NONE)
319 | 29 => SOME MsgShutdown
320 | 30 => SOME MsgYes
321 | 31 => SOME MsgNo
322 | 32 => Option.map MsgQuery (recvQuery bio)
323 | 33 => Option.map MsgSocket (recvSockPerm bio)
324 | 34 => Option.map MsgFirewall (recvList OpenSSL.readString bio)
325 | 35 => SOME MsgRegenerateTc
326 | _ => NONE)
327
328 end