1 (* HCoop
Domtool (http
://hcoop
.sourceforge
.net
/)
2 * Copyright (c
) 2006, 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
23 structure SM
= DataStructures
.StringMap
24 structure SS
= DataStructures
.StringSet
26 val nodes
= map #
1 Config
.nodeIps
27 val nodeMap
= foldl (fn ((node
, ip
), mp
) => SM
.insert (mp
, node
, ip
))
28 SM
.empty Config
.nodeIps
31 fun setUser ur
= usr
:= ur
34 val your_doms
= ref SS
.empty
35 fun your_domains () = !your_doms
38 case map
Int.fromString (String.fields (fn ch
=> ch
= #
".") s
) of
39 [SOME n1
, SOME n2
, SOME n3
, SOME n4
] =>
40 n1
>= 0 andalso n1
< 256 andalso n2
>= 0 andalso n2
< 256 andalso n3
>= 0 andalso n3
< 256 andalso n4
>= 0 andalso n4
< 256
43 fun isIdent ch
= Char.isLower ch
orelse Char.isDigit ch
46 size s
> 0 andalso size s
< 20
47 andalso CharVector
.all (fn ch
=> isIdent ch
orelse ch
= #
"-") s
50 size s
> 0 andalso size s
< 100
51 andalso List.all
validHost (String.fields (fn ch
=> ch
= #
".") s
)
53 fun validNode s
= List.exists (fn s
' => s
= s
') nodes
55 fun yourDomain s
= SS
.member (your_domains (), s
)
57 val _
= Env
.type_one
"ip"
61 val _
= Env
.type_one
"host"
65 val _
= Env
.type_one
"domain"
69 val _
= Env
.type_one
"your_domain"
73 val _
= Env
.type_one
"node"
79 val dl
= ErrorMsg
.dummyLoc
81 val nsD
= (EString Config
.defaultNs
, dl
)
82 val serialD
= (EVar
"serialAuto", dl
)
83 val refD
= (EInt Config
.defaultRefresh
, dl
)
84 val retD
= (EInt Config
.defaultRetry
, dl
)
85 val expD
= (EInt Config
.defaultExpiry
, dl
)
86 val minD
= (EInt Config
.defaultMinimum
, dl
)
88 val soaD
= multiApp ((EVar
"soa", dl
),
90 [nsD
, serialD
, refD
, retD
, expD
, minD
])
92 val masterD
= (EApp ((EVar
"internalMaster", dl
),
93 (EString Config
.defaultNode
, dl
)),
96 val _
= Main
.registerDefault ("DNS",
97 (TBase
"dnsKind", dl
),
98 (multiApp ((EVar
"useDns", dl
),
100 [soaD
, masterD
, (EList
[], dl
)])))
102 val _
= Main
.registerDefault ("TTL",
104 (EInt Config
.Bind
.defaultTTL
, dl
))
106 type soa
= {ns
: string,
113 val serial
= fn (EVar
"serialAuto", _
) => SOME NONE
114 |
(EApp ((EVar
"serialConst", _
), n
), _
) => Option
.map
SOME (Env
.int n
)
117 val soa
= fn (EApp ((EApp ((EApp ((EApp ((EApp ((EApp
118 ((EVar
"soa", _
), ns
), _
),
124 (case (Env
.string ns
, serial sl
, Env
.int rf
,
125 Env
.int ret
, Env
.int exp
, Env
.int min
) of
126 (SOME ns
, SOME sl
, SOME rf
,
127 SOME ret
, SOME exp
, SOME min
) =>
138 ExternalMaster
of string
139 | InternalMaster
of string
141 val master
= fn (EApp ((EVar
"externalMaster", _
), e
), _
) => Option
.map
ExternalMaster (Env
.string e
)
142 |
(EApp ((EVar
"internalMaster", _
), e
), _
) => Option
.map
InternalMaster (Env
.string e
)
146 UseDns
of {soa
: soa
,
148 slaves
: string list
}
151 val dnsKind
= fn (EApp ((EApp ((EApp
152 ((EVar
"useDns", _
), sa
), _
),
155 (case (soa sa
, master mstr
, Env
.list Env
.string slaves
) of
156 (SOME sa
, SOME mstr
, SOME slaves
) =>
157 SOME (UseDns
{soa
= sa
,
163 val befores
= ref (fn (_
: string) => ())
164 val afters
= ref (fn (_
: string) => ())
166 fun registerBefore f
=
170 befores
:= (fn x
=> (old x
; f x
))
173 fun registerAfter f
=
177 afters
:= (fn x
=> (old x
; f x
))
181 val currentPath
= ref (fn (_
: string) => "")
185 fun currentDomain () = !current
187 fun domainFile
{node
, name
} = ((*print ("Opening " ^
!currentPath node ^ name ^
"\n");*)
188 TextIO.openOut (!currentPath node ^ name
))
192 val toks
= String.fields (fn ch
=> ch
= #
".") domain
194 val elems
= foldr (fn (piece
, elems
) =>
196 val elems
= piece
:: elems
200 val path
= String.concatWith
"/"
201 (Config
.resultRoot
:: node
:: rev elems
)
202 val tmpPath
= String.concatWith
"/"
203 (Config
.tmpDir
:: node
:: rev elems
)
205 (if Posix
.FileSys
.ST
.isDir
206 (Posix
.FileSys
.stat path
) then
209 (OS
.FileSys
.remove path
;
210 OS
.FileSys
.mkDir path
))
211 handle OS
.SysErr _
=> OS
.FileSys
.mkDir path
;
213 (if Posix
.FileSys
.ST
.isDir
214 (Posix
.FileSys
.stat tmpPath
) then
217 (OS
.FileSys
.remove tmpPath
;
218 OS
.FileSys
.mkDir tmpPath
))
219 handle OS
.SysErr _
=> OS
.FileSys
.mkDir tmpPath
226 fn (root
, site
) => String.concatWith
"/" (root
:: site
:: rev ("" :: elems
))
229 datatype file_action
' =
230 Add
' of {src
: string, dst
: string}
232 | Modify
' of {src
: string, dst
: string}
234 fun findDiffs (site
, dom
, acts
) =
237 val realPath
= gp (Config
.resultRoot
, site
)
238 val tmpPath
= gp (Config
.tmpDir
, site
)
240 (*val _
= print ("getDiffs(" ^ site ^
", " ^ dom ^
")... " ^ realPath ^
"; " ^ tmpPath ^
"\n")*)
242 val dir
= Posix
.FileSys
.opendir realPath
245 case Posix
.FileSys
.readdir dir
of
246 NONE
=> (Posix
.FileSys
.closedir dir
;
250 val real = OS
.Path
.joinDirFile
{dir
= realPath
,
252 val tmp
= OS
.Path
.joinDirFile
{dir
= tmpPath
,
255 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat
real) then
257 else if Posix
.FileSys
.access (tmp
, []) then
258 if Slave
.shell
[Config
.diff
, " ", real, " ", tmp
] then
261 loopReal ((site
, dom
, realPath
, Modify
' {src
= tmp
, dst
= real}) :: acts
)
263 loopReal ((site
, dom
, realPath
, Delete
' real) :: acts
)
266 val acts
= loopReal acts
268 val dir
= Posix
.FileSys
.opendir tmpPath
271 case Posix
.FileSys
.readdir dir
of
272 NONE
=> (Posix
.FileSys
.closedir dir
;
276 val real = OS
.Path
.joinDirFile
{dir
= realPath
,
278 val tmp
= OS
.Path
.joinDirFile
{dir
= tmpPath
,
281 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat tmp
) then
283 else if Posix
.FileSys
.access (real, []) then
286 loopTmp ((site
, dom
, realPath
, Add
' {src
= tmp
, dst
= real}) :: acts
)
289 val acts
= loopTmp acts
294 fun findAllDiffs () =
296 val dir
= Posix
.FileSys
.opendir Config
.tmpDir
297 val len
= length (String.fields (fn ch
=> ch
= #
"/") Config
.tmpDir
) + 1
299 fun exploreSites diffs
=
300 case Posix
.FileSys
.readdir dir
of
304 fun explore (dname
, diffs
) =
306 val dir
= Posix
.FileSys
.opendir dname
309 case Posix
.FileSys
.readdir dir
of
313 val fname
= OS
.Path
.joinDirFile
{dir
= dname
,
316 loop (if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat fname
) then
318 val dom
= String.fields (fn ch
=> ch
= #
"/") fname
319 val dom
= List.drop (dom
, len
)
320 val dom
= String.concatWith
"." (rev dom
)
322 val dname
' = OS
.Path
.joinDirFile
{dir
= dname
,
326 findDiffs (site
, dom
, diffs
))
333 before Posix
.FileSys
.closedir dir
336 explore (OS
.Path
.joinDirFile
{dir
= Config
.tmpDir
,
341 before Posix
.FileSys
.closedir dir
344 val masterNode
: string option ref
= ref NONE
345 fun dnsMaster () = !masterNode
347 val _
= Env
.containerV_one
"domain"
348 ("domain", Env
.string)
351 val kind
= Env
.env
dnsKind (evs
, "DNS")
352 val ttl
= Env
.env Env
.int (evs
, "TTL")
354 val path
= getPath dom
356 val () = (current
:= dom
;
357 currentPath
:= (fn site
=> path (Config
.tmpDir
, site
)))
359 fun saveSoa (kind
, soa
: soa
) node
=
361 val outf
= domainFile
{node
= node
, name
= "soa"}
363 TextIO.output (outf
, kind
);
364 TextIO.output (outf
, "\n");
365 TextIO.output (outf
, Int.toString ttl
);
366 TextIO.output (outf
, "\n");
367 TextIO.output (outf
, #ns soa
);
368 TextIO.output (outf
, "\n");
371 | SOME n
=> TextIO.output (outf
, Int.toString n
);
372 TextIO.output (outf
, "\n");
373 TextIO.output (outf
, Int.toString (#ref soa
));
374 TextIO.output (outf
, "\n");
375 TextIO.output (outf
, Int.toString (#ret soa
));
376 TextIO.output (outf
, "\n");
377 TextIO.output (outf
, Int.toString (#exp soa
));
378 TextIO.output (outf
, "\n");
379 TextIO.output (outf
, Int.toString (#min soa
));
380 TextIO.output (outf
, "\n");
384 fun saveNamed (kind
, soa
: soa
, masterIp
) node
=
386 val outf
= domainFile
{node
= node
, name
= "named.conf"}
388 TextIO.output (outf
, "\nzone \"");
389 TextIO.output (outf
, dom
);
390 TextIO.output (outf
, "\" IN {\n\ttype ");
391 TextIO.output (outf
, kind
);
392 TextIO.output (outf
, ";\n\tfile \"");
393 TextIO.output (outf
, Config
.Bind
.zonePath
);
394 TextIO.output (outf
, "/");
395 TextIO.output (outf
, dom
);
396 TextIO.output (outf
, ".zone\";\n");
398 "master" => TextIO.output (outf
, "\tallow-update { none; };\n")
399 | _
=> (TextIO.output (outf
, "\tmasters { ");
400 TextIO.output (outf
, masterIp
);
401 TextIO.output (outf
, " };\n"));
402 TextIO.output (outf
, "};\n");
407 NoDns
=> masterNode
:= NONE
412 InternalMaster node
=> valOf (SM
.find (nodeMap
, node
))
413 | ExternalMaster ip
=> ip
415 app (saveSoa ("slave", #soa dns
)) (#slaves dns
);
416 app (saveNamed ("slave", #soa dns
, masterIp
)) (#slaves dns
);
418 InternalMaster node
=>
419 (masterNode
:= SOME node
;
420 saveSoa ("master", #soa dns
) node
;
421 saveNamed ("master", #soa dns
, masterIp
) node
)
422 | _
=> masterNode
:= NONE
;
426 fn () => !afters (!current
))
428 val () = Env
.registerPreTycheck (fn () => (setUser Config
.testUser
;
429 Acl
.read Config
.aclFile
;
430 your_doms
:= Acl
.class
{user
= getUser (),
433 val () = Env
.registerPre (fn () => (ignore (Slave
.shellF ([Config
.rm
, " -rf ", Config
.tmpDir
, ""],
434 fn cl
=> "Temp file cleanup failed: " ^ cl
));
435 OS
.FileSys
.mkDir Config
.tmpDir
;
436 app (fn node
=> OS
.FileSys
.mkDir
437 (OS
.Path
.joinDirFile
{dir
= Config
.tmpDir
,
440 app (fn node
=> OS
.FileSys
.mkDir
441 (OS
.Path
.joinDirFile
{dir
= Config
.resultRoot
,
443 handle OS
.SysErr _
=> ())
446 val () = Env
.registerPost (fn () =>
448 val diffs
= findAllDiffs ()
450 val diffs
= map (fn (site
, dom
, dir
, Add
' {src
, dst
}) =>
451 (Slave
.shellF ([Config
.cp
, " ", src
, " ", dst
],
452 fn cl
=> "Copy failed: " ^ cl
);
458 |
(site
, dom
, dir
, Delete
' dst
) =>
459 (OS
.FileSys
.remove dst
460 handle OS
.SysErr _
=>
461 ErrorMsg
.error
NONE ("Delete failed for " ^ dst
);
463 {action
= Slave
.Delete
,
467 |
(site
, dom
, dir
, Modify
' {src
, dst
}) =>
468 (Slave
.shellF ([Config
.cp
, " ", src
, " ", dst
],
469 fn cl
=> "Copy failed: " ^ cl
);
471 {action
= Slave
.Modify
,
476 if !ErrorMsg
.anyErrors
then
479 Slave
.handleChanges (map #
2 diffs
)(*;
480 ignore (Slave
.shellF ([Config
.rm
, " -rf ", Config
.tmpDir
, ""],
481 fn cl
=> "Temp file cleanup failed: " ^ cl
))*)