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
127 noIpv4
8 orelse hasIpv4 ()
130 fun isIdent ch
= Char.isLower ch
orelse Char.isDigit ch
133 size s
> 0 andalso size s
< 50
134 andalso CharVector
.all (fn ch
=> isIdent ch
orelse ch
= #
"-") s
137 size s
> 0 andalso size s
< 200
138 andalso List.all
validHost (String.fields (fn ch
=> ch
= #
".") s
)
140 fun validNode s
= List.exists (fn s
' => s
= s
') nodes
142 fun yourDomain s
= !fakePrivs
orelse SS
.member (your_domains (), s
)
143 fun yourUser s
= !fakePrivs
orelse SS
.member (your_users (), s
)
144 fun yourGroup s
= !fakePrivs
orelse SS
.member (your_groups (), s
)
146 fun checkPath paths path
=
148 (List.all (fn s
=> s
<> "..") (String.fields (fn ch
=> ch
= #
"/") path
)
149 andalso CharVector
.all (fn ch
=> Char.isAlphaNum ch
orelse ch
= #
"." orelse ch
= #
"/"
150 orelse ch
= #
"-" orelse ch
= #
"_") path
151 andalso SS
.exists (fn s
' => path
= s
' orelse String.isPrefix (s
' ^
"/") path
) (paths ()))
152 val yourPath
= checkPath your_paths
153 val readablePath
= checkPath readable_paths
155 fun yourIp s
= !fakePrivs
orelse SS
.member (your_ips (), s
)
156 fun yourIpv6 s
= !fakePrivs
orelse SS
.member (your_ipv6s (), s
)
158 fun yourDomainHost s
=
162 val (pref
, suf
) = Substring
.splitl (fn ch
=> ch
<> #
".") (Substring
.full s
)
164 Substring
.size suf
> 0
165 andalso validHost (Substring
.string pref
)
166 andalso yourDomain (Substring
.string
167 (Substring
.slice (suf
, 1, NONE
)))
170 val yourDomain
= yourDomainHost
172 fun validUser s
= size s
> 0 andalso size s
< 20
173 andalso CharVector
.all
Char.isAlphaNum s
175 fun validEmailUser s
=
176 size s
> 0 andalso size s
< 50
177 andalso CharVector
.all (fn ch
=> Char.isAlphaNum ch
183 val validGroup
= validUser
185 val _
= Env
.type_one
"no_spaces"
187 (CharVector
.all (fn ch
=> Char.isPrint ch
andalso not (Char.isSpace ch
)
188 andalso ch
<> #
"\"" andalso ch
<> #
"'"))
189 val _
= Env
.type_one
"no_newlines"
191 (CharVector
.all (fn ch
=> Char.isPrint ch
andalso ch
<> #
"\n" andalso ch
<> #
"\r"
192 andalso ch
<> #
"\""))
194 val _
= Env
.type_one
"ip"
198 val _
= Env
.type_one
"ipv6"
202 val _
= Env
.type_one
"host"
206 val _
= Env
.type_one
"domain"
210 val _
= Env
.type_one
"your_domain"
214 val _
= Env
.type_one
"your_domain_host"
218 val _
= Env
.type_one
"user"
222 val _
= Env
.type_one
"group"
226 val _
= Env
.type_one
"your_user"
230 val _
= Env
.type_one
"your_group"
234 val _
= Env
.type_one
"your_path"
238 val _
= Env
.type_one
"readable_path"
242 val _
= Env
.type_one
"your_ip"
246 val _
= Env
.type_one
"your_ipv6"
250 val _
= Env
.type_one
"node"
254 val _
= Env
.type_one
"mime_type"
256 (CharVector
.exists (fn ch
=> ch
= #
"/"))
258 val _
= Env
.registerFunction ("your_ip_to_ip",
262 val _
= Env
.registerFunction ("your_ipv6_to_ipv6",
266 val _
= Env
.registerFunction ("dns_node_to_node",
270 val _
= Env
.registerFunction ("mail_node_to_node",
277 val dl
= ErrorMsg
.dummyLoc
279 val _
= Env
.registerFunction ("end_in_slash",
280 fn [(EString
"", _
)] => SOME (EString
"/", dl
)
281 |
[(EString s
, _
)] =>
282 SOME (EString (if String.sub (s
, size s
- 1) = #
"/" then
289 val _
= Env
.registerFunction ("you",
290 fn [] => SOME (EString (getUser ()), dl
)
293 val _
= Env
.registerFunction ("defaultMailbox",
294 fn [] => SOME (EString (getUser ()), dl
)
297 val _
= Env
.registerFunction ("defaultMailUser",
298 fn [] => SOME (EString (getUser ()), dl
)
302 type soa
= {ns
: string,
309 val serial
= fn (EVar
"serialAuto", _
) => SOME NONE
310 |
(EApp ((EVar
"serialConst", _
), n
), _
) => Option
.map
SOME (Env
.int n
)
313 val soa
= fn (EApp ((EApp ((EApp ((EApp ((EApp ((EApp
314 ((EVar
"soa", _
), ns
), _
),
320 (case (Env
.string ns
, serial sl
, Env
.int rf
,
321 Env
.int ret
, Env
.int exp
, Env
.int min
) of
322 (SOME ns
, SOME sl
, SOME rf
,
323 SOME ret
, SOME exp
, SOME min
) =>
334 ExternalMaster
of string
335 | InternalMaster
of string
339 val _
= Env
.registerFunction ("ip_of_node",
340 fn [(EString node
, _
)] => SOME (EString (nodeIp node
), dl
)
343 val _
= Env
.registerFunction ("ipv6_of_node",
344 fn [(EString node
, _
)] => SOME (EString (nodeIpv6 node
), dl
)
347 val master
= fn (EApp ((EVar
"externalMaster", _
), e
), _
) => Option
.map
ExternalMaster (ip e
)
348 |
(EApp ((EVar
"internalMaster", _
), e
), _
) => Option
.map
InternalMaster (Env
.string e
)
352 UseDns
of {soa
: soa
,
354 slaves
: string list
}
357 val dnsKind
= fn (EApp ((EApp ((EApp
358 ((EVar
"useDns", _
), sa
), _
),
361 (case (soa sa
, master mstr
, Env
.list Env
.string slaves
) of
362 (SOME sa
, SOME mstr
, SOME slaves
) =>
363 SOME (UseDns
{soa
= sa
,
367 |
(EVar
"noDns", _
) => SOME NoDns
370 val befores
= ref (fn (_
: string) => ())
371 val afters
= ref (fn (_
: string) => ())
373 fun registerBefore f
=
377 befores
:= (fn x
=> (old x
; f x
))
380 fun registerAfter f
=
384 afters
:= (fn x
=> (old x
; f x
))
387 val globals
= ref (fn () => ())
388 val locals
= ref (fn () => ())
390 fun registerResetGlobal f
=
394 globals
:= (fn x
=> (old x
; f x
))
397 fun registerResetLocal f
=
401 locals
:= (fn x
=> (old x
; f x
))
404 fun resetGlobal () = (!globals ();
405 ignore (OS
.Process
.system (Config
.rm ^
" -rf " ^ Config
.resultRoot ^
"/*")))
406 fun resetLocal () = !locals ()
409 val currentPath
= ref (fn (_
: string) => "")
410 val currentPathAli
= ref (fn (_
: string, _
: string) => "")
414 fun currentDomain () = !current
416 val currentsAli
= ref ([] : string list
)
418 fun currentAliasDomains () = !currentsAli
419 fun currentDomains () = currentDomain () :: currentAliasDomains ()
421 fun domainFile
{node
, name
} = ((*print ("Opening " ^
!currentPath node ^ name ^
"\n");*)
422 TextIO.openOut (!currentPath node ^ name
))
424 type files
= {write
: string -> unit
,
425 writeDom
: unit
-> unit
,
426 close
: unit
-> unit
}
428 fun domainsFile
{node
, name
} =
430 val doms
= currentDomains ()
431 val files
= map (fn dom
=> (dom
, TextIO.openOut (!currentPathAli (dom
, node
) ^ name
))) doms
433 {write
= fn s
=> app (fn (_
, outf
) => TextIO.output (outf
, s
)) files
,
434 writeDom
= fn () => app (fn (dom
, outf
) => TextIO.output (outf
, dom
)) files
,
435 close
= fn () => app (fn (_
, outf
) => TextIO.closeOut outf
) files
}
440 val toks
= String.fields (fn ch
=> ch
= #
".") domain
442 val elems
= foldr (fn (piece
, elems
) =>
444 val elems
= piece
:: elems
448 val path
= String.concatWith
"/"
449 (Config
.resultRoot
:: node
:: rev elems
)
450 val tmpPath
= String.concatWith
"/"
451 (Config
.tmpDir
:: node
:: rev elems
)
453 (if Posix
.FileSys
.ST
.isDir
454 (Posix
.FileSys
.stat path
) then
457 (OS
.FileSys
.remove path
;
458 OS
.FileSys
.mkDir path
))
459 handle OS
.SysErr _
=> OS
.FileSys
.mkDir path
;
461 (if Posix
.FileSys
.ST
.isDir
462 (Posix
.FileSys
.stat tmpPath
) then
465 (OS
.FileSys
.remove tmpPath
;
466 OS
.FileSys
.mkDir tmpPath
))
467 handle OS
.SysErr _
=> OS
.FileSys
.mkDir tmpPath
474 fn (root
, site
) => String.concatWith
"/" (root
:: site
:: rev ("" :: elems
))
477 datatype file_action
' =
478 Add
' of {src
: string, dst
: string}
480 | Modify
' of {src
: string, dst
: string}
482 fun findDiffs (prefixes
, site
, dom
, acts
) =
485 val realPath
= gp (Config
.resultRoot
, site
)
486 val tmpPath
= gp (Config
.tmpDir
, site
)
488 (*val _
= print ("getDiffs(" ^ site ^
", " ^ dom ^
")... " ^ realPath ^
"; " ^ tmpPath ^
"\n")*)
490 val dir
= Posix
.FileSys
.opendir realPath
493 case Posix
.FileSys
.readdir dir
of
494 NONE
=> (Posix
.FileSys
.closedir dir
;
498 val real = OS
.Path
.joinDirFile
{dir
= realPath
,
500 val tmp
= OS
.Path
.joinDirFile
{dir
= tmpPath
,
503 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat
real) then
505 else if Posix
.FileSys
.access (tmp
, []) then
506 if Slave
.shell
[Config
.diff
, " ", real, " ", tmp
] then
509 loopReal ((site
, dom
, realPath
, Modify
' {src
= tmp
, dst
= real}) :: acts
)
510 else if List.exists (fn prefix
=> String.isPrefix prefix
real) prefixes
then
511 loopReal ((site
, dom
, realPath
, Delete
' real) :: acts
)
516 val acts
= loopReal acts
518 val dir
= Posix
.FileSys
.opendir tmpPath
521 case Posix
.FileSys
.readdir dir
of
522 NONE
=> (Posix
.FileSys
.closedir dir
;
526 val real = OS
.Path
.joinDirFile
{dir
= realPath
,
528 val tmp
= OS
.Path
.joinDirFile
{dir
= tmpPath
,
531 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat tmp
) then
533 else if Posix
.FileSys
.access (real, []) then
536 loopTmp ((site
, dom
, realPath
, Add
' {src
= tmp
, dst
= real}) :: acts
)
539 val acts
= loopTmp acts
544 fun findAllDiffs prefixes
=
546 val dir
= Posix
.FileSys
.opendir Config
.tmpDir
547 val len
= length (String.fields (fn ch
=> ch
= #
"/") Config
.tmpDir
) + 1
549 fun exploreSites diffs
=
550 case Posix
.FileSys
.readdir dir
of
554 fun explore (dname
, diffs
) =
556 val dir
= Posix
.FileSys
.opendir dname
559 case Posix
.FileSys
.readdir dir
of
563 val fname
= OS
.Path
.joinDirFile
{dir
= dname
,
566 loop (if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat fname
) then
568 val dom
= String.fields (fn ch
=> ch
= #
"/") fname
569 val dom
= List.drop (dom
, len
)
570 val dom
= String.concatWith
"." (rev dom
)
572 val dname
' = OS
.Path
.joinDirFile
{dir
= dname
,
576 findDiffs (prefixes
, site
, dom
, diffs
))
583 before Posix
.FileSys
.closedir dir
586 exploreSites (explore (OS
.Path
.joinDirFile
{dir
= Config
.tmpDir
,
587 file
= site
}, diffs
))
591 before Posix
.FileSys
.closedir dir
594 val masterNode
: string option ref
= ref NONE
595 fun dnsMaster () = !masterNode
597 val seenDomains
: string list ref
= ref
[]
599 val _
= Env
.containerV_one
"domain"
600 ("domain", Env
.string)
603 val () = seenDomains
:= dom
:: !seenDomains
605 val kind
= Env
.env
dnsKind (evs
, "DNS")
606 val ttl
= Env
.env Env
.int (evs
, "TTL")
607 val aliases
= Env
.env (Env
.list Env
.string) (evs
, "Aliases")
609 val path
= getPath dom
611 val () = (current
:= dom
;
612 currentsAli
:= Slave
.remove (Slave
.removeDups aliases
, dom
);
613 currentPath
:= (fn site
=> path (Config
.tmpDir
, site
));
614 currentPathAli
:= (fn (dom
, site
) => getPath
dom (Config
.tmpDir
, site
)))
616 fun saveSoa (kind
, soa
: soa
) node
=
618 val {write
, writeDom
, close
} = domainsFile
{node
= node
, name
= "soa.conf"}
622 write (Int.toString ttl
);
628 | SOME n
=> write (Int.toString n
);
630 write (Int.toString (#ref soa
));
632 write (Int.toString (#ret soa
));
634 write (Int.toString (#exp soa
));
636 write (Int.toString (#min soa
));
641 fun saveNamed (kind
, soa
: soa
, masterIp
, slaveIps
) node
=
642 if dom
= "localhost" then
645 val {write
, writeDom
, close
} = domainsFile
{node
= node
, name
= "named.conf"}
649 write
"\" {\n\ttype ";
651 write
";\n\tfile \"";
652 write Config
.Bind
.zonePath_real
;
657 "master" => (write
"\tallow-transfer {\n";
658 app (fn ip
=> (write
"\t\t";
660 write
";\n")) slaveIps
;
662 | _
=> (write
"\tmasters { ";
665 write
"// Updated: ";
666 write (Time
.toString (Time
.now ()));
673 NoDns
=> masterNode
:= NONE
678 InternalMaster node
=> nodeIp node
679 | ExternalMaster ip
=> ip
681 val slaveIps
= map
nodeIp (#slaves dns
)
683 app (saveNamed ("slave", #soa dns
, masterIp
, slaveIps
)) (#slaves dns
);
685 InternalMaster node
=>
686 (masterNode
:= SOME node
;
687 saveSoa ("master", #soa dns
) node
;
688 saveNamed ("master", #soa dns
, masterIp
, slaveIps
) node
)
689 | _
=> masterNode
:= NONE
693 fn () => !afters (!current
))
695 val () = Env
.registerPre (fn () => (seenDomains
:= [];
696 ignore (Slave
.shellF ([Config
.rm
, " -rf ", Config
.tmpDir
, ""],
697 fn cl
=> "Temp file cleanup failed: " ^ cl
));
698 OS
.FileSys
.mkDir Config
.tmpDir
;
699 app (fn node
=> OS
.FileSys
.mkDir
700 (OS
.Path
.joinDirFile
{dir
= Config
.tmpDir
,
703 app (fn node
=> OS
.FileSys
.mkDir
704 (OS
.Path
.joinDirFile
{dir
= Config
.resultRoot
,
706 handle OS
.SysErr _
=> ())
709 fun handleSite (site
, files
) =
713 print ("New configuration for node " ^ site ^
"\n");
714 if site
= Config
.dispatcherName
then
715 Slave
.handleChanges files
717 val bio
= OpenSSL
.connect
true (valOf (!ssl_context
),
720 ^
Int.toString Config
.slavePort
)
722 app (fn file
=> Msg
.send (bio
, MsgFile file
)) files
;
723 Msg
.send (bio
, MsgDoFiles
);
725 NONE
=> print
"Slave closed connection unexpectedly\n"
728 MsgOk
=> print ("Slave " ^ site ^
" finished\n")
729 | MsgError s
=> print ("Slave " ^ site
730 ^
" returned error: " ^
732 | _
=> print ("Slave " ^ site
733 ^
" returned unexpected command\n");
738 val () = Env
.registerPost (fn () =>
740 val prefixes
= List.concat
743 val pieces
= String.tokens (fn ch
=> ch
= #
".") dom
744 val path
= String.concatWith
"/" (rev pieces
)
747 Config
.resultRoot ^
"/" ^ node ^
"/" ^ path ^
"/")
751 val diffs
= findAllDiffs prefixes
753 val diffs
= map (fn (site
, dom
, dir
, Add
' {src
, dst
}) =>
754 (Slave
.shellF ([Config
.cp
, " ", src
, " ", dst
],
755 fn cl
=> "Copy failed: " ^ cl
);
761 |
(site
, dom
, dir
, Delete
' dst
) =>
762 (OS
.FileSys
.remove dst
763 handle OS
.SysErr _
=>
764 ErrorMsg
.error
NONE ("Delete failed for " ^ dst
);
766 {action
= Slave
.Delete
true,
770 |
(site
, dom
, dir
, Modify
' {src
, dst
}) =>
771 (Slave
.shellF ([Config
.cp
, " ", src
, " ", dst
],
772 fn cl
=> "Copy failed: " ^ cl
);
774 {action
= Slave
.Modify
,
779 if !ErrorMsg
.anyErrors
then
782 val changed
= foldl (fn ((site
, file
), changed
) =>
784 val ls
= case SM
.find (changed
, site
) of
788 SM
.insert (changed
, site
, file
:: ls
)
791 SM
.appi handleSite changed
793 ignore (Slave
.shellF ([Config
.rm
, " -rf ", Config
.tmpDir
, ""],
794 fn cl
=> "Temp file cleanup failed: " ^ cl
))
797 fun hasPriv priv
= Acl
.query
{user
= getUser (), class
= "priv", value
= "all"}
798 orelse Acl
.query
{user
= getUser (), class
= "priv", value
= priv
}
800 val _
= Env
.type_one
"dns_node"
803 List.exists (fn x
=> x
= node
) Config
.dnsNodes_all
804 orelse (hasPriv
"dns"
805 andalso List.exists (fn x
=> x
= node
) Config
.dnsNodes_admin
))
807 val _
= Env
.type_one
"mail_node"
810 List.exists (fn x
=> x
= node
) Config
.mailNodes_all
811 orelse (hasPriv
"mail"
812 andalso List.exists (fn x
=> x
= node
) Config
.mailNodes_admin
))
814 fun rmdom
' delete resultRoot doms
=
816 fun doNode (node
, _
, _
) =
818 val dname
= OS
.Path
.joinDirFile
{dir
= resultRoot
,
821 fun doDom (dom
, actions
) =
823 val domPath
= String.concatWith
"/" (rev (String.fields (fn ch
=> ch
= #
".") dom
))
824 val dname
= OS
.Path
.concat (dname
, domPath
)
826 fun visitDom (dom
, dname
, actions
) =
828 val dir
= Posix
.FileSys
.opendir dname
831 case Posix
.FileSys
.readdir dir
of
835 val fnameFull
= OS
.Path
.joinDirFile
{dir
= dname
,
838 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat fnameFull
) then
839 loop (visitDom (fname ^
"." ^ dom
,
843 loop ({action
= Slave
.Delete delete
,
846 file
= fnameFull
} :: actions
)
850 before Posix
.FileSys
.closedir dir
852 handle OS
.SysErr (s
, _
) =>
853 (print ("Warning: System error deleting domain " ^ dom ^
" on " ^ node ^
": " ^ s ^
"\n");
856 visitDom (dom
, dname
, actions
)
859 val actions
= foldl doDom
[] doms
861 handleSite (node
, actions
)
863 handle IO
.Io _
=> print ("Warning: IO error deleting domains on " ^ node ^
".\n")
865 fun cleanupNode (node
, _
, _
) =
869 val domPath
= String.concatWith
"/" (rev (String.fields (fn ch
=> ch
= #
".") dom
))
870 val dname
= OS
.Path
.joinDirFile
{dir
= resultRoot
,
872 val dname
= OS
.Path
.concat (dname
, domPath
)
875 ignore (OS
.Process
.system (Config
.rm ^
" -rf " ^ dname
))
883 app doNode Config
.nodeIps
;
884 app cleanupNode Config
.nodeIps
887 val rmdom
= rmdom
' true Config
.resultRoot
888 val rmdom
' = rmdom
' false
890 fun homedirOf uname
=
891 Posix
.SysDB
.Passwd
.home (Posix
.SysDB
.getpwnam uname
)
893 fun homedir () = homedirOf (getUser ())
894 handle e
=> if !fakePrivs
then "/tmp" else raise e
896 type subject
= {node
: string, domain
: string}
898 val describers
: (subject
-> string) list ref
= ref
[]
900 fun registerDescriber f
= describers
:= f
:: !describers
902 fun describeOne arg
= String.concat (map (fn f
=> f arg
) (rev (!describers
)))
904 val line
= "--------------------------------------------------------------\n"
905 val dline
= "==============================================================\n"
908 String.concat (List.mapPartial
910 case describeOne
{node
= node
, domain
= dom
} of
913 SOME (String.concat
[dline
, "Node ", node
, "\n", dline
, "\n", s
]))
916 datatype description
=
917 Filename
of { filename
: string, heading
: string, showEmpty
: bool }
918 | Extension
of { extension
: string, heading
: string -> string }
920 fun considerAll ds
{node
, domain
} =
922 val ds
= map (fn d
=> (d
, ref
[])) ds
924 val path
= Config
.resultRoot
925 val jdf
= OS
.Path
.joinDirFile
926 val path
= jdf
{dir
= path
, file
= node
}
927 val path
= foldr (fn (more
, path
) => jdf
{dir
= path
, file
= more
})
928 path (String.tokens (fn ch
=> ch
= #
".") domain
)
930 if Posix
.FileSys
.access (path
, []) then
932 val dir
= Posix
.FileSys
.opendir path
935 case Posix
.FileSys
.readdir dir
of
938 (app (fn (d
, entries
) =>
940 fun readFile showEmpty entries
' =
942 val fname
= OS
.Path
.joinDirFile
{dir
= path
,
945 val inf
= TextIO.openIn fname
947 fun loop (seenOne
, entries
') =
948 case TextIO.inputLine inf
of
949 NONE
=> if seenOne
orelse showEmpty
then
953 | SOME line
=> loop (true, line
:: entries
')
955 loop (false, entries
')
956 before TextIO.closeIn inf
960 Filename
{filename
, heading
, showEmpty
} =>
961 if fname
= filename
then
962 entries
:= readFile
showEmpty ("\n" :: line
:: "\n" :: heading
:: line
:: !entries
)
965 | Extension
{extension
, heading
} =>
967 val {base
, ext
} = OS
.Path
.splitBaseExt fname
972 if extension
' = extension
then
973 entries
:= readFile
true ("\n" :: line
:: "\n" :: heading base
:: line
:: !entries
)
981 Posix
.FileSys
.closedir dir
;
982 String.concat (List.concat (map (fn (_
, entries
) => rev (!entries
)) ds
))
988 val () = registerDescriber (considerAll
[Filename
{filename
= "soa.conf",
989 heading
= "DNS SOA:",
992 val () = Env
.registerAction ("domainHost",
993 fn (env
, [(EString host
, _
)]) =>
994 SM
.insert (env
, "Hostname",
995 (EString (host ^
"." ^
currentDomain ()), dl
))
996 |
(_
, args
) => Env
.badArgs ("domainHost", args
))
998 val ouc
= ref (fn () => ())
1000 fun registerOnUsersChange f
=
1004 ouc
:= (fn () => (f
' (); f ()))
1007 fun onUsersChange () = !ouc ()