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
31 #include "librpc/gen_ndr/ndr_dns.h"
32 #include "librpc/gen_ndr/ndr_misc.h"
33 #include "system/locale.h"
34 #include "lib/util/util_net.h"
36 /* don't allow an unlimited number of name components */
37 #define MAX_COMPONENTS 128
42 _PUBLIC_
void ndr_print_dns_string(struct ndr_print
*ndr
, const char *name
, const char *s
)
44 ndr_print_string(ndr
, name
, s
);
48 pull one component of a dns_string
50 static enum ndr_err_code
ndr_pull_component(struct ndr_pull
*ndr
,
56 unsigned int loops
= 0;
58 if (*offset
>= ndr
->data_size
) {
59 return ndr_pull_error(ndr
, NDR_ERR_STRING
,
60 "BAD DNS NAME component, bad offset");
62 len
= ndr
->data
[*offset
];
65 *max_offset
= MAX(*max_offset
, *offset
);
67 return NDR_ERR_SUCCESS
;
69 if ((len
& 0xC0) == 0xC0) {
70 /* its a label pointer */
71 if (1 + *offset
>= ndr
->data_size
) {
72 return ndr_pull_error(ndr
, NDR_ERR_STRING
,
73 "BAD DNS NAME component, bad label offset");
75 *max_offset
= MAX(*max_offset
, *offset
+ 2);
76 *offset
= ((len
&0x3F)<<8) | ndr
->data
[1 + *offset
];
77 *max_offset
= MAX(*max_offset
, *offset
);
81 if ((len
& 0xC0) != 0) {
82 /* its a reserved length field */
83 return ndr_pull_error(ndr
, NDR_ERR_STRING
,
84 "BAD DNS NAME component, reserved lenght field: 0x%02x", (len
&0xC));
86 if (*offset
+ len
+ 2 > ndr
->data_size
) {
87 return ndr_pull_error(ndr
, NDR_ERR_STRING
,
88 "BAD DNS NAME component, length too long");
90 *component
= (uint8_t*)talloc_strndup(ndr
, (const char *)&ndr
->data
[1 + *offset
], len
);
91 NDR_ERR_HAVE_NO_MEMORY(*component
);
93 *max_offset
= MAX(*max_offset
, *offset
);
94 return NDR_ERR_SUCCESS
;
97 /* too many pointers */
98 return ndr_pull_error(ndr
, NDR_ERR_STRING
, "BAD DNS NAME component, too many pointers");
102 pull a dns_string from the wire
104 _PUBLIC_
enum ndr_err_code
ndr_pull_dns_string(struct ndr_pull
*ndr
, int ndr_flags
, const char **s
)
106 uint32_t offset
= ndr
->offset
;
107 uint32_t max_offset
= offset
;
108 unsigned num_components
;
111 if (!(ndr_flags
& NDR_SCALARS
)) {
112 return NDR_ERR_SUCCESS
;
115 name
= talloc_strdup(ndr
->current_mem_ctx
, "");
117 /* break up name into a list of components */
118 for (num_components
=0;num_components
<MAX_COMPONENTS
;num_components
++) {
119 uint8_t *component
= NULL
;
120 NDR_CHECK(ndr_pull_component(ndr
, &component
, &offset
, &max_offset
));
121 if (component
== NULL
) break;
122 if (num_components
> 0) {
123 name
= talloc_asprintf_append_buffer(name
, ".%s", component
);
125 name
= talloc_asprintf_append_buffer(name
, "%s", component
);
127 NDR_ERR_HAVE_NO_MEMORY(name
);
129 if (num_components
== MAX_COMPONENTS
) {
130 return ndr_pull_error(ndr
, NDR_ERR_STRING
,
131 "BAD DNS NAME too many components");
135 ndr
->offset
= max_offset
;
137 return NDR_ERR_SUCCESS
;
141 push a dns string to the wire
143 _PUBLIC_
enum ndr_err_code
ndr_push_dns_string(struct ndr_push
*ndr
, int ndr_flags
, const char *s
)
145 if (!(ndr_flags
& NDR_SCALARS
)) {
146 return NDR_ERR_SUCCESS
;
150 enum ndr_err_code ndr_err
;
155 /* see if we have pushed the remaing string allready,
156 * if so we use a label pointer to this string
158 ndr_err
= ndr_token_retrieve_cmp_fn(&ndr
->dns_string_list
, s
, &offset
, (comparison_fn_t
)strcmp
, false);
159 if (NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
162 if (offset
> 0x3FFF) {
163 return ndr_push_error(ndr
, NDR_ERR_STRING
,
164 "offset for dns string label pointer %u[%08X] > 0x00003FFF",
168 b
[0] = 0xC0 | (offset
>>8);
169 b
[1] = (offset
& 0xFF);
171 return ndr_push_bytes(ndr
, b
, 2);
174 complen
= strcspn(s
, ".");
176 /* we need to make sure the length fits into 6 bytes */
177 if (complen
> 0x3F) {
178 return ndr_push_error(ndr
, NDR_ERR_STRING
,
179 "component length %u[%08X] > 0x0000003F",
180 (unsigned)complen
, (unsigned)complen
);
183 compname
= talloc_asprintf(ndr
, "%c%*.*s",
184 (unsigned char)complen
,
185 (unsigned char)complen
,
186 (unsigned char)complen
, s
);
187 NDR_ERR_HAVE_NO_MEMORY(compname
);
189 /* remember the current componemt + the rest of the string
190 * so it can be reused later
192 NDR_CHECK(ndr_token_store(ndr
, &ndr
->dns_string_list
, s
, ndr
->offset
));
194 /* push just this component into the blob */
195 NDR_CHECK(ndr_push_bytes(ndr
, (const uint8_t *)compname
, complen
+1));
196 talloc_free(compname
);
202 /* if we reach the end of the string and have pushed the last component
203 * without using a label pointer, we need to terminate the string
205 return ndr_push_bytes(ndr
, (const uint8_t *)"", 1);
208 _PUBLIC_
enum ndr_err_code
ndr_push_dns_res_rec(struct ndr_push
*ndr
, int ndr_flags
, const struct dns_res_rec
*r
)
211 uint32_t _flags_save_STRUCT
= ndr
->flags
;
212 uint32_t _saved_offset1
, _saved_offset2
;
214 ndr_set_flags(&ndr
->flags
, LIBNDR_PRINT_ARRAY_HEX
|LIBNDR_FLAG_NOALIGN
);
215 if (ndr_flags
& NDR_SCALARS
) {
216 NDR_CHECK(ndr_push_align(ndr
, 4));
217 NDR_CHECK(ndr_push_dns_string(ndr
, NDR_SCALARS
, r
->name
));
218 NDR_CHECK(ndr_push_dns_qtype(ndr
, NDR_SCALARS
, r
->rr_type
));
219 NDR_CHECK(ndr_push_dns_qclass(ndr
, NDR_SCALARS
, r
->rr_class
));
220 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, r
->ttl
));
221 _saved_offset1
= ndr
->offset
;
222 NDR_CHECK(ndr_push_uint16(ndr
, NDR_SCALARS
, 0));
224 NDR_CHECK(ndr_push_set_switch_value(ndr
, &r
->rdata
, r
->rr_type
));
225 NDR_CHECK(ndr_push_dns_rdata(ndr
, NDR_SCALARS
, &r
->rdata
));
226 if (r
->unexpected
.length
> 0) {
227 return ndr_push_error(ndr
, NDR_ERR_LENGTH
,
228 "Invalid...Unexpected blob lenght is too large");
231 if (r
->unexpected
.length
> UINT16_MAX
) {
232 return ndr_push_error(ndr
, NDR_ERR_LENGTH
,
233 "Unexpected blob lenght is too large");
236 NDR_CHECK(ndr_push_bytes(ndr
, r
->unexpected
.data
, r
->unexpected
.length
));
237 NDR_CHECK(ndr_push_trailer_align(ndr
, 4));
238 length
= ndr
->offset
- (_saved_offset1
+ 2);
239 _saved_offset2
= ndr
->offset
;
240 ndr
->offset
= _saved_offset1
;
241 NDR_CHECK(ndr_push_uint16(ndr
, NDR_SCALARS
, length
));
242 ndr
->offset
= _saved_offset2
;
244 if (ndr_flags
& NDR_BUFFERS
) {
245 NDR_CHECK(ndr_push_dns_rdata(ndr
, NDR_BUFFERS
, &r
->rdata
));
247 ndr
->flags
= _flags_save_STRUCT
;
249 return NDR_ERR_SUCCESS
;
252 _PUBLIC_
enum ndr_err_code
ndr_pull_dns_res_rec(struct ndr_pull
*ndr
, int ndr_flags
, struct dns_res_rec
*r
)
255 uint32_t _flags_save_STRUCT
= ndr
->flags
;
256 uint32_t _saved_offset1
;
257 uint32_t pad
, length
;
259 ndr_set_flags(&ndr
->flags
, LIBNDR_PRINT_ARRAY_HEX
|LIBNDR_FLAG_NOALIGN
);
260 if (ndr_flags
& NDR_SCALARS
) {
261 NDR_CHECK(ndr_pull_align(ndr
, 4));
262 NDR_CHECK(ndr_pull_dns_string(ndr
, NDR_SCALARS
, &r
->name
));
263 NDR_CHECK(ndr_pull_dns_qtype(ndr
, NDR_SCALARS
, &r
->rr_type
));
264 NDR_CHECK(ndr_pull_dns_qclass(ndr
, NDR_SCALARS
, &r
->rr_class
));
265 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &r
->ttl
));
266 NDR_CHECK(ndr_pull_uint16(ndr
, NDR_SCALARS
, &r
->length
));
267 _saved_offset1
= ndr
->offset
;
269 NDR_CHECK(ndr_pull_set_switch_value(ndr
, &r
->rdata
, r
->rr_type
));
270 NDR_CHECK(ndr_pull_dns_rdata(ndr
, NDR_SCALARS
, &r
->rdata
));
272 ZERO_STRUCT(r
->rdata
);
274 length
= ndr
->offset
- _saved_offset1
;
275 if (length
> r
->length
) {
276 return ndr_pull_error(ndr
, NDR_ERR_LENGTH
,
280 r
->unexpected
= data_blob_null
;
281 pad
= r
->length
- length
;
283 NDR_PULL_NEED_BYTES(ndr
, pad
);
284 r
->unexpected
= data_blob_talloc(ndr
->current_mem_ctx
,
285 ndr
->data
+ ndr
->offset
,
287 if (r
->unexpected
.data
== NULL
) {
288 return ndr_pull_error(ndr
, NDR_ERR_ALLOC
,
289 "Failed to allocate a data blob");
295 NDR_CHECK(ndr_pull_trailer_align(ndr
, 4));
297 if (ndr_flags
& NDR_BUFFERS
) {
298 NDR_CHECK(ndr_pull_dns_rdata(ndr
, NDR_BUFFERS
, &r
->rdata
));
300 ndr
->flags
= _flags_save_STRUCT
;
302 return NDR_ERR_SUCCESS
;