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
))
39 val your_doms
= ref SS
.empty
40 fun your_domains () = !your_doms
42 val your_usrs
= ref SS
.empty
43 fun your_users () = !your_usrs
45 val your_grps
= ref SS
.empty
46 fun your_groups () = !your_grps
48 val your_pths
= ref SS
.empty
49 fun your_paths () = !your_pths
51 val your_ipss
= ref SS
.empty
52 fun your_ips () = !your_ipss
54 val world_readable
= SS
.addList (SS
.empty
, Config
.worldReadable
)
55 val readable_pths
= ref world_readable
56 fun readable_paths () = !readable_pths
62 val your_paths
= Acl
.class
{user
= getUser (),
65 your_doms
:= Acl
.class
{user
= getUser (),
67 your_usrs
:= Acl
.class
{user
= getUser (),
69 your_grps
:= Acl
.class
{user
= getUser (),
71 your_pths
:= your_paths
;
72 readable_pths
:= SS
.union (your_paths
, world_readable
);
73 your_ipss
:= Acl
.class
{user
= getUser (),
78 case map
Int.fromString (String.fields (fn ch
=> ch
= #
".") s
) of
79 [SOME n1
, SOME n2
, SOME n3
, SOME n4
] =>
80 n1
>= 0 andalso n1
< 256 andalso n2
>= 0 andalso n2
< 256 andalso n3
>= 0 andalso n3
< 256 andalso n4
>= 0 andalso n4
< 256
83 fun isHexDigit ch
= Char.isDigit ch
orelse (ord ch
>= ord #
"a" andalso ord ch
<= ord #
"f")
87 val fields
= String.fields (fn ch
=> ch
= #
":") s
89 val empties
= foldl (fn ("", n
) => n
+ 1
90 |
(_
, n
) => n
) 0 fields
94 andalso length fields
<= maxLen
96 andalso List.all (fn "" => true
98 andalso CharVector
.all isHexDigit s
) fields
104 val maybeIpv4
= List.last fields
105 val theRest
= List.take (fields
, length fields
- 1)
107 validIp maybeIpv4
andalso noIpv4
6
110 noIpv4
8 orelse hasIpv4 ()
113 fun isIdent ch
= Char.isLower ch
orelse Char.isDigit ch
116 size s
> 0 andalso size s
< 50
117 andalso CharVector
.all (fn ch
=> isIdent ch
orelse ch
= #
"-") s
120 size s
> 0 andalso size s
< 200
121 andalso List.all
validHost (String.fields (fn ch
=> ch
= #
".") s
)
123 fun validNode s
= List.exists (fn s
' => s
= s
') nodes
125 fun yourDomain s
= SS
.member (your_domains (), s
)
126 fun yourUser s
= SS
.member (your_users (), s
)
127 fun yourGroup s
= SS
.member (your_groups (), s
)
128 fun checkPath paths path
=
129 List.all (fn s
=> s
<> "..") (String.fields (fn ch
=> ch
= #
"/") path
)
130 andalso CharVector
.all (fn ch
=> Char.isAlphaNum ch
orelse ch
= #
"." orelse ch
= #
"/"
131 orelse ch
= #
"-" orelse ch
= #
"_") path
132 andalso SS
.exists (fn s
' => path
= s
' orelse String.isPrefix (s
' ^
"/") path
) (paths ())
133 val yourPath
= checkPath your_paths
134 val readablePath
= checkPath readable_paths
135 fun yourIp s
= SS
.member (your_ips (), s
)
137 fun yourDomainHost s
=
140 val (pref
, suf
) = Substring
.splitl (fn ch
=> ch
<> #
".") (Substring
.full s
)
142 Substring
.size suf
> 0
143 andalso validHost (Substring
.string pref
)
144 andalso yourDomain (Substring
.string
145 (Substring
.slice (suf
, 1, NONE
)))
148 val yourDomain
= yourDomainHost
150 fun validUser s
= size s
> 0 andalso size s
< 20
151 andalso CharVector
.all
Char.isAlphaNum s
153 fun validEmailUser s
=
154 size s
> 0 andalso size s
< 50
155 andalso CharVector
.all (fn ch
=> Char.isAlphaNum ch
161 val validGroup
= validUser
163 val _
= Env
.type_one
"no_spaces"
165 (CharVector
.all (fn ch
=> Char.isPrint ch
andalso not (Char.isSpace ch
)
166 andalso ch
<> #
"\"" andalso ch
<> #
"'"))
167 val _
= Env
.type_one
"no_newlines"
169 (CharVector
.all (fn ch
=> Char.isPrint ch
andalso ch
<> #
"\n" andalso ch
<> #
"\r"
170 andalso ch
<> #
"\""))
172 val _
= Env
.type_one
"ip"
176 val _
= Env
.type_one
"ipv6"
180 val _
= Env
.type_one
"host"
184 val _
= Env
.type_one
"domain"
188 val _
= Env
.type_one
"your_domain"
192 val _
= Env
.type_one
"your_domain_host"
196 val _
= Env
.type_one
"user"
200 val _
= Env
.type_one
"group"
204 val _
= Env
.type_one
"your_user"
208 val _
= Env
.type_one
"your_group"
212 val _
= Env
.type_one
"your_path"
216 val _
= Env
.type_one
"readable_path"
220 val _
= Env
.type_one
"your_ip"
224 val _
= Env
.type_one
"node"
228 val _
= Env
.registerFunction ("your_ip_to_ip",
232 val _
= Env
.registerFunction ("dns_node_to_node",
236 val _
= Env
.registerFunction ("mail_node_to_node",
243 val dl
= ErrorMsg
.dummyLoc
245 val _
= Env
.registerFunction ("end_in_slash",
246 fn [(EString
"", _
)] => SOME (EString
"/", dl
)
247 |
[(EString s
, _
)] =>
248 SOME (EString (if String.sub (s
, size s
- 1) = #
"/" then
255 val nsD
= (EString Config
.defaultNs
, dl
)
256 val serialD
= (EVar
"serialAuto", dl
)
257 val refD
= (EInt Config
.defaultRefresh
, dl
)
258 val retD
= (EInt Config
.defaultRetry
, dl
)
259 val expD
= (EInt Config
.defaultExpiry
, dl
)
260 val minD
= (EInt Config
.defaultMinimum
, dl
)
262 val soaD
= multiApp ((EVar
"soa", dl
),
264 [nsD
, serialD
, refD
, retD
, expD
, minD
])
266 val masterD
= (EApp ((EVar
"internalMaster", dl
),
267 (EString Config
.masterNode
, dl
)),
270 val slavesD
= (EList (map (fn s
=> (EString s
, dl
)) Config
.slaveNodes
), dl
)
272 val _
= Defaults
.registerDefault ("Aliases",
273 (TList (TBase
"your_domain", dl
), dl
),
274 (fn () => (EList
[], dl
)))
276 val _
= Defaults
.registerDefault ("Mailbox",
278 (fn () => (EString (getUser ()), dl
)))
280 val _
= Defaults
.registerDefault ("DNS",
281 (TBase
"dnsKind", dl
),
282 (fn () => multiApp ((EVar
"useDns", dl
),
284 [soaD
, masterD
, slavesD
])))
286 val _
= Defaults
.registerDefault ("TTL",
288 (fn () => (EInt Config
.Bind
.defaultTTL
, dl
)))
290 type soa
= {ns
: string,
297 val serial
= fn (EVar
"serialAuto", _
) => SOME NONE
298 |
(EApp ((EVar
"serialConst", _
), n
), _
) => Option
.map
SOME (Env
.int n
)
301 val soa
= fn (EApp ((EApp ((EApp ((EApp ((EApp ((EApp
302 ((EVar
"soa", _
), ns
), _
),
308 (case (Env
.string ns
, serial sl
, Env
.int rf
,
309 Env
.int ret
, Env
.int exp
, Env
.int min
) of
310 (SOME ns
, SOME sl
, SOME rf
,
311 SOME ret
, SOME exp
, SOME min
) =>
322 ExternalMaster
of string
323 | InternalMaster
of string
327 val _
= Env
.registerFunction ("ip_of_node",
328 fn [(EString node
, _
)] => SOME (EString (nodeIp node
), dl
)
331 val master
= fn (EApp ((EVar
"externalMaster", _
), e
), _
) => Option
.map
ExternalMaster (ip e
)
332 |
(EApp ((EVar
"internalMaster", _
), e
), _
) => Option
.map
InternalMaster (Env
.string e
)
336 UseDns
of {soa
: soa
,
338 slaves
: string list
}
341 val dnsKind
= fn (EApp ((EApp ((EApp
342 ((EVar
"useDns", _
), sa
), _
),
345 (case (soa sa
, master mstr
, Env
.list Env
.string slaves
) of
346 (SOME sa
, SOME mstr
, SOME slaves
) =>
347 SOME (UseDns
{soa
= sa
,
351 |
(EVar
"noDns", _
) => SOME NoDns
354 val befores
= ref (fn (_
: string) => ())
355 val afters
= ref (fn (_
: string) => ())
357 fun registerBefore f
=
361 befores
:= (fn x
=> (old x
; f x
))
364 fun registerAfter f
=
368 afters
:= (fn x
=> (old x
; f x
))
371 val globals
= ref (fn () => ())
372 val locals
= ref (fn () => ())
374 fun registerResetGlobal f
=
378 globals
:= (fn x
=> (old x
; f x
))
381 fun registerResetLocal f
=
385 locals
:= (fn x
=> (old x
; f x
))
388 fun resetGlobal () = (!globals ();
389 ignore (OS
.Process
.system (Config
.rm ^
" -rf " ^ Config
.resultRoot ^
"/*")))
390 fun resetLocal () = !locals ()
393 val currentPath
= ref (fn (_
: string) => "")
394 val currentPathAli
= ref (fn (_
: string, _
: string) => "")
398 fun currentDomain () = !current
400 val currentsAli
= ref ([] : string list
)
402 fun currentAliasDomains () = !currentsAli
403 fun currentDomains () = currentDomain () :: currentAliasDomains ()
405 fun domainFile
{node
, name
} = ((*print ("Opening " ^
!currentPath node ^ name ^
"\n");*)
406 TextIO.openOut (!currentPath node ^ name
))
408 type files
= {write
: string -> unit
,
409 writeDom
: unit
-> unit
,
410 close
: unit
-> unit
}
412 fun domainsFile
{node
, name
} =
414 val doms
= currentDomains ()
415 val files
= map (fn dom
=> (dom
, TextIO.openOut (!currentPathAli (dom
, node
) ^ name
))) doms
417 {write
= fn s
=> app (fn (_
, outf
) => TextIO.output (outf
, s
)) files
,
418 writeDom
= fn () => app (fn (dom
, outf
) => TextIO.output (outf
, dom
)) files
,
419 close
= fn () => app (fn (_
, outf
) => TextIO.closeOut outf
) files
}
424 val toks
= String.fields (fn ch
=> ch
= #
".") domain
426 val elems
= foldr (fn (piece
, elems
) =>
428 val elems
= piece
:: elems
432 val path
= String.concatWith
"/"
433 (Config
.resultRoot
:: node
:: rev elems
)
434 val tmpPath
= String.concatWith
"/"
435 (Config
.tmpDir
:: node
:: rev elems
)
437 (if Posix
.FileSys
.ST
.isDir
438 (Posix
.FileSys
.stat path
) then
441 (OS
.FileSys
.remove path
;
442 OS
.FileSys
.mkDir path
))
443 handle OS
.SysErr _
=> OS
.FileSys
.mkDir path
;
445 (if Posix
.FileSys
.ST
.isDir
446 (Posix
.FileSys
.stat tmpPath
) then
449 (OS
.FileSys
.remove tmpPath
;
450 OS
.FileSys
.mkDir tmpPath
))
451 handle OS
.SysErr _
=> OS
.FileSys
.mkDir tmpPath
458 fn (root
, site
) => String.concatWith
"/" (root
:: site
:: rev ("" :: elems
))
461 datatype file_action
' =
462 Add
' of {src
: string, dst
: string}
464 | Modify
' of {src
: string, dst
: string}
466 fun findDiffs (prefixes
, site
, dom
, acts
) =
469 val realPath
= gp (Config
.resultRoot
, site
)
470 val tmpPath
= gp (Config
.tmpDir
, site
)
472 (*val _
= print ("getDiffs(" ^ site ^
", " ^ dom ^
")... " ^ realPath ^
"; " ^ tmpPath ^
"\n")*)
474 val dir
= Posix
.FileSys
.opendir realPath
477 case Posix
.FileSys
.readdir dir
of
478 NONE
=> (Posix
.FileSys
.closedir dir
;
482 val real = OS
.Path
.joinDirFile
{dir
= realPath
,
484 val tmp
= OS
.Path
.joinDirFile
{dir
= tmpPath
,
487 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat
real) then
489 else if Posix
.FileSys
.access (tmp
, []) then
490 if Slave
.shell
[Config
.diff
, " ", real, " ", tmp
] then
493 loopReal ((site
, dom
, realPath
, Modify
' {src
= tmp
, dst
= real}) :: acts
)
494 else if List.exists (fn prefix
=> String.isPrefix prefix
real) prefixes
then
495 loopReal ((site
, dom
, realPath
, Delete
' real) :: acts
)
500 val acts
= loopReal acts
502 val dir
= Posix
.FileSys
.opendir tmpPath
505 case Posix
.FileSys
.readdir dir
of
506 NONE
=> (Posix
.FileSys
.closedir dir
;
510 val real = OS
.Path
.joinDirFile
{dir
= realPath
,
512 val tmp
= OS
.Path
.joinDirFile
{dir
= tmpPath
,
515 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat tmp
) then
517 else if Posix
.FileSys
.access (real, []) then
520 loopTmp ((site
, dom
, realPath
, Add
' {src
= tmp
, dst
= real}) :: acts
)
523 val acts
= loopTmp acts
528 fun findAllDiffs prefixes
=
530 val dir
= Posix
.FileSys
.opendir Config
.tmpDir
531 val len
= length (String.fields (fn ch
=> ch
= #
"/") Config
.tmpDir
) + 1
533 fun exploreSites diffs
=
534 case Posix
.FileSys
.readdir dir
of
538 fun explore (dname
, diffs
) =
540 val dir
= Posix
.FileSys
.opendir dname
543 case Posix
.FileSys
.readdir dir
of
547 val fname
= OS
.Path
.joinDirFile
{dir
= dname
,
550 loop (if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat fname
) then
552 val dom
= String.fields (fn ch
=> ch
= #
"/") fname
553 val dom
= List.drop (dom
, len
)
554 val dom
= String.concatWith
"." (rev dom
)
556 val dname
' = OS
.Path
.joinDirFile
{dir
= dname
,
560 findDiffs (prefixes
, site
, dom
, diffs
))
567 before Posix
.FileSys
.closedir dir
570 exploreSites (explore (OS
.Path
.joinDirFile
{dir
= Config
.tmpDir
,
571 file
= site
}, diffs
))
575 before Posix
.FileSys
.closedir dir
578 val masterNode
: string option ref
= ref NONE
579 fun dnsMaster () = !masterNode
581 val seenDomains
: string list ref
= ref
[]
583 val _
= Env
.containerV_one
"domain"
584 ("domain", Env
.string)
587 val () = seenDomains
:= dom
:: !seenDomains
589 val kind
= Env
.env
dnsKind (evs
, "DNS")
590 val ttl
= Env
.env Env
.int (evs
, "TTL")
591 val aliases
= Env
.env (Env
.list Env
.string) (evs
, "Aliases")
593 val path
= getPath dom
595 val () = (current
:= dom
;
596 currentsAli
:= Slave
.remove (Slave
.removeDups aliases
, dom
);
597 currentPath
:= (fn site
=> path (Config
.tmpDir
, site
));
598 currentPathAli
:= (fn (dom
, site
) => getPath
dom (Config
.tmpDir
, site
)))
600 fun saveSoa (kind
, soa
: soa
) node
=
602 val {write
, writeDom
, close
} = domainsFile
{node
= node
, name
= "soa"}
606 write (Int.toString ttl
);
612 | SOME n
=> write (Int.toString n
);
614 write (Int.toString (#ref soa
));
616 write (Int.toString (#ret soa
));
618 write (Int.toString (#exp soa
));
620 write (Int.toString (#min soa
));
625 fun saveNamed (kind
, soa
: soa
, masterIp
, slaveIps
) node
=
626 if dom
= "localhost" then
629 val {write
, writeDom
, close
} = domainsFile
{node
= node
, name
= "named.conf"}
633 write
"\" {\n\ttype ";
635 write
";\n\tfile \"";
636 write Config
.Bind
.zonePath_real
;
641 "master" => (write
"\tallow-transfer {\n";
642 app (fn ip
=> (write
"\t\t";
644 write
";\n")) slaveIps
;
646 | _
=> (write
"\tmasters { ";
649 write
"// Updated: ";
650 write (Time
.toString (Time
.now ()));
657 NoDns
=> masterNode
:= NONE
662 InternalMaster node
=> nodeIp node
663 | ExternalMaster ip
=> ip
665 val slaveIps
= map
nodeIp (#slaves dns
)
667 app (saveSoa ("slave", #soa dns
)) (#slaves dns
);
668 app (saveNamed ("slave", #soa dns
, masterIp
, slaveIps
)) (#slaves dns
);
670 InternalMaster node
=>
671 (masterNode
:= SOME node
;
672 saveSoa ("master", #soa dns
) node
;
673 saveNamed ("master", #soa dns
, masterIp
, slaveIps
) node
)
674 | _
=> masterNode
:= NONE
678 fn () => !afters (!current
))
680 val () = Env
.registerPre (fn () => (seenDomains
:= [];
681 ignore (Slave
.shellF ([Config
.rm
, " -rf ", Config
.tmpDir
, ""],
682 fn cl
=> "Temp file cleanup failed: " ^ cl
));
683 OS
.FileSys
.mkDir Config
.tmpDir
;
684 app (fn node
=> OS
.FileSys
.mkDir
685 (OS
.Path
.joinDirFile
{dir
= Config
.tmpDir
,
688 app (fn node
=> OS
.FileSys
.mkDir
689 (OS
.Path
.joinDirFile
{dir
= Config
.resultRoot
,
691 handle OS
.SysErr _
=> ())
694 fun handleSite (site
, files
) =
698 print ("New configuration for node " ^ site ^
"\n");
699 if site
= Config
.defaultNode
then
700 Slave
.handleChanges files
702 val bio
= OpenSSL
.connect (valOf (!ssl_context
),
705 ^
Int.toString Config
.slavePort
)
707 app (fn file
=> Msg
.send (bio
, MsgFile file
)) files
;
708 Msg
.send (bio
, MsgDoFiles
);
710 NONE
=> print
"Slave closed connection unexpectedly\n"
713 MsgOk
=> print ("Slave " ^ site ^
" finished\n")
714 | MsgError s
=> print ("Slave " ^ site
715 ^
" returned error: " ^
717 | _
=> print ("Slave " ^ site
718 ^
" returned unexpected command\n");
723 val () = Env
.registerPost (fn () =>
725 val prefixes
= List.concat
728 val pieces
= String.tokens (fn ch
=> ch
= #
".") dom
729 val path
= String.concatWith
"/" (rev pieces
)
732 Config
.resultRoot ^
"/" ^ node ^
"/" ^ path ^
"/")
736 val diffs
= findAllDiffs prefixes
738 val diffs
= map (fn (site
, dom
, dir
, Add
' {src
, dst
}) =>
739 (Slave
.shellF ([Config
.cp
, " ", src
, " ", dst
],
740 fn cl
=> "Copy failed: " ^ cl
);
746 |
(site
, dom
, dir
, Delete
' dst
) =>
747 (OS
.FileSys
.remove dst
748 handle OS
.SysErr _
=>
749 ErrorMsg
.error
NONE ("Delete failed for " ^ dst
);
751 {action
= Slave
.Delete
true,
755 |
(site
, dom
, dir
, Modify
' {src
, dst
}) =>
756 (Slave
.shellF ([Config
.cp
, " ", src
, " ", dst
],
757 fn cl
=> "Copy failed: " ^ cl
);
759 {action
= Slave
.Modify
,
764 if !ErrorMsg
.anyErrors
then
767 val changed
= foldl (fn ((site
, file
), changed
) =>
769 val ls
= case SM
.find (changed
, site
) of
773 SM
.insert (changed
, site
, file
:: ls
)
776 SM
.appi handleSite changed
778 ignore (Slave
.shellF ([Config
.rm
, " -rf ", Config
.tmpDir
, ""],
779 fn cl
=> "Temp file cleanup failed: " ^ cl
))
782 fun hasPriv priv
= Acl
.query
{user
= getUser (), class
= "priv", value
= "all"}
783 orelse Acl
.query
{user
= getUser (), class
= "priv", value
= priv
}
785 val _
= Env
.type_one
"dns_node"
788 List.exists (fn x
=> x
= node
) Config
.dnsNodes_all
789 orelse (hasPriv
"dns"
790 andalso List.exists (fn x
=> x
= node
) Config
.dnsNodes_admin
))
792 val _
= Env
.type_one
"mail_node"
795 List.exists (fn x
=> x
= node
) Config
.mailNodes_all
796 orelse (hasPriv
"mail"
797 andalso List.exists (fn x
=> x
= node
) Config
.mailNodes_admin
))
799 fun rmdom
' delete resultRoot doms
=
801 fun doNode (node
, _
) =
803 val dname
= OS
.Path
.joinDirFile
{dir
= resultRoot
,
806 fun doDom (dom
, actions
) =
808 val domPath
= String.concatWith
"/" (rev (String.fields (fn ch
=> ch
= #
".") dom
))
809 val dname
= OS
.Path
.concat (dname
, domPath
)
811 fun visitDom (dom
, dname
, actions
) =
813 val dir
= Posix
.FileSys
.opendir dname
816 case Posix
.FileSys
.readdir dir
of
820 val fnameFull
= OS
.Path
.joinDirFile
{dir
= dname
,
823 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat fnameFull
) then
824 loop (visitDom (fname ^
"." ^ dom
,
828 loop ({action
= Slave
.Delete delete
,
831 file
= fnameFull
} :: actions
)
835 before Posix
.FileSys
.closedir dir
837 handle OS
.SysErr (s
, _
) =>
838 (print ("Warning: System error deleting domain " ^ dom ^
" on " ^ node ^
": " ^ s ^
"\n");
841 visitDom (dom
, dname
, actions
)
844 val actions
= foldl doDom
[] doms
846 handleSite (node
, actions
)
848 handle IO
.Io _
=> print ("Warning: IO error deleting domains on " ^ node ^
".\n")
850 fun cleanupNode (node
, _
) =
854 val domPath
= String.concatWith
"/" (rev (String.fields (fn ch
=> ch
= #
".") dom
))
855 val dname
= OS
.Path
.joinDirFile
{dir
= resultRoot
,
857 val dname
= OS
.Path
.concat (dname
, domPath
)
860 ignore (OS
.Process
.system (Config
.rm ^
" -rf " ^ dname
))
868 app doNode Config
.nodeIps
;
869 app cleanupNode Config
.nodeIps
872 val rmdom
= rmdom
' true Config
.resultRoot
873 val rmdom
' = rmdom
' false
875 fun homedirOf uname
=
876 Posix
.SysDB
.Passwd
.home (Posix
.SysDB
.getpwnam uname
)
878 fun homedir () = homedirOf (getUser ())
880 type subject
= {node
: string, domain
: string}
882 val describers
: (subject
-> string) list ref
= ref
[]
884 fun registerDescriber f
= describers
:= f
:: !describers
886 fun describeOne arg
= String.concat (map (fn f
=> f arg
) (rev (!describers
)))
888 val line
= "--------------------------------------------------------------\n"
889 val dline
= "==============================================================\n"
892 String.concat (List.mapPartial
894 case describeOne
{node
= node
, domain
= dom
} of
897 SOME (String.concat
[dline
, "Node ", node
, "\n", dline
, "\n", s
]))
900 datatype description
=
901 Filename
of { filename
: string, heading
: string, showEmpty
: bool }
902 | Extension
of { extension
: string, heading
: string -> string }
904 fun considerAll ds
{node
, domain
} =
906 val ds
= map (fn d
=> (d
, ref
[])) ds
908 val path
= Config
.resultRoot
909 val jdf
= OS
.Path
.joinDirFile
910 val path
= jdf
{dir
= path
, file
= node
}
911 val path
= foldr (fn (more
, path
) => jdf
{dir
= path
, file
= more
})
912 path (String.tokens (fn ch
=> ch
= #
".") domain
)
914 if Posix
.FileSys
.access (path
, []) then
916 val dir
= Posix
.FileSys
.opendir path
919 case Posix
.FileSys
.readdir dir
of
922 (app (fn (d
, entries
) =>
924 fun readFile showEmpty entries
' =
926 val fname
= OS
.Path
.joinDirFile
{dir
= path
,
929 val inf
= TextIO.openIn fname
931 fun loop (seenOne
, entries
') =
932 case TextIO.inputLine inf
of
933 NONE
=> if seenOne
orelse showEmpty
then
937 | SOME line
=> loop (true, line
:: entries
')
939 loop (false, entries
')
940 before TextIO.closeIn inf
944 Filename
{filename
, heading
, showEmpty
} =>
945 if fname
= filename
then
946 entries
:= readFile
showEmpty ("\n" :: line
:: "\n" :: heading
:: line
:: !entries
)
949 | Extension
{extension
, heading
} =>
951 val {base
, ext
} = OS
.Path
.splitBaseExt fname
956 if extension
' = extension
then
957 entries
:= readFile
true ("\n" :: line
:: "\n" :: heading base
:: line
:: !entries
)
965 Posix
.FileSys
.closedir dir
;
966 String.concat (List.concat (map (fn (_
, entries
) => rev (!entries
)) ds
))
972 val () = registerDescriber (considerAll
[Filename
{filename
= "soa",
973 heading
= "DNS SOA:",
976 val () = Env
.registerAction ("domainHost",
977 fn (env
, [(EString host
, _
)]) =>
978 SM
.insert (env
, "Hostname",
979 (EString (host ^
"." ^
currentDomain ()), dl
))
980 |
(_
, args
) => Env
.badArgs ("domainHost", args
))