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
56 | DefaultTXT
of string
58 | SRV
of string * int * int * int * string
60 val record
= fn (EApp ((EApp ((EVar
"dnsA", _
), e1
), _
), e2
), _
) =>
61 (case (Env
.string e1
, Domain
.ip e2
) of
62 (SOME v1
, SOME v2
) => SOME (A (v1
, v2
))
64 |
(EApp ((EApp ((EVar
"dnsCNAME", _
), e1
), _
), e2
), _
) =>
65 (case (Env
.string e1
, Env
.string e2
) of
66 (SOME v1
, SOME v2
) => SOME (CNAME (v1
, v2
))
68 |
(EApp ((EApp ((EVar
"dnsMX", _
), e1
), _
), e2
), _
) =>
69 (case (Env
.int e1
, Env
.string e2
) of
70 (SOME v1
, SOME v2
) => SOME (MX (v1
, v2
))
72 |
(EApp ((EVar
"dnsNS", _
), e
), _
) =>
73 Option
.map
NS (Env
.string e
)
74 |
(EApp ((EVar
"dnsDefaultA", _
), e
), _
) =>
75 Option
.map
DefaultA (Domain
.ip e
)
76 |
(EApp ((EApp ((EVar
"dnsAAAA", _
), e1
), _
), e2
), _
) =>
77 (case (Env
.string e1
, Env
.string e2
) of
78 (SOME v1
, SOME v2
) => SOME (AAAA (v1
, v2
))
80 |
(EApp ((EVar
"dnsDefaultAAAA", _
), e
), _
) =>
81 Option
.map
DefaultAAAA (Env
.string e
)
82 |
(EApp ((EApp ((EVar
"dnsTXT", _
), e1
), _
), e2
), _
) =>
83 (case (Env
.string e1
, Env
.string e2
) of
84 (SOME v1
, SOME v2
) => SOME (TXT (v1
, v2
))
86 |
(EApp ((EVar
"dnsDefaultTXT", _
), e
), _
) =>
87 Option
.map
DefaultTXT (Env
.string e
)
88 |
(EApp ((EVar
"dnsAFSDB", _
), e
), _
) =>
89 Option
.map
AFSDB (Env
.string e
)
90 |
(EApp ((EApp ((EApp ((EApp ((EApp ((EVar
"dnsSRV", _
), e1
), _
), e2
), _
), e3
), _
), e4
), _
), e5
), _
) =>
91 (case (Env
.string e1
, Env
.int e2
, Env
.int e3
, Env
.int e4
, Env
.string e5
) of
92 (SOME v1
, SOME v2
, SOME v3
, SOME v4
, SOME v5
) => SOME (SRV (v1
, v2
, v3
, v4
, v5
))
96 fun writeRecord (evs
, r
) =
98 NONE
=> print ("Warning: DNS directive for " ^ Domain
.currentDomain ()
99 ^
" ignored because no master DNS server is configured for this domain\n")
102 fun write s
= #write files s
103 fun writeDom () = #writeDom
files ()
104 val ttl
= Env
.env Env
.int (evs
, "TTL")
107 A (from
, to
) => (write from
;
111 write (Int.toString ttl
);
115 | DefaultA to
=> (writeDom ();
117 write (Int.toString ttl
);
121 |
CNAME (from
, to
) => (write from
;
125 write (Int.toString ttl
);
126 write
"\tIN\tCNAME\t";
129 |
MX (num
, host
) => (writeDom ();
131 write (Int.toString ttl
);
133 write (Int.toString num
);
137 | NS host
=> (writeDom ();
139 write (Int.toString ttl
);
143 |
AAAA (from
, to
) => (write from
;
147 write (Int.toString ttl
);
148 write
"\tIN\tAAAA\t";
151 | DefaultAAAA to
=> (writeDom ();
153 write (Int.toString ttl
);
154 write
"\tIN\tAAAA\t";
157 |
TXT (from
, to
) => (write from
;
161 write (Int.toString ttl
);
162 write
"\tIN\tTXT\t\"";
163 write (String.translate (fn #
"\"" => "\\\"" | ch
=> str ch
) to
);
165 | DefaultTXT to
=> (writeDom ();
167 write (Int.toString ttl
);
168 write
"\tIN\tTXT\t\"";
169 write (String.translate (fn #
"\"" => "\\\"" | ch
=> str ch
) to
);
171 | AFSDB host
=> (writeDom ();
173 write (Int.toString ttl
);
174 write
"\tIN\tAFSDB\t";
179 |
SRV (from
, priority
, weight
, port
, to
) => (write from
;
183 write (Int.toString ttl
);
185 write (Int.toString priority
);
187 write (Int.toString weight
);
189 write (Int.toString port
);
195 val () = Env
.actionV_one
"dns"
200 case TextIO.inputLine inf
of
201 NONE
=> raise Fail
"Expected a line for BIND"
202 | SOME s
=> String.substring (s
, 0, size s
- 1)
204 fun readILine inf
= valOf (Int.fromString (readLine inf
))
206 val monthToInt
= fn Date
.Jan
=> 1
221 CharVector
.tabulate (amt
- size s
, fn _
=> ch
) ^ s
227 val date
= Date
.fromTimeUniv (Time
.now ())
229 padBy #
"0" 4 (Int.toString (Date
.year date
))
230 ^ padBy #
"0" 2 (Int.toString (monthToInt (Date
.month date
)))
231 ^ padBy #
"0" 2 (Int.toString (Date
.day date
))
234 val () = Slave
.registerFileHandler (fn fs
=>
236 val {dir
, file
} = OS
.Path
.splitDirFile (#file fs
)
239 if #domain fs
= !didDomain
then
241 else if Slave
.isDelete (#action fs
) then
243 val fname
= OS
.Path
.joinBaseExt
{base
= #domain fs
,
245 val fname
= OS
.Path
.joinDirFile
{dir
= Config
.Bind
.zonePath
,
248 Slave
.shellF ([Config
.rm
, " -f ", fname
],
249 fn cl
=> "Error deleting file: " ^ cl
)
253 val inf
= TextIO.openIn (OS
.Path
.joinDirFile
{dir
= #dir fs
,
255 val kind
= readLine inf
256 val ttl
= readILine inf
257 val ns
= readLine inf
258 val serial
= case readLine inf
of
260 | s
=> Int.fromString s
261 val rf
= readILine inf
262 val ret
= readILine inf
263 val exp
= readILine inf
264 val min
= readILine inf
265 val () = TextIO.closeIn inf
267 val serialPath
= OS
.Path
.joinDirFile
{dir
= Config
.serialDir
,
271 val inf
= TextIO.openIn serialPath
274 before TextIO.closeIn inf
275 end handle IO
.Io
{name
, ...} => NONE
279 SOME n
=> Int.toString n
282 val prefix
= dateString ()
288 if size old
>= 8 andalso
289 String.substring (old
, 0, 8) = prefix
then
290 case Int.fromString (String.extract (old
, 8, NONE
)) of
292 | SOME old
=> padBy #
"0" 2 (Int.toString (old
+1))
297 val outf
= TextIO.openOut serialPath
298 val _
= TextIO.output (outf
, newSerial
)
299 val _
= TextIO.closeOut outf
301 val dns
= OS
.Path
.joinDirFile
{dir
= #dir fs
,
304 val fname
= OS
.Path
.joinBaseExt
{base
= #domain fs
,
306 val fname
= OS
.Path
.joinDirFile
{dir
= Config
.Bind
.zonePath
,
309 val outf
= TextIO.openOut fname
312 TextIO.output (outf
, "$TTL ");
313 TextIO.output (outf
, Int.toString ttl
);
314 TextIO.output (outf
, "\n\n@\tIN\tSOA\t");
315 TextIO.output (outf
, ns
);
316 TextIO.output (outf
, ".\thostmaster.");
317 TextIO.output (outf
, #domain fs
);
318 TextIO.output (outf
, ". ( ");
319 TextIO.output (outf
, newSerial
);
320 TextIO.output (outf
, " ");
321 TextIO.output (outf
, Int.toString rf
);
322 TextIO.output (outf
, " ");
323 TextIO.output (outf
, Int.toString ret
);
324 TextIO.output (outf
, " ");
325 TextIO.output (outf
, Int.toString exp
);
326 TextIO.output (outf
, " ");
327 TextIO.output (outf
, Int.toString min
);
328 TextIO.output (outf
, " )\n\n");
329 TextIO.closeOut outf
;
330 if Posix
.FileSys
.access (dns
, []) then
331 Slave
.shellF ([Config
.cat
, " ", dns
, " >>", fname
],
332 fn cl
=> "Error concatenating file: " ^ cl
)
335 didDomain
:= #domain fs
339 "soa" => dnsChanged ()
340 |
"dns" => dnsChanged ()
341 |
"named.conf" => namedChanged
:= true
345 val () = Slave
.registerPostHandler
347 (if !namedChanged
then
348 Slave
.concatTo (fn s
=> s
= "named.conf") Config
.Bind
.namedConf
351 if !namedChanged
orelse !zoneChanged
then
352 Slave
.shellF ([Config
.Bind
.reload
],
353 fn cl
=> "Error reloading bind with " ^ cl
)
357 val () = Domain
.registerResetLocal (fn () =>
358 ignore (OS
.Process
.system (Config
.rm ^
" -rf /var/domtool/zones/*")))
360 val () = Domain
.registerDescriber (Domain
.considerAll
361 [Domain
.Filename
{filename
= "named.conf",
362 heading
= "named.conf addition:",
364 Domain
.Filename
{filename
= "dns",
365 heading
= "DNS zonefile contents:",
369 size s
> 0 andalso size s
< 20
370 andalso CharVector
.all (fn ch
=> Domain
.isIdent ch
orelse ch
= #
"-" orelse ch
= #
"_") s
372 fun validSRVDomain s
=
373 size s
> 0 andalso size s
< 100
374 andalso List.all
validHost_ (String.fields (fn ch
=> ch
= #
".") s
)
376 val _
= Env
.type_one
"srv_domain"