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
))
34 val your_doms
= ref SS
.empty
35 fun your_domains () = !your_doms
37 val your_usrs
= ref SS
.empty
38 fun your_users () = !your_usrs
40 val your_grps
= ref SS
.empty
41 fun your_groups () = !your_grps
43 val your_pths
= ref SS
.empty
44 fun your_paths () = !your_pths
47 (usr
:= Config
.testUser
;
48 your_doms
:= Acl
.class
{user
= getUser (),
50 your_usrs
:= Acl
.class
{user
= getUser (),
52 your_grps
:= Acl
.class
{user
= getUser (),
54 your_pths
:= Acl
.class
{user
= getUser (),
58 case map
Int.fromString (String.fields (fn ch
=> ch
= #
".") s
) of
59 [SOME n1
, SOME n2
, SOME n3
, SOME n4
] =>
60 n1
>= 0 andalso n1
< 256 andalso n2
>= 0 andalso n2
< 256 andalso n3
>= 0 andalso n3
< 256 andalso n4
>= 0 andalso n4
< 256
63 fun isIdent ch
= Char.isLower ch
orelse Char.isDigit ch
66 size s
> 0 andalso size s
< 20
67 andalso CharVector
.all (fn ch
=> isIdent ch
orelse ch
= #
"-") s
70 size s
> 0 andalso size s
< 100
71 andalso List.all
validHost (String.fields (fn ch
=> ch
= #
".") s
)
73 fun validNode s
= List.exists (fn s
' => s
= s
') nodes
75 fun yourDomain s
= SS
.member (your_domains (), s
)
76 fun yourUser s
= SS
.member (your_users (), s
)
77 fun yourGroup s
= SS
.member (your_groups (), s
)
79 List.all (fn s
=> s
<> "..") (String.fields (fn ch
=> ch
= #
"/") path
)
80 andalso CharVector
.all (fn ch
=> Char.isAlphaNum ch
orelse ch
= #
"." orelse ch
= #
"/"
81 orelse ch
= #
"-" orelse ch
= #
"_") path
82 andalso SS
.exists (fn s
' => path
= s
' orelse String.isPrefix (s
' ^
"/") path
) (your_paths ())
84 fun yourDomainHost s
=
87 val (pref
, suf
) = Substring
.splitl (fn ch
=> ch
<> #
".") (Substring
.full s
)
89 Substring
.size suf
> 0
90 andalso validHost (Substring
.string pref
)
91 andalso yourDomain (Substring
.string
92 (Substring
.slice (suf
, 1, NONE
)))
95 fun validUser s
= size s
> 0 andalso size s
< 20
96 andalso CharVector
.all
Char.isAlphaNum s
98 val validGroup
= validUser
100 val _
= Env
.type_one
"no_spaces"
102 (CharVector
.all (fn ch
=> not (Char.isSpace ch
)))
103 val _
= Env
.type_one
"no_newlines"
105 (CharVector
.all (fn ch
=> ch
<> #
"\n" andalso ch
<> #
"\r"))
107 val _
= Env
.type_one
"ip"
111 val _
= Env
.type_one
"host"
115 val _
= Env
.type_one
"domain"
119 val _
= Env
.type_one
"your_domain"
123 val _
= Env
.type_one
"your_domain_host"
127 val _
= Env
.type_one
"user"
131 val _
= Env
.type_one
"group"
135 val _
= Env
.type_one
"your_user"
139 val _
= Env
.type_one
"your_group"
143 val _
= Env
.type_one
"your_path"
147 val _
= Env
.type_one
"node"
153 val dl
= ErrorMsg
.dummyLoc
155 val nsD
= (EString Config
.defaultNs
, dl
)
156 val serialD
= (EVar
"serialAuto", dl
)
157 val refD
= (EInt Config
.defaultRefresh
, dl
)
158 val retD
= (EInt Config
.defaultRetry
, dl
)
159 val expD
= (EInt Config
.defaultExpiry
, dl
)
160 val minD
= (EInt Config
.defaultMinimum
, dl
)
162 val soaD
= multiApp ((EVar
"soa", dl
),
164 [nsD
, serialD
, refD
, retD
, expD
, minD
])
166 val masterD
= (EApp ((EVar
"internalMaster", dl
),
167 (EString Config
.defaultNode
, dl
)),
170 val _
= Defaults
.registerDefault ("DNS",
171 (TBase
"dnsKind", dl
),
172 (fn () => multiApp ((EVar
"useDns", dl
),
174 [soaD
, masterD
, (EList
[], dl
)])))
176 val _
= Defaults
.registerDefault ("TTL",
178 (fn () => (EInt Config
.Bind
.defaultTTL
, dl
)))
180 type soa
= {ns
: string,
187 val serial
= fn (EVar
"serialAuto", _
) => SOME NONE
188 |
(EApp ((EVar
"serialConst", _
), n
), _
) => Option
.map
SOME (Env
.int n
)
191 val soa
= fn (EApp ((EApp ((EApp ((EApp ((EApp ((EApp
192 ((EVar
"soa", _
), ns
), _
),
198 (case (Env
.string ns
, serial sl
, Env
.int rf
,
199 Env
.int ret
, Env
.int exp
, Env
.int min
) of
200 (SOME ns
, SOME sl
, SOME rf
,
201 SOME ret
, SOME exp
, SOME min
) =>
212 ExternalMaster
of string
213 | InternalMaster
of string
215 val master
= fn (EApp ((EVar
"externalMaster", _
), e
), _
) => Option
.map
ExternalMaster (Env
.string e
)
216 |
(EApp ((EVar
"internalMaster", _
), e
), _
) => Option
.map
InternalMaster (Env
.string e
)
220 UseDns
of {soa
: soa
,
222 slaves
: string list
}
225 val dnsKind
= fn (EApp ((EApp ((EApp
226 ((EVar
"useDns", _
), sa
), _
),
229 (case (soa sa
, master mstr
, Env
.list Env
.string slaves
) of
230 (SOME sa
, SOME mstr
, SOME slaves
) =>
231 SOME (UseDns
{soa
= sa
,
235 |
(EVar
"noDns", _
) => SOME NoDns
238 val befores
= ref (fn (_
: string) => ())
239 val afters
= ref (fn (_
: string) => ())
241 fun registerBefore f
=
245 befores
:= (fn x
=> (old x
; f x
))
248 fun registerAfter f
=
252 afters
:= (fn x
=> (old x
; f x
))
256 val currentPath
= ref (fn (_
: string) => "")
260 fun currentDomain () = !current
262 fun domainFile
{node
, name
} = ((*print ("Opening " ^
!currentPath node ^ name ^
"\n");*)
263 TextIO.openOut (!currentPath node ^ name
))
267 val toks
= String.fields (fn ch
=> ch
= #
".") domain
269 val elems
= foldr (fn (piece
, elems
) =>
271 val elems
= piece
:: elems
275 val path
= String.concatWith
"/"
276 (Config
.resultRoot
:: node
:: rev elems
)
277 val tmpPath
= String.concatWith
"/"
278 (Config
.tmpDir
:: node
:: rev elems
)
280 (if Posix
.FileSys
.ST
.isDir
281 (Posix
.FileSys
.stat path
) then
284 (OS
.FileSys
.remove path
;
285 OS
.FileSys
.mkDir path
))
286 handle OS
.SysErr _
=> OS
.FileSys
.mkDir path
;
288 (if Posix
.FileSys
.ST
.isDir
289 (Posix
.FileSys
.stat tmpPath
) then
292 (OS
.FileSys
.remove tmpPath
;
293 OS
.FileSys
.mkDir tmpPath
))
294 handle OS
.SysErr _
=> OS
.FileSys
.mkDir tmpPath
301 fn (root
, site
) => String.concatWith
"/" (root
:: site
:: rev ("" :: elems
))
304 datatype file_action
' =
305 Add
' of {src
: string, dst
: string}
307 | Modify
' of {src
: string, dst
: string}
309 fun findDiffs (site
, dom
, acts
) =
312 val realPath
= gp (Config
.resultRoot
, site
)
313 val tmpPath
= gp (Config
.tmpDir
, site
)
315 (*val _
= print ("getDiffs(" ^ site ^
", " ^ dom ^
")... " ^ realPath ^
"; " ^ tmpPath ^
"\n")*)
317 val dir
= Posix
.FileSys
.opendir realPath
320 case Posix
.FileSys
.readdir dir
of
321 NONE
=> (Posix
.FileSys
.closedir dir
;
325 val real = OS
.Path
.joinDirFile
{dir
= realPath
,
327 val tmp
= OS
.Path
.joinDirFile
{dir
= tmpPath
,
330 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat
real) then
332 else if Posix
.FileSys
.access (tmp
, []) then
333 if Slave
.shell
[Config
.diff
, " ", real, " ", tmp
] then
336 loopReal ((site
, dom
, realPath
, Modify
' {src
= tmp
, dst
= real}) :: acts
)
338 loopReal ((site
, dom
, realPath
, Delete
' real) :: acts
)
341 val acts
= loopReal acts
343 val dir
= Posix
.FileSys
.opendir tmpPath
346 case Posix
.FileSys
.readdir dir
of
347 NONE
=> (Posix
.FileSys
.closedir dir
;
351 val real = OS
.Path
.joinDirFile
{dir
= realPath
,
353 val tmp
= OS
.Path
.joinDirFile
{dir
= tmpPath
,
356 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat tmp
) then
358 else if Posix
.FileSys
.access (real, []) then
361 loopTmp ((site
, dom
, realPath
, Add
' {src
= tmp
, dst
= real}) :: acts
)
364 val acts
= loopTmp acts
369 fun findAllDiffs () =
371 val dir
= Posix
.FileSys
.opendir Config
.tmpDir
372 val len
= length (String.fields (fn ch
=> ch
= #
"/") Config
.tmpDir
) + 1
374 fun exploreSites diffs
=
375 case Posix
.FileSys
.readdir dir
of
379 fun explore (dname
, diffs
) =
381 val dir
= Posix
.FileSys
.opendir dname
384 case Posix
.FileSys
.readdir dir
of
388 val fname
= OS
.Path
.joinDirFile
{dir
= dname
,
391 loop (if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat fname
) then
393 val dom
= String.fields (fn ch
=> ch
= #
"/") fname
394 val dom
= List.drop (dom
, len
)
395 val dom
= String.concatWith
"." (rev dom
)
397 val dname
' = OS
.Path
.joinDirFile
{dir
= dname
,
401 findDiffs (site
, dom
, diffs
))
408 before Posix
.FileSys
.closedir dir
411 explore (OS
.Path
.joinDirFile
{dir
= Config
.tmpDir
,
416 before Posix
.FileSys
.closedir dir
419 val masterNode
: string option ref
= ref NONE
420 fun dnsMaster () = !masterNode
422 val _
= Env
.containerV_one
"domain"
423 ("domain", Env
.string)
426 val kind
= Env
.env
dnsKind (evs
, "DNS")
427 val ttl
= Env
.env Env
.int (evs
, "TTL")
429 val path
= getPath dom
431 val () = (current
:= dom
;
432 currentPath
:= (fn site
=> path (Config
.tmpDir
, site
)))
434 fun saveSoa (kind
, soa
: soa
) node
=
436 val outf
= domainFile
{node
= node
, name
= "soa"}
438 TextIO.output (outf
, kind
);
439 TextIO.output (outf
, "\n");
440 TextIO.output (outf
, Int.toString ttl
);
441 TextIO.output (outf
, "\n");
442 TextIO.output (outf
, #ns soa
);
443 TextIO.output (outf
, "\n");
446 | SOME n
=> TextIO.output (outf
, Int.toString n
);
447 TextIO.output (outf
, "\n");
448 TextIO.output (outf
, Int.toString (#ref soa
));
449 TextIO.output (outf
, "\n");
450 TextIO.output (outf
, Int.toString (#ret soa
));
451 TextIO.output (outf
, "\n");
452 TextIO.output (outf
, Int.toString (#exp soa
));
453 TextIO.output (outf
, "\n");
454 TextIO.output (outf
, Int.toString (#min soa
));
455 TextIO.output (outf
, "\n");
459 fun saveNamed (kind
, soa
: soa
, masterIp
) node
=
461 val outf
= domainFile
{node
= node
, name
= "named.conf"}
463 TextIO.output (outf
, "\nzone \"");
464 TextIO.output (outf
, dom
);
465 TextIO.output (outf
, "\" IN {\n\ttype ");
466 TextIO.output (outf
, kind
);
467 TextIO.output (outf
, ";\n\tfile \"");
468 TextIO.output (outf
, Config
.Bind
.zonePath
);
469 TextIO.output (outf
, "/");
470 TextIO.output (outf
, dom
);
471 TextIO.output (outf
, ".zone\";\n");
473 "master" => TextIO.output (outf
, "\tallow-update { none; };\n")
474 | _
=> (TextIO.output (outf
, "\tmasters { ");
475 TextIO.output (outf
, masterIp
);
476 TextIO.output (outf
, " };\n"));
477 TextIO.output (outf
, "};\n");
482 NoDns
=> masterNode
:= NONE
487 InternalMaster node
=> valOf (SM
.find (nodeMap
, node
))
488 | ExternalMaster ip
=> ip
490 app (saveSoa ("slave", #soa dns
)) (#slaves dns
);
491 app (saveNamed ("slave", #soa dns
, masterIp
)) (#slaves dns
);
493 InternalMaster node
=>
494 (masterNode
:= SOME node
;
495 saveSoa ("master", #soa dns
) node
;
496 saveNamed ("master", #soa dns
, masterIp
) node
)
497 | _
=> masterNode
:= NONE
;
501 fn () => !afters (!current
))
503 val () = Env
.registerPre (fn () => (ignore (Slave
.shellF ([Config
.rm
, " -rf ", Config
.tmpDir
, ""],
504 fn cl
=> "Temp file cleanup failed: " ^ cl
));
505 OS
.FileSys
.mkDir Config
.tmpDir
;
506 app (fn node
=> OS
.FileSys
.mkDir
507 (OS
.Path
.joinDirFile
{dir
= Config
.tmpDir
,
510 app (fn node
=> OS
.FileSys
.mkDir
511 (OS
.Path
.joinDirFile
{dir
= Config
.resultRoot
,
513 handle OS
.SysErr _
=> ())
516 val () = Env
.registerPost (fn () =>
518 val diffs
= findAllDiffs ()
520 val diffs
= map (fn (site
, dom
, dir
, Add
' {src
, dst
}) =>
521 (Slave
.shellF ([Config
.cp
, " ", src
, " ", dst
],
522 fn cl
=> "Copy failed: " ^ cl
);
528 |
(site
, dom
, dir
, Delete
' dst
) =>
529 (OS
.FileSys
.remove dst
530 handle OS
.SysErr _
=>
531 ErrorMsg
.error
NONE ("Delete failed for " ^ dst
);
533 {action
= Slave
.Delete
,
537 |
(site
, dom
, dir
, Modify
' {src
, dst
}) =>
538 (Slave
.shellF ([Config
.cp
, " ", src
, " ", dst
],
539 fn cl
=> "Copy failed: " ^ cl
);
541 {action
= Slave
.Modify
,
546 if !ErrorMsg
.anyErrors
then
549 Slave
.handleChanges (map #
2 diffs
);
550 ignore (Slave
.shellF ([Config
.rm
, " -rf ", Config
.tmpDir
, ""],
551 fn cl
=> "Temp file cleanup failed: " ^ cl
))