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 "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 NTSTATUS dns_err_to_ntstatus(enum dns_rcode rcode)
35 case DNS_RCODE_OK: return NT_STATUS_OK;
36 case DNS_RCODE_FORMERR: return NT_STATUS_INVALID_PARAMETER;
37 case DNS_RCODE_SERVFAIL: return NT_STATUS_INTERNAL_ERROR;
38 case DNS_RCODE_NXDOMAIN: return NT_STATUS_OBJECT_NAME_NOT_FOUND;
39 case DNS_RCODE_NOTIMP: return NT_STATUS_NOT_IMPLEMENTED;
40 case DNS_RCODE_REFUSED: return NT_STATUS_ACCESS_DENIED;
41 case DNS_RCODE_NOTAUTH: return NT_STATUS_FOOBAR;
42 default: return NT_STATUS_NONE_MAPPED;
46 uint8_t ntstatus_to_dns_err(NTSTATUS status)
48 if (NT_STATUS_EQUAL(NT_STATUS_OK, status)) {
50 } else if (NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER, status)) {
51 return DNS_RCODE_FORMERR;
52 } else if (NT_STATUS_EQUAL(NT_STATUS_INTERNAL_ERROR, status)) {
53 return DNS_RCODE_SERVFAIL;
54 } else if (NT_STATUS_EQUAL(NT_STATUS_OBJECT_NAME_NOT_FOUND, status)) {
55 return DNS_RCODE_NXDOMAIN;
56 } else if (NT_STATUS_EQUAL(NT_STATUS_NOT_IMPLEMENTED, status)) {
57 return DNS_RCODE_NOTIMP;
58 } else if (NT_STATUS_EQUAL(NT_STATUS_ACCESS_DENIED, status)) {
59 return DNS_RCODE_REFUSED;
60 } else if (NT_STATUS_EQUAL(NT_STATUS_FOOBAR, status)) {
61 return DNS_RCODE_NOTAUTH;
63 DEBUG(0, ("No mapping exists for %s\n", nt_errstr(status)));
64 return DNS_RCODE_NOTIMP;
67 bool dns_name_match(const char *zone, const char *name, size_t *host_part_len)
69 size_t zl = strlen(zone);
70 size_t nl = strlen(name);
72 static const size_t fixup = 'a' - 'A';
78 for (zi = zl, ni = nl; zi >= 0; zi--, ni--) {
82 /* convert to lower case */
83 if (zc >= 'A' && zc <= 'Z') {
86 if (nc >= 'A' && nc <= 'Z') {
96 if (name[ni] != '.') {
103 *host_part_len = ni+1;
108 NTSTATUS dns_name2dn(struct dns_server *dns,
115 const struct dns_server_zone *z;
116 size_t host_part_len = 0;
119 return NT_STATUS_INVALID_PARAMETER;
122 /*TODO: Check if 'name' is a valid DNS name */
124 if (strcmp(name, "") == 0) {
125 base = ldb_get_default_basedn(dns->samdb);
126 dn = ldb_dn_copy(mem_ctx, base);
127 ldb_dn_add_child_fmt(dn, "DC=@,DC=RootDNSServers,CN=MicrosoftDNS,CN=System");
132 for (z = dns->zones; z != NULL; z = z->next) {
135 match = dns_name_match(z->name, name, &host_part_len);
142 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
145 if (host_part_len == 0) {
146 dn = ldb_dn_copy(mem_ctx, z->dn);
147 ldb_dn_add_child_fmt(dn, "DC=@");
152 dn = ldb_dn_copy(mem_ctx, z->dn);
153 ldb_dn_add_child_fmt(dn, "DC=%*.*s", (int)host_part_len, (int)host_part_len, name);