Apache log directory creation
[hcoop/domtool2.git] / src / plugins / apache.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 (* Apache HTTPD handling *)
20
21 structure Apache :> APACHE = struct
22
23 open Ast
24
25 val _ = Env.type_one "proxy_port"
26 Env.int
27 (fn n => n > 1024)
28
29 val _ = Env.type_one "proxy_target"
30 Env.string
31 (fn s =>
32 let
33 fun default () = List.exists (fn s' => s = s') Config.Apache.proxyTargets
34 in
35 case String.fields (fn ch => ch = #":") s of
36 ["http", "//localhost", rest] =>
37 (case String.fields (fn ch => ch = #"/") rest of
38 port :: _ =>
39 (case Int.fromString port of
40 NONE => default ()
41 | SOME n => n > 1024 orelse default ())
42 | _ => default ())
43 | _ => default ()
44 end)
45
46 val _ = Env.type_one "rewrite_arg"
47 Env.string
48 (CharVector.all Char.isAlphaNum)
49
50 fun validLocation s =
51 size s > 0 andalso size s < 1000 andalso CharVector.all
52 (fn ch => Char.isAlphaNum ch
53 orelse ch = #"-"
54 orelse ch = #"_"
55 orelse ch = #"."
56 orelse ch = #"/") s
57
58 val _ = Env.type_one "location"
59 Env.string
60 validLocation
61
62 val dl = ErrorMsg.dummyLoc
63
64 val _ = Defaults.registerDefault ("WebNodes",
65 (TList (TBase "node", dl), dl),
66 (fn () => (EList (map (fn s => (EString s, dl)) Config.Apache.webNodes), dl)))
67
68 val _ = Defaults.registerDefault ("SSL",
69 (TBase "bool", dl),
70 (fn () => (EVar "false", dl)))
71
72 val _ = Defaults.registerDefault ("User",
73 (TBase "your_user", dl),
74 (fn () => (EString (Domain.getUser ()), dl)))
75
76 val _ = Defaults.registerDefault ("Group",
77 (TBase "your_group", dl),
78 (fn () => (EString (Domain.getUser ()), dl)))
79
80 val _ = Defaults.registerDefault ("DocumentRoot",
81 (TBase "your_path", dl),
82 (fn () => (EString (Config.homeBase ^ "/" ^ Domain.getUser () ^ "/public_html"), dl)))
83
84 val _ = Defaults.registerDefault ("ServerAdmin",
85 (TBase "email", dl),
86 (fn () => (EString (Domain.getUser () ^ "@" ^ Config.defaultDomain), dl)))
87
88
89 val redirect_code = fn (EVar "temp", _) => SOME "temp"
90 | (EVar "permanent", _) => SOME "permanent"
91 | (EVar "seeother", _) => SOME "seeother"
92 | (EVar "redir300", _) => SOME "300"
93 | (EVar "redir301", _) => SOME "301"
94 | (EVar "redir302", _) => SOME "302"
95 | (EVar "redir303", _) => SOME "303"
96 | (EVar "redir304", _) => SOME "304"
97 | (EVar "redir305", _) => SOME "305"
98 | (EVar "redir307", _) => SOME "307"
99 | _ => NONE
100
101 val flag = fn (EVar "redirect", _) => SOME "R"
102 | (EVar "forbidden", _) => SOME "F"
103 | (EVar "gone", _) => SOME "G"
104 | (EVar "last", _) => SOME "L"
105 | (EVar "chain", _) => SOME "C"
106 | (EVar "nosubreq", _) => SOME "NS"
107 | (EVar "nocase", _) => SOME "NC"
108 | (EVar "qsappend", _) => SOME "QSA"
109 | (EVar "noescape", _) => SOME "NE"
110 | (EVar "passthrough", _) => SOME "PT"
111 | (EApp ((EVar "mimeType", _), e), _) =>
112 Option.map (fn s => "T=" ^ s) (Env.string e)
113 | (EApp ((EVar "redirectWith", _), e), _) =>
114 Option.map (fn s => "R=" ^ s) (redirect_code e)
115 | (EApp ((EVar "skip", _), e), _) =>
116 Option.map (fn n => "S=" ^ Int.toString n) (Env.int e)
117 | (EApp ((EApp ((EVar "env", _), e1), _), e2), _) =>
118 (case Env.string e1 of
119 NONE => NONE
120 | SOME s1 => Option.map (fn s2 => "E=" ^ s1 ^ ":" ^ s2)
121 (Env.string e2))
122
123 | _ => NONE
124
125 val cond_flag = fn (EVar "cond_nocase", _) => SOME "NC"
126 | (EVar "ornext", _) => SOME "OR"
127 | _ => NONE
128
129 val apache_option = fn (EVar "execCGI", _) => SOME "ExecCGI"
130 | (EVar "includesNOEXEC", _) => SOME "IncludesNOEXEC"
131 | (EVar "indexes", _) => SOME "Indexes"
132 | _ => NONE
133
134 val autoindex_width = fn (EVar "autofit", _) => SOME "*"
135 | (EApp ((EVar "characters", _), n), _) =>
136 Option.map Int.toString (Env.int n)
137 | _ => NONE
138
139 val autoindex_option = fn (EApp ((EVar "descriptionWidth", _), w), _) =>
140 Option.map (fn w => ("DescriptionWidth", SOME w))
141 (autoindex_width w)
142 | (EVar "fancyIndexing", _) => SOME ("FancyIndexing", NONE)
143 | (EVar "foldersFirst", _) => SOME ("FoldersFirst", NONE)
144 | (EVar "htmlTable", _) => SOME ("HTMLTable", NONE)
145 | (EVar "iconsAreLinks", _) => SOME ("IconsAreLinks", NONE)
146 | (EApp ((EVar "iconHeight", _), n), _) =>
147 Option.map (fn w => ("IconHeight", SOME (Int.toString w)))
148 (Env.int n)
149 | (EApp ((EVar "iconWidth", _), n), _) =>
150 Option.map (fn w => ("IconWidth", SOME (Int.toString w)))
151 (Env.int n)
152 | (EVar "ignoreCase", _) => SOME ("IgnoreCase", NONE)
153 | (EVar "ignoreClient", _) => SOME ("IgnoreClient", NONE)
154 | (EApp ((EVar "nameWidth", _), w), _) =>
155 Option.map (fn w => ("NameWidth", SOME w))
156 (autoindex_width w)
157 | (EVar "scanHtmlTitles", _) => SOME ("ScanHTMLTitles", NONE)
158 | (EVar "suppressColumnSorting", _) => SOME ("SuppressColumnSorting", NONE)
159 | (EVar "suppressDescription", _) => SOME ("SuppressDescription", NONE)
160 | (EVar "suppressHtmlPreamble", _) => SOME ("SuppressHTMLPreamble", NONE)
161 | (EVar "suppressIcon", _) => SOME ("SuppressIcon", NONE)
162 | (EVar "suppressLastModified", _) => SOME ("SuppressLastModified", NONE)
163 | (EVar "suppressRules", _) => SOME ("SuppressRules", NONE)
164 | (EVar "suppressSize", _) => SOME ("SuppressSize", NONE)
165 | (EVar "trackModified", _) => SOME ("TrackModified", NONE)
166 | (EVar "versionSort", _) => SOME ("VersionSort", NONE)
167 | (EVar "xhtml", _) => SOME ("XHTML", NONE)
168
169 | _ => NONE
170
171 val vhostsChanged = ref false
172
173 val () = Slave.registerPreHandler
174 (fn () => vhostsChanged := false)
175
176 fun findVhostUser fname =
177 let
178 val inf = TextIO.openIn fname
179
180 fun loop () =
181 case TextIO.inputLine inf of
182 NONE => NONE
183 | SOME line =>
184 case String.tokens Char.isSpace line of
185 ["SuexecUserGroup", user, _] => SOME user
186 | _ => loop ()
187 in
188 loop ()
189 before TextIO.closeIn inf
190 end
191
192 val () = Slave.registerFileHandler (fn fs =>
193 case findVhostUser (#file fs) of
194 NONE => print ("Can't find user in " ^ #file fs ^ "! Taking no action.\n")
195 | SOME user =>
196 let
197 val spl = OS.Path.splitDirFile (#file fs)
198 in
199 if String.isSuffix ".vhost" (#file spl)
200 orelse String.isSuffix ".vhost_ssl" (#file spl) then
201 (vhostsChanged := true;
202 case #action fs of
203 Slave.Delete =>
204 let
205 val {base, ...} = OS.Path.splitBaseExt (#file spl)
206
207 val logname = OS.Path.joinDirFile
208 {dir = Config.Apache.logDir,
209 file = user}
210 in
211 ignore (OS.Process.system (Config.rm
212 ^ " -rf "
213 ^ Config.Apache.confDir
214 ^ "/"
215 ^ #file spl));
216 Slave.writeList (logname,
217 List.filter (fn s => s <> base)
218 (Slave.readList logname))
219 end
220
221 | Slave.Add =>
222 let
223 val _ =
224 OS.Process.system (Config.cp
225 ^ " "
226 ^ #file fs
227 ^ " "
228 ^ Config.Apache.confDir
229 ^ "/"
230 ^ #file spl)
231
232 val {base, ...} = OS.Path.splitBaseExt (#file spl)
233
234 val logname = OS.Path.joinDirFile
235 {dir = Config.Apache.logDir,
236 file = user}
237
238 val outf = TextIO.openAppend logname
239 in
240 TextIO.output (outf, base);
241 TextIO.output1 (outf, #"\n");
242 TextIO.closeOut outf
243 end
244
245 | _ =>
246 ignore (OS.Process.system (Config.cp
247 ^ " "
248 ^ #file fs
249 ^ " "
250 ^ Config.Apache.confDir
251 ^ "/"
252 ^ #file spl)))
253 else
254 ()
255 end)
256
257 val () = Slave.registerPostHandler
258 (fn () =>
259 (if !vhostsChanged then
260 Slave.shellF ([Config.Apache.reload],
261 fn cl => "Error reloading Apache with " ^ cl)
262 else
263 ()))
264
265 val vhostFiles : TextIO.outstream list ref = ref []
266 fun write s = app (fn file => TextIO.output (file, s)) (!vhostFiles)
267
268 val rewriteEnabled = ref false
269 val currentVhost = ref ""
270 val currentVhostId = ref ""
271
272 val pre = ref (fn _ : {nodes : string list, id : string, hostname : string} => ())
273 fun registerPre f =
274 let
275 val old = !pre
276 in
277 pre := (fn x => (old x; f x))
278 end
279
280 val post = ref (fn () => ())
281 fun registerPost f =
282 let
283 val old = !post
284 in
285 post := (fn () => (old (); f ()))
286 end
287
288 val aliaser = ref (fn _ : string => ())
289 fun registerAliaser f =
290 let
291 val old = !aliaser
292 in
293 aliaser := (fn x => (old x; f x))
294 end
295
296 val () = Env.containerV_one "vhost"
297 ("host", Env.string)
298 (fn (env, host) =>
299 let
300 val nodes = Env.env (Env.list Env.string) (env, "WebNodes")
301
302 val ssl = Env.env Env.bool (env, "SSL")
303 val user = Env.env Env.string (env, "User")
304 val group = Env.env Env.string (env, "Group")
305 val docroot = Env.env Env.string (env, "DocumentRoot")
306 val sadmin = Env.env Env.string (env, "ServerAdmin")
307
308 val fullHost = host ^ "." ^ Domain.currentDomain ()
309 val vhostId = fullHost ^ (if ssl then ".ssl" else "")
310 val confFile = fullHost ^ (if ssl then ".vhost_ssl" else ".vhost")
311 in
312 currentVhost := fullHost;
313 currentVhostId := vhostId;
314
315 rewriteEnabled := false;
316 vhostFiles := map (fn node =>
317 let
318 val file = Domain.domainFile {node = node,
319 name = confFile}
320 in
321 TextIO.output (file, "<VirtualHost ");
322 TextIO.output (file, Domain.nodeIp node);
323 TextIO.output (file, ":");
324 TextIO.output (file, if ssl then
325 "443"
326 else
327 "80");
328 TextIO.output (file, ">\n");
329 file
330 end)
331 nodes;
332 write "\tServerName ";
333 write fullHost;
334 write "\n\tSuexecUserGroup ";
335 write user;
336 write " ";
337 write group;
338 write "\n\tDocumentRoot ";
339 write docroot;
340 write "\n\tServerAdmin ";
341 write sadmin;
342 write "\n\tErrorLog ";
343 write Config.Apache.logDir;
344 write "/";
345 write vhostId;
346 write "/error.log\n\tCustomLog ";
347 write Config.Apache.logDir;
348 write "/";
349 write vhostId;
350 write "/access.log combined\n";
351 !pre {nodes = nodes, id = vhostId, hostname = fullHost}
352 end,
353 fn () => (!post ();
354 write "</VirtualHost>\n";
355 app TextIO.closeOut (!vhostFiles)))
356
357 val () = Env.container_one "location"
358 ("prefix", Env.string)
359 (fn prefix =>
360 (write "\t<Location ";
361 write prefix;
362 write ">\n"),
363 fn () => write "\t</Location>\n")
364
365 val () = Env.container_one "directory"
366 ("directory", Env.string)
367 (fn directory =>
368 (write "\t<Directory ";
369 write directory;
370 write ">\n"),
371 fn () => write "\t</Directory>\n")
372
373 fun checkRewrite () =
374 if !rewriteEnabled then
375 ()
376 else
377 (write "\tRewriteEngine on\n";
378 rewriteEnabled := true)
379
380 val () = Env.action_three "localProxyRewrite"
381 ("from", Env.string, "to", Env.string, "port", Env.int)
382 (fn (from, to, port) =>
383 (checkRewrite ();
384 write "\tRewriteRule\t";
385 write from;
386 write "\thttp://localhost:";
387 write (Int.toString port);
388 write "/";
389 write to;
390 write " [P]\n"))
391
392 val () = Env.action_two "proxyPass"
393 ("from", Env.string, "to", Env.string)
394 (fn (from, to) =>
395 (write "\tProxyPass\t";
396 write from;
397 write "\t";
398 write to;
399 write "\n"))
400
401 val () = Env.action_two "proxyPassReverse"
402 ("from", Env.string, "to", Env.string)
403 (fn (from, to) =>
404 (write "\tProxyPassReverse\t";
405 write from;
406 write "\t";
407 write to;
408 write "\n"))
409
410 val () = Env.action_three "rewriteRule"
411 ("from", Env.string, "to", Env.string, "flags", Env.list flag)
412 (fn (from, to, flags) =>
413 (checkRewrite ();
414 write "\tRewriteRule\t";
415 write from;
416 write "\t";
417 write to;
418 case flags of
419 [] => ()
420 | flag::rest => (write " [";
421 write flag;
422 app (fn flag => (write ",";
423 write flag)) rest;
424 write "]");
425 write "\n"))
426
427 val () = Env.action_three "rewriteCond"
428 ("test", Env.string, "pattern", Env.string, "flags", Env.list cond_flag)
429 (fn (from, to, flags) =>
430 (checkRewrite ();
431 write "\tRewriteCond\t";
432 write from;
433 write "\t";
434 write to;
435 case flags of
436 [] => ()
437 | flag::rest => (write " [";
438 write flag;
439 app (fn flag => (write ",";
440 write flag)) rest;
441 write "]");
442 write "\n"))
443
444 val () = Env.action_one "rewriteLogLevel"
445 ("level", Env.int)
446 (fn level =>
447 (checkRewrite ();
448 write "\tRewriteLog ";
449 write Config.Apache.logDir;
450 write "/";
451 write (!currentVhostId);
452 write "/rewrite.log\n\tRewriteLogLevel ";
453 write (Int.toString level);
454 write "\n"))
455
456 val () = Env.action_two "alias"
457 ("from", Env.string, "to", Env.string)
458 (fn (from, to) =>
459 (write "\tAlias\t";
460 write from;
461 write " ";
462 write to;
463 write "\n"))
464
465 val () = Env.action_two "scriptAlias"
466 ("from", Env.string, "to", Env.string)
467 (fn (from, to) =>
468 (write "\tScriptAlias\t";
469 write from;
470 write " ";
471 write to;
472 write "\n"))
473
474 val () = Env.action_two "errorDocument"
475 ("code", Env.string, "handler", Env.string)
476 (fn (code, handler) =>
477 (write "\tErrorDocument\t";
478 write code;
479 write " ";
480 write handler;
481 write "\n"))
482
483 val () = Env.action_one "options"
484 ("options", Env.list apache_option)
485 (fn opts =>
486 case opts of
487 [] => ()
488 | _ => (write "\tOptions";
489 app (fn opt => (write " "; write opt)) opts;
490 write "\n"))
491
492 val () = Env.action_one "set_options"
493 ("options", Env.list apache_option)
494 (fn opts =>
495 case opts of
496 [] => ()
497 | _ => (write "\tOptions";
498 app (fn opt => (write " +"; write opt)) opts;
499 write "\n"))
500
501 val () = Env.action_one "unset_options"
502 ("options", Env.list apache_option)
503 (fn opts =>
504 case opts of
505 [] => ()
506 | _ => (write "\tOptions";
507 app (fn opt => (write " -"; write opt)) opts;
508 write "\n"))
509
510 val () = Env.action_one "directoryIndex"
511 ("filenames", Env.list Env.string)
512 (fn opts =>
513 (write "\tDirectoryIndex";
514 app (fn opt => (write " "; write opt)) opts;
515 write "\n"))
516
517 val () = Env.action_one "serverAlias"
518 ("host", Env.string)
519 (fn host =>
520 (write "\tServerAlias ";
521 write host;
522 write "\n";
523 !aliaser host))
524
525 val authType = fn (EVar "basic", _) => SOME "basic"
526 | (EVar "digest", _) => SOME "digest"
527 | _ => NONE
528
529 val () = Env.action_one "authType"
530 ("type", authType)
531 (fn ty =>
532 (write "\tAuthType ";
533 write ty;
534 write "\n"))
535
536 val () = Env.action_one "authName"
537 ("name", Env.string)
538 (fn name =>
539 (write "\tAuthName \"";
540 write name;
541 write "\"\n"))
542
543 val () = Env.action_one "authUserFile"
544 ("file", Env.string)
545 (fn name =>
546 (write "\tAuthUserFile ";
547 write name;
548 write "\n"))
549
550 val () = Env.action_none "requireValidUser"
551 (fn () => write "\tRequire valid-user\n")
552
553 val () = Env.action_one "requireUser"
554 ("users", Env.list Env.string)
555 (fn names =>
556 case names of
557 [] => ()
558 | _ => (write "\tRequire user";
559 app (fn name => (write " "; write name)) names;
560 write "\n"))
561
562 val () = Env.action_one "requireGroup"
563 ("groups", Env.list Env.string)
564 (fn names =>
565 case names of
566 [] => ()
567 | _ => (write "\tRequire group";
568 app (fn name => (write " "; write name)) names;
569 write "\n"))
570
571 val () = Env.action_none "orderAllowDeny"
572 (fn () => write "\tOrder allow,deny\n")
573
574 val () = Env.action_none "orderDenyAllow"
575 (fn () => write "\tOrder deny,allow\n")
576
577 val () = Env.action_none "allowFromAll"
578 (fn () => write "\tAllow from all\n")
579
580 val () = Env.action_one "allowFrom"
581 ("entries", Env.list Env.string)
582 (fn names =>
583 case names of
584 [] => ()
585 | _ => (write "\tAllow from";
586 app (fn name => (write " "; write name)) names;
587 write "\n"))
588
589 val () = Env.action_none "denyFromAll"
590 (fn () => write "\tDeny from all\n")
591
592 val () = Env.action_one "denyFrom"
593 ("entries", Env.list Env.string)
594 (fn names =>
595 case names of
596 [] => ()
597 | _ => (write "\tDeny from";
598 app (fn name => (write " "; write name)) names;
599 write "\n"))
600
601 val () = Env.action_none "satisfyAll"
602 (fn () => write "\tSatisfy all\n")
603
604 val () = Env.action_none "satisfyAny"
605 (fn () => write "\tSatisfy any\n")
606
607 val () = Env.action_one "forceType"
608 ("type", Env.string)
609 (fn ty => (write "\tForceType ";
610 write ty;
611 write "\n"))
612
613 val () = Env.action_none "forceTypeOff"
614 (fn () => write "\tForceType None\n")
615
616 val () = Env.action_two "action"
617 ("what", Env.string, "how", Env.string)
618 (fn (what, how) => (write "\tAction ";
619 write what;
620 write " ";
621 write how;
622 write "\n"))
623
624 val () = Env.action_one "addDefaultCharset"
625 ("charset", Env.string)
626 (fn ty => (write "\tAddDefaultCharset ";
627 write ty;
628 write "\n"))
629
630 val () = Env.action_one "davSvn"
631 ("path", Env.string)
632 (fn path => (write "\tDAV svn\n\tSVNPath ";
633 write path;
634 write "\n"))
635
636 val () = Env.action_one "authzSvnAccessFile"
637 ("path", Env.string)
638 (fn path => (write "\tAuthzSVNAccessFile ";
639 write path;
640 write "\n"))
641
642 val () = Env.action_two "addDescription"
643 ("description", Env.string, "patterns", Env.list Env.string)
644 (fn (desc, pats) =>
645 case pats of
646 [] => ()
647 | _ => (write "\tAddDescription \"";
648 write (String.toString desc);
649 write "\"";
650 app (fn pat => (write " "; write pat)) pats;
651 write "\n"))
652
653 val () = Env.action_one "indexOptions"
654 ("options", Env.list autoindex_option)
655 (fn opts =>
656 case opts of
657 [] => ()
658 | _ => (write "\tIndexOptions";
659 app (fn (opt, arg) =>
660 (write " ";
661 write opt;
662 Option.app (fn arg =>
663 (write "="; write arg)) arg)) opts;
664 write "\n"))
665
666 val () = Env.action_one "set_indexOptions"
667 ("options", Env.list autoindex_option)
668 (fn opts =>
669 case opts of
670 [] => ()
671 | _ => (write "\tIndexOptions";
672 app (fn (opt, arg) =>
673 (write " +";
674 write opt;
675 Option.app (fn arg =>
676 (write "="; write arg)) arg)) opts;
677 write "\n"))
678
679 val () = Env.action_one "unset_indexOptions"
680 ("options", Env.list autoindex_option)
681 (fn opts =>
682 case opts of
683 [] => ()
684 | _ => (write "\tIndexOptions";
685 app (fn (opt, _) =>
686 (write " -";
687 write opt)) opts;
688 write "\n"))
689
690 val () = Env.action_one "headerName"
691 ("name", Env.string)
692 (fn name => (write "\tHeaderName ";
693 write name;
694 write "\n"))
695
696 val () = Env.action_one "readmeName"
697 ("name", Env.string)
698 (fn name => (write "\tReadmeName ";
699 write name;
700 write "\n"))
701
702 end