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
, ipv6
), mp
) => SM
.insert (mp
, node
, (ip
, ipv6
)))
34 SM
.empty Config
.nodeIps
35 fun nodeIp node
= #
1 (valOf (SM
.find (nodeMap
, node
)))
36 fun nodeIpv6 node
= #
2 (valOf (SM
.find (nodeMap
, node
)))
40 val fakePrivs
= ref
false
41 val isClient
= ref
false
43 val your_doms
= ref SS
.empty
44 fun your_domains () = !your_doms
46 val your_usrs
= ref SS
.empty
47 fun your_users () = !your_usrs
49 val your_grps
= ref SS
.empty
50 fun your_groups () = !your_grps
52 val your_pths
= ref SS
.empty
53 fun your_paths () = !your_pths
55 val your_ipss
= ref SS
.empty
56 fun your_ips () = !your_ipss
58 val world_readable
= SS
.addList (SS
.empty
, Config
.worldReadable
)
59 val readable_pths
= ref world_readable
60 fun readable_paths () = !readable_pths
66 val your_paths
= Acl
.class
{user
= getUser (),
70 your_doms
:= Acl
.class
{user
= getUser (),
72 your_usrs
:= Acl
.class
{user
= getUser (),
74 your_grps
:= SS
.add (Acl
.class
{user
= getUser (),
77 your_pths
:= your_paths
;
78 readable_pths
:= SS
.union (your_paths
, world_readable
);
79 your_ipss
:= Acl
.class
{user
= getUser (),
83 fun declareClient () = isClient
:= true
84 fun fakePrivileges () = if !isClient
then
87 raise Fail
"Tried to fake privileges as non-client"
90 case map
Int.fromString (String.fields (fn ch
=> ch
= #
".") s
) of
91 [SOME n1
, SOME n2
, SOME n3
, SOME n4
] =>
92 n1
>= 0 andalso n1
< 256 andalso n2
>= 0 andalso n2
< 256 andalso n3
>= 0 andalso n3
< 256 andalso n4
>= 0 andalso n4
< 256
95 fun isHexDigit ch
= Char.isDigit ch
orelse (ord ch
>= ord #
"a" andalso ord ch
<= ord #
"f")
99 val fields
= String.fields (fn ch
=> ch
= #
":") s
101 val empties
= foldl (fn ("", n
) => n
+ 1
102 |
(_
, n
) => n
) 0 fields
106 andalso length fields
<= maxLen
108 andalso List.all (fn "" => true
110 andalso CharVector
.all isHexDigit s
) fields
116 val maybeIpv4
= List.last fields
117 val theRest
= List.take (fields
, length fields
- 1)
119 validIp maybeIpv4
andalso noIpv4
6
122 noIpv4
8 orelse hasIpv4 ()
125 fun isIdent ch
= Char.isLower ch
orelse Char.isDigit ch
128 size s
> 0 andalso size s
< 50
129 andalso CharVector
.all (fn ch
=> isIdent ch
orelse ch
= #
"-") s
132 size s
> 0 andalso size s
< 200
133 andalso List.all
validHost (String.fields (fn ch
=> ch
= #
".") s
)
135 fun validNode s
= List.exists (fn s
' => s
= s
') nodes
137 fun yourDomain s
= !fakePrivs
orelse SS
.member (your_domains (), s
)
138 fun yourUser s
= !fakePrivs
orelse SS
.member (your_users (), s
)
139 fun yourGroup s
= !fakePrivs
orelse SS
.member (your_groups (), s
)
141 fun checkPath paths path
=
143 (List.all (fn s
=> s
<> "..") (String.fields (fn ch
=> ch
= #
"/") path
)
144 andalso CharVector
.all (fn ch
=> Char.isAlphaNum ch
orelse ch
= #
"." orelse ch
= #
"/"
145 orelse ch
= #
"-" orelse ch
= #
"_") path
146 andalso SS
.exists (fn s
' => path
= s
' orelse String.isPrefix (s
' ^
"/") path
) (paths ()))
147 val yourPath
= checkPath your_paths
148 val readablePath
= checkPath readable_paths
150 fun yourIp s
= !fakePrivs
orelse SS
.member (your_ips (), s
)
152 fun yourDomainHost s
=
156 val (pref
, suf
) = Substring
.splitl (fn ch
=> ch
<> #
".") (Substring
.full s
)
158 Substring
.size suf
> 0
159 andalso validHost (Substring
.string pref
)
160 andalso yourDomain (Substring
.string
161 (Substring
.slice (suf
, 1, NONE
)))
164 val yourDomain
= yourDomainHost
166 fun validUser s
= size s
> 0 andalso size s
< 20
167 andalso CharVector
.all
Char.isAlphaNum s
169 fun validEmailUser s
=
170 size s
> 0 andalso size s
< 50
171 andalso CharVector
.all (fn ch
=> Char.isAlphaNum ch
177 val validGroup
= validUser
179 val _
= Env
.type_one
"no_spaces"
181 (CharVector
.all (fn ch
=> Char.isPrint ch
andalso not (Char.isSpace ch
)
182 andalso ch
<> #
"\"" andalso ch
<> #
"'"))
183 val _
= Env
.type_one
"no_newlines"
185 (CharVector
.all (fn ch
=> Char.isPrint ch
andalso ch
<> #
"\n" andalso ch
<> #
"\r"
186 andalso ch
<> #
"\""))
188 val _
= Env
.type_one
"ip"
192 val _
= Env
.type_one
"ipv6"
196 val _
= Env
.type_one
"host"
200 val _
= Env
.type_one
"domain"
204 val _
= Env
.type_one
"your_domain"
208 val _
= Env
.type_one
"your_domain_host"
212 val _
= Env
.type_one
"user"
216 val _
= Env
.type_one
"group"
220 val _
= Env
.type_one
"your_user"
224 val _
= Env
.type_one
"your_group"
228 val _
= Env
.type_one
"your_path"
232 val _
= Env
.type_one
"readable_path"
236 val _
= Env
.type_one
"your_ip"
240 val _
= Env
.type_one
"node"
244 val _
= Env
.type_one
"mime_type"
246 (CharVector
.exists (fn ch
=> ch
= #
"/"))
248 val _
= Env
.registerFunction ("your_ip_to_ip",
252 val _
= Env
.registerFunction ("dns_node_to_node",
256 val _
= Env
.registerFunction ("mail_node_to_node",
263 val dl
= ErrorMsg
.dummyLoc
265 val _
= Env
.registerFunction ("end_in_slash",
266 fn [(EString
"", _
)] => SOME (EString
"/", dl
)
267 |
[(EString s
, _
)] =>
268 SOME (EString (if String.sub (s
, size s
- 1) = #
"/" then
275 val _
= Env
.registerFunction ("you",
276 fn [] => SOME (EString (getUser ()), dl
)
279 val _
= Env
.registerFunction ("defaultMailbox",
280 fn [] => SOME (EString (getUser ()), dl
)
283 val _
= Env
.registerFunction ("defaultMailUser",
284 fn [] => SOME (EString (getUser ()), dl
)
288 type soa
= {ns
: string,
295 val serial
= fn (EVar
"serialAuto", _
) => SOME NONE
296 |
(EApp ((EVar
"serialConst", _
), n
), _
) => Option
.map
SOME (Env
.int n
)
299 val soa
= fn (EApp ((EApp ((EApp ((EApp ((EApp ((EApp
300 ((EVar
"soa", _
), ns
), _
),
306 (case (Env
.string ns
, serial sl
, Env
.int rf
,
307 Env
.int ret
, Env
.int exp
, Env
.int min
) of
308 (SOME ns
, SOME sl
, SOME rf
,
309 SOME ret
, SOME exp
, SOME min
) =>
320 ExternalMaster
of string
321 | InternalMaster
of string
325 val _
= Env
.registerFunction ("ip_of_node",
326 fn [(EString node
, _
)] => SOME (EString (nodeIp node
), dl
)
329 val master
= fn (EApp ((EVar
"externalMaster", _
), e
), _
) => Option
.map
ExternalMaster (ip e
)
330 |
(EApp ((EVar
"internalMaster", _
), e
), _
) => Option
.map
InternalMaster (Env
.string e
)
334 UseDns
of {soa
: soa
,
336 slaves
: string list
}
339 val dnsKind
= fn (EApp ((EApp ((EApp
340 ((EVar
"useDns", _
), sa
), _
),
343 (case (soa sa
, master mstr
, Env
.list Env
.string slaves
) of
344 (SOME sa
, SOME mstr
, SOME slaves
) =>
345 SOME (UseDns
{soa
= sa
,
349 |
(EVar
"noDns", _
) => SOME NoDns
352 val befores
= ref (fn (_
: string) => ())
353 val afters
= ref (fn (_
: string) => ())
355 fun registerBefore f
=
359 befores
:= (fn x
=> (old x
; f x
))
362 fun registerAfter f
=
366 afters
:= (fn x
=> (old x
; f x
))
369 val globals
= ref (fn () => ())
370 val locals
= ref (fn () => ())
372 fun registerResetGlobal f
=
376 globals
:= (fn x
=> (old x
; f x
))
379 fun registerResetLocal f
=
383 locals
:= (fn x
=> (old x
; f x
))
386 fun resetGlobal () = (!globals ();
387 ignore (OS
.Process
.system (Config
.rm ^
" -rf " ^ Config
.resultRoot ^
"/*")))
388 fun resetLocal () = !locals ()
391 val currentPath
= ref (fn (_
: string) => "")
392 val currentPathAli
= ref (fn (_
: string, _
: string) => "")
396 fun currentDomain () = !current
398 val currentsAli
= ref ([] : string list
)
400 fun currentAliasDomains () = !currentsAli
401 fun currentDomains () = currentDomain () :: currentAliasDomains ()
403 fun domainFile
{node
, name
} = ((*print ("Opening " ^
!currentPath node ^ name ^
"\n");*)
404 TextIO.openOut (!currentPath node ^ name
))
406 type files
= {write
: string -> unit
,
407 writeDom
: unit
-> unit
,
408 close
: unit
-> unit
}
410 fun domainsFile
{node
, name
} =
412 val doms
= currentDomains ()
413 val files
= map (fn dom
=> (dom
, TextIO.openOut (!currentPathAli (dom
, node
) ^ name
))) doms
415 {write
= fn s
=> app (fn (_
, outf
) => TextIO.output (outf
, s
)) files
,
416 writeDom
= fn () => app (fn (dom
, outf
) => TextIO.output (outf
, dom
)) files
,
417 close
= fn () => app (fn (_
, outf
) => TextIO.closeOut outf
) files
}
422 val toks
= String.fields (fn ch
=> ch
= #
".") domain
424 val elems
= foldr (fn (piece
, elems
) =>
426 val elems
= piece
:: elems
430 val path
= String.concatWith
"/"
431 (Config
.resultRoot
:: node
:: rev elems
)
432 val tmpPath
= String.concatWith
"/"
433 (Config
.tmpDir
:: node
:: rev elems
)
435 (if Posix
.FileSys
.ST
.isDir
436 (Posix
.FileSys
.stat path
) then
439 (OS
.FileSys
.remove path
;
440 OS
.FileSys
.mkDir path
))
441 handle OS
.SysErr _
=> OS
.FileSys
.mkDir path
;
443 (if Posix
.FileSys
.ST
.isDir
444 (Posix
.FileSys
.stat tmpPath
) then
447 (OS
.FileSys
.remove tmpPath
;
448 OS
.FileSys
.mkDir tmpPath
))
449 handle OS
.SysErr _
=> OS
.FileSys
.mkDir tmpPath
456 fn (root
, site
) => String.concatWith
"/" (root
:: site
:: rev ("" :: elems
))
459 datatype file_action
' =
460 Add
' of {src
: string, dst
: string}
462 | Modify
' of {src
: string, dst
: string}
464 fun findDiffs (prefixes
, site
, dom
, acts
) =
467 val realPath
= gp (Config
.resultRoot
, site
)
468 val tmpPath
= gp (Config
.tmpDir
, site
)
470 (*val _
= print ("getDiffs(" ^ site ^
", " ^ dom ^
")... " ^ realPath ^
"; " ^ tmpPath ^
"\n")*)
472 val dir
= Posix
.FileSys
.opendir realPath
475 case Posix
.FileSys
.readdir dir
of
476 NONE
=> (Posix
.FileSys
.closedir dir
;
480 val real = OS
.Path
.joinDirFile
{dir
= realPath
,
482 val tmp
= OS
.Path
.joinDirFile
{dir
= tmpPath
,
485 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat
real) then
487 else if Posix
.FileSys
.access (tmp
, []) then
488 if Slave
.shell
[Config
.diff
, " ", real, " ", tmp
] then
491 loopReal ((site
, dom
, realPath
, Modify
' {src
= tmp
, dst
= real}) :: acts
)
492 else if List.exists (fn prefix
=> String.isPrefix prefix
real) prefixes
then
493 loopReal ((site
, dom
, realPath
, Delete
' real) :: acts
)
498 val acts
= loopReal acts
500 val dir
= Posix
.FileSys
.opendir tmpPath
503 case Posix
.FileSys
.readdir dir
of
504 NONE
=> (Posix
.FileSys
.closedir dir
;
508 val real = OS
.Path
.joinDirFile
{dir
= realPath
,
510 val tmp
= OS
.Path
.joinDirFile
{dir
= tmpPath
,
513 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat tmp
) then
515 else if Posix
.FileSys
.access (real, []) then
518 loopTmp ((site
, dom
, realPath
, Add
' {src
= tmp
, dst
= real}) :: acts
)
521 val acts
= loopTmp acts
526 fun findAllDiffs prefixes
=
528 val dir
= Posix
.FileSys
.opendir Config
.tmpDir
529 val len
= length (String.fields (fn ch
=> ch
= #
"/") Config
.tmpDir
) + 1
531 fun exploreSites diffs
=
532 case Posix
.FileSys
.readdir dir
of
536 fun explore (dname
, diffs
) =
538 val dir
= Posix
.FileSys
.opendir dname
541 case Posix
.FileSys
.readdir dir
of
545 val fname
= OS
.Path
.joinDirFile
{dir
= dname
,
548 loop (if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat fname
) then
550 val dom
= String.fields (fn ch
=> ch
= #
"/") fname
551 val dom
= List.drop (dom
, len
)
552 val dom
= String.concatWith
"." (rev dom
)
554 val dname
' = OS
.Path
.joinDirFile
{dir
= dname
,
558 findDiffs (prefixes
, site
, dom
, diffs
))
565 before Posix
.FileSys
.closedir dir
568 exploreSites (explore (OS
.Path
.joinDirFile
{dir
= Config
.tmpDir
,
569 file
= site
}, diffs
))
573 before Posix
.FileSys
.closedir dir
576 val masterNode
: string option ref
= ref NONE
577 fun dnsMaster () = !masterNode
579 val seenDomains
: string list ref
= ref
[]
581 val _
= Env
.containerV_one
"domain"
582 ("domain", Env
.string)
585 val () = seenDomains
:= dom
:: !seenDomains
587 val kind
= Env
.env
dnsKind (evs
, "DNS")
588 val ttl
= Env
.env Env
.int (evs
, "TTL")
589 val aliases
= Env
.env (Env
.list Env
.string) (evs
, "Aliases")
591 val path
= getPath dom
593 val () = (current
:= dom
;
594 currentsAli
:= Slave
.remove (Slave
.removeDups aliases
, dom
);
595 currentPath
:= (fn site
=> path (Config
.tmpDir
, site
));
596 currentPathAli
:= (fn (dom
, site
) => getPath
dom (Config
.tmpDir
, site
)))
598 fun saveSoa (kind
, soa
: soa
) node
=
600 val {write
, writeDom
, close
} = domainsFile
{node
= node
, name
= "soa.conf"}
604 write (Int.toString ttl
);
610 | SOME n
=> write (Int.toString n
);
612 write (Int.toString (#ref soa
));
614 write (Int.toString (#ret soa
));
616 write (Int.toString (#exp soa
));
618 write (Int.toString (#min soa
));
623 fun saveNamed (kind
, soa
: soa
, masterIp
, slaveIps
) node
=
624 if dom
= "localhost" then
627 val {write
, writeDom
, close
} = domainsFile
{node
= node
, name
= "named.conf"}
631 write
"\" {\n\ttype ";
633 write
";\n\tfile \"";
634 write Config
.Bind
.zonePath_real
;
639 "master" => (write
"\tallow-transfer {\n";
640 app (fn ip
=> (write
"\t\t";
642 write
";\n")) slaveIps
;
644 | _
=> (write
"\tmasters { ";
647 write
"// Updated: ";
648 write (Time
.toString (Time
.now ()));
655 NoDns
=> masterNode
:= NONE
660 InternalMaster node
=> nodeIp node
661 | ExternalMaster ip
=> ip
663 val slaveIps
= map
nodeIp (#slaves dns
)
665 app (saveNamed ("slave", #soa dns
, masterIp
, slaveIps
)) (#slaves dns
);
667 InternalMaster node
=>
668 (masterNode
:= SOME node
;
669 saveSoa ("master", #soa dns
) node
;
670 saveNamed ("master", #soa dns
, masterIp
, slaveIps
) node
)
671 | _
=> masterNode
:= NONE
675 fn () => !afters (!current
))
677 val () = Env
.registerPre (fn () => (seenDomains
:= [];
678 ignore (Slave
.shellF ([Config
.rm
, " -rf ", Config
.tmpDir
, ""],
679 fn cl
=> "Temp file cleanup failed: " ^ cl
));
680 OS
.FileSys
.mkDir Config
.tmpDir
;
681 app (fn node
=> OS
.FileSys
.mkDir
682 (OS
.Path
.joinDirFile
{dir
= Config
.tmpDir
,
685 app (fn node
=> OS
.FileSys
.mkDir
686 (OS
.Path
.joinDirFile
{dir
= Config
.resultRoot
,
688 handle OS
.SysErr _
=> ())
691 fun handleSite (site
, files
) =
695 print ("New configuration for node " ^ site ^
"\n");
696 if site
= Config
.dispatcherName
then
697 Slave
.handleChanges files
699 val bio
= OpenSSL
.connect
true (valOf (!ssl_context
),
702 ^
Int.toString Config
.slavePort
)
704 app (fn file
=> Msg
.send (bio
, MsgFile file
)) files
;
705 Msg
.send (bio
, MsgDoFiles
);
707 NONE
=> print
"Slave closed connection unexpectedly\n"
710 MsgOk
=> print ("Slave " ^ site ^
" finished\n")
711 | MsgError s
=> print ("Slave " ^ site
712 ^
" returned error: " ^
714 | _
=> print ("Slave " ^ site
715 ^
" returned unexpected command\n");
720 val () = Env
.registerPost (fn () =>
722 val prefixes
= List.concat
725 val pieces
= String.tokens (fn ch
=> ch
= #
".") dom
726 val path
= String.concatWith
"/" (rev pieces
)
729 Config
.resultRoot ^
"/" ^ node ^
"/" ^ path ^
"/")
733 val diffs
= findAllDiffs prefixes
735 val diffs
= map (fn (site
, dom
, dir
, Add
' {src
, dst
}) =>
736 (Slave
.shellF ([Config
.cp
, " ", src
, " ", dst
],
737 fn cl
=> "Copy failed: " ^ cl
);
743 |
(site
, dom
, dir
, Delete
' dst
) =>
744 (OS
.FileSys
.remove dst
745 handle OS
.SysErr _
=>
746 ErrorMsg
.error
NONE ("Delete failed for " ^ dst
);
748 {action
= Slave
.Delete
true,
752 |
(site
, dom
, dir
, Modify
' {src
, dst
}) =>
753 (Slave
.shellF ([Config
.cp
, " ", src
, " ", dst
],
754 fn cl
=> "Copy failed: " ^ cl
);
756 {action
= Slave
.Modify
,
761 if !ErrorMsg
.anyErrors
then
764 val changed
= foldl (fn ((site
, file
), changed
) =>
766 val ls
= case SM
.find (changed
, site
) of
770 SM
.insert (changed
, site
, file
:: ls
)
773 SM
.appi handleSite changed
775 ignore (Slave
.shellF ([Config
.rm
, " -rf ", Config
.tmpDir
, ""],
776 fn cl
=> "Temp file cleanup failed: " ^ cl
))
779 fun hasPriv priv
= Acl
.query
{user
= getUser (), class
= "priv", value
= "all"}
780 orelse Acl
.query
{user
= getUser (), class
= "priv", value
= priv
}
782 val _
= Env
.type_one
"dns_node"
785 List.exists (fn x
=> x
= node
) Config
.dnsNodes_all
786 orelse (hasPriv
"dns"
787 andalso List.exists (fn x
=> x
= node
) Config
.dnsNodes_admin
))
789 val _
= Env
.type_one
"mail_node"
792 List.exists (fn x
=> x
= node
) Config
.mailNodes_all
793 orelse (hasPriv
"mail"
794 andalso List.exists (fn x
=> x
= node
) Config
.mailNodes_admin
))
796 fun rmdom
' delete resultRoot doms
=
798 fun doNode (node
, _
, _
) =
800 val dname
= OS
.Path
.joinDirFile
{dir
= resultRoot
,
803 fun doDom (dom
, actions
) =
805 val domPath
= String.concatWith
"/" (rev (String.fields (fn ch
=> ch
= #
".") dom
))
806 val dname
= OS
.Path
.concat (dname
, domPath
)
808 fun visitDom (dom
, dname
, actions
) =
810 val dir
= Posix
.FileSys
.opendir dname
813 case Posix
.FileSys
.readdir dir
of
817 val fnameFull
= OS
.Path
.joinDirFile
{dir
= dname
,
820 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat fnameFull
) then
821 loop (visitDom (fname ^
"." ^ dom
,
825 loop ({action
= Slave
.Delete delete
,
828 file
= fnameFull
} :: actions
)
832 before Posix
.FileSys
.closedir dir
834 handle OS
.SysErr (s
, _
) =>
835 (print ("Warning: System error deleting domain " ^ dom ^
" on " ^ node ^
": " ^ s ^
"\n");
838 visitDom (dom
, dname
, actions
)
841 val actions
= foldl doDom
[] doms
843 handleSite (node
, actions
)
845 handle IO
.Io _
=> print ("Warning: IO error deleting domains on " ^ node ^
".\n")
847 fun cleanupNode (node
, _
, _
) =
851 val domPath
= String.concatWith
"/" (rev (String.fields (fn ch
=> ch
= #
".") dom
))
852 val dname
= OS
.Path
.joinDirFile
{dir
= resultRoot
,
854 val dname
= OS
.Path
.concat (dname
, domPath
)
857 ignore (OS
.Process
.system (Config
.rm ^
" -rf " ^ dname
))
865 app doNode Config
.nodeIps
;
866 app cleanupNode Config
.nodeIps
869 val rmdom
= rmdom
' true Config
.resultRoot
870 val rmdom
' = rmdom
' false
872 fun homedirOf uname
=
873 Posix
.SysDB
.Passwd
.home (Posix
.SysDB
.getpwnam uname
)
875 fun homedir () = homedirOf (getUser ())
876 handle e
=> if !fakePrivs
then "/tmp" else raise e
878 type subject
= {node
: string, domain
: string}
880 val describers
: (subject
-> string) list ref
= ref
[]
882 fun registerDescriber f
= describers
:= f
:: !describers
884 fun describeOne arg
= String.concat (map (fn f
=> f arg
) (rev (!describers
)))
886 val line
= "--------------------------------------------------------------\n"
887 val dline
= "==============================================================\n"
890 String.concat (List.mapPartial
892 case describeOne
{node
= node
, domain
= dom
} of
895 SOME (String.concat
[dline
, "Node ", node
, "\n", dline
, "\n", s
]))
898 datatype description
=
899 Filename
of { filename
: string, heading
: string, showEmpty
: bool }
900 | Extension
of { extension
: string, heading
: string -> string }
902 fun considerAll ds
{node
, domain
} =
904 val ds
= map (fn d
=> (d
, ref
[])) ds
906 val path
= Config
.resultRoot
907 val jdf
= OS
.Path
.joinDirFile
908 val path
= jdf
{dir
= path
, file
= node
}
909 val path
= foldr (fn (more
, path
) => jdf
{dir
= path
, file
= more
})
910 path (String.tokens (fn ch
=> ch
= #
".") domain
)
912 if Posix
.FileSys
.access (path
, []) then
914 val dir
= Posix
.FileSys
.opendir path
917 case Posix
.FileSys
.readdir dir
of
920 (app (fn (d
, entries
) =>
922 fun readFile showEmpty entries
' =
924 val fname
= OS
.Path
.joinDirFile
{dir
= path
,
927 val inf
= TextIO.openIn fname
929 fun loop (seenOne
, entries
') =
930 case TextIO.inputLine inf
of
931 NONE
=> if seenOne
orelse showEmpty
then
935 | SOME line
=> loop (true, line
:: entries
')
937 loop (false, entries
')
938 before TextIO.closeIn inf
942 Filename
{filename
, heading
, showEmpty
} =>
943 if fname
= filename
then
944 entries
:= readFile
showEmpty ("\n" :: line
:: "\n" :: heading
:: line
:: !entries
)
947 | Extension
{extension
, heading
} =>
949 val {base
, ext
} = OS
.Path
.splitBaseExt fname
954 if extension
' = extension
then
955 entries
:= readFile
true ("\n" :: line
:: "\n" :: heading base
:: line
:: !entries
)
963 Posix
.FileSys
.closedir dir
;
964 String.concat (List.concat (map (fn (_
, entries
) => rev (!entries
)) ds
))
970 val () = registerDescriber (considerAll
[Filename
{filename
= "soa.conf",
971 heading
= "DNS SOA:",
974 val () = Env
.registerAction ("domainHost",
975 fn (env
, [(EString host
, _
)]) =>
976 SM
.insert (env
, "Hostname",
977 (EString (host ^
"." ^
currentDomain ()), dl
))
978 |
(_
, args
) => Env
.badArgs ("domainHost", args
))
980 val ouc
= ref (fn () => ())
982 fun registerOnUsersChange f
=
986 ouc
:= (fn () => (f
' (); f ()))
989 fun onUsersChange () = !ouc ()