r23792: convert Samba4 to GPLv3
[ira/wip.git] / source4 / libcli / nbt / nbtname.c
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 #include "system/locale.h"
30
31 /* don't allow an unlimited number of name components */
32 #define MAX_COMPONENTS 10
33
34 /**
35   print a nbt string
36 */
37 _PUBLIC_ void ndr_print_nbt_string(struct ndr_print *ndr, const char *name, const char *s)
38 {
39         ndr_print_string(ndr, name, s);
40 }
41
42 /*
43   pull one component of a nbt_string
44 */
45 static NTSTATUS ndr_pull_component(struct ndr_pull *ndr, uint8_t **component,
46                                    uint32_t *offset, uint32_t *max_offset)
47 {
48         uint8_t len;
49         uint_t loops = 0;
50         while (loops < 5) {
51                 if (*offset >= ndr->data_size) {
52                         return NT_STATUS_BAD_NETWORK_NAME;
53                 }
54                 len = ndr->data[*offset];
55                 if (len == 0) {
56                         *offset += 1;
57                         *max_offset = MAX(*max_offset, *offset);
58                         *component = NULL;
59                         return NT_STATUS_OK;
60                 }
61                 if ((len & 0xC0) == 0xC0) {
62                         /* its a label pointer */
63                         if (1 + *offset >= ndr->data_size) {
64                                 return NT_STATUS_BAD_NETWORK_NAME;
65                         }
66                         *max_offset = MAX(*max_offset, *offset + 2);
67                         *offset = ((len&0x3F)<<8) | ndr->data[1 + *offset];
68                         *max_offset = MAX(*max_offset, *offset);
69                         loops++;
70                         continue;
71                 }
72                 if ((len & 0xC0) != 0) {
73                         /* its a reserved length field */
74                         return NT_STATUS_BAD_NETWORK_NAME;
75                 }
76                 if (*offset + len + 2 > ndr->data_size) {
77                         return NT_STATUS_BAD_NETWORK_NAME;
78                 }
79                 *component = (uint8_t*)talloc_strndup(ndr, (const char *)&ndr->data[1 + *offset], len);
80                 NT_STATUS_HAVE_NO_MEMORY(*component);
81                 *offset += len + 1;
82                 *max_offset = MAX(*max_offset, *offset);
83                 return NT_STATUS_OK;
84         }
85
86         /* too many pointers */
87         return NT_STATUS_BAD_NETWORK_NAME;
88 }
89
90 /**
91   pull a nbt_string from the wire
92 */
93 _PUBLIC_ NTSTATUS ndr_pull_nbt_string(struct ndr_pull *ndr, int ndr_flags, const char **s)
94 {
95         NTSTATUS status;
96         uint32_t offset = ndr->offset;
97         uint32_t max_offset = offset;
98         unsigned num_components;
99         char *name;
100
101         if (!(ndr_flags & NDR_SCALARS)) {
102                 return NT_STATUS_OK;
103         }
104
105         name = NULL;
106
107         /* break up name into a list of components */
108         for (num_components=0;num_components<MAX_COMPONENTS;num_components++) {
109                 uint8_t *component;
110                 status = ndr_pull_component(ndr, &component, &offset, &max_offset);
111                 NT_STATUS_NOT_OK_RETURN(status);
112                 if (component == NULL) break;
113                 if (name) {
114                         name = talloc_asprintf_append(name, ".%s", component);
115                         NT_STATUS_HAVE_NO_MEMORY(name);
116                 } else {
117                         name = (char *)component;
118                 }
119         }
120         if (num_components == MAX_COMPONENTS) {
121                 return NT_STATUS_BAD_NETWORK_NAME;
122         }
123         if (num_components == 0) {
124                 name = talloc_strdup(ndr, "");
125                 NT_STATUS_HAVE_NO_MEMORY(name);
126         }
127
128         (*s) = name;
129         ndr->offset = max_offset;
130
131         return NT_STATUS_OK;
132 }
133
134 /**
135   push a nbt string to the wire
136 */
137 _PUBLIC_ NTSTATUS ndr_push_nbt_string(struct ndr_push *ndr, int ndr_flags, const char *s)
138 {
139         if (!(ndr_flags & NDR_SCALARS)) {
140                 return NT_STATUS_OK;
141         }
142
143         while (s && *s) {
144                 NTSTATUS status;
145                 char *compname;
146                 size_t complen;
147                 uint32_t offset;
148
149                 /* see if we have pushed the remaing string allready,
150                  * if so we use a label pointer to this string
151                  */
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)) {
154                         uint8_t b[2];
155                         
156                         if (offset > 0x3FFF) {
157                                 return ndr_push_error(ndr, NDR_ERR_STRING,
158                                                       "offset for nbt string label pointer %u[%08X] > 0x00003FFF",
159                                                       offset, offset);
160                         }
161
162                         b[0] = 0xC0 | (offset>>8);
163                         b[1] = (offset & 0xFF);
164
165                         return ndr_push_bytes(ndr, b, 2);
166                 }
167
168                 complen = strcspn(s, ".");
169
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);
175                 }
176
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);
182
183                 /* remember the current componemt + the rest of the string
184                  * so it can be reused later
185                  */
186                 NDR_CHECK(ndr_token_store(ndr, &ndr->nbt_string_list, s, ndr->offset));
187
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);
191
192                 s += complen;
193                 if (*s == '.') s++;
194         }
195
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
198          */
199         return ndr_push_bytes(ndr, (const uint8_t *)"", 1);
200 }
201
202
203 /*
204   decompress a 'compressed' name component
205  */
206 static NTSTATUS decompress_name(char *name, enum nbt_name_type *type)
207 {
208         int i;
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;
215                 }
216                 name[i] = ((c1-'A')<<4) | (c2-'A');                 
217         }
218         name[i] = 0;
219         if (i == 16) {
220                 *type = (enum nbt_name_type)(name[15]);
221                 name[15] = 0;
222                 i--;
223         } else {
224                 *type = NBT_NAME_CLIENT;
225         }
226
227         /* trim trailing spaces */
228         for (;i>0 && name[i-1]==' ';i--) {
229                 name[i-1] = 0;
230         }
231         
232         return NT_STATUS_OK;
233 }
234
235
236 /*
237   compress a name component
238  */
239 static uint8_t *compress_name(TALLOC_CTX *mem_ctx, 
240                               const uint8_t *name, enum nbt_name_type type)
241 {
242         uint8_t *cname;
243         int i;
244         uint8_t pad_char;
245
246         if (strlen((const char *)name) > 15) {
247                 return NULL;
248         }
249
250         cname = talloc_array(mem_ctx, uint8_t, 33);
251         if (cname == NULL) return NULL;
252
253         for (i=0;name[i];i++) {
254                 cname[2*i]   = 'A' + (name[i]>>4);
255                 cname[1+2*i] = 'A' + (name[i]&0xF);
256         }
257         if (strcmp((const char *)name, "*") == 0) {
258                 pad_char = 0;
259         } else {
260                 pad_char = ' ';
261         }
262         for (;i<15;i++) {
263                 cname[2*i]   = 'A' + (pad_char>>4);
264                 cname[1+2*i] = 'A' + (pad_char&0xF);
265         }
266
267         pad_char = type;
268         cname[2*i]   = 'A' + (pad_char>>4);
269         cname[1+2*i] = 'A' + (pad_char&0xF);
270
271         cname[32] = 0;
272         return cname;
273 }
274
275
276 /**
277   pull a nbt name from the wire
278 */
279 _PUBLIC_ NTSTATUS ndr_pull_nbt_name(struct ndr_pull *ndr, int ndr_flags, struct nbt_name *r)
280 {
281         NTSTATUS status;
282         uint8_t *scope;
283         char *cname;
284         const char *s;
285
286         if (!(ndr_flags & NDR_SCALARS)) {
287                 return NT_STATUS_OK;
288         }
289
290         status = ndr_pull_nbt_string(ndr, ndr_flags, &s);
291         NT_STATUS_NOT_OK_RETURN(status);
292
293         scope = (uint8_t *)strchr(s, '.');
294         if (scope) {
295                 *scope = 0;
296                 r->scope = talloc_strdup(ndr->current_mem_ctx, (const char *)&scope[1]);
297                 NT_STATUS_HAVE_NO_MEMORY(r->scope);
298         } else {
299                 r->scope = NULL;
300         }
301
302         cname = discard_const_p(char, s);
303
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;
308         }
309
310         /* decompress the first component */
311         status = decompress_name(cname, &r->type);
312         NT_STATUS_NOT_OK_RETURN(status);
313
314         r->name = talloc_strdup(ndr->current_mem_ctx, cname);
315         NT_STATUS_HAVE_NO_MEMORY(r->name);
316
317         talloc_free(cname);
318
319         return NT_STATUS_OK;
320 }
321
322 /**
323   push a nbt name to the wire
324 */
325 _PUBLIC_ NTSTATUS ndr_push_nbt_name(struct ndr_push *ndr, int ndr_flags, const struct nbt_name *r)
326 {
327         uint8_t *cname, *fullname;
328         NTSTATUS status;
329
330         if (!(ndr_flags & NDR_SCALARS)) {
331                 return NT_STATUS_OK;
332         }
333
334         if (strlen(r->name) > 15) {
335                 return ndr_push_error(ndr, NDR_ERR_STRING,
336                                       "nbt_name longer as 15 chars: %s",
337                                       r->name);
338         }
339
340         cname = compress_name(ndr, (const uint8_t *)r->name, r->type);
341         NT_STATUS_HAVE_NO_MEMORY(cname);
342
343         if (r->scope) {
344                 fullname = (uint8_t *)talloc_asprintf(ndr, "%s.%s", cname, r->scope);
345                 NT_STATUS_HAVE_NO_MEMORY(fullname);
346                 talloc_free(cname);
347         } else {
348                 fullname = cname;
349         }
350         
351         status = ndr_push_nbt_string(ndr, ndr_flags, (const char *)fullname);
352
353         return status;
354 }
355
356
357 /**
358   copy a nbt name structure
359 */
360 _PUBLIC_ NTSTATUS nbt_name_dup(TALLOC_CTX *mem_ctx, struct nbt_name *name, struct nbt_name *newname)
361 {
362         *newname = *name;
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);
366         if (name->scope) {
367                 NT_STATUS_HAVE_NO_MEMORY(newname->scope);
368         }
369         return NT_STATUS_OK;
370 }
371
372 /**
373   push a nbt name into a blob
374 */
375 _PUBLIC_ NTSTATUS nbt_name_to_blob(TALLOC_CTX *mem_ctx, DATA_BLOB *blob, struct nbt_name *name)
376 {
377         return ndr_push_struct_blob(blob, mem_ctx, name, 
378                                     (ndr_push_flags_fn_t)ndr_push_nbt_name);
379 }
380
381
382 /**
383   pull a nbt name from a blob
384 */
385 _PUBLIC_ NTSTATUS nbt_name_from_blob(TALLOC_CTX *mem_ctx, const DATA_BLOB *blob, struct nbt_name *name)
386 {
387         return ndr_pull_struct_blob(blob, mem_ctx, name, 
388                                     (ndr_pull_flags_fn_t)ndr_pull_nbt_name);
389 }
390
391
392 /**
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
396   truncated name
397 */
398 _PUBLIC_ void nbt_choose_called_name(TALLOC_CTX *mem_ctx,
399                             struct nbt_name *n, const char *name, int type)
400 {
401         n->scope = NULL;
402         n->type = type;
403
404         if (is_ipaddress(name)) {
405                 n->name = "*SMBSERVER";
406                 return;
407         }
408         if (strlen(name) > 15) {
409                 const char *p = strchr(name, '.');
410                 char *s;
411                 if (p - name > 15) {
412                         n->name = "*SMBSERVER";
413                         return;
414                 }
415                 s = talloc_strndup(mem_ctx, name, PTR_DIFF(p, name));
416                 n->name = strupper_talloc(mem_ctx, s);
417                 return;
418         }
419
420         n->name = strupper_talloc(mem_ctx, name);
421 }
422
423
424 /*
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
427 */
428 static const char *nbt_hex_encode(TALLOC_CTX *mem_ctx, const char *s)
429 {
430         int i, len;
431         char *ret;
432         const char *valid_chars = "_-.$@ ";
433 #define NBT_CHAR_ALLOW(c) (isalnum((unsigned char)c) || strchr(valid_chars, c))
434
435         for (len=i=0;s[i];i++,len++) {
436                 if (!NBT_CHAR_ALLOW(s[i])) {
437                         len += 2;
438                 }
439         }
440
441         ret = talloc_array(mem_ctx, char, len+1);
442         if (ret == NULL) return NULL;
443
444         for (len=i=0;s[i];i++) {
445                 if (NBT_CHAR_ALLOW(s[i])) {
446                         ret[len++] = s[i];
447                 } else {
448                         snprintf(&ret[len], 4, "%%%02x", (unsigned char)s[i]);
449                         len += 3;
450                 }
451         }
452         ret[len] = 0;
453
454         return ret;
455 }
456
457
458 /**
459   form a string for a NBT name
460 */
461 _PUBLIC_ char *nbt_name_string(TALLOC_CTX *mem_ctx, const struct nbt_name *name)
462 {
463         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
464         char *ret;
465         if (name->scope) {              
466                 ret = talloc_asprintf(mem_ctx, "%s<%02x>-%s",
467                                       nbt_hex_encode(tmp_ctx, name->name),
468                                       name->type, 
469                                       nbt_hex_encode(tmp_ctx, name->scope));
470         } else {
471                 ret = talloc_asprintf(mem_ctx, "%s<%02x>", 
472                                       nbt_hex_encode(tmp_ctx, name->name), 
473                                       name->type);
474         }
475         talloc_free(tmp_ctx);
476         return ret;
477 }
478
479 /**
480   pull a nbt name, WINS Replication uses another on wire format for nbt name
481 */
482 _PUBLIC_ NTSTATUS ndr_pull_wrepl_nbt_name(struct ndr_pull *ndr, int ndr_flags, struct nbt_name **_r)
483 {
484         struct nbt_name *r;
485         uint8_t *namebuf;
486         uint32_t namebuf_len;
487
488         if (!(ndr_flags & NDR_SCALARS)) {
489                 return NT_STATUS_OK;
490         }
491
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");
496         }
497         NDR_PULL_ALLOC_N(ndr, namebuf, namebuf_len);
498         NDR_CHECK(ndr_pull_array_uint8(ndr, NDR_SCALARS, namebuf, namebuf_len));
499
500         NDR_PULL_ALLOC(ndr, r); 
501
502         /* oh wow, what a nasty bug in windows ... */
503         if (namebuf[0] == 0x1b && namebuf_len >= 16) {
504                 namebuf[0] = namebuf[15];
505                 namebuf[15] = 0x1b;
506         }
507
508         if (namebuf_len < 17) {
509                 r->type = 0x00;
510
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");
513
514                 r->scope= NULL;
515
516                 talloc_free(namebuf);
517                 *_r = r;
518                 return NT_STATUS_OK;
519         }
520
521         r->type = namebuf[15];
522
523         namebuf[15] = '\0';
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");
527
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");
531         } else {
532                 r->scope = NULL;
533         }
534
535         talloc_free(namebuf);
536         *_r = r;
537         return NT_STATUS_OK;
538 }
539
540 /**
541   push a nbt name, WINS Replication uses another on wire format for nbt name
542 */
543 _PUBLIC_ NTSTATUS ndr_push_wrepl_nbt_name(struct ndr_push *ndr, int ndr_flags, const struct nbt_name *r)
544 {
545         uint8_t *namebuf;
546         uint32_t namebuf_len;
547         uint32_t name_len;
548         uint32_t scope_len = 0;
549
550         if (r == NULL) return NT_STATUS_INVALID_PARAMETER_MIX;
551
552         if (!(ndr_flags & NDR_SCALARS)) {
553                 return NT_STATUS_OK;
554         }
555
556         name_len = strlen(r->name);
557         if (name_len > 15) {
558                 return NT_STATUS_INVALID_PARAMETER_MIX;
559         }
560
561         if (r->scope) {
562                 scope_len = strlen(r->scope);
563         }
564         if (scope_len > 238) {
565                 return NT_STATUS_INVALID_PARAMETER_MIX;
566         }
567
568         namebuf = (uint8_t *)talloc_asprintf(ndr, "%-15s%c%s",
569                                              r->name, 'X',
570                                              (r->scope?r->scope:""));
571         if (!namebuf) return ndr_push_error(ndr, NDR_ERR_ALLOC, "out of memory");
572
573         namebuf_len = strlen((char *)namebuf) + 1;
574
575         /*
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
578          */
579         namebuf[15] = r->type;
580
581         /* oh wow, what a nasty bug in windows ... */
582         if (r->type == 0x1b) {
583                 namebuf[15] = namebuf[0];
584                 namebuf[0] = 0x1b;
585         }
586
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));
590
591         talloc_free(namebuf);
592         return NT_STATUS_OK;
593 }
594
595 _PUBLIC_ void ndr_print_wrepl_nbt_name(struct ndr_print *ndr, const char *name, const struct nbt_name *r)
596 {
597         char *s = nbt_name_string(ndr, r);
598         ndr_print_string(ndr, name, s);
599         talloc_free(s);
600 }