d330d9b8 |
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 | |
e2130d9c |
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 | |
c8a739af |
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 | |
d330d9b8 |
64 | fun send (bio, m) = |
65 | case m of |
66 | MsgOk => OpenSSL.writeInt (bio, 1) |
67 | | MsgError s => (OpenSSL.writeInt (bio, 2); |
68 | OpenSSL.writeString (bio, s)) |
69 | | MsgConfig s => (OpenSSL.writeInt (bio, 3); |
70 | OpenSSL.writeString (bio, s)) |
71 | | MsgFile {action, domain, dir, file} => |
72 | (OpenSSL.writeInt (bio, 4); |
73 | OpenSSL.writeInt (bio, a2i action); |
74 | OpenSSL.writeString (bio, domain); |
75 | OpenSSL.writeString (bio, dir); |
76 | OpenSSL.writeString (bio, file)) |
77 | | MsgDoFiles => OpenSSL.writeInt (bio, 5) |
e2130d9c |
78 | | MsgGrant acl => (OpenSSL.writeInt (bio, 6); |
79 | sendAcl (bio, acl)) |
d1aa6a21 |
80 | | MsgRevoke acl => (OpenSSL.writeInt (bio, 7); |
81 | sendAcl (bio, acl)) |
646381db |
82 | | MsgListPerms user => (OpenSSL.writeInt (bio, 8); |
83 | OpenSSL.writeString (bio, user)) |
84 | | MsgPerms classes => (OpenSSL.writeInt (bio, 9); |
c8a739af |
85 | sendList (fn (bio, (class, values)) => |
86 | (OpenSSL.writeString (bio, class); |
87 | sendList OpenSSL.writeString (bio, values))) |
88 | (bio, classes)) |
d0e75410 |
89 | | MsgWhoHas {class, value} => (OpenSSL.writeInt (bio, 10); |
90 | OpenSSL.writeString (bio, class); |
91 | OpenSSL.writeString (bio, value)) |
92 | | MsgWhoHasResponse users => (OpenSSL.writeInt (bio, 11); |
c8a739af |
93 | sendList OpenSSL.writeString (bio, users)) |
94 | | MsgMultiConfig codes => (OpenSSL.writeInt (bio, 12); |
95 | sendList OpenSSL.writeString (bio, codes)) |
aba1f07e |
96 | | MsgRmdom doms => (OpenSSL.writeInt (bio, 13); |
97 | sendList OpenSSL.writeString (bio, doms)) |
f92c6883 |
98 | | MsgRegenerate => OpenSSL.writeInt (bio, 14) |
aba1f07e |
99 | | MsgRmuser dom => (OpenSSL.writeInt (bio, 15); |
100 | OpenSSL.writeString (bio, dom)) |
d330d9b8 |
101 | |
102 | fun checkIt v = |
103 | case v of |
104 | NONE => raise OpenSSL.OpenSSL "Bad Msg format" |
105 | | _ => v |
106 | |
107 | fun recv bio = |
108 | case OpenSSL.readInt bio of |
109 | NONE => NONE |
110 | | SOME n => |
111 | checkIt (case n of |
112 | 1 => SOME MsgOk |
113 | | 2 => Option.map MsgError (OpenSSL.readString bio) |
114 | | 3 => Option.map MsgConfig (OpenSSL.readString bio) |
115 | | 4 => (case (OpenSSL.readInt bio, |
116 | OpenSSL.readString bio, |
117 | OpenSSL.readString bio, |
118 | OpenSSL.readString bio) of |
119 | (SOME action, SOME domain, SOME dir, SOME file) => |
120 | SOME (MsgFile {action = i2a action, |
121 | domain = domain, |
122 | dir = dir, |
123 | file = file}) |
124 | | _ => NONE) |
125 | | 5 => SOME MsgDoFiles |
e2130d9c |
126 | | 6 => (case recvAcl bio of |
127 | SOME acl => SOME (MsgGrant acl) |
128 | | _ => NONE) |
d1aa6a21 |
129 | | 7 => (case recvAcl bio of |
130 | SOME acl => SOME (MsgRevoke acl) |
131 | | _ => NONE) |
646381db |
132 | | 8 => (case OpenSSL.readString bio of |
133 | SOME user => SOME (MsgListPerms user) |
134 | | _ => NONE) |
c8a739af |
135 | | 9 => Option.map MsgPerms |
136 | (recvList (fn bio => |
137 | case (OpenSSL.readString bio, |
138 | recvList OpenSSL.readString bio) of |
139 | (SOME class, SOME values) => SOME (class, values) |
140 | | _ => NONE) bio) |
d0e75410 |
141 | | 10 => (case (OpenSSL.readString bio, OpenSSL.readString bio) of |
142 | (SOME class, SOME value) => SOME (MsgWhoHas {class = class, value = value}) |
143 | | _ => NONE) |
c8a739af |
144 | | 11 => Option.map MsgWhoHasResponse |
145 | (recvList OpenSSL.readString bio) |
146 | | 12 => Option.map MsgMultiConfig |
147 | (recvList OpenSSL.readString bio) |
aba1f07e |
148 | | 13 => Option.map MsgRmdom (recvList OpenSSL.readString bio) |
f92c6883 |
149 | | 14 => SOME MsgRegenerate |
aba1f07e |
150 | | 15 => Option.map MsgRmuser (OpenSSL.readString bio) |
d330d9b8 |
151 | | _ => NONE) |
152 | |
153 | end |