3564140948b3809e30542f7b2c94ae23238ea5b0
[kai/samba.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                 if (ans[ai].rdata.cname_record == NULL) {
51                         return WERR_NOMEM;
52                 }
53                 break;
54         case DNS_QTYPE_A:
55                 ans[ai].rdata.ipv4_record = talloc_strdup(ans, rec->data.ipv4);
56                 if (ans[ai].rdata.ipv4_record == NULL) {
57                         return WERR_NOMEM;
58                 }
59                 break;
60         case DNS_QTYPE_AAAA:
61                 ans[ai].rdata.ipv6_record = rec->data.ipv6;
62                 break;
63         case DNS_TYPE_NS:
64                 ans[ai].rdata.ns_record = rec->data.ns;
65                 break;
66         case DNS_QTYPE_SRV:
67                 ans[ai].rdata.srv_record.priority = rec->data.srv.wPriority;
68                 ans[ai].rdata.srv_record.weight   = rec->data.srv.wWeight;
69                 ans[ai].rdata.srv_record.port     = rec->data.srv.wPort;
70                 ans[ai].rdata.srv_record.target   = talloc_strdup(
71                         ans, rec->data.srv.nameTarget);
72                 if (ans[ai].rdata.srv_record.target == NULL) {
73                         return WERR_NOMEM;
74                 }
75                 break;
76         case DNS_QTYPE_SOA:
77                 ans[ai].rdata.soa_record.mname   = talloc_strdup(
78                         ans, rec->data.soa.mname);
79                 if (ans[ai].rdata.soa_record.mname == NULL) {
80                         return WERR_NOMEM;
81                 }
82                 ans[ai].rdata.soa_record.rname   = talloc_strdup(
83                         ans, rec->data.soa.rname);
84                 if (ans[ai].rdata.soa_record.rname == NULL) {
85                         return WERR_NOMEM;
86                 }
87                 ans[ai].rdata.soa_record.serial  = rec->data.soa.serial;
88                 ans[ai].rdata.soa_record.refresh = rec->data.soa.refresh;
89                 ans[ai].rdata.soa_record.retry   = rec->data.soa.retry;
90                 ans[ai].rdata.soa_record.expire  = rec->data.soa.expire;
91                 ans[ai].rdata.soa_record.minimum = rec->data.soa.minimum;
92                 break;
93         case DNS_QTYPE_PTR:
94                 ans[ai].rdata.ptr_record = talloc_strdup(ans, rec->data.ptr);
95                 break;
96         case DNS_QTYPE_TXT:
97                 tmp = talloc_asprintf(ans, "\"%s\"", rec->data.txt.str[0]);
98                 if (tmp == NULL) {
99                         return WERR_NOMEM;
100                 }
101                 for (i=1; i<rec->data.txt.count; i++) {
102                         tmp = talloc_asprintf_append_buffer(
103                                 tmp, " \"%s\"", rec->data.txt.str[i]);
104                         if (tmp == NULL) {
105                                 return WERR_NOMEM;
106                         }
107                 }
108                 ans[ai].rdata.txt_record.txt = tmp;
109                 break;
110         default:
111                 DEBUG(0, ("Got unhandled type %u query.\n", rec->wType));
112                 return DNS_ERR(NOT_IMPLEMENTED);
113         }
114
115         ans[ai].name = talloc_strdup(ans, question->name);
116         if (ans[ai].name == NULL) {
117                 return WERR_NOMEM;
118         }
119         ans[ai].rr_type = rec->wType;
120         ans[ai].rr_class = DNS_QCLASS_IN;
121         ans[ai].ttl = rec->dwTtlSeconds;
122         ans[ai].length = UINT16_MAX;
123         ai++;
124
125         *answers = ans;
126         *ancount = ai;
127
128         return WERR_OK;
129 }
130
131 static WERROR ask_forwarder(struct dns_server *dns,
132                             TALLOC_CTX *mem_ctx,
133                             struct dns_name_question *question,
134                             struct dns_res_rec **answers, uint16_t *ancount,
135                             struct dns_res_rec **nsrecs, uint16_t *nscount,
136                             struct dns_res_rec **additional, uint16_t *arcount)
137 {
138         struct tevent_context *ev = tevent_context_init(mem_ctx);
139         struct dns_name_packet *out_packet, *in_packet;
140         uint16_t id = random();
141         DATA_BLOB out, in;
142         enum ndr_err_code ndr_err;
143         WERROR werr = WERR_OK;
144         struct tevent_req *req;
145         const char *forwarder = lpcfg_dns_forwarder(dns->task->lp_ctx);
146
147         if (!is_ipaddress(forwarder)) {
148                 DEBUG(0, ("Invalid 'dns forwarder' setting '%s', needs to be "
149                           "an IP address\n", forwarder));
150                 return DNS_ERR(NAME_ERROR);
151         }
152
153         out_packet = talloc_zero(mem_ctx, struct dns_name_packet);
154         W_ERROR_HAVE_NO_MEMORY(out_packet);
155
156         out_packet->id = id;
157         out_packet->operation |= DNS_OPCODE_QUERY | DNS_FLAG_RECURSION_DESIRED;
158
159         out_packet->qdcount = 1;
160         out_packet->questions = question;
161
162         ndr_err = ndr_push_struct_blob(&out, mem_ctx, out_packet,
163                         (ndr_push_flags_fn_t)ndr_push_dns_name_packet);
164         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
165                 return DNS_ERR(SERVER_FAILURE);
166         }
167
168         req = dns_udp_request_send(mem_ctx, ev, forwarder, out.data, out.length);
169         W_ERROR_HAVE_NO_MEMORY(req);
170
171         if(!tevent_req_poll(req, ev)) {
172                 return DNS_ERR(SERVER_FAILURE);
173         }
174
175         werr = dns_udp_request_recv(req, mem_ctx, &in.data, &in.length);
176         W_ERROR_NOT_OK_RETURN(werr);
177
178         in_packet = talloc_zero(mem_ctx, struct dns_name_packet);
179         W_ERROR_HAVE_NO_MEMORY(in_packet);
180
181         ndr_err = ndr_pull_struct_blob(&in, in_packet, in_packet,
182                         (ndr_pull_flags_fn_t)ndr_pull_dns_name_packet);
183         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
184                 return DNS_ERR(SERVER_FAILURE);
185         }
186
187         if (in_packet->id != id) {
188                 DEBUG(0, ("DNS packet id mismatch: 0x%0x, expected 0x%0x\n",
189                           in_packet->id, id));
190                 return DNS_ERR(NAME_ERROR);
191         }
192
193         *ancount = in_packet->ancount;
194         *answers = talloc_move(mem_ctx, &in_packet->answers);
195
196         *nscount = in_packet->nscount;
197         *nsrecs = talloc_move(mem_ctx, &in_packet->nsrecs);
198
199         *arcount = in_packet->arcount;
200         *additional = talloc_move(mem_ctx, &in_packet->additional);
201
202         return werr;
203 }
204
205 static WERROR handle_question(struct dns_server *dns,
206                               TALLOC_CTX *mem_ctx,
207                               const struct dns_name_question *question,
208                               struct dns_res_rec **answers, uint16_t *ancount)
209 {
210         struct dns_res_rec *ans;
211         WERROR werror;
212         unsigned int ri;
213         struct dnsp_DnssrvRpcRecord *recs;
214         uint16_t rec_count, ai = 0;
215         struct ldb_dn *dn = NULL;
216
217         werror = dns_name2dn(dns, mem_ctx, question->name, &dn);
218         W_ERROR_NOT_OK_RETURN(werror);
219
220         werror = dns_lookup_records(dns, mem_ctx, dn, &recs, &rec_count);
221         W_ERROR_NOT_OK_RETURN(werror);
222
223         ans = talloc_zero_array(mem_ctx, struct dns_res_rec, rec_count);
224         W_ERROR_HAVE_NO_MEMORY(ans);
225
226         for (ri = 0; ri < rec_count; ri++) {
227                 if ((question->question_type != DNS_QTYPE_ALL) &&
228                     (recs[ri].wType != question->question_type)) {
229                         continue;
230                 }
231                 werror = create_response_rr(question, &recs[ri], &ans, &ai);
232                 W_ERROR_NOT_OK_RETURN(werror);
233         }
234
235         if (ai == 0) {
236                 return DNS_ERR(NAME_ERROR);
237         }
238
239         *ancount = ai;
240         *answers = ans;
241
242         return WERR_OK;
243
244 }
245
246 WERROR dns_server_process_query(struct dns_server *dns,
247                                 struct dns_request_state *state,
248                                 TALLOC_CTX *mem_ctx,
249                                 struct dns_name_packet *in,
250                                 struct dns_res_rec **answers,    uint16_t *ancount,
251                                 struct dns_res_rec **nsrecs,     uint16_t *nscount,
252                                 struct dns_res_rec **additional, uint16_t *arcount)
253 {
254         uint16_t num_answers=0, num_nsrecs=0, num_additional=0;
255         struct dns_res_rec *ans=NULL, *ns=NULL, *adds=NULL;
256         WERROR werror;
257
258         if (in->qdcount != 1) {
259                 return DNS_ERR(FORMAT_ERROR);
260         }
261
262         /* Windows returns NOT_IMPLEMENTED on this as well */
263         if (in->questions[0].question_class == DNS_QCLASS_NONE) {
264                 return DNS_ERR(NOT_IMPLEMENTED);
265         }
266
267         if (dns_authorative_for_zone(dns, in->questions[0].name)) {
268                 state->flags |= DNS_FLAG_AUTHORITATIVE;
269                 werror = handle_question(dns, mem_ctx, &in->questions[0],
270                                          &ans, &num_answers);
271         } else {
272                 if (state->flags & DNS_FLAG_RECURSION_DESIRED &&
273                     state->flags & DNS_FLAG_RECURSION_AVAIL) {
274                         DEBUG(2, ("Not authoritative for '%s', forwarding\n",
275                                   in->questions[0].name));
276                         werror = ask_forwarder(dns, mem_ctx, &in->questions[0],
277                                                &ans, &num_answers,
278                                                &ns, &num_nsrecs,
279                                                &adds, &num_additional);
280                 } else {
281                         werror = DNS_ERR(NAME_ERROR);
282                 }
283         }
284         W_ERROR_NOT_OK_GOTO(werror, query_failed);
285
286         *answers = ans;
287         *ancount = num_answers;
288
289         /*FIXME: Do something for these */
290         *nsrecs  = ns;
291         *nscount = num_nsrecs;
292
293         *additional = adds;
294         *arcount    = num_additional;
295
296         return WERR_OK;
297
298 query_failed:
299         /*FIXME: add our SOA record to nsrecs */
300         return werror;
301 }