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
)
283 type soa
= {ns
: string,
290 val serial
= fn (EVar
"serialAuto", _
) => SOME NONE
291 |
(EApp ((EVar
"serialConst", _
), n
), _
) => Option
.map
SOME (Env
.int n
)
294 val soa
= fn (EApp ((EApp ((EApp ((EApp ((EApp ((EApp
295 ((EVar
"soa", _
), ns
), _
),
301 (case (Env
.string ns
, serial sl
, Env
.int rf
,
302 Env
.int ret
, Env
.int exp
, Env
.int min
) of
303 (SOME ns
, SOME sl
, SOME rf
,
304 SOME ret
, SOME exp
, SOME min
) =>
315 ExternalMaster
of string
316 | InternalMaster
of string
320 val _
= Env
.registerFunction ("ip_of_node",
321 fn [(EString node
, _
)] => SOME (EString (nodeIp node
), dl
)
324 val master
= fn (EApp ((EVar
"externalMaster", _
), e
), _
) => Option
.map
ExternalMaster (ip e
)
325 |
(EApp ((EVar
"internalMaster", _
), e
), _
) => Option
.map
InternalMaster (Env
.string e
)
329 UseDns
of {soa
: soa
,
331 slaves
: string list
}
334 val dnsKind
= fn (EApp ((EApp ((EApp
335 ((EVar
"useDns", _
), sa
), _
),
338 (case (soa sa
, master mstr
, Env
.list Env
.string slaves
) of
339 (SOME sa
, SOME mstr
, SOME slaves
) =>
340 SOME (UseDns
{soa
= sa
,
344 |
(EVar
"noDns", _
) => SOME NoDns
347 val befores
= ref (fn (_
: string) => ())
348 val afters
= ref (fn (_
: string) => ())
350 fun registerBefore f
=
354 befores
:= (fn x
=> (old x
; f x
))
357 fun registerAfter f
=
361 afters
:= (fn x
=> (old x
; f x
))
364 val globals
= ref (fn () => ())
365 val locals
= ref (fn () => ())
367 fun registerResetGlobal f
=
371 globals
:= (fn x
=> (old x
; f x
))
374 fun registerResetLocal f
=
378 locals
:= (fn x
=> (old x
; f x
))
381 fun resetGlobal () = (!globals ();
382 ignore (OS
.Process
.system (Config
.rm ^
" -rf " ^ Config
.resultRoot ^
"/*")))
383 fun resetLocal () = !locals ()
386 val currentPath
= ref (fn (_
: string) => "")
387 val currentPathAli
= ref (fn (_
: string, _
: string) => "")
391 fun currentDomain () = !current
393 val currentsAli
= ref ([] : string list
)
395 fun currentAliasDomains () = !currentsAli
396 fun currentDomains () = currentDomain () :: currentAliasDomains ()
398 fun domainFile
{node
, name
} = ((*print ("Opening " ^
!currentPath node ^ name ^
"\n");*)
399 TextIO.openOut (!currentPath node ^ name
))
401 type files
= {write
: string -> unit
,
402 writeDom
: unit
-> unit
,
403 close
: unit
-> unit
}
405 fun domainsFile
{node
, name
} =
407 val doms
= currentDomains ()
408 val files
= map (fn dom
=> (dom
, TextIO.openOut (!currentPathAli (dom
, node
) ^ name
))) doms
410 {write
= fn s
=> app (fn (_
, outf
) => TextIO.output (outf
, s
)) files
,
411 writeDom
= fn () => app (fn (dom
, outf
) => TextIO.output (outf
, dom
)) files
,
412 close
= fn () => app (fn (_
, outf
) => TextIO.closeOut outf
) files
}
417 val toks
= String.fields (fn ch
=> ch
= #
".") domain
419 val elems
= foldr (fn (piece
, elems
) =>
421 val elems
= piece
:: elems
425 val path
= String.concatWith
"/"
426 (Config
.resultRoot
:: node
:: rev elems
)
427 val tmpPath
= String.concatWith
"/"
428 (Config
.tmpDir
:: node
:: rev elems
)
430 (if Posix
.FileSys
.ST
.isDir
431 (Posix
.FileSys
.stat path
) then
434 (OS
.FileSys
.remove path
;
435 OS
.FileSys
.mkDir path
))
436 handle OS
.SysErr _
=> OS
.FileSys
.mkDir path
;
438 (if Posix
.FileSys
.ST
.isDir
439 (Posix
.FileSys
.stat tmpPath
) then
442 (OS
.FileSys
.remove tmpPath
;
443 OS
.FileSys
.mkDir tmpPath
))
444 handle OS
.SysErr _
=> OS
.FileSys
.mkDir tmpPath
451 fn (root
, site
) => String.concatWith
"/" (root
:: site
:: rev ("" :: elems
))
454 datatype file_action
' =
455 Add
' of {src
: string, dst
: string}
457 | Modify
' of {src
: string, dst
: string}
459 fun findDiffs (prefixes
, site
, dom
, acts
) =
462 val realPath
= gp (Config
.resultRoot
, site
)
463 val tmpPath
= gp (Config
.tmpDir
, site
)
465 (*val _
= print ("getDiffs(" ^ site ^
", " ^ dom ^
")... " ^ realPath ^
"; " ^ tmpPath ^
"\n")*)
467 val dir
= Posix
.FileSys
.opendir realPath
470 case Posix
.FileSys
.readdir dir
of
471 NONE
=> (Posix
.FileSys
.closedir dir
;
475 val real = OS
.Path
.joinDirFile
{dir
= realPath
,
477 val tmp
= OS
.Path
.joinDirFile
{dir
= tmpPath
,
480 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat
real) then
482 else if Posix
.FileSys
.access (tmp
, []) then
483 if Slave
.shell
[Config
.diff
, " ", real, " ", tmp
] then
486 loopReal ((site
, dom
, realPath
, Modify
' {src
= tmp
, dst
= real}) :: acts
)
487 else if List.exists (fn prefix
=> String.isPrefix prefix
real) prefixes
then
488 loopReal ((site
, dom
, realPath
, Delete
' real) :: acts
)
493 val acts
= loopReal acts
495 val dir
= Posix
.FileSys
.opendir tmpPath
498 case Posix
.FileSys
.readdir dir
of
499 NONE
=> (Posix
.FileSys
.closedir dir
;
503 val real = OS
.Path
.joinDirFile
{dir
= realPath
,
505 val tmp
= OS
.Path
.joinDirFile
{dir
= tmpPath
,
508 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat tmp
) then
510 else if Posix
.FileSys
.access (real, []) then
513 loopTmp ((site
, dom
, realPath
, Add
' {src
= tmp
, dst
= real}) :: acts
)
516 val acts
= loopTmp acts
521 fun findAllDiffs prefixes
=
523 val dir
= Posix
.FileSys
.opendir Config
.tmpDir
524 val len
= length (String.fields (fn ch
=> ch
= #
"/") Config
.tmpDir
) + 1
526 fun exploreSites diffs
=
527 case Posix
.FileSys
.readdir dir
of
531 fun explore (dname
, diffs
) =
533 val dir
= Posix
.FileSys
.opendir dname
536 case Posix
.FileSys
.readdir dir
of
540 val fname
= OS
.Path
.joinDirFile
{dir
= dname
,
543 loop (if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat fname
) then
545 val dom
= String.fields (fn ch
=> ch
= #
"/") fname
546 val dom
= List.drop (dom
, len
)
547 val dom
= String.concatWith
"." (rev dom
)
549 val dname
' = OS
.Path
.joinDirFile
{dir
= dname
,
553 findDiffs (prefixes
, site
, dom
, diffs
))
560 before Posix
.FileSys
.closedir dir
563 exploreSites (explore (OS
.Path
.joinDirFile
{dir
= Config
.tmpDir
,
564 file
= site
}, diffs
))
568 before Posix
.FileSys
.closedir dir
571 val masterNode
: string option ref
= ref NONE
572 fun dnsMaster () = !masterNode
574 val seenDomains
: string list ref
= ref
[]
576 val _
= Env
.containerV_one
"domain"
577 ("domain", Env
.string)
580 val () = seenDomains
:= dom
:: !seenDomains
582 val kind
= Env
.env
dnsKind (evs
, "DNS")
583 val ttl
= Env
.env Env
.int (evs
, "TTL")
584 val aliases
= Env
.env (Env
.list Env
.string) (evs
, "Aliases")
586 val path
= getPath dom
588 val () = (current
:= dom
;
589 currentsAli
:= Slave
.remove (Slave
.removeDups aliases
, dom
);
590 currentPath
:= (fn site
=> path (Config
.tmpDir
, site
));
591 currentPathAli
:= (fn (dom
, site
) => getPath
dom (Config
.tmpDir
, site
)))
593 fun saveSoa (kind
, soa
: soa
) node
=
595 val {write
, writeDom
, close
} = domainsFile
{node
= node
, name
= "soa.conf"}
599 write (Int.toString ttl
);
605 | SOME n
=> write (Int.toString n
);
607 write (Int.toString (#ref soa
));
609 write (Int.toString (#ret soa
));
611 write (Int.toString (#exp soa
));
613 write (Int.toString (#min soa
));
618 fun saveNamed (kind
, soa
: soa
, masterIp
, slaveIps
) node
=
619 if dom
= "localhost" then
622 val {write
, writeDom
, close
} = domainsFile
{node
= node
, name
= "named.conf"}
626 write
"\" {\n\ttype ";
628 write
";\n\tfile \"";
629 write Config
.Bind
.zonePath_real
;
634 "master" => (write
"\tallow-transfer {\n";
635 app (fn ip
=> (write
"\t\t";
637 write
";\n")) slaveIps
;
639 | _
=> (write
"\tmasters { ";
642 write
"// Updated: ";
643 write (Time
.toString (Time
.now ()));
650 NoDns
=> masterNode
:= NONE
655 InternalMaster node
=> nodeIp node
656 | ExternalMaster ip
=> ip
658 val slaveIps
= map
nodeIp (#slaves dns
)
660 app (saveSoa ("slave", #soa dns
)) (#slaves dns
);
661 app (saveNamed ("slave", #soa dns
, masterIp
, slaveIps
)) (#slaves dns
);
663 InternalMaster node
=>
664 (masterNode
:= SOME node
;
665 saveSoa ("master", #soa dns
) node
;
666 saveNamed ("master", #soa dns
, masterIp
, slaveIps
) node
)
667 | _
=> masterNode
:= NONE
671 fn () => !afters (!current
))
673 val () = Env
.registerPre (fn () => (seenDomains
:= [];
674 ignore (Slave
.shellF ([Config
.rm
, " -rf ", Config
.tmpDir
, ""],
675 fn cl
=> "Temp file cleanup failed: " ^ cl
));
676 OS
.FileSys
.mkDir Config
.tmpDir
;
677 app (fn node
=> OS
.FileSys
.mkDir
678 (OS
.Path
.joinDirFile
{dir
= Config
.tmpDir
,
681 app (fn node
=> OS
.FileSys
.mkDir
682 (OS
.Path
.joinDirFile
{dir
= Config
.resultRoot
,
684 handle OS
.SysErr _
=> ())
687 fun handleSite (site
, files
) =
691 print ("New configuration for node " ^ site ^
"\n");
692 if site
= Config
.dispatcherName
then
693 Slave
.handleChanges files
695 val bio
= OpenSSL
.connect
true (valOf (!ssl_context
),
698 ^
Int.toString Config
.slavePort
)
700 app (fn file
=> Msg
.send (bio
, MsgFile file
)) files
;
701 Msg
.send (bio
, MsgDoFiles
);
703 NONE
=> print
"Slave closed connection unexpectedly\n"
706 MsgOk
=> print ("Slave " ^ site ^
" finished\n")
707 | MsgError s
=> print ("Slave " ^ site
708 ^
" returned error: " ^
710 | _
=> print ("Slave " ^ site
711 ^
" returned unexpected command\n");
716 val () = Env
.registerPost (fn () =>
718 val prefixes
= List.concat
721 val pieces
= String.tokens (fn ch
=> ch
= #
".") dom
722 val path
= String.concatWith
"/" (rev pieces
)
725 Config
.resultRoot ^
"/" ^ node ^
"/" ^ path ^
"/")
729 val diffs
= findAllDiffs prefixes
731 val diffs
= map (fn (site
, dom
, dir
, Add
' {src
, dst
}) =>
732 (Slave
.shellF ([Config
.cp
, " ", src
, " ", dst
],
733 fn cl
=> "Copy failed: " ^ cl
);
739 |
(site
, dom
, dir
, Delete
' dst
) =>
740 (OS
.FileSys
.remove dst
741 handle OS
.SysErr _
=>
742 ErrorMsg
.error
NONE ("Delete failed for " ^ dst
);
744 {action
= Slave
.Delete
true,
748 |
(site
, dom
, dir
, Modify
' {src
, dst
}) =>
749 (Slave
.shellF ([Config
.cp
, " ", src
, " ", dst
],
750 fn cl
=> "Copy failed: " ^ cl
);
752 {action
= Slave
.Modify
,
757 if !ErrorMsg
.anyErrors
then
760 val changed
= foldl (fn ((site
, file
), changed
) =>
762 val ls
= case SM
.find (changed
, site
) of
766 SM
.insert (changed
, site
, file
:: ls
)
769 SM
.appi handleSite changed
771 ignore (Slave
.shellF ([Config
.rm
, " -rf ", Config
.tmpDir
, ""],
772 fn cl
=> "Temp file cleanup failed: " ^ cl
))
775 fun hasPriv priv
= Acl
.query
{user
= getUser (), class
= "priv", value
= "all"}
776 orelse Acl
.query
{user
= getUser (), class
= "priv", value
= priv
}
778 val _
= Env
.type_one
"dns_node"
781 List.exists (fn x
=> x
= node
) Config
.dnsNodes_all
782 orelse (hasPriv
"dns"
783 andalso List.exists (fn x
=> x
= node
) Config
.dnsNodes_admin
))
785 val _
= Env
.type_one
"mail_node"
788 List.exists (fn x
=> x
= node
) Config
.mailNodes_all
789 orelse (hasPriv
"mail"
790 andalso List.exists (fn x
=> x
= node
) Config
.mailNodes_admin
))
792 fun rmdom
' delete resultRoot doms
=
794 fun doNode (node
, _
) =
796 val dname
= OS
.Path
.joinDirFile
{dir
= resultRoot
,
799 fun doDom (dom
, actions
) =
801 val domPath
= String.concatWith
"/" (rev (String.fields (fn ch
=> ch
= #
".") dom
))
802 val dname
= OS
.Path
.concat (dname
, domPath
)
804 fun visitDom (dom
, dname
, actions
) =
806 val dir
= Posix
.FileSys
.opendir dname
809 case Posix
.FileSys
.readdir dir
of
813 val fnameFull
= OS
.Path
.joinDirFile
{dir
= dname
,
816 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat fnameFull
) then
817 loop (visitDom (fname ^
"." ^ dom
,
821 loop ({action
= Slave
.Delete delete
,
824 file
= fnameFull
} :: actions
)
828 before Posix
.FileSys
.closedir dir
830 handle OS
.SysErr (s
, _
) =>
831 (print ("Warning: System error deleting domain " ^ dom ^
" on " ^ node ^
": " ^ s ^
"\n");
834 visitDom (dom
, dname
, actions
)
837 val actions
= foldl doDom
[] doms
839 handleSite (node
, actions
)
841 handle IO
.Io _
=> print ("Warning: IO error deleting domains on " ^ node ^
".\n")
843 fun cleanupNode (node
, _
) =
847 val domPath
= String.concatWith
"/" (rev (String.fields (fn ch
=> ch
= #
".") dom
))
848 val dname
= OS
.Path
.joinDirFile
{dir
= resultRoot
,
850 val dname
= OS
.Path
.concat (dname
, domPath
)
853 ignore (OS
.Process
.system (Config
.rm ^
" -rf " ^ dname
))
861 app doNode Config
.nodeIps
;
862 app cleanupNode Config
.nodeIps
865 val rmdom
= rmdom
' true Config
.resultRoot
866 val rmdom
' = rmdom
' false
868 fun homedirOf uname
=
869 Posix
.SysDB
.Passwd
.home (Posix
.SysDB
.getpwnam uname
)
871 fun homedir () = homedirOf (getUser ())
872 handle e
=> if !fakePrivs
then "/tmp" else raise e
874 type subject
= {node
: string, domain
: string}
876 val describers
: (subject
-> string) list ref
= ref
[]
878 fun registerDescriber f
= describers
:= f
:: !describers
880 fun describeOne arg
= String.concat (map (fn f
=> f arg
) (rev (!describers
)))
882 val line
= "--------------------------------------------------------------\n"
883 val dline
= "==============================================================\n"
886 String.concat (List.mapPartial
888 case describeOne
{node
= node
, domain
= dom
} of
891 SOME (String.concat
[dline
, "Node ", node
, "\n", dline
, "\n", s
]))
894 datatype description
=
895 Filename
of { filename
: string, heading
: string, showEmpty
: bool }
896 | Extension
of { extension
: string, heading
: string -> string }
898 fun considerAll ds
{node
, domain
} =
900 val ds
= map (fn d
=> (d
, ref
[])) ds
902 val path
= Config
.resultRoot
903 val jdf
= OS
.Path
.joinDirFile
904 val path
= jdf
{dir
= path
, file
= node
}
905 val path
= foldr (fn (more
, path
) => jdf
{dir
= path
, file
= more
})
906 path (String.tokens (fn ch
=> ch
= #
".") domain
)
908 if Posix
.FileSys
.access (path
, []) then
910 val dir
= Posix
.FileSys
.opendir path
913 case Posix
.FileSys
.readdir dir
of
916 (app (fn (d
, entries
) =>
918 fun readFile showEmpty entries
' =
920 val fname
= OS
.Path
.joinDirFile
{dir
= path
,
923 val inf
= TextIO.openIn fname
925 fun loop (seenOne
, entries
') =
926 case TextIO.inputLine inf
of
927 NONE
=> if seenOne
orelse showEmpty
then
931 | SOME line
=> loop (true, line
:: entries
')
933 loop (false, entries
')
934 before TextIO.closeIn inf
938 Filename
{filename
, heading
, showEmpty
} =>
939 if fname
= filename
then
940 entries
:= readFile
showEmpty ("\n" :: line
:: "\n" :: heading
:: line
:: !entries
)
943 | Extension
{extension
, heading
} =>
945 val {base
, ext
} = OS
.Path
.splitBaseExt fname
950 if extension
' = extension
then
951 entries
:= readFile
true ("\n" :: line
:: "\n" :: heading base
:: line
:: !entries
)
959 Posix
.FileSys
.closedir dir
;
960 String.concat (List.concat (map (fn (_
, entries
) => rev (!entries
)) ds
))
966 val () = registerDescriber (considerAll
[Filename
{filename
= "soa.conf",
967 heading
= "DNS SOA:",
970 val () = Env
.registerAction ("domainHost",
971 fn (env
, [(EString host
, _
)]) =>
972 SM
.insert (env
, "Hostname",
973 (EString (host ^
"." ^
currentDomain ()), dl
))
974 |
(_
, args
) => Env
.badArgs ("domainHost", args
))
976 val ouc
= ref (fn () => ())
978 fun registerOnUsersChange f
=
982 ouc
:= (fn () => (f
' (); f ()))
985 fun onUsersChange () = !ouc ()