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