1 (* HCoop
Domtool (http
://hcoop
.sourceforge
.net
/)
2 * Copyright (c
) 2006-2007, 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 val yourDomain
= yourDomainHost
102 fun validUser s
= size s
> 0 andalso size s
< 20
103 andalso CharVector
.all
Char.isAlphaNum s
105 fun validEmailUser s
=
106 size s
> 0 andalso size s
< 50
107 andalso CharVector
.all (fn ch
=> Char.isAlphaNum ch
113 val validGroup
= validUser
115 val _
= Env
.type_one
"no_spaces"
117 (CharVector
.all (fn ch
=> Char.isPrint ch
andalso not (Char.isSpace ch
)
118 andalso ch
<> #
"\"" andalso ch
<> #
"'"))
119 val _
= Env
.type_one
"no_newlines"
121 (CharVector
.all (fn ch
=> Char.isPrint ch
andalso ch
<> #
"\n" andalso ch
<> #
"\r"
122 andalso ch
<> #
"\"" andalso ch
<> #
"'"))
124 val _
= Env
.type_one
"ip"
128 val _
= Env
.type_one
"host"
132 val _
= Env
.type_one
"domain"
136 val _
= Env
.type_one
"your_domain"
140 val _
= Env
.type_one
"your_domain_host"
144 val _
= Env
.type_one
"user"
148 val _
= Env
.type_one
"group"
152 val _
= Env
.type_one
"your_user"
156 val _
= Env
.type_one
"your_group"
160 val _
= Env
.type_one
"your_path"
164 val _
= Env
.type_one
"node"
168 val _
= Env
.registerFunction ("dns_node_to_node",
172 val _
= Env
.registerFunction ("mail_node_to_node",
177 val dl
= ErrorMsg
.dummyLoc
179 val nsD
= (EString Config
.defaultNs
, dl
)
180 val serialD
= (EVar
"serialAuto", dl
)
181 val refD
= (EInt Config
.defaultRefresh
, dl
)
182 val retD
= (EInt Config
.defaultRetry
, dl
)
183 val expD
= (EInt Config
.defaultExpiry
, dl
)
184 val minD
= (EInt Config
.defaultMinimum
, dl
)
186 val soaD
= multiApp ((EVar
"soa", dl
),
188 [nsD
, serialD
, refD
, retD
, expD
, minD
])
190 val masterD
= (EApp ((EVar
"internalMaster", dl
),
191 (EString Config
.masterNode
, dl
)),
194 val slavesD
= (EList (map (fn s
=> (EString s
, dl
)) Config
.slaveNodes
), dl
)
196 val _
= Defaults
.registerDefault ("Aliases",
197 (TList (TBase
"your_domain", dl
), dl
),
198 (fn () => (EList
[], dl
)))
200 val _
= Defaults
.registerDefault ("Mailbox",
202 (fn () => (EString (getUser ()), dl
)))
204 val _
= Defaults
.registerDefault ("DNS",
205 (TBase
"dnsKind", dl
),
206 (fn () => multiApp ((EVar
"useDns", dl
),
208 [soaD
, masterD
, slavesD
])))
210 val _
= Defaults
.registerDefault ("TTL",
212 (fn () => (EInt Config
.Bind
.defaultTTL
, dl
)))
214 type soa
= {ns
: string,
221 val serial
= fn (EVar
"serialAuto", _
) => SOME NONE
222 |
(EApp ((EVar
"serialConst", _
), n
), _
) => Option
.map
SOME (Env
.int n
)
225 val soa
= fn (EApp ((EApp ((EApp ((EApp ((EApp ((EApp
226 ((EVar
"soa", _
), ns
), _
),
232 (case (Env
.string ns
, serial sl
, Env
.int rf
,
233 Env
.int ret
, Env
.int exp
, Env
.int min
) of
234 (SOME ns
, SOME sl
, SOME rf
,
235 SOME ret
, SOME exp
, SOME min
) =>
246 ExternalMaster
of string
247 | InternalMaster
of string
251 val _
= Env
.registerFunction ("ip_of_node",
252 fn [(EString node
, _
)] => SOME (EString (nodeIp node
), dl
)
255 val master
= fn (EApp ((EVar
"externalMaster", _
), e
), _
) => Option
.map
ExternalMaster (ip e
)
256 |
(EApp ((EVar
"internalMaster", _
), e
), _
) => Option
.map
InternalMaster (Env
.string e
)
260 UseDns
of {soa
: soa
,
262 slaves
: string list
}
265 val dnsKind
= fn (EApp ((EApp ((EApp
266 ((EVar
"useDns", _
), sa
), _
),
269 (case (soa sa
, master mstr
, Env
.list Env
.string slaves
) of
270 (SOME sa
, SOME mstr
, SOME slaves
) =>
271 SOME (UseDns
{soa
= sa
,
275 |
(EVar
"noDns", _
) => SOME NoDns
278 val befores
= ref (fn (_
: string) => ())
279 val afters
= ref (fn (_
: string) => ())
281 fun registerBefore f
=
285 befores
:= (fn x
=> (old x
; f x
))
288 fun registerAfter f
=
292 afters
:= (fn x
=> (old x
; f x
))
295 val globals
= ref (fn () => ())
296 val locals
= ref (fn () => ())
298 fun registerResetGlobal f
=
302 globals
:= (fn x
=> (old x
; f x
))
305 fun registerResetLocal f
=
309 locals
:= (fn x
=> (old x
; f x
))
312 fun resetGlobal () = (!globals ();
313 ignore (OS
.Process
.system (Config
.rm ^
" -rf " ^ Config
.resultRoot ^
"/*")))
314 fun resetLocal () = !locals ()
317 val currentPath
= ref (fn (_
: string) => "")
318 val currentPathAli
= ref (fn (_
: string, _
: string) => "")
322 fun currentDomain () = !current
324 val currentsAli
= ref ([] : string list
)
326 fun currentAliasDomains () = !currentsAli
327 fun currentDomains () = currentDomain () :: currentAliasDomains ()
329 fun domainFile
{node
, name
} = ((*print ("Opening " ^
!currentPath node ^ name ^
"\n");*)
330 TextIO.openOut (!currentPath node ^ name
))
332 type files
= {write
: string -> unit
,
333 writeDom
: unit
-> unit
,
334 close
: unit
-> unit
}
336 fun domainsFile
{node
, name
} =
338 val doms
= currentDomains ()
339 val files
= map (fn dom
=> (dom
, TextIO.openOut (!currentPathAli (dom
, node
) ^ name
))) doms
341 {write
= fn s
=> app (fn (_
, outf
) => TextIO.output (outf
, s
)) files
,
342 writeDom
= fn () => app (fn (dom
, outf
) => TextIO.output (outf
, dom
)) files
,
343 close
= fn () => app (fn (_
, outf
) => TextIO.closeOut outf
) files
}
348 val toks
= String.fields (fn ch
=> ch
= #
".") domain
350 val elems
= foldr (fn (piece
, elems
) =>
352 val elems
= piece
:: elems
356 val path
= String.concatWith
"/"
357 (Config
.resultRoot
:: node
:: rev elems
)
358 val tmpPath
= String.concatWith
"/"
359 (Config
.tmpDir
:: node
:: rev elems
)
361 (if Posix
.FileSys
.ST
.isDir
362 (Posix
.FileSys
.stat path
) then
365 (OS
.FileSys
.remove path
;
366 OS
.FileSys
.mkDir path
))
367 handle OS
.SysErr _
=> OS
.FileSys
.mkDir path
;
369 (if Posix
.FileSys
.ST
.isDir
370 (Posix
.FileSys
.stat tmpPath
) then
373 (OS
.FileSys
.remove tmpPath
;
374 OS
.FileSys
.mkDir tmpPath
))
375 handle OS
.SysErr _
=> OS
.FileSys
.mkDir tmpPath
382 fn (root
, site
) => String.concatWith
"/" (root
:: site
:: rev ("" :: elems
))
385 datatype file_action
' =
386 Add
' of {src
: string, dst
: string}
388 | Modify
' of {src
: string, dst
: string}
390 fun findDiffs (site
, dom
, acts
) =
393 val realPath
= gp (Config
.resultRoot
, site
)
394 val tmpPath
= gp (Config
.tmpDir
, site
)
396 (*val _
= print ("getDiffs(" ^ site ^
", " ^ dom ^
")... " ^ realPath ^
"; " ^ tmpPath ^
"\n")*)
398 val dir
= Posix
.FileSys
.opendir realPath
401 case Posix
.FileSys
.readdir dir
of
402 NONE
=> (Posix
.FileSys
.closedir dir
;
406 val real = OS
.Path
.joinDirFile
{dir
= realPath
,
408 val tmp
= OS
.Path
.joinDirFile
{dir
= tmpPath
,
411 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat
real) then
413 else if Posix
.FileSys
.access (tmp
, []) then
414 if Slave
.shell
[Config
.diff
, " ", real, " ", tmp
] then
417 loopReal ((site
, dom
, realPath
, Modify
' {src
= tmp
, dst
= real}) :: acts
)
419 loopReal ((site
, dom
, realPath
, Delete
' real) :: acts
)
422 val acts
= loopReal acts
424 val dir
= Posix
.FileSys
.opendir tmpPath
427 case Posix
.FileSys
.readdir dir
of
428 NONE
=> (Posix
.FileSys
.closedir dir
;
432 val real = OS
.Path
.joinDirFile
{dir
= realPath
,
434 val tmp
= OS
.Path
.joinDirFile
{dir
= tmpPath
,
437 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat tmp
) then
439 else if Posix
.FileSys
.access (real, []) then
442 loopTmp ((site
, dom
, realPath
, Add
' {src
= tmp
, dst
= real}) :: acts
)
445 val acts
= loopTmp acts
450 fun findAllDiffs () =
452 val dir
= Posix
.FileSys
.opendir Config
.tmpDir
453 val len
= length (String.fields (fn ch
=> ch
= #
"/") Config
.tmpDir
) + 1
455 fun exploreSites diffs
=
456 case Posix
.FileSys
.readdir dir
of
460 fun explore (dname
, diffs
) =
462 val dir
= Posix
.FileSys
.opendir dname
465 case Posix
.FileSys
.readdir dir
of
469 val fname
= OS
.Path
.joinDirFile
{dir
= dname
,
472 loop (if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat fname
) then
474 val dom
= String.fields (fn ch
=> ch
= #
"/") fname
475 val dom
= List.drop (dom
, len
)
476 val dom
= String.concatWith
"." (rev dom
)
478 val dname
' = OS
.Path
.joinDirFile
{dir
= dname
,
482 findDiffs (site
, dom
, diffs
))
489 before Posix
.FileSys
.closedir dir
492 exploreSites (explore (OS
.Path
.joinDirFile
{dir
= Config
.tmpDir
,
493 file
= site
}, diffs
))
497 before Posix
.FileSys
.closedir dir
500 val masterNode
: string option ref
= ref NONE
501 fun dnsMaster () = !masterNode
503 val _
= Env
.containerV_one
"domain"
504 ("domain", Env
.string)
507 val kind
= Env
.env
dnsKind (evs
, "DNS")
508 val ttl
= Env
.env Env
.int (evs
, "TTL")
509 val aliases
= Env
.env (Env
.list Env
.string) (evs
, "Aliases")
511 val path
= getPath dom
513 val () = (current
:= dom
;
514 currentsAli
:= Slave
.remove (Slave
.removeDups aliases
, dom
);
515 currentPath
:= (fn site
=> path (Config
.tmpDir
, site
));
516 currentPathAli
:= (fn (dom
, site
) => getPath
dom (Config
.tmpDir
, site
)))
518 fun saveSoa (kind
, soa
: soa
) node
=
520 val {write
, writeDom
, close
} = domainsFile
{node
= node
, name
= "soa"}
524 write (Int.toString ttl
);
530 | SOME n
=> write (Int.toString n
);
532 write (Int.toString (#ref soa
));
534 write (Int.toString (#ret soa
));
536 write (Int.toString (#exp soa
));
538 write (Int.toString (#min soa
));
543 fun saveNamed (kind
, soa
: soa
, masterIp
, slaveIps
) node
=
544 if dom
= "localhost" then
547 val {write
, writeDom
, close
} = domainsFile
{node
= node
, name
= "named.conf"}
551 write
"\" {\n\ttype ";
553 write
";\n\tfile \"";
554 write Config
.Bind
.zonePath_real
;
559 "master" => (write
"\tallow-transfer {\n";
560 app (fn ip
=> (write
"\t\t";
562 write
";\n")) slaveIps
;
564 | _
=> (write
"\tmasters { ";
572 NoDns
=> masterNode
:= NONE
577 InternalMaster node
=> nodeIp node
578 | ExternalMaster ip
=> ip
580 val slaveIps
= map
nodeIp (#slaves dns
)
582 app (saveSoa ("slave", #soa dns
)) (#slaves dns
);
583 app (saveNamed ("slave", #soa dns
, masterIp
, slaveIps
)) (#slaves dns
);
585 InternalMaster node
=>
586 (masterNode
:= SOME node
;
587 saveSoa ("master", #soa dns
) node
;
588 saveNamed ("master", #soa dns
, masterIp
, slaveIps
) node
)
589 | _
=> masterNode
:= NONE
593 fn () => !afters (!current
))
595 val () = Env
.registerPre (fn () => (ignore (Slave
.shellF ([Config
.rm
, " -rf ", Config
.tmpDir
, ""],
596 fn cl
=> "Temp file cleanup failed: " ^ cl
));
597 OS
.FileSys
.mkDir Config
.tmpDir
;
598 app (fn node
=> OS
.FileSys
.mkDir
599 (OS
.Path
.joinDirFile
{dir
= Config
.tmpDir
,
602 app (fn node
=> OS
.FileSys
.mkDir
603 (OS
.Path
.joinDirFile
{dir
= Config
.resultRoot
,
605 handle OS
.SysErr _
=> ())
608 fun handleSite (site
, files
) =
612 print ("New configuration for node " ^ site ^
"\n");
613 if site
= Config
.defaultNode
then
614 Slave
.handleChanges files
616 val bio
= OpenSSL
.connect (valOf (!ssl_context
),
619 ^
Int.toString Config
.slavePort
)
621 app (fn file
=> Msg
.send (bio
, MsgFile file
)) files
;
622 Msg
.send (bio
, MsgDoFiles
);
624 NONE
=> print
"Slave closed connection unexpectedly\n"
627 MsgOk
=> print ("Slave " ^ site ^
" finished\n")
628 | MsgError s
=> print ("Slave " ^ site
629 ^
" returned error: " ^
631 | _
=> print ("Slave " ^ site
632 ^
" returned unexpected command\n");
637 val () = Env
.registerPost (fn () =>
639 val diffs
= findAllDiffs ()
641 val diffs
= map (fn (site
, dom
, dir
, Add
' {src
, dst
}) =>
642 (Slave
.shellF ([Config
.cp
, " ", src
, " ", dst
],
643 fn cl
=> "Copy failed: " ^ cl
);
649 |
(site
, dom
, dir
, Delete
' dst
) =>
650 (OS
.FileSys
.remove dst
651 handle OS
.SysErr _
=>
652 ErrorMsg
.error
NONE ("Delete failed for " ^ dst
);
654 {action
= Slave
.Delete
,
658 |
(site
, dom
, dir
, Modify
' {src
, dst
}) =>
659 (Slave
.shellF ([Config
.cp
, " ", src
, " ", dst
],
660 fn cl
=> "Copy failed: " ^ cl
);
662 {action
= Slave
.Modify
,
667 if !ErrorMsg
.anyErrors
then
670 val changed
= foldl (fn ((site
, file
), changed
) =>
672 val ls
= case SM
.find (changed
, site
) of
676 SM
.insert (changed
, site
, file
:: ls
)
679 SM
.appi handleSite changed
681 ignore (Slave
.shellF ([Config
.rm
, " -rf ", Config
.tmpDir
, ""],
682 fn cl
=> "Temp file cleanup failed: " ^ cl
))
685 fun hasPriv priv
= Acl
.query
{user
= getUser (), class
= "priv", value
= "all"}
686 orelse Acl
.query
{user
= getUser (), class
= "priv", value
= priv
}
688 val _
= Env
.type_one
"dns_node"
691 List.exists (fn x
=> x
= node
) Config
.dnsNodes_all
692 orelse (hasPriv
"dns"
693 andalso List.exists (fn x
=> x
= node
) Config
.dnsNodes_admin
))
695 val _
= Env
.type_one
"mail_node"
698 List.exists (fn x
=> x
= node
) Config
.mailNodes_all
699 orelse (hasPriv
"mail"
700 andalso List.exists (fn x
=> x
= node
) Config
.mailNodes_admin
))
704 fun doNode (node
, _
) =
706 val dname
= OS
.Path
.joinDirFile
{dir
= Config
.resultRoot
,
709 fun doDom (dom
, actions
) =
711 val domPath
= String.concatWith
"/" (rev (String.fields (fn ch
=> ch
= #
".") dom
))
712 val dname
= OS
.Path
.concat (dname
, domPath
)
714 fun visitDom (dom
, dname
, actions
) =
716 val dir
= Posix
.FileSys
.opendir dname
719 case Posix
.FileSys
.readdir dir
of
723 val fnameFull
= OS
.Path
.joinDirFile
{dir
= dname
,
726 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat fnameFull
) then
727 loop (visitDom (fname ^
"." ^ dom
,
731 loop ({action
= Slave
.Delete
,
734 file
= fnameFull
} :: actions
)
738 before Posix
.FileSys
.closedir dir
740 handle OS
.SysErr _
=>
741 (print ("Warning: System error deleting domain " ^ dom ^
" on " ^ node ^
".\n");
744 visitDom (dom
, dname
, actions
)
747 val actions
= foldl doDom
[] doms
749 handleSite (node
, actions
)
751 handle IO
.Io _
=> print ("Warning: IO error deleting domains on " ^ node ^
".\n")
753 fun cleanupNode (node
, _
) =
757 val domPath
= String.concatWith
"/" (rev (String.fields (fn ch
=> ch
= #
".") dom
))
758 val dname
= OS
.Path
.joinDirFile
{dir
= Config
.resultRoot
,
760 val dname
= OS
.Path
.concat (dname
, domPath
)
762 ignore (OS
.Process
.system (Config
.rm ^
" -rf " ^ dname
))
768 app doNode Config
.nodeIps
;
769 app cleanupNode Config
.nodeIps
772 fun homedirOf uname
=
773 Posix
.SysDB
.Passwd
.home (Posix
.SysDB
.getpwnam uname
)
775 fun homedir () = homedirOf (getUser ())