s4-srvsvc: merge srvsvc_NetFileEnum from s3 idl.
[ira/wip.git] / source4 / torture / rpc / srvsvc.c
1 /* 
2    Unix SMB/CIFS implementation.
3    test suite for srvsvc rpc operations
4
5    Copyright (C) Stefan (metze) Metzmacher 2003
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "torture/torture.h"
23 #include "librpc/gen_ndr/ndr_srvsvc.h"
24 #include "librpc/gen_ndr/ndr_srvsvc_c.h"
25 #include "torture/rpc/rpc.h"
26
27 /**************************/
28 /* srvsvc_NetCharDev      */
29 /**************************/
30 static bool test_NetCharDevGetInfo(struct dcerpc_pipe *p, struct torture_context *tctx,
31                                 const char *devname)
32 {
33         NTSTATUS status;
34         struct srvsvc_NetCharDevGetInfo r;
35         union srvsvc_NetCharDevInfo info;
36         uint32_t levels[] = {0, 1};
37         int i;
38
39         r.in.server_unc = talloc_asprintf(tctx,"\\\\%s",dcerpc_server_name(p));
40         r.in.device_name = devname;
41         r.out.info = &info;
42
43         for (i=0;i<ARRAY_SIZE(levels);i++) {
44                 r.in.level = levels[i];
45                 torture_comment(tctx, "testing NetCharDevGetInfo level %u on device '%s'\n",
46                         r.in.level, r.in.device_name);
47                 status = dcerpc_srvsvc_NetCharDevGetInfo(p, tctx, &r);
48                 torture_assert_ntstatus_ok(tctx, status, "NetCharDevGetInfo failed");
49                 torture_assert_werr_ok(tctx, r.out.result, "NetCharDevGetInfo failed");
50         }
51
52         return true;
53 }
54
55 static bool test_NetCharDevControl(struct dcerpc_pipe *p, struct torture_context *tctx,
56                                 const char *devname)
57 {
58         NTSTATUS status;
59         struct srvsvc_NetCharDevControl r;
60         uint32_t opcodes[] = {0, 1};
61         int i;
62
63         r.in.server_unc = talloc_asprintf(tctx,"\\\\%s",dcerpc_server_name(p));
64         r.in.device_name = devname;
65
66         for (i=0;i<ARRAY_SIZE(opcodes);i++) {
67                 ZERO_STRUCT(r.out);
68                 r.in.opcode = opcodes[i];
69                 torture_comment(tctx, "testing NetCharDevControl opcode %u on device '%s'\n", 
70                         r.in.opcode, r.in.device_name);
71                 status = dcerpc_srvsvc_NetCharDevControl(p, tctx, &r);
72                 torture_assert_ntstatus_ok(tctx, status, "NetCharDevControl failed");
73                 torture_assert_werr_ok(tctx, r.out.result, "NetCharDevControl failed");
74         }
75
76         return true;
77 }
78
79 static bool test_NetCharDevEnum(struct torture_context *tctx, 
80                                                                 struct dcerpc_pipe *p)
81 {
82         NTSTATUS status;
83         struct srvsvc_NetCharDevEnum r;
84         struct srvsvc_NetCharDevInfoCtr info_ctr;
85         struct srvsvc_NetCharDevCtr0 c0;
86         struct srvsvc_NetCharDevCtr0 c1;
87         uint32_t totalentries = 0;
88         uint32_t levels[] = {0, 1};
89         int i;
90
91         ZERO_STRUCT(info_ctr);
92
93         r.in.server_unc = talloc_asprintf(tctx,"\\\\%s",dcerpc_server_name(p));
94         r.in.info_ctr = &info_ctr;
95         r.in.max_buffer = (uint32_t)-1;
96         r.in.resume_handle = NULL;
97         r.out.info_ctr = &info_ctr;
98         r.out.totalentries = &totalentries;
99
100         for (i=0;i<ARRAY_SIZE(levels);i++) {
101                 int j;
102
103                 info_ctr.level = levels[i];
104
105                 switch(info_ctr.level) {
106                 case 0:
107                         ZERO_STRUCT(c0);
108                         info_ctr.ctr.ctr0 = &c0;
109                         break;
110                 case 1:
111                         ZERO_STRUCT(c1);
112                         info_ctr.ctr.ctr0 = &c1;
113                         break;
114                 }
115
116                 torture_comment(tctx, "testing NetCharDevEnum level %u\n", info_ctr.level);
117                 status = dcerpc_srvsvc_NetCharDevEnum(p, tctx, &r);
118                 torture_assert_ntstatus_ok(tctx, status, "NetCharDevEnum failed");
119                 if (!W_ERROR_IS_OK(r.out.result)) {
120                         torture_comment(tctx, "NetCharDevEnum failed: %s\n", win_errstr(r.out.result));
121                         continue;
122                 }
123
124                 /* call test_NetCharDevGetInfo and test_NetCharDevControl for each returned share */
125                 if (info_ctr.level == 1) {
126                         for (j=0;j<r.out.info_ctr->ctr.ctr1->count;j++) {
127                                 const char *device;
128                                 device = r.out.info_ctr->ctr.ctr1->array[j].device;
129                                 if (!test_NetCharDevGetInfo(p, tctx, device)) {
130                                         return false;
131                                 }
132                                 if (!test_NetCharDevControl(p, tctx, device)) {
133                                         return false;
134                                 }
135                         }
136                 }
137         }
138
139         return true;
140 }
141
142 /**************************/
143 /* srvsvc_NetCharDevQ     */
144 /**************************/
145 static bool test_NetCharDevQGetInfo(struct dcerpc_pipe *p, struct torture_context *tctx,
146                                 const char *devicequeue)
147 {
148         NTSTATUS status;
149         struct srvsvc_NetCharDevQGetInfo r;
150         union srvsvc_NetCharDevQInfo info;
151         uint32_t levels[] = {0, 1};
152         int i;
153
154         r.in.server_unc = talloc_asprintf(tctx,"\\\\%s",dcerpc_server_name(p));
155         r.in.queue_name = devicequeue;
156         r.in.user = talloc_asprintf(tctx,"Administrator");
157         r.out.info = &info;
158
159         for (i=0;i<ARRAY_SIZE(levels);i++) {
160                 r.in.level = levels[i];
161                 torture_comment(tctx, "testing NetCharDevQGetInfo level %u on devicequeue '%s'\n",
162                         r.in.level, r.in.queue_name);
163                 status = dcerpc_srvsvc_NetCharDevQGetInfo(p, tctx, &r);
164                 torture_assert_ntstatus_ok(tctx, status, "NetCharDevQGetInfo failed");
165                 torture_assert_werr_ok(tctx, r.out.result, "NetCharDevQGetInfo failed");
166         }
167
168         return true;
169 }
170
171 #if 0
172 static bool test_NetCharDevQSetInfo(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
173                                 const char *devicequeue)
174 {
175         NTSTATUS status;
176         struct srvsvc_NetCharDevQSetInfo r;
177         uint32_t parm_error;
178         uint32_t levels[] = {0, 1};
179         int i;
180         bool ret = true;
181
182         r.in.server_unc = talloc_asprintf(mem_ctx,"\\\\%s",dcerpc_server_name(p));
183         r.in.queue_name = devicequeue;
184
185         for (i=0;i<ARRAY_SIZE(levels);i++) {
186                 ZERO_STRUCT(r.out);
187                 parm_error = 0;
188                 r.in.level = levels[i];
189                 d_printf("testing NetCharDevQSetInfo level %u on devicequeue '%s'\n", 
190                         r.in.level, devicequeue);
191                 switch (r.in.level) {
192                 case 0:
193                         r.in.info.info0 = talloc(mem_ctx, struct srvsvc_NetCharDevQInfo0);
194                         r.in.info.info0->device = r.in.queue_name;
195                         break;
196                 case 1:
197                         r.in.info.info1 = talloc(mem_ctx, struct srvsvc_NetCharDevQInfo1);
198                         r.in.info.info1->device = r.in.queue_name;
199                         r.in.info.info1->priority = 0x000;
200                         r.in.info.info1->devices = r.in.queue_name;
201                         r.in.info.info1->users = 0x000;
202                         r.in.info.info1->num_ahead = 0x000;
203                         break;
204                 default:
205                         break;
206                 }
207                 r.in.parm_error = &parm_error;
208                 status = dcerpc_srvsvc_NetCharDevQSetInfo(p, mem_ctx, &r);
209                 if (!NT_STATUS_IS_OK(status)) {
210                         d_printf("NetCharDevQSetInfo level %u on devicequeue '%s' failed - %s\n",
211                                 r.in.level, r.in.queue_name, nt_errstr(status));
212                         ret = false;
213                         continue;
214                 }
215                 if (!W_ERROR_IS_OK(r.out.result)) {
216                         d_printf("NetCharDevQSetInfo level %u on devicequeue '%s' failed - %s\n",
217                                 r.in.level, r.in.queue_name, win_errstr(r.out.result));
218                         continue;
219                 }
220         }
221
222         return ret;
223 }
224 #endif
225
226 static bool test_NetCharDevQEnum(struct torture_context *tctx, 
227                                  struct dcerpc_pipe *p)
228 {
229         NTSTATUS status;
230         struct srvsvc_NetCharDevQEnum r;
231         struct srvsvc_NetCharDevQInfoCtr info_ctr;
232         struct srvsvc_NetCharDevQCtr0 c0;
233         struct srvsvc_NetCharDevQCtr1 c1;
234         uint32_t totalentries = 0;
235         uint32_t levels[] = {0, 1};
236         int i;
237
238         ZERO_STRUCT(info_ctr);
239
240         r.in.server_unc = talloc_asprintf(tctx,"\\\\%s",dcerpc_server_name(p));
241         r.in.user = talloc_asprintf(tctx,"%s","Administrator");
242         r.in.info_ctr = &info_ctr;
243         r.in.max_buffer = (uint32_t)-1;
244         r.in.resume_handle = NULL;
245         r.out.totalentries = &totalentries;
246         r.out.info_ctr = &info_ctr;
247
248         for (i=0;i<ARRAY_SIZE(levels);i++) {
249                 int j;
250
251                 info_ctr.level = levels[i];
252
253                 switch (info_ctr.level) {
254                 case 0:
255                         ZERO_STRUCT(c0);
256                         info_ctr.ctr.ctr0 = &c0;
257                         break;
258                 case 1:
259                         ZERO_STRUCT(c1);
260                         info_ctr.ctr.ctr1 = &c1;
261                         break;
262                 }
263                 torture_comment(tctx, "testing NetCharDevQEnum level %u\n", info_ctr.level);
264                 status = dcerpc_srvsvc_NetCharDevQEnum(p, tctx, &r);
265                 torture_assert_ntstatus_ok(tctx, status, "NetCharDevQEnum failed");
266                 if (!W_ERROR_IS_OK(r.out.result)) {
267                         torture_comment(tctx, "NetCharDevQEnum failed: %s\n", win_errstr(r.out.result));
268                         continue;
269                 }
270
271                 /* call test_NetCharDevGetInfo and test_NetCharDevControl for each returned share */
272                 if (info_ctr.level == 1) {
273                         for (j=0;j<r.out.info_ctr->ctr.ctr1->count;j++) {
274                                 const char *device;
275                                 device = r.out.info_ctr->ctr.ctr1->array[j].device;
276                                 if (!test_NetCharDevQGetInfo(p, tctx, device)) {
277                                         return false;
278                                 }
279                         }
280                 }
281         }
282
283         return true;
284 }
285
286 /**************************/
287 /* srvsvc_NetConn         */
288 /**************************/
289 static bool test_NetConnEnum(struct torture_context *tctx,
290                              struct dcerpc_pipe *p)
291 {
292         NTSTATUS status;
293         struct srvsvc_NetConnEnum r;
294         struct srvsvc_NetConnInfoCtr info_ctr;
295         struct srvsvc_NetConnCtr0 c0;
296         struct srvsvc_NetConnCtr1 c1;
297         uint32_t totalentries = 0;
298         uint32_t levels[] = {0, 1};
299         int i;
300
301         ZERO_STRUCT(info_ctr);
302
303         r.in.server_unc = talloc_asprintf(tctx,"\\\\%s",dcerpc_server_name(p));
304         r.in.path = talloc_asprintf(tctx,"%s","ADMIN$");
305         r.in.info_ctr = &info_ctr;
306         r.in.max_buffer = (uint32_t)-1;
307         r.in.resume_handle = NULL;
308         r.out.totalentries = &totalentries;
309         r.out.info_ctr = &info_ctr;
310
311         for (i=0;i<ARRAY_SIZE(levels);i++) {
312                 info_ctr.level = levels[i];
313
314                 switch (info_ctr.level) {
315                 case 0:
316                         ZERO_STRUCT(c0);
317                         info_ctr.ctr.ctr0 = &c0;
318                         break;
319                 case 1:
320                         ZERO_STRUCT(c1);
321                         info_ctr.ctr.ctr1 = &c1;
322                         break;
323                 }
324
325                 torture_comment(tctx, "testing NetConnEnum level %u\n", info_ctr.level);
326                 status = dcerpc_srvsvc_NetConnEnum(p, tctx, &r);
327                 torture_assert_ntstatus_ok(tctx, status, "NetConnEnum failed");
328                 if (!W_ERROR_IS_OK(r.out.result)) {
329                         torture_comment(tctx, "NetConnEnum failed: %s\n", win_errstr(r.out.result));
330                 }
331         }
332
333         return true;
334 }
335
336 /**************************/
337 /* srvsvc_NetFile         */
338 /**************************/
339 static bool test_NetFileEnum(struct torture_context *tctx,
340                              struct dcerpc_pipe *p)
341 {
342         NTSTATUS status;
343         struct srvsvc_NetFileEnum r;
344         struct srvsvc_NetFileInfoCtr info_ctr;
345         struct srvsvc_NetFileCtr2 c2;
346         struct srvsvc_NetFileCtr3 c3;
347         uint32_t totalentries = 0;
348         uint32_t levels[] = {2, 3};
349         int i;
350
351         ZERO_STRUCT(info_ctr);
352
353         r.in.server_unc = talloc_asprintf(tctx,"\\\\%s",dcerpc_server_name(p));
354         r.in.path = NULL;
355         r.in.user = NULL;
356         r.in.info_ctr = &info_ctr;
357         r.in.max_buffer = (uint32_t)4096;
358         r.in.resume_handle = NULL;
359         r.out.totalentries = &totalentries;
360         r.out.info_ctr = &info_ctr;
361
362         for (i=0;i<ARRAY_SIZE(levels);i++) {
363                 info_ctr.level = levels[i];
364
365                 switch (info_ctr.level) {
366                 case 2:
367                         ZERO_STRUCT(c2);
368                         info_ctr.ctr.ctr2 = &c2;
369                         break;
370                 case 3:
371                         ZERO_STRUCT(c3);
372                         info_ctr.ctr.ctr3 = &c3;
373                         break;
374                 }
375                 torture_comment(tctx, "testing NetFileEnum level %u\n", info_ctr.level);
376                 status = dcerpc_srvsvc_NetFileEnum(p, tctx, &r);
377                 torture_assert_ntstatus_ok(tctx, status, "NetFileEnum failed");
378                 if (!W_ERROR_IS_OK(r.out.result)) {
379                         torture_comment(tctx, "NetFileEnum failed: %s\n", win_errstr(r.out.result));
380                 }
381         }
382
383         return true;
384 }
385
386 /**************************/
387 /* srvsvc_NetSess         */
388 /**************************/
389 static bool test_NetSessEnum(struct torture_context *tctx,
390                              struct dcerpc_pipe *p)
391 {
392         NTSTATUS status;
393         struct srvsvc_NetSessEnum r;
394         struct srvsvc_NetSessCtr0 c0;
395         uint32_t levels[] = {0, 1, 2, 10, 502};
396         int i;
397
398         r.in.server_unc = talloc_asprintf(tctx,"\\\\%s",dcerpc_server_name(p));
399         r.in.client = NULL;
400         r.in.user = NULL;
401         r.in.ctr.ctr0 = &c0;
402         r.in.ctr.ctr0->count = 0;
403         r.in.ctr.ctr0->array = NULL;
404         r.in.max_buffer = (uint32_t)-1;
405         r.in.resume_handle = NULL;
406
407         for (i=0;i<ARRAY_SIZE(levels);i++) {
408                 ZERO_STRUCT(r.out);
409                 r.in.level = levels[i];
410                 torture_comment(tctx, "testing NetSessEnum level %u\n", r.in.level);
411                 status = dcerpc_srvsvc_NetSessEnum(p, tctx, &r);
412                 torture_assert_ntstatus_ok(tctx, status, "NetSessEnum failed");
413                 if (!W_ERROR_IS_OK(r.out.result)) {
414                         torture_comment(tctx, "NetSessEnum failed: %s\n", win_errstr(r.out.result));
415                 }
416         }
417
418         return true;
419 }
420
421 /**************************/
422 /* srvsvc_NetShare        */
423 /**************************/
424 static bool test_NetShareCheck(struct dcerpc_pipe *p, struct torture_context *tctx,
425                                const char *device_name)
426 {
427         NTSTATUS status;
428         struct srvsvc_NetShareCheck r;
429         enum srvsvc_ShareType type;
430
431         r.in.server_unc = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
432         r.in.device_name = device_name;
433         r.out.type = &type;
434
435         torture_comment(tctx, 
436                         "testing NetShareCheck on device '%s'\n", r.in.device_name);
437
438         status = dcerpc_srvsvc_NetShareCheck(p, tctx, &r);
439         torture_assert_ntstatus_ok(tctx, status, "dcerpc_srvsvc_NetShareCheck failed");
440         torture_assert_werr_ok(tctx, r.out.result, "NetShareCheck failed");
441
442         return true;
443 }
444
445 static bool test_NetShareGetInfo(struct torture_context *tctx, 
446                                  struct dcerpc_pipe *p,
447                                  const char *sharename, bool admin)
448 {
449         NTSTATUS status;
450         struct srvsvc_NetShareGetInfo r;
451         union srvsvc_NetShareInfo info;
452         struct {
453                 uint32_t level;
454                 WERROR anon_status;
455                 WERROR admin_status;
456         } levels[] = {
457                  { 0,           WERR_OK,                WERR_OK },
458                  { 1,           WERR_OK,                WERR_OK },
459                  { 2,           WERR_ACCESS_DENIED,     WERR_OK },
460                  { 501,         WERR_OK,                WERR_OK },
461                  { 502,         WERR_ACCESS_DENIED,     WERR_OK },
462                  { 1005,        WERR_OK,                WERR_OK },
463         };
464         int i;
465
466         r.in.server_unc = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
467         r.in.share_name = sharename;
468         r.out.info = &info;
469
470         for (i=0;i<ARRAY_SIZE(levels);i++) {
471                 WERROR expected;
472
473                 r.in.level = levels[i].level;
474                 expected = levels[i].anon_status;
475                 if (admin) expected = levels[i].admin_status;
476
477                 torture_comment(tctx, "testing NetShareGetInfo level %u on share '%s'\n", 
478                        r.in.level, r.in.share_name);
479
480                 status = dcerpc_srvsvc_NetShareGetInfo(p, tctx, &r);
481                 torture_assert_ntstatus_ok(tctx, status, "NetShareGetInfo failed");
482                 torture_assert_werr_equal(tctx, r.out.result, expected, "NetShareGetInfo failed");
483
484                 if (r.in.level != 2) continue;
485                 if (!r.out.info->info2 || !r.out.info->info2->path) continue;
486                 if (!test_NetShareCheck(p, tctx, r.out.info->info2->path)) {
487                         return false;
488                 }
489         }
490
491         return true;
492 }
493
494 static bool test_NetShareGetInfoAdminFull(struct torture_context *tctx, 
495                                           struct dcerpc_pipe *p)
496 {
497         return test_NetShareGetInfo(tctx, p, "ADMIN$", true);
498 }
499
500 static bool test_NetShareGetInfoAdminAnon(struct torture_context *tctx, 
501                                           struct dcerpc_pipe *p)
502 {
503         return test_NetShareGetInfo(tctx, p, "ADMIN$", false);
504 }
505
506 static bool test_NetShareAddSetDel(struct torture_context *tctx, 
507                                    struct dcerpc_pipe *p)
508 {
509         NTSTATUS status;
510         struct srvsvc_NetShareAdd a;
511         struct srvsvc_NetShareSetInfo r;
512         struct srvsvc_NetShareGetInfo q;
513         struct srvsvc_NetShareDel d;
514         struct sec_desc_buf sd_buf;
515         union srvsvc_NetShareInfo info;
516         struct {
517                 uint32_t level;
518                 WERROR expected;
519         } levels[] = {
520                  { 0,           WERR_UNKNOWN_LEVEL },
521                  { 1,           WERR_OK },
522                  { 2,           WERR_OK },
523                  { 501,         WERR_UNKNOWN_LEVEL },
524                  { 502,         WERR_OK },
525                  { 1004,        WERR_OK },
526                  { 1005,        WERR_OK },
527                  { 1006,        WERR_OK },
528 /*               { 1007,        WERR_OK }, */
529                  { 1501,        WERR_OK },
530         };
531         int i;
532
533         a.in.server_unc = r.in.server_unc = q.in.server_unc = d.in.server_unc =
534                 talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
535         r.in.share_name = talloc_strdup(tctx, "testshare");
536
537         info.info2 = talloc(tctx, struct srvsvc_NetShareInfo2);
538         info.info2->name = r.in.share_name;
539         info.info2->type = STYPE_DISKTREE;
540         info.info2->comment = talloc_strdup(tctx, "test comment");
541         info.info2->permissions = 123434566;
542         info.info2->max_users = -1;
543         info.info2->current_users = 0;
544         info.info2->path = talloc_strdup(tctx, "C:\\");
545         info.info2->password = NULL;
546
547         a.in.info = &info;
548         a.in.level = 2;
549         a.in.parm_error = NULL;
550
551         status = dcerpc_srvsvc_NetShareAdd(p, tctx, &a);
552         torture_assert_ntstatus_ok(tctx, status, "NetShareAdd level 2 on share 'testshare' failed");
553         torture_assert_werr_ok(tctx, a.out.result, "NetShareAdd level 2 on share 'testshare' failed");
554
555         r.in.parm_error = NULL;
556
557         q.in.level = 502;
558
559         for (i = 0; i < ARRAY_SIZE(levels); i++) {
560
561                 r.in.level = levels[i].level;
562                 ZERO_STRUCT(r.out);
563
564                 torture_comment(tctx, "testing NetShareSetInfo level %u on share '%s'\n", 
565                        r.in.level, r.in.share_name);
566
567                 switch (levels[i].level) {
568                 case 0:
569                         info.info0 = talloc(tctx, struct srvsvc_NetShareInfo0);
570                         info.info0->name = r.in.share_name;
571                         break;
572                 case 1:
573                         info.info1 = talloc(tctx, struct srvsvc_NetShareInfo1);
574                         info.info1->name = r.in.share_name;
575                         info.info1->type = STYPE_DISKTREE;
576                         info.info1->comment = talloc_strdup(tctx, "test comment 1");
577                         break;
578                 case 2: 
579                         info.info2 = talloc(tctx, struct srvsvc_NetShareInfo2);
580                         info.info2->name = r.in.share_name;
581                         info.info2->type = STYPE_DISKTREE;
582                         info.info2->comment = talloc_strdup(tctx, "test comment 2");
583                         info.info2->permissions = 0;
584                         info.info2->max_users = 2;
585                         info.info2->current_users = 1;
586                         info.info2->path = talloc_strdup(tctx, "::BLaH::"); /* "C:\\"); */
587                         info.info2->password = NULL;
588                         break;
589                 case 501:
590                         info.info501 = talloc(tctx, struct srvsvc_NetShareInfo501);
591                         info.info501->name = r.in.share_name;
592                         info.info501->type = STYPE_DISKTREE;
593                         info.info501->comment = talloc_strdup(tctx, "test comment 501");
594                         info.info501->csc_policy = 0;
595                         break;
596                 case 502:
597                         ZERO_STRUCT(sd_buf);
598                         info.info502 = talloc(tctx, struct srvsvc_NetShareInfo502);
599                         info.info502->name = r.in.share_name;
600                         info.info502->type = STYPE_DISKTREE;
601                         info.info502->comment = talloc_strdup(tctx, "test comment 502");
602                         info.info502->permissions = 0;
603                         info.info502->max_users = 502;
604                         info.info502->current_users = 1;
605                         info.info502->path = talloc_strdup(tctx, "C:\\");
606                         info.info502->password = NULL;
607                         info.info502->sd_buf = sd_buf;
608                         break;
609                 case 1004:
610                         info.info1004 = talloc(tctx, struct srvsvc_NetShareInfo1004);
611                         info.info1004->comment = talloc_strdup(tctx, "test comment 1004");
612                         break;
613                 case 1005:
614                         info.info1005 = talloc(tctx, struct srvsvc_NetShareInfo1005);
615                         info.info1005->dfs_flags = 0;
616                         break;
617                 case 1006:
618                         info.info1006 = talloc(tctx, struct srvsvc_NetShareInfo1006);
619                         info.info1006->max_users = 1006;
620                         break;
621 /*              case 1007:
622                         info.info1007 = talloc(tctx, struct srvsvc_NetShareInfo1007);
623                         info.info1007->flags = 0;
624                         info.info1007->alternate_directory_name = talloc_strdup(tctx, "test");
625                         break;
626 */
627                 case 1501:
628                         info.info1501 = talloc_zero(tctx, struct sec_desc_buf);
629                         break;
630                 }
631
632                 r.in.info = &info;
633
634                 status = dcerpc_srvsvc_NetShareSetInfo(p, tctx, &r);
635                 torture_assert_ntstatus_ok(tctx, status, "NetShareGetInfo failed");
636                 torture_assert_werr_equal(tctx, r.out.result, levels[i].expected, "NetShareSetInfo failed");
637                 
638                 q.in.share_name = r.in.share_name;
639                 q.out.info = &info;
640
641                 status = dcerpc_srvsvc_NetShareGetInfo(p, tctx, &q);
642                 torture_assert_ntstatus_ok(tctx, status, "NetShareGetInfo failed");
643                 torture_assert_werr_ok(tctx, q.out.result, "NetShareGetInfo failed");
644
645                 torture_assert_str_equal(tctx, q.out.info->info502->name, r.in.share_name,
646                                          "share name invalid");
647
648                 switch (levels[i].level) {
649                 case 0:
650                         break;
651                 case 1:
652                         torture_assert_str_equal(tctx, q.out.info->info502->comment, "test comment 1", "comment");
653                         break;
654                 case 2:
655                         torture_assert_str_equal(tctx, q.out.info->info2->comment, "test comment 2", "comment");
656                         torture_assert_int_equal(tctx, q.out.info->info2->max_users, 2, "max users");
657                         torture_assert_str_equal(tctx, q.out.info->info2->path, "C:\\", "path");
658                         break;
659                 case 501:
660                         torture_assert_str_equal(tctx, q.out.info->info501->comment, "test comment 501", "comment");
661                         break;
662                 case 502:
663                         torture_assert_str_equal(tctx, q.out.info->info502->comment, "test comment 502", "comment");
664                         torture_assert_int_equal(tctx, q.out.info->info502->max_users, 502, "max users");
665                         torture_assert_str_equal(tctx, q.out.info->info502->path, "C:\\", "path");
666                         break;
667                 case 1004:
668                         torture_assert_str_equal(tctx, q.out.info->info1004->comment, "test comment 1004",
669                                                  "comment");
670                         break;
671                 case 1005:
672                         break;
673                 case 1006:
674                         torture_assert_int_equal(tctx, q.out.info->info1006->max_users, 1006, "Max users");
675                         break;
676 /*              case 1007:
677                         break;
678 */
679                 case 1501:
680                         break;
681                 }
682         }
683
684         d.in.share_name = r.in.share_name;
685         d.in.reserved = 0;
686
687         status = dcerpc_srvsvc_NetShareDel(p, tctx, &d);
688         torture_assert_ntstatus_ok(tctx, status, "NetShareDel on share 'testshare502' failed");
689         torture_assert_werr_ok(tctx, a.out.result, "NetShareDel on share 'testshare502' failed");
690
691         return true;
692 }
693
694 /**************************/
695 /* srvsvc_NetShare        */
696 /**************************/
697 static bool test_NetShareEnumAll(struct torture_context *tctx, 
698                                  struct dcerpc_pipe *p, 
699                                  bool admin)
700 {
701         NTSTATUS status;
702         struct srvsvc_NetShareEnumAll r;
703         struct srvsvc_NetShareCtr0 c0;
704         struct {
705                 uint32_t level;
706                 WERROR anon_status;
707                 WERROR admin_status;
708         } levels[] = {
709                  { 0,   WERR_OK,                WERR_OK },
710                  { 1,   WERR_OK,                WERR_OK },
711                  { 2,   WERR_ACCESS_DENIED,     WERR_OK },
712                  { 501, WERR_ACCESS_DENIED,     WERR_OK },
713                  { 502, WERR_ACCESS_DENIED,     WERR_OK },
714         };
715         int i;
716         uint32_t resume_handle;
717
718         ZERO_STRUCT(c0);
719
720         r.in.server_unc = talloc_asprintf(tctx,"\\\\%s",dcerpc_server_name(p));
721         r.in.ctr.ctr0 = &c0;
722         r.in.max_buffer = (uint32_t)-1;
723         r.in.resume_handle = &resume_handle;
724         r.out.resume_handle = &resume_handle;
725
726         for (i=0;i<ARRAY_SIZE(levels);i++) {
727                 int j;
728                 WERROR expected;
729
730                 r.in.level = levels[i].level;
731                 expected = levels[i].anon_status;
732                 if (admin) expected = levels[i].admin_status;
733
734                 ZERO_STRUCT(r.out);
735                 resume_handle = 0;
736
737                 torture_comment(tctx, "testing NetShareEnumAll level %u\n", r.in.level);
738                 status = dcerpc_srvsvc_NetShareEnumAll(p, tctx, &r);
739                 torture_assert_ntstatus_ok(tctx, status, "NetShareEnumAll failed");
740                 torture_assert_werr_equal(tctx, r.out.result, expected, "NetShareEnumAll failed");
741
742                 /* call srvsvc_NetShareGetInfo for each returned share */
743                 if (r.in.level == 2 && r.out.ctr.ctr2) {
744                         for (j=0;j<r.out.ctr.ctr2->count;j++) {
745                                 const char *name;
746                                 name = r.out.ctr.ctr2->array[j].name;
747                                 if (!test_NetShareGetInfo(tctx, p, name, admin)) {
748                                         return false;
749                                 }
750                         }
751                 }
752         }
753
754         return true;
755 }
756
757 static bool test_NetShareEnumAllFull(struct torture_context *tctx,
758                               struct dcerpc_pipe *p)
759 {
760         return test_NetShareEnumAll(tctx, p, true);
761 }
762
763 static bool test_NetShareEnumAllAnon(struct torture_context *tctx,
764                               struct dcerpc_pipe *p)
765 {
766         return test_NetShareEnumAll(tctx, p, false);
767 }
768
769 static bool test_NetShareEnum(struct torture_context *tctx,
770                               struct dcerpc_pipe *p, bool admin)
771 {
772         NTSTATUS status;
773         struct srvsvc_NetShareEnum r;
774         struct srvsvc_NetShareCtr0 c0;
775         struct {
776                 uint32_t level;
777                 WERROR anon_status;
778                 WERROR admin_status;
779         } levels[] = {
780                  { 0,   WERR_OK,                WERR_OK },
781                  { 1,   WERR_OK,                WERR_OK },
782                  { 2,   WERR_ACCESS_DENIED,     WERR_OK },
783                  { 501, WERR_UNKNOWN_LEVEL,     WERR_UNKNOWN_LEVEL },
784                  { 502, WERR_ACCESS_DENIED,     WERR_OK },
785         };
786         int i;
787
788         r.in.server_unc = talloc_asprintf(tctx,"\\\\%s",dcerpc_server_name(p));
789         r.in.ctr.ctr0 = &c0;
790         r.in.ctr.ctr0->count = 0;
791         r.in.ctr.ctr0->array = NULL;
792         r.in.max_buffer = (uint32_t)-1;
793         r.in.resume_handle = NULL;
794
795         for (i=0;i<ARRAY_SIZE(levels);i++) {
796                 WERROR expected;
797
798                 r.in.level = levels[i].level;
799                 expected = levels[i].anon_status;
800                 if (admin) expected = levels[i].admin_status;
801
802                 ZERO_STRUCT(r.out);
803
804                 torture_comment(tctx, "testing NetShareEnum level %u\n", r.in.level);
805                 status = dcerpc_srvsvc_NetShareEnum(p, tctx, &r);
806                 torture_assert_ntstatus_ok(tctx, status, "NetShareEnum failed");
807                 torture_assert_werr_equal(tctx, r.out.result, expected, "NetShareEnum failed");
808         }
809
810         return true;
811 }
812
813 static bool test_NetShareEnumFull(struct torture_context *tctx,
814                                   struct dcerpc_pipe *p)
815 {
816         return test_NetShareEnum(tctx, p, true);
817 }
818
819 static bool test_NetShareEnumAnon(struct torture_context *tctx,
820                                   struct dcerpc_pipe *p)
821 {
822         return test_NetShareEnum(tctx, p, false);
823 }
824
825 /**************************/
826 /* srvsvc_NetSrv          */
827 /**************************/
828 static bool test_NetSrvGetInfo(struct torture_context *tctx, 
829                                struct dcerpc_pipe *p)
830 {
831         NTSTATUS status;
832         struct srvsvc_NetSrvGetInfo r;
833         union srvsvc_NetSrvInfo info;
834         uint32_t levels[] = {100, 101, 102, 502, 503};
835         int i;
836
837         r.in.server_unc = talloc_asprintf(tctx,"\\\\%s",dcerpc_server_name(p));
838
839         for (i=0;i<ARRAY_SIZE(levels);i++) {
840                 r.in.level = levels[i];
841                 r.out.info = &info;
842                 torture_comment(tctx, "testing NetSrvGetInfo level %u\n", r.in.level);
843                 status = dcerpc_srvsvc_NetSrvGetInfo(p, tctx, &r);
844                 torture_assert_ntstatus_ok(tctx, status, "NetSrvGetInfo failed");
845                 if (!W_ERROR_IS_OK(r.out.result)) {
846                         torture_comment(tctx, "NetSrvGetInfo failed: %s\n", win_errstr(r.out.result));
847                 }
848         }
849
850         return true;
851 }
852
853 /**************************/
854 /* srvsvc_NetDisk         */
855 /**************************/
856 static bool test_NetDiskEnum(struct torture_context *tctx, 
857                              struct dcerpc_pipe *p)
858 {
859         NTSTATUS status;
860         struct srvsvc_NetDiskEnum r;
861         struct srvsvc_NetDiskInfo info;
862         uint32_t totalentries = 0;
863         uint32_t levels[] = {0};
864         int i;
865         uint32_t resume_handle=0;
866
867         ZERO_STRUCT(info);
868
869         r.in.server_unc = NULL;
870         r.in.resume_handle = &resume_handle;
871         r.in.info = &info;
872         r.out.info = &info;
873         r.out.totalentries = &totalentries;
874         r.out.resume_handle = &resume_handle;
875
876         for (i=0;i<ARRAY_SIZE(levels);i++) {
877                 ZERO_STRUCTP(r.out.info);
878                 r.in.level = levels[i];
879                 torture_comment(tctx, "testing NetDiskEnum level %u\n", r.in.level);
880                 status = dcerpc_srvsvc_NetDiskEnum(p, tctx, &r);
881                 torture_assert_ntstatus_ok(tctx, status, "NetDiskEnum failed");
882                 torture_assert_werr_ok(tctx, r.out.result, "NetDiskEnum failed");
883         }
884
885         return true;
886 }
887
888 /**************************/
889 /* srvsvc_NetTransport    */
890 /**************************/
891 static bool test_NetTransportEnum(struct torture_context *tctx, 
892                                   struct dcerpc_pipe *p)
893 {
894         NTSTATUS status;
895         struct srvsvc_NetTransportEnum r;
896         struct srvsvc_NetTransportInfoCtr transports;
897         struct srvsvc_NetTransportCtr0 ctr0;
898         struct srvsvc_NetTransportCtr1 ctr1;
899
900         uint32_t totalentries = 0;
901         uint32_t levels[] = {0, 1};
902         int i;
903
904         ZERO_STRUCT(transports);
905
906         r.in.server_unc = talloc_asprintf(tctx,"\\\\%s", dcerpc_server_name(p));
907         r.in.transports = &transports;
908         r.in.max_buffer = (uint32_t)-1;
909         r.in.resume_handle = NULL;
910         r.out.totalentries = &totalentries;
911         r.out.transports = &transports;
912
913         for (i=0;i<ARRAY_SIZE(levels);i++) {
914                 transports.level = levels[i];
915                 switch (transports.level) {
916                 case 0:
917                         ZERO_STRUCT(ctr0);
918                         transports.ctr.ctr0 = &ctr0;
919                         break;
920                 case 1:
921                         ZERO_STRUCT(ctr1);
922                         transports.ctr.ctr1 = &ctr1;
923                         break;
924                 }
925                 torture_comment(tctx, "testing NetTransportEnum level %u\n", transports.level);
926                 status = dcerpc_srvsvc_NetTransportEnum(p, tctx, &r);
927                 torture_assert_ntstatus_ok(tctx, status, "NetTransportEnum failed");
928                 if (!W_ERROR_IS_OK(r.out.result)) {
929                         torture_comment(tctx, "unexpected result: %s\n", win_errstr(r.out.result));
930                 }
931         }
932
933         return true;
934 }
935
936 /**************************/
937 /* srvsvc_NetRemoteTOD    */
938 /**************************/
939 static bool test_NetRemoteTOD(struct torture_context *tctx, 
940                               struct dcerpc_pipe *p)
941 {
942         NTSTATUS status;
943         struct srvsvc_NetRemoteTOD r;
944         struct srvsvc_NetRemoteTODInfo *info = NULL;
945
946         r.in.server_unc = talloc_asprintf(tctx,"\\\\%s",dcerpc_server_name(p));
947         r.out.info = &info;
948
949         torture_comment(tctx, "testing NetRemoteTOD\n");
950         status = dcerpc_srvsvc_NetRemoteTOD(p, tctx, &r);
951         torture_assert_ntstatus_ok(tctx, status, "NetRemoteTOD failed");
952         torture_assert_werr_ok(tctx, r.out.result, "NetRemoteTOD failed");
953
954         return true;
955 }
956
957 /**************************/
958 /* srvsvc_NetName         */
959 /**************************/
960
961 static bool test_NetNameValidate(struct torture_context *tctx, 
962                                                                  struct dcerpc_pipe *p)
963 {
964         NTSTATUS status;
965         struct srvsvc_NetNameValidate r;
966         char *invalidc;
967         char *name;
968         int i, n, min, max;
969
970         r.in.server_unc = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
971         r.in.flags = 0x0;
972
973         d_printf("testing NetNameValidate\n");
974
975         /* valid path types only between 1 and 13 */
976         for (i = 1; i < 14; i++) {
977
978 again:
979                 /* let's limit ourselves to a maximum of 4096 bytes */
980                 r.in.name = name = talloc_array(tctx, char, 4097);
981                 max = 4096;
982                 min = 0;
983                 n = max;
984
985                 while (1) {
986
987                         /* Find maximum length accepted by this type */
988                         ZERO_STRUCT(r.out);
989                         r.in.name_type = i;
990                         memset(name, 'A', n);
991                         name[n] = '\0';
992
993                         status = dcerpc_srvsvc_NetNameValidate(p, tctx, &r);
994                         if (!NT_STATUS_IS_OK(status)) {
995                                 d_printf("NetNameValidate failed while checking maximum size (%s)\n",
996                                                 nt_errstr(status));
997                                 break;
998                         }
999
1000                         if (W_ERROR_IS_OK(r.out.result)) {
1001                                 min = n;
1002                                 n += (max - min + 1)/2;
1003                                 continue;
1004                                 
1005                         } else {
1006                                 if ((min + 1) >= max) break; /* found it */
1007                                 
1008                                 max = n;
1009                                 n -= (max - min)/2;
1010                                 continue;
1011                         }
1012                 }
1013
1014                 talloc_free(name);
1015
1016                 d_printf("Maximum length for type %2d, flags %08x: %d\n", i, r.in.flags, max);
1017
1018                 /* find invalid chars for this type check only ASCII between 0x20 and 0x7e */
1019
1020                 invalidc = talloc_strdup(tctx, "");
1021
1022                 for (n = 0x20; n < 0x7e; n++) {
1023                         r.in.name = name = talloc_asprintf(tctx, "%c", (char)n);
1024
1025                         status = dcerpc_srvsvc_NetNameValidate(p, tctx, &r);
1026                         if (!NT_STATUS_IS_OK(status)) {
1027                                 d_printf("NetNameValidate failed while checking valid chars (%s)\n",
1028                                                 nt_errstr(status));
1029                                 break;
1030                         }
1031
1032                         if (!W_ERROR_IS_OK(r.out.result)) {
1033                                 invalidc = talloc_asprintf_append_buffer(invalidc, "%c", (char)n);
1034                         }
1035
1036                         talloc_free(name);
1037                 }
1038
1039                 d_printf(" Invalid chars for type %2d, flags %08x: \"%s\"\n", i, r.in.flags, invalidc);
1040
1041                 /* only two values are accepted for flags: 0x0 and 0x80000000 */
1042                 if (r.in.flags == 0x0) {
1043                         r.in.flags = 0x80000000;
1044                         goto again;
1045                 }
1046
1047                 r.in.flags = 0x0;
1048         }
1049
1050         return true;
1051 }
1052
1053 struct torture_suite *torture_rpc_srvsvc(TALLOC_CTX *mem_ctx)
1054 {
1055         struct torture_suite *suite = torture_suite_create(mem_ctx, "SRVSVC");
1056         struct torture_rpc_tcase *tcase;
1057         struct torture_test *test;
1058
1059         tcase = torture_suite_add_rpc_iface_tcase(suite, "srvsvc (admin access)", &ndr_table_srvsvc);
1060
1061         torture_rpc_tcase_add_test(tcase, "NetCharDevEnum", test_NetCharDevEnum);
1062         torture_rpc_tcase_add_test(tcase, "NetCharDevQEnum", test_NetCharDevQEnum);
1063         torture_rpc_tcase_add_test(tcase, "NetConnEnum", test_NetConnEnum);
1064         torture_rpc_tcase_add_test(tcase, "NetFileEnum", test_NetFileEnum);
1065         torture_rpc_tcase_add_test(tcase, "NetSessEnum", test_NetSessEnum);
1066         torture_rpc_tcase_add_test(tcase, "NetShareEnumAll", test_NetShareEnumAllFull);
1067         torture_rpc_tcase_add_test(tcase, "NetSrvGetInfo", test_NetSrvGetInfo);
1068         torture_rpc_tcase_add_test(tcase, "NetDiskEnum", test_NetDiskEnum);
1069         torture_rpc_tcase_add_test(tcase, "NetTransportEnum", test_NetTransportEnum);
1070         torture_rpc_tcase_add_test(tcase, "NetRemoteTOD", test_NetRemoteTOD);
1071         torture_rpc_tcase_add_test(tcase, "NetShareEnum", test_NetShareEnumFull);
1072         torture_rpc_tcase_add_test(tcase, "NetShareGetInfo", test_NetShareGetInfoAdminFull);
1073         test = torture_rpc_tcase_add_test(tcase, "NetShareAddSetDel", 
1074                                            test_NetShareAddSetDel);
1075         test->dangerous = true;
1076         torture_rpc_tcase_add_test(tcase, "NetNameValidate", test_NetNameValidate);
1077         
1078         tcase = torture_suite_add_anon_rpc_iface_tcase(suite, 
1079                                                     "srvsvc anonymous access", 
1080                                                     &ndr_table_srvsvc);
1081
1082         torture_rpc_tcase_add_test(tcase, "NetShareEnumAll", 
1083                                    test_NetShareEnumAllAnon);
1084         torture_rpc_tcase_add_test(tcase, "NetShareEnum", 
1085                                    test_NetShareEnumAnon);
1086         torture_rpc_tcase_add_test(tcase, "NetShareGetInfo", 
1087                                    test_NetShareGetInfoAdminAnon);
1088
1089         return suite;
1090 }