Serial number processing
[hcoop/domtool2.git] / src / plugins / bind.sml
1 (* HCoop Domtool (http://hcoop.sourceforge.net/)
2 * Copyright (c) 2006, Adam Chlipala
3 *
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.
8 *
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.
13 *
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.
17 *)
18
19 (* BIND DNS *)
20
21 structure Bind :> BIND = struct
22
23 open Ast
24
25 val namedChanged = ref false
26 val zoneChanged = ref false
27
28 val didDomain = ref ""
29
30 val () = Slave.registerPreHandler (fn () => (namedChanged := false;
31 zoneChanged := false;
32 didDomain := ""))
33
34 val dns : TextIO.outstream option ref = ref NONE
35
36 val _ = Domain.registerBefore
37 (fn _ => dns := Option.map (fn node => Domain.domainFile {node = node,
38 name = "dns"})
39 (Domain.dnsMaster ()))
40
41 val _ = Domain.registerAfter
42 (fn _ => (Option.app TextIO.closeOut (!dns);
43 dns := NONE))
44
45 val dl = ErrorMsg.dummyLoc
46
47 datatype dns_record =
48 A of string * string
49 | CNAME of string * string
50 | MX of int * string
51 | NS of string
52
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))
56 | _ => NONE)
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))
60 | _ => NONE)
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))
64 | _ => NONE)
65 | (EApp ((EVar "dnsNS", _), e), _) =>
66 Option.map NS (Env.string e)
67 | _ => NONE
68
69 fun writeRecord (evs, r) =
70 case !dns of
71 NONE => print "Warning: DNS directive ignored because no master DNS server is configured for this domain\n"
72 | SOME file =>
73 let
74 fun write s = TextIO.output (file, s)
75 val ttl = Env.env Env.int (evs, "TTL")
76 in
77 case r of
78 A (from, to) => (write from;
79 write ".";
80 write (Domain.currentDomain ());
81 write ".\t";
82 write (Int.toString ttl);
83 write "\tIN\tA\t";
84 write to;
85 write "\n")
86 | CNAME (from, to) => (write from;
87 write ".";
88 write (Domain.currentDomain ());
89 write ".\t";
90 write (Int.toString ttl);
91 write "\tIN\tCNAME\t";
92 write to;
93 write ".\n")
94 | MX (num, host) => (write "\t";
95 write (Int.toString ttl);
96 write "\tIN\tMX\t";
97 write (Int.toString num);
98 write "\t";
99 write host;
100 write ".\n")
101 | NS host => (write "\t";
102 write (Int.toString ttl);
103 write "\tIN\tNS\t";
104 write host;
105 write ".\n")
106 end
107
108 val () = Env.actionV_one "dns"
109 ("record", record)
110 writeRecord
111
112 fun readLine inf =
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)
116
117 fun readILine inf = valOf (Int.fromString (readLine inf))
118
119 val monthToInt = fn Date.Jan => 1
120 | Date.Feb => 2
121 | Date.Mar => 3
122 | Date.Apr => 4
123 | Date.May => 5
124 | Date.Jun => 6
125 | Date.Jul => 7
126 | Date.Aug => 8
127 | Date.Sep => 9
128 | Date.Oct => 10
129 | Date.Nov => 11
130 | Date.Dec => 12
131
132 fun padBy ch amt s =
133 if size s < amt then
134 CharVector.tabulate (amt, fn _ => ch) ^ s
135 else
136 s
137
138 fun dateString () =
139 let
140 val date = Date.fromTimeUniv (Time.now ())
141 in
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))
145 end
146
147 val () = Slave.registerFileHandler (fn fs =>
148 let
149 val {dir, file} = OS.Path.splitDirFile (#file fs)
150
151 fun dnsChanged () =
152 if #domain fs = !didDomain then
153 ()
154 else if #action fs = Slave.Delete then
155 let
156 val fname = OS.Path.joinBaseExt {base = #domain fs,
157 ext = SOME "zone"}
158 val fname = OS.Path.joinDirFile {dir = Config.Bind.zonePath,
159 file = fname}
160 in
161 Slave.shellF ([Config.rm, " -f ", fname],
162 fn cl => "Error deleting file: " ^ cl)
163 end
164 else
165 let
166 val inf = TextIO.openIn (OS.Path.joinDirFile {dir = #dir fs,
167 file = "soa"})
168 val kind = readLine inf
169 val ttl = readILine inf
170 val ns = readLine inf
171 val serial = case readLine inf of
172 "" => NONE
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
179
180 val oldSerial = let
181 val inf = TextIO.openIn (OS.Path.joinDirFile {dir = #dir fs,
182 file = "serial"})
183 in
184 SOME (readLine inf)
185 before TextIO.closeIn inf
186 end handle IO.Io _ => NONE
187
188 val newSerial =
189 case serial of
190 SOME n => Int.toString n
191 | NONE =>
192 let
193 val prefix = dateString ()
194 in
195 prefix
196 ^ (case oldSerial of
197 NONE => "00"
198 | SOME old =>
199 if size old >= 8 andalso
200 String.substring (old, 0, 8) = prefix then
201 case Int.fromString (String.extract (old, 8, NONE)) of
202 NONE => "00"
203 | SOME old => padBy #"0" 2 (Int.toString (old+1))
204 else
205 "00")
206 end
207
208 val outf = TextIO.openOut (OS.Path.joinDirFile {dir = #dir fs,
209 file = "serial"})
210 val _ = TextIO.output (outf, newSerial)
211 val _ = TextIO.closeOut outf
212
213 val dns = OS.Path.joinDirFile {dir = #dir fs,
214 file = "dns"}
215
216 val fname = OS.Path.joinBaseExt {base = #domain fs,
217 ext = SOME "zone"}
218 val fname = OS.Path.joinDirFile {dir = Config.Bind.zonePath,
219 file = fname}
220
221 val outf = TextIO.openOut fname
222 in
223 zoneChanged := true;
224 TextIO.output (outf, "$TTL ");
225 TextIO.output (outf, Int.toString ttl);
226 TextIO.output (outf, "\n\n@\tIN\tSOA\t");
227 TextIO.output (outf, ns);
228 TextIO.output (outf, ".\thostmaster.");
229 TextIO.output (outf, #domain fs);
230 TextIO.output (outf, ". ( ");
231 TextIO.output (outf, newSerial);
232 TextIO.output (outf, " ");
233 TextIO.output (outf, Int.toString rf);
234 TextIO.output (outf, " ");
235 TextIO.output (outf, Int.toString ret);
236 TextIO.output (outf, " ");
237 TextIO.output (outf, Int.toString exp);
238 TextIO.output (outf, " ");
239 TextIO.output (outf, Int.toString min);
240 TextIO.output (outf, " )\n\n");
241 TextIO.closeOut outf;
242 if Posix.FileSys.access (dns, []) then
243 Slave.shellF ([Config.cat, " ", dns, " >>", fname],
244 fn cl => "Error concatenating file: " ^ cl)
245 else
246 ();
247 didDomain := #domain fs
248 end
249 in
250 case file of
251 "soa" => dnsChanged ()
252 | "dns" => dnsChanged ()
253 | "named.conf" => namedChanged := true
254 | _ => ()
255 end)
256
257 val () = Slave.registerPostHandler
258 (fn () =>
259 (if !namedChanged then
260 Slave.concatTo (fn s => s = "named.conf") Config.Bind.namedConf
261 else
262 ();
263 if !namedChanged orelse !zoneChanged then
264 Slave.shellF ([Config.Bind.reload],
265 fn cl => "Error reloading bind with " ^ cl)
266 else
267 ()))
268 end