85af7d3e |
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 | |
cec91e45 |
34 | val dns : Domain.files option ref = ref NONE |
85af7d3e |
35 | |
36 | val _ = Domain.registerBefore |
cec91e45 |
37 | (fn _ => dns := Option.map (fn node => Domain.domainsFile {node = node, |
38 | name = "dns"}) |
668e333e |
39 | (Domain.dnsMaster ())) |
85af7d3e |
40 | |
41 | val _ = Domain.registerAfter |
cec91e45 |
42 | (fn _ => (Option.app (fn files => #close files ()) (!dns); |
94a7e258 |
43 | dns := NONE)) |
85af7d3e |
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 |
a54ae325 |
52 | | DefaultA of string |
85af7d3e |
53 | |
54 | val record = fn (EApp ((EApp ((EVar "dnsA", _), e1), _), e2), _) => |
7e90e261 |
55 | (case (Env.string e1, Domain.ip e2) of |
85af7d3e |
56 | (SOME v1, SOME v2) => SOME (A (v1, v2)) |
57 | | _ => NONE) |
58 | | (EApp ((EApp ((EVar "dnsCNAME", _), e1), _), e2), _) => |
59 | (case (Env.string e1, Env.string e2) of |
60 | (SOME v1, SOME v2) => SOME (CNAME (v1, v2)) |
61 | | _ => NONE) |
62 | | (EApp ((EApp ((EVar "dnsMX", _), e1), _), e2), _) => |
63 | (case (Env.int e1, Env.string e2) of |
64 | (SOME v1, SOME v2) => SOME (MX (v1, v2)) |
65 | | _ => NONE) |
66 | | (EApp ((EVar "dnsNS", _), e), _) => |
67 | Option.map NS (Env.string e) |
a54ae325 |
68 | | (EApp ((EVar "dnsDefaultA", _), e), _) => |
69 | Option.map DefaultA (Env.string e) |
85af7d3e |
70 | | _ => NONE |
71 | |
72 | fun writeRecord (evs, r) = |
668e333e |
73 | case !dns of |
74 | NONE => print "Warning: DNS directive ignored because no master DNS server is configured for this domain\n" |
cec91e45 |
75 | | SOME files => |
668e333e |
76 | let |
cec91e45 |
77 | fun write s = #write files s |
78 | fun writeDom () = #writeDom files () |
668e333e |
79 | val ttl = Env.env Env.int (evs, "TTL") |
80 | in |
81 | case r of |
82 | A (from, to) => (write from; |
85af7d3e |
83 | write "."; |
cec91e45 |
84 | writeDom (); |
85af7d3e |
85 | write ".\t"; |
86 | write (Int.toString ttl); |
668e333e |
87 | write "\tIN\tA\t"; |
85af7d3e |
88 | write to; |
668e333e |
89 | write "\n") |
a54ae325 |
90 | | DefaultA to => (writeDom (); |
91 | write ".\t"; |
92 | write (Int.toString ttl); |
93 | write "\tIN\tA\t"; |
94 | write to; |
95 | write "\n") |
668e333e |
96 | | CNAME (from, to) => (write from; |
97 | write "."; |
cec91e45 |
98 | writeDom (); |
668e333e |
99 | write ".\t"; |
100 | write (Int.toString ttl); |
101 | write "\tIN\tCNAME\t"; |
102 | write to; |
103 | write ".\n") |
104 | | MX (num, host) => (write "\t"; |
105 | write (Int.toString ttl); |
106 | write "\tIN\tMX\t"; |
107 | write (Int.toString num); |
108 | write "\t"; |
109 | write host; |
110 | write ".\n") |
111 | | NS host => (write "\t"; |
112 | write (Int.toString ttl); |
113 | write "\tIN\tNS\t"; |
114 | write host; |
115 | write ".\n") |
116 | end |
85af7d3e |
117 | |
118 | val () = Env.actionV_one "dns" |
119 | ("record", record) |
120 | writeRecord |
121 | |
122 | fun readLine inf = |
123 | case TextIO.inputLine inf of |
124 | NONE => raise Fail "Expected a line for BIND" |
125 | | SOME s => String.substring (s, 0, size s - 1) |
126 | |
127 | fun readILine inf = valOf (Int.fromString (readLine inf)) |
128 | |
22fa50e0 |
129 | val monthToInt = fn Date.Jan => 1 |
130 | | Date.Feb => 2 |
131 | | Date.Mar => 3 |
132 | | Date.Apr => 4 |
133 | | Date.May => 5 |
134 | | Date.Jun => 6 |
135 | | Date.Jul => 7 |
136 | | Date.Aug => 8 |
137 | | Date.Sep => 9 |
138 | | Date.Oct => 10 |
139 | | Date.Nov => 11 |
140 | | Date.Dec => 12 |
141 | |
142 | fun padBy ch amt s = |
143 | if size s < amt then |
1a930bd7 |
144 | CharVector.tabulate (amt - size s, fn _ => ch) ^ s |
22fa50e0 |
145 | else |
146 | s |
147 | |
148 | fun dateString () = |
149 | let |
150 | val date = Date.fromTimeUniv (Time.now ()) |
151 | in |
152 | padBy #"0" 4 (Int.toString (Date.year date)) |
153 | ^ padBy #"0" 2 (Int.toString (monthToInt (Date.month date))) |
154 | ^ padBy #"0" 2 (Int.toString (Date.day date)) |
155 | end |
156 | |
85af7d3e |
157 | val () = Slave.registerFileHandler (fn fs => |
158 | let |
159 | val {dir, file} = OS.Path.splitDirFile (#file fs) |
160 | |
161 | fun dnsChanged () = |
162 | if #domain fs = !didDomain then |
163 | () |
164 | else if #action fs = Slave.Delete then |
165 | let |
166 | val fname = OS.Path.joinBaseExt {base = #domain fs, |
167 | ext = SOME "zone"} |
168 | val fname = OS.Path.joinDirFile {dir = Config.Bind.zonePath, |
169 | file = fname} |
170 | in |
94a7e258 |
171 | Slave.shellF ([Config.rm, " -f ", fname], |
172 | fn cl => "Error deleting file: " ^ cl) |
85af7d3e |
173 | end |
174 | else |
175 | let |
176 | val inf = TextIO.openIn (OS.Path.joinDirFile {dir = #dir fs, |
177 | file = "soa"}) |
178 | val kind = readLine inf |
179 | val ttl = readILine inf |
180 | val ns = readLine inf |
181 | val serial = case readLine inf of |
182 | "" => NONE |
183 | | s => Int.fromString s |
184 | val rf = readILine inf |
185 | val ret = readILine inf |
186 | val exp = readILine inf |
187 | val min = readILine inf |
188 | val () = TextIO.closeIn inf |
189 | |
1a930bd7 |
190 | val serialPath = OS.Path.joinDirFile {dir = Config.serialDir, |
191 | file = #domain fs} |
192 | |
22fa50e0 |
193 | val oldSerial = let |
1a930bd7 |
194 | val inf = TextIO.openIn serialPath |
22fa50e0 |
195 | in |
196 | SOME (readLine inf) |
197 | before TextIO.closeIn inf |
1a930bd7 |
198 | end handle IO.Io {name, ...} => NONE |
22fa50e0 |
199 | |
200 | val newSerial = |
201 | case serial of |
202 | SOME n => Int.toString n |
203 | | NONE => |
204 | let |
205 | val prefix = dateString () |
206 | in |
207 | prefix |
208 | ^ (case oldSerial of |
209 | NONE => "00" |
210 | | SOME old => |
211 | if size old >= 8 andalso |
212 | String.substring (old, 0, 8) = prefix then |
213 | case Int.fromString (String.extract (old, 8, NONE)) of |
214 | NONE => "00" |
215 | | SOME old => padBy #"0" 2 (Int.toString (old+1)) |
216 | else |
217 | "00") |
218 | end |
219 | |
1a930bd7 |
220 | val outf = TextIO.openOut serialPath |
22fa50e0 |
221 | val _ = TextIO.output (outf, newSerial) |
222 | val _ = TextIO.closeOut outf |
223 | |
85af7d3e |
224 | val dns = OS.Path.joinDirFile {dir = #dir fs, |
225 | file = "dns"} |
226 | |
227 | val fname = OS.Path.joinBaseExt {base = #domain fs, |
228 | ext = SOME "zone"} |
229 | val fname = OS.Path.joinDirFile {dir = Config.Bind.zonePath, |
230 | file = fname} |
231 | |
232 | val outf = TextIO.openOut fname |
233 | in |
234 | zoneChanged := true; |
235 | TextIO.output (outf, "$TTL "); |
236 | TextIO.output (outf, Int.toString ttl); |
237 | TextIO.output (outf, "\n\n@\tIN\tSOA\t"); |
238 | TextIO.output (outf, ns); |
239 | TextIO.output (outf, ".\thostmaster."); |
240 | TextIO.output (outf, #domain fs); |
dce137aa |
241 | TextIO.output (outf, ". ( "); |
22fa50e0 |
242 | TextIO.output (outf, newSerial); |
85af7d3e |
243 | TextIO.output (outf, " "); |
244 | TextIO.output (outf, Int.toString rf); |
245 | TextIO.output (outf, " "); |
246 | TextIO.output (outf, Int.toString ret); |
247 | TextIO.output (outf, " "); |
248 | TextIO.output (outf, Int.toString exp); |
249 | TextIO.output (outf, " "); |
250 | TextIO.output (outf, Int.toString min); |
251 | TextIO.output (outf, " )\n\n"); |
252 | TextIO.closeOut outf; |
94a7e258 |
253 | if Posix.FileSys.access (dns, []) then |
254 | Slave.shellF ([Config.cat, " ", dns, " >>", fname], |
255 | fn cl => "Error concatenating file: " ^ cl) |
256 | else |
257 | (); |
85af7d3e |
258 | didDomain := #domain fs |
259 | end |
260 | in |
261 | case file of |
262 | "soa" => dnsChanged () |
263 | | "dns" => dnsChanged () |
264 | | "named.conf" => namedChanged := true |
265 | | _ => () |
266 | end) |
267 | |
268 | val () = Slave.registerPostHandler |
269 | (fn () => |
270 | (if !namedChanged then |
271 | Slave.concatTo (fn s => s = "named.conf") Config.Bind.namedConf |
272 | else |
273 | (); |
274 | if !namedChanged orelse !zoneChanged then |
275 | Slave.shellF ([Config.Bind.reload], |
276 | fn cl => "Error reloading bind with " ^ cl) |
277 | else |
278 | ())) |
0ea0ecfa |
279 | |
280 | val () = Domain.registerResetLocal (fn () => |
281 | ignore (OS.Process.system (Config.rm ^ " -rf /var/domtool/zones/*"))) |
282 | |
85af7d3e |
283 | end |