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
)
61 val _
= Env
.type_one
"suexec_flag"
63 (fn b
=> b
orelse Domain
.hasPriv
"www")
66 size s
> 0 andalso size s
< 1000 andalso CharVector
.all
67 (fn ch
=> Char.isAlphaNum ch
73 val _
= Env
.type_one
"location"
77 fun validCert s
= Acl
.query
{user
= Domain
.getUser (),
81 val _
= Env
.type_one
"ssl_cert_path"
86 (EVar
"no_ssl", _
) => SOME NONE
87 |
(EApp ((EVar
"use_cert", _
), s
), _
) => Option
.map
SOME (Env
.string s
)
90 val dl
= ErrorMsg
.dummyLoc
92 val _
= Defaults
.registerDefault ("WebNodes",
93 (TList (TBase
"web_node", dl
), dl
),
94 (fn () => (EList (map (fn s
=> (EString s
, dl
)) Config
.Apache
.webNodes_default
), dl
)))
96 val _
= Defaults
.registerDefault ("SSL",
98 (fn () => (EVar
"no_ssl", dl
)))
100 val _
= Defaults
.registerDefault ("User",
101 (TBase
"your_user", dl
),
102 (fn () => (EString (Domain
.getUser ()), dl
)))
104 val _
= Defaults
.registerDefault ("Group",
105 (TBase
"your_group", dl
),
106 (fn () => (EString (Domain
.getUser ()), dl
)))
108 val _
= Defaults
.registerDefault ("DocumentRoot",
109 (TBase
"your_path", dl
),
110 (fn () => (EString (Domain
.homedir () ^
"/" ^ Config
.Apache
.public_html
), dl
)))
112 val _
= Defaults
.registerDefault ("ServerAdmin",
114 (fn () => (EString (Domain
.getUser () ^
"@" ^ Config
.defaultDomain
), dl
)))
116 val _
= Defaults
.registerDefault ("SuExec",
117 (TBase
"suexec_flag", dl
),
118 (fn () => (EVar
"true", dl
)))
120 val redirect_code
= fn (EVar
"temp", _
) => SOME
"temp"
121 |
(EVar
"permanent", _
) => SOME
"permanent"
122 |
(EVar
"seeother", _
) => SOME
"seeother"
123 |
(EVar
"redir300", _
) => SOME
"300"
124 |
(EVar
"redir301", _
) => SOME
"301"
125 |
(EVar
"redir302", _
) => SOME
"302"
126 |
(EVar
"redir303", _
) => SOME
"303"
127 |
(EVar
"redir304", _
) => SOME
"304"
128 |
(EVar
"redir305", _
) => SOME
"305"
129 |
(EVar
"redir307", _
) => SOME
"307"
132 val flag
= fn (EVar
"redirect", _
) => SOME
"R"
133 |
(EVar
"forbidden", _
) => SOME
"F"
134 |
(EVar
"gone", _
) => SOME
"G"
135 |
(EVar
"last", _
) => SOME
"L"
136 |
(EVar
"chain", _
) => SOME
"C"
137 |
(EVar
"nosubreq", _
) => SOME
"NS"
138 |
(EVar
"nocase", _
) => SOME
"NC"
139 |
(EVar
"qsappend", _
) => SOME
"QSA"
140 |
(EVar
"noescape", _
) => SOME
"NE"
141 |
(EVar
"passthrough", _
) => SOME
"PT"
142 |
(EApp ((EVar
"mimeType", _
), e
), _
) =>
143 Option
.map (fn s
=> "T=" ^ s
) (Env
.string e
)
144 |
(EApp ((EVar
"redirectWith", _
), e
), _
) =>
145 Option
.map (fn s
=> "R=" ^ s
) (redirect_code e
)
146 |
(EApp ((EVar
"skip", _
), e
), _
) =>
147 Option
.map (fn n
=> "S=" ^
Int.toString n
) (Env
.int e
)
148 |
(EApp ((EApp ((EVar
"env", _
), e1
), _
), e2
), _
) =>
149 (case Env
.string e1
of
151 | SOME s1
=> Option
.map (fn s2
=> "E=" ^ s1 ^
":" ^ s2
)
156 val cond_flag
= fn (EVar
"cond_nocase", _
) => SOME
"NC"
157 |
(EVar
"ornext", _
) => SOME
"OR"
160 val apache_option
= fn (EVar
"execCGI", _
) => SOME
"ExecCGI"
161 |
(EVar
"includesNOEXEC", _
) => SOME
"IncludesNOEXEC"
162 |
(EVar
"indexes", _
) => SOME
"Indexes"
165 val autoindex_width
= fn (EVar
"autofit", _
) => SOME
"*"
166 |
(EApp ((EVar
"characters", _
), n
), _
) =>
167 Option
.map
Int.toString (Env
.int n
)
170 val autoindex_option
= fn (EApp ((EVar
"descriptionWidth", _
), w
), _
) =>
171 Option
.map (fn w
=> ("DescriptionWidth", SOME w
))
173 |
(EVar
"fancyIndexing", _
) => SOME ("FancyIndexing", NONE
)
174 |
(EVar
"foldersFirst", _
) => SOME ("FoldersFirst", NONE
)
175 |
(EVar
"htmlTable", _
) => SOME ("HTMLTable", NONE
)
176 |
(EVar
"iconsAreLinks", _
) => SOME ("IconsAreLinks", NONE
)
177 |
(EApp ((EVar
"iconHeight", _
), n
), _
) =>
178 Option
.map (fn w
=> ("IconHeight", SOME (Int.toString w
)))
180 |
(EApp ((EVar
"iconWidth", _
), n
), _
) =>
181 Option
.map (fn w
=> ("IconWidth", SOME (Int.toString w
)))
183 |
(EVar
"ignoreCase", _
) => SOME ("IgnoreCase", NONE
)
184 |
(EVar
"ignoreClient", _
) => SOME ("IgnoreClient", NONE
)
185 |
(EApp ((EVar
"nameWidth", _
), w
), _
) =>
186 Option
.map (fn w
=> ("NameWidth", SOME w
))
188 |
(EVar
"scanHtmlTitles", _
) => SOME ("ScanHTMLTitles", NONE
)
189 |
(EVar
"suppressColumnSorting", _
) => SOME ("SuppressColumnSorting", NONE
)
190 |
(EVar
"suppressDescription", _
) => SOME ("SuppressDescription", NONE
)
191 |
(EVar
"suppressHtmlPreamble", _
) => SOME ("SuppressHTMLPreamble", NONE
)
192 |
(EVar
"suppressIcon", _
) => SOME ("SuppressIcon", NONE
)
193 |
(EVar
"suppressLastModified", _
) => SOME ("SuppressLastModified", NONE
)
194 |
(EVar
"suppressRules", _
) => SOME ("SuppressRules", NONE
)
195 |
(EVar
"suppressSize", _
) => SOME ("SuppressSize", NONE
)
196 |
(EVar
"trackModified", _
) => SOME ("TrackModified", NONE
)
197 |
(EVar
"versionSort", _
) => SOME ("VersionSort", NONE
)
198 |
(EVar
"xhtml", _
) => SOME ("XHTML", NONE
)
202 val vhostsChanged
= ref
false
203 val logDeleted
= ref
false
205 val () = Slave
.registerPreHandler
206 (fn () => (vhostsChanged
:= false;
207 logDeleted
:= false))
209 fun findVhostUser fname
=
211 val inf
= TextIO.openIn fname
214 case TextIO.inputLine inf
of
217 if String.isPrefix
"# Owner: " line
then
218 case String.tokens
Char.isSpace line
of
219 [_
, _
, user
] => SOME user
225 before TextIO.closeIn inf
228 val () = Slave
.registerFileHandler (fn fs
=>
230 val spl
= OS
.Path
.splitDirFile (#file fs
)
232 if String.isSuffix
".vhost" (#file spl
)
233 orelse String.isSuffix
".vhost_ssl" (#file spl
) then let
234 val realVhostFile
= OS
.Path
.joinDirFile
235 {dir
= Config
.Apache
.confDir
,
238 val user
= findVhostUser (#file fs
)
239 val oldUser
= findVhostUser realVhostFile
241 if (oldUser
= NONE
andalso #action fs
<> Slave
.Add
)
242 orelse (user
= NONE
andalso #action fs
<> Slave
.Delete
) then
243 print ("Can't find user in " ^ #file fs ^
" or " ^ realVhostFile ^
"! Taking no action.\n")
246 fun realLogDir user
=
248 val realLogDir
= Domain
.homedirOf (valOf user
)
249 val realLogDir
= OS
.Path
.joinDirFile
252 val realLogDir
= OS
.Path
.joinDirFile
255 val realLogDir
= OS
.Path
.joinDirFile
257 file
= Slave
.hostname ()}
258 val {base
, ...} = OS
.Path
.splitBaseExt (#file spl
)
260 val realLogDir
= OS
.Path
.joinDirFile
264 if String.isSuffix
".vhost_ssl" (#file spl
) then
270 vhostsChanged
:= true;
276 (ignore (OS
.Process
.system Config
.Apache
.down
);
278 ignore (OS
.Process
.system (Config
.rm
281 ignore (OS
.Process
.system (Config
.rm
283 ^ realLogDir oldUser
)))
286 val rld
= realLogDir user
288 ignore (OS
.Process
.system (Config
.cp
293 if Posix
.FileSys
.access (rld
, []) then
300 (ignore (OS
.Process
.system (Config
.cp
305 if user
<> oldUser
then
307 val old
= realLogDir oldUser
308 val rld
= realLogDir user
313 (ignore (OS
.Process
.system Config
.Apache
.down
);
315 ignore (OS
.Process
.system (Config
.rm
317 ^ realLogDir oldUser
));
318 if Posix
.FileSys
.access (rld
, []) then
331 val () = Slave
.registerPostHandler
333 (if !vhostsChanged
then
334 Slave
.shellF ([if !logDeleted
then Config
.Apache
.undown
else Config
.Apache
.reload
],
335 fn cl
=> "Error reloading Apache with " ^ cl
)
339 val vhostFiles
: (string * TextIO.outstream
) list ref
= ref
[]
340 fun write
' s
= app (fn (node
, file
) => TextIO.output (file
, s node
)) (!vhostFiles
)
341 fun write s
= app (fn (_
, file
) => TextIO.output (file
, s
)) (!vhostFiles
)
343 val rewriteEnabled
= ref
false
344 val localRewriteEnabled
= ref
false
345 val currentVhost
= ref
""
346 val currentVhostId
= ref
""
348 val pre
= ref (fn _
: {user
: string, nodes
: string list
, id
: string, hostname
: string} => ())
353 pre
:= (fn x
=> (old x
; f x
))
356 val post
= ref (fn () => ())
361 post
:= (fn () => (old (); f ()))
364 val aliaser
= ref (fn _
: string => ())
365 fun registerAliaser f
=
369 aliaser
:= (fn x
=> (old x
; f x
))
372 val () = Env
.containerV_one
"vhost"
376 val nodes
= Env
.env (Env
.list Env
.string) (env
, "WebNodes")
378 val ssl
= Env
.env
ssl (env
, "SSL")
379 val user
= Env
.env Env
.string (env
, "User")
380 val group
= Env
.env Env
.string (env
, "Group")
381 val docroot
= Env
.env Env
.string (env
, "DocumentRoot")
382 val sadmin
= Env
.env Env
.string (env
, "ServerAdmin")
383 val suexec
= Env
.env Env
.bool (env
, "SuExec")
385 val fullHost
= host ^
"." ^ Domain
.currentDomain ()
386 val vhostId
= fullHost ^
(if Option
.isSome ssl
then ".ssl" else "")
387 val confFile
= fullHost ^
(if Option
.isSome ssl
then ".vhost_ssl" else ".vhost")
389 currentVhost
:= fullHost
;
390 currentVhostId
:= vhostId
;
392 rewriteEnabled
:= false;
393 localRewriteEnabled
:= false;
394 vhostFiles
:= map (fn node
=>
396 val file
= Domain
.domainFile
{node
= node
,
399 TextIO.output (file
, "# Owner: ");
400 TextIO.output (file
, user
);
401 TextIO.output (file
, "\n<VirtualHost ");
402 TextIO.output (file
, Domain
.nodeIp node
);
403 TextIO.output (file
, ":");
404 TextIO.output (file
, case ssl
of
407 TextIO.output (file
, ">\n");
408 TextIO.output (file
, "\tErrorLog ");
409 TextIO.output (file
, Domain
.homedirOf user
);
410 TextIO.output (file
, "/apache/log/");
411 TextIO.output (file
, node
);
412 TextIO.output (file
, "/");
413 TextIO.output (file
, vhostId
);
414 TextIO.output (file
, "/error.log\n\tCustomLog ");
415 TextIO.output (file
, Domain
.homedirOf user
);
416 TextIO.output (file
, "/apache/log/");
417 TextIO.output (file
, node
);
418 TextIO.output (file
, "/");
419 TextIO.output (file
, vhostId
);
420 TextIO.output (file
, "/access.log combined\n");
421 (Domain
.homedirOf user ^
"/apache/log/"
422 ^ node ^
"/" ^ vhostId
, file
)
425 write
"\tServerName ";
428 (write
"\n\tSuexecUserGroup ";
434 write
"\n\tDocumentRoot ";
436 write
"\n\tServerAdmin ";
440 (write
"\n\tSSLEngine on\n\tSSLCertificateFile ";
444 !pre
{user
= user
, nodes
= nodes
, id
= vhostId
, hostname
= fullHost
}
447 write
"</VirtualHost>\n";
448 app (TextIO.closeOut
o #
2) (!vhostFiles
)))
450 val inLocal
= ref
false
452 val () = Env
.container_one
"location"
453 ("prefix", Env
.string)
455 (write
"\t<Location ";
459 fn () => (write
"\t</Location>\n";
461 localRewriteEnabled
:= false))
463 val () = Env
.container_one
"directory"
464 ("directory", Env
.string)
466 (write
"\t<Directory ";
470 fn () => (write
"\t</Directory>\n";
472 localRewriteEnabled
:= false))
474 fun checkRewrite () =
476 if !rewriteEnabled
orelse !localRewriteEnabled
then
479 (write
"\tRewriteEngine on\n";
480 localRewriteEnabled
:= true)
481 else if !rewriteEnabled
then
484 (write
"\tRewriteEngine on\n";
485 rewriteEnabled
:= true)
487 val () = Env
.action_three
"localProxyRewrite"
488 ("from", Env
.string, "to", Env
.string, "port", Env
.int)
489 (fn (from
, to
, port
) =>
491 write
"\tRewriteRule\t";
493 write
"\thttp://localhost:";
494 write (Int.toString port
);
499 val () = Env
.action_two
"proxyPass"
500 ("from", Env
.string, "to", Env
.string)
502 (write
"\tProxyPass\t";
508 val () = Env
.action_two
"proxyPassReverse"
509 ("from", Env
.string, "to", Env
.string)
511 (write
"\tProxyPassReverse\t";
517 val () = Env
.action_three
"rewriteRule"
518 ("from", Env
.string, "to", Env
.string, "flags", Env
.list flag
)
519 (fn (from
, to
, flags
) =>
521 write
"\tRewriteRule\t";
527 | flag
::rest
=> (write
" [";
529 app (fn flag
=> (write
",";
534 val () = Env
.action_three
"rewriteCond"
535 ("test", Env
.string, "pattern", Env
.string, "flags", Env
.list cond_flag
)
536 (fn (from
, to
, flags
) =>
538 write
"\tRewriteCond\t";
544 | flag
::rest
=> (write
" [";
546 app (fn flag
=> (write
",";
551 val () = Env
.action_one
"rewriteLogLevel"
555 write
"\tRewriteLog ";
557 write
"/rewrite.log\n\tRewriteLogLevel ";
558 write (Int.toString level
);
561 val () = Env
.action_two
"alias"
562 ("from", Env
.string, "to", Env
.string)
570 val () = Env
.action_two
"scriptAlias"
571 ("from", Env
.string, "to", Env
.string)
573 (write
"\tScriptAlias\t";
579 val () = Env
.action_two
"errorDocument"
580 ("code", Env
.string, "handler", Env
.string)
581 (fn (code
, handler
) =>
582 (write
"\tErrorDocument\t";
588 val () = Env
.action_one
"options"
589 ("options", Env
.list apache_option
)
593 | _
=> (write
"\tOptions";
594 app (fn opt
=> (write
" "; write opt
)) opts
;
597 val () = Env
.action_one
"set_options"
598 ("options", Env
.list apache_option
)
602 | _
=> (write
"\tOptions";
603 app (fn opt
=> (write
" +"; write opt
)) opts
;
606 val () = Env
.action_one
"unset_options"
607 ("options", Env
.list apache_option
)
611 | _
=> (write
"\tOptions";
612 app (fn opt
=> (write
" -"; write opt
)) opts
;
615 val () = Env
.action_one
"directoryIndex"
616 ("filenames", Env
.list Env
.string)
618 (write
"\tDirectoryIndex";
619 app (fn opt
=> (write
" "; write opt
)) opts
;
622 val () = Env
.action_one
"serverAlias"
625 (write
"\tServerAlias ";
630 val authType
= fn (EVar
"basic", _
) => SOME
"basic"
631 |
(EVar
"digest", _
) => SOME
"digest"
634 val () = Env
.action_one
"authType"
637 (write
"\tAuthType ";
641 val () = Env
.action_one
"authName"
644 (write
"\tAuthName \"";
648 val () = Env
.action_one
"authUserFile"
651 (write
"\tAuthUserFile ";
655 val () = Env
.action_none
"requireValidUser"
656 (fn () => write
"\tRequire valid-user\n")
658 val () = Env
.action_one
"requireUser"
659 ("users", Env
.list Env
.string)
663 | _
=> (write
"\tRequire user";
664 app (fn name
=> (write
" "; write name
)) names
;
667 val () = Env
.action_one
"requireGroup"
668 ("groups", Env
.list Env
.string)
672 | _
=> (write
"\tRequire group";
673 app (fn name
=> (write
" "; write name
)) names
;
676 val () = Env
.action_none
"orderAllowDeny"
677 (fn () => write
"\tOrder allow,deny\n")
679 val () = Env
.action_none
"orderDenyAllow"
680 (fn () => write
"\tOrder deny,allow\n")
682 val () = Env
.action_none
"allowFromAll"
683 (fn () => write
"\tAllow from all\n")
685 val () = Env
.action_one
"allowFrom"
686 ("entries", Env
.list Env
.string)
690 | _
=> (write
"\tAllow from";
691 app (fn name
=> (write
" "; write name
)) names
;
694 val () = Env
.action_none
"denyFromAll"
695 (fn () => write
"\tDeny from all\n")
697 val () = Env
.action_one
"denyFrom"
698 ("entries", Env
.list Env
.string)
702 | _
=> (write
"\tDeny from";
703 app (fn name
=> (write
" "; write name
)) names
;
706 val () = Env
.action_none
"satisfyAll"
707 (fn () => write
"\tSatisfy all\n")
709 val () = Env
.action_none
"satisfyAny"
710 (fn () => write
"\tSatisfy any\n")
712 val () = Env
.action_one
"forceType"
714 (fn ty
=> (write
"\tForceType ";
718 val () = Env
.action_none
"forceTypeOff"
719 (fn () => write
"\tForceType None\n")
721 val () = Env
.action_two
"action"
722 ("what", Env
.string, "how", Env
.string)
723 (fn (what
, how
) => (write
"\tAction ";
729 val () = Env
.action_one
"addDefaultCharset"
730 ("charset", Env
.string)
731 (fn ty
=> (write
"\tAddDefaultCharset ";
735 (*val () = Env
.action_one
"davSvn"
737 (fn path
=> (write
"\tDAV svn\n\tSVNPath ";
741 val () = Env
.action_one
"authzSvnAccessFile"
743 (fn path
=> (write
"\tAuthzSVNAccessFile ";
747 val () = Env
.action_two
"addDescription"
748 ("description", Env
.string, "patterns", Env
.list Env
.string)
752 | _
=> (write
"\tAddDescription \"";
753 write (String.toString desc
);
755 app (fn pat
=> (write
" "; write pat
)) pats
;
758 val () = Env
.action_one
"indexOptions"
759 ("options", Env
.list autoindex_option
)
763 | _
=> (write
"\tIndexOptions";
764 app (fn (opt
, arg
) =>
767 Option
.app (fn arg
=>
768 (write
"="; write arg
)) arg
)) opts
;
771 val () = Env
.action_one
"set_indexOptions"
772 ("options", Env
.list autoindex_option
)
776 | _
=> (write
"\tIndexOptions";
777 app (fn (opt
, arg
) =>
780 Option
.app (fn arg
=>
781 (write
"="; write arg
)) arg
)) opts
;
784 val () = Env
.action_one
"unset_indexOptions"
785 ("options", Env
.list autoindex_option
)
789 | _
=> (write
"\tIndexOptions";
795 val () = Env
.action_one
"headerName"
797 (fn name
=> (write
"\tHeaderName ";
801 val () = Env
.action_one
"readmeName"
803 (fn name
=> (write
"\tReadmeName ";
807 val () = Domain
.registerResetLocal (fn () =>
808 ignore (OS
.Process
.system (Config
.rm ^
" -rf /var/domtool/vhosts/*")))