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
<> #
"\""))
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 ("your_ip_to_ip",
198 val _
= Env
.registerFunction ("dns_node_to_node",
202 val _
= Env
.registerFunction ("mail_node_to_node",
209 val dl
= ErrorMsg
.dummyLoc
211 val _
= Env
.registerFunction ("end_in_slash",
212 fn [(EString
"", _
)] => SOME (EString
"/", dl
)
213 |
[(EString s
, _
)] =>
214 SOME (EString (if String.sub (s
, size s
- 1) = #
"/" then
221 val nsD
= (EString Config
.defaultNs
, dl
)
222 val serialD
= (EVar
"serialAuto", dl
)
223 val refD
= (EInt Config
.defaultRefresh
, dl
)
224 val retD
= (EInt Config
.defaultRetry
, dl
)
225 val expD
= (EInt Config
.defaultExpiry
, dl
)
226 val minD
= (EInt Config
.defaultMinimum
, dl
)
228 val soaD
= multiApp ((EVar
"soa", dl
),
230 [nsD
, serialD
, refD
, retD
, expD
, minD
])
232 val masterD
= (EApp ((EVar
"internalMaster", dl
),
233 (EString Config
.masterNode
, dl
)),
236 val slavesD
= (EList (map (fn s
=> (EString s
, dl
)) Config
.slaveNodes
), dl
)
238 val _
= Defaults
.registerDefault ("Aliases",
239 (TList (TBase
"your_domain", dl
), dl
),
240 (fn () => (EList
[], dl
)))
242 val _
= Defaults
.registerDefault ("Mailbox",
244 (fn () => (EString (getUser ()), dl
)))
246 val _
= Defaults
.registerDefault ("DNS",
247 (TBase
"dnsKind", dl
),
248 (fn () => multiApp ((EVar
"useDns", dl
),
250 [soaD
, masterD
, slavesD
])))
252 val _
= Defaults
.registerDefault ("TTL",
254 (fn () => (EInt Config
.Bind
.defaultTTL
, dl
)))
256 type soa
= {ns
: string,
263 val serial
= fn (EVar
"serialAuto", _
) => SOME NONE
264 |
(EApp ((EVar
"serialConst", _
), n
), _
) => Option
.map
SOME (Env
.int n
)
267 val soa
= fn (EApp ((EApp ((EApp ((EApp ((EApp ((EApp
268 ((EVar
"soa", _
), ns
), _
),
274 (case (Env
.string ns
, serial sl
, Env
.int rf
,
275 Env
.int ret
, Env
.int exp
, Env
.int min
) of
276 (SOME ns
, SOME sl
, SOME rf
,
277 SOME ret
, SOME exp
, SOME min
) =>
288 ExternalMaster
of string
289 | InternalMaster
of string
293 val _
= Env
.registerFunction ("ip_of_node",
294 fn [(EString node
, _
)] => SOME (EString (nodeIp node
), dl
)
297 val master
= fn (EApp ((EVar
"externalMaster", _
), e
), _
) => Option
.map
ExternalMaster (ip e
)
298 |
(EApp ((EVar
"internalMaster", _
), e
), _
) => Option
.map
InternalMaster (Env
.string e
)
302 UseDns
of {soa
: soa
,
304 slaves
: string list
}
307 val dnsKind
= fn (EApp ((EApp ((EApp
308 ((EVar
"useDns", _
), sa
), _
),
311 (case (soa sa
, master mstr
, Env
.list Env
.string slaves
) of
312 (SOME sa
, SOME mstr
, SOME slaves
) =>
313 SOME (UseDns
{soa
= sa
,
317 |
(EVar
"noDns", _
) => SOME NoDns
320 val befores
= ref (fn (_
: string) => ())
321 val afters
= ref (fn (_
: string) => ())
323 fun registerBefore f
=
327 befores
:= (fn x
=> (old x
; f x
))
330 fun registerAfter f
=
334 afters
:= (fn x
=> (old x
; f x
))
337 val globals
= ref (fn () => ())
338 val locals
= ref (fn () => ())
340 fun registerResetGlobal f
=
344 globals
:= (fn x
=> (old x
; f x
))
347 fun registerResetLocal f
=
351 locals
:= (fn x
=> (old x
; f x
))
354 fun resetGlobal () = (!globals ();
355 ignore (OS
.Process
.system (Config
.rm ^
" -rf " ^ Config
.resultRoot ^
"/*")))
356 fun resetLocal () = !locals ()
359 val currentPath
= ref (fn (_
: string) => "")
360 val currentPathAli
= ref (fn (_
: string, _
: string) => "")
364 fun currentDomain () = !current
366 val currentsAli
= ref ([] : string list
)
368 fun currentAliasDomains () = !currentsAli
369 fun currentDomains () = currentDomain () :: currentAliasDomains ()
371 fun domainFile
{node
, name
} = ((*print ("Opening " ^
!currentPath node ^ name ^
"\n");*)
372 TextIO.openOut (!currentPath node ^ name
))
374 type files
= {write
: string -> unit
,
375 writeDom
: unit
-> unit
,
376 close
: unit
-> unit
}
378 fun domainsFile
{node
, name
} =
380 val doms
= currentDomains ()
381 val files
= map (fn dom
=> (dom
, TextIO.openOut (!currentPathAli (dom
, node
) ^ name
))) doms
383 {write
= fn s
=> app (fn (_
, outf
) => TextIO.output (outf
, s
)) files
,
384 writeDom
= fn () => app (fn (dom
, outf
) => TextIO.output (outf
, dom
)) files
,
385 close
= fn () => app (fn (_
, outf
) => TextIO.closeOut outf
) files
}
390 val toks
= String.fields (fn ch
=> ch
= #
".") domain
392 val elems
= foldr (fn (piece
, elems
) =>
394 val elems
= piece
:: elems
398 val path
= String.concatWith
"/"
399 (Config
.resultRoot
:: node
:: rev elems
)
400 val tmpPath
= String.concatWith
"/"
401 (Config
.tmpDir
:: node
:: rev elems
)
403 (if Posix
.FileSys
.ST
.isDir
404 (Posix
.FileSys
.stat path
) then
407 (OS
.FileSys
.remove path
;
408 OS
.FileSys
.mkDir path
))
409 handle OS
.SysErr _
=> OS
.FileSys
.mkDir path
;
411 (if Posix
.FileSys
.ST
.isDir
412 (Posix
.FileSys
.stat tmpPath
) then
415 (OS
.FileSys
.remove tmpPath
;
416 OS
.FileSys
.mkDir tmpPath
))
417 handle OS
.SysErr _
=> OS
.FileSys
.mkDir tmpPath
424 fn (root
, site
) => String.concatWith
"/" (root
:: site
:: rev ("" :: elems
))
427 datatype file_action
' =
428 Add
' of {src
: string, dst
: string}
430 | Modify
' of {src
: string, dst
: string}
432 fun findDiffs (prefixes
, site
, dom
, acts
) =
435 val realPath
= gp (Config
.resultRoot
, site
)
436 val tmpPath
= gp (Config
.tmpDir
, site
)
438 (*val _
= print ("getDiffs(" ^ site ^
", " ^ dom ^
")... " ^ realPath ^
"; " ^ tmpPath ^
"\n")*)
440 val dir
= Posix
.FileSys
.opendir realPath
443 case Posix
.FileSys
.readdir dir
of
444 NONE
=> (Posix
.FileSys
.closedir dir
;
448 val real = OS
.Path
.joinDirFile
{dir
= realPath
,
450 val tmp
= OS
.Path
.joinDirFile
{dir
= tmpPath
,
453 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat
real) then
455 else if Posix
.FileSys
.access (tmp
, []) then
456 if Slave
.shell
[Config
.diff
, " ", real, " ", tmp
] then
459 loopReal ((site
, dom
, realPath
, Modify
' {src
= tmp
, dst
= real}) :: acts
)
460 else if List.exists (fn prefix
=> String.isPrefix prefix
real) prefixes
then
461 loopReal ((site
, dom
, realPath
, Delete
' real) :: acts
)
466 val acts
= loopReal acts
468 val dir
= Posix
.FileSys
.opendir tmpPath
471 case Posix
.FileSys
.readdir dir
of
472 NONE
=> (Posix
.FileSys
.closedir dir
;
476 val real = OS
.Path
.joinDirFile
{dir
= realPath
,
478 val tmp
= OS
.Path
.joinDirFile
{dir
= tmpPath
,
481 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat tmp
) then
483 else if Posix
.FileSys
.access (real, []) then
486 loopTmp ((site
, dom
, realPath
, Add
' {src
= tmp
, dst
= real}) :: acts
)
489 val acts
= loopTmp acts
494 fun findAllDiffs prefixes
=
496 val dir
= Posix
.FileSys
.opendir Config
.tmpDir
497 val len
= length (String.fields (fn ch
=> ch
= #
"/") Config
.tmpDir
) + 1
499 fun exploreSites diffs
=
500 case Posix
.FileSys
.readdir dir
of
504 fun explore (dname
, diffs
) =
506 val dir
= Posix
.FileSys
.opendir dname
509 case Posix
.FileSys
.readdir dir
of
513 val fname
= OS
.Path
.joinDirFile
{dir
= dname
,
516 loop (if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat fname
) then
518 val dom
= String.fields (fn ch
=> ch
= #
"/") fname
519 val dom
= List.drop (dom
, len
)
520 val dom
= String.concatWith
"." (rev dom
)
522 val dname
' = OS
.Path
.joinDirFile
{dir
= dname
,
526 findDiffs (prefixes
, site
, dom
, diffs
))
533 before Posix
.FileSys
.closedir dir
536 exploreSites (explore (OS
.Path
.joinDirFile
{dir
= Config
.tmpDir
,
537 file
= site
}, diffs
))
541 before Posix
.FileSys
.closedir dir
544 val masterNode
: string option ref
= ref NONE
545 fun dnsMaster () = !masterNode
547 val seenDomains
: string list ref
= ref
[]
549 val _
= Env
.containerV_one
"domain"
550 ("domain", Env
.string)
553 val () = seenDomains
:= dom
:: !seenDomains
555 val kind
= Env
.env
dnsKind (evs
, "DNS")
556 val ttl
= Env
.env Env
.int (evs
, "TTL")
557 val aliases
= Env
.env (Env
.list Env
.string) (evs
, "Aliases")
559 val path
= getPath dom
561 val () = (current
:= dom
;
562 currentsAli
:= Slave
.remove (Slave
.removeDups aliases
, dom
);
563 currentPath
:= (fn site
=> path (Config
.tmpDir
, site
));
564 currentPathAli
:= (fn (dom
, site
) => getPath
dom (Config
.tmpDir
, site
)))
566 fun saveSoa (kind
, soa
: soa
) node
=
568 val {write
, writeDom
, close
} = domainsFile
{node
= node
, name
= "soa"}
572 write (Int.toString ttl
);
578 | SOME n
=> write (Int.toString n
);
580 write (Int.toString (#ref soa
));
582 write (Int.toString (#ret soa
));
584 write (Int.toString (#exp soa
));
586 write (Int.toString (#min soa
));
591 fun saveNamed (kind
, soa
: soa
, masterIp
, slaveIps
) node
=
592 if dom
= "localhost" then
595 val {write
, writeDom
, close
} = domainsFile
{node
= node
, name
= "named.conf"}
599 write
"\" {\n\ttype ";
601 write
";\n\tfile \"";
602 write Config
.Bind
.zonePath_real
;
607 "master" => (write
"\tallow-transfer {\n";
608 app (fn ip
=> (write
"\t\t";
610 write
";\n")) slaveIps
;
612 | _
=> (write
"\tmasters { ";
620 NoDns
=> masterNode
:= NONE
625 InternalMaster node
=> nodeIp node
626 | ExternalMaster ip
=> ip
628 val slaveIps
= map
nodeIp (#slaves dns
)
630 app (saveSoa ("slave", #soa dns
)) (#slaves dns
);
631 app (saveNamed ("slave", #soa dns
, masterIp
, slaveIps
)) (#slaves dns
);
633 InternalMaster node
=>
634 (masterNode
:= SOME node
;
635 saveSoa ("master", #soa dns
) node
;
636 saveNamed ("master", #soa dns
, masterIp
, slaveIps
) node
)
637 | _
=> masterNode
:= NONE
641 fn () => !afters (!current
))
643 val () = Env
.registerPre (fn () => (seenDomains
:= [];
644 ignore (Slave
.shellF ([Config
.rm
, " -rf ", Config
.tmpDir
, ""],
645 fn cl
=> "Temp file cleanup failed: " ^ cl
));
646 OS
.FileSys
.mkDir Config
.tmpDir
;
647 app (fn node
=> OS
.FileSys
.mkDir
648 (OS
.Path
.joinDirFile
{dir
= Config
.tmpDir
,
651 app (fn node
=> OS
.FileSys
.mkDir
652 (OS
.Path
.joinDirFile
{dir
= Config
.resultRoot
,
654 handle OS
.SysErr _
=> ())
657 fun handleSite (site
, files
) =
661 print ("New configuration for node " ^ site ^
"\n");
662 if site
= Config
.defaultNode
then
663 Slave
.handleChanges files
665 val bio
= OpenSSL
.connect (valOf (!ssl_context
),
668 ^
Int.toString Config
.slavePort
)
670 app (fn file
=> Msg
.send (bio
, MsgFile file
)) files
;
671 Msg
.send (bio
, MsgDoFiles
);
673 NONE
=> print
"Slave closed connection unexpectedly\n"
676 MsgOk
=> print ("Slave " ^ site ^
" finished\n")
677 | MsgError s
=> print ("Slave " ^ site
678 ^
" returned error: " ^
680 | _
=> print ("Slave " ^ site
681 ^
" returned unexpected command\n");
686 val () = Env
.registerPost (fn () =>
688 val prefixes
= List.concat
691 val pieces
= String.tokens (fn ch
=> ch
= #
".") dom
692 val path
= String.concatWith
"/" (rev pieces
)
695 Config
.resultRoot ^
"/" ^ node ^
"/" ^ path ^
"/")
699 val diffs
= findAllDiffs prefixes
701 val diffs
= map (fn (site
, dom
, dir
, Add
' {src
, dst
}) =>
702 (Slave
.shellF ([Config
.cp
, " ", src
, " ", dst
],
703 fn cl
=> "Copy failed: " ^ cl
);
709 |
(site
, dom
, dir
, Delete
' dst
) =>
710 (OS
.FileSys
.remove dst
711 handle OS
.SysErr _
=>
712 ErrorMsg
.error
NONE ("Delete failed for " ^ dst
);
714 {action
= Slave
.Delete
true,
718 |
(site
, dom
, dir
, Modify
' {src
, dst
}) =>
719 (Slave
.shellF ([Config
.cp
, " ", src
, " ", dst
],
720 fn cl
=> "Copy failed: " ^ cl
);
722 {action
= Slave
.Modify
,
727 if !ErrorMsg
.anyErrors
then
730 val changed
= foldl (fn ((site
, file
), changed
) =>
732 val ls
= case SM
.find (changed
, site
) of
736 SM
.insert (changed
, site
, file
:: ls
)
739 SM
.appi handleSite changed
741 ignore (Slave
.shellF ([Config
.rm
, " -rf ", Config
.tmpDir
, ""],
742 fn cl
=> "Temp file cleanup failed: " ^ cl
))
745 fun hasPriv priv
= Acl
.query
{user
= getUser (), class
= "priv", value
= "all"}
746 orelse Acl
.query
{user
= getUser (), class
= "priv", value
= priv
}
748 val _
= Env
.type_one
"dns_node"
751 List.exists (fn x
=> x
= node
) Config
.dnsNodes_all
752 orelse (hasPriv
"dns"
753 andalso List.exists (fn x
=> x
= node
) Config
.dnsNodes_admin
))
755 val _
= Env
.type_one
"mail_node"
758 List.exists (fn x
=> x
= node
) Config
.mailNodes_all
759 orelse (hasPriv
"mail"
760 andalso List.exists (fn x
=> x
= node
) Config
.mailNodes_admin
))
762 fun rmdom
' delete resultRoot doms
=
764 fun doNode (node
, _
) =
766 val dname
= OS
.Path
.joinDirFile
{dir
= resultRoot
,
769 fun doDom (dom
, actions
) =
771 val domPath
= String.concatWith
"/" (rev (String.fields (fn ch
=> ch
= #
".") dom
))
772 val dname
= OS
.Path
.concat (dname
, domPath
)
774 fun visitDom (dom
, dname
, actions
) =
776 val dir
= Posix
.FileSys
.opendir dname
779 case Posix
.FileSys
.readdir dir
of
783 val fnameFull
= OS
.Path
.joinDirFile
{dir
= dname
,
786 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat fnameFull
) then
787 loop (visitDom (fname ^
"." ^ dom
,
791 loop ({action
= Slave
.Delete delete
,
794 file
= fnameFull
} :: actions
)
798 before Posix
.FileSys
.closedir dir
800 handle OS
.SysErr (s
, _
) =>
801 (print ("Warning: System error deleting domain " ^ dom ^
" on " ^ node ^
": " ^ s ^
"\n");
804 visitDom (dom
, dname
, actions
)
807 val actions
= foldl doDom
[] doms
809 handleSite (node
, actions
)
811 handle IO
.Io _
=> print ("Warning: IO error deleting domains on " ^ node ^
".\n")
813 fun cleanupNode (node
, _
) =
817 val domPath
= String.concatWith
"/" (rev (String.fields (fn ch
=> ch
= #
".") dom
))
818 val dname
= OS
.Path
.joinDirFile
{dir
= resultRoot
,
820 val dname
= OS
.Path
.concat (dname
, domPath
)
823 ignore (OS
.Process
.system (Config
.rm ^
" -rf " ^ dname
))
831 app doNode Config
.nodeIps
;
832 app cleanupNode Config
.nodeIps
835 val rmdom
= rmdom
' true Config
.resultRoot
836 val rmdom
' = rmdom
' false
838 fun homedirOf uname
=
839 Posix
.SysDB
.Passwd
.home (Posix
.SysDB
.getpwnam uname
)
841 fun homedir () = homedirOf (getUser ())
843 type subject
= {node
: string, domain
: string}
845 val describers
: (subject
-> string) list ref
= ref
[]
847 fun registerDescriber f
= describers
:= f
:: !describers
849 fun describeOne arg
= String.concat (map (fn f
=> f arg
) (rev (!describers
)))
851 val line
= "--------------------------------------------------------------\n"
852 val dline
= "==============================================================\n"
855 String.concat (List.mapPartial
857 case describeOne
{node
= node
, domain
= dom
} of
860 SOME (String.concat
[dline
, "Node ", node
, "\n", dline
, "\n", s
]))
863 datatype description
=
864 Filename
of { filename
: string, heading
: string, showEmpty
: bool }
865 | Extension
of { extension
: string, heading
: string -> string }
867 fun considerAll ds
{node
, domain
} =
869 val ds
= map (fn d
=> (d
, ref
[])) ds
871 val path
= Config
.resultRoot
872 val jdf
= OS
.Path
.joinDirFile
873 val path
= jdf
{dir
= path
, file
= node
}
874 val path
= foldr (fn (more
, path
) => jdf
{dir
= path
, file
= more
})
875 path (String.tokens (fn ch
=> ch
= #
".") domain
)
877 if Posix
.FileSys
.access (path
, []) then
879 val dir
= Posix
.FileSys
.opendir path
882 case Posix
.FileSys
.readdir dir
of
885 (app (fn (d
, entries
) =>
887 fun readFile showEmpty entries
' =
889 val fname
= OS
.Path
.joinDirFile
{dir
= path
,
892 val inf
= TextIO.openIn fname
894 fun loop (seenOne
, entries
') =
895 case TextIO.inputLine inf
of
896 NONE
=> if seenOne
orelse showEmpty
then
900 | SOME line
=> loop (true, line
:: entries
')
902 loop (false, entries
')
903 before TextIO.closeIn inf
907 Filename
{filename
, heading
, showEmpty
} =>
908 if fname
= filename
then
909 entries
:= readFile
showEmpty ("\n" :: line
:: "\n" :: heading
:: line
:: !entries
)
912 | Extension
{extension
, heading
} =>
914 val {base
, ext
} = OS
.Path
.splitBaseExt fname
919 if extension
' = extension
then
920 entries
:= readFile
true ("\n" :: line
:: "\n" :: heading base
:: line
:: !entries
)
928 Posix
.FileSys
.closedir dir
;
929 String.concat (List.concat (map (fn (_
, entries
) => rev (!entries
)) ds
))
935 val () = registerDescriber (considerAll
[Filename
{filename
= "soa",
936 heading
= "DNS SOA:",
939 val () = Env
.registerAction ("domainHost",
940 fn (env
, [(EString host
, _
)]) =>
941 SM
.insert (env
, "Hostname",
942 (EString (host ^
"." ^
currentDomain ()), dl
))
943 |
(_
, args
) => Env
.badArgs ("domainHost", args
))