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
54 | CNAME
of host
* string
57 | AAAA
of host
* string
58 | TXT
of host
* string
60 | SRV
of string * int * int * int * string
62 fun hostS (Literal s
) = s ^
"."
63 | hostS Wildcard
= "*."
66 val host
= fn (EApp ((EVar
"literal", _
), e
), _
) =>
67 Option
.map
Literal (Env
.string e
)
68 |
(EVar
"wildcard", _
) =>
70 |
(EVar
"default", _
) =>
74 val record
= fn (EApp ((EApp ((EVar
"dnsA", _
), e1
), _
), e2
), _
) =>
75 (case (host e1
, Domain
.ip e2
) of
76 (SOME v1
, SOME v2
) => SOME (A (v1
, v2
))
78 |
(EApp ((EApp ((EVar
"dnsCNAME", _
), e1
), _
), e2
), _
) =>
79 (case (host e1
, Env
.string e2
) of
80 (SOME v1
, SOME v2
) => SOME (CNAME (v1
, v2
))
82 |
(EApp ((EApp ((EVar
"dnsMX", _
), e1
), _
), e2
), _
) =>
83 (case (Env
.int e1
, Env
.string e2
) of
84 (SOME v1
, SOME v2
) => SOME (MX (v1
, v2
))
86 |
(EApp ((EVar
"dnsNS", _
), e
), _
) =>
87 Option
.map
NS (Env
.string e
)
88 |
(EApp ((EApp ((EVar
"dnsAAAA", _
), e1
), _
), e2
), _
) =>
89 (case (host e1
, Env
.string e2
) of
90 (SOME v1
, SOME v2
) => SOME (AAAA (v1
, v2
))
92 |
(EApp ((EApp ((EVar
"dnsTXT", _
), e1
), _
), e2
), _
) =>
93 (case (host e1
, Env
.string e2
) of
94 (SOME v1
, SOME v2
) => SOME (TXT (v1
, v2
))
96 |
(EApp ((EVar
"dnsAFSDB", _
), e
), _
) =>
97 Option
.map
AFSDB (Env
.string e
)
98 |
(EApp ((EApp ((EApp ((EApp ((EApp ((EVar
"dnsSRV", _
), e1
), _
), e2
), _
), e3
), _
), e4
), _
), e5
), _
) =>
99 (case (Env
.string e1
, Env
.int e2
, Env
.int e3
, Env
.int e4
, Env
.string e5
) of
100 (SOME v1
, SOME v2
, SOME v3
, SOME v4
, SOME v5
) => SOME (SRV (v1
, v2
, v3
, v4
, v5
))
104 fun writeRecord (evs
, r
) =
106 NONE
=> print ("Warning: DNS directive for " ^ Domain
.currentDomain ()
107 ^
" ignored because no master DNS server is configured for this domain\n")
110 fun write s
= #write files s
111 fun writeDom () = #writeDom
files ()
112 val ttl
= Env
.env Env
.int (evs
, "TTL")
115 A (from
, to
) => (write (hostS from
);
118 write (Int.toString ttl
);
122 |
CNAME (from
, to
) => (write (hostS 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 (hostS from
);
146 write (Int.toString ttl
);
147 write
"\tIN\tAAAA\t";
150 |
TXT (from
, to
) => (write (hostS from
);
153 write (Int.toString ttl
);
154 write
"\tIN\tTXT\t\"";
155 write (String.translate (fn #
"\"" => "\\\"" | ch
=> str ch
) to
);
157 | AFSDB host
=> (writeDom ();
159 write (Int.toString ttl
);
160 write
"\tIN\tAFSDB\t";
165 |
SRV (from
, priority
, weight
, port
, to
) => (write from
;
169 write (Int.toString ttl
);
171 write (Int.toString priority
);
173 write (Int.toString weight
);
175 write (Int.toString port
);
181 val () = Env
.actionV_one
"dns"
186 case TextIO.inputLine inf
of
187 NONE
=> raise Fail
"Expected a line for BIND"
188 | SOME s
=> String.substring (s
, 0, size s
- 1)
190 fun readILine inf
= valOf (Int.fromString (readLine inf
))
192 val monthToInt
= fn Date
.Jan
=> 1
207 CharVector
.tabulate (amt
- size s
, fn _
=> ch
) ^ s
213 val date
= Date
.fromTimeUniv (Time
.now ())
215 padBy #
"0" 4 (Int.toString (Date
.year date
))
216 ^ padBy #
"0" 2 (Int.toString (monthToInt (Date
.month date
)))
217 ^ padBy #
"0" 2 (Int.toString (Date
.day date
))
220 val () = Slave
.registerFileHandler (fn fs
=>
222 val {dir
, file
} = OS
.Path
.splitDirFile (#file fs
)
225 if #domain fs
= !didDomain
then
227 else if Slave
.isDelete (#action fs
) then
229 val fname
= OS
.Path
.joinBaseExt
{base
= #domain fs
,
231 val fname
= OS
.Path
.joinDirFile
{dir
= Config
.Bind
.zonePath
,
234 Slave
.shellF ([Config
.rm
, " -f ", fname
],
235 fn cl
=> "Error deleting file: " ^ cl
)
239 val inf
= TextIO.openIn (OS
.Path
.joinDirFile
{dir
= #dir fs
,
241 val kind
= readLine inf
242 val ttl
= readILine inf
243 val ns
= readLine inf
244 val serial
= case readLine inf
of
246 | s
=> Int.fromString s
247 val rf
= readILine inf
248 val ret
= readILine inf
249 val exp
= readILine inf
250 val min
= readILine inf
251 val () = TextIO.closeIn inf
253 val serialPath
= OS
.Path
.joinDirFile
{dir
= Config
.serialDir
,
257 val inf
= TextIO.openIn serialPath
260 before TextIO.closeIn inf
261 end handle IO
.Io
{name
, ...} => NONE
265 SOME n
=> Int.toString n
268 val prefix
= dateString ()
274 if size old
>= 8 andalso
275 String.substring (old
, 0, 8) = prefix
then
276 case Int.fromString (String.extract (old
, 8, NONE
)) of
278 | SOME old
=> padBy #
"0" 2 (Int.toString (old
+1))
283 val outf
= TextIO.openOut serialPath
284 val _
= TextIO.output (outf
, newSerial
)
285 val _
= TextIO.closeOut outf
287 val dns
= OS
.Path
.joinDirFile
{dir
= #dir fs
,
290 val fname
= OS
.Path
.joinBaseExt
{base
= #domain fs
,
292 val fname
= OS
.Path
.joinDirFile
{dir
= Config
.Bind
.zonePath
,
295 val outf
= TextIO.openOut fname
298 TextIO.output (outf
, "$TTL ");
299 TextIO.output (outf
, Int.toString ttl
);
300 TextIO.output (outf
, "\n\n@\tIN\tSOA\t");
301 TextIO.output (outf
, ns
);
302 TextIO.output (outf
, ".\thostmaster.");
303 TextIO.output (outf
, #domain fs
);
304 TextIO.output (outf
, ". ( ");
305 TextIO.output (outf
, newSerial
);
306 TextIO.output (outf
, " ");
307 TextIO.output (outf
, Int.toString rf
);
308 TextIO.output (outf
, " ");
309 TextIO.output (outf
, Int.toString ret
);
310 TextIO.output (outf
, " ");
311 TextIO.output (outf
, Int.toString exp
);
312 TextIO.output (outf
, " ");
313 TextIO.output (outf
, Int.toString min
);
314 TextIO.output (outf
, " )\n\n");
315 TextIO.closeOut outf
;
316 if Posix
.FileSys
.access (dns
, []) then
317 Slave
.shellF ([Config
.cat
, " ", dns
, " >>", fname
],
318 fn cl
=> "Error concatenating file: " ^ cl
)
321 didDomain
:= #domain fs
325 "soa" => dnsChanged ()
326 |
"dns" => dnsChanged ()
327 |
"named.conf" => namedChanged
:= true
331 val () = Slave
.registerPostHandler
333 (if !namedChanged
then
334 Slave
.concatTo (fn s
=> s
= "named.conf") Config
.Bind
.namedConf
337 if !namedChanged
orelse !zoneChanged
then
338 Slave
.shellF ([Config
.Bind
.reload
],
339 fn cl
=> "Error reloading bind with " ^ cl
)
343 val () = Domain
.registerResetLocal (fn () =>
344 ignore (OS
.Process
.system (Config
.rm ^
" -rf /var/domtool/zones/*")))
346 val () = Domain
.registerDescriber (Domain
.considerAll
347 [Domain
.Filename
{filename
= "named.conf",
348 heading
= "named.conf addition:",
350 Domain
.Filename
{filename
= "dns",
351 heading
= "DNS zonefile contents:",
355 size s
> 0 andalso size s
< 20
356 andalso CharVector
.all (fn ch
=> Domain
.isIdent ch
orelse ch
= #
"-" orelse ch
= #
"_") s
358 fun validSRVDomain s
=
359 size s
> 0 andalso size s
< 100
360 andalso List.all
validHost_ (String.fields (fn ch
=> ch
= #
".") s
)
362 val _
= Env
.type_one
"srv_domain"