Fix bug #8157 - std_pcap_cache_reload() fails to parse a cups printcap file correctly.
[Samba.git] / libcli / nbt / nbtname.c
blob5346ee3e8e18542479890745b299791a5a883fbf
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"
31 /* don't allow an unlimited number of name components */
32 #define MAX_COMPONENTS 10
34 /**
35 print a nbt string
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 enum ndr_err_code ndr_pull_component(struct ndr_pull *ndr,
46 uint8_t **component,
47 uint32_t *offset,
48 uint32_t *max_offset)
50 uint8_t len;
51 uint_t loops = 0;
52 while (loops < 5) {
53 if (*offset >= ndr->data_size) {
54 return ndr_pull_error(ndr, NDR_ERR_STRING,
55 "BAD NBT NAME component");
57 len = ndr->data[*offset];
58 if (len == 0) {
59 *offset += 1;
60 *max_offset = MAX(*max_offset, *offset);
61 *component = NULL;
62 return NDR_ERR_SUCCESS;
64 if ((len & 0xC0) == 0xC0) {
65 /* its a label pointer */
66 if (1 + *offset >= ndr->data_size) {
67 return ndr_pull_error(ndr, NDR_ERR_STRING,
68 "BAD NBT NAME component");
70 *max_offset = MAX(*max_offset, *offset + 2);
71 *offset = ((len&0x3F)<<8) | ndr->data[1 + *offset];
72 *max_offset = MAX(*max_offset, *offset);
73 loops++;
74 continue;
76 if ((len & 0xC0) != 0) {
77 /* its a reserved length field */
78 return ndr_pull_error(ndr, NDR_ERR_STRING,
79 "BAD NBT NAME component");
81 if (*offset + len + 2 > ndr->data_size) {
82 return ndr_pull_error(ndr, NDR_ERR_STRING,
83 "BAD NBT NAME component");
85 *component = (uint8_t*)talloc_strndup(
86 ndr->current_mem_ctx,
87 (const char *)&ndr->data[1 + *offset], len);
88 NDR_ERR_HAVE_NO_MEMORY(*component);
89 *offset += len + 1;
90 *max_offset = MAX(*max_offset, *offset);
91 return NDR_ERR_SUCCESS;
94 /* too many pointers */
95 return ndr_pull_error(ndr, NDR_ERR_STRING, "BAD NBT NAME component");
98 /**
99 pull a nbt_string from the wire
101 _PUBLIC_ enum ndr_err_code ndr_pull_nbt_string(struct ndr_pull *ndr, int ndr_flags, const char **s)
103 uint32_t offset = ndr->offset;
104 uint32_t max_offset = offset;
105 unsigned num_components;
106 char *name;
108 if (!(ndr_flags & NDR_SCALARS)) {
109 return NDR_ERR_SUCCESS;
112 name = NULL;
114 /* break up name into a list of components */
115 for (num_components=0;num_components<MAX_COMPONENTS;num_components++) {
116 uint8_t *component = NULL;
117 NDR_CHECK(ndr_pull_component(ndr, &component, &offset, &max_offset));
118 if (component == NULL) break;
119 if (name) {
120 name = talloc_asprintf_append_buffer(name, ".%s", component);
121 NDR_ERR_HAVE_NO_MEMORY(name);
122 } else {
123 name = (char *)component;
126 if (num_components == MAX_COMPONENTS) {
127 return ndr_pull_error(ndr, NDR_ERR_STRING,
128 "BAD NBT NAME too many components");
130 if (num_components == 0) {
131 name = talloc_strdup(ndr->current_mem_ctx, "");
132 NDR_ERR_HAVE_NO_MEMORY(name);
135 (*s) = name;
136 ndr->offset = max_offset;
138 return NDR_ERR_SUCCESS;
142 push a nbt string to the wire
144 _PUBLIC_ enum ndr_err_code ndr_push_nbt_string(struct ndr_push *ndr, int ndr_flags, const char *s)
146 if (!(ndr_flags & NDR_SCALARS)) {
147 return NDR_ERR_SUCCESS;
150 while (s && *s) {
151 enum ndr_err_code ndr_err;
152 char *compname;
153 size_t complen;
154 uint32_t offset;
156 /* see if we have pushed the remaing string allready,
157 * if so we use a label pointer to this string
159 ndr_err = ndr_token_retrieve_cmp_fn(&ndr->nbt_string_list, s, &offset, (comparison_fn_t)strcmp, false);
160 if (NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
161 uint8_t b[2];
163 if (offset > 0x3FFF) {
164 return ndr_push_error(ndr, NDR_ERR_STRING,
165 "offset for nbt string label pointer %u[%08X] > 0x00003FFF",
166 offset, offset);
169 b[0] = 0xC0 | (offset>>8);
170 b[1] = (offset & 0xFF);
172 return ndr_push_bytes(ndr, b, 2);
175 complen = strcspn(s, ".");
177 /* we need to make sure the length fits into 6 bytes */
178 if (complen >= 0x3F) {
179 return ndr_push_error(ndr, NDR_ERR_STRING,
180 "component length %u[%08X] > 0x00003F",
181 (unsigned)complen, (unsigned)complen);
184 compname = talloc_asprintf(ndr, "%c%*.*s",
185 (unsigned char)complen,
186 (unsigned char)complen,
187 (unsigned char)complen, s);
188 NDR_ERR_HAVE_NO_MEMORY(compname);
190 /* remember the current componemt + the rest of the string
191 * so it can be reused later
193 NDR_CHECK(ndr_token_store(ndr, &ndr->nbt_string_list, s, ndr->offset));
195 /* push just this component into the blob */
196 NDR_CHECK(ndr_push_bytes(ndr, (const uint8_t *)compname, complen+1));
197 talloc_free(compname);
199 s += complen;
200 if (*s == '.') s++;
203 /* if we reach the end of the string and have pushed the last component
204 * without using a label pointer, we need to terminate the string
206 return ndr_push_bytes(ndr, (const uint8_t *)"", 1);
211 decompress a 'compressed' name component
213 static bool decompress_name(char *name, enum nbt_name_type *type)
215 int i;
216 for (i=0;name[2*i];i++) {
217 uint8_t c1 = name[2*i];
218 uint8_t c2 = name[1+(2*i)];
219 if (c1 < 'A' || c1 > 'P' ||
220 c2 < 'A' || c2 > 'P') {
221 return false;
223 name[i] = ((c1-'A')<<4) | (c2-'A');
225 name[i] = 0;
226 if (i == 16) {
227 *type = (enum nbt_name_type)(name[15]);
228 name[15] = 0;
229 i--;
230 } else {
231 *type = NBT_NAME_CLIENT;
234 /* trim trailing spaces */
235 for (;i>0 && name[i-1]==' ';i--) {
236 name[i-1] = 0;
239 return true;
244 compress a name component
246 static uint8_t *compress_name(TALLOC_CTX *mem_ctx,
247 const uint8_t *name, enum nbt_name_type type)
249 uint8_t *cname;
250 int i;
251 uint8_t pad_char;
253 if (strlen((const char *)name) > 15) {
254 return NULL;
257 cname = talloc_array(mem_ctx, uint8_t, 33);
258 if (cname == NULL) return NULL;
260 for (i=0;name[i];i++) {
261 cname[2*i] = 'A' + (name[i]>>4);
262 cname[1+2*i] = 'A' + (name[i]&0xF);
264 if (strcmp((const char *)name, "*") == 0) {
265 pad_char = 0;
266 } else {
267 pad_char = ' ';
269 for (;i<15;i++) {
270 cname[2*i] = 'A' + (pad_char>>4);
271 cname[1+2*i] = 'A' + (pad_char&0xF);
274 pad_char = type;
275 cname[2*i] = 'A' + (pad_char>>4);
276 cname[1+2*i] = 'A' + (pad_char&0xF);
278 cname[32] = 0;
279 return cname;
284 pull a nbt name from the wire
286 _PUBLIC_ enum ndr_err_code ndr_pull_nbt_name(struct ndr_pull *ndr, int ndr_flags, struct nbt_name *r)
288 uint8_t *scope;
289 char *cname;
290 const char *s;
291 bool ok;
293 if (!(ndr_flags & NDR_SCALARS)) {
294 return NDR_ERR_SUCCESS;
297 NDR_CHECK(ndr_pull_nbt_string(ndr, ndr_flags, &s));
299 scope = (uint8_t *)strchr(s, '.');
300 if (scope) {
301 *scope = 0;
302 r->scope = talloc_strdup(ndr->current_mem_ctx, (const char *)&scope[1]);
303 NDR_ERR_HAVE_NO_MEMORY(r->scope);
304 } else {
305 r->scope = NULL;
308 cname = discard_const_p(char, s);
310 /* the first component is limited to 16 bytes in the DOS charset,
311 which is 32 in the 'compressed' form */
312 if (strlen(cname) > 32) {
313 return ndr_pull_error(ndr, NDR_ERR_STRING,
314 "NBT NAME cname > 32");
317 /* decompress the first component */
318 ok = decompress_name(cname, &r->type);
319 if (!ok) {
320 return ndr_pull_error(ndr, NDR_ERR_STRING,
321 "NBT NAME failed to decompress");
324 r->name = talloc_strdup(ndr->current_mem_ctx, cname);
325 NDR_ERR_HAVE_NO_MEMORY(r->name);
327 talloc_free(cname);
329 return NDR_ERR_SUCCESS;
333 push a nbt name to the wire
335 _PUBLIC_ enum ndr_err_code ndr_push_nbt_name(struct ndr_push *ndr, int ndr_flags, const struct nbt_name *r)
337 uint8_t *cname, *fullname;
338 enum ndr_err_code ndr_err;
340 if (!(ndr_flags & NDR_SCALARS)) {
341 return NDR_ERR_SUCCESS;
344 if (strlen(r->name) > 15) {
345 return ndr_push_error(ndr, NDR_ERR_STRING,
346 "nbt_name longer as 15 chars: %s",
347 r->name);
350 cname = compress_name(ndr, (const uint8_t *)r->name, r->type);
351 NDR_ERR_HAVE_NO_MEMORY(cname);
353 if (r->scope) {
354 fullname = (uint8_t *)talloc_asprintf(ndr, "%s.%s", cname, r->scope);
355 NDR_ERR_HAVE_NO_MEMORY(fullname);
356 talloc_free(cname);
357 } else {
358 fullname = cname;
361 ndr_err = ndr_push_nbt_string(ndr, ndr_flags, (const char *)fullname);
363 return ndr_err;
368 copy a nbt name structure
370 _PUBLIC_ NTSTATUS nbt_name_dup(TALLOC_CTX *mem_ctx, struct nbt_name *name, struct nbt_name *newname)
372 *newname = *name;
373 newname->name = talloc_strdup(mem_ctx, newname->name);
374 NT_STATUS_HAVE_NO_MEMORY(newname->name);
375 newname->scope = talloc_strdup(mem_ctx, newname->scope);
376 if (name->scope) {
377 NT_STATUS_HAVE_NO_MEMORY(newname->scope);
379 return NT_STATUS_OK;
383 push a nbt name into a blob
385 _PUBLIC_ NTSTATUS nbt_name_to_blob(TALLOC_CTX *mem_ctx, struct smb_iconv_convenience *iconv_convenience, DATA_BLOB *blob, struct nbt_name *name)
387 enum ndr_err_code ndr_err;
389 ndr_err = ndr_push_struct_blob(blob, mem_ctx, iconv_convenience, name, (ndr_push_flags_fn_t)ndr_push_nbt_name);
390 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
391 return ndr_map_error2ntstatus(ndr_err);
394 return NT_STATUS_OK;
398 pull a nbt name from a blob
400 _PUBLIC_ NTSTATUS nbt_name_from_blob(TALLOC_CTX *mem_ctx, const DATA_BLOB *blob, struct nbt_name *name)
402 enum ndr_err_code ndr_err;
404 ndr_err = ndr_pull_struct_blob(blob, mem_ctx, NULL, name,
405 (ndr_pull_flags_fn_t)ndr_pull_nbt_name);
406 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
407 return ndr_map_error2ntstatus(ndr_err);
410 return NT_STATUS_OK;
415 choose a name to use when calling a server in a NBT session request.
416 we use heuristics to see if the name we have been given is a IP
417 address, or a too-long name. If it is then use *SMBSERVER, or a
418 truncated name
420 _PUBLIC_ void nbt_choose_called_name(TALLOC_CTX *mem_ctx,
421 struct nbt_name *n, const char *name, int type)
423 n->scope = NULL;
424 n->type = type;
426 if ((name == NULL) || is_ipaddress(name)) {
427 n->name = "*SMBSERVER";
428 return;
430 if (strlen(name) > 15) {
431 const char *p = strchr(name, '.');
432 char *s;
433 if (p - name > 15) {
434 n->name = "*SMBSERVER";
435 return;
437 s = talloc_strndup(mem_ctx, name, PTR_DIFF(p, name));
438 n->name = talloc_strdup_upper(mem_ctx, s);
439 return;
442 n->name = talloc_strdup_upper(mem_ctx, name);
447 escape a string into a form containing only a small set of characters,
448 the rest is hex encoded. This is similar to URL encoding
450 static const char *nbt_hex_encode(TALLOC_CTX *mem_ctx, const char *s)
452 int i, len;
453 char *ret;
454 const char *valid_chars = "_-.$@ ";
455 #define NBT_CHAR_ALLOW(c) (isalnum((unsigned char)c) || strchr(valid_chars, c))
457 for (len=i=0;s[i];i++,len++) {
458 if (!NBT_CHAR_ALLOW(s[i])) {
459 len += 2;
463 ret = talloc_array(mem_ctx, char, len+1);
464 if (ret == NULL) return NULL;
466 for (len=i=0;s[i];i++) {
467 if (NBT_CHAR_ALLOW(s[i])) {
468 ret[len++] = s[i];
469 } else {
470 snprintf(&ret[len], 4, "%%%02x", (unsigned char)s[i]);
471 len += 3;
474 ret[len] = 0;
476 return ret;
481 form a string for a NBT name
483 _PUBLIC_ char *nbt_name_string(TALLOC_CTX *mem_ctx, const struct nbt_name *name)
485 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
486 char *ret;
487 if (name->scope) {
488 ret = talloc_asprintf(mem_ctx, "%s<%02x>-%s",
489 nbt_hex_encode(tmp_ctx, name->name),
490 name->type,
491 nbt_hex_encode(tmp_ctx, name->scope));
492 } else {
493 ret = talloc_asprintf(mem_ctx, "%s<%02x>",
494 nbt_hex_encode(tmp_ctx, name->name),
495 name->type);
497 talloc_free(tmp_ctx);
498 return ret;
502 pull a nbt name, WINS Replication uses another on wire format for nbt name
504 _PUBLIC_ enum ndr_err_code ndr_pull_wrepl_nbt_name(struct ndr_pull *ndr, int ndr_flags, const struct nbt_name **_r)
506 struct nbt_name *r;
507 uint8_t *namebuf;
508 uint32_t namebuf_len;
510 if (!(ndr_flags & NDR_SCALARS)) {
511 return NDR_ERR_SUCCESS;
514 NDR_CHECK(ndr_pull_align(ndr, 4));
515 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &namebuf_len));
516 if (namebuf_len < 1 || namebuf_len > 255) {
517 return ndr_pull_error(ndr, NDR_ERR_ALLOC, "value out of range");
519 NDR_PULL_ALLOC_N(ndr, namebuf, namebuf_len);
520 NDR_CHECK(ndr_pull_array_uint8(ndr, NDR_SCALARS, namebuf, namebuf_len));
522 NDR_PULL_ALLOC(ndr, r);
524 /* oh wow, what a nasty bug in windows ... */
525 if (namebuf[0] == 0x1b && namebuf_len >= 16) {
526 namebuf[0] = namebuf[15];
527 namebuf[15] = 0x1b;
530 if (namebuf_len < 17) {
531 r->type = 0x00;
533 r->name = talloc_strndup(r, (char *)namebuf, namebuf_len);
534 if (!r->name) return ndr_pull_error(ndr, NDR_ERR_ALLOC, "out of memory");
536 r->scope= NULL;
538 talloc_free(namebuf);
539 *_r = r;
540 return NDR_ERR_SUCCESS;
543 r->type = namebuf[15];
545 namebuf[15] = '\0';
546 trim_string((char *)namebuf, NULL, " ");
547 r->name = talloc_strdup(r, (char *)namebuf);
548 if (!r->name) return ndr_pull_error(ndr, NDR_ERR_ALLOC, "out of memory");
550 if (namebuf_len > 18) {
551 r->scope = talloc_strndup(r, (char *)(namebuf+17), namebuf_len-17);
552 if (!r->scope) return ndr_pull_error(ndr, NDR_ERR_ALLOC, "out of memory");
553 } else {
554 r->scope = NULL;
557 talloc_free(namebuf);
558 *_r = r;
559 return NDR_ERR_SUCCESS;
563 push a nbt name, WINS Replication uses another on wire format for nbt name
565 _PUBLIC_ enum ndr_err_code ndr_push_wrepl_nbt_name(struct ndr_push *ndr, int ndr_flags, const struct nbt_name *r)
567 uint8_t *namebuf;
568 uint32_t namebuf_len;
569 uint32_t _name_len;
570 uint32_t scope_len = 0;
572 if (r == NULL) {
573 return ndr_push_error(ndr, NDR_ERR_INVALID_POINTER,
574 "wrepl_nbt_name NULL pointer");
577 if (!(ndr_flags & NDR_SCALARS)) {
578 return NDR_ERR_SUCCESS;
581 _name_len = strlen(r->name);
582 if (_name_len > 15) {
583 return ndr_push_error(ndr, NDR_ERR_STRING,
584 "wrepl_nbt_name longer as 15 chars: %s",
585 r->name);
588 if (r->scope) {
589 scope_len = strlen(r->scope);
591 if (scope_len > 238) {
592 return ndr_push_error(ndr, NDR_ERR_STRING,
593 "wrepl_nbt_name scope longer as 238 chars: %s",
594 r->scope);
597 namebuf = (uint8_t *)talloc_asprintf(ndr, "%-15s%c%s",
598 r->name, 'X',
599 (r->scope?r->scope:""));
600 if (!namebuf) return ndr_push_error(ndr, NDR_ERR_ALLOC, "out of memory");
602 namebuf_len = strlen((char *)namebuf) + 1;
605 * we need to set the type here, and use a place-holder in the talloc_asprintf()
606 * as the type can be 0x00, and then the namebuf_len = strlen(namebuf); would give wrong results
608 namebuf[15] = r->type;
610 /* oh wow, what a nasty bug in windows ... */
611 if (r->type == 0x1b) {
612 namebuf[15] = namebuf[0];
613 namebuf[0] = 0x1b;
616 NDR_CHECK(ndr_push_align(ndr, 4));
617 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, namebuf_len));
618 NDR_CHECK(ndr_push_array_uint8(ndr, NDR_SCALARS, namebuf, namebuf_len));
620 talloc_free(namebuf);
621 return NDR_ERR_SUCCESS;
624 _PUBLIC_ void ndr_print_wrepl_nbt_name(struct ndr_print *ndr, const char *name, const struct nbt_name *r)
626 char *s = nbt_name_string(ndr, r);
627 ndr_print_string(ndr, name, s);
628 talloc_free(s);
631 _PUBLIC_ enum ndr_err_code ndr_push_nbt_res_rec(struct ndr_push *ndr, int ndr_flags, const struct nbt_res_rec *r)
634 uint32_t _flags_save_STRUCT = ndr->flags;
635 ndr_set_flags(&ndr->flags, LIBNDR_PRINT_ARRAY_HEX);
636 if (ndr_flags & NDR_SCALARS) {
637 NDR_CHECK(ndr_push_align(ndr, 4));
638 NDR_CHECK(ndr_push_nbt_name(ndr, NDR_SCALARS, &r->name));
639 NDR_CHECK(ndr_push_nbt_qtype(ndr, NDR_SCALARS, r->rr_type));
640 NDR_CHECK(ndr_push_nbt_qclass(ndr, NDR_SCALARS, r->rr_class));
641 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, r->ttl));
642 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)));
643 NDR_CHECK(ndr_push_nbt_rdata(ndr, NDR_SCALARS, &r->rdata));
645 if (ndr_flags & NDR_BUFFERS) {
647 ndr->flags = _flags_save_STRUCT;
649 return NDR_ERR_SUCCESS;