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
= SS
.member (your_users (), s
)
136 fun yourGroup s
= SS
.member (your_groups (), s
)
137 fun checkPath paths path
=
138 (List.all (fn s
=> s
<> "..") (String.fields (fn ch
=> ch
= #
"/") path
)
139 andalso CharVector
.all (fn ch
=> Char.isAlphaNum ch
orelse ch
= #
"." orelse ch
= #
"/"
140 orelse ch
= #
"-" orelse ch
= #
"_") path
141 andalso SS
.exists (fn s
' => path
= s
' orelse String.isPrefix (s
' ^
"/") path
) (paths ()))
142 val yourPath
= checkPath your_paths
143 val readablePath
= checkPath readable_paths
144 fun yourIp s
= !fakePrivs
orelse SS
.member (your_ips (), s
)
146 fun yourDomainHost s
=
150 val (pref
, suf
) = Substring
.splitl (fn ch
=> ch
<> #
".") (Substring
.full s
)
152 Substring
.size suf
> 0
153 andalso validHost (Substring
.string pref
)
154 andalso yourDomain (Substring
.string
155 (Substring
.slice (suf
, 1, NONE
)))
158 val yourDomain
= yourDomainHost
160 fun validUser s
= size s
> 0 andalso size s
< 20
161 andalso CharVector
.all
Char.isAlphaNum s
163 fun validEmailUser s
=
164 size s
> 0 andalso size s
< 50
165 andalso CharVector
.all (fn ch
=> Char.isAlphaNum ch
171 val validGroup
= validUser
173 val _
= Env
.type_one
"no_spaces"
175 (CharVector
.all (fn ch
=> Char.isPrint ch
andalso not (Char.isSpace ch
)
176 andalso ch
<> #
"\"" andalso ch
<> #
"'"))
177 val _
= Env
.type_one
"no_newlines"
179 (CharVector
.all (fn ch
=> Char.isPrint ch
andalso ch
<> #
"\n" andalso ch
<> #
"\r"
180 andalso ch
<> #
"\""))
182 val _
= Env
.type_one
"ip"
186 val _
= Env
.type_one
"ipv6"
190 val _
= Env
.type_one
"host"
194 val _
= Env
.type_one
"domain"
198 val _
= Env
.type_one
"your_domain"
202 val _
= Env
.type_one
"your_domain_host"
206 val _
= Env
.type_one
"user"
210 val _
= Env
.type_one
"group"
214 val _
= Env
.type_one
"your_user"
218 val _
= Env
.type_one
"your_group"
222 val _
= Env
.type_one
"your_path"
226 val _
= Env
.type_one
"readable_path"
230 val _
= Env
.type_one
"your_ip"
234 val _
= Env
.type_one
"node"
238 val _
= Env
.registerFunction ("your_ip_to_ip",
242 val _
= Env
.registerFunction ("dns_node_to_node",
246 val _
= Env
.registerFunction ("mail_node_to_node",
253 val dl
= ErrorMsg
.dummyLoc
255 val _
= Env
.registerFunction ("end_in_slash",
256 fn [(EString
"", _
)] => SOME (EString
"/", dl
)
257 |
[(EString s
, _
)] =>
258 SOME (EString (if String.sub (s
, size s
- 1) = #
"/" then
265 val nsD
= (EString Config
.defaultNs
, dl
)
266 val serialD
= (EVar
"serialAuto", dl
)
267 val refD
= (EInt Config
.defaultRefresh
, dl
)
268 val retD
= (EInt Config
.defaultRetry
, dl
)
269 val expD
= (EInt Config
.defaultExpiry
, dl
)
270 val minD
= (EInt Config
.defaultMinimum
, dl
)
272 val soaD
= multiApp ((EVar
"soa", dl
),
274 [nsD
, serialD
, refD
, retD
, expD
, minD
])
276 val masterD
= (EApp ((EVar
"internalMaster", dl
),
277 (EString Config
.masterNode
, dl
)),
280 val slavesD
= (EList (map (fn s
=> (EString s
, dl
)) Config
.slaveNodes
), dl
)
282 val _
= Defaults
.registerDefault ("Aliases",
283 (TList (TBase
"your_domain", dl
), dl
),
284 (fn () => (EList
[], dl
)))
286 val _
= Defaults
.registerDefault ("Mailbox",
288 (fn () => (EString (getUser ()), dl
)))
290 val _
= Defaults
.registerDefault ("DNS",
291 (TBase
"dnsKind", dl
),
292 (fn () => multiApp ((EVar
"useDns", dl
),
294 [soaD
, masterD
, slavesD
])))
296 val _
= Defaults
.registerDefault ("TTL",
298 (fn () => (EInt Config
.Bind
.defaultTTL
, dl
)))
300 type soa
= {ns
: string,
307 val serial
= fn (EVar
"serialAuto", _
) => SOME NONE
308 |
(EApp ((EVar
"serialConst", _
), n
), _
) => Option
.map
SOME (Env
.int n
)
311 val soa
= fn (EApp ((EApp ((EApp ((EApp ((EApp ((EApp
312 ((EVar
"soa", _
), ns
), _
),
318 (case (Env
.string ns
, serial sl
, Env
.int rf
,
319 Env
.int ret
, Env
.int exp
, Env
.int min
) of
320 (SOME ns
, SOME sl
, SOME rf
,
321 SOME ret
, SOME exp
, SOME min
) =>
332 ExternalMaster
of string
333 | InternalMaster
of string
337 val _
= Env
.registerFunction ("ip_of_node",
338 fn [(EString node
, _
)] => SOME (EString (nodeIp node
), dl
)
341 val master
= fn (EApp ((EVar
"externalMaster", _
), e
), _
) => Option
.map
ExternalMaster (ip e
)
342 |
(EApp ((EVar
"internalMaster", _
), e
), _
) => Option
.map
InternalMaster (Env
.string e
)
346 UseDns
of {soa
: soa
,
348 slaves
: string list
}
351 val dnsKind
= fn (EApp ((EApp ((EApp
352 ((EVar
"useDns", _
), sa
), _
),
355 (case (soa sa
, master mstr
, Env
.list Env
.string slaves
) of
356 (SOME sa
, SOME mstr
, SOME slaves
) =>
357 SOME (UseDns
{soa
= sa
,
361 |
(EVar
"noDns", _
) => SOME NoDns
364 val befores
= ref (fn (_
: string) => ())
365 val afters
= ref (fn (_
: string) => ())
367 fun registerBefore f
=
371 befores
:= (fn x
=> (old x
; f x
))
374 fun registerAfter f
=
378 afters
:= (fn x
=> (old x
; f x
))
381 val globals
= ref (fn () => ())
382 val locals
= ref (fn () => ())
384 fun registerResetGlobal f
=
388 globals
:= (fn x
=> (old x
; f x
))
391 fun registerResetLocal f
=
395 locals
:= (fn x
=> (old x
; f x
))
398 fun resetGlobal () = (!globals ();
399 ignore (OS
.Process
.system (Config
.rm ^
" -rf " ^ Config
.resultRoot ^
"/*")))
400 fun resetLocal () = !locals ()
403 val currentPath
= ref (fn (_
: string) => "")
404 val currentPathAli
= ref (fn (_
: string, _
: string) => "")
408 fun currentDomain () = !current
410 val currentsAli
= ref ([] : string list
)
412 fun currentAliasDomains () = !currentsAli
413 fun currentDomains () = currentDomain () :: currentAliasDomains ()
415 fun domainFile
{node
, name
} = ((*print ("Opening " ^
!currentPath node ^ name ^
"\n");*)
416 TextIO.openOut (!currentPath node ^ name
))
418 type files
= {write
: string -> unit
,
419 writeDom
: unit
-> unit
,
420 close
: unit
-> unit
}
422 fun domainsFile
{node
, name
} =
424 val doms
= currentDomains ()
425 val files
= map (fn dom
=> (dom
, TextIO.openOut (!currentPathAli (dom
, node
) ^ name
))) doms
427 {write
= fn s
=> app (fn (_
, outf
) => TextIO.output (outf
, s
)) files
,
428 writeDom
= fn () => app (fn (dom
, outf
) => TextIO.output (outf
, dom
)) files
,
429 close
= fn () => app (fn (_
, outf
) => TextIO.closeOut outf
) files
}
434 val toks
= String.fields (fn ch
=> ch
= #
".") domain
436 val elems
= foldr (fn (piece
, elems
) =>
438 val elems
= piece
:: elems
442 val path
= String.concatWith
"/"
443 (Config
.resultRoot
:: node
:: rev elems
)
444 val tmpPath
= String.concatWith
"/"
445 (Config
.tmpDir
:: node
:: rev elems
)
447 (if Posix
.FileSys
.ST
.isDir
448 (Posix
.FileSys
.stat path
) then
451 (OS
.FileSys
.remove path
;
452 OS
.FileSys
.mkDir path
))
453 handle OS
.SysErr _
=> OS
.FileSys
.mkDir path
;
455 (if Posix
.FileSys
.ST
.isDir
456 (Posix
.FileSys
.stat tmpPath
) then
459 (OS
.FileSys
.remove tmpPath
;
460 OS
.FileSys
.mkDir tmpPath
))
461 handle OS
.SysErr _
=> OS
.FileSys
.mkDir tmpPath
468 fn (root
, site
) => String.concatWith
"/" (root
:: site
:: rev ("" :: elems
))
471 datatype file_action
' =
472 Add
' of {src
: string, dst
: string}
474 | Modify
' of {src
: string, dst
: string}
476 fun findDiffs (prefixes
, site
, dom
, acts
) =
479 val realPath
= gp (Config
.resultRoot
, site
)
480 val tmpPath
= gp (Config
.tmpDir
, site
)
482 (*val _
= print ("getDiffs(" ^ site ^
", " ^ dom ^
")... " ^ realPath ^
"; " ^ tmpPath ^
"\n")*)
484 val dir
= Posix
.FileSys
.opendir realPath
487 case Posix
.FileSys
.readdir dir
of
488 NONE
=> (Posix
.FileSys
.closedir dir
;
492 val real = OS
.Path
.joinDirFile
{dir
= realPath
,
494 val tmp
= OS
.Path
.joinDirFile
{dir
= tmpPath
,
497 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat
real) then
499 else if Posix
.FileSys
.access (tmp
, []) then
500 if Slave
.shell
[Config
.diff
, " ", real, " ", tmp
] then
503 loopReal ((site
, dom
, realPath
, Modify
' {src
= tmp
, dst
= real}) :: acts
)
504 else if List.exists (fn prefix
=> String.isPrefix prefix
real) prefixes
then
505 loopReal ((site
, dom
, realPath
, Delete
' real) :: acts
)
510 val acts
= loopReal acts
512 val dir
= Posix
.FileSys
.opendir tmpPath
515 case Posix
.FileSys
.readdir dir
of
516 NONE
=> (Posix
.FileSys
.closedir dir
;
520 val real = OS
.Path
.joinDirFile
{dir
= realPath
,
522 val tmp
= OS
.Path
.joinDirFile
{dir
= tmpPath
,
525 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat tmp
) then
527 else if Posix
.FileSys
.access (real, []) then
530 loopTmp ((site
, dom
, realPath
, Add
' {src
= tmp
, dst
= real}) :: acts
)
533 val acts
= loopTmp acts
538 fun findAllDiffs prefixes
=
540 val dir
= Posix
.FileSys
.opendir Config
.tmpDir
541 val len
= length (String.fields (fn ch
=> ch
= #
"/") Config
.tmpDir
) + 1
543 fun exploreSites diffs
=
544 case Posix
.FileSys
.readdir dir
of
548 fun explore (dname
, diffs
) =
550 val dir
= Posix
.FileSys
.opendir dname
553 case Posix
.FileSys
.readdir dir
of
557 val fname
= OS
.Path
.joinDirFile
{dir
= dname
,
560 loop (if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat fname
) then
562 val dom
= String.fields (fn ch
=> ch
= #
"/") fname
563 val dom
= List.drop (dom
, len
)
564 val dom
= String.concatWith
"." (rev dom
)
566 val dname
' = OS
.Path
.joinDirFile
{dir
= dname
,
570 findDiffs (prefixes
, site
, dom
, diffs
))
577 before Posix
.FileSys
.closedir dir
580 exploreSites (explore (OS
.Path
.joinDirFile
{dir
= Config
.tmpDir
,
581 file
= site
}, diffs
))
585 before Posix
.FileSys
.closedir dir
588 val masterNode
: string option ref
= ref NONE
589 fun dnsMaster () = !masterNode
591 val seenDomains
: string list ref
= ref
[]
593 val _
= Env
.containerV_one
"domain"
594 ("domain", Env
.string)
597 val () = seenDomains
:= dom
:: !seenDomains
599 val kind
= Env
.env
dnsKind (evs
, "DNS")
600 val ttl
= Env
.env Env
.int (evs
, "TTL")
601 val aliases
= Env
.env (Env
.list Env
.string) (evs
, "Aliases")
603 val path
= getPath dom
605 val () = (current
:= dom
;
606 currentsAli
:= Slave
.remove (Slave
.removeDups aliases
, dom
);
607 currentPath
:= (fn site
=> path (Config
.tmpDir
, site
));
608 currentPathAli
:= (fn (dom
, site
) => getPath
dom (Config
.tmpDir
, site
)))
610 fun saveSoa (kind
, soa
: soa
) node
=
612 val {write
, writeDom
, close
} = domainsFile
{node
= node
, name
= "soa.conf"}
616 write (Int.toString ttl
);
622 | SOME n
=> write (Int.toString n
);
624 write (Int.toString (#ref soa
));
626 write (Int.toString (#ret soa
));
628 write (Int.toString (#exp soa
));
630 write (Int.toString (#min soa
));
635 fun saveNamed (kind
, soa
: soa
, masterIp
, slaveIps
) node
=
636 if dom
= "localhost" then
639 val {write
, writeDom
, close
} = domainsFile
{node
= node
, name
= "named.conf"}
643 write
"\" {\n\ttype ";
645 write
";\n\tfile \"";
646 write Config
.Bind
.zonePath_real
;
651 "master" => (write
"\tallow-transfer {\n";
652 app (fn ip
=> (write
"\t\t";
654 write
";\n")) slaveIps
;
656 | _
=> (write
"\tmasters { ";
659 write
"// Updated: ";
660 write (Time
.toString (Time
.now ()));
667 NoDns
=> masterNode
:= NONE
672 InternalMaster node
=> nodeIp node
673 | ExternalMaster ip
=> ip
675 val slaveIps
= map
nodeIp (#slaves dns
)
677 app (saveSoa ("slave", #soa dns
)) (#slaves dns
);
678 app (saveNamed ("slave", #soa dns
, masterIp
, slaveIps
)) (#slaves dns
);
680 InternalMaster node
=>
681 (masterNode
:= SOME node
;
682 saveSoa ("master", #soa dns
) node
;
683 saveNamed ("master", #soa dns
, masterIp
, slaveIps
) node
)
684 | _
=> masterNode
:= NONE
688 fn () => !afters (!current
))
690 val () = Env
.registerPre (fn () => (seenDomains
:= [];
691 ignore (Slave
.shellF ([Config
.rm
, " -rf ", Config
.tmpDir
, ""],
692 fn cl
=> "Temp file cleanup failed: " ^ cl
));
693 OS
.FileSys
.mkDir Config
.tmpDir
;
694 app (fn node
=> OS
.FileSys
.mkDir
695 (OS
.Path
.joinDirFile
{dir
= Config
.tmpDir
,
698 app (fn node
=> OS
.FileSys
.mkDir
699 (OS
.Path
.joinDirFile
{dir
= Config
.resultRoot
,
701 handle OS
.SysErr _
=> ())
704 fun handleSite (site
, files
) =
708 print ("New configuration for node " ^ site ^
"\n");
709 if site
= Config
.defaultNode
then
710 Slave
.handleChanges files
712 val bio
= OpenSSL
.connect
true (valOf (!ssl_context
),
715 ^
Int.toString Config
.slavePort
)
717 app (fn file
=> Msg
.send (bio
, MsgFile file
)) files
;
718 Msg
.send (bio
, MsgDoFiles
);
720 NONE
=> print
"Slave closed connection unexpectedly\n"
723 MsgOk
=> print ("Slave " ^ site ^
" finished\n")
724 | MsgError s
=> print ("Slave " ^ site
725 ^
" returned error: " ^
727 | _
=> print ("Slave " ^ site
728 ^
" returned unexpected command\n");
733 val () = Env
.registerPost (fn () =>
735 val prefixes
= List.concat
738 val pieces
= String.tokens (fn ch
=> ch
= #
".") dom
739 val path
= String.concatWith
"/" (rev pieces
)
742 Config
.resultRoot ^
"/" ^ node ^
"/" ^ path ^
"/")
746 val diffs
= findAllDiffs prefixes
748 val diffs
= map (fn (site
, dom
, dir
, Add
' {src
, dst
}) =>
749 (Slave
.shellF ([Config
.cp
, " ", src
, " ", dst
],
750 fn cl
=> "Copy failed: " ^ cl
);
756 |
(site
, dom
, dir
, Delete
' dst
) =>
757 (OS
.FileSys
.remove dst
758 handle OS
.SysErr _
=>
759 ErrorMsg
.error
NONE ("Delete failed for " ^ dst
);
761 {action
= Slave
.Delete
true,
765 |
(site
, dom
, dir
, Modify
' {src
, dst
}) =>
766 (Slave
.shellF ([Config
.cp
, " ", src
, " ", dst
],
767 fn cl
=> "Copy failed: " ^ cl
);
769 {action
= Slave
.Modify
,
774 if !ErrorMsg
.anyErrors
then
777 val changed
= foldl (fn ((site
, file
), changed
) =>
779 val ls
= case SM
.find (changed
, site
) of
783 SM
.insert (changed
, site
, file
:: ls
)
786 SM
.appi handleSite changed
788 ignore (Slave
.shellF ([Config
.rm
, " -rf ", Config
.tmpDir
, ""],
789 fn cl
=> "Temp file cleanup failed: " ^ cl
))
792 fun hasPriv priv
= Acl
.query
{user
= getUser (), class
= "priv", value
= "all"}
793 orelse Acl
.query
{user
= getUser (), class
= "priv", value
= priv
}
795 val _
= Env
.type_one
"dns_node"
798 List.exists (fn x
=> x
= node
) Config
.dnsNodes_all
799 orelse (hasPriv
"dns"
800 andalso List.exists (fn x
=> x
= node
) Config
.dnsNodes_admin
))
802 val _
= Env
.type_one
"mail_node"
805 List.exists (fn x
=> x
= node
) Config
.mailNodes_all
806 orelse (hasPriv
"mail"
807 andalso List.exists (fn x
=> x
= node
) Config
.mailNodes_admin
))
809 fun rmdom
' delete resultRoot doms
=
811 fun doNode (node
, _
) =
813 val dname
= OS
.Path
.joinDirFile
{dir
= resultRoot
,
816 fun doDom (dom
, actions
) =
818 val domPath
= String.concatWith
"/" (rev (String.fields (fn ch
=> ch
= #
".") dom
))
819 val dname
= OS
.Path
.concat (dname
, domPath
)
821 fun visitDom (dom
, dname
, actions
) =
823 val dir
= Posix
.FileSys
.opendir dname
826 case Posix
.FileSys
.readdir dir
of
830 val fnameFull
= OS
.Path
.joinDirFile
{dir
= dname
,
833 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat fnameFull
) then
834 loop (visitDom (fname ^
"." ^ dom
,
838 loop ({action
= Slave
.Delete delete
,
841 file
= fnameFull
} :: actions
)
845 before Posix
.FileSys
.closedir dir
847 handle OS
.SysErr (s
, _
) =>
848 (print ("Warning: System error deleting domain " ^ dom ^
" on " ^ node ^
": " ^ s ^
"\n");
851 visitDom (dom
, dname
, actions
)
854 val actions
= foldl doDom
[] doms
856 handleSite (node
, actions
)
858 handle IO
.Io _
=> print ("Warning: IO error deleting domains on " ^ node ^
".\n")
860 fun cleanupNode (node
, _
) =
864 val domPath
= String.concatWith
"/" (rev (String.fields (fn ch
=> ch
= #
".") dom
))
865 val dname
= OS
.Path
.joinDirFile
{dir
= resultRoot
,
867 val dname
= OS
.Path
.concat (dname
, domPath
)
870 ignore (OS
.Process
.system (Config
.rm ^
" -rf " ^ dname
))
878 app doNode Config
.nodeIps
;
879 app cleanupNode Config
.nodeIps
882 val rmdom
= rmdom
' true Config
.resultRoot
883 val rmdom
' = rmdom
' false
885 fun homedirOf uname
=
886 Posix
.SysDB
.Passwd
.home (Posix
.SysDB
.getpwnam uname
)
888 fun homedir () = homedirOf (getUser ())
890 type subject
= {node
: string, domain
: string}
892 val describers
: (subject
-> string) list ref
= ref
[]
894 fun registerDescriber f
= describers
:= f
:: !describers
896 fun describeOne arg
= String.concat (map (fn f
=> f arg
) (rev (!describers
)))
898 val line
= "--------------------------------------------------------------\n"
899 val dline
= "==============================================================\n"
902 String.concat (List.mapPartial
904 case describeOne
{node
= node
, domain
= dom
} of
907 SOME (String.concat
[dline
, "Node ", node
, "\n", dline
, "\n", s
]))
910 datatype description
=
911 Filename
of { filename
: string, heading
: string, showEmpty
: bool }
912 | Extension
of { extension
: string, heading
: string -> string }
914 fun considerAll ds
{node
, domain
} =
916 val ds
= map (fn d
=> (d
, ref
[])) ds
918 val path
= Config
.resultRoot
919 val jdf
= OS
.Path
.joinDirFile
920 val path
= jdf
{dir
= path
, file
= node
}
921 val path
= foldr (fn (more
, path
) => jdf
{dir
= path
, file
= more
})
922 path (String.tokens (fn ch
=> ch
= #
".") domain
)
924 if Posix
.FileSys
.access (path
, []) then
926 val dir
= Posix
.FileSys
.opendir path
929 case Posix
.FileSys
.readdir dir
of
932 (app (fn (d
, entries
) =>
934 fun readFile showEmpty entries
' =
936 val fname
= OS
.Path
.joinDirFile
{dir
= path
,
939 val inf
= TextIO.openIn fname
941 fun loop (seenOne
, entries
') =
942 case TextIO.inputLine inf
of
943 NONE
=> if seenOne
orelse showEmpty
then
947 | SOME line
=> loop (true, line
:: entries
')
949 loop (false, entries
')
950 before TextIO.closeIn inf
954 Filename
{filename
, heading
, showEmpty
} =>
955 if fname
= filename
then
956 entries
:= readFile
showEmpty ("\n" :: line
:: "\n" :: heading
:: line
:: !entries
)
959 | Extension
{extension
, heading
} =>
961 val {base
, ext
} = OS
.Path
.splitBaseExt fname
966 if extension
' = extension
then
967 entries
:= readFile
true ("\n" :: line
:: "\n" :: heading base
:: line
:: !entries
)
975 Posix
.FileSys
.closedir dir
;
976 String.concat (List.concat (map (fn (_
, entries
) => rev (!entries
)) ds
))
982 val () = registerDescriber (considerAll
[Filename
{filename
= "soa.conf",
983 heading
= "DNS SOA:",
986 val () = Env
.registerAction ("domainHost",
987 fn (env
, [(EString host
, _
)]) =>
988 SM
.insert (env
, "Hostname",
989 (EString (host ^
"." ^
currentDomain ()), dl
))
990 |
(_
, args
) => Env
.badArgs ("domainHost", args
))