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
.type_one
"proxy_port"
36 val _
= Env
.type_one
"proxy_target"
40 fun default () = List.exists (fn s
' => s
= s
') Config
.Apache
.proxyTargets
42 case String.fields (fn ch
=> ch
= #
":") s
of
43 ["http", "//localhost", rest
] =>
44 (case String.fields (fn ch
=> ch
= #
"/") rest
of
46 (case Int.fromString port
of
48 | SOME n
=> n
> 1024 orelse default ())
53 val _
= Env
.type_one
"rewrite_arg"
55 (CharVector
.all
Char.isAlphaNum
)
58 size s
> 0 andalso size s
< 1000 andalso CharVector
.all
59 (fn ch
=> Char.isAlphaNum ch
65 val _
= Env
.type_one
"location"
69 val dl
= ErrorMsg
.dummyLoc
71 val _
= Env
.registerFunction ("web_node_to_node",
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 OS
.FileSys
.mkDir realLogDir
)
252 ignore (OS
.Process
.system (Config
.cp
262 val () = Slave
.registerPostHandler
264 (if !vhostsChanged
then
265 Slave
.shellF ([Config
.Apache
.reload
],
266 fn cl
=> "Error reloading Apache with " ^ cl
)
270 val vhostFiles
: (string * TextIO.outstream
) list ref
= ref
[]
271 fun write
' s
= app (fn (node
, file
) => TextIO.output (file
, s node
)) (!vhostFiles
)
272 fun write s
= app (fn (_
, file
) => TextIO.output (file
, s
)) (!vhostFiles
)
274 val rewriteEnabled
= ref
false
275 val currentVhost
= ref
""
276 val currentVhostId
= ref
""
278 val pre
= ref (fn _
: {user
: string, nodes
: string list
, id
: string, hostname
: string} => ())
283 pre
:= (fn x
=> (old x
; f x
))
286 val post
= ref (fn () => ())
291 post
:= (fn () => (old (); f ()))
294 val aliaser
= ref (fn _
: string => ())
295 fun registerAliaser f
=
299 aliaser
:= (fn x
=> (old x
; f x
))
302 val () = Env
.containerV_one
"vhost"
306 val nodes
= Env
.env (Env
.list Env
.string) (env
, "WebNodes")
308 val ssl
= Env
.env Env
.bool (env
, "SSL")
309 val user
= Env
.env Env
.string (env
, "User")
310 val group
= Env
.env Env
.string (env
, "Group")
311 val docroot
= Env
.env Env
.string (env
, "DocumentRoot")
312 val sadmin
= Env
.env Env
.string (env
, "ServerAdmin")
314 val fullHost
= host ^
"." ^ Domain
.currentDomain ()
315 val vhostId
= fullHost ^
(if ssl
then ".ssl" else "")
316 val confFile
= fullHost ^
(if ssl
then ".vhost_ssl" else ".vhost")
318 currentVhost
:= fullHost
;
319 currentVhostId
:= vhostId
;
321 rewriteEnabled
:= false;
322 vhostFiles
:= map (fn node
=>
324 val file
= Domain
.domainFile
{node
= node
,
327 TextIO.output (file
, "<VirtualHost ");
328 TextIO.output (file
, Domain
.nodeIp node
);
329 TextIO.output (file
, ":");
330 TextIO.output (file
, if ssl
then
334 TextIO.output (file
, ">\n");
335 TextIO.output (file
, "\tErrorLog ");
336 TextIO.output (file
, Config
.homeBase
);
337 TextIO.output (file
, "/");
338 TextIO.output (file
, user
);
339 TextIO.output (file
, "/apache/log/");
340 TextIO.output (file
, node
);
341 TextIO.output (file
, "/");
342 TextIO.output (file
, vhostId
);
343 TextIO.output (file
, "/error.log\n\tCustomLog ");
344 TextIO.output (file
, Config
.homeBase
);
345 TextIO.output (file
, "/");
346 TextIO.output (file
, user
);
347 TextIO.output (file
, "/apache/log/");
348 TextIO.output (file
, node
);
349 TextIO.output (file
, "/");
350 TextIO.output (file
, vhostId
);
351 TextIO.output (file
, "/access.log combined\n");
352 (Config
.homeBase ^
"/" ^ user ^
"/apache/log/"
353 ^ node ^
"/" ^ vhostId
, file
)
356 write
"\tServerName ";
358 write
"\n\tSuexecUserGroup ";
362 write
"\n\tDocumentRoot ";
364 write
"\n\tServerAdmin ";
367 !pre
{user
= user
, nodes
= nodes
, id
= vhostId
, hostname
= fullHost
}
370 write
"</VirtualHost>\n";
371 app (TextIO.closeOut
o #
2) (!vhostFiles
)))
373 val () = Env
.container_one
"location"
374 ("prefix", Env
.string)
376 (write
"\t<Location ";
379 fn () => write
"\t</Location>\n")
381 val () = Env
.container_one
"directory"
382 ("directory", Env
.string)
384 (write
"\t<Directory ";
387 fn () => write
"\t</Directory>\n")
389 fun checkRewrite () =
390 if !rewriteEnabled
then
393 (write
"\tRewriteEngine on\n";
394 rewriteEnabled
:= true)
396 val () = Env
.action_three
"localProxyRewrite"
397 ("from", Env
.string, "to", Env
.string, "port", Env
.int)
398 (fn (from
, to
, port
) =>
400 write
"\tRewriteRule\t";
402 write
"\thttp://localhost:";
403 write (Int.toString port
);
408 val () = Env
.action_two
"proxyPass"
409 ("from", Env
.string, "to", Env
.string)
411 (write
"\tProxyPass\t";
417 val () = Env
.action_two
"proxyPassReverse"
418 ("from", Env
.string, "to", Env
.string)
420 (write
"\tProxyPassReverse\t";
426 val () = Env
.action_three
"rewriteRule"
427 ("from", Env
.string, "to", Env
.string, "flags", Env
.list flag
)
428 (fn (from
, to
, flags
) =>
430 write
"\tRewriteRule\t";
436 | flag
::rest
=> (write
" [";
438 app (fn flag
=> (write
",";
443 val () = Env
.action_three
"rewriteCond"
444 ("test", Env
.string, "pattern", Env
.string, "flags", Env
.list cond_flag
)
445 (fn (from
, to
, flags
) =>
447 write
"\tRewriteCond\t";
453 | flag
::rest
=> (write
" [";
455 app (fn flag
=> (write
",";
460 val () = Env
.action_one
"rewriteLogLevel"
464 write
"\tRewriteLog ";
466 write
"/rewrite.log\n\tRewriteLogLevel ";
467 write (Int.toString level
);
470 val () = Env
.action_two
"alias"
471 ("from", Env
.string, "to", Env
.string)
479 val () = Env
.action_two
"scriptAlias"
480 ("from", Env
.string, "to", Env
.string)
482 (write
"\tScriptAlias\t";
488 val () = Env
.action_two
"errorDocument"
489 ("code", Env
.string, "handler", Env
.string)
490 (fn (code
, handler
) =>
491 (write
"\tErrorDocument\t";
497 val () = Env
.action_one
"options"
498 ("options", Env
.list apache_option
)
502 | _
=> (write
"\tOptions";
503 app (fn opt
=> (write
" "; write opt
)) opts
;
506 val () = Env
.action_one
"set_options"
507 ("options", Env
.list apache_option
)
511 | _
=> (write
"\tOptions";
512 app (fn opt
=> (write
" +"; write opt
)) opts
;
515 val () = Env
.action_one
"unset_options"
516 ("options", Env
.list apache_option
)
520 | _
=> (write
"\tOptions";
521 app (fn opt
=> (write
" -"; write opt
)) opts
;
524 val () = Env
.action_one
"directoryIndex"
525 ("filenames", Env
.list Env
.string)
527 (write
"\tDirectoryIndex";
528 app (fn opt
=> (write
" "; write opt
)) opts
;
531 val () = Env
.action_one
"serverAlias"
534 (write
"\tServerAlias ";
539 val authType
= fn (EVar
"basic", _
) => SOME
"basic"
540 |
(EVar
"digest", _
) => SOME
"digest"
543 val () = Env
.action_one
"authType"
546 (write
"\tAuthType ";
550 val () = Env
.action_one
"authName"
553 (write
"\tAuthName \"";
557 val () = Env
.action_one
"authUserFile"
560 (write
"\tAuthUserFile ";
564 val () = Env
.action_none
"requireValidUser"
565 (fn () => write
"\tRequire valid-user\n")
567 val () = Env
.action_one
"requireUser"
568 ("users", Env
.list Env
.string)
572 | _
=> (write
"\tRequire user";
573 app (fn name
=> (write
" "; write name
)) names
;
576 val () = Env
.action_one
"requireGroup"
577 ("groups", Env
.list Env
.string)
581 | _
=> (write
"\tRequire group";
582 app (fn name
=> (write
" "; write name
)) names
;
585 val () = Env
.action_none
"orderAllowDeny"
586 (fn () => write
"\tOrder allow,deny\n")
588 val () = Env
.action_none
"orderDenyAllow"
589 (fn () => write
"\tOrder deny,allow\n")
591 val () = Env
.action_none
"allowFromAll"
592 (fn () => write
"\tAllow from all\n")
594 val () = Env
.action_one
"allowFrom"
595 ("entries", Env
.list Env
.string)
599 | _
=> (write
"\tAllow from";
600 app (fn name
=> (write
" "; write name
)) names
;
603 val () = Env
.action_none
"denyFromAll"
604 (fn () => write
"\tDeny from all\n")
606 val () = Env
.action_one
"denyFrom"
607 ("entries", Env
.list Env
.string)
611 | _
=> (write
"\tDeny from";
612 app (fn name
=> (write
" "; write name
)) names
;
615 val () = Env
.action_none
"satisfyAll"
616 (fn () => write
"\tSatisfy all\n")
618 val () = Env
.action_none
"satisfyAny"
619 (fn () => write
"\tSatisfy any\n")
621 val () = Env
.action_one
"forceType"
623 (fn ty
=> (write
"\tForceType ";
627 val () = Env
.action_none
"forceTypeOff"
628 (fn () => write
"\tForceType None\n")
630 val () = Env
.action_two
"action"
631 ("what", Env
.string, "how", Env
.string)
632 (fn (what
, how
) => (write
"\tAction ";
638 val () = Env
.action_one
"addDefaultCharset"
639 ("charset", Env
.string)
640 (fn ty
=> (write
"\tAddDefaultCharset ";
644 (*val () = Env
.action_one
"davSvn"
646 (fn path
=> (write
"\tDAV svn\n\tSVNPath ";
650 val () = Env
.action_one
"authzSvnAccessFile"
652 (fn path
=> (write
"\tAuthzSVNAccessFile ";
656 val () = Env
.action_two
"addDescription"
657 ("description", Env
.string, "patterns", Env
.list Env
.string)
661 | _
=> (write
"\tAddDescription \"";
662 write (String.toString desc
);
664 app (fn pat
=> (write
" "; write pat
)) pats
;
667 val () = Env
.action_one
"indexOptions"
668 ("options", Env
.list autoindex_option
)
672 | _
=> (write
"\tIndexOptions";
673 app (fn (opt
, arg
) =>
676 Option
.app (fn arg
=>
677 (write
"="; write arg
)) arg
)) opts
;
680 val () = Env
.action_one
"set_indexOptions"
681 ("options", Env
.list autoindex_option
)
685 | _
=> (write
"\tIndexOptions";
686 app (fn (opt
, arg
) =>
689 Option
.app (fn arg
=>
690 (write
"="; write arg
)) arg
)) opts
;
693 val () = Env
.action_one
"unset_indexOptions"
694 ("options", Env
.list autoindex_option
)
698 | _
=> (write
"\tIndexOptions";
704 val () = Env
.action_one
"headerName"
706 (fn name
=> (write
"\tHeaderName ";
710 val () = Env
.action_one
"readmeName"
712 (fn name
=> (write
"\tReadmeName ";