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"
29 #include "system/locale.h"
31 /* don't allow an unlimited number of name components */
32 #define MAX_COMPONENTS 10
37 _PUBLIC_ void ndr_print_nbt_string(struct ndr_print *ndr, const char *name, const char *s)
39 ndr_print_string(ndr, name, s);
43 pull one component of a nbt_string
45 static NTSTATUS ndr_pull_component(struct ndr_pull *ndr, uint8_t **component,
46 uint32_t *offset, uint32_t *max_offset)
51 if (*offset >= ndr->data_size) {
52 return NT_STATUS_BAD_NETWORK_NAME;
54 len = ndr->data[*offset];
57 *max_offset = MAX(*max_offset, *offset);
61 if ((len & 0xC0) == 0xC0) {
62 /* its a label pointer */
63 if (1 + *offset >= ndr->data_size) {
64 return NT_STATUS_BAD_NETWORK_NAME;
66 *max_offset = MAX(*max_offset, *offset + 2);
67 *offset = ((len&0x3F)<<8) | ndr->data[1 + *offset];
68 *max_offset = MAX(*max_offset, *offset);
72 if ((len & 0xC0) != 0) {
73 /* its a reserved length field */
74 return NT_STATUS_BAD_NETWORK_NAME;
76 if (*offset + len + 2 > ndr->data_size) {
77 return NT_STATUS_BAD_NETWORK_NAME;
79 *component = (uint8_t*)talloc_strndup(ndr, (const char *)&ndr->data[1 + *offset], len);
80 NT_STATUS_HAVE_NO_MEMORY(*component);
82 *max_offset = MAX(*max_offset, *offset);
86 /* too many pointers */
87 return NT_STATUS_BAD_NETWORK_NAME;
91 pull a nbt_string from the wire
93 _PUBLIC_ NTSTATUS ndr_pull_nbt_string(struct ndr_pull *ndr, int ndr_flags, const char **s)
96 uint32_t offset = ndr->offset;
97 uint32_t max_offset = offset;
98 unsigned num_components;
101 if (!(ndr_flags & NDR_SCALARS)) {
107 /* break up name into a list of components */
108 for (num_components=0;num_components<MAX_COMPONENTS;num_components++) {
110 status = ndr_pull_component(ndr, &component, &offset, &max_offset);
111 NT_STATUS_NOT_OK_RETURN(status);
112 if (component == NULL) break;
114 name = talloc_asprintf_append(name, ".%s", component);
115 NT_STATUS_HAVE_NO_MEMORY(name);
117 name = (char *)component;
120 if (num_components == MAX_COMPONENTS) {
121 return NT_STATUS_BAD_NETWORK_NAME;
123 if (num_components == 0) {
124 name = talloc_strdup(ndr, "");
125 NT_STATUS_HAVE_NO_MEMORY(name);
129 ndr->offset = max_offset;
135 push a nbt string to the wire
137 _PUBLIC_ NTSTATUS ndr_push_nbt_string(struct ndr_push *ndr, int ndr_flags, const char *s)
139 if (!(ndr_flags & NDR_SCALARS)) {
149 /* see if we have pushed the remaing string allready,
150 * if so we use a label pointer to this string
152 status = ndr_token_retrieve_cmp_fn(&ndr->nbt_string_list, s, &offset, (comparison_fn_t)strcmp, False);
153 if (NT_STATUS_IS_OK(status)) {
156 if (offset > 0x3FFF) {
157 return ndr_push_error(ndr, NDR_ERR_STRING,
158 "offset for nbt string label pointer %u[%08X] > 0x00003FFF",
162 b[0] = 0xC0 | (offset>>8);
163 b[1] = (offset & 0xFF);
165 return ndr_push_bytes(ndr, b, 2);
168 complen = strcspn(s, ".");
170 /* we need to make sure the length fits into 6 bytes */
171 if (complen >= 0x3F) {
172 return ndr_push_error(ndr, NDR_ERR_STRING,
173 "component length %u[%08X] > 0x00003F",
174 (unsigned)complen, (unsigned)complen);
177 compname = talloc_asprintf(ndr, "%c%*.*s",
178 (unsigned char)complen,
179 (unsigned char)complen,
180 (unsigned char)complen, s);
181 NT_STATUS_HAVE_NO_MEMORY(compname);
183 /* remember the current componemt + the rest of the string
184 * so it can be reused later
186 NDR_CHECK(ndr_token_store(ndr, &ndr->nbt_string_list, s, ndr->offset));
188 /* push just this component into the blob */
189 NDR_CHECK(ndr_push_bytes(ndr, (const uint8_t *)compname, complen+1));
190 talloc_free(compname);
196 /* if we reach the end of the string and have pushed the last component
197 * without using a label pointer, we need to terminate the string
199 return ndr_push_bytes(ndr, (const uint8_t *)"", 1);
204 decompress a 'compressed' name component
206 static NTSTATUS decompress_name(char *name, enum nbt_name_type *type)
209 for (i=0;name[2*i];i++) {
210 uint8_t c1 = name[2*i];
211 uint8_t c2 = name[1+(2*i)];
212 if (c1 < 'A' || c1 > 'P' ||
213 c2 < 'A' || c2 > 'P') {
214 return NT_STATUS_BAD_NETWORK_NAME;
216 name[i] = ((c1-'A')<<4) | (c2-'A');
220 *type = (enum nbt_name_type)(name[15]);
224 *type = NBT_NAME_CLIENT;
227 /* trim trailing spaces */
228 for (;i>0 && name[i-1]==' ';i--) {
237 compress a name component
239 static uint8_t *compress_name(TALLOC_CTX *mem_ctx,
240 const uint8_t *name, enum nbt_name_type type)
246 if (strlen((const char *)name) > 15) {
250 cname = talloc_array(mem_ctx, uint8_t, 33);
251 if (cname == NULL) return NULL;
253 for (i=0;name[i];i++) {
254 cname[2*i] = 'A' + (name[i]>>4);
255 cname[1+2*i] = 'A' + (name[i]&0xF);
257 if (strcmp((const char *)name, "*") == 0) {
263 cname[2*i] = 'A' + (pad_char>>4);
264 cname[1+2*i] = 'A' + (pad_char&0xF);
268 cname[2*i] = 'A' + (pad_char>>4);
269 cname[1+2*i] = 'A' + (pad_char&0xF);
277 pull a nbt name from the wire
279 _PUBLIC_ NTSTATUS ndr_pull_nbt_name(struct ndr_pull *ndr, int ndr_flags, struct nbt_name *r)
286 if (!(ndr_flags & NDR_SCALARS)) {
290 status = ndr_pull_nbt_string(ndr, ndr_flags, &s);
291 NT_STATUS_NOT_OK_RETURN(status);
293 scope = (uint8_t *)strchr(s, '.');
296 r->scope = talloc_strdup(ndr->current_mem_ctx, (const char *)&scope[1]);
297 NT_STATUS_HAVE_NO_MEMORY(r->scope);
302 cname = discard_const_p(char, s);
304 /* the first component is limited to 16 bytes in the DOS charset,
305 which is 32 in the 'compressed' form */
306 if (strlen(cname) > 32) {
307 return NT_STATUS_BAD_NETWORK_NAME;
310 /* decompress the first component */
311 status = decompress_name(cname, &r->type);
312 NT_STATUS_NOT_OK_RETURN(status);
314 r->name = talloc_strdup(ndr->current_mem_ctx, cname);
315 NT_STATUS_HAVE_NO_MEMORY(r->name);
323 push a nbt name to the wire
325 _PUBLIC_ NTSTATUS ndr_push_nbt_name(struct ndr_push *ndr, int ndr_flags, const struct nbt_name *r)
327 uint8_t *cname, *fullname;
330 if (!(ndr_flags & NDR_SCALARS)) {
334 if (strlen(r->name) > 15) {
335 return ndr_push_error(ndr, NDR_ERR_STRING,
336 "nbt_name longer as 15 chars: %s",
340 cname = compress_name(ndr, (const uint8_t *)r->name, r->type);
341 NT_STATUS_HAVE_NO_MEMORY(cname);
344 fullname = (uint8_t *)talloc_asprintf(ndr, "%s.%s", cname, r->scope);
345 NT_STATUS_HAVE_NO_MEMORY(fullname);
351 status = ndr_push_nbt_string(ndr, ndr_flags, (const char *)fullname);
358 copy a nbt name structure
360 _PUBLIC_ NTSTATUS nbt_name_dup(TALLOC_CTX *mem_ctx, struct nbt_name *name, struct nbt_name *newname)
363 newname->name = talloc_strdup(mem_ctx, newname->name);
364 NT_STATUS_HAVE_NO_MEMORY(newname->name);
365 newname->scope = talloc_strdup(mem_ctx, newname->scope);
367 NT_STATUS_HAVE_NO_MEMORY(newname->scope);
373 push a nbt name into a blob
375 _PUBLIC_ NTSTATUS nbt_name_to_blob(TALLOC_CTX *mem_ctx, DATA_BLOB *blob, struct nbt_name *name)
377 return ndr_push_struct_blob(blob, mem_ctx, name,
378 (ndr_push_flags_fn_t)ndr_push_nbt_name);
383 pull a nbt name from a blob
385 _PUBLIC_ NTSTATUS nbt_name_from_blob(TALLOC_CTX *mem_ctx, const DATA_BLOB *blob, struct nbt_name *name)
387 return ndr_pull_struct_blob(blob, mem_ctx, name,
388 (ndr_pull_flags_fn_t)ndr_pull_nbt_name);
393 choose a name to use when calling a server in a NBT session request.
394 we use heuristics to see if the name we have been given is a IP
395 address, or a too-long name. If it is then use *SMBSERVER, or a
398 _PUBLIC_ void nbt_choose_called_name(TALLOC_CTX *mem_ctx,
399 struct nbt_name *n, const char *name, int type)
404 if (is_ipaddress(name)) {
405 n->name = "*SMBSERVER";
408 if (strlen(name) > 15) {
409 const char *p = strchr(name, '.');
412 n->name = "*SMBSERVER";
415 s = talloc_strndup(mem_ctx, name, PTR_DIFF(p, name));
416 n->name = strupper_talloc(mem_ctx, s);
420 n->name = strupper_talloc(mem_ctx, name);
425 escape a string into a form containing only a small set of characters,
426 the rest is hex encoded. This is similar to URL encoding
428 static const char *nbt_hex_encode(TALLOC_CTX *mem_ctx, const char *s)
432 const char *valid_chars = "_-.$@ ";
433 #define NBT_CHAR_ALLOW(c) (isalnum((unsigned char)c) || strchr(valid_chars, c))
435 for (len=i=0;s[i];i++,len++) {
436 if (!NBT_CHAR_ALLOW(s[i])) {
441 ret = talloc_array(mem_ctx, char, len+1);
442 if (ret == NULL) return NULL;
444 for (len=i=0;s[i];i++) {
445 if (NBT_CHAR_ALLOW(s[i])) {
448 snprintf(&ret[len], 4, "%%%02x", (unsigned char)s[i]);
459 form a string for a NBT name
461 _PUBLIC_ char *nbt_name_string(TALLOC_CTX *mem_ctx, const struct nbt_name *name)
463 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
466 ret = talloc_asprintf(mem_ctx, "%s<%02x>-%s",
467 nbt_hex_encode(tmp_ctx, name->name),
469 nbt_hex_encode(tmp_ctx, name->scope));
471 ret = talloc_asprintf(mem_ctx, "%s<%02x>",
472 nbt_hex_encode(tmp_ctx, name->name),
475 talloc_free(tmp_ctx);
480 pull a nbt name, WINS Replication uses another on wire format for nbt name
482 _PUBLIC_ NTSTATUS ndr_pull_wrepl_nbt_name(struct ndr_pull *ndr, int ndr_flags, struct nbt_name **_r)
486 uint32_t namebuf_len;
488 if (!(ndr_flags & NDR_SCALARS)) {
492 NDR_CHECK(ndr_pull_align(ndr, 4));
493 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &namebuf_len));
494 if (namebuf_len < 1 || namebuf_len > 255) {
495 return ndr_pull_error(ndr, NDR_ERR_ALLOC, "value out of range");
497 NDR_PULL_ALLOC_N(ndr, namebuf, namebuf_len);
498 NDR_CHECK(ndr_pull_array_uint8(ndr, NDR_SCALARS, namebuf, namebuf_len));
500 NDR_PULL_ALLOC(ndr, r);
502 /* oh wow, what a nasty bug in windows ... */
503 if (namebuf[0] == 0x1b && namebuf_len >= 16) {
504 namebuf[0] = namebuf[15];
508 if (namebuf_len < 17) {
511 r->name = talloc_strndup(r, (char *)namebuf, namebuf_len);
512 if (!r->name) return ndr_pull_error(ndr, NDR_ERR_ALLOC, "out of memory");
516 talloc_free(namebuf);
521 r->type = namebuf[15];
524 trim_string((char *)namebuf, NULL, " ");
525 r->name = talloc_strdup(r, (char *)namebuf);
526 if (!r->name) return ndr_pull_error(ndr, NDR_ERR_ALLOC, "out of memory");
528 if (namebuf_len > 18) {
529 r->scope = talloc_strndup(r, (char *)(namebuf+17), namebuf_len-17);
530 if (!r->scope) return ndr_pull_error(ndr, NDR_ERR_ALLOC, "out of memory");
535 talloc_free(namebuf);
541 push a nbt name, WINS Replication uses another on wire format for nbt name
543 _PUBLIC_ NTSTATUS ndr_push_wrepl_nbt_name(struct ndr_push *ndr, int ndr_flags, const struct nbt_name *r)
546 uint32_t namebuf_len;
548 uint32_t scope_len = 0;
550 if (r == NULL) return NT_STATUS_INVALID_PARAMETER_MIX;
552 if (!(ndr_flags & NDR_SCALARS)) {
556 name_len = strlen(r->name);
558 return NT_STATUS_INVALID_PARAMETER_MIX;
562 scope_len = strlen(r->scope);
564 if (scope_len > 238) {
565 return NT_STATUS_INVALID_PARAMETER_MIX;
568 namebuf = (uint8_t *)talloc_asprintf(ndr, "%-15s%c%s",
570 (r->scope?r->scope:""));
571 if (!namebuf) return ndr_push_error(ndr, NDR_ERR_ALLOC, "out of memory");
573 namebuf_len = strlen((char *)namebuf) + 1;
576 * we need to set the type here, and use a place-holder in the talloc_asprintf()
577 * as the type can be 0x00, and then the namebuf_len = strlen(namebuf); would give wrong results
579 namebuf[15] = r->type;
581 /* oh wow, what a nasty bug in windows ... */
582 if (r->type == 0x1b) {
583 namebuf[15] = namebuf[0];
587 NDR_CHECK(ndr_push_align(ndr, 4));
588 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, namebuf_len));
589 NDR_CHECK(ndr_push_array_uint8(ndr, NDR_SCALARS, namebuf, namebuf_len));
591 talloc_free(namebuf);
595 _PUBLIC_ void ndr_print_wrepl_nbt_name(struct ndr_print *ndr, const char *name, const struct nbt_name *r)
597 char *s = nbt_name_string(ndr, r);
598 ndr_print_string(ndr, name, s);