r7436: As far as I see it, these are the last two "real" ones for Samba4 on AIX to
[kamenim/samba.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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 /*
24   see rfc1002 for the detailed format of compressed names
25 */
26
27 #include "includes.h"
28 #include "system/iconv.h"
29 #include "librpc/gen_ndr/ndr_nbt.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 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, &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 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 = 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 NTSTATUS ndr_push_nbt_string(struct ndr_push *ndr, int ndr_flags, const char *s)
138 {
139         int i;
140         int fulllen;
141         char *fullname;
142
143         if (!(ndr_flags & NDR_SCALARS)) {
144                 return NT_STATUS_OK;
145         }
146
147         if (s == NULL || *s == 0) {
148                 return ndr_push_bytes(ndr, "", 1);
149         }
150
151
152         fullname = talloc_strdup(ndr, "");
153         NT_STATUS_HAVE_NO_MEMORY(fullname);
154
155         while (*s) {
156                 int len = strcspn(s, ".");
157                 fullname = talloc_asprintf_append(fullname, "%c%*.*s",
158                                                   (unsigned char)len,
159                                                   (unsigned char)len,
160                                                   (unsigned char)len, s);
161                 NT_STATUS_HAVE_NO_MEMORY(fullname);
162                 s += len;
163                 if (*s == '.') s++;
164         }
165
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) {
173                         uint8_t b[2];
174                         talloc_free(fullname);
175                         b[0] = 0xC0 | (i>>8);
176                         b[1] = (i&0xFF);
177                         return ndr_push_bytes(ndr, b, 2);
178                 }
179         }
180
181         NDR_CHECK(ndr_push_bytes(ndr, fullname, fulllen));
182
183         talloc_free(fullname);
184
185         return NT_STATUS_OK;
186 }
187
188
189 /*
190   decompress a 'compressed' name component
191  */
192 static NTSTATUS decompress_name(char *name, enum nbt_name_type *type)
193 {
194         int i;
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;
201                 }
202                 name[i] = ((c1-'A')<<4) | (c2-'A');                 
203         }
204         name[i] = 0;
205         if (i == 16) {
206                 *type = (enum nbt_name_type)(name[15]);
207                 name[15] = 0;
208                 i--;
209         } else {
210                 *type = NBT_NAME_CLIENT;
211         }
212
213         /* trim trailing spaces */
214         for (;i>0 && name[i-1]==' ';i--) {
215                 name[i-1] = 0;
216         }
217         
218         return NT_STATUS_OK;
219 }
220
221
222 /*
223   compress a name component
224  */
225 static uint8_t *compress_name(TALLOC_CTX *mem_ctx, 
226                               const uint8_t *name, enum nbt_name_type type)
227 {
228         uint8_t *cname;
229         int i;
230         uint8_t pad_char;
231
232         if (strlen(name) > 15) {
233                 return NULL;
234         }
235
236         cname = talloc_array(mem_ctx, uint8_t, 33);
237         if (cname == NULL) return NULL;
238
239         for (i=0;name[i];i++) {
240                 cname[2*i]   = 'A' + (name[i]>>4);
241                 cname[1+2*i] = 'A' + (name[i]&0xF);
242         }
243         if (strcmp(name, "*") == 0) {
244                 pad_char = 0;
245         } else {
246                 pad_char = ' ';
247         }
248         for (;i<15;i++) {
249                 cname[2*i]   = 'A' + (pad_char>>4);
250                 cname[1+2*i] = 'A' + (pad_char&0xF);
251         }
252
253         pad_char = type;
254         cname[2*i]   = 'A' + (pad_char>>4);
255         cname[1+2*i] = 'A' + (pad_char&0xF);
256
257         cname[32] = 0;
258         return cname;
259 }
260
261
262 /*
263   pull a nbt name from the wire
264 */
265 NTSTATUS ndr_pull_nbt_name(struct ndr_pull *ndr, int ndr_flags, struct nbt_name *r)
266 {
267         NTSTATUS status;
268         uint8_t *scope;
269         char *cname;
270         const char *s;
271
272         if (!(ndr_flags & NDR_SCALARS)) {
273                 return NT_STATUS_OK;
274         }
275
276         status = ndr_pull_nbt_string(ndr, ndr_flags, &s);
277         NT_STATUS_NOT_OK_RETURN(status);
278
279         scope = strchr(s, '.');
280         if (scope) {
281                 *scope = 0;
282                 r->scope = talloc_strdup(ndr, scope+1);
283                 NT_STATUS_HAVE_NO_MEMORY(r->scope);
284         } else {
285                 r->scope = NULL;
286         }
287
288         cname = discard_const_p(char, s);
289
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;
294         }
295
296         /* decompress the first component */
297         status = decompress_name(cname, &r->type);
298         NT_STATUS_NOT_OK_RETURN(status);
299
300         r->name = talloc_strdup(ndr, cname);
301         NT_STATUS_HAVE_NO_MEMORY(r->name);
302
303         talloc_free(cname);
304
305         return NT_STATUS_OK;
306 }
307
308 /*
309   push a nbt name to the wire
310 */
311 NTSTATUS ndr_push_nbt_name(struct ndr_push *ndr, int ndr_flags, struct nbt_name *r)
312 {
313         uint8_t *cname, *fullname;
314         NTSTATUS status;
315
316         if (!(ndr_flags & NDR_SCALARS)) {
317                 return NT_STATUS_OK;
318         }
319
320         cname = compress_name(ndr, r->name, r->type);
321         NT_STATUS_HAVE_NO_MEMORY(cname);
322
323         if (r->scope) {
324                 fullname = talloc_asprintf(ndr, "%s.%s", cname, r->scope);
325                 NT_STATUS_HAVE_NO_MEMORY(fullname);
326                 talloc_free(cname);
327         } else {
328                 fullname = cname;
329         }
330         
331         status = ndr_push_nbt_string(ndr, ndr_flags, fullname);
332         talloc_free(fullname);
333
334         return status;
335 }
336
337
338 /*
339   copy a nbt name structure
340 */
341 NTSTATUS nbt_name_dup(TALLOC_CTX *mem_ctx, struct nbt_name *name, struct nbt_name *newname)
342 {
343         *newname = *name;
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);
347         if (name->scope) {
348                 NT_STATUS_HAVE_NO_MEMORY(newname->scope);
349         }
350         return NT_STATUS_OK;
351 }
352
353 /*
354   push a nbt name into a blob
355 */
356 NTSTATUS nbt_name_to_blob(TALLOC_CTX *mem_ctx, DATA_BLOB *blob, struct nbt_name *name)
357 {
358         return ndr_push_struct_blob(blob, mem_ctx, name, 
359                                     (ndr_push_flags_fn_t)ndr_push_nbt_name);
360 }
361
362
363 /*
364   pull a nbt name from a blob
365 */
366 NTSTATUS nbt_name_from_blob(TALLOC_CTX *mem_ctx, const DATA_BLOB *blob, struct nbt_name *name)
367 {
368         return ndr_pull_struct_blob(blob, mem_ctx, name, 
369                                     (ndr_pull_flags_fn_t)ndr_pull_nbt_name);
370 }
371
372
373 /*
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
377   truncated name
378 */
379 void nbt_choose_called_name(TALLOC_CTX *mem_ctx,
380                             struct nbt_name *n, const char *name, int type)
381 {
382         n->scope = NULL;
383         n->type = type;
384
385         if (is_ipaddress(name)) {
386                 n->name = "*SMBSERVER";
387                 return;
388         }
389         if (strlen(name) > 15) {
390                 const char *p = strchr(name, '.');
391                 char *s;
392                 if (p - name > 15) {
393                         n->name = "*SMBSERVER";
394                         return;
395                 }
396                 s = talloc_strndup(mem_ctx, name, PTR_DIFF(p, name));
397                 n->name = strupper_talloc(mem_ctx, s);
398                 return;
399         }
400
401         n->name = strupper_talloc(mem_ctx, name);
402 }
403
404
405 /*
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
408 */
409 static const char *nbt_hex_encode(TALLOC_CTX *mem_ctx, const char *s)
410 {
411         int i, len;
412         char *ret;
413         const char *valid_chars = "_-.$@ ";
414 #define NBT_CHAR_ALLOW(c) (isalnum(c) || strchr(valid_chars, c))
415
416         for (len=i=0;s[i];i++,len++) {
417                 if (!NBT_CHAR_ALLOW(s[i])) {
418                         len += 2;
419                 }
420         }
421
422         ret = talloc_array(mem_ctx, char, len+1);
423         if (ret == NULL) return NULL;
424
425         for (len=i=0;s[i];i++) {
426                 if (NBT_CHAR_ALLOW(s[i])) {
427                         ret[len++] = s[i];
428                 } else {
429                         snprintf(&ret[len], 4, "%%%02x", (unsigned char)s[i]);
430                         len += 3;
431                 }
432         }
433         ret[len] = 0;
434
435         return ret;
436 }
437
438
439 /*
440   form a string for a NBT name
441 */
442 char *nbt_name_string(TALLOC_CTX *mem_ctx, const struct nbt_name *name)
443 {
444         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
445         char *ret;
446         if (name->scope) {              
447                 ret = talloc_asprintf(mem_ctx, "%s<%02x>-%s",
448                                       nbt_hex_encode(tmp_ctx, name->name),
449                                       name->type, 
450                                       nbt_hex_encode(tmp_ctx, name->scope));
451         } else {
452                 ret = talloc_asprintf(mem_ctx, "%s<%02x>", 
453                                       nbt_hex_encode(tmp_ctx, name->name), 
454                                       name->type);
455         }
456         talloc_free(tmp_ctx);
457         return ret;
458 }
459