libsmbconf: Document smbconf_delete_share().
[Samba.git] / libcli / nbt / nbtname.c
blobfec8e8e9b9af3adc206ea3928904e8502afa46ba
1 /*
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
26 #include "includes.h"
27 #include "librpc/gen_ndr/ndr_nbt.h"
28 #include "librpc/gen_ndr/ndr_misc.h"
29 #include "system/locale.h"
30 #include "lib/util/util_net.h"
32 /* don't allow an unlimited number of name components */
33 #define MAX_COMPONENTS 10
35 /**
36 print a nbt string
38 _PUBLIC_ void ndr_print_nbt_string(struct ndr_print *ndr, const char *name, const char *s)
40 ndr_print_string(ndr, name, s);
44 pull one component of a nbt_string
46 static enum ndr_err_code ndr_pull_component(struct ndr_pull *ndr,
47 uint8_t **component,
48 uint32_t *offset,
49 uint32_t *max_offset)
51 uint8_t len;
52 unsigned int loops = 0;
53 while (loops < 5) {
54 if (*offset >= ndr->data_size) {
55 return ndr_pull_error(ndr, NDR_ERR_STRING,
56 "BAD NBT NAME component");
58 len = ndr->data[*offset];
59 if (len == 0) {
60 *offset += 1;
61 *max_offset = MAX(*max_offset, *offset);
62 *component = NULL;
63 return NDR_ERR_SUCCESS;
65 if ((len & 0xC0) == 0xC0) {
66 /* its a label pointer */
67 if (1 + *offset >= ndr->data_size) {
68 return ndr_pull_error(ndr, NDR_ERR_STRING,
69 "BAD NBT NAME component");
71 *max_offset = MAX(*max_offset, *offset + 2);
72 *offset = ((len&0x3F)<<8) | ndr->data[1 + *offset];
73 *max_offset = MAX(*max_offset, *offset);
74 loops++;
75 continue;
77 if ((len & 0xC0) != 0) {
78 /* its a reserved length field */
79 return ndr_pull_error(ndr, NDR_ERR_STRING,
80 "BAD NBT NAME component");
82 if (*offset + len + 2 > ndr->data_size) {
83 return ndr_pull_error(ndr, NDR_ERR_STRING,
84 "BAD NBT NAME component");
86 *component = (uint8_t*)talloc_strndup(
87 ndr->current_mem_ctx,
88 (const char *)&ndr->data[1 + *offset], len);
89 NDR_ERR_HAVE_NO_MEMORY(*component);
90 *offset += len + 1;
91 *max_offset = MAX(*max_offset, *offset);
92 return NDR_ERR_SUCCESS;
95 /* too many pointers */
96 return ndr_pull_error(ndr, NDR_ERR_STRING, "BAD NBT NAME component");
99 /**
100 pull a nbt_string from the wire
102 _PUBLIC_ enum ndr_err_code ndr_pull_nbt_string(struct ndr_pull *ndr, int ndr_flags, const char **s)
104 uint32_t offset = ndr->offset;
105 uint32_t max_offset = offset;
106 unsigned num_components;
107 char *name;
109 if (!(ndr_flags & NDR_SCALARS)) {
110 return NDR_ERR_SUCCESS;
113 name = NULL;
115 /* break up name into a list of components */
116 for (num_components=0;num_components<MAX_COMPONENTS;num_components++) {
117 uint8_t *component = NULL;
118 NDR_CHECK(ndr_pull_component(ndr, &component, &offset, &max_offset));
119 if (component == NULL) break;
120 if (name) {
121 name = talloc_asprintf_append_buffer(name, ".%s", component);
122 NDR_ERR_HAVE_NO_MEMORY(name);
123 } else {
124 name = (char *)component;
127 if (num_components == MAX_COMPONENTS) {
128 return ndr_pull_error(ndr, NDR_ERR_STRING,
129 "BAD NBT NAME too many components");
131 if (num_components == 0) {
132 name = talloc_strdup(ndr->current_mem_ctx, "");
133 NDR_ERR_HAVE_NO_MEMORY(name);
136 (*s) = name;
137 ndr->offset = max_offset;
139 return NDR_ERR_SUCCESS;
143 push a nbt string to the wire
145 _PUBLIC_ enum ndr_err_code ndr_push_nbt_string(struct ndr_push *ndr, int ndr_flags, const char *s)
147 if (!(ndr_flags & NDR_SCALARS)) {
148 return NDR_ERR_SUCCESS;
151 while (s && *s) {
152 enum ndr_err_code ndr_err;
153 char *compname;
154 size_t complen;
155 uint32_t offset;
157 /* see if we have pushed the remaing string allready,
158 * if so we use a label pointer to this string
160 ndr_err = ndr_token_retrieve_cmp_fn(&ndr->nbt_string_list, s, &offset, (comparison_fn_t)strcmp, false);
161 if (NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
162 uint8_t b[2];
164 if (offset > 0x3FFF) {
165 return ndr_push_error(ndr, NDR_ERR_STRING,
166 "offset for nbt string label pointer %u[%08X] > 0x00003FFF",
167 offset, offset);
170 b[0] = 0xC0 | (offset>>8);
171 b[1] = (offset & 0xFF);
173 return ndr_push_bytes(ndr, b, 2);
176 complen = strcspn(s, ".");
178 /* we need to make sure the length fits into 6 bytes */
179 if (complen > 0x3F) {
180 return ndr_push_error(ndr, NDR_ERR_STRING,
181 "component length %u[%08X] > 0x0000003F",
182 (unsigned)complen, (unsigned)complen);
185 compname = talloc_asprintf(ndr, "%c%*.*s",
186 (unsigned char)complen,
187 (unsigned char)complen,
188 (unsigned char)complen, s);
189 NDR_ERR_HAVE_NO_MEMORY(compname);
191 /* remember the current componemt + the rest of the string
192 * so it can be reused later
194 NDR_CHECK(ndr_token_store(ndr, &ndr->nbt_string_list, s, ndr->offset));
196 /* push just this component into the blob */
197 NDR_CHECK(ndr_push_bytes(ndr, (const uint8_t *)compname, complen+1));
198 talloc_free(compname);
200 s += complen;
201 if (*s == '.') s++;
204 /* if we reach the end of the string and have pushed the last component
205 * without using a label pointer, we need to terminate the string
207 return ndr_push_bytes(ndr, (const uint8_t *)"", 1);
212 decompress a 'compressed' name component
214 static bool decompress_name(char *name, enum nbt_name_type *type)
216 int i;
217 for (i=0;name[2*i];i++) {
218 uint8_t c1 = name[2*i];
219 uint8_t c2 = name[1+(2*i)];
220 if (c1 < 'A' || c1 > 'P' ||
221 c2 < 'A' || c2 > 'P') {
222 return false;
224 name[i] = ((c1-'A')<<4) | (c2-'A');
226 name[i] = 0;
227 if (i == 16) {
228 *type = (enum nbt_name_type)(name[15]);
229 name[15] = 0;
230 i--;
231 } else {
232 *type = NBT_NAME_CLIENT;
235 /* trim trailing spaces */
236 for (;i>0 && name[i-1]==' ';i--) {
237 name[i-1] = 0;
240 return true;
245 compress a name component
247 static uint8_t *compress_name(TALLOC_CTX *mem_ctx,
248 const uint8_t *name, enum nbt_name_type type)
250 uint8_t *cname;
251 int i;
252 uint8_t pad_char;
254 if (strlen((const char *)name) > 15) {
255 return NULL;
258 cname = talloc_array(mem_ctx, uint8_t, 33);
259 if (cname == NULL) return NULL;
261 for (i=0;name[i];i++) {
262 cname[2*i] = 'A' + (name[i]>>4);
263 cname[1+2*i] = 'A' + (name[i]&0xF);
265 if (strcmp((const char *)name, "*") == 0) {
266 pad_char = 0;
267 } else {
268 pad_char = ' ';
270 for (;i<15;i++) {
271 cname[2*i] = 'A' + (pad_char>>4);
272 cname[1+2*i] = 'A' + (pad_char&0xF);
275 pad_char = type;
276 cname[2*i] = 'A' + (pad_char>>4);
277 cname[1+2*i] = 'A' + (pad_char&0xF);
279 cname[32] = 0;
280 return cname;
285 pull a nbt name from the wire
287 _PUBLIC_ enum ndr_err_code ndr_pull_nbt_name(struct ndr_pull *ndr, int ndr_flags, struct nbt_name *r)
289 uint8_t *scope;
290 char *cname;
291 const char *s;
292 bool ok;
294 if (!(ndr_flags & NDR_SCALARS)) {
295 return NDR_ERR_SUCCESS;
298 NDR_CHECK(ndr_pull_nbt_string(ndr, ndr_flags, &s));
300 scope = (uint8_t *)strchr(s, '.');
301 if (scope) {
302 *scope = 0;
303 r->scope = talloc_strdup(ndr->current_mem_ctx, (const char *)&scope[1]);
304 NDR_ERR_HAVE_NO_MEMORY(r->scope);
305 } else {
306 r->scope = NULL;
309 cname = discard_const_p(char, s);
311 /* the first component is limited to 16 bytes in the DOS charset,
312 which is 32 in the 'compressed' form */
313 if (strlen(cname) > 32) {
314 return ndr_pull_error(ndr, NDR_ERR_STRING,
315 "NBT NAME cname > 32");
318 /* decompress the first component */
319 ok = decompress_name(cname, &r->type);
320 if (!ok) {
321 return ndr_pull_error(ndr, NDR_ERR_STRING,
322 "NBT NAME failed to decompress");
325 r->name = talloc_strdup(ndr->current_mem_ctx, cname);
326 NDR_ERR_HAVE_NO_MEMORY(r->name);
328 talloc_free(cname);
330 return NDR_ERR_SUCCESS;
334 push a nbt name to the wire
336 _PUBLIC_ enum ndr_err_code ndr_push_nbt_name(struct ndr_push *ndr, int ndr_flags, const struct nbt_name *r)
338 uint8_t *cname, *fullname;
339 enum ndr_err_code ndr_err;
341 if (!(ndr_flags & NDR_SCALARS)) {
342 return NDR_ERR_SUCCESS;
345 if (strlen(r->name) > 15) {
346 return ndr_push_error(ndr, NDR_ERR_STRING,
347 "nbt_name longer as 15 chars: %s",
348 r->name);
351 cname = compress_name(ndr, (const uint8_t *)r->name, r->type);
352 NDR_ERR_HAVE_NO_MEMORY(cname);
354 if (r->scope) {
355 fullname = (uint8_t *)talloc_asprintf(ndr, "%s.%s", cname, r->scope);
356 NDR_ERR_HAVE_NO_MEMORY(fullname);
357 talloc_free(cname);
358 } else {
359 fullname = cname;
362 ndr_err = ndr_push_nbt_string(ndr, ndr_flags, (const char *)fullname);
364 return ndr_err;
369 copy a nbt name structure
371 _PUBLIC_ NTSTATUS nbt_name_dup(TALLOC_CTX *mem_ctx, struct nbt_name *name, struct nbt_name *newname)
373 *newname = *name;
374 newname->name = talloc_strdup(mem_ctx, newname->name);
375 NT_STATUS_HAVE_NO_MEMORY(newname->name);
376 newname->scope = talloc_strdup(mem_ctx, newname->scope);
377 if (name->scope) {
378 NT_STATUS_HAVE_NO_MEMORY(newname->scope);
380 return NT_STATUS_OK;
384 push a nbt name into a blob
386 _PUBLIC_ NTSTATUS nbt_name_to_blob(TALLOC_CTX *mem_ctx, DATA_BLOB *blob, struct nbt_name *name)
388 enum ndr_err_code ndr_err;
390 ndr_err = ndr_push_struct_blob(blob, mem_ctx, name, (ndr_push_flags_fn_t)ndr_push_nbt_name);
391 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
392 return ndr_map_error2ntstatus(ndr_err);
395 return NT_STATUS_OK;
399 pull a nbt name from a blob
401 _PUBLIC_ NTSTATUS nbt_name_from_blob(TALLOC_CTX *mem_ctx, const DATA_BLOB *blob, struct nbt_name *name)
403 enum ndr_err_code ndr_err;
405 ndr_err = ndr_pull_struct_blob(blob, mem_ctx, name,
406 (ndr_pull_flags_fn_t)ndr_pull_nbt_name);
407 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
408 return ndr_map_error2ntstatus(ndr_err);
411 return NT_STATUS_OK;
416 choose a name to use when calling a server in a NBT session request.
417 we use heuristics to see if the name we have been given is a IP
418 address, or a too-long name. If it is then use *SMBSERVER, or a
419 truncated name
421 _PUBLIC_ void nbt_choose_called_name(TALLOC_CTX *mem_ctx,
422 struct nbt_name *n, const char *name, int type)
424 n->scope = NULL;
425 n->type = type;
427 if ((name == NULL) || is_ipaddress(name)) {
428 n->name = "*SMBSERVER";
429 return;
431 if (strlen(name) > 15) {
432 const char *p = strchr(name, '.');
433 char *s;
434 if (p - name > 15) {
435 n->name = "*SMBSERVER";
436 return;
438 s = talloc_strndup(mem_ctx, name, PTR_DIFF(p, name));
439 n->name = talloc_strdup_upper(mem_ctx, s);
440 return;
443 n->name = talloc_strdup_upper(mem_ctx, name);
448 escape a string into a form containing only a small set of characters,
449 the rest is hex encoded. This is similar to URL encoding
451 static const char *nbt_hex_encode(TALLOC_CTX *mem_ctx, const char *s)
453 int i, len;
454 char *ret;
455 const char *valid_chars = "_-.$@ ";
456 #define NBT_CHAR_ALLOW(c) (isalnum((unsigned char)c) || strchr(valid_chars, c))
458 for (len=i=0;s[i];i++,len++) {
459 if (!NBT_CHAR_ALLOW(s[i])) {
460 len += 2;
464 ret = talloc_array(mem_ctx, char, len+1);
465 if (ret == NULL) return NULL;
467 for (len=i=0;s[i];i++) {
468 if (NBT_CHAR_ALLOW(s[i])) {
469 ret[len++] = s[i];
470 } else {
471 snprintf(&ret[len], 4, "%%%02x", (unsigned char)s[i]);
472 len += 3;
475 ret[len] = 0;
477 return ret;
482 form a string for a NBT name
484 _PUBLIC_ char *nbt_name_string(TALLOC_CTX *mem_ctx, const struct nbt_name *name)
486 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
487 char *ret;
488 if (name->scope) {
489 ret = talloc_asprintf(mem_ctx, "%s<%02x>-%s",
490 nbt_hex_encode(tmp_ctx, name->name),
491 name->type,
492 nbt_hex_encode(tmp_ctx, name->scope));
493 } else {
494 ret = talloc_asprintf(mem_ctx, "%s<%02x>",
495 nbt_hex_encode(tmp_ctx, name->name),
496 name->type);
498 talloc_free(tmp_ctx);
499 return ret;
503 pull a nbt name, WINS Replication uses another on wire format for nbt name
505 _PUBLIC_ enum ndr_err_code ndr_pull_wrepl_nbt_name(struct ndr_pull *ndr, int ndr_flags, const struct nbt_name **_r)
507 struct nbt_name *r;
508 uint8_t *namebuf;
509 uint32_t namebuf_len;
511 if (!(ndr_flags & NDR_SCALARS)) {
512 return NDR_ERR_SUCCESS;
515 NDR_CHECK(ndr_pull_align(ndr, 4));
516 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &namebuf_len));
517 if (namebuf_len < 1 || namebuf_len > 255) {
518 return ndr_pull_error(ndr, NDR_ERR_ALLOC, "value out of range");
520 NDR_PULL_ALLOC_N(ndr, namebuf, namebuf_len);
521 NDR_CHECK(ndr_pull_array_uint8(ndr, NDR_SCALARS, namebuf, namebuf_len));
523 if ((namebuf_len % 4) == 0) {
525 * [MS-WINSRA] — v20091104 was wrong
526 * regarding section "2.2.10.1 Name Record"
528 * If the name buffer is already 4 byte aligned
529 * Windows (at least 2003 SP1 and 2008) add 4 extra
530 * bytes. This can happen when the name has a scope.
532 uint32_t pad;
533 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &pad));
536 NDR_PULL_ALLOC(ndr, r);
538 /* oh wow, what a nasty bug in windows ... */
539 if (namebuf[0] == 0x1b && namebuf_len >= 16) {
540 namebuf[0] = namebuf[15];
541 namebuf[15] = 0x1b;
544 if (namebuf_len < 17) {
545 r->type = 0x00;
547 r->name = talloc_strndup(r, (char *)namebuf, namebuf_len);
548 if (!r->name) return ndr_pull_error(ndr, NDR_ERR_ALLOC, "out of memory");
550 r->scope= NULL;
552 talloc_free(namebuf);
553 *_r = r;
554 return NDR_ERR_SUCCESS;
557 r->type = namebuf[15];
559 namebuf[15] = '\0';
560 trim_string((char *)namebuf, NULL, " ");
561 r->name = talloc_strdup(r, (char *)namebuf);
562 if (!r->name) return ndr_pull_error(ndr, NDR_ERR_ALLOC, "out of memory");
564 if (namebuf_len > 17) {
565 r->scope = talloc_strndup(r, (char *)(namebuf+16), namebuf_len-17);
566 if (!r->scope) return ndr_pull_error(ndr, NDR_ERR_ALLOC, "out of memory");
567 } else {
568 r->scope = NULL;
571 talloc_free(namebuf);
572 *_r = r;
573 return NDR_ERR_SUCCESS;
577 push a nbt name, WINS Replication uses another on wire format for nbt name
579 _PUBLIC_ enum ndr_err_code ndr_push_wrepl_nbt_name(struct ndr_push *ndr, int ndr_flags, const struct nbt_name *r)
581 uint8_t *namebuf;
582 uint32_t namebuf_len;
583 uint32_t _name_len;
584 uint32_t scope_len = 0;
586 if (r == NULL) {
587 return ndr_push_error(ndr, NDR_ERR_INVALID_POINTER,
588 "wrepl_nbt_name NULL pointer");
591 if (!(ndr_flags & NDR_SCALARS)) {
592 return NDR_ERR_SUCCESS;
595 _name_len = strlen(r->name);
596 if (_name_len > 15) {
597 return ndr_push_error(ndr, NDR_ERR_STRING,
598 "wrepl_nbt_name longer as 15 chars: %s",
599 r->name);
602 if (r->scope) {
603 scope_len = strlen(r->scope);
605 if (scope_len > 238) {
606 return ndr_push_error(ndr, NDR_ERR_STRING,
607 "wrepl_nbt_name scope longer as 238 chars: %s",
608 r->scope);
611 namebuf = (uint8_t *)talloc_asprintf(ndr, "%-15s%c%s",
612 r->name, 'X',
613 (r->scope?r->scope:""));
614 if (!namebuf) return ndr_push_error(ndr, NDR_ERR_ALLOC, "out of memory");
616 namebuf_len = strlen((char *)namebuf) + 1;
619 * we need to set the type here, and use a place-holder in the talloc_asprintf()
620 * as the type can be 0x00, and then the namebuf_len = strlen(namebuf); would give wrong results
622 namebuf[15] = r->type;
624 /* oh wow, what a nasty bug in windows ... */
625 if (r->type == 0x1b) {
626 namebuf[15] = namebuf[0];
627 namebuf[0] = 0x1b;
630 NDR_CHECK(ndr_push_align(ndr, 4));
631 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, namebuf_len));
632 NDR_CHECK(ndr_push_array_uint8(ndr, NDR_SCALARS, namebuf, namebuf_len));
634 if ((namebuf_len % 4) == 0) {
636 * [MS-WINSRA] — v20091104 was wrong
637 * regarding section "2.2.10.1 Name Record"
639 * If the name buffer is already 4 byte aligned
640 * Windows (at least 2003 SP1 and 2008) add 4 extra
641 * bytes. This can happen when the name has a scope.
643 NDR_CHECK(ndr_push_zero(ndr, 4));
646 talloc_free(namebuf);
647 return NDR_ERR_SUCCESS;
650 _PUBLIC_ void ndr_print_wrepl_nbt_name(struct ndr_print *ndr, const char *name, const struct nbt_name *r)
652 char *s = nbt_name_string(ndr, r);
653 ndr_print_string(ndr, name, s);
654 talloc_free(s);
657 _PUBLIC_ enum ndr_err_code ndr_push_nbt_res_rec(struct ndr_push *ndr, int ndr_flags, const struct nbt_res_rec *r)
660 uint32_t _flags_save_STRUCT = ndr->flags;
661 ndr_set_flags(&ndr->flags, LIBNDR_PRINT_ARRAY_HEX);
662 if (ndr_flags & NDR_SCALARS) {
663 NDR_CHECK(ndr_push_align(ndr, 4));
664 NDR_CHECK(ndr_push_nbt_name(ndr, NDR_SCALARS, &r->name));
665 NDR_CHECK(ndr_push_nbt_qtype(ndr, NDR_SCALARS, r->rr_type));
666 NDR_CHECK(ndr_push_nbt_qclass(ndr, NDR_SCALARS, r->rr_class));
667 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, r->ttl));
668 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)));
669 NDR_CHECK(ndr_push_nbt_rdata(ndr, NDR_SCALARS, &r->rdata));
671 if (ndr_flags & NDR_BUFFERS) {
673 ndr->flags = _flags_save_STRUCT;
675 return NDR_ERR_SUCCESS;