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
31 val nodes
= map #
1 Config
.nodeIps
32 val nodeMap
= foldl (fn ((node
, ip
), mp
) => SM
.insert (mp
, node
, ip
))
33 SM
.empty Config
.nodeIps
34 fun nodeIp node
= valOf (SM
.find (nodeMap
, node
))
39 val your_doms
= ref SS
.empty
40 fun your_domains () = !your_doms
42 val your_usrs
= ref SS
.empty
43 fun your_users () = !your_usrs
45 val your_grps
= ref SS
.empty
46 fun your_groups () = !your_grps
48 val your_pths
= ref SS
.empty
49 fun your_paths () = !your_pths
51 val your_ipss
= ref SS
.empty
52 fun your_ips () = !your_ipss
54 val world_readable
= SS
.addList (SS
.empty
, Config
.worldReadable
)
55 val readable_pths
= ref world_readable
56 fun readable_paths () = !readable_pths
62 val your_paths
= Acl
.class
{user
= getUser (),
65 your_doms
:= Acl
.class
{user
= getUser (),
67 your_usrs
:= Acl
.class
{user
= getUser (),
69 your_grps
:= Acl
.class
{user
= getUser (),
71 your_pths
:= your_paths
;
72 readable_pths
:= SS
.union (your_paths
, world_readable
);
73 your_ipss
:= Acl
.class
{user
= getUser (),
78 case map
Int.fromString (String.fields (fn ch
=> ch
= #
".") s
) of
79 [SOME n1
, SOME n2
, SOME n3
, SOME n4
] =>
80 n1
>= 0 andalso n1
< 256 andalso n2
>= 0 andalso n2
< 256 andalso n3
>= 0 andalso n3
< 256 andalso n4
>= 0 andalso n4
< 256
83 fun isIdent ch
= Char.isLower ch
orelse Char.isDigit ch
86 size s
> 0 andalso size s
< 20
87 andalso CharVector
.all (fn ch
=> isIdent ch
orelse ch
= #
"-") s
90 size s
> 0 andalso size s
< 100
91 andalso List.all
validHost (String.fields (fn ch
=> ch
= #
".") s
)
93 fun validNode s
= List.exists (fn s
' => s
= s
') nodes
95 fun yourDomain s
= SS
.member (your_domains (), s
)
96 fun yourUser s
= SS
.member (your_users (), s
)
97 fun yourGroup s
= SS
.member (your_groups (), s
)
98 fun checkPath paths path
=
99 List.all (fn s
=> s
<> "..") (String.fields (fn ch
=> ch
= #
"/") path
)
100 andalso CharVector
.all (fn ch
=> Char.isAlphaNum ch
orelse ch
= #
"." orelse ch
= #
"/"
101 orelse ch
= #
"-" orelse ch
= #
"_") path
102 andalso SS
.exists (fn s
' => path
= s
' orelse String.isPrefix (s
' ^
"/") path
) (paths ())
103 val yourPath
= checkPath your_paths
104 val readablePath
= checkPath readable_paths
105 fun yourIp s
= SS
.member (your_ips (), s
)
107 fun yourDomainHost s
=
110 val (pref
, suf
) = Substring
.splitl (fn ch
=> ch
<> #
".") (Substring
.full s
)
112 Substring
.size suf
> 0
113 andalso validHost (Substring
.string pref
)
114 andalso yourDomain (Substring
.string
115 (Substring
.slice (suf
, 1, NONE
)))
118 val yourDomain
= yourDomainHost
120 fun validUser s
= size s
> 0 andalso size s
< 20
121 andalso CharVector
.all
Char.isAlphaNum s
123 fun validEmailUser s
=
124 size s
> 0 andalso size s
< 50
125 andalso CharVector
.all (fn ch
=> Char.isAlphaNum ch
131 val validGroup
= validUser
133 val _
= Env
.type_one
"no_spaces"
135 (CharVector
.all (fn ch
=> Char.isPrint ch
andalso not (Char.isSpace ch
)
136 andalso ch
<> #
"\"" andalso ch
<> #
"'"))
137 val _
= Env
.type_one
"no_newlines"
139 (CharVector
.all (fn ch
=> Char.isPrint ch
andalso ch
<> #
"\n" andalso ch
<> #
"\r"
140 andalso ch
<> #
"\"" andalso ch
<> #
"'"))
142 val _
= Env
.type_one
"ip"
146 val _
= Env
.type_one
"host"
150 val _
= Env
.type_one
"domain"
154 val _
= Env
.type_one
"your_domain"
158 val _
= Env
.type_one
"your_domain_host"
162 val _
= Env
.type_one
"user"
166 val _
= Env
.type_one
"group"
170 val _
= Env
.type_one
"your_user"
174 val _
= Env
.type_one
"your_group"
178 val _
= Env
.type_one
"your_path"
182 val _
= Env
.type_one
"readable_path"
186 val _
= Env
.type_one
"your_ip"
190 val _
= Env
.type_one
"node"
194 val _
= Env
.registerFunction ("dns_node_to_node",
198 val _
= Env
.registerFunction ("mail_node_to_node",
203 val dl
= ErrorMsg
.dummyLoc
205 val nsD
= (EString Config
.defaultNs
, dl
)
206 val serialD
= (EVar
"serialAuto", dl
)
207 val refD
= (EInt Config
.defaultRefresh
, dl
)
208 val retD
= (EInt Config
.defaultRetry
, dl
)
209 val expD
= (EInt Config
.defaultExpiry
, dl
)
210 val minD
= (EInt Config
.defaultMinimum
, dl
)
212 val soaD
= multiApp ((EVar
"soa", dl
),
214 [nsD
, serialD
, refD
, retD
, expD
, minD
])
216 val masterD
= (EApp ((EVar
"internalMaster", dl
),
217 (EString Config
.masterNode
, dl
)),
220 val slavesD
= (EList (map (fn s
=> (EString s
, dl
)) Config
.slaveNodes
), dl
)
222 val _
= Defaults
.registerDefault ("Aliases",
223 (TList (TBase
"your_domain", dl
), dl
),
224 (fn () => (EList
[], dl
)))
226 val _
= Defaults
.registerDefault ("Mailbox",
228 (fn () => (EString (getUser ()), dl
)))
230 val _
= Defaults
.registerDefault ("DNS",
231 (TBase
"dnsKind", dl
),
232 (fn () => multiApp ((EVar
"useDns", dl
),
234 [soaD
, masterD
, slavesD
])))
236 val _
= Defaults
.registerDefault ("TTL",
238 (fn () => (EInt Config
.Bind
.defaultTTL
, dl
)))
240 type soa
= {ns
: string,
247 val serial
= fn (EVar
"serialAuto", _
) => SOME NONE
248 |
(EApp ((EVar
"serialConst", _
), n
), _
) => Option
.map
SOME (Env
.int n
)
251 val soa
= fn (EApp ((EApp ((EApp ((EApp ((EApp ((EApp
252 ((EVar
"soa", _
), ns
), _
),
258 (case (Env
.string ns
, serial sl
, Env
.int rf
,
259 Env
.int ret
, Env
.int exp
, Env
.int min
) of
260 (SOME ns
, SOME sl
, SOME rf
,
261 SOME ret
, SOME exp
, SOME min
) =>
272 ExternalMaster
of string
273 | InternalMaster
of string
277 val _
= Env
.registerFunction ("ip_of_node",
278 fn [(EString node
, _
)] => SOME (EString (nodeIp node
), dl
)
281 val master
= fn (EApp ((EVar
"externalMaster", _
), e
), _
) => Option
.map
ExternalMaster (ip e
)
282 |
(EApp ((EVar
"internalMaster", _
), e
), _
) => Option
.map
InternalMaster (Env
.string e
)
286 UseDns
of {soa
: soa
,
288 slaves
: string list
}
291 val dnsKind
= fn (EApp ((EApp ((EApp
292 ((EVar
"useDns", _
), sa
), _
),
295 (case (soa sa
, master mstr
, Env
.list Env
.string slaves
) of
296 (SOME sa
, SOME mstr
, SOME slaves
) =>
297 SOME (UseDns
{soa
= sa
,
301 |
(EVar
"noDns", _
) => SOME NoDns
304 val befores
= ref (fn (_
: string) => ())
305 val afters
= ref (fn (_
: string) => ())
307 fun registerBefore f
=
311 befores
:= (fn x
=> (old x
; f x
))
314 fun registerAfter f
=
318 afters
:= (fn x
=> (old x
; f x
))
321 val globals
= ref (fn () => ())
322 val locals
= ref (fn () => ())
324 fun registerResetGlobal f
=
328 globals
:= (fn x
=> (old x
; f x
))
331 fun registerResetLocal f
=
335 locals
:= (fn x
=> (old x
; f x
))
338 fun resetGlobal () = (!globals ();
339 ignore (OS
.Process
.system (Config
.rm ^
" -rf " ^ Config
.resultRoot ^
"/*")))
340 fun resetLocal () = !locals ()
343 val currentPath
= ref (fn (_
: string) => "")
344 val currentPathAli
= ref (fn (_
: string, _
: string) => "")
348 fun currentDomain () = !current
350 val currentsAli
= ref ([] : string list
)
352 fun currentAliasDomains () = !currentsAli
353 fun currentDomains () = currentDomain () :: currentAliasDomains ()
355 fun domainFile
{node
, name
} = ((*print ("Opening " ^
!currentPath node ^ name ^
"\n");*)
356 TextIO.openOut (!currentPath node ^ name
))
358 type files
= {write
: string -> unit
,
359 writeDom
: unit
-> unit
,
360 close
: unit
-> unit
}
362 fun domainsFile
{node
, name
} =
364 val doms
= currentDomains ()
365 val files
= map (fn dom
=> (dom
, TextIO.openOut (!currentPathAli (dom
, node
) ^ name
))) doms
367 {write
= fn s
=> app (fn (_
, outf
) => TextIO.output (outf
, s
)) files
,
368 writeDom
= fn () => app (fn (dom
, outf
) => TextIO.output (outf
, dom
)) files
,
369 close
= fn () => app (fn (_
, outf
) => TextIO.closeOut outf
) files
}
374 val toks
= String.fields (fn ch
=> ch
= #
".") domain
376 val elems
= foldr (fn (piece
, elems
) =>
378 val elems
= piece
:: elems
382 val path
= String.concatWith
"/"
383 (Config
.resultRoot
:: node
:: rev elems
)
384 val tmpPath
= String.concatWith
"/"
385 (Config
.tmpDir
:: node
:: rev elems
)
387 (if Posix
.FileSys
.ST
.isDir
388 (Posix
.FileSys
.stat path
) then
391 (OS
.FileSys
.remove path
;
392 OS
.FileSys
.mkDir path
))
393 handle OS
.SysErr _
=> OS
.FileSys
.mkDir path
;
395 (if Posix
.FileSys
.ST
.isDir
396 (Posix
.FileSys
.stat tmpPath
) then
399 (OS
.FileSys
.remove tmpPath
;
400 OS
.FileSys
.mkDir tmpPath
))
401 handle OS
.SysErr _
=> OS
.FileSys
.mkDir tmpPath
408 fn (root
, site
) => String.concatWith
"/" (root
:: site
:: rev ("" :: elems
))
411 datatype file_action
' =
412 Add
' of {src
: string, dst
: string}
414 | Modify
' of {src
: string, dst
: string}
416 fun findDiffs (prefixes
, site
, dom
, acts
) =
419 val realPath
= gp (Config
.resultRoot
, site
)
420 val tmpPath
= gp (Config
.tmpDir
, site
)
422 (*val _
= print ("getDiffs(" ^ site ^
", " ^ dom ^
")... " ^ realPath ^
"; " ^ tmpPath ^
"\n")*)
424 val dir
= Posix
.FileSys
.opendir realPath
427 case Posix
.FileSys
.readdir dir
of
428 NONE
=> (Posix
.FileSys
.closedir dir
;
432 val real = OS
.Path
.joinDirFile
{dir
= realPath
,
434 val tmp
= OS
.Path
.joinDirFile
{dir
= tmpPath
,
437 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat
real) then
439 else if Posix
.FileSys
.access (tmp
, []) then
440 if Slave
.shell
[Config
.diff
, " ", real, " ", tmp
] then
443 loopReal ((site
, dom
, realPath
, Modify
' {src
= tmp
, dst
= real}) :: acts
)
444 else if List.exists (fn prefix
=> String.isPrefix prefix
real) prefixes
then
445 loopReal ((site
, dom
, realPath
, Delete
' real) :: acts
)
450 val acts
= loopReal acts
452 val dir
= Posix
.FileSys
.opendir tmpPath
455 case Posix
.FileSys
.readdir dir
of
456 NONE
=> (Posix
.FileSys
.closedir dir
;
460 val real = OS
.Path
.joinDirFile
{dir
= realPath
,
462 val tmp
= OS
.Path
.joinDirFile
{dir
= tmpPath
,
465 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat tmp
) then
467 else if Posix
.FileSys
.access (real, []) then
470 loopTmp ((site
, dom
, realPath
, Add
' {src
= tmp
, dst
= real}) :: acts
)
473 val acts
= loopTmp acts
478 fun findAllDiffs prefixes
=
480 val dir
= Posix
.FileSys
.opendir Config
.tmpDir
481 val len
= length (String.fields (fn ch
=> ch
= #
"/") Config
.tmpDir
) + 1
483 fun exploreSites diffs
=
484 case Posix
.FileSys
.readdir dir
of
488 fun explore (dname
, diffs
) =
490 val dir
= Posix
.FileSys
.opendir dname
493 case Posix
.FileSys
.readdir dir
of
497 val fname
= OS
.Path
.joinDirFile
{dir
= dname
,
500 loop (if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat fname
) then
502 val dom
= String.fields (fn ch
=> ch
= #
"/") fname
503 val dom
= List.drop (dom
, len
)
504 val dom
= String.concatWith
"." (rev dom
)
506 val dname
' = OS
.Path
.joinDirFile
{dir
= dname
,
510 findDiffs (prefixes
, site
, dom
, diffs
))
517 before Posix
.FileSys
.closedir dir
520 exploreSites (explore (OS
.Path
.joinDirFile
{dir
= Config
.tmpDir
,
521 file
= site
}, diffs
))
525 before Posix
.FileSys
.closedir dir
528 val masterNode
: string option ref
= ref NONE
529 fun dnsMaster () = !masterNode
531 val seenDomains
: string list ref
= ref
[]
533 val _
= Env
.containerV_one
"domain"
534 ("domain", Env
.string)
537 val () = seenDomains
:= dom
:: !seenDomains
539 val kind
= Env
.env
dnsKind (evs
, "DNS")
540 val ttl
= Env
.env Env
.int (evs
, "TTL")
541 val aliases
= Env
.env (Env
.list Env
.string) (evs
, "Aliases")
543 val path
= getPath dom
545 val () = (current
:= dom
;
546 currentsAli
:= Slave
.remove (Slave
.removeDups aliases
, dom
);
547 currentPath
:= (fn site
=> path (Config
.tmpDir
, site
));
548 currentPathAli
:= (fn (dom
, site
) => getPath
dom (Config
.tmpDir
, site
)))
550 fun saveSoa (kind
, soa
: soa
) node
=
552 val {write
, writeDom
, close
} = domainsFile
{node
= node
, name
= "soa"}
556 write (Int.toString ttl
);
562 | SOME n
=> write (Int.toString n
);
564 write (Int.toString (#ref soa
));
566 write (Int.toString (#ret soa
));
568 write (Int.toString (#exp soa
));
570 write (Int.toString (#min soa
));
575 fun saveNamed (kind
, soa
: soa
, masterIp
, slaveIps
) node
=
576 if dom
= "localhost" then
579 val {write
, writeDom
, close
} = domainsFile
{node
= node
, name
= "named.conf"}
583 write
"\" {\n\ttype ";
585 write
";\n\tfile \"";
586 write Config
.Bind
.zonePath_real
;
591 "master" => (write
"\tallow-transfer {\n";
592 app (fn ip
=> (write
"\t\t";
594 write
";\n")) slaveIps
;
596 | _
=> (write
"\tmasters { ";
604 NoDns
=> masterNode
:= NONE
609 InternalMaster node
=> nodeIp node
610 | ExternalMaster ip
=> ip
612 val slaveIps
= map
nodeIp (#slaves dns
)
614 app (saveSoa ("slave", #soa dns
)) (#slaves dns
);
615 app (saveNamed ("slave", #soa dns
, masterIp
, slaveIps
)) (#slaves dns
);
617 InternalMaster node
=>
618 (masterNode
:= SOME node
;
619 saveSoa ("master", #soa dns
) node
;
620 saveNamed ("master", #soa dns
, masterIp
, slaveIps
) node
)
621 | _
=> masterNode
:= NONE
625 fn () => !afters (!current
))
627 val () = Env
.registerPre (fn () => (seenDomains
:= [];
628 ignore (Slave
.shellF ([Config
.rm
, " -rf ", Config
.tmpDir
, ""],
629 fn cl
=> "Temp file cleanup failed: " ^ cl
));
630 OS
.FileSys
.mkDir Config
.tmpDir
;
631 app (fn node
=> OS
.FileSys
.mkDir
632 (OS
.Path
.joinDirFile
{dir
= Config
.tmpDir
,
635 app (fn node
=> OS
.FileSys
.mkDir
636 (OS
.Path
.joinDirFile
{dir
= Config
.resultRoot
,
638 handle OS
.SysErr _
=> ())
641 fun handleSite (site
, files
) =
645 print ("New configuration for node " ^ site ^
"\n");
646 if site
= Config
.defaultNode
then
647 Slave
.handleChanges files
649 val bio
= OpenSSL
.connect (valOf (!ssl_context
),
652 ^
Int.toString Config
.slavePort
)
654 app (fn file
=> Msg
.send (bio
, MsgFile file
)) files
;
655 Msg
.send (bio
, MsgDoFiles
);
657 NONE
=> print
"Slave closed connection unexpectedly\n"
660 MsgOk
=> print ("Slave " ^ site ^
" finished\n")
661 | MsgError s
=> print ("Slave " ^ site
662 ^
" returned error: " ^
664 | _
=> print ("Slave " ^ site
665 ^
" returned unexpected command\n");
670 val () = Env
.registerPost (fn () =>
672 val prefixes
= List.concat
675 val pieces
= String.tokens (fn ch
=> ch
= #
".") dom
676 val path
= String.concatWith
"/" (rev pieces
)
679 Config
.resultRoot ^
"/" ^ node ^
"/" ^ path ^
"/")
683 val diffs
= findAllDiffs prefixes
685 val diffs
= map (fn (site
, dom
, dir
, Add
' {src
, dst
}) =>
686 (Slave
.shellF ([Config
.cp
, " ", src
, " ", dst
],
687 fn cl
=> "Copy failed: " ^ cl
);
693 |
(site
, dom
, dir
, Delete
' dst
) =>
694 (OS
.FileSys
.remove dst
695 handle OS
.SysErr _
=>
696 ErrorMsg
.error
NONE ("Delete failed for " ^ dst
);
698 {action
= Slave
.Delete
true,
702 |
(site
, dom
, dir
, Modify
' {src
, dst
}) =>
703 (Slave
.shellF ([Config
.cp
, " ", src
, " ", dst
],
704 fn cl
=> "Copy failed: " ^ cl
);
706 {action
= Slave
.Modify
,
711 if !ErrorMsg
.anyErrors
then
714 val changed
= foldl (fn ((site
, file
), changed
) =>
716 val ls
= case SM
.find (changed
, site
) of
720 SM
.insert (changed
, site
, file
:: ls
)
723 SM
.appi handleSite changed
725 ignore (Slave
.shellF ([Config
.rm
, " -rf ", Config
.tmpDir
, ""],
726 fn cl
=> "Temp file cleanup failed: " ^ cl
))
729 fun hasPriv priv
= Acl
.query
{user
= getUser (), class
= "priv", value
= "all"}
730 orelse Acl
.query
{user
= getUser (), class
= "priv", value
= priv
}
732 val _
= Env
.type_one
"dns_node"
735 List.exists (fn x
=> x
= node
) Config
.dnsNodes_all
736 orelse (hasPriv
"dns"
737 andalso List.exists (fn x
=> x
= node
) Config
.dnsNodes_admin
))
739 val _
= Env
.type_one
"mail_node"
742 List.exists (fn x
=> x
= node
) Config
.mailNodes_all
743 orelse (hasPriv
"mail"
744 andalso List.exists (fn x
=> x
= node
) Config
.mailNodes_admin
))
746 fun rmdom
' delete resultRoot doms
=
748 fun doNode (node
, _
) =
750 val dname
= OS
.Path
.joinDirFile
{dir
= resultRoot
,
753 fun doDom (dom
, actions
) =
755 val domPath
= String.concatWith
"/" (rev (String.fields (fn ch
=> ch
= #
".") dom
))
756 val dname
= OS
.Path
.concat (dname
, domPath
)
758 fun visitDom (dom
, dname
, actions
) =
760 val dir
= Posix
.FileSys
.opendir dname
763 case Posix
.FileSys
.readdir dir
of
767 val fnameFull
= OS
.Path
.joinDirFile
{dir
= dname
,
770 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat fnameFull
) then
771 loop (visitDom (fname ^
"." ^ dom
,
775 loop ({action
= Slave
.Delete delete
,
778 file
= fnameFull
} :: actions
)
782 before Posix
.FileSys
.closedir dir
784 handle OS
.SysErr (s
, _
) =>
785 (print ("Warning: System error deleting domain " ^ dom ^
" on " ^ node ^
": " ^ s ^
"\n");
788 visitDom (dom
, dname
, actions
)
791 val actions
= foldl doDom
[] doms
793 handleSite (node
, actions
)
795 handle IO
.Io _
=> print ("Warning: IO error deleting domains on " ^ node ^
".\n")
797 fun cleanupNode (node
, _
) =
801 val domPath
= String.concatWith
"/" (rev (String.fields (fn ch
=> ch
= #
".") dom
))
802 val dname
= OS
.Path
.joinDirFile
{dir
= resultRoot
,
804 val dname
= OS
.Path
.concat (dname
, domPath
)
807 ignore (OS
.Process
.system (Config
.rm ^
" -rf " ^ dname
))
815 app doNode Config
.nodeIps
;
816 app cleanupNode Config
.nodeIps
819 val rmdom
= rmdom
' true Config
.resultRoot
820 val rmdom
' = rmdom
' false
822 fun homedirOf uname
=
823 Posix
.SysDB
.Passwd
.home (Posix
.SysDB
.getpwnam uname
)
825 fun homedir () = homedirOf (getUser ())
827 type subject
= {node
: string, domain
: string}
829 val describers
: (subject
-> string) list ref
= ref
[]
831 fun registerDescriber f
= describers
:= f
:: !describers
833 fun describeOne arg
= String.concat (map (fn f
=> f arg
) (rev (!describers
)))
835 val line
= "--------------------------------------------------------------\n"
836 val dline
= "==============================================================\n"
839 String.concat (List.mapPartial
841 case describeOne
{node
= node
, domain
= dom
} of
844 SOME (String.concat
[dline
, "Node ", node
, "\n", dline
, "\n", s
]))
847 datatype description
=
848 Filename
of { filename
: string, heading
: string, showEmpty
: bool }
849 | Extension
of { extension
: string, heading
: string -> string }
851 fun considerAll ds
{node
, domain
} =
853 val ds
= map (fn d
=> (d
, ref
[])) ds
855 val path
= Config
.resultRoot
856 val jdf
= OS
.Path
.joinDirFile
857 val path
= jdf
{dir
= path
, file
= node
}
858 val path
= foldr (fn (more
, path
) => jdf
{dir
= path
, file
= more
})
859 path (String.tokens (fn ch
=> ch
= #
".") domain
)
861 if Posix
.FileSys
.access (path
, []) then
863 val dir
= Posix
.FileSys
.opendir path
866 case Posix
.FileSys
.readdir dir
of
869 (app (fn (d
, entries
) =>
871 fun readFile showEmpty entries
' =
873 val fname
= OS
.Path
.joinDirFile
{dir
= path
,
876 val inf
= TextIO.openIn fname
878 fun loop (seenOne
, entries
') =
879 case TextIO.inputLine inf
of
880 NONE
=> if seenOne
orelse showEmpty
then
884 | SOME line
=> loop (true, line
:: entries
')
886 loop (false, entries
')
887 before TextIO.closeIn inf
891 Filename
{filename
, heading
, showEmpty
} =>
892 if fname
= filename
then
893 entries
:= readFile
showEmpty ("\n" :: line
:: ":\n" :: heading
:: line
:: !entries
)
896 | Extension
{extension
, heading
} =>
898 val {base
, ext
} = OS
.Path
.splitBaseExt fname
903 if extension
' = extension
then
904 entries
:= readFile
true ("\n" :: line
:: ":\n" :: heading base
:: line
:: !entries
)
912 Posix
.FileSys
.closedir dir
;
913 String.concat (List.concat (map (fn (_
, entries
) => rev (!entries
)) ds
))
919 val () = registerDescriber (considerAll
[Filename
{filename
= "soa",
923 val () = Env
.registerAction ("domainHost",
924 fn (env
, [(EString host
, _
)]) =>
925 SM
.insert (env
, "Hostname",
926 (EString (host ^
"." ^
currentDomain ()), dl
))
927 |
(_
, args
) => Env
.badArgs ("domainHost", args
))