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
: TextIO.outstream option ref
= ref NONE
36 val _
= Domain
.registerBefore
37 (fn _
=> dns
:= Option
.map (fn node
=> Domain
.domainFile
{node
= node
,
39 (Domain
.dnsMaster ()))
41 val _
= Domain
.registerAfter
42 (fn _
=> (Option
.app
TextIO.closeOut (!dns
);
45 val dl
= ErrorMsg
.dummyLoc
49 | CNAME
of string * string
53 val record
= fn (EApp ((EApp ((EVar
"dnsA", _
), e1
), _
), e2
), _
) =>
54 (case (Env
.string e1
, Domain
.ip e2
) of
55 (SOME v1
, SOME v2
) => SOME (A (v1
, v2
))
57 |
(EApp ((EApp ((EVar
"dnsCNAME", _
), e1
), _
), e2
), _
) =>
58 (case (Env
.string e1
, Env
.string e2
) of
59 (SOME v1
, SOME v2
) => SOME (CNAME (v1
, v2
))
61 |
(EApp ((EApp ((EVar
"dnsMX", _
), e1
), _
), e2
), _
) =>
62 (case (Env
.int e1
, Env
.string e2
) of
63 (SOME v1
, SOME v2
) => SOME (MX (v1
, v2
))
65 |
(EApp ((EVar
"dnsNS", _
), e
), _
) =>
66 Option
.map
NS (Env
.string e
)
69 fun writeRecord (evs
, r
) =
71 NONE
=> print
"Warning: DNS directive ignored because no master DNS server is configured for this domain\n"
74 fun write s
= TextIO.output (file
, s
)
75 val ttl
= Env
.env Env
.int (evs
, "TTL")
78 A (from
, to
) => (write from
;
80 write (Domain
.currentDomain ());
82 write (Int.toString ttl
);
86 |
CNAME (from
, to
) => (write from
;
88 write (Domain
.currentDomain ());
90 write (Int.toString ttl
);
91 write
"\tIN\tCNAME\t";
94 |
MX (num
, host
) => (write
"\t";
95 write (Int.toString ttl
);
97 write (Int.toString num
);
101 | NS host
=> (write
"\t";
102 write (Int.toString ttl
);
108 val () = Env
.actionV_one
"dns"
113 case TextIO.inputLine inf
of
114 NONE
=> raise Fail
"Expected a line for BIND"
115 | SOME s
=> String.substring (s
, 0, size s
- 1)
117 fun readILine inf
= valOf (Int.fromString (readLine inf
))
119 val monthToInt
= fn Date
.Jan
=> 1
134 CharVector
.tabulate (amt
- size s
, fn _
=> ch
) ^ s
140 val date
= Date
.fromTimeUniv (Time
.now ())
142 padBy #
"0" 4 (Int.toString (Date
.year date
))
143 ^ padBy #
"0" 2 (Int.toString (monthToInt (Date
.month date
)))
144 ^ padBy #
"0" 2 (Int.toString (Date
.day date
))
147 val () = Slave
.registerFileHandler (fn fs
=>
149 val {dir
, file
} = OS
.Path
.splitDirFile (#file fs
)
152 if #domain fs
= !didDomain
then
154 else if #action fs
= Slave
.Delete
then
156 val fname
= OS
.Path
.joinBaseExt
{base
= #domain fs
,
158 val fname
= OS
.Path
.joinDirFile
{dir
= Config
.Bind
.zonePath
,
161 Slave
.shellF ([Config
.rm
, " -f ", fname
],
162 fn cl
=> "Error deleting file: " ^ cl
)
166 val inf
= TextIO.openIn (OS
.Path
.joinDirFile
{dir
= #dir fs
,
168 val kind
= readLine inf
169 val ttl
= readILine inf
170 val ns
= readLine inf
171 val serial
= case readLine inf
of
173 | s
=> Int.fromString s
174 val rf
= readILine inf
175 val ret
= readILine inf
176 val exp
= readILine inf
177 val min
= readILine inf
178 val () = TextIO.closeIn inf
180 val serialPath
= OS
.Path
.joinDirFile
{dir
= Config
.serialDir
,
184 val inf
= TextIO.openIn serialPath
187 before TextIO.closeIn inf
188 end handle IO
.Io
{name
, ...} => NONE
192 SOME n
=> Int.toString n
195 val prefix
= dateString ()
201 if size old
>= 8 andalso
202 String.substring (old
, 0, 8) = prefix
then
203 case Int.fromString (String.extract (old
, 8, NONE
)) of
205 | SOME old
=> padBy #
"0" 2 (Int.toString (old
+1))
210 val outf
= TextIO.openOut serialPath
211 val _
= TextIO.output (outf
, newSerial
)
212 val _
= TextIO.closeOut outf
214 val dns
= OS
.Path
.joinDirFile
{dir
= #dir fs
,
217 val fname
= OS
.Path
.joinBaseExt
{base
= #domain fs
,
219 val fname
= OS
.Path
.joinDirFile
{dir
= Config
.Bind
.zonePath
,
222 val outf
= TextIO.openOut fname
225 TextIO.output (outf
, "$TTL ");
226 TextIO.output (outf
, Int.toString ttl
);
227 TextIO.output (outf
, "\n\n@\tIN\tSOA\t");
228 TextIO.output (outf
, ns
);
229 TextIO.output (outf
, ".\thostmaster.");
230 TextIO.output (outf
, #domain fs
);
231 TextIO.output (outf
, ". ( ");
232 TextIO.output (outf
, newSerial
);
233 TextIO.output (outf
, " ");
234 TextIO.output (outf
, Int.toString rf
);
235 TextIO.output (outf
, " ");
236 TextIO.output (outf
, Int.toString ret
);
237 TextIO.output (outf
, " ");
238 TextIO.output (outf
, Int.toString exp
);
239 TextIO.output (outf
, " ");
240 TextIO.output (outf
, Int.toString min
);
241 TextIO.output (outf
, " )\n\n");
242 TextIO.closeOut outf
;
243 if Posix
.FileSys
.access (dns
, []) then
244 Slave
.shellF ([Config
.cat
, " ", dns
, " >>", fname
],
245 fn cl
=> "Error concatenating file: " ^ cl
)
248 didDomain
:= #domain fs
252 "soa" => dnsChanged ()
253 |
"dns" => dnsChanged ()
254 |
"named.conf" => namedChanged
:= true
258 val () = Slave
.registerPostHandler
260 (if !namedChanged
then
261 Slave
.concatTo (fn s
=> s
= "named.conf") Config
.Bind
.namedConf
264 if !namedChanged
orelse !zoneChanged
then
265 Slave
.shellF ([Config
.Bind
.reload
],
266 fn cl
=> "Error reloading bind with " ^ cl
)
270 val () = Domain
.registerResetLocal (fn () =>
271 ignore (OS
.Process
.system (Config
.rm ^
" -rf /var/domtool/zones/*")))