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
"web_node"
28 List.exists (fn x
=> x
= node
) Config
.Apache
.webNodes_all
29 orelse (Domain
.hasPriv
"www"
30 andalso List.exists (fn x
=> x
= node
) Config
.Apache
.webNodes_admin
))
32 val _
= Env
.registerFunction ("web_node_to_node",
36 val _
= Env
.type_one
"proxy_port"
40 val _
= Env
.type_one
"proxy_target"
44 fun default () = List.exists (fn s
' => s
= s
') Config
.Apache
.proxyTargets
46 case String.fields (fn ch
=> ch
= #
":") s
of
47 ["http", "//localhost", rest
] =>
48 (case String.fields (fn ch
=> ch
= #
"/") rest
of
50 (case Int.fromString port
of
52 | SOME n
=> n
> 1024 orelse default ())
57 val _
= Env
.type_one
"rewrite_arg"
59 (CharVector
.all
Char.isAlphaNum
)
62 size s
> 0 andalso size s
< 1000 andalso CharVector
.all
63 (fn ch
=> Char.isAlphaNum ch
69 val _
= Env
.type_one
"location"
73 val dl
= ErrorMsg
.dummyLoc
75 val _
= Defaults
.registerDefault ("WebNodes",
76 (TList (TBase
"web_node", dl
), dl
),
77 (fn () => (EList (map (fn s
=> (EString s
, dl
)) Config
.Apache
.webNodes_default
), dl
)))
79 val _
= Defaults
.registerDefault ("SSL",
81 (fn () => (EVar
"false", dl
)))
83 val _
= Defaults
.registerDefault ("User",
84 (TBase
"your_user", dl
),
85 (fn () => (EString (Domain
.getUser ()), dl
)))
87 val _
= Defaults
.registerDefault ("Group",
88 (TBase
"your_group", dl
),
89 (fn () => (EString (Domain
.getUser ()), dl
)))
91 val _
= Defaults
.registerDefault ("DocumentRoot",
92 (TBase
"your_path", dl
),
93 (fn () => (EString (Config
.homeBase ^
"/" ^ Domain
.getUser () ^
"/" ^ Config
.Apache
.public_html
), dl
)))
95 val _
= Defaults
.registerDefault ("ServerAdmin",
97 (fn () => (EString (Domain
.getUser () ^
"@" ^ Config
.defaultDomain
), dl
)))
100 val redirect_code
= fn (EVar
"temp", _
) => SOME
"temp"
101 |
(EVar
"permanent", _
) => SOME
"permanent"
102 |
(EVar
"seeother", _
) => SOME
"seeother"
103 |
(EVar
"redir300", _
) => SOME
"300"
104 |
(EVar
"redir301", _
) => SOME
"301"
105 |
(EVar
"redir302", _
) => SOME
"302"
106 |
(EVar
"redir303", _
) => SOME
"303"
107 |
(EVar
"redir304", _
) => SOME
"304"
108 |
(EVar
"redir305", _
) => SOME
"305"
109 |
(EVar
"redir307", _
) => SOME
"307"
112 val flag
= fn (EVar
"redirect", _
) => SOME
"R"
113 |
(EVar
"forbidden", _
) => SOME
"F"
114 |
(EVar
"gone", _
) => SOME
"G"
115 |
(EVar
"last", _
) => SOME
"L"
116 |
(EVar
"chain", _
) => SOME
"C"
117 |
(EVar
"nosubreq", _
) => SOME
"NS"
118 |
(EVar
"nocase", _
) => SOME
"NC"
119 |
(EVar
"qsappend", _
) => SOME
"QSA"
120 |
(EVar
"noescape", _
) => SOME
"NE"
121 |
(EVar
"passthrough", _
) => SOME
"PT"
122 |
(EApp ((EVar
"mimeType", _
), e
), _
) =>
123 Option
.map (fn s
=> "T=" ^ s
) (Env
.string e
)
124 |
(EApp ((EVar
"redirectWith", _
), e
), _
) =>
125 Option
.map (fn s
=> "R=" ^ s
) (redirect_code e
)
126 |
(EApp ((EVar
"skip", _
), e
), _
) =>
127 Option
.map (fn n
=> "S=" ^
Int.toString n
) (Env
.int e
)
128 |
(EApp ((EApp ((EVar
"env", _
), e1
), _
), e2
), _
) =>
129 (case Env
.string e1
of
131 | SOME s1
=> Option
.map (fn s2
=> "E=" ^ s1 ^
":" ^ s2
)
136 val cond_flag
= fn (EVar
"cond_nocase", _
) => SOME
"NC"
137 |
(EVar
"ornext", _
) => SOME
"OR"
140 val apache_option
= fn (EVar
"execCGI", _
) => SOME
"ExecCGI"
141 |
(EVar
"includesNOEXEC", _
) => SOME
"IncludesNOEXEC"
142 |
(EVar
"indexes", _
) => SOME
"Indexes"
145 val autoindex_width
= fn (EVar
"autofit", _
) => SOME
"*"
146 |
(EApp ((EVar
"characters", _
), n
), _
) =>
147 Option
.map
Int.toString (Env
.int n
)
150 val autoindex_option
= fn (EApp ((EVar
"descriptionWidth", _
), w
), _
) =>
151 Option
.map (fn w
=> ("DescriptionWidth", SOME w
))
153 |
(EVar
"fancyIndexing", _
) => SOME ("FancyIndexing", NONE
)
154 |
(EVar
"foldersFirst", _
) => SOME ("FoldersFirst", NONE
)
155 |
(EVar
"htmlTable", _
) => SOME ("HTMLTable", NONE
)
156 |
(EVar
"iconsAreLinks", _
) => SOME ("IconsAreLinks", NONE
)
157 |
(EApp ((EVar
"iconHeight", _
), n
), _
) =>
158 Option
.map (fn w
=> ("IconHeight", SOME (Int.toString w
)))
160 |
(EApp ((EVar
"iconWidth", _
), n
), _
) =>
161 Option
.map (fn w
=> ("IconWidth", SOME (Int.toString w
)))
163 |
(EVar
"ignoreCase", _
) => SOME ("IgnoreCase", NONE
)
164 |
(EVar
"ignoreClient", _
) => SOME ("IgnoreClient", NONE
)
165 |
(EApp ((EVar
"nameWidth", _
), w
), _
) =>
166 Option
.map (fn w
=> ("NameWidth", SOME w
))
168 |
(EVar
"scanHtmlTitles", _
) => SOME ("ScanHTMLTitles", NONE
)
169 |
(EVar
"suppressColumnSorting", _
) => SOME ("SuppressColumnSorting", NONE
)
170 |
(EVar
"suppressDescription", _
) => SOME ("SuppressDescription", NONE
)
171 |
(EVar
"suppressHtmlPreamble", _
) => SOME ("SuppressHTMLPreamble", NONE
)
172 |
(EVar
"suppressIcon", _
) => SOME ("SuppressIcon", NONE
)
173 |
(EVar
"suppressLastModified", _
) => SOME ("SuppressLastModified", NONE
)
174 |
(EVar
"suppressRules", _
) => SOME ("SuppressRules", NONE
)
175 |
(EVar
"suppressSize", _
) => SOME ("SuppressSize", NONE
)
176 |
(EVar
"trackModified", _
) => SOME ("TrackModified", NONE
)
177 |
(EVar
"versionSort", _
) => SOME ("VersionSort", NONE
)
178 |
(EVar
"xhtml", _
) => SOME ("XHTML", NONE
)
182 val vhostsChanged
= ref
false
184 val () = Slave
.registerPreHandler
185 (fn () => vhostsChanged
:= false)
187 fun findVhostUser fname
=
189 val inf
= TextIO.openIn fname
192 case TextIO.inputLine inf
of
195 case String.tokens
Char.isSpace line
of
196 ["SuexecUserGroup", user
, _
] => SOME user
200 before TextIO.closeIn inf
203 val () = Slave
.registerFileHandler (fn fs
=>
205 val spl
= OS
.Path
.splitDirFile (#file fs
)
207 if String.isSuffix
".vhost" (#file spl
)
208 orelse String.isSuffix
".vhost_ssl" (#file spl
) then
209 case findVhostUser (#file fs
) of
210 NONE
=> print ("Can't find user in " ^ #file fs ^
"! Taking no action.\n")
213 val realVhostFile
= OS
.Path
.joinDirFile
214 {dir
= Config
.Apache
.confDir
,
217 val realLogDir
= OS
.Path
.joinDirFile
218 {dir
= Config
.homeBase
,
220 val realLogDir
= OS
.Path
.joinDirFile
223 val realLogDir
= OS
.Path
.joinDirFile
226 val realLogDir
= OS
.Path
.joinDirFile
228 file
= Slave
.hostname ()}
229 val {base
, ...} = OS
.Path
.splitBaseExt (#file spl
)
230 val realLogDir
= OS
.Path
.joinDirFile
234 vhostsChanged
:= true;
237 (ignore (OS
.Process
.system (Config
.rm
240 ignore (OS
.Process
.system (Config
.rm
244 (ignore (OS
.Process
.system (Config
.cp
249 if Posix
.FileSys
.access (realLogDir
, []) then
252 OS
.FileSys
.mkDir realLogDir
)
255 ignore (OS
.Process
.system (Config
.cp
265 val () = Slave
.registerPostHandler
267 (if !vhostsChanged
then
268 Slave
.shellF ([Config
.Apache
.reload
],
269 fn cl
=> "Error reloading Apache with " ^ cl
)
273 val vhostFiles
: (string * TextIO.outstream
) list ref
= ref
[]
274 fun write
' s
= app (fn (node
, file
) => TextIO.output (file
, s node
)) (!vhostFiles
)
275 fun write s
= app (fn (_
, file
) => TextIO.output (file
, s
)) (!vhostFiles
)
277 val rewriteEnabled
= ref
false
278 val localRewriteEnabled
= ref
false
279 val currentVhost
= ref
""
280 val currentVhostId
= ref
""
282 val pre
= ref (fn _
: {user
: string, nodes
: string list
, id
: string, hostname
: string} => ())
287 pre
:= (fn x
=> (old x
; f x
))
290 val post
= ref (fn () => ())
295 post
:= (fn () => (old (); f ()))
298 val aliaser
= ref (fn _
: string => ())
299 fun registerAliaser f
=
303 aliaser
:= (fn x
=> (old x
; f x
))
306 val () = Env
.containerV_one
"vhost"
310 val nodes
= Env
.env (Env
.list Env
.string) (env
, "WebNodes")
312 val ssl
= Env
.env Env
.bool (env
, "SSL")
313 val user
= Env
.env Env
.string (env
, "User")
314 val group
= Env
.env Env
.string (env
, "Group")
315 val docroot
= Env
.env Env
.string (env
, "DocumentRoot")
316 val sadmin
= Env
.env Env
.string (env
, "ServerAdmin")
318 val fullHost
= host ^
"." ^ Domain
.currentDomain ()
319 val vhostId
= fullHost ^
(if ssl
then ".ssl" else "")
320 val confFile
= fullHost ^
(if ssl
then ".vhost_ssl" else ".vhost")
322 currentVhost
:= fullHost
;
323 currentVhostId
:= vhostId
;
325 rewriteEnabled
:= false;
326 localRewriteEnabled
:= false;
327 vhostFiles
:= map (fn node
=>
329 val file
= Domain
.domainFile
{node
= node
,
332 TextIO.output (file
, "<VirtualHost ");
333 TextIO.output (file
, Domain
.nodeIp node
);
334 TextIO.output (file
, ":");
335 TextIO.output (file
, if ssl
then
339 TextIO.output (file
, ">\n");
340 TextIO.output (file
, "\tErrorLog ");
341 TextIO.output (file
, Config
.homeBase
);
342 TextIO.output (file
, "/");
343 TextIO.output (file
, user
);
344 TextIO.output (file
, "/apache/log/");
345 TextIO.output (file
, node
);
346 TextIO.output (file
, "/");
347 TextIO.output (file
, vhostId
);
348 TextIO.output (file
, "/error.log\n\tCustomLog ");
349 TextIO.output (file
, Config
.homeBase
);
350 TextIO.output (file
, "/");
351 TextIO.output (file
, user
);
352 TextIO.output (file
, "/apache/log/");
353 TextIO.output (file
, node
);
354 TextIO.output (file
, "/");
355 TextIO.output (file
, vhostId
);
356 TextIO.output (file
, "/access.log combined\n");
357 (Config
.homeBase ^
"/" ^ user ^
"/apache/log/"
358 ^ node ^
"/" ^ vhostId
, file
)
361 write
"\tServerName ";
363 write
"\n\tSuexecUserGroup ";
367 write
"\n\tDocumentRoot ";
369 write
"\n\tServerAdmin ";
372 !pre
{user
= user
, nodes
= nodes
, id
= vhostId
, hostname
= fullHost
}
375 write
"</VirtualHost>\n";
376 app (TextIO.closeOut
o #
2) (!vhostFiles
)))
378 val inLocal
= ref
false
380 val () = Env
.container_one
"location"
381 ("prefix", Env
.string)
383 (write
"\t<Location ";
387 fn () => (write
"\t</Location>\n";
389 localRewriteEnabled
:= false))
391 val () = Env
.container_one
"directory"
392 ("directory", Env
.string)
394 (write
"\t<Directory ";
398 fn () => (write
"\t</Directory>\n";
400 localRewriteEnabled
:= false))
402 fun checkRewrite () =
404 if !rewriteEnabled
orelse !localRewriteEnabled
then
407 (write
"\tRewriteEngine on\n";
408 localRewriteEnabled
:= true)
409 else if !rewriteEnabled
then
412 (write
"\tRewriteEngine on\n";
413 rewriteEnabled
:= true)
415 val () = Env
.action_three
"localProxyRewrite"
416 ("from", Env
.string, "to", Env
.string, "port", Env
.int)
417 (fn (from
, to
, port
) =>
419 write
"\tRewriteRule\t";
421 write
"\thttp://localhost:";
422 write (Int.toString port
);
427 val () = Env
.action_two
"proxyPass"
428 ("from", Env
.string, "to", Env
.string)
430 (write
"\tProxyPass\t";
436 val () = Env
.action_two
"proxyPassReverse"
437 ("from", Env
.string, "to", Env
.string)
439 (write
"\tProxyPassReverse\t";
445 val () = Env
.action_three
"rewriteRule"
446 ("from", Env
.string, "to", Env
.string, "flags", Env
.list flag
)
447 (fn (from
, to
, flags
) =>
449 write
"\tRewriteRule\t";
455 | flag
::rest
=> (write
" [";
457 app (fn flag
=> (write
",";
462 val () = Env
.action_three
"rewriteCond"
463 ("test", Env
.string, "pattern", Env
.string, "flags", Env
.list cond_flag
)
464 (fn (from
, to
, flags
) =>
466 write
"\tRewriteCond\t";
472 | flag
::rest
=> (write
" [";
474 app (fn flag
=> (write
",";
479 val () = Env
.action_one
"rewriteLogLevel"
483 write
"\tRewriteLog ";
485 write
"/rewrite.log\n\tRewriteLogLevel ";
486 write (Int.toString level
);
489 val () = Env
.action_two
"alias"
490 ("from", Env
.string, "to", Env
.string)
498 val () = Env
.action_two
"scriptAlias"
499 ("from", Env
.string, "to", Env
.string)
501 (write
"\tScriptAlias\t";
507 val () = Env
.action_two
"errorDocument"
508 ("code", Env
.string, "handler", Env
.string)
509 (fn (code
, handler
) =>
510 (write
"\tErrorDocument\t";
516 val () = Env
.action_one
"options"
517 ("options", Env
.list apache_option
)
521 | _
=> (write
"\tOptions";
522 app (fn opt
=> (write
" "; write opt
)) opts
;
525 val () = Env
.action_one
"set_options"
526 ("options", Env
.list apache_option
)
530 | _
=> (write
"\tOptions";
531 app (fn opt
=> (write
" +"; write opt
)) opts
;
534 val () = Env
.action_one
"unset_options"
535 ("options", Env
.list apache_option
)
539 | _
=> (write
"\tOptions";
540 app (fn opt
=> (write
" -"; write opt
)) opts
;
543 val () = Env
.action_one
"directoryIndex"
544 ("filenames", Env
.list Env
.string)
546 (write
"\tDirectoryIndex";
547 app (fn opt
=> (write
" "; write opt
)) opts
;
550 val () = Env
.action_one
"serverAlias"
553 (write
"\tServerAlias ";
558 val authType
= fn (EVar
"basic", _
) => SOME
"basic"
559 |
(EVar
"digest", _
) => SOME
"digest"
562 val () = Env
.action_one
"authType"
565 (write
"\tAuthType ";
569 val () = Env
.action_one
"authName"
572 (write
"\tAuthName \"";
576 val () = Env
.action_one
"authUserFile"
579 (write
"\tAuthUserFile ";
583 val () = Env
.action_none
"requireValidUser"
584 (fn () => write
"\tRequire valid-user\n")
586 val () = Env
.action_one
"requireUser"
587 ("users", Env
.list Env
.string)
591 | _
=> (write
"\tRequire user";
592 app (fn name
=> (write
" "; write name
)) names
;
595 val () = Env
.action_one
"requireGroup"
596 ("groups", Env
.list Env
.string)
600 | _
=> (write
"\tRequire group";
601 app (fn name
=> (write
" "; write name
)) names
;
604 val () = Env
.action_none
"orderAllowDeny"
605 (fn () => write
"\tOrder allow,deny\n")
607 val () = Env
.action_none
"orderDenyAllow"
608 (fn () => write
"\tOrder deny,allow\n")
610 val () = Env
.action_none
"allowFromAll"
611 (fn () => write
"\tAllow from all\n")
613 val () = Env
.action_one
"allowFrom"
614 ("entries", Env
.list Env
.string)
618 | _
=> (write
"\tAllow from";
619 app (fn name
=> (write
" "; write name
)) names
;
622 val () = Env
.action_none
"denyFromAll"
623 (fn () => write
"\tDeny from all\n")
625 val () = Env
.action_one
"denyFrom"
626 ("entries", Env
.list Env
.string)
630 | _
=> (write
"\tDeny from";
631 app (fn name
=> (write
" "; write name
)) names
;
634 val () = Env
.action_none
"satisfyAll"
635 (fn () => write
"\tSatisfy all\n")
637 val () = Env
.action_none
"satisfyAny"
638 (fn () => write
"\tSatisfy any\n")
640 val () = Env
.action_one
"forceType"
642 (fn ty
=> (write
"\tForceType ";
646 val () = Env
.action_none
"forceTypeOff"
647 (fn () => write
"\tForceType None\n")
649 val () = Env
.action_two
"action"
650 ("what", Env
.string, "how", Env
.string)
651 (fn (what
, how
) => (write
"\tAction ";
657 val () = Env
.action_one
"addDefaultCharset"
658 ("charset", Env
.string)
659 (fn ty
=> (write
"\tAddDefaultCharset ";
663 (*val () = Env
.action_one
"davSvn"
665 (fn path
=> (write
"\tDAV svn\n\tSVNPath ";
669 val () = Env
.action_one
"authzSvnAccessFile"
671 (fn path
=> (write
"\tAuthzSVNAccessFile ";
675 val () = Env
.action_two
"addDescription"
676 ("description", Env
.string, "patterns", Env
.list Env
.string)
680 | _
=> (write
"\tAddDescription \"";
681 write (String.toString desc
);
683 app (fn pat
=> (write
" "; write pat
)) pats
;
686 val () = Env
.action_one
"indexOptions"
687 ("options", Env
.list autoindex_option
)
691 | _
=> (write
"\tIndexOptions";
692 app (fn (opt
, arg
) =>
695 Option
.app (fn arg
=>
696 (write
"="; write arg
)) arg
)) opts
;
699 val () = Env
.action_one
"set_indexOptions"
700 ("options", Env
.list autoindex_option
)
704 | _
=> (write
"\tIndexOptions";
705 app (fn (opt
, arg
) =>
708 Option
.app (fn arg
=>
709 (write
"="; write arg
)) arg
)) opts
;
712 val () = Env
.action_one
"unset_indexOptions"
713 ("options", Env
.list autoindex_option
)
717 | _
=> (write
"\tIndexOptions";
723 val () = Env
.action_one
"headerName"
725 (fn name
=> (write
"\tHeaderName ";
729 val () = Env
.action_one
"readmeName"
731 (fn name
=> (write
"\tReadmeName ";