r26671: rap: Convert the RAP tests to the new torture API and run them by default.
[ira/wip.git] / source / torture / rap / rap.c
1 /* 
2    Unix SMB/CIFS implementation.
3    test suite for various RAP operations
4    Copyright (C) Volker Lendecke 2004
5    Copyright (C) Tim Potter 2005
6    Copyright (C) Jelmer Vernooij 2007
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 #include "includes.h"
23 #include "libcli/libcli.h"
24 #include "torture/torture.h"
25 #include "torture/util.h"
26 #include "libcli/rap/rap.h"
27 #include "libcli/raw/libcliraw.h"
28 #include "libcli/libcli.h"
29 #include "librpc/ndr/libndr.h"
30 #include "param/param.h"
31
32 #define RAP_GOTO(call) do { \
33         NTSTATUS _status; \
34         _status = call; \
35         if (!NT_STATUS_IS_OK(_status)) { \
36                 result = _status; \
37                 goto done; \
38         } \
39 } while (0)
40
41 #define NDR_GOTO(call) do { \
42         enum ndr_err_code _ndr_err; \
43         _ndr_err = call; \
44         if (!NDR_ERR_CODE_IS_SUCCESS(_ndr_err)) { \
45                 result = ndr_map_error2ntstatus(_ndr_err); \
46                 goto done; \
47         } \
48 } while (0)
49
50 #define NDR_RETURN(call) do { \
51         enum ndr_err_code _ndr_err; \
52         _ndr_err = call; \
53         if (!NDR_ERR_CODE_IS_SUCCESS(_ndr_err)) { \
54                 return ndr_map_error2ntstatus(_ndr_err); \
55         } \
56 } while (0)
57
58 struct rap_call {
59         uint16_t callno;
60         char *paramdesc;
61         const char *datadesc;
62
63         uint16_t status;
64         uint16_t convert;
65         
66         uint16_t rcv_paramlen, rcv_datalen;
67
68         struct ndr_push *ndr_push_param;
69         struct ndr_push *ndr_push_data;
70         struct ndr_pull *ndr_pull_param;
71         struct ndr_pull *ndr_pull_data;
72 };
73
74 #define RAPNDR_FLAGS (LIBNDR_FLAG_NOALIGN|LIBNDR_FLAG_STR_ASCII|LIBNDR_FLAG_STR_NULLTERM);
75
76 static struct rap_call *new_rap_cli_call(TALLOC_CTX *mem_ctx, uint16_t callno)
77 {
78         struct rap_call *call;
79
80         call = talloc(mem_ctx, struct rap_call);
81
82         if (call == NULL)
83                 return NULL;
84
85         call->callno = callno;
86         call->rcv_paramlen = 4;
87
88         call->paramdesc = NULL;
89         call->datadesc = NULL;
90
91         call->ndr_push_param = ndr_push_init_ctx(mem_ctx, lp_iconv_convenience(global_loadparm));
92         call->ndr_push_param->flags = RAPNDR_FLAGS;
93
94         call->ndr_push_data = ndr_push_init_ctx(mem_ctx, lp_iconv_convenience(global_loadparm));
95         call->ndr_push_data->flags = RAPNDR_FLAGS;
96
97         return call;
98 }
99
100 static void rap_cli_push_paramdesc(struct rap_call *call, char desc)
101 {
102         int len = 0;
103
104         if (call->paramdesc != NULL)
105                 len = strlen(call->paramdesc);
106
107         call->paramdesc = talloc_realloc(call,
108                                          call->paramdesc,
109                                          char,
110                                          len+2);
111
112         call->paramdesc[len] = desc;
113         call->paramdesc[len+1] = '\0';
114 }
115
116 static void rap_cli_push_word(struct rap_call *call, uint16_t val)
117 {
118         rap_cli_push_paramdesc(call, 'W');
119         ndr_push_uint16(call->ndr_push_param, NDR_SCALARS, val);
120 }
121
122 static void rap_cli_push_dword(struct rap_call *call, uint32_t val)
123 {
124         rap_cli_push_paramdesc(call, 'D');
125         ndr_push_uint32(call->ndr_push_param, NDR_SCALARS, val);
126 }
127
128 static void rap_cli_push_rcvbuf(struct rap_call *call, int len)
129 {
130         rap_cli_push_paramdesc(call, 'r');
131         rap_cli_push_paramdesc(call, 'L');
132         ndr_push_uint16(call->ndr_push_param, NDR_SCALARS, len);
133         call->rcv_datalen = len;
134 }
135
136 static void rap_cli_expect_multiple_entries(struct rap_call *call)
137 {
138         rap_cli_push_paramdesc(call, 'e');
139         rap_cli_push_paramdesc(call, 'h');
140         call->rcv_paramlen += 4; /* uint16_t entry count, uint16_t total */
141 }
142
143 static void rap_cli_expect_word(struct rap_call *call)
144 {
145         rap_cli_push_paramdesc(call, 'h');
146         call->rcv_paramlen += 2;
147 }
148
149 static void rap_cli_push_string(struct rap_call *call, const char *str)
150 {
151         if (str == NULL) {
152                 rap_cli_push_paramdesc(call, 'O');
153                 return;
154         }
155         rap_cli_push_paramdesc(call, 'z');
156         ndr_push_string(call->ndr_push_param, NDR_SCALARS, str);
157 }
158
159 static void rap_cli_expect_format(struct rap_call *call, const char *format)
160 {
161         call->datadesc = format;
162 }
163
164 static NTSTATUS rap_pull_string(TALLOC_CTX *mem_ctx, struct ndr_pull *ndr,
165                                 uint16_t convert, char **dest)
166 {
167         uint16_t string_offset;
168         uint16_t ignore;
169         const char *p;
170         size_t len;
171
172         NDR_RETURN(ndr_pull_uint16(ndr, NDR_SCALARS, &string_offset));
173         NDR_RETURN(ndr_pull_uint16(ndr, NDR_SCALARS, &ignore));
174
175         string_offset -= convert;
176
177         if (string_offset+1 > ndr->data_size)
178                 return NT_STATUS_INVALID_PARAMETER;
179
180         p = (const char *)(ndr->data + string_offset);
181         len = strnlen(p, ndr->data_size-string_offset);
182
183         if ( string_offset + len + 1 >  ndr->data_size )
184                 return NT_STATUS_INVALID_PARAMETER;
185
186         *dest = talloc_zero_array(mem_ctx, char, len+1);
187         pull_string(ndr->iconv_convenience, *dest, p, len+1, len, STR_ASCII);
188
189         return NT_STATUS_OK;
190 }
191
192 static NTSTATUS rap_cli_do_call(struct smbcli_tree *tree, struct rap_call *call)
193 {
194         NTSTATUS result;
195         DATA_BLOB param_blob;
196         struct ndr_push *params;
197         struct smb_trans2 trans;
198
199         params = ndr_push_init_ctx(call, lp_iconv_convenience(global_loadparm));
200
201         if (params == NULL)
202                 return NT_STATUS_NO_MEMORY;
203
204         params->flags = RAPNDR_FLAGS;
205
206         trans.in.max_param = call->rcv_paramlen;
207         trans.in.max_data = smb_raw_max_trans_data(tree, call->rcv_paramlen);
208         trans.in.max_setup = 0;
209         trans.in.flags = 0;
210         trans.in.timeout = 0;
211         trans.in.setup_count = 0;
212         trans.in.setup = NULL;
213         trans.in.trans_name = "\\PIPE\\LANMAN";
214
215         NDR_RETURN(ndr_push_uint16(params, NDR_SCALARS, call->callno));
216         if (call->paramdesc)
217                 NDR_RETURN(ndr_push_string(params, NDR_SCALARS, call->paramdesc));
218         if (call->datadesc)
219                 NDR_RETURN(ndr_push_string(params, NDR_SCALARS, call->datadesc));
220
221         param_blob = ndr_push_blob(call->ndr_push_param);
222         NDR_RETURN(ndr_push_bytes(params, param_blob.data,
223                                  param_blob.length));
224
225         trans.in.params = ndr_push_blob(params);
226         trans.in.data = data_blob(NULL, 0);
227
228         result = smb_raw_trans(tree, call, &trans);
229
230         if (!NT_STATUS_IS_OK(result))
231                 return result;
232
233         call->ndr_pull_param = ndr_pull_init_blob(&trans.out.params, call,
234                                                   lp_iconv_convenience(global_loadparm));
235         call->ndr_pull_param->flags = RAPNDR_FLAGS;
236
237         call->ndr_pull_data = ndr_pull_init_blob(&trans.out.data, call,
238                                                  lp_iconv_convenience(global_loadparm));
239         call->ndr_pull_data->flags = RAPNDR_FLAGS;
240
241         return result;
242 }
243
244
245 static NTSTATUS smbcli_rap_netshareenum(struct smbcli_tree *tree,
246                                         TALLOC_CTX *mem_ctx,
247                                         struct rap_NetShareEnum *r)
248 {
249         struct rap_call *call;
250         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
251         int i;
252
253         call = new_rap_cli_call(tree, RAP_WshareEnum);
254
255         if (call == NULL)
256                 return NT_STATUS_NO_MEMORY;
257
258         rap_cli_push_word(call, r->in.level); /* Level */
259         rap_cli_push_rcvbuf(call, r->in.bufsize);
260         rap_cli_expect_multiple_entries(call);
261
262         switch(r->in.level) {
263         case 0:
264                 rap_cli_expect_format(call, "B13");
265                 break;
266         case 1:
267                 rap_cli_expect_format(call, "B13BWz");
268                 break;
269         }
270
271         result = rap_cli_do_call(tree, call);
272
273         if (!NT_STATUS_IS_OK(result))
274                 goto done;
275
276         NDR_GOTO(ndr_pull_uint16(call->ndr_pull_param, NDR_SCALARS, &r->out.status));
277         NDR_GOTO(ndr_pull_uint16(call->ndr_pull_param, NDR_SCALARS, &r->out.convert));
278         NDR_GOTO(ndr_pull_uint16(call->ndr_pull_param, NDR_SCALARS, &r->out.count));
279         NDR_GOTO(ndr_pull_uint16(call->ndr_pull_param, NDR_SCALARS, &r->out.available));
280
281         r->out.info = talloc_array(mem_ctx, union rap_shareenum_info, r->out.count);
282
283         if (r->out.info == NULL) {
284                 result = NT_STATUS_NO_MEMORY;
285                 goto done;
286         }
287
288         for (i=0; i<r->out.count; i++) {
289                 switch(r->in.level) {
290                 case 0:
291                         NDR_GOTO(ndr_pull_bytes(call->ndr_pull_data,
292                                               (uint8_t *)r->out.info[i].info0.name, 13));
293                         break;
294                 case 1:
295                         NDR_GOTO(ndr_pull_bytes(call->ndr_pull_data,
296                                               (uint8_t *)r->out.info[i].info1.name, 13));
297                         NDR_GOTO(ndr_pull_bytes(call->ndr_pull_data,
298                                               (uint8_t *)&r->out.info[i].info1.pad, 1));
299                         NDR_GOTO(ndr_pull_uint16(call->ndr_pull_data,
300                                                NDR_SCALARS, &r->out.info[i].info1.type));
301                         RAP_GOTO(rap_pull_string(mem_ctx, call->ndr_pull_data,
302                                                r->out.convert,
303                                                &r->out.info[i].info1.comment));
304                         break;
305                 }
306         }
307
308         result = NT_STATUS_OK;
309
310  done:
311         talloc_free(call);
312         return result;
313 }
314
315 static bool test_netshareenum(struct torture_context *tctx, 
316                               struct smbcli_state *cli)
317 {
318         struct rap_NetShareEnum r;
319         int i;
320
321         r.in.level = 1;
322         r.in.bufsize = 8192;
323
324         torture_assert_ntstatus_ok(tctx, 
325                 smbcli_rap_netshareenum(cli->tree, tctx, &r), "");
326
327         for (i=0; i<r.out.count; i++) {
328                 printf("%s %d %s\n", r.out.info[i].info1.name,
329                        r.out.info[i].info1.type,
330                        r.out.info[i].info1.comment);
331         }
332
333         return true;
334 }
335
336 static NTSTATUS smbcli_rap_netserverenum2(struct smbcli_tree *tree,
337                                           TALLOC_CTX *mem_ctx,
338                                           struct rap_NetServerEnum2 *r)
339 {
340         struct rap_call *call;
341         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
342         int i;
343
344         call = new_rap_cli_call(mem_ctx, RAP_NetServerEnum2);
345
346         if (call == NULL)
347                 return NT_STATUS_NO_MEMORY;
348
349         rap_cli_push_word(call, r->in.level);
350         rap_cli_push_rcvbuf(call, r->in.bufsize);
351         rap_cli_expect_multiple_entries(call);
352         rap_cli_push_dword(call, r->in.servertype);
353         rap_cli_push_string(call, r->in.domain);
354
355         switch(r->in.level) {
356         case 0:
357                 rap_cli_expect_format(call, "B16");
358                 break;
359         case 1:
360                 rap_cli_expect_format(call, "B16BBDz");
361                 break;
362         }
363
364         result = rap_cli_do_call(tree, call);
365
366         if (!NT_STATUS_IS_OK(result))
367                 goto done;
368
369         result = NT_STATUS_INVALID_PARAMETER;
370
371         NDR_GOTO(ndr_pull_uint16(call->ndr_pull_param, NDR_SCALARS, &r->out.status));
372         NDR_GOTO(ndr_pull_uint16(call->ndr_pull_param, NDR_SCALARS, &r->out.convert));
373         NDR_GOTO(ndr_pull_uint16(call->ndr_pull_param, NDR_SCALARS, &r->out.count));
374         NDR_GOTO(ndr_pull_uint16(call->ndr_pull_param, NDR_SCALARS, &r->out.available));
375
376         r->out.info = talloc_array(mem_ctx, union rap_server_info, r->out.count);
377
378         if (r->out.info == NULL) {
379                 result = NT_STATUS_NO_MEMORY;
380                 goto done;
381         }
382
383         for (i=0; i<r->out.count; i++) {
384                 switch(r->in.level) {
385                 case 0:
386                         NDR_GOTO(ndr_pull_bytes(call->ndr_pull_data,
387                                               (uint8_t *)r->out.info[i].info0.name, 16));
388                         break;
389                 case 1:
390                         NDR_GOTO(ndr_pull_bytes(call->ndr_pull_data,
391                                               (uint8_t *)r->out.info[i].info1.name, 16));
392                         NDR_GOTO(ndr_pull_bytes(call->ndr_pull_data,
393                                               &r->out.info[i].info1.version_major, 1));
394                         NDR_GOTO(ndr_pull_bytes(call->ndr_pull_data,
395                                               &r->out.info[i].info1.version_minor, 1));
396                         NDR_GOTO(ndr_pull_uint32(call->ndr_pull_data,
397                                                NDR_SCALARS, &r->out.info[i].info1.servertype));
398                         RAP_GOTO(rap_pull_string(mem_ctx, call->ndr_pull_data,
399                                                r->out.convert,
400                                                &r->out.info[i].info1.comment));
401                 }
402         }
403
404         result = NT_STATUS_OK;
405
406  done:
407         talloc_free(call);
408         return result;
409 }
410
411 static bool test_netserverenum(struct torture_context *tctx, 
412                                struct smbcli_state *cli)
413 {
414         struct rap_NetServerEnum2 r;
415         int i;
416
417         r.in.level = 0;
418         r.in.bufsize = 8192;
419         r.in.servertype = 0xffffffff;
420         r.in.servertype = 0x80000000;
421         r.in.domain = NULL;
422
423         torture_assert_ntstatus_ok(tctx, 
424                    smbcli_rap_netserverenum2(cli->tree, tctx, &r), "");
425
426         for (i=0; i<r.out.count; i++) {
427                 switch (r.in.level) {
428                 case 0:
429                         printf("%s\n", r.out.info[i].info0.name);
430                         break;
431                 case 1:
432                         printf("%s %x %s\n", r.out.info[i].info1.name,
433                                r.out.info[i].info1.servertype,
434                                r.out.info[i].info1.comment);
435                         break;
436                 }
437         }
438
439         return true;
440 }
441
442 _PUBLIC_ NTSTATUS smbcli_rap_netservergetinfo(struct smbcli_tree *tree,
443                                      TALLOC_CTX *mem_ctx,
444                                      struct rap_WserverGetInfo *r)
445 {
446         struct rap_call *call;
447         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
448
449         if (!(call = new_rap_cli_call(mem_ctx, RAP_WserverGetInfo))) {
450                 return NT_STATUS_NO_MEMORY;
451         }
452
453         rap_cli_push_word(call, r->in.level);
454         rap_cli_push_rcvbuf(call, r->in.bufsize);
455         rap_cli_expect_word(call);
456
457         switch(r->in.level) {
458         case 0:
459                 rap_cli_expect_format(call, "B16");
460                 break;
461         case 1:
462                 rap_cli_expect_format(call, "B16BBDz");
463                 break;
464         default:
465                 result = NT_STATUS_INVALID_PARAMETER;
466                 goto done;
467         }
468
469         result = rap_cli_do_call(tree, call);
470
471         if (!NT_STATUS_IS_OK(result))
472                 goto done;
473
474         NDR_GOTO(ndr_pull_uint16(call->ndr_pull_param, NDR_SCALARS, &r->out.status));
475         NDR_GOTO(ndr_pull_uint16(call->ndr_pull_param, NDR_SCALARS, &r->out.convert));
476         NDR_GOTO(ndr_pull_uint16(call->ndr_pull_param, NDR_SCALARS, &r->out.available));
477
478         switch(r->in.level) {
479         case 0:
480                 NDR_GOTO(ndr_pull_bytes(call->ndr_pull_data,
481                                       (uint8_t *)r->out.info.info0.name, 16));
482                 break;
483         case 1:
484                 NDR_GOTO(ndr_pull_bytes(call->ndr_pull_data,
485                                       (uint8_t *)r->out.info.info1.name, 16));
486                 NDR_GOTO(ndr_pull_bytes(call->ndr_pull_data,
487                                       &r->out.info.info1.version_major, 1));
488                 NDR_GOTO(ndr_pull_bytes(call->ndr_pull_data,
489                                       &r->out.info.info1.version_minor, 1));
490                 NDR_GOTO(ndr_pull_uint32(call->ndr_pull_data,
491                                        NDR_SCALARS, &r->out.info.info1.servertype));
492                 RAP_GOTO(rap_pull_string(mem_ctx, call->ndr_pull_data,
493                                        r->out.convert,
494                                        &r->out.info.info1.comment));
495         }
496  done:
497         talloc_free(call);
498         return result;
499 }
500
501 static bool test_netservergetinfo(struct torture_context *tctx, 
502                                   struct smbcli_state *cli)
503 {
504         struct rap_WserverGetInfo r;
505         bool res = true;
506
507         r.in.bufsize = 0xffff;
508
509         r.in.level = 0;
510         torture_assert_ntstatus_ok(tctx, smbcli_rap_netservergetinfo(cli->tree, tctx, &r), "");
511         r.in.level = 1;
512         torture_assert_ntstatus_ok(tctx, smbcli_rap_netservergetinfo(cli->tree, tctx, &r), "");
513
514         return res;
515 }
516
517 bool torture_rap_scan(struct torture_context *torture, struct smbcli_state *cli)
518 {
519         int callno;
520
521         for (callno = 0; callno < 0xffff; callno++) {
522                 struct rap_call *call = new_rap_cli_call(torture, callno);
523                 NTSTATUS result;
524
525                 result = rap_cli_do_call(cli->tree, call);
526
527                 if (!NT_STATUS_EQUAL(result, NT_STATUS_INVALID_PARAMETER))
528                         continue;
529
530                 printf("callno %d is RAP call\n", callno);
531         }
532
533         return true;
534 }
535
536 NTSTATUS torture_rap_init(void)
537 {
538         struct torture_suite *suite = torture_suite_create(talloc_autofree_context(), "RAP");
539         struct torture_suite *suite_basic = torture_suite_create(suite, "BASIC");
540
541         torture_suite_add_suite(suite, suite_basic);
542
543         torture_suite_add_1smb_test(suite_basic, "netserverenum", 
544                                     test_netserverenum);
545         torture_suite_add_1smb_test(suite_basic, "netshareenum",
546                                     test_netshareenum);
547         torture_suite_add_1smb_test(suite_basic, "netservergetinfo",
548                                     test_netservergetinfo);
549
550         torture_suite_add_1smb_test(suite, "SCAN", torture_rap_scan);
551
552         suite->description = talloc_strdup(suite, 
553                                                 "Remote Administration Protocol tests");
554
555         torture_register_suite(suite);
556
557         return NT_STATUS_OK;
558 }