r7691: * add .gdbinit to the svn:ignore files
[vlendec/samba-autobuild/.git] / source3 / rpc_client / cli_spoolss.c
1 /*
2    Unix SMB/CIFS implementation.
3    RPC pipe client
4
5    Copyright (C) Gerald Carter                2001-2002,
6    Copyright (C) Tim Potter                   2000-2002,
7    Copyright (C) Andrew Tridgell              1994-2000,
8    Copyright (C) Luke Kenneth Casson Leighton 1996-2000,
9    Copyright (C) Jean-Francois Micouleau      1999-2000.
10
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 2 of the License, or
14    (at your option) any later version.
15
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with this program; if not, write to the Free Software
23    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
25
26 #include "includes.h"
27
28 /** @defgroup spoolss SPOOLSS - NT printing routines
29  *  @ingroup rpc_client
30  *
31  * @{
32  **/
33
34 /*********************************************************************
35  Decode various spoolss rpc's and info levels
36  ********************************************************************/
37
38 /**********************************************************************
39 **********************************************************************/
40 static void decode_printer_info_0(TALLOC_CTX *mem_ctx, RPC_BUFFER *buffer,
41                                 uint32 returned, PRINTER_INFO_0 **info)
42 {
43         uint32 i;
44         PRINTER_INFO_0  *inf;
45
46         inf=TALLOC_ARRAY(mem_ctx, PRINTER_INFO_0, returned);
47         memset(inf, 0, returned*sizeof(PRINTER_INFO_0));
48
49         prs_set_offset(&buffer->prs,0);
50
51         for (i=0; i<returned; i++) {
52                 smb_io_printer_info_0("", buffer, &inf[i], 0);
53         }
54
55         *info=inf;
56 }
57
58 /**********************************************************************
59 **********************************************************************/
60 static void decode_printer_info_1(TALLOC_CTX *mem_ctx, RPC_BUFFER *buffer,
61                                 uint32 returned, PRINTER_INFO_1 **info)
62 {
63         uint32 i;
64         PRINTER_INFO_1  *inf;
65
66         inf=TALLOC_ARRAY(mem_ctx, PRINTER_INFO_1, returned);
67         memset(inf, 0, returned*sizeof(PRINTER_INFO_1));
68
69         prs_set_offset(&buffer->prs,0);
70
71         for (i=0; i<returned; i++) {
72                 smb_io_printer_info_1("", buffer, &inf[i], 0);
73         }
74
75         *info=inf;
76 }
77
78 /**********************************************************************
79 **********************************************************************/
80 static void decode_printer_info_2(TALLOC_CTX *mem_ctx, RPC_BUFFER *buffer, 
81                                 uint32 returned, PRINTER_INFO_2 **info)
82 {
83         uint32 i;
84         PRINTER_INFO_2  *inf;
85
86         inf=TALLOC_ARRAY(mem_ctx, PRINTER_INFO_2, returned);
87         memset(inf, 0, returned*sizeof(PRINTER_INFO_2));
88
89         prs_set_offset(&buffer->prs,0);
90
91         for (i=0; i<returned; i++) {
92                 /* a little initialization as we go */
93                 inf[i].secdesc = NULL;
94                 smb_io_printer_info_2("", buffer, &inf[i], 0);
95         }
96
97         *info=inf;
98 }
99
100 /**********************************************************************
101 **********************************************************************/
102 static void decode_printer_info_3(TALLOC_CTX *mem_ctx, RPC_BUFFER *buffer, 
103                                 uint32 returned, PRINTER_INFO_3 **info)
104 {
105         uint32 i;
106         PRINTER_INFO_3  *inf;
107
108         inf=TALLOC_ARRAY(mem_ctx, PRINTER_INFO_3, returned);
109         memset(inf, 0, returned*sizeof(PRINTER_INFO_3));
110
111         prs_set_offset(&buffer->prs,0);
112
113         for (i=0; i<returned; i++) {
114                 inf[i].secdesc = NULL;
115                 smb_io_printer_info_3("", buffer, &inf[i], 0);
116         }
117
118         *info=inf;
119 }
120
121 /**********************************************************************
122 **********************************************************************/
123 static void decode_printer_info_7(TALLOC_CTX *mem_ctx, RPC_BUFFER *buffer,
124                                 uint32 returned, PRINTER_INFO_7 **info)
125 {
126         uint32 i;
127         PRINTER_INFO_7  *inf;
128
129         inf=TALLOC_ARRAY(mem_ctx, PRINTER_INFO_7, returned);
130         memset(inf, 0, returned*sizeof(PRINTER_INFO_7));
131
132         prs_set_offset(&buffer->prs,0);
133
134         for (i=0; i<returned; i++) {
135                 smb_io_printer_info_7("", buffer, &inf[i], 0);
136         }
137
138         *info=inf;
139 }
140
141
142 /**********************************************************************
143 **********************************************************************/
144 static void decode_port_info_1(TALLOC_CTX *mem_ctx, RPC_BUFFER *buffer, 
145                         uint32 returned, PORT_INFO_1 **info)
146 {
147         uint32 i;
148         PORT_INFO_1 *inf;
149
150         inf=TALLOC_ARRAY(mem_ctx, PORT_INFO_1, returned);
151         memset(inf, 0, returned*sizeof(PORT_INFO_1));
152
153         prs_set_offset(&buffer->prs, 0);
154
155         for (i=0; i<returned; i++) {
156                 smb_io_port_info_1("", buffer, &(inf[i]), 0);
157         }
158
159         *info=inf;
160 }
161
162 /**********************************************************************
163 **********************************************************************/
164 static void decode_port_info_2(TALLOC_CTX *mem_ctx, RPC_BUFFER *buffer, 
165                         uint32 returned, PORT_INFO_2 **info)
166 {
167         uint32 i;
168         PORT_INFO_2 *inf;
169
170         inf=TALLOC_ARRAY(mem_ctx, PORT_INFO_2, returned);
171         memset(inf, 0, returned*sizeof(PORT_INFO_2));
172
173         prs_set_offset(&buffer->prs, 0);
174
175         for (i=0; i<returned; i++) {
176                 smb_io_port_info_2("", buffer, &(inf[i]), 0);
177         }
178
179         *info=inf;
180 }
181
182 /**********************************************************************
183 **********************************************************************/
184 static void decode_printer_driver_1(TALLOC_CTX *mem_ctx, RPC_BUFFER *buffer, 
185                         uint32 returned, DRIVER_INFO_1 **info)
186 {
187         uint32 i;
188         DRIVER_INFO_1 *inf;
189
190         inf=TALLOC_ARRAY(mem_ctx, DRIVER_INFO_1, returned);
191         memset(inf, 0, returned*sizeof(DRIVER_INFO_1));
192
193         prs_set_offset(&buffer->prs,0);
194
195         for (i=0; i<returned; i++) {
196                 smb_io_printer_driver_info_1("", buffer, &(inf[i]), 0);
197         }
198
199         *info=inf;
200 }
201
202 /**********************************************************************
203 **********************************************************************/
204 static void decode_printer_driver_2(TALLOC_CTX *mem_ctx, RPC_BUFFER *buffer, 
205                         uint32 returned, DRIVER_INFO_2 **info)
206 {
207         uint32 i;
208         DRIVER_INFO_2 *inf;
209
210         inf=TALLOC_ARRAY(mem_ctx, DRIVER_INFO_2, returned);
211         memset(inf, 0, returned*sizeof(DRIVER_INFO_2));
212
213         prs_set_offset(&buffer->prs,0);
214
215         for (i=0; i<returned; i++) {
216                 smb_io_printer_driver_info_2("", buffer, &(inf[i]), 0);
217         }
218
219         *info=inf;
220 }
221
222 /**********************************************************************
223 **********************************************************************/
224 static void decode_printer_driver_3(TALLOC_CTX *mem_ctx, RPC_BUFFER *buffer, 
225                         uint32 returned, DRIVER_INFO_3 **info)
226 {
227         uint32 i;
228         DRIVER_INFO_3 *inf;
229
230         inf=TALLOC_ARRAY(mem_ctx, DRIVER_INFO_3, returned);
231         memset(inf, 0, returned*sizeof(DRIVER_INFO_3));
232
233         prs_set_offset(&buffer->prs,0);
234
235         for (i=0; i<returned; i++) {
236                 smb_io_printer_driver_info_3("", buffer, &(inf[i]), 0);
237         }
238
239         *info=inf;
240 }
241
242 /**********************************************************************
243 **********************************************************************/
244 static void decode_printerdriverdir_1 (TALLOC_CTX *mem_ctx, RPC_BUFFER *buffer,
245                         uint32 returned, DRIVER_DIRECTORY_1 **info
246 )
247 {
248         DRIVER_DIRECTORY_1 *inf;
249  
250         inf=TALLOC_P(mem_ctx, DRIVER_DIRECTORY_1);
251         memset(inf, 0, sizeof(DRIVER_DIRECTORY_1));
252
253         prs_set_offset(&buffer->prs, 0);
254
255         smb_io_driverdir_1("", buffer, inf, 0);
256  
257         *info=inf;
258 }
259
260 /** Return a handle to the specified printer or print server.
261  *
262  * @param cli              Pointer to client state structure which is open
263  * on the SPOOLSS pipe.
264  *
265  * @param mem_ctx          Pointer to an initialised talloc context.
266  *
267  * @param printername      The name of the printer or print server to be
268  * opened in UNC format.
269  *
270  * @param datatype         Specifies the default data type for the printer.
271  *
272  * @param access_required  The access rights requested on the printer or
273  * print server.
274  *
275  * @param station          The UNC name of the requesting workstation.
276  *
277  * @param username         The name of the user requesting the open.
278  *
279  * @param pol              Returned policy handle.
280  */
281
282 /*********************************************************************************
283  Win32 API - OpenPrinter()
284  ********************************************************************************/
285
286 WERROR cli_spoolss_open_printer_ex(struct cli_state *cli, TALLOC_CTX *mem_ctx,
287                                 const char *printername, const char *datatype, uint32 access_required,
288                                 const char *station, const char *username, POLICY_HND *pol)
289 {
290         prs_struct qbuf, rbuf;
291         SPOOL_Q_OPEN_PRINTER_EX q;
292         SPOOL_R_OPEN_PRINTER_EX r;
293         WERROR result = W_ERROR(ERRgeneral);
294
295         ZERO_STRUCT(q);
296         ZERO_STRUCT(r);
297
298         /* Initialise parse structures */
299
300         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
301         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
302
303         /* Initialise input parameters */
304
305         make_spoolss_q_open_printer_ex(&q, printername, datatype,
306                                        access_required, station, username);
307
308         /* Marshall data and send request */
309
310         if (!spoolss_io_q_open_printer_ex("", &q, &qbuf, 0) ||
311             !rpc_api_pipe_req(cli, PI_SPOOLSS, SPOOLSS_OPENPRINTEREX, &qbuf, &rbuf))
312                 goto done;
313
314         /* Unmarshall response */
315
316         if (!spoolss_io_r_open_printer_ex("", &r, &rbuf, 0))
317                 goto done;
318
319         /* Return output parameters */
320
321         result = r.status;
322
323         if (W_ERROR_IS_OK(result))
324                 *pol = r.handle;
325
326  done:
327         prs_mem_free(&qbuf);
328         prs_mem_free(&rbuf);
329
330         return result;
331 }
332
333 /** Close a printer handle
334  *
335  * @param cli              Pointer to client state structure which is open
336  * on the SPOOLSS pipe.
337  *
338  * @param mem_ctx          Pointer to an initialised talloc context.
339  *
340  * @param pol              Policy handle of printer or print server to close.
341  */
342 /*********************************************************************************
343  Win32 API - ClosePrinter()
344  ********************************************************************************/
345
346 WERROR cli_spoolss_close_printer(struct cli_state *cli, TALLOC_CTX *mem_ctx,
347                                  POLICY_HND *pol)
348 {
349         prs_struct qbuf, rbuf;
350         SPOOL_Q_CLOSEPRINTER q;
351         SPOOL_R_CLOSEPRINTER r;
352         WERROR result = W_ERROR(ERRgeneral);
353
354         ZERO_STRUCT(q);
355         ZERO_STRUCT(r);
356
357         /* Initialise parse structures */
358
359         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
360         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
361
362         /* Initialise input parameters */
363
364         make_spoolss_q_closeprinter(&q, pol);
365
366         /* Marshall data and send request */
367
368         if (!spoolss_io_q_closeprinter("", &q, &qbuf, 0) ||
369             !rpc_api_pipe_req(cli, PI_SPOOLSS, SPOOLSS_CLOSEPRINTER, &qbuf, &rbuf))
370                 goto done;
371
372         /* Unmarshall response */
373
374         if (!spoolss_io_r_closeprinter("", &r, &rbuf, 0))
375                 goto done;
376
377         /* Return output parameters */
378
379         result = r.status;
380
381         if (W_ERROR_IS_OK(result))
382                 *pol = r.handle;
383
384  done:
385         prs_mem_free(&qbuf);
386         prs_mem_free(&rbuf);
387
388         return result;
389 }
390
391 /** Enumerate printers on a print server.
392  *
393  * @param cli              Pointer to client state structure which is open
394  *                         on the SPOOLSS pipe.
395  * @param mem_ctx          Pointer to an initialised talloc context.
396  *
397  * @param offered          Buffer size offered in the request.
398  * @param needed           Number of bytes needed to complete the request.
399  *                         may be NULL.
400  *
401  * @param flags            Selected from PRINTER_ENUM_* flags.
402  * @param level            Request information level.
403  *
404  * @param num_printers     Pointer to number of printers returned.  May be
405  *                         NULL.
406  * @param ctr              Return structure for printer information.  May
407  *                         be NULL.
408  */
409 /*********************************************************************************
410  Win32 API - EnumPrinters()
411  ********************************************************************************/
412
413 WERROR cli_spoolss_enum_printers(struct cli_state *cli, TALLOC_CTX *mem_ctx,
414                                  uint32 offered, uint32 *needed,
415                                  char *name, uint32 flags, uint32 level,
416                                  uint32 *num_printers, PRINTER_INFO_CTR *ctr)
417 {
418         prs_struct qbuf, rbuf;
419         SPOOL_Q_ENUMPRINTERS q;
420         SPOOL_R_ENUMPRINTERS r;
421         RPC_BUFFER buffer;
422         WERROR result = W_ERROR(ERRgeneral);
423
424         ZERO_STRUCT(q);
425         ZERO_STRUCT(r);
426
427         /* Initialise input parameters */
428
429         rpcbuf_init(&buffer, offered, mem_ctx);
430
431         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
432         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
433
434         make_spoolss_q_enumprinters(&q, flags, name, level, &buffer, 
435                                     offered);
436
437         /* Marshall data and send request */
438         
439         if (!spoolss_io_q_enumprinters("", &q, &qbuf, 0) ||
440             !rpc_api_pipe_req(cli, PI_SPOOLSS, SPOOLSS_ENUMPRINTERS, &qbuf, &rbuf))
441                 goto done;
442
443         /* Unmarshall response */
444
445         if (spoolss_io_r_enumprinters("", &r, &rbuf, 0)) {
446                 if (needed)
447                         *needed = r.needed;
448         }
449         
450         result = r.status;
451
452         /* Return output parameters */
453
454         if (!W_ERROR_IS_OK(r.status))
455                 goto done;
456
457         if (num_printers)
458                 *num_printers = r.returned;
459
460         if (!ctr)
461                 goto done;
462
463         switch (level) {
464         case 0:
465                 decode_printer_info_0(mem_ctx, r.buffer, r.returned, 
466                                       &ctr->printers_0);
467                 break;
468         case 1:
469                 decode_printer_info_1(mem_ctx, r.buffer, r.returned, 
470                                       &ctr->printers_1);
471                 break;
472         case 2:
473                 decode_printer_info_2(mem_ctx, r.buffer, r.returned, 
474                                       &ctr->printers_2);
475                 break;
476         case 3:
477                 decode_printer_info_3(mem_ctx, r.buffer, r.returned, 
478                                       &ctr->printers_3);
479                 break;
480         }                       
481         
482  done:
483         prs_mem_free(&qbuf);
484         prs_mem_free(&rbuf);
485
486         return result;  
487 }
488
489 /*********************************************************************************
490  Win32 API - EnumPorts()
491  ********************************************************************************/
492 /** Enumerate printer ports on a print server.
493  *
494  * @param cli              Pointer to client state structure which is open
495  *                         on the SPOOLSS pipe.
496  * @param mem_ctx          Pointer to an initialised talloc context.
497  *
498  * @param offered          Buffer size offered in the request.
499  * @param needed           Number of bytes needed to complete the request.
500  *                         May be NULL.
501  *
502  * @param level            Requested information level.
503  *
504  * @param num_ports        Pointer to number of ports returned.  May be NULL.
505  * @param ctr              Pointer to structure holding port information.
506  *                         May be NULL.
507  */
508
509 WERROR cli_spoolss_enum_ports(struct cli_state *cli, TALLOC_CTX *mem_ctx,
510                               uint32 offered, uint32 *needed,
511                               uint32 level, uint32 *num_ports, PORT_INFO_CTR *ctr)
512 {
513         prs_struct qbuf, rbuf;
514         SPOOL_Q_ENUMPORTS q;
515         SPOOL_R_ENUMPORTS r;
516         RPC_BUFFER buffer;
517         WERROR result = W_ERROR(ERRgeneral);
518         fstring server;
519
520         ZERO_STRUCT(q);
521         ZERO_STRUCT(r);
522
523         slprintf(server, sizeof(fstring)-1, "\\\\%s", cli->desthost);
524         strupper_m(server);
525
526         /* Initialise input parameters */
527         
528         rpcbuf_init(&buffer, offered, mem_ctx);
529         
530         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
531         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
532         
533         make_spoolss_q_enumports(&q, server, level, &buffer, offered);
534         
535         /* Marshall data and send request */
536
537         if (!spoolss_io_q_enumports("", &q, &qbuf, 0) ||
538             !rpc_api_pipe_req(cli, PI_SPOOLSS, SPOOLSS_ENUMPORTS, &qbuf, &rbuf))
539                 goto done;
540
541         /* Unmarshall response */
542
543         if (spoolss_io_r_enumports("", &r, &rbuf, 0)) {
544                 if (needed)
545                         *needed = r.needed;
546         }
547                 
548         result = r.status;
549
550         /* Return output parameters */
551
552         if (!W_ERROR_IS_OK(result))
553                 goto done;
554
555         if (num_ports)
556                 *num_ports = r.returned;
557
558         if (!ctr)
559                 goto done;
560         
561         switch (level) {
562         case 1:
563                 decode_port_info_1(mem_ctx, r.buffer, r.returned, 
564                                    &ctr->port.info_1);
565                 break;
566         case 2:
567                 decode_port_info_2(mem_ctx, r.buffer, r.returned, 
568                                    &ctr->port.info_2);
569                 break;
570         }                       
571
572  done:
573         prs_mem_free(&qbuf);
574         prs_mem_free(&rbuf);
575         
576         return result;  
577 }
578
579 /*********************************************************************************
580  Win32 API - GetPrinter()
581  ********************************************************************************/
582
583 WERROR cli_spoolss_getprinter(struct cli_state *cli, TALLOC_CTX *mem_ctx,
584                               uint32 offered, uint32 *needed,
585                               POLICY_HND *pol, uint32 level, 
586                               PRINTER_INFO_CTR *ctr)
587 {
588         prs_struct qbuf, rbuf;
589         SPOOL_Q_GETPRINTER q;
590         SPOOL_R_GETPRINTER r;
591         RPC_BUFFER buffer;
592         WERROR result = W_ERROR(ERRgeneral);
593
594         ZERO_STRUCT(q);
595         ZERO_STRUCT(r);
596
597         /* Initialise input parameters */
598
599         rpcbuf_init(&buffer, offered, mem_ctx);
600         
601         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
602         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
603
604         make_spoolss_q_getprinter(mem_ctx, &q, pol, level, &buffer, offered);
605         
606         /* Marshall data and send request */
607
608         if (!spoolss_io_q_getprinter("", &q, &qbuf, 0) ||
609             !rpc_api_pipe_req(cli, PI_SPOOLSS, SPOOLSS_GETPRINTER, &qbuf, &rbuf))
610                 goto done;
611
612         /* Unmarshall response */
613
614         if (!spoolss_io_r_getprinter("", &r, &rbuf, 0))
615                 goto done;
616
617         if (needed)
618                 *needed = r.needed;
619         
620         /* Return output parameters */
621
622         result = r.status;
623
624         if (W_ERROR_IS_OK(result)) {
625                 switch (level) {
626                 case 0:
627                         decode_printer_info_0(mem_ctx, r.buffer, 1, &ctr->printers_0);
628                         break;
629                 case 1:
630                         decode_printer_info_1(mem_ctx, r.buffer, 1, &ctr->printers_1);
631                         break;
632                 case 2:
633                         decode_printer_info_2(mem_ctx, r.buffer, 1, &ctr->printers_2);
634                         break;
635                 case 3:
636                         decode_printer_info_3(mem_ctx, r.buffer, 1, &ctr->printers_3);
637                         break;
638                 case 7:
639                         decode_printer_info_7(mem_ctx, r.buffer, 1, &ctr->printers_7);
640                         break;
641                 }                       
642         }
643         
644  done:
645         prs_mem_free(&qbuf);
646         prs_mem_free(&rbuf);
647
648         return result;  
649 }
650
651 /*********************************************************************************
652  Win32 API - SetPrinter()
653  ********************************************************************************/
654 /** Set printer info 
655  *
656  * @param cli              Pointer to client state structure which is open
657  *                         on the SPOOLSS pipe.
658  * @param mem_ctx          Pointer to an initialised talloc context.
659  *
660  * @param pol              Policy handle on printer to set info.
661  * @param level            Information level to set.
662  * @param ctr              Pointer to structure holding printer information.
663  * @param command          Specifies the action performed.  See
664  * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/prntspol_13ua.asp 
665  * for details.
666  *
667  */
668
669 WERROR cli_spoolss_setprinter(struct cli_state *cli, TALLOC_CTX *mem_ctx,
670                               POLICY_HND *pol, uint32 level, 
671                               PRINTER_INFO_CTR *ctr, uint32 command)
672 {
673         prs_struct qbuf, rbuf;
674         SPOOL_Q_SETPRINTER q;
675         SPOOL_R_SETPRINTER r;
676         WERROR result = W_ERROR(ERRgeneral);
677
678         ZERO_STRUCT(q);
679         ZERO_STRUCT(r);
680
681         /* Initialise input parameters */
682
683         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
684         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
685                 
686         if (!make_spoolss_q_setprinter(mem_ctx, &q, pol, level, ctr, command))
687                 goto done;
688
689         /* Marshall data and send request */
690
691         if (!spoolss_io_q_setprinter("", &q, &qbuf, 0) ||
692             !rpc_api_pipe_req(cli, PI_SPOOLSS, SPOOLSS_SETPRINTER, &qbuf, &rbuf))
693                 goto done;
694
695         /* Unmarshall response */
696
697         if (!spoolss_io_r_setprinter("", &r, &rbuf, 0))
698                 goto done;
699         
700         result = r.status;
701
702 done:
703         prs_mem_free(&qbuf);
704         prs_mem_free(&rbuf);
705
706         return result;  
707 }
708
709 /*********************************************************************************
710  Win32 API - GetPrinterDriver()
711  ********************************************************************************/
712 /** Get installed printer drivers for a given printer
713  *
714  * @param cli              Pointer to client state structure which is open
715  * on the SPOOLSS pipe.
716  *
717  * @param mem_ctx          Pointer to an initialised talloc context.
718  *
719  * @param offered          Buffer size offered in the request.
720  * @param needed           Number of bytes needed to complete the request.
721  *                         may be NULL.
722  *
723  * @param pol              Pointer to an open policy handle for the printer
724  *                         opened with cli_spoolss_open_printer_ex().
725  * @param level            Requested information level.
726  * @param env              The print environment or archictecture.  This is
727  *                         "Windows NT x86" for NT4.
728  * @param ctr              Returned printer driver information.
729  */
730
731 WERROR cli_spoolss_getprinterdriver(struct cli_state *cli, 
732                                     TALLOC_CTX *mem_ctx, 
733                                     uint32 offered, uint32 *needed,
734                                     POLICY_HND *pol, uint32 level, 
735                                     const char *env, int version, PRINTER_DRIVER_CTR *ctr)
736 {
737         prs_struct qbuf, rbuf;
738         SPOOL_Q_GETPRINTERDRIVER2 q;
739         SPOOL_R_GETPRINTERDRIVER2 r;
740         RPC_BUFFER buffer;
741         WERROR result = W_ERROR(ERRgeneral);
742         fstring server;
743
744         ZERO_STRUCT(q);
745         ZERO_STRUCT(r);
746
747         fstrcpy(server, cli->desthost);
748         strupper_m(server);
749
750         /* Initialise input parameters */
751
752         rpcbuf_init(&buffer, offered, mem_ctx);
753
754         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
755         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
756
757         make_spoolss_q_getprinterdriver2(&q, pol, env, level, version, 2,
758                                          &buffer, offered);
759
760         /* Marshall data and send request */
761
762         if (!spoolss_io_q_getprinterdriver2 ("", &q, &qbuf, 0) ||
763             !rpc_api_pipe_req (cli, PI_SPOOLSS, SPOOLSS_GETPRINTERDRIVER2, &qbuf, &rbuf)) 
764                 goto done;
765
766         /* Unmarshall response */
767
768         if (spoolss_io_r_getprinterdriver2 ("", &r, &rbuf, 0)) {
769                 if (needed)
770                         *needed = r.needed;
771         }
772
773         result = r.status;
774
775         /* Return output parameters */
776
777         if (!W_ERROR_IS_OK(result))
778                 goto done;
779
780         if (!ctr)
781                 goto done;
782
783         switch (level) {
784         case 1:
785                 decode_printer_driver_1(mem_ctx, r.buffer, 1, &ctr->info1);
786                 break;
787         case 2:
788                 decode_printer_driver_2(mem_ctx, r.buffer, 1, &ctr->info2);
789                 break;
790         case 3:
791                 decode_printer_driver_3(mem_ctx, r.buffer, 1, &ctr->info3);
792                 break;
793         default:
794                 DEBUG(10, ("cli_spoolss_getprinterdriver: unknown info level %d", level));
795                 return WERR_UNKNOWN_LEVEL;
796         }
797
798  done:
799         prs_mem_free(&qbuf);
800         prs_mem_free(&rbuf);
801                 
802         return result;  
803 }
804
805 /*********************************************************************************
806  Win32 API - EnumPrinterDrivers()
807  ********************************************************************************/
808 /**********************************************************************
809  * Get installed printer drivers for a given printer
810  */
811 WERROR cli_spoolss_enumprinterdrivers (struct cli_state *cli, 
812                                        TALLOC_CTX *mem_ctx,
813                                        uint32 offered, uint32 *needed,
814                                        uint32 level, const char *env,
815                                        uint32 *num_drivers,
816                                        PRINTER_DRIVER_CTR *ctr)
817 {
818         prs_struct qbuf, rbuf;
819         SPOOL_Q_ENUMPRINTERDRIVERS q;
820         SPOOL_R_ENUMPRINTERDRIVERS r;
821         RPC_BUFFER buffer;
822         WERROR result = W_ERROR(ERRgeneral);
823         fstring server;
824
825         ZERO_STRUCT(q);
826         ZERO_STRUCT(r);
827
828         slprintf(server, sizeof(fstring)-1, "\\\\%s", cli->desthost);
829         strupper_m(server);
830
831         /* Initialise input parameters */
832
833         rpcbuf_init(&buffer, offered, mem_ctx);
834
835         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
836         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
837
838         /* Write the request */
839
840         make_spoolss_q_enumprinterdrivers(&q, server, env, level, &buffer, 
841                                           offered);
842         
843         /* Marshall data and send request */
844         
845         if (!spoolss_io_q_enumprinterdrivers ("", &q, &qbuf, 0) ||
846             !rpc_api_pipe_req (cli, PI_SPOOLSS, SPOOLSS_ENUMPRINTERDRIVERS, &qbuf, &rbuf))
847                 goto done;
848
849         /* Unmarshall response */
850
851         if (!spoolss_io_r_enumprinterdrivers ("", &r, &rbuf, 0))
852                 goto done;
853
854         if (needed)
855                 *needed = r.needed;
856
857         if (num_drivers)
858                 *num_drivers = r.returned;
859
860         result = r.status;
861
862         /* Return output parameters */
863
864         if (W_ERROR_IS_OK(result) && (r.returned != 0)) {
865                 *num_drivers = r.returned;
866
867                 switch (level) {
868                 case 1:
869                         decode_printer_driver_1(mem_ctx, r.buffer, r.returned, &ctr->info1);
870                         break;
871                 case 2:
872                         decode_printer_driver_2(mem_ctx, r.buffer, r.returned, &ctr->info2);
873                         break;
874                 case 3:
875                         decode_printer_driver_3(mem_ctx, r.buffer, r.returned, &ctr->info3);
876                         break;
877                 default:
878                         DEBUG(10, ("cli_spoolss_enumprinterdrivers: unknown info level %d\n",
879                                    level));
880                         return WERR_UNKNOWN_LEVEL;
881                 }
882         }
883
884  done:
885         prs_mem_free(&qbuf);
886         prs_mem_free(&rbuf);
887                 
888         return result;
889 }
890
891
892 /*********************************************************************************
893  Win32 API - GetPrinterDriverDirectory()
894  ********************************************************************************/
895 /**********************************************************************
896  * Get installed printer drivers for a given printer
897  */
898 WERROR cli_spoolss_getprinterdriverdir (struct cli_state *cli, 
899                                         TALLOC_CTX *mem_ctx,
900                                         uint32 offered, uint32 *needed,
901                                         uint32 level, char *env,
902                                         DRIVER_DIRECTORY_CTR *ctr)
903 {
904         prs_struct                      qbuf, rbuf;
905         SPOOL_Q_GETPRINTERDRIVERDIR     q;
906         SPOOL_R_GETPRINTERDRIVERDIR     r;
907         RPC_BUFFER                      buffer;
908         WERROR result = W_ERROR(ERRgeneral);
909         fstring                         server;
910
911         ZERO_STRUCT(q);
912         ZERO_STRUCT(r);
913
914         slprintf(server, sizeof(fstring)-1, "\\\\%s", cli->desthost);
915         strupper_m(server);
916
917         /* Initialise input parameters */
918
919         rpcbuf_init(&buffer, offered, mem_ctx);
920
921         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
922         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
923
924         /* Write the request */
925
926         make_spoolss_q_getprinterdriverdir(&q, server, env, level, &buffer, 
927                                            offered);
928
929         /* Marshall data and send request */
930
931         if (!spoolss_io_q_getprinterdriverdir ("", &q, &qbuf, 0) ||
932             !rpc_api_pipe_req (cli, PI_SPOOLSS, SPOOLSS_GETPRINTERDRIVERDIRECTORY,
933                                &qbuf, &rbuf)) 
934                 goto done;
935
936         /* Unmarshall response */
937
938         if (spoolss_io_r_getprinterdriverdir ("", &r, &rbuf, 0)) {
939                 if (needed)
940                         *needed = r.needed;
941         }
942                 
943         /* Return output parameters */
944
945         result = r.status;
946
947         if (W_ERROR_IS_OK(result)) {
948                 switch (level) {
949                 case 1:
950                         decode_printerdriverdir_1(mem_ctx, r.buffer, 1, 
951                                                   &ctr->info1);
952                         break;
953                 }                       
954         }
955                 
956         done:
957                 prs_mem_free(&qbuf);
958                 prs_mem_free(&rbuf);
959
960         return result;
961 }
962
963 /*********************************************************************************
964  Win32 API - AddPrinterDriver()
965  ********************************************************************************/
966 /**********************************************************************
967  * Install a printer driver
968  */
969 WERROR cli_spoolss_addprinterdriver (struct cli_state *cli, 
970                                      TALLOC_CTX *mem_ctx, uint32 level,
971                                      PRINTER_DRIVER_CTR *ctr)
972 {
973         prs_struct                      qbuf, rbuf;
974         SPOOL_Q_ADDPRINTERDRIVER        q;
975         SPOOL_R_ADDPRINTERDRIVER        r;
976         WERROR result = W_ERROR(ERRgeneral);
977         fstring                         server;
978
979         ZERO_STRUCT(q);
980         ZERO_STRUCT(r);
981         
982         slprintf(server, sizeof(fstring)-1, "\\\\%s", cli->desthost);
983         strupper_m(server);
984
985         /* Initialise input parameters */
986
987         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
988         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
989
990         /* Write the request */
991
992         make_spoolss_q_addprinterdriver (mem_ctx, &q, server, level, ctr);
993
994         /* Marshall data and send request */
995
996         if (!spoolss_io_q_addprinterdriver ("", &q, &qbuf, 0) ||
997             !rpc_api_pipe_req (cli, PI_SPOOLSS, SPOOLSS_ADDPRINTERDRIVER, &qbuf, &rbuf))
998                 goto done;
999
1000         /* Unmarshall response */
1001
1002         if (!spoolss_io_r_addprinterdriver ("", &r, &rbuf, 0))
1003                 goto done;
1004                 
1005         /* Return output parameters */
1006
1007         result = r.status;
1008
1009 done:
1010         prs_mem_free(&qbuf);
1011         prs_mem_free(&rbuf);
1012         
1013         return result;  
1014 }
1015
1016 /*********************************************************************************
1017  Win32 API - AddPrinter()
1018  ********************************************************************************/
1019 /**********************************************************************
1020  * Install a printer
1021  */
1022 WERROR cli_spoolss_addprinterex (struct cli_state *cli, TALLOC_CTX *mem_ctx,
1023                                  uint32 level, PRINTER_INFO_CTR*ctr)
1024 {
1025         prs_struct                      qbuf, rbuf;
1026         SPOOL_Q_ADDPRINTEREX            q;
1027         SPOOL_R_ADDPRINTEREX            r;
1028         WERROR result = W_ERROR(ERRgeneral);
1029         fstring                         server,
1030                                         client,
1031                                         user;
1032
1033         ZERO_STRUCT(q);
1034         ZERO_STRUCT(r);
1035
1036         slprintf(client, sizeof(fstring)-1, "\\\\%s", cli->desthost);
1037         strupper_m(client);
1038         slprintf(server, sizeof(fstring)-1, "\\\\%s", cli->desthost);
1039         strupper_m(server);
1040         fstrcpy  (user, cli->user_name);
1041
1042         /* Initialise input parameters */
1043
1044         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1045         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1046
1047         /* Write the request */
1048
1049         make_spoolss_q_addprinterex (mem_ctx, &q, server, client, user,
1050                                      level, ctr);
1051
1052         /* Marshall data and send request */
1053
1054         if (!spoolss_io_q_addprinterex ("", &q, &qbuf, 0) ||
1055             !rpc_api_pipe_req (cli, PI_SPOOLSS, SPOOLSS_ADDPRINTEREX, &qbuf, &rbuf)) 
1056                 goto done;
1057                 
1058         /* Unmarshall response */
1059
1060         if (!spoolss_io_r_addprinterex ("", &r, &rbuf, 0))
1061                 goto done;
1062                 
1063         /* Return output parameters */
1064
1065         result = r.status;
1066
1067  done:
1068         prs_mem_free(&qbuf);
1069         prs_mem_free(&rbuf);
1070
1071         return result;  
1072 }
1073
1074 /**********************************************************************
1075  * Delete a Printer Driver from the server (DOES remove 
1076  * the driver files)
1077  */
1078 WERROR cli_spoolss_deleteprinterdriverex(struct cli_state *cli, 
1079                                          TALLOC_CTX *mem_ctx, const char *arch,
1080                                          const char *driver, int version)
1081 {
1082         prs_struct    qbuf, rbuf;
1083         SPOOL_Q_DELETEPRINTERDRIVEREX q;
1084         SPOOL_R_DELETEPRINTERDRIVEREX r;
1085         WERROR result = W_ERROR(ERRgeneral);
1086         fstring    server;
1087
1088         ZERO_STRUCT(q);
1089         ZERO_STRUCT(r);
1090
1091
1092         /* Initialise input parameters */
1093         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1094         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1095
1096         slprintf(server, sizeof(fstring)-1, "\\\\%s", cli->desthost);
1097         strupper_m(server);
1098
1099         /* Write the request */
1100         make_spoolss_q_deleteprinterdriverex(mem_ctx, &q, server, arch, driver, version);
1101
1102         /* Marshall data and send request */
1103
1104         if (!spoolss_io_q_deleteprinterdriverex ("", &q, &qbuf, 0) 
1105                 || !rpc_api_pipe_req (cli, PI_SPOOLSS, SPOOLSS_DELETEPRINTERDRIVEREX , &qbuf, &rbuf)) 
1106         {
1107                 goto done;
1108         }
1109
1110         /* Unmarshall response */
1111
1112         if (!spoolss_io_r_deleteprinterdriverex ("", &r, &rbuf, 0))
1113                 goto done;
1114   
1115         /* Return output parameters */
1116
1117         result = r.status;
1118
1119 done:
1120         prs_mem_free(&qbuf);
1121         prs_mem_free(&rbuf);
1122
1123         return result; 
1124 }
1125
1126 /*********************************************************************************
1127  Win32 API - DeltePrinterDriver()
1128  ********************************************************************************/
1129 /**********************************************************************
1130  * Delete a Printer Driver from the server (does not remove 
1131  * the driver files
1132  */
1133 WERROR cli_spoolss_deleteprinterdriver (struct cli_state *cli, 
1134                                         TALLOC_CTX *mem_ctx, const char *arch,
1135                                         const char *driver)
1136 {
1137         prs_struct                      qbuf, rbuf;
1138         SPOOL_Q_DELETEPRINTERDRIVER     q;
1139         SPOOL_R_DELETEPRINTERDRIVER     r;
1140         WERROR result = W_ERROR(ERRgeneral);
1141         fstring                         server;
1142
1143         ZERO_STRUCT(q);
1144         ZERO_STRUCT(r);
1145
1146
1147         /* Initialise input parameters */
1148         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1149         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1150
1151         slprintf(server, sizeof(fstring)-1, "\\\\%s", cli->desthost);
1152         strupper_m(server);
1153
1154         /* Write the request */
1155
1156         make_spoolss_q_deleteprinterdriver(mem_ctx, &q, server, arch, driver);
1157
1158         /* Marshall data and send request */
1159
1160         if (!spoolss_io_q_deleteprinterdriver ("", &q, &qbuf, 0) ||
1161             !rpc_api_pipe_req (cli, PI_SPOOLSS, SPOOLSS_DELETEPRINTERDRIVER , &qbuf, &rbuf))
1162                 goto done;
1163
1164         /* Unmarshall response */
1165
1166         if (!spoolss_io_r_deleteprinterdriver ("", &r, &rbuf, 0))
1167                 goto done;
1168                 
1169         /* Return output parameters */
1170
1171         result = r.status;
1172
1173  done:
1174         prs_mem_free(&qbuf);
1175         prs_mem_free(&rbuf);
1176
1177         return result;  
1178 }
1179
1180 /*********************************************************************************
1181  Win32 API - GetPrinterProcessorDirectory()
1182  ********************************************************************************/
1183
1184 WERROR cli_spoolss_getprintprocessordirectory(struct cli_state *cli,
1185                                               TALLOC_CTX *mem_ctx,
1186                                               uint32 offered, uint32 *needed,
1187                                               char *name, char *environment,
1188                                               fstring procdir)
1189 {
1190         prs_struct qbuf, rbuf;
1191         SPOOL_Q_GETPRINTPROCESSORDIRECTORY q;
1192         SPOOL_R_GETPRINTPROCESSORDIRECTORY r;
1193         int level = 1;
1194         WERROR result = W_ERROR(ERRgeneral);
1195         RPC_BUFFER buffer;
1196
1197         ZERO_STRUCT(q);
1198         ZERO_STRUCT(r);
1199
1200         /* Initialise parse structures */
1201
1202         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1203         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1204
1205         /* Initialise input parameters */
1206
1207         rpcbuf_init(&buffer, offered, mem_ctx);
1208
1209         make_spoolss_q_getprintprocessordirectory(
1210                 &q, name, environment, level, &buffer, offered);
1211
1212         /* Marshall data and send request */
1213
1214         if (!spoolss_io_q_getprintprocessordirectory("", &q, &qbuf, 0) ||
1215             !rpc_api_pipe_req(cli, PI_SPOOLSS, SPOOLSS_GETPRINTPROCESSORDIRECTORY,
1216                               &qbuf, &rbuf))
1217                 goto done;
1218                 
1219         /* Unmarshall response */
1220                 
1221         if (!spoolss_io_r_getprintprocessordirectory("", &r, &rbuf, 0))
1222                 goto done;
1223
1224         /* Return output parameters */
1225                 
1226         result = r.status;
1227
1228         if (needed)
1229                 *needed = r.needed;
1230
1231         if (W_ERROR_IS_OK(result))
1232                 fstrcpy(procdir, "Not implemented!");
1233
1234  done:
1235         prs_mem_free(&qbuf);
1236         prs_mem_free(&rbuf);
1237
1238         return result;
1239 }
1240
1241 /** Add a form to a printer.
1242  *
1243  * @param cli              Pointer to client state structure which is open
1244  *                         on the SPOOLSS pipe.
1245  * @param mem_ctx          Pointer to an initialised talloc context.
1246  *
1247  * @param handle           Policy handle opened with cli_spoolss_open_printer_ex
1248  *                         or cli_spoolss_addprinterex.
1249  * @param level            Form info level to add - should always be 1.
1250  * @param form             A pointer to the form to be added.
1251  *
1252  */
1253
1254 WERROR cli_spoolss_addform(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1255                            POLICY_HND *handle, uint32 level, FORM *form)
1256 {
1257         prs_struct qbuf, rbuf;
1258         SPOOL_Q_ADDFORM q;
1259         SPOOL_R_ADDFORM r;
1260         WERROR result = W_ERROR(ERRgeneral);
1261
1262         ZERO_STRUCT(q);
1263         ZERO_STRUCT(r);
1264
1265         /* Initialise parse structures */
1266
1267         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1268         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1269
1270         /* Initialise input parameters */
1271
1272         make_spoolss_q_addform(&q, handle, level, form);
1273         
1274         /* Marshall data and send request */
1275
1276         if (!spoolss_io_q_addform("", &q, &qbuf, 0) ||
1277             !rpc_api_pipe_req(cli, PI_SPOOLSS, SPOOLSS_ADDFORM, &qbuf, &rbuf))
1278                 goto done;
1279
1280         /* Unmarshall response */
1281
1282         if (!spoolss_io_r_addform("", &r, &rbuf, 0))
1283                 goto done;
1284
1285         /* Return output parameters */
1286
1287         result = r.status;
1288
1289  done:
1290         prs_mem_free(&qbuf);
1291         prs_mem_free(&rbuf);
1292
1293         return result;
1294 }
1295
1296 /** Set a form on a printer.
1297  *
1298  * @param cli              Pointer to client state structure which is open
1299  *                         on the SPOOLSS pipe.
1300  * @param mem_ctx          Pointer to an initialised talloc context.
1301  *
1302  * @param handle           Policy handle opened with cli_spoolss_open_printer_ex 
1303  *                         or cli_spoolss_addprinterex.
1304  * @param level            Form info level to set - should always be 1.
1305  * @param form             A pointer to the form to be set.
1306  *
1307  */
1308
1309 WERROR cli_spoolss_setform(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1310                            POLICY_HND *handle, uint32 level, 
1311                            const char *form_name, FORM *form)
1312 {
1313         prs_struct qbuf, rbuf;
1314         SPOOL_Q_SETFORM q;
1315         SPOOL_R_SETFORM r;
1316         WERROR result = W_ERROR(ERRgeneral);
1317
1318         ZERO_STRUCT(q);
1319         ZERO_STRUCT(r);
1320
1321         /* Initialise parse structures */
1322
1323         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1324         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1325
1326         /* Initialise input parameters */
1327
1328         make_spoolss_q_setform(&q, handle, level, form_name, form);
1329         
1330         /* Marshall data and send request */
1331
1332         if (!spoolss_io_q_setform("", &q, &qbuf, 0) ||
1333             !rpc_api_pipe_req(cli, PI_SPOOLSS, SPOOLSS_SETFORM, &qbuf, &rbuf))
1334                 goto done;
1335
1336         /* Unmarshall response */
1337
1338         if (!spoolss_io_r_setform("", &r, &rbuf, 0))
1339                 goto done;
1340
1341         /* Return output parameters */
1342
1343         result = r.status;
1344
1345         if (!W_ERROR_IS_OK(result))
1346                 goto done;
1347
1348
1349
1350  done:
1351         prs_mem_free(&qbuf);
1352         prs_mem_free(&rbuf);
1353
1354         return result;
1355 }
1356
1357 /** Get a form on a printer.
1358  *
1359  * @param cli              Pointer to client state structure which is open
1360  *                         on the SPOOLSS pipe.
1361  * @param mem_ctx          Pointer to an initialised talloc context.
1362  *
1363  * @param handle           Policy handle opened with cli_spoolss_open_printer_ex 
1364  *                         or cli_spoolss_addprinterex.
1365  * @param formname         Name of the form to get
1366  * @param level            Form info level to get - should always be 1.
1367  *
1368  */
1369
1370 WERROR cli_spoolss_getform(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1371                            uint32 offered, uint32 *needed,
1372                            POLICY_HND *handle, const char *formname, 
1373                            uint32 level, FORM_1 *form)
1374 {
1375         prs_struct qbuf, rbuf;
1376         SPOOL_Q_GETFORM q;
1377         SPOOL_R_GETFORM r;
1378         WERROR result = W_ERROR(ERRgeneral);
1379         RPC_BUFFER buffer;
1380
1381         ZERO_STRUCT(q);
1382         ZERO_STRUCT(r);
1383
1384         /* Initialise parse structures */
1385
1386         rpcbuf_init(&buffer, offered, mem_ctx);
1387
1388         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1389         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1390
1391         /* Initialise input parameters */
1392
1393         make_spoolss_q_getform(&q, handle, formname, level, &buffer, offered);
1394         
1395         /* Marshall data and send request */
1396
1397         if (!spoolss_io_q_getform("", &q, &qbuf, 0) ||
1398             !rpc_api_pipe_req(cli, PI_SPOOLSS, SPOOLSS_GETFORM, &qbuf, &rbuf))
1399                 goto done;
1400
1401         /* Unmarshall response */
1402
1403         if (!spoolss_io_r_getform("", &r, &rbuf, 0))
1404                 goto done;
1405
1406         /* Return output parameters */
1407
1408         result = r.status;
1409
1410         if (needed)
1411                 *needed = r.needed;
1412
1413         if (W_ERROR_IS_OK(result)) {
1414                 switch(level) {
1415                 case 1:
1416                         smb_io_form_1("", r.buffer, form, 0);
1417                         break;
1418                 default:
1419                         DEBUG(10, ("cli_spoolss_getform: unknown info level %d", level));
1420                         return WERR_UNKNOWN_LEVEL;
1421                 }
1422         }
1423
1424  done:
1425         prs_mem_free(&qbuf);
1426         prs_mem_free(&rbuf);
1427
1428         return result;
1429 }
1430
1431 /** Delete a form on a printer.
1432  *
1433  * @param cli              Pointer to client state structure which is open
1434  *                         on the SPOOLSS pipe.
1435  * @param mem_ctx          Pointer to an initialised talloc context.
1436  *
1437  * @param handle           Policy handle opened with cli_spoolss_open_printer_ex 
1438  *                         or cli_spoolss_addprinterex.
1439  * @param form             The name of the form to delete.
1440  *
1441  */
1442
1443 WERROR cli_spoolss_deleteform(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1444                               POLICY_HND *handle, const char *form_name)
1445 {
1446         prs_struct qbuf, rbuf;
1447         SPOOL_Q_DELETEFORM q;
1448         SPOOL_R_DELETEFORM r;
1449         WERROR result = W_ERROR(ERRgeneral);
1450
1451         ZERO_STRUCT(q);
1452         ZERO_STRUCT(r);
1453
1454         /* Initialise parse structures */
1455
1456         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1457         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1458
1459         /* Initialise input parameters */
1460
1461         make_spoolss_q_deleteform(&q, handle, form_name);
1462         
1463         /* Marshall data and send request */
1464
1465         if (!spoolss_io_q_deleteform("", &q, &qbuf, 0) ||
1466             !rpc_api_pipe_req(cli, PI_SPOOLSS, SPOOLSS_DELETEFORM, &qbuf, &rbuf))
1467                 goto done;
1468
1469         /* Unmarshall response */
1470
1471         if (!spoolss_io_r_deleteform("", &r, &rbuf, 0))
1472                 goto done;
1473
1474         /* Return output parameters */
1475
1476         result = r.status;
1477
1478  done:
1479         prs_mem_free(&qbuf);
1480         prs_mem_free(&rbuf);
1481
1482         return result;
1483 }
1484
1485 static void decode_forms_1(TALLOC_CTX *mem_ctx, RPC_BUFFER *buffer, 
1486                            uint32 num_forms, FORM_1 **forms)
1487 {
1488         int i;
1489
1490         *forms = TALLOC_ARRAY(mem_ctx, FORM_1, num_forms);
1491         prs_set_offset(&buffer->prs,0);
1492
1493         for (i = 0; i < num_forms; i++)
1494                 smb_io_form_1("", buffer, &((*forms)[i]), 0);
1495 }
1496
1497 /** Enumerate forms
1498  *
1499  * @param cli              Pointer to client state structure which is open
1500  *                         on the SPOOLSS pipe.
1501  * @param mem_ctx          Pointer to an initialised talloc context.
1502  *
1503  * @param offered          Buffer size offered in the request.
1504  * @param needed           Number of bytes needed to complete the request.
1505  *                         may be NULL.
1506  *                         or cli_spoolss_addprinterex.
1507  * @param level            Form info level to get - should always be 1.
1508  * @param handle           Open policy handle
1509  *
1510  */
1511
1512 WERROR cli_spoolss_enumforms(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1513                              uint32 offered, uint32 *needed,
1514                              POLICY_HND *handle, int level, uint32 *num_forms,
1515                              FORM_1 **forms)
1516 {
1517         prs_struct qbuf, rbuf;
1518         SPOOL_Q_ENUMFORMS q;
1519         SPOOL_R_ENUMFORMS r;
1520         WERROR result = W_ERROR(ERRgeneral);
1521         RPC_BUFFER buffer;
1522
1523         ZERO_STRUCT(q);
1524         ZERO_STRUCT(r);
1525
1526         /* Initialise parse structures */
1527
1528         rpcbuf_init(&buffer, offered, mem_ctx);
1529
1530         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1531         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1532
1533         /* Initialise input parameters */
1534
1535         make_spoolss_q_enumforms(&q, handle, level, &buffer, offered);
1536
1537         /* Marshall data and send request */
1538
1539         if (!spoolss_io_q_enumforms("", &q, &qbuf, 0) ||
1540             !rpc_api_pipe_req(cli, PI_SPOOLSS, SPOOLSS_ENUMFORMS, &qbuf, &rbuf))
1541                 goto done;
1542
1543         /* Unmarshall response */
1544
1545         if (!spoolss_io_r_enumforms("", &r, &rbuf, 0))
1546                 goto done;
1547
1548         /* Return output parameters */
1549
1550         result = r.status;
1551
1552         if (needed)
1553                 *needed = r.needed;
1554
1555         if (num_forms)
1556                 *num_forms = r.numofforms;
1557
1558         decode_forms_1(mem_ctx, r.buffer, *num_forms, forms);
1559
1560  done:
1561         prs_mem_free(&qbuf);
1562         prs_mem_free(&rbuf);
1563
1564         return result;
1565 }
1566
1567 static void decode_jobs_1(TALLOC_CTX *mem_ctx, RPC_BUFFER *buffer, 
1568                           uint32 num_jobs, JOB_INFO_1 **jobs)
1569 {
1570         uint32 i;
1571
1572         *jobs = TALLOC_ARRAY(mem_ctx, JOB_INFO_1, num_jobs);
1573         prs_set_offset(&buffer->prs,0);
1574
1575         for (i = 0; i < num_jobs; i++) 
1576                 smb_io_job_info_1("", buffer, &((*jobs)[i]), 0);
1577 }
1578
1579 static void decode_jobs_2(TALLOC_CTX *mem_ctx, RPC_BUFFER *buffer, 
1580                           uint32 num_jobs, JOB_INFO_2 **jobs)
1581 {
1582         uint32 i;
1583
1584         *jobs = TALLOC_ARRAY(mem_ctx, JOB_INFO_2, num_jobs);
1585         prs_set_offset(&buffer->prs,0);
1586
1587         for (i = 0; i < num_jobs; i++) 
1588                 smb_io_job_info_2("", buffer, &((*jobs)[i]), 0);
1589 }
1590
1591 /* Enumerate jobs */
1592
1593 WERROR cli_spoolss_enumjobs(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1594                             uint32 offered, uint32 *needed,
1595                             POLICY_HND *hnd, uint32 level, uint32 firstjob, 
1596                             uint32 num_jobs, uint32 *returned, JOB_INFO_CTR *ctr)
1597 {
1598         prs_struct qbuf, rbuf;
1599         SPOOL_Q_ENUMJOBS q;
1600         SPOOL_R_ENUMJOBS r;
1601         WERROR result = W_ERROR(ERRgeneral);
1602         RPC_BUFFER buffer;
1603
1604         ZERO_STRUCT(q);
1605         ZERO_STRUCT(r);
1606
1607         /* Initialise parse structures */
1608
1609         rpcbuf_init(&buffer, offered, mem_ctx);
1610
1611         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1612         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1613
1614         /* Initialise input parameters */
1615
1616         make_spoolss_q_enumjobs(&q, hnd, firstjob, num_jobs, level, &buffer, 
1617                                 offered);
1618
1619         /* Marshall data and send request */
1620
1621         if (!spoolss_io_q_enumjobs("", &q, &qbuf, 0) ||
1622             !rpc_api_pipe_req(cli, PI_SPOOLSS, SPOOLSS_ENUMJOBS, &qbuf, &rbuf))
1623                 goto done;
1624
1625         /* Unmarshall response */
1626
1627         if (!spoolss_io_r_enumjobs("", &r, &rbuf, 0))
1628                 goto done;
1629
1630         /* Return output parameters */
1631
1632         result = r.status;
1633
1634         if (needed)
1635                 *needed = r.needed;
1636
1637         if (!W_ERROR_IS_OK(r.status))
1638                 goto done;
1639
1640         *returned = r.returned;
1641
1642         switch(level) {
1643         case 1:
1644                 decode_jobs_1(mem_ctx, r.buffer, r.returned,
1645                               &ctr->job.job_info_1);
1646                 break;
1647         case 2:
1648                 decode_jobs_2(mem_ctx, r.buffer, r.returned,
1649                               &ctr->job.job_info_2);
1650                 break;
1651         default:
1652                 DEBUG(3, ("unsupported info level %d", level));
1653                 break;
1654         }
1655
1656  done:
1657         prs_mem_free(&qbuf);
1658         prs_mem_free(&rbuf);
1659
1660         return result;
1661 }
1662
1663 /* Set job */
1664
1665 WERROR cli_spoolss_setjob(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1666                           POLICY_HND *hnd, uint32 jobid, uint32 level, 
1667                           uint32 command)
1668 {
1669         prs_struct qbuf, rbuf;
1670         SPOOL_Q_SETJOB q;
1671         SPOOL_R_SETJOB r;
1672         WERROR result = W_ERROR(ERRgeneral);
1673
1674         ZERO_STRUCT(q);
1675         ZERO_STRUCT(r);
1676
1677         /* Initialise parse structures */
1678
1679         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1680         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1681
1682         /* Initialise input parameters */
1683
1684         make_spoolss_q_setjob(&q, hnd, jobid, level, command);
1685
1686         /* Marshall data and send request */
1687
1688         if (!spoolss_io_q_setjob("", &q, &qbuf, 0) ||
1689             !rpc_api_pipe_req(cli, PI_SPOOLSS, SPOOLSS_SETJOB, &qbuf, &rbuf))
1690                 goto done;
1691
1692         /* Unmarshall response */
1693
1694         if (!spoolss_io_r_setjob("", &r, &rbuf, 0))
1695                 goto done;
1696
1697         /* Return output parameters */
1698
1699         result = r.status;
1700
1701  done:
1702         prs_mem_free(&qbuf);
1703         prs_mem_free(&rbuf);
1704
1705         return result;
1706 }
1707
1708 /* Get job */
1709
1710 WERROR cli_spoolss_getjob(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1711                           uint32 offered, uint32 *needed,
1712                           POLICY_HND *hnd, uint32 jobid, uint32 level,
1713                           JOB_INFO_CTR *ctr)
1714 {
1715         prs_struct qbuf, rbuf;
1716         SPOOL_Q_GETJOB q;
1717         SPOOL_R_GETJOB r;
1718         WERROR result = W_ERROR(ERRgeneral);
1719         RPC_BUFFER buffer;
1720
1721         ZERO_STRUCT(q);
1722         ZERO_STRUCT(r);
1723
1724         /* Initialise parse structures */
1725
1726         rpcbuf_init(&buffer, offered, mem_ctx);
1727
1728         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1729         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1730
1731         /* Initialise input parameters */
1732
1733         make_spoolss_q_getjob(&q, hnd, jobid, level, &buffer, offered);
1734
1735         /* Marshall data and send request */
1736
1737         if (!spoolss_io_q_getjob("", &q, &qbuf, 0) ||
1738             !rpc_api_pipe_req(cli, PI_SPOOLSS, SPOOLSS_GETJOB, &qbuf, &rbuf))
1739                 goto done;
1740
1741         /* Unmarshall response */
1742
1743         if (!spoolss_io_r_getjob("", &r, &rbuf, 0))
1744                 goto done;
1745
1746         /* Return output parameters */
1747
1748         result = r.status;
1749
1750         if (needed)
1751                 *needed = r.needed;
1752
1753         if (!W_ERROR_IS_OK(r.status))
1754                 goto done;
1755
1756         switch(level) {
1757         case 1:
1758                 decode_jobs_1(mem_ctx, r.buffer, 1, &ctr->job.job_info_1);
1759                 break;
1760         case 2:
1761                 decode_jobs_2(mem_ctx, r.buffer, 1, &ctr->job.job_info_2);
1762                 break;
1763         default:
1764                 DEBUG(3, ("unsupported info level %d", level));
1765                 break;
1766         }
1767
1768  done:
1769         prs_mem_free(&qbuf);
1770         prs_mem_free(&rbuf);
1771
1772         return result;
1773 }
1774
1775 /* Startpageprinter.  Sent to notify the spooler when a page is about to be
1776    sent to a printer. */ 
1777
1778 WERROR cli_spoolss_startpageprinter(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1779                                     POLICY_HND *hnd)
1780 {
1781         prs_struct qbuf, rbuf;
1782         SPOOL_Q_STARTPAGEPRINTER q;
1783         SPOOL_R_STARTPAGEPRINTER r;
1784         WERROR result = W_ERROR(ERRgeneral);
1785
1786         ZERO_STRUCT(q);
1787         ZERO_STRUCT(r);
1788
1789         /* Initialise parse structures */
1790
1791         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1792         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1793
1794         /* Initialise input parameters */
1795
1796         make_spoolss_q_startpageprinter(&q, hnd);
1797
1798         /* Marshall data and send request */
1799
1800         if (!spoolss_io_q_startpageprinter("", &q, &qbuf, 0) ||
1801             !rpc_api_pipe_req(cli, PI_SPOOLSS, SPOOLSS_STARTPAGEPRINTER, &qbuf, &rbuf))
1802                 goto done;
1803
1804         /* Unmarshall response */
1805
1806         if (!spoolss_io_r_startpageprinter("", &r, &rbuf, 0))
1807                 goto done;
1808
1809         /* Return output parameters */
1810
1811         result = r.status;
1812
1813  done:
1814         prs_mem_free(&qbuf);
1815         prs_mem_free(&rbuf);
1816
1817         return result;
1818 }
1819
1820 /* Endpageprinter.  Sent to notify the spooler when a page has finished
1821    being sent to a printer. */
1822
1823 WERROR cli_spoolss_endpageprinter(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1824                                   POLICY_HND *hnd)
1825 {
1826         prs_struct qbuf, rbuf;
1827         SPOOL_Q_ENDPAGEPRINTER q;
1828         SPOOL_R_ENDPAGEPRINTER r;
1829         WERROR result = W_ERROR(ERRgeneral);
1830
1831         ZERO_STRUCT(q);
1832         ZERO_STRUCT(r);
1833
1834         /* Initialise parse structures */
1835
1836         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1837         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1838
1839         /* Initialise input parameters */
1840
1841         make_spoolss_q_endpageprinter(&q, hnd);
1842
1843         /* Marshall data and send request */
1844
1845         if (!spoolss_io_q_endpageprinter("", &q, &qbuf, 0) ||
1846             !rpc_api_pipe_req(cli, PI_SPOOLSS, SPOOLSS_ENDPAGEPRINTER, &qbuf, &rbuf))
1847                 goto done;
1848
1849         /* Unmarshall response */
1850
1851         if (!spoolss_io_r_endpageprinter("", &r, &rbuf, 0))
1852                 goto done;
1853
1854         /* Return output parameters */
1855
1856         result = r.status;
1857
1858  done:
1859         prs_mem_free(&qbuf);
1860         prs_mem_free(&rbuf);
1861
1862         return result;
1863 }
1864
1865 /* Startdocprinter.  Sent to notify the spooler that a document is about
1866    to be spooled for printing. */
1867
1868 WERROR cli_spoolss_startdocprinter(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1869                                    POLICY_HND *hnd, char *docname, 
1870                                    char *outputfile, char *datatype, 
1871                                    uint32 *jobid)
1872 {
1873         prs_struct qbuf, rbuf;
1874         SPOOL_Q_STARTDOCPRINTER q;
1875         SPOOL_R_STARTDOCPRINTER r;
1876         WERROR result = W_ERROR(ERRgeneral);
1877         uint32 level = 1;
1878
1879         ZERO_STRUCT(q);
1880         ZERO_STRUCT(r);
1881
1882         /* Initialise parse structures */
1883
1884         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1885         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1886
1887         /* Initialise input parameters */
1888
1889         make_spoolss_q_startdocprinter(&q, hnd, level, docname, outputfile, 
1890                                        datatype);
1891
1892         /* Marshall data and send request */
1893
1894         if (!spoolss_io_q_startdocprinter("", &q, &qbuf, 0) ||
1895             !rpc_api_pipe_req(cli, PI_SPOOLSS, SPOOLSS_STARTDOCPRINTER, &qbuf, &rbuf))
1896                 goto done;
1897
1898         /* Unmarshall response */
1899
1900         if (!spoolss_io_r_startdocprinter("", &r, &rbuf, 0))
1901                 goto done;
1902
1903         /* Return output parameters */
1904
1905         result = r.status;
1906         
1907         if (W_ERROR_IS_OK(result))
1908                 *jobid = r.jobid;
1909
1910  done:
1911         prs_mem_free(&qbuf);
1912         prs_mem_free(&rbuf);
1913
1914         return result;
1915 }
1916
1917 /* Enddocprinter.  Sent to notify the spooler that a document has finished
1918    being spooled. */
1919
1920 WERROR cli_spoolss_enddocprinter(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1921                                   POLICY_HND *hnd)
1922 {
1923         prs_struct qbuf, rbuf;
1924         SPOOL_Q_ENDDOCPRINTER q;
1925         SPOOL_R_ENDDOCPRINTER r;
1926         WERROR result = W_ERROR(ERRgeneral);
1927
1928         ZERO_STRUCT(q);
1929         ZERO_STRUCT(r);
1930
1931         /* Initialise parse structures */
1932
1933         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1934         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1935
1936         /* Initialise input parameters */
1937
1938         make_spoolss_q_enddocprinter(&q, hnd);
1939
1940         /* Marshall data and send request */
1941
1942         if (!spoolss_io_q_enddocprinter("", &q, &qbuf, 0) ||
1943             !rpc_api_pipe_req(cli, PI_SPOOLSS, SPOOLSS_ENDDOCPRINTER, &qbuf, &rbuf))
1944                 goto done;
1945
1946         /* Unmarshall response */
1947
1948         if (!spoolss_io_r_enddocprinter("", &r, &rbuf, 0))
1949                 goto done;
1950
1951         /* Return output parameters */
1952
1953         result = r.status;
1954
1955  done:
1956         prs_mem_free(&qbuf);
1957         prs_mem_free(&rbuf);
1958
1959         return result;
1960 }
1961
1962 /* Get printer data */
1963
1964 WERROR cli_spoolss_getprinterdata(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1965                                   uint32 offered, uint32 *needed,
1966                                   POLICY_HND *hnd, const char *valuename, 
1967                                   REGISTRY_VALUE *value)
1968 {
1969         prs_struct qbuf, rbuf;
1970         SPOOL_Q_GETPRINTERDATA q;
1971         SPOOL_R_GETPRINTERDATA r;
1972         WERROR result = W_ERROR(ERRgeneral);
1973
1974         ZERO_STRUCT(q);
1975         ZERO_STRUCT(r);
1976
1977         /* Initialise parse structures */
1978
1979         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1980         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1981
1982         /* Initialise input parameters */
1983
1984         make_spoolss_q_getprinterdata(&q, hnd, valuename, offered);
1985
1986         /* Marshall data and send request */
1987
1988         if (!spoolss_io_q_getprinterdata("", &q, &qbuf, 0) ||
1989             !rpc_api_pipe_req(cli, PI_SPOOLSS, SPOOLSS_GETPRINTERDATA, &qbuf, &rbuf))
1990                 goto done;
1991
1992         /* Unmarshall response */
1993
1994         if (!spoolss_io_r_getprinterdata("", &r, &rbuf, 0))
1995                 goto done;
1996         
1997         result = r.status;
1998
1999         if (needed)
2000                 *needed = r.needed;
2001
2002         if (!W_ERROR_IS_OK(r.status))
2003                 goto done;      
2004
2005         /* Return output parameters */
2006
2007         value->data_p = TALLOC_MEMDUP(mem_ctx, r.data, r.needed);
2008         value->type = r.type;
2009         value->size = r.size;
2010
2011  done:
2012         prs_mem_free(&qbuf);
2013         prs_mem_free(&rbuf);
2014
2015         return result;
2016 }
2017
2018 WERROR cli_spoolss_getprinterdataex(struct cli_state *cli, TALLOC_CTX *mem_ctx,
2019                                     uint32 offered, uint32 *needed,
2020                                     POLICY_HND *hnd, const char *keyname, 
2021                                     const char *valuename, 
2022                                     REGISTRY_VALUE *value)
2023 {
2024         prs_struct qbuf, rbuf;
2025         SPOOL_Q_GETPRINTERDATAEX q;
2026         SPOOL_R_GETPRINTERDATAEX r;
2027         WERROR result = W_ERROR(ERRgeneral);
2028
2029         ZERO_STRUCT(q);
2030         ZERO_STRUCT(r);
2031
2032         /* Initialise parse structures */
2033
2034         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
2035         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
2036
2037         /* Initialise input parameters */
2038
2039         make_spoolss_q_getprinterdataex(&q, hnd, keyname, valuename, offered);
2040
2041         /* Marshall data and send request */
2042
2043         if (!spoolss_io_q_getprinterdataex("", &q, &qbuf, 0) ||
2044             !rpc_api_pipe_req(cli, PI_SPOOLSS, SPOOLSS_GETPRINTERDATAEX, &qbuf, &rbuf))
2045                 goto done;
2046
2047         /* Unmarshall response */
2048
2049         if (!spoolss_io_r_getprinterdataex("", &r, &rbuf, 0))
2050                 goto done;
2051         
2052         result = r.status;
2053
2054         if (needed)
2055                 *needed = r.needed;
2056
2057         if (!W_ERROR_IS_OK(r.status))
2058                 goto done;      
2059
2060         /* Return output parameters */
2061
2062         value->data_p = TALLOC_MEMDUP(mem_ctx, r.data, r.needed);
2063         value->type = r.type;
2064         value->size = r.needed;
2065
2066  done:
2067         prs_mem_free(&qbuf);
2068         prs_mem_free(&rbuf);
2069
2070         return result;
2071 }
2072
2073 /* Set printer data */
2074
2075 WERROR cli_spoolss_setprinterdata(struct cli_state *cli, TALLOC_CTX *mem_ctx,
2076                                   POLICY_HND *hnd, REGISTRY_VALUE *value)
2077 {
2078         prs_struct qbuf, rbuf;
2079         SPOOL_Q_SETPRINTERDATA q;
2080         SPOOL_R_SETPRINTERDATA r;
2081         WERROR result = W_ERROR(ERRgeneral);
2082
2083         ZERO_STRUCT(q);
2084         ZERO_STRUCT(r);
2085
2086         /* Initialise parse structures */
2087
2088         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
2089         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
2090
2091         /* Initialise input parameters */
2092
2093         make_spoolss_q_setprinterdata(
2094                 &q, hnd, value->valuename, value->type, (char *)value->data_p, value->size);
2095
2096         /* Marshall data and send request */
2097
2098         if (!spoolss_io_q_setprinterdata("", &q, &qbuf, 0) ||
2099             !rpc_api_pipe_req(cli, PI_SPOOLSS, SPOOLSS_SETPRINTERDATA, &qbuf, &rbuf))
2100                 goto done;
2101
2102         /* Unmarshall response */
2103
2104         if (!spoolss_io_r_setprinterdata("", &r, &rbuf, 0))
2105                 goto done;
2106         
2107         result = r.status;
2108
2109         if (!W_ERROR_IS_OK(r.status))
2110                 goto done;      
2111
2112  done:
2113         prs_mem_free(&qbuf);
2114         prs_mem_free(&rbuf);
2115
2116         return result;
2117 }
2118
2119 WERROR cli_spoolss_setprinterdataex(struct cli_state *cli, TALLOC_CTX *mem_ctx,
2120                                     POLICY_HND *hnd, char *keyname, 
2121                                     REGISTRY_VALUE *value)
2122 {
2123         prs_struct qbuf, rbuf;
2124         SPOOL_Q_SETPRINTERDATAEX q;
2125         SPOOL_R_SETPRINTERDATAEX r;
2126         WERROR result = W_ERROR(ERRgeneral);
2127
2128         ZERO_STRUCT(q);
2129         ZERO_STRUCT(r);
2130
2131         /* Initialise parse structures */
2132
2133         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
2134         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
2135
2136         /* Initialise input parameters */
2137
2138         make_spoolss_q_setprinterdataex(
2139                 &q, hnd, keyname, value->valuename, value->type, (char *)value->data_p, 
2140                 value->size);
2141
2142         /* Marshall data and send request */
2143
2144         if (!spoolss_io_q_setprinterdataex("", &q, &qbuf, 0) ||
2145             !rpc_api_pipe_req(cli, PI_SPOOLSS, SPOOLSS_SETPRINTERDATAEX, &qbuf, &rbuf))
2146                 goto done;
2147
2148         /* Unmarshall response */
2149
2150         if (!spoolss_io_r_setprinterdataex("", &r, &rbuf, 0))
2151                 goto done;
2152         
2153         result = r.status;
2154
2155         if (!W_ERROR_IS_OK(r.status))
2156                 goto done;      
2157
2158  done:
2159         prs_mem_free(&qbuf);
2160         prs_mem_free(&rbuf);
2161
2162         return result;
2163 }
2164
2165 /* Enum printer data */
2166
2167 WERROR cli_spoolss_enumprinterdata(struct cli_state *cli, TALLOC_CTX *mem_ctx,
2168                                    POLICY_HND *hnd, uint32 ndx,
2169                                    uint32 value_offered, uint32 data_offered,
2170                                    uint32 *value_needed, uint32 *data_needed,
2171                                    REGISTRY_VALUE *value)
2172 {
2173         prs_struct qbuf, rbuf;
2174         SPOOL_Q_ENUMPRINTERDATA q;
2175         SPOOL_R_ENUMPRINTERDATA r;
2176         WERROR result = W_ERROR(ERRgeneral);
2177
2178         ZERO_STRUCT(q);
2179         ZERO_STRUCT(r);
2180
2181         /* Initialise parse structures */
2182
2183         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
2184         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
2185
2186         /* Initialise input parameters */
2187
2188         make_spoolss_q_enumprinterdata(&q, hnd, ndx, value_offered, data_offered);
2189
2190         /* Marshall data and send request */
2191
2192         if (!spoolss_io_q_enumprinterdata("", &q, &qbuf, 0) ||
2193             !rpc_api_pipe_req(cli, PI_SPOOLSS, SPOOLSS_ENUMPRINTERDATA, &qbuf, &rbuf))
2194                 goto done;
2195
2196         /* Unmarshall response */
2197
2198         if (!spoolss_io_r_enumprinterdata("", &r, &rbuf, 0))
2199                 goto done;
2200         
2201         result = r.status;
2202
2203         if (!W_ERROR_IS_OK(r.status))
2204                 goto done;
2205
2206         /* Return data */
2207         
2208         if (value_needed)
2209                 *value_needed = r.realvaluesize;
2210
2211         if (data_needed)
2212                 *data_needed = r.realdatasize;
2213
2214         if (value) {
2215                 rpcstr_pull(value->valuename, r.value, sizeof(value->valuename), -1,
2216                             STR_TERMINATE);
2217                 value->data_p = TALLOC_MEMDUP(mem_ctx, r.data, r.realdatasize);
2218                 value->type = r.type;
2219                 value->size = r.realdatasize;
2220         }
2221
2222  done:
2223         prs_mem_free(&qbuf);
2224         prs_mem_free(&rbuf);
2225
2226         return result;
2227 }
2228
2229 WERROR cli_spoolss_enumprinterdataex(struct cli_state *cli, TALLOC_CTX *mem_ctx,
2230                                      uint32 offered, uint32 *needed,
2231                                      POLICY_HND *hnd, const char *keyname, 
2232                                      REGVAL_CTR *ctr)
2233 {
2234         prs_struct qbuf, rbuf;
2235         SPOOL_Q_ENUMPRINTERDATAEX q;
2236         SPOOL_R_ENUMPRINTERDATAEX r;
2237         WERROR result = W_ERROR(ERRgeneral);
2238         int i;
2239
2240         ZERO_STRUCT(q);
2241         ZERO_STRUCT(r);
2242
2243         /* Initialise parse structures */
2244
2245         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
2246         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
2247
2248         /* Initialise input parameters */
2249
2250         make_spoolss_q_enumprinterdataex(&q, hnd, keyname, offered);
2251
2252         /* Marshall data and send request */
2253
2254         if (!spoolss_io_q_enumprinterdataex("", &q, &qbuf, 0) ||
2255             !rpc_api_pipe_req(cli, PI_SPOOLSS, SPOOLSS_ENUMPRINTERDATAEX, &qbuf, &rbuf))
2256                 goto done;
2257
2258         /* Unmarshall response */
2259
2260         if (!spoolss_io_r_enumprinterdataex("", &r, &rbuf, 0))
2261                 goto done;
2262         
2263         result = r.status;
2264         
2265         if (needed)
2266                 *needed = r.needed;
2267         
2268         if (!W_ERROR_IS_OK(r.status))
2269                 goto done;
2270
2271         /* Return data */
2272
2273         regval_ctr_init(ctr);
2274
2275         for (i = 0; i < r.returned; i++) {
2276                 PRINTER_ENUM_VALUES *v = &r.ctr.values[i];
2277                 fstring name;
2278
2279                 rpcstr_pull(name, v->valuename.buffer, sizeof(name), -1, 
2280                             STR_TERMINATE);
2281                 regval_ctr_addvalue(ctr, name, v->type, (const char *)v->data, v->data_len);
2282         }
2283
2284  done:
2285         prs_mem_free(&qbuf);
2286         prs_mem_free(&rbuf);
2287
2288         return result;
2289 }
2290
2291 /* Write data to printer */
2292
2293 WERROR cli_spoolss_writeprinter(struct cli_state *cli, TALLOC_CTX *mem_ctx,
2294                                 POLICY_HND *hnd, uint32 data_size, char *data,
2295                                 uint32 *num_written)
2296 {
2297         prs_struct qbuf, rbuf;
2298         SPOOL_Q_WRITEPRINTER q;
2299         SPOOL_R_WRITEPRINTER r;
2300         WERROR result = W_ERROR(ERRgeneral);
2301
2302         ZERO_STRUCT(q);
2303         ZERO_STRUCT(r);
2304
2305         /* Initialise parse structures */
2306
2307         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
2308         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
2309
2310         /* Initialise input parameters */
2311
2312         make_spoolss_q_writeprinter(&q, hnd, data_size, data);
2313
2314         /* Marshall data and send request */
2315
2316         if (!spoolss_io_q_writeprinter("", &q, &qbuf, 0) ||
2317             !rpc_api_pipe_req(cli, PI_SPOOLSS, SPOOLSS_WRITEPRINTER, &qbuf, &rbuf))
2318                 goto done;
2319
2320         /* Unmarshall response */
2321
2322         if (!spoolss_io_r_writeprinter("", &r, &rbuf, 0))
2323                 goto done;
2324         
2325         result = r.status;
2326
2327         if (!W_ERROR_IS_OK(r.status))
2328                 goto done;      
2329
2330         if (num_written)
2331                 *num_written = r.buffer_written;
2332
2333  done:
2334         prs_mem_free(&qbuf);
2335         prs_mem_free(&rbuf);
2336
2337         return result;
2338 }
2339
2340 /* Delete printer data */
2341
2342 WERROR cli_spoolss_deleteprinterdata(struct cli_state *cli, TALLOC_CTX *mem_ctx,
2343                                      POLICY_HND *hnd, char *valuename)
2344 {
2345         prs_struct qbuf, rbuf;
2346         SPOOL_Q_DELETEPRINTERDATA q;
2347         SPOOL_R_DELETEPRINTERDATA r;
2348         WERROR result = W_ERROR(ERRgeneral);
2349
2350         ZERO_STRUCT(q);
2351         ZERO_STRUCT(r);
2352
2353         /* Initialise parse structures */
2354
2355         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
2356         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
2357
2358         /* Initialise input parameters */
2359
2360         make_spoolss_q_deleteprinterdata(&q, hnd, valuename);
2361
2362         /* Marshall data and send request */
2363
2364         if (!spoolss_io_q_deleteprinterdata("", &q, &qbuf, 0) ||
2365             !rpc_api_pipe_req(cli, PI_SPOOLSS, SPOOLSS_DELETEPRINTERDATA, &qbuf, &rbuf))
2366                 goto done;
2367
2368         /* Unmarshall response */
2369
2370         if (!spoolss_io_r_deleteprinterdata("", &r, &rbuf, 0))
2371                 goto done;
2372         
2373         result = r.status;
2374
2375         if (!W_ERROR_IS_OK(r.status))
2376                 goto done;      
2377
2378  done:
2379         prs_mem_free(&qbuf);
2380         prs_mem_free(&rbuf);
2381
2382         return result;
2383 }
2384
2385 WERROR cli_spoolss_deleteprinterdataex(struct cli_state *cli, TALLOC_CTX *mem_ctx,
2386                                        POLICY_HND *hnd, char *keyname, 
2387                                        char *valuename)
2388 {
2389         prs_struct qbuf, rbuf;
2390         SPOOL_Q_DELETEPRINTERDATAEX q;
2391         SPOOL_R_DELETEPRINTERDATAEX r;
2392         WERROR result = W_ERROR(ERRgeneral);
2393
2394         ZERO_STRUCT(q);
2395         ZERO_STRUCT(r);
2396
2397         /* Initialise parse structures */
2398
2399         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
2400         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
2401
2402         /* Initialise input parameters */
2403
2404         make_spoolss_q_deleteprinterdataex(&q, hnd, keyname, valuename);
2405
2406         /* Marshall data and send request */
2407
2408         if (!spoolss_io_q_deleteprinterdataex("", &q, &qbuf, 0) ||
2409             !rpc_api_pipe_req(cli, PI_SPOOLSS, SPOOLSS_DELETEPRINTERDATAEX, &qbuf, &rbuf))
2410                 goto done;
2411
2412         /* Unmarshall response */
2413
2414         if (!spoolss_io_r_deleteprinterdataex("", &r, &rbuf, 0))
2415                 goto done;
2416         
2417         result = r.status;
2418
2419         if (!W_ERROR_IS_OK(r.status))
2420                 goto done;      
2421
2422  done:
2423         prs_mem_free(&qbuf);
2424         prs_mem_free(&rbuf);
2425
2426         return result;
2427 }
2428
2429 WERROR cli_spoolss_enumprinterkey(struct cli_state *cli, TALLOC_CTX *mem_ctx,
2430                                   uint32 offered, uint32 *needed,
2431                                   POLICY_HND *hnd, const char *keyname,
2432                                   uint16 **keylist, uint32 *len)
2433 {
2434         prs_struct qbuf, rbuf;
2435         SPOOL_Q_ENUMPRINTERKEY q;
2436         SPOOL_R_ENUMPRINTERKEY r;
2437         WERROR result = W_ERROR(ERRgeneral);
2438
2439         ZERO_STRUCT(q);
2440         ZERO_STRUCT(r);
2441
2442         /* Initialise parse structures */
2443
2444         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
2445         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
2446
2447         /* Initialise input parameters */
2448
2449         make_spoolss_q_enumprinterkey(&q, hnd, keyname, offered);
2450
2451         /* Marshall data and send request */
2452
2453         if (!spoolss_io_q_enumprinterkey("", &q, &qbuf, 0) ||
2454             !rpc_api_pipe_req(cli, PI_SPOOLSS, SPOOLSS_ENUMPRINTERKEY, &qbuf, &rbuf))
2455                 goto done;
2456
2457         /* Unmarshall response */
2458
2459         if (!spoolss_io_r_enumprinterkey("", &r, &rbuf, 0))
2460                 goto done;
2461         
2462         result = r.status;
2463
2464         if (needed)
2465                 *needed = r.needed;
2466
2467         if (!W_ERROR_IS_OK(r.status))
2468                 goto done;      
2469
2470         /* Copy results */
2471         
2472         if (keylist) {
2473                 *keylist = SMB_MALLOC_ARRAY(uint16, r.keys.buf_len);
2474                 memcpy(*keylist, r.keys.buffer, r.keys.buf_len * 2);
2475                 if (len)
2476                         *len = r.keys.buf_len * 2;
2477         }
2478
2479  done:
2480         prs_mem_free(&qbuf);
2481         prs_mem_free(&rbuf);
2482
2483         return result;  
2484 }
2485
2486 WERROR cli_spoolss_deleteprinterkey(struct cli_state *cli, TALLOC_CTX *mem_ctx,
2487                                     POLICY_HND *hnd, char *keyname)
2488 {
2489         prs_struct qbuf, rbuf;
2490         SPOOL_Q_DELETEPRINTERKEY q;
2491         SPOOL_R_DELETEPRINTERKEY r;
2492         WERROR result = W_ERROR(ERRgeneral);
2493
2494         ZERO_STRUCT(q);
2495         ZERO_STRUCT(r);
2496
2497         /* Initialise parse structures */
2498
2499         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
2500         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
2501
2502         /* Initialise input parameters */
2503
2504         make_spoolss_q_deleteprinterkey(&q, hnd, keyname);
2505
2506         /* Marshall data and send request */
2507
2508         if (!spoolss_io_q_deleteprinterkey("", &q, &qbuf, 0) ||
2509             !rpc_api_pipe_req(cli, PI_SPOOLSS, SPOOLSS_DELETEPRINTERKEY, &qbuf, &rbuf))
2510                 goto done;
2511
2512         /* Unmarshall response */
2513
2514         if (!spoolss_io_r_deleteprinterkey("", &r, &rbuf, 0))
2515                 goto done;
2516         
2517         result = r.status;
2518
2519         if (!W_ERROR_IS_OK(r.status))
2520                 goto done;      
2521
2522  done:
2523         prs_mem_free(&qbuf);
2524         prs_mem_free(&rbuf);
2525
2526         return result;          
2527 }
2528
2529 /** @} **/