source: branches/samba-3.3.x/source/libcli/nbt/nbtname.c

Last change on this file was 206, checked in by Herwig Bauernfeind, 16 years ago

Import Samba 3.3 branch at 3.0.0 level (psmedley's port)

File size: 15.9 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3
4 manipulate nbt name structures
5
6 Copyright (C) Andrew Tridgell 2005
7
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.
12
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.
17
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/>.
20*/
21
22/*
23 see rfc1002 for the detailed format of compressed names
24*/
25
26#include "includes.h"
27#include "librpc/gen_ndr/ndr_nbt.h"
28#include "librpc/gen_ndr/ndr_misc.h"
29
30/* don't allow an unlimited number of name components */
31#define MAX_COMPONENTS 10
32
33/**
34 print a nbt string
35*/
36_PUBLIC_ void ndr_print_nbt_string(struct ndr_print *ndr, const char *name, const char *s)
37{
38 ndr_print_string(ndr, name, s);
39}
40
41/*
42 pull one component of a nbt_string
43*/
44static enum ndr_err_code ndr_pull_component(struct ndr_pull *ndr,
45 uint8_t **component,
46 uint32_t *offset,
47 uint32_t *max_offset)
48{
49 uint8_t len;
50 uint_t loops = 0;
51 while (loops < 5) {
52 if (*offset >= ndr->data_size) {
53 return ndr_pull_error(ndr, NDR_ERR_STRING,
54 "BAD NBT NAME component");
55 }
56 len = ndr->data[*offset];
57 if (len == 0) {
58 *offset += 1;
59 *max_offset = MAX(*max_offset, *offset);
60 *component = NULL;
61 return NDR_ERR_SUCCESS;
62 }
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");
68 }
69 *max_offset = MAX(*max_offset, *offset + 2);
70 *offset = ((len&0x3F)<<8) | ndr->data[1 + *offset];
71 *max_offset = MAX(*max_offset, *offset);
72 loops++;
73 continue;
74 }
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");
79 }
80 if (*offset + len + 2 > ndr->data_size) {
81 return ndr_pull_error(ndr, NDR_ERR_STRING,
82 "BAD NBT NAME component");
83 }
84 *component = (uint8_t*)talloc_strndup(ndr, (const char *)&ndr->data[1 + *offset], len);
85 NDR_ERR_HAVE_NO_MEMORY(*component);
86 *offset += len + 1;
87 *max_offset = MAX(*max_offset, *offset);
88 return NDR_ERR_SUCCESS;
89 }
90
91 /* too many pointers */
92 return ndr_pull_error(ndr, NDR_ERR_STRING, "BAD NBT NAME component");
93}
94
95/**
96 pull a nbt_string from the wire
97*/
98_PUBLIC_ enum ndr_err_code ndr_pull_nbt_string(struct ndr_pull *ndr, int ndr_flags, const char **s)
99{
100 uint32_t offset = ndr->offset;
101 uint32_t max_offset = offset;
102 unsigned num_components;
103 char *name;
104
105 if (!(ndr_flags & NDR_SCALARS)) {
106 return NDR_ERR_SUCCESS;
107 }
108
109 name = NULL;
110
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;
116 if (name) {
117 name = talloc_asprintf_append_buffer(name, ".%s", component);
118 NDR_ERR_HAVE_NO_MEMORY(name);
119 } else {
120 name = (char *)component;
121 }
122 }
123 if (num_components == MAX_COMPONENTS) {
124 return ndr_pull_error(ndr, NDR_ERR_STRING,
125 "BAD NBT NAME too many components");
126 }
127 if (num_components == 0) {
128 name = talloc_strdup(ndr, "");
129 NDR_ERR_HAVE_NO_MEMORY(name);
130 }
131
132 (*s) = name;
133 ndr->offset = max_offset;
134
135 return NDR_ERR_SUCCESS;
136}
137
138/**
139 push a nbt string to the wire
140*/
141_PUBLIC_ enum ndr_err_code ndr_push_nbt_string(struct ndr_push *ndr, int ndr_flags, const char *s)
142{
143 if (!(ndr_flags & NDR_SCALARS)) {
144 return NDR_ERR_SUCCESS;
145 }
146
147 while (s && *s) {
148 enum ndr_err_code ndr_err;
149 char *compname;
150 size_t complen;
151 uint32_t offset;
152
153 /* see if we have pushed the remaing string allready,
154 * if so we use a label pointer to this string
155 */
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)) {
158 uint8_t b[2];
159
160 if (offset > 0x3FFF) {
161 return ndr_push_error(ndr, NDR_ERR_STRING,
162 "offset for nbt string label pointer %u[%08X] > 0x00003FFF",
163 offset, offset);
164 }
165
166 b[0] = 0xC0 | (offset>>8);
167 b[1] = (offset & 0xFF);
168
169 return ndr_push_bytes(ndr, b, 2);
170 }
171
172 complen = strcspn(s, ".");
173
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);
179 }
180
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);
186
187 /* remember the current componemt + the rest of the string
188 * so it can be reused later
189 */
190 NDR_CHECK(ndr_token_store(ndr, &ndr->nbt_string_list, s, ndr->offset));
191
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);
195
196 s += complen;
197 if (*s == '.') s++;
198 }
199
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
202 */
203 return ndr_push_bytes(ndr, (const uint8_t *)"", 1);
204}
205
206
207/*
208 decompress a 'compressed' name component
209 */
210static bool decompress_name(char *name, enum nbt_name_type *type)
211{
212 int i;
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') {
218 return false;
219 }
220 name[i] = ((c1-'A')<<4) | (c2-'A');
221 }
222 name[i] = 0;
223 if (i == 16) {
224 *type = (enum nbt_name_type)(name[15]);
225 name[15] = 0;
226 i--;
227 } else {
228 *type = NBT_NAME_CLIENT;
229 }
230
231 /* trim trailing spaces */
232 for (;i>0 && name[i-1]==' ';i--) {
233 name[i-1] = 0;
234 }
235
236 return true;
237}
238
239
240/*
241 compress a name component
242 */
243static uint8_t *compress_name(TALLOC_CTX *mem_ctx,
244 const uint8_t *name, enum nbt_name_type type)
245{
246 uint8_t *cname;
247 int i;
248 uint8_t pad_char;
249
250 if (strlen((const char *)name) > 15) {
251 return NULL;
252 }
253
254 cname = talloc_array(mem_ctx, uint8_t, 33);
255 if (cname == NULL) return NULL;
256
257 for (i=0;name[i];i++) {
258 cname[2*i] = 'A' + (name[i]>>4);
259 cname[1+2*i] = 'A' + (name[i]&0xF);
260 }
261 if (strcmp((const char *)name, "*") == 0) {
262 pad_char = 0;
263 } else {
264 pad_char = ' ';
265 }
266 for (;i<15;i++) {
267 cname[2*i] = 'A' + (pad_char>>4);
268 cname[1+2*i] = 'A' + (pad_char&0xF);
269 }
270
271 pad_char = type;
272 cname[2*i] = 'A' + (pad_char>>4);
273 cname[1+2*i] = 'A' + (pad_char&0xF);
274
275 cname[32] = 0;
276 return cname;
277}
278
279
280/**
281 pull a nbt name from the wire
282*/
283_PUBLIC_ enum ndr_err_code ndr_pull_nbt_name(struct ndr_pull *ndr, int ndr_flags, struct nbt_name *r)
284{
285 uint8_t *scope;
286 char *cname;
287 const char *s;
288 bool ok;
289
290 if (!(ndr_flags & NDR_SCALARS)) {
291 return NDR_ERR_SUCCESS;
292 }
293
294 NDR_CHECK(ndr_pull_nbt_string(ndr, ndr_flags, &s));
295
296 scope = (uint8_t *)strchr(s, '.');
297 if (scope) {
298 *scope = 0;
299 r->scope = talloc_strdup(ndr->current_mem_ctx, (const char *)&scope[1]);
300 NDR_ERR_HAVE_NO_MEMORY(r->scope);
301 } else {
302 r->scope = NULL;
303 }
304
305 cname = discard_const_p(char, s);
306
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");
312 }
313
314 /* decompress the first component */
315 ok = decompress_name(cname, &r->type);
316 if (!ok) {
317 return ndr_pull_error(ndr, NDR_ERR_STRING,
318 "NBT NAME failed to decompress");
319 }
320
321 r->name = talloc_strdup(ndr->current_mem_ctx, cname);
322 NDR_ERR_HAVE_NO_MEMORY(r->name);
323
324 talloc_free(cname);
325
326 return NDR_ERR_SUCCESS;
327}
328
329/**
330 push a nbt name to the wire
331*/
332_PUBLIC_ enum ndr_err_code ndr_push_nbt_name(struct ndr_push *ndr, int ndr_flags, const struct nbt_name *r)
333{
334 uint8_t *cname, *fullname;
335 enum ndr_err_code ndr_err;
336
337 if (!(ndr_flags & NDR_SCALARS)) {
338 return NDR_ERR_SUCCESS;
339 }
340
341 if (strlen(r->name) > 15) {
342 return ndr_push_error(ndr, NDR_ERR_STRING,
343 "nbt_name longer as 15 chars: %s",
344 r->name);
345 }
346
347 cname = compress_name(ndr, (const uint8_t *)r->name, r->type);
348 NDR_ERR_HAVE_NO_MEMORY(cname);
349
350 if (r->scope) {
351 fullname = (uint8_t *)talloc_asprintf(ndr, "%s.%s", cname, r->scope);
352 NDR_ERR_HAVE_NO_MEMORY(fullname);
353 talloc_free(cname);
354 } else {
355 fullname = cname;
356 }
357
358 ndr_err = ndr_push_nbt_string(ndr, ndr_flags, (const char *)fullname);
359
360 return ndr_err;
361}
362
363
364/**
365 copy a nbt name structure
366*/
367_PUBLIC_ NTSTATUS nbt_name_dup(TALLOC_CTX *mem_ctx, struct nbt_name *name, struct nbt_name *newname)
368{
369 *newname = *name;
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);
373 if (name->scope) {
374 NT_STATUS_HAVE_NO_MEMORY(newname->scope);
375 }
376 return NT_STATUS_OK;
377}
378
379/**
380 push a nbt name into a blob
381*/
382_PUBLIC_ NTSTATUS nbt_name_to_blob(TALLOC_CTX *mem_ctx, DATA_BLOB *blob, struct nbt_name *name)
383{
384 enum ndr_err_code ndr_err;
385
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);
389 }
390
391 return NT_STATUS_OK;
392}
393
394/**
395 pull a nbt name from a blob
396*/
397_PUBLIC_ NTSTATUS nbt_name_from_blob(TALLOC_CTX *mem_ctx, const DATA_BLOB *blob, struct nbt_name *name)
398{
399 enum ndr_err_code ndr_err;
400
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);
405 }
406
407 return NT_STATUS_OK;
408}
409
410
411/**
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
415 truncated name
416*/
417_PUBLIC_ void nbt_choose_called_name(TALLOC_CTX *mem_ctx,
418 struct nbt_name *n, const char *name, int type)
419{
420 n->scope = NULL;
421 n->type = type;
422
423 if ((name == NULL) || is_ipaddress(name)) {
424 n->name = "*SMBSERVER";
425 return;
426 }
427 if (strlen(name) > 15) {
428 const char *p = strchr(name, '.');
429 char *s;
430 if (p - name > 15) {
431 n->name = "*SMBSERVER";
432 return;
433 }
434 s = talloc_strndup(mem_ctx, name, PTR_DIFF(p, name));
435 n->name = talloc_strdup_upper(mem_ctx, s);
436 return;
437 }
438
439 n->name = talloc_strdup_upper(mem_ctx, name);
440}
441
442
443/*
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
446*/
447static const char *nbt_hex_encode(TALLOC_CTX *mem_ctx, const char *s)
448{
449 int i, len;
450 char *ret;
451 const char *valid_chars = "_-.$@ ";
452#define NBT_CHAR_ALLOW(c) (isalnum((unsigned char)c) || strchr(valid_chars, c))
453
454 for (len=i=0;s[i];i++,len++) {
455 if (!NBT_CHAR_ALLOW(s[i])) {
456 len += 2;
457 }
458 }
459
460 ret = talloc_array(mem_ctx, char, len+1);
461 if (ret == NULL) return NULL;
462
463 for (len=i=0;s[i];i++) {
464 if (NBT_CHAR_ALLOW(s[i])) {
465 ret[len++] = s[i];
466 } else {
467 snprintf(&ret[len], 4, "%%%02x", (unsigned char)s[i]);
468 len += 3;
469 }
470 }
471 ret[len] = 0;
472
473 return ret;
474}
475
476
477/**
478 form a string for a NBT name
479*/
480_PUBLIC_ char *nbt_name_string(TALLOC_CTX *mem_ctx, const struct nbt_name *name)
481{
482 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
483 char *ret;
484 if (name->scope) {
485 ret = talloc_asprintf(mem_ctx, "%s<%02x>-%s",
486 nbt_hex_encode(tmp_ctx, name->name),
487 name->type,
488 nbt_hex_encode(tmp_ctx, name->scope));
489 } else {
490 ret = talloc_asprintf(mem_ctx, "%s<%02x>",
491 nbt_hex_encode(tmp_ctx, name->name),
492 name->type);
493 }
494 talloc_free(tmp_ctx);
495 return ret;
496}
497
498/**
499 pull a nbt name, WINS Replication uses another on wire format for nbt name
500*/
501_PUBLIC_ enum ndr_err_code ndr_pull_wrepl_nbt_name(struct ndr_pull *ndr, int ndr_flags, const struct nbt_name **_r)
502{
503 struct nbt_name *r;
504 uint8_t *namebuf;
505 uint32_t namebuf_len;
506
507 if (!(ndr_flags & NDR_SCALARS)) {
508 return NDR_ERR_SUCCESS;
509 }
510
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");
515 }
516 NDR_PULL_ALLOC_N(ndr, namebuf, namebuf_len);
517 NDR_CHECK(ndr_pull_array_uint8(ndr, NDR_SCALARS, namebuf, namebuf_len));
518
519 NDR_PULL_ALLOC(ndr, r);
520
521 /* oh wow, what a nasty bug in windows ... */
522 if (namebuf[0] == 0x1b && namebuf_len >= 16) {
523 namebuf[0] = namebuf[15];
524 namebuf[15] = 0x1b;
525 }
526
527 if (namebuf_len < 17) {
528 r->type = 0x00;
529
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");
532
533 r->scope= NULL;
534
535 talloc_free(namebuf);
536 *_r = r;
537 return NDR_ERR_SUCCESS;
538 }
539
540 r->type = namebuf[15];
541
542 namebuf[15] = '\0';
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");
546
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");
550 } else {
551 r->scope = NULL;
552 }
553
554 talloc_free(namebuf);
555 *_r = r;
556 return NDR_ERR_SUCCESS;
557}
558
559/**
560 push a nbt name, WINS Replication uses another on wire format for nbt name
561*/
562_PUBLIC_ enum ndr_err_code ndr_push_wrepl_nbt_name(struct ndr_push *ndr, int ndr_flags, const struct nbt_name *r)
563{
564 uint8_t *namebuf;
565 uint32_t namebuf_len;
566 uint32_t _name_len;
567 uint32_t scope_len = 0;
568
569 if (r == NULL) {
570 return ndr_push_error(ndr, NDR_ERR_INVALID_POINTER,
571 "wrepl_nbt_name NULL pointer");
572 }
573
574 if (!(ndr_flags & NDR_SCALARS)) {
575 return NDR_ERR_SUCCESS;
576 }
577
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",
582 r->name);
583 }
584
585 if (r->scope) {
586 scope_len = strlen(r->scope);
587 }
588 if (scope_len > 238) {
589 return ndr_push_error(ndr, NDR_ERR_STRING,
590 "wrepl_nbt_name scope longer as 238 chars: %s",
591 r->scope);
592 }
593
594 namebuf = (uint8_t *)talloc_asprintf(ndr, "%-15s%c%s",
595 r->name, 'X',
596 (r->scope?r->scope:""));
597 if (!namebuf) return ndr_push_error(ndr, NDR_ERR_ALLOC, "out of memory");
598
599 namebuf_len = strlen((char *)namebuf) + 1;
600
601 /*
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
604 */
605 namebuf[15] = r->type;
606
607 /* oh wow, what a nasty bug in windows ... */
608 if (r->type == 0x1b) {
609 namebuf[15] = namebuf[0];
610 namebuf[0] = 0x1b;
611 }
612
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));
616
617 talloc_free(namebuf);
618 return NDR_ERR_SUCCESS;
619}
620
621_PUBLIC_ void ndr_print_wrepl_nbt_name(struct ndr_print *ndr, const char *name, const struct nbt_name *r)
622{
623 char *s = nbt_name_string(ndr, r);
624 ndr_print_string(ndr, name, s);
625 talloc_free(s);
626}
627
628_PUBLIC_ enum ndr_err_code ndr_push_nbt_res_rec(struct ndr_push *ndr, int ndr_flags, const struct nbt_res_rec *r)
629{
630 {
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));
641 }
642 if (ndr_flags & NDR_BUFFERS) {
643 }
644 ndr->flags = _flags_save_STRUCT;
645 }
646 return NDR_ERR_SUCCESS;
647}
Note: See TracBrowser for help on using the repository browser.