12b8884e2d5c98bcaf0262a032cb528ccf99d783
[samba-svnmirror.git] / source / 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   pull one component of a compressed name
36 */
37 static NTSTATUS ndr_pull_component(struct ndr_pull *ndr, uint8_t **component,
38                                    uint32_t *offset, uint32_t *max_offset)
39 {
40         uint8_t len;
41         uint_t loops = 0;
42         while (loops < 5) {
43                 if (*offset >= ndr->data_size) {
44                         return NT_STATUS_BAD_NETWORK_NAME;
45                 }
46                 len = ndr->data[*offset];
47                 if (len == 0) {
48                         *offset += 1;
49                         *max_offset = MAX(*max_offset, *offset);
50                         *component = NULL;
51                         return NT_STATUS_OK;
52                 }
53                 if ((len & 0xC0) == 0xC0) {
54                         /* its a label pointer */
55                         if (1 + *offset >= ndr->data_size) {
56                                 return NT_STATUS_BAD_NETWORK_NAME;
57                         }
58                         *max_offset = MAX(*max_offset, *offset + 2);
59                         *offset = ((len&0x3F)<<8) | ndr->data[1 + *offset];
60                         *max_offset = MAX(*max_offset, *offset);
61                         loops++;
62                         continue;
63                 }
64                 if ((len & 0xC0) != 0) {
65                         /* its a reserved length field */
66                         return NT_STATUS_BAD_NETWORK_NAME;
67                 }
68                 if (*offset + len + 2 > ndr->data_size) {
69                         return NT_STATUS_BAD_NETWORK_NAME;
70                 }
71                 *component = (uint8_t*)talloc_strndup(ndr, &ndr->data[1 + *offset], len);
72                 NT_STATUS_HAVE_NO_MEMORY(*component);
73                 *offset += len + 1;
74                 *max_offset = MAX(*max_offset, *offset);
75                 return NT_STATUS_OK;
76         }
77
78         /* too many pointers */
79         return NT_STATUS_BAD_NETWORK_NAME;
80 }
81
82 /*
83   decompress a 'compressed' name component
84  */
85 static NTSTATUS decompress_name(char *name, enum nbt_name_type *type)
86 {
87         int i;
88         for (i=0;name[2*i];i++) {
89                 uint8_t c1 = name[2*i];
90                 uint8_t c2 = name[1+(2*i)];
91                 if (c1 < 'A' || c1 > 'P' ||
92                     c2 < 'A' || c2 > 'P') {
93                         return NT_STATUS_BAD_NETWORK_NAME;
94                 }
95                 name[i] = ((c1-'A')<<4) | (c2-'A');                 
96         }
97         name[i] = 0;
98         if (i == 16) {
99                 *type = (enum nbt_name_type)(name[15]);
100                 name[15] = 0;
101                 i--;
102         } else {
103                 *type = NBT_NAME_CLIENT;
104         }
105
106         /* trim trailing spaces */
107         for (;i>0 && name[i-1]==' ';i--) {
108                 name[i-1] = 0;
109         }
110         
111         return NT_STATUS_OK;
112 }
113
114
115 /*
116   compress a name component
117  */
118 static uint8_t *compress_name(TALLOC_CTX *mem_ctx, 
119                               const uint8_t *name, enum nbt_name_type type)
120 {
121         uint8_t *cname;
122         int i;
123         uint8_t pad_char;
124
125         if (strlen(name) > 15) {
126                 return NULL;
127         }
128
129         cname = talloc_array(mem_ctx, uint8_t, 33);
130         if (cname == NULL) return NULL;
131
132         for (i=0;name[i];i++) {
133                 cname[2*i]   = 'A' + (name[i]>>4);
134                 cname[1+2*i] = 'A' + (name[i]&0xF);
135         }
136         if (strcmp(name, "*") == 0) {
137                 pad_char = 0;
138         } else {
139                 pad_char = ' ';
140         }
141         for (;i<15;i++) {
142                 cname[2*i]   = 'A' + (pad_char>>4);
143                 cname[1+2*i] = 'A' + (pad_char&0xF);
144         }
145
146         pad_char = type;
147         cname[2*i]   = 'A' + (pad_char>>4);
148         cname[1+2*i] = 'A' + (pad_char&0xF);
149
150         cname[32] = 0;
151         return cname;
152 }
153
154 /*
155   pull a nbt name from the wire
156 */
157 NTSTATUS ndr_pull_nbt_name(struct ndr_pull *ndr, int ndr_flags, struct nbt_name *r)
158 {
159         NTSTATUS status;
160         uint_t num_components;
161         uint32_t offset = ndr->offset;
162         uint32_t max_offset = offset;
163         uint8_t *components[MAX_COMPONENTS];
164         int i;
165         uint8_t *scope;
166
167         if (!(ndr_flags & NDR_SCALARS)) {
168                 return NT_STATUS_OK;
169         }
170
171         /* break up name into a list of components */
172         for (num_components=0;num_components<MAX_COMPONENTS;num_components++) {
173                 status = ndr_pull_component(ndr, &components[num_components], 
174                                             &offset, &max_offset);
175                 NT_STATUS_NOT_OK_RETURN(status);
176                 if (components[num_components] == NULL) break;
177         }
178         if (num_components == MAX_COMPONENTS ||
179             num_components == 0) {
180                 return NT_STATUS_BAD_NETWORK_NAME;
181         }
182
183         ndr->offset = max_offset;
184
185         /* the first component is limited to 16 bytes in the DOS charset,
186            which is 32 in the 'compressed' form */
187         if (strlen(components[0]) > 32) {
188                 return NT_STATUS_BAD_NETWORK_NAME;
189         }
190
191         /* decompress the first component */
192         status = decompress_name(components[0], &r->type);
193         NT_STATUS_NOT_OK_RETURN(status);
194
195         r->name = components[0];
196
197         /* combine the remaining components into the scope */
198         scope = components[1];
199         for (i=2;i<num_components;i++) {
200                 scope = talloc_asprintf_append(scope, ".%s", components[i]);
201                 NT_STATUS_HAVE_NO_MEMORY(scope);
202         }
203
204         r->scope = scope;
205
206         return NT_STATUS_OK;
207 }
208
209 /*
210   push a nbt name to the wire
211 */
212 NTSTATUS ndr_push_nbt_name(struct ndr_push *ndr, int ndr_flags, struct nbt_name *r)
213 {
214         uint_t num_components;
215         uint8_t *components[MAX_COMPONENTS];
216         char *dscope=NULL, *p;
217         uint8_t *cname, *fullname;
218         int i;
219         int fulllen;
220
221         if (!(ndr_flags & NDR_SCALARS)) {
222                 return NT_STATUS_OK;
223         }
224
225         if (r->scope) {
226                 dscope = talloc_strdup(ndr, r->scope);
227                 NT_STATUS_HAVE_NO_MEMORY(dscope);
228         }
229
230         cname = compress_name(ndr, r->name, r->type);
231         NT_STATUS_HAVE_NO_MEMORY(cname);
232
233         /* form the base components */
234         components[0] = cname;
235         num_components = 1;
236
237         while (dscope && (p=strchr(dscope, '.')) && 
238                num_components < MAX_COMPONENTS) {
239                 *p = 0;
240                 components[num_components] = dscope;
241                 dscope = p+1;
242                 num_components++;
243         }
244         if (dscope && num_components < MAX_COMPONENTS) {
245                 components[num_components++] = dscope;
246         }
247         if (num_components == MAX_COMPONENTS) {
248                 return NT_STATUS_BAD_NETWORK_NAME;
249         }
250
251         fullname = talloc_asprintf(ndr, "%c%s", (unsigned char)strlen(cname), cname);
252         NT_STATUS_HAVE_NO_MEMORY(fullname);
253                 
254         for (i=1;i<num_components;i++) {
255                 fullname = talloc_asprintf_append(fullname, "%c%s", 
256                                                   (unsigned char)strlen(components[i]), components[i]);
257                 NT_STATUS_HAVE_NO_MEMORY(fullname);
258         }
259
260         /* see if we can find the fullname in the existing packet - if
261            so, we can use a NBT name pointer. This allows us to fit
262            longer names into the packet */
263         fulllen = strlen(fullname)+1;
264         for (i=0;i + fulllen < ndr->offset;i++) {
265                 if (ndr->data[i] == fullname[0] &&
266                     memcmp(fullname, &ndr->data[i], fulllen) == 0) {
267                         talloc_free(fullname);
268                         return ndr_push_uint16(ndr, NDR_SCALARS, 0xC000 | i);
269                 }
270         }
271
272         NDR_CHECK(ndr_push_bytes(ndr, fullname, fulllen));
273
274         talloc_free(fullname);
275
276         return NT_STATUS_OK;
277 }
278
279
280 /*
281   copy a nbt name structure
282 */
283 NTSTATUS nbt_name_dup(TALLOC_CTX *mem_ctx, struct nbt_name *name, struct nbt_name *newname)
284 {
285         *newname = *name;
286         newname->name = talloc_strdup(mem_ctx, newname->name);
287         NT_STATUS_HAVE_NO_MEMORY(newname->name);
288         newname->scope = talloc_strdup(mem_ctx, newname->scope);
289         if (name->scope) {
290                 NT_STATUS_HAVE_NO_MEMORY(newname->scope);
291         }
292         return NT_STATUS_OK;
293 }
294
295 /*
296   push a nbt name into a blob
297 */
298 NTSTATUS nbt_name_to_blob(TALLOC_CTX *mem_ctx, DATA_BLOB *blob, struct nbt_name *name)
299 {
300         return ndr_push_struct_blob(blob, mem_ctx, name, 
301                                     (ndr_push_flags_fn_t)ndr_push_nbt_name);
302 }
303
304
305 /*
306   pull a nbt name from a blob
307 */
308 NTSTATUS nbt_name_from_blob(TALLOC_CTX *mem_ctx, const DATA_BLOB *blob, struct nbt_name *name)
309 {
310         return ndr_pull_struct_blob(blob, mem_ctx, name, 
311                                     (ndr_pull_flags_fn_t)ndr_pull_nbt_name);
312 }
313
314
315 /*
316   choose a name to use when calling a server in a NBT session request.
317   we use heuristics to see if the name we have been given is a IP
318   address, or a too-long name. If it is then use *SMBSERVER, or a
319   truncated name
320 */
321 void nbt_choose_called_name(TALLOC_CTX *mem_ctx,
322                             struct nbt_name *n, const char *name, int type)
323 {
324         n->scope = NULL;
325         n->type = type;
326
327         if (is_ipaddress(name)) {
328                 n->name = "*SMBSERVER";
329                 return;
330         }
331         if (strlen(name) > 15) {
332                 const char *p = strchr(name, '.');
333                 char *s;
334                 if (p - name > 15) {
335                         n->name = "*SMBSERVER";
336                         return;
337                 }
338                 s = talloc_strndup(mem_ctx, name, PTR_DIFF(p, name));
339                 n->name = strupper_talloc(mem_ctx, s);
340                 return;
341         }
342
343         n->name = strupper_talloc(mem_ctx, name);
344 }
345
346
347 /*
348   escape a string into a form containing only a small set of characters,
349   the rest is hex encoded. This is similar to URL encoding
350 */
351 static const char *nbt_hex_encode(TALLOC_CTX *mem_ctx, const char *s)
352 {
353         int i, len;
354         char *ret;
355         const char *valid_chars = "_-.$@ ";
356 #define NBT_CHAR_ALLOW(c) (isalnum(c) || strchr(valid_chars, c))
357
358         for (len=i=0;s[i];i++,len++) {
359                 if (!NBT_CHAR_ALLOW(s[i])) {
360                         len += 2;
361                 }
362         }
363
364         ret = talloc_array(mem_ctx, char, len+1);
365         if (ret == NULL) return NULL;
366
367         for (len=i=0;s[i];i++) {
368                 if (NBT_CHAR_ALLOW(s[i])) {
369                         ret[len++] = s[i];
370                 } else {
371                         snprintf(&ret[len], 4, "%%%02x", (unsigned char)s[i]);
372                         len += 3;
373                 }
374         }
375         ret[len] = 0;
376
377         return ret;
378 }
379
380
381 /*
382   form a string for a NBT name
383 */
384 char *nbt_name_string(TALLOC_CTX *mem_ctx, const struct nbt_name *name)
385 {
386         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
387         char *ret;
388         if (name->scope) {              
389                 ret = talloc_asprintf(mem_ctx, "%s<%02x>-%s",
390                                       nbt_hex_encode(tmp_ctx, name->name),
391                                       name->type, 
392                                       nbt_hex_encode(tmp_ctx, name->scope));
393         } else {
394                 ret = talloc_asprintf(mem_ctx, "%s<%02x>", 
395                                       nbt_hex_encode(tmp_ctx, name->name), 
396                                       name->type);
397         }
398         talloc_free(tmp_ctx);
399         return ret;
400 }
401