2 Unix SMB/CIFS implementation.
4 manipulate nbt name structures
6 Copyright (C) Andrew Tridgell 2005
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/>.
23 see rfc1002 for the detailed format of compressed names
27 #include "librpc/gen_ndr/ndr_nbt.h"
28 #include "librpc/gen_ndr/ndr_misc.h"
30 /* don't allow an unlimited number of name components */
31 #define MAX_COMPONENTS 10
36 _PUBLIC_
void ndr_print_nbt_string(struct ndr_print
*ndr
, const char *name
, const char *s
)
38 ndr_print_string(ndr
, name
, s
);
42 pull one component of a nbt_string
44 static enum ndr_err_code
ndr_pull_component(struct ndr_pull
*ndr
,
52 if (*offset
>= ndr
->data_size
) {
53 return ndr_pull_error(ndr
, NDR_ERR_STRING
,
54 "BAD NBT NAME component");
56 len
= ndr
->data
[*offset
];
59 *max_offset
= MAX(*max_offset
, *offset
);
61 return NDR_ERR_SUCCESS
;
63 if ((len
& 0xC0) == 0xC0) {
64 /* its a label pointer */
65 if (1 + *offset
>= ndr
->data_size
) {
66 return ndr_pull_error(ndr
, NDR_ERR_STRING
,
67 "BAD NBT NAME component");
69 *max_offset
= MAX(*max_offset
, *offset
+ 2);
70 *offset
= ((len
&0x3F)<<8) | ndr
->data
[1 + *offset
];
71 *max_offset
= MAX(*max_offset
, *offset
);
75 if ((len
& 0xC0) != 0) {
76 /* its a reserved length field */
77 return ndr_pull_error(ndr
, NDR_ERR_STRING
,
78 "BAD NBT NAME component");
80 if (*offset
+ len
+ 2 > ndr
->data_size
) {
81 return ndr_pull_error(ndr
, NDR_ERR_STRING
,
82 "BAD NBT NAME component");
84 *component
= (uint8_t*)talloc_strndup(ndr
, (const char *)&ndr
->data
[1 + *offset
], len
);
85 NDR_ERR_HAVE_NO_MEMORY(*component
);
87 *max_offset
= MAX(*max_offset
, *offset
);
88 return NDR_ERR_SUCCESS
;
91 /* too many pointers */
92 return ndr_pull_error(ndr
, NDR_ERR_STRING
, "BAD NBT NAME component");
96 pull a nbt_string from the wire
98 _PUBLIC_
enum ndr_err_code
ndr_pull_nbt_string(struct ndr_pull
*ndr
, int ndr_flags
, const char **s
)
100 uint32_t offset
= ndr
->offset
;
101 uint32_t max_offset
= offset
;
102 unsigned num_components
;
105 if (!(ndr_flags
& NDR_SCALARS
)) {
106 return NDR_ERR_SUCCESS
;
111 /* break up name into a list of components */
112 for (num_components
=0;num_components
<MAX_COMPONENTS
;num_components
++) {
113 uint8_t *component
= NULL
;
114 NDR_CHECK(ndr_pull_component(ndr
, &component
, &offset
, &max_offset
));
115 if (component
== NULL
) break;
117 name
= talloc_asprintf_append_buffer(name
, ".%s", component
);
118 NDR_ERR_HAVE_NO_MEMORY(name
);
120 name
= (char *)component
;
123 if (num_components
== MAX_COMPONENTS
) {
124 return ndr_pull_error(ndr
, NDR_ERR_STRING
,
125 "BAD NBT NAME too many components");
127 if (num_components
== 0) {
128 name
= talloc_strdup(ndr
, "");
129 NDR_ERR_HAVE_NO_MEMORY(name
);
133 ndr
->offset
= max_offset
;
135 return NDR_ERR_SUCCESS
;
139 push a nbt string to the wire
141 _PUBLIC_
enum ndr_err_code
ndr_push_nbt_string(struct ndr_push
*ndr
, int ndr_flags
, const char *s
)
143 if (!(ndr_flags
& NDR_SCALARS
)) {
144 return NDR_ERR_SUCCESS
;
148 enum ndr_err_code ndr_err
;
153 /* see if we have pushed the remaing string allready,
154 * if so we use a label pointer to this string
156 ndr_err
= ndr_token_retrieve_cmp_fn(&ndr
->nbt_string_list
, s
, &offset
, (comparison_fn_t
)strcmp
, false);
157 if (NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
160 if (offset
> 0x3FFF) {
161 return ndr_push_error(ndr
, NDR_ERR_STRING
,
162 "offset for nbt string label pointer %u[%08X] > 0x00003FFF",
166 b
[0] = 0xC0 | (offset
>>8);
167 b
[1] = (offset
& 0xFF);
169 return ndr_push_bytes(ndr
, b
, 2);
172 complen
= strcspn(s
, ".");
174 /* we need to make sure the length fits into 6 bytes */
175 if (complen
>= 0x3F) {
176 return ndr_push_error(ndr
, NDR_ERR_STRING
,
177 "component length %u[%08X] > 0x00003F",
178 (unsigned)complen
, (unsigned)complen
);
181 compname
= talloc_asprintf(ndr
, "%c%*.*s",
182 (unsigned char)complen
,
183 (unsigned char)complen
,
184 (unsigned char)complen
, s
);
185 NDR_ERR_HAVE_NO_MEMORY(compname
);
187 /* remember the current componemt + the rest of the string
188 * so it can be reused later
190 NDR_CHECK(ndr_token_store(ndr
, &ndr
->nbt_string_list
, s
, ndr
->offset
));
192 /* push just this component into the blob */
193 NDR_CHECK(ndr_push_bytes(ndr
, (const uint8_t *)compname
, complen
+1));
194 talloc_free(compname
);
200 /* if we reach the end of the string and have pushed the last component
201 * without using a label pointer, we need to terminate the string
203 return ndr_push_bytes(ndr
, (const uint8_t *)"", 1);
208 decompress a 'compressed' name component
210 static bool decompress_name(char *name
, enum nbt_name_type
*type
)
213 for (i
=0;name
[2*i
];i
++) {
214 uint8_t c1
= name
[2*i
];
215 uint8_t c2
= name
[1+(2*i
)];
216 if (c1
< 'A' || c1
> 'P' ||
217 c2
< 'A' || c2
> 'P') {
220 name
[i
] = ((c1
-'A')<<4) | (c2
-'A');
224 *type
= (enum nbt_name_type
)(name
[15]);
228 *type
= NBT_NAME_CLIENT
;
231 /* trim trailing spaces */
232 for (;i
>0 && name
[i
-1]==' ';i
--) {
241 compress a name component
243 static uint8_t *compress_name(TALLOC_CTX
*mem_ctx
,
244 const uint8_t *name
, enum nbt_name_type type
)
250 if (strlen((const char *)name
) > 15) {
254 cname
= talloc_array(mem_ctx
, uint8_t, 33);
255 if (cname
== NULL
) return NULL
;
257 for (i
=0;name
[i
];i
++) {
258 cname
[2*i
] = 'A' + (name
[i
]>>4);
259 cname
[1+2*i
] = 'A' + (name
[i
]&0xF);
261 if (strcmp((const char *)name
, "*") == 0) {
267 cname
[2*i
] = 'A' + (pad_char
>>4);
268 cname
[1+2*i
] = 'A' + (pad_char
&0xF);
272 cname
[2*i
] = 'A' + (pad_char
>>4);
273 cname
[1+2*i
] = 'A' + (pad_char
&0xF);
281 pull a nbt name from the wire
283 _PUBLIC_
enum ndr_err_code
ndr_pull_nbt_name(struct ndr_pull
*ndr
, int ndr_flags
, struct nbt_name
*r
)
290 if (!(ndr_flags
& NDR_SCALARS
)) {
291 return NDR_ERR_SUCCESS
;
294 NDR_CHECK(ndr_pull_nbt_string(ndr
, ndr_flags
, &s
));
296 scope
= (uint8_t *)strchr(s
, '.');
299 r
->scope
= talloc_strdup(ndr
->current_mem_ctx
, (const char *)&scope
[1]);
300 NDR_ERR_HAVE_NO_MEMORY(r
->scope
);
305 cname
= discard_const_p(char, s
);
307 /* the first component is limited to 16 bytes in the DOS charset,
308 which is 32 in the 'compressed' form */
309 if (strlen(cname
) > 32) {
310 return ndr_pull_error(ndr
, NDR_ERR_STRING
,
311 "NBT NAME cname > 32");
314 /* decompress the first component */
315 ok
= decompress_name(cname
, &r
->type
);
317 return ndr_pull_error(ndr
, NDR_ERR_STRING
,
318 "NBT NAME failed to decompress");
321 r
->name
= talloc_strdup(ndr
->current_mem_ctx
, cname
);
322 NDR_ERR_HAVE_NO_MEMORY(r
->name
);
326 return NDR_ERR_SUCCESS
;
330 push a nbt name to the wire
332 _PUBLIC_
enum ndr_err_code
ndr_push_nbt_name(struct ndr_push
*ndr
, int ndr_flags
, const struct nbt_name
*r
)
334 uint8_t *cname
, *fullname
;
335 enum ndr_err_code ndr_err
;
337 if (!(ndr_flags
& NDR_SCALARS
)) {
338 return NDR_ERR_SUCCESS
;
341 if (strlen(r
->name
) > 15) {
342 return ndr_push_error(ndr
, NDR_ERR_STRING
,
343 "nbt_name longer as 15 chars: %s",
347 cname
= compress_name(ndr
, (const uint8_t *)r
->name
, r
->type
);
348 NDR_ERR_HAVE_NO_MEMORY(cname
);
351 fullname
= (uint8_t *)talloc_asprintf(ndr
, "%s.%s", cname
, r
->scope
);
352 NDR_ERR_HAVE_NO_MEMORY(fullname
);
358 ndr_err
= ndr_push_nbt_string(ndr
, ndr_flags
, (const char *)fullname
);
365 copy a nbt name structure
367 _PUBLIC_ NTSTATUS
nbt_name_dup(TALLOC_CTX
*mem_ctx
, struct nbt_name
*name
, struct nbt_name
*newname
)
370 newname
->name
= talloc_strdup(mem_ctx
, newname
->name
);
371 NT_STATUS_HAVE_NO_MEMORY(newname
->name
);
372 newname
->scope
= talloc_strdup(mem_ctx
, newname
->scope
);
374 NT_STATUS_HAVE_NO_MEMORY(newname
->scope
);
380 push a nbt name into a blob
382 _PUBLIC_ NTSTATUS
nbt_name_to_blob(TALLOC_CTX
*mem_ctx
, DATA_BLOB
*blob
, struct nbt_name
*name
)
384 enum ndr_err_code ndr_err
;
386 ndr_err
= ndr_push_struct_blob(blob
, mem_ctx
, name
, (ndr_push_flags_fn_t
)ndr_push_nbt_name
);
387 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
388 return ndr_map_error2ntstatus(ndr_err
);
395 pull a nbt name from a blob
397 _PUBLIC_ NTSTATUS
nbt_name_from_blob(TALLOC_CTX
*mem_ctx
, const DATA_BLOB
*blob
, struct nbt_name
*name
)
399 enum ndr_err_code ndr_err
;
401 ndr_err
= ndr_pull_struct_blob(blob
, mem_ctx
, name
,
402 (ndr_pull_flags_fn_t
)ndr_pull_nbt_name
);
403 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
404 return ndr_map_error2ntstatus(ndr_err
);
412 choose a name to use when calling a server in a NBT session request.
413 we use heuristics to see if the name we have been given is a IP
414 address, or a too-long name. If it is then use *SMBSERVER, or a
417 _PUBLIC_
void nbt_choose_called_name(TALLOC_CTX
*mem_ctx
,
418 struct nbt_name
*n
, const char *name
, int type
)
423 if ((name
== NULL
) || is_ipaddress(name
)) {
424 n
->name
= "*SMBSERVER";
427 if (strlen(name
) > 15) {
428 const char *p
= strchr(name
, '.');
431 n
->name
= "*SMBSERVER";
434 s
= talloc_strndup(mem_ctx
, name
, PTR_DIFF(p
, name
));
435 n
->name
= talloc_strdup_upper(mem_ctx
, s
);
439 n
->name
= talloc_strdup_upper(mem_ctx
, name
);
444 escape a string into a form containing only a small set of characters,
445 the rest is hex encoded. This is similar to URL encoding
447 static const char *nbt_hex_encode(TALLOC_CTX
*mem_ctx
, const char *s
)
451 const char *valid_chars
= "_-.$@ ";
452 #define NBT_CHAR_ALLOW(c) (isalnum((unsigned char)c) || strchr(valid_chars, c))
454 for (len
=i
=0;s
[i
];i
++,len
++) {
455 if (!NBT_CHAR_ALLOW(s
[i
])) {
460 ret
= talloc_array(mem_ctx
, char, len
+1);
461 if (ret
== NULL
) return NULL
;
463 for (len
=i
=0;s
[i
];i
++) {
464 if (NBT_CHAR_ALLOW(s
[i
])) {
467 snprintf(&ret
[len
], 4, "%%%02x", (unsigned char)s
[i
]);
478 form a string for a NBT name
480 _PUBLIC_
char *nbt_name_string(TALLOC_CTX
*mem_ctx
, const struct nbt_name
*name
)
482 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
485 ret
= talloc_asprintf(mem_ctx
, "%s<%02x>-%s",
486 nbt_hex_encode(tmp_ctx
, name
->name
),
488 nbt_hex_encode(tmp_ctx
, name
->scope
));
490 ret
= talloc_asprintf(mem_ctx
, "%s<%02x>",
491 nbt_hex_encode(tmp_ctx
, name
->name
),
494 talloc_free(tmp_ctx
);
499 pull a nbt name, WINS Replication uses another on wire format for nbt name
501 _PUBLIC_
enum ndr_err_code
ndr_pull_wrepl_nbt_name(struct ndr_pull
*ndr
, int ndr_flags
, const struct nbt_name
**_r
)
505 uint32_t namebuf_len
;
507 if (!(ndr_flags
& NDR_SCALARS
)) {
508 return NDR_ERR_SUCCESS
;
511 NDR_CHECK(ndr_pull_align(ndr
, 4));
512 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &namebuf_len
));
513 if (namebuf_len
< 1 || namebuf_len
> 255) {
514 return ndr_pull_error(ndr
, NDR_ERR_ALLOC
, "value out of range");
516 NDR_PULL_ALLOC_N(ndr
, namebuf
, namebuf_len
);
517 NDR_CHECK(ndr_pull_array_uint8(ndr
, NDR_SCALARS
, namebuf
, namebuf_len
));
519 NDR_PULL_ALLOC(ndr
, r
);
521 /* oh wow, what a nasty bug in windows ... */
522 if (namebuf
[0] == 0x1b && namebuf_len
>= 16) {
523 namebuf
[0] = namebuf
[15];
527 if (namebuf_len
< 17) {
530 r
->name
= talloc_strndup(r
, (char *)namebuf
, namebuf_len
);
531 if (!r
->name
) return ndr_pull_error(ndr
, NDR_ERR_ALLOC
, "out of memory");
535 talloc_free(namebuf
);
537 return NDR_ERR_SUCCESS
;
540 r
->type
= namebuf
[15];
543 trim_string((char *)namebuf
, NULL
, " ");
544 r
->name
= talloc_strdup(r
, (char *)namebuf
);
545 if (!r
->name
) return ndr_pull_error(ndr
, NDR_ERR_ALLOC
, "out of memory");
547 if (namebuf_len
> 18) {
548 r
->scope
= talloc_strndup(r
, (char *)(namebuf
+17), namebuf_len
-17);
549 if (!r
->scope
) return ndr_pull_error(ndr
, NDR_ERR_ALLOC
, "out of memory");
554 talloc_free(namebuf
);
556 return NDR_ERR_SUCCESS
;
560 push a nbt name, WINS Replication uses another on wire format for nbt name
562 _PUBLIC_
enum ndr_err_code
ndr_push_wrepl_nbt_name(struct ndr_push
*ndr
, int ndr_flags
, const struct nbt_name
*r
)
565 uint32_t namebuf_len
;
567 uint32_t scope_len
= 0;
570 return ndr_push_error(ndr
, NDR_ERR_INVALID_POINTER
,
571 "wrepl_nbt_name NULL pointer");
574 if (!(ndr_flags
& NDR_SCALARS
)) {
575 return NDR_ERR_SUCCESS
;
578 _name_len
= strlen(r
->name
);
579 if (_name_len
> 15) {
580 return ndr_push_error(ndr
, NDR_ERR_STRING
,
581 "wrepl_nbt_name longer as 15 chars: %s",
586 scope_len
= strlen(r
->scope
);
588 if (scope_len
> 238) {
589 return ndr_push_error(ndr
, NDR_ERR_STRING
,
590 "wrepl_nbt_name scope longer as 238 chars: %s",
594 namebuf
= (uint8_t *)talloc_asprintf(ndr
, "%-15s%c%s",
596 (r
->scope
?r
->scope
:""));
597 if (!namebuf
) return ndr_push_error(ndr
, NDR_ERR_ALLOC
, "out of memory");
599 namebuf_len
= strlen((char *)namebuf
) + 1;
602 * we need to set the type here, and use a place-holder in the talloc_asprintf()
603 * as the type can be 0x00, and then the namebuf_len = strlen(namebuf); would give wrong results
605 namebuf
[15] = r
->type
;
607 /* oh wow, what a nasty bug in windows ... */
608 if (r
->type
== 0x1b) {
609 namebuf
[15] = namebuf
[0];
613 NDR_CHECK(ndr_push_align(ndr
, 4));
614 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, namebuf_len
));
615 NDR_CHECK(ndr_push_array_uint8(ndr
, NDR_SCALARS
, namebuf
, namebuf_len
));
617 talloc_free(namebuf
);
618 return NDR_ERR_SUCCESS
;
621 _PUBLIC_
void ndr_print_wrepl_nbt_name(struct ndr_print
*ndr
, const char *name
, const struct nbt_name
*r
)
623 char *s
= nbt_name_string(ndr
, r
);
624 ndr_print_string(ndr
, name
, s
);
628 _PUBLIC_
enum ndr_err_code
ndr_push_nbt_res_rec(struct ndr_push
*ndr
, int ndr_flags
, const struct nbt_res_rec
*r
)
631 uint32_t _flags_save_STRUCT
= ndr
->flags
;
632 ndr_set_flags(&ndr
->flags
, LIBNDR_PRINT_ARRAY_HEX
);
633 if (ndr_flags
& NDR_SCALARS
) {
634 NDR_CHECK(ndr_push_align(ndr
, 4));
635 NDR_CHECK(ndr_push_nbt_name(ndr
, NDR_SCALARS
, &r
->name
));
636 NDR_CHECK(ndr_push_nbt_qtype(ndr
, NDR_SCALARS
, r
->rr_type
));
637 NDR_CHECK(ndr_push_nbt_qclass(ndr
, NDR_SCALARS
, r
->rr_class
));
638 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, r
->ttl
));
639 NDR_CHECK(ndr_push_set_switch_value(ndr
, &r
->rdata
, ((((r
->rr_type
) == NBT_QTYPE_NETBIOS
) && ((r
->rdata
).data
.length
== 2))?0:r
->rr_type
)));
640 NDR_CHECK(ndr_push_nbt_rdata(ndr
, NDR_SCALARS
, &r
->rdata
));
642 if (ndr_flags
& NDR_BUFFERS
) {
644 ndr
->flags
= _flags_save_STRUCT
;
646 return NDR_ERR_SUCCESS
;