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
30 fun get_context () = valOf (!ssl_context
)
32 val nodes
= map #
1 Config
.nodeIps
33 val nodeMap
= foldl (fn ((node
, ip
, ipv6
), mp
) => SM
.insert (mp
, node
, (ip
, ipv6
)))
34 SM
.empty Config
.nodeIps
35 fun nodeIp node
= #
1 (valOf (SM
.find (nodeMap
, node
)))
36 fun nodeIpv6 node
= #
2 (valOf (SM
.find (nodeMap
, node
)))
40 val fakePrivs
= ref
false
41 val isClient
= ref
false
43 val your_doms
= ref SS
.empty
44 fun your_domains () = !your_doms
46 val your_usrs
= ref SS
.empty
47 fun your_users () = !your_usrs
49 val your_grps
= ref SS
.empty
50 fun your_groups () = !your_grps
52 val your_pths
= ref SS
.empty
53 fun your_paths () = !your_pths
55 val your_ipss
= ref SS
.empty
56 fun your_ips () = !your_ipss
58 val your_ipv6ss
= ref SS
.empty
59 fun your_ipv6s () = !your_ipv6ss
61 val world_readable
= SS
.addList (SS
.empty
, Config
.worldReadable
)
62 val readable_pths
= ref world_readable
63 fun readable_paths () = !readable_pths
69 val your_paths
= Acl
.class
{user
= getUser (),
73 your_doms
:= Acl
.class
{user
= getUser (),
75 your_usrs
:= Acl
.class
{user
= getUser (),
77 your_grps
:= SS
.add (Acl
.class
{user
= getUser (),
80 your_pths
:= your_paths
;
81 readable_pths
:= SS
.union (your_paths
, world_readable
);
82 your_ipss
:= Acl
.class
{user
= getUser (),
84 your_ipv6ss
:= Acl
.class
{user
= getUser (),
88 fun declareClient () = isClient
:= true
89 fun fakePrivileges () = if !isClient
then
92 raise Fail
"Tried to fake privileges as non-client"
95 case map
Int.fromString (String.fields (fn ch
=> ch
= #
".") s
) of
96 [SOME n1
, SOME n2
, SOME n3
, SOME n4
] =>
97 n1
>= 0 andalso n1
< 256 andalso n2
>= 0 andalso n2
< 256 andalso n3
>= 0 andalso n3
< 256 andalso n4
>= 0 andalso n4
< 256
100 fun isHexDigit ch
= Char.isDigit ch
orelse (ord ch
>= ord #
"a" andalso ord ch
<= ord #
"f")
104 val fields
= String.fields (fn ch
=> ch
= #
":") s
106 val empties
= foldl (fn ("", n
) => n
+ 1
107 |
(_
, n
) => n
) 0 fields
111 andalso length fields
<= maxLen
113 andalso List.all (fn "" => true
115 andalso CharVector
.all isHexDigit s
) fields
121 val maybeIpv4
= List.last fields
122 val theRest
= List.take (fields
, length fields
- 1)
124 validIp maybeIpv4
andalso noIpv4
6
130 andalso List.last fields
= "1"
132 localHost () orelse noIpv4
8 orelse hasIpv4 ()
135 fun isIdent ch
= Char.isLower ch
orelse Char.isDigit ch
138 size s
> 0 andalso size s
< 50
139 andalso CharVector
.all (fn ch
=> isIdent ch
orelse ch
= #
"-") s
142 size s
> 0 andalso size s
< 200
143 andalso List.all
validHost (String.fields (fn ch
=> ch
= #
".") s
)
145 fun validNode s
= List.exists (fn s
' => s
= s
') nodes
147 fun yourDomain s
= !fakePrivs
orelse SS
.member (your_domains (), s
)
148 fun yourUser s
= !fakePrivs
orelse SS
.member (your_users (), s
)
149 fun yourGroup s
= !fakePrivs
orelse SS
.member (your_groups (), s
)
151 fun checkPath paths path
=
153 (List.all (fn s
=> s
<> "..") (String.fields (fn ch
=> ch
= #
"/") path
)
154 andalso CharVector
.all (fn ch
=> Char.isAlphaNum ch
orelse ch
= #
"." orelse ch
= #
"/"
155 orelse ch
= #
"-" orelse ch
= #
"_") path
156 andalso SS
.exists (fn s
' => path
= s
' orelse String.isPrefix (s
' ^
"/") path
) (paths ()))
157 val yourPath
= checkPath your_paths
158 val readablePath
= checkPath readable_paths
160 fun yourIp s
= !fakePrivs
orelse SS
.member (your_ips (), s
)
161 fun yourIpv6 s
= !fakePrivs
orelse SS
.member (your_ipv6s (), s
)
163 fun yourDomainHost s
=
167 val (pref
, suf
) = Substring
.splitl (fn ch
=> ch
<> #
".") (Substring
.full s
)
169 Substring
.size suf
> 0
170 andalso validHost (Substring
.string pref
)
171 andalso yourDomain (Substring
.string
172 (Substring
.slice (suf
, 1, NONE
)))
175 val yourDomain
= yourDomainHost
177 fun validUser s
= size s
> 0 andalso size s
< 20
178 andalso CharVector
.all
Char.isAlphaNum s
180 fun validEmailUser s
=
181 size s
> 0 andalso size s
< 50
182 andalso CharVector
.all (fn ch
=> Char.isAlphaNum ch
188 val validGroup
= validUser
190 val _
= Env
.type_one
"no_spaces"
192 (CharVector
.all (fn ch
=> Char.isPrint ch
andalso not (Char.isSpace ch
)
193 andalso ch
<> #
"\"" andalso ch
<> #
"'"))
194 val _
= Env
.type_one
"no_newlines"
196 (CharVector
.all (fn ch
=> Char.isPrint ch
andalso ch
<> #
"\n" andalso ch
<> #
"\r"
197 andalso ch
<> #
"\""))
199 val _
= Env
.type_one
"ip"
203 val _
= Env
.type_one
"ipv6"
207 val _
= Env
.type_one
"host"
211 val _
= Env
.type_one
"domain"
215 val _
= Env
.type_one
"your_domain"
219 val _
= Env
.type_one
"your_domain_host"
223 val _
= Env
.type_one
"user"
227 val _
= Env
.type_one
"group"
231 val _
= Env
.type_one
"your_user"
235 val _
= Env
.type_one
"your_group"
239 val _
= Env
.type_one
"your_path"
243 val _
= Env
.type_one
"readable_path"
247 val _
= Env
.type_one
"your_ip"
251 val _
= Env
.type_one
"your_ipv6"
255 val _
= Env
.type_one
"node"
259 val _
= Env
.type_one
"mime_type"
261 (CharVector
.exists (fn ch
=> ch
= #
"/"))
263 val _
= Env
.registerFunction ("your_ip_to_ip",
267 val _
= Env
.registerFunction ("your_ipv6_to_ipv6",
271 val _
= Env
.registerFunction ("dns_node_to_node",
275 val _
= Env
.registerFunction ("mail_node_to_node",
282 val dl
= ErrorMsg
.dummyLoc
284 val _
= Env
.registerFunction ("end_in_slash",
285 fn [(EString
"", _
)] => SOME (EString
"/", dl
)
286 |
[(EString s
, _
)] =>
287 SOME (EString (if String.sub (s
, size s
- 1) = #
"/" then
294 val _
= Env
.registerFunction ("you",
295 fn [] => SOME (EString (getUser ()), dl
)
298 val _
= Env
.registerFunction ("defaultMailbox",
299 fn [] => SOME (EString (getUser ()), dl
)
302 val _
= Env
.registerFunction ("defaultMailUser",
303 fn [] => SOME (EString (getUser ()), dl
)
307 type soa
= {ns
: string,
314 val serial
= fn (EVar
"serialAuto", _
) => SOME NONE
315 |
(EApp ((EVar
"serialConst", _
), n
), _
) => Option
.map
SOME (Env
.int n
)
318 val soa
= fn (EApp ((EApp ((EApp ((EApp ((EApp ((EApp
319 ((EVar
"soa", _
), ns
), _
),
325 (case (Env
.string ns
, serial sl
, Env
.int rf
,
326 Env
.int ret
, Env
.int exp
, Env
.int min
) of
327 (SOME ns
, SOME sl
, SOME rf
,
328 SOME ret
, SOME exp
, SOME min
) =>
339 ExternalMaster
of string
340 | InternalMaster
of string
344 val _
= Env
.registerFunction ("ip_of_node",
345 fn [(EString node
, _
)] => SOME (EString (nodeIp node
), dl
)
348 val _
= Env
.registerFunction ("ipv6_of_node",
349 fn [(EString node
, _
)] => SOME (EString (nodeIpv6 node
), dl
)
352 val master
= fn (EApp ((EVar
"externalMaster", _
), e
), _
) => Option
.map
ExternalMaster (ip e
)
353 |
(EApp ((EVar
"internalMaster", _
), e
), _
) => Option
.map
InternalMaster (Env
.string e
)
357 UseDns
of {soa
: soa
,
359 slaves
: string list
}
362 val dnsKind
= fn (EApp ((EApp ((EApp
363 ((EVar
"useDns", _
), sa
), _
),
366 (case (soa sa
, master mstr
, Env
.list Env
.string slaves
) of
367 (SOME sa
, SOME mstr
, SOME slaves
) =>
368 SOME (UseDns
{soa
= sa
,
372 |
(EVar
"noDns", _
) => SOME NoDns
375 val befores
= ref (fn (_
: string) => ())
376 val afters
= ref (fn (_
: string) => ())
378 fun registerBefore f
=
382 befores
:= (fn x
=> (old x
; f x
))
385 fun registerAfter f
=
389 afters
:= (fn x
=> (old x
; f x
))
392 val globals
= ref (fn () => ())
393 val locals
= ref (fn () => ())
395 fun registerResetGlobal f
=
399 globals
:= (fn x
=> (old x
; f x
))
402 fun registerResetLocal f
=
406 locals
:= (fn x
=> (old x
; f x
))
409 fun resetGlobal () = (!globals ();
410 ignore (OS
.Process
.system (Config
.rm ^
" -rf " ^ Config
.resultRoot ^
"/*")))
411 fun resetLocal () = !locals ()
414 val currentPath
= ref (fn (_
: string) => "")
415 val currentPathAli
= ref (fn (_
: string, _
: string) => "")
419 fun currentDomain () = !current
421 val currentsAli
= ref ([] : string list
)
423 fun currentAliasDomains () = !currentsAli
424 fun currentDomains () = currentDomain () :: currentAliasDomains ()
426 fun domainFile
{node
, name
} = ((*print ("Opening " ^
!currentPath node ^ name ^
"\n");*)
427 TextIO.openOut (!currentPath node ^ name
))
429 type files
= {write
: string -> unit
,
430 writeDom
: unit
-> unit
,
431 close
: unit
-> unit
}
433 fun domainsFile
{node
, name
} =
435 val doms
= currentDomains ()
436 val files
= map (fn dom
=> (dom
, TextIO.openOut (!currentPathAli (dom
, node
) ^ name
))) doms
438 {write
= fn s
=> app (fn (_
, outf
) => TextIO.output (outf
, s
)) files
,
439 writeDom
= fn () => app (fn (dom
, outf
) => TextIO.output (outf
, dom
)) files
,
440 close
= fn () => app (fn (_
, outf
) => TextIO.closeOut outf
) files
}
445 val toks
= String.fields (fn ch
=> ch
= #
".") domain
447 val elems
= foldr (fn (piece
, elems
) =>
449 val elems
= piece
:: elems
453 val path
= String.concatWith
"/"
454 (Config
.resultRoot
:: node
:: rev elems
)
455 val tmpPath
= String.concatWith
"/"
456 (Config
.tmpDir
:: node
:: rev elems
)
458 (if Posix
.FileSys
.ST
.isDir
459 (Posix
.FileSys
.stat path
) then
462 (OS
.FileSys
.remove path
;
463 OS
.FileSys
.mkDir path
))
464 handle OS
.SysErr _
=> OS
.FileSys
.mkDir path
;
466 (if Posix
.FileSys
.ST
.isDir
467 (Posix
.FileSys
.stat tmpPath
) then
470 (OS
.FileSys
.remove tmpPath
;
471 OS
.FileSys
.mkDir tmpPath
))
472 handle OS
.SysErr _
=> OS
.FileSys
.mkDir tmpPath
479 fn (root
, site
) => String.concatWith
"/" (root
:: site
:: rev ("" :: elems
))
482 datatype file_action
' =
483 Add
' of {src
: string, dst
: string}
485 | Modify
' of {src
: string, dst
: string}
487 fun findDiffs (prefixes
, site
, dom
, acts
) =
490 val realPath
= gp (Config
.resultRoot
, site
)
491 val tmpPath
= gp (Config
.tmpDir
, site
)
493 (*val _
= print ("getDiffs(" ^ site ^
", " ^ dom ^
")... " ^ realPath ^
"; " ^ tmpPath ^
"\n")*)
495 val dir
= Posix
.FileSys
.opendir realPath
498 case Posix
.FileSys
.readdir dir
of
499 NONE
=> (Posix
.FileSys
.closedir dir
;
503 val real = OS
.Path
.joinDirFile
{dir
= realPath
,
505 val tmp
= OS
.Path
.joinDirFile
{dir
= tmpPath
,
508 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat
real) then
510 else if Posix
.FileSys
.access (tmp
, []) then
511 if Slave
.shell
[Config
.diff
, " ", real, " ", tmp
] then
514 loopReal ((site
, dom
, realPath
, Modify
' {src
= tmp
, dst
= real}) :: acts
)
515 else if List.exists (fn prefix
=> String.isPrefix prefix
real) prefixes
then
516 loopReal ((site
, dom
, realPath
, Delete
' real) :: acts
)
521 val acts
= loopReal acts
523 val dir
= Posix
.FileSys
.opendir tmpPath
526 case Posix
.FileSys
.readdir dir
of
527 NONE
=> (Posix
.FileSys
.closedir dir
;
531 val real = OS
.Path
.joinDirFile
{dir
= realPath
,
533 val tmp
= OS
.Path
.joinDirFile
{dir
= tmpPath
,
536 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat tmp
) then
538 else if Posix
.FileSys
.access (real, []) then
541 loopTmp ((site
, dom
, realPath
, Add
' {src
= tmp
, dst
= real}) :: acts
)
544 val acts
= loopTmp acts
549 fun findAllDiffs prefixes
=
551 val dir
= Posix
.FileSys
.opendir Config
.tmpDir
552 val len
= length (String.fields (fn ch
=> ch
= #
"/") Config
.tmpDir
) + 1
554 fun exploreSites diffs
=
555 case Posix
.FileSys
.readdir dir
of
559 fun explore (dname
, diffs
) =
561 val dir
= Posix
.FileSys
.opendir dname
564 case Posix
.FileSys
.readdir dir
of
568 val fname
= OS
.Path
.joinDirFile
{dir
= dname
,
571 loop (if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat fname
) then
573 val dom
= String.fields (fn ch
=> ch
= #
"/") fname
574 val dom
= List.drop (dom
, len
)
575 val dom
= String.concatWith
"." (rev dom
)
577 val dname
' = OS
.Path
.joinDirFile
{dir
= dname
,
581 findDiffs (prefixes
, site
, dom
, diffs
))
588 before Posix
.FileSys
.closedir dir
591 exploreSites (explore (OS
.Path
.joinDirFile
{dir
= Config
.tmpDir
,
592 file
= site
}, diffs
))
596 before Posix
.FileSys
.closedir dir
599 val masterNode
: string option ref
= ref NONE
600 fun dnsMaster () = !masterNode
602 val seenDomains
: string list ref
= ref
[]
604 val _
= Env
.containerV_one
"domain"
605 ("domain", Env
.string)
608 val () = seenDomains
:= dom
:: !seenDomains
610 val kind
= Env
.env
dnsKind (evs
, "DNS")
611 val ttl
= Env
.env Env
.int (evs
, "TTL")
612 val aliases
= Env
.env (Env
.list Env
.string) (evs
, "Aliases")
614 val path
= getPath dom
616 val () = (current
:= dom
;
617 currentsAli
:= Slave
.remove (Slave
.removeDups aliases
, dom
);
618 currentPath
:= (fn site
=> path (Config
.tmpDir
, site
));
619 currentPathAli
:= (fn (dom
, site
) => getPath
dom (Config
.tmpDir
, site
)))
621 fun saveSoa (kind
, soa
: soa
) node
=
623 val {write
, writeDom
, close
} = domainsFile
{node
= node
, name
= "soa.conf"}
627 write (Int.toString ttl
);
633 | SOME n
=> write (Int.toString n
);
635 write (Int.toString (#ref soa
));
637 write (Int.toString (#ret soa
));
639 write (Int.toString (#exp soa
));
641 write (Int.toString (#min soa
));
646 fun saveNamed (kind
, soa
: soa
, masterIp
, slaveIps
) node
=
647 if dom
= "localhost" then
650 val {write
, writeDom
, close
} = domainsFile
{node
= node
, name
= "named.conf"}
654 write
"\" {\n\ttype ";
656 write
";\n\tfile \"";
657 write Config
.Bind
.zonePath_real
;
662 "master" => (write
"\tallow-transfer {\n";
663 app (fn ip
=> (write
"\t\t";
665 write
";\n")) slaveIps
;
667 | _
=> (write
"\tmasters { ";
670 write
"// Updated: ";
671 write (Time
.toString (Time
.now ()));
678 NoDns
=> masterNode
:= NONE
683 InternalMaster node
=> nodeIp node
684 | ExternalMaster ip
=> ip
686 val slaveIps
= map
nodeIp (#slaves dns
)
688 app (saveNamed ("slave", #soa dns
, masterIp
, slaveIps
)) (#slaves dns
);
690 InternalMaster node
=>
691 (masterNode
:= SOME node
;
692 saveSoa ("master", #soa dns
) node
;
693 saveNamed ("master", #soa dns
, masterIp
, slaveIps
) node
)
694 | _
=> masterNode
:= NONE
698 fn () => !afters (!current
))
700 val () = Env
.registerPre (fn () => (seenDomains
:= [];
701 ignore (Slave
.shellF ([Config
.rm
, " -rf ", Config
.tmpDir
, ""],
702 fn cl
=> "Temp file cleanup failed: " ^ cl
));
703 OS
.FileSys
.mkDir Config
.tmpDir
;
704 app (fn node
=> OS
.FileSys
.mkDir
705 (OS
.Path
.joinDirFile
{dir
= Config
.tmpDir
,
708 app (fn node
=> OS
.FileSys
.mkDir
709 (OS
.Path
.joinDirFile
{dir
= Config
.resultRoot
,
711 handle OS
.SysErr _
=> ())
714 fun handleSite (site
, files
) =
718 print ("New configuration for node " ^ site ^
"\n");
719 if site
= Config
.dispatcherName
then
720 Slave
.handleChanges files
722 val bio
= OpenSSL
.connect
true (valOf (!ssl_context
),
725 ^
Int.toString Config
.slavePort
)
727 app (fn file
=> Msg
.send (bio
, MsgFile file
)) files
;
728 Msg
.send (bio
, MsgDoFiles
);
730 NONE
=> print
"Slave closed connection unexpectedly\n"
733 MsgOk
=> print ("Slave " ^ site ^
" finished\n")
734 | MsgError s
=> print ("Slave " ^ site
735 ^
" returned error: " ^
737 | _
=> print ("Slave " ^ site
738 ^
" returned unexpected command\n");
743 val () = Env
.registerPost (fn () =>
745 val prefixes
= List.concat
748 val pieces
= String.tokens (fn ch
=> ch
= #
".") dom
749 val path
= String.concatWith
"/" (rev pieces
)
752 Config
.resultRoot ^
"/" ^ node ^
"/" ^ path ^
"/")
756 val diffs
= findAllDiffs prefixes
758 val diffs
= map (fn (site
, dom
, dir
, Add
' {src
, dst
}) =>
759 (Slave
.shellF ([Config
.cp
, " ", src
, " ", dst
],
760 fn cl
=> "Copy failed: " ^ cl
);
766 |
(site
, dom
, dir
, Delete
' dst
) =>
767 (OS
.FileSys
.remove dst
768 handle OS
.SysErr _
=>
769 ErrorMsg
.error
NONE ("Delete failed for " ^ dst
);
771 {action
= Slave
.Delete
true,
775 |
(site
, dom
, dir
, Modify
' {src
, dst
}) =>
776 (Slave
.shellF ([Config
.cp
, " ", src
, " ", dst
],
777 fn cl
=> "Copy failed: " ^ cl
);
779 {action
= Slave
.Modify
,
784 if !ErrorMsg
.anyErrors
then
787 val changed
= foldl (fn ((site
, file
), changed
) =>
789 val ls
= case SM
.find (changed
, site
) of
793 SM
.insert (changed
, site
, file
:: ls
)
796 SM
.appi handleSite changed
798 ignore (Slave
.shellF ([Config
.rm
, " -rf ", Config
.tmpDir
, ""],
799 fn cl
=> "Temp file cleanup failed: " ^ cl
))
802 fun hasPriv priv
= Acl
.query
{user
= getUser (), class
= "priv", value
= "all"}
803 orelse Acl
.query
{user
= getUser (), class
= "priv", value
= priv
}
805 val _
= Env
.type_one
"dns_node"
808 List.exists (fn x
=> x
= node
) Config
.dnsNodes_all
809 orelse (hasPriv
"dns"
810 andalso List.exists (fn x
=> x
= node
) Config
.dnsNodes_admin
))
812 val _
= Env
.type_one
"mail_node"
815 List.exists (fn x
=> x
= node
) Config
.mailNodes_all
816 orelse (hasPriv
"mail"
817 andalso List.exists (fn x
=> x
= node
) Config
.mailNodes_admin
))
819 fun rmdom
' delete resultRoot doms
=
821 fun doNode (node
, _
, _
) =
823 val dname
= OS
.Path
.joinDirFile
{dir
= resultRoot
,
826 fun doDom (dom
, actions
) =
828 val domPath
= String.concatWith
"/" (rev (String.fields (fn ch
=> ch
= #
".") dom
))
829 val dname
= OS
.Path
.concat (dname
, domPath
)
831 fun visitDom (dom
, dname
, actions
) =
833 val dir
= Posix
.FileSys
.opendir dname
836 case Posix
.FileSys
.readdir dir
of
840 val fnameFull
= OS
.Path
.joinDirFile
{dir
= dname
,
843 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat fnameFull
) then
844 loop (visitDom (fname ^
"." ^ dom
,
848 loop ({action
= Slave
.Delete delete
,
851 file
= fnameFull
} :: actions
)
855 before Posix
.FileSys
.closedir dir
857 handle OS
.SysErr (s
, _
) =>
858 (print ("Warning: System error deleting domain " ^ dom ^
" on " ^ node ^
": " ^ s ^
"\n");
861 visitDom (dom
, dname
, actions
)
864 val actions
= foldl doDom
[] doms
866 handleSite (node
, actions
)
868 handle IO
.Io _
=> print ("Warning: IO error deleting domains on " ^ node ^
".\n")
870 fun cleanupNode (node
, _
, _
) =
874 val domPath
= String.concatWith
"/" (rev (String.fields (fn ch
=> ch
= #
".") dom
))
875 val dname
= OS
.Path
.joinDirFile
{dir
= resultRoot
,
877 val dname
= OS
.Path
.concat (dname
, domPath
)
880 ignore (OS
.Process
.system (Config
.rm ^
" -rf " ^ dname
))
888 app doNode Config
.nodeIps
;
889 app cleanupNode Config
.nodeIps
892 val rmdom
= rmdom
' true Config
.resultRoot
893 val rmdom
' = rmdom
' false
895 fun homedirOf uname
=
896 Posix
.SysDB
.Passwd
.home (Posix
.SysDB
.getpwnam uname
)
898 fun homedir () = homedirOf (getUser ())
899 handle e
=> if !fakePrivs
then "/tmp" else raise e
901 type subject
= {node
: string, domain
: string}
903 val describers
: (subject
-> string) list ref
= ref
[]
905 fun registerDescriber f
= describers
:= f
:: !describers
907 fun describeOne arg
= String.concat (map (fn f
=> f arg
) (rev (!describers
)))
909 val line
= "--------------------------------------------------------------\n"
910 val dline
= "==============================================================\n"
913 String.concat (List.mapPartial
915 case describeOne
{node
= node
, domain
= dom
} of
918 SOME (String.concat
[dline
, "Node ", node
, "\n", dline
, "\n", s
]))
921 datatype description
=
922 Filename
of { filename
: string, heading
: string, showEmpty
: bool }
923 | Extension
of { extension
: string, heading
: string -> string }
925 fun considerAll ds
{node
, domain
} =
927 val ds
= map (fn d
=> (d
, ref
[])) ds
929 val path
= Config
.resultRoot
930 val jdf
= OS
.Path
.joinDirFile
931 val path
= jdf
{dir
= path
, file
= node
}
932 val path
= foldr (fn (more
, path
) => jdf
{dir
= path
, file
= more
})
933 path (String.tokens (fn ch
=> ch
= #
".") domain
)
935 if Posix
.FileSys
.access (path
, []) then
937 val dir
= Posix
.FileSys
.opendir path
940 case Posix
.FileSys
.readdir dir
of
943 (app (fn (d
, entries
) =>
945 fun readFile showEmpty entries
' =
947 val fname
= OS
.Path
.joinDirFile
{dir
= path
,
950 val inf
= TextIO.openIn fname
952 fun loop (seenOne
, entries
') =
953 case TextIO.inputLine inf
of
954 NONE
=> if seenOne
orelse showEmpty
then
958 | SOME line
=> loop (true, line
:: entries
')
960 loop (false, entries
')
961 before TextIO.closeIn inf
965 Filename
{filename
, heading
, showEmpty
} =>
966 if fname
= filename
then
967 entries
:= readFile
showEmpty ("\n" :: line
:: "\n" :: heading
:: line
:: !entries
)
970 | Extension
{extension
, heading
} =>
972 val {base
, ext
} = OS
.Path
.splitBaseExt fname
977 if extension
' = extension
then
978 entries
:= readFile
true ("\n" :: line
:: "\n" :: heading base
:: line
:: !entries
)
986 Posix
.FileSys
.closedir dir
;
987 String.concat (List.concat (map (fn (_
, entries
) => rev (!entries
)) ds
))
993 val () = registerDescriber (considerAll
[Filename
{filename
= "soa.conf",
994 heading
= "DNS SOA:",
997 val () = Env
.registerAction ("domainHost",
998 fn (env
, [(EString host
, _
)]) =>
999 SM
.insert (env
, "Hostname",
1000 (EString (host ^
"." ^
currentDomain ()), dl
))
1001 |
(_
, args
) => Env
.badArgs ("domainHost", args
))
1003 val ouc
= ref (fn () => ())
1005 fun registerOnUsersChange f
=
1009 ouc
:= (fn () => (f
' (); f ()))
1012 fun onUsersChange () = !ouc ()