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/>.
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"
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
;
43 ans
[ai
].rdata
.cname_record
= talloc_strdup(ans
, rec
->data
.cname
);
46 ans
[ai
].rdata
.ipv4_record
= talloc_strdup(ans
, rec
->data
.ipv4
);
49 ans
[ai
].rdata
.ipv6_record
= rec
->data
.ipv6
;
52 ans
[ai
].rdata
.ns_record
= rec
->data
.ns
;
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
;
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
;
70 return DNS_ERR(NOT_IMPLEMENTED
);
73 ans
[ai
].name
= talloc_strdup(ans
, question
->name
);
74 ans
[ai
].rr_type
= rec
->wType
;
75 ans
[ai
].rr_class
= DNS_QCLASS_IN
;
76 ans
[ai
].ttl
= rec
->dwTtlSeconds
;
77 ans
[ai
].length
= UINT16_MAX
;
86 static WERROR
handle_question(struct dns_server
*dns
,
88 const struct dns_name_question
*question
,
89 struct dns_res_rec
**answers
, uint16_t *ancount
)
91 struct dns_res_rec
*ans
;
92 struct ldb_dn
*dn
= NULL
;
94 static const char * const attrs
[] = { "dnsRecord", NULL
};
96 uint16_t ai
= *ancount
;
98 struct ldb_message
*msg
= NULL
;
99 struct dnsp_DnssrvRpcRecord
*recs
;
100 struct ldb_message_element
*el
;
102 werror
= dns_name2dn(dns
, mem_ctx
, question
->name
, &dn
);
103 W_ERROR_NOT_OK_RETURN(werror
);
105 ret
= dsdb_search_one(dns
->samdb
, mem_ctx
, &msg
, dn
,
106 LDB_SCOPE_BASE
, attrs
, 0, "%s", "(objectClass=dnsNode)");
107 if (ret
!= LDB_SUCCESS
) {
108 return DNS_ERR(NAME_ERROR
);
111 el
= ldb_msg_find_element(msg
, attrs
[0]);
113 return DNS_ERR(NAME_ERROR
);
116 recs
= talloc_array(mem_ctx
, struct dnsp_DnssrvRpcRecord
, el
->num_values
);
117 for (ri
= 0; ri
< el
->num_values
; ri
++) {
118 struct ldb_val
*v
= &el
->values
[ri
];
119 enum ndr_err_code ndr_err
;
121 ndr_err
= ndr_pull_struct_blob(v
, recs
, &recs
[ri
],
122 (ndr_pull_flags_fn_t
)ndr_pull_dnsp_DnssrvRpcRecord
);
123 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
124 DEBUG(0, ("Failed to grab dnsp_DnssrvRpcRecord\n"));
125 return DNS_ERR(SERVER_FAILURE
);
129 ans
= talloc_realloc(mem_ctx
, *answers
, struct dns_res_rec
,
130 ai
+ el
->num_values
);
131 W_ERROR_HAVE_NO_MEMORY(ans
);
133 for (ri
= 0; ri
< el
->num_values
; ri
++) {
134 if ((question
->question_type
!= DNS_QTYPE_ALL
) &&
135 (recs
[ri
].wType
!= question
->question_type
)) {
138 create_response_rr(question
, &recs
[ri
], &ans
, &ai
);
141 if (*ancount
== ai
) {
142 return DNS_ERR(NAME_ERROR
);
152 WERROR
dns_server_process_query(struct dns_server
*dns
,
154 struct dns_name_packet
*in
,
155 struct dns_res_rec
**answers
, uint16_t *ancount
,
156 struct dns_res_rec
**nsrecs
, uint16_t *nscount
,
157 struct dns_res_rec
**additional
, uint16_t *arcount
)
159 uint16_t num_answers
=0;
160 struct dns_res_rec
*ans
=NULL
;
163 if (in
->qdcount
!= 1) {
164 return DNS_ERR(FORMAT_ERROR
);
167 /* Windows returns NOT_IMPLEMENTED on this as well */
168 if (in
->questions
[0].question_class
== DNS_QCLASS_NONE
) {
169 return DNS_ERR(NOT_IMPLEMENTED
);
172 ans
= talloc_array(mem_ctx
, struct dns_res_rec
, 0);
173 W_ERROR_HAVE_NO_MEMORY(ans
);
175 werror
= handle_question(dns
, mem_ctx
, &in
->questions
[0], &ans
, &num_answers
);
176 W_ERROR_NOT_OK_GOTO(werror
, query_failed
);
179 *ancount
= num_answers
;
181 /*FIXME: Do something for these */
191 /*FIXME: add our SOA record to nsrecs */