winbind: Keep "force_reauth" in invalidate_cm_connection
[Samba.git] / librpc / ndr / ndr_dns.c
blobd37c8cc2ecee5f7db675587242e9c66e20e3e79a
1 /*
2 Unix SMB/CIFS implementation.
4 manipulate dns name structures
6 Copyright (C) 2010 Kai Blin <kai@samba.org>
8 Heavily based on nbtname.c which is:
10 Copyright (C) Andrew Tridgell 2005
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 see rfc1002 for the detailed format of compressed names
30 #include "includes.h"
31 #include "librpc/gen_ndr/ndr_dns.h"
32 #include "librpc/gen_ndr/ndr_misc.h"
33 #include "librpc/gen_ndr/ndr_dnsp.h"
34 #include "system/locale.h"
35 #include "lib/util/util_net.h"
37 /* don't allow an unlimited number of name components */
38 #define MAX_COMPONENTS 128
40 /**
41 print a dns string
43 _PUBLIC_ void ndr_print_dns_string(struct ndr_print *ndr,
44 const char *name,
45 const char *s)
47 ndr_print_string(ndr, name, s);
51 pull one component of a dns_string
53 static enum ndr_err_code ndr_pull_component(struct ndr_pull *ndr,
54 uint8_t **component,
55 uint32_t *offset,
56 uint32_t *max_offset)
58 uint8_t len;
59 unsigned int loops = 0;
60 while (loops < 5) {
61 if (*offset >= ndr->data_size) {
62 return ndr_pull_error(ndr, NDR_ERR_STRING,
63 "BAD DNS NAME component, bad offset");
65 len = ndr->data[*offset];
66 if (len == 0) {
67 *offset += 1;
68 *max_offset = MAX(*max_offset, *offset);
69 *component = NULL;
70 return NDR_ERR_SUCCESS;
72 if ((len & 0xC0) == 0xC0) {
73 /* its a label pointer */
74 if (1 + *offset >= ndr->data_size) {
75 return ndr_pull_error(ndr, NDR_ERR_STRING,
76 "BAD DNS NAME component, " \
77 "bad label offset");
79 *max_offset = MAX(*max_offset, *offset + 2);
80 *offset = ((len&0x3F)<<8) | ndr->data[1 + *offset];
81 *max_offset = MAX(*max_offset, *offset);
82 loops++;
83 continue;
85 if ((len & 0xC0) != 0) {
86 /* its a reserved length field */
87 return ndr_pull_error(ndr, NDR_ERR_STRING,
88 "BAD DNS NAME component, " \
89 "reserved length field: 0x%02x",
90 (len &0xC));
92 if (*offset + len + 1 > ndr->data_size) {
93 return ndr_pull_error(ndr, NDR_ERR_STRING,
94 "BAD DNS NAME component, "\
95 "length too long");
97 *component = (uint8_t*)talloc_strndup(ndr,
98 (const char *)&ndr->data[1 + *offset], len);
99 NDR_ERR_HAVE_NO_MEMORY(*component);
100 *offset += len + 1;
101 *max_offset = MAX(*max_offset, *offset);
102 return NDR_ERR_SUCCESS;
105 /* too many pointers */
106 return ndr_pull_error(ndr, NDR_ERR_STRING,
107 "BAD DNS NAME component, too many pointers");
111 pull a dns_string from the wire
113 _PUBLIC_ enum ndr_err_code ndr_pull_dns_string(struct ndr_pull *ndr,
114 int ndr_flags,
115 const char **s)
117 uint32_t offset = ndr->offset;
118 uint32_t max_offset = offset;
119 unsigned num_components;
120 char *name;
122 if (!(ndr_flags & NDR_SCALARS)) {
123 return NDR_ERR_SUCCESS;
126 name = talloc_strdup(ndr->current_mem_ctx, "");
128 /* break up name into a list of components */
129 for (num_components=0; num_components<MAX_COMPONENTS;
130 num_components++) {
131 uint8_t *component = NULL;
132 NDR_CHECK(ndr_pull_component(ndr, &component, &offset,
133 &max_offset));
134 if (component == NULL) break;
135 if (num_components > 0) {
136 name = talloc_asprintf_append_buffer(name, ".%s",
137 component);
138 } else {
139 name = talloc_asprintf_append_buffer(name, "%s",
140 component);
142 NDR_ERR_HAVE_NO_MEMORY(name);
144 if (num_components == MAX_COMPONENTS) {
145 return ndr_pull_error(ndr, NDR_ERR_STRING,
146 "BAD DNS NAME too many components");
149 (*s) = name;
150 ndr->offset = max_offset;
152 return NDR_ERR_SUCCESS;
156 push a dns string to the wire
158 _PUBLIC_ enum ndr_err_code ndr_push_dns_string(struct ndr_push *ndr,
159 int ndr_flags,
160 const char *s)
162 if (!(ndr_flags & NDR_SCALARS)) {
163 return NDR_ERR_SUCCESS;
166 while (s && *s) {
167 enum ndr_err_code ndr_err;
168 char *compname;
169 size_t complen;
170 uint32_t offset;
172 if (!(ndr->flags & LIBNDR_FLAG_NO_COMPRESSION)) {
173 /* see if we have pushed the remaining string already,
174 * if so we use a label pointer to this string
176 ndr_err = ndr_token_retrieve_cmp_fn(&ndr->dns_string_list, s,
177 &offset,
178 (comparison_fn_t)strcmp,
179 false);
180 if (NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
181 uint8_t b[2];
183 if (offset > 0x3FFF) {
184 return ndr_push_error(ndr, NDR_ERR_STRING,
185 "offset for dns string " \
186 "label pointer " \
187 "%u[%08X] > 0x00003FFF",
188 offset, offset);
191 b[0] = 0xC0 | (offset>>8);
192 b[1] = (offset & 0xFF);
194 return ndr_push_bytes(ndr, b, 2);
198 complen = strcspn(s, ".");
200 /* we need to make sure the length fits into 6 bytes */
201 if (complen > 0x3F) {
202 return ndr_push_error(ndr, NDR_ERR_STRING,
203 "component length %u[%08X] > " \
204 "0x0000003F",
205 (unsigned)complen,
206 (unsigned)complen);
209 compname = talloc_asprintf(ndr, "%c%*.*s",
210 (unsigned char)complen,
211 (unsigned char)complen,
212 (unsigned char)complen, s);
213 NDR_ERR_HAVE_NO_MEMORY(compname);
215 /* remember the current component + the rest of the string
216 * so it can be reused later
218 if (!(ndr->flags & LIBNDR_FLAG_NO_COMPRESSION)) {
219 NDR_CHECK(ndr_token_store(ndr, &ndr->dns_string_list, s,
220 ndr->offset));
223 /* push just this component into the blob */
224 NDR_CHECK(ndr_push_bytes(ndr, (const uint8_t *)compname,
225 complen+1));
226 talloc_free(compname);
228 s += complen;
229 if (*s == '.') s++;
232 /* if we reach the end of the string and have pushed the last component
233 * without using a label pointer, we need to terminate the string
235 return ndr_push_bytes(ndr, (const uint8_t *)"", 1);
238 _PUBLIC_ enum ndr_err_code ndr_pull_dns_txt_record(struct ndr_pull *ndr, int ndr_flags, struct dns_txt_record *r)
240 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
241 if (ndr_flags & NDR_SCALARS) {
242 enum ndr_err_code ndr_err;
243 uint32_t data_size = ndr->data_size;
244 uint32_t record_size = 0;
245 ndr_err = ndr_token_retrieve(&ndr->array_size_list, r,
246 &record_size);
247 if (NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
248 NDR_PULL_NEED_BYTES(ndr, record_size);
249 ndr->data_size = ndr->offset + record_size;
251 NDR_CHECK(ndr_pull_align(ndr, 1));
252 NDR_CHECK(ndr_pull_dnsp_string_list(ndr, NDR_SCALARS, &r->txt));
253 NDR_CHECK(ndr_pull_trailer_align(ndr, 1));
254 ndr->data_size = data_size;
256 if (ndr_flags & NDR_BUFFERS) {
258 return NDR_ERR_SUCCESS;
261 _PUBLIC_ enum ndr_err_code ndr_push_dns_res_rec(struct ndr_push *ndr,
262 int ndr_flags,
263 const struct dns_res_rec *r)
265 uint32_t _flags_save_STRUCT = ndr->flags;
266 uint32_t _saved_offset1, _saved_offset2;
267 uint16_t length;
268 ndr_set_flags(&ndr->flags, LIBNDR_PRINT_ARRAY_HEX |
269 LIBNDR_FLAG_NOALIGN);
270 if (ndr_flags & NDR_SCALARS) {
271 uint32_t _flags_save_name = ndr->flags;
273 NDR_CHECK(ndr_push_align(ndr, 4));
275 switch (r->rr_type) {
276 case DNS_QTYPE_TKEY:
277 case DNS_QTYPE_TSIG:
278 ndr_set_flags(&ndr->flags, LIBNDR_FLAG_NO_COMPRESSION);
279 break;
280 default:
281 break;
283 NDR_CHECK(ndr_push_dns_string(ndr, NDR_SCALARS, r->name));
284 ndr->flags = _flags_save_name;
286 NDR_CHECK(ndr_push_dns_qtype(ndr, NDR_SCALARS, r->rr_type));
287 NDR_CHECK(ndr_push_dns_qclass(ndr, NDR_SCALARS, r->rr_class));
288 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, r->ttl));
289 _saved_offset1 = ndr->offset;
290 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, 0));
291 if (r->length > 0) {
292 uint32_t _saved_offset3;
294 NDR_CHECK(ndr_push_set_switch_value(ndr, &r->rdata,
295 r->rr_type));
296 _saved_offset3 = ndr->offset;
297 NDR_CHECK(ndr_push_dns_rdata(ndr, NDR_SCALARS,
298 &r->rdata));
299 if ((ndr->offset != _saved_offset3) &&
300 (r->unexpected.length > 0)) {
302 * ndr_push_dns_rdata pushed a known
303 * record, but we have something
304 * unexpected. That's invalid.
306 return ndr_push_error(ndr,
307 NDR_ERR_LENGTH,
308 "Invalid...Unexpected " \
309 "blob length is too " \
310 "large");
313 if (r->unexpected.length > UINT16_MAX) {
314 return ndr_push_error(ndr, NDR_ERR_LENGTH,
315 "Unexpected blob length "\
316 "is too large");
319 NDR_CHECK(ndr_push_bytes(ndr, r->unexpected.data,
320 r->unexpected.length));
321 NDR_CHECK(ndr_push_trailer_align(ndr, 4));
322 length = ndr->offset - (_saved_offset1 + 2);
323 _saved_offset2 = ndr->offset;
324 ndr->offset = _saved_offset1;
325 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, length));
326 ndr->offset = _saved_offset2;
328 if (ndr_flags & NDR_BUFFERS) {
329 NDR_CHECK(ndr_push_dns_rdata(ndr, NDR_BUFFERS,
330 &r->rdata));
332 ndr->flags = _flags_save_STRUCT;
333 return NDR_ERR_SUCCESS;
336 _PUBLIC_ enum ndr_err_code ndr_pull_dns_res_rec(struct ndr_pull *ndr,
337 int ndr_flags,
338 struct dns_res_rec *r)
340 uint32_t _flags_save_STRUCT = ndr->flags;
341 uint32_t _saved_offset1;
342 uint32_t pad, length;
344 ndr_set_flags(&ndr->flags, LIBNDR_PRINT_ARRAY_HEX |
345 LIBNDR_FLAG_NOALIGN);
346 if (ndr_flags & NDR_SCALARS) {
347 NDR_CHECK(ndr_pull_align(ndr, 4));
348 NDR_CHECK(ndr_pull_dns_string(ndr, NDR_SCALARS, &r->name));
349 NDR_CHECK(ndr_pull_dns_qtype(ndr, NDR_SCALARS, &r->rr_type));
350 NDR_CHECK(ndr_pull_dns_qclass(ndr, NDR_SCALARS, &r->rr_class));
351 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &r->ttl));
352 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &r->length));
353 _saved_offset1 = ndr->offset;
354 if (r->length > 0) {
355 NDR_CHECK(ndr_token_store(ndr, &ndr->array_size_list,
356 &r->rdata,
357 r->length));
358 NDR_CHECK(ndr_pull_set_switch_value(ndr, &r->rdata,
359 r->rr_type));
360 NDR_CHECK(ndr_pull_dns_rdata(ndr, NDR_SCALARS,
361 &r->rdata));
362 } else {
363 ZERO_STRUCT(r->rdata);
365 length = ndr->offset - _saved_offset1;
366 if (length > r->length) {
367 return ndr_pull_error(ndr, NDR_ERR_LENGTH, "TODO");
370 r->unexpected = data_blob_null;
371 pad = r->length - length;
372 if (pad > 0) {
373 NDR_PULL_NEED_BYTES(ndr, pad);
374 r->unexpected = data_blob_talloc(ndr->current_mem_ctx,
375 ndr->data +
376 ndr->offset,
377 pad);
378 if (r->unexpected.data == NULL) {
379 return ndr_pull_error(ndr,
380 NDR_ERR_ALLOC,
381 "Failed to allocate a " \
382 "data blob");
384 ndr->offset += pad;
388 NDR_CHECK(ndr_pull_trailer_align(ndr, 4));
390 if (ndr_flags & NDR_BUFFERS) {
391 NDR_CHECK(ndr_pull_dns_rdata(ndr, NDR_BUFFERS, &r->rdata));
393 ndr->flags = _flags_save_STRUCT;
394 return NDR_ERR_SUCCESS;