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
29 fun nodeIp node
= valOf (SM
.find (nodeMap
, node
))
32 fun setUser ur
= usr
:= ur
35 val your_doms
= ref SS
.empty
36 fun your_domains () = !your_doms
38 val your_usrs
= ref SS
.empty
39 fun your_users () = !your_usrs
41 val your_grps
= ref SS
.empty
42 fun your_groups () = !your_grps
44 val your_pths
= ref SS
.empty
45 fun your_paths () = !your_pths
48 case map
Int.fromString (String.fields (fn ch
=> ch
= #
".") s
) of
49 [SOME n1
, SOME n2
, SOME n3
, SOME n4
] =>
50 n1
>= 0 andalso n1
< 256 andalso n2
>= 0 andalso n2
< 256 andalso n3
>= 0 andalso n3
< 256 andalso n4
>= 0 andalso n4
< 256
53 fun isIdent ch
= Char.isLower ch
orelse Char.isDigit ch
56 size s
> 0 andalso size s
< 20
57 andalso CharVector
.all (fn ch
=> isIdent ch
orelse ch
= #
"-") s
60 size s
> 0 andalso size s
< 100
61 andalso List.all
validHost (String.fields (fn ch
=> ch
= #
".") s
)
63 fun validNode s
= List.exists (fn s
' => s
= s
') nodes
65 fun yourDomain s
= SS
.member (your_domains (), s
)
66 fun yourUser s
= SS
.member (your_users (), s
)
67 fun yourGroup s
= SS
.member (your_groups (), s
)
69 List.all (fn s
=> s
<> "..") (String.fields (fn ch
=> ch
= #
"/") path
)
70 andalso CharVector
.all (fn ch
=> Char.isAlphaNum ch
orelse ch
= #
"." orelse ch
= #
"/"
71 orelse ch
= #
"-" orelse ch
= #
"_") path
72 andalso SS
.exists (fn s
' => path
= s
' orelse String.isPrefix (s
' ^
"/") path
) (your_paths ())
74 val _
= Env
.type_one
"ip"
78 val _
= Env
.type_one
"host"
82 val _
= Env
.type_one
"domain"
86 val _
= Env
.type_one
"your_domain"
90 val _
= Env
.type_one
"your_user"
94 val _
= Env
.type_one
"your_group"
98 val _
= Env
.type_one
"your_path"
102 val _
= Env
.type_one
"node"
108 val dl
= ErrorMsg
.dummyLoc
110 val nsD
= (EString Config
.defaultNs
, dl
)
111 val serialD
= (EVar
"serialAuto", dl
)
112 val refD
= (EInt Config
.defaultRefresh
, dl
)
113 val retD
= (EInt Config
.defaultRetry
, dl
)
114 val expD
= (EInt Config
.defaultExpiry
, dl
)
115 val minD
= (EInt Config
.defaultMinimum
, dl
)
117 val soaD
= multiApp ((EVar
"soa", dl
),
119 [nsD
, serialD
, refD
, retD
, expD
, minD
])
121 val masterD
= (EApp ((EVar
"internalMaster", dl
),
122 (EString Config
.defaultNode
, dl
)),
125 val _
= Main
.registerDefault ("DNS",
126 (TBase
"dnsKind", dl
),
127 (fn () => multiApp ((EVar
"useDns", dl
),
129 [soaD
, masterD
, (EList
[], dl
)])))
131 val _
= Main
.registerDefault ("TTL",
133 (fn () => (EInt Config
.Bind
.defaultTTL
, dl
)))
135 type soa
= {ns
: string,
142 val serial
= fn (EVar
"serialAuto", _
) => SOME NONE
143 |
(EApp ((EVar
"serialConst", _
), n
), _
) => Option
.map
SOME (Env
.int n
)
146 val soa
= fn (EApp ((EApp ((EApp ((EApp ((EApp ((EApp
147 ((EVar
"soa", _
), ns
), _
),
153 (case (Env
.string ns
, serial sl
, Env
.int rf
,
154 Env
.int ret
, Env
.int exp
, Env
.int min
) of
155 (SOME ns
, SOME sl
, SOME rf
,
156 SOME ret
, SOME exp
, SOME min
) =>
167 ExternalMaster
of string
168 | InternalMaster
of string
170 val master
= fn (EApp ((EVar
"externalMaster", _
), e
), _
) => Option
.map
ExternalMaster (Env
.string e
)
171 |
(EApp ((EVar
"internalMaster", _
), e
), _
) => Option
.map
InternalMaster (Env
.string e
)
175 UseDns
of {soa
: soa
,
177 slaves
: string list
}
180 val dnsKind
= fn (EApp ((EApp ((EApp
181 ((EVar
"useDns", _
), sa
), _
),
184 (case (soa sa
, master mstr
, Env
.list Env
.string slaves
) of
185 (SOME sa
, SOME mstr
, SOME slaves
) =>
186 SOME (UseDns
{soa
= sa
,
192 val befores
= ref (fn (_
: string) => ())
193 val afters
= ref (fn (_
: string) => ())
195 fun registerBefore f
=
199 befores
:= (fn x
=> (old x
; f x
))
202 fun registerAfter f
=
206 afters
:= (fn x
=> (old x
; f x
))
210 val currentPath
= ref (fn (_
: string) => "")
214 fun currentDomain () = !current
216 fun domainFile
{node
, name
} = ((*print ("Opening " ^
!currentPath node ^ name ^
"\n");*)
217 TextIO.openOut (!currentPath node ^ name
))
221 val toks
= String.fields (fn ch
=> ch
= #
".") domain
223 val elems
= foldr (fn (piece
, elems
) =>
225 val elems
= piece
:: elems
229 val path
= String.concatWith
"/"
230 (Config
.resultRoot
:: node
:: rev elems
)
231 val tmpPath
= String.concatWith
"/"
232 (Config
.tmpDir
:: node
:: rev elems
)
234 (if Posix
.FileSys
.ST
.isDir
235 (Posix
.FileSys
.stat path
) then
238 (OS
.FileSys
.remove path
;
239 OS
.FileSys
.mkDir path
))
240 handle OS
.SysErr _
=> OS
.FileSys
.mkDir path
;
242 (if Posix
.FileSys
.ST
.isDir
243 (Posix
.FileSys
.stat tmpPath
) then
246 (OS
.FileSys
.remove tmpPath
;
247 OS
.FileSys
.mkDir tmpPath
))
248 handle OS
.SysErr _
=> OS
.FileSys
.mkDir tmpPath
255 fn (root
, site
) => String.concatWith
"/" (root
:: site
:: rev ("" :: elems
))
258 datatype file_action
' =
259 Add
' of {src
: string, dst
: string}
261 | Modify
' of {src
: string, dst
: string}
263 fun findDiffs (site
, dom
, acts
) =
266 val realPath
= gp (Config
.resultRoot
, site
)
267 val tmpPath
= gp (Config
.tmpDir
, site
)
269 (*val _
= print ("getDiffs(" ^ site ^
", " ^ dom ^
")... " ^ realPath ^
"; " ^ tmpPath ^
"\n")*)
271 val dir
= Posix
.FileSys
.opendir realPath
274 case Posix
.FileSys
.readdir dir
of
275 NONE
=> (Posix
.FileSys
.closedir dir
;
279 val real = OS
.Path
.joinDirFile
{dir
= realPath
,
281 val tmp
= OS
.Path
.joinDirFile
{dir
= tmpPath
,
284 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat
real) then
286 else if Posix
.FileSys
.access (tmp
, []) then
287 if Slave
.shell
[Config
.diff
, " ", real, " ", tmp
] then
290 loopReal ((site
, dom
, realPath
, Modify
' {src
= tmp
, dst
= real}) :: acts
)
292 loopReal ((site
, dom
, realPath
, Delete
' real) :: acts
)
295 val acts
= loopReal acts
297 val dir
= Posix
.FileSys
.opendir tmpPath
300 case Posix
.FileSys
.readdir dir
of
301 NONE
=> (Posix
.FileSys
.closedir dir
;
305 val real = OS
.Path
.joinDirFile
{dir
= realPath
,
307 val tmp
= OS
.Path
.joinDirFile
{dir
= tmpPath
,
310 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat tmp
) then
312 else if Posix
.FileSys
.access (real, []) then
315 loopTmp ((site
, dom
, realPath
, Add
' {src
= tmp
, dst
= real}) :: acts
)
318 val acts
= loopTmp acts
323 fun findAllDiffs () =
325 val dir
= Posix
.FileSys
.opendir Config
.tmpDir
326 val len
= length (String.fields (fn ch
=> ch
= #
"/") Config
.tmpDir
) + 1
328 fun exploreSites diffs
=
329 case Posix
.FileSys
.readdir dir
of
333 fun explore (dname
, diffs
) =
335 val dir
= Posix
.FileSys
.opendir dname
338 case Posix
.FileSys
.readdir dir
of
342 val fname
= OS
.Path
.joinDirFile
{dir
= dname
,
345 loop (if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat fname
) then
347 val dom
= String.fields (fn ch
=> ch
= #
"/") fname
348 val dom
= List.drop (dom
, len
)
349 val dom
= String.concatWith
"." (rev dom
)
351 val dname
' = OS
.Path
.joinDirFile
{dir
= dname
,
355 findDiffs (site
, dom
, diffs
))
362 before Posix
.FileSys
.closedir dir
365 explore (OS
.Path
.joinDirFile
{dir
= Config
.tmpDir
,
370 before Posix
.FileSys
.closedir dir
373 val masterNode
: string option ref
= ref NONE
374 fun dnsMaster () = !masterNode
376 val _
= Env
.containerV_one
"domain"
377 ("domain", Env
.string)
380 val kind
= Env
.env
dnsKind (evs
, "DNS")
381 val ttl
= Env
.env Env
.int (evs
, "TTL")
383 val path
= getPath dom
385 val () = (current
:= dom
;
386 currentPath
:= (fn site
=> path (Config
.tmpDir
, site
)))
388 fun saveSoa (kind
, soa
: soa
) node
=
390 val outf
= domainFile
{node
= node
, name
= "soa"}
392 TextIO.output (outf
, kind
);
393 TextIO.output (outf
, "\n");
394 TextIO.output (outf
, Int.toString ttl
);
395 TextIO.output (outf
, "\n");
396 TextIO.output (outf
, #ns soa
);
397 TextIO.output (outf
, "\n");
400 | SOME n
=> TextIO.output (outf
, Int.toString n
);
401 TextIO.output (outf
, "\n");
402 TextIO.output (outf
, Int.toString (#ref soa
));
403 TextIO.output (outf
, "\n");
404 TextIO.output (outf
, Int.toString (#ret soa
));
405 TextIO.output (outf
, "\n");
406 TextIO.output (outf
, Int.toString (#exp soa
));
407 TextIO.output (outf
, "\n");
408 TextIO.output (outf
, Int.toString (#min soa
));
409 TextIO.output (outf
, "\n");
413 fun saveNamed (kind
, soa
: soa
, masterIp
) node
=
415 val outf
= domainFile
{node
= node
, name
= "named.conf"}
417 TextIO.output (outf
, "\nzone \"");
418 TextIO.output (outf
, dom
);
419 TextIO.output (outf
, "\" IN {\n\ttype ");
420 TextIO.output (outf
, kind
);
421 TextIO.output (outf
, ";\n\tfile \"");
422 TextIO.output (outf
, Config
.Bind
.zonePath
);
423 TextIO.output (outf
, "/");
424 TextIO.output (outf
, dom
);
425 TextIO.output (outf
, ".zone\";\n");
427 "master" => TextIO.output (outf
, "\tallow-update { none; };\n")
428 | _
=> (TextIO.output (outf
, "\tmasters { ");
429 TextIO.output (outf
, masterIp
);
430 TextIO.output (outf
, " };\n"));
431 TextIO.output (outf
, "};\n");
436 NoDns
=> masterNode
:= NONE
441 InternalMaster node
=> valOf (SM
.find (nodeMap
, node
))
442 | ExternalMaster ip
=> ip
444 app (saveSoa ("slave", #soa dns
)) (#slaves dns
);
445 app (saveNamed ("slave", #soa dns
, masterIp
)) (#slaves dns
);
447 InternalMaster node
=>
448 (masterNode
:= SOME node
;
449 saveSoa ("master", #soa dns
) node
;
450 saveNamed ("master", #soa dns
, masterIp
) node
)
451 | _
=> masterNode
:= NONE
;
455 fn () => !afters (!current
))
457 val () = Env
.registerPreTycheck (fn () => (setUser Config
.testUser
;
458 Acl
.read Config
.aclFile
;
459 your_doms
:= Acl
.class
{user
= getUser (),
461 your_usrs
:= Acl
.class
{user
= getUser (),
463 your_grps
:= Acl
.class
{user
= getUser (),
465 your_pths
:= Acl
.class
{user
= getUser (),
468 val () = Env
.registerPre (fn () => (ignore (Slave
.shellF ([Config
.rm
, " -rf ", Config
.tmpDir
, ""],
469 fn cl
=> "Temp file cleanup failed: " ^ cl
));
470 OS
.FileSys
.mkDir Config
.tmpDir
;
471 app (fn node
=> OS
.FileSys
.mkDir
472 (OS
.Path
.joinDirFile
{dir
= Config
.tmpDir
,
475 app (fn node
=> OS
.FileSys
.mkDir
476 (OS
.Path
.joinDirFile
{dir
= Config
.resultRoot
,
478 handle OS
.SysErr _
=> ())
481 val () = Env
.registerPost (fn () =>
483 val diffs
= findAllDiffs ()
485 val diffs
= map (fn (site
, dom
, dir
, Add
' {src
, dst
}) =>
486 (Slave
.shellF ([Config
.cp
, " ", src
, " ", dst
],
487 fn cl
=> "Copy failed: " ^ cl
);
493 |
(site
, dom
, dir
, Delete
' dst
) =>
494 (OS
.FileSys
.remove dst
495 handle OS
.SysErr _
=>
496 ErrorMsg
.error
NONE ("Delete failed for " ^ dst
);
498 {action
= Slave
.Delete
,
502 |
(site
, dom
, dir
, Modify
' {src
, dst
}) =>
503 (Slave
.shellF ([Config
.cp
, " ", src
, " ", dst
],
504 fn cl
=> "Copy failed: " ^ cl
);
506 {action
= Slave
.Modify
,
511 if !ErrorMsg
.anyErrors
then
514 Slave
.handleChanges (map #
2 diffs
)(*;
515 ignore (Slave
.shellF ([Config
.rm
, " -rf ", Config
.tmpDir
, ""],
516 fn cl
=> "Temp file cleanup failed: " ^ cl
))*)