0080a80951e69db2c72237a29dd924cab8120d9f
[samba.git] / source4 / torture / rap / rap.c
1 /* 
2    Unix SMB/CIFS implementation.
3    test suite for various RAP operations
4    Copyright (C) Volker Lendecke 2004
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22 #include "rap.h"
23
24 struct rap_call {
25         TALLOC_CTX *mem_ctx;
26         uint16 callno;
27         char *paramdesc;
28         const char *datadesc;
29
30         uint16 status;
31         uint16 convert;
32         
33         uint16 rcv_paramlen, rcv_datalen;
34
35         struct ndr_push *ndr_push_param;
36         struct ndr_push *ndr_push_data;
37         struct ndr_pull *ndr_pull_param;
38         struct ndr_pull *ndr_pull_data;
39 };
40
41 #define RAPNDR_FLAGS (LIBNDR_FLAG_NOALIGN|LIBNDR_FLAG_STR_ASCII|LIBNDR_FLAG_STR_NULLTERM);
42
43 static struct rap_call *new_rap_cli_call(uint16 callno)
44 {
45         struct rap_call *call;
46         TALLOC_CTX *mem_ctx = talloc_init("rap_call");
47
48         if (mem_ctx == NULL)
49                 return NULL;
50
51         call = talloc_p(mem_ctx, struct rap_call);
52
53         if (call == NULL)
54                 return NULL;
55
56         ZERO_STRUCTP(call);
57
58         call->callno = callno;
59         call->rcv_paramlen = 4;
60         call->mem_ctx = mem_ctx;
61
62         call->ndr_push_param = ndr_push_init_ctx(mem_ctx);
63         call->ndr_push_param->flags = RAPNDR_FLAGS;
64
65         call->ndr_push_data = ndr_push_init_ctx(mem_ctx);
66         call->ndr_push_data->flags = RAPNDR_FLAGS;
67
68         return call;
69 }
70
71 static void destroy_rap_call(struct rap_call *call)
72 {
73         talloc_destroy(call->mem_ctx);
74 }
75
76 static void rap_cli_push_paramdesc(struct rap_call *call, char desc)
77 {
78         int len = 0;
79
80         if (call->paramdesc != NULL)
81                 len = strlen(call->paramdesc);
82
83         call->paramdesc = talloc_realloc(call->mem_ctx,
84                                          call->paramdesc,
85                                          uint8_t,
86                                          len+2);
87         call->paramdesc[len] = desc;
88         call->paramdesc[len+1] = '\0';
89 }
90
91 static void rap_cli_push_word(struct rap_call *call, uint16 val)
92 {
93         rap_cli_push_paramdesc(call, 'W');
94         ndr_push_uint16(call->ndr_push_param, val);
95 }
96
97 static void rap_cli_push_dword(struct rap_call *call, uint32 val)
98 {
99         rap_cli_push_paramdesc(call, 'D');
100         ndr_push_uint32(call->ndr_push_param, val);
101 }
102
103 static void rap_cli_push_rcvbuf(struct rap_call *call, int len)
104 {
105         rap_cli_push_paramdesc(call, 'r');
106         rap_cli_push_paramdesc(call, 'L');
107         ndr_push_uint16(call->ndr_push_param, len);
108         call->rcv_datalen = len;
109 }
110
111 static void rap_cli_expect_multiple_entries(struct rap_call *call)
112 {
113         rap_cli_push_paramdesc(call, 'e');
114         rap_cli_push_paramdesc(call, 'h');
115         call->rcv_paramlen += 4; /* uint16 entry count, uint16 total */
116 }
117
118 static void rap_cli_push_string(struct rap_call *call, const char *str)
119 {
120         if (str == NULL) {
121                 rap_cli_push_paramdesc(call, 'O');
122                 return;
123         }
124         rap_cli_push_paramdesc(call, 'z');
125         ndr_push_string(call->ndr_push_param, NDR_SCALARS, str);
126 }
127
128 static void rap_cli_expect_format(struct rap_call *call, const char *format)
129 {
130         call->datadesc = format;
131 }
132
133 static NTSTATUS rap_pull_string(TALLOC_CTX *mem_ctx, struct ndr_pull *ndr,
134                                 uint16 convert, char **dest)
135 {
136         uint16 string_offset;
137         uint16 ignore;
138         const char *p;
139         size_t len;
140
141         NDR_CHECK(ndr_pull_uint16(ndr, &string_offset));
142         NDR_CHECK(ndr_pull_uint16(ndr, &ignore));
143
144         string_offset -= convert;
145
146         if (string_offset+1 > ndr->data_size)
147                 return NT_STATUS_INVALID_PARAMETER;
148
149         p = (const char *)(ndr->data + string_offset);
150         len = strnlen(p, ndr->data_size-string_offset);
151
152         if ( string_offset + len + 1 >  ndr->data_size )
153                 return NT_STATUS_INVALID_PARAMETER;
154
155         *dest = talloc_zero_size(mem_ctx, len+1);
156         pull_ascii(*dest, p, len+1, len, 0);
157
158         return NT_STATUS_OK;
159 }
160
161 static NTSTATUS rap_cli_do_call(struct smbcli_state *cli, TALLOC_CTX *mem_ctx,
162                                 struct rap_call *call)
163 {
164         NTSTATUS result;
165         DATA_BLOB param_blob;
166         struct ndr_push *params;
167         struct smb_trans2 trans;
168
169         params = ndr_push_init_ctx(mem_ctx);
170
171         if (params == NULL)
172                 return NT_STATUS_NO_MEMORY;
173
174         params->flags = RAPNDR_FLAGS;
175
176         trans.in.max_param = call->rcv_paramlen;
177         trans.in.max_data = smb_raw_max_trans_data(cli->tree, call->rcv_paramlen);
178         trans.in.max_setup = 0;
179         trans.in.flags = 0;
180         trans.in.timeout = 0;
181         trans.in.setup_count = 0;
182         trans.in.setup = NULL;
183         trans.in.trans_name = "\\PIPE\\LANMAN";
184
185         NDR_CHECK(ndr_push_uint16(params, call->callno));
186         NDR_CHECK(ndr_push_string(params, NDR_SCALARS, call->paramdesc));
187         NDR_CHECK(ndr_push_string(params, NDR_SCALARS, call->datadesc));
188
189         param_blob = ndr_push_blob(call->ndr_push_param);
190         NDR_CHECK(ndr_push_bytes(params, param_blob.data,
191                                  param_blob.length));
192
193         trans.in.params = ndr_push_blob(params);
194         trans.in.data = data_blob(NULL, 0);
195
196         result = smb_raw_trans(cli->tree, call->mem_ctx, &trans);
197
198         if (!NT_STATUS_IS_OK(result))
199                 return result;
200
201         call->ndr_pull_param = ndr_pull_init_blob(&trans.out.params,
202                                                   call->mem_ctx);
203         call->ndr_pull_param->flags = RAPNDR_FLAGS;
204
205         call->ndr_pull_data = ndr_pull_init_blob(&trans.out.data,
206                                                  call->mem_ctx);
207         call->ndr_pull_data->flags = RAPNDR_FLAGS;
208
209         return result;
210 }
211
212 #define NDR_OK(call) do { NTSTATUS _status; \
213                              _status = call; \
214                              if (!NT_STATUS_IS_OK(_status)) \
215                                 goto done; \
216                         } while (0)
217
218 static NTSTATUS smbcli_rap_netshareenum(struct smbcli_state *cli,
219                                      TALLOC_CTX *mem_ctx,
220                                      struct rap_NetShareEnum *r)
221 {
222         struct rap_call *call;
223         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
224         int i;
225
226         call = new_rap_cli_call(0);
227
228         if (call == NULL)
229                 return NT_STATUS_NO_MEMORY;
230
231         rap_cli_push_word(call, r->in.level); /* Level */
232         rap_cli_push_rcvbuf(call, r->in.bufsize);
233         rap_cli_expect_multiple_entries(call);
234
235         switch(r->in.level) {
236         case 0:
237                 rap_cli_expect_format(call, "B13");
238                 break;
239         case 1:
240                 rap_cli_expect_format(call, "B13BWz");
241                 break;
242         }
243
244         result = rap_cli_do_call(cli, mem_ctx, call);
245
246         if (!NT_STATUS_IS_OK(result))
247                 goto done;
248
249         NDR_OK(ndr_pull_uint16(call->ndr_pull_param, &r->out.status));
250         NDR_OK(ndr_pull_uint16(call->ndr_pull_param, &r->out.convert));
251         NDR_OK(ndr_pull_uint16(call->ndr_pull_param, &r->out.count));
252         NDR_OK(ndr_pull_uint16(call->ndr_pull_param, &r->out.available));
253
254         r->out.info = talloc_array_p(mem_ctx, union rap_shareenum_info,
255                                      r->out.count);
256
257         if (r->out.info == NULL)
258                 return NT_STATUS_NO_MEMORY;
259
260         for (i=0; i<r->out.count; i++) {
261                 switch(r->in.level) {
262                 case 0:
263                         NDR_OK(ndr_pull_bytes(call->ndr_pull_data,
264                                               (uint8_t *)r->out.info[i].info0.name, 13));
265                         break;
266                 case 1:
267                         NDR_OK(ndr_pull_bytes(call->ndr_pull_data,
268                                               (uint8_t *)r->out.info[i].info1.name, 13));
269                         NDR_OK(ndr_pull_bytes(call->ndr_pull_data,
270                                               (uint8_t *)&r->out.info[i].info1.pad, 1));
271                         NDR_OK(ndr_pull_uint16(call->ndr_pull_data,
272                                                &r->out.info[i].info1.type));
273                         NDR_OK(rap_pull_string(mem_ctx, call->ndr_pull_data,
274                                                r->out.convert,
275                                                &r->out.info[i].info1.comment));
276                         break;
277                 }
278         }
279
280         result = NT_STATUS_OK;
281
282  done:
283         destroy_rap_call(call);
284
285         return result;
286 }
287
288 static BOOL test_netshareenum(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
289 {
290         struct rap_NetShareEnum r;
291         int i;
292
293         r.in.level = 1;
294         r.in.bufsize = 8192;
295
296         if (!NT_STATUS_IS_OK(smbcli_rap_netshareenum(cli, mem_ctx, &r)))
297                 return False;
298
299         for (i=0; i<r.out.count; i++) {
300                 printf("%s %d %s\n", r.out.info[i].info1.name,
301                        r.out.info[i].info1.type,
302                        r.out.info[i].info1.comment);
303         }
304
305         return True;
306 }
307
308 static NTSTATUS smbcli_rap_netserverenum2(struct smbcli_state *cli,
309                                        TALLOC_CTX *mem_ctx,
310                                        struct rap_NetServerEnum2 *r)
311 {
312         struct rap_call *call;
313         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
314         int i;
315
316         call = new_rap_cli_call(104);
317
318         if (call == NULL)
319                 return NT_STATUS_NO_MEMORY;
320
321         rap_cli_push_word(call, r->in.level);
322         rap_cli_push_rcvbuf(call, r->in.bufsize);
323         rap_cli_expect_multiple_entries(call);
324         rap_cli_push_dword(call, r->in.servertype);
325         rap_cli_push_string(call, r->in.domain);
326
327         switch(r->in.level) {
328         case 0:
329                 rap_cli_expect_format(call, "B16");
330                 break;
331         case 1:
332                 rap_cli_expect_format(call, "B16BBDz");
333                 break;
334         }
335
336         result = rap_cli_do_call(cli, mem_ctx, call);
337
338         if (!NT_STATUS_IS_OK(result))
339                 goto done;
340
341         result = NT_STATUS_INVALID_PARAMETER;
342
343         NDR_OK(ndr_pull_uint16(call->ndr_pull_param, &r->out.status));
344         NDR_OK(ndr_pull_uint16(call->ndr_pull_param, &r->out.convert));
345         NDR_OK(ndr_pull_uint16(call->ndr_pull_param, &r->out.count));
346         NDR_OK(ndr_pull_uint16(call->ndr_pull_param, &r->out.available));
347
348         r->out.info = talloc_array_p(mem_ctx, union rap_server_info,
349                                      r->out.count);
350
351         if (r->out.info == NULL)
352                 return NT_STATUS_NO_MEMORY;
353
354         for (i=0; i<r->out.count; i++) {
355                 switch(r->in.level) {
356                 case 0:
357                         NDR_OK(ndr_pull_bytes(call->ndr_pull_data,
358                                               (uint8_t *)r->out.info[i].info0.name, 16));
359                         break;
360                 case 1:
361                         NDR_OK(ndr_pull_bytes(call->ndr_pull_data,
362                                               (uint8_t *)r->out.info[i].info1.name, 16));
363                         NDR_OK(ndr_pull_bytes(call->ndr_pull_data,
364                                               &r->out.info[i].info1.version_major, 1));
365                         NDR_OK(ndr_pull_bytes(call->ndr_pull_data,
366                                               &r->out.info[i].info1.version_minor, 1));
367                         NDR_OK(ndr_pull_uint32(call->ndr_pull_data,
368                                                &r->out.info[i].info1.servertype));
369                         NDR_OK(rap_pull_string(mem_ctx, call->ndr_pull_data,
370                                                r->out.convert,
371                                                &r->out.info[i].info1.comment));
372                 }
373         }
374
375         result = NT_STATUS_OK;
376
377  done:
378         destroy_rap_call(call);
379
380         return result;
381 }
382
383 static BOOL test_netserverenum(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
384 {
385         struct rap_NetServerEnum2 r;
386         int i;
387
388         r.in.level = 0;
389         r.in.bufsize = 8192;
390         r.in.servertype = 0xffffffff;
391         r.in.servertype = 0x80000000;
392         r.in.domain = NULL;
393
394         if (!NT_STATUS_IS_OK(smbcli_rap_netserverenum2(cli, mem_ctx, &r)))
395                 return False;
396
397         for (i=0; i<r.out.count; i++) {
398                 switch (r.in.level) {
399                 case 0:
400                         printf("%s\n", r.out.info[i].info0.name);
401                         break;
402                 case 1:
403                         printf("%s %x %s\n", r.out.info[i].info1.name,
404                                r.out.info[i].info1.servertype,
405                                r.out.info[i].info1.comment);
406                         break;
407                 }
408         }
409
410         return True;
411 }
412
413
414
415 static BOOL test_rap(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
416 {
417         BOOL res = True;
418
419         if (!test_netserverenum(cli, mem_ctx))
420                 res = False;
421
422         if (!test_netshareenum(cli, mem_ctx))
423                 res = False;
424
425         return res;
426 }
427
428 BOOL torture_raw_rap(void)
429 {
430         struct smbcli_state *cli;
431         BOOL ret = True;
432         TALLOC_CTX *mem_ctx;
433
434         if (!torture_open_connection(&cli)) {
435                 return False;
436         }
437
438         mem_ctx = talloc_init("torture_raw_rap");
439
440         if (!test_rap(cli, mem_ctx)) {
441                 ret = False;
442         }
443
444         torture_close_connection(cli);
445         talloc_destroy(mem_ctx);
446         return ret;
447 }