s4 dns: Split up the code into multiple files for easier development
[kai/samba.git] / source4 / dns_server / dns_utils.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    DNS server utils
5
6    Copyright (C) 2010 Kai Blin  <kai@samba.org>
7
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.
12
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.
17
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/>.
20 */
21
22 #include "includes.h"
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"
27 #include <ldb.h>
28 #include "dsdb/samdb/samdb.h"
29 #include "dsdb/common/util.h"
30 #include "dns_server/dns_server.h"
31
32 NTSTATUS dns_err_to_ntstatus(enum dns_rcode rcode)
33 {
34         switch (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;
43         }
44 }
45
46 uint8_t ntstatus_to_dns_err(NTSTATUS status)
47 {
48         if (NT_STATUS_EQUAL(NT_STATUS_OK, status)) {
49                 return DNS_RCODE_OK;
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;
62         }
63         DEBUG(0, ("No mapping exists for %s\n", nt_errstr(status)));
64         return DNS_RCODE_NOTIMP;
65 }
66
67 bool dns_name_match(const char *zone, const char *name, size_t *host_part_len)
68 {
69         size_t zl = strlen(zone);
70         size_t nl = strlen(name);
71         ssize_t zi, ni;
72         static const size_t fixup = 'a' - 'A';
73
74         if (zl > nl) {
75                 return false;
76         }
77
78         for (zi = zl, ni = nl; zi >= 0; zi--, ni--) {
79                 char zc = zone[zi];
80                 char nc = name[ni];
81
82                 /* convert to lower case */
83                 if (zc >= 'A' && zc <= 'Z') {
84                         zc += fixup;
85                 }
86                 if (nc >= 'A' && nc <= 'Z') {
87                         nc += fixup;
88                 }
89
90                 if (zc != nc) {
91                         return false;
92                 }
93         }
94
95         if (ni >= 0) {
96                 if (name[ni] != '.') {
97                         return false;
98                 }
99
100                 ni--;
101         }
102
103         *host_part_len = ni+1;
104
105         return true;
106 }
107
108 NTSTATUS dns_name2dn(struct dns_server *dns,
109                      TALLOC_CTX *mem_ctx,
110                      const char *name,
111                      struct ldb_dn **_dn)
112 {
113         struct ldb_dn *base;
114         struct ldb_dn *dn;
115         const struct dns_server_zone *z;
116         size_t host_part_len = 0;
117
118         if (name == NULL) {
119                 return NT_STATUS_INVALID_PARAMETER;
120         }
121
122         /*TODO: Check if 'name' is a valid DNS name */
123
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");
128                 *_dn = dn;
129                 return NT_STATUS_OK;
130         }
131
132         for (z = dns->zones; z != NULL; z = z->next) {
133                 bool match;
134
135                 match = dns_name_match(z->name, name, &host_part_len);
136                 if (match) {
137                         break;
138                 }
139         }
140
141         if (z == NULL) {
142                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
143         }
144
145         if (host_part_len == 0) {
146                 dn = ldb_dn_copy(mem_ctx, z->dn);
147                 ldb_dn_add_child_fmt(dn, "DC=@");
148                 *_dn = dn;
149                 return NT_STATUS_OK;
150         }
151
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);
154         *_dn = dn;
155         return NT_STATUS_OK;
156 }