s4 dns: Forward questions we can't answer to another server
[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 "libcli/util/werror.h"
24 #include "librpc/ndr/libndr.h"
25 #include "librpc/gen_ndr/ndr_dns.h"
26 #include "librpc/gen_ndr/ndr_dnsp.h"
27 #include <ldb.h>
28 #include "dsdb/samdb/samdb.h"
29 #include "dsdb/common/util.h"
30 #include "dns_server/dns_server.h"
31 #include "libcli/dns/libdns.h"
32
33 static WERROR create_response_rr(const struct dns_name_question *question,
34                                  const struct dnsp_DnssrvRpcRecord *rec,
35                                  struct dns_res_rec **answers, uint16_t *ancount)
36 {
37         struct dns_res_rec *ans = *answers;
38         uint16_t ai = *ancount;
39         char *tmp;
40         uint32_t i;
41
42         ZERO_STRUCT(ans[ai]);
43
44         switch (rec->wType) {
45         case DNS_QTYPE_CNAME:
46                 ans[ai].rdata.cname_record = talloc_strdup(ans, rec->data.cname);
47                 break;
48         case DNS_QTYPE_A:
49                 ans[ai].rdata.ipv4_record = talloc_strdup(ans, rec->data.ipv4);
50                 break;
51         case DNS_QTYPE_AAAA:
52                 ans[ai].rdata.ipv6_record = rec->data.ipv6;
53                 break;
54         case DNS_TYPE_NS:
55                 ans[ai].rdata.ns_record = rec->data.ns;
56                 break;
57         case DNS_QTYPE_SRV:
58                 ans[ai].rdata.srv_record.priority = rec->data.srv.wPriority;
59                 ans[ai].rdata.srv_record.weight   = rec->data.srv.wWeight;
60                 ans[ai].rdata.srv_record.port     = rec->data.srv.wPort;
61                 ans[ai].rdata.srv_record.target   = rec->data.srv.nameTarget;
62                 break;
63         case DNS_QTYPE_SOA:
64                 ans[ai].rdata.soa_record.mname   = rec->data.soa.mname;
65                 ans[ai].rdata.soa_record.rname   = rec->data.soa.rname;
66                 ans[ai].rdata.soa_record.serial  = rec->data.soa.serial;
67                 ans[ai].rdata.soa_record.refresh = rec->data.soa.refresh;
68                 ans[ai].rdata.soa_record.retry   = rec->data.soa.retry;
69                 ans[ai].rdata.soa_record.expire  = rec->data.soa.expire;
70                 ans[ai].rdata.soa_record.minimum = rec->data.soa.minimum;
71                 break;
72         case DNS_QTYPE_PTR:
73                 ans[ai].rdata.ptr_record = talloc_strdup(ans, rec->data.ptr);
74                 break;
75         case DNS_QTYPE_TXT:
76                 tmp = talloc_asprintf(ans, "\"%s\"", rec->data.txt.str[0]);
77                 for (i=1; i<rec->data.txt.count; i++) {
78                         tmp = talloc_asprintf_append(tmp, " \"%s\"",
79                                                      rec->data.txt.str[i]);
80                 }
81                 ans[ai].rdata.txt_record.txt = tmp;
82                 break;
83         default:
84                 DEBUG(0, ("Got unhandled type %u query.\n", rec->wType));
85                 return DNS_ERR(NOT_IMPLEMENTED);
86         }
87
88         ans[ai].name = talloc_strdup(ans, question->name);
89         ans[ai].rr_type = rec->wType;
90         ans[ai].rr_class = DNS_QCLASS_IN;
91         ans[ai].ttl = rec->dwTtlSeconds;
92         ans[ai].length = UINT16_MAX;
93         ai++;
94
95         *answers = ans;
96         *ancount = ai;
97
98         return WERR_OK;
99 }
100
101 static WERROR ask_forwarder(TALLOC_CTX *mem_ctx,
102                             struct dns_name_question *question,
103                             struct dns_res_rec **answers, uint16_t *ancount,
104                             struct dns_res_rec **nsrecs, uint16_t *nscount,
105                             struct dns_res_rec **additional, uint16_t *arcount)
106 {
107         struct tevent_context *ev = tevent_context_init(mem_ctx);
108         struct dns_name_packet *out_packet, *in_packet;
109         uint16_t id = random();
110         DATA_BLOB out, in;
111         enum ndr_err_code ndr_err;
112         WERROR werr = WERR_OK;
113         struct tevent_req *req;
114
115         out_packet = talloc_zero(mem_ctx, struct dns_name_packet);
116         W_ERROR_HAVE_NO_MEMORY(out_packet);
117
118         out_packet->id = id;
119         out_packet->operation |= DNS_OPCODE_QUERY | DNS_FLAG_RECURSION_DESIRED;
120
121         out_packet->qdcount = 1;
122         out_packet->questions = question;
123
124         ndr_err = ndr_push_struct_blob(&out, mem_ctx, out_packet,
125                         (ndr_push_flags_fn_t)ndr_push_dns_name_packet);
126         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
127                 return DNS_ERR(SERVER_FAILURE);
128         }
129
130         /* FIXME: Don't hardcode this, get that from a configuration somewhere */
131         req = dns_udp_request_send(mem_ctx, ev, "127.0.0.1", out.data, out.length);
132         W_ERROR_HAVE_NO_MEMORY(req);
133
134         if(!tevent_req_poll(req, ev)) {
135                 return DNS_ERR(SERVER_FAILURE);
136         }
137
138         werr = dns_udp_request_recv(req, mem_ctx, &in.data, &in.length);
139         W_ERROR_NOT_OK_RETURN(werr);
140
141         in_packet = talloc_zero(mem_ctx, struct dns_name_packet);
142         W_ERROR_HAVE_NO_MEMORY(in_packet);
143
144         ndr_err = ndr_pull_struct_blob(&in, in_packet, in_packet,
145                         (ndr_pull_flags_fn_t)ndr_pull_dns_name_packet);
146         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
147                 return DNS_ERR(SERVER_FAILURE);
148         }
149
150         if (in_packet->id != id) {
151                 DEBUG(0, ("DNS packet id mismatch: 0x%0x, expected 0x%0x\n",
152                           in_packet->id, id));
153                 return DNS_ERR(NAME_ERROR);
154         }
155
156         *ancount = in_packet->ancount;
157         *answers = talloc_move(mem_ctx, &in_packet->answers);
158
159         *nscount = in_packet->nscount;
160         *nsrecs = talloc_move(mem_ctx, &in_packet->nsrecs);
161
162         *arcount = in_packet->arcount;
163         *additional = talloc_move(mem_ctx, &in_packet->additional);
164
165         return werr;
166 }
167
168 static WERROR handle_question(struct dns_server *dns,
169                               TALLOC_CTX *mem_ctx,
170                               const struct dns_name_question *question,
171                               struct dns_res_rec **answers, uint16_t *ancount)
172 {
173         struct dns_res_rec *ans;
174         WERROR werror;
175         unsigned int ri;
176         struct dnsp_DnssrvRpcRecord *recs;
177         uint16_t rec_count, ai = 0;
178         struct ldb_dn *dn = NULL;
179
180         werror = dns_name2dn(dns, mem_ctx, question->name, &dn);
181         W_ERROR_NOT_OK_RETURN(werror);
182
183         werror = dns_lookup_records(dns, mem_ctx, dn, &recs, &rec_count);
184         W_ERROR_NOT_OK_RETURN(werror);
185
186         ans = talloc_zero_array(mem_ctx, struct dns_res_rec, rec_count);
187         W_ERROR_HAVE_NO_MEMORY(ans);
188
189         for (ri = 0; ri < rec_count; ri++) {
190                 if ((question->question_type != DNS_QTYPE_ALL) &&
191                     (recs[ri].wType != question->question_type)) {
192                         continue;
193                 }
194                 werror = create_response_rr(question, &recs[ri], &ans, &ai);
195                 W_ERROR_NOT_OK_RETURN(werror);
196         }
197
198         if (ai == 0) {
199                 return DNS_ERR(NAME_ERROR);
200         }
201
202         *ancount = ai;
203         *answers = ans;
204
205         return WERR_OK;
206
207 }
208
209 WERROR dns_server_process_query(struct dns_server *dns,
210                                 TALLOC_CTX *mem_ctx,
211                                 struct dns_name_packet *in,
212                                 struct dns_res_rec **answers,    uint16_t *ancount,
213                                 struct dns_res_rec **nsrecs,     uint16_t *nscount,
214                                 struct dns_res_rec **additional, uint16_t *arcount)
215 {
216         uint16_t num_answers=0, num_nsrecs=0, num_additional=0;
217         struct dns_res_rec *ans=NULL, *ns=NULL, *adds=NULL;
218         WERROR werror;
219
220         if (in->qdcount != 1) {
221                 return DNS_ERR(FORMAT_ERROR);
222         }
223
224         /* Windows returns NOT_IMPLEMENTED on this as well */
225         if (in->questions[0].question_class == DNS_QCLASS_NONE) {
226                 return DNS_ERR(NOT_IMPLEMENTED);
227         }
228
229         werror = handle_question(dns, mem_ctx, &in->questions[0], &ans, &num_answers);
230         if(W_ERROR_EQUAL(DNS_ERR(NAME_ERROR), werror)) {
231                 DEBUG(2, ("I don't feel responsible for '%s', forwarding\n", in->questions[0].name));
232                 werror = ask_forwarder(mem_ctx, &in->questions[0], &ans, &num_answers,
233                                        &ns, &num_nsrecs, &adds, &num_additional);
234         }
235         W_ERROR_NOT_OK_GOTO(werror, query_failed);
236
237         *answers = ans;
238         *ancount = num_answers;
239
240         /*FIXME: Do something for these */
241         *nsrecs  = ns;
242         *nscount = num_nsrecs;
243
244         *additional = adds;
245         *arcount    = num_additional;
246
247         return WERR_OK;
248
249 query_failed:
250         /*FIXME: add our SOA record to nsrecs */
251         return werror;
252 }