c28fbc89b6172d41ee20b716f170413be603f552
[metze/samba/wip.git] / source4 / dns_server / dnsserver_common.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    DNS server utils
5
6    Copyright (C) 2010 Kai Blin
7    Copyright (C) 2014 Stefan Metzmacher
8
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.
13
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.
18
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/>.
21 */
22
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"
33
34 uint8_t werr_to_dns_err(WERROR werr)
35 {
36         if (W_ERROR_EQUAL(WERR_OK, werr)) {
37                 return DNS_RCODE_OK;
38         } else if (W_ERROR_EQUAL(DNS_ERR(FORMAT_ERROR), werr)) {
39                 return DNS_RCODE_FORMERR;
40         } else if (W_ERROR_EQUAL(DNS_ERR(SERVER_FAILURE), werr)) {
41                 return DNS_RCODE_SERVFAIL;
42         } else if (W_ERROR_EQUAL(DNS_ERR(NAME_ERROR), werr)) {
43                 return DNS_RCODE_NXDOMAIN;
44         } else if (W_ERROR_EQUAL(WERR_DNS_ERROR_NAME_DOES_NOT_EXIST, werr)) {
45                 return DNS_RCODE_NXDOMAIN;
46         } else if (W_ERROR_EQUAL(DNS_ERR(NOT_IMPLEMENTED), werr)) {
47                 return DNS_RCODE_NOTIMP;
48         } else if (W_ERROR_EQUAL(DNS_ERR(REFUSED), werr)) {
49                 return DNS_RCODE_REFUSED;
50         } else if (W_ERROR_EQUAL(DNS_ERR(YXDOMAIN), werr)) {
51                 return DNS_RCODE_YXDOMAIN;
52         } else if (W_ERROR_EQUAL(DNS_ERR(YXRRSET), werr)) {
53                 return DNS_RCODE_YXRRSET;
54         } else if (W_ERROR_EQUAL(DNS_ERR(NXRRSET), werr)) {
55                 return DNS_RCODE_NXRRSET;
56         } else if (W_ERROR_EQUAL(DNS_ERR(NOTAUTH), werr)) {
57                 return DNS_RCODE_NOTAUTH;
58         } else if (W_ERROR_EQUAL(DNS_ERR(NOTZONE), werr)) {
59                 return DNS_RCODE_NOTZONE;
60         } else if (W_ERROR_EQUAL(DNS_ERR(BADKEY), werr)) {
61                 return DNS_RCODE_BADKEY;
62         }
63         DEBUG(5, ("No mapping exists for %s\n", win_errstr(werr)));
64         return DNS_RCODE_SERVFAIL;
65 }
66
67 WERROR dns_common_extract(const struct ldb_message_element *el,
68                           TALLOC_CTX *mem_ctx,
69                           struct dnsp_DnssrvRpcRecord **records,
70                           uint16_t *num_records)
71 {
72         uint16_t ri;
73         struct dnsp_DnssrvRpcRecord *recs;
74
75         *records = NULL;
76         *num_records = 0;
77
78         recs = talloc_zero_array(mem_ctx, struct dnsp_DnssrvRpcRecord,
79                                  el->num_values);
80         if (recs == NULL) {
81                 return WERR_NOMEM;
82         }
83         for (ri = 0; ri < el->num_values; ri++) {
84                 struct ldb_val *v = &el->values[ri];
85                 enum ndr_err_code ndr_err;
86
87                 ndr_err = ndr_pull_struct_blob(v, recs, &recs[ri],
88                                 (ndr_pull_flags_fn_t)ndr_pull_dnsp_DnssrvRpcRecord);
89                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
90                         TALLOC_FREE(recs);
91                         DEBUG(0, ("Failed to grab dnsp_DnssrvRpcRecord\n"));
92                         return DNS_ERR(SERVER_FAILURE);
93                 }
94         }
95         *records = recs;
96         *num_records = el->num_values;
97         return WERR_OK;
98 }
99
100 WERROR dns_common_lookup(struct ldb_context *samdb,
101                          TALLOC_CTX *mem_ctx,
102                          struct ldb_dn *dn,
103                          struct dnsp_DnssrvRpcRecord **records,
104                          uint16_t *num_records)
105 {
106         static const char * const attrs[] = {
107                 "dnsRecord",
108                 NULL
109         };
110         int ret;
111         WERROR werr;
112         struct ldb_message *msg = NULL;
113         struct ldb_message_element *el;
114
115         *records = NULL;
116         *num_records = 0;
117
118         ret = dsdb_search_one(samdb, mem_ctx, &msg, dn,
119                               LDB_SCOPE_BASE, attrs, 0,
120                               "(objectClass=dnsNode)");
121         if (ret == LDB_ERR_NO_SUCH_OBJECT) {
122                 return WERR_DNS_ERROR_NAME_DOES_NOT_EXIST;
123         }
124         if (ret != LDB_SUCCESS) {
125                 /* TODO: we need to check if there's a glue record we need to
126                  * create a referral to */
127                 return DNS_ERR(NAME_ERROR);
128         }
129
130         el = ldb_msg_find_element(msg, "dnsRecord");
131         if (el == NULL) {
132                 TALLOC_FREE(msg);
133                 return DNS_ERR(NAME_ERROR);
134         }
135
136         werr = dns_common_extract(el, mem_ctx, records, num_records);
137         TALLOC_FREE(msg);
138         if (!W_ERROR_IS_OK(werr)) {
139                 return werr;
140         }
141
142         return WERR_OK;
143 }
144
145 WERROR dns_common_replace(struct ldb_context *samdb,
146                           TALLOC_CTX *mem_ctx,
147                           struct ldb_dn *dn,
148                           bool needs_add,
149                           uint32_t serial,
150                           struct dnsp_DnssrvRpcRecord *records,
151                           uint16_t rec_count)
152 {
153         struct ldb_message_element *el;
154         uint16_t i;
155         int ret;
156         struct ldb_message *msg = NULL;
157
158         msg = ldb_msg_new(mem_ctx);
159         W_ERROR_HAVE_NO_MEMORY(msg);
160
161         msg->dn = dn;
162
163         ret = ldb_msg_add_empty(msg, "dnsRecord", LDB_FLAG_MOD_REPLACE, &el);
164         if (ret != LDB_SUCCESS) {
165                 return DNS_ERR(SERVER_FAILURE);
166         }
167
168         el->values = talloc_zero_array(el, struct ldb_val, rec_count);
169         if (rec_count > 0) {
170                 W_ERROR_HAVE_NO_MEMORY(el->values);
171         }
172
173         for (i = 0; i < rec_count; i++) {
174                 static const struct dnsp_DnssrvRpcRecord zero;
175                 struct ldb_val *v = &el->values[el->num_values];
176                 enum ndr_err_code ndr_err;
177
178                 if (memcmp(&records[i], &zero, sizeof(zero)) == 0) {
179                         continue;
180                 }
181
182                 records[i].dwSerial = serial;
183                 ndr_err = ndr_push_struct_blob(v, el->values, &records[i],
184                                 (ndr_push_flags_fn_t)ndr_push_dnsp_DnssrvRpcRecord);
185                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
186                         DEBUG(0, ("Failed to push dnsp_DnssrvRpcRecord\n"));
187                         return DNS_ERR(SERVER_FAILURE);
188                 }
189                 el->num_values++;
190         }
191
192         if (needs_add) {
193                 if (el->num_values == 0) {
194                         return WERR_OK;
195                 }
196
197                 ret = ldb_msg_add_string(msg, "objectClass", "dnsNode");
198                 if (ret != LDB_SUCCESS) {
199                         return DNS_ERR(SERVER_FAILURE);
200                 }
201
202                 ret = ldb_add(samdb, msg);
203                 if (ret != LDB_SUCCESS) {
204                         return DNS_ERR(SERVER_FAILURE);
205                 }
206
207                 return WERR_OK;
208         }
209
210         if (el->num_values == 0) {
211                 el->flags = LDB_FLAG_MOD_DELETE;
212         }
213
214         ret = ldb_modify(samdb, msg);
215         if (ret != LDB_SUCCESS) {
216                 NTSTATUS nt = dsdb_ldb_err_to_ntstatus(ret);
217                 return ntstatus_to_werror(nt);
218         }
219
220         return WERR_OK;
221 }