r5611: Add EnumPrinterDataEx-test (for now just for PrinterDriverData, we need
[samba.git] / source4 / torture / rpc / spoolss.c
1 /* 
2    Unix SMB/CIFS implementation.
3    test suite for spoolss rpc operations
4
5    Copyright (C) Tim Potter 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 2 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, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23 #include "librpc/gen_ndr/ndr_spoolss.h"
24
25 static BOOL test_GetPrinter(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
26                      struct policy_handle *handle)
27 {
28         NTSTATUS status;
29         struct spoolss_GetPrinter r;
30         uint16_t levels[] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
31         int i;
32         BOOL ret = True;
33         
34         for (i=0;i<ARRAY_SIZE(levels);i++) {
35                 uint32_t buf_size = 0;
36                 r.in.handle = handle;
37                 r.in.level = levels[i];
38                 r.in.buffer = NULL;
39                 r.in.buf_size = &buf_size;
40                 r.out.buf_size = &buf_size;
41
42                 printf("Testing GetPrinter level %u\n", r.in.level);
43
44                 status = dcerpc_spoolss_GetPrinter(p, mem_ctx, &r);
45                 if (!NT_STATUS_IS_OK(status)) {
46                         printf("GetPrinter failed - %s\n", nt_errstr(status));
47                         ret = False;
48                         continue;
49                 }
50                 
51                 if (W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
52                         DATA_BLOB blob = data_blob_talloc(mem_ctx, NULL, buf_size);
53                         data_blob_clear(&blob);
54                         r.in.buffer = &blob;
55                         status = dcerpc_spoolss_GetPrinter(p, mem_ctx, &r);
56                 }
57                 
58                 if (!NT_STATUS_IS_OK(status)) {
59                         printf("GetPrinter failed - %s\n", nt_errstr(status));
60                         ret = False;
61                         continue;
62                 }
63                 
64                 if (!W_ERROR_IS_OK(r.out.result)) {
65                         printf("GetPrinter failed - %s\n", 
66                                win_errstr(r.out.result));
67                         ret = False;
68                         continue;
69                 }
70         }
71
72         return ret;
73 }
74
75
76 static BOOL test_ClosePrinter(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
77                        struct policy_handle *handle)
78 {
79         NTSTATUS status;
80         struct spoolss_ClosePrinter r;
81
82         r.in.handle = handle;
83         r.out.handle = handle;
84
85         printf("Testing ClosePrinter\n");
86
87         status = dcerpc_spoolss_ClosePrinter(p, mem_ctx, &r);
88         if (!NT_STATUS_IS_OK(status)) {
89                 printf("ClosePrinter failed - %s\n", nt_errstr(status));
90                 return False;
91         }
92
93         return True;
94 }
95
96 static BOOL test_GetForm(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
97                          struct policy_handle *handle, 
98                          const char *formname)
99 {
100         NTSTATUS status;
101         struct spoolss_GetForm r;
102         uint32_t buf_size;
103
104         r.in.handle = handle;
105         r.in.formname = formname;
106         r.in.level = 1;
107         r.in.buffer = NULL;
108         buf_size = 0;
109         r.in.buf_size = r.out.buf_size = &buf_size;
110
111         printf("Testing GetForm\n");
112
113         status = dcerpc_spoolss_GetForm(p, mem_ctx, &r);
114
115         if (!NT_STATUS_IS_OK(status)) {
116                 printf("GetForm failed - %s\n", nt_errstr(status));
117                 return False;
118         }
119
120         if (W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
121                 DATA_BLOB blob = data_blob_talloc(mem_ctx, NULL, buf_size);
122
123                 data_blob_clear(&blob);
124                 r.in.buffer = &blob;
125
126                 status = dcerpc_spoolss_GetForm(p, mem_ctx, &r);
127
128                 if (!r.out.info) {
129                         printf("No form info returned");
130                         return False;
131                 }
132         }
133
134         return True;
135 }
136
137 static BOOL test_EnumForms(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
138                     struct policy_handle *handle)
139 {
140         NTSTATUS status;
141         struct spoolss_EnumForms r;
142         uint32_t buf_size;
143
144         r.in.handle = handle;
145         r.in.level = 1;
146         r.in.buffer = NULL;
147         buf_size = 0;
148         r.in.buf_size = &buf_size;
149         r.out.buf_size = &buf_size;
150
151         printf("Testing EnumForms\n");
152
153         status = dcerpc_spoolss_EnumForms(p, mem_ctx, &r);
154
155         if (!NT_STATUS_IS_OK(status)) {
156                 printf("EnumForms failed - %s\n", nt_errstr(status));
157                 return False;
158         }
159
160         if (W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
161                 DATA_BLOB blob = data_blob_talloc(mem_ctx, NULL, buf_size);
162                 union spoolss_FormInfo *info;
163                 int j;
164
165                 data_blob_clear(&blob);
166                 r.in.buffer = &blob;
167
168                 status = dcerpc_spoolss_EnumForms(p, mem_ctx, &r);
169
170                 if (!r.out.info) {
171                         printf("No forms returned");
172                         return False;
173                 }
174
175                 info = *r.out.info;
176
177                 for (j = 0; j < r.out.count; j++) {
178                         test_GetForm(p, mem_ctx, handle, info[j].info1.formname);
179                 }
180         }
181
182         if (!NT_STATUS_IS_OK(status)) {
183                 printf("EnumForms failed - %s\n", nt_errstr(status));
184                 return False;
185         }
186
187         if (!W_ERROR_IS_OK(r.out.result)) {
188                 printf("EnumForms failed - %s\n", win_errstr(r.out.result));
189                 return False;
190         }
191
192         return True;
193 }
194
195 static BOOL test_DeleteForm(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
196                             struct policy_handle *handle, 
197                             const char *formname)
198 {
199         NTSTATUS status;
200         struct spoolss_DeleteForm r;
201
202         r.in.handle = handle;
203         r.in.formname = formname;
204
205         status = dcerpc_spoolss_DeleteForm(p, mem_ctx, &r);
206
207         if (!NT_STATUS_IS_OK(status)) {
208                 printf("DeleteForm failed - %s\n", nt_errstr(status));
209                 return False;
210         }
211
212         if (!W_ERROR_IS_OK(r.out.result)) {
213                 printf("DeleteForm failed - %s\n", win_errstr(r.out.result));
214                 return False;
215         }
216
217         return True;
218 }
219
220 static BOOL test_AddForm(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
221                   struct policy_handle *handle)
222 {
223         struct spoolss_AddForm r;
224         struct spoolss_AddFormInfo1 form;
225         NTSTATUS status;
226         const char *formname = "testform3";
227         BOOL ret = True;
228
229         r.in.handle = handle;
230         r.in.level = 1;
231         form.flags = 2;         /* User form */
232         form.formname = formname;
233         form.width = 1;
234         form.length = 2;
235         form.left = 3;
236         form.top = 4;
237         form.right = 5;
238         form.bottom = 6;
239         r.in.info.info1 = &form;
240         
241         status = dcerpc_spoolss_AddForm(p, mem_ctx, &r);
242
243         if (!NT_STATUS_IS_OK(status)) {
244                 printf("AddForm failed - %s\n", nt_errstr(status));
245                 return False;
246         }
247
248         if (!W_ERROR_IS_OK(r.out.result)) {
249                 printf("AddForm failed - %s\n", nt_errstr(status));
250                 goto done;
251         }
252
253         {
254                 struct spoolss_SetForm sf;
255
256                 sf.in.handle = handle;
257                 sf.in.formname = formname;
258                 sf.in.level = 1;
259                 sf.in.info.info1 = &form;
260                 form.width = 1234;
261
262                 status = dcerpc_spoolss_SetForm(p, mem_ctx, &sf);
263
264                 if (!NT_STATUS_IS_OK(status)) {
265                         printf("SetForm failed - %s\n", nt_errstr(status));
266                         ret = False;
267                         goto done;
268                 }
269
270                 if (!W_ERROR_IS_OK(r.out.result)) {
271                         printf("SetForm failed - %s\n", 
272                                win_errstr(r.out.result));
273                         ret = False;
274                         goto done;
275                 }
276         }
277
278  done:
279         if (!test_DeleteForm(p, mem_ctx, handle, formname)) {
280                 printf("DeleteForm failed\n");
281                 ret = False;
282         }
283
284         return ret;
285 }
286
287 static BOOL test_EnumPorts(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
288 {
289         NTSTATUS status;
290         struct spoolss_EnumPorts r;
291         uint32_t buf_size;
292
293         r.in.servername = talloc_asprintf(mem_ctx, "\\\\%s", 
294                                           dcerpc_server_name(p));
295         r.in.level = 2;
296         r.in.buffer = NULL;
297         buf_size = 0;
298         r.in.buf_size = &buf_size;
299         r.out.buf_size = &buf_size;
300
301         printf("Testing EnumPorts\n");
302
303         status = dcerpc_spoolss_EnumPorts(p, mem_ctx, &r);
304
305         if (!NT_STATUS_IS_OK(status)) {
306                 printf("EnumPorts failed - %s\n", nt_errstr(status));
307                 return False;
308         }
309
310         if (W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
311                 DATA_BLOB blob = data_blob_talloc(mem_ctx, NULL, buf_size);
312
313                 data_blob_clear(&blob);
314                 r.in.buffer = &blob;
315
316                 status = dcerpc_spoolss_EnumPorts(p, mem_ctx, &r);
317
318                 if (!NT_STATUS_IS_OK(status)) {
319                         printf("EnumPorts failed - %s\n", nt_errstr(status));
320                         return False;
321                 }
322
323                 if (!r.out.info) {
324                         printf("No ports returned");
325                         return False;
326                 }
327         }
328
329         return True;
330 }
331
332 static BOOL test_AddPort(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
333 {
334         NTSTATUS status;
335         struct spoolss_AddPort r;
336
337         r.in.server_name = talloc_asprintf(mem_ctx, "\\\\%s", 
338                                            dcerpc_server_name(p));
339         r.in.unknown = 0;
340         r.in.monitor_name = "foo";
341
342         printf ("Testing AddPort\n");
343
344         status = dcerpc_spoolss_AddPort(p, mem_ctx, &r);
345
346         if (!NT_STATUS_IS_OK(status)) {
347                 printf("AddPort failed - %s\n", nt_errstr(status));
348                 return False;
349         }
350
351         /* win2k3 returns WERR_NOT_SUPPORTED */
352
353 #if 0
354
355         if (!W_ERROR_IS_OK(r.out.result)) {
356                 printf("AddPort failed - %s\n", win_errstr(r.out.result));
357                 return False;
358         }
359
360 #endif
361
362         return True;
363 }
364
365 static BOOL test_GetJob(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
366                   struct policy_handle *handle, uint32_t job_id)
367 {
368         NTSTATUS status;
369         struct spoolss_GetJob r;
370         uint32_t buf_size;
371
372         r.in.handle = handle;
373         r.in.job_id = job_id;
374         r.in.level = 1;
375         r.in.buffer = NULL;
376         buf_size = 0;
377         r.in.buf_size = r.out.buf_size = &buf_size;
378
379         printf("Testing GetJob\n");
380
381         status = dcerpc_spoolss_GetJob(p, mem_ctx, &r);
382
383         if (!NT_STATUS_IS_OK(status)) {
384                 printf("GetJob failed - %s\n", nt_errstr(status));
385                 return False;
386         }
387
388         if (W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
389                 DATA_BLOB blob = data_blob_talloc(mem_ctx, NULL, buf_size);
390
391                 data_blob_clear(&blob);
392                 r.in.buffer = &blob;
393
394                 status = dcerpc_spoolss_GetJob(p, mem_ctx, &r);
395
396                 if (!r.out.info) {
397                         printf("No job info returned");
398                         return False;
399                 }
400         }
401
402         return True;
403 }
404
405 static BOOL test_SetJob(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
406                  struct policy_handle *handle, uint32_t job_id, uint32_t command)
407 {
408         NTSTATUS status;
409         struct spoolss_SetJob r;
410
411         r.in.handle = handle;
412         r.in.job_id = job_id;
413         r.in.level = 0;
414         r.in.command = command;
415
416         printf("Testing SetJob\n");
417
418         status = dcerpc_spoolss_SetJob(p, mem_ctx, &r);
419
420         if (!NT_STATUS_IS_OK(status)) {
421                 printf("SetJob failed - %s\n", nt_errstr(status));
422                 return False;
423         }
424
425         return True;
426 }
427
428 static BOOL test_EnumJobs(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
429                    struct policy_handle *handle)
430 {
431         NTSTATUS status;
432         struct spoolss_EnumJobs r;
433         uint32_t buf_size;
434
435         r.in.handle = handle;
436         r.in.firstjob = 0;
437         r.in.numjobs = 0xffffffff;
438         r.in.level = 1;
439         r.in.buffer = NULL;
440         buf_size = 0;
441         r.in.buf_size = &buf_size;
442         r.out.buf_size = &buf_size;
443
444         printf("Testing EnumJobs\n");
445
446         status = dcerpc_spoolss_EnumJobs(p, mem_ctx, &r);
447
448         if (!NT_STATUS_IS_OK(status)) {
449                 printf("EnumJobs failed - %s\n", nt_errstr(status));
450                 return False;
451         }
452
453         if (W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
454                 DATA_BLOB blob = data_blob_talloc(mem_ctx, NULL, buf_size);
455                 union spoolss_JobInfo *info;
456                 int j;
457
458                 data_blob_clear(&blob);
459                 r.in.buffer = &blob;
460
461                 status = dcerpc_spoolss_EnumJobs(p, mem_ctx, &r);
462
463                 if (!r.out.info) {
464                         printf("No jobs returned");
465                         return True;
466                 }
467
468                 info = *r.out.info;
469
470                 for (j = 0; j < r.out.count; j++) {
471                         test_GetJob(p, mem_ctx, handle, info[j].info1.job_id);
472                         test_SetJob(p, mem_ctx, handle, info[j].info1.job_id, 1);
473                 }
474
475         } else if (!W_ERROR_IS_OK(r.out.result)) {
476                 printf("EnumJobs failed - %s\n", win_errstr(r.out.result));
477                 return False;
478         }
479
480         return True;
481 }
482
483 static BOOL test_GetPrinterData(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
484                                 struct policy_handle *handle, 
485                                 const char *value_name)
486 {
487         NTSTATUS status;
488         struct spoolss_GetPrinterData r;
489         uint32_t buf_size;
490
491         r.in.handle = handle;
492         r.in.value_name = value_name;
493         buf_size = 0;
494         r.in.buf_size = r.out.buf_size = &buf_size;
495
496         printf("Testing GetPrinterData\n");
497
498         status = dcerpc_spoolss_GetPrinterData(p, mem_ctx, &r);
499
500         if (!NT_STATUS_IS_OK(status)) {
501                 printf("GetPrinterData failed - %s\n", nt_errstr(status));
502                 return False;
503         }
504
505         if (W_ERROR_EQUAL(r.out.result, WERR_MORE_DATA)) {
506
507                 status = dcerpc_spoolss_GetPrinterData(p, mem_ctx, &r);
508
509                 if (!NT_STATUS_IS_OK(status)) {
510                         printf("GetPrinterData failed - %s\n", 
511                                nt_errstr(status));
512                         return False;
513                 }
514
515                 if (!W_ERROR_IS_OK(r.out.result)) {
516                         printf("GetPrinterData failed - %s\n", 
517                                win_errstr(r.out.result));
518                         return False;
519                 }
520         }
521
522         return True;
523 }
524
525 static BOOL test_GetPrinterDataEx(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
526                                   struct policy_handle *handle, 
527                                   const char *key_name,
528                                   const char *value_name)
529 {
530         NTSTATUS status;
531         struct spoolss_GetPrinterDataEx r;
532         uint32_t buf_size;
533
534         r.in.handle = handle;
535         r.in.key_name = key_name;
536         r.in.value_name = value_name;
537         buf_size = 0;
538         r.in.buf_size = r.out.buf_size = &buf_size;
539
540         printf("Testing GetPrinterDataEx\n");
541
542         status = dcerpc_spoolss_GetPrinterDataEx(p, mem_ctx, &r);
543         if (!NT_STATUS_IS_OK(status)) {
544                 if (NT_STATUS_EQUAL(status,NT_STATUS_NET_WRITE_FAULT) &&
545                     p->last_fault_code == DCERPC_FAULT_OP_RNG_ERROR) {
546                         printf("GetPrinterDataEx not supported by server\n");
547                         return True;
548                 }
549                 printf("GetPrinterDataEx failed - %s\n", nt_errstr(status));
550                 return False;
551         }
552
553         if (W_ERROR_EQUAL(r.out.result, WERR_MORE_DATA)) {
554
555                 status = dcerpc_spoolss_GetPrinterDataEx(p, mem_ctx, &r);
556
557                 if (!NT_STATUS_IS_OK(status)) {
558                         printf("GetPrinterDataEx failed - %s\n", 
559                                nt_errstr(status));
560                         return False;
561                 }
562
563                 if (!W_ERROR_IS_OK(r.out.result)) {
564                         printf("GetPrinterDataEx failed - %s\n", 
565                                win_errstr(r.out.result));
566                         return False;
567                 }
568         }
569
570         return True;
571 }
572
573 static BOOL test_EnumPrinterData(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
574                                  struct policy_handle *handle)
575 {
576         NTSTATUS status;
577         struct spoolss_EnumPrinterData r;
578
579         r.in.handle = handle;
580         r.in.enum_index = 0;
581
582         do {
583                 uint32_t data_size;
584                 
585                 r.in.value_offered = 0;
586                 data_size = 0;
587                 r.in.data_size = &data_size;
588                 r.out.data_size = &data_size;
589
590                 printf("Testing EnumPrinterData\n");
591
592                 status = dcerpc_spoolss_EnumPrinterData(p, mem_ctx, &r);
593
594                 if (!NT_STATUS_IS_OK(status)) {
595                         printf("EnumPrinterData failed - %s\n", nt_errstr(status));
596                         return False;
597                 }
598
599                 r.in.value_offered = r.out.value_needed;
600
601                 status = dcerpc_spoolss_EnumPrinterData(p, mem_ctx, &r);
602
603                 if (!NT_STATUS_IS_OK(status)) {
604                         printf("EnumPrinterData failed - %s\n", nt_errstr(status));
605                         return False;
606                 }
607                 
608                 test_GetPrinterData(p, mem_ctx, handle, r.out.value_name);
609
610                 test_GetPrinterDataEx(
611                         p, mem_ctx, handle, "PrinterDriverData", 
612                         r.out.value_name);
613
614                 r.in.enum_index++;
615
616         } while (W_ERROR_IS_OK(r.out.result));
617
618         return True;
619 }
620
621 static BOOL test_EnumPrinterDataEx(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
622                                    struct policy_handle *handle)
623 {
624         NTSTATUS status;
625         struct spoolss_EnumPrinterDataEx r;
626
627         r.in.handle = handle;
628         r.in.key_name = "PrinterDriverData";
629         r.in.buf_size = 0;
630
631         printf("Testing EnumPrinterDataEx\n");
632
633         status = dcerpc_spoolss_EnumPrinterDataEx(p, mem_ctx, &r);
634
635         if (!NT_STATUS_IS_OK(status)) {
636                 printf("EnumPrinterDataEx failed - %s\n", nt_errstr(status));
637                 return False;
638         }
639
640         r.in.buf_size = r.out.buf_size;
641
642         status = dcerpc_spoolss_EnumPrinterDataEx(p, mem_ctx, &r);
643
644         if (!NT_STATUS_IS_OK(status)) {
645                 printf("EnumPrinterDataEx failed - %s\n", nt_errstr(status));
646                 return False;
647         }
648
649         return True;
650 }
651
652
653 static BOOL test_DeletePrinterData(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
654                                    struct policy_handle *handle, 
655                                    const char *value_name)
656 {
657         NTSTATUS status;
658         struct spoolss_DeletePrinterData r;
659
660         r.in.handle = handle;
661         r.in.value_name = value_name;
662
663         printf("Testing DeletePrinterData\n");
664
665         status = dcerpc_spoolss_DeletePrinterData(p, mem_ctx, &r);
666
667         if (!NT_STATUS_IS_OK(status)) {
668                 printf("DeletePrinterData failed - %s\n", nt_errstr(status));
669                 return False;
670         }
671
672         return True;
673 }
674
675 static BOOL test_SetPrinterData(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
676                          struct policy_handle *handle)
677 {
678         NTSTATUS status;
679         struct spoolss_SetPrinterData r;
680         const char *value_name = "spottyfoot";
681         
682         r.in.handle = handle;
683         r.in.value_name = value_name;
684         r.in.type = 0;
685         r.in.buffer = data_blob_talloc(mem_ctx, "dog", 4);
686         r.in.real_len = 4;
687
688         printf("Testing SetPrinterData\n");
689
690         status = dcerpc_spoolss_SetPrinterData(p, mem_ctx, &r);
691
692         if (!NT_STATUS_IS_OK(status)) {
693                 printf("SetPrinterData failed - %s\n", nt_errstr(status));
694                 return False;
695         }
696
697         if (!test_DeletePrinterData(p, mem_ctx, handle, value_name)) {
698                 return False;
699         }
700
701         return True;
702 }
703
704 static BOOL test_SecondaryClosePrinter(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
705                                        struct policy_handle *handle)
706 {
707         NTSTATUS status;
708         struct dcerpc_pipe *p2;
709         BOOL ret = True;
710
711         /* only makes sense on SMB */
712         if (p->conn->transport.transport != NCACN_NP) {
713                 return True;
714         }
715
716         printf("testing close on secondary pipe\n");
717
718         status = dcerpc_secondary_connection(p, &p2, 
719                                              DCERPC_SPOOLSS_NAME, 
720                                              DCERPC_SPOOLSS_UUID, 
721                                              DCERPC_SPOOLSS_VERSION);
722         if (!NT_STATUS_IS_OK(status)) {
723                 printf("Failed to create secondary connection\n");
724                 return False;
725         }
726
727         if (test_ClosePrinter(p2, mem_ctx, handle)) {
728                 printf("ERROR: Allowed close on secondary connection!\n");
729                 ret = False;
730         }
731
732         if (p2->last_fault_code != DCERPC_FAULT_CONTEXT_MISMATCH) {
733                 printf("Unexpected fault code 0x%x - expected 0x%x\n",
734                        p2->last_fault_code, DCERPC_FAULT_CONTEXT_MISMATCH);
735                 ret = False;
736         }
737
738         dcerpc_pipe_close(p2);
739
740         return ret;
741 }
742
743 static BOOL test_OpenPrinter_badname(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, const char *name)
744 {
745         NTSTATUS status;
746         struct spoolss_OpenPrinter op;
747         struct spoolss_OpenPrinterEx opEx;
748         struct policy_handle handle;
749         BOOL ret = True;
750
751         op.in.printername       = name;
752         op.in.datatype          = NULL;
753         op.in.devmode_ctr.size  = 0;
754         op.in.devmode_ctr.devmode= NULL;
755         op.in.access_mask       = 0;
756         op.out.handle           = &handle;
757
758         printf("\nTesting OpenPrinter(%s) with bad name\n", op.in.printername);
759
760         status = dcerpc_spoolss_OpenPrinter(p, mem_ctx, &op);
761         if (!NT_STATUS_IS_OK(status)) {
762                 printf("OpenPrinter failed - %s\n", nt_errstr(status));
763                 ret = False;
764         }
765         if (!W_ERROR_EQUAL(WERR_INVALID_PRINTER_NAME,op.out.result)) {
766                 printf("OpenPrinter(%s) unexpected result[%s] should be WERR_INVALID_PRINTER_NAME\n",
767                         name, win_errstr(op.out.result));
768         }
769
770         if (W_ERROR_IS_OK(op.out.result)) {
771                 ret &=test_ClosePrinter(p, mem_ctx, &handle);
772         }
773
774         opEx.in.printername             = name;
775         opEx.in.datatype                = NULL;
776         opEx.in.devmode_ctr.size        = 0;
777         opEx.in.devmode_ctr.devmode     = NULL;
778         opEx.in.access_mask             = 0;
779         opEx.in.level                   = 1;
780         opEx.in.userlevel.level1        = NULL;
781         opEx.out.handle                 = &handle;
782
783         printf("\nTesting OpenPrinterEx(%s) with bad name\n", opEx.in.printername);
784
785         status = dcerpc_spoolss_OpenPrinterEx(p, mem_ctx, &opEx);
786         if (!NT_STATUS_IS_OK(status)) {
787                 printf("OpenPrinter failed - %s\n", nt_errstr(status));
788                 ret = False;
789         }
790         if (!W_ERROR_EQUAL(WERR_INVALID_PRINTER_NAME,opEx.out.result)) {
791                 printf("OpenPrinterEx(%s) unexpected result[%s] should be WERR_INVALID_PRINTER_NAME\n",
792                         name, win_errstr(opEx.out.result));
793         }
794
795         if (W_ERROR_IS_OK(opEx.out.result)) {
796                 ret &=test_ClosePrinter(p, mem_ctx, &handle);
797         }
798
799         return ret;
800 }
801
802 static BOOL test_OpenPrinter_badnames(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
803 {
804         BOOL ret = True;
805         char *name;
806
807         ret &= test_OpenPrinter_badname(p, mem_ctx, "__INVALID_PRINTER__");
808         ret &= test_OpenPrinter_badname(p, mem_ctx, "\\\\__INVALID_HOST__");
809         ret &= test_OpenPrinter_badname(p, mem_ctx, "");
810         ret &= test_OpenPrinter_badname(p, mem_ctx, "\\\\\\");
811         ret &= test_OpenPrinter_badname(p, mem_ctx, "\\\\\\__INVALID_PRINTER__");
812
813         name = talloc_asprintf(mem_ctx, "\\\\%s\\", dcerpc_server_name(p));
814         ret &= test_OpenPrinter_badname(p, mem_ctx, name);
815         talloc_free(name);
816
817         name = talloc_asprintf(mem_ctx, "\\\\%s\\__INVALID_PRINTER__", dcerpc_server_name(p));
818         ret &= test_OpenPrinter_badname(p, mem_ctx, name);
819         talloc_free(name);
820
821         return ret;
822 }
823
824 static BOOL test_OpenPrinter(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
825                              const char *name)
826 {
827         NTSTATUS status;
828         struct spoolss_OpenPrinter r;
829         struct policy_handle handle;
830         BOOL ret = True;
831
832         r.in.printername        = talloc_asprintf(mem_ctx, "\\\\%s\\%s", dcerpc_server_name(p), name);
833         r.in.datatype           = NULL;
834         r.in.devmode_ctr.size   = 0;
835         r.in.devmode_ctr.devmode= NULL;
836         r.in.access_mask        = SEC_FLAG_MAXIMUM_ALLOWED;
837         r.out.handle            = &handle;
838
839         printf("\nTesting OpenPrinter(%s)\n", r.in.printername);
840
841         status = dcerpc_spoolss_OpenPrinter(p, mem_ctx, &r);
842
843         if (!NT_STATUS_IS_OK(status)) {
844                 printf("OpenPrinter failed - %s\n", nt_errstr(status));
845                 return False;
846         }
847
848         if (!W_ERROR_IS_OK(r.out.result)) {
849                 printf("OpenPrinter failed - %s\n", win_errstr(r.out.result));
850                 return False;
851         }
852
853         if (!test_GetPrinter(p, mem_ctx, &handle)) {
854                 ret = False;
855         }
856
857         if (!test_SecondaryClosePrinter(p, mem_ctx, &handle)) {
858                 ret = False;
859         }
860
861         if (!test_ClosePrinter(p, mem_ctx, &handle)) {
862                 ret = False;
863         }
864
865         return ret;
866 }
867
868 static BOOL call_OpenPrinterEx(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
869                                const char *name, struct policy_handle *handle)
870 {
871         struct spoolss_OpenPrinterEx r;
872         struct spoolss_UserLevel1 userlevel1;
873         NTSTATUS status;
874
875         if (name && name[0]) {
876                 r.in.printername = talloc_asprintf(mem_ctx, "\\\\%s\\%s", 
877                                                    dcerpc_server_name(p), name);
878         } else {
879                 r.in.printername = talloc_asprintf(mem_ctx, "\\\\%s", 
880                                                    dcerpc_server_name(p));
881         }
882
883         r.in.datatype           = NULL;
884         r.in.devmode_ctr.size   = 0;
885         r.in.devmode_ctr.devmode= NULL;
886         r.in.access_mask        = SEC_FLAG_MAXIMUM_ALLOWED;
887         r.in.level              = 1;
888         r.in.userlevel.level1   = &userlevel1;
889         r.out.handle = handle;
890
891         userlevel1.size = 1234;
892         userlevel1.client = "hello";
893         userlevel1.user = "spottyfoot!";
894         userlevel1.build = 1;
895         userlevel1.major = 2;
896         userlevel1.minor = 3;
897         userlevel1.processor = 4;
898
899         printf("Testing OpenPrinterEx(%s)\n", r.in.printername);
900
901         status = dcerpc_spoolss_OpenPrinterEx(p, mem_ctx, &r);
902
903         if (!NT_STATUS_IS_OK(status)) {
904                 printf("OpenPrinterEx failed - %s\n", nt_errstr(status));
905                 return False;
906         }
907         
908         if (!W_ERROR_IS_OK(r.out.result)) {
909                 printf("OpenPrinterEx failed - %s\n", win_errstr(r.out.result));
910                 return False;
911         }
912
913         return True;
914 }
915
916 static BOOL test_OpenPrinterEx(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
917                                const char *name)
918 {
919         struct policy_handle handle;
920         BOOL ret = True;
921
922         if (!call_OpenPrinterEx(p, mem_ctx, name, &handle)) {
923                 return False;
924         }
925
926         if (!test_GetPrinter(p, mem_ctx, &handle)) {
927                 ret = False;
928         }
929
930         if (!test_EnumForms(p, mem_ctx, &handle)) {
931                 ret = False;
932         }
933
934         if (!test_AddForm(p, mem_ctx, &handle)) {
935                 ret = False;
936         }
937
938         if (!test_EnumPrinterData(p, mem_ctx, &handle)) {
939                 ret = False;
940         }
941
942         if (!test_EnumPrinterDataEx(p, mem_ctx, &handle)) {
943                 ret = False;
944         }
945
946         if (!test_EnumJobs(p, mem_ctx, &handle)) {
947                 ret = False;
948         }
949
950         if (!test_SetPrinterData(p, mem_ctx, &handle)) {
951                 ret = False;
952         }
953
954         if (!test_SecondaryClosePrinter(p, mem_ctx, &handle)) {
955                 ret = False;
956         }
957
958         if (!test_ClosePrinter(p, mem_ctx, &handle)) {
959                 ret = False;
960         }
961         
962         return ret;
963 }
964
965 static BOOL test_EnumPrinters(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
966 {
967         struct spoolss_EnumPrinters r;
968         NTSTATUS status;
969         uint16_t levels[] = {1, 2, 4, 5};
970         int i;
971         BOOL ret = True;
972
973         for (i=0;i<ARRAY_SIZE(levels);i++) {
974                 uint32_t buf_size = 0;
975                 union spoolss_PrinterInfo *info;
976                 int j;
977
978                 r.in.flags      = PRINTER_ENUM_LOCAL;
979                 r.in.server     = "";
980                 r.in.level      = levels[i];
981                 r.in.buffer     = NULL;
982                 r.in.buf_size   = &buf_size;
983                 r.out.buf_size  = &buf_size;
984
985                 printf("\nTesting EnumPrinters level %u\n", r.in.level);
986
987                 status = dcerpc_spoolss_EnumPrinters(p, mem_ctx, &r);
988                 if (!NT_STATUS_IS_OK(status)) {
989                         printf("EnumPrinters failed - %s\n", nt_errstr(status));
990                         ret = False;
991                         continue;
992                 }
993
994                 if (W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
995                         DATA_BLOB blob = data_blob_talloc(mem_ctx, NULL, buf_size);
996                         data_blob_clear(&blob);
997                         r.in.buffer = &blob;
998                         status = dcerpc_spoolss_EnumPrinters(p, mem_ctx, &r);
999                 }
1000
1001                 if (!NT_STATUS_IS_OK(status)) {
1002                         printf("EnumPrinters failed - %s\n", 
1003                                nt_errstr(status));
1004                         continue;
1005                 }
1006
1007                 if (!W_ERROR_IS_OK(r.out.result)) {
1008                         printf("EnumPrinters failed - %s\n", 
1009                                win_errstr(r.out.result));
1010                         continue;
1011                 }
1012
1013                 if (!r.out.info) {
1014                         printf("No printers returned");
1015                         continue;
1016                 }
1017
1018                 info = *r.out.info;
1019
1020                 for (j=0;j<r.out.count;j++) {
1021                         if (r.in.level == 1) {
1022                                 /* the names appear to be comma-separated name lists? */
1023                                 char *name = talloc_strdup(mem_ctx, info[j].info1.name);
1024                                 char *comma = strchr(name, ',');
1025                                 if (comma) *comma = 0;
1026                                 if (!test_OpenPrinter(p, mem_ctx, name)) {
1027                                         ret = False;
1028                                 }
1029                                 if (!test_OpenPrinterEx(p, mem_ctx, name)) {
1030                                         ret = False;
1031                                 }
1032                         }
1033                 }
1034         }
1035
1036         return ret;
1037 }
1038
1039 #if 0
1040 static BOOL test_GetPrinterDriver2(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1041                                    struct policy_handle *handle, 
1042                                    const char *driver_name)
1043 {
1044         NTSTATUS status;
1045         struct spoolss_GetPrinterDriver2 r;
1046         uint32_t buf_size;
1047
1048         r.in.handle = handle;
1049         r.in.architecture = "W32X86";
1050         r.in.level = 1;
1051         buf_size = 0;
1052         r.in.buffer = NULL;
1053         r.in.buf_size = r.out.buf_size = &buf_size;
1054         r.in.client_major_version = 0;
1055         r.in.client_minor_version = 0;
1056
1057         printf("Testing GetPrinterDriver2\n");
1058
1059         status = dcerpc_spoolss_GetPrinterDriver2(p, mem_ctx, &r);
1060
1061         if (!NT_STATUS_IS_OK(status)) {
1062                 printf("GetPrinterDriver2 failed - %s\n", nt_errstr(status));
1063                 return False;
1064         }
1065
1066         if (W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
1067                 status = dcerpc_spoolss_GetPrinterDriver2(p, mem_ctx, &r);
1068         }
1069                 
1070         if (!NT_STATUS_IS_OK(status)) {
1071                 printf("GetPrinterDriver2 failed - %s\n", 
1072                        nt_errstr(status));
1073                 return False;
1074         }
1075
1076         if (!W_ERROR_IS_OK(r.out.result)) {
1077                 printf("GetPrinterDriver2 failed - %s\n", 
1078                        win_errstr(r.out.result));
1079                 return False;
1080         }
1081
1082         return True;
1083 }
1084 #endif
1085         
1086 static BOOL test_EnumPrinterDrivers(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
1087 {
1088         struct spoolss_EnumPrinterDrivers r;
1089         NTSTATUS status;
1090         uint16_t levels[] = {1, 2, 3, 4, 5, 6};
1091         int i;
1092         BOOL ret = True;
1093
1094         for (i=0;i<ARRAY_SIZE(levels);i++) {
1095                 uint32_t buf_size;
1096
1097                 r.in.server = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
1098                 r.in.environment = "Windows NT x86";
1099                 r.in.level = levels[i];
1100                 r.in.buffer = NULL;
1101                 buf_size = 0;
1102                 r.in.buf_size = &buf_size;
1103                 r.out.buf_size = &buf_size;
1104
1105                 printf("\nTesting EnumPrinterDrivers level %u\n", r.in.level);
1106
1107                 status = dcerpc_spoolss_EnumPrinterDrivers(p, mem_ctx, &r);
1108
1109                 if (!NT_STATUS_IS_OK(status)) {
1110                         printf("EnumPrinterDrivers failed - %s\n", 
1111                                nt_errstr(status));
1112                         ret = False;
1113                         continue;
1114                 }
1115
1116                 if (W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
1117                         DATA_BLOB blob = data_blob_talloc(
1118                                 mem_ctx, NULL, buf_size);
1119
1120                         data_blob_clear(&blob);
1121                         r.in.buffer = &blob;
1122                         status = dcerpc_spoolss_EnumPrinterDrivers(p, mem_ctx, &r);
1123                 }
1124                 
1125                 if (!NT_STATUS_IS_OK(status)) {
1126                         printf("EnumPrinterDrivers failed - %s\n", 
1127                                nt_errstr(status));
1128                         ret = False;
1129                         break;
1130                 }
1131
1132                 if (!W_ERROR_IS_OK(r.out.result)) {
1133                         printf("EnumPrinterDrivers failed - %s\n", 
1134                                win_errstr(r.out.result));
1135                         ret = False;
1136                         break;
1137                 }
1138
1139                 if (!r.out.info) {
1140                         printf("No printer drivers returned");
1141                         break;
1142                 }
1143         }
1144
1145         return ret;
1146 }
1147
1148 BOOL torture_rpc_spoolss(void)
1149 {
1150         NTSTATUS status;
1151         struct dcerpc_pipe *p;
1152         TALLOC_CTX *mem_ctx;
1153         BOOL ret = True;
1154
1155         status = torture_rpc_connection(&p, 
1156                                         DCERPC_SPOOLSS_NAME,
1157                                         DCERPC_SPOOLSS_UUID,
1158                                         DCERPC_SPOOLSS_VERSION);
1159         if (!NT_STATUS_IS_OK(status)) {
1160                 return False;
1161         }
1162
1163         mem_ctx = talloc_init("torture_rpc_spoolss");
1164
1165         ret &= test_OpenPrinter_badnames(p, mem_ctx);
1166
1167         ret &= test_AddPort(p, mem_ctx);
1168
1169         ret &= test_EnumPorts(p, mem_ctx);
1170
1171         ret &= test_EnumPrinters(p, mem_ctx);
1172
1173         ret &= test_EnumPrinterDrivers(p, mem_ctx);
1174
1175         talloc_free(mem_ctx);
1176
1177         torture_rpc_close(p);
1178
1179         return ret;
1180 }