Changing Apache log and Webalizer stuff
[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 let
194 val spl = OS.Path.splitDirFile (#file fs)
195 in
196 if String.isSuffix ".vhost" (#file spl)
197 orelse String.isSuffix ".vhost_ssl" (#file spl) then
198 case findVhostUser (#file fs) of
199 NONE => print ("Can't find user in " ^ #file fs ^ "! Taking no action.\n")
200 | SOME user =>
201 let
202 val realVhostFile = OS.Path.joinDirFile
203 {dir = Config.Apache.confDir,
204 file = #file spl}
205
206 val realLogDir = OS.Path.joinDirFile
207 {dir = Config.homeBase,
208 file = user}
209 val realLogDir = OS.Path.joinDirFile
210 {dir = realLogDir,
211 file = "apache"}
212 val realLogDir = OS.Path.joinDirFile
213 {dir = realLogDir,
214 file = "log"}
215 val realLogDir = OS.Path.joinDirFile
216 {dir = realLogDir,
217 file = Slave.hostname ()}
218 val {base, ...} = OS.Path.splitBaseExt (#file spl)
219 val realLogDir = OS.Path.joinDirFile
220 {dir = realLogDir,
221 file = base}
222 in
223 vhostsChanged := true;
224 case #action fs of
225 Slave.Delete =>
226 (ignore (OS.Process.system (Config.rm
227 ^ " -rf "
228 ^ realVhostFile));
229 ignore (OS.Process.system (Config.rm
230 ^ " -rf "
231 ^ realLogDir)))
232 | Slave.Add =>
233 (ignore (OS.Process.system (Config.cp
234 ^ " "
235 ^ #file fs
236 ^ " "
237 ^ realVhostFile));
238 OS.FileSys.mkDir realLogDir)
239
240 | _ =>
241 ignore (OS.Process.system (Config.cp
242 ^ " "
243 ^ #file fs
244 ^ " "
245 ^ realVhostFile))
246 end
247 else
248 ()
249 end)
250
251 val () = Slave.registerPostHandler
252 (fn () =>
253 (if !vhostsChanged then
254 Slave.shellF ([Config.Apache.reload],
255 fn cl => "Error reloading Apache with " ^ cl)
256 else
257 ()))
258
259 val vhostFiles : (string * TextIO.outstream) list ref = ref []
260 fun write' s = app (fn (node, file) => TextIO.output (file, s node)) (!vhostFiles)
261 fun write s = app (fn (_, file) => TextIO.output (file, s)) (!vhostFiles)
262
263 val rewriteEnabled = ref false
264 val currentVhost = ref ""
265 val currentVhostId = ref ""
266
267 val pre = ref (fn _ : {user : string, nodes : string list, id : string, hostname : string} => ())
268 fun registerPre f =
269 let
270 val old = !pre
271 in
272 pre := (fn x => (old x; f x))
273 end
274
275 val post = ref (fn () => ())
276 fun registerPost f =
277 let
278 val old = !post
279 in
280 post := (fn () => (old (); f ()))
281 end
282
283 val aliaser = ref (fn _ : string => ())
284 fun registerAliaser f =
285 let
286 val old = !aliaser
287 in
288 aliaser := (fn x => (old x; f x))
289 end
290
291 val () = Env.containerV_one "vhost"
292 ("host", Env.string)
293 (fn (env, host) =>
294 let
295 val nodes = Env.env (Env.list Env.string) (env, "WebNodes")
296
297 val ssl = Env.env Env.bool (env, "SSL")
298 val user = Env.env Env.string (env, "User")
299 val group = Env.env Env.string (env, "Group")
300 val docroot = Env.env Env.string (env, "DocumentRoot")
301 val sadmin = Env.env Env.string (env, "ServerAdmin")
302
303 val fullHost = host ^ "." ^ Domain.currentDomain ()
304 val vhostId = fullHost ^ (if ssl then ".ssl" else "")
305 val confFile = fullHost ^ (if ssl then ".vhost_ssl" else ".vhost")
306 in
307 currentVhost := fullHost;
308 currentVhostId := vhostId;
309
310 rewriteEnabled := false;
311 vhostFiles := map (fn node =>
312 let
313 val file = Domain.domainFile {node = node,
314 name = confFile}
315 in
316 TextIO.output (file, "<VirtualHost ");
317 TextIO.output (file, Domain.nodeIp node);
318 TextIO.output (file, ":");
319 TextIO.output (file, if ssl then
320 "443"
321 else
322 "80");
323 TextIO.output (file, ">\n");
324 TextIO.output (file, "\tErrorLog ");
325 TextIO.output (file, Config.homeBase);
326 TextIO.output (file, "/");
327 TextIO.output (file, user);
328 TextIO.output (file, "/apache/log/");
329 TextIO.output (file, node);
330 TextIO.output (file, "/");
331 TextIO.output (file, vhostId);
332 TextIO.output (file, "/error.log\n\tCustomLog ");
333 TextIO.output (file, Config.homeBase);
334 TextIO.output (file, "/");
335 TextIO.output (file, user);
336 TextIO.output (file, "/apache/log/");
337 TextIO.output (file, node);
338 TextIO.output (file, "/");
339 TextIO.output (file, vhostId);
340 TextIO.output (file, "/access.log combined\n");
341 (Config.homeBase ^ "/" ^ user ^ "/apache/log/"
342 ^ node ^ "/" ^ vhostId, file)
343 end)
344 nodes;
345 write "\tServerName ";
346 write fullHost;
347 write "\n\tSuexecUserGroup ";
348 write user;
349 write " ";
350 write group;
351 write "\n\tDocumentRoot ";
352 write docroot;
353 write "\n\tServerAdmin ";
354 write sadmin;
355 write "\n";
356 !pre {user = user, nodes = nodes, id = vhostId, hostname = fullHost}
357 end,
358 fn () => (!post ();
359 write "</VirtualHost>\n";
360 app (TextIO.closeOut o #2) (!vhostFiles)))
361
362 val () = Env.container_one "location"
363 ("prefix", Env.string)
364 (fn prefix =>
365 (write "\t<Location ";
366 write prefix;
367 write ">\n"),
368 fn () => write "\t</Location>\n")
369
370 val () = Env.container_one "directory"
371 ("directory", Env.string)
372 (fn directory =>
373 (write "\t<Directory ";
374 write directory;
375 write ">\n"),
376 fn () => write "\t</Directory>\n")
377
378 fun checkRewrite () =
379 if !rewriteEnabled then
380 ()
381 else
382 (write "\tRewriteEngine on\n";
383 rewriteEnabled := true)
384
385 val () = Env.action_three "localProxyRewrite"
386 ("from", Env.string, "to", Env.string, "port", Env.int)
387 (fn (from, to, port) =>
388 (checkRewrite ();
389 write "\tRewriteRule\t";
390 write from;
391 write "\thttp://localhost:";
392 write (Int.toString port);
393 write "/";
394 write to;
395 write " [P]\n"))
396
397 val () = Env.action_two "proxyPass"
398 ("from", Env.string, "to", Env.string)
399 (fn (from, to) =>
400 (write "\tProxyPass\t";
401 write from;
402 write "\t";
403 write to;
404 write "\n"))
405
406 val () = Env.action_two "proxyPassReverse"
407 ("from", Env.string, "to", Env.string)
408 (fn (from, to) =>
409 (write "\tProxyPassReverse\t";
410 write from;
411 write "\t";
412 write to;
413 write "\n"))
414
415 val () = Env.action_three "rewriteRule"
416 ("from", Env.string, "to", Env.string, "flags", Env.list flag)
417 (fn (from, to, flags) =>
418 (checkRewrite ();
419 write "\tRewriteRule\t";
420 write from;
421 write "\t";
422 write to;
423 case flags of
424 [] => ()
425 | flag::rest => (write " [";
426 write flag;
427 app (fn flag => (write ",";
428 write flag)) rest;
429 write "]");
430 write "\n"))
431
432 val () = Env.action_three "rewriteCond"
433 ("test", Env.string, "pattern", Env.string, "flags", Env.list cond_flag)
434 (fn (from, to, flags) =>
435 (checkRewrite ();
436 write "\tRewriteCond\t";
437 write from;
438 write "\t";
439 write to;
440 case flags of
441 [] => ()
442 | flag::rest => (write " [";
443 write flag;
444 app (fn flag => (write ",";
445 write flag)) rest;
446 write "]");
447 write "\n"))
448
449 val () = Env.action_one "rewriteLogLevel"
450 ("level", Env.int)
451 (fn level =>
452 (checkRewrite ();
453 write "\tRewriteLog ";
454 write' (fn x => x);
455 write "/rewrite.log\n\tRewriteLogLevel ";
456 write (Int.toString level);
457 write "\n"))
458
459 val () = Env.action_two "alias"
460 ("from", Env.string, "to", Env.string)
461 (fn (from, to) =>
462 (write "\tAlias\t";
463 write from;
464 write " ";
465 write to;
466 write "\n"))
467
468 val () = Env.action_two "scriptAlias"
469 ("from", Env.string, "to", Env.string)
470 (fn (from, to) =>
471 (write "\tScriptAlias\t";
472 write from;
473 write " ";
474 write to;
475 write "\n"))
476
477 val () = Env.action_two "errorDocument"
478 ("code", Env.string, "handler", Env.string)
479 (fn (code, handler) =>
480 (write "\tErrorDocument\t";
481 write code;
482 write " ";
483 write handler;
484 write "\n"))
485
486 val () = Env.action_one "options"
487 ("options", Env.list apache_option)
488 (fn opts =>
489 case opts of
490 [] => ()
491 | _ => (write "\tOptions";
492 app (fn opt => (write " "; write opt)) opts;
493 write "\n"))
494
495 val () = Env.action_one "set_options"
496 ("options", Env.list apache_option)
497 (fn opts =>
498 case opts of
499 [] => ()
500 | _ => (write "\tOptions";
501 app (fn opt => (write " +"; write opt)) opts;
502 write "\n"))
503
504 val () = Env.action_one "unset_options"
505 ("options", Env.list apache_option)
506 (fn opts =>
507 case opts of
508 [] => ()
509 | _ => (write "\tOptions";
510 app (fn opt => (write " -"; write opt)) opts;
511 write "\n"))
512
513 val () = Env.action_one "directoryIndex"
514 ("filenames", Env.list Env.string)
515 (fn opts =>
516 (write "\tDirectoryIndex";
517 app (fn opt => (write " "; write opt)) opts;
518 write "\n"))
519
520 val () = Env.action_one "serverAlias"
521 ("host", Env.string)
522 (fn host =>
523 (write "\tServerAlias ";
524 write host;
525 write "\n";
526 !aliaser host))
527
528 val authType = fn (EVar "basic", _) => SOME "basic"
529 | (EVar "digest", _) => SOME "digest"
530 | _ => NONE
531
532 val () = Env.action_one "authType"
533 ("type", authType)
534 (fn ty =>
535 (write "\tAuthType ";
536 write ty;
537 write "\n"))
538
539 val () = Env.action_one "authName"
540 ("name", Env.string)
541 (fn name =>
542 (write "\tAuthName \"";
543 write name;
544 write "\"\n"))
545
546 val () = Env.action_one "authUserFile"
547 ("file", Env.string)
548 (fn name =>
549 (write "\tAuthUserFile ";
550 write name;
551 write "\n"))
552
553 val () = Env.action_none "requireValidUser"
554 (fn () => write "\tRequire valid-user\n")
555
556 val () = Env.action_one "requireUser"
557 ("users", Env.list Env.string)
558 (fn names =>
559 case names of
560 [] => ()
561 | _ => (write "\tRequire user";
562 app (fn name => (write " "; write name)) names;
563 write "\n"))
564
565 val () = Env.action_one "requireGroup"
566 ("groups", Env.list Env.string)
567 (fn names =>
568 case names of
569 [] => ()
570 | _ => (write "\tRequire group";
571 app (fn name => (write " "; write name)) names;
572 write "\n"))
573
574 val () = Env.action_none "orderAllowDeny"
575 (fn () => write "\tOrder allow,deny\n")
576
577 val () = Env.action_none "orderDenyAllow"
578 (fn () => write "\tOrder deny,allow\n")
579
580 val () = Env.action_none "allowFromAll"
581 (fn () => write "\tAllow from all\n")
582
583 val () = Env.action_one "allowFrom"
584 ("entries", Env.list Env.string)
585 (fn names =>
586 case names of
587 [] => ()
588 | _ => (write "\tAllow from";
589 app (fn name => (write " "; write name)) names;
590 write "\n"))
591
592 val () = Env.action_none "denyFromAll"
593 (fn () => write "\tDeny from all\n")
594
595 val () = Env.action_one "denyFrom"
596 ("entries", Env.list Env.string)
597 (fn names =>
598 case names of
599 [] => ()
600 | _ => (write "\tDeny from";
601 app (fn name => (write " "; write name)) names;
602 write "\n"))
603
604 val () = Env.action_none "satisfyAll"
605 (fn () => write "\tSatisfy all\n")
606
607 val () = Env.action_none "satisfyAny"
608 (fn () => write "\tSatisfy any\n")
609
610 val () = Env.action_one "forceType"
611 ("type", Env.string)
612 (fn ty => (write "\tForceType ";
613 write ty;
614 write "\n"))
615
616 val () = Env.action_none "forceTypeOff"
617 (fn () => write "\tForceType None\n")
618
619 val () = Env.action_two "action"
620 ("what", Env.string, "how", Env.string)
621 (fn (what, how) => (write "\tAction ";
622 write what;
623 write " ";
624 write how;
625 write "\n"))
626
627 val () = Env.action_one "addDefaultCharset"
628 ("charset", Env.string)
629 (fn ty => (write "\tAddDefaultCharset ";
630 write ty;
631 write "\n"))
632
633 val () = Env.action_one "davSvn"
634 ("path", Env.string)
635 (fn path => (write "\tDAV svn\n\tSVNPath ";
636 write path;
637 write "\n"))
638
639 val () = Env.action_one "authzSvnAccessFile"
640 ("path", Env.string)
641 (fn path => (write "\tAuthzSVNAccessFile ";
642 write path;
643 write "\n"))
644
645 val () = Env.action_two "addDescription"
646 ("description", Env.string, "patterns", Env.list Env.string)
647 (fn (desc, pats) =>
648 case pats of
649 [] => ()
650 | _ => (write "\tAddDescription \"";
651 write (String.toString desc);
652 write "\"";
653 app (fn pat => (write " "; write pat)) pats;
654 write "\n"))
655
656 val () = Env.action_one "indexOptions"
657 ("options", Env.list autoindex_option)
658 (fn opts =>
659 case opts of
660 [] => ()
661 | _ => (write "\tIndexOptions";
662 app (fn (opt, arg) =>
663 (write " ";
664 write opt;
665 Option.app (fn arg =>
666 (write "="; write arg)) arg)) opts;
667 write "\n"))
668
669 val () = Env.action_one "set_indexOptions"
670 ("options", Env.list autoindex_option)
671 (fn opts =>
672 case opts of
673 [] => ()
674 | _ => (write "\tIndexOptions";
675 app (fn (opt, arg) =>
676 (write " +";
677 write opt;
678 Option.app (fn arg =>
679 (write "="; write arg)) arg)) opts;
680 write "\n"))
681
682 val () = Env.action_one "unset_indexOptions"
683 ("options", Env.list autoindex_option)
684 (fn opts =>
685 case opts of
686 [] => ()
687 | _ => (write "\tIndexOptions";
688 app (fn (opt, _) =>
689 (write " -";
690 write opt)) opts;
691 write "\n"))
692
693 val () = Env.action_one "headerName"
694 ("name", Env.string)
695 (fn name => (write "\tHeaderName ";
696 write name;
697 write "\n"))
698
699 val () = Env.action_one "readmeName"
700 ("name", Env.string)
701 (fn name => (write "\tReadmeName ";
702 write name;
703 write "\n"))
704
705 end