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
)))
77 val _
= Env
.type_one
"no_newlines"
79 (CharVector
.all (fn ch
=> ch
<> #
"\n" andalso ch
<> #
"\r"))
81 val _
= Env
.type_one
"ip"
85 val _
= Env
.type_one
"host"
89 val _
= Env
.type_one
"domain"
93 val _
= Env
.type_one
"your_domain"
97 val _
= Env
.type_one
"your_user"
101 val _
= Env
.type_one
"your_group"
105 val _
= Env
.type_one
"your_path"
109 val _
= Env
.type_one
"node"
115 val dl
= ErrorMsg
.dummyLoc
117 val nsD
= (EString Config
.defaultNs
, dl
)
118 val serialD
= (EVar
"serialAuto", dl
)
119 val refD
= (EInt Config
.defaultRefresh
, dl
)
120 val retD
= (EInt Config
.defaultRetry
, dl
)
121 val expD
= (EInt Config
.defaultExpiry
, dl
)
122 val minD
= (EInt Config
.defaultMinimum
, dl
)
124 val soaD
= multiApp ((EVar
"soa", dl
),
126 [nsD
, serialD
, refD
, retD
, expD
, minD
])
128 val masterD
= (EApp ((EVar
"internalMaster", dl
),
129 (EString Config
.defaultNode
, dl
)),
132 val _
= Main
.registerDefault ("DNS",
133 (TBase
"dnsKind", dl
),
134 (fn () => multiApp ((EVar
"useDns", dl
),
136 [soaD
, masterD
, (EList
[], dl
)])))
138 val _
= Main
.registerDefault ("TTL",
140 (fn () => (EInt Config
.Bind
.defaultTTL
, dl
)))
142 type soa
= {ns
: string,
149 val serial
= fn (EVar
"serialAuto", _
) => SOME NONE
150 |
(EApp ((EVar
"serialConst", _
), n
), _
) => Option
.map
SOME (Env
.int n
)
153 val soa
= fn (EApp ((EApp ((EApp ((EApp ((EApp ((EApp
154 ((EVar
"soa", _
), ns
), _
),
160 (case (Env
.string ns
, serial sl
, Env
.int rf
,
161 Env
.int ret
, Env
.int exp
, Env
.int min
) of
162 (SOME ns
, SOME sl
, SOME rf
,
163 SOME ret
, SOME exp
, SOME min
) =>
174 ExternalMaster
of string
175 | InternalMaster
of string
177 val master
= fn (EApp ((EVar
"externalMaster", _
), e
), _
) => Option
.map
ExternalMaster (Env
.string e
)
178 |
(EApp ((EVar
"internalMaster", _
), e
), _
) => Option
.map
InternalMaster (Env
.string e
)
182 UseDns
of {soa
: soa
,
184 slaves
: string list
}
187 val dnsKind
= fn (EApp ((EApp ((EApp
188 ((EVar
"useDns", _
), sa
), _
),
191 (case (soa sa
, master mstr
, Env
.list Env
.string slaves
) of
192 (SOME sa
, SOME mstr
, SOME slaves
) =>
193 SOME (UseDns
{soa
= sa
,
199 val befores
= ref (fn (_
: string) => ())
200 val afters
= ref (fn (_
: string) => ())
202 fun registerBefore f
=
206 befores
:= (fn x
=> (old x
; f x
))
209 fun registerAfter f
=
213 afters
:= (fn x
=> (old x
; f x
))
217 val currentPath
= ref (fn (_
: string) => "")
221 fun currentDomain () = !current
223 fun domainFile
{node
, name
} = ((*print ("Opening " ^
!currentPath node ^ name ^
"\n");*)
224 TextIO.openOut (!currentPath node ^ name
))
228 val toks
= String.fields (fn ch
=> ch
= #
".") domain
230 val elems
= foldr (fn (piece
, elems
) =>
232 val elems
= piece
:: elems
236 val path
= String.concatWith
"/"
237 (Config
.resultRoot
:: node
:: rev elems
)
238 val tmpPath
= String.concatWith
"/"
239 (Config
.tmpDir
:: node
:: rev elems
)
241 (if Posix
.FileSys
.ST
.isDir
242 (Posix
.FileSys
.stat path
) then
245 (OS
.FileSys
.remove path
;
246 OS
.FileSys
.mkDir path
))
247 handle OS
.SysErr _
=> OS
.FileSys
.mkDir path
;
249 (if Posix
.FileSys
.ST
.isDir
250 (Posix
.FileSys
.stat tmpPath
) then
253 (OS
.FileSys
.remove tmpPath
;
254 OS
.FileSys
.mkDir tmpPath
))
255 handle OS
.SysErr _
=> OS
.FileSys
.mkDir tmpPath
262 fn (root
, site
) => String.concatWith
"/" (root
:: site
:: rev ("" :: elems
))
265 datatype file_action
' =
266 Add
' of {src
: string, dst
: string}
268 | Modify
' of {src
: string, dst
: string}
270 fun findDiffs (site
, dom
, acts
) =
273 val realPath
= gp (Config
.resultRoot
, site
)
274 val tmpPath
= gp (Config
.tmpDir
, site
)
276 (*val _
= print ("getDiffs(" ^ site ^
", " ^ dom ^
")... " ^ realPath ^
"; " ^ tmpPath ^
"\n")*)
278 val dir
= Posix
.FileSys
.opendir realPath
281 case Posix
.FileSys
.readdir dir
of
282 NONE
=> (Posix
.FileSys
.closedir dir
;
286 val real = OS
.Path
.joinDirFile
{dir
= realPath
,
288 val tmp
= OS
.Path
.joinDirFile
{dir
= tmpPath
,
291 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat
real) then
293 else if Posix
.FileSys
.access (tmp
, []) then
294 if Slave
.shell
[Config
.diff
, " ", real, " ", tmp
] then
297 loopReal ((site
, dom
, realPath
, Modify
' {src
= tmp
, dst
= real}) :: acts
)
299 loopReal ((site
, dom
, realPath
, Delete
' real) :: acts
)
302 val acts
= loopReal acts
304 val dir
= Posix
.FileSys
.opendir tmpPath
307 case Posix
.FileSys
.readdir dir
of
308 NONE
=> (Posix
.FileSys
.closedir dir
;
312 val real = OS
.Path
.joinDirFile
{dir
= realPath
,
314 val tmp
= OS
.Path
.joinDirFile
{dir
= tmpPath
,
317 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat tmp
) then
319 else if Posix
.FileSys
.access (real, []) then
322 loopTmp ((site
, dom
, realPath
, Add
' {src
= tmp
, dst
= real}) :: acts
)
325 val acts
= loopTmp acts
330 fun findAllDiffs () =
332 val dir
= Posix
.FileSys
.opendir Config
.tmpDir
333 val len
= length (String.fields (fn ch
=> ch
= #
"/") Config
.tmpDir
) + 1
335 fun exploreSites diffs
=
336 case Posix
.FileSys
.readdir dir
of
340 fun explore (dname
, diffs
) =
342 val dir
= Posix
.FileSys
.opendir dname
345 case Posix
.FileSys
.readdir dir
of
349 val fname
= OS
.Path
.joinDirFile
{dir
= dname
,
352 loop (if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat fname
) then
354 val dom
= String.fields (fn ch
=> ch
= #
"/") fname
355 val dom
= List.drop (dom
, len
)
356 val dom
= String.concatWith
"." (rev dom
)
358 val dname
' = OS
.Path
.joinDirFile
{dir
= dname
,
362 findDiffs (site
, dom
, diffs
))
369 before Posix
.FileSys
.closedir dir
372 explore (OS
.Path
.joinDirFile
{dir
= Config
.tmpDir
,
377 before Posix
.FileSys
.closedir dir
380 val masterNode
: string option ref
= ref NONE
381 fun dnsMaster () = !masterNode
383 val _
= Env
.containerV_one
"domain"
384 ("domain", Env
.string)
387 val kind
= Env
.env
dnsKind (evs
, "DNS")
388 val ttl
= Env
.env Env
.int (evs
, "TTL")
390 val path
= getPath dom
392 val () = (current
:= dom
;
393 currentPath
:= (fn site
=> path (Config
.tmpDir
, site
)))
395 fun saveSoa (kind
, soa
: soa
) node
=
397 val outf
= domainFile
{node
= node
, name
= "soa"}
399 TextIO.output (outf
, kind
);
400 TextIO.output (outf
, "\n");
401 TextIO.output (outf
, Int.toString ttl
);
402 TextIO.output (outf
, "\n");
403 TextIO.output (outf
, #ns soa
);
404 TextIO.output (outf
, "\n");
407 | SOME n
=> TextIO.output (outf
, Int.toString n
);
408 TextIO.output (outf
, "\n");
409 TextIO.output (outf
, Int.toString (#ref soa
));
410 TextIO.output (outf
, "\n");
411 TextIO.output (outf
, Int.toString (#ret soa
));
412 TextIO.output (outf
, "\n");
413 TextIO.output (outf
, Int.toString (#exp soa
));
414 TextIO.output (outf
, "\n");
415 TextIO.output (outf
, Int.toString (#min soa
));
416 TextIO.output (outf
, "\n");
420 fun saveNamed (kind
, soa
: soa
, masterIp
) node
=
422 val outf
= domainFile
{node
= node
, name
= "named.conf"}
424 TextIO.output (outf
, "\nzone \"");
425 TextIO.output (outf
, dom
);
426 TextIO.output (outf
, "\" IN {\n\ttype ");
427 TextIO.output (outf
, kind
);
428 TextIO.output (outf
, ";\n\tfile \"");
429 TextIO.output (outf
, Config
.Bind
.zonePath
);
430 TextIO.output (outf
, "/");
431 TextIO.output (outf
, dom
);
432 TextIO.output (outf
, ".zone\";\n");
434 "master" => TextIO.output (outf
, "\tallow-update { none; };\n")
435 | _
=> (TextIO.output (outf
, "\tmasters { ");
436 TextIO.output (outf
, masterIp
);
437 TextIO.output (outf
, " };\n"));
438 TextIO.output (outf
, "};\n");
443 NoDns
=> masterNode
:= NONE
448 InternalMaster node
=> valOf (SM
.find (nodeMap
, node
))
449 | ExternalMaster ip
=> ip
451 app (saveSoa ("slave", #soa dns
)) (#slaves dns
);
452 app (saveNamed ("slave", #soa dns
, masterIp
)) (#slaves dns
);
454 InternalMaster node
=>
455 (masterNode
:= SOME node
;
456 saveSoa ("master", #soa dns
) node
;
457 saveNamed ("master", #soa dns
, masterIp
) node
)
458 | _
=> masterNode
:= NONE
;
462 fn () => !afters (!current
))
464 val () = Env
.registerPreTycheck (fn () => (setUser Config
.testUser
;
465 Acl
.read Config
.aclFile
;
466 your_doms
:= Acl
.class
{user
= getUser (),
468 your_usrs
:= Acl
.class
{user
= getUser (),
470 your_grps
:= Acl
.class
{user
= getUser (),
472 your_pths
:= Acl
.class
{user
= getUser (),
475 val () = Env
.registerPre (fn () => (ignore (Slave
.shellF ([Config
.rm
, " -rf ", Config
.tmpDir
, ""],
476 fn cl
=> "Temp file cleanup failed: " ^ cl
));
477 OS
.FileSys
.mkDir Config
.tmpDir
;
478 app (fn node
=> OS
.FileSys
.mkDir
479 (OS
.Path
.joinDirFile
{dir
= Config
.tmpDir
,
482 app (fn node
=> OS
.FileSys
.mkDir
483 (OS
.Path
.joinDirFile
{dir
= Config
.resultRoot
,
485 handle OS
.SysErr _
=> ())
488 val () = Env
.registerPost (fn () =>
490 val diffs
= findAllDiffs ()
492 val diffs
= map (fn (site
, dom
, dir
, Add
' {src
, dst
}) =>
493 (Slave
.shellF ([Config
.cp
, " ", src
, " ", dst
],
494 fn cl
=> "Copy failed: " ^ cl
);
500 |
(site
, dom
, dir
, Delete
' dst
) =>
501 (OS
.FileSys
.remove dst
502 handle OS
.SysErr _
=>
503 ErrorMsg
.error
NONE ("Delete failed for " ^ dst
);
505 {action
= Slave
.Delete
,
509 |
(site
, dom
, dir
, Modify
' {src
, dst
}) =>
510 (Slave
.shellF ([Config
.cp
, " ", src
, " ", dst
],
511 fn cl
=> "Copy failed: " ^ cl
);
513 {action
= Slave
.Modify
,
518 if !ErrorMsg
.anyErrors
then
521 Slave
.handleChanges (map #
2 diffs
)(*;
522 ignore (Slave
.shellF ([Config
.rm
, " -rf ", Config
.tmpDir
, ""],
523 fn cl
=> "Temp file cleanup failed: " ^ cl
))*)