e680130a |
1 | (* HCoop Domtool (http://hcoop.sourceforge.net/) |
3410e495 |
2 | * Copyright (c) 2006-2007, Adam Chlipala |
e680130a |
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. |
ae3a5b8c |
17 | *) |
e680130a |
18 | |
19 | (* Main interface *) |
20 | |
21 | structure Main :> MAIN = struct |
22 | |
d330d9b8 |
23 | open Ast MsgTypes Print |
e680130a |
24 | |
85af7d3e |
25 | structure SM = StringMap |
26 | |
53d222a3 |
27 | fun init () = Acl.read Config.aclFile |
e680130a |
28 | |
17ef447e |
29 | fun check' G fname = |
a11c0ff3 |
30 | let |
31 | val prog = Parse.parse fname |
32 | in |
33 | if !ErrorMsg.anyErrors then |
17ef447e |
34 | G |
a11c0ff3 |
35 | else |
53d222a3 |
36 | Tycheck.checkFile G (Defaults.tInit ()) prog |
a11c0ff3 |
37 | end |
38 | |
17ef447e |
39 | fun basis () = |
e680130a |
40 | let |
17ef447e |
41 | val dir = Posix.FileSys.opendir Config.libRoot |
42 | |
43 | fun loop files = |
44 | case Posix.FileSys.readdir dir of |
c12828f2 |
45 | NONE => (Posix.FileSys.closedir dir; |
46 | files) |
17ef447e |
47 | | SOME fname => |
48 | if String.isSuffix ".dtl" fname then |
c12828f2 |
49 | loop (OS.Path.joinDirFile {dir = Config.libRoot, |
50 | file = fname} |
17ef447e |
51 | :: files) |
52 | else |
53 | loop files |
54 | |
55 | val files = loop [] |
c8a739af |
56 | val (_, files) = Order.order NONE files |
17ef447e |
57 | in |
85af7d3e |
58 | if !ErrorMsg.anyErrors then |
59 | Env.empty |
60 | else |
89c9edc9 |
61 | (Tycheck.allowExterns (); |
62 | foldl (fn (fname, G) => check' G fname) Env.empty files |
63 | before Tycheck.disallowExterns ()) |
17ef447e |
64 | end |
65 | |
66 | fun check fname = |
67 | let |
68 | val _ = ErrorMsg.reset () |
4e8a3f2b |
69 | val _ = Env.preTycheck () |
17ef447e |
70 | |
71 | val b = basis () |
e680130a |
72 | in |
73 | if !ErrorMsg.anyErrors then |
d330d9b8 |
74 | raise ErrorMsg.Error |
e680130a |
75 | else |
76 | let |
89c9edc9 |
77 | val _ = Tycheck.disallowExterns () |
4cc63b03 |
78 | val _ = ErrorMsg.reset () |
17ef447e |
79 | val prog = Parse.parse fname |
e680130a |
80 | in |
add6f172 |
81 | if !ErrorMsg.anyErrors then |
d330d9b8 |
82 | raise ErrorMsg.Error |
add6f172 |
83 | else |
17ef447e |
84 | let |
53d222a3 |
85 | val G' = Tycheck.checkFile b (Defaults.tInit ()) prog |
17ef447e |
86 | in |
d330d9b8 |
87 | if !ErrorMsg.anyErrors then |
88 | raise ErrorMsg.Error |
89 | else |
90 | (G', #3 prog) |
17ef447e |
91 | end |
e680130a |
92 | end |
93 | end |
94 | |
c8a739af |
95 | val notTmp = CharVector.all (fn ch => Char.isAlphaNum ch orelse ch = #"." orelse ch = #"_" orelse ch = #"-") |
96 | |
97 | fun checkDir dname = |
98 | let |
99 | val b = basis () |
100 | |
101 | val dir = Posix.FileSys.opendir dname |
102 | |
103 | fun loop files = |
104 | case Posix.FileSys.readdir dir of |
105 | NONE => (Posix.FileSys.closedir dir; |
106 | files) |
107 | | SOME fname => |
108 | if notTmp fname then |
109 | loop (OS.Path.joinDirFile {dir = dname, |
110 | file = fname} |
111 | :: files) |
112 | else |
113 | loop files |
114 | |
115 | val files = loop [] |
116 | val (_, files) = Order.order (SOME b) files |
117 | in |
118 | if !ErrorMsg.anyErrors then |
f92c6883 |
119 | raise ErrorMsg.Error |
c8a739af |
120 | else |
121 | (foldl (fn (fname, G) => check' G fname) b files; |
f92c6883 |
122 | if !ErrorMsg.anyErrors then |
123 | raise ErrorMsg.Error |
124 | else |
125 | ()) |
c8a739af |
126 | end |
127 | |
17ef447e |
128 | fun reduce fname = |
a11c0ff3 |
129 | let |
17ef447e |
130 | val (G, body) = check fname |
a11c0ff3 |
131 | in |
132 | if !ErrorMsg.anyErrors then |
17ef447e |
133 | NONE |
a11c0ff3 |
134 | else |
17ef447e |
135 | case body of |
136 | SOME body => |
137 | let |
138 | val body' = Reduce.reduceExp G body |
139 | in |
140 | (*printd (PD.hovBox (PD.PPS.Rel 0, |
141 | [PD.string "Result:", |
142 | PD.space 1, |
143 | p_exp body']))*) |
144 | SOME body' |
145 | end |
146 | | _ => NONE |
a11c0ff3 |
147 | end |
148 | |
17ef447e |
149 | fun eval fname = |
150 | case reduce fname of |
151 | (SOME body') => |
152 | if !ErrorMsg.anyErrors then |
d330d9b8 |
153 | raise ErrorMsg.Error |
17ef447e |
154 | else |
53d222a3 |
155 | Eval.exec (Defaults.eInit ()) body' |
d330d9b8 |
156 | | NONE => raise ErrorMsg.Error |
17ef447e |
157 | |
f92c6883 |
158 | fun eval' fname = |
159 | case reduce fname of |
160 | (SOME body') => |
161 | if !ErrorMsg.anyErrors then |
162 | raise ErrorMsg.Error |
163 | else |
164 | ignore (Eval.exec' (Defaults.eInit ()) body') |
165 | | NONE => raise ErrorMsg.Error |
166 | |
2569e66d |
167 | val dispatcher = |
168 | Config.dispatcher ^ ":" ^ Int.toString Config.dispatcherPort |
1f8889bd |
169 | |
aef9c54f |
170 | val self = |
171 | "localhost:" ^ Int.toString Config.slavePort |
172 | |
3d55b046 |
173 | fun context x = |
174 | (OpenSSL.context false x) |
b247eba3 |
175 | handle e as OpenSSL.OpenSSL s => |
3d55b046 |
176 | (print "Couldn't find your certificate.\nYou probably haven't been given any Domtool privileges.\n"; |
b247eba3 |
177 | print ("Additional information: " ^ s ^ "\n"); |
3d55b046 |
178 | raise e) |
179 | |
12ad758b |
180 | fun setupUser () = |
904eb905 |
181 | let |
ab3ebaba |
182 | val user = |
183 | case Posix.ProcEnv.getenv "DOMTOOL_USER" of |
184 | NONE => |
185 | let |
186 | val uid = Posix.ProcEnv.getuid () |
187 | in |
188 | Posix.SysDB.Passwd.name (Posix.SysDB.getpwuid uid) |
189 | end |
190 | | SOME user => user |
12ad758b |
191 | in |
192 | Acl.read Config.aclFile; |
193 | Domain.setUser user; |
194 | user |
195 | end |
196 | |
197 | fun requestContext f = |
198 | let |
199 | val user = setupUser () |
e2130d9c |
200 | |
201 | val () = f () |
53d222a3 |
202 | |
3d55b046 |
203 | val context = context (Config.certDir ^ "/" ^ user ^ ".pem", |
204 | Config.keyDir ^ "/" ^ user ^ "/key.pem", |
205 | Config.trustStore) |
e2130d9c |
206 | in |
207 | (user, context) |
208 | end |
904eb905 |
209 | |
e2130d9c |
210 | fun requestBio f = |
211 | let |
212 | val (user, context) = requestContext f |
213 | in |
214 | (user, OpenSSL.connect (context, dispatcher)) |
215 | end |
216 | |
aef9c54f |
217 | fun requestSlaveBio () = |
218 | let |
219 | val (user, context) = requestContext (fn () => ()) |
220 | in |
221 | (user, OpenSSL.connect (context, self)) |
222 | end |
223 | |
e2130d9c |
224 | fun request fname = |
225 | let |
226 | val (user, bio) = requestBio (fn () => ignore (check fname)) |
1f8889bd |
227 | |
2569e66d |
228 | val inf = TextIO.openIn fname |
229 | |
d330d9b8 |
230 | fun loop lines = |
2569e66d |
231 | case TextIO.inputLine inf of |
d330d9b8 |
232 | NONE => String.concat (List.rev lines) |
233 | | SOME line => loop (line :: lines) |
234 | |
235 | val code = loop [] |
1f8889bd |
236 | in |
2569e66d |
237 | TextIO.closeIn inf; |
d330d9b8 |
238 | Msg.send (bio, MsgConfig code); |
239 | case Msg.recv bio of |
240 | NONE => print "Server closed connection unexpectedly.\n" |
241 | | SOME m => |
242 | case m of |
243 | MsgOk => print "Configuration succeeded.\n" |
244 | | MsgError s => print ("Configuration failed: " ^ s ^ "\n") |
245 | | _ => print "Unexpected server reply.\n"; |
2569e66d |
246 | OpenSSL.close bio |
1f8889bd |
247 | end |
53d222a3 |
248 | handle ErrorMsg.Error => () |
1f8889bd |
249 | |
c8a739af |
250 | fun requestDir dname = |
251 | let |
59815d99 |
252 | val _ = if Posix.FileSys.access (dname, []) then |
253 | () |
254 | else |
255 | (print ("Can't access " ^ dname ^ ".\n"); |
256 | print "Did you mean to run domtool on a specific file, instead of asking for all\n"; |
257 | print "files in your ~/domtool directory?\n"; |
258 | OS.Process.exit OS.Process.failure) |
259 | |
f92c6883 |
260 | val _ = ErrorMsg.reset () |
261 | |
262 | val (user, bio) = requestBio (fn () => checkDir dname) |
c8a739af |
263 | |
264 | val b = basis () |
265 | |
266 | val dir = Posix.FileSys.opendir dname |
267 | |
268 | fun loop files = |
269 | case Posix.FileSys.readdir dir of |
270 | NONE => (Posix.FileSys.closedir dir; |
271 | files) |
272 | | SOME fname => |
273 | if notTmp fname then |
274 | loop (OS.Path.joinDirFile {dir = dname, |
275 | file = fname} |
276 | :: files) |
277 | else |
278 | loop files |
279 | |
280 | val files = loop [] |
281 | val (_, files) = Order.order (SOME b) files |
282 | |
283 | val _ = if !ErrorMsg.anyErrors then |
284 | raise ErrorMsg.Error |
285 | else |
286 | () |
287 | |
288 | val codes = map (fn fname => |
289 | let |
290 | val inf = TextIO.openIn fname |
291 | |
292 | fun loop lines = |
293 | case TextIO.inputLine inf of |
294 | NONE => String.concat (rev lines) |
295 | | SOME line => loop (line :: lines) |
296 | in |
297 | loop [] |
298 | before TextIO.closeIn inf |
299 | end) files |
300 | in |
f92c6883 |
301 | if !ErrorMsg.anyErrors then |
302 | () |
303 | else |
304 | (Msg.send (bio, MsgMultiConfig codes); |
305 | case Msg.recv bio of |
306 | NONE => print "Server closed connection unexpectedly.\n" |
307 | | SOME m => |
308 | case m of |
309 | MsgOk => print "Configuration succeeded.\n" |
310 | | MsgError s => print ("Configuration failed: " ^ s ^ "\n") |
311 | | _ => print "Unexpected server reply.\n"; |
312 | OpenSSL.close bio) |
c8a739af |
313 | end |
314 | handle ErrorMsg.Error => () |
315 | |
93176f2b |
316 | fun requestPing () = |
317 | let |
318 | val (_, bio) = requestBio (fn () => ()) |
319 | in |
320 | OpenSSL.close bio; |
321 | OS.Process.success |
322 | end |
323 | handle _ => OS.Process.failure |
324 | |
4c68bd31 |
325 | fun requestShutdown () = |
326 | let |
327 | val (_, bio) = requestBio (fn () => ()) |
328 | in |
329 | Msg.send (bio, MsgShutdown); |
330 | case Msg.recv bio of |
331 | NONE => print "Server closed connection unexpectedly.\n" |
332 | | SOME m => |
333 | case m of |
334 | MsgOk => print "Shutdown begun.\n" |
335 | | MsgError s => print ("Shutdown failed: " ^ s ^ "\n") |
336 | | _ => print "Unexpected server reply.\n"; |
337 | OpenSSL.close bio |
338 | end |
339 | |
aef9c54f |
340 | fun requestSlavePing () = |
341 | let |
342 | val (_, bio) = requestSlaveBio () |
343 | in |
344 | OpenSSL.close bio; |
345 | OS.Process.success |
346 | end |
347 | handle _ => OS.Process.failure |
348 | |
349 | fun requestSlaveShutdown () = |
350 | let |
351 | val (_, bio) = requestSlaveBio () |
352 | in |
353 | Msg.send (bio, MsgShutdown); |
354 | case Msg.recv bio of |
355 | NONE => print "Server closed connection unexpectedly.\n" |
356 | | SOME m => |
357 | case m of |
358 | MsgOk => print "Shutdown begun.\n" |
359 | | MsgError s => print ("Shutdown failed: " ^ s ^ "\n") |
360 | | _ => print "Unexpected server reply.\n"; |
361 | OpenSSL.close bio |
362 | end |
363 | |
e2130d9c |
364 | fun requestGrant acl = |
365 | let |
366 | val (user, bio) = requestBio (fn () => ()) |
367 | in |
368 | Msg.send (bio, MsgGrant acl); |
369 | case Msg.recv bio of |
370 | NONE => print "Server closed connection unexpectedly.\n" |
371 | | SOME m => |
372 | case m of |
373 | MsgOk => print "Grant succeeded.\n" |
374 | | MsgError s => print ("Grant failed: " ^ s ^ "\n") |
375 | | _ => print "Unexpected server reply.\n"; |
376 | OpenSSL.close bio |
377 | end |
378 | |
d1aa6a21 |
379 | fun requestRevoke acl = |
380 | let |
381 | val (user, bio) = requestBio (fn () => ()) |
382 | in |
383 | Msg.send (bio, MsgRevoke acl); |
384 | case Msg.recv bio of |
385 | NONE => print "Server closed connection unexpectedly.\n" |
386 | | SOME m => |
387 | case m of |
388 | MsgOk => print "Revoke succeeded.\n" |
389 | | MsgError s => print ("Revoke failed: " ^ s ^ "\n") |
390 | | _ => print "Unexpected server reply.\n"; |
391 | OpenSSL.close bio |
392 | end |
393 | |
646381db |
394 | fun requestListPerms user = |
395 | let |
396 | val (_, bio) = requestBio (fn () => ()) |
397 | in |
398 | Msg.send (bio, MsgListPerms user); |
399 | (case Msg.recv bio of |
400 | NONE => (print "Server closed connection unexpectedly.\n"; |
401 | NONE) |
402 | | SOME m => |
403 | case m of |
404 | MsgPerms perms => SOME perms |
405 | | MsgError s => (print ("Listing failed: " ^ s ^ "\n"); |
406 | NONE) |
407 | | _ => (print "Unexpected server reply.\n"; |
408 | NONE)) |
409 | before OpenSSL.close bio |
410 | end |
411 | |
d0e75410 |
412 | fun requestWhoHas perm = |
413 | let |
414 | val (_, bio) = requestBio (fn () => ()) |
415 | in |
416 | Msg.send (bio, MsgWhoHas perm); |
417 | (case Msg.recv bio of |
418 | NONE => (print "Server closed connection unexpectedly.\n"; |
419 | NONE) |
420 | | SOME m => |
421 | case m of |
422 | MsgWhoHasResponse users => SOME users |
423 | | MsgError s => (print ("whohas failed: " ^ s ^ "\n"); |
424 | NONE) |
425 | | _ => (print "Unexpected server reply.\n"; |
426 | NONE)) |
427 | before OpenSSL.close bio |
428 | end |
429 | |
f92c6883 |
430 | fun requestRegen () = |
431 | let |
432 | val (_, bio) = requestBio (fn () => ()) |
433 | in |
434 | Msg.send (bio, MsgRegenerate); |
435 | case Msg.recv bio of |
436 | NONE => print "Server closed connection unexpectedly.\n" |
437 | | SOME m => |
438 | case m of |
439 | MsgOk => print "Regeneration succeeded.\n" |
440 | | MsgError s => print ("Regeneration failed: " ^ s ^ "\n") |
441 | | _ => print "Unexpected server reply.\n"; |
442 | OpenSSL.close bio |
443 | end |
444 | |
b4f5896d |
445 | fun requestRegenTc () = |
446 | let |
447 | val (_, bio) = requestBio (fn () => ()) |
448 | in |
449 | Msg.send (bio, MsgRegenerateTc); |
450 | case Msg.recv bio of |
451 | NONE => print "Server closed connection unexpectedly.\n" |
452 | | SOME m => |
453 | case m of |
454 | MsgOk => print "All configuration validated.\n" |
455 | | MsgError s => print ("Configuration validation failed: " ^ s ^ "\n") |
456 | | _ => print "Unexpected server reply.\n"; |
457 | OpenSSL.close bio |
458 | end |
459 | |
7d32cf2f |
460 | fun requestRmdom dom = |
461 | let |
462 | val (_, bio) = requestBio (fn () => ()) |
463 | in |
464 | Msg.send (bio, MsgRmdom dom); |
465 | case Msg.recv bio of |
466 | NONE => print "Server closed connection unexpectedly.\n" |
467 | | SOME m => |
468 | case m of |
469 | MsgOk => print "Removal succeeded.\n" |
470 | | MsgError s => print ("Removal failed: " ^ s ^ "\n") |
471 | | _ => print "Unexpected server reply.\n"; |
472 | OpenSSL.close bio |
473 | end |
474 | |
aba1f07e |
475 | fun requestRmuser user = |
476 | let |
477 | val (_, bio) = requestBio (fn () => ()) |
478 | in |
479 | Msg.send (bio, MsgRmuser user); |
480 | case Msg.recv bio of |
481 | NONE => print "Server closed connection unexpectedly.\n" |
482 | | SOME m => |
483 | case m of |
484 | MsgOk => print "Removal succeeded.\n" |
485 | | MsgError s => print ("Removal failed: " ^ s ^ "\n") |
486 | | _ => print "Unexpected server reply.\n"; |
487 | OpenSSL.close bio |
488 | end |
489 | |
634d7082 |
490 | fun requestDbUser dbtype = |
491 | let |
492 | val (_, bio) = requestBio (fn () => ()) |
493 | in |
494 | Msg.send (bio, MsgCreateDbUser dbtype); |
495 | case Msg.recv bio of |
496 | NONE => print "Server closed connection unexpectedly.\n" |
497 | | SOME m => |
498 | case m of |
499 | MsgOk => print "Your user has been created.\n" |
500 | | MsgError s => print ("Creation failed: " ^ s ^ "\n") |
501 | | _ => print "Unexpected server reply.\n"; |
502 | OpenSSL.close bio |
503 | end |
504 | |
dc6789fc |
505 | fun requestDbPasswd rc = |
506 | let |
507 | val (_, bio) = requestBio (fn () => ()) |
508 | in |
509 | Msg.send (bio, MsgDbPasswd rc); |
510 | case Msg.recv bio of |
511 | NONE => print "Server closed connection unexpectedly.\n" |
512 | | SOME m => |
513 | case m of |
514 | MsgOk => print "Your password has been changed.\n" |
515 | | MsgError s => print ("Password set failed: " ^ s ^ "\n") |
516 | | _ => print "Unexpected server reply.\n"; |
517 | OpenSSL.close bio |
518 | end |
519 | |
d34cbcb8 |
520 | fun requestDbTable p = |
521 | let |
522 | val (user, bio) = requestBio (fn () => ()) |
523 | in |
524 | Msg.send (bio, MsgCreateDbTable p); |
525 | case Msg.recv bio of |
526 | NONE => print "Server closed connection unexpectedly.\n" |
527 | | SOME m => |
528 | case m of |
529 | MsgOk => print ("Your database " ^ user ^ "_" ^ #dbname p ^ " has been created.\n") |
530 | | MsgError s => print ("Creation failed: " ^ s ^ "\n") |
531 | | _ => print "Unexpected server reply.\n"; |
532 | OpenSSL.close bio |
533 | end |
534 | |
0a58b2f3 |
535 | fun requestListMailboxes domain = |
536 | let |
537 | val (_, bio) = requestBio (fn () => ()) |
538 | in |
539 | Msg.send (bio, MsgListMailboxes domain); |
540 | (case Msg.recv bio of |
bb8cc8c9 |
541 | NONE => Vmail.Error "Server closed connection unexpectedly." |
0a58b2f3 |
542 | | SOME m => |
543 | case m of |
544 | MsgMailboxes users => (Msg.send (bio, MsgOk); |
545 | Vmail.Listing users) |
546 | | MsgError s => Vmail.Error ("Creation failed: " ^ s) |
bb8cc8c9 |
547 | | _ => Vmail.Error "Unexpected server reply.") |
0a58b2f3 |
548 | before OpenSSL.close bio |
549 | end |
550 | |
c45f1662 |
551 | fun requestNewMailbox p = |
552 | let |
553 | val (_, bio) = requestBio (fn () => ()) |
554 | in |
555 | Msg.send (bio, MsgNewMailbox p); |
556 | case Msg.recv bio of |
557 | NONE => print "Server closed connection unexpectedly.\n" |
558 | | SOME m => |
559 | case m of |
560 | MsgOk => print ("A mapping for " ^ #user p ^ "@" ^ #domain p ^ " has been created.\n") |
561 | | MsgError s => print ("Creation failed: " ^ s ^ "\n") |
562 | | _ => print "Unexpected server reply.\n"; |
563 | OpenSSL.close bio |
564 | end |
565 | |
566 | fun requestPasswdMailbox p = |
567 | let |
568 | val (_, bio) = requestBio (fn () => ()) |
569 | in |
570 | Msg.send (bio, MsgPasswdMailbox p); |
571 | case Msg.recv bio of |
572 | NONE => print "Server closed connection unexpectedly.\n" |
573 | | SOME m => |
574 | case m of |
575 | MsgOk => print ("The password for " ^ #user p ^ "@" ^ #domain p ^ " has been changed.\n") |
576 | | MsgError s => print ("Set failed: " ^ s ^ "\n") |
577 | | _ => print "Unexpected server reply.\n"; |
578 | OpenSSL.close bio |
579 | end |
580 | |
581 | fun requestRmMailbox p = |
582 | let |
583 | val (_, bio) = requestBio (fn () => ()) |
584 | in |
585 | Msg.send (bio, MsgRmMailbox p); |
586 | case Msg.recv bio of |
587 | NONE => print "Server closed connection unexpectedly.\n" |
588 | | SOME m => |
589 | case m of |
590 | MsgOk => print ("The mapping for mailbox " ^ #user p ^ "@" ^ #domain p ^ " has been deleted.\n") |
591 | | MsgError s => print ("Remove failed: " ^ s ^ "\n") |
592 | | _ => print "Unexpected server reply.\n"; |
593 | OpenSSL.close bio |
594 | end |
595 | |
bb8cc8c9 |
596 | fun requestSaQuery addr = |
597 | let |
598 | val (_, bio) = requestBio (fn () => ()) |
599 | in |
600 | Msg.send (bio, MsgSaQuery addr); |
601 | (case Msg.recv bio of |
602 | NONE => print "Server closed connection unexpectedly.\n" |
603 | | SOME m => |
604 | case m of |
605 | MsgSaStatus b => (print ("SpamAssassin filtering for " ^ addr ^ " is " |
606 | ^ (if b then "ON" else "OFF") ^ ".\n"); |
607 | Msg.send (bio, MsgOk)) |
608 | | MsgError s => print ("Query failed: " ^ s ^ "\n") |
609 | | _ => print "Unexpected server reply.\n") |
610 | before OpenSSL.close bio |
611 | end |
612 | |
613 | fun requestSaSet p = |
614 | let |
615 | val (_, bio) = requestBio (fn () => ()) |
616 | in |
617 | Msg.send (bio, MsgSaSet p); |
618 | case Msg.recv bio of |
619 | NONE => print "Server closed connection unexpectedly.\n" |
620 | | SOME m => |
621 | case m of |
622 | MsgOk => print ("SpamAssassin filtering for " ^ #1 p ^ " is now " |
623 | ^ (if #2 p then "ON" else "OFF") ^ ".\n") |
624 | | MsgError s => print ("Set failed: " ^ s ^ "\n") |
625 | | _ => print "Unexpected server reply.\n"; |
626 | OpenSSL.close bio |
627 | end |
628 | |
c685120e |
629 | fun requestSmtpLog domain = |
630 | let |
631 | val (_, bio) = requestBio (fn () => ()) |
632 | |
633 | val _ = Msg.send (bio, MsgSmtpLogReq domain) |
634 | |
635 | fun loop () = |
636 | case Msg.recv bio of |
637 | NONE => print "Server closed connection unexpectedly.\n" |
638 | | SOME m => |
639 | case m of |
640 | MsgOk => () |
641 | | MsgSmtpLogRes line => (print line; |
642 | loop ()) |
643 | | MsgError s => print ("Log search failed: " ^ s ^ "\n") |
644 | | _ => print "Unexpected server reply.\n" |
645 | in |
646 | loop (); |
647 | OpenSSL.close bio |
648 | end |
649 | |
8301e970 |
650 | fun requestApt {node, pkg} = |
651 | let |
e5f8cb7b |
652 | val (user, context) = requestContext (fn () => ()) |
653 | val bio = OpenSSL.connect (context, if node = Config.masterNode then |
654 | dispatcher |
655 | else |
656 | Domain.nodeIp node ^ ":" ^ Int.toString Config.slavePort) |
8301e970 |
657 | |
e5f8cb7b |
658 | val _ = Msg.send (bio, MsgQuery (QApt pkg)) |
8301e970 |
659 | |
660 | fun loop () = |
661 | case Msg.recv bio of |
662 | NONE => (print "Server closed connection unexpectedly.\n"; |
663 | OS.Process.failure) |
664 | | SOME m => |
665 | case m of |
666 | MsgYes => (print "Package is installed.\n"; |
667 | OS.Process.success) |
668 | | MsgNo => (print "Package is not installed.\n"; |
669 | OS.Process.failure) |
670 | | MsgError s => (print ("APT query failed: " ^ s ^ "\n"); |
671 | OS.Process.failure) |
672 | | _ => (print "Unexpected server reply.\n"; |
673 | OS.Process.failure) |
674 | in |
675 | loop () |
676 | before OpenSSL.close bio |
677 | end |
678 | |
891b033c |
679 | fun requestCron {node, uname} = |
680 | let |
681 | val (user, context) = requestContext (fn () => ()) |
682 | val bio = OpenSSL.connect (context, if node = Config.masterNode then |
683 | dispatcher |
684 | else |
685 | Domain.nodeIp node ^ ":" ^ Int.toString Config.slavePort) |
686 | |
687 | val _ = Msg.send (bio, MsgQuery (QCron uname)) |
688 | |
689 | fun loop () = |
690 | case Msg.recv bio of |
691 | NONE => (print "Server closed connection unexpectedly.\n"; |
692 | OS.Process.failure) |
693 | | SOME m => |
694 | case m of |
695 | MsgYes => (print "User has cron permissions.\n"; |
696 | OS.Process.success) |
697 | | MsgNo => (print "User does not have cron permissions.\n"; |
698 | OS.Process.failure) |
699 | | MsgError s => (print ("Cron query failed: " ^ s ^ "\n"); |
700 | OS.Process.failure) |
701 | | _ => (print "Unexpected server reply.\n"; |
702 | OS.Process.failure) |
703 | in |
704 | loop () |
705 | before OpenSSL.close bio |
706 | end |
707 | |
708 | fun requestFtp {node, uname} = |
709 | let |
710 | val (user, context) = requestContext (fn () => ()) |
711 | val bio = OpenSSL.connect (context, if node = Config.masterNode then |
712 | dispatcher |
713 | else |
714 | Domain.nodeIp node ^ ":" ^ Int.toString Config.slavePort) |
715 | |
716 | val _ = Msg.send (bio, MsgQuery (QFtp uname)) |
717 | |
718 | fun loop () = |
719 | case Msg.recv bio of |
720 | NONE => (print "Server closed connection unexpectedly.\n"; |
721 | OS.Process.failure) |
722 | | SOME m => |
723 | case m of |
724 | MsgYes => (print "User has FTP permissions.\n"; |
725 | OS.Process.success) |
726 | | MsgNo => (print "User does not have FTP permissions.\n"; |
727 | OS.Process.failure) |
728 | | MsgError s => (print ("FTP query failed: " ^ s ^ "\n"); |
729 | OS.Process.failure) |
730 | | _ => (print "Unexpected server reply.\n"; |
731 | OS.Process.failure) |
732 | in |
733 | loop () |
734 | before OpenSSL.close bio |
735 | end |
736 | |
4ee81d53 |
737 | fun requestTrustedPath {node, uname} = |
738 | let |
739 | val (user, context) = requestContext (fn () => ()) |
740 | val bio = OpenSSL.connect (context, if node = Config.masterNode then |
741 | dispatcher |
742 | else |
743 | Domain.nodeIp node ^ ":" ^ Int.toString Config.slavePort) |
744 | |
745 | val _ = Msg.send (bio, MsgQuery (QTrustedPath uname)) |
746 | |
747 | fun loop () = |
748 | case Msg.recv bio of |
749 | NONE => (print "Server closed connection unexpectedly.\n"; |
750 | OS.Process.failure) |
751 | | SOME m => |
752 | case m of |
753 | MsgYes => (print "User has trusted path restriction.\n"; |
754 | OS.Process.success) |
755 | | MsgNo => (print "User does not have trusted path restriction.\n"; |
756 | OS.Process.failure) |
757 | | MsgError s => (print ("Trusted path query failed: " ^ s ^ "\n"); |
758 | OS.Process.failure) |
759 | | _ => (print "Unexpected server reply.\n"; |
760 | OS.Process.failure) |
761 | in |
762 | loop () |
763 | before OpenSSL.close bio |
764 | end |
765 | |
9f063ecc |
766 | fun requestSocketPerm {node, uname} = |
767 | let |
768 | val (user, context) = requestContext (fn () => ()) |
769 | val bio = OpenSSL.connect (context, if node = Config.masterNode then |
770 | dispatcher |
771 | else |
772 | Domain.nodeIp node ^ ":" ^ Int.toString Config.slavePort) |
773 | |
774 | val _ = Msg.send (bio, MsgQuery (QSocket uname)) |
775 | |
776 | fun loop () = |
777 | case Msg.recv bio of |
778 | NONE => (print "Server closed connection unexpectedly.\n"; |
779 | OS.Process.failure) |
780 | | SOME m => |
781 | case m of |
782 | MsgSocket p => (case p of |
783 | Any => print "Any\n" |
784 | | Client => print "Client\n" |
785 | | Server => print "Server\n" |
786 | | Nada => print "Nada\n"; |
787 | OS.Process.success) |
788 | | MsgError s => (print ("Socket permission query failed: " ^ s ^ "\n"); |
789 | OS.Process.failure) |
790 | | _ => (print "Unexpected server reply.\n"; |
791 | OS.Process.failure) |
792 | in |
793 | loop () |
794 | before OpenSSL.close bio |
795 | end |
796 | |
5514f5b5 |
797 | fun requestFirewall {node, uname} = |
798 | let |
799 | val (user, context) = requestContext (fn () => ()) |
800 | val bio = OpenSSL.connect (context, if node = Config.masterNode then |
801 | dispatcher |
802 | else |
803 | Domain.nodeIp node ^ ":" ^ Int.toString Config.slavePort) |
804 | |
805 | val _ = Msg.send (bio, MsgQuery (QFirewall uname)) |
806 | |
807 | fun loop () = |
808 | case Msg.recv bio of |
809 | NONE => (print "Server closed connection unexpectedly.\n"; |
810 | OS.Process.failure) |
811 | | SOME m => |
812 | case m of |
813 | MsgFirewall ls => (app (fn s => (print s; print "\n")) ls; |
814 | OS.Process.success) |
815 | | MsgError s => (print ("Firewall query failed: " ^ s ^ "\n"); |
816 | OS.Process.failure) |
817 | | _ => (print "Unexpected server reply.\n"; |
818 | OS.Process.failure) |
819 | in |
820 | loop () |
821 | before OpenSSL.close bio |
822 | end |
823 | |
0ea0ecfa |
824 | fun regenerate context = |
f92c6883 |
825 | let |
414d52ef |
826 | val _ = ErrorMsg.reset () |
827 | |
f92c6883 |
828 | val b = basis () |
0ea0ecfa |
829 | val () = Tycheck.disallowExterns () |
830 | |
831 | val () = Domain.resetGlobal () |
832 | |
b4f5896d |
833 | val ok = ref true |
834 | |
0ea0ecfa |
835 | fun contactNode (node, ip) = |
836 | if node = Config.defaultNode then |
837 | Domain.resetLocal () |
838 | else let |
839 | val bio = OpenSSL.connect (context, |
840 | ip |
841 | ^ ":" |
842 | ^ Int.toString Config.slavePort) |
843 | in |
844 | Msg.send (bio, MsgRegenerate); |
845 | case Msg.recv bio of |
846 | NONE => print "Slave closed connection unexpectedly\n" |
847 | | SOME m => |
848 | case m of |
849 | MsgOk => print ("Slave " ^ node ^ " pre-regeneration finished\n") |
850 | | MsgError s => print ("Slave " ^ node |
851 | ^ " returned error: " ^ |
852 | s ^ "\n") |
853 | | _ => print ("Slave " ^ node |
854 | ^ " returned unexpected command\n"); |
855 | OpenSSL.close bio |
733f6a28 |
856 | end |
857 | handle OpenSSL.OpenSSL s => print ("OpenSSL error: " ^ s ^ "\n") |
f92c6883 |
858 | |
859 | fun doUser user = |
860 | let |
861 | val _ = Domain.setUser user |
862 | val _ = ErrorMsg.reset () |
863 | |
864 | val dname = Config.domtoolDir user |
b4f5896d |
865 | in |
866 | if Posix.FileSys.access (dname, []) then |
867 | let |
868 | val dir = Posix.FileSys.opendir dname |
869 | |
870 | fun loop files = |
871 | case Posix.FileSys.readdir dir of |
872 | NONE => (Posix.FileSys.closedir dir; |
873 | files) |
874 | | SOME fname => |
875 | if notTmp fname then |
876 | loop (OS.Path.joinDirFile {dir = dname, |
877 | file = fname} |
878 | :: files) |
879 | else |
880 | loop files |
f92c6883 |
881 | |
b4f5896d |
882 | val files = loop [] |
883 | val (_, files) = Order.order (SOME b) files |
884 | in |
885 | if !ErrorMsg.anyErrors then |
886 | (ErrorMsg.reset (); |
887 | print ("User " ^ user ^ "'s configuration has errors!\n")) |
f92c6883 |
888 | else |
b4f5896d |
889 | app eval' files |
890 | end |
f92c6883 |
891 | else |
b4f5896d |
892 | () |
f92c6883 |
893 | end |
b4f5896d |
894 | handle IO.Io _ => () |
895 | | OS.SysErr (s, _) => (print ("System error processing user " ^ user ^ ": " ^ s ^ "\n"); |
896 | ok := false) |
897 | | ErrorMsg.Error => (ErrorMsg.reset (); |
898 | print ("User " ^ user ^ " had a compilation error.\n"); |
899 | ok := false) |
900 | | _ => (print "Unknown exception during regeneration!\n"; |
901 | ok := false) |
f92c6883 |
902 | in |
0ea0ecfa |
903 | app contactNode Config.nodeIps; |
f92c6883 |
904 | Env.pre (); |
905 | app doUser (Acl.users ()); |
b4f5896d |
906 | Env.post (); |
907 | !ok |
908 | end |
909 | |
910 | fun regenerateTc context = |
911 | let |
912 | val _ = ErrorMsg.reset () |
913 | |
914 | val b = basis () |
915 | val () = Tycheck.disallowExterns () |
916 | |
917 | val () = Domain.resetGlobal () |
918 | |
919 | val ok = ref true |
920 | |
921 | fun doUser user = |
922 | let |
923 | val _ = Domain.setUser user |
924 | val _ = ErrorMsg.reset () |
925 | |
926 | val dname = Config.domtoolDir user |
927 | in |
928 | if Posix.FileSys.access (dname, []) then |
929 | let |
930 | val dir = Posix.FileSys.opendir dname |
931 | |
932 | fun loop files = |
933 | case Posix.FileSys.readdir dir of |
934 | NONE => (Posix.FileSys.closedir dir; |
935 | files) |
936 | | SOME fname => |
937 | if notTmp fname then |
938 | loop (OS.Path.joinDirFile {dir = dname, |
939 | file = fname} |
940 | :: files) |
941 | else |
942 | loop files |
943 | |
944 | val files = loop [] |
945 | val (_, files) = Order.order (SOME b) files |
946 | in |
947 | if !ErrorMsg.anyErrors then |
948 | (ErrorMsg.reset (); |
949 | print ("User " ^ user ^ "'s configuration has errors!\n"); |
950 | ok := false) |
951 | else |
952 | app (ignore o check) files |
953 | end |
954 | else |
955 | () |
956 | end |
957 | handle IO.Io _ => () |
958 | | OS.SysErr (s, _) => print ("System error processing user " ^ user ^ ": " ^ s ^ "\n") |
959 | | ErrorMsg.Error => (ErrorMsg.reset (); |
960 | print ("User " ^ user ^ " had a compilation error.\n")) |
961 | | _ => print "Unknown exception during -tc regeneration!\n" |
962 | in |
963 | app doUser (Acl.users ()); |
964 | !ok |
f92c6883 |
965 | end |
966 | |
aba1f07e |
967 | fun rmuser user = |
968 | let |
969 | val doms = Acl.class {user = user, class = "domain"} |
970 | val doms = List.filter (fn dom => |
971 | case Acl.whoHas {class = "domain", value = dom} of |
972 | [_] => true |
973 | | _ => false) (StringSet.listItems doms) |
974 | in |
975 | Acl.rmuser user; |
976 | Domain.rmdom doms |
977 | end |
978 | |
aef9c54f |
979 | fun now () = Date.toString (Date.fromTimeUniv (Time.now ())) |
980 | |
e5f8cb7b |
981 | fun answerQuery q = |
982 | case q of |
983 | QApt pkg => if Apt.installed pkg then MsgYes else MsgNo |
891b033c |
984 | | QCron user => if Cron.allowed user then MsgYes else MsgNo |
985 | | QFtp user => if Ftp.allowed user then MsgYes else MsgNo |
4ee81d53 |
986 | | QTrustedPath user => if TrustedPath.query user then MsgYes else MsgNo |
9f063ecc |
987 | | QSocket user => MsgSocket (SocketPerm.query user) |
5514f5b5 |
988 | | QFirewall user => MsgFirewall (Firewall.query user) |
e5f8cb7b |
989 | |
990 | fun describeQuery q = |
991 | case q of |
992 | QApt pkg => "Requested installation status of package " ^ pkg |
891b033c |
993 | | QCron user => "Asked about cron permissions for user " ^ user |
994 | | QFtp user => "Asked about FTP permissions for user " ^ user |
4ee81d53 |
995 | | QTrustedPath user => "Asked about trusted path settings for user " ^ user |
9f063ecc |
996 | | QSocket user => "Asked about socket permissions for user " ^ user |
5514f5b5 |
997 | | QFirewall user => "Asked about firewall rules for user " ^ user |
e5f8cb7b |
998 | |
2569e66d |
999 | fun service () = |
904eb905 |
1000 | let |
53d222a3 |
1001 | val () = Acl.read Config.aclFile |
1002 | |
3d55b046 |
1003 | val context = context (Config.serverCert, |
1004 | Config.serverKey, |
1005 | Config.trustStore) |
d330d9b8 |
1006 | val _ = Domain.set_context context |
2569e66d |
1007 | |
cbb8f260 |
1008 | val sock = OpenSSL.listen (context, Config.dispatcherPort) |
2569e66d |
1009 | |
1010 | fun loop () = |
cbb8f260 |
1011 | case OpenSSL.accept sock of |
2569e66d |
1012 | NONE => () |
1013 | | SOME bio => |
1014 | let |
53d222a3 |
1015 | val user = OpenSSL.peerCN bio |
aef9c54f |
1016 | val () = print ("\nConnection from " ^ user ^ " at " ^ now () ^ "\n") |
53d222a3 |
1017 | val () = Domain.setUser user |
1018 | |
c45f1662 |
1019 | fun doIt f cleanup = |
1020 | ((case f () of |
1021 | (msgLocal, SOME msgRemote) => |
1022 | (print msgLocal; |
1023 | print "\n"; |
1024 | Msg.send (bio, MsgError msgRemote)) |
1025 | | (msgLocal, NONE) => |
1026 | (print msgLocal; |
1027 | print "\n"; |
1028 | Msg.send (bio, MsgOk))) |
da9abf26 |
1029 | handle e as (OpenSSL.OpenSSL s) => |
733f6a28 |
1030 | (print ("OpenSSL error: " ^ s ^ "\n"); |
1031 | app (fn x => print (x ^ "\n")) (SMLofNJ.exnHistory e); |
1032 | Msg.send (bio, MsgError ("OpenSSL error: " ^ s)) |
1033 | handle OpenSSL.OpenSSL _ => ()) |
c45f1662 |
1034 | | OS.SysErr (s, _) => |
1035 | (print "System error: "; |
1036 | print s; |
1037 | print "\n"; |
1038 | Msg.send (bio, MsgError ("System error: " ^ s)) |
1039 | handle OpenSSL.OpenSSL _ => ()) |
1040 | | Fail s => |
1041 | (print "Failure: "; |
1042 | print s; |
1043 | print "\n"; |
1044 | Msg.send (bio, MsgError ("Failure: " ^ s)) |
1045 | handle OpenSSL.OpenSSL _ => ()) |
1046 | | ErrorMsg.Error => |
1047 | (print "Compilation error\n"; |
1048 | Msg.send (bio, MsgError "Error during configuration evaluation") |
1049 | handle OpenSSL.OpenSSL _ => ()); |
1050 | (cleanup (); |
1051 | ignore (OpenSSL.readChar bio); |
1052 | OpenSSL.close bio) |
1053 | handle OpenSSL.OpenSSL _ => (); |
1054 | loop ()) |
1055 | |
c8a739af |
1056 | fun doConfig codes = |
1057 | let |
1058 | val _ = print "Configuration:\n" |
1059 | val _ = app (fn s => (print s; print "\n")) codes |
1060 | val _ = print "\n" |
1061 | |
1062 | val outname = OS.FileSys.tmpName () |
1063 | |
1064 | fun doOne code = |
1065 | let |
1066 | val outf = TextIO.openOut outname |
1067 | in |
1068 | TextIO.output (outf, code); |
1069 | TextIO.closeOut outf; |
f92c6883 |
1070 | eval' outname |
c8a739af |
1071 | end |
1072 | in |
c45f1662 |
1073 | doIt (fn () => (Env.pre (); |
1074 | app doOne codes; |
1075 | Env.post (); |
1076 | Msg.send (bio, MsgOk); |
1077 | ("Configuration complete.", NONE))) |
1078 | (fn () => OS.FileSys.remove outname) |
c8a739af |
1079 | end |
1080 | |
bb8cc8c9 |
1081 | fun checkAddr s = |
1082 | case String.fields (fn ch => ch = #"@") s of |
1083 | [user'] => |
1084 | if user = user' then |
1085 | SOME (SetSA.User s) |
1086 | else |
1087 | NONE |
1088 | | [user', domain] => |
1089 | if Domain.validEmailUser user' andalso Domain.yourDomain domain then |
1090 | SOME (SetSA.Email s) |
1091 | else |
1092 | NONE |
1093 | | _ => NONE |
1094 | |
d330d9b8 |
1095 | fun cmdLoop () = |
1096 | case Msg.recv bio of |
1097 | NONE => (OpenSSL.close bio |
1098 | handle OpenSSL.OpenSSL _ => (); |
1099 | loop ()) |
1100 | | SOME m => |
1101 | case m of |
c8a739af |
1102 | MsgConfig code => doConfig [code] |
1103 | | MsgMultiConfig codes => doConfig codes |
e2130d9c |
1104 | |
4c68bd31 |
1105 | | MsgShutdown => |
93176f2b |
1106 | if Acl.query {user = user, class = "priv", value = "all"} |
1107 | orelse Acl.query {user = user, class = "priv", value = "shutdown"} then |
aef9c54f |
1108 | print ("Domtool dispatcher shutting down at " ^ now () ^ "\n\n") |
4c68bd31 |
1109 | else |
93176f2b |
1110 | (print "Unauthorized shutdown command!\n"; |
1111 | OpenSSL.close bio |
4c68bd31 |
1112 | handle OpenSSL.OpenSSL _ => (); |
1113 | loop ()) |
1114 | |
e2130d9c |
1115 | | MsgGrant acl => |
c45f1662 |
1116 | doIt (fn () => |
1117 | if Acl.query {user = user, class = "priv", value = "all"} then |
1118 | (Acl.grant acl; |
1119 | Acl.write Config.aclFile; |
1120 | ("Granted permission " ^ #value acl ^ " to " ^ #user acl ^ " in " ^ #class acl ^ ".", |
1121 | NONE)) |
1122 | else |
1123 | ("Unauthorized user asked to grant a permission!", |
1124 | SOME "Not authorized to grant privileges")) |
1125 | (fn () => ()) |
1126 | |
d1aa6a21 |
1127 | | MsgRevoke acl => |
c45f1662 |
1128 | doIt (fn () => |
1129 | if Acl.query {user = user, class = "priv", value = "all"} then |
1130 | (Acl.revoke acl; |
1131 | Acl.write Config.aclFile; |
1132 | ("Revoked permission " ^ #value acl ^ " from " ^ #user acl ^ " in " ^ #class acl ^ ".", |
1133 | NONE)) |
1134 | else |
1135 | ("Unauthorized user asked to revoke a permission!", |
1136 | SOME "Not authorized to revoke privileges")) |
1137 | (fn () => ()) |
e2130d9c |
1138 | |
646381db |
1139 | | MsgListPerms user => |
c45f1662 |
1140 | doIt (fn () => |
1141 | (Msg.send (bio, MsgPerms (Acl.queryAll user)); |
1142 | ("Sent permission list for user " ^ user ^ ".", |
1143 | NONE))) |
1144 | (fn () => ()) |
646381db |
1145 | |
d0e75410 |
1146 | | MsgWhoHas perm => |
c45f1662 |
1147 | doIt (fn () => |
1148 | (Msg.send (bio, MsgWhoHasResponse (Acl.whoHas perm)); |
1149 | ("Sent whohas response for " ^ #class perm ^ " / " ^ #value perm ^ ".", |
1150 | NONE))) |
1151 | (fn () => ()) |
d0e75410 |
1152 | |
aba1f07e |
1153 | | MsgRmdom doms => |
c45f1662 |
1154 | doIt (fn () => |
1155 | if Acl.query {user = user, class = "priv", value = "all"} |
1156 | orelse List.all (fn dom => Acl.query {user = user, class = "domain", value = dom}) doms then |
1157 | (Domain.rmdom doms; |
1158 | app (fn dom => |
1159 | Acl.revokeFromAll {class = "domain", value = dom}) doms; |
1160 | Acl.write Config.aclFile; |
1161 | ("Removed domains" ^ foldl (fn (d, s) => s ^ " " ^ d) "" doms ^ ".", |
1162 | NONE)) |
1163 | else |
1164 | ("Unauthorized user asked to remove a domain!", |
1165 | SOME "Not authorized to remove that domain")) |
1166 | (fn () => ()) |
f92c6883 |
1167 | |
1168 | | MsgRegenerate => |
c45f1662 |
1169 | doIt (fn () => |
1170 | if Acl.query {user = user, class = "priv", value = "regen"} |
1171 | orelse Acl.query {user = user, class = "priv", value = "all"} then |
b4f5896d |
1172 | (if regenerate context then |
1173 | ("Regenerated all configuration.", |
1174 | NONE) |
1175 | else |
1176 | ("Error regenerating configuration!", |
1177 | SOME "Error regenerating configuration! Consult /var/log/domtool.log.")) |
c45f1662 |
1178 | else |
1179 | ("Unauthorized user asked to regenerate!", |
1180 | SOME "Not authorized to regenerate")) |
1181 | (fn () => ()) |
aba1f07e |
1182 | |
b4f5896d |
1183 | | MsgRegenerateTc => |
1184 | doIt (fn () => |
1185 | if Acl.query {user = user, class = "priv", value = "regen"} |
1186 | orelse Acl.query {user = user, class = "priv", value = "all"} then |
1187 | (if regenerateTc context then |
1188 | ("Checked all configuration.", |
1189 | NONE) |
1190 | else |
1191 | ("Found a compilation error!", |
1192 | SOME "Found a compilation error! Consult /var/log/domtool.log.")) |
1193 | else |
1194 | ("Unauthorized user asked to regenerate -tc!", |
1195 | SOME "Not authorized to regenerate -tc")) |
1196 | (fn () => ()) |
1197 | |
6eee5b5c |
1198 | | MsgRmuser user' => |
c45f1662 |
1199 | doIt (fn () => |
1200 | if Acl.query {user = user, class = "priv", value = "all"} then |
1201 | (rmuser user'; |
1202 | Acl.write Config.aclFile; |
1203 | ("Removed user " ^ user' ^ ".", |
1204 | NONE)) |
1205 | else |
1206 | ("Unauthorized user asked to remove a user!", |
1207 | SOME "Not authorized to remove users")) |
1208 | (fn () => ()) |
634d7082 |
1209 | |
2bc895e7 |
1210 | | MsgCreateDbUser {dbtype, passwd} => |
c45f1662 |
1211 | doIt (fn () => |
1212 | case Dbms.lookup dbtype of |
1213 | NONE => ("Database user creation request with unknown datatype type " ^ dbtype, |
1214 | SOME ("Unknown database type " ^ dbtype)) |
1215 | | SOME handler => |
1216 | case #adduser handler {user = user, passwd = passwd} of |
1217 | NONE => ("Added " ^ dbtype ^ " user " ^ user ^ ".", |
1218 | NONE) |
1219 | | SOME msg => |
1220 | ("Error adding a " ^ dbtype ^ " user " ^ user ^ ": " ^ msg, |
1221 | SOME ("Error adding user: " ^ msg))) |
1222 | (fn () => ()) |
7d32cf2f |
1223 | |
dc6789fc |
1224 | | MsgDbPasswd {dbtype, passwd} => |
1225 | doIt (fn () => |
1226 | case Dbms.lookup dbtype of |
1227 | NONE => ("Database passwd request with unknown datatype type " ^ dbtype, |
1228 | SOME ("Unknown database type " ^ dbtype)) |
1229 | | SOME handler => |
1230 | case #passwd handler {user = user, passwd = passwd} of |
1231 | NONE => ("Changed " ^ dbtype ^ " password of user " ^ user ^ ".", |
1232 | NONE) |
1233 | | SOME msg => |
1234 | ("Error setting " ^ dbtype ^ " password of user " ^ user ^ ": " ^ msg, |
1235 | SOME ("Error adding user: " ^ msg))) |
1236 | (fn () => ()) |
1237 | |
d34cbcb8 |
1238 | | MsgCreateDbTable {dbtype, dbname} => |
c45f1662 |
1239 | doIt (fn () => |
1240 | if Dbms.validDbname dbname then |
1241 | case Dbms.lookup dbtype of |
1242 | NONE => ("Database creation request with unknown datatype type " ^ dbtype, |
1243 | SOME ("Unknown database type " ^ dbtype)) |
1244 | | SOME handler => |
1245 | case #createdb handler {user = user, dbname = dbname} of |
1246 | NONE => ("Created database " ^ user ^ "_" ^ dbname ^ ".", |
1247 | NONE) |
1248 | | SOME msg => ("Error creating database " ^ user ^ "_" ^ dbname ^ ": " ^ msg, |
1249 | SOME ("Error creating database: " ^ msg)) |
1250 | else |
1251 | ("Invalid database name " ^ user ^ "_" ^ dbname, |
1252 | SOME ("Invalid database name " ^ dbname))) |
1253 | (fn () => ()) |
1254 | |
0a58b2f3 |
1255 | | MsgListMailboxes domain => |
1256 | doIt (fn () => |
1257 | if not (Domain.yourDomain domain) then |
1258 | ("User wasn't authorized to list mailboxes for " ^ domain, |
1259 | SOME "You're not authorized to configure that domain.") |
1260 | else |
1261 | case Vmail.list domain of |
1262 | Vmail.Listing users => (Msg.send (bio, MsgMailboxes users); |
1263 | ("Sent mailbox list for " ^ domain, |
1264 | NONE)) |
1265 | | Vmail.Error msg => ("Error listing mailboxes for " ^ domain ^ ": " ^ msg, |
1266 | SOME msg)) |
1267 | (fn () => ()) |
1268 | |
c45f1662 |
1269 | | MsgNewMailbox {domain, user = emailUser, passwd, mailbox} => |
1270 | doIt (fn () => |
1271 | if not (Domain.yourDomain domain) then |
1272 | ("User wasn't authorized to add a mailbox to " ^ domain, |
1273 | SOME "You're not authorized to configure that domain.") |
bb8cc8c9 |
1274 | else if not (Domain.validEmailUser emailUser) then |
c45f1662 |
1275 | ("Invalid e-mail username " ^ emailUser, |
1276 | SOME "Invalid e-mail username") |
1277 | else if not (CharVector.all Char.isGraph passwd) then |
1278 | ("Invalid password", |
1279 | SOME "Invalid password; may only contain printable, non-space characters") |
1280 | else if not (Domain.yourPath mailbox) then |
1281 | ("User wasn't authorized to add a mailbox at " ^ mailbox, |
1282 | SOME "You're not authorized to use that mailbox location.") |
1283 | else |
1284 | case Vmail.add {requester = user, |
1285 | domain = domain, user = emailUser, |
1286 | passwd = passwd, mailbox = mailbox} of |
1287 | NONE => ("Added mailbox " ^ emailUser ^ "@" ^ domain ^ " at " ^ mailbox, |
1288 | NONE) |
87af74d0 |
1289 | | SOME msg => ("Error adding mailbox " ^ emailUser ^ "@" ^ domain ^ ": " ^ msg, |
c45f1662 |
1290 | SOME msg)) |
1291 | (fn () => ()) |
1292 | |
1293 | | MsgPasswdMailbox {domain, user = emailUser, passwd} => |
1294 | doIt (fn () => |
1295 | if not (Domain.yourDomain domain) then |
1296 | ("User wasn't authorized to change password of a mailbox for " ^ domain, |
1297 | SOME "You're not authorized to configure that domain.") |
bb8cc8c9 |
1298 | else if not (Domain.validEmailUser emailUser) then |
c45f1662 |
1299 | ("Invalid e-mail username " ^ emailUser, |
1300 | SOME "Invalid e-mail username") |
1301 | else if not (CharVector.all Char.isGraph passwd) then |
1302 | ("Invalid password", |
1303 | SOME "Invalid password; may only contain printable, non-space characters") |
1304 | else |
1305 | case Vmail.passwd {domain = domain, user = emailUser, |
1306 | passwd = passwd} of |
1307 | NONE => ("Changed password of mailbox " ^ emailUser ^ "@" ^ domain, |
1308 | NONE) |
87af74d0 |
1309 | | SOME msg => ("Error changing mailbox password for " ^ emailUser ^ "@" ^ domain ^ ": " ^ msg, |
c45f1662 |
1310 | SOME msg)) |
1311 | (fn () => ()) |
1312 | |
1313 | | MsgRmMailbox {domain, user = emailUser} => |
1314 | doIt (fn () => |
1315 | if not (Domain.yourDomain domain) then |
1316 | ("User wasn't authorized to change password of a mailbox for " ^ domain, |
1317 | SOME "You're not authorized to configure that domain.") |
bb8cc8c9 |
1318 | else if not (Domain.validEmailUser emailUser) then |
c45f1662 |
1319 | ("Invalid e-mail username " ^ emailUser, |
1320 | SOME "Invalid e-mail username") |
1321 | else |
1322 | case Vmail.rm {domain = domain, user = emailUser} of |
1323 | NONE => ("Deleted mailbox " ^ emailUser ^ "@" ^ domain, |
1324 | NONE) |
87af74d0 |
1325 | | SOME msg => ("Error deleting mailbox " ^ emailUser ^ "@" ^ domain ^ ": " ^ msg, |
c45f1662 |
1326 | SOME msg)) |
1327 | (fn () => ()) |
d34cbcb8 |
1328 | |
bb8cc8c9 |
1329 | | MsgSaQuery addr => |
1330 | doIt (fn () => |
1331 | case checkAddr addr of |
1332 | NONE => ("User tried to query SA filtering for " ^ addr, |
1333 | SOME "You aren't allowed to configure SA filtering for that recipient.") |
1334 | | SOME addr' => (Msg.send (bio, MsgSaStatus (SetSA.query addr')); |
1335 | ("Queried SA filtering status for " ^ addr, |
1336 | NONE))) |
1337 | (fn () => ()) |
1338 | |
1339 | | MsgSaSet (addr, b) => |
1340 | doIt (fn () => |
1341 | case checkAddr addr of |
1342 | NONE => ("User tried to set SA filtering for " ^ addr, |
1343 | SOME "You aren't allowed to configure SA filtering for that recipient.") |
1344 | | SOME addr' => (SetSA.set (addr', b); |
1345 | Msg.send (bio, MsgOk); |
1346 | ("Set SA filtering status for " ^ addr ^ " to " |
1347 | ^ (if b then "ON" else "OFF"), |
1348 | NONE))) |
1349 | (fn () => ()) |
1350 | |
c685120e |
1351 | | MsgSmtpLogReq domain => |
1352 | doIt (fn () => |
1353 | if not (Domain.yourDomain domain) then |
1354 | ("Unauthorized user tried to request SMTP logs for " ^ domain, |
1355 | SOME "You aren't authorized to configure that domain.") |
1356 | else |
1357 | (SmtpLog.search (fn line => Msg.send (bio, MsgSmtpLogRes line)) |
1358 | domain; |
1359 | ("Requested SMTP logs for " ^ domain, |
1360 | NONE))) |
1361 | (fn () => ()) |
1362 | |
e5f8cb7b |
1363 | | MsgQuery q => |
1364 | doIt (fn () => (Msg.send (bio, answerQuery q); |
1365 | (describeQuery q, |
8301e970 |
1366 | NONE))) |
1367 | (fn () => ()) |
1368 | |
d330d9b8 |
1369 | | _ => |
c45f1662 |
1370 | doIt (fn () => ("Unexpected command", |
1371 | SOME "Unexpected command")) |
1372 | (fn () => ()) |
d330d9b8 |
1373 | in |
1374 | cmdLoop () |
1375 | end |
da9abf26 |
1376 | handle e as (OpenSSL.OpenSSL s) => |
7e90e261 |
1377 | (print ("OpenSSL error: " ^ s ^ "\n"); |
da9abf26 |
1378 | app (fn x => print (x ^ "\n")) (SMLofNJ.exnHistory e); |
7e90e261 |
1379 | OpenSSL.close bio |
1380 | handle OpenSSL.OpenSSL _ => (); |
1381 | loop ()) |
1382 | | OS.SysErr (s, _) => |
1383 | (print ("System error: " ^ s ^ "\n"); |
1384 | OpenSSL.close bio |
1385 | handle OpenSSL.OpenSSL _ => (); |
1386 | loop ()) |
8a2bb410 |
1387 | | IO.Io {name, function, cause} => |
1388 | (print ("IO error: " ^ function ^ " for " ^ name ^ "\n"); |
1389 | app (fn x => print (x ^ "\n")) (SMLofNJ.exnHistory cause); |
1390 | OpenSSL.close bio |
1391 | handle OpenSSL.OpenSSL _ => (); |
1392 | loop ()) |
97d03e40 |
1393 | | e => |
e532c47d |
1394 | (print "Unknown exception in main loop!\n"; |
97d03e40 |
1395 | app (fn x => print (x ^ "\n")) (SMLofNJ.exnHistory e); |
e532c47d |
1396 | OpenSSL.close bio |
1397 | handle OpenSSL.OpenSSL _ => (); |
1398 | loop ()) |
d330d9b8 |
1399 | in |
aef9c54f |
1400 | print ("Domtool dispatcher starting up at " ^ now () ^ "\n"); |
0cfb3669 |
1401 | print "Listening for connections....\n"; |
d330d9b8 |
1402 | loop (); |
1403 | OpenSSL.shutdown sock |
1404 | end |
1405 | |
1406 | fun slave () = |
1407 | let |
f58a3627 |
1408 | val host = Slave.hostname () |
d330d9b8 |
1409 | |
3d55b046 |
1410 | val context = context (Config.certDir ^ "/" ^ host ^ ".pem", |
1411 | Config.keyDir ^ "/" ^ host ^ "/key.pem", |
1412 | Config.trustStore) |
d330d9b8 |
1413 | |
1414 | val sock = OpenSSL.listen (context, Config.slavePort) |
1415 | |
aef9c54f |
1416 | val _ = print ("Slave server starting at " ^ now () ^ "\n") |
1417 | |
d330d9b8 |
1418 | fun loop () = |
1419 | case OpenSSL.accept sock of |
1420 | NONE => () |
1421 | | SOME bio => |
1422 | let |
1423 | val peer = OpenSSL.peerCN bio |
aef9c54f |
1424 | val () = print ("\nConnection from " ^ peer ^ " at " ^ now () ^ "\n") |
2569e66d |
1425 | in |
aef9c54f |
1426 | if peer = Config.dispatcherName then let |
d330d9b8 |
1427 | fun loop' files = |
1428 | case Msg.recv bio of |
1429 | NONE => print "Dispatcher closed connection unexpectedly\n" |
1430 | | SOME m => |
1431 | case m of |
1432 | MsgFile file => loop' (file :: files) |
1433 | | MsgDoFiles => (Slave.handleChanges files; |
1434 | Msg.send (bio, MsgOk)) |
0ea0ecfa |
1435 | | MsgRegenerate => (Domain.resetLocal (); |
1436 | Msg.send (bio, MsgOk)) |
d330d9b8 |
1437 | | _ => (print "Dispatcher sent unexpected command\n"; |
1438 | Msg.send (bio, MsgError "Unexpected command")) |
1439 | in |
1440 | loop' []; |
1441 | ignore (OpenSSL.readChar bio); |
1442 | OpenSSL.close bio; |
1443 | loop () |
1444 | end |
aef9c54f |
1445 | else if peer = "domtool" then |
1446 | case Msg.recv bio of |
1447 | SOME MsgShutdown => (OpenSSL.close bio; |
1448 | print ("Shutting down at " ^ now () ^ "\n\n")) |
1449 | | _ => (OpenSSL.close bio; |
1450 | loop ()) |
1451 | else |
e5f8cb7b |
1452 | case Msg.recv bio of |
1453 | SOME (MsgQuery q) => (print (describeQuery q ^ "\n"); |
1454 | Msg.send (bio, answerQuery q); |
1455 | ignore (OpenSSL.readChar bio); |
1456 | OpenSSL.close bio; |
1457 | loop ()) |
1458 | | _ => (OpenSSL.close bio; |
1459 | loop ()) |
91c5a390 |
1460 | end handle OpenSSL.OpenSSL s => |
1461 | (print ("OpenSSL error: "^ s ^ "\n"); |
1462 | OpenSSL.close bio |
1463 | handle OpenSSL.OpenSSL _ => (); |
1464 | loop ()) |
3c9daec4 |
1465 | | e as OS.SysErr (s, _) => |
1466 | (app (fn s => print (s ^ "\n")) (SMLofNJ.exnHistory e); |
1467 | print ("System error: "^ s ^ "\n"); |
1d2fd26b |
1468 | OpenSSL.close bio |
1469 | handle OpenSSL.OpenSSL _ => (); |
1470 | loop ()) |
904eb905 |
1471 | in |
2569e66d |
1472 | loop (); |
1473 | OpenSSL.shutdown sock |
904eb905 |
1474 | end |
1475 | |
e26d6b6e |
1476 | fun listBasis () = |
91c5a390 |
1477 | let |
1478 | val dir = Posix.FileSys.opendir Config.libRoot |
1479 | |
1480 | fun loop files = |
1481 | case Posix.FileSys.readdir dir of |
1482 | NONE => (Posix.FileSys.closedir dir; |
1483 | files) |
1484 | | SOME fname => |
1485 | if String.isSuffix ".dtl" fname then |
1486 | loop (OS.Path.joinDirFile {dir = Config.libRoot, |
1487 | file = fname} |
1488 | :: files) |
1489 | else |
1490 | loop files |
91c5a390 |
1491 | in |
e26d6b6e |
1492 | loop [] |
91c5a390 |
1493 | end |
1494 | |
e26d6b6e |
1495 | fun autodocBasis outdir = |
1496 | Autodoc.autodoc {outdir = outdir, infiles = listBasis ()} |
1497 | |
e680130a |
1498 | end |