1 (* HCoop
Domtool (http
://hcoop
.sourceforge
.net
/)
2 * Copyright (c
) 2006, Adam Chlipala
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
.
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
.
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
.
19 (* Apache HTTPD handling
*)
21 structure Apache
:> APACHE
= struct
25 val _
= Env
.type_one
"proxy_port"
29 val _
= Env
.type_one
"proxy_target"
33 fun default () = List.exists (fn s
' => s
= s
') Config
.Apache
.proxyTargets
35 case String.fields (fn ch
=> ch
= #
":") s
of
36 ["http", "//localhost", rest
] =>
37 (case String.fields (fn ch
=> ch
= #
"/") rest
of
39 (case Int.fromString port
of
41 | SOME n
=> n
> 1024 orelse default ())
46 val _
= Env
.type_one
"rewrite_arg"
48 (CharVector
.all
Char.isAlphaNum
)
51 size s
> 0 andalso size s
< 1000 andalso CharVector
.all
52 (fn ch
=> Char.isAlphaNum ch
58 val _
= Env
.type_one
"location"
62 val dl
= ErrorMsg
.dummyLoc
64 val _
= Main
.registerDefault ("WebNodes",
65 (TList (TBase
"node", dl
), dl
),
66 (fn () => (EList (map (fn s
=> (EString s
, dl
)) Config
.Apache
.webNodes
), dl
)))
68 val _
= Main
.registerDefault ("SSL",
70 (fn () => (EVar
"false", dl
)))
72 val _
= Main
.registerDefault ("User",
73 (TBase
"your_user", dl
),
74 (fn () => (EString (Domain
.getUser ()), dl
)))
76 val _
= Main
.registerDefault ("Group",
77 (TBase
"your_group", dl
),
78 (fn () => (EString (Domain
.getUser ()), dl
)))
80 val _
= Main
.registerDefault ("DocumentRoot",
81 (TBase
"your_path", dl
),
82 (fn () => (EString ("/home/" ^ Domain
.getUser () ^
"/public_html"), dl
)))
84 val _
= Main
.registerDefault ("ServerAdmin",
86 (fn () => (EString (Domain
.getUser () ^
"@" ^ Config
.defaultDomain
), dl
)))
89 val 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"
101 val 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
120 | SOME s1
=> Option
.map (fn s2
=> "E=" ^ s1 ^
":" ^ s2
)
125 val cond_flag
= fn (EVar
"cond_nocase", _
) => SOME
"NC"
126 |
(EVar
"ornext", _
) => SOME
"OR"
129 val apache_option
= fn (EVar
"execCGI", _
) => SOME
"ExecCGI"
130 |
(EVar
"includesNOEXEC", _
) => SOME
"IncludesNOEXEC"
131 |
(EVar
"indexes", _
) => SOME
"Indexes"
135 val vhostsChanged
= ref
false
137 val () = Slave
.registerPreHandler
138 (fn () => vhostsChanged
:= false)
140 val () = Slave
.registerFileHandler (fn fs
=>
142 val spl
= OS
.Path
.splitDirFile (#file fs
)
144 if String.isSuffix
".vhost" (#file spl
)
145 orelse String.isSuffix
".vhost_ssl" (#file spl
) then
146 (vhostsChanged
:= true;
149 ignore (OS
.Process
.system (Config
.rm
151 ^ Config
.Apache
.confDir
155 ignore (OS
.Process
.system (Config
.cp
159 ^ Config
.Apache
.confDir
166 val () = Slave
.registerPostHandler
168 (if !vhostsChanged
then
169 Slave
.shellF ([Config
.Apache
.reload
],
170 fn cl
=> "Error reloading Apache with " ^ cl
)
174 val vhostFiles
: TextIO.outstream list ref
= ref
[]
175 fun write s
= app (fn file
=> TextIO.output (file
, s
)) (!vhostFiles
)
177 val rewriteEnabled
= ref
false
178 val currentVhost
= ref
""
179 val currentVhostId
= ref
""
181 val () = Env
.containerV_one
"vhost"
185 val nodes
= Env
.env (Env
.list Env
.string) (env
, "WebNodes")
187 val ssl
= Env
.env Env
.bool (env
, "SSL")
188 val user
= Env
.env Env
.string (env
, "User")
189 val group
= Env
.env Env
.string (env
, "Group")
190 val docroot
= Env
.env Env
.string (env
, "DocumentRoot")
191 val sadmin
= Env
.env Env
.string (env
, "ServerAdmin")
193 val fullHost
= host ^
"." ^ Domain
.currentDomain ()
194 val vhostId
= fullHost ^
(if ssl
then ".ssl" else "")
195 val confFile
= fullHost ^
(if ssl
then ".vhost_ssl" else ".vhost")
197 currentVhost
:= fullHost
;
198 currentVhostId
:= vhostId
;
200 rewriteEnabled
:= false;
201 vhostFiles
:= map (fn node
=>
203 val file
= Domain
.domainFile
{node
= node
,
206 TextIO.output (file
, "<VirtualHost ");
207 TextIO.output (file
, Domain
.nodeIp node
);
208 TextIO.output (file
, ":");
209 TextIO.output (file
, if ssl
then
213 TextIO.output (file
, ">\n");
217 write
"\tServerName ";
219 write
"\n\tSuexecUserGroup ";
223 write
"\n\tDocumentRoot ";
225 write
"\n\tServerAdmin ";
227 write
"\n\tErrorLog ";
228 write Config
.Apache
.logDir
;
231 write
"/error.log\n\tCustomLog ";
232 write Config
.Apache
.logDir
;
235 write
"/access.log combined\n"
237 fn () => (write
"</VirtualHost>\n";
238 app
TextIO.closeOut (!vhostFiles
)))
240 val () = Env
.container_one
"location"
241 ("prefix", Env
.string)
243 (write
"\t<Location ";
246 fn () => write
"\t</Location>\n")
248 val () = Env
.container_one
"directory"
249 ("directory", Env
.string)
251 (write
"\t<Directory ";
254 fn () => write
"\t</Directory>\n")
256 fun checkRewrite () =
257 if !rewriteEnabled
then
260 (write
"\tRewriteEngine on\n";
261 rewriteEnabled
:= true)
263 val () = Env
.action_three
"localProxyRewrite"
264 ("from", Env
.string, "to", Env
.string, "port", Env
.int)
265 (fn (from
, to
, port
) =>
267 write
"\tRewriteRule\t";
269 write
"\thttp://localhost:";
270 write (Int.toString port
);
275 val () = Env
.action_two
"proxyPass"
276 ("from", Env
.string, "to", Env
.string)
278 (write
"\tProxyPass\t";
284 val () = Env
.action_two
"proxyPassReverse"
285 ("from", Env
.string, "to", Env
.string)
287 (write
"\tProxyPassReverse\t";
293 val () = Env
.action_three
"rewriteRule"
294 ("from", Env
.string, "to", Env
.string, "flags", Env
.list flag
)
295 (fn (from
, to
, flags
) =>
297 write
"\tRewriteRule\t";
303 | flag
::rest
=> (write
" [";
305 app (fn flag
=> (write
",";
310 val () = Env
.action_three
"rewriteCond"
311 ("test", Env
.string, "pattern", Env
.string, "flags", Env
.list cond_flag
)
312 (fn (from
, to
, flags
) =>
314 write
"\tRewriteCond\t";
320 | flag
::rest
=> (write
" [";
322 app (fn flag
=> (write
",";
327 val () = Env
.action_one
"rewriteLogLevel"
331 write
"\tRewriteLog ";
332 write Config
.Apache
.logDir
;
334 write (!currentVhostId
);
335 write
"/rewrite.log\n\tRewriteLogLevel ";
336 write (Int.toString level
);
339 val () = Env
.action_two
"alias"
340 ("from", Env
.string, "to", Env
.string)
348 val () = Env
.action_two
"scriptAlias"
349 ("from", Env
.string, "to", Env
.string)
351 (write
"\tScriptAlias\t";
357 val () = Env
.action_two
"errorDocument"
358 ("code", Env
.string, "handler", Env
.string)
359 (fn (code
, handler
) =>
360 (write
"\tErrorDocument\t";
366 val () = Env
.action_one
"options"
367 ("options", Env
.list apache_option
)
371 | _
=> (write
"\tOptions";
372 app (fn opt
=> (write
" "; write opt
)) opts
;
375 val () = Env
.action_one
"set_options"
376 ("options", Env
.list apache_option
)
380 | _
=> (write
"\tOptions";
381 app (fn opt
=> (write
" +"; write opt
)) opts
;
384 val () = Env
.action_one
"unset_options"
385 ("options", Env
.list apache_option
)
389 | _
=> (write
"\tOptions";
390 app (fn opt
=> (write
" -"; write opt
)) opts
;
393 val () = Env
.action_one
"directoryIndex"
394 ("filenames", Env
.list Env
.string)
396 (write
"\tDirectoryIndex";
397 app (fn opt
=> (write
" "; write opt
)) opts
;
400 val () = Env
.action_one
"serverAlias"
403 (write
"\tServerAlias ";
407 val authType
= fn (EVar
"basic", _
) => SOME
"basic"
408 |
(EVar
"digest", _
) => SOME
"digest"
411 val () = Env
.action_one
"authType"
414 (write
"\tAuthType ";
418 val () = Env
.action_one
"authName"
421 (write
"\tAuthName \"";
425 val () = Env
.action_one
"authUserFile"
428 (write
"\tAuthUserFile ";
432 val () = Env
.action_none
"requireValidUser"
433 (fn () => write
"\tRequire valid-user\n")
435 val () = Env
.action_one
"requireUser"
436 ("users", Env
.list Env
.string)
440 | _
=> (write
"\tRequire user";
441 app (fn name
=> (write
" "; write name
)) names
;
444 val () = Env
.action_one
"requireGroup"
445 ("groups", Env
.list Env
.string)
449 | _
=> (write
"\tRequire group";
450 app (fn name
=> (write
" "; write name
)) names
;
453 val () = Env
.action_none
"orderAllowDeny"
454 (fn () => write
"\tOrder allow,deny\n")
456 val () = Env
.action_none
"orderDenyAllow"
457 (fn () => write
"\tOrder deny,allow\n")
459 val () = Env
.action_none
"allowFromAll"
460 (fn () => write
"\tAllow from all\n")
462 val () = Env
.action_one
"allowFrom"
463 ("entries", Env
.list Env
.string)
467 | _
=> (write
"\tAllow from";
468 app (fn name
=> (write
" "; write name
)) names
;
471 val () = Env
.action_none
"denyFromAll"
472 (fn () => write
"\tDeny from all\n")
474 val () = Env
.action_one
"denyFrom"
475 ("entries", Env
.list Env
.string)
479 | _
=> (write
"\tDeny from";
480 app (fn name
=> (write
" "; write name
)) names
;
483 val () = Env
.action_none
"satisfyAll"
484 (fn () => write
"\tSatisfy all\n")
486 val () = Env
.action_none
"satisfyAny"
487 (fn () => write
"\tSatisfy any\n")
489 val () = Env
.action_one
"forceType"
491 (fn ty
=> (write
"\tForceType ";
495 val () = Env
.action_none
"forceTypeOff"
496 (fn () => write
"\tForceType None\n")
498 val () = Env
.action_two
"action"
499 ("what", Env
.string, "how", Env
.string)
500 (fn (what
, how
) => (write
"\tAction ";
506 val () = Env
.action_one
"addDefaultCharset"
507 ("charset", Env
.string)
508 (fn ty
=> (write
"\tAddDefaultCharset ";
512 val () = Env
.action_one
"davSvn"
514 (fn path
=> (write
"\tDAV svn\n\tSVNPath ";
518 val () = Env
.action_one
"authzSvnAccessFile"
520 (fn path
=> (write
"\tAuthzSVNAccessFile ";