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