s3-winbindd: use cli_rpc_pipe_open_with_creds()
[Samba.git] / source4 / dns_server / dns_utils.c
blobc757c1576266929335892c54666244ba008f12e0
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 #undef DBGC_CLASS
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);
40 ssize_t zi, ni;
41 static const size_t fixup = 'a' - 'A';
43 if (zl > nl) {
44 return false;
47 for (zi = zl, ni = nl; zi >= 0; zi--, ni--) {
48 char zc = zone[zi];
49 char nc = name[ni];
51 /* convert to lower case */
52 if (zc >= 'A' && zc <= 'Z') {
53 zc += fixup;
55 if (nc >= 'A' && nc <= 'Z') {
56 nc += fixup;
59 if (zc != nc) {
60 return false;
64 if (ni >= 0) {
65 if (name[ni] != '.') {
66 return false;
69 ni--;
72 *host_part_len = ni+1;
74 return true;
77 /* Names are equal if they match and there's nothing left over */
78 bool dns_name_equal(const char *name1, const char *name2)
80 size_t host_part_len;
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)
92 bool status;
93 int i;
95 if (rec1->wType != rec2->wType) {
96 return false;
99 /* see if the data matches */
100 switch (rec1->wType) {
101 case DNS_TYPE_A:
102 return strcmp(rec1->data.ipv4, rec2->data.ipv4) == 0;
103 case DNS_TYPE_AAAA:
104 return strcmp(rec1->data.ipv6, rec2->data.ipv6) == 0;
105 case DNS_TYPE_CNAME:
106 return dns_name_equal(rec1->data.cname, rec2->data.cname);
107 case DNS_TYPE_TXT:
108 if (rec1->data.txt.count != rec2->data.txt.count) {
109 return false;
111 status = true;
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);
116 return status;
117 case DNS_TYPE_PTR:
118 return strcmp(rec1->data.ptr, rec2->data.ptr) == 0;
119 case DNS_TYPE_NS:
120 return dns_name_equal(rec1->data.ns, rec2->data.ns);
122 case DNS_TYPE_SRV:
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);
128 case DNS_TYPE_MX:
129 return rec1->data.mx.wPriority == rec2->data.mx.wPriority &&
130 dns_name_equal(rec1->data.mx.nameTarget, rec2->data.mx.nameTarget);
132 case DNS_TYPE_HINFO:
133 return strcmp(rec1->data.hinfo.cpu, rec2->data.hinfo.cpu) == 0 &&
134 strcmp(rec1->data.hinfo.os, rec2->data.hinfo.os) == 0;
136 case DNS_TYPE_SOA:
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;
144 default:
145 break;
148 return false;
151 WERROR dns_lookup_records(struct dns_server *dns,
152 TALLOC_CTX *mem_ctx,
153 struct ldb_dn *dn,
154 struct dnsp_DnssrvRpcRecord **records,
155 uint16_t *rec_count)
157 return dns_common_lookup(dns->samdb, mem_ctx, dn,
158 records, rec_count, NULL);
161 WERROR dns_replace_records(struct dns_server *dns,
162 TALLOC_CTX *mem_ctx,
163 struct ldb_dn *dn,
164 bool needs_add,
165 struct dnsp_DnssrvRpcRecord *records,
166 uint16_t rec_count)
168 /* TODO: Autogenerate this somehow */
169 uint32_t dwSerial = 110;
170 return dns_common_replace(dns->samdb, mem_ctx, dn,
171 needs_add, dwSerial, records, rec_count);
174 bool dns_authorative_for_zone(struct dns_server *dns,
175 const char *name)
177 const struct dns_server_zone *z;
178 size_t host_part_len = 0;
180 if (name == NULL) {
181 return false;
184 if (strcmp(name, "") == 0) {
185 return true;
187 for (z = dns->zones; z != NULL; z = z->next) {
188 bool match;
190 match = dns_name_match(z->name, name, &host_part_len);
191 if (match) {
192 break;
195 if (z == NULL) {
196 return false;
199 return true;
202 WERROR dns_name2dn(struct dns_server *dns,
203 TALLOC_CTX *mem_ctx,
204 const char *name,
205 struct ldb_dn **_dn)
207 struct ldb_dn *base;
208 struct ldb_dn *dn;
209 const struct dns_server_zone *z;
210 size_t host_part_len = 0;
212 if (name == NULL) {
213 return DNS_ERR(FORMAT_ERROR);
216 /*TODO: Check if 'name' is a valid DNS name */
218 if (strcmp(name, "") == 0) {
219 base = ldb_get_default_basedn(dns->samdb);
220 dn = ldb_dn_copy(mem_ctx, base);
221 ldb_dn_add_child_fmt(dn, "DC=@,DC=RootDNSServers,CN=MicrosoftDNS,CN=System");
222 *_dn = dn;
223 return WERR_OK;
226 for (z = dns->zones; z != NULL; z = z->next) {
227 bool match;
229 match = dns_name_match(z->name, name, &host_part_len);
230 if (match) {
231 break;
235 if (z == NULL) {
236 return DNS_ERR(NAME_ERROR);
239 if (host_part_len == 0) {
240 dn = ldb_dn_copy(mem_ctx, z->dn);
241 ldb_dn_add_child_fmt(dn, "DC=@");
242 *_dn = dn;
243 return WERR_OK;
246 dn = ldb_dn_copy(mem_ctx, z->dn);
247 ldb_dn_add_child_fmt(dn, "DC=%*.*s", (int)host_part_len, (int)host_part_len, name);
248 *_dn = dn;
249 return WERR_OK;
252 WERROR dns_generate_options(struct dns_server *dns,
253 TALLOC_CTX *mem_ctx,
254 struct dns_res_rec **options)
256 struct dns_res_rec *o;
258 o = talloc_zero(mem_ctx, struct dns_res_rec);
259 if (o == NULL) {
260 return WERR_NOMEM;
262 o->name = '\0';
263 o->rr_type = DNS_QTYPE_OPT;
264 /* This is ugly, but RFC2671 wants the payload size in this field */
265 o->rr_class = (enum dns_qclass) dns->max_payload;
266 o->ttl = 0;
267 o->length = 0;
269 *options = o;
270 return WERR_OK;