Apache directory options
[hcoop/domtool2.git] / src / plugins / apache.sml
CommitLineData
8a7c40fa
AC
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
f8dfbbcc
AC
25val _ = Env.type_one "proxy_port"
26 Env.int
e95a129e
AC
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)
f8dfbbcc
AC
45
46val _ = Env.type_one "rewrite_arg"
47 Env.string
48 (CharVector.all Char.isAlphaNum)
49
2882ee37
AC
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
8a7c40fa
AC
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
f8dfbbcc
AC
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
e95a129e
AC
125val cond_flag = fn (EVar "cond_nocase", _) => SOME "NC"
126 | (EVar "ornext", _) => SOME "OR"
127 | _ => NONE
128
d441e69f
AC
129val apache_option = fn (EVar "execCGI", _) => SOME "ExecCGI"
130 | (EVar "includesNOEXEC", _) => SOME "IncludesNOEXEC"
131 | (EVar "indexes", _) => SOME "Indexes"
132 | _ => NONE
133
f8dfbbcc 134
8a7c40fa
AC
135val vhostsChanged = ref false
136
137val () = Slave.registerPreHandler
138 (fn () => vhostsChanged := false)
139
140val () = Slave.registerFileHandler (fn fs =>
141 let
142 val spl = OS.Path.splitDirFile (#file fs)
143 in
144 if String.isSuffix ".vhost" (#file spl)
145 orelse String.isSuffix ".vhost_ssl" (#file spl) then
146 (vhostsChanged := true;
147 case #action fs of
148 Slave.Delete =>
149 ignore (OS.Process.system (Config.rm
150 ^ " -rf "
151 ^ Config.Apache.confDir
152 ^ "/"
153 ^ #file spl))
154 | _ =>
155 ignore (OS.Process.system (Config.cp
156 ^ " "
157 ^ #file fs
158 ^ " "
159 ^ Config.Apache.confDir
160 ^ "/"
161 ^ #file spl)))
162 else
163 ()
164 end)
165
166val () = Slave.registerPostHandler
167 (fn () =>
168 (if !vhostsChanged then
169 Slave.shellF ([Config.Apache.reload],
170 fn cl => "Error reloading Apache with " ^ cl)
171 else
172 ()))
173
174val vhostFiles : TextIO.outstream list ref = ref []
175fun write s = app (fn file => TextIO.output (file, s)) (!vhostFiles)
176
f8dfbbcc
AC
177val rewriteEnabled = ref false
178
8a7c40fa
AC
179val () = Env.containerV_one "vhost"
180 ("host", Env.string)
181 (fn (env, host) =>
182 let
183 val nodes = Env.env (Env.list Env.string) (env, "WebNodes")
184
185 val ssl = Env.env Env.bool (env, "SSL")
186 val user = Env.env Env.string (env, "User")
187 val group = Env.env Env.string (env, "Group")
188 val docroot = Env.env Env.string (env, "DocumentRoot")
189 val sadmin = Env.env Env.string (env, "ServerAdmin")
190
191 val fullHost = host ^ "." ^ Domain.currentDomain ()
192 val confFile = fullHost ^ (if ssl then ".vhost_ssl" else ".vhost")
193 in
f8dfbbcc 194 rewriteEnabled := false;
8a7c40fa
AC
195 vhostFiles := map (fn node =>
196 let
197 val file = Domain.domainFile {node = node,
198 name = confFile}
199 in
200 TextIO.output (file, "<VirtualHost ");
201 TextIO.output (file, Domain.nodeIp node);
202 TextIO.output (file, ":");
203 TextIO.output (file, if ssl then
204 "443"
205 else
206 "80");
207 TextIO.output (file, ">\n");
208 file
209 end)
210 nodes;
211 write "\tSuexecUserGroup ";
212 write user;
213 write " ";
214 write group;
215 write "\n\tDocumentRoot ";
216 write docroot;
217 write "\n\tServerAdmin ";
218 write sadmin;
219 write "\n"
220 end,
221 fn () => (write "</VirtualHost>\n";
222 app TextIO.closeOut (!vhostFiles)))
223
2882ee37
AC
224val () = Env.container_one "location"
225 ("prefix", Env.string)
226 (fn prefix =>
227 (write "\t<Location ";
228 write prefix;
229 write ">\n"),
230 fn () => write "\t</Location>\n")
231
232val () = Env.container_one "directory"
233 ("directory", Env.string)
234 (fn directory =>
235 (write "\t<Directory ";
236 write directory;
237 write ">\n"),
238 fn () => write "\t</Directory>\n")
239
f8dfbbcc
AC
240fun checkRewrite () =
241 if !rewriteEnabled then
242 ()
243 else
244 (write "\tRewriteEngine on\n";
245 rewriteEnabled := true)
246
247val () = Env.action_three "localProxyRewrite"
248 ("from", Env.string, "to", Env.string, "port", Env.int)
249 (fn (from, to, port) =>
250 (checkRewrite ();
251 write "\tRewriteRule\t";
252 write from;
253 write "\thttp://localhost:";
254 write (Int.toString port);
255 write "/";
256 write to;
257 write " [P]\n"))
258
e95a129e
AC
259val () = Env.action_two "proxyPass"
260 ("from", Env.string, "to", Env.string)
261 (fn (from, to) =>
262 (write "\tProxyPass\t";
263 write from;
264 write "\t";
265 write to;
266 write "\n"))
267
268val () = Env.action_two "proxyPassReverse"
269 ("from", Env.string, "to", Env.string)
270 (fn (from, to) =>
271 (write "\tProxyPassReverse\t";
272 write from;
273 write "\t";
274 write to;
275 write "\n"))
f8dfbbcc
AC
276
277val () = Env.action_three "rewriteRule"
278 ("from", Env.string, "to", Env.string, "flags", Env.list flag)
279 (fn (from, to, flags) =>
280 (checkRewrite ();
281 write "\tRewriteRule\t";
282 write from;
283 write "\t";
284 write to;
285 case flags of
286 [] => ()
287 | flag::rest => (write " [";
288 write flag;
289 app (fn flag => (write ",";
290 write flag)) rest;
291 write "]");
292 write "\n"))
293
e95a129e
AC
294val () = Env.action_three "rewriteCond"
295 ("test", Env.string, "pattern", Env.string, "flags", Env.list cond_flag)
296 (fn (from, to, flags) =>
297 (checkRewrite ();
298 write "\tRewriteCond\t";
299 write from;
300 write "\t";
301 write to;
302 case flags of
303 [] => ()
304 | flag::rest => (write " [";
305 write flag;
306 app (fn flag => (write ",";
307 write flag)) rest;
308 write "]");
309 write "\n"))
310
d5754b53
AC
311val () = Env.action_two "alias"
312 ("from", Env.string, "to", Env.string)
313 (fn (from, to) =>
314 (write "\tAlias\t";
315 write from;
316 write " ";
317 write to;
318 write "\n"))
319
320val () = Env.action_two "scriptAlias"
321 ("from", Env.string, "to", Env.string)
322 (fn (from, to) =>
323 (write "\tScriptAlias\t";
324 write from;
325 write " ";
326 write to;
327 write "\n"))
328
329val () = Env.action_two "errorDocument"
330 ("code", Env.string, "handler", Env.string)
331 (fn (code, handler) =>
332 (write "\tErrorDocument\t";
333 write code;
334 write " ";
335 write handler;
336 write "\n"))
337
d441e69f
AC
338val () = Env.action_one "options"
339 ("options", Env.list apache_option)
340 (fn opts =>
341 case opts of
342 [] => ()
343 | _ => (write "\tOptions";
344 app (fn opt => (write " "; write opt)) opts;
345 write "\n"))
346
347val () = Env.action_one "set_options"
348 ("options", Env.list apache_option)
349 (fn opts =>
350 case opts of
351 [] => ()
352 | _ => (write "\tOptions";
353 app (fn opt => (write " +"; write opt)) opts;
354 write "\n"))
355
356val () = Env.action_one "unset_options"
357 ("options", Env.list apache_option)
358 (fn opts =>
359 case opts of
360 [] => ()
361 | _ => (write "\tOptions";
362 app (fn opt => (write " -"; write opt)) opts;
363 write "\n"))
d5754b53 364
8a7c40fa 365end