blob: d6fdef687d476674a2f46057d978110d32bcb7a9 [file] [log] [blame]
Simon Kelleyc47e3ba2014-01-08 17:07:54 +00001/* dnsmasq is Copyright (c) 2000-2014 Simon Kelley
Simon Kelley4f7b3042012-11-28 21:27:02 +00002
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; version 2 dated June, 1991, or
6 (at your option) version 3 dated 29 June, 2007.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program. If not, see <http://www.gnu.org/licenses/>.
15*/
16
17#include "dnsmasq.h"
18
Simon Kelley4820dce2012-12-18 18:30:30 +000019#ifdef HAVE_AUTH
Simon Kelleyb75e9362012-12-07 11:50:41 +000020
Simon Kelleyc50f25a2013-11-21 11:29:27 +000021static struct addrlist *find_subnet(struct auth_zone *zone, int flag, struct all_addr *addr_u)
Simon Kelley4f7b3042012-11-28 21:27:02 +000022{
Simon Kelley376d48c2013-11-13 13:04:30 +000023 struct addrlist *subnet;
Simon Kelley4f7b3042012-11-28 21:27:02 +000024
25 for (subnet = zone->subnet; subnet; subnet = subnet->next)
26 {
Simon Kelley376d48c2013-11-13 13:04:30 +000027 if (!(subnet->flags & ADDRLIST_IPV6))
Simon Kelley4f7b3042012-11-28 21:27:02 +000028 {
Simon Kelley376d48c2013-11-13 13:04:30 +000029 struct in_addr netmask, addr = addr_u->addr.addr4;
30
31 if (!(flag & F_IPV4))
32 continue;
Simon Kelley4f7b3042012-11-28 21:27:02 +000033
Simon Kelley376d48c2013-11-13 13:04:30 +000034 netmask.s_addr = htonl(~((1 << (32 - subnet->prefixlen)) - 1));
Simon Kelley4f7b3042012-11-28 21:27:02 +000035
Simon Kelley376d48c2013-11-13 13:04:30 +000036 if (is_same_net(addr, subnet->addr.addr.addr4, netmask))
Simon Kelley45dd1fe2012-12-04 20:49:24 +000037 return subnet;
Simon Kelley4f7b3042012-11-28 21:27:02 +000038 }
39#ifdef HAVE_IPV6
Simon Kelley376d48c2013-11-13 13:04:30 +000040 else if (is_same_net6(&(addr_u->addr.addr6), &subnet->addr.addr.addr6, subnet->prefixlen))
Simon Kelley45dd1fe2012-12-04 20:49:24 +000041 return subnet;
Simon Kelley4f7b3042012-11-28 21:27:02 +000042#endif
43
44 }
Simon Kelley45dd1fe2012-12-04 20:49:24 +000045 return NULL;
Simon Kelley4f7b3042012-11-28 21:27:02 +000046}
47
Simon Kelleyc50f25a2013-11-21 11:29:27 +000048static int filter_zone(struct auth_zone *zone, int flag, struct all_addr *addr_u)
49{
50 /* No zones specified, no filter */
51 if (!zone->subnet)
52 return 1;
53
54 return find_subnet(zone, flag, addr_u) != NULL;
55}
56
Simon Kelleyb485ed92013-10-18 22:00:39 +010057int in_zone(struct auth_zone *zone, char *name, char **cut)
Simon Kelleyb75e9362012-12-07 11:50:41 +000058{
59 size_t namelen = strlen(name);
60 size_t domainlen = strlen(zone->domain);
61
62 if (cut)
63 *cut = NULL;
64
65 if (namelen >= domainlen &&
66 hostname_isequal(zone->domain, &name[namelen - domainlen]))
67 {
68
69 if (namelen == domainlen)
70 return 1;
71
72 if (name[namelen - domainlen - 1] == '.')
73 {
74 if (cut)
75 *cut = &name[namelen - domainlen - 1];
76 return 1;
77 }
78 }
79
80 return 0;
81}
Simon Kelley4f7b3042012-11-28 21:27:02 +000082
83
Simon Kelley19b16892013-10-20 10:19:39 +010084size_t answer_auth(struct dns_header *header, char *limit, size_t qlen, time_t now, union mysockaddr *peer_addr, int local_query)
Simon Kelley4f7b3042012-11-28 21:27:02 +000085{
86 char *name = daemon->namebuff;
87 unsigned char *p, *ansp;
88 int qtype, qclass;
Simon Kelleyb75e9362012-12-07 11:50:41 +000089 int nameoffset, axfroffset = 0;
Simon Kelley4f7b3042012-11-28 21:27:02 +000090 int q, anscount = 0, authcount = 0;
91 struct crec *crecp;
Simon Kelley19b16892013-10-20 10:19:39 +010092 int auth = !local_query, trunc = 0, nxdomain = 1, soa = 0, ns = 0, axfr = 0;
Simon Kelley4f7b3042012-11-28 21:27:02 +000093 struct auth_zone *zone = NULL;
Simon Kelley376d48c2013-11-13 13:04:30 +000094 struct addrlist *subnet = NULL;
Simon Kelleyb75e9362012-12-07 11:50:41 +000095 char *cut;
Simon Kelleye1ff4192012-12-09 17:08:47 +000096 struct mx_srv_record *rec, *move, **up;
97 struct txt_record *txt;
98 struct interface_name *intr;
99 struct naptr *na;
100 struct all_addr addr;
101 struct cname *a;
102
Simon Kelley4f7b3042012-11-28 21:27:02 +0000103 if (ntohs(header->qdcount) == 0 || OPCODE(header) != QUERY )
104 return 0;
Simon Kelley6008bdb2013-10-21 21:47:03 +0100105
Simon Kelley4f7b3042012-11-28 21:27:02 +0000106 /* determine end of question section (we put answers there) */
107 if (!(ansp = skip_questions(header, qlen)))
108 return 0; /* bad packet */
109
110 /* now process each question, answers go in RRs after the question */
111 p = (unsigned char *)(header+1);
112
113 for (q = ntohs(header->qdcount); q != 0; q--)
114 {
Simon Kelley8273ea52012-11-29 21:12:33 +0000115 unsigned short flag = 0;
Simon Kelley4f7b3042012-11-28 21:27:02 +0000116 int found = 0;
Simon Kelleye1ff4192012-12-09 17:08:47 +0000117
Simon Kelley4f7b3042012-11-28 21:27:02 +0000118 /* save pointer to name for copying into answers */
119 nameoffset = p - (unsigned char *)header;
120
121 /* now extract name as .-concatenated string into name */
122 if (!extract_name(header, qlen, &p, name, 1, 4))
123 return 0; /* bad packet */
124
125 GETSHORT(qtype, p);
126 GETSHORT(qclass, p);
127
128 if (qclass != C_IN)
Simon Kelleyf8abe0c2012-12-15 11:59:25 +0000129 {
130 auth = 0;
131 continue;
132 }
Simon Kelleyb75e9362012-12-07 11:50:41 +0000133
Simon Kelley4f7b3042012-11-28 21:27:02 +0000134 if (qtype == T_PTR)
135 {
Simon Kelley4f7b3042012-11-28 21:27:02 +0000136 if (!(flag = in_arpa_name_2_addr(name, &addr)))
137 continue;
138
Simon Kelley19b16892013-10-20 10:19:39 +0100139 if (!local_query)
Simon Kelley4f7b3042012-11-28 21:27:02 +0000140 {
Simon Kelley19b16892013-10-20 10:19:39 +0100141 for (zone = daemon->auth_zones; zone; zone = zone->next)
Simon Kelleyc50f25a2013-11-21 11:29:27 +0000142 if ((subnet = find_subnet(zone, flag, &addr)))
Simon Kelley19b16892013-10-20 10:19:39 +0100143 break;
144
145 if (!zone)
146 {
147 auth = 0;
148 continue;
149 }
Simon Kelley4f7b3042012-11-28 21:27:02 +0000150 }
Simon Kelley19b16892013-10-20 10:19:39 +0100151
Simon Kelley115ac3e2013-05-20 11:28:32 +0100152 intr = NULL;
Simon Kelley86e3b9a2012-11-30 13:46:48 +0000153
Simon Kelley115ac3e2013-05-20 11:28:32 +0100154 if (flag == F_IPV4)
155 for (intr = daemon->int_names; intr; intr = intr->next)
156 {
157 struct addrlist *addrlist;
158
Simon Kelley376d48c2013-11-13 13:04:30 +0000159 for (addrlist = intr->addr; addrlist; addrlist = addrlist->next)
160 if (!(addrlist->flags & ADDRLIST_IPV6) && addr.addr.addr4.s_addr == addrlist->addr.addr.addr4.s_addr)
Simon Kelley115ac3e2013-05-20 11:28:32 +0100161 break;
162
163 if (addrlist)
164 break;
165 else
166 while (intr->next && strcmp(intr->intr, intr->next->intr) == 0)
167 intr = intr->next;
168 }
169#ifdef HAVE_IPV6
170 else if (flag == F_IPV6)
171 for (intr = daemon->int_names; intr; intr = intr->next)
172 {
173 struct addrlist *addrlist;
174
Simon Kelley376d48c2013-11-13 13:04:30 +0000175 for (addrlist = intr->addr; addrlist; addrlist = addrlist->next)
176 if ((addrlist->flags & ADDRLIST_IPV6) && IN6_ARE_ADDR_EQUAL(&addr.addr.addr6, &addrlist->addr.addr.addr6))
Simon Kelley115ac3e2013-05-20 11:28:32 +0100177 break;
178
179 if (addrlist)
180 break;
181 else
182 while (intr->next && strcmp(intr->intr, intr->next->intr) == 0)
183 intr = intr->next;
184 }
185#endif
186
187 if (intr)
188 {
189 if (in_zone(zone, intr->name, NULL))
190 {
191 found = 1;
Simon Kelley8ab91e92013-10-21 20:50:04 +0100192 log_query(flag | F_REVERSE | F_CONFIG, intr->name, &addr, NULL);
Simon Kelley115ac3e2013-05-20 11:28:32 +0100193 if (add_resource_record(header, limit, &trunc, nameoffset, &ansp,
194 daemon->auth_ttl, NULL,
195 T_PTR, C_IN, "d", intr->name))
196 anscount++;
Simon Kelley86e3b9a2012-11-30 13:46:48 +0000197 }
198 }
Simon Kelley115ac3e2013-05-20 11:28:32 +0100199
Simon Kelley4f7b3042012-11-28 21:27:02 +0000200 if ((crecp = cache_find_by_addr(NULL, &addr, now, flag)))
201 do {
202 strcpy(name, cache_get_name(crecp));
203
204 if (crecp->flags & F_DHCP && !option_bool(OPT_DHCP_FQDN))
205 {
206 char *p = strchr(name, '.');
207 if (p)
208 *p = 0; /* must be bare name */
209
210 /* add external domain */
211 strcat(name, ".");
212 strcat(name, zone->domain);
213 log_query(flag | F_DHCP | F_REVERSE, name, &addr, record_source(crecp->uid));
Simon Kelley8273ea52012-11-29 21:12:33 +0000214 found = 1;
Simon Kelley4f7b3042012-11-28 21:27:02 +0000215 if (add_resource_record(header, limit, &trunc, nameoffset, &ansp,
216 daemon->auth_ttl, NULL,
217 T_PTR, C_IN, "d", name))
Simon Kelley8273ea52012-11-29 21:12:33 +0000218 anscount++;
Simon Kelley4f7b3042012-11-28 21:27:02 +0000219 }
Simon Kelleyb75e9362012-12-07 11:50:41 +0000220 else if (crecp->flags & (F_DHCP | F_HOSTS) && in_zone(zone, name, NULL))
Simon Kelley4f7b3042012-11-28 21:27:02 +0000221 {
Simon Kelley4f7b3042012-11-28 21:27:02 +0000222 log_query(crecp->flags & ~F_FORWARD, name, &addr, record_source(crecp->uid));
Simon Kelley8273ea52012-11-29 21:12:33 +0000223 found = 1;
Simon Kelley4f7b3042012-11-28 21:27:02 +0000224 if (add_resource_record(header, limit, &trunc, nameoffset, &ansp,
225 daemon->auth_ttl, NULL,
226 T_PTR, C_IN, "d", name))
Simon Kelley8273ea52012-11-29 21:12:33 +0000227 anscount++;
Simon Kelley4f7b3042012-11-28 21:27:02 +0000228 }
229 else
230 continue;
231
232 } while ((crecp = cache_find_by_addr(crecp, &addr, now, flag)));
Simon Kelley86e3b9a2012-11-30 13:46:48 +0000233
Simon Kelley4f7b3042012-11-28 21:27:02 +0000234 if (!found)
235 log_query(flag | F_NEG | F_NXDOMAIN | F_REVERSE | F_AUTH, NULL, &addr, NULL);
236
237 continue;
238 }
239
Simon Kelley5c0bd5b2012-12-01 16:42:47 +0000240 cname_restart:
Simon Kelley4f7b3042012-11-28 21:27:02 +0000241 for (zone = daemon->auth_zones; zone; zone = zone->next)
Simon Kelleyb75e9362012-12-07 11:50:41 +0000242 if (in_zone(zone, name, &cut))
243 break;
Simon Kelley86e3b9a2012-11-30 13:46:48 +0000244
245 if (!zone)
Simon Kelley4f7b3042012-11-28 21:27:02 +0000246 {
247 auth = 0;
248 continue;
249 }
250
Simon Kelley8273ea52012-11-29 21:12:33 +0000251 for (rec = daemon->mxnames; rec; rec = rec->next)
252 if (!rec->issrv && hostname_isequal(name, rec->name))
Simon Kelley86e3b9a2012-11-30 13:46:48 +0000253 {
254 nxdomain = 0;
255
256 if (qtype == T_MX)
257 {
258 found = 1;
259 log_query(F_CONFIG | F_RRNAME, name, NULL, "<MX>");
260 if (add_resource_record(header, limit, &trunc, nameoffset, &ansp, daemon->auth_ttl,
261 NULL, T_MX, C_IN, "sd", rec->weight, rec->target))
262 anscount++;
263 }
264 }
Simon Kelley8273ea52012-11-29 21:12:33 +0000265
Simon Kelley86e3b9a2012-11-30 13:46:48 +0000266 for (move = NULL, up = &daemon->mxnames, rec = daemon->mxnames; rec; rec = rec->next)
267 if (rec->issrv && hostname_isequal(name, rec->name))
268 {
269 nxdomain = 0;
270
271 if (qtype == T_SRV)
272 {
273 found = 1;
274 log_query(F_CONFIG | F_RRNAME, name, NULL, "<SRV>");
275 if (add_resource_record(header, limit, &trunc, nameoffset, &ansp, daemon->auth_ttl,
276 NULL, T_SRV, C_IN, "sssd",
277 rec->priority, rec->weight, rec->srvport, rec->target))
278
279 anscount++;
280 }
281
282 /* unlink first SRV record found */
283 if (!move)
284 {
285 move = rec;
286 *up = rec->next;
287 }
288 else
289 up = &rec->next;
290 }
291 else
292 up = &rec->next;
293
294 /* put first SRV record back at the end. */
295 if (move)
296 {
297 *up = move;
298 move->next = NULL;
299 }
300
301 for (txt = daemon->rr; txt; txt = txt->next)
302 if (hostname_isequal(name, txt->name))
303 {
304 nxdomain = 0;
305 if (txt->class == qtype)
306 {
307 found = 1;
308 log_query(F_CONFIG | F_RRNAME, name, NULL, "<RR>");
309 if (add_resource_record(header, limit, &trunc, nameoffset, &ansp, daemon->auth_ttl,
310 NULL, txt->class, C_IN, "t", txt->len, txt->txt))
311 anscount++;
312 }
313 }
314
315 for (txt = daemon->txt; txt; txt = txt->next)
316 if (txt->class == C_IN && hostname_isequal(name, txt->name))
317 {
318 nxdomain = 0;
319 if (qtype == T_TXT)
320 {
321 found = 1;
322 log_query(F_CONFIG | F_RRNAME, name, NULL, "<TXT>");
323 if (add_resource_record(header, limit, &trunc, nameoffset, &ansp, daemon->auth_ttl,
324 NULL, T_TXT, C_IN, "t", txt->len, txt->txt))
325 anscount++;
326 }
327 }
328
329 for (na = daemon->naptr; na; na = na->next)
330 if (hostname_isequal(name, na->name))
331 {
332 nxdomain = 0;
333 if (qtype == T_NAPTR)
334 {
335 found = 1;
336 log_query(F_CONFIG | F_RRNAME, name, NULL, "<NAPTR>");
337 if (add_resource_record(header, limit, &trunc, nameoffset, &ansp, daemon->auth_ttl,
338 NULL, T_NAPTR, C_IN, "sszzzd",
339 na->order, na->pref, na->flags, na->services, na->regexp, na->replace))
340 anscount++;
341 }
342 }
Simon Kelley115ac3e2013-05-20 11:28:32 +0100343
344 if (qtype == T_A)
345 flag = F_IPV4;
346
347#ifdef HAVE_IPV6
348 if (qtype == T_AAAA)
349 flag = F_IPV6;
350#endif
351
Simon Kelley8ab91e92013-10-21 20:50:04 +0100352 for (intr = daemon->int_names; intr; intr = intr->next)
353 if (hostname_isequal(name, intr->name))
354 {
355 struct addrlist *addrlist;
356
Simon Kelley8ab91e92013-10-21 20:50:04 +0100357 nxdomain = 0;
358
359 if (flag)
Simon Kelley376d48c2013-11-13 13:04:30 +0000360 for (addrlist = intr->addr; addrlist; addrlist = addrlist->next)
361 if (((addrlist->flags & ADDRLIST_IPV6) ? T_AAAA : T_A) == qtype &&
362 (local_query || filter_zone(zone, flag, &addrlist->addr)))
Simon Kelleyfb63dd12013-10-21 18:19:35 +0100363 {
364 found = 1;
365 log_query(F_FORWARD | F_CONFIG | flag, name, &addrlist->addr, NULL);
366 if (add_resource_record(header, limit, &trunc, nameoffset, &ansp,
367 daemon->auth_ttl, NULL, qtype, C_IN,
368 qtype == T_A ? "4" : "6", &addrlist->addr))
369 anscount++;
370 }
371 }
Simon Kelley5c0bd5b2012-12-01 16:42:47 +0000372
373 for (a = daemon->cnames; a; a = a->next)
374 if (hostname_isequal(name, a->alias) )
375 {
376 log_query(F_CONFIG | F_CNAME, name, NULL, NULL);
377 strcpy(name, a->target);
378 if (!strchr(name, '.'))
379 {
380 strcat(name, ".");
381 strcat(name, zone->domain);
382 }
383 found = 1;
384 if (add_resource_record(header, limit, &trunc, nameoffset, &ansp,
Simon Kelley93bafe62013-10-21 21:19:34 +0100385 daemon->auth_ttl, &nameoffset,
Simon Kelley5c0bd5b2012-12-01 16:42:47 +0000386 T_CNAME, C_IN, "d", name))
387 anscount++;
388
389 goto cname_restart;
390 }
Simon Kelley86e3b9a2012-11-30 13:46:48 +0000391
Simon Kelleye1ff4192012-12-09 17:08:47 +0000392 if (!cut)
Simon Kelley4f7b3042012-11-28 21:27:02 +0000393 {
Simon Kelleye1ff4192012-12-09 17:08:47 +0000394 nxdomain = 0;
395
396 if (qtype == T_SOA)
397 {
Simon Kelley5f8002f2013-10-21 17:40:18 +0100398 auth = soa = 1; /* inhibits auth section */
Simon Kelleye1ff4192012-12-09 17:08:47 +0000399 found = 1;
400 log_query(F_RRNAME | F_AUTH, zone->domain, NULL, "<SOA>");
401 }
402 else if (qtype == T_AXFR)
403 {
Simon Kelleyc6cb7402013-01-07 21:55:54 +0000404 struct iname *peers;
405
406 if (peer_addr->sa.sa_family == AF_INET)
407 peer_addr->in.sin_port = 0;
408#ifdef HAVE_IPV6
409 else
410 peer_addr->in6.sin6_port = 0;
411#endif
412
413 for (peers = daemon->auth_peers; peers; peers = peers->next)
414 if (sockaddr_isequal(peer_addr, &peers->addr))
415 break;
416
417 /* Refuse all AXFR unless --auth-sec-servers is set */
418 if ((!peers && daemon->auth_peers) || !daemon->secondary_forward_server)
Simon Kelley49678762012-12-09 18:24:58 +0000419 {
Simon Kelley49678762012-12-09 18:24:58 +0000420 if (peer_addr->sa.sa_family == AF_INET)
Simon Kelleyc6cb7402013-01-07 21:55:54 +0000421 inet_ntop(AF_INET, &peer_addr->in.sin_addr, daemon->addrbuff, ADDRSTRLEN);
Simon Kelley49678762012-12-09 18:24:58 +0000422#ifdef HAVE_IPV6
423 else
Simon Kelleyc6cb7402013-01-07 21:55:54 +0000424 inet_ntop(AF_INET6, &peer_addr->in6.sin6_addr, daemon->addrbuff, ADDRSTRLEN);
Simon Kelley49678762012-12-09 18:24:58 +0000425#endif
426
Simon Kelleyc6cb7402013-01-07 21:55:54 +0000427 my_syslog(LOG_WARNING, _("ignoring zone transfer request from %s"), daemon->addrbuff);
428 return 0;
Simon Kelley49678762012-12-09 18:24:58 +0000429 }
Simon Kelleyc6cb7402013-01-07 21:55:54 +0000430
Simon Kelley5f8002f2013-10-21 17:40:18 +0100431 auth = 1;
Simon Kelleye1ff4192012-12-09 17:08:47 +0000432 soa = 1; /* inhibits auth section */
433 ns = 1; /* ensure we include NS records! */
434 axfr = 1;
435 found = 1;
436 axfroffset = nameoffset;
437 log_query(F_RRNAME | F_AUTH, zone->domain, NULL, "<AXFR>");
438 }
439 else if (qtype == T_NS)
440 {
Simon Kelley5f8002f2013-10-21 17:40:18 +0100441 auth = 1;
Simon Kelleye1ff4192012-12-09 17:08:47 +0000442 ns = 1; /* inhibits auth section */
443 found = 1;
444 log_query(F_RRNAME | F_AUTH, zone->domain, NULL, "<NS>");
445 }
Simon Kelley4f7b3042012-11-28 21:27:02 +0000446 }
Simon Kelley8273ea52012-11-29 21:12:33 +0000447
Simon Kelleyb75e9362012-12-07 11:50:41 +0000448 if (!option_bool(OPT_DHCP_FQDN) && cut)
Simon Kelley4f7b3042012-11-28 21:27:02 +0000449 {
Simon Kelleyb75e9362012-12-07 11:50:41 +0000450 *cut = 0; /* remove domain part */
Simon Kelley4f7b3042012-11-28 21:27:02 +0000451
Simon Kelley5c0bd5b2012-12-01 16:42:47 +0000452 if (!strchr(name, '.') && (crecp = cache_find_by_name(NULL, name, now, F_IPV4 | F_IPV6)))
Simon Kelley4f7b3042012-11-28 21:27:02 +0000453 {
454 if (crecp->flags & F_DHCP)
455 do
456 {
457 nxdomain = 0;
Simon Kelley60225f42012-12-28 11:29:01 +0000458 if ((crecp->flags & flag) &&
Simon Kelley376d48c2013-11-13 13:04:30 +0000459 (local_query || filter_zone(zone, flag, &(crecp->addr.addr))))
Simon Kelley4f7b3042012-11-28 21:27:02 +0000460 {
Simon Kelleyb75e9362012-12-07 11:50:41 +0000461 *cut = '.'; /* restore domain part */
Simon Kelley4f7b3042012-11-28 21:27:02 +0000462 log_query(crecp->flags, name, &crecp->addr.addr, record_source(crecp->uid));
Simon Kelleyb75e9362012-12-07 11:50:41 +0000463 *cut = 0; /* remove domain part */
Simon Kelley8273ea52012-11-29 21:12:33 +0000464 found = 1;
Simon Kelley4f7b3042012-11-28 21:27:02 +0000465 if (add_resource_record(header, limit, &trunc, nameoffset, &ansp,
466 daemon->auth_ttl, NULL, qtype, C_IN,
467 qtype == T_A ? "4" : "6", &crecp->addr))
Simon Kelley8273ea52012-11-29 21:12:33 +0000468 anscount++;
Simon Kelley4f7b3042012-11-28 21:27:02 +0000469 }
470 } while ((crecp = cache_find_by_name(crecp, name, now, F_IPV4 | F_IPV6)));
471 }
472
Simon Kelleyb75e9362012-12-07 11:50:41 +0000473 *cut = '.'; /* restore domain part */
Simon Kelley4f7b3042012-11-28 21:27:02 +0000474 }
475
Simon Kelley86e3b9a2012-11-30 13:46:48 +0000476 if ((crecp = cache_find_by_name(NULL, name, now, F_IPV4 | F_IPV6)))
Simon Kelley4f7b3042012-11-28 21:27:02 +0000477 {
478 if ((crecp->flags & F_HOSTS) || (((crecp->flags & F_DHCP) && option_bool(OPT_DHCP_FQDN))))
479 do
480 {
481 nxdomain = 0;
Simon Kelley376d48c2013-11-13 13:04:30 +0000482 if ((crecp->flags & flag) && (local_query || filter_zone(zone, flag, &(crecp->addr.addr))))
Simon Kelley4f7b3042012-11-28 21:27:02 +0000483 {
484 log_query(crecp->flags, name, &crecp->addr.addr, record_source(crecp->uid));
Simon Kelley8273ea52012-11-29 21:12:33 +0000485 found = 1;
Simon Kelley4f7b3042012-11-28 21:27:02 +0000486 if (add_resource_record(header, limit, &trunc, nameoffset, &ansp,
487 daemon->auth_ttl, NULL, qtype, C_IN,
488 qtype == T_A ? "4" : "6", &crecp->addr))
Simon Kelley8273ea52012-11-29 21:12:33 +0000489 anscount++;
Simon Kelley4f7b3042012-11-28 21:27:02 +0000490 }
491 } while ((crecp = cache_find_by_name(crecp, name, now, F_IPV4 | F_IPV6)));
492 }
Simon Kelley8273ea52012-11-29 21:12:33 +0000493
Simon Kelley4f7b3042012-11-28 21:27:02 +0000494 if (!found)
495 log_query(flag | F_NEG | (nxdomain ? F_NXDOMAIN : 0) | F_FORWARD | F_AUTH, name, NULL, NULL);
Simon Kelley8273ea52012-11-29 21:12:33 +0000496
Simon Kelley4f7b3042012-11-28 21:27:02 +0000497 }
Simon Kelley45dd1fe2012-12-04 20:49:24 +0000498
Simon Kelley4f7b3042012-11-28 21:27:02 +0000499 /* Add auth section */
Simon Kelleyaa67fe72013-02-04 21:32:34 +0000500 if (auth && zone)
Simon Kelley4f7b3042012-11-28 21:27:02 +0000501 {
Simon Kelleyb75e9362012-12-07 11:50:41 +0000502 char *authname;
Simon Kelleye1ff4192012-12-09 17:08:47 +0000503 int newoffset, offset = 0;
504
Simon Kelley45dd1fe2012-12-04 20:49:24 +0000505 if (!subnet)
Simon Kelleyb75e9362012-12-07 11:50:41 +0000506 authname = zone->domain;
Simon Kelley45dd1fe2012-12-04 20:49:24 +0000507 else
508 {
509 /* handle NS and SOA for PTR records */
Simon Kelleyb75e9362012-12-07 11:50:41 +0000510
511 authname = name;
512
Simon Kelley376d48c2013-11-13 13:04:30 +0000513 if (!(subnet->flags & ADDRLIST_IPV6))
Simon Kelley45dd1fe2012-12-04 20:49:24 +0000514 {
Simon Kelley376d48c2013-11-13 13:04:30 +0000515 in_addr_t a = ntohl(subnet->addr.addr.addr4.s_addr) >> 8;
Simon Kelley45dd1fe2012-12-04 20:49:24 +0000516 char *p = name;
517
Simon Kelleybaa80ae2013-05-29 16:32:07 +0100518 if (subnet->prefixlen >= 24)
Simon Kelley45dd1fe2012-12-04 20:49:24 +0000519 p += sprintf(p, "%d.", a & 0xff);
520 a = a >> 8;
Simon Kelleybaa80ae2013-05-29 16:32:07 +0100521 if (subnet->prefixlen >= 16 )
Simon Kelley45dd1fe2012-12-04 20:49:24 +0000522 p += sprintf(p, "%d.", a & 0xff);
523 a = a >> 8;
524 p += sprintf(p, "%d.in-addr.arpa", a & 0xff);
525
526 }
527#ifdef HAVE_IPV6
528 else
529 {
530 char *p = name;
531 int i;
532
533 for (i = subnet->prefixlen-1; i >= 0; i -= 4)
534 {
Simon Kelley376d48c2013-11-13 13:04:30 +0000535 int dig = ((unsigned char *)&subnet->addr.addr.addr6)[i>>3];
Simon Kelley45dd1fe2012-12-04 20:49:24 +0000536 p += sprintf(p, "%.1x.", (i>>2) & 1 ? dig & 15 : dig >> 4);
537 }
538 p += sprintf(p, "ip6.arpa");
539
540 }
541#endif
542 }
543
544 /* handle NS and SOA in auth section or for explicit queries */
Simon Kelleye1ff4192012-12-09 17:08:47 +0000545 newoffset = ansp - (unsigned char *)header;
546 if (((anscount == 0 && !ns) || soa) &&
Simon Kelley45dd1fe2012-12-04 20:49:24 +0000547 add_resource_record(header, limit, &trunc, 0, &ansp,
548 daemon->auth_ttl, NULL, T_SOA, C_IN, "ddlllll",
Simon Kelleyb75e9362012-12-07 11:50:41 +0000549 authname, daemon->authserver, daemon->hostmaster,
Simon Kelley45dd1fe2012-12-04 20:49:24 +0000550 daemon->soa_sn, daemon->soa_refresh,
551 daemon->soa_retry, daemon->soa_expiry,
552 daemon->auth_ttl))
553 {
Simon Kelleye1ff4192012-12-09 17:08:47 +0000554 offset = newoffset;
Simon Kelley45dd1fe2012-12-04 20:49:24 +0000555 if (soa)
556 anscount++;
557 else
558 authcount++;
559 }
Simon Kelleye1ff4192012-12-09 17:08:47 +0000560
561 if (anscount != 0 || ns)
562 {
563 struct name_list *secondary;
564
565 newoffset = ansp - (unsigned char *)header;
566 if (add_resource_record(header, limit, &trunc, -offset, &ansp,
567 daemon->auth_ttl, NULL, T_NS, C_IN, "d", offset == 0 ? authname : NULL, daemon->authserver))
568 {
569 if (offset == 0)
570 offset = newoffset;
571 if (ns)
572 anscount++;
573 else
574 authcount++;
575 }
Simon Kelleyb75e9362012-12-07 11:50:41 +0000576
Simon Kelleye1ff4192012-12-09 17:08:47 +0000577 if (!subnet)
578 for (secondary = daemon->secondary_forward_server; secondary; secondary = secondary->next)
579 if (add_resource_record(header, limit, &trunc, offset, &ansp,
580 daemon->auth_ttl, NULL, T_NS, C_IN, "d", secondary->name))
581 {
582 if (ns)
583 anscount++;
584 else
585 authcount++;
586 }
587 }
588
Simon Kelleyb75e9362012-12-07 11:50:41 +0000589 if (axfr)
590 {
Simon Kelleye1ff4192012-12-09 17:08:47 +0000591 for (rec = daemon->mxnames; rec; rec = rec->next)
592 if (in_zone(zone, rec->name, &cut))
593 {
594 if (cut)
595 *cut = 0;
596
597 if (rec->issrv)
598 {
599 if (add_resource_record(header, limit, &trunc, -axfroffset, &ansp, daemon->auth_ttl,
600 NULL, T_SRV, C_IN, "sssd", cut ? rec->name : NULL,
601 rec->priority, rec->weight, rec->srvport, rec->target))
602
603 anscount++;
604 }
605 else
606 {
607 if (add_resource_record(header, limit, &trunc, -axfroffset, &ansp, daemon->auth_ttl,
608 NULL, T_MX, C_IN, "sd", cut ? rec->name : NULL, rec->weight, rec->target))
609 anscount++;
610 }
611
612 /* restore config data */
613 if (cut)
614 *cut = '.';
615 }
616
617 for (txt = daemon->rr; txt; txt = txt->next)
618 if (in_zone(zone, txt->name, &cut))
619 {
620 if (cut)
621 *cut = 0;
622
623 if (add_resource_record(header, limit, &trunc, -axfroffset, &ansp, daemon->auth_ttl,
624 NULL, txt->class, C_IN, "t", cut ? txt->name : NULL, txt->len, txt->txt))
625 anscount++;
626
627 /* restore config data */
628 if (cut)
629 *cut = '.';
630 }
631
632 for (txt = daemon->txt; txt; txt = txt->next)
633 if (txt->class == C_IN && in_zone(zone, txt->name, &cut))
634 {
635 if (cut)
636 *cut = 0;
637
638 if (add_resource_record(header, limit, &trunc, -axfroffset, &ansp, daemon->auth_ttl,
639 NULL, T_TXT, C_IN, "t", cut ? txt->name : NULL, txt->len, txt->txt))
640 anscount++;
641
642 /* restore config data */
643 if (cut)
644 *cut = '.';
645 }
646
647 for (na = daemon->naptr; na; na = na->next)
648 if (in_zone(zone, na->name, &cut))
649 {
650 if (cut)
651 *cut = 0;
652
653 if (add_resource_record(header, limit, &trunc, -axfroffset, &ansp, daemon->auth_ttl,
654 NULL, T_NAPTR, C_IN, "sszzzd", cut ? na->name : NULL,
655 na->order, na->pref, na->flags, na->services, na->regexp, na->replace))
656 anscount++;
657
658 /* restore config data */
659 if (cut)
660 *cut = '.';
661 }
662
663 for (intr = daemon->int_names; intr; intr = intr->next)
Simon Kelley115ac3e2013-05-20 11:28:32 +0100664 if (in_zone(zone, intr->name, &cut))
Simon Kelleye1ff4192012-12-09 17:08:47 +0000665 {
Simon Kelley115ac3e2013-05-20 11:28:32 +0100666 struct addrlist *addrlist;
667
Simon Kelleye1ff4192012-12-09 17:08:47 +0000668 if (cut)
669 *cut = 0;
670
Simon Kelley376d48c2013-11-13 13:04:30 +0000671 for (addrlist = intr->addr; addrlist; addrlist = addrlist->next)
Simon Kelley587ad4f2013-11-15 15:47:51 +0000672 if (!(addrlist->flags & ADDRLIST_IPV6) &&
673 (local_query || filter_zone(zone, F_IPV4, &addrlist->addr)) &&
Simon Kelley115ac3e2013-05-20 11:28:32 +0100674 add_resource_record(header, limit, &trunc, -axfroffset, &ansp,
675 daemon->auth_ttl, NULL, T_A, C_IN, "4", cut ? intr->name : NULL, &addrlist->addr))
676 anscount++;
677
678#ifdef HAVE_IPV6
Simon Kelley376d48c2013-11-13 13:04:30 +0000679 for (addrlist = intr->addr; addrlist; addrlist = addrlist->next)
Simon Kelley587ad4f2013-11-15 15:47:51 +0000680 if ((addrlist->flags & ADDRLIST_IPV6) &&
681 (local_query || filter_zone(zone, F_IPV6, &addrlist->addr)) &&
Simon Kelley115ac3e2013-05-20 11:28:32 +0100682 add_resource_record(header, limit, &trunc, -axfroffset, &ansp,
683 daemon->auth_ttl, NULL, T_AAAA, C_IN, "6", cut ? intr->name : NULL, &addrlist->addr))
684 anscount++;
685#endif
Simon Kelleye1ff4192012-12-09 17:08:47 +0000686
687 /* restore config data */
688 if (cut)
689 *cut = '.';
690 }
Simon Kelley115ac3e2013-05-20 11:28:32 +0100691
Simon Kelleye1ff4192012-12-09 17:08:47 +0000692 for (a = daemon->cnames; a; a = a->next)
693 if (in_zone(zone, a->alias, &cut))
694 {
695 strcpy(name, a->target);
696 if (!strchr(name, '.'))
697 {
698 strcat(name, ".");
699 strcat(name, zone->domain);
700 }
701
702 if (cut)
703 *cut = 0;
704
705 if (add_resource_record(header, limit, &trunc, -axfroffset, &ansp,
706 daemon->auth_ttl, NULL,
707 T_CNAME, C_IN, "d", cut ? a->alias : NULL, name))
708 anscount++;
709 }
710
Simon Kelleyb75e9362012-12-07 11:50:41 +0000711 cache_enumerate(1);
712 while ((crecp = cache_enumerate(0)))
713 {
714 if ((crecp->flags & (F_IPV4 | F_IPV6)) &&
715 !(crecp->flags & (F_NEG | F_NXDOMAIN)) &&
716 (crecp->flags & F_FORWARD))
717 {
718 if ((crecp->flags & F_DHCP) && !option_bool(OPT_DHCP_FQDN))
719 {
720 char *cache_name = cache_get_name(crecp);
Simon Kelley19b16892013-10-20 10:19:39 +0100721 if (!strchr(cache_name, '.') &&
Simon Kelley376d48c2013-11-13 13:04:30 +0000722 (local_query || filter_zone(zone, (crecp->flags & (F_IPV6 | F_IPV4)), &(crecp->addr.addr))))
Simon Kelleyb75e9362012-12-07 11:50:41 +0000723 {
724 qtype = T_A;
725#ifdef HAVE_IPV6
726 if (crecp->flags & F_IPV6)
727 qtype = T_AAAA;
728#endif
729 if (add_resource_record(header, limit, &trunc, -axfroffset, &ansp,
730 daemon->auth_ttl, NULL, qtype, C_IN,
731 (crecp->flags & F_IPV4) ? "4" : "6", cache_name, &crecp->addr))
732 anscount++;
733 }
734 }
735
736 if ((crecp->flags & F_HOSTS) || (((crecp->flags & F_DHCP) && option_bool(OPT_DHCP_FQDN))))
737 {
738 strcpy(name, cache_get_name(crecp));
Simon Kelley19b16892013-10-20 10:19:39 +0100739 if (in_zone(zone, name, &cut) &&
Simon Kelley376d48c2013-11-13 13:04:30 +0000740 (local_query || filter_zone(zone, (crecp->flags & (F_IPV6 | F_IPV4)), &(crecp->addr.addr))))
Simon Kelleyb75e9362012-12-07 11:50:41 +0000741 {
742 qtype = T_A;
743#ifdef HAVE_IPV6
744 if (crecp->flags & F_IPV6)
745 qtype = T_AAAA;
746#endif
747 if (cut)
Simon Kelleye1ff4192012-12-09 17:08:47 +0000748 *cut = 0;
749
750 if (add_resource_record(header, limit, &trunc, -axfroffset, &ansp,
751 daemon->auth_ttl, NULL, qtype, C_IN,
752 (crecp->flags & F_IPV4) ? "4" : "6", cut ? name : NULL, &crecp->addr))
753 anscount++;
Simon Kelleyb75e9362012-12-07 11:50:41 +0000754 }
755 }
756 }
757 }
758
759 /* repeat SOA as last record */
760 if (add_resource_record(header, limit, &trunc, axfroffset, &ansp,
761 daemon->auth_ttl, NULL, T_SOA, C_IN, "ddlllll",
762 daemon->authserver, daemon->hostmaster,
763 daemon->soa_sn, daemon->soa_refresh,
764 daemon->soa_retry, daemon->soa_expiry,
765 daemon->auth_ttl))
766 anscount++;
Simon Kelley4c985da2013-03-22 14:07:38 +0000767
Simon Kelleyb75e9362012-12-07 11:50:41 +0000768 }
Simon Kelley4c985da2013-03-22 14:07:38 +0000769
770 }
Simon Kelley45dd1fe2012-12-04 20:49:24 +0000771
Simon Kelley4f7b3042012-11-28 21:27:02 +0000772 /* done all questions, set up header and return length of result */
773 /* clear authoritative and truncated flags, set QR flag */
774 header->hb3 = (header->hb3 & ~(HB3_AA | HB3_TC)) | HB3_QR;
Simon Kelley93bafe62013-10-21 21:19:34 +0100775
776 if (local_query)
777 {
778 /* set RA flag */
779 header->hb4 |= HB4_RA;
780 }
781 else
782 {
783 /* clear RA flag */
784 header->hb4 &= ~HB4_RA;
785 }
Simon Kelley4f7b3042012-11-28 21:27:02 +0000786
787 /* authoritive */
788 if (auth)
789 header->hb3 |= HB3_AA;
790
791 /* truncation */
792 if (trunc)
793 header->hb3 |= HB3_TC;
794
Simon Kelley57310502013-10-21 18:26:20 +0100795 if ((auth || local_query) && nxdomain)
Simon Kelley4f7b3042012-11-28 21:27:02 +0000796 SET_RCODE(header, NXDOMAIN);
797 else
798 SET_RCODE(header, NOERROR); /* no error */
799 header->ancount = htons(anscount);
800 header->nscount = htons(authcount);
Simon Kelleyaa792352012-12-06 19:41:35 +0000801 header->arcount = htons(0);
Simon Kelley4f7b3042012-11-28 21:27:02 +0000802 return ansp - (unsigned char *)header;
803}
804
Simon Kelley4820dce2012-12-18 18:30:30 +0000805#endif
Simon Kelleyb75e9362012-12-07 11:50:41 +0000806
Simon Kelley4f7b3042012-11-28 21:27:02 +0000807
808