s4 dns: Add support for PTR queries.
[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
32 static WERROR create_response_rr(const struct dns_name_question *question,
33                                  const struct dnsp_DnssrvRpcRecord *rec,
34                                  struct dns_res_rec **answers, uint16_t *ancount)
35 {
36         struct dns_res_rec *ans = *answers;
37         uint16_t ai = *ancount;
38
39         ZERO_STRUCT(ans[ai]);
40
41         switch (rec->wType) {
42         case DNS_QTYPE_CNAME:
43                 ans[ai].rdata.cname_record = talloc_strdup(ans, rec->data.cname);
44                 break;
45         case DNS_QTYPE_A:
46                 ans[ai].rdata.ipv4_record = talloc_strdup(ans, rec->data.ipv4);
47                 break;
48         case DNS_QTYPE_AAAA:
49                 ans[ai].rdata.ipv6_record = rec->data.ipv6;
50                 break;
51         case DNS_TYPE_NS:
52                 ans[ai].rdata.ns_record = rec->data.ns;
53                 break;
54         case DNS_QTYPE_SRV:
55                 ans[ai].rdata.srv_record.priority = rec->data.srv.wPriority;
56                 ans[ai].rdata.srv_record.weight   = rec->data.srv.wWeight;
57                 ans[ai].rdata.srv_record.port     = rec->data.srv.wPort;
58                 ans[ai].rdata.srv_record.target   = rec->data.srv.nameTarget;
59                 break;
60         case DNS_QTYPE_SOA:
61                 ans[ai].rdata.soa_record.mname   = rec->data.soa.mname;
62                 ans[ai].rdata.soa_record.rname   = rec->data.soa.rname;
63                 ans[ai].rdata.soa_record.serial  = rec->data.soa.serial;
64                 ans[ai].rdata.soa_record.refresh = rec->data.soa.refresh;
65                 ans[ai].rdata.soa_record.retry   = rec->data.soa.retry;
66                 ans[ai].rdata.soa_record.expire  = rec->data.soa.expire;
67                 ans[ai].rdata.soa_record.minimum = rec->data.soa.minimum;
68                 break;
69         case DNS_QTYPE_PTR:
70                 ans[ai].rdata.ptr_record = talloc_strdup(ans, rec->data.ptr);
71                 break;
72         default:
73                 return DNS_ERR(NOT_IMPLEMENTED);
74         }
75
76         ans[ai].name = talloc_strdup(ans, question->name);
77         ans[ai].rr_type = rec->wType;
78         ans[ai].rr_class = DNS_QCLASS_IN;
79         ans[ai].ttl = rec->dwTtlSeconds;
80         ans[ai].length = UINT16_MAX;
81         ai++;
82
83         *answers = ans;
84         *ancount = ai;
85
86         return WERR_OK;
87 }
88
89 static WERROR handle_question(struct dns_server *dns,
90                               TALLOC_CTX *mem_ctx,
91                               const struct dns_name_question *question,
92                               struct dns_res_rec **answers, uint16_t *ancount)
93 {
94         struct dns_res_rec *ans;
95         WERROR werror;
96         unsigned int ri;
97         struct dnsp_DnssrvRpcRecord *recs;
98         uint16_t rec_count, ai = 0;
99         struct ldb_dn *dn = NULL;
100
101         werror = dns_name2dn(dns, mem_ctx, question->name, &dn);
102         W_ERROR_NOT_OK_RETURN(werror);
103
104         werror = dns_lookup_records(dns, mem_ctx, dn, &recs, &rec_count);
105         W_ERROR_NOT_OK_RETURN(werror);
106
107         ans = talloc_zero_array(mem_ctx, struct dns_res_rec, rec_count);
108         W_ERROR_HAVE_NO_MEMORY(ans);
109
110         for (ri = 0; ri < rec_count; ri++) {
111                 if ((question->question_type != DNS_QTYPE_ALL) &&
112                     (recs[ri].wType != question->question_type)) {
113                         continue;
114                 }
115                 werror = create_response_rr(question, &recs[ri], &ans, &ai);
116                 W_ERROR_NOT_OK_RETURN(werror);
117         }
118
119         if (ai == 0) {
120                 return DNS_ERR(NAME_ERROR);
121         }
122
123         *ancount = ai;
124         *answers = ans;
125
126         return WERR_OK;
127
128 }
129
130 WERROR dns_server_process_query(struct dns_server *dns,
131                                 TALLOC_CTX *mem_ctx,
132                                 struct dns_name_packet *in,
133                                 struct dns_res_rec **answers,    uint16_t *ancount,
134                                 struct dns_res_rec **nsrecs,     uint16_t *nscount,
135                                 struct dns_res_rec **additional, uint16_t *arcount)
136 {
137         uint16_t num_answers=0;
138         struct dns_res_rec *ans=NULL;
139         WERROR werror;
140
141         if (in->qdcount != 1) {
142                 return DNS_ERR(FORMAT_ERROR);
143         }
144
145         /* Windows returns NOT_IMPLEMENTED on this as well */
146         if (in->questions[0].question_class == DNS_QCLASS_NONE) {
147                 return DNS_ERR(NOT_IMPLEMENTED);
148         }
149
150         werror = handle_question(dns, mem_ctx, &in->questions[0], &ans, &num_answers);
151         W_ERROR_NOT_OK_GOTO(werror, query_failed);
152
153         *answers = ans;
154         *ancount = num_answers;
155
156         /*FIXME: Do something for these */
157         *nsrecs  = NULL;
158         *nscount = 0;
159
160         *additional = NULL;
161         *arcount    = 0;
162
163         return WERR_OK;
164
165 query_failed:
166         /*FIXME: add our SOA record to nsrecs */
167         return werror;
168 }