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 fun yourDomainHost s
=
76 val (pref
, suf
) = Substring
.splitl (fn ch
=> ch
<> #
".") (Substring
.full s
)
78 Substring
.size suf
> 0
79 andalso validHost (Substring
.string pref
)
80 andalso yourDomain (Substring
.string
81 (Substring
.slice (suf
, 1, NONE
)))
84 fun validUser s
= size s
> 0 andalso size s
< 20
85 andalso CharVector
.all
Char.isAlphaNum s
87 val validGroup
= validUser
89 val _
= Env
.type_one
"no_spaces"
91 (CharVector
.all (fn ch
=> not (Char.isSpace ch
)))
92 val _
= Env
.type_one
"no_newlines"
94 (CharVector
.all (fn ch
=> ch
<> #
"\n" andalso ch
<> #
"\r"))
96 val _
= Env
.type_one
"ip"
100 val _
= Env
.type_one
"host"
104 val _
= Env
.type_one
"domain"
108 val _
= Env
.type_one
"your_domain"
112 val _
= Env
.type_one
"your_domain_host"
116 val _
= Env
.type_one
"user"
120 val _
= Env
.type_one
"group"
124 val _
= Env
.type_one
"your_user"
128 val _
= Env
.type_one
"your_group"
132 val _
= Env
.type_one
"your_path"
136 val _
= Env
.type_one
"node"
142 val dl
= ErrorMsg
.dummyLoc
144 val nsD
= (EString Config
.defaultNs
, dl
)
145 val serialD
= (EVar
"serialAuto", dl
)
146 val refD
= (EInt Config
.defaultRefresh
, dl
)
147 val retD
= (EInt Config
.defaultRetry
, dl
)
148 val expD
= (EInt Config
.defaultExpiry
, dl
)
149 val minD
= (EInt Config
.defaultMinimum
, dl
)
151 val soaD
= multiApp ((EVar
"soa", dl
),
153 [nsD
, serialD
, refD
, retD
, expD
, minD
])
155 val masterD
= (EApp ((EVar
"internalMaster", dl
),
156 (EString Config
.defaultNode
, dl
)),
159 val _
= Main
.registerDefault ("DNS",
160 (TBase
"dnsKind", dl
),
161 (fn () => multiApp ((EVar
"useDns", dl
),
163 [soaD
, masterD
, (EList
[], dl
)])))
165 val _
= Main
.registerDefault ("TTL",
167 (fn () => (EInt Config
.Bind
.defaultTTL
, dl
)))
169 type soa
= {ns
: string,
176 val serial
= fn (EVar
"serialAuto", _
) => SOME NONE
177 |
(EApp ((EVar
"serialConst", _
), n
), _
) => Option
.map
SOME (Env
.int n
)
180 val soa
= fn (EApp ((EApp ((EApp ((EApp ((EApp ((EApp
181 ((EVar
"soa", _
), ns
), _
),
187 (case (Env
.string ns
, serial sl
, Env
.int rf
,
188 Env
.int ret
, Env
.int exp
, Env
.int min
) of
189 (SOME ns
, SOME sl
, SOME rf
,
190 SOME ret
, SOME exp
, SOME min
) =>
201 ExternalMaster
of string
202 | InternalMaster
of string
204 val master
= fn (EApp ((EVar
"externalMaster", _
), e
), _
) => Option
.map
ExternalMaster (Env
.string e
)
205 |
(EApp ((EVar
"internalMaster", _
), e
), _
) => Option
.map
InternalMaster (Env
.string e
)
209 UseDns
of {soa
: soa
,
211 slaves
: string list
}
214 val dnsKind
= fn (EApp ((EApp ((EApp
215 ((EVar
"useDns", _
), sa
), _
),
218 (case (soa sa
, master mstr
, Env
.list Env
.string slaves
) of
219 (SOME sa
, SOME mstr
, SOME slaves
) =>
220 SOME (UseDns
{soa
= sa
,
226 val befores
= ref (fn (_
: string) => ())
227 val afters
= ref (fn (_
: string) => ())
229 fun registerBefore f
=
233 befores
:= (fn x
=> (old x
; f x
))
236 fun registerAfter f
=
240 afters
:= (fn x
=> (old x
; f x
))
244 val currentPath
= ref (fn (_
: string) => "")
248 fun currentDomain () = !current
250 fun domainFile
{node
, name
} = ((*print ("Opening " ^
!currentPath node ^ name ^
"\n");*)
251 TextIO.openOut (!currentPath node ^ name
))
255 val toks
= String.fields (fn ch
=> ch
= #
".") domain
257 val elems
= foldr (fn (piece
, elems
) =>
259 val elems
= piece
:: elems
263 val path
= String.concatWith
"/"
264 (Config
.resultRoot
:: node
:: rev elems
)
265 val tmpPath
= String.concatWith
"/"
266 (Config
.tmpDir
:: node
:: rev elems
)
268 (if Posix
.FileSys
.ST
.isDir
269 (Posix
.FileSys
.stat path
) then
272 (OS
.FileSys
.remove path
;
273 OS
.FileSys
.mkDir path
))
274 handle OS
.SysErr _
=> OS
.FileSys
.mkDir path
;
276 (if Posix
.FileSys
.ST
.isDir
277 (Posix
.FileSys
.stat tmpPath
) then
280 (OS
.FileSys
.remove tmpPath
;
281 OS
.FileSys
.mkDir tmpPath
))
282 handle OS
.SysErr _
=> OS
.FileSys
.mkDir tmpPath
289 fn (root
, site
) => String.concatWith
"/" (root
:: site
:: rev ("" :: elems
))
292 datatype file_action
' =
293 Add
' of {src
: string, dst
: string}
295 | Modify
' of {src
: string, dst
: string}
297 fun findDiffs (site
, dom
, acts
) =
300 val realPath
= gp (Config
.resultRoot
, site
)
301 val tmpPath
= gp (Config
.tmpDir
, site
)
303 (*val _
= print ("getDiffs(" ^ site ^
", " ^ dom ^
")... " ^ realPath ^
"; " ^ tmpPath ^
"\n")*)
305 val dir
= Posix
.FileSys
.opendir realPath
308 case Posix
.FileSys
.readdir dir
of
309 NONE
=> (Posix
.FileSys
.closedir dir
;
313 val real = OS
.Path
.joinDirFile
{dir
= realPath
,
315 val tmp
= OS
.Path
.joinDirFile
{dir
= tmpPath
,
318 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat
real) then
320 else if Posix
.FileSys
.access (tmp
, []) then
321 if Slave
.shell
[Config
.diff
, " ", real, " ", tmp
] then
324 loopReal ((site
, dom
, realPath
, Modify
' {src
= tmp
, dst
= real}) :: acts
)
326 loopReal ((site
, dom
, realPath
, Delete
' real) :: acts
)
329 val acts
= loopReal acts
331 val dir
= Posix
.FileSys
.opendir tmpPath
334 case Posix
.FileSys
.readdir dir
of
335 NONE
=> (Posix
.FileSys
.closedir dir
;
339 val real = OS
.Path
.joinDirFile
{dir
= realPath
,
341 val tmp
= OS
.Path
.joinDirFile
{dir
= tmpPath
,
344 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat tmp
) then
346 else if Posix
.FileSys
.access (real, []) then
349 loopTmp ((site
, dom
, realPath
, Add
' {src
= tmp
, dst
= real}) :: acts
)
352 val acts
= loopTmp acts
357 fun findAllDiffs () =
359 val dir
= Posix
.FileSys
.opendir Config
.tmpDir
360 val len
= length (String.fields (fn ch
=> ch
= #
"/") Config
.tmpDir
) + 1
362 fun exploreSites diffs
=
363 case Posix
.FileSys
.readdir dir
of
367 fun explore (dname
, diffs
) =
369 val dir
= Posix
.FileSys
.opendir dname
372 case Posix
.FileSys
.readdir dir
of
376 val fname
= OS
.Path
.joinDirFile
{dir
= dname
,
379 loop (if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat fname
) then
381 val dom
= String.fields (fn ch
=> ch
= #
"/") fname
382 val dom
= List.drop (dom
, len
)
383 val dom
= String.concatWith
"." (rev dom
)
385 val dname
' = OS
.Path
.joinDirFile
{dir
= dname
,
389 findDiffs (site
, dom
, diffs
))
396 before Posix
.FileSys
.closedir dir
399 explore (OS
.Path
.joinDirFile
{dir
= Config
.tmpDir
,
404 before Posix
.FileSys
.closedir dir
407 val masterNode
: string option ref
= ref NONE
408 fun dnsMaster () = !masterNode
410 val _
= Env
.containerV_one
"domain"
411 ("domain", Env
.string)
414 val kind
= Env
.env
dnsKind (evs
, "DNS")
415 val ttl
= Env
.env Env
.int (evs
, "TTL")
417 val path
= getPath dom
419 val () = (current
:= dom
;
420 currentPath
:= (fn site
=> path (Config
.tmpDir
, site
)))
422 fun saveSoa (kind
, soa
: soa
) node
=
424 val outf
= domainFile
{node
= node
, name
= "soa"}
426 TextIO.output (outf
, kind
);
427 TextIO.output (outf
, "\n");
428 TextIO.output (outf
, Int.toString ttl
);
429 TextIO.output (outf
, "\n");
430 TextIO.output (outf
, #ns soa
);
431 TextIO.output (outf
, "\n");
434 | SOME n
=> TextIO.output (outf
, Int.toString n
);
435 TextIO.output (outf
, "\n");
436 TextIO.output (outf
, Int.toString (#ref soa
));
437 TextIO.output (outf
, "\n");
438 TextIO.output (outf
, Int.toString (#ret soa
));
439 TextIO.output (outf
, "\n");
440 TextIO.output (outf
, Int.toString (#exp soa
));
441 TextIO.output (outf
, "\n");
442 TextIO.output (outf
, Int.toString (#min soa
));
443 TextIO.output (outf
, "\n");
447 fun saveNamed (kind
, soa
: soa
, masterIp
) node
=
449 val outf
= domainFile
{node
= node
, name
= "named.conf"}
451 TextIO.output (outf
, "\nzone \"");
452 TextIO.output (outf
, dom
);
453 TextIO.output (outf
, "\" IN {\n\ttype ");
454 TextIO.output (outf
, kind
);
455 TextIO.output (outf
, ";\n\tfile \"");
456 TextIO.output (outf
, Config
.Bind
.zonePath
);
457 TextIO.output (outf
, "/");
458 TextIO.output (outf
, dom
);
459 TextIO.output (outf
, ".zone\";\n");
461 "master" => TextIO.output (outf
, "\tallow-update { none; };\n")
462 | _
=> (TextIO.output (outf
, "\tmasters { ");
463 TextIO.output (outf
, masterIp
);
464 TextIO.output (outf
, " };\n"));
465 TextIO.output (outf
, "};\n");
470 NoDns
=> masterNode
:= NONE
475 InternalMaster node
=> valOf (SM
.find (nodeMap
, node
))
476 | ExternalMaster ip
=> ip
478 app (saveSoa ("slave", #soa dns
)) (#slaves dns
);
479 app (saveNamed ("slave", #soa dns
, masterIp
)) (#slaves dns
);
481 InternalMaster node
=>
482 (masterNode
:= SOME node
;
483 saveSoa ("master", #soa dns
) node
;
484 saveNamed ("master", #soa dns
, masterIp
) node
)
485 | _
=> masterNode
:= NONE
;
489 fn () => !afters (!current
))
491 val () = Env
.registerPreTycheck (fn () => (setUser Config
.testUser
;
492 Acl
.read Config
.aclFile
;
493 your_doms
:= Acl
.class
{user
= getUser (),
495 your_usrs
:= Acl
.class
{user
= getUser (),
497 your_grps
:= Acl
.class
{user
= getUser (),
499 your_pths
:= Acl
.class
{user
= getUser (),
502 val () = Env
.registerPre (fn () => (ignore (Slave
.shellF ([Config
.rm
, " -rf ", Config
.tmpDir
, ""],
503 fn cl
=> "Temp file cleanup failed: " ^ cl
));
504 OS
.FileSys
.mkDir Config
.tmpDir
;
505 app (fn node
=> OS
.FileSys
.mkDir
506 (OS
.Path
.joinDirFile
{dir
= Config
.tmpDir
,
509 app (fn node
=> OS
.FileSys
.mkDir
510 (OS
.Path
.joinDirFile
{dir
= Config
.resultRoot
,
512 handle OS
.SysErr _
=> ())
515 val () = Env
.registerPost (fn () =>
517 val diffs
= findAllDiffs ()
519 val diffs
= map (fn (site
, dom
, dir
, Add
' {src
, dst
}) =>
520 (Slave
.shellF ([Config
.cp
, " ", src
, " ", dst
],
521 fn cl
=> "Copy failed: " ^ cl
);
527 |
(site
, dom
, dir
, Delete
' dst
) =>
528 (OS
.FileSys
.remove dst
529 handle OS
.SysErr _
=>
530 ErrorMsg
.error
NONE ("Delete failed for " ^ dst
);
532 {action
= Slave
.Delete
,
536 |
(site
, dom
, dir
, Modify
' {src
, dst
}) =>
537 (Slave
.shellF ([Config
.cp
, " ", src
, " ", dst
],
538 fn cl
=> "Copy failed: " ^ cl
);
540 {action
= Slave
.Modify
,
545 if !ErrorMsg
.anyErrors
then
548 Slave
.handleChanges (map #
2 diffs
)(*;
549 ignore (Slave
.shellF ([Config
.rm
, " -rf ", Config
.tmpDir
, ""],
550 fn cl
=> "Temp file cleanup failed: " ^ cl
))*)