2 Unix SMB/CIFS implementation.
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/ntstatus.h"
24 #include "libcli/util/werror.h"
25 #include "librpc/ndr/libndr.h"
26 #include "librpc/gen_ndr/ndr_dns.h"
27 #include "librpc/gen_ndr/ndr_dnsp.h"
29 #include "dsdb/samdb/samdb.h"
30 #include "dsdb/common/util.h"
31 #include "dns_server/dns_server.h"
34 #define DBGC_CLASS DBGC_DNS
36 bool dns_name_match(const char *zone
, const char *name
, size_t *host_part_len
)
38 size_t zl
= strlen(zone
);
39 size_t nl
= strlen(name
);
41 static const size_t fixup
= 'a' - 'A';
47 for (zi
= zl
, ni
= nl
; zi
>= 0; zi
--, ni
--) {
51 /* convert to lower case */
52 if (zc
>= 'A' && zc
<= 'Z') {
55 if (nc
>= 'A' && nc
<= 'Z') {
65 if (name
[ni
] != '.') {
72 *host_part_len
= ni
+1;
77 /* Names are equal if they match and there's nothing left over */
78 bool dns_name_equal(const char *name1
, const char *name2
)
81 bool ret
= dns_name_match(name1
, name2
, &host_part_len
);
83 return ret
&& (host_part_len
== 0);
87 see if two dns records match
89 bool dns_records_match(struct dnsp_DnssrvRpcRecord
*rec1
,
90 struct dnsp_DnssrvRpcRecord
*rec2
)
95 if (rec1
->wType
!= rec2
->wType
) {
99 /* see if the data matches */
100 switch (rec1
->wType
) {
102 return strcmp(rec1
->data
.ipv4
, rec2
->data
.ipv4
) == 0;
104 return strcmp(rec1
->data
.ipv6
, rec2
->data
.ipv6
) == 0;
106 return dns_name_equal(rec1
->data
.cname
, rec2
->data
.cname
);
108 if (rec1
->data
.txt
.count
!= rec2
->data
.txt
.count
) {
112 for (i
=0; i
<rec1
->data
.txt
.count
; i
++) {
113 status
= status
&& (strcmp(rec1
->data
.txt
.str
[i
],
114 rec2
->data
.txt
.str
[i
]) == 0);
118 return strcmp(rec1
->data
.ptr
, rec2
->data
.ptr
) == 0;
120 return dns_name_equal(rec1
->data
.ns
, rec2
->data
.ns
);
123 return rec1
->data
.srv
.wPriority
== rec2
->data
.srv
.wPriority
&&
124 rec1
->data
.srv
.wWeight
== rec2
->data
.srv
.wWeight
&&
125 rec1
->data
.srv
.wPort
== rec2
->data
.srv
.wPort
&&
126 dns_name_equal(rec1
->data
.srv
.nameTarget
, rec2
->data
.srv
.nameTarget
);
129 return rec1
->data
.mx
.wPriority
== rec2
->data
.mx
.wPriority
&&
130 dns_name_equal(rec1
->data
.mx
.nameTarget
, rec2
->data
.mx
.nameTarget
);
133 return strcmp(rec1
->data
.hinfo
.cpu
, rec2
->data
.hinfo
.cpu
) == 0 &&
134 strcmp(rec1
->data
.hinfo
.os
, rec2
->data
.hinfo
.os
) == 0;
137 return dns_name_equal(rec1
->data
.soa
.mname
, rec2
->data
.soa
.mname
) &&
138 dns_name_equal(rec1
->data
.soa
.rname
, rec2
->data
.soa
.rname
) &&
139 rec1
->data
.soa
.serial
== rec2
->data
.soa
.serial
&&
140 rec1
->data
.soa
.refresh
== rec2
->data
.soa
.refresh
&&
141 rec1
->data
.soa
.retry
== rec2
->data
.soa
.retry
&&
142 rec1
->data
.soa
.expire
== rec2
->data
.soa
.expire
&&
143 rec1
->data
.soa
.minimum
== rec2
->data
.soa
.minimum
;
151 WERROR
dns_lookup_records(struct dns_server
*dns
,
154 struct dnsp_DnssrvRpcRecord
**records
,
157 return dns_common_lookup(dns
->samdb
, mem_ctx
, dn
, records
, rec_count
);
160 WERROR
dns_replace_records(struct dns_server
*dns
,
164 struct dnsp_DnssrvRpcRecord
*records
,
167 struct ldb_message_element
*el
;
170 struct ldb_message
*msg
= NULL
;
172 msg
= ldb_msg_new(mem_ctx
);
173 W_ERROR_HAVE_NO_MEMORY(msg
);
177 ret
= ldb_msg_add_empty(msg
, "dnsRecord", LDB_FLAG_MOD_REPLACE
, &el
);
178 if (ret
!= LDB_SUCCESS
) {
179 return DNS_ERR(SERVER_FAILURE
);
182 el
->values
= talloc_zero_array(el
, struct ldb_val
, rec_count
);
184 W_ERROR_HAVE_NO_MEMORY(el
->values
);
187 for (i
= 0; i
< rec_count
; i
++) {
188 static const struct dnsp_DnssrvRpcRecord zero
;
189 struct ldb_val
*v
= &el
->values
[el
->num_values
];
190 enum ndr_err_code ndr_err
;
192 if (memcmp(&records
[i
], &zero
, sizeof(zero
)) == 0) {
195 ndr_err
= ndr_push_struct_blob(v
, el
->values
, &records
[i
],
196 (ndr_push_flags_fn_t
)ndr_push_dnsp_DnssrvRpcRecord
);
197 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
198 DEBUG(0, ("Failed to grab dnsp_DnssrvRpcRecord\n"));
199 return DNS_ERR(SERVER_FAILURE
);
205 if (el
->num_values
== 0) {
209 /* TODO: Delete object? */
210 el
->flags
= LDB_FLAG_MOD_DELETE
;
214 ret
= ldb_msg_add_string(msg
, "objectClass", "dnsNode");
215 if (ret
!= LDB_SUCCESS
) {
216 return DNS_ERR(SERVER_FAILURE
);
219 ret
= ldb_add(dns
->samdb
, msg
);
220 if (ret
!= LDB_SUCCESS
) {
221 return DNS_ERR(SERVER_FAILURE
);
227 ret
= ldb_modify(dns
->samdb
, msg
);
228 if (ret
!= LDB_SUCCESS
) {
229 return DNS_ERR(SERVER_FAILURE
);
235 bool dns_authorative_for_zone(struct dns_server
*dns
,
238 const struct dns_server_zone
*z
;
239 size_t host_part_len
= 0;
245 if (strcmp(name
, "") == 0) {
248 for (z
= dns
->zones
; z
!= NULL
; z
= z
->next
) {
251 match
= dns_name_match(z
->name
, name
, &host_part_len
);
263 WERROR
dns_name2dn(struct dns_server
*dns
,
270 const struct dns_server_zone
*z
;
271 size_t host_part_len
= 0;
274 return DNS_ERR(FORMAT_ERROR
);
277 /*TODO: Check if 'name' is a valid DNS name */
279 if (strcmp(name
, "") == 0) {
280 base
= ldb_get_default_basedn(dns
->samdb
);
281 dn
= ldb_dn_copy(mem_ctx
, base
);
282 ldb_dn_add_child_fmt(dn
, "DC=@,DC=RootDNSServers,CN=MicrosoftDNS,CN=System");
287 for (z
= dns
->zones
; z
!= NULL
; z
= z
->next
) {
290 match
= dns_name_match(z
->name
, name
, &host_part_len
);
297 return DNS_ERR(NAME_ERROR
);
300 if (host_part_len
== 0) {
301 dn
= ldb_dn_copy(mem_ctx
, z
->dn
);
302 ldb_dn_add_child_fmt(dn
, "DC=@");
307 dn
= ldb_dn_copy(mem_ctx
, z
->dn
);
308 ldb_dn_add_child_fmt(dn
, "DC=%*.*s", (int)host_part_len
, (int)host_part_len
, name
);
313 WERROR
dns_generate_options(struct dns_server
*dns
,
315 struct dns_res_rec
**options
)
317 struct dns_res_rec
*o
;
319 o
= talloc_zero(mem_ctx
, struct dns_res_rec
);
324 o
->rr_type
= DNS_QTYPE_OPT
;
325 /* This is ugly, but RFC2671 wants the payload size in this field */
326 o
->rr_class
= (enum dns_qclass
) dns
->max_payload
;