b47cfa2997a56b6fc2dc44fa407df0fb2d608f3e
[jelmer/samba4-debian.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    
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                                          len+2);
86         call->paramdesc[len] = desc;
87         call->paramdesc[len+1] = '\0';
88 }
89
90 static void rap_cli_push_word(struct rap_call *call, uint16 val)
91 {
92         rap_cli_push_paramdesc(call, 'W');
93         ndr_push_uint16(call->ndr_push_param, val);
94 }
95
96 static void rap_cli_push_dword(struct rap_call *call, uint32 val)
97 {
98         rap_cli_push_paramdesc(call, 'D');
99         ndr_push_uint32(call->ndr_push_param, val);
100 }
101
102 static void rap_cli_push_rcvbuf(struct rap_call *call, int len)
103 {
104         rap_cli_push_paramdesc(call, 'r');
105         rap_cli_push_paramdesc(call, 'L');
106         ndr_push_uint16(call->ndr_push_param, len);
107         call->rcv_datalen = len;
108 }
109
110 static void rap_cli_expect_multiple_entries(struct rap_call *call)
111 {
112         rap_cli_push_paramdesc(call, 'e');
113         rap_cli_push_paramdesc(call, 'h');
114         call->rcv_paramlen += 4; /* uint16 entry count, uint16 total */
115 }
116
117 static void rap_cli_push_string(struct rap_call *call, const char *str)
118 {
119         if (str == NULL) {
120                 rap_cli_push_paramdesc(call, 'O');
121                 return;
122         }
123         rap_cli_push_paramdesc(call, 'z');
124         ndr_push_string(call->ndr_push_param, NDR_SCALARS, str);
125 }
126
127 static void rap_cli_expect_format(struct rap_call *call, const char *format)
128 {
129         call->datadesc = format;
130 }
131
132 static NTSTATUS rap_pull_string(TALLOC_CTX *mem_ctx, struct ndr_pull *ndr,
133                                 uint16 convert, char **dest)
134 {
135         uint16 string_offset;
136         uint16 ignore;
137         const char *p;
138         size_t len;
139
140         NDR_CHECK(ndr_pull_uint16(ndr, &string_offset));
141         NDR_CHECK(ndr_pull_uint16(ndr, &ignore));
142
143         string_offset -= convert;
144
145         if (string_offset+1 > ndr->data_size)
146                 return NT_STATUS_INVALID_PARAMETER;
147
148         p = (const char *)(ndr->data + string_offset);
149         len = strnlen(p, ndr->data_size-string_offset);
150
151         if ( string_offset + len + 1 >  ndr->data_size )
152                 return NT_STATUS_INVALID_PARAMETER;
153
154         *dest = talloc_zero(mem_ctx, len+1);
155         pull_ascii(*dest, p, len+1, len, 0);
156
157         return NT_STATUS_OK;
158 }
159
160 static NTSTATUS rap_cli_do_call(struct smbcli_state *cli, TALLOC_CTX *mem_ctx,
161                                 struct rap_call *call)
162 {
163         NTSTATUS result;
164         DATA_BLOB param_blob;
165         struct ndr_push *params;
166         struct smb_trans2 trans;
167
168         params = ndr_push_init_ctx(mem_ctx);
169
170         if (params == NULL)
171                 return NT_STATUS_NO_MEMORY;
172
173         params->flags = RAPNDR_FLAGS;
174
175         trans.in.max_param = call->rcv_paramlen;
176         trans.in.max_data = smb_raw_max_trans_data(cli->tree, call->rcv_paramlen);
177         trans.in.max_setup = 0;
178         trans.in.flags = 0;
179         trans.in.timeout = 0;
180         trans.in.setup_count = 0;
181         trans.in.setup = NULL;
182         trans.in.trans_name = "\\PIPE\\LANMAN";
183
184         NDR_CHECK(ndr_push_uint16(params, call->callno));
185         NDR_CHECK(ndr_push_string(params, NDR_SCALARS, call->paramdesc));
186         NDR_CHECK(ndr_push_string(params, NDR_SCALARS, call->datadesc));
187
188         param_blob = ndr_push_blob(call->ndr_push_param);
189         NDR_CHECK(ndr_push_bytes(params, param_blob.data,
190                                  param_blob.length));
191
192         trans.in.params = ndr_push_blob(params);
193         trans.in.data = data_blob(NULL, 0);
194
195         result = smb_raw_trans(cli->tree, call->mem_ctx, &trans);
196
197         if (!NT_STATUS_IS_OK(result))
198                 return result;
199
200         call->ndr_pull_param = ndr_pull_init_blob(&trans.out.params,
201                                                   call->mem_ctx);
202         call->ndr_pull_param->flags = RAPNDR_FLAGS;
203
204         call->ndr_pull_data = ndr_pull_init_blob(&trans.out.data,
205                                                  call->mem_ctx);
206         call->ndr_pull_data->flags = RAPNDR_FLAGS;
207
208         return result;
209 }
210
211 #define NDR_OK(call) do { NTSTATUS _status; \
212                              _status = call; \
213                              if (!NT_STATUS_IS_OK(_status)) \
214                                 goto done; \
215                         } while (0)
216
217 static NTSTATUS smbcli_rap_netshareenum(struct smbcli_state *cli,
218                                      TALLOC_CTX *mem_ctx,
219                                      struct rap_NetShareEnum *r)
220 {
221         struct rap_call *call;
222         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
223         int i;
224
225         call = new_rap_cli_call(0);
226
227         if (call == NULL)
228                 return NT_STATUS_NO_MEMORY;
229
230         rap_cli_push_word(call, r->in.level); /* Level */
231         rap_cli_push_rcvbuf(call, r->in.bufsize);
232         rap_cli_expect_multiple_entries(call);
233
234         switch(r->in.level) {
235         case 0:
236                 rap_cli_expect_format(call, "B13");
237                 break;
238         case 1:
239                 rap_cli_expect_format(call, "B13BWz");
240                 break;
241         }
242
243         result = rap_cli_do_call(cli, mem_ctx, call);
244
245         if (!NT_STATUS_IS_OK(result))
246                 goto done;
247
248         NDR_OK(ndr_pull_uint16(call->ndr_pull_param, &r->out.status));
249         NDR_OK(ndr_pull_uint16(call->ndr_pull_param, &r->out.convert));
250         NDR_OK(ndr_pull_uint16(call->ndr_pull_param, &r->out.count));
251         NDR_OK(ndr_pull_uint16(call->ndr_pull_param, &r->out.available));
252
253         r->out.info = talloc_array_p(mem_ctx, union rap_shareenum_info,
254                                      r->out.count);
255
256         if (r->out.info == NULL)
257                 return NT_STATUS_NO_MEMORY;
258
259         for (i=0; i<r->out.count; i++) {
260                 switch(r->in.level) {
261                 case 0:
262                         NDR_OK(ndr_pull_bytes(call->ndr_pull_data,
263                                               (uint8_t *)r->out.info[i].info0.name, 13));
264                         break;
265                 case 1:
266                         NDR_OK(ndr_pull_bytes(call->ndr_pull_data,
267                                               (uint8_t *)r->out.info[i].info1.name, 13));
268                         NDR_OK(ndr_pull_bytes(call->ndr_pull_data,
269                                               (uint8_t *)&r->out.info[i].info1.pad, 1));
270                         NDR_OK(ndr_pull_uint16(call->ndr_pull_data,
271                                                &r->out.info[i].info1.type));
272                         NDR_OK(rap_pull_string(mem_ctx, call->ndr_pull_data,
273                                                r->out.convert,
274                                                &r->out.info[i].info1.comment));
275                         break;
276                 }
277         }
278
279         result = NT_STATUS_OK;
280
281  done:
282         destroy_rap_call(call);
283
284         return result;
285 }
286
287 static BOOL test_netshareenum(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
288 {
289         struct rap_NetShareEnum r;
290         int i;
291
292         r.in.level = 1;
293         r.in.bufsize = 8192;
294
295         if (!NT_STATUS_IS_OK(smbcli_rap_netshareenum(cli, mem_ctx, &r)))
296                 return False;
297
298         for (i=0; i<r.out.count; i++) {
299                 printf("%s %d %s\n", r.out.info[i].info1.name,
300                        r.out.info[i].info1.type,
301                        r.out.info[i].info1.comment);
302         }
303
304         return True;
305 }
306
307 static NTSTATUS smbcli_rap_netserverenum2(struct smbcli_state *cli,
308                                        TALLOC_CTX *mem_ctx,
309                                        struct rap_NetServerEnum2 *r)
310 {
311         struct rap_call *call;
312         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
313         int i;
314
315         call = new_rap_cli_call(104);
316
317         if (call == NULL)
318                 return NT_STATUS_NO_MEMORY;
319
320         rap_cli_push_word(call, r->in.level);
321         rap_cli_push_rcvbuf(call, r->in.bufsize);
322         rap_cli_expect_multiple_entries(call);
323         rap_cli_push_dword(call, r->in.servertype);
324         rap_cli_push_string(call, r->in.domain);
325
326         switch(r->in.level) {
327         case 0:
328                 rap_cli_expect_format(call, "B16");
329                 break;
330         case 1:
331                 rap_cli_expect_format(call, "B16BBDz");
332                 break;
333         }
334
335         result = rap_cli_do_call(cli, mem_ctx, call);
336
337         if (!NT_STATUS_IS_OK(result))
338                 goto done;
339
340         result = NT_STATUS_INVALID_PARAMETER;
341
342         NDR_OK(ndr_pull_uint16(call->ndr_pull_param, &r->out.status));
343         NDR_OK(ndr_pull_uint16(call->ndr_pull_param, &r->out.convert));
344         NDR_OK(ndr_pull_uint16(call->ndr_pull_param, &r->out.count));
345         NDR_OK(ndr_pull_uint16(call->ndr_pull_param, &r->out.available));
346
347         r->out.info = talloc_array_p(mem_ctx, union rap_server_info,
348                                      r->out.count);
349
350         if (r->out.info == NULL)
351                 return NT_STATUS_NO_MEMORY;
352
353         for (i=0; i<r->out.count; i++) {
354                 switch(r->in.level) {
355                 case 0:
356                         NDR_OK(ndr_pull_bytes(call->ndr_pull_data,
357                                               (uint8_t *)r->out.info[i].info0.name, 16));
358                         break;
359                 case 1:
360                         NDR_OK(ndr_pull_bytes(call->ndr_pull_data,
361                                               (uint8_t *)r->out.info[i].info1.name, 16));
362                         NDR_OK(ndr_pull_bytes(call->ndr_pull_data,
363                                               &r->out.info[i].info1.version_major, 1));
364                         NDR_OK(ndr_pull_bytes(call->ndr_pull_data,
365                                               &r->out.info[i].info1.version_minor, 1));
366                         NDR_OK(ndr_pull_uint32(call->ndr_pull_data,
367                                                &r->out.info[i].info1.servertype));
368                         NDR_OK(rap_pull_string(mem_ctx, call->ndr_pull_data,
369                                                r->out.convert,
370                                                &r->out.info[i].info1.comment));
371                 }
372         }
373
374         result = NT_STATUS_OK;
375
376  done:
377         destroy_rap_call(call);
378
379         return result;
380 }
381
382 static BOOL test_netserverenum(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
383 {
384         struct rap_NetServerEnum2 r;
385         int i;
386
387         r.in.level = 0;
388         r.in.bufsize = 8192;
389         r.in.servertype = 0xffffffff;
390         r.in.servertype = 0x80000000;
391         r.in.domain = NULL;
392
393         if (!NT_STATUS_IS_OK(smbcli_rap_netserverenum2(cli, mem_ctx, &r)))
394                 return False;
395
396         for (i=0; i<r.out.count; i++) {
397                 switch (r.in.level) {
398                 case 0:
399                         printf("%s\n", r.out.info[i].info0.name);
400                         break;
401                 case 1:
402                         printf("%s %x %s\n", r.out.info[i].info1.name,
403                                r.out.info[i].info1.servertype,
404                                r.out.info[i].info1.comment);
405                         break;
406                 }
407         }
408
409         return True;
410 }
411
412
413
414 static BOOL test_rap(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
415 {
416         BOOL res = True;
417
418         if (!test_netserverenum(cli, mem_ctx))
419                 res = False;
420
421         if (!test_netshareenum(cli, mem_ctx))
422                 res = False;
423
424         return res;
425 }
426
427 BOOL torture_raw_rap(void)
428 {
429         struct smbcli_state *cli;
430         BOOL ret = True;
431         TALLOC_CTX *mem_ctx;
432
433         if (!torture_open_connection(&cli)) {
434                 return False;
435         }
436
437         mem_ctx = talloc_init("torture_raw_rap");
438
439         if (!test_rap(cli, mem_ctx)) {
440                 ret = False;
441         }
442
443         torture_close_connection(cli);
444         talloc_destroy(mem_ctx);
445         return ret;
446 }