s3-torture/utable.c: replace cli_read_old() with cli_read()
[Samba/gebeck_regimport.git] / source4 / dns_server / dns_query.c
blobf730a70b75ea1a600fecbbe99a77b33b6f7b044d
1 /*
2 Unix SMB/CIFS implementation.
4 DNS server handler for queries
6 Copyright (C) 2010 Kai Blin <kai@samba.org>
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.
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.
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/>.
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"
32 static WERROR handle_question(struct dns_server *dns,
33 TALLOC_CTX *mem_ctx,
34 const struct dns_name_question *question,
35 struct dns_res_rec **answers, uint16_t *ancount)
37 struct dns_res_rec *ans;
38 struct ldb_dn *dn = NULL;
39 WERROR werror;
40 static const char * const attrs[] = { "dnsRecord", NULL};
41 int ret;
42 uint16_t ai = *ancount;
43 unsigned int ri;
44 struct ldb_message *msg = NULL;
45 struct dnsp_DnssrvRpcRecord *recs;
46 struct ldb_message_element *el;
48 werror = dns_name2dn(dns, mem_ctx, question->name, &dn);
49 W_ERROR_NOT_OK_RETURN(werror);
51 ret = dsdb_search_one(dns->samdb, mem_ctx, &msg, dn,
52 LDB_SCOPE_BASE, attrs, 0, "%s", "(objectClass=dnsNode)");
53 if (ret != LDB_SUCCESS) {
54 return DNS_ERR(NAME_ERROR);
57 el = ldb_msg_find_element(msg, attrs[0]);
58 if (el == NULL) {
59 return DNS_ERR(NAME_ERROR);
62 recs = talloc_array(mem_ctx, struct dnsp_DnssrvRpcRecord, el->num_values);
63 for (ri = 0; ri < el->num_values; ri++) {
64 struct ldb_val *v = &el->values[ri];
65 enum ndr_err_code ndr_err;
67 ndr_err = ndr_pull_struct_blob(v, recs, &recs[ri],
68 (ndr_pull_flags_fn_t)ndr_pull_dnsp_DnssrvRpcRecord);
69 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
70 DEBUG(0, ("Failed to grab dnsp_DnssrvRpcRecord\n"));
71 return DNS_ERR(SERVER_FAILURE);
75 ans = talloc_realloc(mem_ctx, *answers, struct dns_res_rec,
76 ai + el->num_values);
77 W_ERROR_HAVE_NO_MEMORY(ans);
79 switch (question->question_type) {
80 case DNS_QTYPE_CNAME:
81 for (ri = 0; ri < el->num_values; ri++) {
82 if (recs[ri].wType != question->question_type) {
83 continue;
86 ZERO_STRUCT(ans[ai]);
87 ans[ai].name = talloc_strdup(ans, question->name);
88 ans[ai].rr_type = DNS_QTYPE_CNAME;
89 ans[ai].rr_class = DNS_QCLASS_IP;
90 ans[ai].ttl = recs[ri].dwTtlSeconds;
91 ans[ai].length = UINT16_MAX;
92 ans[ai].rdata.cname_record = talloc_strdup(ans, recs[ri].data.cname);
93 ai++;
95 break;
96 case DNS_QTYPE_A:
97 for (ri = 0; ri < el->num_values; ri++) {
98 if (recs[ri].wType != question->question_type) {
99 continue;
102 /* TODO: if the record actually is a DNS_QTYPE_A */
104 ZERO_STRUCT(ans[ai]);
105 ans[ai].name = talloc_strdup(ans, question->name);
106 ans[ai].rr_type = DNS_QTYPE_A;
107 ans[ai].rr_class = DNS_QCLASS_IP;
108 ans[ai].ttl = recs[ri].dwTtlSeconds;
109 ans[ai].length = UINT16_MAX;
110 ans[ai].rdata.ipv4_record = talloc_strdup(ans, recs[ri].data.ipv4);
111 ai++;
113 break;
114 case DNS_QTYPE_AAAA:
115 for (ri = 0; ri < el->num_values; ri++) {
116 if (recs[ri].wType != question->question_type) {
117 continue;
120 ZERO_STRUCT(ans[ai]);
121 ans[ai].name = talloc_strdup(ans, question->name);
122 ans[ai].rr_type = DNS_QTYPE_AAAA;
123 ans[ai].rr_class = DNS_QCLASS_IP;
124 ans[ai].ttl = recs[ri].dwTtlSeconds;
125 ans[ai].length = UINT16_MAX;
126 ans[ai].rdata.ipv6_record = recs[ri].data.ipv6;
127 ai++;
129 break;
130 case DNS_QTYPE_NS:
131 for (ri = 0; ri < el->num_values; ri++) {
132 if (recs[ri].wType != question->question_type) {
133 continue;
136 ZERO_STRUCT(ans[ai]);
137 ans[ai].name = question->name;
138 ans[ai].rr_type = DNS_QTYPE_NS;
139 ans[ai].rr_class = DNS_QCLASS_IP;
140 ans[ai].ttl = recs[ri].dwTtlSeconds;
141 ans[ai].length = UINT16_MAX;
142 ans[ai].rdata.ns_record = recs[ri].data.ns;
143 ai++;
145 break;
146 case DNS_QTYPE_SRV:
147 for (ri = 0; ri < el->num_values; ri++) {
148 if (recs[ri].wType != question->question_type) {
149 continue;
152 ZERO_STRUCT(ans[ai]);
153 ans[ai].name = question->name;
154 ans[ai].rr_type = DNS_QTYPE_SRV;
155 ans[ai].rr_class = DNS_QCLASS_IP;
156 ans[ai].ttl = recs[ri].dwTtlSeconds;
157 ans[ai].length = UINT16_MAX;
158 ans[ai].rdata.srv_record.priority = recs[ri].data.srv.wPriority;
159 ans[ai].rdata.srv_record.weight = recs[ri].data.srv.wWeight;
160 ans[ai].rdata.srv_record.port = recs[ri].data.srv.wPort;
161 ans[ai].rdata.srv_record.target = recs[ri].data.srv.nameTarget;
162 ai++;
164 break;
165 case DNS_QTYPE_SOA:
166 for (ri = 0; ri < el->num_values; ri++) {
167 if (recs[ri].wType != question->question_type) {
168 continue;
171 ZERO_STRUCT(ans[ai]);
172 ans[ai].name = question->name;
173 ans[ai].rr_type = DNS_QTYPE_SOA;
174 ans[ai].rr_class = DNS_QCLASS_IP;
175 ans[ai].ttl = recs[ri].dwTtlSeconds;
176 ans[ai].length = UINT16_MAX;
177 ans[ai].rdata.soa_record.mname = recs[ri].data.soa.mname;
178 ans[ai].rdata.soa_record.rname = recs[ri].data.soa.rname;
179 ans[ai].rdata.soa_record.serial = recs[ri].data.soa.serial;
180 ans[ai].rdata.soa_record.refresh= recs[ri].data.soa.refresh;
181 ans[ai].rdata.soa_record.retry = recs[ri].data.soa.retry;
182 ans[ai].rdata.soa_record.expire = recs[ri].data.soa.expire;
183 ans[ai].rdata.soa_record.minimum= recs[ri].data.soa.minimum;
184 ai++;
186 break;
187 default:
188 return DNS_ERR(NOT_IMPLEMENTED);
191 if (*ancount == ai) {
192 return DNS_ERR(NAME_ERROR);
195 *ancount = ai;
196 *answers = ans;
198 return WERR_OK;
202 WERROR dns_server_process_query(struct dns_server *dns,
203 TALLOC_CTX *mem_ctx,
204 struct dns_name_packet *in,
205 struct dns_res_rec **answers, uint16_t *ancount,
206 struct dns_res_rec **nsrecs, uint16_t *nscount,
207 struct dns_res_rec **additional, uint16_t *arcount)
209 uint16_t i, num_answers=0;
210 struct dns_res_rec *ans=NULL;
211 WERROR werror;
213 ans = talloc_array(mem_ctx, struct dns_res_rec, 0);
214 W_ERROR_HAVE_NO_MEMORY(ans);
216 for (i = 0; i < in->qdcount; ++i) {
217 werror = handle_question(dns, mem_ctx, &in->questions[i], &ans, &num_answers);
218 W_ERROR_NOT_OK_RETURN(werror);
221 *answers = ans;
222 *ancount = num_answers;
224 /*FIXME: Do something for these */
225 *nsrecs = NULL;
226 *nscount = 0;
228 *additional = NULL;
229 *arcount = 0;
231 return WERR_OK;