mod_autoindex
[hcoop/domtool2.git] / src / plugins / apache.sml
... / ...
CommitLineData
1(* HCoop Domtool (http://hcoop.sourceforge.net/)
2 * Copyright (c) 2006, Adam Chlipala
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 *)
18
19(* Apache HTTPD handling *)
20
21structure Apache :> APACHE = struct
22
23open Ast
24
25val _ = Env.type_one "proxy_port"
26 Env.int
27 (fn n => n > 1024)
28
29val _ = Env.type_one "proxy_target"
30 Env.string
31 (fn s =>
32 let
33 fun default () = List.exists (fn s' => s = s') Config.Apache.proxyTargets
34 in
35 case String.fields (fn ch => ch = #":") s of
36 ["http", "//localhost", rest] =>
37 (case String.fields (fn ch => ch = #"/") rest of
38 port :: _ =>
39 (case Int.fromString port of
40 NONE => default ()
41 | SOME n => n > 1024 orelse default ())
42 | _ => default ())
43 | _ => default ()
44 end)
45
46val _ = Env.type_one "rewrite_arg"
47 Env.string
48 (CharVector.all Char.isAlphaNum)
49
50fun validLocation s =
51 size s > 0 andalso size s < 1000 andalso CharVector.all
52 (fn ch => Char.isAlphaNum ch
53 orelse ch = #"-"
54 orelse ch = #"_"
55 orelse ch = #"."
56 orelse ch = #"/") s
57
58val _ = Env.type_one "location"
59 Env.string
60 validLocation
61
62val dl = ErrorMsg.dummyLoc
63
64val _ = Main.registerDefault ("WebNodes",
65 (TList (TBase "node", dl), dl),
66 (fn () => (EList (map (fn s => (EString s, dl)) Config.Apache.webNodes), dl)))
67
68val _ = Main.registerDefault ("SSL",
69 (TBase "bool", dl),
70 (fn () => (EVar "false", dl)))
71
72val _ = Main.registerDefault ("User",
73 (TBase "your_user", dl),
74 (fn () => (EString (Domain.getUser ()), dl)))
75
76val _ = Main.registerDefault ("Group",
77 (TBase "your_group", dl),
78 (fn () => (EString (Domain.getUser ()), dl)))
79
80val _ = Main.registerDefault ("DocumentRoot",
81 (TBase "your_path", dl),
82 (fn () => (EString ("/home/" ^ Domain.getUser () ^ "/public_html"), dl)))
83
84val _ = Main.registerDefault ("ServerAdmin",
85 (TBase "email", dl),
86 (fn () => (EString (Domain.getUser () ^ "@" ^ Config.defaultDomain), dl)))
87
88
89val redirect_code = fn (EVar "temp", _) => SOME "temp"
90 | (EVar "permanent", _) => SOME "permanent"
91 | (EVar "seeother", _) => SOME "seeother"
92 | (EVar "redir300", _) => SOME "300"
93 | (EVar "redir301", _) => SOME "301"
94 | (EVar "redir302", _) => SOME "302"
95 | (EVar "redir303", _) => SOME "303"
96 | (EVar "redir304", _) => SOME "304"
97 | (EVar "redir305", _) => SOME "305"
98 | (EVar "redir307", _) => SOME "307"
99 | _ => NONE
100
101val flag = fn (EVar "redirect", _) => SOME "R"
102 | (EVar "forbidden", _) => SOME "F"
103 | (EVar "gone", _) => SOME "G"
104 | (EVar "last", _) => SOME "L"
105 | (EVar "chain", _) => SOME "C"
106 | (EVar "nosubreq", _) => SOME "NS"
107 | (EVar "nocase", _) => SOME "NC"
108 | (EVar "qsappend", _) => SOME "QSA"
109 | (EVar "noescape", _) => SOME "NE"
110 | (EVar "passthrough", _) => SOME "PT"
111 | (EApp ((EVar "mimeType", _), e), _) =>
112 Option.map (fn s => "T=" ^ s) (Env.string e)
113 | (EApp ((EVar "redirectWith", _), e), _) =>
114 Option.map (fn s => "R=" ^ s) (redirect_code e)
115 | (EApp ((EVar "skip", _), e), _) =>
116 Option.map (fn n => "S=" ^ Int.toString n) (Env.int e)
117 | (EApp ((EApp ((EVar "env", _), e1), _), e2), _) =>
118 (case Env.string e1 of
119 NONE => NONE
120 | SOME s1 => Option.map (fn s2 => "E=" ^ s1 ^ ":" ^ s2)
121 (Env.string e2))
122
123 | _ => NONE
124
125val cond_flag = fn (EVar "cond_nocase", _) => SOME "NC"
126 | (EVar "ornext", _) => SOME "OR"
127 | _ => NONE
128
129val apache_option = fn (EVar "execCGI", _) => SOME "ExecCGI"
130 | (EVar "includesNOEXEC", _) => SOME "IncludesNOEXEC"
131 | (EVar "indexes", _) => SOME "Indexes"
132 | _ => NONE
133
134val autoindex_width = fn (EVar "autofit", _) => SOME "*"
135 | (EApp ((EVar "characters", _), n), _) =>
136 Option.map Int.toString (Env.int n)
137 | _ => NONE
138
139val autoindex_option = fn (EApp ((EVar "descriptionWidth", _), w), _) =>
140 Option.map (fn w => ("DescriptionWidth", SOME w))
141 (autoindex_width w)
142 | (EVar "fancyIndexing", _) => SOME ("FancyIndexing", NONE)
143 | (EVar "foldersFirst", _) => SOME ("FoldersFirst", NONE)
144 | (EVar "htmlTable", _) => SOME ("HTMLTable", NONE)
145 | (EVar "iconsAreLinks", _) => SOME ("IconsAreLinks", NONE)
146 | (EApp ((EVar "iconHeight", _), n), _) =>
147 Option.map (fn w => ("IconHeight", SOME (Int.toString w)))
148 (Env.int n)
149 | (EApp ((EVar "iconWidth", _), n), _) =>
150 Option.map (fn w => ("IconWidth", SOME (Int.toString w)))
151 (Env.int n)
152 | (EVar "ignoreCase", _) => SOME ("IgnoreCase", NONE)
153 | (EVar "ignoreClient", _) => SOME ("IgnoreClient", NONE)
154 | (EApp ((EVar "nameWidth", _), w), _) =>
155 Option.map (fn w => ("NameWidth", SOME w))
156 (autoindex_width w)
157 | (EVar "scanHtmlTitles", _) => SOME ("ScanHTMLTitles", NONE)
158 | (EVar "suppressColumnSorting", _) => SOME ("SuppressColumnSorting", NONE)
159 | (EVar "suppressDescription", _) => SOME ("SuppressDescription", NONE)
160 | (EVar "suppressHtmlPreamble", _) => SOME ("SuppressHTMLPreamble", NONE)
161 | (EVar "suppressIcon", _) => SOME ("SuppressIcon", NONE)
162 | (EVar "suppressLastModified", _) => SOME ("SuppressLastModified", NONE)
163 | (EVar "suppressRules", _) => SOME ("SuppressRules", NONE)
164 | (EVar "suppressSize", _) => SOME ("SuppressSize", NONE)
165 | (EVar "trackModified", _) => SOME ("TrackModified", NONE)
166 | (EVar "versionSort", _) => SOME ("VersionSort", NONE)
167 | (EVar "xhtml", _) => SOME ("XHTML", NONE)
168
169 | _ => NONE
170
171val vhostsChanged = ref false
172
173val () = Slave.registerPreHandler
174 (fn () => vhostsChanged := false)
175
176val () = Slave.registerFileHandler (fn fs =>
177 let
178 val spl = OS.Path.splitDirFile (#file fs)
179 in
180 if String.isSuffix ".vhost" (#file spl)
181 orelse String.isSuffix ".vhost_ssl" (#file spl) then
182 (vhostsChanged := true;
183 case #action fs of
184 Slave.Delete =>
185 ignore (OS.Process.system (Config.rm
186 ^ " -rf "
187 ^ Config.Apache.confDir
188 ^ "/"
189 ^ #file spl))
190 | _ =>
191 ignore (OS.Process.system (Config.cp
192 ^ " "
193 ^ #file fs
194 ^ " "
195 ^ Config.Apache.confDir
196 ^ "/"
197 ^ #file spl)))
198 else
199 ()
200 end)
201
202val () = Slave.registerPostHandler
203 (fn () =>
204 (if !vhostsChanged then
205 Slave.shellF ([Config.Apache.reload],
206 fn cl => "Error reloading Apache with " ^ cl)
207 else
208 ()))
209
210val vhostFiles : TextIO.outstream list ref = ref []
211fun write s = app (fn file => TextIO.output (file, s)) (!vhostFiles)
212
213val rewriteEnabled = ref false
214val currentVhost = ref ""
215val currentVhostId = ref ""
216
217val () = Env.containerV_one "vhost"
218 ("host", Env.string)
219 (fn (env, host) =>
220 let
221 val nodes = Env.env (Env.list Env.string) (env, "WebNodes")
222
223 val ssl = Env.env Env.bool (env, "SSL")
224 val user = Env.env Env.string (env, "User")
225 val group = Env.env Env.string (env, "Group")
226 val docroot = Env.env Env.string (env, "DocumentRoot")
227 val sadmin = Env.env Env.string (env, "ServerAdmin")
228
229 val fullHost = host ^ "." ^ Domain.currentDomain ()
230 val vhostId = fullHost ^ (if ssl then ".ssl" else "")
231 val confFile = fullHost ^ (if ssl then ".vhost_ssl" else ".vhost")
232 in
233 currentVhost := fullHost;
234 currentVhostId := vhostId;
235
236 rewriteEnabled := false;
237 vhostFiles := map (fn node =>
238 let
239 val file = Domain.domainFile {node = node,
240 name = confFile}
241 in
242 TextIO.output (file, "<VirtualHost ");
243 TextIO.output (file, Domain.nodeIp node);
244 TextIO.output (file, ":");
245 TextIO.output (file, if ssl then
246 "443"
247 else
248 "80");
249 TextIO.output (file, ">\n");
250 file
251 end)
252 nodes;
253 write "\tServerName ";
254 write fullHost;
255 write "\n\tSuexecUserGroup ";
256 write user;
257 write " ";
258 write group;
259 write "\n\tDocumentRoot ";
260 write docroot;
261 write "\n\tServerAdmin ";
262 write sadmin;
263 write "\n\tErrorLog ";
264 write Config.Apache.logDir;
265 write "/";
266 write vhostId;
267 write "/error.log\n\tCustomLog ";
268 write Config.Apache.logDir;
269 write "/";
270 write vhostId;
271 write "/access.log combined\n"
272 end,
273 fn () => (write "</VirtualHost>\n";
274 app TextIO.closeOut (!vhostFiles)))
275
276val () = Env.container_one "location"
277 ("prefix", Env.string)
278 (fn prefix =>
279 (write "\t<Location ";
280 write prefix;
281 write ">\n"),
282 fn () => write "\t</Location>\n")
283
284val () = Env.container_one "directory"
285 ("directory", Env.string)
286 (fn directory =>
287 (write "\t<Directory ";
288 write directory;
289 write ">\n"),
290 fn () => write "\t</Directory>\n")
291
292fun checkRewrite () =
293 if !rewriteEnabled then
294 ()
295 else
296 (write "\tRewriteEngine on\n";
297 rewriteEnabled := true)
298
299val () = Env.action_three "localProxyRewrite"
300 ("from", Env.string, "to", Env.string, "port", Env.int)
301 (fn (from, to, port) =>
302 (checkRewrite ();
303 write "\tRewriteRule\t";
304 write from;
305 write "\thttp://localhost:";
306 write (Int.toString port);
307 write "/";
308 write to;
309 write " [P]\n"))
310
311val () = Env.action_two "proxyPass"
312 ("from", Env.string, "to", Env.string)
313 (fn (from, to) =>
314 (write "\tProxyPass\t";
315 write from;
316 write "\t";
317 write to;
318 write "\n"))
319
320val () = Env.action_two "proxyPassReverse"
321 ("from", Env.string, "to", Env.string)
322 (fn (from, to) =>
323 (write "\tProxyPassReverse\t";
324 write from;
325 write "\t";
326 write to;
327 write "\n"))
328
329val () = Env.action_three "rewriteRule"
330 ("from", Env.string, "to", Env.string, "flags", Env.list flag)
331 (fn (from, to, flags) =>
332 (checkRewrite ();
333 write "\tRewriteRule\t";
334 write from;
335 write "\t";
336 write to;
337 case flags of
338 [] => ()
339 | flag::rest => (write " [";
340 write flag;
341 app (fn flag => (write ",";
342 write flag)) rest;
343 write "]");
344 write "\n"))
345
346val () = Env.action_three "rewriteCond"
347 ("test", Env.string, "pattern", Env.string, "flags", Env.list cond_flag)
348 (fn (from, to, flags) =>
349 (checkRewrite ();
350 write "\tRewriteCond\t";
351 write from;
352 write "\t";
353 write to;
354 case flags of
355 [] => ()
356 | flag::rest => (write " [";
357 write flag;
358 app (fn flag => (write ",";
359 write flag)) rest;
360 write "]");
361 write "\n"))
362
363val () = Env.action_one "rewriteLogLevel"
364 ("level", Env.int)
365 (fn level =>
366 (checkRewrite ();
367 write "\tRewriteLog ";
368 write Config.Apache.logDir;
369 write "/";
370 write (!currentVhostId);
371 write "/rewrite.log\n\tRewriteLogLevel ";
372 write (Int.toString level);
373 write "\n"))
374
375val () = Env.action_two "alias"
376 ("from", Env.string, "to", Env.string)
377 (fn (from, to) =>
378 (write "\tAlias\t";
379 write from;
380 write " ";
381 write to;
382 write "\n"))
383
384val () = Env.action_two "scriptAlias"
385 ("from", Env.string, "to", Env.string)
386 (fn (from, to) =>
387 (write "\tScriptAlias\t";
388 write from;
389 write " ";
390 write to;
391 write "\n"))
392
393val () = Env.action_two "errorDocument"
394 ("code", Env.string, "handler", Env.string)
395 (fn (code, handler) =>
396 (write "\tErrorDocument\t";
397 write code;
398 write " ";
399 write handler;
400 write "\n"))
401
402val () = Env.action_one "options"
403 ("options", Env.list apache_option)
404 (fn opts =>
405 case opts of
406 [] => ()
407 | _ => (write "\tOptions";
408 app (fn opt => (write " "; write opt)) opts;
409 write "\n"))
410
411val () = Env.action_one "set_options"
412 ("options", Env.list apache_option)
413 (fn opts =>
414 case opts of
415 [] => ()
416 | _ => (write "\tOptions";
417 app (fn opt => (write " +"; write opt)) opts;
418 write "\n"))
419
420val () = Env.action_one "unset_options"
421 ("options", Env.list apache_option)
422 (fn opts =>
423 case opts of
424 [] => ()
425 | _ => (write "\tOptions";
426 app (fn opt => (write " -"; write opt)) opts;
427 write "\n"))
428
429val () = Env.action_one "directoryIndex"
430 ("filenames", Env.list Env.string)
431 (fn opts =>
432 (write "\tDirectoryIndex";
433 app (fn opt => (write " "; write opt)) opts;
434 write "\n"))
435
436val () = Env.action_one "serverAlias"
437 ("host", Env.string)
438 (fn host =>
439 (write "\tServerAlias ";
440 write host;
441 write "\n"))
442
443val authType = fn (EVar "basic", _) => SOME "basic"
444 | (EVar "digest", _) => SOME "digest"
445 | _ => NONE
446
447val () = Env.action_one "authType"
448 ("type", authType)
449 (fn ty =>
450 (write "\tAuthType ";
451 write ty;
452 write "\n"))
453
454val () = Env.action_one "authName"
455 ("name", Env.string)
456 (fn name =>
457 (write "\tAuthName \"";
458 write name;
459 write "\"\n"))
460
461val () = Env.action_one "authUserFile"
462 ("file", Env.string)
463 (fn name =>
464 (write "\tAuthUserFile ";
465 write name;
466 write "\n"))
467
468val () = Env.action_none "requireValidUser"
469 (fn () => write "\tRequire valid-user\n")
470
471val () = Env.action_one "requireUser"
472 ("users", Env.list Env.string)
473 (fn names =>
474 case names of
475 [] => ()
476 | _ => (write "\tRequire user";
477 app (fn name => (write " "; write name)) names;
478 write "\n"))
479
480val () = Env.action_one "requireGroup"
481 ("groups", Env.list Env.string)
482 (fn names =>
483 case names of
484 [] => ()
485 | _ => (write "\tRequire group";
486 app (fn name => (write " "; write name)) names;
487 write "\n"))
488
489val () = Env.action_none "orderAllowDeny"
490 (fn () => write "\tOrder allow,deny\n")
491
492val () = Env.action_none "orderDenyAllow"
493 (fn () => write "\tOrder deny,allow\n")
494
495val () = Env.action_none "allowFromAll"
496 (fn () => write "\tAllow from all\n")
497
498val () = Env.action_one "allowFrom"
499 ("entries", Env.list Env.string)
500 (fn names =>
501 case names of
502 [] => ()
503 | _ => (write "\tAllow from";
504 app (fn name => (write " "; write name)) names;
505 write "\n"))
506
507val () = Env.action_none "denyFromAll"
508 (fn () => write "\tDeny from all\n")
509
510val () = Env.action_one "denyFrom"
511 ("entries", Env.list Env.string)
512 (fn names =>
513 case names of
514 [] => ()
515 | _ => (write "\tDeny from";
516 app (fn name => (write " "; write name)) names;
517 write "\n"))
518
519val () = Env.action_none "satisfyAll"
520 (fn () => write "\tSatisfy all\n")
521
522val () = Env.action_none "satisfyAny"
523 (fn () => write "\tSatisfy any\n")
524
525val () = Env.action_one "forceType"
526 ("type", Env.string)
527 (fn ty => (write "\tForceType ";
528 write ty;
529 write "\n"))
530
531val () = Env.action_none "forceTypeOff"
532 (fn () => write "\tForceType None\n")
533
534val () = Env.action_two "action"
535 ("what", Env.string, "how", Env.string)
536 (fn (what, how) => (write "\tAction ";
537 write what;
538 write " ";
539 write how;
540 write "\n"))
541
542val () = Env.action_one "addDefaultCharset"
543 ("charset", Env.string)
544 (fn ty => (write "\tAddDefaultCharset ";
545 write ty;
546 write "\n"))
547
548val () = Env.action_one "davSvn"
549 ("path", Env.string)
550 (fn path => (write "\tDAV svn\n\tSVNPath ";
551 write path;
552 write "\n"))
553
554val () = Env.action_one "authzSvnAccessFile"
555 ("path", Env.string)
556 (fn path => (write "\tAuthzSVNAccessFile ";
557 write path;
558 write "\n"))
559
560val () = Env.action_two "addDescription"
561 ("description", Env.string, "patterns", Env.list Env.string)
562 (fn (desc, pats) =>
563 case pats of
564 [] => ()
565 | _ => (write "\tAddDescription \"";
566 write (String.toString desc);
567 write "\"";
568 app (fn pat => (write " "; write pat)) pats;
569 write "\n"))
570
571val () = Env.action_one "indexOptions"
572 ("options", Env.list autoindex_option)
573 (fn opts =>
574 case opts of
575 [] => ()
576 | _ => (write "\tIndexOptions";
577 app (fn (opt, arg) =>
578 (write " ";
579 write opt;
580 Option.app (fn arg =>
581 (write "="; write arg)) arg)) opts;
582 write "\n"))
583
584val () = Env.action_one "set_indexOptions"
585 ("options", Env.list autoindex_option)
586 (fn opts =>
587 case opts of
588 [] => ()
589 | _ => (write "\tIndexOptions";
590 app (fn (opt, arg) =>
591 (write " +";
592 write opt;
593 Option.app (fn arg =>
594 (write "="; write arg)) arg)) opts;
595 write "\n"))
596
597val () = Env.action_one "unset_indexOptions"
598 ("options", Env.list autoindex_option)
599 (fn opts =>
600 case opts of
601 [] => ()
602 | _ => (write "\tIndexOptions";
603 app (fn (opt, _) =>
604 (write " -";
605 write opt)) opts;
606 write "\n"))
607
608val () = Env.action_one "headerName"
609 ("name", Env.string)
610 (fn name => (write "\tHeaderName ";
611 write name;
612 write "\n"))
613
614val () = Env.action_one "readmeName"
615 ("name", Env.string)
616 (fn name => (write "\tReadmeName ";
617 write name;
618 write "\n"))
619
620end