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 val dl
= ErrorMsg
.dummyLoc
79 val _
= Defaults
.registerDefault ("WebNodes",
80 (TList (TBase
"web_node", dl
), dl
),
81 (fn () => (EList (map (fn s
=> (EString s
, dl
)) Config
.Apache
.webNodes_default
), dl
)))
83 val _
= Defaults
.registerDefault ("SSL",
85 (fn () => (EVar
"false", dl
)))
87 val _
= Defaults
.registerDefault ("User",
88 (TBase
"your_user", dl
),
89 (fn () => (EString (Domain
.getUser ()), dl
)))
91 val _
= Defaults
.registerDefault ("Group",
92 (TBase
"your_group", dl
),
93 (fn () => (EString (Domain
.getUser ()), dl
)))
95 val _
= Defaults
.registerDefault ("DocumentRoot",
96 (TBase
"your_path", dl
),
97 (fn () => (EString (Config
.homeBase ^
"/" ^ Domain
.getUser () ^
"/" ^ Config
.Apache
.public_html
), dl
)))
99 val _
= Defaults
.registerDefault ("ServerAdmin",
101 (fn () => (EString (Domain
.getUser () ^
"@" ^ Config
.defaultDomain
), dl
)))
103 val _
= Defaults
.registerDefault ("SuExec",
104 (TBase
"suexec_flag", dl
),
105 (fn () => (EApp ((EVar
"suexec_flag", dl
),
106 (EVar
"true", dl
)), dl
)))
108 val redirect_code
= fn (EVar
"temp", _
) => SOME
"temp"
109 |
(EVar
"permanent", _
) => SOME
"permanent"
110 |
(EVar
"seeother", _
) => SOME
"seeother"
111 |
(EVar
"redir300", _
) => SOME
"300"
112 |
(EVar
"redir301", _
) => SOME
"301"
113 |
(EVar
"redir302", _
) => SOME
"302"
114 |
(EVar
"redir303", _
) => SOME
"303"
115 |
(EVar
"redir304", _
) => SOME
"304"
116 |
(EVar
"redir305", _
) => SOME
"305"
117 |
(EVar
"redir307", _
) => SOME
"307"
120 val flag
= fn (EVar
"redirect", _
) => SOME
"R"
121 |
(EVar
"forbidden", _
) => SOME
"F"
122 |
(EVar
"gone", _
) => SOME
"G"
123 |
(EVar
"last", _
) => SOME
"L"
124 |
(EVar
"chain", _
) => SOME
"C"
125 |
(EVar
"nosubreq", _
) => SOME
"NS"
126 |
(EVar
"nocase", _
) => SOME
"NC"
127 |
(EVar
"qsappend", _
) => SOME
"QSA"
128 |
(EVar
"noescape", _
) => SOME
"NE"
129 |
(EVar
"passthrough", _
) => SOME
"PT"
130 |
(EApp ((EVar
"mimeType", _
), e
), _
) =>
131 Option
.map (fn s
=> "T=" ^ s
) (Env
.string e
)
132 |
(EApp ((EVar
"redirectWith", _
), e
), _
) =>
133 Option
.map (fn s
=> "R=" ^ s
) (redirect_code e
)
134 |
(EApp ((EVar
"skip", _
), e
), _
) =>
135 Option
.map (fn n
=> "S=" ^
Int.toString n
) (Env
.int e
)
136 |
(EApp ((EApp ((EVar
"env", _
), e1
), _
), e2
), _
) =>
137 (case Env
.string e1
of
139 | SOME s1
=> Option
.map (fn s2
=> "E=" ^ s1 ^
":" ^ s2
)
144 val cond_flag
= fn (EVar
"cond_nocase", _
) => SOME
"NC"
145 |
(EVar
"ornext", _
) => SOME
"OR"
148 val apache_option
= fn (EVar
"execCGI", _
) => SOME
"ExecCGI"
149 |
(EVar
"includesNOEXEC", _
) => SOME
"IncludesNOEXEC"
150 |
(EVar
"indexes", _
) => SOME
"Indexes"
153 val autoindex_width
= fn (EVar
"autofit", _
) => SOME
"*"
154 |
(EApp ((EVar
"characters", _
), n
), _
) =>
155 Option
.map
Int.toString (Env
.int n
)
158 val autoindex_option
= fn (EApp ((EVar
"descriptionWidth", _
), w
), _
) =>
159 Option
.map (fn w
=> ("DescriptionWidth", SOME w
))
161 |
(EVar
"fancyIndexing", _
) => SOME ("FancyIndexing", NONE
)
162 |
(EVar
"foldersFirst", _
) => SOME ("FoldersFirst", NONE
)
163 |
(EVar
"htmlTable", _
) => SOME ("HTMLTable", NONE
)
164 |
(EVar
"iconsAreLinks", _
) => SOME ("IconsAreLinks", NONE
)
165 |
(EApp ((EVar
"iconHeight", _
), n
), _
) =>
166 Option
.map (fn w
=> ("IconHeight", SOME (Int.toString w
)))
168 |
(EApp ((EVar
"iconWidth", _
), n
), _
) =>
169 Option
.map (fn w
=> ("IconWidth", SOME (Int.toString w
)))
171 |
(EVar
"ignoreCase", _
) => SOME ("IgnoreCase", NONE
)
172 |
(EVar
"ignoreClient", _
) => SOME ("IgnoreClient", NONE
)
173 |
(EApp ((EVar
"nameWidth", _
), w
), _
) =>
174 Option
.map (fn w
=> ("NameWidth", SOME w
))
176 |
(EVar
"scanHtmlTitles", _
) => SOME ("ScanHTMLTitles", NONE
)
177 |
(EVar
"suppressColumnSorting", _
) => SOME ("SuppressColumnSorting", NONE
)
178 |
(EVar
"suppressDescription", _
) => SOME ("SuppressDescription", NONE
)
179 |
(EVar
"suppressHtmlPreamble", _
) => SOME ("SuppressHTMLPreamble", NONE
)
180 |
(EVar
"suppressIcon", _
) => SOME ("SuppressIcon", NONE
)
181 |
(EVar
"suppressLastModified", _
) => SOME ("SuppressLastModified", NONE
)
182 |
(EVar
"suppressRules", _
) => SOME ("SuppressRules", NONE
)
183 |
(EVar
"suppressSize", _
) => SOME ("SuppressSize", NONE
)
184 |
(EVar
"trackModified", _
) => SOME ("TrackModified", NONE
)
185 |
(EVar
"versionSort", _
) => SOME ("VersionSort", NONE
)
186 |
(EVar
"xhtml", _
) => SOME ("XHTML", NONE
)
190 val vhostsChanged
= ref
false
191 val logDeleted
= ref
false
193 val () = Slave
.registerPreHandler
194 (fn () => (vhostsChanged
:= false;
195 logDeleted
:= false))
197 fun findVhostUser fname
=
199 val inf
= TextIO.openIn fname
202 case TextIO.inputLine inf
of
205 if String.isPrefix
"# Owner: " line
then
206 case String.tokens
Char.isSpace line
of
207 [_
, _
, user
] => SOME user
213 before TextIO.closeIn inf
216 val () = Slave
.registerFileHandler (fn fs
=>
218 val spl
= OS
.Path
.splitDirFile (#file fs
)
220 if String.isSuffix
".vhost" (#file spl
)
221 orelse String.isSuffix
".vhost_ssl" (#file spl
) then let
222 val realVhostFile
= OS
.Path
.joinDirFile
223 {dir
= Config
.Apache
.confDir
,
226 val user
= findVhostUser (#file fs
)
227 val oldUser
= findVhostUser realVhostFile
229 if (oldUser
= NONE
andalso #action fs
<> Slave
.Add
)
230 orelse (user
= NONE
andalso #action fs
<> Slave
.Delete
) then
231 print ("Can't find user in " ^ #file fs ^
" or " ^ realVhostFile ^
"! Taking no action.\n")
234 fun realLogDir user
=
236 val realLogDir
= OS
.Path
.joinDirFile
237 {dir
= Config
.homeBase
,
239 val realLogDir
= OS
.Path
.joinDirFile
242 val realLogDir
= OS
.Path
.joinDirFile
245 val realLogDir
= OS
.Path
.joinDirFile
247 file
= Slave
.hostname ()}
248 val {base
, ...} = OS
.Path
.splitBaseExt (#file spl
)
255 vhostsChanged
:= true;
261 (ignore (OS
.Process
.system Config
.Apache
.down
);
263 ignore (OS
.Process
.system (Config
.rm
266 ignore (OS
.Process
.system (Config
.rm
268 ^ realLogDir oldUser
)))
271 val rld
= realLogDir user
273 ignore (OS
.Process
.system (Config
.cp
278 if Posix
.FileSys
.access (rld
, []) then
285 (ignore (OS
.Process
.system (Config
.cp
290 if user
<> oldUser
then
292 val old
= realLogDir oldUser
293 val rld
= realLogDir user
298 (ignore (OS
.Process
.system Config
.Apache
.down
);
300 ignore (OS
.Process
.system (Config
.rm
302 ^ realLogDir oldUser
));
303 if Posix
.FileSys
.access (rld
, []) then
316 val () = Slave
.registerPostHandler
318 (if !vhostsChanged
then
319 Slave
.shellF ([if !logDeleted
then Config
.Apache
.undown
else Config
.Apache
.reload
],
320 fn cl
=> "Error reloading Apache with " ^ cl
)
324 val vhostFiles
: (string * TextIO.outstream
) list ref
= ref
[]
325 fun write
' s
= app (fn (node
, file
) => TextIO.output (file
, s node
)) (!vhostFiles
)
326 fun write s
= app (fn (_
, file
) => TextIO.output (file
, s
)) (!vhostFiles
)
328 val rewriteEnabled
= ref
false
329 val localRewriteEnabled
= ref
false
330 val currentVhost
= ref
""
331 val currentVhostId
= ref
""
333 val pre
= ref (fn _
: {user
: string, nodes
: string list
, id
: string, hostname
: string} => ())
338 pre
:= (fn x
=> (old x
; f x
))
341 val post
= ref (fn () => ())
346 post
:= (fn () => (old (); f ()))
349 val aliaser
= ref (fn _
: string => ())
350 fun registerAliaser f
=
354 aliaser
:= (fn x
=> (old x
; f x
))
357 fun suexec_flag (EApp ((EVar
"suexec_flag", _
), e
), _
) = Env
.bool e
358 | suexec_flag _
= NONE
360 val () = Env
.containerV_one
"vhost"
364 val nodes
= Env
.env (Env
.list Env
.string) (env
, "WebNodes")
366 val ssl
= Env
.env Env
.bool (env
, "SSL")
367 val user
= Env
.env Env
.string (env
, "User")
368 val group
= Env
.env Env
.string (env
, "Group")
369 val docroot
= Env
.env Env
.string (env
, "DocumentRoot")
370 val sadmin
= Env
.env Env
.string (env
, "ServerAdmin")
371 val suexec
= Env
.env
suexec_flag (env
, "SuExec")
373 val fullHost
= host ^
"." ^ Domain
.currentDomain ()
374 val vhostId
= fullHost ^
(if ssl
then ".ssl" else "")
375 val confFile
= fullHost ^
(if ssl
then ".vhost_ssl" else ".vhost")
377 currentVhost
:= fullHost
;
378 currentVhostId
:= vhostId
;
380 rewriteEnabled
:= false;
381 localRewriteEnabled
:= false;
382 vhostFiles
:= map (fn node
=>
384 val file
= Domain
.domainFile
{node
= node
,
387 TextIO.output (file
, "# Owner: ");
388 TextIO.output (file
, user
);
389 TextIO.output (file
, "\n<VirtualHost ");
390 TextIO.output (file
, Domain
.nodeIp node
);
391 TextIO.output (file
, ":");
392 TextIO.output (file
, if ssl
then
396 TextIO.output (file
, ">\n");
397 TextIO.output (file
, "\tErrorLog ");
398 TextIO.output (file
, Config
.homeBase
);
399 TextIO.output (file
, "/");
400 TextIO.output (file
, user
);
401 TextIO.output (file
, "/apache/log/");
402 TextIO.output (file
, node
);
403 TextIO.output (file
, "/");
404 TextIO.output (file
, vhostId
);
405 TextIO.output (file
, "/error.log\n\tCustomLog ");
406 TextIO.output (file
, Config
.homeBase
);
407 TextIO.output (file
, "/");
408 TextIO.output (file
, user
);
409 TextIO.output (file
, "/apache/log/");
410 TextIO.output (file
, node
);
411 TextIO.output (file
, "/");
412 TextIO.output (file
, vhostId
);
413 TextIO.output (file
, "/access.log combined\n");
414 (Config
.homeBase ^
"/" ^ user ^
"/apache/log/"
415 ^ node ^
"/" ^ vhostId
, file
)
418 write
"\tServerName ";
421 (write
"\n\tSuexecUserGroup ";
427 write
"\n\tDocumentRoot ";
429 write
"\n\tServerAdmin ";
432 (write
"\n\tSSLEngine on")
436 !pre
{user
= user
, nodes
= nodes
, id
= vhostId
, hostname
= fullHost
}
439 write
"</VirtualHost>\n";
440 app (TextIO.closeOut
o #
2) (!vhostFiles
)))
442 val inLocal
= ref
false
444 val () = Env
.container_one
"location"
445 ("prefix", Env
.string)
447 (write
"\t<Location ";
451 fn () => (write
"\t</Location>\n";
453 localRewriteEnabled
:= false))
455 val () = Env
.container_one
"directory"
456 ("directory", Env
.string)
458 (write
"\t<Directory ";
462 fn () => (write
"\t</Directory>\n";
464 localRewriteEnabled
:= false))
466 fun checkRewrite () =
468 if !rewriteEnabled
orelse !localRewriteEnabled
then
471 (write
"\tRewriteEngine on\n";
472 localRewriteEnabled
:= true)
473 else if !rewriteEnabled
then
476 (write
"\tRewriteEngine on\n";
477 rewriteEnabled
:= true)
479 val () = Env
.action_three
"localProxyRewrite"
480 ("from", Env
.string, "to", Env
.string, "port", Env
.int)
481 (fn (from
, to
, port
) =>
483 write
"\tRewriteRule\t";
485 write
"\thttp://localhost:";
486 write (Int.toString port
);
491 val () = Env
.action_two
"proxyPass"
492 ("from", Env
.string, "to", Env
.string)
494 (write
"\tProxyPass\t";
500 val () = Env
.action_two
"proxyPassReverse"
501 ("from", Env
.string, "to", Env
.string)
503 (write
"\tProxyPassReverse\t";
509 val () = Env
.action_three
"rewriteRule"
510 ("from", Env
.string, "to", Env
.string, "flags", Env
.list flag
)
511 (fn (from
, to
, flags
) =>
513 write
"\tRewriteRule\t";
519 | flag
::rest
=> (write
" [";
521 app (fn flag
=> (write
",";
526 val () = Env
.action_three
"rewriteCond"
527 ("test", Env
.string, "pattern", Env
.string, "flags", Env
.list cond_flag
)
528 (fn (from
, to
, flags
) =>
530 write
"\tRewriteCond\t";
536 | flag
::rest
=> (write
" [";
538 app (fn flag
=> (write
",";
543 val () = Env
.action_one
"rewriteLogLevel"
547 write
"\tRewriteLog ";
549 write
"/rewrite.log\n\tRewriteLogLevel ";
550 write (Int.toString level
);
553 val () = Env
.action_two
"alias"
554 ("from", Env
.string, "to", Env
.string)
562 val () = Env
.action_two
"scriptAlias"
563 ("from", Env
.string, "to", Env
.string)
565 (write
"\tScriptAlias\t";
571 val () = Env
.action_two
"errorDocument"
572 ("code", Env
.string, "handler", Env
.string)
573 (fn (code
, handler
) =>
574 (write
"\tErrorDocument\t";
580 val () = Env
.action_one
"options"
581 ("options", Env
.list apache_option
)
585 | _
=> (write
"\tOptions";
586 app (fn opt
=> (write
" "; write opt
)) opts
;
589 val () = Env
.action_one
"set_options"
590 ("options", Env
.list apache_option
)
594 | _
=> (write
"\tOptions";
595 app (fn opt
=> (write
" +"; write opt
)) opts
;
598 val () = Env
.action_one
"unset_options"
599 ("options", Env
.list apache_option
)
603 | _
=> (write
"\tOptions";
604 app (fn opt
=> (write
" -"; write opt
)) opts
;
607 val () = Env
.action_one
"directoryIndex"
608 ("filenames", Env
.list Env
.string)
610 (write
"\tDirectoryIndex";
611 app (fn opt
=> (write
" "; write opt
)) opts
;
614 val () = Env
.action_one
"serverAlias"
617 (write
"\tServerAlias ";
622 val authType
= fn (EVar
"basic", _
) => SOME
"basic"
623 |
(EVar
"digest", _
) => SOME
"digest"
626 val () = Env
.action_one
"authType"
629 (write
"\tAuthType ";
633 val () = Env
.action_one
"authName"
636 (write
"\tAuthName \"";
640 val () = Env
.action_one
"authUserFile"
643 (write
"\tAuthUserFile ";
647 val () = Env
.action_none
"requireValidUser"
648 (fn () => write
"\tRequire valid-user\n")
650 val () = Env
.action_one
"requireUser"
651 ("users", Env
.list Env
.string)
655 | _
=> (write
"\tRequire user";
656 app (fn name
=> (write
" "; write name
)) names
;
659 val () = Env
.action_one
"requireGroup"
660 ("groups", Env
.list Env
.string)
664 | _
=> (write
"\tRequire group";
665 app (fn name
=> (write
" "; write name
)) names
;
668 val () = Env
.action_none
"orderAllowDeny"
669 (fn () => write
"\tOrder allow,deny\n")
671 val () = Env
.action_none
"orderDenyAllow"
672 (fn () => write
"\tOrder deny,allow\n")
674 val () = Env
.action_none
"allowFromAll"
675 (fn () => write
"\tAllow from all\n")
677 val () = Env
.action_one
"allowFrom"
678 ("entries", Env
.list Env
.string)
682 | _
=> (write
"\tAllow from";
683 app (fn name
=> (write
" "; write name
)) names
;
686 val () = Env
.action_none
"denyFromAll"
687 (fn () => write
"\tDeny from all\n")
689 val () = Env
.action_one
"denyFrom"
690 ("entries", Env
.list Env
.string)
694 | _
=> (write
"\tDeny from";
695 app (fn name
=> (write
" "; write name
)) names
;
698 val () = Env
.action_none
"satisfyAll"
699 (fn () => write
"\tSatisfy all\n")
701 val () = Env
.action_none
"satisfyAny"
702 (fn () => write
"\tSatisfy any\n")
704 val () = Env
.action_one
"forceType"
706 (fn ty
=> (write
"\tForceType ";
710 val () = Env
.action_none
"forceTypeOff"
711 (fn () => write
"\tForceType None\n")
713 val () = Env
.action_two
"action"
714 ("what", Env
.string, "how", Env
.string)
715 (fn (what
, how
) => (write
"\tAction ";
721 val () = Env
.action_one
"addDefaultCharset"
722 ("charset", Env
.string)
723 (fn ty
=> (write
"\tAddDefaultCharset ";
727 (*val () = Env
.action_one
"davSvn"
729 (fn path
=> (write
"\tDAV svn\n\tSVNPath ";
733 val () = Env
.action_one
"authzSvnAccessFile"
735 (fn path
=> (write
"\tAuthzSVNAccessFile ";
739 val () = Env
.action_two
"addDescription"
740 ("description", Env
.string, "patterns", Env
.list Env
.string)
744 | _
=> (write
"\tAddDescription \"";
745 write (String.toString desc
);
747 app (fn pat
=> (write
" "; write pat
)) pats
;
750 val () = Env
.action_one
"indexOptions"
751 ("options", Env
.list autoindex_option
)
755 | _
=> (write
"\tIndexOptions";
756 app (fn (opt
, arg
) =>
759 Option
.app (fn arg
=>
760 (write
"="; write arg
)) arg
)) opts
;
763 val () = Env
.action_one
"set_indexOptions"
764 ("options", Env
.list autoindex_option
)
768 | _
=> (write
"\tIndexOptions";
769 app (fn (opt
, arg
) =>
772 Option
.app (fn arg
=>
773 (write
"="; write arg
)) arg
)) opts
;
776 val () = Env
.action_one
"unset_indexOptions"
777 ("options", Env
.list autoindex_option
)
781 | _
=> (write
"\tIndexOptions";
787 val () = Env
.action_one
"headerName"
789 (fn name
=> (write
"\tHeaderName ";
793 val () = Env
.action_one
"readmeName"
795 (fn name
=> (write
"\tReadmeName ";
799 val () = Domain
.registerResetLocal (fn () =>
800 ignore (OS
.Process
.system (Config
.rm ^
" -rf /var/domtool/vhosts/*")))