Install public header files again and include required prototypes.
[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 #include "param/param.h"
31
32 /* don't allow an unlimited number of name components */
33 #define MAX_COMPONENTS 10
34
35 /**
36   print a nbt string
37 */
38 _PUBLIC_ void ndr_print_nbt_string(struct ndr_print *ndr, const char *name, const char *s)
39 {
40         ndr_print_string(ndr, name, s);
41 }
42
43 /*
44   pull one component of a nbt_string
45 */
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)
50 {
51         uint8_t len;
52         uint_t 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");
57                 }
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;
64                 }
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");
70                         }
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;
76                 }
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");
81                 }
82                 if (*offset + len + 2 > ndr->data_size) {
83                         return ndr_pull_error(ndr, NDR_ERR_STRING,
84                                               "BAD NBT NAME component");
85                 }
86                 *component = (uint8_t*)talloc_strndup(ndr, (const char *)&ndr->data[1 + *offset], len);
87                 NDR_ERR_HAVE_NO_MEMORY(*component);
88                 *offset += len + 1;
89                 *max_offset = MAX(*max_offset, *offset);
90                 return NDR_ERR_SUCCESS;
91         }
92
93         /* too many pointers */
94         return ndr_pull_error(ndr, NDR_ERR_STRING, "BAD NBT NAME component");
95 }
96
97 /**
98   pull a nbt_string from the wire
99 */
100 _PUBLIC_ enum ndr_err_code ndr_pull_nbt_string(struct ndr_pull *ndr, int ndr_flags, const char **s)
101 {
102         uint32_t offset = ndr->offset;
103         uint32_t max_offset = offset;
104         unsigned num_components;
105         char *name;
106
107         if (!(ndr_flags & NDR_SCALARS)) {
108                 return NDR_ERR_SUCCESS;
109         }
110
111         name = NULL;
112
113         /* break up name into a list of components */
114         for (num_components=0;num_components<MAX_COMPONENTS;num_components++) {
115                 uint8_t *component;
116                 NDR_CHECK(ndr_pull_component(ndr, &component, &offset, &max_offset));
117                 if (component == NULL) break;
118                 if (name) {
119                         name = talloc_asprintf_append_buffer(name, ".%s", component);
120                         NDR_ERR_HAVE_NO_MEMORY(name);
121                 } else {
122                         name = (char *)component;
123                 }
124         }
125         if (num_components == MAX_COMPONENTS) {
126                 return ndr_pull_error(ndr, NDR_ERR_STRING,
127                                       "BAD NBT NAME too many components");
128         }
129         if (num_components == 0) {
130                 name = talloc_strdup(ndr, "");
131                 NDR_ERR_HAVE_NO_MEMORY(name);
132         }
133
134         (*s) = name;
135         ndr->offset = max_offset;
136
137         return NDR_ERR_SUCCESS;
138 }
139
140 /**
141   push a nbt string to the wire
142 */
143 _PUBLIC_ enum ndr_err_code ndr_push_nbt_string(struct ndr_push *ndr, int ndr_flags, const char *s)
144 {
145         if (!(ndr_flags & NDR_SCALARS)) {
146                 return NDR_ERR_SUCCESS;
147         }
148
149         while (s && *s) {
150                 enum ndr_err_code ndr_err;
151                 char *compname;
152                 size_t complen;
153                 uint32_t offset;
154
155                 /* see if we have pushed the remaing string allready,
156                  * if so we use a label pointer to this string
157                  */
158                 ndr_err = ndr_token_retrieve_cmp_fn(&ndr->nbt_string_list, s, &offset, (comparison_fn_t)strcmp, false);
159                 if (NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
160                         uint8_t b[2];
161                         
162                         if (offset > 0x3FFF) {
163                                 return ndr_push_error(ndr, NDR_ERR_STRING,
164                                                       "offset for nbt string label pointer %u[%08X] > 0x00003FFF",
165                                                       offset, offset);
166                         }
167
168                         b[0] = 0xC0 | (offset>>8);
169                         b[1] = (offset & 0xFF);
170
171                         return ndr_push_bytes(ndr, b, 2);
172                 }
173
174                 complen = strcspn(s, ".");
175
176                 /* we need to make sure the length fits into 6 bytes */
177                 if (complen >= 0x3F) {
178                         return ndr_push_error(ndr, NDR_ERR_STRING,
179                                               "component length %u[%08X] > 0x00003F",
180                                               (unsigned)complen, (unsigned)complen);
181                 }
182
183                 compname = talloc_asprintf(ndr, "%c%*.*s",
184                                                 (unsigned char)complen,
185                                                 (unsigned char)complen,
186                                                 (unsigned char)complen, s);
187                 NDR_ERR_HAVE_NO_MEMORY(compname);
188
189                 /* remember the current componemt + the rest of the string
190                  * so it can be reused later
191                  */
192                 NDR_CHECK(ndr_token_store(ndr, &ndr->nbt_string_list, s, ndr->offset));
193
194                 /* push just this component into the blob */
195                 NDR_CHECK(ndr_push_bytes(ndr, (const uint8_t *)compname, complen+1));
196                 talloc_free(compname);
197
198                 s += complen;
199                 if (*s == '.') s++;
200         }
201
202         /* if we reach the end of the string and have pushed the last component
203          * without using a label pointer, we need to terminate the string
204          */
205         return ndr_push_bytes(ndr, (const uint8_t *)"", 1);
206 }
207
208
209 /*
210   decompress a 'compressed' name component
211  */
212 static bool decompress_name(char *name, enum nbt_name_type *type)
213 {
214         int i;
215         for (i=0;name[2*i];i++) {
216                 uint8_t c1 = name[2*i];
217                 uint8_t c2 = name[1+(2*i)];
218                 if (c1 < 'A' || c1 > 'P' ||
219                     c2 < 'A' || c2 > 'P') {
220                         return false;
221                 }
222                 name[i] = ((c1-'A')<<4) | (c2-'A');                 
223         }
224         name[i] = 0;
225         if (i == 16) {
226                 *type = (enum nbt_name_type)(name[15]);
227                 name[15] = 0;
228                 i--;
229         } else {
230                 *type = NBT_NAME_CLIENT;
231         }
232
233         /* trim trailing spaces */
234         for (;i>0 && name[i-1]==' ';i--) {
235                 name[i-1] = 0;
236         }
237         
238         return true;
239 }
240
241
242 /*
243   compress a name component
244  */
245 static uint8_t *compress_name(TALLOC_CTX *mem_ctx, 
246                               const uint8_t *name, enum nbt_name_type type)
247 {
248         uint8_t *cname;
249         int i;
250         uint8_t pad_char;
251
252         if (strlen((const char *)name) > 15) {
253                 return NULL;
254         }
255
256         cname = talloc_array(mem_ctx, uint8_t, 33);
257         if (cname == NULL) return NULL;
258
259         for (i=0;name[i];i++) {
260                 cname[2*i]   = 'A' + (name[i]>>4);
261                 cname[1+2*i] = 'A' + (name[i]&0xF);
262         }
263         if (strcmp((const char *)name, "*") == 0) {
264                 pad_char = 0;
265         } else {
266                 pad_char = ' ';
267         }
268         for (;i<15;i++) {
269                 cname[2*i]   = 'A' + (pad_char>>4);
270                 cname[1+2*i] = 'A' + (pad_char&0xF);
271         }
272
273         pad_char = type;
274         cname[2*i]   = 'A' + (pad_char>>4);
275         cname[1+2*i] = 'A' + (pad_char&0xF);
276
277         cname[32] = 0;
278         return cname;
279 }
280
281
282 /**
283   pull a nbt name from the wire
284 */
285 _PUBLIC_ enum ndr_err_code ndr_pull_nbt_name(struct ndr_pull *ndr, int ndr_flags, struct nbt_name *r)
286 {
287         uint8_t *scope;
288         char *cname;
289         const char *s;
290         bool ok;
291
292         if (!(ndr_flags & NDR_SCALARS)) {
293                 return NDR_ERR_SUCCESS;
294         }
295
296         NDR_CHECK(ndr_pull_nbt_string(ndr, ndr_flags, &s));
297
298         scope = (uint8_t *)strchr(s, '.');
299         if (scope) {
300                 *scope = 0;
301                 r->scope = talloc_strdup(ndr->current_mem_ctx, (const char *)&scope[1]);
302                 NDR_ERR_HAVE_NO_MEMORY(r->scope);
303         } else {
304                 r->scope = NULL;
305         }
306
307         cname = discard_const_p(char, s);
308
309         /* the first component is limited to 16 bytes in the DOS charset,
310            which is 32 in the 'compressed' form */
311         if (strlen(cname) > 32) {
312                 return ndr_pull_error(ndr, NDR_ERR_STRING,
313                                       "NBT NAME cname > 32");
314         }
315
316         /* decompress the first component */
317         ok = decompress_name(cname, &r->type);
318         if (!ok) {
319                 return ndr_pull_error(ndr, NDR_ERR_STRING,
320                                       "NBT NAME failed to decompress");
321         }
322
323         r->name = talloc_strdup(ndr->current_mem_ctx, cname);
324         NDR_ERR_HAVE_NO_MEMORY(r->name);
325
326         talloc_free(cname);
327
328         return NDR_ERR_SUCCESS;
329 }
330
331 /**
332   push a nbt name to the wire
333 */
334 _PUBLIC_ enum ndr_err_code ndr_push_nbt_name(struct ndr_push *ndr, int ndr_flags, const struct nbt_name *r)
335 {
336         uint8_t *cname, *fullname;
337         enum ndr_err_code ndr_err;
338
339         if (!(ndr_flags & NDR_SCALARS)) {
340                 return NDR_ERR_SUCCESS;
341         }
342
343         if (strlen(r->name) > 15) {
344                 return ndr_push_error(ndr, NDR_ERR_STRING,
345                                       "nbt_name longer as 15 chars: %s",
346                                       r->name);
347         }
348
349         cname = compress_name(ndr, (const uint8_t *)r->name, r->type);
350         NDR_ERR_HAVE_NO_MEMORY(cname);
351
352         if (r->scope) {
353                 fullname = (uint8_t *)talloc_asprintf(ndr, "%s.%s", cname, r->scope);
354                 NDR_ERR_HAVE_NO_MEMORY(fullname);
355                 talloc_free(cname);
356         } else {
357                 fullname = cname;
358         }
359         
360         ndr_err = ndr_push_nbt_string(ndr, ndr_flags, (const char *)fullname);
361
362         return ndr_err;
363 }
364
365
366 /**
367   copy a nbt name structure
368 */
369 _PUBLIC_ NTSTATUS nbt_name_dup(TALLOC_CTX *mem_ctx, struct nbt_name *name, struct nbt_name *newname)
370 {
371         *newname = *name;
372         newname->name = talloc_strdup(mem_ctx, newname->name);
373         NT_STATUS_HAVE_NO_MEMORY(newname->name);
374         newname->scope = talloc_strdup(mem_ctx, newname->scope);
375         if (name->scope) {
376                 NT_STATUS_HAVE_NO_MEMORY(newname->scope);
377         }
378         return NT_STATUS_OK;
379 }
380
381 /**
382   push a nbt name into a blob
383 */
384 _PUBLIC_ NTSTATUS nbt_name_to_blob(TALLOC_CTX *mem_ctx, struct smb_iconv_convenience *iconv_convenience, DATA_BLOB *blob, struct nbt_name *name)
385 {
386         enum ndr_err_code ndr_err;
387
388         ndr_err = ndr_push_struct_blob(blob, mem_ctx, iconv_convenience, name, (ndr_push_flags_fn_t)ndr_push_nbt_name);
389         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
390                 return ndr_map_error2ntstatus(ndr_err);
391         }
392
393         return NT_STATUS_OK;
394 }
395
396 /**
397   pull a nbt name from a blob
398 */
399 _PUBLIC_ NTSTATUS nbt_name_from_blob(TALLOC_CTX *mem_ctx, const DATA_BLOB *blob, struct nbt_name *name)
400 {
401         enum ndr_err_code ndr_err;
402
403         ndr_err = ndr_pull_struct_blob(blob, mem_ctx, NULL, name,
404                                        (ndr_pull_flags_fn_t)ndr_pull_nbt_name);
405         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
406                 return ndr_map_error2ntstatus(ndr_err);
407         }
408
409         return NT_STATUS_OK;
410 }
411
412
413 /**
414   choose a name to use when calling a server in a NBT session request.
415   we use heuristics to see if the name we have been given is a IP
416   address, or a too-long name. If it is then use *SMBSERVER, or a
417   truncated name
418 */
419 _PUBLIC_ void nbt_choose_called_name(TALLOC_CTX *mem_ctx,
420                             struct nbt_name *n, const char *name, int type)
421 {
422         n->scope = NULL;
423         n->type = type;
424
425         if (is_ipaddress(name) || name == NULL) {
426                 n->name = "*SMBSERVER";
427                 return;
428         }
429         if (strlen(name) > 15) {
430                 const char *p = strchr(name, '.');
431                 char *s;
432                 if (p - name > 15) {
433                         n->name = "*SMBSERVER";
434                         return;
435                 }
436                 s = talloc_strndup(mem_ctx, name, PTR_DIFF(p, name));
437                 n->name = strupper_talloc(mem_ctx, s);
438                 return;
439         }
440
441         n->name = strupper_talloc(mem_ctx, name);
442 }
443
444
445 /*
446   escape a string into a form containing only a small set of characters,
447   the rest is hex encoded. This is similar to URL encoding
448 */
449 static const char *nbt_hex_encode(TALLOC_CTX *mem_ctx, const char *s)
450 {
451         int i, len;
452         char *ret;
453         const char *valid_chars = "_-.$@ ";
454 #define NBT_CHAR_ALLOW(c) (isalnum((unsigned char)c) || strchr(valid_chars, c))
455
456         for (len=i=0;s[i];i++,len++) {
457                 if (!NBT_CHAR_ALLOW(s[i])) {
458                         len += 2;
459                 }
460         }
461
462         ret = talloc_array(mem_ctx, char, len+1);
463         if (ret == NULL) return NULL;
464
465         for (len=i=0;s[i];i++) {
466                 if (NBT_CHAR_ALLOW(s[i])) {
467                         ret[len++] = s[i];
468                 } else {
469                         snprintf(&ret[len], 4, "%%%02x", (unsigned char)s[i]);
470                         len += 3;
471                 }
472         }
473         ret[len] = 0;
474
475         return ret;
476 }
477
478
479 /**
480   form a string for a NBT name
481 */
482 _PUBLIC_ char *nbt_name_string(TALLOC_CTX *mem_ctx, const struct nbt_name *name)
483 {
484         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
485         char *ret;
486         if (name->scope) {              
487                 ret = talloc_asprintf(mem_ctx, "%s<%02x>-%s",
488                                       nbt_hex_encode(tmp_ctx, name->name),
489                                       name->type, 
490                                       nbt_hex_encode(tmp_ctx, name->scope));
491         } else {
492                 ret = talloc_asprintf(mem_ctx, "%s<%02x>", 
493                                       nbt_hex_encode(tmp_ctx, name->name), 
494                                       name->type);
495         }
496         talloc_free(tmp_ctx);
497         return ret;
498 }
499
500 /**
501   pull a nbt name, WINS Replication uses another on wire format for nbt name
502 */
503 _PUBLIC_ enum ndr_err_code ndr_pull_wrepl_nbt_name(struct ndr_pull *ndr, int ndr_flags, const struct nbt_name **_r)
504 {
505         struct nbt_name *r;
506         uint8_t *namebuf;
507         uint32_t namebuf_len;
508
509         if (!(ndr_flags & NDR_SCALARS)) {
510                 return NDR_ERR_SUCCESS;
511         }
512
513         NDR_CHECK(ndr_pull_align(ndr, 4));
514         NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &namebuf_len));
515         if (namebuf_len < 1 || namebuf_len > 255) {
516                 return ndr_pull_error(ndr, NDR_ERR_ALLOC, "value out of range");
517         }
518         NDR_PULL_ALLOC_N(ndr, namebuf, namebuf_len);
519         NDR_CHECK(ndr_pull_array_uint8(ndr, NDR_SCALARS, namebuf, namebuf_len));
520
521         NDR_PULL_ALLOC(ndr, r); 
522
523         /* oh wow, what a nasty bug in windows ... */
524         if (namebuf[0] == 0x1b && namebuf_len >= 16) {
525                 namebuf[0] = namebuf[15];
526                 namebuf[15] = 0x1b;
527         }
528
529         if (namebuf_len < 17) {
530                 r->type = 0x00;
531
532                 r->name = talloc_strndup(r, (char *)namebuf, namebuf_len);
533                 if (!r->name) return ndr_pull_error(ndr, NDR_ERR_ALLOC, "out of memory");
534
535                 r->scope= NULL;
536
537                 talloc_free(namebuf);
538                 *_r = r;
539                 return NDR_ERR_SUCCESS;
540         }
541
542         r->type = namebuf[15];
543
544         namebuf[15] = '\0';
545         trim_string((char *)namebuf, NULL, " ");
546         r->name = talloc_strdup(r, (char *)namebuf);
547         if (!r->name) return ndr_pull_error(ndr, NDR_ERR_ALLOC, "out of memory");
548
549         if (namebuf_len > 18) {
550                 r->scope = talloc_strndup(r, (char *)(namebuf+17), namebuf_len-17);
551                 if (!r->scope) return ndr_pull_error(ndr, NDR_ERR_ALLOC, "out of memory");
552         } else {
553                 r->scope = NULL;
554         }
555
556         talloc_free(namebuf);
557         *_r = r;
558         return NDR_ERR_SUCCESS;
559 }
560
561 /**
562   push a nbt name, WINS Replication uses another on wire format for nbt name
563 */
564 _PUBLIC_ enum ndr_err_code ndr_push_wrepl_nbt_name(struct ndr_push *ndr, int ndr_flags, const struct nbt_name *r)
565 {
566         uint8_t *namebuf;
567         uint32_t namebuf_len;
568         uint32_t name_len;
569         uint32_t scope_len = 0;
570
571         if (r == NULL) {
572                 return ndr_push_error(ndr, NDR_ERR_INVALID_POINTER,
573                                       "wrepl_nbt_name NULL pointer");
574         }
575
576         if (!(ndr_flags & NDR_SCALARS)) {
577                 return NDR_ERR_SUCCESS;
578         }
579
580         name_len = strlen(r->name);
581         if (name_len > 15) {
582                 return ndr_push_error(ndr, NDR_ERR_STRING,
583                                       "wrepl_nbt_name longer as 15 chars: %s",
584                                       r->name);
585         }
586
587         if (r->scope) {
588                 scope_len = strlen(r->scope);
589         }
590         if (scope_len > 238) {
591                 return ndr_push_error(ndr, NDR_ERR_STRING,
592                                       "wrepl_nbt_name scope longer as 238 chars: %s",
593                                       r->scope);
594         }
595
596         namebuf = (uint8_t *)talloc_asprintf(ndr, "%-15s%c%s",
597                                              r->name, 'X',
598                                              (r->scope?r->scope:""));
599         if (!namebuf) return ndr_push_error(ndr, NDR_ERR_ALLOC, "out of memory");
600
601         namebuf_len = strlen((char *)namebuf) + 1;
602
603         /*
604          * we need to set the type here, and use a place-holder in the talloc_asprintf()
605          * as the type can be 0x00, and then the namebuf_len = strlen(namebuf); would give wrong results
606          */
607         namebuf[15] = r->type;
608
609         /* oh wow, what a nasty bug in windows ... */
610         if (r->type == 0x1b) {
611                 namebuf[15] = namebuf[0];
612                 namebuf[0] = 0x1b;
613         }
614
615         NDR_CHECK(ndr_push_align(ndr, 4));
616         NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, namebuf_len));
617         NDR_CHECK(ndr_push_array_uint8(ndr, NDR_SCALARS, namebuf, namebuf_len));
618
619         talloc_free(namebuf);
620         return NDR_ERR_SUCCESS;
621 }
622
623 _PUBLIC_ void ndr_print_wrepl_nbt_name(struct ndr_print *ndr, const char *name, const struct nbt_name *r)
624 {
625         char *s = nbt_name_string(ndr, r);
626         ndr_print_string(ndr, name, s);
627         talloc_free(s);
628 }