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
= SS
.member (your_users (), s
)
138 fun yourGroup s
= SS
.member (your_groups (), s
)
139 fun checkPath paths path
=
140 (List.all (fn s
=> s
<> "..") (String.fields (fn ch
=> ch
= #
"/") path
)
141 andalso CharVector
.all (fn ch
=> Char.isAlphaNum ch
orelse ch
= #
"." orelse ch
= #
"/"
142 orelse ch
= #
"-" orelse ch
= #
"_") path
143 andalso SS
.exists (fn s
' => path
= s
' orelse String.isPrefix (s
' ^
"/") path
) (paths ()))
144 val yourPath
= checkPath your_paths
145 val readablePath
= checkPath readable_paths
146 fun yourIp s
= !fakePrivs
orelse SS
.member (your_ips (), s
)
148 fun yourDomainHost s
=
152 val (pref
, suf
) = Substring
.splitl (fn ch
=> ch
<> #
".") (Substring
.full s
)
154 Substring
.size suf
> 0
155 andalso validHost (Substring
.string pref
)
156 andalso yourDomain (Substring
.string
157 (Substring
.slice (suf
, 1, NONE
)))
160 val yourDomain
= yourDomainHost
162 fun validUser s
= size s
> 0 andalso size s
< 20
163 andalso CharVector
.all
Char.isAlphaNum s
165 fun validEmailUser s
=
166 size s
> 0 andalso size s
< 50
167 andalso CharVector
.all (fn ch
=> Char.isAlphaNum ch
173 val validGroup
= validUser
175 val _
= Env
.type_one
"no_spaces"
177 (CharVector
.all (fn ch
=> Char.isPrint ch
andalso not (Char.isSpace ch
)
178 andalso ch
<> #
"\"" andalso ch
<> #
"'"))
179 val _
= Env
.type_one
"no_newlines"
181 (CharVector
.all (fn ch
=> Char.isPrint ch
andalso ch
<> #
"\n" andalso ch
<> #
"\r"
182 andalso ch
<> #
"\""))
184 val _
= Env
.type_one
"ip"
188 val _
= Env
.type_one
"ipv6"
192 val _
= Env
.type_one
"host"
196 val _
= Env
.type_one
"domain"
200 val _
= Env
.type_one
"your_domain"
204 val _
= Env
.type_one
"your_domain_host"
208 val _
= Env
.type_one
"user"
212 val _
= Env
.type_one
"group"
216 val _
= Env
.type_one
"your_user"
220 val _
= Env
.type_one
"your_group"
224 val _
= Env
.type_one
"your_path"
228 val _
= Env
.type_one
"readable_path"
232 val _
= Env
.type_one
"your_ip"
236 val _
= Env
.type_one
"node"
240 val _
= Env
.type_one
"mime_type"
242 (CharVector
.exists (fn ch
=> ch
= #
"/"))
244 val _
= Env
.registerFunction ("your_ip_to_ip",
248 val _
= Env
.registerFunction ("dns_node_to_node",
252 val _
= Env
.registerFunction ("mail_node_to_node",
259 val dl
= ErrorMsg
.dummyLoc
261 val _
= Env
.registerFunction ("end_in_slash",
262 fn [(EString
"", _
)] => SOME (EString
"/", dl
)
263 |
[(EString s
, _
)] =>
264 SOME (EString (if String.sub (s
, size s
- 1) = #
"/" then
271 val _
= Env
.registerFunction ("you",
272 fn [] => SOME (EString (getUser ()), dl
)
275 val _
= Env
.registerFunction ("defaultMailbox",
276 fn [] => SOME (EString (getUser ()), dl
)
280 type soa
= {ns
: string,
287 val serial
= fn (EVar
"serialAuto", _
) => SOME NONE
288 |
(EApp ((EVar
"serialConst", _
), n
), _
) => Option
.map
SOME (Env
.int n
)
291 val soa
= fn (EApp ((EApp ((EApp ((EApp ((EApp ((EApp
292 ((EVar
"soa", _
), ns
), _
),
298 (case (Env
.string ns
, serial sl
, Env
.int rf
,
299 Env
.int ret
, Env
.int exp
, Env
.int min
) of
300 (SOME ns
, SOME sl
, SOME rf
,
301 SOME ret
, SOME exp
, SOME min
) =>
312 ExternalMaster
of string
313 | InternalMaster
of string
317 val _
= Env
.registerFunction ("ip_of_node",
318 fn [(EString node
, _
)] => SOME (EString (nodeIp node
), dl
)
321 val master
= fn (EApp ((EVar
"externalMaster", _
), e
), _
) => Option
.map
ExternalMaster (ip e
)
322 |
(EApp ((EVar
"internalMaster", _
), e
), _
) => Option
.map
InternalMaster (Env
.string e
)
326 UseDns
of {soa
: soa
,
328 slaves
: string list
}
331 val dnsKind
= fn (EApp ((EApp ((EApp
332 ((EVar
"useDns", _
), sa
), _
),
335 (case (soa sa
, master mstr
, Env
.list Env
.string slaves
) of
336 (SOME sa
, SOME mstr
, SOME slaves
) =>
337 SOME (UseDns
{soa
= sa
,
341 |
(EVar
"noDns", _
) => SOME NoDns
344 val befores
= ref (fn (_
: string) => ())
345 val afters
= ref (fn (_
: string) => ())
347 fun registerBefore f
=
351 befores
:= (fn x
=> (old x
; f x
))
354 fun registerAfter f
=
358 afters
:= (fn x
=> (old x
; f x
))
361 val globals
= ref (fn () => ())
362 val locals
= ref (fn () => ())
364 fun registerResetGlobal f
=
368 globals
:= (fn x
=> (old x
; f x
))
371 fun registerResetLocal f
=
375 locals
:= (fn x
=> (old x
; f x
))
378 fun resetGlobal () = (!globals ();
379 ignore (OS
.Process
.system (Config
.rm ^
" -rf " ^ Config
.resultRoot ^
"/*")))
380 fun resetLocal () = !locals ()
383 val currentPath
= ref (fn (_
: string) => "")
384 val currentPathAli
= ref (fn (_
: string, _
: string) => "")
388 fun currentDomain () = !current
390 val currentsAli
= ref ([] : string list
)
392 fun currentAliasDomains () = !currentsAli
393 fun currentDomains () = currentDomain () :: currentAliasDomains ()
395 fun domainFile
{node
, name
} = ((*print ("Opening " ^
!currentPath node ^ name ^
"\n");*)
396 TextIO.openOut (!currentPath node ^ name
))
398 type files
= {write
: string -> unit
,
399 writeDom
: unit
-> unit
,
400 close
: unit
-> unit
}
402 fun domainsFile
{node
, name
} =
404 val doms
= currentDomains ()
405 val files
= map (fn dom
=> (dom
, TextIO.openOut (!currentPathAli (dom
, node
) ^ name
))) doms
407 {write
= fn s
=> app (fn (_
, outf
) => TextIO.output (outf
, s
)) files
,
408 writeDom
= fn () => app (fn (dom
, outf
) => TextIO.output (outf
, dom
)) files
,
409 close
= fn () => app (fn (_
, outf
) => TextIO.closeOut outf
) files
}
414 val toks
= String.fields (fn ch
=> ch
= #
".") domain
416 val elems
= foldr (fn (piece
, elems
) =>
418 val elems
= piece
:: elems
422 val path
= String.concatWith
"/"
423 (Config
.resultRoot
:: node
:: rev elems
)
424 val tmpPath
= String.concatWith
"/"
425 (Config
.tmpDir
:: node
:: rev elems
)
427 (if Posix
.FileSys
.ST
.isDir
428 (Posix
.FileSys
.stat path
) then
431 (OS
.FileSys
.remove path
;
432 OS
.FileSys
.mkDir path
))
433 handle OS
.SysErr _
=> OS
.FileSys
.mkDir path
;
435 (if Posix
.FileSys
.ST
.isDir
436 (Posix
.FileSys
.stat tmpPath
) then
439 (OS
.FileSys
.remove tmpPath
;
440 OS
.FileSys
.mkDir tmpPath
))
441 handle OS
.SysErr _
=> OS
.FileSys
.mkDir tmpPath
448 fn (root
, site
) => String.concatWith
"/" (root
:: site
:: rev ("" :: elems
))
451 datatype file_action
' =
452 Add
' of {src
: string, dst
: string}
454 | Modify
' of {src
: string, dst
: string}
456 fun findDiffs (prefixes
, site
, dom
, acts
) =
459 val realPath
= gp (Config
.resultRoot
, site
)
460 val tmpPath
= gp (Config
.tmpDir
, site
)
462 (*val _
= print ("getDiffs(" ^ site ^
", " ^ dom ^
")... " ^ realPath ^
"; " ^ tmpPath ^
"\n")*)
464 val dir
= Posix
.FileSys
.opendir realPath
467 case Posix
.FileSys
.readdir dir
of
468 NONE
=> (Posix
.FileSys
.closedir dir
;
472 val real = OS
.Path
.joinDirFile
{dir
= realPath
,
474 val tmp
= OS
.Path
.joinDirFile
{dir
= tmpPath
,
477 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat
real) then
479 else if Posix
.FileSys
.access (tmp
, []) then
480 if Slave
.shell
[Config
.diff
, " ", real, " ", tmp
] then
483 loopReal ((site
, dom
, realPath
, Modify
' {src
= tmp
, dst
= real}) :: acts
)
484 else if List.exists (fn prefix
=> String.isPrefix prefix
real) prefixes
then
485 loopReal ((site
, dom
, realPath
, Delete
' real) :: acts
)
490 val acts
= loopReal acts
492 val dir
= Posix
.FileSys
.opendir tmpPath
495 case Posix
.FileSys
.readdir dir
of
496 NONE
=> (Posix
.FileSys
.closedir dir
;
500 val real = OS
.Path
.joinDirFile
{dir
= realPath
,
502 val tmp
= OS
.Path
.joinDirFile
{dir
= tmpPath
,
505 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat tmp
) then
507 else if Posix
.FileSys
.access (real, []) then
510 loopTmp ((site
, dom
, realPath
, Add
' {src
= tmp
, dst
= real}) :: acts
)
513 val acts
= loopTmp acts
518 fun findAllDiffs prefixes
=
520 val dir
= Posix
.FileSys
.opendir Config
.tmpDir
521 val len
= length (String.fields (fn ch
=> ch
= #
"/") Config
.tmpDir
) + 1
523 fun exploreSites diffs
=
524 case Posix
.FileSys
.readdir dir
of
528 fun explore (dname
, diffs
) =
530 val dir
= Posix
.FileSys
.opendir dname
533 case Posix
.FileSys
.readdir dir
of
537 val fname
= OS
.Path
.joinDirFile
{dir
= dname
,
540 loop (if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat fname
) then
542 val dom
= String.fields (fn ch
=> ch
= #
"/") fname
543 val dom
= List.drop (dom
, len
)
544 val dom
= String.concatWith
"." (rev dom
)
546 val dname
' = OS
.Path
.joinDirFile
{dir
= dname
,
550 findDiffs (prefixes
, site
, dom
, diffs
))
557 before Posix
.FileSys
.closedir dir
560 exploreSites (explore (OS
.Path
.joinDirFile
{dir
= Config
.tmpDir
,
561 file
= site
}, diffs
))
565 before Posix
.FileSys
.closedir dir
568 val masterNode
: string option ref
= ref NONE
569 fun dnsMaster () = !masterNode
571 val seenDomains
: string list ref
= ref
[]
573 val _
= Env
.containerV_one
"domain"
574 ("domain", Env
.string)
577 val () = seenDomains
:= dom
:: !seenDomains
579 val kind
= Env
.env
dnsKind (evs
, "DNS")
580 val ttl
= Env
.env Env
.int (evs
, "TTL")
581 val aliases
= Env
.env (Env
.list Env
.string) (evs
, "Aliases")
583 val path
= getPath dom
585 val () = (current
:= dom
;
586 currentsAli
:= Slave
.remove (Slave
.removeDups aliases
, dom
);
587 currentPath
:= (fn site
=> path (Config
.tmpDir
, site
));
588 currentPathAli
:= (fn (dom
, site
) => getPath
dom (Config
.tmpDir
, site
)))
590 fun saveSoa (kind
, soa
: soa
) node
=
592 val {write
, writeDom
, close
} = domainsFile
{node
= node
, name
= "soa.conf"}
596 write (Int.toString ttl
);
602 | SOME n
=> write (Int.toString n
);
604 write (Int.toString (#ref soa
));
606 write (Int.toString (#ret soa
));
608 write (Int.toString (#exp soa
));
610 write (Int.toString (#min soa
));
615 fun saveNamed (kind
, soa
: soa
, masterIp
, slaveIps
) node
=
616 if dom
= "localhost" then
619 val {write
, writeDom
, close
} = domainsFile
{node
= node
, name
= "named.conf"}
623 write
"\" {\n\ttype ";
625 write
";\n\tfile \"";
626 write Config
.Bind
.zonePath_real
;
631 "master" => (write
"\tallow-transfer {\n";
632 app (fn ip
=> (write
"\t\t";
634 write
";\n")) slaveIps
;
636 | _
=> (write
"\tmasters { ";
639 write
"// Updated: ";
640 write (Time
.toString (Time
.now ()));
647 NoDns
=> masterNode
:= NONE
652 InternalMaster node
=> nodeIp node
653 | ExternalMaster ip
=> ip
655 val slaveIps
= map
nodeIp (#slaves dns
)
657 app (saveSoa ("slave", #soa dns
)) (#slaves dns
);
658 app (saveNamed ("slave", #soa dns
, masterIp
, slaveIps
)) (#slaves dns
);
660 InternalMaster node
=>
661 (masterNode
:= SOME node
;
662 saveSoa ("master", #soa dns
) node
;
663 saveNamed ("master", #soa dns
, masterIp
, slaveIps
) node
)
664 | _
=> masterNode
:= NONE
668 fn () => !afters (!current
))
670 val () = Env
.registerPre (fn () => (seenDomains
:= [];
671 ignore (Slave
.shellF ([Config
.rm
, " -rf ", Config
.tmpDir
, ""],
672 fn cl
=> "Temp file cleanup failed: " ^ cl
));
673 OS
.FileSys
.mkDir Config
.tmpDir
;
674 app (fn node
=> OS
.FileSys
.mkDir
675 (OS
.Path
.joinDirFile
{dir
= Config
.tmpDir
,
678 app (fn node
=> OS
.FileSys
.mkDir
679 (OS
.Path
.joinDirFile
{dir
= Config
.resultRoot
,
681 handle OS
.SysErr _
=> ())
684 fun handleSite (site
, files
) =
688 print ("New configuration for node " ^ site ^
"\n");
689 if site
= Config
.defaultNode
then
690 Slave
.handleChanges files
692 val bio
= OpenSSL
.connect
true (valOf (!ssl_context
),
695 ^
Int.toString Config
.slavePort
)
697 app (fn file
=> Msg
.send (bio
, MsgFile file
)) files
;
698 Msg
.send (bio
, MsgDoFiles
);
700 NONE
=> print
"Slave closed connection unexpectedly\n"
703 MsgOk
=> print ("Slave " ^ site ^
" finished\n")
704 | MsgError s
=> print ("Slave " ^ site
705 ^
" returned error: " ^
707 | _
=> print ("Slave " ^ site
708 ^
" returned unexpected command\n");
713 val () = Env
.registerPost (fn () =>
715 val prefixes
= List.concat
718 val pieces
= String.tokens (fn ch
=> ch
= #
".") dom
719 val path
= String.concatWith
"/" (rev pieces
)
722 Config
.resultRoot ^
"/" ^ node ^
"/" ^ path ^
"/")
726 val diffs
= findAllDiffs prefixes
728 val diffs
= map (fn (site
, dom
, dir
, Add
' {src
, dst
}) =>
729 (Slave
.shellF ([Config
.cp
, " ", src
, " ", dst
],
730 fn cl
=> "Copy failed: " ^ cl
);
736 |
(site
, dom
, dir
, Delete
' dst
) =>
737 (OS
.FileSys
.remove dst
738 handle OS
.SysErr _
=>
739 ErrorMsg
.error
NONE ("Delete failed for " ^ dst
);
741 {action
= Slave
.Delete
true,
745 |
(site
, dom
, dir
, Modify
' {src
, dst
}) =>
746 (Slave
.shellF ([Config
.cp
, " ", src
, " ", dst
],
747 fn cl
=> "Copy failed: " ^ cl
);
749 {action
= Slave
.Modify
,
754 if !ErrorMsg
.anyErrors
then
757 val changed
= foldl (fn ((site
, file
), changed
) =>
759 val ls
= case SM
.find (changed
, site
) of
763 SM
.insert (changed
, site
, file
:: ls
)
766 SM
.appi handleSite changed
768 ignore (Slave
.shellF ([Config
.rm
, " -rf ", Config
.tmpDir
, ""],
769 fn cl
=> "Temp file cleanup failed: " ^ cl
))
772 fun hasPriv priv
= Acl
.query
{user
= getUser (), class
= "priv", value
= "all"}
773 orelse Acl
.query
{user
= getUser (), class
= "priv", value
= priv
}
775 val _
= Env
.type_one
"dns_node"
778 List.exists (fn x
=> x
= node
) Config
.dnsNodes_all
779 orelse (hasPriv
"dns"
780 andalso List.exists (fn x
=> x
= node
) Config
.dnsNodes_admin
))
782 val _
= Env
.type_one
"mail_node"
785 List.exists (fn x
=> x
= node
) Config
.mailNodes_all
786 orelse (hasPriv
"mail"
787 andalso List.exists (fn x
=> x
= node
) Config
.mailNodes_admin
))
789 fun rmdom
' delete resultRoot doms
=
791 fun doNode (node
, _
) =
793 val dname
= OS
.Path
.joinDirFile
{dir
= resultRoot
,
796 fun doDom (dom
, actions
) =
798 val domPath
= String.concatWith
"/" (rev (String.fields (fn ch
=> ch
= #
".") dom
))
799 val dname
= OS
.Path
.concat (dname
, domPath
)
801 fun visitDom (dom
, dname
, actions
) =
803 val dir
= Posix
.FileSys
.opendir dname
806 case Posix
.FileSys
.readdir dir
of
810 val fnameFull
= OS
.Path
.joinDirFile
{dir
= dname
,
813 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat fnameFull
) then
814 loop (visitDom (fname ^
"." ^ dom
,
818 loop ({action
= Slave
.Delete delete
,
821 file
= fnameFull
} :: actions
)
825 before Posix
.FileSys
.closedir dir
827 handle OS
.SysErr (s
, _
) =>
828 (print ("Warning: System error deleting domain " ^ dom ^
" on " ^ node ^
": " ^ s ^
"\n");
831 visitDom (dom
, dname
, actions
)
834 val actions
= foldl doDom
[] doms
836 handleSite (node
, actions
)
838 handle IO
.Io _
=> print ("Warning: IO error deleting domains on " ^ node ^
".\n")
840 fun cleanupNode (node
, _
) =
844 val domPath
= String.concatWith
"/" (rev (String.fields (fn ch
=> ch
= #
".") dom
))
845 val dname
= OS
.Path
.joinDirFile
{dir
= resultRoot
,
847 val dname
= OS
.Path
.concat (dname
, domPath
)
850 ignore (OS
.Process
.system (Config
.rm ^
" -rf " ^ dname
))
858 app doNode Config
.nodeIps
;
859 app cleanupNode Config
.nodeIps
862 val rmdom
= rmdom
' true Config
.resultRoot
863 val rmdom
' = rmdom
' false
865 fun homedirOf uname
=
866 Posix
.SysDB
.Passwd
.home (Posix
.SysDB
.getpwnam uname
)
868 fun homedir () = homedirOf (getUser ())
870 type subject
= {node
: string, domain
: string}
872 val describers
: (subject
-> string) list ref
= ref
[]
874 fun registerDescriber f
= describers
:= f
:: !describers
876 fun describeOne arg
= String.concat (map (fn f
=> f arg
) (rev (!describers
)))
878 val line
= "--------------------------------------------------------------\n"
879 val dline
= "==============================================================\n"
882 String.concat (List.mapPartial
884 case describeOne
{node
= node
, domain
= dom
} of
887 SOME (String.concat
[dline
, "Node ", node
, "\n", dline
, "\n", s
]))
890 datatype description
=
891 Filename
of { filename
: string, heading
: string, showEmpty
: bool }
892 | Extension
of { extension
: string, heading
: string -> string }
894 fun considerAll ds
{node
, domain
} =
896 val ds
= map (fn d
=> (d
, ref
[])) ds
898 val path
= Config
.resultRoot
899 val jdf
= OS
.Path
.joinDirFile
900 val path
= jdf
{dir
= path
, file
= node
}
901 val path
= foldr (fn (more
, path
) => jdf
{dir
= path
, file
= more
})
902 path (String.tokens (fn ch
=> ch
= #
".") domain
)
904 if Posix
.FileSys
.access (path
, []) then
906 val dir
= Posix
.FileSys
.opendir path
909 case Posix
.FileSys
.readdir dir
of
912 (app (fn (d
, entries
) =>
914 fun readFile showEmpty entries
' =
916 val fname
= OS
.Path
.joinDirFile
{dir
= path
,
919 val inf
= TextIO.openIn fname
921 fun loop (seenOne
, entries
') =
922 case TextIO.inputLine inf
of
923 NONE
=> if seenOne
orelse showEmpty
then
927 | SOME line
=> loop (true, line
:: entries
')
929 loop (false, entries
')
930 before TextIO.closeIn inf
934 Filename
{filename
, heading
, showEmpty
} =>
935 if fname
= filename
then
936 entries
:= readFile
showEmpty ("\n" :: line
:: "\n" :: heading
:: line
:: !entries
)
939 | Extension
{extension
, heading
} =>
941 val {base
, ext
} = OS
.Path
.splitBaseExt fname
946 if extension
' = extension
then
947 entries
:= readFile
true ("\n" :: line
:: "\n" :: heading base
:: line
:: !entries
)
955 Posix
.FileSys
.closedir dir
;
956 String.concat (List.concat (map (fn (_
, entries
) => rev (!entries
)) ds
))
962 val () = registerDescriber (considerAll
[Filename
{filename
= "soa.conf",
963 heading
= "DNS SOA:",
966 val () = Env
.registerAction ("domainHost",
967 fn (env
, [(EString host
, _
)]) =>
968 SM
.insert (env
, "Hostname",
969 (EString (host ^
"." ^
currentDomain ()), dl
))
970 |
(_
, args
) => Env
.badArgs ("domainHost", args
))
972 val ouc
= ref (fn () => ())
974 fun registerOnUsersChange f
=
978 ouc
:= (fn () => (f
' (); f ()))
981 fun onUsersChange () = !ouc ()