1 (* HCoop
Domtool (http
://hcoop
.sourceforge
.net
/)
2 * Copyright (c
) 2006-2007, 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 (* Domain
-related primitive actions
*)
21 structure Domain
:> DOMAIN
= struct
25 structure SM
= DataStructures
.StringMap
26 structure SS
= DataStructures
.StringSet
28 val ssl_context
= ref (NONE
: OpenSSL
.context option
)
29 fun set_context ctx
= ssl_context
:= SOME ctx
30 fun get_context () = valOf (!ssl_context
)
32 val nodes
= map #
1 Config
.nodeIps
33 val nodeMap
= foldl (fn ((node
, ip
), mp
) => SM
.insert (mp
, node
, ip
))
34 SM
.empty Config
.nodeIps
35 fun nodeIp node
= valOf (SM
.find (nodeMap
, node
))
39 val fakePrivs
= ref
false
40 val isClient
= ref
false
42 val your_doms
= ref SS
.empty
43 fun your_domains () = !your_doms
45 val your_usrs
= ref SS
.empty
46 fun your_users () = !your_usrs
48 val your_grps
= ref SS
.empty
49 fun your_groups () = !your_grps
51 val your_pths
= ref SS
.empty
52 fun your_paths () = !your_pths
54 val your_ipss
= ref SS
.empty
55 fun your_ips () = !your_ipss
57 val world_readable
= SS
.addList (SS
.empty
, Config
.worldReadable
)
58 val readable_pths
= ref world_readable
59 fun readable_paths () = !readable_pths
65 val your_paths
= Acl
.class
{user
= getUser (),
69 your_doms
:= Acl
.class
{user
= getUser (),
71 your_usrs
:= Acl
.class
{user
= getUser (),
73 your_grps
:= Acl
.class
{user
= getUser (),
75 your_pths
:= your_paths
;
76 readable_pths
:= SS
.union (your_paths
, world_readable
);
77 your_ipss
:= Acl
.class
{user
= getUser (),
81 fun declareClient () = isClient
:= true
82 fun fakePrivileges () = if !isClient
then
85 raise Fail
"Tried to fake privileges as non-client"
88 case map
Int.fromString (String.fields (fn ch
=> ch
= #
".") s
) of
89 [SOME n1
, SOME n2
, SOME n3
, SOME n4
] =>
90 n1
>= 0 andalso n1
< 256 andalso n2
>= 0 andalso n2
< 256 andalso n3
>= 0 andalso n3
< 256 andalso n4
>= 0 andalso n4
< 256
93 fun isHexDigit ch
= Char.isDigit ch
orelse (ord ch
>= ord #
"a" andalso ord ch
<= ord #
"f")
97 val fields
= String.fields (fn ch
=> ch
= #
":") s
99 val empties
= foldl (fn ("", n
) => n
+ 1
100 |
(_
, n
) => n
) 0 fields
104 andalso length fields
<= maxLen
106 andalso List.all (fn "" => true
108 andalso CharVector
.all isHexDigit s
) fields
114 val maybeIpv4
= List.last fields
115 val theRest
= List.take (fields
, length fields
- 1)
117 validIp maybeIpv4
andalso noIpv4
6
120 noIpv4
8 orelse hasIpv4 ()
123 fun isIdent ch
= Char.isLower ch
orelse Char.isDigit ch
126 size s
> 0 andalso size s
< 50
127 andalso CharVector
.all (fn ch
=> isIdent ch
orelse ch
= #
"-") s
130 size s
> 0 andalso size s
< 200
131 andalso List.all
validHost (String.fields (fn ch
=> ch
= #
".") s
)
133 fun validNode s
= List.exists (fn s
' => s
= s
') nodes
135 fun yourDomain s
= !fakePrivs
orelse SS
.member (your_domains (), s
)
136 fun yourUser s
= SS
.member (your_users (), s
)
137 fun yourGroup s
= SS
.member (your_groups (), s
)
138 fun checkPath paths path
=
139 (List.all (fn s
=> s
<> "..") (String.fields (fn ch
=> ch
= #
"/") path
)
140 andalso CharVector
.all (fn ch
=> Char.isAlphaNum ch
orelse ch
= #
"." orelse ch
= #
"/"
141 orelse ch
= #
"-" orelse ch
= #
"_") path
142 andalso SS
.exists (fn s
' => path
= s
' orelse String.isPrefix (s
' ^
"/") path
) (paths ()))
143 val yourPath
= checkPath your_paths
144 val readablePath
= checkPath readable_paths
145 fun yourIp s
= !fakePrivs
orelse SS
.member (your_ips (), s
)
147 fun yourDomainHost s
=
151 val (pref
, suf
) = Substring
.splitl (fn ch
=> ch
<> #
".") (Substring
.full s
)
153 Substring
.size suf
> 0
154 andalso validHost (Substring
.string pref
)
155 andalso yourDomain (Substring
.string
156 (Substring
.slice (suf
, 1, NONE
)))
159 val yourDomain
= yourDomainHost
161 fun validUser s
= size s
> 0 andalso size s
< 20
162 andalso CharVector
.all
Char.isAlphaNum s
164 fun validEmailUser s
=
165 size s
> 0 andalso size s
< 50
166 andalso CharVector
.all (fn ch
=> Char.isAlphaNum ch
172 val validGroup
= validUser
174 val _
= Env
.type_one
"no_spaces"
176 (CharVector
.all (fn ch
=> Char.isPrint ch
andalso not (Char.isSpace ch
)
177 andalso ch
<> #
"\"" andalso ch
<> #
"'"))
178 val _
= Env
.type_one
"no_newlines"
180 (CharVector
.all (fn ch
=> Char.isPrint ch
andalso ch
<> #
"\n" andalso ch
<> #
"\r"
181 andalso ch
<> #
"\""))
183 val _
= Env
.type_one
"ip"
187 val _
= Env
.type_one
"ipv6"
191 val _
= Env
.type_one
"host"
195 val _
= Env
.type_one
"domain"
199 val _
= Env
.type_one
"your_domain"
203 val _
= Env
.type_one
"your_domain_host"
207 val _
= Env
.type_one
"user"
211 val _
= Env
.type_one
"group"
215 val _
= Env
.type_one
"your_user"
219 val _
= Env
.type_one
"your_group"
223 val _
= Env
.type_one
"your_path"
227 val _
= Env
.type_one
"readable_path"
231 val _
= Env
.type_one
"your_ip"
235 val _
= Env
.type_one
"node"
239 val _
= Env
.type_one
"mime_type"
241 (CharVector
.exists (fn ch
=> ch
= #
"/"))
243 val _
= Env
.registerFunction ("your_ip_to_ip",
247 val _
= Env
.registerFunction ("dns_node_to_node",
251 val _
= Env
.registerFunction ("mail_node_to_node",
258 val dl
= ErrorMsg
.dummyLoc
260 val _
= Env
.registerFunction ("end_in_slash",
261 fn [(EString
"", _
)] => SOME (EString
"/", dl
)
262 |
[(EString s
, _
)] =>
263 SOME (EString (if String.sub (s
, size s
- 1) = #
"/" then
270 val _
= Env
.registerFunction ("you",
271 fn [] => SOME (EString (getUser ()), dl
)
274 val _
= Env
.registerFunction ("defaultMailbox",
275 fn [] => SOME (EString (getUser ()), dl
)
279 type soa
= {ns
: string,
286 val serial
= fn (EVar
"serialAuto", _
) => SOME NONE
287 |
(EApp ((EVar
"serialConst", _
), n
), _
) => Option
.map
SOME (Env
.int n
)
290 val soa
= fn (EApp ((EApp ((EApp ((EApp ((EApp ((EApp
291 ((EVar
"soa", _
), ns
), _
),
297 (case (Env
.string ns
, serial sl
, Env
.int rf
,
298 Env
.int ret
, Env
.int exp
, Env
.int min
) of
299 (SOME ns
, SOME sl
, SOME rf
,
300 SOME ret
, SOME exp
, SOME min
) =>
311 ExternalMaster
of string
312 | InternalMaster
of string
316 val _
= Env
.registerFunction ("ip_of_node",
317 fn [(EString node
, _
)] => SOME (EString (nodeIp node
), dl
)
320 val master
= fn (EApp ((EVar
"externalMaster", _
), e
), _
) => Option
.map
ExternalMaster (ip e
)
321 |
(EApp ((EVar
"internalMaster", _
), e
), _
) => Option
.map
InternalMaster (Env
.string e
)
325 UseDns
of {soa
: soa
,
327 slaves
: string list
}
330 val dnsKind
= fn (EApp ((EApp ((EApp
331 ((EVar
"useDns", _
), sa
), _
),
334 (case (soa sa
, master mstr
, Env
.list Env
.string slaves
) of
335 (SOME sa
, SOME mstr
, SOME slaves
) =>
336 SOME (UseDns
{soa
= sa
,
340 |
(EVar
"noDns", _
) => SOME NoDns
343 val befores
= ref (fn (_
: string) => ())
344 val afters
= ref (fn (_
: string) => ())
346 fun registerBefore f
=
350 befores
:= (fn x
=> (old x
; f x
))
353 fun registerAfter f
=
357 afters
:= (fn x
=> (old x
; f x
))
360 val globals
= ref (fn () => ())
361 val locals
= ref (fn () => ())
363 fun registerResetGlobal f
=
367 globals
:= (fn x
=> (old x
; f x
))
370 fun registerResetLocal f
=
374 locals
:= (fn x
=> (old x
; f x
))
377 fun resetGlobal () = (!globals ();
378 ignore (OS
.Process
.system (Config
.rm ^
" -rf " ^ Config
.resultRoot ^
"/*")))
379 fun resetLocal () = !locals ()
382 val currentPath
= ref (fn (_
: string) => "")
383 val currentPathAli
= ref (fn (_
: string, _
: string) => "")
387 fun currentDomain () = !current
389 val currentsAli
= ref ([] : string list
)
391 fun currentAliasDomains () = !currentsAli
392 fun currentDomains () = currentDomain () :: currentAliasDomains ()
394 fun domainFile
{node
, name
} = ((*print ("Opening " ^
!currentPath node ^ name ^
"\n");*)
395 TextIO.openOut (!currentPath node ^ name
))
397 type files
= {write
: string -> unit
,
398 writeDom
: unit
-> unit
,
399 close
: unit
-> unit
}
401 fun domainsFile
{node
, name
} =
403 val doms
= currentDomains ()
404 val files
= map (fn dom
=> (dom
, TextIO.openOut (!currentPathAli (dom
, node
) ^ name
))) doms
406 {write
= fn s
=> app (fn (_
, outf
) => TextIO.output (outf
, s
)) files
,
407 writeDom
= fn () => app (fn (dom
, outf
) => TextIO.output (outf
, dom
)) files
,
408 close
= fn () => app (fn (_
, outf
) => TextIO.closeOut outf
) files
}
413 val toks
= String.fields (fn ch
=> ch
= #
".") domain
415 val elems
= foldr (fn (piece
, elems
) =>
417 val elems
= piece
:: elems
421 val path
= String.concatWith
"/"
422 (Config
.resultRoot
:: node
:: rev elems
)
423 val tmpPath
= String.concatWith
"/"
424 (Config
.tmpDir
:: node
:: rev elems
)
426 (if Posix
.FileSys
.ST
.isDir
427 (Posix
.FileSys
.stat path
) then
430 (OS
.FileSys
.remove path
;
431 OS
.FileSys
.mkDir path
))
432 handle OS
.SysErr _
=> OS
.FileSys
.mkDir path
;
434 (if Posix
.FileSys
.ST
.isDir
435 (Posix
.FileSys
.stat tmpPath
) then
438 (OS
.FileSys
.remove tmpPath
;
439 OS
.FileSys
.mkDir tmpPath
))
440 handle OS
.SysErr _
=> OS
.FileSys
.mkDir tmpPath
447 fn (root
, site
) => String.concatWith
"/" (root
:: site
:: rev ("" :: elems
))
450 datatype file_action
' =
451 Add
' of {src
: string, dst
: string}
453 | Modify
' of {src
: string, dst
: string}
455 fun findDiffs (prefixes
, site
, dom
, acts
) =
458 val realPath
= gp (Config
.resultRoot
, site
)
459 val tmpPath
= gp (Config
.tmpDir
, site
)
461 (*val _
= print ("getDiffs(" ^ site ^
", " ^ dom ^
")... " ^ realPath ^
"; " ^ tmpPath ^
"\n")*)
463 val dir
= Posix
.FileSys
.opendir realPath
466 case Posix
.FileSys
.readdir dir
of
467 NONE
=> (Posix
.FileSys
.closedir dir
;
471 val real = OS
.Path
.joinDirFile
{dir
= realPath
,
473 val tmp
= OS
.Path
.joinDirFile
{dir
= tmpPath
,
476 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat
real) then
478 else if Posix
.FileSys
.access (tmp
, []) then
479 if Slave
.shell
[Config
.diff
, " ", real, " ", tmp
] then
482 loopReal ((site
, dom
, realPath
, Modify
' {src
= tmp
, dst
= real}) :: acts
)
483 else if List.exists (fn prefix
=> String.isPrefix prefix
real) prefixes
then
484 loopReal ((site
, dom
, realPath
, Delete
' real) :: acts
)
489 val acts
= loopReal acts
491 val dir
= Posix
.FileSys
.opendir tmpPath
494 case Posix
.FileSys
.readdir dir
of
495 NONE
=> (Posix
.FileSys
.closedir dir
;
499 val real = OS
.Path
.joinDirFile
{dir
= realPath
,
501 val tmp
= OS
.Path
.joinDirFile
{dir
= tmpPath
,
504 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat tmp
) then
506 else if Posix
.FileSys
.access (real, []) then
509 loopTmp ((site
, dom
, realPath
, Add
' {src
= tmp
, dst
= real}) :: acts
)
512 val acts
= loopTmp acts
517 fun findAllDiffs prefixes
=
519 val dir
= Posix
.FileSys
.opendir Config
.tmpDir
520 val len
= length (String.fields (fn ch
=> ch
= #
"/") Config
.tmpDir
) + 1
522 fun exploreSites diffs
=
523 case Posix
.FileSys
.readdir dir
of
527 fun explore (dname
, diffs
) =
529 val dir
= Posix
.FileSys
.opendir dname
532 case Posix
.FileSys
.readdir dir
of
536 val fname
= OS
.Path
.joinDirFile
{dir
= dname
,
539 loop (if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat fname
) then
541 val dom
= String.fields (fn ch
=> ch
= #
"/") fname
542 val dom
= List.drop (dom
, len
)
543 val dom
= String.concatWith
"." (rev dom
)
545 val dname
' = OS
.Path
.joinDirFile
{dir
= dname
,
549 findDiffs (prefixes
, site
, dom
, diffs
))
556 before Posix
.FileSys
.closedir dir
559 exploreSites (explore (OS
.Path
.joinDirFile
{dir
= Config
.tmpDir
,
560 file
= site
}, diffs
))
564 before Posix
.FileSys
.closedir dir
567 val masterNode
: string option ref
= ref NONE
568 fun dnsMaster () = !masterNode
570 val seenDomains
: string list ref
= ref
[]
572 val _
= Env
.containerV_one
"domain"
573 ("domain", Env
.string)
576 val () = seenDomains
:= dom
:: !seenDomains
578 val kind
= Env
.env
dnsKind (evs
, "DNS")
579 val ttl
= Env
.env Env
.int (evs
, "TTL")
580 val aliases
= Env
.env (Env
.list Env
.string) (evs
, "Aliases")
582 val path
= getPath dom
584 val () = (current
:= dom
;
585 currentsAli
:= Slave
.remove (Slave
.removeDups aliases
, dom
);
586 currentPath
:= (fn site
=> path (Config
.tmpDir
, site
));
587 currentPathAli
:= (fn (dom
, site
) => getPath
dom (Config
.tmpDir
, site
)))
589 fun saveSoa (kind
, soa
: soa
) node
=
591 val {write
, writeDom
, close
} = domainsFile
{node
= node
, name
= "soa.conf"}
595 write (Int.toString ttl
);
601 | SOME n
=> write (Int.toString n
);
603 write (Int.toString (#ref soa
));
605 write (Int.toString (#ret soa
));
607 write (Int.toString (#exp soa
));
609 write (Int.toString (#min soa
));
614 fun saveNamed (kind
, soa
: soa
, masterIp
, slaveIps
) node
=
615 if dom
= "localhost" then
618 val {write
, writeDom
, close
} = domainsFile
{node
= node
, name
= "named.conf"}
622 write
"\" {\n\ttype ";
624 write
";\n\tfile \"";
625 write Config
.Bind
.zonePath_real
;
630 "master" => (write
"\tallow-transfer {\n";
631 app (fn ip
=> (write
"\t\t";
633 write
";\n")) slaveIps
;
635 | _
=> (write
"\tmasters { ";
638 write
"// Updated: ";
639 write (Time
.toString (Time
.now ()));
646 NoDns
=> masterNode
:= NONE
651 InternalMaster node
=> nodeIp node
652 | ExternalMaster ip
=> ip
654 val slaveIps
= map
nodeIp (#slaves dns
)
656 app (saveSoa ("slave", #soa dns
)) (#slaves dns
);
657 app (saveNamed ("slave", #soa dns
, masterIp
, slaveIps
)) (#slaves dns
);
659 InternalMaster node
=>
660 (masterNode
:= SOME node
;
661 saveSoa ("master", #soa dns
) node
;
662 saveNamed ("master", #soa dns
, masterIp
, slaveIps
) node
)
663 | _
=> masterNode
:= NONE
667 fn () => !afters (!current
))
669 val () = Env
.registerPre (fn () => (seenDomains
:= [];
670 ignore (Slave
.shellF ([Config
.rm
, " -rf ", Config
.tmpDir
, ""],
671 fn cl
=> "Temp file cleanup failed: " ^ cl
));
672 OS
.FileSys
.mkDir Config
.tmpDir
;
673 app (fn node
=> OS
.FileSys
.mkDir
674 (OS
.Path
.joinDirFile
{dir
= Config
.tmpDir
,
677 app (fn node
=> OS
.FileSys
.mkDir
678 (OS
.Path
.joinDirFile
{dir
= Config
.resultRoot
,
680 handle OS
.SysErr _
=> ())
683 fun handleSite (site
, files
) =
687 print ("New configuration for node " ^ site ^
"\n");
688 if site
= Config
.defaultNode
then
689 Slave
.handleChanges files
691 val bio
= OpenSSL
.connect
true (valOf (!ssl_context
),
694 ^
Int.toString Config
.slavePort
)
696 app (fn file
=> Msg
.send (bio
, MsgFile file
)) files
;
697 Msg
.send (bio
, MsgDoFiles
);
699 NONE
=> print
"Slave closed connection unexpectedly\n"
702 MsgOk
=> print ("Slave " ^ site ^
" finished\n")
703 | MsgError s
=> print ("Slave " ^ site
704 ^
" returned error: " ^
706 | _
=> print ("Slave " ^ site
707 ^
" returned unexpected command\n");
712 val () = Env
.registerPost (fn () =>
714 val prefixes
= List.concat
717 val pieces
= String.tokens (fn ch
=> ch
= #
".") dom
718 val path
= String.concatWith
"/" (rev pieces
)
721 Config
.resultRoot ^
"/" ^ node ^
"/" ^ path ^
"/")
725 val diffs
= findAllDiffs prefixes
727 val diffs
= map (fn (site
, dom
, dir
, Add
' {src
, dst
}) =>
728 (Slave
.shellF ([Config
.cp
, " ", src
, " ", dst
],
729 fn cl
=> "Copy failed: " ^ cl
);
735 |
(site
, dom
, dir
, Delete
' dst
) =>
736 (OS
.FileSys
.remove dst
737 handle OS
.SysErr _
=>
738 ErrorMsg
.error
NONE ("Delete failed for " ^ dst
);
740 {action
= Slave
.Delete
true,
744 |
(site
, dom
, dir
, Modify
' {src
, dst
}) =>
745 (Slave
.shellF ([Config
.cp
, " ", src
, " ", dst
],
746 fn cl
=> "Copy failed: " ^ cl
);
748 {action
= Slave
.Modify
,
753 if !ErrorMsg
.anyErrors
then
756 val changed
= foldl (fn ((site
, file
), changed
) =>
758 val ls
= case SM
.find (changed
, site
) of
762 SM
.insert (changed
, site
, file
:: ls
)
765 SM
.appi handleSite changed
767 ignore (Slave
.shellF ([Config
.rm
, " -rf ", Config
.tmpDir
, ""],
768 fn cl
=> "Temp file cleanup failed: " ^ cl
))
771 fun hasPriv priv
= Acl
.query
{user
= getUser (), class
= "priv", value
= "all"}
772 orelse Acl
.query
{user
= getUser (), class
= "priv", value
= priv
}
774 val _
= Env
.type_one
"dns_node"
777 List.exists (fn x
=> x
= node
) Config
.dnsNodes_all
778 orelse (hasPriv
"dns"
779 andalso List.exists (fn x
=> x
= node
) Config
.dnsNodes_admin
))
781 val _
= Env
.type_one
"mail_node"
784 List.exists (fn x
=> x
= node
) Config
.mailNodes_all
785 orelse (hasPriv
"mail"
786 andalso List.exists (fn x
=> x
= node
) Config
.mailNodes_admin
))
788 fun rmdom
' delete resultRoot doms
=
790 fun doNode (node
, _
) =
792 val dname
= OS
.Path
.joinDirFile
{dir
= resultRoot
,
795 fun doDom (dom
, actions
) =
797 val domPath
= String.concatWith
"/" (rev (String.fields (fn ch
=> ch
= #
".") dom
))
798 val dname
= OS
.Path
.concat (dname
, domPath
)
800 fun visitDom (dom
, dname
, actions
) =
802 val dir
= Posix
.FileSys
.opendir dname
805 case Posix
.FileSys
.readdir dir
of
809 val fnameFull
= OS
.Path
.joinDirFile
{dir
= dname
,
812 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat fnameFull
) then
813 loop (visitDom (fname ^
"." ^ dom
,
817 loop ({action
= Slave
.Delete delete
,
820 file
= fnameFull
} :: actions
)
824 before Posix
.FileSys
.closedir dir
826 handle OS
.SysErr (s
, _
) =>
827 (print ("Warning: System error deleting domain " ^ dom ^
" on " ^ node ^
": " ^ s ^
"\n");
830 visitDom (dom
, dname
, actions
)
833 val actions
= foldl doDom
[] doms
835 handleSite (node
, actions
)
837 handle IO
.Io _
=> print ("Warning: IO error deleting domains on " ^ node ^
".\n")
839 fun cleanupNode (node
, _
) =
843 val domPath
= String.concatWith
"/" (rev (String.fields (fn ch
=> ch
= #
".") dom
))
844 val dname
= OS
.Path
.joinDirFile
{dir
= resultRoot
,
846 val dname
= OS
.Path
.concat (dname
, domPath
)
849 ignore (OS
.Process
.system (Config
.rm ^
" -rf " ^ dname
))
857 app doNode Config
.nodeIps
;
858 app cleanupNode Config
.nodeIps
861 val rmdom
= rmdom
' true Config
.resultRoot
862 val rmdom
' = rmdom
' false
864 fun homedirOf uname
=
865 Posix
.SysDB
.Passwd
.home (Posix
.SysDB
.getpwnam uname
)
867 fun homedir () = homedirOf (getUser ())
869 type subject
= {node
: string, domain
: string}
871 val describers
: (subject
-> string) list ref
= ref
[]
873 fun registerDescriber f
= describers
:= f
:: !describers
875 fun describeOne arg
= String.concat (map (fn f
=> f arg
) (rev (!describers
)))
877 val line
= "--------------------------------------------------------------\n"
878 val dline
= "==============================================================\n"
881 String.concat (List.mapPartial
883 case describeOne
{node
= node
, domain
= dom
} of
886 SOME (String.concat
[dline
, "Node ", node
, "\n", dline
, "\n", s
]))
889 datatype description
=
890 Filename
of { filename
: string, heading
: string, showEmpty
: bool }
891 | Extension
of { extension
: string, heading
: string -> string }
893 fun considerAll ds
{node
, domain
} =
895 val ds
= map (fn d
=> (d
, ref
[])) ds
897 val path
= Config
.resultRoot
898 val jdf
= OS
.Path
.joinDirFile
899 val path
= jdf
{dir
= path
, file
= node
}
900 val path
= foldr (fn (more
, path
) => jdf
{dir
= path
, file
= more
})
901 path (String.tokens (fn ch
=> ch
= #
".") domain
)
903 if Posix
.FileSys
.access (path
, []) then
905 val dir
= Posix
.FileSys
.opendir path
908 case Posix
.FileSys
.readdir dir
of
911 (app (fn (d
, entries
) =>
913 fun readFile showEmpty entries
' =
915 val fname
= OS
.Path
.joinDirFile
{dir
= path
,
918 val inf
= TextIO.openIn fname
920 fun loop (seenOne
, entries
') =
921 case TextIO.inputLine inf
of
922 NONE
=> if seenOne
orelse showEmpty
then
926 | SOME line
=> loop (true, line
:: entries
')
928 loop (false, entries
')
929 before TextIO.closeIn inf
933 Filename
{filename
, heading
, showEmpty
} =>
934 if fname
= filename
then
935 entries
:= readFile
showEmpty ("\n" :: line
:: "\n" :: heading
:: line
:: !entries
)
938 | Extension
{extension
, heading
} =>
940 val {base
, ext
} = OS
.Path
.splitBaseExt fname
945 if extension
' = extension
then
946 entries
:= readFile
true ("\n" :: line
:: "\n" :: heading base
:: line
:: !entries
)
954 Posix
.FileSys
.closedir dir
;
955 String.concat (List.concat (map (fn (_
, entries
) => rev (!entries
)) ds
))
961 val () = registerDescriber (considerAll
[Filename
{filename
= "soa.conf",
962 heading
= "DNS SOA:",
965 val () = Env
.registerAction ("domainHost",
966 fn (env
, [(EString host
, _
)]) =>
967 SM
.insert (env
, "Hostname",
968 (EString (host ^
"." ^
currentDomain ()), dl
))
969 |
(_
, args
) => Env
.badArgs ("domainHost", args
))
971 val ouc
= ref (fn () => ())
973 fun registerOnUsersChange f
=
977 ouc
:= (fn () => (f
' (); f ()))
980 fun onUsersChange () = !ouc ()