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
.type_one
"mime_type"
240 (CharVector
.exists (fn ch
=> ch
= #
"/"))
242 val _
= Env
.registerFunction ("your_ip_to_ip",
246 val _
= Env
.registerFunction ("dns_node_to_node",
250 val _
= Env
.registerFunction ("mail_node_to_node",
257 val dl
= ErrorMsg
.dummyLoc
259 val _
= Env
.registerFunction ("end_in_slash",
260 fn [(EString
"", _
)] => SOME (EString
"/", dl
)
261 |
[(EString s
, _
)] =>
262 SOME (EString (if String.sub (s
, size s
- 1) = #
"/" then
269 val nsD
= (EString Config
.defaultNs
, dl
)
270 val serialD
= (EVar
"serialAuto", dl
)
271 val refD
= (EInt Config
.defaultRefresh
, dl
)
272 val retD
= (EInt Config
.defaultRetry
, dl
)
273 val expD
= (EInt Config
.defaultExpiry
, dl
)
274 val minD
= (EInt Config
.defaultMinimum
, dl
)
276 val soaD
= multiApp ((EVar
"soa", dl
),
278 [nsD
, serialD
, refD
, retD
, expD
, minD
])
280 val masterD
= (EApp ((EVar
"internalMaster", dl
),
281 (EString Config
.masterNode
, dl
)),
284 val slavesD
= (EList (map (fn s
=> (EString s
, dl
)) Config
.slaveNodes
), dl
)
286 val _
= Defaults
.registerDefault ("Aliases",
287 (TList (TBase
"your_domain", dl
), dl
),
288 (fn () => (EList
[], dl
)))
290 val _
= Defaults
.registerDefault ("Mailbox",
292 (fn () => (EString (getUser ()), dl
)))
294 val _
= Defaults
.registerDefault ("DNS",
295 (TBase
"dnsKind", dl
),
296 (fn () => multiApp ((EVar
"useDns", dl
),
298 [soaD
, masterD
, slavesD
])))
300 val _
= Defaults
.registerDefault ("TTL",
302 (fn () => (EInt Config
.Bind
.defaultTTL
, dl
)))
304 type soa
= {ns
: string,
311 val serial
= fn (EVar
"serialAuto", _
) => SOME NONE
312 |
(EApp ((EVar
"serialConst", _
), n
), _
) => Option
.map
SOME (Env
.int n
)
315 val soa
= fn (EApp ((EApp ((EApp ((EApp ((EApp ((EApp
316 ((EVar
"soa", _
), ns
), _
),
322 (case (Env
.string ns
, serial sl
, Env
.int rf
,
323 Env
.int ret
, Env
.int exp
, Env
.int min
) of
324 (SOME ns
, SOME sl
, SOME rf
,
325 SOME ret
, SOME exp
, SOME min
) =>
336 ExternalMaster
of string
337 | InternalMaster
of string
341 val _
= Env
.registerFunction ("ip_of_node",
342 fn [(EString node
, _
)] => SOME (EString (nodeIp node
), dl
)
345 val master
= fn (EApp ((EVar
"externalMaster", _
), e
), _
) => Option
.map
ExternalMaster (ip e
)
346 |
(EApp ((EVar
"internalMaster", _
), e
), _
) => Option
.map
InternalMaster (Env
.string e
)
350 UseDns
of {soa
: soa
,
352 slaves
: string list
}
355 val dnsKind
= fn (EApp ((EApp ((EApp
356 ((EVar
"useDns", _
), sa
), _
),
359 (case (soa sa
, master mstr
, Env
.list Env
.string slaves
) of
360 (SOME sa
, SOME mstr
, SOME slaves
) =>
361 SOME (UseDns
{soa
= sa
,
365 |
(EVar
"noDns", _
) => SOME NoDns
368 val befores
= ref (fn (_
: string) => ())
369 val afters
= ref (fn (_
: string) => ())
371 fun registerBefore f
=
375 befores
:= (fn x
=> (old x
; f x
))
378 fun registerAfter f
=
382 afters
:= (fn x
=> (old x
; f x
))
385 val globals
= ref (fn () => ())
386 val locals
= ref (fn () => ())
388 fun registerResetGlobal f
=
392 globals
:= (fn x
=> (old x
; f x
))
395 fun registerResetLocal f
=
399 locals
:= (fn x
=> (old x
; f x
))
402 fun resetGlobal () = (!globals ();
403 ignore (OS
.Process
.system (Config
.rm ^
" -rf " ^ Config
.resultRoot ^
"/*")))
404 fun resetLocal () = !locals ()
407 val currentPath
= ref (fn (_
: string) => "")
408 val currentPathAli
= ref (fn (_
: string, _
: string) => "")
412 fun currentDomain () = !current
414 val currentsAli
= ref ([] : string list
)
416 fun currentAliasDomains () = !currentsAli
417 fun currentDomains () = currentDomain () :: currentAliasDomains ()
419 fun domainFile
{node
, name
} = ((*print ("Opening " ^
!currentPath node ^ name ^
"\n");*)
420 TextIO.openOut (!currentPath node ^ name
))
422 type files
= {write
: string -> unit
,
423 writeDom
: unit
-> unit
,
424 close
: unit
-> unit
}
426 fun domainsFile
{node
, name
} =
428 val doms
= currentDomains ()
429 val files
= map (fn dom
=> (dom
, TextIO.openOut (!currentPathAli (dom
, node
) ^ name
))) doms
431 {write
= fn s
=> app (fn (_
, outf
) => TextIO.output (outf
, s
)) files
,
432 writeDom
= fn () => app (fn (dom
, outf
) => TextIO.output (outf
, dom
)) files
,
433 close
= fn () => app (fn (_
, outf
) => TextIO.closeOut outf
) files
}
438 val toks
= String.fields (fn ch
=> ch
= #
".") domain
440 val elems
= foldr (fn (piece
, elems
) =>
442 val elems
= piece
:: elems
446 val path
= String.concatWith
"/"
447 (Config
.resultRoot
:: node
:: rev elems
)
448 val tmpPath
= String.concatWith
"/"
449 (Config
.tmpDir
:: node
:: rev elems
)
451 (if Posix
.FileSys
.ST
.isDir
452 (Posix
.FileSys
.stat path
) then
455 (OS
.FileSys
.remove path
;
456 OS
.FileSys
.mkDir path
))
457 handle OS
.SysErr _
=> OS
.FileSys
.mkDir path
;
459 (if Posix
.FileSys
.ST
.isDir
460 (Posix
.FileSys
.stat tmpPath
) then
463 (OS
.FileSys
.remove tmpPath
;
464 OS
.FileSys
.mkDir tmpPath
))
465 handle OS
.SysErr _
=> OS
.FileSys
.mkDir tmpPath
472 fn (root
, site
) => String.concatWith
"/" (root
:: site
:: rev ("" :: elems
))
475 datatype file_action
' =
476 Add
' of {src
: string, dst
: string}
478 | Modify
' of {src
: string, dst
: string}
480 fun findDiffs (prefixes
, site
, dom
, acts
) =
483 val realPath
= gp (Config
.resultRoot
, site
)
484 val tmpPath
= gp (Config
.tmpDir
, site
)
486 (*val _
= print ("getDiffs(" ^ site ^
", " ^ dom ^
")... " ^ realPath ^
"; " ^ tmpPath ^
"\n")*)
488 val dir
= Posix
.FileSys
.opendir realPath
491 case Posix
.FileSys
.readdir dir
of
492 NONE
=> (Posix
.FileSys
.closedir dir
;
496 val real = OS
.Path
.joinDirFile
{dir
= realPath
,
498 val tmp
= OS
.Path
.joinDirFile
{dir
= tmpPath
,
501 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat
real) then
503 else if Posix
.FileSys
.access (tmp
, []) then
504 if Slave
.shell
[Config
.diff
, " ", real, " ", tmp
] then
507 loopReal ((site
, dom
, realPath
, Modify
' {src
= tmp
, dst
= real}) :: acts
)
508 else if List.exists (fn prefix
=> String.isPrefix prefix
real) prefixes
then
509 loopReal ((site
, dom
, realPath
, Delete
' real) :: acts
)
514 val acts
= loopReal acts
516 val dir
= Posix
.FileSys
.opendir tmpPath
519 case Posix
.FileSys
.readdir dir
of
520 NONE
=> (Posix
.FileSys
.closedir dir
;
524 val real = OS
.Path
.joinDirFile
{dir
= realPath
,
526 val tmp
= OS
.Path
.joinDirFile
{dir
= tmpPath
,
529 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat tmp
) then
531 else if Posix
.FileSys
.access (real, []) then
534 loopTmp ((site
, dom
, realPath
, Add
' {src
= tmp
, dst
= real}) :: acts
)
537 val acts
= loopTmp acts
542 fun findAllDiffs prefixes
=
544 val dir
= Posix
.FileSys
.opendir Config
.tmpDir
545 val len
= length (String.fields (fn ch
=> ch
= #
"/") Config
.tmpDir
) + 1
547 fun exploreSites diffs
=
548 case Posix
.FileSys
.readdir dir
of
552 fun explore (dname
, diffs
) =
554 val dir
= Posix
.FileSys
.opendir dname
557 case Posix
.FileSys
.readdir dir
of
561 val fname
= OS
.Path
.joinDirFile
{dir
= dname
,
564 loop (if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat fname
) then
566 val dom
= String.fields (fn ch
=> ch
= #
"/") fname
567 val dom
= List.drop (dom
, len
)
568 val dom
= String.concatWith
"." (rev dom
)
570 val dname
' = OS
.Path
.joinDirFile
{dir
= dname
,
574 findDiffs (prefixes
, site
, dom
, diffs
))
581 before Posix
.FileSys
.closedir dir
584 exploreSites (explore (OS
.Path
.joinDirFile
{dir
= Config
.tmpDir
,
585 file
= site
}, diffs
))
589 before Posix
.FileSys
.closedir dir
592 val masterNode
: string option ref
= ref NONE
593 fun dnsMaster () = !masterNode
595 val seenDomains
: string list ref
= ref
[]
597 val _
= Env
.containerV_one
"domain"
598 ("domain", Env
.string)
601 val () = seenDomains
:= dom
:: !seenDomains
603 val kind
= Env
.env
dnsKind (evs
, "DNS")
604 val ttl
= Env
.env Env
.int (evs
, "TTL")
605 val aliases
= Env
.env (Env
.list Env
.string) (evs
, "Aliases")
607 val path
= getPath dom
609 val () = (current
:= dom
;
610 currentsAli
:= Slave
.remove (Slave
.removeDups aliases
, dom
);
611 currentPath
:= (fn site
=> path (Config
.tmpDir
, site
));
612 currentPathAli
:= (fn (dom
, site
) => getPath
dom (Config
.tmpDir
, site
)))
614 fun saveSoa (kind
, soa
: soa
) node
=
616 val {write
, writeDom
, close
} = domainsFile
{node
= node
, name
= "soa.conf"}
620 write (Int.toString ttl
);
626 | SOME n
=> write (Int.toString n
);
628 write (Int.toString (#ref soa
));
630 write (Int.toString (#ret soa
));
632 write (Int.toString (#exp soa
));
634 write (Int.toString (#min soa
));
639 fun saveNamed (kind
, soa
: soa
, masterIp
, slaveIps
) node
=
640 if dom
= "localhost" then
643 val {write
, writeDom
, close
} = domainsFile
{node
= node
, name
= "named.conf"}
647 write
"\" {\n\ttype ";
649 write
";\n\tfile \"";
650 write Config
.Bind
.zonePath_real
;
655 "master" => (write
"\tallow-transfer {\n";
656 app (fn ip
=> (write
"\t\t";
658 write
";\n")) slaveIps
;
660 | _
=> (write
"\tmasters { ";
663 write
"// Updated: ";
664 write (Time
.toString (Time
.now ()));
671 NoDns
=> masterNode
:= NONE
676 InternalMaster node
=> nodeIp node
677 | ExternalMaster ip
=> ip
679 val slaveIps
= map
nodeIp (#slaves dns
)
681 app (saveSoa ("slave", #soa dns
)) (#slaves dns
);
682 app (saveNamed ("slave", #soa dns
, masterIp
, slaveIps
)) (#slaves dns
);
684 InternalMaster node
=>
685 (masterNode
:= SOME node
;
686 saveSoa ("master", #soa dns
) node
;
687 saveNamed ("master", #soa dns
, masterIp
, slaveIps
) node
)
688 | _
=> masterNode
:= NONE
692 fn () => !afters (!current
))
694 val () = Env
.registerPre (fn () => (seenDomains
:= [];
695 ignore (Slave
.shellF ([Config
.rm
, " -rf ", Config
.tmpDir
, ""],
696 fn cl
=> "Temp file cleanup failed: " ^ cl
));
697 OS
.FileSys
.mkDir Config
.tmpDir
;
698 app (fn node
=> OS
.FileSys
.mkDir
699 (OS
.Path
.joinDirFile
{dir
= Config
.tmpDir
,
702 app (fn node
=> OS
.FileSys
.mkDir
703 (OS
.Path
.joinDirFile
{dir
= Config
.resultRoot
,
705 handle OS
.SysErr _
=> ())
708 fun handleSite (site
, files
) =
712 print ("New configuration for node " ^ site ^
"\n");
713 if site
= Config
.defaultNode
then
714 Slave
.handleChanges files
716 val bio
= OpenSSL
.connect
true (valOf (!ssl_context
),
719 ^
Int.toString Config
.slavePort
)
721 app (fn file
=> Msg
.send (bio
, MsgFile file
)) files
;
722 Msg
.send (bio
, MsgDoFiles
);
724 NONE
=> print
"Slave closed connection unexpectedly\n"
727 MsgOk
=> print ("Slave " ^ site ^
" finished\n")
728 | MsgError s
=> print ("Slave " ^ site
729 ^
" returned error: " ^
731 | _
=> print ("Slave " ^ site
732 ^
" returned unexpected command\n");
737 val () = Env
.registerPost (fn () =>
739 val prefixes
= List.concat
742 val pieces
= String.tokens (fn ch
=> ch
= #
".") dom
743 val path
= String.concatWith
"/" (rev pieces
)
746 Config
.resultRoot ^
"/" ^ node ^
"/" ^ path ^
"/")
750 val diffs
= findAllDiffs prefixes
752 val diffs
= map (fn (site
, dom
, dir
, Add
' {src
, dst
}) =>
753 (Slave
.shellF ([Config
.cp
, " ", src
, " ", dst
],
754 fn cl
=> "Copy failed: " ^ cl
);
760 |
(site
, dom
, dir
, Delete
' dst
) =>
761 (OS
.FileSys
.remove dst
762 handle OS
.SysErr _
=>
763 ErrorMsg
.error
NONE ("Delete failed for " ^ dst
);
765 {action
= Slave
.Delete
true,
769 |
(site
, dom
, dir
, Modify
' {src
, dst
}) =>
770 (Slave
.shellF ([Config
.cp
, " ", src
, " ", dst
],
771 fn cl
=> "Copy failed: " ^ cl
);
773 {action
= Slave
.Modify
,
778 if !ErrorMsg
.anyErrors
then
781 val changed
= foldl (fn ((site
, file
), changed
) =>
783 val ls
= case SM
.find (changed
, site
) of
787 SM
.insert (changed
, site
, file
:: ls
)
790 SM
.appi handleSite changed
792 ignore (Slave
.shellF ([Config
.rm
, " -rf ", Config
.tmpDir
, ""],
793 fn cl
=> "Temp file cleanup failed: " ^ cl
))
796 fun hasPriv priv
= Acl
.query
{user
= getUser (), class
= "priv", value
= "all"}
797 orelse Acl
.query
{user
= getUser (), class
= "priv", value
= priv
}
799 val _
= Env
.type_one
"dns_node"
802 List.exists (fn x
=> x
= node
) Config
.dnsNodes_all
803 orelse (hasPriv
"dns"
804 andalso List.exists (fn x
=> x
= node
) Config
.dnsNodes_admin
))
806 val _
= Env
.type_one
"mail_node"
809 List.exists (fn x
=> x
= node
) Config
.mailNodes_all
810 orelse (hasPriv
"mail"
811 andalso List.exists (fn x
=> x
= node
) Config
.mailNodes_admin
))
813 fun rmdom
' delete resultRoot doms
=
815 fun doNode (node
, _
) =
817 val dname
= OS
.Path
.joinDirFile
{dir
= resultRoot
,
820 fun doDom (dom
, actions
) =
822 val domPath
= String.concatWith
"/" (rev (String.fields (fn ch
=> ch
= #
".") dom
))
823 val dname
= OS
.Path
.concat (dname
, domPath
)
825 fun visitDom (dom
, dname
, actions
) =
827 val dir
= Posix
.FileSys
.opendir dname
830 case Posix
.FileSys
.readdir dir
of
834 val fnameFull
= OS
.Path
.joinDirFile
{dir
= dname
,
837 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat fnameFull
) then
838 loop (visitDom (fname ^
"." ^ dom
,
842 loop ({action
= Slave
.Delete delete
,
845 file
= fnameFull
} :: actions
)
849 before Posix
.FileSys
.closedir dir
851 handle OS
.SysErr (s
, _
) =>
852 (print ("Warning: System error deleting domain " ^ dom ^
" on " ^ node ^
": " ^ s ^
"\n");
855 visitDom (dom
, dname
, actions
)
858 val actions
= foldl doDom
[] doms
860 handleSite (node
, actions
)
862 handle IO
.Io _
=> print ("Warning: IO error deleting domains on " ^ node ^
".\n")
864 fun cleanupNode (node
, _
) =
868 val domPath
= String.concatWith
"/" (rev (String.fields (fn ch
=> ch
= #
".") dom
))
869 val dname
= OS
.Path
.joinDirFile
{dir
= resultRoot
,
871 val dname
= OS
.Path
.concat (dname
, domPath
)
874 ignore (OS
.Process
.system (Config
.rm ^
" -rf " ^ dname
))
882 app doNode Config
.nodeIps
;
883 app cleanupNode Config
.nodeIps
886 val rmdom
= rmdom
' true Config
.resultRoot
887 val rmdom
' = rmdom
' false
889 fun homedirOf uname
=
890 Posix
.SysDB
.Passwd
.home (Posix
.SysDB
.getpwnam uname
)
892 fun homedir () = homedirOf (getUser ())
894 type subject
= {node
: string, domain
: string}
896 val describers
: (subject
-> string) list ref
= ref
[]
898 fun registerDescriber f
= describers
:= f
:: !describers
900 fun describeOne arg
= String.concat (map (fn f
=> f arg
) (rev (!describers
)))
902 val line
= "--------------------------------------------------------------\n"
903 val dline
= "==============================================================\n"
906 String.concat (List.mapPartial
908 case describeOne
{node
= node
, domain
= dom
} of
911 SOME (String.concat
[dline
, "Node ", node
, "\n", dline
, "\n", s
]))
914 datatype description
=
915 Filename
of { filename
: string, heading
: string, showEmpty
: bool }
916 | Extension
of { extension
: string, heading
: string -> string }
918 fun considerAll ds
{node
, domain
} =
920 val ds
= map (fn d
=> (d
, ref
[])) ds
922 val path
= Config
.resultRoot
923 val jdf
= OS
.Path
.joinDirFile
924 val path
= jdf
{dir
= path
, file
= node
}
925 val path
= foldr (fn (more
, path
) => jdf
{dir
= path
, file
= more
})
926 path (String.tokens (fn ch
=> ch
= #
".") domain
)
928 if Posix
.FileSys
.access (path
, []) then
930 val dir
= Posix
.FileSys
.opendir path
933 case Posix
.FileSys
.readdir dir
of
936 (app (fn (d
, entries
) =>
938 fun readFile showEmpty entries
' =
940 val fname
= OS
.Path
.joinDirFile
{dir
= path
,
943 val inf
= TextIO.openIn fname
945 fun loop (seenOne
, entries
') =
946 case TextIO.inputLine inf
of
947 NONE
=> if seenOne
orelse showEmpty
then
951 | SOME line
=> loop (true, line
:: entries
')
953 loop (false, entries
')
954 before TextIO.closeIn inf
958 Filename
{filename
, heading
, showEmpty
} =>
959 if fname
= filename
then
960 entries
:= readFile
showEmpty ("\n" :: line
:: "\n" :: heading
:: line
:: !entries
)
963 | Extension
{extension
, heading
} =>
965 val {base
, ext
} = OS
.Path
.splitBaseExt fname
970 if extension
' = extension
then
971 entries
:= readFile
true ("\n" :: line
:: "\n" :: heading base
:: line
:: !entries
)
979 Posix
.FileSys
.closedir dir
;
980 String.concat (List.concat (map (fn (_
, entries
) => rev (!entries
)) ds
))
986 val () = registerDescriber (considerAll
[Filename
{filename
= "soa.conf",
987 heading
= "DNS SOA:",
990 val () = Env
.registerAction ("domainHost",
991 fn (env
, [(EString host
, _
)]) =>
992 SM
.insert (env
, "Hostname",
993 (EString (host ^
"." ^
currentDomain ()), dl
))
994 |
(_
, args
) => Env
.badArgs ("domainHost", args
))
996 val ouc
= ref (fn () => ())
998 fun registerOnUsersChange f
=
1002 ouc
:= (fn () => (f
' (); f ()))
1005 fun onUsersChange () = !ouc ()