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"
156 val _
= Env
.registerFunction ("dns_node_to_node",
160 val _
= Env
.registerFunction ("mail_node_to_node",
165 val dl
= ErrorMsg
.dummyLoc
167 val nsD
= (EString Config
.defaultNs
, dl
)
168 val serialD
= (EVar
"serialAuto", dl
)
169 val refD
= (EInt Config
.defaultRefresh
, dl
)
170 val retD
= (EInt Config
.defaultRetry
, dl
)
171 val expD
= (EInt Config
.defaultExpiry
, dl
)
172 val minD
= (EInt Config
.defaultMinimum
, dl
)
174 val soaD
= multiApp ((EVar
"soa", dl
),
176 [nsD
, serialD
, refD
, retD
, expD
, minD
])
178 val masterD
= (EApp ((EVar
"internalMaster", dl
),
179 (EString Config
.masterNode
, dl
)),
182 val slavesD
= (EList (map (fn s
=> (EString s
, dl
)) Config
.slaveNodes
), dl
)
184 val _
= Defaults
.registerDefault ("Mailbox",
186 (fn () => (EString (getUser ()), dl
)))
188 val _
= Defaults
.registerDefault ("DNS",
189 (TBase
"dnsKind", dl
),
190 (fn () => multiApp ((EVar
"useDns", dl
),
192 [soaD
, masterD
, slavesD
])))
194 val _
= Defaults
.registerDefault ("TTL",
196 (fn () => (EInt Config
.Bind
.defaultTTL
, dl
)))
198 type soa
= {ns
: string,
205 val serial
= fn (EVar
"serialAuto", _
) => SOME NONE
206 |
(EApp ((EVar
"serialConst", _
), n
), _
) => Option
.map
SOME (Env
.int n
)
209 val soa
= fn (EApp ((EApp ((EApp ((EApp ((EApp ((EApp
210 ((EVar
"soa", _
), ns
), _
),
216 (case (Env
.string ns
, serial sl
, Env
.int rf
,
217 Env
.int ret
, Env
.int exp
, Env
.int min
) of
218 (SOME ns
, SOME sl
, SOME rf
,
219 SOME ret
, SOME exp
, SOME min
) =>
230 ExternalMaster
of string
231 | InternalMaster
of string
235 val _
= Env
.registerFunction ("ip_of_node",
236 fn [(EString node
, _
)] => SOME (EString (nodeIp node
), dl
)
239 val master
= fn (EApp ((EVar
"externalMaster", _
), e
), _
) => Option
.map
ExternalMaster (ip e
)
240 |
(EApp ((EVar
"internalMaster", _
), e
), _
) => Option
.map
InternalMaster (Env
.string e
)
244 UseDns
of {soa
: soa
,
246 slaves
: string list
}
249 val dnsKind
= fn (EApp ((EApp ((EApp
250 ((EVar
"useDns", _
), sa
), _
),
253 (case (soa sa
, master mstr
, Env
.list Env
.string slaves
) of
254 (SOME sa
, SOME mstr
, SOME slaves
) =>
255 SOME (UseDns
{soa
= sa
,
259 |
(EVar
"noDns", _
) => SOME NoDns
262 val befores
= ref (fn (_
: string) => ())
263 val afters
= ref (fn (_
: string) => ())
265 fun registerBefore f
=
269 befores
:= (fn x
=> (old x
; f x
))
272 fun registerAfter f
=
276 afters
:= (fn x
=> (old x
; f x
))
280 val currentPath
= ref (fn (_
: string) => "")
284 fun currentDomain () = !current
286 fun domainFile
{node
, name
} = ((*print ("Opening " ^
!currentPath node ^ name ^
"\n");*)
287 TextIO.openOut (!currentPath node ^ name
))
291 val toks
= String.fields (fn ch
=> ch
= #
".") domain
293 val elems
= foldr (fn (piece
, elems
) =>
295 val elems
= piece
:: elems
299 val path
= String.concatWith
"/"
300 (Config
.resultRoot
:: node
:: rev elems
)
301 val tmpPath
= String.concatWith
"/"
302 (Config
.tmpDir
:: node
:: rev elems
)
304 (if Posix
.FileSys
.ST
.isDir
305 (Posix
.FileSys
.stat path
) then
308 (OS
.FileSys
.remove path
;
309 OS
.FileSys
.mkDir path
))
310 handle OS
.SysErr _
=> OS
.FileSys
.mkDir path
;
312 (if Posix
.FileSys
.ST
.isDir
313 (Posix
.FileSys
.stat tmpPath
) then
316 (OS
.FileSys
.remove tmpPath
;
317 OS
.FileSys
.mkDir tmpPath
))
318 handle OS
.SysErr _
=> OS
.FileSys
.mkDir tmpPath
325 fn (root
, site
) => String.concatWith
"/" (root
:: site
:: rev ("" :: elems
))
328 datatype file_action
' =
329 Add
' of {src
: string, dst
: string}
331 | Modify
' of {src
: string, dst
: string}
333 fun findDiffs (site
, dom
, acts
) =
336 val realPath
= gp (Config
.resultRoot
, site
)
337 val tmpPath
= gp (Config
.tmpDir
, site
)
339 (*val _
= print ("getDiffs(" ^ site ^
", " ^ dom ^
")... " ^ realPath ^
"; " ^ tmpPath ^
"\n")*)
341 val dir
= Posix
.FileSys
.opendir realPath
344 case Posix
.FileSys
.readdir dir
of
345 NONE
=> (Posix
.FileSys
.closedir dir
;
349 val real = OS
.Path
.joinDirFile
{dir
= realPath
,
351 val tmp
= OS
.Path
.joinDirFile
{dir
= tmpPath
,
354 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat
real) then
356 else if Posix
.FileSys
.access (tmp
, []) then
357 if Slave
.shell
[Config
.diff
, " ", real, " ", tmp
] then
360 loopReal ((site
, dom
, realPath
, Modify
' {src
= tmp
, dst
= real}) :: acts
)
362 loopReal ((site
, dom
, realPath
, Delete
' real) :: acts
)
365 val acts
= loopReal acts
367 val dir
= Posix
.FileSys
.opendir tmpPath
370 case Posix
.FileSys
.readdir dir
of
371 NONE
=> (Posix
.FileSys
.closedir dir
;
375 val real = OS
.Path
.joinDirFile
{dir
= realPath
,
377 val tmp
= OS
.Path
.joinDirFile
{dir
= tmpPath
,
380 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat tmp
) then
382 else if Posix
.FileSys
.access (real, []) then
385 loopTmp ((site
, dom
, realPath
, Add
' {src
= tmp
, dst
= real}) :: acts
)
388 val acts
= loopTmp acts
393 fun findAllDiffs () =
395 val dir
= Posix
.FileSys
.opendir Config
.tmpDir
396 val len
= length (String.fields (fn ch
=> ch
= #
"/") Config
.tmpDir
) + 1
398 fun exploreSites diffs
=
399 case Posix
.FileSys
.readdir dir
of
403 fun explore (dname
, diffs
) =
405 val dir
= Posix
.FileSys
.opendir dname
408 case Posix
.FileSys
.readdir dir
of
412 val fname
= OS
.Path
.joinDirFile
{dir
= dname
,
415 loop (if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat fname
) then
417 val dom
= String.fields (fn ch
=> ch
= #
"/") fname
418 val dom
= List.drop (dom
, len
)
419 val dom
= String.concatWith
"." (rev dom
)
421 val dname
' = OS
.Path
.joinDirFile
{dir
= dname
,
425 findDiffs (site
, dom
, diffs
))
432 before Posix
.FileSys
.closedir dir
435 exploreSites (explore (OS
.Path
.joinDirFile
{dir
= Config
.tmpDir
,
436 file
= site
}, diffs
))
440 before Posix
.FileSys
.closedir dir
443 val masterNode
: string option ref
= ref NONE
444 fun dnsMaster () = !masterNode
446 val _
= Env
.containerV_one
"domain"
447 ("domain", Env
.string)
450 val kind
= Env
.env
dnsKind (evs
, "DNS")
451 val ttl
= Env
.env Env
.int (evs
, "TTL")
453 val path
= getPath dom
455 val () = (current
:= dom
;
456 currentPath
:= (fn site
=> path (Config
.tmpDir
, site
)))
458 fun saveSoa (kind
, soa
: soa
) node
=
460 val outf
= domainFile
{node
= node
, name
= "soa"}
462 TextIO.output (outf
, kind
);
463 TextIO.output (outf
, "\n");
464 TextIO.output (outf
, Int.toString ttl
);
465 TextIO.output (outf
, "\n");
466 TextIO.output (outf
, #ns soa
);
467 TextIO.output (outf
, "\n");
470 | SOME n
=> TextIO.output (outf
, Int.toString n
);
471 TextIO.output (outf
, "\n");
472 TextIO.output (outf
, Int.toString (#ref soa
));
473 TextIO.output (outf
, "\n");
474 TextIO.output (outf
, Int.toString (#ret soa
));
475 TextIO.output (outf
, "\n");
476 TextIO.output (outf
, Int.toString (#exp soa
));
477 TextIO.output (outf
, "\n");
478 TextIO.output (outf
, Int.toString (#min soa
));
479 TextIO.output (outf
, "\n");
483 fun saveNamed (kind
, soa
: soa
, masterIp
) node
=
485 val outf
= domainFile
{node
= node
, name
= "named.conf"}
487 TextIO.output (outf
, "\nzone \"");
488 TextIO.output (outf
, dom
);
489 TextIO.output (outf
, "\" IN {\n\ttype ");
490 TextIO.output (outf
, kind
);
491 TextIO.output (outf
, ";\n\tfile \"");
492 TextIO.output (outf
, Config
.Bind
.zonePath_real
);
493 TextIO.output (outf
, "/");
494 TextIO.output (outf
, dom
);
495 TextIO.output (outf
, ".zone\";\n");
497 "master" => TextIO.output (outf
, "\tallow-update { none; };\n")
498 | _
=> (TextIO.output (outf
, "\tmasters { ");
499 TextIO.output (outf
, masterIp
);
500 TextIO.output (outf
, "; };\n"));
501 TextIO.output (outf
, "};\n");
506 NoDns
=> masterNode
:= NONE
511 InternalMaster node
=> valOf (SM
.find (nodeMap
, node
))
512 | ExternalMaster ip
=> ip
514 app (saveSoa ("slave", #soa dns
)) (#slaves dns
);
515 app (saveNamed ("slave", #soa dns
, masterIp
)) (#slaves dns
);
517 InternalMaster node
=>
518 (masterNode
:= SOME node
;
519 saveSoa ("master", #soa dns
) node
;
520 saveNamed ("master", #soa dns
, masterIp
) node
)
521 | _
=> masterNode
:= NONE
;
525 fn () => !afters (!current
))
527 val () = Env
.registerPre (fn () => (ignore (Slave
.shellF ([Config
.rm
, " -rf ", Config
.tmpDir
, ""],
528 fn cl
=> "Temp file cleanup failed: " ^ cl
));
529 OS
.FileSys
.mkDir Config
.tmpDir
;
530 app (fn node
=> OS
.FileSys
.mkDir
531 (OS
.Path
.joinDirFile
{dir
= Config
.tmpDir
,
534 app (fn node
=> OS
.FileSys
.mkDir
535 (OS
.Path
.joinDirFile
{dir
= Config
.resultRoot
,
537 handle OS
.SysErr _
=> ())
540 val () = Env
.registerPost (fn () =>
542 val diffs
= findAllDiffs ()
544 val diffs
= map (fn (site
, dom
, dir
, Add
' {src
, dst
}) =>
545 (Slave
.shellF ([Config
.cp
, " ", src
, " ", dst
],
546 fn cl
=> "Copy failed: " ^ cl
);
552 |
(site
, dom
, dir
, Delete
' dst
) =>
553 (OS
.FileSys
.remove dst
554 handle OS
.SysErr _
=>
555 ErrorMsg
.error
NONE ("Delete failed for " ^ dst
);
557 {action
= Slave
.Delete
,
561 |
(site
, dom
, dir
, Modify
' {src
, dst
}) =>
562 (Slave
.shellF ([Config
.cp
, " ", src
, " ", dst
],
563 fn cl
=> "Copy failed: " ^ cl
);
565 {action
= Slave
.Modify
,
570 if !ErrorMsg
.anyErrors
then
573 val changed
= foldl (fn ((site
, file
), changed
) =>
575 val ls
= case SM
.find (changed
, site
) of
579 SM
.insert (changed
, site
, file
:: ls
)
582 fun handleSite (site
, files
) =
586 print ("New configuration for node " ^ site ^
"\n");
587 if site
= Config
.defaultNode
then
588 Slave
.handleChanges files
590 val bio
= OpenSSL
.connect (valOf (!ssl_context
),
593 ^
Int.toString Config
.slavePort
)
595 app (fn file
=> Msg
.send (bio
, MsgFile file
)) files
;
596 Msg
.send (bio
, MsgDoFiles
);
598 NONE
=> print
"Slave closed connection unexpectedly\n"
601 MsgOk
=> print ("Slave " ^ site ^
" finished\n")
602 | MsgError s
=> print ("Slave " ^ site
603 ^
" returned error: " ^
605 | _
=> print ("Slave " ^ site
606 ^
" returned unexpected command\n");
611 SM
.appi handleSite changed
613 ignore (Slave
.shellF ([Config
.rm
, " -rf ", Config
.tmpDir
, ""],
614 fn cl
=> "Temp file cleanup failed: " ^ cl
))
617 fun hasPriv priv
= Acl
.query
{user
= getUser (), class
= "priv", value
= "all"}
618 orelse Acl
.query
{user
= getUser (), class
= "priv", value
= priv
}
620 val _
= Env
.type_one
"dns_node"
623 List.exists (fn x
=> x
= node
) Config
.dnsNodes_all
624 orelse (hasPriv
"dns"
625 andalso List.exists (fn x
=> x
= node
) Config
.dnsNodes_admin
))
627 val _
= Env
.type_one
"mail_node"
630 List.exists (fn x
=> x
= node
) Config
.mailNodes_all
631 orelse (hasPriv
"mail"
632 andalso List.exists (fn x
=> x
= node
) Config
.mailNodes_admin
))