s4-srvsvc: merge srvsvc_NetSessEnum 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_NetSessInfoCtr info_ctr;
395         struct srvsvc_NetSessCtr0 c0;
396         struct srvsvc_NetSessCtr1 c1;
397         struct srvsvc_NetSessCtr2 c2;
398         struct srvsvc_NetSessCtr10 c10;
399         struct srvsvc_NetSessCtr502 c502;
400         uint32_t totalentries = 0;
401         uint32_t levels[] = {0, 1, 2, 10, 502};
402         int i;
403
404         ZERO_STRUCT(info_ctr);
405
406         r.in.server_unc = talloc_asprintf(tctx,"\\\\%s",dcerpc_server_name(p));
407         r.in.client = NULL;
408         r.in.user = NULL;
409         r.in.info_ctr = &info_ctr;
410         r.in.max_buffer = (uint32_t)-1;
411         r.in.resume_handle = NULL;
412         r.out.totalentries = &totalentries;
413         r.out.info_ctr = &info_ctr;
414
415         for (i=0;i<ARRAY_SIZE(levels);i++) {
416                 info_ctr.level = levels[i];
417
418                 switch (info_ctr.level) {
419                 case 0:
420                         ZERO_STRUCT(c0);
421                         info_ctr.ctr.ctr0 = &c0;
422                         break;
423                 case 1:
424                         ZERO_STRUCT(c1);
425                         info_ctr.ctr.ctr1 = &c1;
426                         break;
427                 case 2:
428                         ZERO_STRUCT(c2);
429                         info_ctr.ctr.ctr2 = &c2;
430                         break;
431                 case 10:
432                         ZERO_STRUCT(c10);
433                         info_ctr.ctr.ctr10 = &c10;
434                         break;
435                 case 502:
436                         ZERO_STRUCT(c502);
437                         info_ctr.ctr.ctr502 = &c502;
438                         break;
439                 }
440
441                 torture_comment(tctx, "testing NetSessEnum level %u\n", info_ctr.level);
442                 status = dcerpc_srvsvc_NetSessEnum(p, tctx, &r);
443                 torture_assert_ntstatus_ok(tctx, status, "NetSessEnum failed");
444                 if (!W_ERROR_IS_OK(r.out.result)) {
445                         torture_comment(tctx, "NetSessEnum failed: %s\n", win_errstr(r.out.result));
446                 }
447         }
448
449         return true;
450 }
451
452 /**************************/
453 /* srvsvc_NetShare        */
454 /**************************/
455 static bool test_NetShareCheck(struct dcerpc_pipe *p, struct torture_context *tctx,
456                                const char *device_name)
457 {
458         NTSTATUS status;
459         struct srvsvc_NetShareCheck r;
460         enum srvsvc_ShareType type;
461
462         r.in.server_unc = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
463         r.in.device_name = device_name;
464         r.out.type = &type;
465
466         torture_comment(tctx, 
467                         "testing NetShareCheck on device '%s'\n", r.in.device_name);
468
469         status = dcerpc_srvsvc_NetShareCheck(p, tctx, &r);
470         torture_assert_ntstatus_ok(tctx, status, "dcerpc_srvsvc_NetShareCheck failed");
471         torture_assert_werr_ok(tctx, r.out.result, "NetShareCheck failed");
472
473         return true;
474 }
475
476 static bool test_NetShareGetInfo(struct torture_context *tctx, 
477                                  struct dcerpc_pipe *p,
478                                  const char *sharename, bool admin)
479 {
480         NTSTATUS status;
481         struct srvsvc_NetShareGetInfo r;
482         union srvsvc_NetShareInfo info;
483         struct {
484                 uint32_t level;
485                 WERROR anon_status;
486                 WERROR admin_status;
487         } levels[] = {
488                  { 0,           WERR_OK,                WERR_OK },
489                  { 1,           WERR_OK,                WERR_OK },
490                  { 2,           WERR_ACCESS_DENIED,     WERR_OK },
491                  { 501,         WERR_OK,                WERR_OK },
492                  { 502,         WERR_ACCESS_DENIED,     WERR_OK },
493                  { 1005,        WERR_OK,                WERR_OK },
494         };
495         int i;
496
497         r.in.server_unc = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
498         r.in.share_name = sharename;
499         r.out.info = &info;
500
501         for (i=0;i<ARRAY_SIZE(levels);i++) {
502                 WERROR expected;
503
504                 r.in.level = levels[i].level;
505                 expected = levels[i].anon_status;
506                 if (admin) expected = levels[i].admin_status;
507
508                 torture_comment(tctx, "testing NetShareGetInfo level %u on share '%s'\n", 
509                        r.in.level, r.in.share_name);
510
511                 status = dcerpc_srvsvc_NetShareGetInfo(p, tctx, &r);
512                 torture_assert_ntstatus_ok(tctx, status, "NetShareGetInfo failed");
513                 torture_assert_werr_equal(tctx, r.out.result, expected, "NetShareGetInfo failed");
514
515                 if (r.in.level != 2) continue;
516                 if (!r.out.info->info2 || !r.out.info->info2->path) continue;
517                 if (!test_NetShareCheck(p, tctx, r.out.info->info2->path)) {
518                         return false;
519                 }
520         }
521
522         return true;
523 }
524
525 static bool test_NetShareGetInfoAdminFull(struct torture_context *tctx, 
526                                           struct dcerpc_pipe *p)
527 {
528         return test_NetShareGetInfo(tctx, p, "ADMIN$", true);
529 }
530
531 static bool test_NetShareGetInfoAdminAnon(struct torture_context *tctx, 
532                                           struct dcerpc_pipe *p)
533 {
534         return test_NetShareGetInfo(tctx, p, "ADMIN$", false);
535 }
536
537 static bool test_NetShareAddSetDel(struct torture_context *tctx, 
538                                    struct dcerpc_pipe *p)
539 {
540         NTSTATUS status;
541         struct srvsvc_NetShareAdd a;
542         struct srvsvc_NetShareSetInfo r;
543         struct srvsvc_NetShareGetInfo q;
544         struct srvsvc_NetShareDel d;
545         struct sec_desc_buf sd_buf;
546         union srvsvc_NetShareInfo info;
547         struct {
548                 uint32_t level;
549                 WERROR expected;
550         } levels[] = {
551                  { 0,           WERR_UNKNOWN_LEVEL },
552                  { 1,           WERR_OK },
553                  { 2,           WERR_OK },
554                  { 501,         WERR_UNKNOWN_LEVEL },
555                  { 502,         WERR_OK },
556                  { 1004,        WERR_OK },
557                  { 1005,        WERR_OK },
558                  { 1006,        WERR_OK },
559 /*               { 1007,        WERR_OK }, */
560                  { 1501,        WERR_OK },
561         };
562         int i;
563
564         a.in.server_unc = r.in.server_unc = q.in.server_unc = d.in.server_unc =
565                 talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
566         r.in.share_name = talloc_strdup(tctx, "testshare");
567
568         info.info2 = talloc(tctx, struct srvsvc_NetShareInfo2);
569         info.info2->name = r.in.share_name;
570         info.info2->type = STYPE_DISKTREE;
571         info.info2->comment = talloc_strdup(tctx, "test comment");
572         info.info2->permissions = 123434566;
573         info.info2->max_users = -1;
574         info.info2->current_users = 0;
575         info.info2->path = talloc_strdup(tctx, "C:\\");
576         info.info2->password = NULL;
577
578         a.in.info = &info;
579         a.in.level = 2;
580         a.in.parm_error = NULL;
581
582         status = dcerpc_srvsvc_NetShareAdd(p, tctx, &a);
583         torture_assert_ntstatus_ok(tctx, status, "NetShareAdd level 2 on share 'testshare' failed");
584         torture_assert_werr_ok(tctx, a.out.result, "NetShareAdd level 2 on share 'testshare' failed");
585
586         r.in.parm_error = NULL;
587
588         q.in.level = 502;
589
590         for (i = 0; i < ARRAY_SIZE(levels); i++) {
591
592                 r.in.level = levels[i].level;
593                 ZERO_STRUCT(r.out);
594
595                 torture_comment(tctx, "testing NetShareSetInfo level %u on share '%s'\n", 
596                        r.in.level, r.in.share_name);
597
598                 switch (levels[i].level) {
599                 case 0:
600                         info.info0 = talloc(tctx, struct srvsvc_NetShareInfo0);
601                         info.info0->name = r.in.share_name;
602                         break;
603                 case 1:
604                         info.info1 = talloc(tctx, struct srvsvc_NetShareInfo1);
605                         info.info1->name = r.in.share_name;
606                         info.info1->type = STYPE_DISKTREE;
607                         info.info1->comment = talloc_strdup(tctx, "test comment 1");
608                         break;
609                 case 2: 
610                         info.info2 = talloc(tctx, struct srvsvc_NetShareInfo2);
611                         info.info2->name = r.in.share_name;
612                         info.info2->type = STYPE_DISKTREE;
613                         info.info2->comment = talloc_strdup(tctx, "test comment 2");
614                         info.info2->permissions = 0;
615                         info.info2->max_users = 2;
616                         info.info2->current_users = 1;
617                         info.info2->path = talloc_strdup(tctx, "::BLaH::"); /* "C:\\"); */
618                         info.info2->password = NULL;
619                         break;
620                 case 501:
621                         info.info501 = talloc(tctx, struct srvsvc_NetShareInfo501);
622                         info.info501->name = r.in.share_name;
623                         info.info501->type = STYPE_DISKTREE;
624                         info.info501->comment = talloc_strdup(tctx, "test comment 501");
625                         info.info501->csc_policy = 0;
626                         break;
627                 case 502:
628                         ZERO_STRUCT(sd_buf);
629                         info.info502 = talloc(tctx, struct srvsvc_NetShareInfo502);
630                         info.info502->name = r.in.share_name;
631                         info.info502->type = STYPE_DISKTREE;
632                         info.info502->comment = talloc_strdup(tctx, "test comment 502");
633                         info.info502->permissions = 0;
634                         info.info502->max_users = 502;
635                         info.info502->current_users = 1;
636                         info.info502->path = talloc_strdup(tctx, "C:\\");
637                         info.info502->password = NULL;
638                         info.info502->sd_buf = sd_buf;
639                         break;
640                 case 1004:
641                         info.info1004 = talloc(tctx, struct srvsvc_NetShareInfo1004);
642                         info.info1004->comment = talloc_strdup(tctx, "test comment 1004");
643                         break;
644                 case 1005:
645                         info.info1005 = talloc(tctx, struct srvsvc_NetShareInfo1005);
646                         info.info1005->dfs_flags = 0;
647                         break;
648                 case 1006:
649                         info.info1006 = talloc(tctx, struct srvsvc_NetShareInfo1006);
650                         info.info1006->max_users = 1006;
651                         break;
652 /*              case 1007:
653                         info.info1007 = talloc(tctx, struct srvsvc_NetShareInfo1007);
654                         info.info1007->flags = 0;
655                         info.info1007->alternate_directory_name = talloc_strdup(tctx, "test");
656                         break;
657 */
658                 case 1501:
659                         info.info1501 = talloc_zero(tctx, struct sec_desc_buf);
660                         break;
661                 }
662
663                 r.in.info = &info;
664
665                 status = dcerpc_srvsvc_NetShareSetInfo(p, tctx, &r);
666                 torture_assert_ntstatus_ok(tctx, status, "NetShareGetInfo failed");
667                 torture_assert_werr_equal(tctx, r.out.result, levels[i].expected, "NetShareSetInfo failed");
668                 
669                 q.in.share_name = r.in.share_name;
670                 q.out.info = &info;
671
672                 status = dcerpc_srvsvc_NetShareGetInfo(p, tctx, &q);
673                 torture_assert_ntstatus_ok(tctx, status, "NetShareGetInfo failed");
674                 torture_assert_werr_ok(tctx, q.out.result, "NetShareGetInfo failed");
675
676                 torture_assert_str_equal(tctx, q.out.info->info502->name, r.in.share_name,
677                                          "share name invalid");
678
679                 switch (levels[i].level) {
680                 case 0:
681                         break;
682                 case 1:
683                         torture_assert_str_equal(tctx, q.out.info->info502->comment, "test comment 1", "comment");
684                         break;
685                 case 2:
686                         torture_assert_str_equal(tctx, q.out.info->info2->comment, "test comment 2", "comment");
687                         torture_assert_int_equal(tctx, q.out.info->info2->max_users, 2, "max users");
688                         torture_assert_str_equal(tctx, q.out.info->info2->path, "C:\\", "path");
689                         break;
690                 case 501:
691                         torture_assert_str_equal(tctx, q.out.info->info501->comment, "test comment 501", "comment");
692                         break;
693                 case 502:
694                         torture_assert_str_equal(tctx, q.out.info->info502->comment, "test comment 502", "comment");
695                         torture_assert_int_equal(tctx, q.out.info->info502->max_users, 502, "max users");
696                         torture_assert_str_equal(tctx, q.out.info->info502->path, "C:\\", "path");
697                         break;
698                 case 1004:
699                         torture_assert_str_equal(tctx, q.out.info->info1004->comment, "test comment 1004",
700                                                  "comment");
701                         break;
702                 case 1005:
703                         break;
704                 case 1006:
705                         torture_assert_int_equal(tctx, q.out.info->info1006->max_users, 1006, "Max users");
706                         break;
707 /*              case 1007:
708                         break;
709 */
710                 case 1501:
711                         break;
712                 }
713         }
714
715         d.in.share_name = r.in.share_name;
716         d.in.reserved = 0;
717
718         status = dcerpc_srvsvc_NetShareDel(p, tctx, &d);
719         torture_assert_ntstatus_ok(tctx, status, "NetShareDel on share 'testshare502' failed");
720         torture_assert_werr_ok(tctx, a.out.result, "NetShareDel on share 'testshare502' failed");
721
722         return true;
723 }
724
725 /**************************/
726 /* srvsvc_NetShare        */
727 /**************************/
728 static bool test_NetShareEnumAll(struct torture_context *tctx, 
729                                  struct dcerpc_pipe *p, 
730                                  bool admin)
731 {
732         NTSTATUS status;
733         struct srvsvc_NetShareEnumAll r;
734         struct srvsvc_NetShareCtr0 c0;
735         struct {
736                 uint32_t level;
737                 WERROR anon_status;
738                 WERROR admin_status;
739         } levels[] = {
740                  { 0,   WERR_OK,                WERR_OK },
741                  { 1,   WERR_OK,                WERR_OK },
742                  { 2,   WERR_ACCESS_DENIED,     WERR_OK },
743                  { 501, WERR_ACCESS_DENIED,     WERR_OK },
744                  { 502, WERR_ACCESS_DENIED,     WERR_OK },
745         };
746         int i;
747         uint32_t resume_handle;
748
749         ZERO_STRUCT(c0);
750
751         r.in.server_unc = talloc_asprintf(tctx,"\\\\%s",dcerpc_server_name(p));
752         r.in.ctr.ctr0 = &c0;
753         r.in.max_buffer = (uint32_t)-1;
754         r.in.resume_handle = &resume_handle;
755         r.out.resume_handle = &resume_handle;
756
757         for (i=0;i<ARRAY_SIZE(levels);i++) {
758                 int j;
759                 WERROR expected;
760
761                 r.in.level = levels[i].level;
762                 expected = levels[i].anon_status;
763                 if (admin) expected = levels[i].admin_status;
764
765                 ZERO_STRUCT(r.out);
766                 resume_handle = 0;
767
768                 torture_comment(tctx, "testing NetShareEnumAll level %u\n", r.in.level);
769                 status = dcerpc_srvsvc_NetShareEnumAll(p, tctx, &r);
770                 torture_assert_ntstatus_ok(tctx, status, "NetShareEnumAll failed");
771                 torture_assert_werr_equal(tctx, r.out.result, expected, "NetShareEnumAll failed");
772
773                 /* call srvsvc_NetShareGetInfo for each returned share */
774                 if (r.in.level == 2 && r.out.ctr.ctr2) {
775                         for (j=0;j<r.out.ctr.ctr2->count;j++) {
776                                 const char *name;
777                                 name = r.out.ctr.ctr2->array[j].name;
778                                 if (!test_NetShareGetInfo(tctx, p, name, admin)) {
779                                         return false;
780                                 }
781                         }
782                 }
783         }
784
785         return true;
786 }
787
788 static bool test_NetShareEnumAllFull(struct torture_context *tctx,
789                               struct dcerpc_pipe *p)
790 {
791         return test_NetShareEnumAll(tctx, p, true);
792 }
793
794 static bool test_NetShareEnumAllAnon(struct torture_context *tctx,
795                               struct dcerpc_pipe *p)
796 {
797         return test_NetShareEnumAll(tctx, p, false);
798 }
799
800 static bool test_NetShareEnum(struct torture_context *tctx,
801                               struct dcerpc_pipe *p, bool admin)
802 {
803         NTSTATUS status;
804         struct srvsvc_NetShareEnum r;
805         struct srvsvc_NetShareCtr0 c0;
806         struct {
807                 uint32_t level;
808                 WERROR anon_status;
809                 WERROR admin_status;
810         } levels[] = {
811                  { 0,   WERR_OK,                WERR_OK },
812                  { 1,   WERR_OK,                WERR_OK },
813                  { 2,   WERR_ACCESS_DENIED,     WERR_OK },
814                  { 501, WERR_UNKNOWN_LEVEL,     WERR_UNKNOWN_LEVEL },
815                  { 502, WERR_ACCESS_DENIED,     WERR_OK },
816         };
817         int i;
818
819         r.in.server_unc = talloc_asprintf(tctx,"\\\\%s",dcerpc_server_name(p));
820         r.in.ctr.ctr0 = &c0;
821         r.in.ctr.ctr0->count = 0;
822         r.in.ctr.ctr0->array = NULL;
823         r.in.max_buffer = (uint32_t)-1;
824         r.in.resume_handle = NULL;
825
826         for (i=0;i<ARRAY_SIZE(levels);i++) {
827                 WERROR expected;
828
829                 r.in.level = levels[i].level;
830                 expected = levels[i].anon_status;
831                 if (admin) expected = levels[i].admin_status;
832
833                 ZERO_STRUCT(r.out);
834
835                 torture_comment(tctx, "testing NetShareEnum level %u\n", r.in.level);
836                 status = dcerpc_srvsvc_NetShareEnum(p, tctx, &r);
837                 torture_assert_ntstatus_ok(tctx, status, "NetShareEnum failed");
838                 torture_assert_werr_equal(tctx, r.out.result, expected, "NetShareEnum failed");
839         }
840
841         return true;
842 }
843
844 static bool test_NetShareEnumFull(struct torture_context *tctx,
845                                   struct dcerpc_pipe *p)
846 {
847         return test_NetShareEnum(tctx, p, true);
848 }
849
850 static bool test_NetShareEnumAnon(struct torture_context *tctx,
851                                   struct dcerpc_pipe *p)
852 {
853         return test_NetShareEnum(tctx, p, false);
854 }
855
856 /**************************/
857 /* srvsvc_NetSrv          */
858 /**************************/
859 static bool test_NetSrvGetInfo(struct torture_context *tctx, 
860                                struct dcerpc_pipe *p)
861 {
862         NTSTATUS status;
863         struct srvsvc_NetSrvGetInfo r;
864         union srvsvc_NetSrvInfo info;
865         uint32_t levels[] = {100, 101, 102, 502, 503};
866         int i;
867
868         r.in.server_unc = talloc_asprintf(tctx,"\\\\%s",dcerpc_server_name(p));
869
870         for (i=0;i<ARRAY_SIZE(levels);i++) {
871                 r.in.level = levels[i];
872                 r.out.info = &info;
873                 torture_comment(tctx, "testing NetSrvGetInfo level %u\n", r.in.level);
874                 status = dcerpc_srvsvc_NetSrvGetInfo(p, tctx, &r);
875                 torture_assert_ntstatus_ok(tctx, status, "NetSrvGetInfo failed");
876                 if (!W_ERROR_IS_OK(r.out.result)) {
877                         torture_comment(tctx, "NetSrvGetInfo failed: %s\n", win_errstr(r.out.result));
878                 }
879         }
880
881         return true;
882 }
883
884 /**************************/
885 /* srvsvc_NetDisk         */
886 /**************************/
887 static bool test_NetDiskEnum(struct torture_context *tctx, 
888                              struct dcerpc_pipe *p)
889 {
890         NTSTATUS status;
891         struct srvsvc_NetDiskEnum r;
892         struct srvsvc_NetDiskInfo info;
893         uint32_t totalentries = 0;
894         uint32_t levels[] = {0};
895         int i;
896         uint32_t resume_handle=0;
897
898         ZERO_STRUCT(info);
899
900         r.in.server_unc = NULL;
901         r.in.resume_handle = &resume_handle;
902         r.in.info = &info;
903         r.out.info = &info;
904         r.out.totalentries = &totalentries;
905         r.out.resume_handle = &resume_handle;
906
907         for (i=0;i<ARRAY_SIZE(levels);i++) {
908                 ZERO_STRUCTP(r.out.info);
909                 r.in.level = levels[i];
910                 torture_comment(tctx, "testing NetDiskEnum level %u\n", r.in.level);
911                 status = dcerpc_srvsvc_NetDiskEnum(p, tctx, &r);
912                 torture_assert_ntstatus_ok(tctx, status, "NetDiskEnum failed");
913                 torture_assert_werr_ok(tctx, r.out.result, "NetDiskEnum failed");
914         }
915
916         return true;
917 }
918
919 /**************************/
920 /* srvsvc_NetTransport    */
921 /**************************/
922 static bool test_NetTransportEnum(struct torture_context *tctx, 
923                                   struct dcerpc_pipe *p)
924 {
925         NTSTATUS status;
926         struct srvsvc_NetTransportEnum r;
927         struct srvsvc_NetTransportInfoCtr transports;
928         struct srvsvc_NetTransportCtr0 ctr0;
929         struct srvsvc_NetTransportCtr1 ctr1;
930
931         uint32_t totalentries = 0;
932         uint32_t levels[] = {0, 1};
933         int i;
934
935         ZERO_STRUCT(transports);
936
937         r.in.server_unc = talloc_asprintf(tctx,"\\\\%s", dcerpc_server_name(p));
938         r.in.transports = &transports;
939         r.in.max_buffer = (uint32_t)-1;
940         r.in.resume_handle = NULL;
941         r.out.totalentries = &totalentries;
942         r.out.transports = &transports;
943
944         for (i=0;i<ARRAY_SIZE(levels);i++) {
945                 transports.level = levels[i];
946                 switch (transports.level) {
947                 case 0:
948                         ZERO_STRUCT(ctr0);
949                         transports.ctr.ctr0 = &ctr0;
950                         break;
951                 case 1:
952                         ZERO_STRUCT(ctr1);
953                         transports.ctr.ctr1 = &ctr1;
954                         break;
955                 }
956                 torture_comment(tctx, "testing NetTransportEnum level %u\n", transports.level);
957                 status = dcerpc_srvsvc_NetTransportEnum(p, tctx, &r);
958                 torture_assert_ntstatus_ok(tctx, status, "NetTransportEnum failed");
959                 if (!W_ERROR_IS_OK(r.out.result)) {
960                         torture_comment(tctx, "unexpected result: %s\n", win_errstr(r.out.result));
961                 }
962         }
963
964         return true;
965 }
966
967 /**************************/
968 /* srvsvc_NetRemoteTOD    */
969 /**************************/
970 static bool test_NetRemoteTOD(struct torture_context *tctx, 
971                               struct dcerpc_pipe *p)
972 {
973         NTSTATUS status;
974         struct srvsvc_NetRemoteTOD r;
975         struct srvsvc_NetRemoteTODInfo *info = NULL;
976
977         r.in.server_unc = talloc_asprintf(tctx,"\\\\%s",dcerpc_server_name(p));
978         r.out.info = &info;
979
980         torture_comment(tctx, "testing NetRemoteTOD\n");
981         status = dcerpc_srvsvc_NetRemoteTOD(p, tctx, &r);
982         torture_assert_ntstatus_ok(tctx, status, "NetRemoteTOD failed");
983         torture_assert_werr_ok(tctx, r.out.result, "NetRemoteTOD failed");
984
985         return true;
986 }
987
988 /**************************/
989 /* srvsvc_NetName         */
990 /**************************/
991
992 static bool test_NetNameValidate(struct torture_context *tctx, 
993                                                                  struct dcerpc_pipe *p)
994 {
995         NTSTATUS status;
996         struct srvsvc_NetNameValidate r;
997         char *invalidc;
998         char *name;
999         int i, n, min, max;
1000
1001         r.in.server_unc = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
1002         r.in.flags = 0x0;
1003
1004         d_printf("testing NetNameValidate\n");
1005
1006         /* valid path types only between 1 and 13 */
1007         for (i = 1; i < 14; i++) {
1008
1009 again:
1010                 /* let's limit ourselves to a maximum of 4096 bytes */
1011                 r.in.name = name = talloc_array(tctx, char, 4097);
1012                 max = 4096;
1013                 min = 0;
1014                 n = max;
1015
1016                 while (1) {
1017
1018                         /* Find maximum length accepted by this type */
1019                         ZERO_STRUCT(r.out);
1020                         r.in.name_type = i;
1021                         memset(name, 'A', n);
1022                         name[n] = '\0';
1023
1024                         status = dcerpc_srvsvc_NetNameValidate(p, tctx, &r);
1025                         if (!NT_STATUS_IS_OK(status)) {
1026                                 d_printf("NetNameValidate failed while checking maximum size (%s)\n",
1027                                                 nt_errstr(status));
1028                                 break;
1029                         }
1030
1031                         if (W_ERROR_IS_OK(r.out.result)) {
1032                                 min = n;
1033                                 n += (max - min + 1)/2;
1034                                 continue;
1035                                 
1036                         } else {
1037                                 if ((min + 1) >= max) break; /* found it */
1038                                 
1039                                 max = n;
1040                                 n -= (max - min)/2;
1041                                 continue;
1042                         }
1043                 }
1044
1045                 talloc_free(name);
1046
1047                 d_printf("Maximum length for type %2d, flags %08x: %d\n", i, r.in.flags, max);
1048
1049                 /* find invalid chars for this type check only ASCII between 0x20 and 0x7e */
1050
1051                 invalidc = talloc_strdup(tctx, "");
1052
1053                 for (n = 0x20; n < 0x7e; n++) {
1054                         r.in.name = name = talloc_asprintf(tctx, "%c", (char)n);
1055
1056                         status = dcerpc_srvsvc_NetNameValidate(p, tctx, &r);
1057                         if (!NT_STATUS_IS_OK(status)) {
1058                                 d_printf("NetNameValidate failed while checking valid chars (%s)\n",
1059                                                 nt_errstr(status));
1060                                 break;
1061                         }
1062
1063                         if (!W_ERROR_IS_OK(r.out.result)) {
1064                                 invalidc = talloc_asprintf_append_buffer(invalidc, "%c", (char)n);
1065                         }
1066
1067                         talloc_free(name);
1068                 }
1069
1070                 d_printf(" Invalid chars for type %2d, flags %08x: \"%s\"\n", i, r.in.flags, invalidc);
1071
1072                 /* only two values are accepted for flags: 0x0 and 0x80000000 */
1073                 if (r.in.flags == 0x0) {
1074                         r.in.flags = 0x80000000;
1075                         goto again;
1076                 }
1077
1078                 r.in.flags = 0x0;
1079         }
1080
1081         return true;
1082 }
1083
1084 struct torture_suite *torture_rpc_srvsvc(TALLOC_CTX *mem_ctx)
1085 {
1086         struct torture_suite *suite = torture_suite_create(mem_ctx, "SRVSVC");
1087         struct torture_rpc_tcase *tcase;
1088         struct torture_test *test;
1089
1090         tcase = torture_suite_add_rpc_iface_tcase(suite, "srvsvc (admin access)", &ndr_table_srvsvc);
1091
1092         torture_rpc_tcase_add_test(tcase, "NetCharDevEnum", test_NetCharDevEnum);
1093         torture_rpc_tcase_add_test(tcase, "NetCharDevQEnum", test_NetCharDevQEnum);
1094         torture_rpc_tcase_add_test(tcase, "NetConnEnum", test_NetConnEnum);
1095         torture_rpc_tcase_add_test(tcase, "NetFileEnum", test_NetFileEnum);
1096         torture_rpc_tcase_add_test(tcase, "NetSessEnum", test_NetSessEnum);
1097         torture_rpc_tcase_add_test(tcase, "NetShareEnumAll", test_NetShareEnumAllFull);
1098         torture_rpc_tcase_add_test(tcase, "NetSrvGetInfo", test_NetSrvGetInfo);
1099         torture_rpc_tcase_add_test(tcase, "NetDiskEnum", test_NetDiskEnum);
1100         torture_rpc_tcase_add_test(tcase, "NetTransportEnum", test_NetTransportEnum);
1101         torture_rpc_tcase_add_test(tcase, "NetRemoteTOD", test_NetRemoteTOD);
1102         torture_rpc_tcase_add_test(tcase, "NetShareEnum", test_NetShareEnumFull);
1103         torture_rpc_tcase_add_test(tcase, "NetShareGetInfo", test_NetShareGetInfoAdminFull);
1104         test = torture_rpc_tcase_add_test(tcase, "NetShareAddSetDel", 
1105                                            test_NetShareAddSetDel);
1106         test->dangerous = true;
1107         torture_rpc_tcase_add_test(tcase, "NetNameValidate", test_NetNameValidate);
1108         
1109         tcase = torture_suite_add_anon_rpc_iface_tcase(suite, 
1110                                                     "srvsvc anonymous access", 
1111                                                     &ndr_table_srvsvc);
1112
1113         torture_rpc_tcase_add_test(tcase, "NetShareEnumAll", 
1114                                    test_NetShareEnumAllAnon);
1115         torture_rpc_tcase_add_test(tcase, "NetShareEnum", 
1116                                    test_NetShareEnumAnon);
1117         torture_rpc_tcase_add_test(tcase, "NetShareGetInfo", 
1118                                    test_NetShareGetInfoAdminAnon);
1119
1120         return suite;
1121 }