94ced04a98b2f62351350394de05c51313744e98
[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_DeletePrinterData(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
622                                    struct policy_handle *handle, 
623                                    const char *value_name)
624 {
625         NTSTATUS status;
626         struct spoolss_DeletePrinterData r;
627
628         r.in.handle = handle;
629         r.in.value_name = value_name;
630
631         printf("Testing DeletePrinterData\n");
632
633         status = dcerpc_spoolss_DeletePrinterData(p, mem_ctx, &r);
634
635         if (!NT_STATUS_IS_OK(status)) {
636                 printf("DeletePrinterData failed - %s\n", nt_errstr(status));
637                 return False;
638         }
639
640         return True;
641 }
642
643 static BOOL test_SetPrinterData(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
644                          struct policy_handle *handle)
645 {
646         NTSTATUS status;
647         struct spoolss_SetPrinterData r;
648         const char *value_name = "spottyfoot";
649         
650         r.in.handle = handle;
651         r.in.value_name = value_name;
652         r.in.type = 0;
653         r.in.buffer = data_blob_talloc(mem_ctx, "dog", 4);
654         r.in.real_len = 4;
655
656         printf("Testing SetPrinterData\n");
657
658         status = dcerpc_spoolss_SetPrinterData(p, mem_ctx, &r);
659
660         if (!NT_STATUS_IS_OK(status)) {
661                 printf("SetPrinterData failed - %s\n", nt_errstr(status));
662                 return False;
663         }
664
665         if (!test_DeletePrinterData(p, mem_ctx, handle, value_name)) {
666                 return False;
667         }
668
669         return True;
670 }
671
672 static BOOL test_SecondaryClosePrinter(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
673                                        struct policy_handle *handle)
674 {
675         NTSTATUS status;
676         struct dcerpc_pipe *p2;
677         BOOL ret = True;
678
679         /* only makes sense on SMB */
680         if (p->conn->transport.transport != NCACN_NP) {
681                 return True;
682         }
683
684         printf("testing close on secondary pipe\n");
685
686         status = dcerpc_secondary_connection(p, &p2, 
687                                              DCERPC_SPOOLSS_NAME, 
688                                              DCERPC_SPOOLSS_UUID, 
689                                              DCERPC_SPOOLSS_VERSION);
690         if (!NT_STATUS_IS_OK(status)) {
691                 printf("Failed to create secondary connection\n");
692                 return False;
693         }
694
695         if (test_ClosePrinter(p2, mem_ctx, handle)) {
696                 printf("ERROR: Allowed close on secondary connection!\n");
697                 ret = False;
698         }
699
700         if (p2->last_fault_code != DCERPC_FAULT_CONTEXT_MISMATCH) {
701                 printf("Unexpected fault code 0x%x - expected 0x%x\n",
702                        p2->last_fault_code, DCERPC_FAULT_CONTEXT_MISMATCH);
703                 ret = False;
704         }
705
706         dcerpc_pipe_close(p2);
707
708         return ret;
709 }
710
711 static BOOL test_OpenPrinter_badname(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, const char *name)
712 {
713         NTSTATUS status;
714         struct spoolss_OpenPrinter op;
715         struct spoolss_OpenPrinterEx opEx;
716         struct policy_handle handle;
717         BOOL ret = True;
718
719         op.in.printername       = name;
720         op.in.datatype          = NULL;
721         op.in.devmode_ctr.size  = 0;
722         op.in.devmode_ctr.devmode= NULL;
723         op.in.access_mask       = 0;
724         op.out.handle           = &handle;
725
726         printf("\nTesting OpenPrinter(%s) with bad name\n", op.in.printername);
727
728         status = dcerpc_spoolss_OpenPrinter(p, mem_ctx, &op);
729         if (!NT_STATUS_IS_OK(status)) {
730                 printf("OpenPrinter failed - %s\n", nt_errstr(status));
731                 ret = False;
732         }
733         if (!W_ERROR_EQUAL(WERR_INVALID_PRINTER_NAME,op.out.result)) {
734                 printf("OpenPrinter(%s) unexpected result[%s] should be WERR_INVALID_PRINTER_NAME\n",
735                         name, win_errstr(op.out.result));
736         }
737
738         if (W_ERROR_IS_OK(op.out.result)) {
739                 ret &=test_ClosePrinter(p, mem_ctx, &handle);
740         }
741
742         opEx.in.printername             = name;
743         opEx.in.datatype                = NULL;
744         opEx.in.devmode_ctr.size        = 0;
745         opEx.in.devmode_ctr.devmode     = NULL;
746         opEx.in.access_mask             = 0;
747         opEx.in.level                   = 1;
748         opEx.in.userlevel.level1        = NULL;
749         opEx.out.handle                 = &handle;
750
751         printf("\nTesting OpenPrinterEx(%s) with bad name\n", opEx.in.printername);
752
753         status = dcerpc_spoolss_OpenPrinterEx(p, mem_ctx, &opEx);
754         if (!NT_STATUS_IS_OK(status)) {
755                 printf("OpenPrinter failed - %s\n", nt_errstr(status));
756                 ret = False;
757         }
758         if (!W_ERROR_EQUAL(WERR_INVALID_PRINTER_NAME,opEx.out.result)) {
759                 printf("OpenPrinterEx(%s) unexpected result[%s] should be WERR_INVALID_PRINTER_NAME\n",
760                         name, win_errstr(opEx.out.result));
761         }
762
763         if (W_ERROR_IS_OK(opEx.out.result)) {
764                 ret &=test_ClosePrinter(p, mem_ctx, &handle);
765         }
766
767         return ret;
768 }
769
770 static BOOL test_OpenPrinter_badnames(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
771 {
772         BOOL ret = True;
773         char *name;
774
775         ret &= test_OpenPrinter_badname(p, mem_ctx, "__INVALID_PRINTER__");
776         ret &= test_OpenPrinter_badname(p, mem_ctx, "\\\\__INVALID_HOST__");
777         ret &= test_OpenPrinter_badname(p, mem_ctx, "");
778         ret &= test_OpenPrinter_badname(p, mem_ctx, "\\\\\\");
779         ret &= test_OpenPrinter_badname(p, mem_ctx, "\\\\\\__INVALID_PRINTER__");
780
781         name = talloc_asprintf(mem_ctx, "\\\\%s\\", dcerpc_server_name(p));
782         ret &= test_OpenPrinter_badname(p, mem_ctx, name);
783         talloc_free(name);
784
785         name = talloc_asprintf(mem_ctx, "\\\\%s\\__INVALID_PRINTER__", dcerpc_server_name(p));
786         ret &= test_OpenPrinter_badname(p, mem_ctx, name);
787         talloc_free(name);
788
789         return ret;
790 }
791
792 static BOOL test_OpenPrinter(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
793                              const char *name)
794 {
795         NTSTATUS status;
796         struct spoolss_OpenPrinter r;
797         struct policy_handle handle;
798         BOOL ret = True;
799
800         r.in.printername        = talloc_asprintf(mem_ctx, "\\\\%s\\%s", dcerpc_server_name(p), name);
801         r.in.datatype           = NULL;
802         r.in.devmode_ctr.size   = 0;
803         r.in.devmode_ctr.devmode= NULL;
804         r.in.access_mask        = SEC_FLAG_MAXIMUM_ALLOWED;
805         r.out.handle            = &handle;
806
807         printf("\nTesting OpenPrinter(%s)\n", r.in.printername);
808
809         status = dcerpc_spoolss_OpenPrinter(p, mem_ctx, &r);
810
811         if (!NT_STATUS_IS_OK(status)) {
812                 printf("OpenPrinter failed - %s\n", nt_errstr(status));
813                 return False;
814         }
815
816         if (!W_ERROR_IS_OK(r.out.result)) {
817                 printf("OpenPrinter failed - %s\n", win_errstr(r.out.result));
818                 return False;
819         }
820
821         if (!test_GetPrinter(p, mem_ctx, &handle)) {
822                 ret = False;
823         }
824
825         if (!test_SecondaryClosePrinter(p, mem_ctx, &handle)) {
826                 ret = False;
827         }
828
829         if (!test_ClosePrinter(p, mem_ctx, &handle)) {
830                 ret = False;
831         }
832
833         return ret;
834 }
835
836 static BOOL call_OpenPrinterEx(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
837                                const char *name, struct policy_handle *handle)
838 {
839         struct spoolss_OpenPrinterEx r;
840         struct spoolss_UserLevel1 userlevel1;
841         NTSTATUS status;
842
843         if (name && name[0]) {
844                 r.in.printername = talloc_asprintf(mem_ctx, "\\\\%s\\%s", 
845                                                    dcerpc_server_name(p), name);
846         } else {
847                 r.in.printername = talloc_asprintf(mem_ctx, "\\\\%s", 
848                                                    dcerpc_server_name(p));
849         }
850
851         r.in.datatype           = NULL;
852         r.in.devmode_ctr.size   = 0;
853         r.in.devmode_ctr.devmode= NULL;
854         r.in.access_mask        = SEC_FLAG_MAXIMUM_ALLOWED;
855         r.in.level              = 1;
856         r.in.userlevel.level1   = &userlevel1;
857         r.out.handle = handle;
858
859         userlevel1.size = 1234;
860         userlevel1.client = "hello";
861         userlevel1.user = "spottyfoot!";
862         userlevel1.build = 1;
863         userlevel1.major = 2;
864         userlevel1.minor = 3;
865         userlevel1.processor = 4;
866
867         printf("Testing OpenPrinterEx(%s)\n", r.in.printername);
868
869         status = dcerpc_spoolss_OpenPrinterEx(p, mem_ctx, &r);
870
871         if (!NT_STATUS_IS_OK(status)) {
872                 printf("OpenPrinterEx failed - %s\n", nt_errstr(status));
873                 return False;
874         }
875         
876         if (!W_ERROR_IS_OK(r.out.result)) {
877                 printf("OpenPrinterEx failed - %s\n", win_errstr(r.out.result));
878                 return False;
879         }
880
881         return True;
882 }
883
884 static BOOL test_OpenPrinterEx(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
885                                const char *name)
886 {
887         struct policy_handle handle;
888         BOOL ret = True;
889
890         if (!call_OpenPrinterEx(p, mem_ctx, name, &handle)) {
891                 return False;
892         }
893
894         if (!test_GetPrinter(p, mem_ctx, &handle)) {
895                 ret = False;
896         }
897
898         if (!test_EnumForms(p, mem_ctx, &handle)) {
899                 ret = False;
900         }
901
902         if (!test_AddForm(p, mem_ctx, &handle)) {
903                 ret = False;
904         }
905
906         if (!test_EnumPrinterData(p, mem_ctx, &handle)) {
907                 ret = False;
908         }
909
910         if (!test_EnumJobs(p, mem_ctx, &handle)) {
911                 ret = False;
912         }
913
914         if (!test_SetPrinterData(p, mem_ctx, &handle)) {
915                 ret = False;
916         }
917
918         if (!test_SecondaryClosePrinter(p, mem_ctx, &handle)) {
919                 ret = False;
920         }
921
922         if (!test_ClosePrinter(p, mem_ctx, &handle)) {
923                 ret = False;
924         }
925         
926         return ret;
927 }
928
929 static BOOL test_EnumPrinters(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
930 {
931         struct spoolss_EnumPrinters r;
932         NTSTATUS status;
933         uint16_t levels[] = {1, 2, 4, 5};
934         int i;
935         BOOL ret = True;
936
937         for (i=0;i<ARRAY_SIZE(levels);i++) {
938                 uint32_t buf_size = 0;
939                 union spoolss_PrinterInfo *info;
940                 int j;
941
942                 r.in.flags      = PRINTER_ENUM_LOCAL;
943                 r.in.server     = "";
944                 r.in.level      = levels[i];
945                 r.in.buffer     = NULL;
946                 r.in.buf_size   = &buf_size;
947                 r.out.buf_size  = &buf_size;
948
949                 printf("\nTesting EnumPrinters level %u\n", r.in.level);
950
951                 status = dcerpc_spoolss_EnumPrinters(p, mem_ctx, &r);
952                 if (!NT_STATUS_IS_OK(status)) {
953                         printf("EnumPrinters failed - %s\n", nt_errstr(status));
954                         ret = False;
955                         continue;
956                 }
957
958                 if (W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
959                         DATA_BLOB blob = data_blob_talloc(mem_ctx, NULL, buf_size);
960                         data_blob_clear(&blob);
961                         r.in.buffer = &blob;
962                         status = dcerpc_spoolss_EnumPrinters(p, mem_ctx, &r);
963                 }
964
965                 if (!NT_STATUS_IS_OK(status)) {
966                         printf("EnumPrinters failed - %s\n", 
967                                nt_errstr(status));
968                         continue;
969                 }
970
971                 if (!W_ERROR_IS_OK(r.out.result)) {
972                         printf("EnumPrinters failed - %s\n", 
973                                win_errstr(r.out.result));
974                         continue;
975                 }
976
977                 if (!r.out.info) {
978                         printf("No printers returned");
979                         continue;
980                 }
981
982                 info = *r.out.info;
983
984                 for (j=0;j<r.out.count;j++) {
985                         if (r.in.level == 1) {
986                                 /* the names appear to be comma-separated name lists? */
987                                 char *name = talloc_strdup(mem_ctx, info[j].info1.name);
988                                 char *comma = strchr(name, ',');
989                                 if (comma) *comma = 0;
990                                 if (!test_OpenPrinter(p, mem_ctx, name)) {
991                                         ret = False;
992                                 }
993                                 if (!test_OpenPrinterEx(p, mem_ctx, name)) {
994                                         ret = False;
995                                 }
996                         }
997                 }
998         }
999
1000         return ret;
1001 }
1002
1003 #if 0
1004 static BOOL test_GetPrinterDriver2(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1005                                    struct policy_handle *handle, 
1006                                    const char *driver_name)
1007 {
1008         NTSTATUS status;
1009         struct spoolss_GetPrinterDriver2 r;
1010         uint32_t buf_size;
1011
1012         r.in.handle = handle;
1013         r.in.architecture = "W32X86";
1014         r.in.level = 1;
1015         buf_size = 0;
1016         r.in.buffer = NULL;
1017         r.in.buf_size = r.out.buf_size = &buf_size;
1018         r.in.client_major_version = 0;
1019         r.in.client_minor_version = 0;
1020
1021         printf("Testing GetPrinterDriver2\n");
1022
1023         status = dcerpc_spoolss_GetPrinterDriver2(p, mem_ctx, &r);
1024
1025         if (!NT_STATUS_IS_OK(status)) {
1026                 printf("GetPrinterDriver2 failed - %s\n", nt_errstr(status));
1027                 return False;
1028         }
1029
1030         if (W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
1031                 status = dcerpc_spoolss_GetPrinterDriver2(p, mem_ctx, &r);
1032         }
1033                 
1034         if (!NT_STATUS_IS_OK(status)) {
1035                 printf("GetPrinterDriver2 failed - %s\n", 
1036                        nt_errstr(status));
1037                 return False;
1038         }
1039
1040         if (!W_ERROR_IS_OK(r.out.result)) {
1041                 printf("GetPrinterDriver2 failed - %s\n", 
1042                        win_errstr(r.out.result));
1043                 return False;
1044         }
1045
1046         return True;
1047 }
1048 #endif
1049         
1050 static BOOL test_EnumPrinterDrivers(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
1051 {
1052         struct spoolss_EnumPrinterDrivers r;
1053         NTSTATUS status;
1054         uint16_t levels[] = {1, 2, 3, 4, 5, 6};
1055         int i;
1056         BOOL ret = True;
1057
1058         for (i=0;i<ARRAY_SIZE(levels);i++) {
1059                 uint32_t buf_size;
1060
1061                 r.in.server = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
1062                 r.in.environment = "Windows NT x86";
1063                 r.in.level = levels[i];
1064                 r.in.buffer = NULL;
1065                 buf_size = 0;
1066                 r.in.buf_size = &buf_size;
1067                 r.out.buf_size = &buf_size;
1068
1069                 printf("\nTesting EnumPrinterDrivers level %u\n", r.in.level);
1070
1071                 status = dcerpc_spoolss_EnumPrinterDrivers(p, mem_ctx, &r);
1072
1073                 if (!NT_STATUS_IS_OK(status)) {
1074                         printf("EnumPrinterDrivers failed - %s\n", 
1075                                nt_errstr(status));
1076                         ret = False;
1077                         continue;
1078                 }
1079
1080                 if (W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
1081                         DATA_BLOB blob = data_blob_talloc(
1082                                 mem_ctx, NULL, buf_size);
1083
1084                         data_blob_clear(&blob);
1085                         r.in.buffer = &blob;
1086                         status = dcerpc_spoolss_EnumPrinterDrivers(p, mem_ctx, &r);
1087                 }
1088                 
1089                 if (!NT_STATUS_IS_OK(status)) {
1090                         printf("EnumPrinterDrivers failed - %s\n", 
1091                                nt_errstr(status));
1092                         ret = False;
1093                         break;
1094                 }
1095
1096                 if (!W_ERROR_IS_OK(r.out.result)) {
1097                         printf("EnumPrinterDrivers failed - %s\n", 
1098                                win_errstr(r.out.result));
1099                         ret = False;
1100                         break;
1101                 }
1102
1103                 if (!r.out.info) {
1104                         printf("No printer drivers returned");
1105                         break;
1106                 }
1107         }
1108
1109         return ret;
1110 }
1111
1112 BOOL torture_rpc_spoolss(void)
1113 {
1114         NTSTATUS status;
1115         struct dcerpc_pipe *p;
1116         TALLOC_CTX *mem_ctx;
1117         BOOL ret = True;
1118
1119         status = torture_rpc_connection(&p, 
1120                                         DCERPC_SPOOLSS_NAME,
1121                                         DCERPC_SPOOLSS_UUID,
1122                                         DCERPC_SPOOLSS_VERSION);
1123         if (!NT_STATUS_IS_OK(status)) {
1124                 return False;
1125         }
1126
1127         mem_ctx = talloc_init("torture_rpc_spoolss");
1128
1129         ret &= test_OpenPrinter_badnames(p, mem_ctx);
1130
1131         ret &= test_AddPort(p, mem_ctx);
1132
1133         ret &= test_EnumPorts(p, mem_ctx);
1134
1135         ret &= test_EnumPrinters(p, mem_ctx);
1136
1137         ret &= test_EnumPrinterDrivers(p, mem_ctx);
1138
1139         talloc_free(mem_ctx);
1140
1141         torture_rpc_close(p);
1142
1143         return ret;
1144 }