s4 dns: Implement dns name equality check
[Samba/gebeck_regimport.git] / source4 / dns_server / dns_utils.c
blobf179546849973b177abf73f7f404e8eaa56971f5
1 /*
2 Unix SMB/CIFS implementation.
4 DNS server utils
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/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"
28 #include <ldb.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)) {
36 return DNS_RCODE_OK;
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);
66 ssize_t zi, ni;
67 static const size_t fixup = 'a' - 'A';
69 if (zl > nl) {
70 return false;
73 for (zi = zl, ni = nl; zi >= 0; zi--, ni--) {
74 char zc = zone[zi];
75 char nc = name[ni];
77 /* convert to lower case */
78 if (zc >= 'A' && zc <= 'Z') {
79 zc += fixup;
81 if (nc >= 'A' && nc <= 'Z') {
82 nc += fixup;
85 if (zc != nc) {
86 return false;
90 if (ni >= 0) {
91 if (name[ni] != '.') {
92 return false;
95 ni--;
98 *host_part_len = ni+1;
100 return true;
103 /* Names are equal if they match and there's nothing left over */
104 bool dns_name_equal(const char *name1, const char *name2)
106 size_t host_part_len;
107 bool ret = dns_name_match(name1, name2, &host_part_len);
109 return ret && (host_part_len == 0);
112 WERROR dns_name2dn(struct dns_server *dns,
113 TALLOC_CTX *mem_ctx,
114 const char *name,
115 struct ldb_dn **_dn)
117 struct ldb_dn *base;
118 struct ldb_dn *dn;
119 const struct dns_server_zone *z;
120 size_t host_part_len = 0;
122 if (name == NULL) {
123 return DNS_ERR(FORMAT_ERROR);
126 /*TODO: Check if 'name' is a valid DNS name */
128 if (strcmp(name, "") == 0) {
129 base = ldb_get_default_basedn(dns->samdb);
130 dn = ldb_dn_copy(mem_ctx, base);
131 ldb_dn_add_child_fmt(dn, "DC=@,DC=RootDNSServers,CN=MicrosoftDNS,CN=System");
132 *_dn = dn;
133 return WERR_OK;
136 for (z = dns->zones; z != NULL; z = z->next) {
137 bool match;
139 match = dns_name_match(z->name, name, &host_part_len);
140 if (match) {
141 break;
145 if (z == NULL) {
146 return DNS_ERR(NAME_ERROR);
149 if (host_part_len == 0) {
150 dn = ldb_dn_copy(mem_ctx, z->dn);
151 ldb_dn_add_child_fmt(dn, "DC=@");
152 *_dn = dn;
153 return WERR_OK;
156 dn = ldb_dn_copy(mem_ctx, z->dn);
157 ldb_dn_add_child_fmt(dn, "DC=%*.*s", (int)host_part_len, (int)host_part_len, name);
158 *_dn = dn;
159 return WERR_OK;