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
"no_spaces"
76 (CharVector
.all (fn ch
=> not (Char.isSpace ch
)))
78 val _
= Env
.type_one
"ip"
82 val _
= Env
.type_one
"host"
86 val _
= Env
.type_one
"domain"
90 val _
= Env
.type_one
"your_domain"
94 val _
= Env
.type_one
"your_user"
98 val _
= Env
.type_one
"your_group"
102 val _
= Env
.type_one
"your_path"
106 val _
= Env
.type_one
"node"
112 val dl
= ErrorMsg
.dummyLoc
114 val nsD
= (EString Config
.defaultNs
, dl
)
115 val serialD
= (EVar
"serialAuto", dl
)
116 val refD
= (EInt Config
.defaultRefresh
, dl
)
117 val retD
= (EInt Config
.defaultRetry
, dl
)
118 val expD
= (EInt Config
.defaultExpiry
, dl
)
119 val minD
= (EInt Config
.defaultMinimum
, dl
)
121 val soaD
= multiApp ((EVar
"soa", dl
),
123 [nsD
, serialD
, refD
, retD
, expD
, minD
])
125 val masterD
= (EApp ((EVar
"internalMaster", dl
),
126 (EString Config
.defaultNode
, dl
)),
129 val _
= Main
.registerDefault ("DNS",
130 (TBase
"dnsKind", dl
),
131 (fn () => multiApp ((EVar
"useDns", dl
),
133 [soaD
, masterD
, (EList
[], dl
)])))
135 val _
= Main
.registerDefault ("TTL",
137 (fn () => (EInt Config
.Bind
.defaultTTL
, dl
)))
139 type soa
= {ns
: string,
146 val serial
= fn (EVar
"serialAuto", _
) => SOME NONE
147 |
(EApp ((EVar
"serialConst", _
), n
), _
) => Option
.map
SOME (Env
.int n
)
150 val soa
= fn (EApp ((EApp ((EApp ((EApp ((EApp ((EApp
151 ((EVar
"soa", _
), ns
), _
),
157 (case (Env
.string ns
, serial sl
, Env
.int rf
,
158 Env
.int ret
, Env
.int exp
, Env
.int min
) of
159 (SOME ns
, SOME sl
, SOME rf
,
160 SOME ret
, SOME exp
, SOME min
) =>
171 ExternalMaster
of string
172 | InternalMaster
of string
174 val master
= fn (EApp ((EVar
"externalMaster", _
), e
), _
) => Option
.map
ExternalMaster (Env
.string e
)
175 |
(EApp ((EVar
"internalMaster", _
), e
), _
) => Option
.map
InternalMaster (Env
.string e
)
179 UseDns
of {soa
: soa
,
181 slaves
: string list
}
184 val dnsKind
= fn (EApp ((EApp ((EApp
185 ((EVar
"useDns", _
), sa
), _
),
188 (case (soa sa
, master mstr
, Env
.list Env
.string slaves
) of
189 (SOME sa
, SOME mstr
, SOME slaves
) =>
190 SOME (UseDns
{soa
= sa
,
196 val befores
= ref (fn (_
: string) => ())
197 val afters
= ref (fn (_
: string) => ())
199 fun registerBefore f
=
203 befores
:= (fn x
=> (old x
; f x
))
206 fun registerAfter f
=
210 afters
:= (fn x
=> (old x
; f x
))
214 val currentPath
= ref (fn (_
: string) => "")
218 fun currentDomain () = !current
220 fun domainFile
{node
, name
} = ((*print ("Opening " ^
!currentPath node ^ name ^
"\n");*)
221 TextIO.openOut (!currentPath node ^ name
))
225 val toks
= String.fields (fn ch
=> ch
= #
".") domain
227 val elems
= foldr (fn (piece
, elems
) =>
229 val elems
= piece
:: elems
233 val path
= String.concatWith
"/"
234 (Config
.resultRoot
:: node
:: rev elems
)
235 val tmpPath
= String.concatWith
"/"
236 (Config
.tmpDir
:: node
:: rev elems
)
238 (if Posix
.FileSys
.ST
.isDir
239 (Posix
.FileSys
.stat path
) then
242 (OS
.FileSys
.remove path
;
243 OS
.FileSys
.mkDir path
))
244 handle OS
.SysErr _
=> OS
.FileSys
.mkDir path
;
246 (if Posix
.FileSys
.ST
.isDir
247 (Posix
.FileSys
.stat tmpPath
) then
250 (OS
.FileSys
.remove tmpPath
;
251 OS
.FileSys
.mkDir tmpPath
))
252 handle OS
.SysErr _
=> OS
.FileSys
.mkDir tmpPath
259 fn (root
, site
) => String.concatWith
"/" (root
:: site
:: rev ("" :: elems
))
262 datatype file_action
' =
263 Add
' of {src
: string, dst
: string}
265 | Modify
' of {src
: string, dst
: string}
267 fun findDiffs (site
, dom
, acts
) =
270 val realPath
= gp (Config
.resultRoot
, site
)
271 val tmpPath
= gp (Config
.tmpDir
, site
)
273 (*val _
= print ("getDiffs(" ^ site ^
", " ^ dom ^
")... " ^ realPath ^
"; " ^ tmpPath ^
"\n")*)
275 val dir
= Posix
.FileSys
.opendir realPath
278 case Posix
.FileSys
.readdir dir
of
279 NONE
=> (Posix
.FileSys
.closedir dir
;
283 val real = OS
.Path
.joinDirFile
{dir
= realPath
,
285 val tmp
= OS
.Path
.joinDirFile
{dir
= tmpPath
,
288 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat
real) then
290 else if Posix
.FileSys
.access (tmp
, []) then
291 if Slave
.shell
[Config
.diff
, " ", real, " ", tmp
] then
294 loopReal ((site
, dom
, realPath
, Modify
' {src
= tmp
, dst
= real}) :: acts
)
296 loopReal ((site
, dom
, realPath
, Delete
' real) :: acts
)
299 val acts
= loopReal acts
301 val dir
= Posix
.FileSys
.opendir tmpPath
304 case Posix
.FileSys
.readdir dir
of
305 NONE
=> (Posix
.FileSys
.closedir dir
;
309 val real = OS
.Path
.joinDirFile
{dir
= realPath
,
311 val tmp
= OS
.Path
.joinDirFile
{dir
= tmpPath
,
314 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat tmp
) then
316 else if Posix
.FileSys
.access (real, []) then
319 loopTmp ((site
, dom
, realPath
, Add
' {src
= tmp
, dst
= real}) :: acts
)
322 val acts
= loopTmp acts
327 fun findAllDiffs () =
329 val dir
= Posix
.FileSys
.opendir Config
.tmpDir
330 val len
= length (String.fields (fn ch
=> ch
= #
"/") Config
.tmpDir
) + 1
332 fun exploreSites diffs
=
333 case Posix
.FileSys
.readdir dir
of
337 fun explore (dname
, diffs
) =
339 val dir
= Posix
.FileSys
.opendir dname
342 case Posix
.FileSys
.readdir dir
of
346 val fname
= OS
.Path
.joinDirFile
{dir
= dname
,
349 loop (if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat fname
) then
351 val dom
= String.fields (fn ch
=> ch
= #
"/") fname
352 val dom
= List.drop (dom
, len
)
353 val dom
= String.concatWith
"." (rev dom
)
355 val dname
' = OS
.Path
.joinDirFile
{dir
= dname
,
359 findDiffs (site
, dom
, diffs
))
366 before Posix
.FileSys
.closedir dir
369 explore (OS
.Path
.joinDirFile
{dir
= Config
.tmpDir
,
374 before Posix
.FileSys
.closedir dir
377 val masterNode
: string option ref
= ref NONE
378 fun dnsMaster () = !masterNode
380 val _
= Env
.containerV_one
"domain"
381 ("domain", Env
.string)
384 val kind
= Env
.env
dnsKind (evs
, "DNS")
385 val ttl
= Env
.env Env
.int (evs
, "TTL")
387 val path
= getPath dom
389 val () = (current
:= dom
;
390 currentPath
:= (fn site
=> path (Config
.tmpDir
, site
)))
392 fun saveSoa (kind
, soa
: soa
) node
=
394 val outf
= domainFile
{node
= node
, name
= "soa"}
396 TextIO.output (outf
, kind
);
397 TextIO.output (outf
, "\n");
398 TextIO.output (outf
, Int.toString ttl
);
399 TextIO.output (outf
, "\n");
400 TextIO.output (outf
, #ns soa
);
401 TextIO.output (outf
, "\n");
404 | SOME n
=> TextIO.output (outf
, Int.toString n
);
405 TextIO.output (outf
, "\n");
406 TextIO.output (outf
, Int.toString (#ref soa
));
407 TextIO.output (outf
, "\n");
408 TextIO.output (outf
, Int.toString (#ret soa
));
409 TextIO.output (outf
, "\n");
410 TextIO.output (outf
, Int.toString (#exp soa
));
411 TextIO.output (outf
, "\n");
412 TextIO.output (outf
, Int.toString (#min soa
));
413 TextIO.output (outf
, "\n");
417 fun saveNamed (kind
, soa
: soa
, masterIp
) node
=
419 val outf
= domainFile
{node
= node
, name
= "named.conf"}
421 TextIO.output (outf
, "\nzone \"");
422 TextIO.output (outf
, dom
);
423 TextIO.output (outf
, "\" IN {\n\ttype ");
424 TextIO.output (outf
, kind
);
425 TextIO.output (outf
, ";\n\tfile \"");
426 TextIO.output (outf
, Config
.Bind
.zonePath
);
427 TextIO.output (outf
, "/");
428 TextIO.output (outf
, dom
);
429 TextIO.output (outf
, ".zone\";\n");
431 "master" => TextIO.output (outf
, "\tallow-update { none; };\n")
432 | _
=> (TextIO.output (outf
, "\tmasters { ");
433 TextIO.output (outf
, masterIp
);
434 TextIO.output (outf
, " };\n"));
435 TextIO.output (outf
, "};\n");
440 NoDns
=> masterNode
:= NONE
445 InternalMaster node
=> valOf (SM
.find (nodeMap
, node
))
446 | ExternalMaster ip
=> ip
448 app (saveSoa ("slave", #soa dns
)) (#slaves dns
);
449 app (saveNamed ("slave", #soa dns
, masterIp
)) (#slaves dns
);
451 InternalMaster node
=>
452 (masterNode
:= SOME node
;
453 saveSoa ("master", #soa dns
) node
;
454 saveNamed ("master", #soa dns
, masterIp
) node
)
455 | _
=> masterNode
:= NONE
;
459 fn () => !afters (!current
))
461 val () = Env
.registerPreTycheck (fn () => (setUser Config
.testUser
;
462 Acl
.read Config
.aclFile
;
463 your_doms
:= Acl
.class
{user
= getUser (),
465 your_usrs
:= Acl
.class
{user
= getUser (),
467 your_grps
:= Acl
.class
{user
= getUser (),
469 your_pths
:= Acl
.class
{user
= getUser (),
472 val () = Env
.registerPre (fn () => (ignore (Slave
.shellF ([Config
.rm
, " -rf ", Config
.tmpDir
, ""],
473 fn cl
=> "Temp file cleanup failed: " ^ cl
));
474 OS
.FileSys
.mkDir Config
.tmpDir
;
475 app (fn node
=> OS
.FileSys
.mkDir
476 (OS
.Path
.joinDirFile
{dir
= Config
.tmpDir
,
479 app (fn node
=> OS
.FileSys
.mkDir
480 (OS
.Path
.joinDirFile
{dir
= Config
.resultRoot
,
482 handle OS
.SysErr _
=> ())
485 val () = Env
.registerPost (fn () =>
487 val diffs
= findAllDiffs ()
489 val diffs
= map (fn (site
, dom
, dir
, Add
' {src
, dst
}) =>
490 (Slave
.shellF ([Config
.cp
, " ", src
, " ", dst
],
491 fn cl
=> "Copy failed: " ^ cl
);
497 |
(site
, dom
, dir
, Delete
' dst
) =>
498 (OS
.FileSys
.remove dst
499 handle OS
.SysErr _
=>
500 ErrorMsg
.error
NONE ("Delete failed for " ^ dst
);
502 {action
= Slave
.Delete
,
506 |
(site
, dom
, dir
, Modify
' {src
, dst
}) =>
507 (Slave
.shellF ([Config
.cp
, " ", src
, " ", dst
],
508 fn cl
=> "Copy failed: " ^ cl
);
510 {action
= Slave
.Modify
,
515 if !ErrorMsg
.anyErrors
then
518 Slave
.handleChanges (map #
2 diffs
)(*;
519 ignore (Slave
.shellF ([Config
.rm
, " -rf ", Config
.tmpDir
, ""],
520 fn cl
=> "Temp file cleanup failed: " ^ cl
))*)