dlz_bind9: Build shared libraries for both BIND versions 9.8 and 9.9
[sfrench/samba-autobuild/.git] / source4 / dns_server / dns_query.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    DNS server handler for queries
5
6    Copyright (C) 2010 Kai Blin  <kai@samba.org>
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "smbd/service_task.h"
24 #include "libcli/util/werror.h"
25 #include "librpc/ndr/libndr.h"
26 #include "librpc/gen_ndr/ndr_dns.h"
27 #include "librpc/gen_ndr/ndr_dnsp.h"
28 #include <ldb.h>
29 #include "param/param.h"
30 #include "dsdb/samdb/samdb.h"
31 #include "dsdb/common/util.h"
32 #include "dns_server/dns_server.h"
33 #include "libcli/dns/libdns.h"
34 #include "lib/util/util_net.h"
35
36 static WERROR create_response_rr(const struct dns_name_question *question,
37                                  const struct dnsp_DnssrvRpcRecord *rec,
38                                  struct dns_res_rec **answers, uint16_t *ancount)
39 {
40         struct dns_res_rec *ans = *answers;
41         uint16_t ai = *ancount;
42         char *tmp;
43         uint32_t i;
44
45         ZERO_STRUCT(ans[ai]);
46
47         switch (rec->wType) {
48         case DNS_QTYPE_CNAME:
49                 ans[ai].rdata.cname_record = talloc_strdup(ans, rec->data.cname);
50                 break;
51         case DNS_QTYPE_A:
52                 ans[ai].rdata.ipv4_record = talloc_strdup(ans, rec->data.ipv4);
53                 break;
54         case DNS_QTYPE_AAAA:
55                 ans[ai].rdata.ipv6_record = rec->data.ipv6;
56                 break;
57         case DNS_TYPE_NS:
58                 ans[ai].rdata.ns_record = rec->data.ns;
59                 break;
60         case DNS_QTYPE_SRV:
61                 ans[ai].rdata.srv_record.priority = rec->data.srv.wPriority;
62                 ans[ai].rdata.srv_record.weight   = rec->data.srv.wWeight;
63                 ans[ai].rdata.srv_record.port     = rec->data.srv.wPort;
64                 ans[ai].rdata.srv_record.target   = rec->data.srv.nameTarget;
65                 break;
66         case DNS_QTYPE_SOA:
67                 ans[ai].rdata.soa_record.mname   = rec->data.soa.mname;
68                 ans[ai].rdata.soa_record.rname   = rec->data.soa.rname;
69                 ans[ai].rdata.soa_record.serial  = rec->data.soa.serial;
70                 ans[ai].rdata.soa_record.refresh = rec->data.soa.refresh;
71                 ans[ai].rdata.soa_record.retry   = rec->data.soa.retry;
72                 ans[ai].rdata.soa_record.expire  = rec->data.soa.expire;
73                 ans[ai].rdata.soa_record.minimum = rec->data.soa.minimum;
74                 break;
75         case DNS_QTYPE_PTR:
76                 ans[ai].rdata.ptr_record = talloc_strdup(ans, rec->data.ptr);
77                 break;
78         case DNS_QTYPE_TXT:
79                 tmp = talloc_asprintf(ans, "\"%s\"", rec->data.txt.str[0]);
80                 for (i=1; i<rec->data.txt.count; i++) {
81                         tmp = talloc_asprintf_append(tmp, " \"%s\"",
82                                                      rec->data.txt.str[i]);
83                 }
84                 ans[ai].rdata.txt_record.txt = tmp;
85                 break;
86         default:
87                 DEBUG(0, ("Got unhandled type %u query.\n", rec->wType));
88                 return DNS_ERR(NOT_IMPLEMENTED);
89         }
90
91         ans[ai].name = talloc_strdup(ans, question->name);
92         ans[ai].rr_type = rec->wType;
93         ans[ai].rr_class = DNS_QCLASS_IN;
94         ans[ai].ttl = rec->dwTtlSeconds;
95         ans[ai].length = UINT16_MAX;
96         ai++;
97
98         *answers = ans;
99         *ancount = ai;
100
101         return WERR_OK;
102 }
103
104 static WERROR ask_forwarder(struct dns_server *dns,
105                             TALLOC_CTX *mem_ctx,
106                             struct dns_name_question *question,
107                             struct dns_res_rec **answers, uint16_t *ancount,
108                             struct dns_res_rec **nsrecs, uint16_t *nscount,
109                             struct dns_res_rec **additional, uint16_t *arcount)
110 {
111         struct tevent_context *ev = tevent_context_init(mem_ctx);
112         struct dns_name_packet *out_packet, *in_packet;
113         uint16_t id = random();
114         DATA_BLOB out, in;
115         enum ndr_err_code ndr_err;
116         WERROR werr = WERR_OK;
117         struct tevent_req *req;
118         const char *forwarder = lpcfg_dns_forwarder(dns->task->lp_ctx);
119
120         if (!is_ipaddress(forwarder)) {
121                 DEBUG(0, ("Invalid 'dns forwarder' setting '%s', needs to be "
122                           "an IP address\n", forwarder));
123                 return DNS_ERR(NAME_ERROR);
124         }
125
126         out_packet = talloc_zero(mem_ctx, struct dns_name_packet);
127         W_ERROR_HAVE_NO_MEMORY(out_packet);
128
129         out_packet->id = id;
130         out_packet->operation |= DNS_OPCODE_QUERY | DNS_FLAG_RECURSION_DESIRED;
131
132         out_packet->qdcount = 1;
133         out_packet->questions = question;
134
135         ndr_err = ndr_push_struct_blob(&out, mem_ctx, out_packet,
136                         (ndr_push_flags_fn_t)ndr_push_dns_name_packet);
137         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
138                 return DNS_ERR(SERVER_FAILURE);
139         }
140
141         req = dns_udp_request_send(mem_ctx, ev, forwarder, out.data, out.length);
142         W_ERROR_HAVE_NO_MEMORY(req);
143
144         if(!tevent_req_poll(req, ev)) {
145                 return DNS_ERR(SERVER_FAILURE);
146         }
147
148         werr = dns_udp_request_recv(req, mem_ctx, &in.data, &in.length);
149         W_ERROR_NOT_OK_RETURN(werr);
150
151         in_packet = talloc_zero(mem_ctx, struct dns_name_packet);
152         W_ERROR_HAVE_NO_MEMORY(in_packet);
153
154         ndr_err = ndr_pull_struct_blob(&in, in_packet, in_packet,
155                         (ndr_pull_flags_fn_t)ndr_pull_dns_name_packet);
156         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
157                 return DNS_ERR(SERVER_FAILURE);
158         }
159
160         if (in_packet->id != id) {
161                 DEBUG(0, ("DNS packet id mismatch: 0x%0x, expected 0x%0x\n",
162                           in_packet->id, id));
163                 return DNS_ERR(NAME_ERROR);
164         }
165
166         *ancount = in_packet->ancount;
167         *answers = talloc_move(mem_ctx, &in_packet->answers);
168
169         *nscount = in_packet->nscount;
170         *nsrecs = talloc_move(mem_ctx, &in_packet->nsrecs);
171
172         *arcount = in_packet->arcount;
173         *additional = talloc_move(mem_ctx, &in_packet->additional);
174
175         return werr;
176 }
177
178 static WERROR handle_question(struct dns_server *dns,
179                               TALLOC_CTX *mem_ctx,
180                               const struct dns_name_question *question,
181                               struct dns_res_rec **answers, uint16_t *ancount)
182 {
183         struct dns_res_rec *ans;
184         WERROR werror;
185         unsigned int ri;
186         struct dnsp_DnssrvRpcRecord *recs;
187         uint16_t rec_count, ai = 0;
188         struct ldb_dn *dn = NULL;
189
190         werror = dns_name2dn(dns, mem_ctx, question->name, &dn);
191         W_ERROR_NOT_OK_RETURN(werror);
192
193         werror = dns_lookup_records(dns, mem_ctx, dn, &recs, &rec_count);
194         W_ERROR_NOT_OK_RETURN(werror);
195
196         ans = talloc_zero_array(mem_ctx, struct dns_res_rec, rec_count);
197         W_ERROR_HAVE_NO_MEMORY(ans);
198
199         for (ri = 0; ri < rec_count; ri++) {
200                 if ((question->question_type != DNS_QTYPE_ALL) &&
201                     (recs[ri].wType != question->question_type)) {
202                         continue;
203                 }
204                 werror = create_response_rr(question, &recs[ri], &ans, &ai);
205                 W_ERROR_NOT_OK_RETURN(werror);
206         }
207
208         if (ai == 0) {
209                 return DNS_ERR(NAME_ERROR);
210         }
211
212         *ancount = ai;
213         *answers = ans;
214
215         return WERR_OK;
216
217 }
218
219 WERROR dns_server_process_query(struct dns_server *dns,
220                                 struct dns_request_state *state,
221                                 TALLOC_CTX *mem_ctx,
222                                 struct dns_name_packet *in,
223                                 struct dns_res_rec **answers,    uint16_t *ancount,
224                                 struct dns_res_rec **nsrecs,     uint16_t *nscount,
225                                 struct dns_res_rec **additional, uint16_t *arcount)
226 {
227         uint16_t num_answers=0, num_nsrecs=0, num_additional=0;
228         struct dns_res_rec *ans=NULL, *ns=NULL, *adds=NULL;
229         WERROR werror;
230
231         if (in->qdcount != 1) {
232                 return DNS_ERR(FORMAT_ERROR);
233         }
234
235         /* Windows returns NOT_IMPLEMENTED on this as well */
236         if (in->questions[0].question_class == DNS_QCLASS_NONE) {
237                 return DNS_ERR(NOT_IMPLEMENTED);
238         }
239
240         if (dns_authorative_for_zone(dns, in->questions[0].name)) {
241                 state->flags |= DNS_FLAG_AUTHORITATIVE;
242                 werror = handle_question(dns, mem_ctx, &in->questions[0],
243                                          &ans, &num_answers);
244         } else {
245                 if (state->flags & DNS_FLAG_RECURSION_DESIRED &&
246                     state->flags & DNS_FLAG_RECURSION_AVAIL) {
247                         DEBUG(2, ("Not authorative for '%s', forwarding\n",
248                                   in->questions[0].name));
249                         werror = ask_forwarder(dns, mem_ctx, &in->questions[0],
250                                                &ans, &num_answers,
251                                                &ns, &num_nsrecs,
252                                                &adds, &num_additional);
253                 } else {
254                         werror = DNS_ERR(NAME_ERROR);
255                 }
256         }
257         W_ERROR_NOT_OK_GOTO(werror, query_failed);
258
259         *answers = ans;
260         *ancount = num_answers;
261
262         /*FIXME: Do something for these */
263         *nsrecs  = ns;
264         *nscount = num_nsrecs;
265
266         *additional = adds;
267         *arcount    = num_additional;
268
269         return WERR_OK;
270
271 query_failed:
272         /*FIXME: add our SOA record to nsrecs */
273         return werror;
274 }