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
55 | TXT
of string * string
57 val record
= fn (EApp ((EApp ((EVar
"dnsA", _
), e1
), _
), e2
), _
) =>
58 (case (Env
.string e1
, Domain
.ip e2
) of
59 (SOME v1
, SOME v2
) => SOME (A (v1
, v2
))
61 |
(EApp ((EApp ((EVar
"dnsCNAME", _
), e1
), _
), e2
), _
) =>
62 (case (Env
.string e1
, Env
.string e2
) of
63 (SOME v1
, SOME v2
) => SOME (CNAME (v1
, v2
))
65 |
(EApp ((EApp ((EVar
"dnsMX", _
), e1
), _
), e2
), _
) =>
66 (case (Env
.int e1
, Env
.string e2
) of
67 (SOME v1
, SOME v2
) => SOME (MX (v1
, v2
))
69 |
(EApp ((EVar
"dnsNS", _
), e
), _
) =>
70 Option
.map
NS (Env
.string e
)
71 |
(EApp ((EVar
"dnsDefaultA", _
), e
), _
) =>
72 Option
.map
DefaultA (Domain
.ip e
)
73 |
(EApp ((EApp ((EVar
"dnsAAAA", _
), e1
), _
), e2
), _
) =>
74 (case (Env
.string e1
, Env
.string e2
) of
75 (SOME v1
, SOME v2
) => SOME (AAAA (v1
, v2
))
77 |
(EApp ((EVar
"dnsDefaultAAAA", _
), e
), _
) =>
78 Option
.map
DefaultAAAA (Env
.string e
)
79 |
(EApp ((EApp ((EVar
"dnsTXT", _
), e1
), _
), e2
), _
) =>
80 (case (Env
.string e1
, Env
.string e2
) of
81 (SOME v1
, SOME v2
) => SOME (TXT (v1
, v2
))
85 fun writeRecord (evs
, r
) =
87 NONE
=> print ("Warning: DNS directive for " ^ Domain
.currentDomain ()
88 ^
" ignored because no master DNS server is configured for this domain\n")
91 fun write s
= #write files s
92 fun writeDom () = #writeDom
files ()
93 val ttl
= Env
.env Env
.int (evs
, "TTL")
96 A (from
, to
) => (write from
;
100 write (Int.toString ttl
);
104 | DefaultA to
=> (writeDom ();
106 write (Int.toString ttl
);
110 |
CNAME (from
, to
) => (write from
;
114 write (Int.toString ttl
);
115 write
"\tIN\tCNAME\t";
118 |
MX (num
, host
) => (writeDom ();
120 write (Int.toString ttl
);
122 write (Int.toString num
);
126 | NS host
=> (writeDom ();
128 write (Int.toString ttl
);
132 |
AAAA (from
, to
) => (write from
;
136 write (Int.toString ttl
);
137 write
"\tIN\tAAAA\t";
140 | DefaultAAAA to
=> (writeDom ();
142 write (Int.toString ttl
);
143 write
"\tIN\tAAAA\t";
146 |
TXT (from
, to
) => (write from
;
150 write (Int.toString ttl
);
151 write
"\tIN\tTXT\t\"";
152 write (String.translate (fn #
"\"" => "\\\"" | ch
=> str ch
) to
);
156 val () = Env
.actionV_one
"dns"
161 case TextIO.inputLine inf
of
162 NONE
=> raise Fail
"Expected a line for BIND"
163 | SOME s
=> String.substring (s
, 0, size s
- 1)
165 fun readILine inf
= valOf (Int.fromString (readLine inf
))
167 val monthToInt
= fn Date
.Jan
=> 1
182 CharVector
.tabulate (amt
- size s
, fn _
=> ch
) ^ s
188 val date
= Date
.fromTimeUniv (Time
.now ())
190 padBy #
"0" 4 (Int.toString (Date
.year date
))
191 ^ padBy #
"0" 2 (Int.toString (monthToInt (Date
.month date
)))
192 ^ padBy #
"0" 2 (Int.toString (Date
.day date
))
195 val () = Slave
.registerFileHandler (fn fs
=>
197 val {dir
, file
} = OS
.Path
.splitDirFile (#file fs
)
200 if #domain fs
= !didDomain
then
202 else if Slave
.isDelete (#action fs
) then
204 val fname
= OS
.Path
.joinBaseExt
{base
= #domain fs
,
206 val fname
= OS
.Path
.joinDirFile
{dir
= Config
.Bind
.zonePath
,
209 Slave
.shellF ([Config
.rm
, " -f ", fname
],
210 fn cl
=> "Error deleting file: " ^ cl
)
214 val inf
= TextIO.openIn (OS
.Path
.joinDirFile
{dir
= #dir fs
,
216 val kind
= readLine inf
217 val ttl
= readILine inf
218 val ns
= readLine inf
219 val serial
= case readLine inf
of
221 | s
=> Int.fromString s
222 val rf
= readILine inf
223 val ret
= readILine inf
224 val exp
= readILine inf
225 val min
= readILine inf
226 val () = TextIO.closeIn inf
228 val serialPath
= OS
.Path
.joinDirFile
{dir
= Config
.serialDir
,
232 val inf
= TextIO.openIn serialPath
235 before TextIO.closeIn inf
236 end handle IO
.Io
{name
, ...} => NONE
240 SOME n
=> Int.toString n
243 val prefix
= dateString ()
249 if size old
>= 8 andalso
250 String.substring (old
, 0, 8) = prefix
then
251 case Int.fromString (String.extract (old
, 8, NONE
)) of
253 | SOME old
=> padBy #
"0" 2 (Int.toString (old
+1))
258 val outf
= TextIO.openOut serialPath
259 val _
= TextIO.output (outf
, newSerial
)
260 val _
= TextIO.closeOut outf
262 val dns
= OS
.Path
.joinDirFile
{dir
= #dir fs
,
265 val fname
= OS
.Path
.joinBaseExt
{base
= #domain fs
,
267 val fname
= OS
.Path
.joinDirFile
{dir
= Config
.Bind
.zonePath
,
270 val outf
= TextIO.openOut fname
273 TextIO.output (outf
, "$TTL ");
274 TextIO.output (outf
, Int.toString ttl
);
275 TextIO.output (outf
, "\n\n@\tIN\tSOA\t");
276 TextIO.output (outf
, ns
);
277 TextIO.output (outf
, ".\thostmaster.");
278 TextIO.output (outf
, #domain fs
);
279 TextIO.output (outf
, ". ( ");
280 TextIO.output (outf
, newSerial
);
281 TextIO.output (outf
, " ");
282 TextIO.output (outf
, Int.toString rf
);
283 TextIO.output (outf
, " ");
284 TextIO.output (outf
, Int.toString ret
);
285 TextIO.output (outf
, " ");
286 TextIO.output (outf
, Int.toString exp
);
287 TextIO.output (outf
, " ");
288 TextIO.output (outf
, Int.toString min
);
289 TextIO.output (outf
, " )\n\n");
290 TextIO.closeOut outf
;
291 if Posix
.FileSys
.access (dns
, []) then
292 Slave
.shellF ([Config
.cat
, " ", dns
, " >>", fname
],
293 fn cl
=> "Error concatenating file: " ^ cl
)
296 didDomain
:= #domain fs
300 "soa" => dnsChanged ()
301 |
"dns" => dnsChanged ()
302 |
"named.conf" => namedChanged
:= true
306 val () = Slave
.registerPostHandler
308 (if !namedChanged
then
309 Slave
.concatTo (fn s
=> s
= "named.conf") Config
.Bind
.namedConf
312 if !namedChanged
orelse !zoneChanged
then
313 Slave
.shellF ([Config
.Bind
.reload
],
314 fn cl
=> "Error reloading bind with " ^ cl
)
318 val () = Domain
.registerResetLocal (fn () =>
319 ignore (OS
.Process
.system (Config
.rm ^
" -rf /var/domtool/zones/*")))
321 val () = Domain
.registerDescriber (Domain
.considerAll
322 [Domain
.Filename
{filename
= "named.conf",
323 heading
= "named.conf addition:",
325 Domain
.Filename
{filename
= "dns",
326 heading
= "DNS zonefile contents:",