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 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 localRewriteEnabled
= ref
false
276 val currentVhost
= ref
""
277 val currentVhostId
= ref
""
279 val pre
= ref (fn _
: {user
: string, nodes
: string list
, id
: string, hostname
: string} => ())
284 pre
:= (fn x
=> (old x
; f x
))
287 val post
= ref (fn () => ())
292 post
:= (fn () => (old (); f ()))
295 val aliaser
= ref (fn _
: string => ())
296 fun registerAliaser f
=
300 aliaser
:= (fn x
=> (old x
; f x
))
303 val () = Env
.containerV_one
"vhost"
307 val nodes
= Env
.env (Env
.list Env
.string) (env
, "WebNodes")
309 val ssl
= Env
.env Env
.bool (env
, "SSL")
310 val user
= Env
.env Env
.string (env
, "User")
311 val group
= Env
.env Env
.string (env
, "Group")
312 val docroot
= Env
.env Env
.string (env
, "DocumentRoot")
313 val sadmin
= Env
.env Env
.string (env
, "ServerAdmin")
315 val fullHost
= host ^
"." ^ Domain
.currentDomain ()
316 val vhostId
= fullHost ^
(if ssl
then ".ssl" else "")
317 val confFile
= fullHost ^
(if ssl
then ".vhost_ssl" else ".vhost")
319 currentVhost
:= fullHost
;
320 currentVhostId
:= vhostId
;
322 rewriteEnabled
:= false;
323 localRewriteEnabled
:= false;
324 vhostFiles
:= map (fn node
=>
326 val file
= Domain
.domainFile
{node
= node
,
329 TextIO.output (file
, "<VirtualHost ");
330 TextIO.output (file
, Domain
.nodeIp node
);
331 TextIO.output (file
, ":");
332 TextIO.output (file
, if ssl
then
336 TextIO.output (file
, ">\n");
337 TextIO.output (file
, "\tErrorLog ");
338 TextIO.output (file
, Config
.homeBase
);
339 TextIO.output (file
, "/");
340 TextIO.output (file
, user
);
341 TextIO.output (file
, "/apache/log/");
342 TextIO.output (file
, node
);
343 TextIO.output (file
, "/");
344 TextIO.output (file
, vhostId
);
345 TextIO.output (file
, "/error.log\n\tCustomLog ");
346 TextIO.output (file
, Config
.homeBase
);
347 TextIO.output (file
, "/");
348 TextIO.output (file
, user
);
349 TextIO.output (file
, "/apache/log/");
350 TextIO.output (file
, node
);
351 TextIO.output (file
, "/");
352 TextIO.output (file
, vhostId
);
353 TextIO.output (file
, "/access.log combined\n");
354 (Config
.homeBase ^
"/" ^ user ^
"/apache/log/"
355 ^ node ^
"/" ^ vhostId
, file
)
358 write
"\tServerName ";
360 write
"\n\tSuexecUserGroup ";
364 write
"\n\tDocumentRoot ";
366 write
"\n\tServerAdmin ";
369 !pre
{user
= user
, nodes
= nodes
, id
= vhostId
, hostname
= fullHost
}
372 write
"</VirtualHost>\n";
373 app (TextIO.closeOut
o #
2) (!vhostFiles
)))
375 val inLocal
= ref
false
377 val () = Env
.container_one
"location"
378 ("prefix", Env
.string)
380 (write
"\t<Location ";
384 fn () => (write
"\t</Location>\n";
386 localRewriteEnabled
:= false))
388 val () = Env
.container_one
"directory"
389 ("directory", Env
.string)
391 (write
"\t<Directory ";
395 fn () => (write
"\t</Directory>\n";
397 localRewriteEnabled
:= false))
399 fun checkRewrite () =
401 if !rewriteEnabled
orelse !localRewriteEnabled
then
404 (write
"\tRewriteEngine on\n";
405 localRewriteEnabled
:= true)
406 else if !rewriteEnabled
then
409 (write
"\tRewriteEngine on\n";
410 rewriteEnabled
:= true)
412 val () = Env
.action_three
"localProxyRewrite"
413 ("from", Env
.string, "to", Env
.string, "port", Env
.int)
414 (fn (from
, to
, port
) =>
416 write
"\tRewriteRule\t";
418 write
"\thttp://localhost:";
419 write (Int.toString port
);
424 val () = Env
.action_two
"proxyPass"
425 ("from", Env
.string, "to", Env
.string)
427 (write
"\tProxyPass\t";
433 val () = Env
.action_two
"proxyPassReverse"
434 ("from", Env
.string, "to", Env
.string)
436 (write
"\tProxyPassReverse\t";
442 val () = Env
.action_three
"rewriteRule"
443 ("from", Env
.string, "to", Env
.string, "flags", Env
.list flag
)
444 (fn (from
, to
, flags
) =>
446 write
"\tRewriteRule\t";
452 | flag
::rest
=> (write
" [";
454 app (fn flag
=> (write
",";
459 val () = Env
.action_three
"rewriteCond"
460 ("test", Env
.string, "pattern", Env
.string, "flags", Env
.list cond_flag
)
461 (fn (from
, to
, flags
) =>
463 write
"\tRewriteCond\t";
469 | flag
::rest
=> (write
" [";
471 app (fn flag
=> (write
",";
476 val () = Env
.action_one
"rewriteLogLevel"
480 write
"\tRewriteLog ";
482 write
"/rewrite.log\n\tRewriteLogLevel ";
483 write (Int.toString level
);
486 val () = Env
.action_two
"alias"
487 ("from", Env
.string, "to", Env
.string)
495 val () = Env
.action_two
"scriptAlias"
496 ("from", Env
.string, "to", Env
.string)
498 (write
"\tScriptAlias\t";
504 val () = Env
.action_two
"errorDocument"
505 ("code", Env
.string, "handler", Env
.string)
506 (fn (code
, handler
) =>
507 (write
"\tErrorDocument\t";
513 val () = Env
.action_one
"options"
514 ("options", Env
.list apache_option
)
518 | _
=> (write
"\tOptions";
519 app (fn opt
=> (write
" "; write opt
)) opts
;
522 val () = Env
.action_one
"set_options"
523 ("options", Env
.list apache_option
)
527 | _
=> (write
"\tOptions";
528 app (fn opt
=> (write
" +"; write opt
)) opts
;
531 val () = Env
.action_one
"unset_options"
532 ("options", Env
.list apache_option
)
536 | _
=> (write
"\tOptions";
537 app (fn opt
=> (write
" -"; write opt
)) opts
;
540 val () = Env
.action_one
"directoryIndex"
541 ("filenames", Env
.list Env
.string)
543 (write
"\tDirectoryIndex";
544 app (fn opt
=> (write
" "; write opt
)) opts
;
547 val () = Env
.action_one
"serverAlias"
550 (write
"\tServerAlias ";
555 val authType
= fn (EVar
"basic", _
) => SOME
"basic"
556 |
(EVar
"digest", _
) => SOME
"digest"
559 val () = Env
.action_one
"authType"
562 (write
"\tAuthType ";
566 val () = Env
.action_one
"authName"
569 (write
"\tAuthName \"";
573 val () = Env
.action_one
"authUserFile"
576 (write
"\tAuthUserFile ";
580 val () = Env
.action_none
"requireValidUser"
581 (fn () => write
"\tRequire valid-user\n")
583 val () = Env
.action_one
"requireUser"
584 ("users", Env
.list Env
.string)
588 | _
=> (write
"\tRequire user";
589 app (fn name
=> (write
" "; write name
)) names
;
592 val () = Env
.action_one
"requireGroup"
593 ("groups", Env
.list Env
.string)
597 | _
=> (write
"\tRequire group";
598 app (fn name
=> (write
" "; write name
)) names
;
601 val () = Env
.action_none
"orderAllowDeny"
602 (fn () => write
"\tOrder allow,deny\n")
604 val () = Env
.action_none
"orderDenyAllow"
605 (fn () => write
"\tOrder deny,allow\n")
607 val () = Env
.action_none
"allowFromAll"
608 (fn () => write
"\tAllow from all\n")
610 val () = Env
.action_one
"allowFrom"
611 ("entries", Env
.list Env
.string)
615 | _
=> (write
"\tAllow from";
616 app (fn name
=> (write
" "; write name
)) names
;
619 val () = Env
.action_none
"denyFromAll"
620 (fn () => write
"\tDeny from all\n")
622 val () = Env
.action_one
"denyFrom"
623 ("entries", Env
.list Env
.string)
627 | _
=> (write
"\tDeny from";
628 app (fn name
=> (write
" "; write name
)) names
;
631 val () = Env
.action_none
"satisfyAll"
632 (fn () => write
"\tSatisfy all\n")
634 val () = Env
.action_none
"satisfyAny"
635 (fn () => write
"\tSatisfy any\n")
637 val () = Env
.action_one
"forceType"
639 (fn ty
=> (write
"\tForceType ";
643 val () = Env
.action_none
"forceTypeOff"
644 (fn () => write
"\tForceType None\n")
646 val () = Env
.action_two
"action"
647 ("what", Env
.string, "how", Env
.string)
648 (fn (what
, how
) => (write
"\tAction ";
654 val () = Env
.action_one
"addDefaultCharset"
655 ("charset", Env
.string)
656 (fn ty
=> (write
"\tAddDefaultCharset ";
660 (*val () = Env
.action_one
"davSvn"
662 (fn path
=> (write
"\tDAV svn\n\tSVNPath ";
666 val () = Env
.action_one
"authzSvnAccessFile"
668 (fn path
=> (write
"\tAuthzSVNAccessFile ";
672 val () = Env
.action_two
"addDescription"
673 ("description", Env
.string, "patterns", Env
.list Env
.string)
677 | _
=> (write
"\tAddDescription \"";
678 write (String.toString desc
);
680 app (fn pat
=> (write
" "; write pat
)) pats
;
683 val () = Env
.action_one
"indexOptions"
684 ("options", Env
.list autoindex_option
)
688 | _
=> (write
"\tIndexOptions";
689 app (fn (opt
, arg
) =>
692 Option
.app (fn arg
=>
693 (write
"="; write arg
)) arg
)) opts
;
696 val () = Env
.action_one
"set_indexOptions"
697 ("options", Env
.list autoindex_option
)
701 | _
=> (write
"\tIndexOptions";
702 app (fn (opt
, arg
) =>
705 Option
.app (fn arg
=>
706 (write
"="; write arg
)) arg
)) opts
;
709 val () = Env
.action_one
"unset_indexOptions"
710 ("options", Env
.list autoindex_option
)
714 | _
=> (write
"\tIndexOptions";
720 val () = Env
.action_one
"headerName"
722 (fn name
=> (write
"\tHeaderName ";
726 val () = Env
.action_one
"readmeName"
728 (fn name
=> (write
"\tReadmeName ";