a490419fa6141186cf4f4273b01d7bbe54550bc0
[hcoop/domtool2.git] / src / plugins / apache.sml
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
22 structure Apache :> APACHE = struct
23
24 open Ast
25
26 val dl = ErrorMsg.dummyLoc
27
28 fun 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
33 val _ = Env.type_one "web_node"
34 Env.string
35 webNode
36
37 val _ = Env.registerFunction ("web_node_to_node",
38 fn [e] => SOME e
39 | _ => NONE)
40
41 fun 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
47 fun webPlaceDefault node = (EApp ((EVar "web_place_default", dl), (EString node, dl)), dl)
48
49 val _ = Env.registerFunction ("web_place_to_web_node",
50 fn [e] => Option.map (fn (node, _, _) => (EString node, dl)) (webPlace e)
51 | _ => NONE)
52
53 val _ = Env.registerFunction ("web_place_to_node",
54 fn [e] => Option.map (fn (node, _, _) => (EString node, dl)) (webPlace e)
55 | _ => NONE)
56
57 val _ = Env.registerFunction ("web_place_to_ip",
58 fn [e] => Option.map (fn (_, ip, _) => (EString ip, dl)) (webPlace e)
59 | _ => NONE)
60
61 val _ = Env.registerFunction ("web_place_to_ipv6",
62 fn [e] => Option.map (fn (_, _, ipv6) => (EString ipv6, dl)) (webPlace e)
63 | _ => NONE)
64
65 val _ = Env.type_one "proxy_port"
66 Env.int
67 (fn n => n > 1024)
68
69 fun 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
90 val _ = Env.type_one "proxy_target"
91 Env.string
92 (validProxyTarget (fn s => List.exists (fn s' => s = s') (Config.Apache.proxyTargets @ ["!"])))
93
94 val _ = Env.type_one "proxy_reverse_target"
95 Env.string
96 (validProxyTarget (fn s => List.exists (fn s' => s = s') Config.Apache.proxyTargets))
97
98 val _ = 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
103 val _ = Env.type_one "suexec_flag"
104 Env.bool
105 (fn b => b orelse Domain.hasPriv "www")
106
107 val _ = Env.type_one "regexp"
108 Env.string
109 Pcre.validRegexp
110
111 fun 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
120 val _ = Env.type_one "location"
121 Env.string
122 validLocation
123
124 fun validCert s = Acl.query {user = Domain.getUser (),
125 class = "cert",
126 value = s}
127
128 fun validCaCert s = Acl.query {user = Domain.getUser (),
129 class = "cacert",
130 value = s}
131
132 val _ = Env.type_one "ssl_cert_path"
133 Env.string
134 validCert
135
136 val _ = Env.type_one "ssl_cacert_path"
137 Env.string
138 validCaCert
139
140 fun 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
145 fun validExtension s =
146 size s > 0
147 andalso size s < 20
148 andalso CharVector.all (fn ch => Char.isAlphaNum ch orelse ch = #"_") s
149
150 val _ = Env.type_one "file_extension"
151 Env.string
152 validExtension
153
154 val _ = Env.registerFunction ("defaultServerAdmin",
155 fn [] => SOME (EString (Domain.getUser () ^ "@" ^ Config.defaultDomain), dl)
156 | _ => NONE)
157
158 val 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
171 val 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
195 val cond_flag = fn (EVar "cond_nocase", _) => SOME "NC"
196 | (EVar "ornext", _) => SOME "OR"
197 | _ => NONE
198
199 val 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
206 val autoindex_width = fn (EVar "autofit", _) => SOME "*"
207 | (EApp ((EVar "characters", _), n), _) =>
208 Option.map Int.toString (Env.int n)
209 | _ => NONE
210
211 val 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
243 val interval_base = fn (EVar "access", _) => SOME "access"
244 | (EVar "modification", _) => SOME "modification"
245 | _ => NONE
246
247 val 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
256 val vhostsChanged = ref false
257 val logDeleted = ref false
258 val delayedLogMoves = ref (fn () => ())
259
260 val () = Slave.registerPreHandler
261 (fn () => (vhostsChanged := false;
262 logDeleted := false;
263 delayedLogMoves := (fn () => print "Executing delayed log moves/deletes.\n")))
264
265 fun 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
284 val webNodes_full = Config.Apache.webNodes_all @ Config.Apache.webNodes_admin
285
286 fun isVersion1 node =
287 List.exists (fn (n, {version = ConfigTypes.APACHE_1_3, ...}) => n = node
288 | _ => false) webNodes_full
289
290 fun imVersion1 () = isVersion1 (Slave.hostname ())
291
292 fun isWaklog node =
293 List.exists (fn (n, {auth = ConfigTypes.MOD_WAKLOG, ...}) => n = node
294 | _ => false) webNodes_full
295
296 fun down () = if imVersion1 () then Config.Apache.down1 else Config.Apache.down
297 fun undown () = if imVersion1 () then Config.Apache.undown1 else Config.Apache.undown
298 fun reload () = if imVersion1 () then Config.Apache.reload1 else Config.Apache.reload
299 fun fixperms () = if imVersion1 () then Config.Apache.fixperms1 else Config.Apache.fixperms
300
301 fun logDir {user, node, vhostId} =
302 String.concat [Config.Apache.logDirOf (isVersion1 node) user,
303 "/",
304 node,
305 "/",
306 vhostId]
307
308 fun realLogDir {user, node, vhostId} =
309 String.concat [Config.Apache.realLogDirOf user,
310 "/",
311 node,
312 "/",
313 vhostId]
314
315 val () = 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
422 val () = 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
431 val vhostFiles : (string * TextIO.outstream) list ref = ref []
432 fun write' s = app (fn (node, file) => TextIO.output (file, s node)) (!vhostFiles)
433 fun write s = app (fn (_, file) => TextIO.output (file, s)) (!vhostFiles)
434
435 val rewriteEnabled = ref false
436 val localRewriteEnabled = ref false
437 val expiresEnabled = ref false
438 val localExpiresEnabled = ref false
439 val currentVhost = ref ""
440 val currentVhostId = ref ""
441 val sslEnabled = ref false
442
443 val pre = ref (fn _ : {user : string, nodes : string list, id : string, hostname : string} => ())
444 fun registerPre f =
445 let
446 val old = !pre
447 in
448 pre := (fn x => (old x; f x))
449 end
450
451 val post = ref (fn () => ())
452 fun registerPost f =
453 let
454 val old = !post
455 in
456 post := (fn () => (old (); f ()))
457 end
458
459 fun doPre x = !pre x
460 fun doPost () = !post ()
461
462 val aliaser = ref (fn _ : string => ())
463 fun registerAliaser f =
464 let
465 val old = !aliaser
466 in
467 aliaser := (fn x => (old x; f x))
468 end
469
470 fun vhostPost () = (!post ();
471 write "</VirtualHost>\n";
472 app (TextIO.closeOut o #2) (!vhostFiles))
473
474 val php_version = fn (EVar "php56", _) => SOME 56
475 | (EVar "php72", _) => SOME 72
476 | (EVar "php73", _) => SOME 73
477 | _ => NONE
478
479 fun vhostBody (env, makeFullHost) =
480 let
481 val places = Env.env (Env.list webPlace) (env, "WebPlaces")
482
483 val ssl = Env.env ssl (env, "SSL")
484 val user = Env.env Env.string (env, "User")
485 val group = Env.env Env.string (env, "Group")
486 val docroot = Env.env Env.string (env, "DocumentRoot")
487 val sadmin = Env.env Env.string (env, "ServerAdmin")
488 val suexec = Env.env Env.bool (env, "SuExec")
489 val php = Env.env php_version (env, "PhpVersion")
490
491 val fullHost = makeFullHost (Domain.currentDomain ())
492 val vhostId = fullHost ^ (if Option.isSome ssl then ".ssl" else "")
493 val confFile = fullHost ^ (if Option.isSome ssl then ".vhost_ssl" else ".vhost")
494 in
495 currentVhost := fullHost;
496 currentVhostId := vhostId;
497 sslEnabled := Option.isSome ssl;
498
499 rewriteEnabled := false;
500 localRewriteEnabled := false;
501 expiresEnabled := false;
502 localExpiresEnabled := false;
503 vhostFiles := map (fn (node, ip, ipv6) =>
504 let
505 val file = Domain.domainFile {node = node,
506 name = confFile}
507
508 val ld = logDir {user = user, node = node, vhostId = vhostId}
509 in
510 TextIO.output (file, "# Owner: ");
511 TextIO.output (file, user);
512 TextIO.output (file, "\n<VirtualHost ");
513
514 TextIO.output (file, ip);
515 TextIO.output (file, ":");
516 TextIO.output (file, case ssl of
517 SOME _ => "443"
518 | NONE => "80");
519
520 TextIO.output (file, " [");
521 TextIO.output (file, ipv6);
522 TextIO.output (file, "]");
523 TextIO.output (file, ":");
524 TextIO.output (file, case ssl of
525 SOME _ => "443"
526 | NONE => "80");
527
528 TextIO.output (file, ">\n");
529 TextIO.output (file, "\tErrorLog ");
530 TextIO.output (file, ld);
531 TextIO.output (file, "/error.log\n\tCustomLog ");
532 TextIO.output (file, ld);
533 TextIO.output (file, "/access.log combined\n");
534 TextIO.output (file, "\tServerName ");
535 TextIO.output (file, fullHost);
536 app
537 (fn dom => (TextIO.output (file, "\n\tServerAlias ");
538 TextIO.output (file, makeFullHost dom)))
539 (Domain.currentAliasDomains ());
540
541 if suexec then
542 if isVersion1 node then
543 (TextIO.output (file, "\n\tUser ");
544 TextIO.output (file, user);
545 TextIO.output (file, "\n\tGroup ");
546 TextIO.output (file, group))
547 else
548 (TextIO.output (file, "\n\tSuexecUserGroup ");
549 TextIO.output (file, user);
550 TextIO.output (file, " ");
551 TextIO.output (file, group))
552 else
553 ();
554
555 if isWaklog node then
556 (TextIO.output (file, "\n\tWaklogEnabled on\n\tWaklogLocationPrincipal ");
557 TextIO.output (file, user);
558 TextIO.output (file, "/daemon@HCOOP.NET /etc/keytabs/user.daemon/");
559 TextIO.output (file, user))
560 else
561 ();
562
563 TextIO.output (file, "\n\tDAVLockDB /var/lock/apache2/dav/");
564 TextIO.output (file, user);
565 TextIO.output (file, "/DAVLock");
566
567 TextIO.output (file, "\n\tAddHandler fcgid-script .php .phtml");
568 map (fn ext => (TextIO.output (file, "\n\tFcgidWrapper \"");
569 (* kerberos wrapper, simulates waklog+mod_cgi *)
570 if isWaklog node then
571 (TextIO.output (file, Config.Apache.fastCgiWrapperOf user);
572 TextIO.output (file, " "))
573 else
574 ();
575 TextIO.output (file, Config.Apache.phpFastCgiWrapper php);
576 TextIO.output (file, "\" ");
577 TextIO.output (file, ext)))
578 [".php", ".phtml"];
579 (ld, file)
580 end)
581 places;
582 write "\n\tDocumentRoot ";
583 write docroot;
584 write "\n\tServerAdmin ";
585 write sadmin;
586 case ssl of
587 SOME cert =>
588 (write "\n\tSSLEngine on\n\tSSLCertificateFile ";
589 write cert)
590 | NONE => ();
591 write "\n";
592 !pre {user = user, nodes = map #1 places, id = vhostId, hostname = fullHost};
593 app (fn dom => !aliaser (makeFullHost dom)) (Domain.currentAliasDomains ())
594 end
595
596 val () = Env.containerV_one "vhost"
597 ("host", Env.string)
598 (fn (env, host) => vhostBody (env, fn dom => host ^ "." ^ dom),
599 vhostPost)
600
601 val () = Env.containerV_none "vhostDefault"
602 (fn env => vhostBody (env, fn dom => dom),
603 vhostPost)
604
605 val inLocal = ref false
606
607 val () = Env.container_one "location"
608 ("prefix", Env.string)
609 (fn prefix =>
610 (write "\t<Location ";
611 write prefix;
612 write ">\n";
613 inLocal := true),
614 fn () => (write "\t</Location>\n";
615 inLocal := false;
616 localRewriteEnabled := false;
617 localExpiresEnabled := false))
618
619 val () = Env.container_one "directory"
620 ("directory", Env.string)
621 (fn directory =>
622 (write "\t<Directory ";
623 write directory;
624 write ">\n";
625 inLocal := true),
626 fn () => (write "\t</Directory>\n";
627 inLocal := false;
628 localRewriteEnabled := false;
629 localExpiresEnabled := false))
630
631 val () = Env.container_one "filesMatch"
632 ("regexp", Env.string)
633 (fn prefix =>
634 (write "\t<FilesMatch \"";
635 write prefix;
636 write "\">\n"),
637 fn () => (write "\t</FilesMatch>\n";
638 localRewriteEnabled := false;
639 localExpiresEnabled := false))
640
641 fun checkRewrite () =
642 if !inLocal then
643 if !localRewriteEnabled then
644 ()
645 else
646 (write "\tRewriteEngine on\n";
647 localRewriteEnabled := true)
648 else if !rewriteEnabled then
649 ()
650 else
651 (write "\tRewriteEngine on\n";
652 rewriteEnabled := true)
653
654 fun checkExpires () =
655 if !inLocal then
656 if !localExpiresEnabled then
657 ()
658 else
659 (write "\tExpiresActive on\n";
660 localExpiresEnabled := true)
661 else if !expiresEnabled then
662 ()
663 else
664 (write "\tExpiresActive on\n";
665 expiresEnabled := true)
666
667 val () = Env.action_four "proxyRewrite"
668 ("from", Env.string, "tohost", Env.string, "to", Env.string, "flags", Env.list flag)
669 (fn (from, tohost, to, flags) =>
670 (checkRewrite ();
671 write "\tRewriteRule\t\"";
672 write from;
673 write "\"\t\"";
674 write tohost;
675 write "/"; (* ensure rewrite rule can't change port *)
676 write to;
677 write "\"";
678 write " [P";
679 case flags of
680 [] => ()
681 | flag::rest => (write ",";
682 write flag;
683 app (fn flag => (write ",";
684 write flag)) rest);
685
686 write "]\n"))
687
688 val () = Env.action_four "expiresByType"
689 ("mime", Env.string, "base", interval_base, "num", Env.int, "inter", interval)
690 (fn (mime, base, num, inter) =>
691 (checkExpires ();
692 write "\tExpiresByType\t\"";
693 write mime;
694 write "\"\t\"";
695 write base;
696 write " plus ";
697 if num < 0 then
698 (write "-";
699 write (Int.toString (~num)))
700 else
701 write (Int.toString num);
702 write " ";
703 write inter;
704 write "\"\n"))
705
706 val () = Env.action_two "proxyPass"
707 ("from", Env.string, "to", Env.string)
708 (fn (from, to) =>
709 (write "\tProxyPass\t";
710 write from;
711 write "\t";
712 write to;
713 write "\tretry=0\n"))
714
715 val () = Env.action_two "proxyPassReverse"
716 ("from", Env.string, "to", Env.string)
717 (fn (from, to) =>
718 (write "\tProxyPassReverse\t";
719 write from;
720 write "\t";
721 write to;
722 write "\n"))
723
724 val () = Env.action_one "proxyPreserveHost"
725 ("enable", Env.bool)
726 (fn (enable) =>
727 (write "\tProxyPreserveHost\t";
728 if enable then write "On" else write "Off";
729 write "\n"))
730
731 val () = Env.action_three "rewriteRule"
732 ("from", Env.string, "to", Env.string, "flags", Env.list flag)
733 (fn (from, to, flags) =>
734 (checkRewrite ();
735 write "\tRewriteRule\t\"";
736 write from;
737 write "\"\t\"";
738 write to;
739 write "\"";
740 case flags of
741 [] => ()
742 | flag::rest => (write " [";
743 write flag;
744 app (fn flag => (write ",";
745 write flag)) rest;
746 write "]");
747 write "\n"))
748
749 val () = Env.action_three "rewriteCond"
750 ("test", Env.string, "pattern", Env.string, "flags", Env.list cond_flag)
751 (fn (from, to, flags) =>
752 (checkRewrite ();
753 write "\tRewriteCond\t\"";
754 write from;
755 write "\"\t\"";
756 write to;
757 write "\"";
758 case flags of
759 [] => ()
760 | flag::rest => (write " [";
761 write flag;
762 app (fn flag => (write ",";
763 write flag)) rest;
764 write "]");
765 write "\n"))
766
767 val () = Env.action_one "rewriteBase"
768 ("prefix", Env.string)
769 (fn prefix =>
770 (checkRewrite ();
771 write "\tRewriteBase\t\"";
772 write prefix;
773 write "\"\n"))
774
775 val _ = Env.type_one "mod_rewrite_trace_level"
776 Env.int
777 (fn n => n > 0 andalso n <= 8)
778
779 val () = Env.action_one "rewriteLogLevel"
780 ("level", Env.int)
781 (fn 0 =>
782 (checkRewrite ();
783 write "\tLogLevel rewrite:warn\n")
784 | level =>
785 (checkRewrite ();
786 write "\tLogLevel rewrite:trace";
787 write (Int.toString level);
788 write "\n"))
789
790 val () = Env.action_two "alias"
791 ("from", Env.string, "to", Env.string)
792 (fn (from, to) =>
793 (write "\tAlias\t";
794 write from;
795 write " ";
796 write to;
797 write "\n"))
798
799 val () = Env.action_two "scriptAlias"
800 ("from", Env.string, "to", Env.string)
801 (fn (from, to) =>
802 (write "\tScriptAlias\t";
803 write from;
804 write " ";
805 write to;
806 write "\n"))
807
808 val () = Env.action_two "fastScriptAlias"
809 ("from", Env.string, "to", Env.string)
810 (fn (from, to) =>
811 let
812 (* mod_fcgid + kerberos limit this to working with
813 individual fcgi programs. assume the target path is a
814 file and any trailing `/' is just aliasing
815 syntax. Directory+File on the script is used to
816 activate fcgid instead of Location on the alias to
817 limit effects (alias+location also match in inverse
818 order causing pernicious side-effects *)
819 val fcgi_path = if String.sub (to, size to - 1) = #"/"
820 then
821 String.substring (to, 0, size to - 1)
822 else
823 to
824 val fcgi_dir = OS.Path.dir fcgi_path
825 val fcgi_file = OS.Path.file fcgi_path
826 in
827 write "\tAlias\t"; write from; write " "; write to; write "\n";
828
829 write "\t<Directory "; write fcgi_dir; write ">\n";
830 write "\t<Files "; write fcgi_file; write ">\n";
831 write "\tSetHandler fcgid-script\n";
832
833 (* FIXME: only set kerberos wrapper of waklog is on *)
834 (* won't be trivial, since we don't have access to node here *)
835 write "\tFcgidWrapper \"";
836 write (Config.Apache.fastCgiWrapperOf (Domain.getUser ()));
837 write " ";
838 write fcgi_path;
839 write "\"\n";
840
841 write "\t</Files>\n\t</Directory>\n"
842 end)
843
844 val () = Env.action_two "errorDocument"
845 ("code", Env.string, "handler", Env.string)
846 (fn (code, handler) =>
847 let
848 val hasSpaces = CharVector.exists Char.isSpace handler
849
850 fun maybeQuote () =
851 if hasSpaces then
852 write "\""
853 else
854 ()
855 in
856 write "\tErrorDocument\t";
857 write code;
858 write " ";
859 maybeQuote ();
860 write handler;
861 maybeQuote ();
862 write "\n"
863 end)
864
865 val () = Env.action_one "options"
866 ("options", Env.list apache_option)
867 (fn opts =>
868 case opts of
869 [] => ()
870 | _ => (write "\tOptions";
871 app (fn opt => (write " "; write opt)) opts;
872 write "\n"))
873
874 val () = Env.action_one "set_options"
875 ("options", Env.list apache_option)
876 (fn opts =>
877 case opts of
878 [] => ()
879 | _ => (write "\tOptions";
880 app (fn opt => (write " +"; write opt)) opts;
881 write "\n"))
882
883 val () = Env.action_one "unset_options"
884 ("options", Env.list apache_option)
885 (fn opts =>
886 case opts of
887 [] => ()
888 | _ => (write "\tOptions";
889 app (fn opt => (write " -"; write opt)) opts;
890 write "\n"))
891
892 val () = Env.action_one "cgiExtension"
893 ("extension", Env.string)
894 (fn ext => (write "\tAddHandler cgi-script ";
895 write ext;
896 write "\n"))
897
898 val () = Env.action_one "directoryIndex"
899 ("filenames", Env.list Env.string)
900 (fn opts =>
901 (write "\tDirectoryIndex";
902 app (fn opt => (write " "; write opt)) opts;
903 write "\n"))
904
905 val () = Env.action_one "directorySlash"
906 ("enable", Env.bool)
907 (fn enable =>
908 (write "\tDirectorySlash ";
909 if enable then write "On" else write "Off";
910 write "\n"))
911
912 val () = Env.action_one "serverAliasHost"
913 ("host", Env.string)
914 (fn host =>
915 (write "\tServerAlias ";
916 write host;
917 write "\n";
918 !aliaser host))
919
920 val () = Env.action_one "serverAlias"
921 ("host", Env.string)
922 (fn host =>
923 (app
924 (fn dom =>
925 let
926 val full = host ^ "." ^ dom
927 in
928 write "\tServerAlias ";
929 write full;
930 write "\n";
931 !aliaser full
932 end)
933 (Domain.currentDomains ())))
934
935 val () = Env.action_none "serverAliasDefault"
936 (fn () =>
937 (app
938 (fn dom =>
939 (write "\tServerAlias ";
940 write dom;
941 write "\n";
942 !aliaser dom))
943 (Domain.currentDomains ())))
944
945 val authType = fn (EVar "basic", _) => SOME "basic"
946 | (EVar "digest", _) => SOME "digest"
947 | (EVar "kerberos", _) => SOME "kerberos"
948 | _ => NONE
949
950 fun allowAuthType "kerberos" = !sslEnabled
951 | allowAuthType _ = true
952
953 val () = Env.action_one "authType"
954 ("type", authType)
955 (fn ty =>
956 if allowAuthType ty then
957 (write "\tAuthType ";
958 write ty;
959 write "\n";
960 case ty of
961 "kerberos" =>
962 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"
963 | _ => ())
964 else
965 print "WARNING: Skipped Kerberos authType because this isn't an SSL vhost.\n")
966
967 val () = Env.action_one "authName"
968 ("name", Env.string)
969 (fn name =>
970 (write "\tAuthName \"";
971 write name;
972 write "\"\n"))
973
974 val () = Env.action_one "authUserFile"
975 ("file", Env.string)
976 (fn name =>
977 (write "\tAuthUserFile ";
978 write name;
979 write "\n"))
980
981 val () = Env.action_one "authGroupFile"
982 ("file", Env.string)
983 (fn name =>
984 (write "\tAuthGroupFile ";
985 write name;
986 write "\n"))
987
988 val () = Env.action_none "requireValidUser"
989 (fn () => write "\tRequire valid-user\n")
990
991 val () = Env.action_one "requireUser"
992 ("users", Env.list Env.string)
993 (fn names =>
994 case names of
995 [] => ()
996 | _ => (write "\tRequire user";
997 app (fn name => (write " "; write name)) names;
998 write "\n"))
999
1000 val () = Env.action_one "requireGroup"
1001 ("groups", Env.list Env.string)
1002 (fn names =>
1003 case names of
1004 [] => ()
1005 | _ => (write "\tRequire group";
1006 app (fn name => (write " "; write name)) names;
1007 write "\n"))
1008
1009 val () = Env.action_none "orderAllowDeny"
1010 (fn () => write "\tOrder allow,deny\n")
1011
1012 val () = Env.action_none "orderDenyAllow"
1013 (fn () => write "\tOrder deny,allow\n")
1014
1015 val () = Env.action_none "allowFromAll"
1016 (fn () => write "\tAllow from all\n")
1017
1018 val () = Env.action_one "allowFrom"
1019 ("entries", Env.list Env.string)
1020 (fn names =>
1021 case names of
1022 [] => ()
1023 | _ => (write "\tAllow from";
1024 app (fn name => (write " "; write name)) names;
1025 write "\n"))
1026
1027 val () = Env.action_none "denyFromAll"
1028 (fn () => write "\tDeny from all\n")
1029
1030 val () = Env.action_one "denyFrom"
1031 ("entries", Env.list Env.string)
1032 (fn names =>
1033 case names of
1034 [] => ()
1035 | _ => (write "\tDeny from";
1036 app (fn name => (write " "; write name)) names;
1037 write "\n"))
1038
1039 val () = Env.action_none "satisfyAll"
1040 (fn () => write "\tSatisfy all\n")
1041
1042 val () = Env.action_none "satisfyAny"
1043 (fn () => write "\tSatisfy any\n")
1044
1045 val () = Env.action_one "forceType"
1046 ("type", Env.string)
1047 (fn ty => (write "\tForceType ";
1048 write ty;
1049 write "\n"))
1050
1051 val () = Env.action_none "forceTypeOff"
1052 (fn () => write "\tForceType None\n")
1053
1054 val () = Env.action_two "action"
1055 ("what", Env.string, "how", Env.string)
1056 (fn (what, how) => (write "\tAction ";
1057 write what;
1058 write " ";
1059 write how;
1060 write "\n"))
1061
1062 val () = Env.action_one "addDefaultCharset"
1063 ("charset", Env.string)
1064 (fn ty => (write "\tAddDefaultCharset ";
1065 write ty;
1066 write "\n"))
1067
1068 (*val () = Env.action_one "davSvn"
1069 ("path", Env.string)
1070 (fn path => (write "\tDAV svn\n\tSVNPath ";
1071 write path;
1072 write "\n"))
1073
1074 val () = Env.action_one "authzSvnAccessFile"
1075 ("path", Env.string)
1076 (fn path => (write "\tAuthzSVNAccessFile ";
1077 write path;
1078 write "\n"))*)
1079
1080 val () = Env.action_none "davFilesystem"
1081 (fn path => write "\tDAV filesystem\n")
1082
1083 val () = Env.action_two "addDescription"
1084 ("description", Env.string, "patterns", Env.list Env.string)
1085 (fn (desc, pats) =>
1086 case pats of
1087 [] => ()
1088 | _ => (write "\tAddDescription \"";
1089 write (String.toString desc);
1090 write "\"";
1091 app (fn pat => (write " "; write pat)) pats;
1092 write "\n"))
1093
1094 val () = Env.action_two "addIcon"
1095 ("icon", Env.string, "patterns", Env.list Env.string)
1096 (fn (icon, pats) =>
1097 case pats of
1098 [] => ()
1099 | _ => (write "\tAddIcon \"";
1100 write icon;
1101 write "\"";
1102 app (fn pat => (write " "; write pat)) pats;
1103 write "\n"))
1104
1105 val () = Env.action_one "indexOptions"
1106 ("options", Env.list autoindex_option)
1107 (fn opts =>
1108 case opts of
1109 [] => ()
1110 | _ => (write "\tIndexOptions";
1111 app (fn (opt, arg) =>
1112 (write " ";
1113 write opt;
1114 Option.app (fn arg =>
1115 (write "="; write arg)) arg)) opts;
1116 write "\n"))
1117
1118 val () = Env.action_one "indexIgnore"
1119 ("patterns", Env.list Env.string)
1120 (fn pats =>
1121 case pats of
1122 [] => ()
1123 | _ => (write "\tIndexIgnore";
1124 app (fn pat => (write " "; write pat)) pats;
1125 write "\n"))
1126
1127 val () = Env.action_one "set_indexOptions"
1128 ("options", Env.list autoindex_option)
1129 (fn opts =>
1130 case opts of
1131 [] => ()
1132 | _ => (write "\tIndexOptions";
1133 app (fn (opt, arg) =>
1134 (write " +";
1135 write opt;
1136 Option.app (fn arg =>
1137 (write "="; write arg)) arg)) opts;
1138 write "\n"))
1139
1140 val () = Env.action_one "unset_indexOptions"
1141 ("options", Env.list autoindex_option)
1142 (fn opts =>
1143 case opts of
1144 [] => ()
1145 | _ => (write "\tIndexOptions";
1146 app (fn (opt, _) =>
1147 (write " -";
1148 write opt)) opts;
1149 write "\n"))
1150
1151 val () = Env.action_one "headerName"
1152 ("name", Env.string)
1153 (fn name => (write "\tHeaderName ";
1154 write name;
1155 write "\n"))
1156
1157 val () = Env.action_one "readmeName"
1158 ("name", Env.string)
1159 (fn name => (write "\tReadmeName ";
1160 write name;
1161 write "\n"))
1162
1163 val () = Env.action_two "setEnv"
1164 ("key", Env.string, "value", Env.string)
1165 (fn (key, value) => (write "\tSetEnv \"";
1166 write key;
1167 write "\" \"";
1168 write (String.translate (fn #"\"" => "\\\""
1169 | ch => str ch) value);
1170 write "\"\n"))
1171
1172 val () = Env.action_three "setEnvIf"
1173 ("attribute", Env.string, "match", Env.string, "env_variables", Env.list Env.string)
1174 (fn (attribute, match, envs) =>
1175 case envs of
1176 [] => (print "WARNING: Skipped setEnvIf, no environment variables provided.\n")
1177 | envs =>
1178 (write "\tSetEnvIf\t\"";
1179 write attribute;
1180 write "\"\t\"";
1181 write match;
1182 write "\"";
1183 app (fn env => (write "\t"; write env)) envs;
1184 write "\n"))
1185
1186 val () = Env.action_three "setEnvIfNoCase"
1187 ("attribute", Env.string, "match", Env.string, "env_variables", Env.list Env.string)
1188 (fn (attribute, match, envs) =>
1189 case envs of
1190 [] => (print "WARNING: Skipped setEnvIfNoCase, no environment variables provided.\n")
1191 | envs =>
1192 (write "\tSetEnvIfNoCase\t\"";
1193 write attribute;
1194 write "\"\t\"";
1195 write match;
1196 write "\"";
1197 app (fn env => (write "\t"; write env)) envs;
1198 write "\n"))
1199
1200 val () = Env.action_one "diskCache"
1201 ("path", Env.string)
1202 (fn path => (write "\tCacheEnable disk \"";
1203 write path;
1204 write "\"\n"))
1205
1206 val () = Env.action_one "phpVersion"
1207 ("version", php_version)
1208 (fn version => (write "\tAddHandler fcgid-script .php .phtml\n";
1209 (* FIXME: only set kerberos wrapper of waklog is on *)
1210 (* won't be trivial, since we don't have access to node here *)
1211 write "\n\tFcgidWrapper \"";
1212 write (Config.Apache.fastCgiWrapperOf (Domain.getUser ()));
1213 write " ";
1214 write (Config.Apache.phpFastCgiWrapper version);
1215 write "\" .php .phtml\n"))
1216
1217 val () = Env.action_two "addType"
1218 ("mime type", Env.string, "extension", Env.string)
1219 (fn (mt, ext) => (write "\tAddType ";
1220 write mt;
1221 write " ";
1222 write ext;
1223 write "\n"))
1224
1225 val filter = fn (EVar "includes", _) => SOME "INCLUDES"
1226 | (EVar "deflate", _) => SOME "DEFLATE"
1227 | _ => NONE
1228
1229 val () = Env.action_two "addOutputFilter"
1230 ("filters", Env.list filter, "extensions", Env.list Env.string)
1231 (fn (f :: fs, exts as (_ :: _)) =>
1232 (write "\tAddOutputFilter ";
1233 write f;
1234 app (fn f => (write ";"; write f)) fs;
1235 app (fn ext => (write " "; write ext)) exts;
1236 write "\n")
1237 | _ => ())
1238
1239 val () = Env.action_one "sslCertificateChainFile"
1240 ("ssl_cacert_path", Env.string)
1241 (fn cacert =>
1242 if !sslEnabled then
1243 (write "\tSSLCertificateChainFile \"";
1244 write cacert;
1245 write "\"\n")
1246 else
1247 print "WARNING: Skipped sslCertificateChainFile because this isn't an SSL vhost.\n")
1248
1249 val () = Domain.registerResetLocal (fn () =>
1250 ignore (OS.Process.system (Config.rm ^ " -rf " ^ Config.Apache.confDir ^ "/*")))
1251
1252 val () = Domain.registerDescriber (Domain.considerAll
1253 [Domain.Extension {extension = "vhost",
1254 heading = fn host => "Web vhost " ^ host ^ ":"},
1255 Domain.Extension {extension = "vhost_ssl",
1256 heading = fn host => "SSL web vhost " ^ host ^ ":"}])
1257
1258 val () = Env.action_one "allowEncodedSlashes"
1259 ("enable", Env.bool)
1260 (fn enable => (write "\tAllowEncodedSlashes ";
1261 write (if enable then "NoDecode" else "Off");
1262 write "\n"))
1263 val () = Env.action_none "testNoHtaccess"
1264 (fn path => write "\tAllowOverride None\n")
1265
1266 fun writeWaklogUserFile () =
1267 let
1268 val users = Acl.users ()
1269 val outf = TextIO.openOut Config.Apache.waklogUserFile
1270 in
1271 app (fn user => if String.isSuffix "_admin" user then
1272 ()
1273 else
1274 (TextIO.output (outf, "<Location /~");
1275 TextIO.output (outf, user);
1276 TextIO.output (outf, ">\n\tWaklogEnabled on\n\tWaklogLocationPrincipal ");
1277 TextIO.output (outf, user);
1278 TextIO.output (outf, "/daemon@HCOOP.NET /etc/keytabs/user.daemon/");
1279 TextIO.output (outf, user);
1280 TextIO.output (outf, "\n</Location>\n\n"))) users;
1281 TextIO.closeOut outf
1282 end
1283
1284 val () = Domain.registerOnUsersChange writeWaklogUserFile
1285
1286 end