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 _
= Defaults
.registerDefault ("WebNodes",
65 (TList (TBase
"node", dl
), dl
),
66 (fn () => (EList (map (fn s
=> (EString s
, dl
)) Config
.Apache
.webNodes
), dl
)))
68 val _
= Defaults
.registerDefault ("SSL",
70 (fn () => (EVar
"false", dl
)))
72 val _
= Defaults
.registerDefault ("User",
73 (TBase
"your_user", dl
),
74 (fn () => (EString (Domain
.getUser ()), dl
)))
76 val _
= Defaults
.registerDefault ("Group",
77 (TBase
"your_group", dl
),
78 (fn () => (EString (Domain
.getUser ()), dl
)))
80 val _
= Defaults
.registerDefault ("DocumentRoot",
81 (TBase
"your_path", dl
),
82 (fn () => (EString (Config
.homeBase ^
"/" ^ Domain
.getUser () ^
"/" ^ Config
.Apache
.public_html
), dl
)))
84 val _
= Defaults
.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"
134 val autoindex_width
= fn (EVar
"autofit", _
) => SOME
"*"
135 |
(EApp ((EVar
"characters", _
), n
), _
) =>
136 Option
.map
Int.toString (Env
.int n
)
139 val autoindex_option
= fn (EApp ((EVar
"descriptionWidth", _
), w
), _
) =>
140 Option
.map (fn w
=> ("DescriptionWidth", SOME 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
)))
149 |
(EApp ((EVar
"iconWidth", _
), n
), _
) =>
150 Option
.map (fn w
=> ("IconWidth", SOME (Int.toString w
)))
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
))
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
)
171 val vhostsChanged
= ref
false
173 val () = Slave
.registerPreHandler
174 (fn () => vhostsChanged
:= false)
176 fun findVhostUser fname
=
178 val inf
= TextIO.openIn fname
181 case TextIO.inputLine inf
of
184 case String.tokens
Char.isSpace line
of
185 ["SuexecUserGroup", user
, _
] => SOME user
189 before TextIO.closeIn inf
192 val () = Slave
.registerFileHandler (fn fs
=>
194 val spl
= OS
.Path
.splitDirFile (#file fs
)
196 if String.isSuffix
".vhost" (#file spl
)
197 orelse String.isSuffix
".vhost_ssl" (#file spl
) then
198 case findVhostUser (#file fs
) of
199 NONE
=> print ("Can't find user in " ^ #file fs ^
"! Taking no action.\n")
202 val realVhostFile
= OS
.Path
.joinDirFile
203 {dir
= Config
.Apache
.confDir
,
206 val realLogDir
= OS
.Path
.joinDirFile
207 {dir
= Config
.homeBase
,
209 val realLogDir
= OS
.Path
.joinDirFile
212 val realLogDir
= OS
.Path
.joinDirFile
215 val realLogDir
= OS
.Path
.joinDirFile
217 file
= Slave
.hostname ()}
218 val {base
, ...} = OS
.Path
.splitBaseExt (#file spl
)
219 val realLogDir
= OS
.Path
.joinDirFile
223 vhostsChanged
:= true;
226 (ignore (OS
.Process
.system (Config
.rm
229 ignore (OS
.Process
.system (Config
.rm
233 (ignore (OS
.Process
.system (Config
.cp
238 OS
.FileSys
.mkDir realLogDir
)
241 ignore (OS
.Process
.system (Config
.cp
251 val () = Slave
.registerPostHandler
253 (if !vhostsChanged
then
254 Slave
.shellF ([Config
.Apache
.reload
],
255 fn cl
=> "Error reloading Apache with " ^ cl
)
259 val vhostFiles
: (string * TextIO.outstream
) list ref
= ref
[]
260 fun write
' s
= app (fn (node
, file
) => TextIO.output (file
, s node
)) (!vhostFiles
)
261 fun write s
= app (fn (_
, file
) => TextIO.output (file
, s
)) (!vhostFiles
)
263 val rewriteEnabled
= ref
false
264 val currentVhost
= ref
""
265 val currentVhostId
= ref
""
267 val pre
= ref (fn _
: {user
: string, nodes
: string list
, id
: string, hostname
: string} => ())
272 pre
:= (fn x
=> (old x
; f x
))
275 val post
= ref (fn () => ())
280 post
:= (fn () => (old (); f ()))
283 val aliaser
= ref (fn _
: string => ())
284 fun registerAliaser f
=
288 aliaser
:= (fn x
=> (old x
; f x
))
291 val () = Env
.containerV_one
"vhost"
295 val nodes
= Env
.env (Env
.list Env
.string) (env
, "WebNodes")
297 val ssl
= Env
.env Env
.bool (env
, "SSL")
298 val user
= Env
.env Env
.string (env
, "User")
299 val group
= Env
.env Env
.string (env
, "Group")
300 val docroot
= Env
.env Env
.string (env
, "DocumentRoot")
301 val sadmin
= Env
.env Env
.string (env
, "ServerAdmin")
303 val fullHost
= host ^
"." ^ Domain
.currentDomain ()
304 val vhostId
= fullHost ^
(if ssl
then ".ssl" else "")
305 val confFile
= fullHost ^
(if ssl
then ".vhost_ssl" else ".vhost")
307 currentVhost
:= fullHost
;
308 currentVhostId
:= vhostId
;
310 rewriteEnabled
:= false;
311 vhostFiles
:= map (fn node
=>
313 val file
= Domain
.domainFile
{node
= node
,
316 TextIO.output (file
, "<VirtualHost ");
317 TextIO.output (file
, Domain
.nodeIp node
);
318 TextIO.output (file
, ":");
319 TextIO.output (file
, if ssl
then
323 TextIO.output (file
, ">\n");
324 TextIO.output (file
, "\tErrorLog ");
325 TextIO.output (file
, Config
.homeBase
);
326 TextIO.output (file
, "/");
327 TextIO.output (file
, user
);
328 TextIO.output (file
, "/apache/log/");
329 TextIO.output (file
, node
);
330 TextIO.output (file
, "/");
331 TextIO.output (file
, vhostId
);
332 TextIO.output (file
, "/error.log\n\tCustomLog ");
333 TextIO.output (file
, Config
.homeBase
);
334 TextIO.output (file
, "/");
335 TextIO.output (file
, user
);
336 TextIO.output (file
, "/apache/log/");
337 TextIO.output (file
, node
);
338 TextIO.output (file
, "/");
339 TextIO.output (file
, vhostId
);
340 TextIO.output (file
, "/access.log combined\n");
341 (Config
.homeBase ^
"/" ^ user ^
"/apache/log/"
342 ^ node ^
"/" ^ vhostId
, file
)
345 write
"\tServerName ";
347 write
"\n\tSuexecUserGroup ";
351 write
"\n\tDocumentRoot ";
353 write
"\n\tServerAdmin ";
356 !pre
{user
= user
, nodes
= nodes
, id
= vhostId
, hostname
= fullHost
}
359 write
"</VirtualHost>\n";
360 app (TextIO.closeOut
o #
2) (!vhostFiles
)))
362 val () = Env
.container_one
"location"
363 ("prefix", Env
.string)
365 (write
"\t<Location ";
368 fn () => write
"\t</Location>\n")
370 val () = Env
.container_one
"directory"
371 ("directory", Env
.string)
373 (write
"\t<Directory ";
376 fn () => write
"\t</Directory>\n")
378 fun checkRewrite () =
379 if !rewriteEnabled
then
382 (write
"\tRewriteEngine on\n";
383 rewriteEnabled
:= true)
385 val () = Env
.action_three
"localProxyRewrite"
386 ("from", Env
.string, "to", Env
.string, "port", Env
.int)
387 (fn (from
, to
, port
) =>
389 write
"\tRewriteRule\t";
391 write
"\thttp://localhost:";
392 write (Int.toString port
);
397 val () = Env
.action_two
"proxyPass"
398 ("from", Env
.string, "to", Env
.string)
400 (write
"\tProxyPass\t";
406 val () = Env
.action_two
"proxyPassReverse"
407 ("from", Env
.string, "to", Env
.string)
409 (write
"\tProxyPassReverse\t";
415 val () = Env
.action_three
"rewriteRule"
416 ("from", Env
.string, "to", Env
.string, "flags", Env
.list flag
)
417 (fn (from
, to
, flags
) =>
419 write
"\tRewriteRule\t";
425 | flag
::rest
=> (write
" [";
427 app (fn flag
=> (write
",";
432 val () = Env
.action_three
"rewriteCond"
433 ("test", Env
.string, "pattern", Env
.string, "flags", Env
.list cond_flag
)
434 (fn (from
, to
, flags
) =>
436 write
"\tRewriteCond\t";
442 | flag
::rest
=> (write
" [";
444 app (fn flag
=> (write
",";
449 val () = Env
.action_one
"rewriteLogLevel"
453 write
"\tRewriteLog ";
455 write
"/rewrite.log\n\tRewriteLogLevel ";
456 write (Int.toString level
);
459 val () = Env
.action_two
"alias"
460 ("from", Env
.string, "to", Env
.string)
468 val () = Env
.action_two
"scriptAlias"
469 ("from", Env
.string, "to", Env
.string)
471 (write
"\tScriptAlias\t";
477 val () = Env
.action_two
"errorDocument"
478 ("code", Env
.string, "handler", Env
.string)
479 (fn (code
, handler
) =>
480 (write
"\tErrorDocument\t";
486 val () = Env
.action_one
"options"
487 ("options", Env
.list apache_option
)
491 | _
=> (write
"\tOptions";
492 app (fn opt
=> (write
" "; write opt
)) opts
;
495 val () = Env
.action_one
"set_options"
496 ("options", Env
.list apache_option
)
500 | _
=> (write
"\tOptions";
501 app (fn opt
=> (write
" +"; write opt
)) opts
;
504 val () = Env
.action_one
"unset_options"
505 ("options", Env
.list apache_option
)
509 | _
=> (write
"\tOptions";
510 app (fn opt
=> (write
" -"; write opt
)) opts
;
513 val () = Env
.action_one
"directoryIndex"
514 ("filenames", Env
.list Env
.string)
516 (write
"\tDirectoryIndex";
517 app (fn opt
=> (write
" "; write opt
)) opts
;
520 val () = Env
.action_one
"serverAlias"
523 (write
"\tServerAlias ";
528 val authType
= fn (EVar
"basic", _
) => SOME
"basic"
529 |
(EVar
"digest", _
) => SOME
"digest"
532 val () = Env
.action_one
"authType"
535 (write
"\tAuthType ";
539 val () = Env
.action_one
"authName"
542 (write
"\tAuthName \"";
546 val () = Env
.action_one
"authUserFile"
549 (write
"\tAuthUserFile ";
553 val () = Env
.action_none
"requireValidUser"
554 (fn () => write
"\tRequire valid-user\n")
556 val () = Env
.action_one
"requireUser"
557 ("users", Env
.list Env
.string)
561 | _
=> (write
"\tRequire user";
562 app (fn name
=> (write
" "; write name
)) names
;
565 val () = Env
.action_one
"requireGroup"
566 ("groups", Env
.list Env
.string)
570 | _
=> (write
"\tRequire group";
571 app (fn name
=> (write
" "; write name
)) names
;
574 val () = Env
.action_none
"orderAllowDeny"
575 (fn () => write
"\tOrder allow,deny\n")
577 val () = Env
.action_none
"orderDenyAllow"
578 (fn () => write
"\tOrder deny,allow\n")
580 val () = Env
.action_none
"allowFromAll"
581 (fn () => write
"\tAllow from all\n")
583 val () = Env
.action_one
"allowFrom"
584 ("entries", Env
.list Env
.string)
588 | _
=> (write
"\tAllow from";
589 app (fn name
=> (write
" "; write name
)) names
;
592 val () = Env
.action_none
"denyFromAll"
593 (fn () => write
"\tDeny from all\n")
595 val () = Env
.action_one
"denyFrom"
596 ("entries", Env
.list Env
.string)
600 | _
=> (write
"\tDeny from";
601 app (fn name
=> (write
" "; write name
)) names
;
604 val () = Env
.action_none
"satisfyAll"
605 (fn () => write
"\tSatisfy all\n")
607 val () = Env
.action_none
"satisfyAny"
608 (fn () => write
"\tSatisfy any\n")
610 val () = Env
.action_one
"forceType"
612 (fn ty
=> (write
"\tForceType ";
616 val () = Env
.action_none
"forceTypeOff"
617 (fn () => write
"\tForceType None\n")
619 val () = Env
.action_two
"action"
620 ("what", Env
.string, "how", Env
.string)
621 (fn (what
, how
) => (write
"\tAction ";
627 val () = Env
.action_one
"addDefaultCharset"
628 ("charset", Env
.string)
629 (fn ty
=> (write
"\tAddDefaultCharset ";
633 (*val () = Env
.action_one
"davSvn"
635 (fn path
=> (write
"\tDAV svn\n\tSVNPath ";
639 val () = Env
.action_one
"authzSvnAccessFile"
641 (fn path
=> (write
"\tAuthzSVNAccessFile ";
645 val () = Env
.action_two
"addDescription"
646 ("description", Env
.string, "patterns", Env
.list Env
.string)
650 | _
=> (write
"\tAddDescription \"";
651 write (String.toString desc
);
653 app (fn pat
=> (write
" "; write pat
)) pats
;
656 val () = Env
.action_one
"indexOptions"
657 ("options", Env
.list autoindex_option
)
661 | _
=> (write
"\tIndexOptions";
662 app (fn (opt
, arg
) =>
665 Option
.app (fn arg
=>
666 (write
"="; write arg
)) arg
)) opts
;
669 val () = Env
.action_one
"set_indexOptions"
670 ("options", Env
.list autoindex_option
)
674 | _
=> (write
"\tIndexOptions";
675 app (fn (opt
, arg
) =>
678 Option
.app (fn arg
=>
679 (write
"="; write arg
)) arg
)) opts
;
682 val () = Env
.action_one
"unset_indexOptions"
683 ("options", Env
.list autoindex_option
)
687 | _
=> (write
"\tIndexOptions";
693 val () = Env
.action_one
"headerName"
695 (fn name
=> (write
"\tHeaderName ";
699 val () = Env
.action_one
"readmeName"
701 (fn name
=> (write
"\tReadmeName ";