s3-selftest: Remove some unnecessary comma
[Samba/gebeck_regimport.git] / source4 / dns_server / dns_query.c
blob4277659cf7e70348d3e92079427a8a9d4253bb5e
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 create_response_rr(const struct dns_name_question *question,
33 const struct dnsp_DnssrvRpcRecord *rec,
34 struct dns_res_rec **answers, uint16_t *ancount)
36 struct dns_res_rec *ans = *answers;
37 uint16_t ai = *ancount;
38 char *tmp;
39 uint32_t i;
41 ZERO_STRUCT(ans[ai]);
43 switch (rec->wType) {
44 case DNS_QTYPE_CNAME:
45 ans[ai].rdata.cname_record = talloc_strdup(ans, rec->data.cname);
46 break;
47 case DNS_QTYPE_A:
48 ans[ai].rdata.ipv4_record = talloc_strdup(ans, rec->data.ipv4);
49 break;
50 case DNS_QTYPE_AAAA:
51 ans[ai].rdata.ipv6_record = rec->data.ipv6;
52 break;
53 case DNS_TYPE_NS:
54 ans[ai].rdata.ns_record = rec->data.ns;
55 break;
56 case DNS_QTYPE_SRV:
57 ans[ai].rdata.srv_record.priority = rec->data.srv.wPriority;
58 ans[ai].rdata.srv_record.weight = rec->data.srv.wWeight;
59 ans[ai].rdata.srv_record.port = rec->data.srv.wPort;
60 ans[ai].rdata.srv_record.target = rec->data.srv.nameTarget;
61 break;
62 case DNS_QTYPE_SOA:
63 ans[ai].rdata.soa_record.mname = rec->data.soa.mname;
64 ans[ai].rdata.soa_record.rname = rec->data.soa.rname;
65 ans[ai].rdata.soa_record.serial = rec->data.soa.serial;
66 ans[ai].rdata.soa_record.refresh = rec->data.soa.refresh;
67 ans[ai].rdata.soa_record.retry = rec->data.soa.retry;
68 ans[ai].rdata.soa_record.expire = rec->data.soa.expire;
69 ans[ai].rdata.soa_record.minimum = rec->data.soa.minimum;
70 break;
71 case DNS_QTYPE_PTR:
72 ans[ai].rdata.ptr_record = talloc_strdup(ans, rec->data.ptr);
73 break;
74 case DNS_QTYPE_TXT:
75 tmp = talloc_asprintf(ans, "\"%s\"", rec->data.txt.str[0]);
76 for (i=1; i<rec->data.txt.count; i++) {
77 tmp = talloc_asprintf_append(tmp, " \"%s\"",
78 rec->data.txt.str[i]);
80 ans[ai].rdata.txt_record.txt = tmp;
81 break;
82 default:
83 DEBUG(0, ("Got unhandled type %u query.\n", rec->wType));
84 return DNS_ERR(NOT_IMPLEMENTED);
87 ans[ai].name = talloc_strdup(ans, question->name);
88 ans[ai].rr_type = rec->wType;
89 ans[ai].rr_class = DNS_QCLASS_IN;
90 ans[ai].ttl = rec->dwTtlSeconds;
91 ans[ai].length = UINT16_MAX;
92 ai++;
94 *answers = ans;
95 *ancount = ai;
97 return WERR_OK;
100 static WERROR handle_question(struct dns_server *dns,
101 TALLOC_CTX *mem_ctx,
102 const struct dns_name_question *question,
103 struct dns_res_rec **answers, uint16_t *ancount)
105 struct dns_res_rec *ans;
106 WERROR werror;
107 unsigned int ri;
108 struct dnsp_DnssrvRpcRecord *recs;
109 uint16_t rec_count, ai = 0;
110 struct ldb_dn *dn = NULL;
112 werror = dns_name2dn(dns, mem_ctx, question->name, &dn);
113 W_ERROR_NOT_OK_RETURN(werror);
115 werror = dns_lookup_records(dns, mem_ctx, dn, &recs, &rec_count);
116 W_ERROR_NOT_OK_RETURN(werror);
118 ans = talloc_zero_array(mem_ctx, struct dns_res_rec, rec_count);
119 W_ERROR_HAVE_NO_MEMORY(ans);
121 for (ri = 0; ri < rec_count; ri++) {
122 if ((question->question_type != DNS_QTYPE_ALL) &&
123 (recs[ri].wType != question->question_type)) {
124 continue;
126 werror = create_response_rr(question, &recs[ri], &ans, &ai);
127 W_ERROR_NOT_OK_RETURN(werror);
130 if (ai == 0) {
131 return DNS_ERR(NAME_ERROR);
134 *ancount = ai;
135 *answers = ans;
137 return WERR_OK;
141 WERROR dns_server_process_query(struct dns_server *dns,
142 TALLOC_CTX *mem_ctx,
143 struct dns_name_packet *in,
144 struct dns_res_rec **answers, uint16_t *ancount,
145 struct dns_res_rec **nsrecs, uint16_t *nscount,
146 struct dns_res_rec **additional, uint16_t *arcount)
148 uint16_t num_answers=0;
149 struct dns_res_rec *ans=NULL;
150 WERROR werror;
152 if (in->qdcount != 1) {
153 return DNS_ERR(FORMAT_ERROR);
156 /* Windows returns NOT_IMPLEMENTED on this as well */
157 if (in->questions[0].question_class == DNS_QCLASS_NONE) {
158 return DNS_ERR(NOT_IMPLEMENTED);
161 werror = handle_question(dns, mem_ctx, &in->questions[0], &ans, &num_answers);
162 W_ERROR_NOT_OK_GOTO(werror, query_failed);
164 *answers = ans;
165 *ancount = num_answers;
167 /*FIXME: Do something for these */
168 *nsrecs = NULL;
169 *nscount = 0;
171 *additional = NULL;
172 *arcount = 0;
174 return WERR_OK;
176 query_failed:
177 /*FIXME: add our SOA record to nsrecs */
178 return werror;