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 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
=> not (Char.isSpace ch
)))
118 val _
= Env
.type_one
"no_newlines"
120 (CharVector
.all (fn ch
=> ch
<> #
"\n" andalso ch
<> #
"\r"))
122 val _
= Env
.type_one
"ip"
126 val _
= Env
.type_one
"host"
130 val _
= Env
.type_one
"domain"
134 val _
= Env
.type_one
"your_domain"
138 val _
= Env
.type_one
"your_domain_host"
142 val _
= Env
.type_one
"user"
146 val _
= Env
.type_one
"group"
150 val _
= Env
.type_one
"your_user"
154 val _
= Env
.type_one
"your_group"
158 val _
= Env
.type_one
"your_path"
162 val _
= Env
.type_one
"node"
166 val _
= Env
.registerFunction ("dns_node_to_node",
170 val _
= Env
.registerFunction ("mail_node_to_node",
175 val dl
= ErrorMsg
.dummyLoc
177 val nsD
= (EString Config
.defaultNs
, dl
)
178 val serialD
= (EVar
"serialAuto", dl
)
179 val refD
= (EInt Config
.defaultRefresh
, dl
)
180 val retD
= (EInt Config
.defaultRetry
, dl
)
181 val expD
= (EInt Config
.defaultExpiry
, dl
)
182 val minD
= (EInt Config
.defaultMinimum
, dl
)
184 val soaD
= multiApp ((EVar
"soa", dl
),
186 [nsD
, serialD
, refD
, retD
, expD
, minD
])
188 val masterD
= (EApp ((EVar
"internalMaster", dl
),
189 (EString Config
.masterNode
, dl
)),
192 val slavesD
= (EList (map (fn s
=> (EString s
, dl
)) Config
.slaveNodes
), dl
)
194 val _
= Defaults
.registerDefault ("Aliases",
195 (TList (TBase
"your_domain", dl
), dl
),
196 (fn () => (EList
[], dl
)))
198 val _
= Defaults
.registerDefault ("Mailbox",
200 (fn () => (EString (getUser ()), dl
)))
202 val _
= Defaults
.registerDefault ("DNS",
203 (TBase
"dnsKind", dl
),
204 (fn () => multiApp ((EVar
"useDns", dl
),
206 [soaD
, masterD
, slavesD
])))
208 val _
= Defaults
.registerDefault ("TTL",
210 (fn () => (EInt Config
.Bind
.defaultTTL
, dl
)))
212 type soa
= {ns
: string,
219 val serial
= fn (EVar
"serialAuto", _
) => SOME NONE
220 |
(EApp ((EVar
"serialConst", _
), n
), _
) => Option
.map
SOME (Env
.int n
)
223 val soa
= fn (EApp ((EApp ((EApp ((EApp ((EApp ((EApp
224 ((EVar
"soa", _
), ns
), _
),
230 (case (Env
.string ns
, serial sl
, Env
.int rf
,
231 Env
.int ret
, Env
.int exp
, Env
.int min
) of
232 (SOME ns
, SOME sl
, SOME rf
,
233 SOME ret
, SOME exp
, SOME min
) =>
244 ExternalMaster
of string
245 | InternalMaster
of string
249 val _
= Env
.registerFunction ("ip_of_node",
250 fn [(EString node
, _
)] => SOME (EString (nodeIp node
), dl
)
253 val master
= fn (EApp ((EVar
"externalMaster", _
), e
), _
) => Option
.map
ExternalMaster (ip e
)
254 |
(EApp ((EVar
"internalMaster", _
), e
), _
) => Option
.map
InternalMaster (Env
.string e
)
258 UseDns
of {soa
: soa
,
260 slaves
: string list
}
263 val dnsKind
= fn (EApp ((EApp ((EApp
264 ((EVar
"useDns", _
), sa
), _
),
267 (case (soa sa
, master mstr
, Env
.list Env
.string slaves
) of
268 (SOME sa
, SOME mstr
, SOME slaves
) =>
269 SOME (UseDns
{soa
= sa
,
273 |
(EVar
"noDns", _
) => SOME NoDns
276 val befores
= ref (fn (_
: string) => ())
277 val afters
= ref (fn (_
: string) => ())
279 fun registerBefore f
=
283 befores
:= (fn x
=> (old x
; f x
))
286 fun registerAfter f
=
290 afters
:= (fn x
=> (old x
; f x
))
293 val globals
= ref (fn () => ())
294 val locals
= ref (fn () => ())
296 fun registerResetGlobal f
=
300 globals
:= (fn x
=> (old x
; f x
))
303 fun registerResetLocal f
=
307 locals
:= (fn x
=> (old x
; f x
))
310 fun resetGlobal () = (!globals ();
311 ignore (OS
.Process
.system (Config
.rm ^
" -rf " ^ Config
.resultRoot ^
"/*")))
312 fun resetLocal () = !locals ()
315 val currentPath
= ref (fn (_
: string) => "")
316 val currentPathAli
= ref (fn (_
: string, _
: string) => "")
320 fun currentDomain () = !current
322 val currentsAli
= ref ([] : string list
)
324 fun currentAliasDomains () = !currentsAli
325 fun currentDomains () = currentDomain () :: currentAliasDomains ()
327 fun domainFile
{node
, name
} = ((*print ("Opening " ^
!currentPath node ^ name ^
"\n");*)
328 TextIO.openOut (!currentPath node ^ name
))
330 type files
= {write
: string -> unit
,
331 writeDom
: unit
-> unit
,
332 close
: unit
-> unit
}
334 fun domainsFile
{node
, name
} =
336 val doms
= currentDomains ()
337 val files
= map (fn dom
=> (dom
, TextIO.openOut (!currentPathAli (dom
, node
) ^ name
))) doms
339 {write
= fn s
=> app (fn (_
, outf
) => TextIO.output (outf
, s
)) files
,
340 writeDom
= fn () => app (fn (dom
, outf
) => TextIO.output (outf
, dom
)) files
,
341 close
= fn () => app (fn (_
, outf
) => TextIO.closeOut outf
) files
}
346 val toks
= String.fields (fn ch
=> ch
= #
".") domain
348 val elems
= foldr (fn (piece
, elems
) =>
350 val elems
= piece
:: elems
354 val path
= String.concatWith
"/"
355 (Config
.resultRoot
:: node
:: rev elems
)
356 val tmpPath
= String.concatWith
"/"
357 (Config
.tmpDir
:: node
:: rev elems
)
359 (if Posix
.FileSys
.ST
.isDir
360 (Posix
.FileSys
.stat path
) then
363 (OS
.FileSys
.remove path
;
364 OS
.FileSys
.mkDir path
))
365 handle OS
.SysErr _
=> OS
.FileSys
.mkDir path
;
367 (if Posix
.FileSys
.ST
.isDir
368 (Posix
.FileSys
.stat tmpPath
) then
371 (OS
.FileSys
.remove tmpPath
;
372 OS
.FileSys
.mkDir tmpPath
))
373 handle OS
.SysErr _
=> OS
.FileSys
.mkDir tmpPath
380 fn (root
, site
) => String.concatWith
"/" (root
:: site
:: rev ("" :: elems
))
383 datatype file_action
' =
384 Add
' of {src
: string, dst
: string}
386 | Modify
' of {src
: string, dst
: string}
388 fun findDiffs (site
, dom
, acts
) =
391 val realPath
= gp (Config
.resultRoot
, site
)
392 val tmpPath
= gp (Config
.tmpDir
, site
)
394 (*val _
= print ("getDiffs(" ^ site ^
", " ^ dom ^
")... " ^ realPath ^
"; " ^ tmpPath ^
"\n")*)
396 val dir
= Posix
.FileSys
.opendir realPath
399 case Posix
.FileSys
.readdir dir
of
400 NONE
=> (Posix
.FileSys
.closedir dir
;
404 val real = OS
.Path
.joinDirFile
{dir
= realPath
,
406 val tmp
= OS
.Path
.joinDirFile
{dir
= tmpPath
,
409 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat
real) then
411 else if Posix
.FileSys
.access (tmp
, []) then
412 if Slave
.shell
[Config
.diff
, " ", real, " ", tmp
] then
415 loopReal ((site
, dom
, realPath
, Modify
' {src
= tmp
, dst
= real}) :: acts
)
417 loopReal ((site
, dom
, realPath
, Delete
' real) :: acts
)
420 val acts
= loopReal acts
422 val dir
= Posix
.FileSys
.opendir tmpPath
425 case Posix
.FileSys
.readdir dir
of
426 NONE
=> (Posix
.FileSys
.closedir dir
;
430 val real = OS
.Path
.joinDirFile
{dir
= realPath
,
432 val tmp
= OS
.Path
.joinDirFile
{dir
= tmpPath
,
435 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat tmp
) then
437 else if Posix
.FileSys
.access (real, []) then
440 loopTmp ((site
, dom
, realPath
, Add
' {src
= tmp
, dst
= real}) :: acts
)
443 val acts
= loopTmp acts
448 fun findAllDiffs () =
450 val dir
= Posix
.FileSys
.opendir Config
.tmpDir
451 val len
= length (String.fields (fn ch
=> ch
= #
"/") Config
.tmpDir
) + 1
453 fun exploreSites diffs
=
454 case Posix
.FileSys
.readdir dir
of
458 fun explore (dname
, diffs
) =
460 val dir
= Posix
.FileSys
.opendir dname
463 case Posix
.FileSys
.readdir dir
of
467 val fname
= OS
.Path
.joinDirFile
{dir
= dname
,
470 loop (if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat fname
) then
472 val dom
= String.fields (fn ch
=> ch
= #
"/") fname
473 val dom
= List.drop (dom
, len
)
474 val dom
= String.concatWith
"." (rev dom
)
476 val dname
' = OS
.Path
.joinDirFile
{dir
= dname
,
480 findDiffs (site
, dom
, diffs
))
487 before Posix
.FileSys
.closedir dir
490 exploreSites (explore (OS
.Path
.joinDirFile
{dir
= Config
.tmpDir
,
491 file
= site
}, diffs
))
495 before Posix
.FileSys
.closedir dir
498 val masterNode
: string option ref
= ref NONE
499 fun dnsMaster () = !masterNode
501 val _
= Env
.containerV_one
"domain"
502 ("domain", Env
.string)
505 val kind
= Env
.env
dnsKind (evs
, "DNS")
506 val ttl
= Env
.env Env
.int (evs
, "TTL")
507 val aliases
= Env
.env (Env
.list Env
.string) (evs
, "Aliases")
509 val path
= getPath dom
511 val () = (current
:= dom
;
512 currentsAli
:= Slave
.remove (Slave
.removeDups aliases
, dom
);
513 currentPath
:= (fn site
=> path (Config
.tmpDir
, site
));
514 currentPathAli
:= (fn (dom
, site
) => getPath
dom (Config
.tmpDir
, site
)))
516 fun saveSoa (kind
, soa
: soa
) node
=
518 val {write
, writeDom
, close
} = domainsFile
{node
= node
, name
= "soa"}
522 write (Int.toString ttl
);
528 | SOME n
=> write (Int.toString n
);
530 write (Int.toString (#ref soa
));
532 write (Int.toString (#ret soa
));
534 write (Int.toString (#exp soa
));
536 write (Int.toString (#min soa
));
541 fun saveNamed (kind
, soa
: soa
, masterIp
) node
=
542 if dom
= "localhost" then
545 val {write
, writeDom
, close
} = domainsFile
{node
= node
, name
= "named.conf"}
549 write
"\" IN {\n\ttype ";
551 write
";\n\tfile \"";
552 write Config
.Bind
.zonePath_real
;
557 "master" => write
"\tallow-update { none; };\n"
558 | _
=> (write
"\tmasters { ";
566 NoDns
=> masterNode
:= NONE
571 InternalMaster node
=> valOf (SM
.find (nodeMap
, node
))
572 | ExternalMaster ip
=> ip
574 app (saveSoa ("slave", #soa dns
)) (#slaves dns
);
575 app (saveNamed ("slave", #soa dns
, masterIp
)) (#slaves dns
);
577 InternalMaster node
=>
578 (masterNode
:= SOME node
;
579 saveSoa ("master", #soa dns
) node
;
580 saveNamed ("master", #soa dns
, masterIp
) node
)
581 | _
=> masterNode
:= NONE
;
585 fn () => !afters (!current
))
587 val () = Env
.registerPre (fn () => (ignore (Slave
.shellF ([Config
.rm
, " -rf ", Config
.tmpDir
, ""],
588 fn cl
=> "Temp file cleanup failed: " ^ cl
));
589 OS
.FileSys
.mkDir Config
.tmpDir
;
590 app (fn node
=> OS
.FileSys
.mkDir
591 (OS
.Path
.joinDirFile
{dir
= Config
.tmpDir
,
594 app (fn node
=> OS
.FileSys
.mkDir
595 (OS
.Path
.joinDirFile
{dir
= Config
.resultRoot
,
597 handle OS
.SysErr _
=> ())
600 fun handleSite (site
, files
) =
604 print ("New configuration for node " ^ site ^
"\n");
605 if site
= Config
.defaultNode
then
606 Slave
.handleChanges files
608 val bio
= OpenSSL
.connect (valOf (!ssl_context
),
611 ^
Int.toString Config
.slavePort
)
613 app (fn file
=> Msg
.send (bio
, MsgFile file
)) files
;
614 Msg
.send (bio
, MsgDoFiles
);
616 NONE
=> print
"Slave closed connection unexpectedly\n"
619 MsgOk
=> print ("Slave " ^ site ^
" finished\n")
620 | MsgError s
=> print ("Slave " ^ site
621 ^
" returned error: " ^
623 | _
=> print ("Slave " ^ site
624 ^
" returned unexpected command\n");
629 val () = Env
.registerPost (fn () =>
631 val diffs
= findAllDiffs ()
633 val diffs
= map (fn (site
, dom
, dir
, Add
' {src
, dst
}) =>
634 (Slave
.shellF ([Config
.cp
, " ", src
, " ", dst
],
635 fn cl
=> "Copy failed: " ^ cl
);
641 |
(site
, dom
, dir
, Delete
' dst
) =>
642 (OS
.FileSys
.remove dst
643 handle OS
.SysErr _
=>
644 ErrorMsg
.error
NONE ("Delete failed for " ^ dst
);
646 {action
= Slave
.Delete
,
650 |
(site
, dom
, dir
, Modify
' {src
, dst
}) =>
651 (Slave
.shellF ([Config
.cp
, " ", src
, " ", dst
],
652 fn cl
=> "Copy failed: " ^ cl
);
654 {action
= Slave
.Modify
,
659 if !ErrorMsg
.anyErrors
then
662 val changed
= foldl (fn ((site
, file
), changed
) =>
664 val ls
= case SM
.find (changed
, site
) of
668 SM
.insert (changed
, site
, file
:: ls
)
671 SM
.appi handleSite changed
673 ignore (Slave
.shellF ([Config
.rm
, " -rf ", Config
.tmpDir
, ""],
674 fn cl
=> "Temp file cleanup failed: " ^ cl
))
677 fun hasPriv priv
= Acl
.query
{user
= getUser (), class
= "priv", value
= "all"}
678 orelse Acl
.query
{user
= getUser (), class
= "priv", value
= priv
}
680 val _
= Env
.type_one
"dns_node"
683 List.exists (fn x
=> x
= node
) Config
.dnsNodes_all
684 orelse (hasPriv
"dns"
685 andalso List.exists (fn x
=> x
= node
) Config
.dnsNodes_admin
))
687 val _
= Env
.type_one
"mail_node"
690 List.exists (fn x
=> x
= node
) Config
.mailNodes_all
691 orelse (hasPriv
"mail"
692 andalso List.exists (fn x
=> x
= node
) Config
.mailNodes_admin
))
696 fun doNode (node
, _
) =
698 val dname
= OS
.Path
.joinDirFile
{dir
= Config
.resultRoot
,
701 fun doDom (dom
, actions
) =
703 val domPath
= String.concatWith
"/" (rev (String.fields (fn ch
=> ch
= #
".") dom
))
704 val dname
= OS
.Path
.concat (dname
, domPath
)
706 fun visitDom (dom
, dname
, actions
) =
708 val dir
= Posix
.FileSys
.opendir dname
711 case Posix
.FileSys
.readdir dir
of
715 val fnameFull
= OS
.Path
.joinDirFile
{dir
= dname
,
718 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat fnameFull
) then
719 loop (visitDom (fname ^
"." ^ dom
,
723 loop ({action
= Slave
.Delete
,
726 file
= fnameFull
} :: actions
)
730 before Posix
.FileSys
.closedir dir
732 handle OS
.SysErr _
=>
733 (print ("Warning: System error deleting domain " ^ dom ^
" on " ^ node ^
".\n");
736 visitDom (dom
, dname
, actions
)
739 val actions
= foldl doDom
[] doms
741 handleSite (node
, actions
)
743 handle IO
.Io _
=> print ("Warning: IO error deleting domains on " ^ node ^
".\n")
745 fun cleanupNode (node
, _
) =
749 val domPath
= String.concatWith
"/" (rev (String.fields (fn ch
=> ch
= #
".") dom
))
750 val dname
= OS
.Path
.joinDirFile
{dir
= Config
.resultRoot
,
752 val dname
= OS
.Path
.concat (dname
, domPath
)
754 ignore (OS
.Process
.system (Config
.rm ^
" -rf " ^ dname
))
760 app doNode Config
.nodeIps
;
761 app cleanupNode Config
.nodeIps
764 fun homedirOf uname
=
765 Posix
.SysDB
.Passwd
.home (Posix
.SysDB
.getpwnam uname
)
767 fun homedir () = homedirOf (getUser ())