s4:dns_server: make sure dns_common_lookup() doesn't return tombstones
[Samba.git] / source4 / dns_server / dnsserver_common.c
blob286673434711588b24aa479ac65689e7d7a3079a
1 /*
2 Unix SMB/CIFS implementation.
4 DNS server utils
6 Copyright (C) 2010 Kai Blin
7 Copyright (C) 2014 Stefan Metzmacher
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
24 #include "libcli/util/ntstatus.h"
25 #include "libcli/util/werror.h"
26 #include "librpc/ndr/libndr.h"
27 #include "librpc/gen_ndr/ndr_dns.h"
28 #include "librpc/gen_ndr/ndr_dnsp.h"
29 #include <ldb.h>
30 #include "dsdb/samdb/samdb.h"
31 #include "dsdb/common/util.h"
32 #include "dns_server/dnsserver_common.h"
34 #undef DBGC_CLASS
35 #define DBGC_CLASS DBGC_DNS
37 uint8_t werr_to_dns_err(WERROR werr)
39 if (W_ERROR_EQUAL(WERR_OK, werr)) {
40 return DNS_RCODE_OK;
41 } else if (W_ERROR_EQUAL(DNS_ERR(FORMAT_ERROR), werr)) {
42 return DNS_RCODE_FORMERR;
43 } else if (W_ERROR_EQUAL(DNS_ERR(SERVER_FAILURE), werr)) {
44 return DNS_RCODE_SERVFAIL;
45 } else if (W_ERROR_EQUAL(DNS_ERR(NAME_ERROR), werr)) {
46 return DNS_RCODE_NXDOMAIN;
47 } else if (W_ERROR_EQUAL(WERR_DNS_ERROR_NAME_DOES_NOT_EXIST, werr)) {
48 return DNS_RCODE_NXDOMAIN;
49 } else if (W_ERROR_EQUAL(DNS_ERR(NOT_IMPLEMENTED), werr)) {
50 return DNS_RCODE_NOTIMP;
51 } else if (W_ERROR_EQUAL(DNS_ERR(REFUSED), werr)) {
52 return DNS_RCODE_REFUSED;
53 } else if (W_ERROR_EQUAL(DNS_ERR(YXDOMAIN), werr)) {
54 return DNS_RCODE_YXDOMAIN;
55 } else if (W_ERROR_EQUAL(DNS_ERR(YXRRSET), werr)) {
56 return DNS_RCODE_YXRRSET;
57 } else if (W_ERROR_EQUAL(DNS_ERR(NXRRSET), werr)) {
58 return DNS_RCODE_NXRRSET;
59 } else if (W_ERROR_EQUAL(DNS_ERR(NOTAUTH), werr)) {
60 return DNS_RCODE_NOTAUTH;
61 } else if (W_ERROR_EQUAL(DNS_ERR(NOTZONE), werr)) {
62 return DNS_RCODE_NOTZONE;
63 } else if (W_ERROR_EQUAL(DNS_ERR(BADKEY), werr)) {
64 return DNS_RCODE_BADKEY;
66 DEBUG(5, ("No mapping exists for %s\n", win_errstr(werr)));
67 return DNS_RCODE_SERVFAIL;
70 WERROR dns_common_extract(const struct ldb_message_element *el,
71 TALLOC_CTX *mem_ctx,
72 struct dnsp_DnssrvRpcRecord **records,
73 uint16_t *num_records)
75 uint16_t ri;
76 struct dnsp_DnssrvRpcRecord *recs;
78 *records = NULL;
79 *num_records = 0;
81 recs = talloc_zero_array(mem_ctx, struct dnsp_DnssrvRpcRecord,
82 el->num_values);
83 if (recs == NULL) {
84 return WERR_NOMEM;
86 for (ri = 0; ri < el->num_values; ri++) {
87 struct ldb_val *v = &el->values[ri];
88 enum ndr_err_code ndr_err;
90 ndr_err = ndr_pull_struct_blob(v, recs, &recs[ri],
91 (ndr_pull_flags_fn_t)ndr_pull_dnsp_DnssrvRpcRecord);
92 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
93 TALLOC_FREE(recs);
94 DEBUG(0, ("Failed to grab dnsp_DnssrvRpcRecord\n"));
95 return DNS_ERR(SERVER_FAILURE);
98 *records = recs;
99 *num_records = el->num_values;
100 return WERR_OK;
103 WERROR dns_common_lookup(struct ldb_context *samdb,
104 TALLOC_CTX *mem_ctx,
105 struct ldb_dn *dn,
106 struct dnsp_DnssrvRpcRecord **records,
107 uint16_t *num_records,
108 bool *tombstoned)
110 static const char * const attrs[] = {
111 "dnsRecord",
112 "dNSTombstoned",
113 NULL
115 int ret;
116 WERROR werr;
117 struct ldb_message *msg = NULL;
118 struct ldb_message_element *el;
120 *records = NULL;
121 *num_records = 0;
123 if (tombstoned != NULL) {
124 *tombstoned = false;
125 ret = dsdb_search_one(samdb, mem_ctx, &msg, dn,
126 LDB_SCOPE_BASE, attrs, 0,
127 "(objectClass=dnsNode)");
128 } else {
129 ret = dsdb_search_one(samdb, mem_ctx, &msg, dn,
130 LDB_SCOPE_BASE, attrs, 0,
131 "(&(objectClass=dnsNode)(!(dNSTombstoned=TRUE)))");
133 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
134 return WERR_DNS_ERROR_NAME_DOES_NOT_EXIST;
136 if (ret != LDB_SUCCESS) {
137 /* TODO: we need to check if there's a glue record we need to
138 * create a referral to */
139 return DNS_ERR(NAME_ERROR);
142 if (tombstoned != NULL) {
143 *tombstoned = ldb_msg_find_attr_as_bool(msg,
144 "dNSTombstoned", false);
147 el = ldb_msg_find_element(msg, "dnsRecord");
148 if (el == NULL) {
149 TALLOC_FREE(msg);
150 if (tombstoned != NULL) {
151 struct dnsp_DnssrvRpcRecord *recs;
153 * records produced by older Samba releases
154 * keep dnsNode objects without dnsRecord and
155 * without setting dNSTombstoned=TRUE.
157 * We just pretend they're tombstones.
159 recs = talloc_array(mem_ctx,
160 struct dnsp_DnssrvRpcRecord,
162 if (recs == NULL) {
163 return WERR_NOMEM;
165 recs[0] = (struct dnsp_DnssrvRpcRecord) {
166 .wType = DNS_TYPE_TOMBSTONE,
168 * A value of timestamp != 0
169 * indicated that the object was already
170 * a tombstone, this will be used
171 * in dns_common_replace()
173 .data.timestamp = 1,
176 *tombstoned = true;
177 *records = recs;
178 *num_records = 1;
179 return WERR_OK;
181 return DNS_ERR(NAME_ERROR);
184 werr = dns_common_extract(el, mem_ctx, records, num_records);
185 TALLOC_FREE(msg);
186 if (!W_ERROR_IS_OK(werr)) {
187 return werr;
190 return WERR_OK;
193 WERROR dns_common_replace(struct ldb_context *samdb,
194 TALLOC_CTX *mem_ctx,
195 struct ldb_dn *dn,
196 bool needs_add,
197 uint32_t serial,
198 struct dnsp_DnssrvRpcRecord *records,
199 uint16_t rec_count)
201 struct ldb_message_element *el;
202 uint16_t i;
203 int ret;
204 struct ldb_message *msg = NULL;
206 msg = ldb_msg_new(mem_ctx);
207 W_ERROR_HAVE_NO_MEMORY(msg);
209 msg->dn = dn;
211 ret = ldb_msg_add_empty(msg, "dnsRecord", LDB_FLAG_MOD_REPLACE, &el);
212 if (ret != LDB_SUCCESS) {
213 return DNS_ERR(SERVER_FAILURE);
216 el->values = talloc_zero_array(el, struct ldb_val, rec_count);
217 if (rec_count > 0) {
218 W_ERROR_HAVE_NO_MEMORY(el->values);
221 for (i = 0; i < rec_count; i++) {
222 static const struct dnsp_DnssrvRpcRecord zero;
223 struct ldb_val *v = &el->values[el->num_values];
224 enum ndr_err_code ndr_err;
226 if (memcmp(&records[i], &zero, sizeof(zero)) == 0) {
227 continue;
230 records[i].dwSerial = serial;
231 ndr_err = ndr_push_struct_blob(v, el->values, &records[i],
232 (ndr_push_flags_fn_t)ndr_push_dnsp_DnssrvRpcRecord);
233 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
234 DEBUG(0, ("Failed to push dnsp_DnssrvRpcRecord\n"));
235 return DNS_ERR(SERVER_FAILURE);
237 el->num_values++;
240 if (needs_add) {
241 if (el->num_values == 0) {
242 return WERR_OK;
245 ret = ldb_msg_add_string(msg, "objectClass", "dnsNode");
246 if (ret != LDB_SUCCESS) {
247 return DNS_ERR(SERVER_FAILURE);
250 ret = ldb_add(samdb, msg);
251 if (ret != LDB_SUCCESS) {
252 return DNS_ERR(SERVER_FAILURE);
255 return WERR_OK;
258 if (el->num_values == 0) {
259 el->flags = LDB_FLAG_MOD_DELETE;
262 ret = ldb_modify(samdb, msg);
263 if (ret != LDB_SUCCESS) {
264 NTSTATUS nt = dsdb_ldb_err_to_ntstatus(ret);
265 return ntstatus_to_werror(nt);
268 return WERR_OK;