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