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"
33 uint8_t werr_to_dns_err(WERROR werr
)
35 if (W_ERROR_EQUAL(WERR_OK
, werr
)) {
37 } else if (W_ERROR_EQUAL(DNS_ERR(FORMAT_ERROR
), werr
)) {
38 return DNS_RCODE_FORMERR
;
39 } else if (W_ERROR_EQUAL(DNS_ERR(SERVER_FAILURE
), werr
)) {
40 return DNS_RCODE_SERVFAIL
;
41 } else if (W_ERROR_EQUAL(DNS_ERR(NAME_ERROR
), werr
)) {
42 return DNS_RCODE_NXDOMAIN
;
43 } else if (W_ERROR_EQUAL(DNS_ERR(NOT_IMPLEMENTED
), werr
)) {
44 return DNS_RCODE_NOTIMP
;
45 } else if (W_ERROR_EQUAL(DNS_ERR(REFUSED
), werr
)) {
46 return DNS_RCODE_REFUSED
;
47 } else if (W_ERROR_EQUAL(DNS_ERR(YXDOMAIN
), werr
)) {
48 return DNS_RCODE_YXDOMAIN
;
49 } else if (W_ERROR_EQUAL(DNS_ERR(YXRRSET
), werr
)) {
50 return DNS_RCODE_YXRRSET
;
51 } else if (W_ERROR_EQUAL(DNS_ERR(NXRRSET
), werr
)) {
52 return DNS_RCODE_NXRRSET
;
53 } else if (W_ERROR_EQUAL(DNS_ERR(NOTAUTH
), werr
)) {
54 return DNS_RCODE_NOTAUTH
;
55 } else if (W_ERROR_EQUAL(DNS_ERR(NOTZONE
), werr
)) {
56 return DNS_RCODE_NOTZONE
;
58 DEBUG(5, ("No mapping exists for %%s\n"));
59 return DNS_RCODE_SERVFAIL
;
62 bool dns_name_match(const char *zone
, const char *name
, size_t *host_part_len
)
64 size_t zl
= strlen(zone
);
65 size_t nl
= strlen(name
);
67 static const size_t fixup
= 'a' - 'A';
73 for (zi
= zl
, ni
= nl
; zi
>= 0; zi
--, ni
--) {
77 /* convert to lower case */
78 if (zc
>= 'A' && zc
<= 'Z') {
81 if (nc
>= 'A' && nc
<= 'Z') {
91 if (name
[ni
] != '.') {
98 *host_part_len
= ni
+1;
103 WERROR
dns_name2dn(struct dns_server
*dns
,
110 const struct dns_server_zone
*z
;
111 size_t host_part_len
= 0;
114 return DNS_ERR(FORMAT_ERROR
);
117 /*TODO: Check if 'name' is a valid DNS name */
119 if (strcmp(name
, "") == 0) {
120 base
= ldb_get_default_basedn(dns
->samdb
);
121 dn
= ldb_dn_copy(mem_ctx
, base
);
122 ldb_dn_add_child_fmt(dn
, "DC=@,DC=RootDNSServers,CN=MicrosoftDNS,CN=System");
127 for (z
= dns
->zones
; z
!= NULL
; z
= z
->next
) {
130 match
= dns_name_match(z
->name
, name
, &host_part_len
);
137 return DNS_ERR(NAME_ERROR
);
140 if (host_part_len
== 0) {
141 dn
= ldb_dn_copy(mem_ctx
, z
->dn
);
142 ldb_dn_add_child_fmt(dn
, "DC=@");
147 dn
= ldb_dn_copy(mem_ctx
, z
->dn
);
148 ldb_dn_add_child_fmt(dn
, "DC=%*.*s", (int)host_part_len
, (int)host_part_len
, name
);