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
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
.masterNode
, dl
)),
175 val slavesD
= (EList (map (fn s
=> (EString s
, dl
)) Config
.slaveNodes
), dl
)
177 val _
= Defaults
.registerDefault ("Mailbox",
179 (fn () => (EString (getUser ()), dl
)))
181 val _
= Defaults
.registerDefault ("DNS",
182 (TBase
"dnsKind", dl
),
183 (fn () => multiApp ((EVar
"useDns", dl
),
185 [soaD
, masterD
, slavesD
])))
187 val _
= Defaults
.registerDefault ("TTL",
189 (fn () => (EInt Config
.Bind
.defaultTTL
, dl
)))
191 type soa
= {ns
: string,
198 val serial
= fn (EVar
"serialAuto", _
) => SOME NONE
199 |
(EApp ((EVar
"serialConst", _
), n
), _
) => Option
.map
SOME (Env
.int n
)
202 val soa
= fn (EApp ((EApp ((EApp ((EApp ((EApp ((EApp
203 ((EVar
"soa", _
), ns
), _
),
209 (case (Env
.string ns
, serial sl
, Env
.int rf
,
210 Env
.int ret
, Env
.int exp
, Env
.int min
) of
211 (SOME ns
, SOME sl
, SOME rf
,
212 SOME ret
, SOME exp
, SOME min
) =>
223 ExternalMaster
of string
224 | InternalMaster
of string
228 val _
= Env
.registerFunction ("ip_of_node",
229 fn [(EString node
, _
)] => SOME (EString (nodeIp node
), dl
)
232 val master
= fn (EApp ((EVar
"externalMaster", _
), e
), _
) => Option
.map
ExternalMaster (ip e
)
233 |
(EApp ((EVar
"internalMaster", _
), e
), _
) => Option
.map
InternalMaster (Env
.string e
)
237 UseDns
of {soa
: soa
,
239 slaves
: string list
}
242 val dnsKind
= fn (EApp ((EApp ((EApp
243 ((EVar
"useDns", _
), sa
), _
),
246 (case (soa sa
, master mstr
, Env
.list Env
.string slaves
) of
247 (SOME sa
, SOME mstr
, SOME slaves
) =>
248 SOME (UseDns
{soa
= sa
,
252 |
(EVar
"noDns", _
) => SOME NoDns
255 val befores
= ref (fn (_
: string) => ())
256 val afters
= ref (fn (_
: string) => ())
258 fun registerBefore f
=
262 befores
:= (fn x
=> (old x
; f x
))
265 fun registerAfter f
=
269 afters
:= (fn x
=> (old x
; f x
))
273 val currentPath
= ref (fn (_
: string) => "")
277 fun currentDomain () = !current
279 fun domainFile
{node
, name
} = ((*print ("Opening " ^
!currentPath node ^ name ^
"\n");*)
280 TextIO.openOut (!currentPath node ^ name
))
284 val toks
= String.fields (fn ch
=> ch
= #
".") domain
286 val elems
= foldr (fn (piece
, elems
) =>
288 val elems
= piece
:: elems
292 val path
= String.concatWith
"/"
293 (Config
.resultRoot
:: node
:: rev elems
)
294 val tmpPath
= String.concatWith
"/"
295 (Config
.tmpDir
:: node
:: rev elems
)
297 (if Posix
.FileSys
.ST
.isDir
298 (Posix
.FileSys
.stat path
) then
301 (OS
.FileSys
.remove path
;
302 OS
.FileSys
.mkDir path
))
303 handle OS
.SysErr _
=> OS
.FileSys
.mkDir path
;
305 (if Posix
.FileSys
.ST
.isDir
306 (Posix
.FileSys
.stat tmpPath
) then
309 (OS
.FileSys
.remove tmpPath
;
310 OS
.FileSys
.mkDir tmpPath
))
311 handle OS
.SysErr _
=> OS
.FileSys
.mkDir tmpPath
318 fn (root
, site
) => String.concatWith
"/" (root
:: site
:: rev ("" :: elems
))
321 datatype file_action
' =
322 Add
' of {src
: string, dst
: string}
324 | Modify
' of {src
: string, dst
: string}
326 fun findDiffs (site
, dom
, acts
) =
329 val realPath
= gp (Config
.resultRoot
, site
)
330 val tmpPath
= gp (Config
.tmpDir
, site
)
332 (*val _
= print ("getDiffs(" ^ site ^
", " ^ dom ^
")... " ^ realPath ^
"; " ^ tmpPath ^
"\n")*)
334 val dir
= Posix
.FileSys
.opendir realPath
337 case Posix
.FileSys
.readdir dir
of
338 NONE
=> (Posix
.FileSys
.closedir dir
;
342 val real = OS
.Path
.joinDirFile
{dir
= realPath
,
344 val tmp
= OS
.Path
.joinDirFile
{dir
= tmpPath
,
347 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat
real) then
349 else if Posix
.FileSys
.access (tmp
, []) then
350 if Slave
.shell
[Config
.diff
, " ", real, " ", tmp
] then
353 loopReal ((site
, dom
, realPath
, Modify
' {src
= tmp
, dst
= real}) :: acts
)
355 loopReal ((site
, dom
, realPath
, Delete
' real) :: acts
)
358 val acts
= loopReal acts
360 val dir
= Posix
.FileSys
.opendir tmpPath
363 case Posix
.FileSys
.readdir dir
of
364 NONE
=> (Posix
.FileSys
.closedir dir
;
368 val real = OS
.Path
.joinDirFile
{dir
= realPath
,
370 val tmp
= OS
.Path
.joinDirFile
{dir
= tmpPath
,
373 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat tmp
) then
375 else if Posix
.FileSys
.access (real, []) then
378 loopTmp ((site
, dom
, realPath
, Add
' {src
= tmp
, dst
= real}) :: acts
)
381 val acts
= loopTmp acts
386 fun findAllDiffs () =
388 val dir
= Posix
.FileSys
.opendir Config
.tmpDir
389 val len
= length (String.fields (fn ch
=> ch
= #
"/") Config
.tmpDir
) + 1
391 fun exploreSites diffs
=
392 case Posix
.FileSys
.readdir dir
of
396 fun explore (dname
, diffs
) =
398 val dir
= Posix
.FileSys
.opendir dname
401 case Posix
.FileSys
.readdir dir
of
405 val fname
= OS
.Path
.joinDirFile
{dir
= dname
,
408 loop (if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat fname
) then
410 val dom
= String.fields (fn ch
=> ch
= #
"/") fname
411 val dom
= List.drop (dom
, len
)
412 val dom
= String.concatWith
"." (rev dom
)
414 val dname
' = OS
.Path
.joinDirFile
{dir
= dname
,
418 findDiffs (site
, dom
, diffs
))
425 before Posix
.FileSys
.closedir dir
428 exploreSites (explore (OS
.Path
.joinDirFile
{dir
= Config
.tmpDir
,
429 file
= site
}, diffs
))
433 before Posix
.FileSys
.closedir dir
436 val masterNode
: string option ref
= ref NONE
437 fun dnsMaster () = !masterNode
439 val _
= Env
.containerV_one
"domain"
440 ("domain", Env
.string)
443 val kind
= Env
.env
dnsKind (evs
, "DNS")
444 val ttl
= Env
.env Env
.int (evs
, "TTL")
446 val path
= getPath dom
448 val () = (current
:= dom
;
449 currentPath
:= (fn site
=> path (Config
.tmpDir
, site
)))
451 fun saveSoa (kind
, soa
: soa
) node
=
453 val outf
= domainFile
{node
= node
, name
= "soa"}
455 TextIO.output (outf
, kind
);
456 TextIO.output (outf
, "\n");
457 TextIO.output (outf
, Int.toString ttl
);
458 TextIO.output (outf
, "\n");
459 TextIO.output (outf
, #ns soa
);
460 TextIO.output (outf
, "\n");
463 | SOME n
=> TextIO.output (outf
, Int.toString n
);
464 TextIO.output (outf
, "\n");
465 TextIO.output (outf
, Int.toString (#ref soa
));
466 TextIO.output (outf
, "\n");
467 TextIO.output (outf
, Int.toString (#ret soa
));
468 TextIO.output (outf
, "\n");
469 TextIO.output (outf
, Int.toString (#exp soa
));
470 TextIO.output (outf
, "\n");
471 TextIO.output (outf
, Int.toString (#min soa
));
472 TextIO.output (outf
, "\n");
476 fun saveNamed (kind
, soa
: soa
, masterIp
) node
=
478 val outf
= domainFile
{node
= node
, name
= "named.conf"}
480 TextIO.output (outf
, "\nzone \"");
481 TextIO.output (outf
, dom
);
482 TextIO.output (outf
, "\" IN {\n\ttype ");
483 TextIO.output (outf
, kind
);
484 TextIO.output (outf
, ";\n\tfile \"");
485 TextIO.output (outf
, Config
.Bind
.zonePath_real
);
486 TextIO.output (outf
, "/");
487 TextIO.output (outf
, dom
);
488 TextIO.output (outf
, ".zone\";\n");
490 "master" => TextIO.output (outf
, "\tallow-update { none; };\n")
491 | _
=> (TextIO.output (outf
, "\tmasters { ");
492 TextIO.output (outf
, masterIp
);
493 TextIO.output (outf
, "; };\n"));
494 TextIO.output (outf
, "};\n");
499 NoDns
=> masterNode
:= NONE
504 InternalMaster node
=> valOf (SM
.find (nodeMap
, node
))
505 | ExternalMaster ip
=> ip
507 app (saveSoa ("slave", #soa dns
)) (#slaves dns
);
508 app (saveNamed ("slave", #soa dns
, masterIp
)) (#slaves dns
);
510 InternalMaster node
=>
511 (masterNode
:= SOME node
;
512 saveSoa ("master", #soa dns
) node
;
513 saveNamed ("master", #soa dns
, masterIp
) node
)
514 | _
=> masterNode
:= NONE
;
518 fn () => !afters (!current
))
520 val () = Env
.registerPre (fn () => (ignore (Slave
.shellF ([Config
.rm
, " -rf ", Config
.tmpDir
, ""],
521 fn cl
=> "Temp file cleanup failed: " ^ cl
));
522 OS
.FileSys
.mkDir Config
.tmpDir
;
523 app (fn node
=> OS
.FileSys
.mkDir
524 (OS
.Path
.joinDirFile
{dir
= Config
.tmpDir
,
527 app (fn node
=> OS
.FileSys
.mkDir
528 (OS
.Path
.joinDirFile
{dir
= Config
.resultRoot
,
530 handle OS
.SysErr _
=> ())
533 val () = Env
.registerPost (fn () =>
535 val diffs
= findAllDiffs ()
537 val diffs
= map (fn (site
, dom
, dir
, Add
' {src
, dst
}) =>
538 (Slave
.shellF ([Config
.cp
, " ", src
, " ", dst
],
539 fn cl
=> "Copy failed: " ^ cl
);
545 |
(site
, dom
, dir
, Delete
' dst
) =>
546 (OS
.FileSys
.remove dst
547 handle OS
.SysErr _
=>
548 ErrorMsg
.error
NONE ("Delete failed for " ^ dst
);
550 {action
= Slave
.Delete
,
554 |
(site
, dom
, dir
, Modify
' {src
, dst
}) =>
555 (Slave
.shellF ([Config
.cp
, " ", src
, " ", dst
],
556 fn cl
=> "Copy failed: " ^ cl
);
558 {action
= Slave
.Modify
,
563 if !ErrorMsg
.anyErrors
then
566 val changed
= foldl (fn ((site
, file
), changed
) =>
568 val ls
= case SM
.find (changed
, site
) of
572 SM
.insert (changed
, site
, file
:: ls
)
575 fun handleSite (site
, files
) =
579 print ("New configuration for node " ^ site ^
"\n");
580 if site
= Config
.defaultNode
then
581 Slave
.handleChanges files
583 val bio
= OpenSSL
.connect (valOf (!ssl_context
),
586 ^
Int.toString Config
.slavePort
)
588 app (fn file
=> Msg
.send (bio
, MsgFile file
)) files
;
589 Msg
.send (bio
, MsgDoFiles
);
591 NONE
=> print
"Slave closed connection unexpectedly\n"
594 MsgOk
=> print ("Slave " ^ site ^
" finished\n")
595 | MsgError s
=> print ("Slave " ^ site
596 ^
" returned error: " ^
598 | _
=> print ("Slave " ^ site
599 ^
" returned unexpected command\n");
604 SM
.appi handleSite changed
606 ignore (Slave
.shellF ([Config
.rm
, " -rf ", Config
.tmpDir
, ""],
607 fn cl
=> "Temp file cleanup failed: " ^ cl
))
610 fun inGroup group
= Acl
.query
{user
= getUser (), class
= "group", value
= "root"}
611 orelse Acl
.query
{user
= getUser (), class
= "group", value
= group
}