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
:= SS
.add (Acl
.class
{user
= getUser (),
76 your_pths
:= your_paths
;
77 readable_pths
:= SS
.union (your_paths
, world_readable
);
78 your_ipss
:= Acl
.class
{user
= getUser (),
82 fun declareClient () = isClient
:= true
83 fun fakePrivileges () = if !isClient
then
86 raise Fail
"Tried to fake privileges as non-client"
89 case map
Int.fromString (String.fields (fn ch
=> ch
= #
".") s
) of
90 [SOME n1
, SOME n2
, SOME n3
, SOME n4
] =>
91 n1
>= 0 andalso n1
< 256 andalso n2
>= 0 andalso n2
< 256 andalso n3
>= 0 andalso n3
< 256 andalso n4
>= 0 andalso n4
< 256
94 fun isHexDigit ch
= Char.isDigit ch
orelse (ord ch
>= ord #
"a" andalso ord ch
<= ord #
"f")
98 val fields
= String.fields (fn ch
=> ch
= #
":") s
100 val empties
= foldl (fn ("", n
) => n
+ 1
101 |
(_
, n
) => n
) 0 fields
105 andalso length fields
<= maxLen
107 andalso List.all (fn "" => true
109 andalso CharVector
.all isHexDigit s
) fields
115 val maybeIpv4
= List.last fields
116 val theRest
= List.take (fields
, length fields
- 1)
118 validIp maybeIpv4
andalso noIpv4
6
121 noIpv4
8 orelse hasIpv4 ()
124 fun isIdent ch
= Char.isLower ch
orelse Char.isDigit ch
127 size s
> 0 andalso size s
< 50
128 andalso CharVector
.all (fn ch
=> isIdent ch
orelse ch
= #
"-") s
131 size s
> 0 andalso size s
< 200
132 andalso List.all
validHost (String.fields (fn ch
=> ch
= #
".") s
)
134 fun validNode s
= List.exists (fn s
' => s
= s
') nodes
136 fun yourDomain s
= !fakePrivs
orelse SS
.member (your_domains (), s
)
137 fun yourUser s
= !fakePrivs
orelse SS
.member (your_users (), s
)
138 fun yourGroup s
= !fakePrivs
orelse SS
.member (your_groups (), s
)
140 fun checkPath paths path
=
142 (List.all (fn s
=> s
<> "..") (String.fields (fn ch
=> ch
= #
"/") path
)
143 andalso CharVector
.all (fn ch
=> Char.isAlphaNum ch
orelse ch
= #
"." orelse ch
= #
"/"
144 orelse ch
= #
"-" orelse ch
= #
"_") path
145 andalso SS
.exists (fn s
' => path
= s
' orelse String.isPrefix (s
' ^
"/") path
) (paths ()))
146 val yourPath
= checkPath your_paths
147 val readablePath
= checkPath readable_paths
149 fun yourIp s
= !fakePrivs
orelse SS
.member (your_ips (), s
)
151 fun yourDomainHost s
=
155 val (pref
, suf
) = Substring
.splitl (fn ch
=> ch
<> #
".") (Substring
.full s
)
157 Substring
.size suf
> 0
158 andalso validHost (Substring
.string pref
)
159 andalso yourDomain (Substring
.string
160 (Substring
.slice (suf
, 1, NONE
)))
163 val yourDomain
= yourDomainHost
165 fun validUser s
= size s
> 0 andalso size s
< 20
166 andalso CharVector
.all
Char.isAlphaNum s
168 fun validEmailUser s
=
169 size s
> 0 andalso size s
< 50
170 andalso CharVector
.all (fn ch
=> Char.isAlphaNum ch
176 val validGroup
= validUser
178 val _
= Env
.type_one
"no_spaces"
180 (CharVector
.all (fn ch
=> Char.isPrint ch
andalso not (Char.isSpace ch
)
181 andalso ch
<> #
"\"" andalso ch
<> #
"'"))
182 val _
= Env
.type_one
"no_newlines"
184 (CharVector
.all (fn ch
=> Char.isPrint ch
andalso ch
<> #
"\n" andalso ch
<> #
"\r"
185 andalso ch
<> #
"\""))
187 val _
= Env
.type_one
"ip"
191 val _
= Env
.type_one
"ipv6"
195 val _
= Env
.type_one
"host"
199 val _
= Env
.type_one
"domain"
203 val _
= Env
.type_one
"your_domain"
207 val _
= Env
.type_one
"your_domain_host"
211 val _
= Env
.type_one
"user"
215 val _
= Env
.type_one
"group"
219 val _
= Env
.type_one
"your_user"
223 val _
= Env
.type_one
"your_group"
227 val _
= Env
.type_one
"your_path"
231 val _
= Env
.type_one
"readable_path"
235 val _
= Env
.type_one
"your_ip"
239 val _
= Env
.type_one
"node"
243 val _
= Env
.type_one
"mime_type"
245 (CharVector
.exists (fn ch
=> ch
= #
"/"))
247 val _
= Env
.registerFunction ("your_ip_to_ip",
251 val _
= Env
.registerFunction ("dns_node_to_node",
255 val _
= Env
.registerFunction ("mail_node_to_node",
262 val dl
= ErrorMsg
.dummyLoc
264 val _
= Env
.registerFunction ("end_in_slash",
265 fn [(EString
"", _
)] => SOME (EString
"/", dl
)
266 |
[(EString s
, _
)] =>
267 SOME (EString (if String.sub (s
, size s
- 1) = #
"/" then
274 val _
= Env
.registerFunction ("you",
275 fn [] => SOME (EString (getUser ()), dl
)
278 val _
= Env
.registerFunction ("defaultMailbox",
279 fn [] => SOME (EString (getUser ()), dl
)
282 val _
= Env
.registerFunction ("defaultMailUser",
283 fn [] => SOME (EString (getUser ()), dl
)
287 type soa
= {ns
: string,
294 val serial
= fn (EVar
"serialAuto", _
) => SOME NONE
295 |
(EApp ((EVar
"serialConst", _
), n
), _
) => Option
.map
SOME (Env
.int n
)
298 val soa
= fn (EApp ((EApp ((EApp ((EApp ((EApp ((EApp
299 ((EVar
"soa", _
), ns
), _
),
305 (case (Env
.string ns
, serial sl
, Env
.int rf
,
306 Env
.int ret
, Env
.int exp
, Env
.int min
) of
307 (SOME ns
, SOME sl
, SOME rf
,
308 SOME ret
, SOME exp
, SOME min
) =>
319 ExternalMaster
of string
320 | InternalMaster
of string
324 val _
= Env
.registerFunction ("ip_of_node",
325 fn [(EString node
, _
)] => SOME (EString (nodeIp node
), dl
)
328 val master
= fn (EApp ((EVar
"externalMaster", _
), e
), _
) => Option
.map
ExternalMaster (ip e
)
329 |
(EApp ((EVar
"internalMaster", _
), e
), _
) => Option
.map
InternalMaster (Env
.string e
)
333 UseDns
of {soa
: soa
,
335 slaves
: string list
}
338 val dnsKind
= fn (EApp ((EApp ((EApp
339 ((EVar
"useDns", _
), sa
), _
),
342 (case (soa sa
, master mstr
, Env
.list Env
.string slaves
) of
343 (SOME sa
, SOME mstr
, SOME slaves
) =>
344 SOME (UseDns
{soa
= sa
,
348 |
(EVar
"noDns", _
) => SOME NoDns
351 val befores
= ref (fn (_
: string) => ())
352 val afters
= ref (fn (_
: string) => ())
354 fun registerBefore f
=
358 befores
:= (fn x
=> (old x
; f x
))
361 fun registerAfter f
=
365 afters
:= (fn x
=> (old x
; f x
))
368 val globals
= ref (fn () => ())
369 val locals
= ref (fn () => ())
371 fun registerResetGlobal f
=
375 globals
:= (fn x
=> (old x
; f x
))
378 fun registerResetLocal f
=
382 locals
:= (fn x
=> (old x
; f x
))
385 fun resetGlobal () = (!globals ();
386 ignore (OS
.Process
.system (Config
.rm ^
" -rf " ^ Config
.resultRoot ^
"/*")))
387 fun resetLocal () = !locals ()
390 val currentPath
= ref (fn (_
: string) => "")
391 val currentPathAli
= ref (fn (_
: string, _
: string) => "")
395 fun currentDomain () = !current
397 val currentsAli
= ref ([] : string list
)
399 fun currentAliasDomains () = !currentsAli
400 fun currentDomains () = currentDomain () :: currentAliasDomains ()
402 fun domainFile
{node
, name
} = ((*print ("Opening " ^
!currentPath node ^ name ^
"\n");*)
403 TextIO.openOut (!currentPath node ^ name
))
405 type files
= {write
: string -> unit
,
406 writeDom
: unit
-> unit
,
407 close
: unit
-> unit
}
409 fun domainsFile
{node
, name
} =
411 val doms
= currentDomains ()
412 val files
= map (fn dom
=> (dom
, TextIO.openOut (!currentPathAli (dom
, node
) ^ name
))) doms
414 {write
= fn s
=> app (fn (_
, outf
) => TextIO.output (outf
, s
)) files
,
415 writeDom
= fn () => app (fn (dom
, outf
) => TextIO.output (outf
, dom
)) files
,
416 close
= fn () => app (fn (_
, outf
) => TextIO.closeOut outf
) files
}
421 val toks
= String.fields (fn ch
=> ch
= #
".") domain
423 val elems
= foldr (fn (piece
, elems
) =>
425 val elems
= piece
:: elems
429 val path
= String.concatWith
"/"
430 (Config
.resultRoot
:: node
:: rev elems
)
431 val tmpPath
= String.concatWith
"/"
432 (Config
.tmpDir
:: node
:: rev elems
)
434 (if Posix
.FileSys
.ST
.isDir
435 (Posix
.FileSys
.stat path
) then
438 (OS
.FileSys
.remove path
;
439 OS
.FileSys
.mkDir path
))
440 handle OS
.SysErr _
=> OS
.FileSys
.mkDir path
;
442 (if Posix
.FileSys
.ST
.isDir
443 (Posix
.FileSys
.stat tmpPath
) then
446 (OS
.FileSys
.remove tmpPath
;
447 OS
.FileSys
.mkDir tmpPath
))
448 handle OS
.SysErr _
=> OS
.FileSys
.mkDir tmpPath
455 fn (root
, site
) => String.concatWith
"/" (root
:: site
:: rev ("" :: elems
))
458 datatype file_action
' =
459 Add
' of {src
: string, dst
: string}
461 | Modify
' of {src
: string, dst
: string}
463 fun findDiffs (prefixes
, site
, dom
, acts
) =
466 val realPath
= gp (Config
.resultRoot
, site
)
467 val tmpPath
= gp (Config
.tmpDir
, site
)
469 (*val _
= print ("getDiffs(" ^ site ^
", " ^ dom ^
")... " ^ realPath ^
"; " ^ tmpPath ^
"\n")*)
471 val dir
= Posix
.FileSys
.opendir realPath
474 case Posix
.FileSys
.readdir dir
of
475 NONE
=> (Posix
.FileSys
.closedir dir
;
479 val real = OS
.Path
.joinDirFile
{dir
= realPath
,
481 val tmp
= OS
.Path
.joinDirFile
{dir
= tmpPath
,
484 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat
real) then
486 else if Posix
.FileSys
.access (tmp
, []) then
487 if Slave
.shell
[Config
.diff
, " ", real, " ", tmp
] then
490 loopReal ((site
, dom
, realPath
, Modify
' {src
= tmp
, dst
= real}) :: acts
)
491 else if List.exists (fn prefix
=> String.isPrefix prefix
real) prefixes
then
492 loopReal ((site
, dom
, realPath
, Delete
' real) :: acts
)
497 val acts
= loopReal acts
499 val dir
= Posix
.FileSys
.opendir tmpPath
502 case Posix
.FileSys
.readdir dir
of
503 NONE
=> (Posix
.FileSys
.closedir dir
;
507 val real = OS
.Path
.joinDirFile
{dir
= realPath
,
509 val tmp
= OS
.Path
.joinDirFile
{dir
= tmpPath
,
512 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat tmp
) then
514 else if Posix
.FileSys
.access (real, []) then
517 loopTmp ((site
, dom
, realPath
, Add
' {src
= tmp
, dst
= real}) :: acts
)
520 val acts
= loopTmp acts
525 fun findAllDiffs prefixes
=
527 val dir
= Posix
.FileSys
.opendir Config
.tmpDir
528 val len
= length (String.fields (fn ch
=> ch
= #
"/") Config
.tmpDir
) + 1
530 fun exploreSites diffs
=
531 case Posix
.FileSys
.readdir dir
of
535 fun explore (dname
, diffs
) =
537 val dir
= Posix
.FileSys
.opendir dname
540 case Posix
.FileSys
.readdir dir
of
544 val fname
= OS
.Path
.joinDirFile
{dir
= dname
,
547 loop (if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat fname
) then
549 val dom
= String.fields (fn ch
=> ch
= #
"/") fname
550 val dom
= List.drop (dom
, len
)
551 val dom
= String.concatWith
"." (rev dom
)
553 val dname
' = OS
.Path
.joinDirFile
{dir
= dname
,
557 findDiffs (prefixes
, site
, dom
, diffs
))
564 before Posix
.FileSys
.closedir dir
567 exploreSites (explore (OS
.Path
.joinDirFile
{dir
= Config
.tmpDir
,
568 file
= site
}, diffs
))
572 before Posix
.FileSys
.closedir dir
575 val masterNode
: string option ref
= ref NONE
576 fun dnsMaster () = !masterNode
578 val seenDomains
: string list ref
= ref
[]
580 val _
= Env
.containerV_one
"domain"
581 ("domain", Env
.string)
584 val () = seenDomains
:= dom
:: !seenDomains
586 val kind
= Env
.env
dnsKind (evs
, "DNS")
587 val ttl
= Env
.env Env
.int (evs
, "TTL")
588 val aliases
= Env
.env (Env
.list Env
.string) (evs
, "Aliases")
590 val path
= getPath dom
592 val () = (current
:= dom
;
593 currentsAli
:= Slave
.remove (Slave
.removeDups aliases
, dom
);
594 currentPath
:= (fn site
=> path (Config
.tmpDir
, site
));
595 currentPathAli
:= (fn (dom
, site
) => getPath
dom (Config
.tmpDir
, site
)))
597 fun saveSoa (kind
, soa
: soa
) node
=
599 val {write
, writeDom
, close
} = domainsFile
{node
= node
, name
= "soa.conf"}
603 write (Int.toString ttl
);
609 | SOME n
=> write (Int.toString n
);
611 write (Int.toString (#ref soa
));
613 write (Int.toString (#ret soa
));
615 write (Int.toString (#exp soa
));
617 write (Int.toString (#min soa
));
622 fun saveNamed (kind
, soa
: soa
, masterIp
, slaveIps
) node
=
623 if dom
= "localhost" then
626 val {write
, writeDom
, close
} = domainsFile
{node
= node
, name
= "named.conf"}
630 write
"\" {\n\ttype ";
632 write
";\n\tfile \"";
633 write Config
.Bind
.zonePath_real
;
638 "master" => (write
"\tallow-transfer {\n";
639 app (fn ip
=> (write
"\t\t";
641 write
";\n")) slaveIps
;
643 | _
=> (write
"\tmasters { ";
646 write
"// Updated: ";
647 write (Time
.toString (Time
.now ()));
654 NoDns
=> masterNode
:= NONE
659 InternalMaster node
=> nodeIp node
660 | ExternalMaster ip
=> ip
662 val slaveIps
= map
nodeIp (#slaves dns
)
664 app (saveNamed ("slave", #soa dns
, masterIp
, slaveIps
)) (#slaves dns
);
666 InternalMaster node
=>
667 (masterNode
:= SOME node
;
668 saveSoa ("master", #soa dns
) node
;
669 saveNamed ("master", #soa dns
, masterIp
, slaveIps
) node
)
670 | _
=> masterNode
:= NONE
674 fn () => !afters (!current
))
676 val () = Env
.registerPre (fn () => (seenDomains
:= [];
677 ignore (Slave
.shellF ([Config
.rm
, " -rf ", Config
.tmpDir
, ""],
678 fn cl
=> "Temp file cleanup failed: " ^ cl
));
679 OS
.FileSys
.mkDir Config
.tmpDir
;
680 app (fn node
=> OS
.FileSys
.mkDir
681 (OS
.Path
.joinDirFile
{dir
= Config
.tmpDir
,
684 app (fn node
=> OS
.FileSys
.mkDir
685 (OS
.Path
.joinDirFile
{dir
= Config
.resultRoot
,
687 handle OS
.SysErr _
=> ())
690 fun handleSite (site
, files
) =
694 print ("New configuration for node " ^ site ^
"\n");
695 if site
= Config
.dispatcherName
then
696 Slave
.handleChanges files
698 val bio
= OpenSSL
.connect
true (valOf (!ssl_context
),
701 ^
Int.toString Config
.slavePort
)
703 app (fn file
=> Msg
.send (bio
, MsgFile file
)) files
;
704 Msg
.send (bio
, MsgDoFiles
);
706 NONE
=> print
"Slave closed connection unexpectedly\n"
709 MsgOk
=> print ("Slave " ^ site ^
" finished\n")
710 | MsgError s
=> print ("Slave " ^ site
711 ^
" returned error: " ^
713 | _
=> print ("Slave " ^ site
714 ^
" returned unexpected command\n");
719 val () = Env
.registerPost (fn () =>
721 val prefixes
= List.concat
724 val pieces
= String.tokens (fn ch
=> ch
= #
".") dom
725 val path
= String.concatWith
"/" (rev pieces
)
728 Config
.resultRoot ^
"/" ^ node ^
"/" ^ path ^
"/")
732 val diffs
= findAllDiffs prefixes
734 val diffs
= map (fn (site
, dom
, dir
, Add
' {src
, dst
}) =>
735 (Slave
.shellF ([Config
.cp
, " ", src
, " ", dst
],
736 fn cl
=> "Copy failed: " ^ cl
);
742 |
(site
, dom
, dir
, Delete
' dst
) =>
743 (OS
.FileSys
.remove dst
744 handle OS
.SysErr _
=>
745 ErrorMsg
.error
NONE ("Delete failed for " ^ dst
);
747 {action
= Slave
.Delete
true,
751 |
(site
, dom
, dir
, Modify
' {src
, dst
}) =>
752 (Slave
.shellF ([Config
.cp
, " ", src
, " ", dst
],
753 fn cl
=> "Copy failed: " ^ cl
);
755 {action
= Slave
.Modify
,
760 if !ErrorMsg
.anyErrors
then
763 val changed
= foldl (fn ((site
, file
), changed
) =>
765 val ls
= case SM
.find (changed
, site
) of
769 SM
.insert (changed
, site
, file
:: ls
)
772 SM
.appi handleSite changed
774 ignore (Slave
.shellF ([Config
.rm
, " -rf ", Config
.tmpDir
, ""],
775 fn cl
=> "Temp file cleanup failed: " ^ cl
))
778 fun hasPriv priv
= Acl
.query
{user
= getUser (), class
= "priv", value
= "all"}
779 orelse Acl
.query
{user
= getUser (), class
= "priv", value
= priv
}
781 val _
= Env
.type_one
"dns_node"
784 List.exists (fn x
=> x
= node
) Config
.dnsNodes_all
785 orelse (hasPriv
"dns"
786 andalso List.exists (fn x
=> x
= node
) Config
.dnsNodes_admin
))
788 val _
= Env
.type_one
"mail_node"
791 List.exists (fn x
=> x
= node
) Config
.mailNodes_all
792 orelse (hasPriv
"mail"
793 andalso List.exists (fn x
=> x
= node
) Config
.mailNodes_admin
))
795 fun rmdom
' delete resultRoot doms
=
797 fun doNode (node
, _
) =
799 val dname
= OS
.Path
.joinDirFile
{dir
= resultRoot
,
802 fun doDom (dom
, actions
) =
804 val domPath
= String.concatWith
"/" (rev (String.fields (fn ch
=> ch
= #
".") dom
))
805 val dname
= OS
.Path
.concat (dname
, domPath
)
807 fun visitDom (dom
, dname
, actions
) =
809 val dir
= Posix
.FileSys
.opendir dname
812 case Posix
.FileSys
.readdir dir
of
816 val fnameFull
= OS
.Path
.joinDirFile
{dir
= dname
,
819 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat fnameFull
) then
820 loop (visitDom (fname ^
"." ^ dom
,
824 loop ({action
= Slave
.Delete delete
,
827 file
= fnameFull
} :: actions
)
831 before Posix
.FileSys
.closedir dir
833 handle OS
.SysErr (s
, _
) =>
834 (print ("Warning: System error deleting domain " ^ dom ^
" on " ^ node ^
": " ^ s ^
"\n");
837 visitDom (dom
, dname
, actions
)
840 val actions
= foldl doDom
[] doms
842 handleSite (node
, actions
)
844 handle IO
.Io _
=> print ("Warning: IO error deleting domains on " ^ node ^
".\n")
846 fun cleanupNode (node
, _
) =
850 val domPath
= String.concatWith
"/" (rev (String.fields (fn ch
=> ch
= #
".") dom
))
851 val dname
= OS
.Path
.joinDirFile
{dir
= resultRoot
,
853 val dname
= OS
.Path
.concat (dname
, domPath
)
856 ignore (OS
.Process
.system (Config
.rm ^
" -rf " ^ dname
))
864 app doNode Config
.nodeIps
;
865 app cleanupNode Config
.nodeIps
868 val rmdom
= rmdom
' true Config
.resultRoot
869 val rmdom
' = rmdom
' false
871 fun homedirOf uname
=
872 Posix
.SysDB
.Passwd
.home (Posix
.SysDB
.getpwnam uname
)
874 fun homedir () = homedirOf (getUser ())
875 handle e
=> if !fakePrivs
then "/tmp" else raise e
877 type subject
= {node
: string, domain
: string}
879 val describers
: (subject
-> string) list ref
= ref
[]
881 fun registerDescriber f
= describers
:= f
:: !describers
883 fun describeOne arg
= String.concat (map (fn f
=> f arg
) (rev (!describers
)))
885 val line
= "--------------------------------------------------------------\n"
886 val dline
= "==============================================================\n"
889 String.concat (List.mapPartial
891 case describeOne
{node
= node
, domain
= dom
} of
894 SOME (String.concat
[dline
, "Node ", node
, "\n", dline
, "\n", s
]))
897 datatype description
=
898 Filename
of { filename
: string, heading
: string, showEmpty
: bool }
899 | Extension
of { extension
: string, heading
: string -> string }
901 fun considerAll ds
{node
, domain
} =
903 val ds
= map (fn d
=> (d
, ref
[])) ds
905 val path
= Config
.resultRoot
906 val jdf
= OS
.Path
.joinDirFile
907 val path
= jdf
{dir
= path
, file
= node
}
908 val path
= foldr (fn (more
, path
) => jdf
{dir
= path
, file
= more
})
909 path (String.tokens (fn ch
=> ch
= #
".") domain
)
911 if Posix
.FileSys
.access (path
, []) then
913 val dir
= Posix
.FileSys
.opendir path
916 case Posix
.FileSys
.readdir dir
of
919 (app (fn (d
, entries
) =>
921 fun readFile showEmpty entries
' =
923 val fname
= OS
.Path
.joinDirFile
{dir
= path
,
926 val inf
= TextIO.openIn fname
928 fun loop (seenOne
, entries
') =
929 case TextIO.inputLine inf
of
930 NONE
=> if seenOne
orelse showEmpty
then
934 | SOME line
=> loop (true, line
:: entries
')
936 loop (false, entries
')
937 before TextIO.closeIn inf
941 Filename
{filename
, heading
, showEmpty
} =>
942 if fname
= filename
then
943 entries
:= readFile
showEmpty ("\n" :: line
:: "\n" :: heading
:: line
:: !entries
)
946 | Extension
{extension
, heading
} =>
948 val {base
, ext
} = OS
.Path
.splitBaseExt fname
953 if extension
' = extension
then
954 entries
:= readFile
true ("\n" :: line
:: "\n" :: heading base
:: line
:: !entries
)
962 Posix
.FileSys
.closedir dir
;
963 String.concat (List.concat (map (fn (_
, entries
) => rev (!entries
)) ds
))
969 val () = registerDescriber (considerAll
[Filename
{filename
= "soa.conf",
970 heading
= "DNS SOA:",
973 val () = Env
.registerAction ("domainHost",
974 fn (env
, [(EString host
, _
)]) =>
975 SM
.insert (env
, "Hostname",
976 (EString (host ^
"." ^
currentDomain ()), dl
))
977 |
(_
, args
) => Env
.badArgs ("domainHost", args
))
979 val ouc
= ref (fn () => ())
981 fun registerOnUsersChange f
=
985 ouc
:= (fn () => (f
' (); f ()))
988 fun onUsersChange () = !ouc ()