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 ("web_node_to_node",
161 val dl
= ErrorMsg
.dummyLoc
163 val nsD
= (EString Config
.defaultNs
, dl
)
164 val serialD
= (EVar
"serialAuto", dl
)
165 val refD
= (EInt Config
.defaultRefresh
, dl
)
166 val retD
= (EInt Config
.defaultRetry
, dl
)
167 val expD
= (EInt Config
.defaultExpiry
, dl
)
168 val minD
= (EInt Config
.defaultMinimum
, dl
)
170 val soaD
= multiApp ((EVar
"soa", dl
),
172 [nsD
, serialD
, refD
, retD
, expD
, minD
])
174 val masterD
= (EApp ((EVar
"internalMaster", dl
),
175 (EString Config
.masterNode
, dl
)),
178 val slavesD
= (EList (map (fn s
=> (EString s
, dl
)) Config
.slaveNodes
), dl
)
180 val _
= Defaults
.registerDefault ("Mailbox",
182 (fn () => (EString (getUser ()), dl
)))
184 val _
= Defaults
.registerDefault ("DNS",
185 (TBase
"dnsKind", dl
),
186 (fn () => multiApp ((EVar
"useDns", dl
),
188 [soaD
, masterD
, slavesD
])))
190 val _
= Defaults
.registerDefault ("TTL",
192 (fn () => (EInt Config
.Bind
.defaultTTL
, dl
)))
194 type soa
= {ns
: string,
201 val serial
= fn (EVar
"serialAuto", _
) => SOME NONE
202 |
(EApp ((EVar
"serialConst", _
), n
), _
) => Option
.map
SOME (Env
.int n
)
205 val soa
= fn (EApp ((EApp ((EApp ((EApp ((EApp ((EApp
206 ((EVar
"soa", _
), ns
), _
),
212 (case (Env
.string ns
, serial sl
, Env
.int rf
,
213 Env
.int ret
, Env
.int exp
, Env
.int min
) of
214 (SOME ns
, SOME sl
, SOME rf
,
215 SOME ret
, SOME exp
, SOME min
) =>
226 ExternalMaster
of string
227 | InternalMaster
of string
231 val _
= Env
.registerFunction ("ip_of_node",
232 fn [(EString node
, _
)] => SOME (EString (nodeIp node
), dl
)
235 val master
= fn (EApp ((EVar
"externalMaster", _
), e
), _
) => Option
.map
ExternalMaster (ip e
)
236 |
(EApp ((EVar
"internalMaster", _
), e
), _
) => Option
.map
InternalMaster (Env
.string e
)
240 UseDns
of {soa
: soa
,
242 slaves
: string list
}
245 val dnsKind
= fn (EApp ((EApp ((EApp
246 ((EVar
"useDns", _
), sa
), _
),
249 (case (soa sa
, master mstr
, Env
.list Env
.string slaves
) of
250 (SOME sa
, SOME mstr
, SOME slaves
) =>
251 SOME (UseDns
{soa
= sa
,
255 |
(EVar
"noDns", _
) => SOME NoDns
258 val befores
= ref (fn (_
: string) => ())
259 val afters
= ref (fn (_
: string) => ())
261 fun registerBefore f
=
265 befores
:= (fn x
=> (old x
; f x
))
268 fun registerAfter f
=
272 afters
:= (fn x
=> (old x
; f x
))
276 val currentPath
= ref (fn (_
: string) => "")
280 fun currentDomain () = !current
282 fun domainFile
{node
, name
} = ((*print ("Opening " ^
!currentPath node ^ name ^
"\n");*)
283 TextIO.openOut (!currentPath node ^ name
))
287 val toks
= String.fields (fn ch
=> ch
= #
".") domain
289 val elems
= foldr (fn (piece
, elems
) =>
291 val elems
= piece
:: elems
295 val path
= String.concatWith
"/"
296 (Config
.resultRoot
:: node
:: rev elems
)
297 val tmpPath
= String.concatWith
"/"
298 (Config
.tmpDir
:: node
:: rev elems
)
300 (if Posix
.FileSys
.ST
.isDir
301 (Posix
.FileSys
.stat path
) then
304 (OS
.FileSys
.remove path
;
305 OS
.FileSys
.mkDir path
))
306 handle OS
.SysErr _
=> OS
.FileSys
.mkDir path
;
308 (if Posix
.FileSys
.ST
.isDir
309 (Posix
.FileSys
.stat tmpPath
) then
312 (OS
.FileSys
.remove tmpPath
;
313 OS
.FileSys
.mkDir tmpPath
))
314 handle OS
.SysErr _
=> OS
.FileSys
.mkDir tmpPath
321 fn (root
, site
) => String.concatWith
"/" (root
:: site
:: rev ("" :: elems
))
324 datatype file_action
' =
325 Add
' of {src
: string, dst
: string}
327 | Modify
' of {src
: string, dst
: string}
329 fun findDiffs (site
, dom
, acts
) =
332 val realPath
= gp (Config
.resultRoot
, site
)
333 val tmpPath
= gp (Config
.tmpDir
, site
)
335 (*val _
= print ("getDiffs(" ^ site ^
", " ^ dom ^
")... " ^ realPath ^
"; " ^ tmpPath ^
"\n")*)
337 val dir
= Posix
.FileSys
.opendir realPath
340 case Posix
.FileSys
.readdir dir
of
341 NONE
=> (Posix
.FileSys
.closedir dir
;
345 val real = OS
.Path
.joinDirFile
{dir
= realPath
,
347 val tmp
= OS
.Path
.joinDirFile
{dir
= tmpPath
,
350 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat
real) then
352 else if Posix
.FileSys
.access (tmp
, []) then
353 if Slave
.shell
[Config
.diff
, " ", real, " ", tmp
] then
356 loopReal ((site
, dom
, realPath
, Modify
' {src
= tmp
, dst
= real}) :: acts
)
358 loopReal ((site
, dom
, realPath
, Delete
' real) :: acts
)
361 val acts
= loopReal acts
363 val dir
= Posix
.FileSys
.opendir tmpPath
366 case Posix
.FileSys
.readdir dir
of
367 NONE
=> (Posix
.FileSys
.closedir dir
;
371 val real = OS
.Path
.joinDirFile
{dir
= realPath
,
373 val tmp
= OS
.Path
.joinDirFile
{dir
= tmpPath
,
376 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat tmp
) then
378 else if Posix
.FileSys
.access (real, []) then
381 loopTmp ((site
, dom
, realPath
, Add
' {src
= tmp
, dst
= real}) :: acts
)
384 val acts
= loopTmp acts
389 fun findAllDiffs () =
391 val dir
= Posix
.FileSys
.opendir Config
.tmpDir
392 val len
= length (String.fields (fn ch
=> ch
= #
"/") Config
.tmpDir
) + 1
394 fun exploreSites diffs
=
395 case Posix
.FileSys
.readdir dir
of
399 fun explore (dname
, diffs
) =
401 val dir
= Posix
.FileSys
.opendir dname
404 case Posix
.FileSys
.readdir dir
of
408 val fname
= OS
.Path
.joinDirFile
{dir
= dname
,
411 loop (if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat fname
) then
413 val dom
= String.fields (fn ch
=> ch
= #
"/") fname
414 val dom
= List.drop (dom
, len
)
415 val dom
= String.concatWith
"." (rev dom
)
417 val dname
' = OS
.Path
.joinDirFile
{dir
= dname
,
421 findDiffs (site
, dom
, diffs
))
428 before Posix
.FileSys
.closedir dir
431 exploreSites (explore (OS
.Path
.joinDirFile
{dir
= Config
.tmpDir
,
432 file
= site
}, diffs
))
436 before Posix
.FileSys
.closedir dir
439 val masterNode
: string option ref
= ref NONE
440 fun dnsMaster () = !masterNode
442 val _
= Env
.containerV_one
"domain"
443 ("domain", Env
.string)
446 val kind
= Env
.env
dnsKind (evs
, "DNS")
447 val ttl
= Env
.env Env
.int (evs
, "TTL")
449 val path
= getPath dom
451 val () = (current
:= dom
;
452 currentPath
:= (fn site
=> path (Config
.tmpDir
, site
)))
454 fun saveSoa (kind
, soa
: soa
) node
=
456 val outf
= domainFile
{node
= node
, name
= "soa"}
458 TextIO.output (outf
, kind
);
459 TextIO.output (outf
, "\n");
460 TextIO.output (outf
, Int.toString ttl
);
461 TextIO.output (outf
, "\n");
462 TextIO.output (outf
, #ns soa
);
463 TextIO.output (outf
, "\n");
466 | SOME n
=> TextIO.output (outf
, Int.toString n
);
467 TextIO.output (outf
, "\n");
468 TextIO.output (outf
, Int.toString (#ref soa
));
469 TextIO.output (outf
, "\n");
470 TextIO.output (outf
, Int.toString (#ret soa
));
471 TextIO.output (outf
, "\n");
472 TextIO.output (outf
, Int.toString (#exp soa
));
473 TextIO.output (outf
, "\n");
474 TextIO.output (outf
, Int.toString (#min soa
));
475 TextIO.output (outf
, "\n");
479 fun saveNamed (kind
, soa
: soa
, masterIp
) node
=
481 val outf
= domainFile
{node
= node
, name
= "named.conf"}
483 TextIO.output (outf
, "\nzone \"");
484 TextIO.output (outf
, dom
);
485 TextIO.output (outf
, "\" IN {\n\ttype ");
486 TextIO.output (outf
, kind
);
487 TextIO.output (outf
, ";\n\tfile \"");
488 TextIO.output (outf
, Config
.Bind
.zonePath_real
);
489 TextIO.output (outf
, "/");
490 TextIO.output (outf
, dom
);
491 TextIO.output (outf
, ".zone\";\n");
493 "master" => TextIO.output (outf
, "\tallow-update { none; };\n")
494 | _
=> (TextIO.output (outf
, "\tmasters { ");
495 TextIO.output (outf
, masterIp
);
496 TextIO.output (outf
, "; };\n"));
497 TextIO.output (outf
, "};\n");
502 NoDns
=> masterNode
:= NONE
507 InternalMaster node
=> valOf (SM
.find (nodeMap
, node
))
508 | ExternalMaster ip
=> ip
510 app (saveSoa ("slave", #soa dns
)) (#slaves dns
);
511 app (saveNamed ("slave", #soa dns
, masterIp
)) (#slaves dns
);
513 InternalMaster node
=>
514 (masterNode
:= SOME node
;
515 saveSoa ("master", #soa dns
) node
;
516 saveNamed ("master", #soa dns
, masterIp
) node
)
517 | _
=> masterNode
:= NONE
;
521 fn () => !afters (!current
))
523 val () = Env
.registerPre (fn () => (ignore (Slave
.shellF ([Config
.rm
, " -rf ", Config
.tmpDir
, ""],
524 fn cl
=> "Temp file cleanup failed: " ^ cl
));
525 OS
.FileSys
.mkDir Config
.tmpDir
;
526 app (fn node
=> OS
.FileSys
.mkDir
527 (OS
.Path
.joinDirFile
{dir
= Config
.tmpDir
,
530 app (fn node
=> OS
.FileSys
.mkDir
531 (OS
.Path
.joinDirFile
{dir
= Config
.resultRoot
,
533 handle OS
.SysErr _
=> ())
536 val () = Env
.registerPost (fn () =>
538 val diffs
= findAllDiffs ()
540 val diffs
= map (fn (site
, dom
, dir
, Add
' {src
, dst
}) =>
541 (Slave
.shellF ([Config
.cp
, " ", src
, " ", dst
],
542 fn cl
=> "Copy failed: " ^ cl
);
548 |
(site
, dom
, dir
, Delete
' dst
) =>
549 (OS
.FileSys
.remove dst
550 handle OS
.SysErr _
=>
551 ErrorMsg
.error
NONE ("Delete failed for " ^ dst
);
553 {action
= Slave
.Delete
,
557 |
(site
, dom
, dir
, Modify
' {src
, dst
}) =>
558 (Slave
.shellF ([Config
.cp
, " ", src
, " ", dst
],
559 fn cl
=> "Copy failed: " ^ cl
);
561 {action
= Slave
.Modify
,
566 if !ErrorMsg
.anyErrors
then
569 val changed
= foldl (fn ((site
, file
), changed
) =>
571 val ls
= case SM
.find (changed
, site
) of
575 SM
.insert (changed
, site
, file
:: ls
)
578 fun handleSite (site
, files
) =
582 print ("New configuration for node " ^ site ^
"\n");
583 if site
= Config
.defaultNode
then
584 Slave
.handleChanges files
586 val bio
= OpenSSL
.connect (valOf (!ssl_context
),
589 ^
Int.toString Config
.slavePort
)
591 app (fn file
=> Msg
.send (bio
, MsgFile file
)) files
;
592 Msg
.send (bio
, MsgDoFiles
);
594 NONE
=> print
"Slave closed connection unexpectedly\n"
597 MsgOk
=> print ("Slave " ^ site ^
" finished\n")
598 | MsgError s
=> print ("Slave " ^ site
599 ^
" returned error: " ^
601 | _
=> print ("Slave " ^ site
602 ^
" returned unexpected command\n");
607 SM
.appi handleSite changed
609 ignore (Slave
.shellF ([Config
.rm
, " -rf ", Config
.tmpDir
, ""],
610 fn cl
=> "Temp file cleanup failed: " ^ cl
))
613 fun hasPriv priv
= Acl
.query
{user
= getUser (), class
= "priv", value
= "all"}
614 orelse Acl
.query
{user
= getUser (), class
= "priv", value
= priv
}
616 val _
= Env
.type_one
"dns_node"
619 List.exists (fn x
=> x
= node
) Config
.dnsNodes_all
620 orelse (hasPriv
"dns"
621 andalso List.exists (fn x
=> x
= node
) Config
.dnsNodes_admin
))