apache: support mod_dir DirectorySlash
[hcoop/domtool2.git] / src / plugins / apache.sml
... / ...
CommitLineData
1(* HCoop Domtool (http://hcoop.sourceforge.net/)
2 * Copyright (c) 2006-2009, Adam Chlipala
3 * Copyright (c) 2013,2014,2015,2017,2018,2019 Clinton Ebadi
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 *)
19
20(* Apache HTTPD handling *)
21
22structure Apache :> APACHE = struct
23
24open Ast
25
26val dl = ErrorMsg.dummyLoc
27
28fun webNode node =
29 List.exists (fn (x, _) => x = node) Config.Apache.webNodes_all
30 orelse (Domain.hasPriv "www"
31 andalso List.exists (fn (x, _) => x = node) Config.Apache.webNodes_admin)
32
33val _ = Env.type_one "web_node"
34 Env.string
35 webNode
36
37val _ = Env.registerFunction ("web_node_to_node",
38 fn [e] => SOME e
39 | _ => NONE)
40
41fun webPlace (EApp ((EVar "web_place_default", _), (EString node, _)), _) =
42 SOME (node, Domain.nodeIp node, Domain.nodeIpv6 node)
43 | webPlace (EApp ((EApp ((EApp ((EVar "web_place", _), (EString node, _)), _), (EString ip, _)), _), (EString ipv6, _)), _) =
44 SOME (node, ip, ipv6)
45 | webPlace _ = NONE
46
47fun webPlaceDefault node = (EApp ((EVar "web_place_default", dl), (EString node, dl)), dl)
48
49val _ = Env.registerFunction ("web_place_to_web_node",
50 fn [e] => Option.map (fn (node, _, _) => (EString node, dl)) (webPlace e)
51 | _ => NONE)
52
53val _ = Env.registerFunction ("web_place_to_node",
54 fn [e] => Option.map (fn (node, _, _) => (EString node, dl)) (webPlace e)
55 | _ => NONE)
56
57val _ = Env.registerFunction ("web_place_to_ip",
58 fn [e] => Option.map (fn (_, ip, _) => (EString ip, dl)) (webPlace e)
59 | _ => NONE)
60
61val _ = Env.registerFunction ("web_place_to_ipv6",
62 fn [e] => Option.map (fn (_, _, ipv6) => (EString ipv6, dl)) (webPlace e)
63 | _ => NONE)
64
65val _ = Env.type_one "proxy_port"
66 Env.int
67 (fn n => n > 1024)
68
69fun validProxyTarget default s =
70 case String.fields (fn ch => ch = #":") s of
71 "http" :: host :: rest =>
72 let
73 val rest = String.concatWith ":" rest
74 in
75 if List.exists (fn h' => host = h') (map (fn h => String.concat ["//", h]) Config.Apache.proxyHosts)
76 then
77 CharVector.all (fn ch => Char.isPrint ch andalso not (Char.isSpace ch)
78 andalso ch <> #"\"" andalso ch <> #"'") rest
79 andalso case String.fields (fn ch => ch = #"/") rest of
80 port :: _ =>
81 (case Int.fromString port of
82 NONE => default s
83 | SOME n => n > 1024 orelse default s)
84 | _ => default s
85 else
86 default s
87 end
88 | _ => default s
89
90val _ = Env.type_one "proxy_target"
91 Env.string
92 (validProxyTarget (fn s => List.exists (fn s' => s = s') (Config.Apache.proxyTargets @ ["!"])))
93
94val _ = Env.type_one "proxy_reverse_target"
95 Env.string
96 (validProxyTarget (fn s => List.exists (fn s' => s = s') Config.Apache.proxyTargets))
97
98val _ = Env.type_one "rewrite_arg"
99 Env.string
100 (* #":" is permitted here, but really ought to be disallowed or escaped for E=VAR:VAL *)
101 (CharVector.all (fn ch => (Char.isGraph ch) andalso not (List.exists (fn c => ch = c) [ #"[", #"]", #",", #"\"", #"'", #"=", #"\\" ])))
102
103val _ = Env.type_one "suexec_flag"
104 Env.bool
105 (fn b => b orelse Domain.hasPriv "www")
106
107val _ = Env.type_one "regexp"
108 Env.string
109 Pcre.validRegexp
110
111fun validLocation s =
112 size s > 0 andalso size s < 1000 andalso CharVector.all
113 (fn ch => Char.isAlphaNum ch
114 orelse ch = #"-"
115 orelse ch = #"_"
116 orelse ch = #"."
117 orelse ch = #"/"
118 orelse ch = #"~") s
119
120val _ = Env.type_one "location"
121 Env.string
122 validLocation
123
124fun validCert s = Acl.query {user = Domain.getUser (),
125 class = "cert",
126 value = s}
127
128fun validCaCert s = Acl.query {user = Domain.getUser (),
129 class = "cacert",
130 value = s}
131
132val _ = Env.type_one "ssl_cert_path"
133 Env.string
134 validCert
135
136val _ = Env.type_one "ssl_cacert_path"
137 Env.string
138 validCaCert
139
140fun ssl e = case e of
141 (EVar "no_ssl", _) => SOME NONE
142 | (EApp ((EVar "use_cert", _), s), _) => Option.map SOME (Env.string s)
143 | _ => NONE
144
145fun validExtension s =
146 size s > 0
147 andalso size s < 20
148 andalso CharVector.all (fn ch => Char.isAlphaNum ch orelse ch = #"_") s
149
150val _ = Env.type_one "file_extension"
151 Env.string
152 validExtension
153
154val _ = Env.registerFunction ("defaultServerAdmin",
155 fn [] => SOME (EString (Domain.getUser () ^ "@" ^ Config.defaultDomain), dl)
156 | _ => NONE)
157
158val redirect_code = fn (EVar "temp", _) => SOME "temp"
159 | (EVar "permanent", _) => SOME "permanent"
160 | (EVar "seeother", _) => SOME "seeother"
161 | (EVar "redir300", _) => SOME "300"
162 | (EVar "redir301", _) => SOME "301"
163 | (EVar "redir302", _) => SOME "302"
164 | (EVar "redir303", _) => SOME "303"
165 | (EVar "redir304", _) => SOME "304"
166 | (EVar "redir305", _) => SOME "305"
167 | (EVar "redir307", _) => SOME "307"
168 | (EVar "notfound", _) => SOME "404"
169 | _ => NONE
170
171val flag = fn (EVar "redirect", _) => SOME "R"
172 | (EVar "forbidden", _) => SOME "F"
173 | (EVar "gone", _) => SOME "G"
174 | (EVar "last", _) => SOME "L"
175 | (EVar "chain", _) => SOME "C"
176 | (EVar "nosubreq", _) => SOME "NS"
177 | (EVar "nocase", _) => SOME "NC"
178 | (EVar "qsappend", _) => SOME "QSA"
179 | (EVar "noescape", _) => SOME "NE"
180 | (EVar "passthrough", _) => SOME "PT"
181 | (EApp ((EVar "mimeType", _), e), _) =>
182 Option.map (fn s => "T=" ^ s) (Env.string e)
183 | (EApp ((EVar "redirectWith", _), e), _) =>
184 Option.map (fn s => "R=" ^ s) (redirect_code e)
185 | (EApp ((EVar "skip", _), e), _) =>
186 Option.map (fn n => "S=" ^ Int.toString n) (Env.int e)
187 | (EApp ((EApp ((EVar "env", _), e1), _), e2), _) =>
188 (case Env.string e1 of
189 NONE => NONE
190 | SOME s1 => Option.map (fn s2 => "E=" ^ s1 ^ ":" ^ s2)
191 (Env.string e2))
192
193 | _ => NONE
194
195val cond_flag = fn (EVar "cond_nocase", _) => SOME "NC"
196 | (EVar "ornext", _) => SOME "OR"
197 | _ => NONE
198
199val apache_option = fn (EVar "execCGI", _) => SOME "ExecCGI"
200 | (EVar "includesNOEXEC", _) => SOME "IncludesNOEXEC"
201 | (EVar "indexes", _) => SOME "Indexes"
202 | (EVar "followSymLinks", _) => SOME "FollowSymLinks"
203 | (EVar "multiViews", _) => SOME "MultiViews"
204 | _ => NONE
205
206val autoindex_width = fn (EVar "autofit", _) => SOME "*"
207 | (EApp ((EVar "characters", _), n), _) =>
208 Option.map Int.toString (Env.int n)
209 | _ => NONE
210
211val autoindex_option = fn (EApp ((EVar "descriptionWidth", _), w), _) =>
212 Option.map (fn w => ("DescriptionWidth", SOME w))
213 (autoindex_width w)
214 | (EVar "fancyIndexing", _) => SOME ("FancyIndexing", NONE)
215 | (EVar "foldersFirst", _) => SOME ("FoldersFirst", NONE)
216 | (EVar "htmlTable", _) => SOME ("HTMLTable", NONE)
217 | (EVar "iconsAreLinks", _) => SOME ("IconsAreLinks", NONE)
218 | (EApp ((EVar "iconHeight", _), n), _) =>
219 Option.map (fn w => ("IconHeight", SOME (Int.toString w)))
220 (Env.int n)
221 | (EApp ((EVar "iconWidth", _), n), _) =>
222 Option.map (fn w => ("IconWidth", SOME (Int.toString w)))
223 (Env.int n)
224 | (EVar "ignoreCase", _) => SOME ("IgnoreCase", NONE)
225 | (EVar "ignoreClient", _) => SOME ("IgnoreClient", NONE)
226 | (EApp ((EVar "nameWidth", _), w), _) =>
227 Option.map (fn w => ("NameWidth", SOME w))
228 (autoindex_width w)
229 | (EVar "scanHtmlTitles", _) => SOME ("ScanHTMLTitles", NONE)
230 | (EVar "suppressColumnSorting", _) => SOME ("SuppressColumnSorting", NONE)
231 | (EVar "suppressDescription", _) => SOME ("SuppressDescription", NONE)
232 | (EVar "suppressHtmlPreamble", _) => SOME ("SuppressHTMLPreamble", NONE)
233 | (EVar "suppressIcon", _) => SOME ("SuppressIcon", NONE)
234 | (EVar "suppressLastModified", _) => SOME ("SuppressLastModified", NONE)
235 | (EVar "suppressRules", _) => SOME ("SuppressRules", NONE)
236 | (EVar "suppressSize", _) => SOME ("SuppressSize", NONE)
237 | (EVar "trackModified", _) => SOME ("TrackModified", NONE)
238 | (EVar "versionSort", _) => SOME ("VersionSort", NONE)
239 | (EVar "xhtml", _) => SOME ("XHTML", NONE)
240
241 | _ => NONE
242
243val interval_base = fn (EVar "access", _) => SOME "access"
244 | (EVar "modification", _) => SOME "modification"
245 | _ => NONE
246
247val interval = fn (EVar "years", _) => SOME "years"
248 | (EVar "months", _) => SOME "months"
249 | (EVar "weeks", _) => SOME "weeks"
250 | (EVar "days", _) => SOME "days"
251 | (EVar "hours", _) => SOME "hours"
252 | (EVar "minutes", _) => SOME "minutes"
253 | (EVar "seconds", _) => SOME "seconds"
254 | _ => NONE
255
256val vhostsChanged = ref false
257val logDeleted = ref false
258val delayedLogMoves = ref (fn () => ())
259
260val () = Slave.registerPreHandler
261 (fn () => (vhostsChanged := false;
262 logDeleted := false;
263 delayedLogMoves := (fn () => print "Executing delayed log moves/deletes.\n")))
264
265fun findVhostUser fname =
266 let
267 val inf = TextIO.openIn fname
268
269 fun loop () =
270 case TextIO.inputLine inf of
271 NONE => NONE
272 | SOME line =>
273 if String.isPrefix "# Owner: " line then
274 case String.tokens Char.isSpace line of
275 [_, _, user] => SOME user
276 | _ => NONE
277 else
278 loop ()
279 in
280 loop ()
281 before TextIO.closeIn inf
282 end handle _ => NONE
283
284val webNodes_full = Config.Apache.webNodes_all @ Config.Apache.webNodes_admin
285
286fun isVersion1 node =
287 List.exists (fn (n, {version = ConfigTypes.APACHE_1_3, ...}) => n = node
288 | _ => false) webNodes_full
289
290fun imVersion1 () = isVersion1 (Slave.hostname ())
291
292fun isWaklog node =
293 List.exists (fn (n, {auth = ConfigTypes.MOD_WAKLOG, ...}) => n = node
294 | _ => false) webNodes_full
295
296fun down () = if imVersion1 () then Config.Apache.down1 else Config.Apache.down
297fun undown () = if imVersion1 () then Config.Apache.undown1 else Config.Apache.undown
298fun reload () = if imVersion1 () then Config.Apache.reload1 else Config.Apache.reload
299fun fixperms () = if imVersion1 () then Config.Apache.fixperms1 else Config.Apache.fixperms
300
301fun logDir {user, node, vhostId} =
302 String.concat [Config.Apache.logDirOf (isVersion1 node) user,
303 "/",
304 node,
305 "/",
306 vhostId]
307
308fun realLogDir {user, node, vhostId} =
309 String.concat [Config.Apache.realLogDirOf user,
310 "/",
311 node,
312 "/",
313 vhostId]
314
315val () = Slave.registerFileHandler (fn fs =>
316 let
317 val spl = OS.Path.splitDirFile (#file fs)
318 in
319 if String.isSuffix ".vhost" (#file spl)
320 orelse String.isSuffix ".vhost_ssl" (#file spl) then let
321 val realVhostFile = OS.Path.joinDirFile
322 {dir = Config.Apache.confDir,
323 file = #file spl}
324
325 val user = findVhostUser (#file fs)
326 val oldUser = case #action fs of
327 Slave.Delete false => user
328 | _ => findVhostUser realVhostFile
329 in
330 if (oldUser = NONE andalso #action fs <> Slave.Add)
331 orelse (user = NONE andalso not (Slave.isDelete (#action fs))) then
332 print ("Can't find user in " ^ #file fs ^ " or " ^ realVhostFile ^ "! Taking no action.\n")
333 else
334 let
335 val vhostId = if OS.Path.ext (#file spl) = SOME "vhost_ssl" then
336 OS.Path.base (#file spl) ^ ".ssl"
337 else
338 OS.Path.base (#file spl)
339
340 fun realLogDir user =
341 logDir {user = valOf user,
342 node = Slave.hostname (),
343 vhostId = vhostId}
344
345 fun backupLogs () =
346 OS.Path.joinDirFile
347 {dir = Config.Apache.backupLogDirOf
348 (isVersion1 (Slave.hostname ())),
349 file = vhostId}
350 in
351 vhostsChanged := true;
352 case #action fs of
353 Slave.Delete _ =>
354 let
355 val ldir = realLogDir oldUser
356 val dlm = !delayedLogMoves
357 in
358 if !logDeleted then
359 ()
360 else
361 ((*ignore (OS.Process.system (down ()));*)
362 ignore (OS.Process.system (fixperms ()));
363 logDeleted := true);
364 ignore (OS.Process.system (Config.rm
365 ^ " -rf "
366 ^ realVhostFile));
367 delayedLogMoves := (fn () => (dlm ();
368 Slave.moveDirCreate {from = ldir,
369 to = backupLogs ()}))
370 end
371 | Slave.Add =>
372 let
373 val rld = realLogDir user
374 in
375 ignore (OS.Process.system (Config.cp
376 ^ " "
377 ^ #file fs
378 ^ " "
379 ^ realVhostFile));
380 if Posix.FileSys.access (rld, []) then
381 ()
382 else
383 Slave.moveDirCreate {from = backupLogs (),
384 to = rld}
385 end
386
387 | _ =>
388 (ignore (OS.Process.system (Config.cp
389 ^ " "
390 ^ #file fs
391 ^ " "
392 ^ realVhostFile));
393 if user <> oldUser then
394 let
395 val old = realLogDir oldUser
396 val rld = realLogDir user
397
398 val dlm = !delayedLogMoves
399 in
400 if !logDeleted then
401 ()
402 else
403 ((*ignore (OS.Process.system (down ()));*)
404 logDeleted := true);
405 delayedLogMoves := (fn () => (dlm ();
406 ignore (OS.Process.system (Config.rm
407 ^ " -rf "
408 ^ realLogDir oldUser))));
409 if Posix.FileSys.access (rld, []) then
410 ()
411 else
412 Slave.mkDirAll rld
413 end
414 else
415 ())
416 end
417 end
418 else
419 ()
420 end)
421
422val () = Slave.registerPostHandler
423 (fn () =>
424 (if !vhostsChanged then
425 (Slave.shellF ([reload ()],
426 fn cl => "Error reloading Apache with " ^ cl);
427 if !logDeleted then !delayedLogMoves () else ())
428 else
429 ()))
430
431val vhostFiles : (string * TextIO.outstream) list ref = ref []
432fun write' s = app (fn (node, file) => TextIO.output (file, s node)) (!vhostFiles)
433fun write s = app (fn (_, file) => TextIO.output (file, s)) (!vhostFiles)
434
435val rewriteEnabled = ref false
436val localRewriteEnabled = ref false
437val expiresEnabled = ref false
438val localExpiresEnabled = ref false
439val currentVhost = ref ""
440val currentVhostId = ref ""
441val sslEnabled = ref false
442
443val pre = ref (fn _ : {user : string, nodes : string list, id : string, hostname : string} => ())
444fun registerPre f =
445 let
446 val old = !pre
447 in
448 pre := (fn x => (old x; f x))
449 end
450
451val post = ref (fn () => ())
452fun registerPost f =
453 let
454 val old = !post
455 in
456 post := (fn () => (old (); f ()))
457 end
458
459fun doPre x = !pre x
460fun doPost () = !post ()
461
462val aliaser = ref (fn _ : string => ())
463fun registerAliaser f =
464 let
465 val old = !aliaser
466 in
467 aliaser := (fn x => (old x; f x))
468 end
469
470fun vhostPost () = (!post ();
471 write "</VirtualHost>\n";
472 app (TextIO.closeOut o #2) (!vhostFiles))
473
474val php_version = fn (EVar "php56", _) => SOME 56
475 | (EVar "php72", _) => SOME 72
476 | _ => NONE
477
478fun vhostBody (env, makeFullHost) =
479 let
480 val places = Env.env (Env.list webPlace) (env, "WebPlaces")
481
482 val ssl = Env.env ssl (env, "SSL")
483 val user = Env.env Env.string (env, "User")
484 val group = Env.env Env.string (env, "Group")
485 val docroot = Env.env Env.string (env, "DocumentRoot")
486 val sadmin = Env.env Env.string (env, "ServerAdmin")
487 val suexec = Env.env Env.bool (env, "SuExec")
488 val php = Env.env php_version (env, "PhpVersion")
489
490 val fullHost = makeFullHost (Domain.currentDomain ())
491 val vhostId = fullHost ^ (if Option.isSome ssl then ".ssl" else "")
492 val confFile = fullHost ^ (if Option.isSome ssl then ".vhost_ssl" else ".vhost")
493 in
494 currentVhost := fullHost;
495 currentVhostId := vhostId;
496 sslEnabled := Option.isSome ssl;
497
498 rewriteEnabled := false;
499 localRewriteEnabled := false;
500 expiresEnabled := false;
501 localExpiresEnabled := false;
502 vhostFiles := map (fn (node, ip, ipv6) =>
503 let
504 val file = Domain.domainFile {node = node,
505 name = confFile}
506
507 val ld = logDir {user = user, node = node, vhostId = vhostId}
508 in
509 TextIO.output (file, "# Owner: ");
510 TextIO.output (file, user);
511 TextIO.output (file, "\n<VirtualHost ");
512
513 TextIO.output (file, ip);
514 TextIO.output (file, ":");
515 TextIO.output (file, case ssl of
516 SOME _ => "443"
517 | NONE => "80");
518
519 TextIO.output (file, " [");
520 TextIO.output (file, ipv6);
521 TextIO.output (file, "]");
522 TextIO.output (file, ":");
523 TextIO.output (file, case ssl of
524 SOME _ => "443"
525 | NONE => "80");
526
527 TextIO.output (file, ">\n");
528 TextIO.output (file, "\tErrorLog ");
529 TextIO.output (file, ld);
530 TextIO.output (file, "/error.log\n\tCustomLog ");
531 TextIO.output (file, ld);
532 TextIO.output (file, "/access.log combined\n");
533 TextIO.output (file, "\tServerName ");
534 TextIO.output (file, fullHost);
535 app
536 (fn dom => (TextIO.output (file, "\n\tServerAlias ");
537 TextIO.output (file, makeFullHost dom)))
538 (Domain.currentAliasDomains ());
539
540 if suexec then
541 if isVersion1 node then
542 (TextIO.output (file, "\n\tUser ");
543 TextIO.output (file, user);
544 TextIO.output (file, "\n\tGroup ");
545 TextIO.output (file, group))
546 else
547 (TextIO.output (file, "\n\tSuexecUserGroup ");
548 TextIO.output (file, user);
549 TextIO.output (file, " ");
550 TextIO.output (file, group))
551 else
552 ();
553
554 if isWaklog node then
555 (TextIO.output (file, "\n\tWaklogEnabled on\n\tWaklogLocationPrincipal ");
556 TextIO.output (file, user);
557 TextIO.output (file, "/daemon@HCOOP.NET /etc/keytabs/user.daemon/");
558 TextIO.output (file, user))
559 else
560 ();
561
562 TextIO.output (file, "\n\tDAVLockDB /var/lock/apache2/dav/");
563 TextIO.output (file, user);
564 TextIO.output (file, "/DAVLock");
565
566 TextIO.output (file, "\n\tAddHandler fcgid-script .php .phtml");
567 map (fn ext => (TextIO.output (file, "\n\tFcgidWrapper \"");
568 (* kerberos wrapper, simulates waklog+mod_cgi *)
569 if isWaklog node then
570 (TextIO.output (file, Config.Apache.fastCgiWrapperOf user);
571 TextIO.output (file, " "))
572 else
573 ();
574 TextIO.output (file, Config.Apache.phpFastCgiWrapper php);
575 TextIO.output (file, "\" ");
576 TextIO.output (file, ext)))
577 [".php", ".phtml"];
578 (ld, file)
579 end)
580 places;
581 write "\n\tDocumentRoot ";
582 write docroot;
583 write "\n\tServerAdmin ";
584 write sadmin;
585 case ssl of
586 SOME cert =>
587 (write "\n\tSSLEngine on\n\tSSLCertificateFile ";
588 write cert)
589 | NONE => ();
590 write "\n";
591 !pre {user = user, nodes = map #1 places, id = vhostId, hostname = fullHost};
592 app (fn dom => !aliaser (makeFullHost dom)) (Domain.currentAliasDomains ())
593 end
594
595val () = Env.containerV_one "vhost"
596 ("host", Env.string)
597 (fn (env, host) => vhostBody (env, fn dom => host ^ "." ^ dom),
598 vhostPost)
599
600val () = Env.containerV_none "vhostDefault"
601 (fn env => vhostBody (env, fn dom => dom),
602 vhostPost)
603
604val inLocal = ref false
605
606val () = Env.container_one "location"
607 ("prefix", Env.string)
608 (fn prefix =>
609 (write "\t<Location ";
610 write prefix;
611 write ">\n";
612 inLocal := true),
613 fn () => (write "\t</Location>\n";
614 inLocal := false;
615 localRewriteEnabled := false;
616 localExpiresEnabled := false))
617
618val () = Env.container_one "directory"
619 ("directory", Env.string)
620 (fn directory =>
621 (write "\t<Directory ";
622 write directory;
623 write ">\n";
624 inLocal := true),
625 fn () => (write "\t</Directory>\n";
626 inLocal := false;
627 localRewriteEnabled := false;
628 localExpiresEnabled := false))
629
630val () = Env.container_one "filesMatch"
631 ("regexp", Env.string)
632 (fn prefix =>
633 (write "\t<FilesMatch \"";
634 write prefix;
635 write "\">\n"),
636 fn () => (write "\t</FilesMatch>\n";
637 localRewriteEnabled := false;
638 localExpiresEnabled := false))
639
640fun checkRewrite () =
641 if !inLocal then
642 if !localRewriteEnabled then
643 ()
644 else
645 (write "\tRewriteEngine on\n";
646 localRewriteEnabled := true)
647 else if !rewriteEnabled then
648 ()
649 else
650 (write "\tRewriteEngine on\n";
651 rewriteEnabled := true)
652
653fun checkExpires () =
654 if !inLocal then
655 if !localExpiresEnabled then
656 ()
657 else
658 (write "\tExpiresActive on\n";
659 localExpiresEnabled := true)
660 else if !expiresEnabled then
661 ()
662 else
663 (write "\tExpiresActive on\n";
664 expiresEnabled := true)
665
666val () = Env.action_three "localProxyRewrite"
667 ("from", Env.string, "to", Env.string, "port", Env.int)
668 (fn (from, to, port) =>
669 (checkRewrite ();
670 write "\tRewriteRule\t\"";
671 write from;
672 write "\"\thttp://localhost:";
673 write (Int.toString port);
674 write "/";
675 write to;
676 write " [P]\n"))
677
678val () = Env.action_four "expiresByType"
679 ("mime", Env.string, "base", interval_base, "num", Env.int, "inter", interval)
680 (fn (mime, base, num, inter) =>
681 (checkExpires ();
682 write "\tExpiresByType\t\"";
683 write mime;
684 write "\"\t\"";
685 write base;
686 write " plus ";
687 if num < 0 then
688 (write "-";
689 write (Int.toString (~num)))
690 else
691 write (Int.toString num);
692 write " ";
693 write inter;
694 write "\"\n"))
695
696val () = Env.action_two "proxyPass"
697 ("from", Env.string, "to", Env.string)
698 (fn (from, to) =>
699 (write "\tProxyPass\t";
700 write from;
701 write "\t";
702 write to;
703 write "\tretry=0\n"))
704
705val () = Env.action_two "proxyPassReverse"
706 ("from", Env.string, "to", Env.string)
707 (fn (from, to) =>
708 (write "\tProxyPassReverse\t";
709 write from;
710 write "\t";
711 write to;
712 write "\n"))
713
714val () = Env.action_one "proxyPreserveHost"
715 ("enable", Env.bool)
716 (fn (enable) =>
717 (write "\tProxyPreserveHost\t";
718 if enable then write "On" else write "Off";
719 write "\n"))
720
721val () = Env.action_three "rewriteRule"
722 ("from", Env.string, "to", Env.string, "flags", Env.list flag)
723 (fn (from, to, flags) =>
724 (checkRewrite ();
725 write "\tRewriteRule\t\"";
726 write from;
727 write "\"\t\"";
728 write to;
729 write "\"";
730 case flags of
731 [] => ()
732 | flag::rest => (write " [";
733 write flag;
734 app (fn flag => (write ",";
735 write flag)) rest;
736 write "]");
737 write "\n"))
738
739val () = Env.action_three "rewriteCond"
740 ("test", Env.string, "pattern", Env.string, "flags", Env.list cond_flag)
741 (fn (from, to, flags) =>
742 (checkRewrite ();
743 write "\tRewriteCond\t\"";
744 write from;
745 write "\"\t\"";
746 write to;
747 write "\"";
748 case flags of
749 [] => ()
750 | flag::rest => (write " [";
751 write flag;
752 app (fn flag => (write ",";
753 write flag)) rest;
754 write "]");
755 write "\n"))
756
757val () = Env.action_one "rewriteBase"
758 ("prefix", Env.string)
759 (fn prefix =>
760 (checkRewrite ();
761 write "\tRewriteBase\t\"";
762 write prefix;
763 write "\"\n"))
764
765val () = Env.action_one "rewriteLogLevel"
766 ("level", Env.int)
767 (fn level =>
768 (checkRewrite ();
769 write "\tRewriteLog ";
770 write' (fn x => x);
771 write "/rewrite.log\n\tRewriteLogLevel ";
772 write (Int.toString level);
773 write "\n"))
774
775val () = Env.action_two "alias"
776 ("from", Env.string, "to", Env.string)
777 (fn (from, to) =>
778 (write "\tAlias\t";
779 write from;
780 write " ";
781 write to;
782 write "\n"))
783
784val () = Env.action_two "scriptAlias"
785 ("from", Env.string, "to", Env.string)
786 (fn (from, to) =>
787 (write "\tScriptAlias\t";
788 write from;
789 write " ";
790 write to;
791 write "\n"))
792
793val () = Env.action_two "fastScriptAlias"
794 ("from", Env.string, "to", Env.string)
795 (fn (from, to) =>
796 let
797 (* mod_fcgid + kerberos limit this to working with
798 individual fcgi programs. assume the target path is a
799 file and any trailing `/' is just aliasing
800 syntax. Directory+File on the script is used to
801 activate fcgid instead of Location on the alias to
802 limit effects (alias+location also match in inverse
803 order causing pernicious side-effects *)
804 val fcgi_path = if String.sub (to, size to - 1) = #"/"
805 then
806 String.substring (to, 0, size to - 1)
807 else
808 to
809 val fcgi_dir = OS.Path.dir fcgi_path
810 val fcgi_file = OS.Path.file fcgi_path
811 in
812 write "\tAlias\t"; write from; write " "; write to; write "\n";
813
814 write "\t<Directory "; write fcgi_dir; write ">\n";
815 write "\t<Files "; write fcgi_file; write ">\n";
816 write "\tSetHandler fcgid-script\n";
817
818 (* FIXME: only set kerberos wrapper of waklog is on *)
819 (* won't be trivial, since we don't have access to node here *)
820 write "\tFcgidWrapper \"";
821 write (Config.Apache.fastCgiWrapperOf (Domain.getUser ()));
822 write " ";
823 write fcgi_path;
824 write "\"\n";
825
826 write "\t</Files>\n\t</Directory>\n"
827 end)
828
829val () = Env.action_two "errorDocument"
830 ("code", Env.string, "handler", Env.string)
831 (fn (code, handler) =>
832 let
833 val hasSpaces = CharVector.exists Char.isSpace handler
834
835 fun maybeQuote () =
836 if hasSpaces then
837 write "\""
838 else
839 ()
840 in
841 write "\tErrorDocument\t";
842 write code;
843 write " ";
844 maybeQuote ();
845 write handler;
846 maybeQuote ();
847 write "\n"
848 end)
849
850val () = Env.action_one "options"
851 ("options", Env.list apache_option)
852 (fn opts =>
853 case opts of
854 [] => ()
855 | _ => (write "\tOptions";
856 app (fn opt => (write " "; write opt)) opts;
857 write "\n"))
858
859val () = Env.action_one "set_options"
860 ("options", Env.list apache_option)
861 (fn opts =>
862 case opts of
863 [] => ()
864 | _ => (write "\tOptions";
865 app (fn opt => (write " +"; write opt)) opts;
866 write "\n"))
867
868val () = Env.action_one "unset_options"
869 ("options", Env.list apache_option)
870 (fn opts =>
871 case opts of
872 [] => ()
873 | _ => (write "\tOptions";
874 app (fn opt => (write " -"; write opt)) opts;
875 write "\n"))
876
877val () = Env.action_one "cgiExtension"
878 ("extension", Env.string)
879 (fn ext => (write "\tAddHandler cgi-script ";
880 write ext;
881 write "\n"))
882
883val () = Env.action_one "directoryIndex"
884 ("filenames", Env.list Env.string)
885 (fn opts =>
886 (write "\tDirectoryIndex";
887 app (fn opt => (write " "; write opt)) opts;
888 write "\n"))
889
890val () = Env.action_one "directorySlash"
891 ("enable", Env.bool)
892 (fn enable =>
893 (write "\tDirectorySlash ";
894 if enable then write "On" else write "Off";
895 write "\n"))
896
897val () = Env.action_one "serverAliasHost"
898 ("host", Env.string)
899 (fn host =>
900 (write "\tServerAlias ";
901 write host;
902 write "\n";
903 !aliaser host))
904
905val () = Env.action_one "serverAlias"
906 ("host", Env.string)
907 (fn host =>
908 (app
909 (fn dom =>
910 let
911 val full = host ^ "." ^ dom
912 in
913 write "\tServerAlias ";
914 write full;
915 write "\n";
916 !aliaser full
917 end)
918 (Domain.currentDomains ())))
919
920val () = Env.action_none "serverAliasDefault"
921 (fn () =>
922 (app
923 (fn dom =>
924 (write "\tServerAlias ";
925 write dom;
926 write "\n";
927 !aliaser dom))
928 (Domain.currentDomains ())))
929
930val authType = fn (EVar "basic", _) => SOME "basic"
931 | (EVar "digest", _) => SOME "digest"
932 | (EVar "kerberos", _) => SOME "kerberos"
933 | _ => NONE
934
935fun allowAuthType "kerberos" = !sslEnabled
936 | allowAuthType _ = true
937
938val () = Env.action_one "authType"
939 ("type", authType)
940 (fn ty =>
941 if allowAuthType ty then
942 (write "\tAuthType ";
943 write ty;
944 write "\n";
945 case ty of
946 "kerberos" =>
947 write "\tKrbServiceName HTTP\n\tKrb5Keytab /etc/keytabs/service/apache\n\tKrbMethodNegotiate on\n\tKrbMethodK5Passwd on\n\tKrbVerifyKDC on\n\tKrbAuthRealms HCOOP.NET\n\tKrbSaveCredentials on\n"
948 | _ => ())
949 else
950 print "WARNING: Skipped Kerberos authType because this isn't an SSL vhost.\n")
951
952val () = Env.action_one "authName"
953 ("name", Env.string)
954 (fn name =>
955 (write "\tAuthName \"";
956 write name;
957 write "\"\n"))
958
959val () = Env.action_one "authUserFile"
960 ("file", Env.string)
961 (fn name =>
962 (write "\tAuthUserFile ";
963 write name;
964 write "\n"))
965
966val () = Env.action_one "authGroupFile"
967 ("file", Env.string)
968 (fn name =>
969 (write "\tAuthGroupFile ";
970 write name;
971 write "\n"))
972
973val () = Env.action_none "requireValidUser"
974 (fn () => write "\tRequire valid-user\n")
975
976val () = Env.action_one "requireUser"
977 ("users", Env.list Env.string)
978 (fn names =>
979 case names of
980 [] => ()
981 | _ => (write "\tRequire user";
982 app (fn name => (write " "; write name)) names;
983 write "\n"))
984
985val () = Env.action_one "requireGroup"
986 ("groups", Env.list Env.string)
987 (fn names =>
988 case names of
989 [] => ()
990 | _ => (write "\tRequire group";
991 app (fn name => (write " "; write name)) names;
992 write "\n"))
993
994val () = Env.action_none "orderAllowDeny"
995 (fn () => write "\tOrder allow,deny\n")
996
997val () = Env.action_none "orderDenyAllow"
998 (fn () => write "\tOrder deny,allow\n")
999
1000val () = Env.action_none "allowFromAll"
1001 (fn () => write "\tAllow from all\n")
1002
1003val () = Env.action_one "allowFrom"
1004 ("entries", Env.list Env.string)
1005 (fn names =>
1006 case names of
1007 [] => ()
1008 | _ => (write "\tAllow from";
1009 app (fn name => (write " "; write name)) names;
1010 write "\n"))
1011
1012val () = Env.action_none "denyFromAll"
1013 (fn () => write "\tDeny from all\n")
1014
1015val () = Env.action_one "denyFrom"
1016 ("entries", Env.list Env.string)
1017 (fn names =>
1018 case names of
1019 [] => ()
1020 | _ => (write "\tDeny from";
1021 app (fn name => (write " "; write name)) names;
1022 write "\n"))
1023
1024val () = Env.action_none "satisfyAll"
1025 (fn () => write "\tSatisfy all\n")
1026
1027val () = Env.action_none "satisfyAny"
1028 (fn () => write "\tSatisfy any\n")
1029
1030val () = Env.action_one "forceType"
1031 ("type", Env.string)
1032 (fn ty => (write "\tForceType ";
1033 write ty;
1034 write "\n"))
1035
1036val () = Env.action_none "forceTypeOff"
1037 (fn () => write "\tForceType None\n")
1038
1039val () = Env.action_two "action"
1040 ("what", Env.string, "how", Env.string)
1041 (fn (what, how) => (write "\tAction ";
1042 write what;
1043 write " ";
1044 write how;
1045 write "\n"))
1046
1047val () = Env.action_one "addDefaultCharset"
1048 ("charset", Env.string)
1049 (fn ty => (write "\tAddDefaultCharset ";
1050 write ty;
1051 write "\n"))
1052
1053(*val () = Env.action_one "davSvn"
1054 ("path", Env.string)
1055 (fn path => (write "\tDAV svn\n\tSVNPath ";
1056 write path;
1057 write "\n"))
1058
1059val () = Env.action_one "authzSvnAccessFile"
1060 ("path", Env.string)
1061 (fn path => (write "\tAuthzSVNAccessFile ";
1062 write path;
1063 write "\n"))*)
1064
1065val () = Env.action_none "davFilesystem"
1066 (fn path => write "\tDAV filesystem\n")
1067
1068val () = Env.action_two "addDescription"
1069 ("description", Env.string, "patterns", Env.list Env.string)
1070 (fn (desc, pats) =>
1071 case pats of
1072 [] => ()
1073 | _ => (write "\tAddDescription \"";
1074 write (String.toString desc);
1075 write "\"";
1076 app (fn pat => (write " "; write pat)) pats;
1077 write "\n"))
1078
1079val () = Env.action_two "addIcon"
1080 ("icon", Env.string, "patterns", Env.list Env.string)
1081 (fn (icon, pats) =>
1082 case pats of
1083 [] => ()
1084 | _ => (write "\tAddIcon \"";
1085 write icon;
1086 write "\"";
1087 app (fn pat => (write " "; write pat)) pats;
1088 write "\n"))
1089
1090val () = Env.action_one "indexOptions"
1091 ("options", Env.list autoindex_option)
1092 (fn opts =>
1093 case opts of
1094 [] => ()
1095 | _ => (write "\tIndexOptions";
1096 app (fn (opt, arg) =>
1097 (write " ";
1098 write opt;
1099 Option.app (fn arg =>
1100 (write "="; write arg)) arg)) opts;
1101 write "\n"))
1102
1103val () = Env.action_one "indexIgnore"
1104 ("patterns", Env.list Env.string)
1105 (fn pats =>
1106 case pats of
1107 [] => ()
1108 | _ => (write "\tIndexIgnore";
1109 app (fn pat => (write " "; write pat)) pats;
1110 write "\n"))
1111
1112val () = Env.action_one "set_indexOptions"
1113 ("options", Env.list autoindex_option)
1114 (fn opts =>
1115 case opts of
1116 [] => ()
1117 | _ => (write "\tIndexOptions";
1118 app (fn (opt, arg) =>
1119 (write " +";
1120 write opt;
1121 Option.app (fn arg =>
1122 (write "="; write arg)) arg)) opts;
1123 write "\n"))
1124
1125val () = Env.action_one "unset_indexOptions"
1126 ("options", Env.list autoindex_option)
1127 (fn opts =>
1128 case opts of
1129 [] => ()
1130 | _ => (write "\tIndexOptions";
1131 app (fn (opt, _) =>
1132 (write " -";
1133 write opt)) opts;
1134 write "\n"))
1135
1136val () = Env.action_one "headerName"
1137 ("name", Env.string)
1138 (fn name => (write "\tHeaderName ";
1139 write name;
1140 write "\n"))
1141
1142val () = Env.action_one "readmeName"
1143 ("name", Env.string)
1144 (fn name => (write "\tReadmeName ";
1145 write name;
1146 write "\n"))
1147
1148val () = Env.action_two "setEnv"
1149 ("key", Env.string, "value", Env.string)
1150 (fn (key, value) => (write "\tSetEnv \"";
1151 write key;
1152 write "\" \"";
1153 write (String.translate (fn #"\"" => "\\\""
1154 | ch => str ch) value);
1155 write "\"\n"))
1156
1157val () = Env.action_three "setEnvIf"
1158 ("attribute", Env.string, "match", Env.string, "env_variables", Env.list Env.string)
1159 (fn (attribute, match, envs) =>
1160 case envs of
1161 [] => (print "WARNING: Skipped setEnvIf, no environment variables provided.\n")
1162 | envs =>
1163 (write "\tSetEnvIf\t\"";
1164 write attribute;
1165 write "\"\t\"";
1166 write match;
1167 write "\"";
1168 app (fn env => (write "\t"; write env)) envs;
1169 write "\n"))
1170
1171val () = Env.action_three "setEnvIfNoCase"
1172 ("attribute", Env.string, "match", Env.string, "env_variables", Env.list Env.string)
1173 (fn (attribute, match, envs) =>
1174 case envs of
1175 [] => (print "WARNING: Skipped setEnvIfNoCase, no environment variables provided.\n")
1176 | envs =>
1177 (write "\tSetEnvIfNoCase\t\"";
1178 write attribute;
1179 write "\"\t\"";
1180 write match;
1181 write "\"";
1182 app (fn env => (write "\t"; write env)) envs;
1183 write "\n"))
1184
1185val () = Env.action_one "diskCache"
1186 ("path", Env.string)
1187 (fn path => (write "\tCacheEnable disk \"";
1188 write path;
1189 write "\"\n"))
1190
1191val () = Env.action_one "phpVersion"
1192 ("version", php_version)
1193 (fn version => (write "\tAddHandler fcgid-script .php .phtml\n";
1194 (* FIXME: only set kerberos wrapper of waklog is on *)
1195 (* won't be trivial, since we don't have access to node here *)
1196 write "\n\tFcgidWrapper \"";
1197 write (Config.Apache.fastCgiWrapperOf (Domain.getUser ()));
1198 write " ";
1199 write (Config.Apache.phpFastCgiWrapper version);
1200 write "\" .php .phtml\n"))
1201
1202val () = Env.action_two "addType"
1203 ("mime type", Env.string, "extension", Env.string)
1204 (fn (mt, ext) => (write "\tAddType ";
1205 write mt;
1206 write " ";
1207 write ext;
1208 write "\n"))
1209
1210val filter = fn (EVar "includes", _) => SOME "INCLUDES"
1211 | (EVar "deflate", _) => SOME "DEFLATE"
1212 | _ => NONE
1213
1214val () = Env.action_two "addOutputFilter"
1215 ("filters", Env.list filter, "extensions", Env.list Env.string)
1216 (fn (f :: fs, exts as (_ :: _)) =>
1217 (write "\tAddOutputFilter ";
1218 write f;
1219 app (fn f => (write ";"; write f)) fs;
1220 app (fn ext => (write " "; write ext)) exts;
1221 write "\n")
1222 | _ => ())
1223
1224val () = Env.action_one "sslCertificateChainFile"
1225 ("ssl_cacert_path", Env.string)
1226 (fn cacert =>
1227 if !sslEnabled then
1228 (write "\tSSLCertificateChainFile \"";
1229 write cacert;
1230 write "\"\n")
1231 else
1232 print "WARNING: Skipped sslCertificateChainFile because this isn't an SSL vhost.\n")
1233
1234val () = Domain.registerResetLocal (fn () =>
1235 ignore (OS.Process.system (Config.rm ^ " -rf " ^ Config.Apache.confDir ^ "/*")))
1236
1237val () = Domain.registerDescriber (Domain.considerAll
1238 [Domain.Extension {extension = "vhost",
1239 heading = fn host => "Web vhost " ^ host ^ ":"},
1240 Domain.Extension {extension = "vhost_ssl",
1241 heading = fn host => "SSL web vhost " ^ host ^ ":"}])
1242
1243val () = Env.action_one "allowEncodedSlashes"
1244 ("enable", Env.bool)
1245 (fn enable => (write "\tAllowEncodedSlashes ";
1246 write (if enable then "NoDecode" else "Off");
1247 write "\n"))
1248val () = Env.action_none "testNoHtaccess"
1249 (fn path => write "\tAllowOverride None\n")
1250
1251fun writeWaklogUserFile () =
1252 let
1253 val users = Acl.users ()
1254 val outf = TextIO.openOut Config.Apache.waklogUserFile
1255 in
1256 app (fn user => if String.isSuffix "_admin" user then
1257 ()
1258 else
1259 (TextIO.output (outf, "<Location /~");
1260 TextIO.output (outf, user);
1261 TextIO.output (outf, ">\n\tWaklogEnabled on\n\tWaklogLocationPrincipal ");
1262 TextIO.output (outf, user);
1263 TextIO.output (outf, "/daemon@HCOOP.NET /etc/keytabs/user.daemon/");
1264 TextIO.output (outf, user);
1265 TextIO.output (outf, "\n</Location>\n\n"))) users;
1266 TextIO.closeOut outf
1267 end
1268
1269val () = Domain.registerOnUsersChange writeWaklogUserFile
1270
1271end