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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 see rfc1002 for the detailed format of compressed names
28 #include "system/iconv.h"
29 #include "librpc/gen_ndr/ndr_nbt.h"
31 /* don't allow an unlimited number of name components */
32 #define MAX_COMPONENTS 10
37 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, &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 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);
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 NTSTATUS ndr_push_nbt_string(struct ndr_push *ndr, int ndr_flags, const char *s)
143 if (!(ndr_flags & NDR_SCALARS)) {
147 if (s == NULL || *s == 0) {
148 return ndr_push_bytes(ndr, "", 1);
152 fullname = talloc_strdup(ndr, "");
153 NT_STATUS_HAVE_NO_MEMORY(fullname);
156 int len = strcspn(s, ".");
157 fullname = talloc_asprintf_append(fullname, "%c%*.*s",
160 (unsigned char)len, s);
161 NT_STATUS_HAVE_NO_MEMORY(fullname);
166 /* see if we can find the fullname in the existing packet - if
167 so, we can use a NBT name pointer. This allows us to fit
168 longer names into the packet */
169 fulllen = strlen(fullname)+1;
170 for (i=0;i + fulllen <= ndr->offset;i++) {
171 if (ndr->data[i] == fullname[0] &&
172 memcmp(fullname, &ndr->data[i], fulllen) == 0) {
174 talloc_free(fullname);
175 b[0] = 0xC0 | (i>>8);
177 return ndr_push_bytes(ndr, b, 2);
181 NDR_CHECK(ndr_push_bytes(ndr, fullname, fulllen));
183 talloc_free(fullname);
190 decompress a 'compressed' name component
192 static NTSTATUS decompress_name(char *name, enum nbt_name_type *type)
195 for (i=0;name[2*i];i++) {
196 uint8_t c1 = name[2*i];
197 uint8_t c2 = name[1+(2*i)];
198 if (c1 < 'A' || c1 > 'P' ||
199 c2 < 'A' || c2 > 'P') {
200 return NT_STATUS_BAD_NETWORK_NAME;
202 name[i] = ((c1-'A')<<4) | (c2-'A');
206 *type = (enum nbt_name_type)(name[15]);
210 *type = NBT_NAME_CLIENT;
213 /* trim trailing spaces */
214 for (;i>0 && name[i-1]==' ';i--) {
223 compress a name component
225 static uint8_t *compress_name(TALLOC_CTX *mem_ctx,
226 const uint8_t *name, enum nbt_name_type type)
232 if (strlen(name) > 15) {
236 cname = talloc_array(mem_ctx, uint8_t, 33);
237 if (cname == NULL) return NULL;
239 for (i=0;name[i];i++) {
240 cname[2*i] = 'A' + (name[i]>>4);
241 cname[1+2*i] = 'A' + (name[i]&0xF);
243 if (strcmp(name, "*") == 0) {
249 cname[2*i] = 'A' + (pad_char>>4);
250 cname[1+2*i] = 'A' + (pad_char&0xF);
254 cname[2*i] = 'A' + (pad_char>>4);
255 cname[1+2*i] = 'A' + (pad_char&0xF);
263 pull a nbt name from the wire
265 NTSTATUS ndr_pull_nbt_name(struct ndr_pull *ndr, int ndr_flags, struct nbt_name *r)
272 if (!(ndr_flags & NDR_SCALARS)) {
276 status = ndr_pull_nbt_string(ndr, ndr_flags, &s);
277 NT_STATUS_NOT_OK_RETURN(status);
279 scope = strchr(s, '.');
282 r->scope = talloc_strdup(ndr, scope+1);
283 NT_STATUS_HAVE_NO_MEMORY(r->scope);
288 cname = discard_const_p(char, s);
290 /* the first component is limited to 16 bytes in the DOS charset,
291 which is 32 in the 'compressed' form */
292 if (strlen(cname) > 32) {
293 return NT_STATUS_BAD_NETWORK_NAME;
296 /* decompress the first component */
297 status = decompress_name(cname, &r->type);
298 NT_STATUS_NOT_OK_RETURN(status);
300 r->name = talloc_strdup(ndr, cname);
301 NT_STATUS_HAVE_NO_MEMORY(r->name);
309 push a nbt name to the wire
311 NTSTATUS ndr_push_nbt_name(struct ndr_push *ndr, int ndr_flags, struct nbt_name *r)
313 uint8_t *cname, *fullname;
316 if (!(ndr_flags & NDR_SCALARS)) {
320 cname = compress_name(ndr, r->name, r->type);
321 NT_STATUS_HAVE_NO_MEMORY(cname);
324 fullname = talloc_asprintf(ndr, "%s.%s", cname, r->scope);
325 NT_STATUS_HAVE_NO_MEMORY(fullname);
331 status = ndr_push_nbt_string(ndr, ndr_flags, fullname);
332 talloc_free(fullname);
339 copy a nbt name structure
341 NTSTATUS nbt_name_dup(TALLOC_CTX *mem_ctx, struct nbt_name *name, struct nbt_name *newname)
344 newname->name = talloc_strdup(mem_ctx, newname->name);
345 NT_STATUS_HAVE_NO_MEMORY(newname->name);
346 newname->scope = talloc_strdup(mem_ctx, newname->scope);
348 NT_STATUS_HAVE_NO_MEMORY(newname->scope);
354 push a nbt name into a blob
356 NTSTATUS nbt_name_to_blob(TALLOC_CTX *mem_ctx, DATA_BLOB *blob, struct nbt_name *name)
358 return ndr_push_struct_blob(blob, mem_ctx, name,
359 (ndr_push_flags_fn_t)ndr_push_nbt_name);
364 pull a nbt name from a blob
366 NTSTATUS nbt_name_from_blob(TALLOC_CTX *mem_ctx, const DATA_BLOB *blob, struct nbt_name *name)
368 return ndr_pull_struct_blob(blob, mem_ctx, name,
369 (ndr_pull_flags_fn_t)ndr_pull_nbt_name);
374 choose a name to use when calling a server in a NBT session request.
375 we use heuristics to see if the name we have been given is a IP
376 address, or a too-long name. If it is then use *SMBSERVER, or a
379 void nbt_choose_called_name(TALLOC_CTX *mem_ctx,
380 struct nbt_name *n, const char *name, int type)
385 if (is_ipaddress(name)) {
386 n->name = "*SMBSERVER";
389 if (strlen(name) > 15) {
390 const char *p = strchr(name, '.');
393 n->name = "*SMBSERVER";
396 s = talloc_strndup(mem_ctx, name, PTR_DIFF(p, name));
397 n->name = strupper_talloc(mem_ctx, s);
401 n->name = strupper_talloc(mem_ctx, name);
406 escape a string into a form containing only a small set of characters,
407 the rest is hex encoded. This is similar to URL encoding
409 static const char *nbt_hex_encode(TALLOC_CTX *mem_ctx, const char *s)
413 const char *valid_chars = "_-.$@ ";
414 #define NBT_CHAR_ALLOW(c) (isalnum(c) || strchr(valid_chars, c))
416 for (len=i=0;s[i];i++,len++) {
417 if (!NBT_CHAR_ALLOW(s[i])) {
422 ret = talloc_array(mem_ctx, char, len+1);
423 if (ret == NULL) return NULL;
425 for (len=i=0;s[i];i++) {
426 if (NBT_CHAR_ALLOW(s[i])) {
429 snprintf(&ret[len], 4, "%%%02x", (unsigned char)s[i]);
440 form a string for a NBT name
442 char *nbt_name_string(TALLOC_CTX *mem_ctx, const struct nbt_name *name)
444 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
447 ret = talloc_asprintf(mem_ctx, "%s<%02x>-%s",
448 nbt_hex_encode(tmp_ctx, name->name),
450 nbt_hex_encode(tmp_ctx, name->scope));
452 ret = talloc_asprintf(mem_ctx, "%s<%02x>",
453 nbt_hex_encode(tmp_ctx, name->name),
456 talloc_free(tmp_ctx);