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
25 structure SM
= DataStructures
.StringMap
26 structure SS
= DataStructures
.StringSet
28 val ssl_context
= ref (NONE
: OpenSSL
.context option
)
29 fun set_context ctx
= ssl_context
:= SOME ctx
31 val nodes
= map #
1 Config
.nodeIps
32 val nodeMap
= foldl (fn ((node
, ip
), mp
) => SM
.insert (mp
, node
, ip
))
33 SM
.empty Config
.nodeIps
34 fun nodeIp node
= valOf (SM
.find (nodeMap
, node
))
39 val your_doms
= ref SS
.empty
40 fun your_domains () = !your_doms
42 val your_usrs
= ref SS
.empty
43 fun your_users () = !your_usrs
45 val your_grps
= ref SS
.empty
46 fun your_groups () = !your_grps
48 val your_pths
= ref SS
.empty
49 fun your_paths () = !your_pths
52 (usr
:= Config
.testUser
;
53 your_doms
:= Acl
.class
{user
= getUser (),
55 your_usrs
:= Acl
.class
{user
= getUser (),
57 your_grps
:= Acl
.class
{user
= getUser (),
59 your_pths
:= Acl
.class
{user
= getUser (),
63 case map
Int.fromString (String.fields (fn ch
=> ch
= #
".") s
) of
64 [SOME n1
, SOME n2
, SOME n3
, SOME n4
] =>
65 n1
>= 0 andalso n1
< 256 andalso n2
>= 0 andalso n2
< 256 andalso n3
>= 0 andalso n3
< 256 andalso n4
>= 0 andalso n4
< 256
68 fun isIdent ch
= Char.isLower ch
orelse Char.isDigit ch
71 size s
> 0 andalso size s
< 20
72 andalso CharVector
.all (fn ch
=> isIdent ch
orelse ch
= #
"-") s
75 size s
> 0 andalso size s
< 100
76 andalso List.all
validHost (String.fields (fn ch
=> ch
= #
".") s
)
78 fun validNode s
= List.exists (fn s
' => s
= s
') nodes
80 fun yourDomain s
= SS
.member (your_domains (), s
)
81 fun yourUser s
= SS
.member (your_users (), s
)
82 fun yourGroup s
= SS
.member (your_groups (), s
)
84 List.all (fn s
=> s
<> "..") (String.fields (fn ch
=> ch
= #
"/") path
)
85 andalso CharVector
.all (fn ch
=> Char.isAlphaNum ch
orelse ch
= #
"." orelse ch
= #
"/"
86 orelse ch
= #
"-" orelse ch
= #
"_") path
87 andalso SS
.exists (fn s
' => path
= s
' orelse String.isPrefix (s
' ^
"/") path
) (your_paths ())
89 fun yourDomainHost s
=
92 val (pref
, suf
) = Substring
.splitl (fn ch
=> ch
<> #
".") (Substring
.full s
)
94 Substring
.size suf
> 0
95 andalso validHost (Substring
.string pref
)
96 andalso yourDomain (Substring
.string
97 (Substring
.slice (suf
, 1, NONE
)))
100 fun validUser s
= size s
> 0 andalso size s
< 20
101 andalso CharVector
.all
Char.isAlphaNum s
103 val validGroup
= validUser
105 val _
= Env
.type_one
"no_spaces"
107 (CharVector
.all (fn ch
=> not (Char.isSpace ch
)))
108 val _
= Env
.type_one
"no_newlines"
110 (CharVector
.all (fn ch
=> ch
<> #
"\n" andalso ch
<> #
"\r"))
112 val _
= Env
.type_one
"ip"
116 val _
= Env
.type_one
"host"
120 val _
= Env
.type_one
"domain"
124 val _
= Env
.type_one
"your_domain"
128 val _
= Env
.type_one
"your_domain_host"
132 val _
= Env
.type_one
"user"
136 val _
= Env
.type_one
"group"
140 val _
= Env
.type_one
"your_user"
144 val _
= Env
.type_one
"your_group"
148 val _
= Env
.type_one
"your_path"
152 val _
= Env
.type_one
"node"
158 val dl
= ErrorMsg
.dummyLoc
160 val nsD
= (EString Config
.defaultNs
, dl
)
161 val serialD
= (EVar
"serialAuto", dl
)
162 val refD
= (EInt Config
.defaultRefresh
, dl
)
163 val retD
= (EInt Config
.defaultRetry
, dl
)
164 val expD
= (EInt Config
.defaultExpiry
, dl
)
165 val minD
= (EInt Config
.defaultMinimum
, dl
)
167 val soaD
= multiApp ((EVar
"soa", dl
),
169 [nsD
, serialD
, refD
, retD
, expD
, minD
])
171 val masterD
= (EApp ((EVar
"internalMaster", dl
),
172 (EString Config
.defaultNode
, dl
)),
175 val _
= Defaults
.registerDefault ("DNS",
176 (TBase
"dnsKind", dl
),
177 (fn () => multiApp ((EVar
"useDns", dl
),
179 [soaD
, masterD
, (EList
[], dl
)])))
181 val _
= Defaults
.registerDefault ("TTL",
183 (fn () => (EInt Config
.Bind
.defaultTTL
, dl
)))
185 type soa
= {ns
: string,
192 val serial
= fn (EVar
"serialAuto", _
) => SOME NONE
193 |
(EApp ((EVar
"serialConst", _
), n
), _
) => Option
.map
SOME (Env
.int n
)
196 val soa
= fn (EApp ((EApp ((EApp ((EApp ((EApp ((EApp
197 ((EVar
"soa", _
), ns
), _
),
203 (case (Env
.string ns
, serial sl
, Env
.int rf
,
204 Env
.int ret
, Env
.int exp
, Env
.int min
) of
205 (SOME ns
, SOME sl
, SOME rf
,
206 SOME ret
, SOME exp
, SOME min
) =>
217 ExternalMaster
of string
218 | InternalMaster
of string
220 val master
= fn (EApp ((EVar
"externalMaster", _
), e
), _
) => Option
.map
ExternalMaster (Env
.string e
)
221 |
(EApp ((EVar
"internalMaster", _
), e
), _
) => Option
.map
InternalMaster (Env
.string e
)
225 UseDns
of {soa
: soa
,
227 slaves
: string list
}
230 val dnsKind
= fn (EApp ((EApp ((EApp
231 ((EVar
"useDns", _
), sa
), _
),
234 (case (soa sa
, master mstr
, Env
.list Env
.string slaves
) of
235 (SOME sa
, SOME mstr
, SOME slaves
) =>
236 SOME (UseDns
{soa
= sa
,
240 |
(EVar
"noDns", _
) => SOME NoDns
243 val befores
= ref (fn (_
: string) => ())
244 val afters
= ref (fn (_
: string) => ())
246 fun registerBefore f
=
250 befores
:= (fn x
=> (old x
; f x
))
253 fun registerAfter f
=
257 afters
:= (fn x
=> (old x
; f x
))
261 val currentPath
= ref (fn (_
: string) => "")
265 fun currentDomain () = !current
267 fun domainFile
{node
, name
} = ((*print ("Opening " ^
!currentPath node ^ name ^
"\n");*)
268 TextIO.openOut (!currentPath node ^ name
))
272 val toks
= String.fields (fn ch
=> ch
= #
".") domain
274 val elems
= foldr (fn (piece
, elems
) =>
276 val elems
= piece
:: elems
280 val path
= String.concatWith
"/"
281 (Config
.resultRoot
:: node
:: rev elems
)
282 val tmpPath
= String.concatWith
"/"
283 (Config
.tmpDir
:: node
:: rev elems
)
285 (if Posix
.FileSys
.ST
.isDir
286 (Posix
.FileSys
.stat path
) then
289 (OS
.FileSys
.remove path
;
290 OS
.FileSys
.mkDir path
))
291 handle OS
.SysErr _
=> OS
.FileSys
.mkDir path
;
293 (if Posix
.FileSys
.ST
.isDir
294 (Posix
.FileSys
.stat tmpPath
) then
297 (OS
.FileSys
.remove tmpPath
;
298 OS
.FileSys
.mkDir tmpPath
))
299 handle OS
.SysErr _
=> OS
.FileSys
.mkDir tmpPath
306 fn (root
, site
) => String.concatWith
"/" (root
:: site
:: rev ("" :: elems
))
309 datatype file_action
' =
310 Add
' of {src
: string, dst
: string}
312 | Modify
' of {src
: string, dst
: string}
314 fun findDiffs (site
, dom
, acts
) =
317 val realPath
= gp (Config
.resultRoot
, site
)
318 val tmpPath
= gp (Config
.tmpDir
, site
)
320 (*val _
= print ("getDiffs(" ^ site ^
", " ^ dom ^
")... " ^ realPath ^
"; " ^ tmpPath ^
"\n")*)
322 val dir
= Posix
.FileSys
.opendir realPath
325 case Posix
.FileSys
.readdir dir
of
326 NONE
=> (Posix
.FileSys
.closedir dir
;
330 val real = OS
.Path
.joinDirFile
{dir
= realPath
,
332 val tmp
= OS
.Path
.joinDirFile
{dir
= tmpPath
,
335 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat
real) then
337 else if Posix
.FileSys
.access (tmp
, []) then
338 if Slave
.shell
[Config
.diff
, " ", real, " ", tmp
] then
341 loopReal ((site
, dom
, realPath
, Modify
' {src
= tmp
, dst
= real}) :: acts
)
343 loopReal ((site
, dom
, realPath
, Delete
' real) :: acts
)
346 val acts
= loopReal acts
348 val dir
= Posix
.FileSys
.opendir tmpPath
351 case Posix
.FileSys
.readdir dir
of
352 NONE
=> (Posix
.FileSys
.closedir dir
;
356 val real = OS
.Path
.joinDirFile
{dir
= realPath
,
358 val tmp
= OS
.Path
.joinDirFile
{dir
= tmpPath
,
361 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat tmp
) then
363 else if Posix
.FileSys
.access (real, []) then
366 loopTmp ((site
, dom
, realPath
, Add
' {src
= tmp
, dst
= real}) :: acts
)
369 val acts
= loopTmp acts
374 fun findAllDiffs () =
376 val dir
= Posix
.FileSys
.opendir Config
.tmpDir
377 val len
= length (String.fields (fn ch
=> ch
= #
"/") Config
.tmpDir
) + 1
379 fun exploreSites diffs
=
380 case Posix
.FileSys
.readdir dir
of
384 fun explore (dname
, diffs
) =
386 val dir
= Posix
.FileSys
.opendir dname
389 case Posix
.FileSys
.readdir dir
of
393 val fname
= OS
.Path
.joinDirFile
{dir
= dname
,
396 loop (if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat fname
) then
398 val dom
= String.fields (fn ch
=> ch
= #
"/") fname
399 val dom
= List.drop (dom
, len
)
400 val dom
= String.concatWith
"." (rev dom
)
402 val dname
' = OS
.Path
.joinDirFile
{dir
= dname
,
406 findDiffs (site
, dom
, diffs
))
413 before Posix
.FileSys
.closedir dir
416 exploreSites (explore (OS
.Path
.joinDirFile
{dir
= Config
.tmpDir
,
417 file
= site
}, diffs
))
421 before Posix
.FileSys
.closedir dir
424 val masterNode
: string option ref
= ref NONE
425 fun dnsMaster () = !masterNode
427 val _
= Env
.containerV_one
"domain"
428 ("domain", Env
.string)
431 val kind
= Env
.env
dnsKind (evs
, "DNS")
432 val ttl
= Env
.env Env
.int (evs
, "TTL")
434 val path
= getPath dom
436 val () = (current
:= dom
;
437 currentPath
:= (fn site
=> path (Config
.tmpDir
, site
)))
439 fun saveSoa (kind
, soa
: soa
) node
=
441 val outf
= domainFile
{node
= node
, name
= "soa"}
443 TextIO.output (outf
, kind
);
444 TextIO.output (outf
, "\n");
445 TextIO.output (outf
, Int.toString ttl
);
446 TextIO.output (outf
, "\n");
447 TextIO.output (outf
, #ns soa
);
448 TextIO.output (outf
, "\n");
451 | SOME n
=> TextIO.output (outf
, Int.toString n
);
452 TextIO.output (outf
, "\n");
453 TextIO.output (outf
, Int.toString (#ref soa
));
454 TextIO.output (outf
, "\n");
455 TextIO.output (outf
, Int.toString (#ret soa
));
456 TextIO.output (outf
, "\n");
457 TextIO.output (outf
, Int.toString (#exp soa
));
458 TextIO.output (outf
, "\n");
459 TextIO.output (outf
, Int.toString (#min soa
));
460 TextIO.output (outf
, "\n");
464 fun saveNamed (kind
, soa
: soa
, masterIp
) node
=
466 val outf
= domainFile
{node
= node
, name
= "named.conf"}
468 TextIO.output (outf
, "\nzone \"");
469 TextIO.output (outf
, dom
);
470 TextIO.output (outf
, "\" IN {\n\ttype ");
471 TextIO.output (outf
, kind
);
472 TextIO.output (outf
, ";\n\tfile \"");
473 TextIO.output (outf
, Config
.Bind
.zonePath
);
474 TextIO.output (outf
, "/");
475 TextIO.output (outf
, dom
);
476 TextIO.output (outf
, ".zone\";\n");
478 "master" => TextIO.output (outf
, "\tallow-update { none; };\n")
479 | _
=> (TextIO.output (outf
, "\tmasters { ");
480 TextIO.output (outf
, masterIp
);
481 TextIO.output (outf
, " };\n"));
482 TextIO.output (outf
, "};\n");
487 NoDns
=> masterNode
:= NONE
492 InternalMaster node
=> valOf (SM
.find (nodeMap
, node
))
493 | ExternalMaster ip
=> ip
495 app (saveSoa ("slave", #soa dns
)) (#slaves dns
);
496 app (saveNamed ("slave", #soa dns
, masterIp
)) (#slaves dns
);
498 InternalMaster node
=>
499 (masterNode
:= SOME node
;
500 saveSoa ("master", #soa dns
) node
;
501 saveNamed ("master", #soa dns
, masterIp
) node
)
502 | _
=> masterNode
:= NONE
;
506 fn () => !afters (!current
))
508 val () = Env
.registerPre (fn () => (ignore (Slave
.shellF ([Config
.rm
, " -rf ", Config
.tmpDir
, ""],
509 fn cl
=> "Temp file cleanup failed: " ^ cl
));
510 OS
.FileSys
.mkDir Config
.tmpDir
;
511 app (fn node
=> OS
.FileSys
.mkDir
512 (OS
.Path
.joinDirFile
{dir
= Config
.tmpDir
,
515 app (fn node
=> OS
.FileSys
.mkDir
516 (OS
.Path
.joinDirFile
{dir
= Config
.resultRoot
,
518 handle OS
.SysErr _
=> ())
521 val () = Env
.registerPost (fn () =>
523 val diffs
= findAllDiffs ()
525 val diffs
= map (fn (site
, dom
, dir
, Add
' {src
, dst
}) =>
526 (Slave
.shellF ([Config
.cp
, " ", src
, " ", dst
],
527 fn cl
=> "Copy failed: " ^ cl
);
533 |
(site
, dom
, dir
, Delete
' dst
) =>
534 (OS
.FileSys
.remove dst
535 handle OS
.SysErr _
=>
536 ErrorMsg
.error
NONE ("Delete failed for " ^ dst
);
538 {action
= Slave
.Delete
,
542 |
(site
, dom
, dir
, Modify
' {src
, dst
}) =>
543 (Slave
.shellF ([Config
.cp
, " ", src
, " ", dst
],
544 fn cl
=> "Copy failed: " ^ cl
);
546 {action
= Slave
.Modify
,
551 if !ErrorMsg
.anyErrors
then
554 val changed
= foldl (fn ((site
, file
), changed
) =>
556 val ls
= case SM
.find (changed
, site
) of
560 SM
.insert (changed
, site
, file
:: ls
)
563 fun handleSite (site
, files
) =
567 print ("New configuration for node " ^ site ^
"\n");
568 if site
= Config
.defaultNode
then
569 Slave
.handleChanges files
571 val bio
= OpenSSL
.connect (valOf (!ssl_context
),
574 ^
Int.toString Config
.slavePort
)
576 app (fn file
=> Msg
.send (bio
, MsgFile file
)) files
;
577 Msg
.send (bio
, MsgDoFiles
);
579 NONE
=> print
"Slave closed connection unexpectedly\n"
582 MsgOk
=> print ("Slave " ^ site ^
" finished\n")
583 | MsgError s
=> print ("Slave " ^ site
584 ^
" returned error: " ^
586 | _
=> print ("Slave " ^ site
587 ^
" returned unexpected command\n");
592 SM
.appi handleSite changed
594 ignore (Slave
.shellF ([Config
.rm
, " -rf ", Config
.tmpDir
, ""],
595 fn cl
=> "Temp file cleanup failed: " ^ cl
))