r5326: removed the charset conversion from the nbtname code, so we no longer
[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 "librpc/gen_ndr/ndr_nbt.h"
29
30 /* don't allow an unlimited number of name components */
31 #define MAX_COMPONENTS 10
32
33 /*
34   pull one component of a compressed name
35 */
36 static NTSTATUS ndr_pull_component(struct ndr_pull *ndr, uint8_t **component,
37                                    uint32_t *offset, uint32_t *max_offset)
38 {
39         uint8_t len;
40         uint_t loops = 0;
41         while (loops < 5) {
42                 if (*offset >= ndr->data_size) {
43                         return NT_STATUS_BAD_NETWORK_NAME;
44                 }
45                 len = ndr->data[*offset];
46                 if (len == 0) {
47                         *offset += 1;
48                         *max_offset = MAX(*max_offset, *offset);
49                         *component = NULL;
50                         return NT_STATUS_OK;
51                 }
52                 if ((len & 0xC0) == 0xC0) {
53                         /* its a label pointer */
54                         if (1 + *offset >= ndr->data_size) {
55                                 return NT_STATUS_BAD_NETWORK_NAME;
56                         }
57                         *offset = ((len&0x3F)<<8) | ndr->data[1 + *offset];
58                         *max_offset = MAX(*max_offset, *offset + 1);
59                         loops++;
60                         continue;
61                 }
62                 if ((len & 0xC0) != 0) {
63                         /* its a reserved length field */
64                         return NT_STATUS_BAD_NETWORK_NAME;
65                 }
66                 if (*offset + len + 2 > ndr->data_size) {
67                         return NT_STATUS_BAD_NETWORK_NAME;
68                 }
69                 *component = (uint8_t*)talloc_strndup(ndr, &ndr->data[1 + *offset], len);
70                 NT_STATUS_HAVE_NO_MEMORY(*component);
71                 *offset += len + 1;
72                 *max_offset = MAX(*max_offset, *offset);
73                 return NT_STATUS_OK;
74         }
75
76         /* too many pointers */
77         return NT_STATUS_BAD_NETWORK_NAME;
78 }
79
80 /*
81   decompress a 'compressed' name component
82  */
83 static NTSTATUS decompress_name(char *name, enum nbt_name_type *type)
84 {
85         int i;
86         for (i=0;name[2*i];i++) {
87                 uint8_t c1 = name[2*i];
88                 uint8_t c2 = name[1+(2*i)];
89                 if (c1 < 'A' || c1 > 'P' ||
90                     c2 < 'A' || c2 > 'P') {
91                         return NT_STATUS_BAD_NETWORK_NAME;
92                 }
93                 name[i] = ((c1-'A')<<4) | (c2-'A');                 
94         }
95         name[i] = 0;
96         if (i == 16) {
97                 *type = (enum nbt_name_type)(name[15]);
98                 name[15] = 0;
99                 i--;
100         } else {
101                 *type = NBT_NAME_CLIENT;
102         }
103
104         /* trim trailing spaces */
105         for (;i>0 && name[i-1]==' ';i--) {
106                 name[i-1] = 0;
107         }
108         
109         return NT_STATUS_OK;
110 }
111
112
113 /*
114   compress a name component
115  */
116 static uint8_t *compress_name(TALLOC_CTX *mem_ctx, 
117                               uint8_t *name, enum nbt_name_type type)
118 {
119         uint8_t *cname;
120         int i;
121         uint8_t pad_char;
122
123         if (strlen(name) > 15) {
124                 return NULL;
125         }
126
127         cname = talloc_array(mem_ctx, uint8_t, 33);
128         if (cname == NULL) return NULL;
129
130         for (i=0;name[i];i++) {
131                 cname[2*i]   = 'A' + (name[i]>>4);
132                 cname[1+2*i] = 'A' + (name[i]&0xF);
133         }
134         if (name[0] == '*') {
135                 pad_char = 0;
136         } else {
137                 pad_char = ' ';
138         }
139         for (;i<15;i++) {
140                 cname[2*i]   = 'A' + (pad_char>>4);
141                 cname[1+2*i] = 'A' + (pad_char&0xF);
142         }
143
144         pad_char = type;
145         cname[2*i]   = 'A' + (pad_char>>4);
146         cname[1+2*i] = 'A' + (pad_char&0xF);
147
148         cname[32] = 0;
149         return cname;
150 }
151
152 /*
153   pull a nbt name from the wire
154 */
155 NTSTATUS ndr_pull_nbt_name(struct ndr_pull *ndr, int ndr_flags, struct nbt_name *r)
156 {
157         NTSTATUS status;
158         uint_t num_components;
159         uint32_t offset = ndr->offset;
160         uint32_t max_offset = offset;
161         uint8_t *components[MAX_COMPONENTS];
162         int i;
163         uint8_t *scope;
164
165         if (!(ndr_flags & NDR_SCALARS)) {
166                 return NT_STATUS_OK;
167         }
168
169         /* break up name into a list of components */
170         for (num_components=0;num_components<MAX_COMPONENTS;num_components++) {
171                 status = ndr_pull_component(ndr, &components[num_components], 
172                                             &offset, &max_offset);
173                 NT_STATUS_NOT_OK_RETURN(status);
174                 if (components[num_components] == NULL) break;
175         }
176         if (num_components == MAX_COMPONENTS ||
177             num_components == 0) {
178                 return NT_STATUS_BAD_NETWORK_NAME;
179         }
180
181         ndr->offset = max_offset;
182
183         /* the first component is limited to 16 bytes in the DOS charset,
184            which is 32 in the 'compressed' form */
185         if (strlen(components[0]) > 32) {
186                 return NT_STATUS_BAD_NETWORK_NAME;
187         }
188
189         /* decompress the first component */
190         status = decompress_name(components[0], &r->type);
191         NT_STATUS_NOT_OK_RETURN(status);
192
193         r->name = components[0];
194
195         /* combine the remaining components into the scope */
196         scope = components[1];
197         for (i=2;i<num_components;i++) {
198                 scope = talloc_asprintf_append(scope, ".%s", components[i]);
199                 NT_STATUS_HAVE_NO_MEMORY(scope);
200         }
201
202         r->scope = scope;
203
204         return NT_STATUS_OK;
205 }
206
207 /*
208   push a nbt name to the wire
209 */
210 NTSTATUS ndr_push_nbt_name(struct ndr_push *ndr, int ndr_flags, struct nbt_name *r)
211 {
212         uint_t num_components;
213         uint8_t *components[MAX_COMPONENTS];
214         char *dname, *dscope=NULL, *p;
215         uint8_t *cname;
216         int i;
217
218         if (!(ndr_flags & NDR_SCALARS)) {
219                 return NT_STATUS_OK;
220         }
221
222         dname = strupper_talloc(ndr, r->name);
223         NT_STATUS_HAVE_NO_MEMORY(dname);
224         if (r->scope) {
225                 dscope = strupper_talloc(ndr, r->scope);
226                 NT_STATUS_HAVE_NO_MEMORY(dscope);
227         }
228
229         cname = compress_name(ndr, dname, r->type);
230         NT_STATUS_HAVE_NO_MEMORY(cname);
231
232         /* form the base components */
233         components[0] = cname;
234         num_components = 1;
235
236         while (dscope && (p=strchr(dscope, '.')) && 
237                num_components < MAX_COMPONENTS) {
238                 *p = 0;
239                 components[num_components] = dscope;
240                 dscope = p+1;
241                 num_components++;
242         }
243         if (dscope && num_components < MAX_COMPONENTS) {
244                 components[num_components++] = dscope;
245         }
246         if (num_components == MAX_COMPONENTS) {
247                 return NT_STATUS_BAD_NETWORK_NAME;
248         }
249                 
250         /* push the components */
251         for (i=0;i<num_components;i++) {
252                 uint8_t len = strlen(components[i]);
253                 NDR_CHECK(ndr_push_uint8(ndr, NDR_SCALARS, len));
254                 NDR_CHECK(ndr_push_bytes(ndr, components[i], len));
255         }
256         NDR_CHECK(ndr_push_uint8(ndr, NDR_SCALARS, 0));
257
258         return NT_STATUS_OK;
259 }
260
261
262 /*
263   copy a nbt name structure
264 */
265 NTSTATUS nbt_name_dup(TALLOC_CTX *mem_ctx, struct nbt_name *name, struct nbt_name *newname)
266 {
267         *newname = *name;
268         newname->name = talloc_strdup(mem_ctx, newname->name);
269         NT_STATUS_HAVE_NO_MEMORY(newname->name);
270         newname->scope = talloc_strdup(mem_ctx, newname->scope);
271         if (name->scope) {
272                 NT_STATUS_HAVE_NO_MEMORY(newname->scope);
273         }
274         return NT_STATUS_OK;
275 }
276
277 /*
278   push a nbt name into a blob
279 */
280 NTSTATUS nbt_name_to_blob(TALLOC_CTX *mem_ctx, DATA_BLOB *blob, struct nbt_name *name)
281 {
282         return ndr_push_struct_blob(blob, mem_ctx, name, 
283                                     (ndr_push_flags_fn_t)ndr_push_nbt_name);
284 }
285
286
287 /*
288   pull a nbt name from a blob
289 */
290 NTSTATUS nbt_name_from_blob(TALLOC_CTX *mem_ctx, const DATA_BLOB *blob, struct nbt_name *name)
291 {
292         return ndr_pull_struct_blob(blob, mem_ctx, name, 
293                                     (ndr_pull_flags_fn_t)ndr_pull_nbt_name);
294 }
295
296
297 /*
298   choose a name to use when calling a server in a NBT session request.
299   we use heuristics to see if the name we have been given is a IP
300   address, or a too-long name. If it is then use *SMBSERVER, or a
301   truncated name
302 */
303 void nbt_choose_called_name(TALLOC_CTX *mem_ctx,
304                             struct nbt_name *n, const char *name, int type)
305 {
306         n->scope = NULL;
307         n->type = type;
308
309         if (is_ipaddress(name)) {
310                 n->name = "*SMBSERVER";
311                 return;
312         }
313         if (strlen(name) > 15) {
314                 const char *p = strchr(name, '.');
315                 if (p - name > 15) {
316                         n->name = "*SMBSERVER";
317                         return;
318                 }
319                 n->name = talloc_strndup(mem_ctx, name, PTR_DIFF(p, name));
320                 return;
321         }
322
323         n->name = talloc_strdup(mem_ctx, name);
324 }