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
))
38 val fakePrivs
= ref
false
39 val isClient
= ref
false
41 val your_doms
= ref SS
.empty
42 fun your_domains () = !your_doms
44 val your_usrs
= ref SS
.empty
45 fun your_users () = !your_usrs
47 val your_grps
= ref SS
.empty
48 fun your_groups () = !your_grps
50 val your_pths
= ref SS
.empty
51 fun your_paths () = !your_pths
53 val your_ipss
= ref SS
.empty
54 fun your_ips () = !your_ipss
56 val world_readable
= SS
.addList (SS
.empty
, Config
.worldReadable
)
57 val readable_pths
= ref world_readable
58 fun readable_paths () = !readable_pths
64 val your_paths
= Acl
.class
{user
= getUser (),
68 your_doms
:= Acl
.class
{user
= getUser (),
70 your_usrs
:= Acl
.class
{user
= getUser (),
72 your_grps
:= Acl
.class
{user
= getUser (),
74 your_pths
:= your_paths
;
75 readable_pths
:= SS
.union (your_paths
, world_readable
);
76 your_ipss
:= Acl
.class
{user
= getUser (),
80 fun declareClient () = isClient
:= true
81 fun fakePrivileges () = if !isClient
then
84 raise Fail
"Tried to fake privileges as non-client"
87 case map
Int.fromString (String.fields (fn ch
=> ch
= #
".") s
) of
88 [SOME n1
, SOME n2
, SOME n3
, SOME n4
] =>
89 n1
>= 0 andalso n1
< 256 andalso n2
>= 0 andalso n2
< 256 andalso n3
>= 0 andalso n3
< 256 andalso n4
>= 0 andalso n4
< 256
92 fun isHexDigit ch
= Char.isDigit ch
orelse (ord ch
>= ord #
"a" andalso ord ch
<= ord #
"f")
96 val fields
= String.fields (fn ch
=> ch
= #
":") s
98 val empties
= foldl (fn ("", n
) => n
+ 1
99 |
(_
, n
) => n
) 0 fields
103 andalso length fields
<= maxLen
105 andalso List.all (fn "" => true
107 andalso CharVector
.all isHexDigit s
) fields
113 val maybeIpv4
= List.last fields
114 val theRest
= List.take (fields
, length fields
- 1)
116 validIp maybeIpv4
andalso noIpv4
6
119 noIpv4
8 orelse hasIpv4 ()
122 fun isIdent ch
= Char.isLower ch
orelse Char.isDigit ch
125 size s
> 0 andalso size s
< 50
126 andalso CharVector
.all (fn ch
=> isIdent ch
orelse ch
= #
"-") s
129 size s
> 0 andalso size s
< 200
130 andalso List.all
validHost (String.fields (fn ch
=> ch
= #
".") s
)
132 fun validNode s
= List.exists (fn s
' => s
= s
') nodes
134 fun yourDomain s
= !fakePrivs
orelse SS
.member (your_domains (), s
)
135 fun yourUser s
= !fakePrivs
orelse SS
.member (your_users (), s
)
136 fun yourGroup s
= !fakePrivs
orelse SS
.member (your_groups (), s
)
137 fun checkPath paths path
=
139 (List.all (fn s
=> s
<> "..") (String.fields (fn ch
=> ch
= #
"/") path
)
140 andalso CharVector
.all (fn ch
=> Char.isAlphaNum ch
orelse ch
= #
"." orelse ch
= #
"/"
141 orelse ch
= #
"-" orelse ch
= #
"_") path
142 andalso SS
.exists (fn s
' => path
= s
' orelse String.isPrefix (s
' ^
"/") path
) (paths ()))
143 val yourPath
= checkPath your_paths
144 val readablePath
= checkPath readable_paths
145 fun yourIp s
= !fakePrivs
orelse SS
.member (your_ips (), s
)
147 fun yourDomainHost s
=
151 val (pref
, suf
) = Substring
.splitl (fn ch
=> ch
<> #
".") (Substring
.full s
)
153 Substring
.size suf
> 0
154 andalso validHost (Substring
.string pref
)
155 andalso yourDomain (Substring
.string
156 (Substring
.slice (suf
, 1, NONE
)))
159 val yourDomain
= yourDomainHost
161 fun validUser s
= size s
> 0 andalso size s
< 20
162 andalso CharVector
.all
Char.isAlphaNum s
164 fun validEmailUser s
=
165 size s
> 0 andalso size s
< 50
166 andalso CharVector
.all (fn ch
=> Char.isAlphaNum ch
172 val validGroup
= validUser
174 val _
= Env
.type_one
"no_spaces"
176 (CharVector
.all (fn ch
=> Char.isPrint ch
andalso not (Char.isSpace ch
)
177 andalso ch
<> #
"\"" andalso ch
<> #
"'"))
178 val _
= Env
.type_one
"no_newlines"
180 (CharVector
.all (fn ch
=> Char.isPrint ch
andalso ch
<> #
"\n" andalso ch
<> #
"\r"
181 andalso ch
<> #
"\""))
183 val _
= Env
.type_one
"ip"
187 val _
= Env
.type_one
"ipv6"
191 val _
= Env
.type_one
"host"
195 val _
= Env
.type_one
"domain"
199 val _
= Env
.type_one
"your_domain"
203 val _
= Env
.type_one
"your_domain_host"
207 val _
= Env
.type_one
"user"
211 val _
= Env
.type_one
"group"
215 val _
= Env
.type_one
"your_user"
219 val _
= Env
.type_one
"your_group"
223 val _
= Env
.type_one
"your_path"
227 val _
= Env
.type_one
"readable_path"
231 val _
= Env
.type_one
"your_ip"
235 val _
= Env
.type_one
"node"
239 val _
= Env
.registerFunction ("your_ip_to_ip",
243 val _
= Env
.registerFunction ("dns_node_to_node",
247 val _
= Env
.registerFunction ("mail_node_to_node",
254 val dl
= ErrorMsg
.dummyLoc
256 val _
= Env
.registerFunction ("end_in_slash",
257 fn [(EString
"", _
)] => SOME (EString
"/", dl
)
258 |
[(EString s
, _
)] =>
259 SOME (EString (if String.sub (s
, size s
- 1) = #
"/" then
266 val nsD
= (EString Config
.defaultNs
, dl
)
267 val serialD
= (EVar
"serialAuto", dl
)
268 val refD
= (EInt Config
.defaultRefresh
, dl
)
269 val retD
= (EInt Config
.defaultRetry
, dl
)
270 val expD
= (EInt Config
.defaultExpiry
, dl
)
271 val minD
= (EInt Config
.defaultMinimum
, dl
)
273 val soaD
= multiApp ((EVar
"soa", dl
),
275 [nsD
, serialD
, refD
, retD
, expD
, minD
])
277 val masterD
= (EApp ((EVar
"internalMaster", dl
),
278 (EString Config
.masterNode
, dl
)),
281 val slavesD
= (EList (map (fn s
=> (EString s
, dl
)) Config
.slaveNodes
), dl
)
283 val _
= Defaults
.registerDefault ("Aliases",
284 (TList (TBase
"your_domain", dl
), dl
),
285 (fn () => (EList
[], dl
)))
287 val _
= Defaults
.registerDefault ("Mailbox",
289 (fn () => (EString (getUser ()), dl
)))
291 val _
= Defaults
.registerDefault ("DNS",
292 (TBase
"dnsKind", dl
),
293 (fn () => multiApp ((EVar
"useDns", dl
),
295 [soaD
, masterD
, slavesD
])))
297 val _
= Defaults
.registerDefault ("TTL",
299 (fn () => (EInt Config
.Bind
.defaultTTL
, dl
)))
301 type soa
= {ns
: string,
308 val serial
= fn (EVar
"serialAuto", _
) => SOME NONE
309 |
(EApp ((EVar
"serialConst", _
), n
), _
) => Option
.map
SOME (Env
.int n
)
312 val soa
= fn (EApp ((EApp ((EApp ((EApp ((EApp ((EApp
313 ((EVar
"soa", _
), ns
), _
),
319 (case (Env
.string ns
, serial sl
, Env
.int rf
,
320 Env
.int ret
, Env
.int exp
, Env
.int min
) of
321 (SOME ns
, SOME sl
, SOME rf
,
322 SOME ret
, SOME exp
, SOME min
) =>
333 ExternalMaster
of string
334 | InternalMaster
of string
338 val _
= Env
.registerFunction ("ip_of_node",
339 fn [(EString node
, _
)] => SOME (EString (nodeIp node
), dl
)
342 val master
= fn (EApp ((EVar
"externalMaster", _
), e
), _
) => Option
.map
ExternalMaster (ip e
)
343 |
(EApp ((EVar
"internalMaster", _
), e
), _
) => Option
.map
InternalMaster (Env
.string e
)
347 UseDns
of {soa
: soa
,
349 slaves
: string list
}
352 val dnsKind
= fn (EApp ((EApp ((EApp
353 ((EVar
"useDns", _
), sa
), _
),
356 (case (soa sa
, master mstr
, Env
.list Env
.string slaves
) of
357 (SOME sa
, SOME mstr
, SOME slaves
) =>
358 SOME (UseDns
{soa
= sa
,
362 |
(EVar
"noDns", _
) => SOME NoDns
365 val befores
= ref (fn (_
: string) => ())
366 val afters
= ref (fn (_
: string) => ())
368 fun registerBefore f
=
372 befores
:= (fn x
=> (old x
; f x
))
375 fun registerAfter f
=
379 afters
:= (fn x
=> (old x
; f x
))
382 val globals
= ref (fn () => ())
383 val locals
= ref (fn () => ())
385 fun registerResetGlobal f
=
389 globals
:= (fn x
=> (old x
; f x
))
392 fun registerResetLocal f
=
396 locals
:= (fn x
=> (old x
; f x
))
399 fun resetGlobal () = (!globals ();
400 ignore (OS
.Process
.system (Config
.rm ^
" -rf " ^ Config
.resultRoot ^
"/*")))
401 fun resetLocal () = !locals ()
404 val currentPath
= ref (fn (_
: string) => "")
405 val currentPathAli
= ref (fn (_
: string, _
: string) => "")
409 fun currentDomain () = !current
411 val currentsAli
= ref ([] : string list
)
413 fun currentAliasDomains () = !currentsAli
414 fun currentDomains () = currentDomain () :: currentAliasDomains ()
416 fun domainFile
{node
, name
} = ((*print ("Opening " ^
!currentPath node ^ name ^
"\n");*)
417 TextIO.openOut (!currentPath node ^ name
))
419 type files
= {write
: string -> unit
,
420 writeDom
: unit
-> unit
,
421 close
: unit
-> unit
}
423 fun domainsFile
{node
, name
} =
425 val doms
= currentDomains ()
426 val files
= map (fn dom
=> (dom
, TextIO.openOut (!currentPathAli (dom
, node
) ^ name
))) doms
428 {write
= fn s
=> app (fn (_
, outf
) => TextIO.output (outf
, s
)) files
,
429 writeDom
= fn () => app (fn (dom
, outf
) => TextIO.output (outf
, dom
)) files
,
430 close
= fn () => app (fn (_
, outf
) => TextIO.closeOut outf
) files
}
435 val toks
= String.fields (fn ch
=> ch
= #
".") domain
437 val elems
= foldr (fn (piece
, elems
) =>
439 val elems
= piece
:: elems
443 val path
= String.concatWith
"/"
444 (Config
.resultRoot
:: node
:: rev elems
)
445 val tmpPath
= String.concatWith
"/"
446 (Config
.tmpDir
:: node
:: rev elems
)
448 (if Posix
.FileSys
.ST
.isDir
449 (Posix
.FileSys
.stat path
) then
452 (OS
.FileSys
.remove path
;
453 OS
.FileSys
.mkDir path
))
454 handle OS
.SysErr _
=> OS
.FileSys
.mkDir path
;
456 (if Posix
.FileSys
.ST
.isDir
457 (Posix
.FileSys
.stat tmpPath
) then
460 (OS
.FileSys
.remove tmpPath
;
461 OS
.FileSys
.mkDir tmpPath
))
462 handle OS
.SysErr _
=> OS
.FileSys
.mkDir tmpPath
469 fn (root
, site
) => String.concatWith
"/" (root
:: site
:: rev ("" :: elems
))
472 datatype file_action
' =
473 Add
' of {src
: string, dst
: string}
475 | Modify
' of {src
: string, dst
: string}
477 fun findDiffs (prefixes
, site
, dom
, acts
) =
480 val realPath
= gp (Config
.resultRoot
, site
)
481 val tmpPath
= gp (Config
.tmpDir
, site
)
483 (*val _
= print ("getDiffs(" ^ site ^
", " ^ dom ^
")... " ^ realPath ^
"; " ^ tmpPath ^
"\n")*)
485 val dir
= Posix
.FileSys
.opendir realPath
488 case Posix
.FileSys
.readdir dir
of
489 NONE
=> (Posix
.FileSys
.closedir dir
;
493 val real = OS
.Path
.joinDirFile
{dir
= realPath
,
495 val tmp
= OS
.Path
.joinDirFile
{dir
= tmpPath
,
498 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat
real) then
500 else if Posix
.FileSys
.access (tmp
, []) then
501 if Slave
.shell
[Config
.diff
, " ", real, " ", tmp
] then
504 loopReal ((site
, dom
, realPath
, Modify
' {src
= tmp
, dst
= real}) :: acts
)
505 else if List.exists (fn prefix
=> String.isPrefix prefix
real) prefixes
then
506 loopReal ((site
, dom
, realPath
, Delete
' real) :: acts
)
511 val acts
= loopReal acts
513 val dir
= Posix
.FileSys
.opendir tmpPath
516 case Posix
.FileSys
.readdir dir
of
517 NONE
=> (Posix
.FileSys
.closedir dir
;
521 val real = OS
.Path
.joinDirFile
{dir
= realPath
,
523 val tmp
= OS
.Path
.joinDirFile
{dir
= tmpPath
,
526 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat tmp
) then
528 else if Posix
.FileSys
.access (real, []) then
531 loopTmp ((site
, dom
, realPath
, Add
' {src
= tmp
, dst
= real}) :: acts
)
534 val acts
= loopTmp acts
539 fun findAllDiffs prefixes
=
541 val dir
= Posix
.FileSys
.opendir Config
.tmpDir
542 val len
= length (String.fields (fn ch
=> ch
= #
"/") Config
.tmpDir
) + 1
544 fun exploreSites diffs
=
545 case Posix
.FileSys
.readdir dir
of
549 fun explore (dname
, diffs
) =
551 val dir
= Posix
.FileSys
.opendir dname
554 case Posix
.FileSys
.readdir dir
of
558 val fname
= OS
.Path
.joinDirFile
{dir
= dname
,
561 loop (if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat fname
) then
563 val dom
= String.fields (fn ch
=> ch
= #
"/") fname
564 val dom
= List.drop (dom
, len
)
565 val dom
= String.concatWith
"." (rev dom
)
567 val dname
' = OS
.Path
.joinDirFile
{dir
= dname
,
571 findDiffs (prefixes
, site
, dom
, diffs
))
578 before Posix
.FileSys
.closedir dir
581 exploreSites (explore (OS
.Path
.joinDirFile
{dir
= Config
.tmpDir
,
582 file
= site
}, diffs
))
586 before Posix
.FileSys
.closedir dir
589 val masterNode
: string option ref
= ref NONE
590 fun dnsMaster () = !masterNode
592 val seenDomains
: string list ref
= ref
[]
594 val _
= Env
.containerV_one
"domain"
595 ("domain", Env
.string)
598 val () = seenDomains
:= dom
:: !seenDomains
600 val kind
= Env
.env
dnsKind (evs
, "DNS")
601 val ttl
= Env
.env Env
.int (evs
, "TTL")
602 val aliases
= Env
.env (Env
.list Env
.string) (evs
, "Aliases")
604 val path
= getPath dom
606 val () = (current
:= dom
;
607 currentsAli
:= Slave
.remove (Slave
.removeDups aliases
, dom
);
608 currentPath
:= (fn site
=> path (Config
.tmpDir
, site
));
609 currentPathAli
:= (fn (dom
, site
) => getPath
dom (Config
.tmpDir
, site
)))
611 fun saveSoa (kind
, soa
: soa
) node
=
613 val {write
, writeDom
, close
} = domainsFile
{node
= node
, name
= "soa"}
617 write (Int.toString ttl
);
623 | SOME n
=> write (Int.toString n
);
625 write (Int.toString (#ref soa
));
627 write (Int.toString (#ret soa
));
629 write (Int.toString (#exp soa
));
631 write (Int.toString (#min soa
));
636 fun saveNamed (kind
, soa
: soa
, masterIp
, slaveIps
) node
=
637 if dom
= "localhost" then
640 val {write
, writeDom
, close
} = domainsFile
{node
= node
, name
= "named.conf"}
644 write
"\" {\n\ttype ";
646 write
";\n\tfile \"";
647 write Config
.Bind
.zonePath_real
;
652 "master" => (write
"\tallow-transfer {\n";
653 app (fn ip
=> (write
"\t\t";
655 write
";\n")) slaveIps
;
657 | _
=> (write
"\tmasters { ";
660 write
"// Updated: ";
661 write (Time
.toString (Time
.now ()));
668 NoDns
=> masterNode
:= NONE
673 InternalMaster node
=> nodeIp node
674 | ExternalMaster ip
=> ip
676 val slaveIps
= map
nodeIp (#slaves dns
)
678 app (saveSoa ("slave", #soa dns
)) (#slaves dns
);
679 app (saveNamed ("slave", #soa dns
, masterIp
, slaveIps
)) (#slaves dns
);
681 InternalMaster node
=>
682 (masterNode
:= SOME node
;
683 saveSoa ("master", #soa dns
) node
;
684 saveNamed ("master", #soa dns
, masterIp
, slaveIps
) node
)
685 | _
=> masterNode
:= NONE
689 fn () => !afters (!current
))
691 val () = Env
.registerPre (fn () => (seenDomains
:= [];
692 ignore (Slave
.shellF ([Config
.rm
, " -rf ", Config
.tmpDir
, ""],
693 fn cl
=> "Temp file cleanup failed: " ^ cl
));
694 OS
.FileSys
.mkDir Config
.tmpDir
;
695 app (fn node
=> OS
.FileSys
.mkDir
696 (OS
.Path
.joinDirFile
{dir
= Config
.tmpDir
,
699 app (fn node
=> OS
.FileSys
.mkDir
700 (OS
.Path
.joinDirFile
{dir
= Config
.resultRoot
,
702 handle OS
.SysErr _
=> ())
705 fun handleSite (site
, files
) =
709 print ("New configuration for node " ^ site ^
"\n");
710 if site
= Config
.defaultNode
then
711 Slave
.handleChanges files
713 val bio
= OpenSSL
.connect
true (valOf (!ssl_context
),
716 ^
Int.toString Config
.slavePort
)
718 app (fn file
=> Msg
.send (bio
, MsgFile file
)) files
;
719 Msg
.send (bio
, MsgDoFiles
);
721 NONE
=> print
"Slave closed connection unexpectedly\n"
724 MsgOk
=> print ("Slave " ^ site ^
" finished\n")
725 | MsgError s
=> print ("Slave " ^ site
726 ^
" returned error: " ^
728 | _
=> print ("Slave " ^ site
729 ^
" returned unexpected command\n");
734 val () = Env
.registerPost (fn () =>
736 val prefixes
= List.concat
739 val pieces
= String.tokens (fn ch
=> ch
= #
".") dom
740 val path
= String.concatWith
"/" (rev pieces
)
743 Config
.resultRoot ^
"/" ^ node ^
"/" ^ path ^
"/")
747 val diffs
= findAllDiffs prefixes
749 val diffs
= map (fn (site
, dom
, dir
, Add
' {src
, dst
}) =>
750 (Slave
.shellF ([Config
.cp
, " ", src
, " ", dst
],
751 fn cl
=> "Copy failed: " ^ cl
);
757 |
(site
, dom
, dir
, Delete
' dst
) =>
758 (OS
.FileSys
.remove dst
759 handle OS
.SysErr _
=>
760 ErrorMsg
.error
NONE ("Delete failed for " ^ dst
);
762 {action
= Slave
.Delete
true,
766 |
(site
, dom
, dir
, Modify
' {src
, dst
}) =>
767 (Slave
.shellF ([Config
.cp
, " ", src
, " ", dst
],
768 fn cl
=> "Copy failed: " ^ cl
);
770 {action
= Slave
.Modify
,
775 if !ErrorMsg
.anyErrors
then
778 val changed
= foldl (fn ((site
, file
), changed
) =>
780 val ls
= case SM
.find (changed
, site
) of
784 SM
.insert (changed
, site
, file
:: ls
)
787 SM
.appi handleSite changed
789 ignore (Slave
.shellF ([Config
.rm
, " -rf ", Config
.tmpDir
, ""],
790 fn cl
=> "Temp file cleanup failed: " ^ cl
))
793 fun hasPriv priv
= Acl
.query
{user
= getUser (), class
= "priv", value
= "all"}
794 orelse Acl
.query
{user
= getUser (), class
= "priv", value
= priv
}
796 val _
= Env
.type_one
"dns_node"
799 List.exists (fn x
=> x
= node
) Config
.dnsNodes_all
800 orelse (hasPriv
"dns"
801 andalso List.exists (fn x
=> x
= node
) Config
.dnsNodes_admin
))
803 val _
= Env
.type_one
"mail_node"
806 List.exists (fn x
=> x
= node
) Config
.mailNodes_all
807 orelse (hasPriv
"mail"
808 andalso List.exists (fn x
=> x
= node
) Config
.mailNodes_admin
))
810 fun rmdom
' delete resultRoot doms
=
812 fun doNode (node
, _
) =
814 val dname
= OS
.Path
.joinDirFile
{dir
= resultRoot
,
817 fun doDom (dom
, actions
) =
819 val domPath
= String.concatWith
"/" (rev (String.fields (fn ch
=> ch
= #
".") dom
))
820 val dname
= OS
.Path
.concat (dname
, domPath
)
822 fun visitDom (dom
, dname
, actions
) =
824 val dir
= Posix
.FileSys
.opendir dname
827 case Posix
.FileSys
.readdir dir
of
831 val fnameFull
= OS
.Path
.joinDirFile
{dir
= dname
,
834 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat fnameFull
) then
835 loop (visitDom (fname ^
"." ^ dom
,
839 loop ({action
= Slave
.Delete delete
,
842 file
= fnameFull
} :: actions
)
846 before Posix
.FileSys
.closedir dir
848 handle OS
.SysErr (s
, _
) =>
849 (print ("Warning: System error deleting domain " ^ dom ^
" on " ^ node ^
": " ^ s ^
"\n");
852 visitDom (dom
, dname
, actions
)
855 val actions
= foldl doDom
[] doms
857 handleSite (node
, actions
)
859 handle IO
.Io _
=> print ("Warning: IO error deleting domains on " ^ node ^
".\n")
861 fun cleanupNode (node
, _
) =
865 val domPath
= String.concatWith
"/" (rev (String.fields (fn ch
=> ch
= #
".") dom
))
866 val dname
= OS
.Path
.joinDirFile
{dir
= resultRoot
,
868 val dname
= OS
.Path
.concat (dname
, domPath
)
871 ignore (OS
.Process
.system (Config
.rm ^
" -rf " ^ dname
))
879 app doNode Config
.nodeIps
;
880 app cleanupNode Config
.nodeIps
883 val rmdom
= rmdom
' true Config
.resultRoot
884 val rmdom
' = rmdom
' false
886 fun homedirOf uname
=
887 Posix
.SysDB
.Passwd
.home (Posix
.SysDB
.getpwnam uname
)
889 fun homedir () = homedirOf (getUser ())
891 type subject
= {node
: string, domain
: string}
893 val describers
: (subject
-> string) list ref
= ref
[]
895 fun registerDescriber f
= describers
:= f
:: !describers
897 fun describeOne arg
= String.concat (map (fn f
=> f arg
) (rev (!describers
)))
899 val line
= "--------------------------------------------------------------\n"
900 val dline
= "==============================================================\n"
903 String.concat (List.mapPartial
905 case describeOne
{node
= node
, domain
= dom
} of
908 SOME (String.concat
[dline
, "Node ", node
, "\n", dline
, "\n", s
]))
911 datatype description
=
912 Filename
of { filename
: string, heading
: string, showEmpty
: bool }
913 | Extension
of { extension
: string, heading
: string -> string }
915 fun considerAll ds
{node
, domain
} =
917 val ds
= map (fn d
=> (d
, ref
[])) ds
919 val path
= Config
.resultRoot
920 val jdf
= OS
.Path
.joinDirFile
921 val path
= jdf
{dir
= path
, file
= node
}
922 val path
= foldr (fn (more
, path
) => jdf
{dir
= path
, file
= more
})
923 path (String.tokens (fn ch
=> ch
= #
".") domain
)
925 if Posix
.FileSys
.access (path
, []) then
927 val dir
= Posix
.FileSys
.opendir path
930 case Posix
.FileSys
.readdir dir
of
933 (app (fn (d
, entries
) =>
935 fun readFile showEmpty entries
' =
937 val fname
= OS
.Path
.joinDirFile
{dir
= path
,
940 val inf
= TextIO.openIn fname
942 fun loop (seenOne
, entries
') =
943 case TextIO.inputLine inf
of
944 NONE
=> if seenOne
orelse showEmpty
then
948 | SOME line
=> loop (true, line
:: entries
')
950 loop (false, entries
')
951 before TextIO.closeIn inf
955 Filename
{filename
, heading
, showEmpty
} =>
956 if fname
= filename
then
957 entries
:= readFile
showEmpty ("\n" :: line
:: "\n" :: heading
:: line
:: !entries
)
960 | Extension
{extension
, heading
} =>
962 val {base
, ext
} = OS
.Path
.splitBaseExt fname
967 if extension
' = extension
then
968 entries
:= readFile
true ("\n" :: line
:: "\n" :: heading base
:: line
:: !entries
)
976 Posix
.FileSys
.closedir dir
;
977 String.concat (List.concat (map (fn (_
, entries
) => rev (!entries
)) ds
))
983 val () = registerDescriber (considerAll
[Filename
{filename
= "soa",
984 heading
= "DNS SOA:",
987 val () = Env
.registerAction ("domainHost",
988 fn (env
, [(EString host
, _
)]) =>
989 SM
.insert (env
, "Hostname",
990 (EString (host ^
"." ^
currentDomain ()), dl
))
991 |
(_
, args
) => Env
.badArgs ("domainHost", args
))