summaryrefslogtreecommitdiff
path: root/source3/rpc_client/msrpc_spoolss.c
blob: 56c70730ba7d5c9eaa1992ffbfff17719532a699 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
/* 
   Unix SMB/CIFS implementation.
   NT Domain Authentication SMB / MSRPC client
   Copyright (C) Andrew Tridgell              1994-2000
   Copyright (C) Luke Kenneth Casson Leighton 1996-2000
   Copyright (C) Jean-Francois Micouleau      1999-2000
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"
#include "nterr.h"
#include "rpc_parse.h"
#include "rpcclient.h"

#undef DBGC_CLASS
#define DBGC_CLASS DBGC_RPC_CLI

#define DEBUG_TESTING

extern FILE* out_hnd;

extern struct user_creds *usr_creds;

/********************************************************************
initialize a spoolss NEW_BUFFER.
********************************************************************/
void init_buffer(NEW_BUFFER *buffer, uint32 size, TALLOC_CTX *ctx)
{
	buffer->ptr = (size!=0)? 1:0;
	buffer->size=size;
	buffer->string_at_end=size;
	prs_init(&buffer->prs, size, ctx, MARSHALL);
	buffer->struct_start = prs_offset(&buffer->prs);
}

static void decode_printer_info_0(NEW_BUFFER *buffer, uint32 returned, 
				  PRINTER_INFO_0 **info)
{
        uint32 i;
        PRINTER_INFO_0  *inf;

        inf=(PRINTER_INFO_0 *)malloc(returned*sizeof(PRINTER_INFO_0));

        buffer->prs.data_offset=0;

        for (i=0; i<returned; i++) {
                new_smb_io_printer_info_0("", buffer, &(inf[i]), 0);
        }

        *info=inf;
}

static void decode_printer_info_1(NEW_BUFFER *buffer, uint32 returned, 
				  PRINTER_INFO_1 **info)
{
        uint32 i;
        PRINTER_INFO_1  *inf;

        inf=(PRINTER_INFO_1 *)malloc(returned*sizeof(PRINTER_INFO_1));

        buffer->prs.data_offset=0;

        for (i=0; i<returned; i++) {
                new_smb_io_printer_info_1("", buffer, &(inf[i]), 0);
        }

        *info=inf;
}

static void decode_printer_info_2(NEW_BUFFER *buffer, uint32 returned, 
				  PRINTER_INFO_2 **info)
{
        uint32 i;
        PRINTER_INFO_2  *inf;

        inf=(PRINTER_INFO_2 *)malloc(returned*sizeof(PRINTER_INFO_2));

        buffer->prs.data_offset=0;

        for (i=0; i<returned; i++) {
		/* a little initialization as we go */
		inf[i].secdesc = NULL;
                new_smb_io_printer_info_2("", buffer, &(inf[i]), 0);
        }

        *info=inf;
}

static void decode_printer_info_3(NEW_BUFFER *buffer, uint32 returned, 
				  PRINTER_INFO_3 **info)
{
        uint32 i;
        PRINTER_INFO_3  *inf;

        inf=(PRINTER_INFO_3 *)malloc(returned*sizeof(PRINTER_INFO_3));

        buffer->prs.data_offset=0;

        for (i=0; i<returned; i++) {
                new_smb_io_printer_info_3("", buffer, &(inf[i]), 0);
        }

        *info=inf;
}

static void decode_printer_driver_1(NEW_BUFFER *buffer, uint32 returned, 
			 	    DRIVER_INFO_1 **info)
{
        uint32 i;
        DRIVER_INFO_1 *inf;

        inf=(DRIVER_INFO_1 *)malloc(returned*sizeof(DRIVER_INFO_1));

        buffer->prs.data_offset=0;

        for (i=0; i<returned; i++) {
                new_smb_io_printer_driver_info_1("", buffer, &(inf[i]), 0);
        }

        *info=inf;
}

static void decode_printer_driver_2(NEW_BUFFER *buffer, uint32 returned, 
				    DRIVER_INFO_2 **info)
{
        uint32 i;
        DRIVER_INFO_2 *inf;

        inf=(DRIVER_INFO_2 *)malloc(returned*sizeof(DRIVER_INFO_2));

        buffer->prs.data_offset=0;

        for (i=0; i<returned; i++) {
                new_smb_io_printer_driver_info_2("", buffer, &(inf[i]), 0);
        }

        *info=inf;
}

static void decode_printer_driver_3(NEW_BUFFER *buffer, uint32 returned, 
				    DRIVER_INFO_3 **info)
{
        uint32 i;
        DRIVER_INFO_3 *inf;

        inf=(DRIVER_INFO_3 *)malloc(returned*sizeof(DRIVER_INFO_3));

        buffer->prs.data_offset=0;

        for (i=0; i<returned; i++) {
                new_smb_io_printer_driver_info_3("", buffer, &(inf[i]), 0);
        }

        *info=inf;
}

static void decode_printerdriverdir_info_1(NEW_BUFFER *buffer, DRIVER_DIRECTORY_1 *info)
{
/*      DRIVER_DIRECTORY_1 *inf;

        inf=(DRIVER_DIRECTORY_1 *)malloc(returned*sizeof(DRIVER_DIRECTORY_1));
*/
        prs_set_offset(&buffer->prs, 0);

        new_smb_io_driverdir_1("", buffer, info, 0);

/*      *info=inf;*/
}

/**********************************************************************
 Decode a PORT_INFO_1 struct from a NEW_BUFFER 
**********************************************************************/
void decode_port_info_1(NEW_BUFFER *buffer, uint32 returned, 
			PORT_INFO_1 **info)
{
        uint32 i;
        PORT_INFO_1 *inf;

        inf=(PORT_INFO_1*)malloc(returned*sizeof(PORT_INFO_1));

        prs_set_offset(&buffer->prs, 0);

        for (i=0; i<returned; i++) {
                new_smb_io_port_info_1("", buffer, &(inf[i]), 0);
        }

        *info=inf;
}

/**********************************************************************
 Decode a PORT_INFO_2 struct from a NEW_BUFFER 
**********************************************************************/
void decode_port_info_2(NEW_BUFFER *buffer, uint32 returned, 
			PORT_INFO_2 **info)
{
        uint32 i;
        PORT_INFO_2 *inf;

        inf=(PORT_INFO_2*)malloc(returned*sizeof(PORT_INFO_2));

        prs_set_offset(&buffer->prs, 0);

        for (i=0; i<returned; i++) {
                new_smb_io_port_info_2("", buffer, &(inf[i]), 0);
        }

        *info=inf;
}


/****************************************************************************
nt spoolss query
****************************************************************************/
BOOL msrpc_spoolss_enum_printers(char* srv_name, uint32 flags, 
				 uint32 level, PRINTER_INFO_CTR ctr)
{
	NTSTATUS status;
	NEW_BUFFER buffer;
	uint32 needed;
	uint32 returned;
	TALLOC_CTX *mem_ctx = NULL;
	
	if ((mem_ctx=talloc_init()) == NULL)
	{
		DEBUG(0,("msrpc_spoolss_enum_printers: talloc_init failed!\n"));
		return False;
	}
	init_buffer(&buffer, 0, mem_ctx);
	
	/* send a NULL buffer first */
	status=spoolss_enum_printers(flags, srv_name, level, &buffer, 0, 
				     &needed, &returned);
	
	if (status==ERROR_INSUFFICIENT_BUFFER) {
		init_buffer(&buffer, needed, mem_ctx);
		status=spoolss_enum_printers(flags, srv_name, level, &buffer, 
					     needed, &needed, &returned);
	}
	
	if (status!=NT_STATUS_OK)
	{
		DEBUG(0,("spoolss_enum_printers: %s\n", nt_errstr(status)));
		if (mem_ctx)
			talloc_destroy(mem_ctx);
		return False;
	}
		
	/* is there anything to process? */
	if (returned != 0)
	{
		switch (level) {
		case 1:
			decode_printer_info_1(&buffer, returned, &(ctr.printers_1));
			break;
		case 2:
			decode_printer_info_2(&buffer, returned, &(ctr.printers_2));
			break;
		case 3:
			decode_printer_info_3(&buffer, returned, &(ctr.printers_3));
			break;
		}		

		display_printer_info_ctr(out_hnd, ACTION_HEADER   , level, returned, ctr);
		display_printer_info_ctr(out_hnd, ACTION_ENUMERATE, level, returned, ctr);
		display_printer_info_ctr(out_hnd, ACTION_FOOTER   , level, returned, ctr);
	}

	if (mem_ctx)
		talloc_destroy(mem_ctx);
		
	return True;
}

/****************************************************************************
nt spoolss query
****************************************************************************/
BOOL msrpc_spoolss_enum_ports(char* srv_name, 
				 uint32 level, PORT_INFO_CTR *ctr)
{
	NTSTATUS status;
	NEW_BUFFER buffer;
	uint32 needed;
	uint32 returned;
	TALLOC_CTX *mem_ctx = NULL;
	
	if ((mem_ctx=talloc_init()) == NULL)
	{
		DEBUG(0,("msrpc_spoolss_enum_ports: talloc_init failed!\n"));
		return False;
	}
	
	init_buffer(&buffer, 0, mem_ctx);
	
	/* send a NULL buffer first */
	status=spoolss_enum_ports(srv_name, level, &buffer, 0, 
				     &needed, &returned);
	
	if (status==ERROR_INSUFFICIENT_BUFFER) {
		init_buffer(&buffer, needed, mem_ctx);
		status=spoolss_enum_ports(srv_name, level, &buffer, 
					  needed, &needed, &returned);
	}
	
	report(out_hnd, "\tstatus:[%d (%x)]\n", status, status);
	
	if (status!=NT_STATUS_OK)
	{
		if (mem_ctx)
			talloc_destroy(mem_ctx);
		return False;
	}
		
	/* is there anything to process? */
	if (returned != 0)
	{
		switch (level) {
		case 1:
			decode_port_info_1(&buffer, returned, &ctr->port.info_1);
			break;
		case 2:
			decode_port_info_2(&buffer, returned, &ctr->port.info_2);
			break;
		default:
			DEBUG(0,("Unable to decode unknown PORT_INFO_%d\n", level));
			break;
		}		

		display_port_info_ctr(out_hnd, ACTION_HEADER   , level, returned, ctr);
		display_port_info_ctr(out_hnd, ACTION_ENUMERATE, level, returned, ctr);
		display_port_info_ctr(out_hnd, ACTION_FOOTER   , level, returned, ctr);
	}
	if (mem_ctx)
		talloc_destroy(mem_ctx);



	return True;
}

/****************************************************************************
nt spoolss query
****************************************************************************/
uint32 msrpc_spoolss_getprinterdata( const char* printer_name,
                                const char* station,
                                const char* user_name,
                                const char* value_name,
                                uint32 *type,
                                NEW_BUFFER *buffer,
                                void *fn)
{
        POLICY_HND hnd;
        NTSTATUS status;
        uint32 needed;
        uint32 size;
        char *data;
        UNISTR2 uni_val_name;
	TALLOC_CTX *mem_ctx = NULL;
	
        DEBUG(4,("spoolgetdata - printer: %s server: %s user: %s value: %s\n",
                printer_name, station, user_name, value_name));

        if(!spoolss_open_printer_ex( printer_name, 0, 0, station, user_name,
                                &hnd))
        {
                return NT_STATUS_ACCESS_DENIED;
        }

        init_unistr2(&uni_val_name, value_name, 0);
        size = 0;
        data = NULL;

	if ((mem_ctx=talloc_init()) == NULL)
	{
		DEBUG(0,("msrpc_spoolss_getprinterdata: talloc_init failed!\n"));
		return False;
	}
        init_buffer(buffer, size, mem_ctx);

        status = spoolss_getprinterdata(&hnd, &uni_val_name, size, type, &size,
                        (unsigned char *)data, &needed);

        if (status == ERROR_INSUFFICIENT_BUFFER)
        {
                size = needed;
                init_buffer(buffer, size, mem_ctx);
                data = prs_data_p(&buffer->prs);
                status = spoolss_getprinterdata(&hnd, &uni_val_name,
                                size, type, &size,
                                (unsigned char *)data, &needed);
        }
	
	if (mem_ctx)
		talloc_destroy(mem_ctx);

        if (status != NT_STATUS_OK) 
	{
                if (!spoolss_closeprinter(&hnd))
                        return NT_STATUS_ACCESS_DENIED;
                return status;
        }

#if  0
        if (fn != NULL)
                fn(printer_name, station, level, returned, *ctr);
#endif

        return status;
}

/****************************************************************************
nt spoolss query
****************************************************************************/
BOOL msrpc_spoolss_enum_jobs( const char* printer_name,
                                const char* station, const char* user_name,
                                uint32 level,
                                void ***ctr, JOB_INFO_FN(fn))
{
        POLICY_HND hnd;
        NTSTATUS status;
        NEW_BUFFER buffer;
        uint32 needed;
        uint32 returned;
        uint32 firstjob=0;
        uint32 numofjobs=0xffff;
	TALLOC_CTX *mem_ctx = NULL;
	
        DEBUG(4,("spoolopen - printer: %s server: %s user: %s\n",
                printer_name, station, user_name));

        if(!spoolss_open_printer_ex( printer_name, 0, 0, station, user_name, &hnd))
                return False;

	if ((mem_ctx=talloc_init()) == NULL)
	{
		DEBUG(0,("msrpc_spoolss_enum_jobs: talloc_init failed!\n"));
		return False;
	}
        init_buffer(&buffer, 0, mem_ctx);
        status = spoolss_enum_jobs(&hnd, firstjob, numofjobs, level, 
				   &buffer, 0, &needed, &returned);

        if (status == ERROR_INSUFFICIENT_BUFFER)
        {
                init_buffer(&buffer, needed, mem_ctx);
                status = spoolss_enum_jobs( &hnd, firstjob, numofjobs, level, 
					    &buffer, needed, &needed, &returned);
        }

	if (mem_ctx)
		talloc_destroy(mem_ctx);
		
        if (status!=NT_STATUS_OK) {
                if (!spoolss_closeprinter(&hnd))
                        return False;
                return False;
        }

        if (fn != NULL)
                fn(printer_name, station, level, returned, *ctr);

        return True;
}


/****************************************************************************
nt spoolss query
****************************************************************************/
BOOL msrpc_spoolss_enum_printerdata( const char* printer_name, 
		const char* station, const char* user_name )
{
	POLICY_HND hnd;
	NTSTATUS status;
	uint32 idx;
	uint32 valuelen;
	uint16 *value;
	uint32 rvaluelen;
	uint32 type;
	uint32 datalen;
	uint8  *data;
	uint32 rdatalen;
	uint32 maxvaluelen;
	uint32 maxdatalen;
	
	DEBUG(4,("msrpc_spoolss_enum_printerdata - printer: %s\n", printer_name));

	if(!spoolss_open_printer_ex( printer_name, 0, 0, station, user_name, &hnd))
		return False;

	
	idx=0;
	valuelen=0;
	rvaluelen=0;
	type=0;
	datalen=0;
	rdatalen=0;
	
	status = spoolss_enum_printerdata(&hnd, idx, &valuelen, value, 
					  &rvaluelen, &type, &datalen, 
					  data, &rdatalen);

	DEBUG(4,("spoolenum_printerdata - got size: biggest value:[%d], biggest data:[%d]\n", rvaluelen, rdatalen));

	maxvaluelen=valuelen=rvaluelen;
	maxdatalen=datalen=rdatalen;

	value=(uint16 *)malloc(valuelen*sizeof(uint16));
	data=(uint8 *)malloc(datalen*sizeof(uint8));

	display_printer_enumdata(out_hnd, ACTION_HEADER, idx, valuelen, 
				 value, rvaluelen, type, datalen, data, rdatalen);
	
	do {
		valuelen=maxvaluelen;
		datalen=maxdatalen;

		status = spoolss_enum_printerdata(&hnd, idx, &valuelen, 
						  value, &rvaluelen, &type, 
 						  &datalen, data, &rdatalen);
		display_printer_enumdata(out_hnd, ACTION_ENUMERATE, idx, 
					 valuelen, value, rvaluelen, type, 
					 datalen, data, rdatalen);
		idx++;

	} while (status != 0x0103); /* NO_MORE_ITEMS */

	display_printer_enumdata(out_hnd, ACTION_FOOTER, idx, valuelen, 
				 value, rvaluelen, type, datalen, data, rdatalen);

	
	if (status!=NT_STATUS_OK) {
		/* 
		 * the check on this if statement is redundant
		 * since is the status is bad we're going to 
		 * return False anyways.  The caller will be 
		 * unable to determine if there really was a problem
		 * with the spoolss_closeprinter() call  --jerry
		 */
		spoolss_closeprinter(&hnd);
		return False;
	}
	
	return True;
}

/****************************************************************************
nt spoolss query
****************************************************************************/
BOOL msrpc_spoolss_getprinter( const char* printer_name, const uint32 level,
                const char* station, const char* user_name,
                PRINTER_INFO_CTR ctr)
{
        POLICY_HND hnd;
        NTSTATUS status=0;
        NEW_BUFFER buffer;
        uint32 needed=1000;
	TALLOC_CTX *mem_ctx = NULL;
	
        DEBUG(4,("spoolenum_getprinter - printer: %s\n", printer_name));

        if(!spoolss_open_printer_ex( printer_name, "", PRINTER_ALL_ACCESS, station, user_name, &hnd))
                return False;

	if ((mem_ctx=talloc_init()) == NULL)
	{
		DEBUG(0,("msrpc_spoolss_getprinter: talloc_init failed!\n"));
		return False;
	}
        init_buffer(&buffer, needed, mem_ctx);

        status = spoolss_getprinter(&hnd, level, &buffer, needed, &needed);

        if (status==ERROR_INSUFFICIENT_BUFFER) {
                init_buffer(&buffer, needed, mem_ctx);
                status = spoolss_getprinter(&hnd, level, &buffer, needed, &needed);
        }

        report(out_hnd, "\tstatus:[%d (%x)]\n", status, status);

        if (status!=NT_STATUS_OK)
	{
		if (mem_ctx)
			talloc_destroy(mem_ctx);
                return False;
	}

        switch (level) {
        case 0:
                decode_printer_info_0(&buffer, 1, &(ctr.printers_0));
                break;
        case 1:
                decode_printer_info_1(&buffer, 1, &(ctr.printers_1));
                break;
        case 2:
                decode_printer_info_2(&buffer, 1, &(ctr.printers_2));
                break;
        case 3:
                decode_printer_info_3(&buffer, 1, &(ctr.printers_3));
                break;
        }

        display_printer_info_ctr(out_hnd, ACTION_HEADER   , level, 1, ctr);
        display_printer_info_ctr(out_hnd, ACTION_ENUMERATE, level, 1, ctr);
        display_printer_info_ctr(out_hnd, ACTION_FOOTER   , level, 1, ctr);

	if (mem_ctx)
		talloc_destroy(mem_ctx);

        if (status!=NT_STATUS_OK) {
                if (!spoolss_closeprinter(&hnd))
                        return False;
                return False;
        }

        return True;
}

/****************************************************************************
nt spoolss query
****************************************************************************/
BOOL msrpc_spoolss_getprinterdriver( const char* printer_name,
                const char *environment, const uint32 level,
                const char* station, const char* user_name,
                PRINTER_DRIVER_CTR ctr)
{
        POLICY_HND hnd;
        NTSTATUS status=0;
        NEW_BUFFER buffer;
        uint32 needed;
	TALLOC_CTX *mem_ctx = NULL;
	
        DEBUG(4,("msrpc_spoolss_enum_getprinterdriver - printer: %s\n", printer_name));

        if(!spoolss_open_printer_ex( printer_name, "", PRINTER_ALL_ACCESS, station, user_name, &hnd))
                return False;

	if ((mem_ctx=talloc_init()) == NULL)
	{
		DEBUG(0,("msrpc_spoolss_getprinterdriver: talloc_init failed!\n"));
		return False;
	}
        init_buffer(&buffer, 0, mem_ctx);

        status = spoolss_getprinterdriver(&hnd, environment, level, &buffer, 0, &needed);

        if (status==ERROR_INSUFFICIENT_BUFFER) {
                init_buffer(&buffer, needed, mem_ctx);
                status = spoolss_getprinterdriver(&hnd, environment, level, &buffer, needed, &needed);
        }

        /* report(out_hnd, "\tstatus:[%d (%x)]\n", status, status); */

        if (status!=NT_STATUS_OK)
	{
		if (mem_ctx)
			talloc_destroy(mem_ctx);
                return False;
	}

        switch (level) {
		case 1:
			decode_printer_driver_1(&buffer, 1, &(ctr.info1));
			break;
		case 2:
			decode_printer_driver_2(&buffer, 1, &(ctr.info2));
			break;
		case 3:
			decode_printer_driver_3(&buffer, 1, &(ctr.info3));
			break;
        }

        display_printer_driver_ctr(out_hnd, ACTION_HEADER   , level, 1, ctr);
        display_printer_driver_ctr(out_hnd, ACTION_ENUMERATE, level, 1, ctr);
        display_printer_driver_ctr(out_hnd, ACTION_FOOTER   , level, 1, ctr);

	if (mem_ctx)
		talloc_destroy(mem_ctx);
		
        if (status!=NT_STATUS_OK) {
                if (!spoolss_closeprinter(&hnd))
                        return False;
                return False;
        }

        return True;
}

/****************************************************************************
nt spoolss query
****************************************************************************/
BOOL msrpc_spoolss_enumprinterdrivers( const char* srv_name,
                const char *environment, const uint32 level,
                PRINTER_DRIVER_CTR ctr)
{
        NTSTATUS status=0;
        NEW_BUFFER buffer;
        uint32 needed;
        uint32 returned;
	TALLOC_CTX *mem_ctx = NULL;
	
        DEBUG(4,("msrpc_spoolss_enum_enumprinterdrivers - server: %s\n", srv_name));

	if ((mem_ctx=talloc_init()) == NULL)
	{
		DEBUG(0,("msrpc_spoolss_enumprinterdrivers: talloc_init failed!\n"));
		return False;
	}
        init_buffer(&buffer, 0, mem_ctx);

        status = spoolss_enum_printerdrivers(srv_name, environment,
                                level, &buffer, 0, &needed, &returned);

        if (status == ERROR_INSUFFICIENT_BUFFER)
        {
                init_buffer(&buffer, needed, mem_ctx);
                status = spoolss_enum_printerdrivers( srv_name, environment,
                                level, &buffer, needed, &needed, &returned);
        }

        report(out_hnd, "\tstatus:[%d (%x)]\n", status, status);

        if (status!=NT_STATUS_OK)
	{
		if (mem_ctx)
			talloc_destroy(mem_ctx);
                return False;
	}

        switch (level)
        {
                case 1:
                        decode_printer_driver_1(&buffer, returned, &(ctr.info1));
                        break;
                case 2:
                        decode_printer_driver_2(&buffer, returned, &(ctr.info2));
                        break;
                case 3:
                        decode_printer_driver_3(&buffer, returned, &(ctr.info3));
                        break;
        }

        display_printer_driver_ctr(out_hnd, ACTION_HEADER   , level, returned, ctr);
        display_printer_driver_ctr(out_hnd, ACTION_ENUMERATE, level, returned, ctr);
        display_printer_driver_ctr(out_hnd, ACTION_FOOTER   , level, returned, ctr);

	if (mem_ctx)
		talloc_destroy(mem_ctx);
		
        return True;
}

/****************************************************************************
nt spoolss query
****************************************************************************/
BOOL msrpc_spoolss_getprinterdriverdir(char* srv_name, char* env_name, uint32 level, DRIVER_DIRECTORY_CTR ctr)
{
        NTSTATUS status;
        NEW_BUFFER buffer;
        uint32 needed;
	TALLOC_CTX *mem_ctx = NULL;
	
	if ((mem_ctx=talloc_init()) == NULL)
	{
		DEBUG(0,("msrpc_spoolss_getprinterdriverdir: talloc_init failed!\n"));
		return False;
	}
        init_buffer(&buffer, 0, mem_ctx);

        /* send a NULL buffer first */
        status=spoolss_getprinterdriverdir(srv_name, env_name, level, &buffer, 0, &needed);

        if (status==ERROR_INSUFFICIENT_BUFFER) {
                init_buffer(&buffer, needed, mem_ctx);
                status=spoolss_getprinterdriverdir(srv_name, env_name, level, &buffer, needed, &needed);
        }

        report(out_hnd, "\tstatus:[%d (%x)]\n", status, status);

        if (status!=NT_STATUS_OK)
	{
		if (mem_ctx)
			talloc_destroy(mem_ctx);
                return False;
	}

        switch (level) {
        case 1:
                decode_printerdriverdir_info_1(&buffer, &(ctr.driver.info_1));
                break;
        }

        display_printerdriverdir_info_ctr(out_hnd, ACTION_HEADER   , level, ctr);
        display_printerdriverdir_info_ctr(out_hnd, ACTION_ENUMERATE, level, ctr);
        display_printerdriverdir_info_ctr(out_hnd, ACTION_FOOTER   , level, ctr);
	
	if (mem_ctx)
		talloc_destroy(mem_ctx);

        return True;
}