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
.
21 structure Bind
:> BIND
= struct
25 val namedChanged
= ref
false
26 val zoneChanged
= ref
false
28 val didDomain
= ref
""
30 val () = Slave
.registerPreHandler (fn () => (namedChanged
:= false;
34 val dns
: Domain
.files option ref
= ref NONE
36 val _
= Domain
.registerBefore
37 (fn _
=> dns
:= Option
.map (fn node
=> Domain
.domainsFile
{node
= node
,
39 (Domain
.dnsMaster ()))
41 val _
= Domain
.registerAfter
42 (fn _
=> (Option
.app (fn files
=> #close
files ()) (!dns
);
45 val dl
= ErrorMsg
.dummyLoc
49 | CNAME
of string * string
53 | AAAA
of string * string
54 | DefaultAAAA
of string
56 val record
= fn (EApp ((EApp ((EVar
"dnsA", _
), e1
), _
), e2
), _
) =>
57 (case (Env
.string e1
, Domain
.ip e2
) of
58 (SOME v1
, SOME v2
) => SOME (A (v1
, v2
))
60 |
(EApp ((EApp ((EVar
"dnsCNAME", _
), e1
), _
), e2
), _
) =>
61 (case (Env
.string e1
, Env
.string e2
) of
62 (SOME v1
, SOME v2
) => SOME (CNAME (v1
, v2
))
64 |
(EApp ((EApp ((EVar
"dnsMX", _
), e1
), _
), e2
), _
) =>
65 (case (Env
.int e1
, Env
.string e2
) of
66 (SOME v1
, SOME v2
) => SOME (MX (v1
, v2
))
68 |
(EApp ((EVar
"dnsNS", _
), e
), _
) =>
69 Option
.map
NS (Env
.string e
)
70 |
(EApp ((EVar
"dnsDefaultA", _
), e
), _
) =>
71 Option
.map
DefaultA (Domain
.ip e
)
72 |
(EApp ((EApp ((EVar
"dnsAAAA", _
), e1
), _
), e2
), _
) =>
73 (case (Env
.string e1
, Env
.string e2
) of
74 (SOME v1
, SOME v2
) => SOME (AAAA (v1
, v2
))
76 |
(EApp ((EVar
"dnsDefaultAAAA", _
), e
), _
) =>
77 Option
.map
DefaultAAAA (Env
.string e
)
80 fun writeRecord (evs
, r
) =
82 NONE
=> print ("Warning: DNS directive for " ^ Domain
.currentDomain ()
83 ^
" ignored because no master DNS server is configured for this domain\n")
86 fun write s
= #write files s
87 fun writeDom () = #writeDom
files ()
88 val ttl
= Env
.env Env
.int (evs
, "TTL")
91 A (from
, to
) => (write from
;
95 write (Int.toString ttl
);
99 | DefaultA to
=> (writeDom ();
101 write (Int.toString ttl
);
105 |
CNAME (from
, to
) => (write from
;
109 write (Int.toString ttl
);
110 write
"\tIN\tCNAME\t";
113 |
MX (num
, host
) => (writeDom ();
115 write (Int.toString ttl
);
117 write (Int.toString num
);
121 | NS host
=> (writeDom ();
123 write (Int.toString ttl
);
127 |
AAAA (from
, to
) => (write from
;
131 write (Int.toString ttl
);
132 write
"\tIN\tAAAA\t";
135 | DefaultAAAA to
=> (writeDom ();
137 write (Int.toString ttl
);
138 write
"\tIN\tAAAA\t";
143 val () = Env
.actionV_one
"dns"
148 case TextIO.inputLine inf
of
149 NONE
=> raise Fail
"Expected a line for BIND"
150 | SOME s
=> String.substring (s
, 0, size s
- 1)
152 fun readILine inf
= valOf (Int.fromString (readLine inf
))
154 val monthToInt
= fn Date
.Jan
=> 1
169 CharVector
.tabulate (amt
- size s
, fn _
=> ch
) ^ s
175 val date
= Date
.fromTimeUniv (Time
.now ())
177 padBy #
"0" 4 (Int.toString (Date
.year date
))
178 ^ padBy #
"0" 2 (Int.toString (monthToInt (Date
.month date
)))
179 ^ padBy #
"0" 2 (Int.toString (Date
.day date
))
182 val () = Slave
.registerFileHandler (fn fs
=>
184 val {dir
, file
} = OS
.Path
.splitDirFile (#file fs
)
187 if #domain fs
= !didDomain
then
189 else if Slave
.isDelete (#action fs
) then
191 val fname
= OS
.Path
.joinBaseExt
{base
= #domain fs
,
193 val fname
= OS
.Path
.joinDirFile
{dir
= Config
.Bind
.zonePath
,
196 Slave
.shellF ([Config
.rm
, " -f ", fname
],
197 fn cl
=> "Error deleting file: " ^ cl
)
201 val inf
= TextIO.openIn (OS
.Path
.joinDirFile
{dir
= #dir fs
,
203 val kind
= readLine inf
204 val ttl
= readILine inf
205 val ns
= readLine inf
206 val serial
= case readLine inf
of
208 | s
=> Int.fromString s
209 val rf
= readILine inf
210 val ret
= readILine inf
211 val exp
= readILine inf
212 val min
= readILine inf
213 val () = TextIO.closeIn inf
215 val serialPath
= OS
.Path
.joinDirFile
{dir
= Config
.serialDir
,
219 val inf
= TextIO.openIn serialPath
222 before TextIO.closeIn inf
223 end handle IO
.Io
{name
, ...} => NONE
227 SOME n
=> Int.toString n
230 val prefix
= dateString ()
236 if size old
>= 8 andalso
237 String.substring (old
, 0, 8) = prefix
then
238 case Int.fromString (String.extract (old
, 8, NONE
)) of
240 | SOME old
=> padBy #
"0" 2 (Int.toString (old
+1))
245 val outf
= TextIO.openOut serialPath
246 val _
= TextIO.output (outf
, newSerial
)
247 val _
= TextIO.closeOut outf
249 val dns
= OS
.Path
.joinDirFile
{dir
= #dir fs
,
252 val fname
= OS
.Path
.joinBaseExt
{base
= #domain fs
,
254 val fname
= OS
.Path
.joinDirFile
{dir
= Config
.Bind
.zonePath
,
257 val outf
= TextIO.openOut fname
260 TextIO.output (outf
, "$TTL ");
261 TextIO.output (outf
, Int.toString ttl
);
262 TextIO.output (outf
, "\n\n@\tIN\tSOA\t");
263 TextIO.output (outf
, ns
);
264 TextIO.output (outf
, ".\thostmaster.");
265 TextIO.output (outf
, #domain fs
);
266 TextIO.output (outf
, ". ( ");
267 TextIO.output (outf
, newSerial
);
268 TextIO.output (outf
, " ");
269 TextIO.output (outf
, Int.toString rf
);
270 TextIO.output (outf
, " ");
271 TextIO.output (outf
, Int.toString ret
);
272 TextIO.output (outf
, " ");
273 TextIO.output (outf
, Int.toString exp
);
274 TextIO.output (outf
, " ");
275 TextIO.output (outf
, Int.toString min
);
276 TextIO.output (outf
, " )\n\n");
277 TextIO.closeOut outf
;
278 if Posix
.FileSys
.access (dns
, []) then
279 Slave
.shellF ([Config
.cat
, " ", dns
, " >>", fname
],
280 fn cl
=> "Error concatenating file: " ^ cl
)
283 didDomain
:= #domain fs
287 "soa" => dnsChanged ()
288 |
"dns" => dnsChanged ()
289 |
"named.conf" => namedChanged
:= true
293 val () = Slave
.registerPostHandler
295 (if !namedChanged
then
296 Slave
.concatTo (fn s
=> s
= "named.conf") Config
.Bind
.namedConf
299 if !namedChanged
orelse !zoneChanged
then
300 Slave
.shellF ([Config
.Bind
.reload
],
301 fn cl
=> "Error reloading bind with " ^ cl
)
305 val () = Domain
.registerResetLocal (fn () =>
306 ignore (OS
.Process
.system (Config
.rm ^
" -rf /var/domtool/zones/*")))
308 val () = Domain
.registerDescriber (Domain
.considerAll
309 [Domain
.Filename
{filename
= "named.conf",
310 heading
= "named.conf addition:",
312 Domain
.Filename
{filename
= "dns",
313 heading
= "DNS zonefile contents:",