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
183 val logDeleted
= ref
false
185 val () = Slave
.registerPreHandler
186 (fn () => (vhostsChanged
:= false;
187 logDeleted
:= false))
189 fun findVhostUser fname
=
191 val inf
= TextIO.openIn fname
194 case TextIO.inputLine inf
of
197 case String.tokens
Char.isSpace line
of
198 ["SuexecUserGroup", user
, _
] => SOME user
202 before TextIO.closeIn inf
205 val () = Slave
.registerFileHandler (fn fs
=>
207 val spl
= OS
.Path
.splitDirFile (#file fs
)
209 if String.isSuffix
".vhost" (#file spl
)
210 orelse String.isSuffix
".vhost_ssl" (#file spl
) then
211 case findVhostUser (#file fs
) of
212 NONE
=> print ("Can't find user in " ^ #file fs ^
"! Taking no action.\n")
215 val realVhostFile
= OS
.Path
.joinDirFile
216 {dir
= Config
.Apache
.confDir
,
219 val realLogDir
= OS
.Path
.joinDirFile
220 {dir
= Config
.homeBase
,
222 val realLogDir
= OS
.Path
.joinDirFile
225 val realLogDir
= OS
.Path
.joinDirFile
228 val realLogDir
= OS
.Path
.joinDirFile
230 file
= Slave
.hostname ()}
231 val {base
, ...} = OS
.Path
.splitBaseExt (#file spl
)
232 val realLogDir
= OS
.Path
.joinDirFile
236 vhostsChanged
:= true;
242 (ignore (OS
.Process
.system Config
.Apache
.down
);
244 ignore (OS
.Process
.system (Config
.rm
247 ignore (OS
.Process
.system (Config
.rm
251 (ignore (OS
.Process
.system (Config
.cp
256 if Posix
.FileSys
.access (realLogDir
, []) then
259 OS
.FileSys
.mkDir realLogDir
)
262 ignore (OS
.Process
.system (Config
.cp
272 val () = Slave
.registerPostHandler
274 (if !vhostsChanged
then
275 Slave
.shellF ([if !logDeleted
then Config
.Apache
.undown
else Config
.Apache
.reload
],
276 fn cl
=> "Error reloading Apache with " ^ cl
)
280 val vhostFiles
: (string * TextIO.outstream
) list ref
= ref
[]
281 fun write
' s
= app (fn (node
, file
) => TextIO.output (file
, s node
)) (!vhostFiles
)
282 fun write s
= app (fn (_
, file
) => TextIO.output (file
, s
)) (!vhostFiles
)
284 val rewriteEnabled
= ref
false
285 val localRewriteEnabled
= ref
false
286 val currentVhost
= ref
""
287 val currentVhostId
= ref
""
289 val pre
= ref (fn _
: {user
: string, nodes
: string list
, id
: string, hostname
: string} => ())
294 pre
:= (fn x
=> (old x
; f x
))
297 val post
= ref (fn () => ())
302 post
:= (fn () => (old (); f ()))
305 val aliaser
= ref (fn _
: string => ())
306 fun registerAliaser f
=
310 aliaser
:= (fn x
=> (old x
; f x
))
313 val () = Env
.containerV_one
"vhost"
317 val nodes
= Env
.env (Env
.list Env
.string) (env
, "WebNodes")
319 val ssl
= Env
.env Env
.bool (env
, "SSL")
320 val user
= Env
.env Env
.string (env
, "User")
321 val group
= Env
.env Env
.string (env
, "Group")
322 val docroot
= Env
.env Env
.string (env
, "DocumentRoot")
323 val sadmin
= Env
.env Env
.string (env
, "ServerAdmin")
325 val fullHost
= host ^
"." ^ Domain
.currentDomain ()
326 val vhostId
= fullHost ^
(if ssl
then ".ssl" else "")
327 val confFile
= fullHost ^
(if ssl
then ".vhost_ssl" else ".vhost")
329 currentVhost
:= fullHost
;
330 currentVhostId
:= vhostId
;
332 rewriteEnabled
:= false;
333 localRewriteEnabled
:= false;
334 vhostFiles
:= map (fn node
=>
336 val file
= Domain
.domainFile
{node
= node
,
339 TextIO.output (file
, "<VirtualHost ");
340 TextIO.output (file
, Domain
.nodeIp node
);
341 TextIO.output (file
, ":");
342 TextIO.output (file
, if ssl
then
346 TextIO.output (file
, ">\n");
347 TextIO.output (file
, "\tErrorLog ");
348 TextIO.output (file
, Config
.homeBase
);
349 TextIO.output (file
, "/");
350 TextIO.output (file
, user
);
351 TextIO.output (file
, "/apache/log/");
352 TextIO.output (file
, node
);
353 TextIO.output (file
, "/");
354 TextIO.output (file
, vhostId
);
355 TextIO.output (file
, "/error.log\n\tCustomLog ");
356 TextIO.output (file
, Config
.homeBase
);
357 TextIO.output (file
, "/");
358 TextIO.output (file
, user
);
359 TextIO.output (file
, "/apache/log/");
360 TextIO.output (file
, node
);
361 TextIO.output (file
, "/");
362 TextIO.output (file
, vhostId
);
363 TextIO.output (file
, "/access.log combined\n");
364 (Config
.homeBase ^
"/" ^ user ^
"/apache/log/"
365 ^ node ^
"/" ^ vhostId
, file
)
368 write
"\tServerName ";
370 write
"\n\tSuexecUserGroup ";
374 write
"\n\tDocumentRoot ";
376 write
"\n\tServerAdmin ";
379 !pre
{user
= user
, nodes
= nodes
, id
= vhostId
, hostname
= fullHost
}
382 write
"</VirtualHost>\n";
383 app (TextIO.closeOut
o #
2) (!vhostFiles
)))
385 val inLocal
= ref
false
387 val () = Env
.container_one
"location"
388 ("prefix", Env
.string)
390 (write
"\t<Location ";
394 fn () => (write
"\t</Location>\n";
396 localRewriteEnabled
:= false))
398 val () = Env
.container_one
"directory"
399 ("directory", Env
.string)
401 (write
"\t<Directory ";
405 fn () => (write
"\t</Directory>\n";
407 localRewriteEnabled
:= false))
409 fun checkRewrite () =
411 if !rewriteEnabled
orelse !localRewriteEnabled
then
414 (write
"\tRewriteEngine on\n";
415 localRewriteEnabled
:= true)
416 else if !rewriteEnabled
then
419 (write
"\tRewriteEngine on\n";
420 rewriteEnabled
:= true)
422 val () = Env
.action_three
"localProxyRewrite"
423 ("from", Env
.string, "to", Env
.string, "port", Env
.int)
424 (fn (from
, to
, port
) =>
426 write
"\tRewriteRule\t";
428 write
"\thttp://localhost:";
429 write (Int.toString port
);
434 val () = Env
.action_two
"proxyPass"
435 ("from", Env
.string, "to", Env
.string)
437 (write
"\tProxyPass\t";
443 val () = Env
.action_two
"proxyPassReverse"
444 ("from", Env
.string, "to", Env
.string)
446 (write
"\tProxyPassReverse\t";
452 val () = Env
.action_three
"rewriteRule"
453 ("from", Env
.string, "to", Env
.string, "flags", Env
.list flag
)
454 (fn (from
, to
, flags
) =>
456 write
"\tRewriteRule\t";
462 | flag
::rest
=> (write
" [";
464 app (fn flag
=> (write
",";
469 val () = Env
.action_three
"rewriteCond"
470 ("test", Env
.string, "pattern", Env
.string, "flags", Env
.list cond_flag
)
471 (fn (from
, to
, flags
) =>
473 write
"\tRewriteCond\t";
479 | flag
::rest
=> (write
" [";
481 app (fn flag
=> (write
",";
486 val () = Env
.action_one
"rewriteLogLevel"
490 write
"\tRewriteLog ";
492 write
"/rewrite.log\n\tRewriteLogLevel ";
493 write (Int.toString level
);
496 val () = Env
.action_two
"alias"
497 ("from", Env
.string, "to", Env
.string)
505 val () = Env
.action_two
"scriptAlias"
506 ("from", Env
.string, "to", Env
.string)
508 (write
"\tScriptAlias\t";
514 val () = Env
.action_two
"errorDocument"
515 ("code", Env
.string, "handler", Env
.string)
516 (fn (code
, handler
) =>
517 (write
"\tErrorDocument\t";
523 val () = Env
.action_one
"options"
524 ("options", Env
.list apache_option
)
528 | _
=> (write
"\tOptions";
529 app (fn opt
=> (write
" "; write opt
)) opts
;
532 val () = Env
.action_one
"set_options"
533 ("options", Env
.list apache_option
)
537 | _
=> (write
"\tOptions";
538 app (fn opt
=> (write
" +"; write opt
)) opts
;
541 val () = Env
.action_one
"unset_options"
542 ("options", Env
.list apache_option
)
546 | _
=> (write
"\tOptions";
547 app (fn opt
=> (write
" -"; write opt
)) opts
;
550 val () = Env
.action_one
"directoryIndex"
551 ("filenames", Env
.list Env
.string)
553 (write
"\tDirectoryIndex";
554 app (fn opt
=> (write
" "; write opt
)) opts
;
557 val () = Env
.action_one
"serverAlias"
560 (write
"\tServerAlias ";
565 val authType
= fn (EVar
"basic", _
) => SOME
"basic"
566 |
(EVar
"digest", _
) => SOME
"digest"
569 val () = Env
.action_one
"authType"
572 (write
"\tAuthType ";
576 val () = Env
.action_one
"authName"
579 (write
"\tAuthName \"";
583 val () = Env
.action_one
"authUserFile"
586 (write
"\tAuthUserFile ";
590 val () = Env
.action_none
"requireValidUser"
591 (fn () => write
"\tRequire valid-user\n")
593 val () = Env
.action_one
"requireUser"
594 ("users", Env
.list Env
.string)
598 | _
=> (write
"\tRequire user";
599 app (fn name
=> (write
" "; write name
)) names
;
602 val () = Env
.action_one
"requireGroup"
603 ("groups", Env
.list Env
.string)
607 | _
=> (write
"\tRequire group";
608 app (fn name
=> (write
" "; write name
)) names
;
611 val () = Env
.action_none
"orderAllowDeny"
612 (fn () => write
"\tOrder allow,deny\n")
614 val () = Env
.action_none
"orderDenyAllow"
615 (fn () => write
"\tOrder deny,allow\n")
617 val () = Env
.action_none
"allowFromAll"
618 (fn () => write
"\tAllow from all\n")
620 val () = Env
.action_one
"allowFrom"
621 ("entries", Env
.list Env
.string)
625 | _
=> (write
"\tAllow from";
626 app (fn name
=> (write
" "; write name
)) names
;
629 val () = Env
.action_none
"denyFromAll"
630 (fn () => write
"\tDeny from all\n")
632 val () = Env
.action_one
"denyFrom"
633 ("entries", Env
.list Env
.string)
637 | _
=> (write
"\tDeny from";
638 app (fn name
=> (write
" "; write name
)) names
;
641 val () = Env
.action_none
"satisfyAll"
642 (fn () => write
"\tSatisfy all\n")
644 val () = Env
.action_none
"satisfyAny"
645 (fn () => write
"\tSatisfy any\n")
647 val () = Env
.action_one
"forceType"
649 (fn ty
=> (write
"\tForceType ";
653 val () = Env
.action_none
"forceTypeOff"
654 (fn () => write
"\tForceType None\n")
656 val () = Env
.action_two
"action"
657 ("what", Env
.string, "how", Env
.string)
658 (fn (what
, how
) => (write
"\tAction ";
664 val () = Env
.action_one
"addDefaultCharset"
665 ("charset", Env
.string)
666 (fn ty
=> (write
"\tAddDefaultCharset ";
670 (*val () = Env
.action_one
"davSvn"
672 (fn path
=> (write
"\tDAV svn\n\tSVNPath ";
676 val () = Env
.action_one
"authzSvnAccessFile"
678 (fn path
=> (write
"\tAuthzSVNAccessFile ";
682 val () = Env
.action_two
"addDescription"
683 ("description", Env
.string, "patterns", Env
.list Env
.string)
687 | _
=> (write
"\tAddDescription \"";
688 write (String.toString desc
);
690 app (fn pat
=> (write
" "; write pat
)) pats
;
693 val () = Env
.action_one
"indexOptions"
694 ("options", Env
.list autoindex_option
)
698 | _
=> (write
"\tIndexOptions";
699 app (fn (opt
, arg
) =>
702 Option
.app (fn arg
=>
703 (write
"="; write arg
)) arg
)) opts
;
706 val () = Env
.action_one
"set_indexOptions"
707 ("options", Env
.list autoindex_option
)
711 | _
=> (write
"\tIndexOptions";
712 app (fn (opt
, arg
) =>
715 Option
.app (fn arg
=>
716 (write
"="; write arg
)) arg
)) opts
;
719 val () = Env
.action_one
"unset_indexOptions"
720 ("options", Env
.list autoindex_option
)
724 | _
=> (write
"\tIndexOptions";
730 val () = Env
.action_one
"headerName"
732 (fn name
=> (write
"\tHeaderName ";
736 val () = Env
.action_one
"readmeName"
738 (fn name
=> (write
"\tReadmeName ";